xref: /webtrees/app/Module/ChartsBlockModule.php (revision 054bfc98f4da5c9e0ac8c7f8822979a91cbbbe76)
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 = array()) {
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 (array('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 = '<table cellspacing="0" cellpadding="0" border="0"><tr>';
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 .= '<td>';
90				ob_start();
91				FunctionsPrint::printPedigreePerson($person, $details);
92				$content .= ob_get_clean();
93				$content .= '</td>';
94				$content .= '<td>';
95				ob_start();
96				$chartController->printPersonPedigree($person, 1);
97				$content .= ob_get_clean();
98				$content .= '</td>';
99				break;
100			case 'descendants':
101				$title .= I18N::translate('Descendants of %s', $person->getFullName());
102				$chartController = new HourglassController($person->getXref(), $details, false);
103				$controller->addInlineJavascript($chartController->setupJavascript());
104				$content .= '<td>';
105				ob_start();
106				$chartController->printDescendency($person, 1, false);
107				$content .= ob_get_clean();
108				$content .= '</td>';
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 .= '<td>';
115				ob_start();
116				$chartController->printDescendency($person, 1, false);
117				$content .= ob_get_clean();
118				$content .= '</td>';
119				$content .= '<td>';
120				ob_start();
121				$chartController->printPersonPedigree($person, 1);
122				$content .= ob_get_clean();
123				$content .= '</td>';
124				break;
125			case 'treenav':
126				$title .= I18N::translate('Interactive tree of %s', $person->getFullName());
127				$mod = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
128				$tv  = new TreeView;
129				$content .= '<td>';
130				$content .= '<script>jQuery("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
131				$content .= '<script src="' . $mod->js() . '"></script>';
132				list($html, $js) = $tv->drawViewport($person, 2);
133				$content .= $html . '<script>' . $js . '</script>';
134				$content .= '</td>';
135				break;
136			}
137			$content .= '</tr></table>';
138		} else {
139			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
140		}
141
142		if ($template) {
143			return Theme::theme()->formatBlock($id, $title, $class, $content);
144		} else {
145			return $content;
146		}
147	}
148
149	/** {@inheritdoc} */
150	public function loadAjax() {
151		return true;
152	}
153
154	/** {@inheritdoc} */
155	public function isUserBlock() {
156		return true;
157	}
158
159	/** {@inheritdoc} */
160	public function isGedcomBlock() {
161		return true;
162	}
163
164	/**
165	 * An HTML form to edit block settings
166	 *
167	 * @param int $block_id
168	 */
169	public function configureBlock($block_id) {
170		global $WT_TREE, $controller;
171
172		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
173		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
174
175		if (Filter::postBool('save') && Filter::checkCsrf()) {
176			$this->setBlockSetting($block_id, 'details', Filter::postBool('details'));
177			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
178			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
179		}
180
181		$details = $this->getBlockSetting($block_id, 'details', '0');
182		$type    = $this->getBlockSetting($block_id, 'type', 'pedigree');
183		$pid     = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
184
185		$charts = array(
186			'pedigree'    => I18N::translate('Pedigree'),
187			'descendants' => I18N::translate('Descendants'),
188			'hourglass'   => I18N::translate('Hourglass chart'),
189			'treenav'     => I18N::translate('Interactive tree'),
190		);
191		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
192
193		$controller
194			->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
195			->addInlineJavascript('autocomplete();');
196	?>
197		<tr>
198			<td class="descriptionbox wrap width33">
199				<?php echo I18N::translate('Chart type'); ?>
200			</td>
201			<td class="optionbox">
202				<?php echo FunctionsEdit::selectEditControl('type', $charts, null, $type); ?>
203			</td>
204		</tr>
205		<tr>
206			<td class="descriptionbox wrap width33">
207				<?php echo I18N::translate('Show details'); ?>
208			</td>
209		<td class="optionbox">
210			<?php echo FunctionsEdit::editFieldYesNo('details', $details); ?>
211			</td>
212		</tr>
213		<tr>
214			<td class="descriptionbox wrap width33">
215				<label for="pid">
216					<?php echo I18N::translate('Individual'); ?>
217				</label>
218			</td>
219			<td class="optionbox">
220				<input data-autocomplete-type="INDI" type="text" name="pid" id="pid" value="<?php echo $pid; ?>" size="5">
221				<?php
222				echo FunctionsPrint::printFindIndividualLink('pid');
223				$root = Individual::getInstance($pid, $WT_TREE);
224				if ($root) {
225					echo ' <span class="list_item">', $root->getFullName(), $root->formatFirstMajorFact(WT_EVENTS_BIRT, 1), '</span>';
226				}
227				?>
228			</td>
229		</tr>
230		<?php
231	}
232}
233