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