xref: /webtrees/app/Module/ChartsBlockModule.php (revision f1afa930065e1881d2c7258685f166ed0223ed23)
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\Filter;
20use Fisharebest\Webtrees\Http\Controllers\PedigreeChartController;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
24use Fisharebest\Webtrees\Tree;
25
26/**
27 * Class ChartsBlockModule
28 */
29class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface {
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module/block */
33			I18N::translate('Charts');
34	}
35
36	/** {@inheritdoc} */
37	public function getDescription() {
38		return /* I18N: Description of the “Charts” module */
39			I18N::translate('An alternative way to display charts.');
40	}
41
42	/**
43	 * Generate the HTML content of this block.
44	 *
45	 * @param Tree     $tree
46	 * @param int      $block_id
47	 * @param bool     $template
48	 * @param string[] $cfg
49	 *
50	 * @return string
51	 */
52	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
53		global $ctype;
54
55		$PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
56		$gedcomid         = $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		extract($cfg, EXTR_OVERWRITE);
62
63		$person = Individual::getInstance($pid, $tree);
64		if (!$person) {
65			$pid = $PEDIGREE_ROOT_ID;
66			$this->setBlockSetting($block_id, 'pid', $pid);
67			$person = Individual::getInstance($pid, $tree);
68		}
69
70		$title = $this->getTitle();
71
72		if ($person) {
73			$content = '';
74			switch ($type) {
75				case 'pedigree':
76					$title     = I18N::translate('Pedigree of %s', $person->getFullName());
77					$chart_url = route('pedigree-chart', [
78						'xref'        => $person->getXref(),
79						'ged'         => $person->getTree()->getName(),
80						'generations' => 3,
81						'layout'      => PedigreeChartController::PORTRAIT,
82					]);
83					$content = view('blocks/chart', [
84						'block_id'  => $block_id,
85						'chart_url' => $chart_url,
86					]);
87					break;
88				case 'descendants':
89					$title           = I18N::translate('Descendants of %s', $person->getFullName());
90					$chart_url = route('descendants-chart', [
91						'xref'        => $person->getXref(),
92						'ged'         => $person->getTree()->getName(),
93						'generations' => 2,
94						'chart_style' => 0,
95					]);
96					$content = view('blocks/chart', [
97						'block_id'  => $block_id,
98						'chart_url' => $chart_url,
99					]);
100					break;
101				case 'hourglass':
102					$title           = I18N::translate('Hourglass chart of %s', $person->getFullName());
103					$chart_url = route('hourglass-chart', [
104						'xref'        => $person->getXref(),
105						'ged'         => $person->getTree()->getName(),
106						'generations' => 2,
107						'layout'      => PedigreeChartController::PORTRAIT,
108					]);
109					$content = view('blocks/chart', [
110						'block_id'  => $block_id,
111						'chart_url' => $chart_url,
112					]);
113					break;
114				case 'treenav':
115					$title   = I18N::translate('Interactive tree of %s', $person->getFullName());
116					$mod     = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
117					$tv      = new TreeView;
118					$content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
119					$content .= '<script src="' . $mod->js() . '"></script>';
120					list($html, $js) = $tv->drawViewport($person, 2);
121					$content .= $html . '<script>' . $js . '</script>';
122					break;
123			}
124		} else {
125			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
126		}
127
128		if ($template) {
129			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
130				$config_url = route('tree-page-block-edit', [
131					'block_id' => $block_id,
132					'ged'      => $tree->getName(),
133				]);
134			} elseif ($ctype === 'user' && Auth::check()) {
135				$config_url = route('user-page-block-edit', [
136					'block_id' => $block_id,
137					'ged'      => $tree->getName(),
138				]);
139			} else {
140				$config_url = '';
141			}
142
143			return view('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 Tree $tree
174	 * @param int  $block_id
175	 *
176	 * @return void
177	 */
178	public function configureBlock(Tree $tree, int $block_id) {
179		$PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
180		$gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
181
182		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
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			return;
187		}
188
189		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
190		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
191
192		$charts = [
193			'pedigree'    => I18N::translate('Pedigree'),
194			'descendants' => I18N::translate('Descendants'),
195			'hourglass'   => I18N::translate('Hourglass chart'),
196			'treenav'     => I18N::translate('Interactive tree'),
197		];
198		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
199
200		$individual = Individual::getInstance($pid, $tree);
201
202		echo view('blocks/charts-config', [
203			'charts'     => $charts,
204			'individual' => $individual,
205			'tree'       => $tree,
206			'type'       => $type,
207		]);
208	}
209}
210