xref: /webtrees/app/Module/ChartsBlockModule.php (revision 564ae2d7821e40d23a34164b35b213506f3b0344)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Controller\HourglassController;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\Functions\FunctionsPrint;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
26use Fisharebest\Webtrees\Theme;
27
28/**
29 * Class ChartsBlockModule
30 */
31class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface {
32	/** {@inheritdoc} */
33	public function getTitle() {
34		return /* I18N: Name of a module/block */ I18N::translate('Charts');
35	}
36
37	/** {@inheritdoc} */
38	public function getDescription() {
39		return /* I18N: Description of the “Charts” module */ I18N::translate('An alternative way to display charts.');
40	}
41
42	/**
43	 * Generate the HTML content of this block.
44	 *
45	 * @param int      $block_id
46	 * @param bool     $template
47	 * @param string[] $cfg
48	 *
49	 * @return string
50	 */
51	public function getBlock($block_id, $template = true, $cfg = []) {
52		global $WT_TREE, $ctype, $controller;
53
54		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
55		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
56
57		$details = $this->getBlockSetting($block_id, 'details', '0');
58		$type    = $this->getBlockSetting($block_id, 'type', 'pedigree');
59		$pid     = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
60
61		foreach (['details', 'type', 'pid', 'block'] as $name) {
62			if (array_key_exists($name, $cfg)) {
63				$$name = $cfg[$name];
64			}
65		}
66
67		$person = Individual::getInstance($pid, $WT_TREE);
68		if (!$person) {
69			$pid = $PEDIGREE_ROOT_ID;
70			$this->setBlockSetting($block_id, 'pid', $pid);
71			$person = Individual::getInstance($pid, $WT_TREE);
72		}
73
74		$id    = $this->getName() . $block_id;
75		$class = $this->getName() . '_block';
76		if ($ctype == 'gedcom' && Auth::isManager($WT_TREE) || $ctype == 'user' && Auth::check()) {
77			$title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
78		} else {
79			$title = '';
80		}
81
82		if ($person) {
83			$content = '';
84			switch ($type) {
85			case 'pedigree':
86				$title .= I18N::translate('Pedigree of %s', $person->getFullName());
87				$chartController = new HourglassController($person->getXref(), $details, false);
88				$controller->addInlineJavascript($chartController->setupJavascript());
89				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
90				$content .= '<td>';
91				ob_start();
92				FunctionsPrint::printPedigreePerson($person, $details);
93				$content .= ob_get_clean();
94				$content .= '</td>';
95				$content .= '<td>';
96				ob_start();
97				$chartController->printPersonPedigree($person, 1);
98				$content .= ob_get_clean();
99				$content .= '</td>';
100				$content .= '</tr></table>';
101				break;
102			case 'descendants':
103				$title .= I18N::translate('Descendants of %s', $person->getFullName());
104				$chartController = new HourglassController($person->getXref(), $details, false);
105				$controller->addInlineJavascript($chartController->setupJavascript());
106				ob_start();
107				$chartController->printDescendency($person, 1, false);
108				$content .= ob_get_clean();
109				break;
110			case 'hourglass':
111				$title .= I18N::translate('Hourglass chart of %s', $person->getFullName());
112				$chartController = new HourglassController($person->getXref(), $details, false);
113				$controller->addInlineJavascript($chartController->setupJavascript());
114				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
115				$content .= '<td>';
116				ob_start();
117				$chartController->printDescendency($person, 1, false);
118				$content .= ob_get_clean();
119				$content .= '</td>';
120				$content .= '<td>';
121				ob_start();
122				$chartController->printPersonPedigree($person, 1);
123				$content .= ob_get_clean();
124				$content .= '</td>';
125				$content .= '</tr></table>';
126				break;
127			case 'treenav':
128				$title .= I18N::translate('Interactive tree of %s', $person->getFullName());
129				$mod = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
130				$tv  = new TreeView;
131				$content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
132				$content .= '<script src="' . $mod->js() . '"></script>';
133				list($html, $js) = $tv->drawViewport($person, 2);
134				$content .= $html . '<script>' . $js . '</script>';
135				break;
136			}
137		} else {
138			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
139		}
140
141		if ($template) {
142			return Theme::theme()->formatBlock($id, $title, $class, $content);
143		} else {
144			return $content;
145		}
146	}
147
148	/** {@inheritdoc} */
149	public function loadAjax() {
150		return true;
151	}
152
153	/** {@inheritdoc} */
154	public function isUserBlock() {
155		return true;
156	}
157
158	/** {@inheritdoc} */
159	public function isGedcomBlock() {
160		return true;
161	}
162
163	/**
164	 * An HTML form to edit block settings
165	 *
166	 * @param int $block_id
167	 */
168	public function configureBlock($block_id) {
169		global $WT_TREE, $controller;
170
171		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
172		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
173
174		if (Filter::postBool('save') && Filter::checkCsrf()) {
175			$this->setBlockSetting($block_id, 'details', Filter::postBool('details'));
176			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
177			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
178		}
179
180		$details = $this->getBlockSetting($block_id, 'details', '0');
181		$type    = $this->getBlockSetting($block_id, 'type', 'pedigree');
182		$pid     = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
183
184		$charts = [
185			'pedigree'    => I18N::translate('Pedigree'),
186			'descendants' => I18N::translate('Descendants'),
187			'hourglass'   => I18N::translate('Hourglass chart'),
188			'treenav'     => I18N::translate('Interactive tree'),
189		];
190		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
191
192		$controller
193			->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
194			->addInlineJavascript('autocomplete();');
195	?>
196		<tr>
197			<td class="descriptionbox wrap width33">
198				<?= I18N::translate('Chart type') ?>
199			</td>
200			<td class="optionbox">
201				<?php echo FunctionsEdit::selectEditControl('type', $charts, null, $type); ?>
202			</td>
203		</tr>
204		<tr>
205			<td class="descriptionbox wrap width33">
206				<?= I18N::translate('Show details') ?>
207			</td>
208		<td class="optionbox">
209			<?php echo FunctionsEdit::editFieldYesNo('details', $details); ?>
210			</td>
211		</tr>
212		<tr>
213			<td class="descriptionbox wrap width33">
214				<label for="pid">
215					<?= I18N::translate('Individual') ?>
216				</label>
217			</td>
218			<td class="optionbox">
219				<input data-autocomplete-type="INDI" type="text" name="pid" id="pid" value="<?= $pid ?>" size="5">
220				<?php
221				echo FunctionsPrint::printFindIndividualLink('pid');
222				$root = Individual::getInstance($pid, $WT_TREE);
223				if ($root) {
224					echo ' <span class="list_item">', $root->getFullName(), $root->formatFirstMajorFact(WT_EVENTS_BIRT, 1), '</span>';
225				}
226				?>
227			</td>
228		</tr>
229		<?php
230	}
231}
232