xref: /webtrees/app/Module/ChartsBlockModule.php (revision cfc69e1537c1c5cc06248a704654eb9e64de47c5)
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\Controller\HourglassController;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Http\Controllers\PedigreeChartController;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
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 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 = []): string {
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		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
58		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
59
60		extract($cfg, EXTR_OVERWRITE);
61
62		$person = Individual::getInstance($pid, $WT_TREE);
63		if (!$person) {
64			$pid = $PEDIGREE_ROOT_ID;
65			$this->setBlockSetting($block_id, 'pid', $pid);
66			$person = Individual::getInstance($pid, $WT_TREE);
67		}
68
69		$title = $this->getTitle();
70
71		if ($person) {
72			$content = '';
73			switch ($type) {
74				case 'pedigree':
75					$title     = I18N::translate('Pedigree of %s', $person->getFullName());
76					$chart_url = route('pedigree-chart', [
77						'xref'        => $person->getXref(),
78						'ged'         => $person->getTree()->getName(),
79						'generations' => 3,
80						'layout'      => PedigreeChartController::PORTRAIT,
81					]);
82					$content = view('blocks/chart', [
83						'block_id'  => $block_id,
84						'chart_url' => $chart_url,
85					]);
86					break;
87				case 'descendants':
88					$title           = I18N::translate('Descendants of %s', $person->getFullName());
89					$chart_url = route('descendants-chart', [
90						'xref'        => $person->getXref(),
91						'ged'         => $person->getTree()->getName(),
92						'generations' => 2,
93						'chart_style' => 0,
94					]);
95					$content = view('blocks/chart', [
96						'block_id'  => $block_id,
97						'chart_url' => $chart_url,
98					]);
99					break;
100				case 'hourglass':
101					$title           = I18N::translate('Hourglass chart of %s', $person->getFullName());
102					$chart_url = route('hourglass-chart', [
103						'xref'        => $person->getXref(),
104						'ged'         => $person->getTree()->getName(),
105						'generations' => 2,
106						'layout'      => PedigreeChartController::PORTRAIT,
107					]);
108					$content = view('blocks/chart', [
109						'block_id'  => $block_id,
110						'chart_url' => $chart_url,
111					]);
112					break;
113				case 'treenav':
114					$title   = I18N::translate('Interactive tree of %s', $person->getFullName());
115					$mod     = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
116					$tv      = new TreeView;
117					$content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
118					$content .= '<script src="' . $mod->js() . '"></script>';
119					list($html, $js) = $tv->drawViewport($person, 2);
120					$content .= $html . '<script>' . $js . '</script>';
121					break;
122			}
123		} else {
124			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
125		}
126
127		if ($template) {
128			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
129				$config_url = route('tree-page-block-edit', [
130					'block_id' => $block_id,
131					'ged'      => $WT_TREE->getName(),
132				]);
133			} elseif ($ctype === 'user' && Auth::check()) {
134				$config_url = route('user-page-block-edit', [
135					'block_id' => $block_id,
136					'ged'      => $WT_TREE->getName(),
137				]);
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;
178
179		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
180		$gedcomid         = $WT_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, $WT_TREE);
201
202		echo view('blocks/charts-config', [
203			'charts'     => $charts,
204			'individual' => $individual,
205			'tree'       => $WT_TREE,
206			'type'       => $type,
207		]);
208	}
209}
210