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