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