xref: /webtrees/app/Module/ChartsBlockModule.php (revision c6e83c54c82a4223bbce242d10a4522ff2b7fcbc)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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\Bootstrap4;
20use Fisharebest\Webtrees\Controller\HourglassController;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\Functions\FunctionsPrint;
24use Fisharebest\Webtrees\Html;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
28use Fisharebest\Webtrees\View;
29
30/**
31 * Class ChartsBlockModule
32 */
33class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface {
34	/** {@inheritdoc} */
35	public function getTitle() {
36		return /* I18N: Name of a module/block */ I18N::translate('Charts');
37	}
38
39	/** {@inheritdoc} */
40	public function getDescription() {
41		return /* I18N: Description of the “Charts” module */ I18N::translate('An alternative way to display charts.');
42	}
43
44	/**
45	 * Generate the HTML content of this block.
46	 *
47	 * @param int      $block_id
48	 * @param bool     $template
49	 * @param string[] $cfg
50	 *
51	 * @return string
52	 */
53	public function getBlock($block_id, $template = true, $cfg = []): string {
54		global $WT_TREE, $ctype, $controller;
55
56		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
57		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
58
59		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
60		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
61
62		foreach (['type', 'pid'] as $name) {
63			if (array_key_exists($name, $cfg)) {
64				$$name = $cfg[$name];
65			}
66		}
67
68		$person = Individual::getInstance($pid, $WT_TREE);
69		if (!$person) {
70			$pid = $PEDIGREE_ROOT_ID;
71			$this->setBlockSetting($block_id, 'pid', $pid);
72			$person = Individual::getInstance($pid, $WT_TREE);
73		}
74
75		$title = $this->getTitle();
76
77		if ($person) {
78			$content = '';
79			switch ($type) {
80				case 'pedigree':
81					$title           = I18N::translate('Pedigree of %s', $person->getFullName());
82					$chartController = new HourglassController($person->getXref());
83					$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
84					$content .= '<td class="myCharts">';
85					ob_start();
86					FunctionsPrint::printPedigreePerson($person);
87					$content .= ob_get_clean();
88					$content .= '</td>';
89					$content .= '<td>';
90					ob_start();
91					$chartController->printPersonPedigree($person, 1);
92					$content .= ob_get_clean();
93					$content .= '</td>';
94					$content .= '</tr></table>';
95					$content .= '<script>' . $chartController->setupJavascript() . '</script>';
96					break;
97				case 'descendants':
98					$title           = I18N::translate('Descendants of %s', $person->getFullName());
99					$chartController = new HourglassController($person->getXref());
100					ob_start();
101					$chartController->printDescendency($person, 1, false);
102					$content .= ob_get_clean();
103					$content .= '<script>' . $chartController->setupJavascript() . '</script>';
104					break;
105				case 'hourglass':
106					$title           = I18N::translate('Hourglass chart of %s', $person->getFullName());
107					$chartController = new HourglassController($person->getXref());
108					$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
109					$content .= '<td>';
110					ob_start();
111					$chartController->printDescendency($person, 1, false);
112					$content .= ob_get_clean();
113					$content .= '</td>';
114					$content .= '<td>';
115					ob_start();
116					$chartController->printPersonPedigree($person, 1);
117					$content .= ob_get_clean();
118					$content .= '</td>';
119					$content .= '</tr></table>';
120					$content .= '<script>' . $chartController->setupJavascript() . '</script>';
121					break;
122				case 'treenav':
123					$title = I18N::translate('Interactive tree of %s', $person->getFullName());
124					$mod   = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
125					$tv    = new TreeView;
126					$content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
127					$content .= '<script src="' . $mod->js() . '"></script>';
128					list($html, $js) = $tv->drawViewport($person, 2);
129					$content .= $html . '<script>' . $js . '</script>';
130					break;
131			}
132		} else {
133			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
134		}
135
136		if ($template) {
137			if ($ctype == 'gedcom' && Auth::isManager($WT_TREE) || $ctype == 'user' && Auth::check()) {
138				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
139			} else {
140				$config_url = '';
141			}
142
143			return View::make('blocks/template', [
144				'block'      => str_replace('_', '-', $this->getName()),
145				'id'         => $block_id,
146				'config_url' => $config_url,
147				'title'      => strip_tags($title),
148				'content'    => $content,
149			]);
150		} else {
151			return $content;
152		}
153	}
154
155	/** {@inheritdoc} */
156	public function loadAjax(): bool {
157		return true;
158	}
159
160	/** {@inheritdoc} */
161	public function isUserBlock(): bool {
162		return true;
163	}
164
165	/** {@inheritdoc} */
166	public function isGedcomBlock(): bool {
167		return true;
168	}
169
170	/**
171	 * An HTML form to edit block settings
172	 *
173	 * @param int $block_id
174	 *
175	 * @return void
176	 */
177	public function configureBlock($block_id) {
178		global $WT_TREE, $controller;
179
180		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
181		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
182
183		if (Filter::postBool('save') && Filter::checkCsrf()) {
184			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
185			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
186		}
187
188		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
189		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
190
191		$charts = [
192			'pedigree'    => I18N::translate('Pedigree'),
193			'descendants' => I18N::translate('Descendants'),
194			'hourglass'   => I18N::translate('Hourglass chart'),
195			'treenav'     => I18N::translate('Interactive tree'),
196		];
197		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
198
199		?>
200		<div class="form-group row">
201			<label class="col-sm-3 col-form-label" for="type">
202				<?= I18N::translate('Chart type') ?>
203			</label>
204			<div class="col-sm-9">
205				<?= Bootstrap4::select($charts, $type, ['id' => 'type', 'name' => 'type']) ?>
206			</div>
207		</div>
208		<div class="form-group row">
209			<label class="col-sm-3 col-form-label" for="pid">
210				<label for="pid">
211					<?= I18N::translate('Individual') ?>
212				</label>
213			</label>
214			<div class="col-sm-9">
215				<?= FunctionsEdit::formControlIndividual(Individual::getInstance($pid, $WT_TREE), ['id' => 'pid', 'name' => 'pid']) ?>
216			</div>
217		</div>
218		<?php
219	}
220}
221