xref: /webtrees/app/Module/ChartsBlockModule.php (revision 397e599a3131684b8a335ebfddcfe63828b9f51b)
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)) {
137				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
138			} elseif ($ctype === 'user' && Auth::check()) {
139				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
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 int $block_id
175	 *
176	 * @return void
177	 */
178	public function configureBlock($block_id) {
179		global $WT_TREE, $controller;
180
181		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
182		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
183
184		if (Filter::postBool('save') && Filter::checkCsrf()) {
185			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
186			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
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		?>
201		<div class="form-group row">
202			<label class="col-sm-3 col-form-label" for="type">
203				<?= I18N::translate('Chart type') ?>
204			</label>
205			<div class="col-sm-9">
206				<?= Bootstrap4::select($charts, $type, ['id' => 'type', 'name' => 'type']) ?>
207			</div>
208		</div>
209		<div class="form-group row">
210			<label class="col-sm-3 col-form-label" for="pid">
211				<label for="pid">
212					<?= I18N::translate('Individual') ?>
213				</label>
214			</label>
215			<div class="col-sm-9">
216				<?= FunctionsEdit::formControlIndividual(Individual::getInstance($pid, $WT_TREE), ['id' => 'pid', 'name' => 'pid']) ?>
217			</div>
218		</div>
219		<?php
220	}
221}
222