xref: /webtrees/app/Module/ChartsBlockModule.php (revision 397e599a3131684b8a335ebfddcfe63828b9f51b)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Controller\HourglassController;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
249c6524dcSGreg Roachuse Fisharebest\Webtrees\Html;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class ChartsBlockModule
318c2e8227SGreg Roach */
32e2a378d3SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface {
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getTitle() {
358c2e8227SGreg Roach		return /* I18N: Name of a module/block */ I18N::translate('Charts');
368c2e8227SGreg Roach	}
378c2e8227SGreg Roach
388c2e8227SGreg Roach	/** {@inheritdoc} */
398c2e8227SGreg Roach	public function getDescription() {
408c2e8227SGreg Roach		return /* I18N: Description of the “Charts” module */ I18N::translate('An alternative way to display charts.');
418c2e8227SGreg Roach	}
428c2e8227SGreg Roach
4376692c8bSGreg Roach	/**
4476692c8bSGreg Roach	 * Generate the HTML content of this block.
4576692c8bSGreg Roach	 *
4676692c8bSGreg Roach	 * @param int      $block_id
4776692c8bSGreg Roach	 * @param bool     $template
48727f238cSGreg Roach	 * @param string[] $cfg
4976692c8bSGreg Roach	 *
5076692c8bSGreg Roach	 * @return string
5176692c8bSGreg Roach	 */
52a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
538c2e8227SGreg Roach		global $WT_TREE, $ctype, $controller;
548c2e8227SGreg Roach
558c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
564b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
578c2e8227SGreg Roach
58e2a378d3SGreg Roach		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
59e2a378d3SGreg Roach		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
608c2e8227SGreg Roach
6115d603e7SGreg Roach		foreach (['type', 'pid'] as $name) {
628c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
638c2e8227SGreg Roach				$$name = $cfg[$name];
648c2e8227SGreg Roach			}
658c2e8227SGreg Roach		}
668c2e8227SGreg Roach
6724ec66ceSGreg Roach		$person = Individual::getInstance($pid, $WT_TREE);
688c2e8227SGreg Roach		if (!$person) {
698c2e8227SGreg Roach			$pid = $PEDIGREE_ROOT_ID;
70e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'pid', $pid);
7124ec66ceSGreg Roach			$person = Individual::getInstance($pid, $WT_TREE);
728c2e8227SGreg Roach		}
738c2e8227SGreg Roach
749c6524dcSGreg Roach		$title = $this->getTitle();
758c2e8227SGreg Roach
768c2e8227SGreg Roach		if ($person) {
77f679d3acSGreg Roach			$content = '';
788c2e8227SGreg Roach			switch ($type) {
798c2e8227SGreg Roach				case 'pedigree':
809c6524dcSGreg Roach					$title           = I18N::translate('Pedigree of %s', $person->getFullName());
8115d603e7SGreg Roach					$chartController = new HourglassController($person->getXref());
82f679d3acSGreg Roach					$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
8369fe1563Smakitso					$content .= '<td class="myCharts">';
848c2e8227SGreg Roach					ob_start();
8515d603e7SGreg Roach					FunctionsPrint::printPedigreePerson($person);
868c2e8227SGreg Roach					$content .= ob_get_clean();
878c2e8227SGreg Roach					$content .= '</td>';
88a86dd8b1SGreg Roach					$content .= '<td>';
898c2e8227SGreg Roach					ob_start();
908c2e8227SGreg Roach					$chartController->printPersonPedigree($person, 1);
918c2e8227SGreg Roach					$content .= ob_get_clean();
928c2e8227SGreg Roach					$content .= '</td>';
93f679d3acSGreg Roach					$content .= '</tr></table>';
9402b6a1ebSGreg Roach					$content .= '<script>' . $chartController->setupJavascript() . '</script>';
958c2e8227SGreg Roach					break;
968c2e8227SGreg Roach				case 'descendants':
979c6524dcSGreg Roach					$title           = I18N::translate('Descendants of %s', $person->getFullName());
9815d603e7SGreg Roach					$chartController = new HourglassController($person->getXref());
998c2e8227SGreg Roach					ob_start();
1008c2e8227SGreg Roach					$chartController->printDescendency($person, 1, false);
1018c2e8227SGreg Roach					$content .= ob_get_clean();
10202b6a1ebSGreg Roach					$content .= '<script>' . $chartController->setupJavascript() . '</script>';
1038c2e8227SGreg Roach					break;
1048c2e8227SGreg Roach				case 'hourglass':
1059c6524dcSGreg Roach					$title           = I18N::translate('Hourglass chart of %s', $person->getFullName());
10615d603e7SGreg Roach					$chartController = new HourglassController($person->getXref());
107f679d3acSGreg Roach					$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
108a86dd8b1SGreg Roach					$content .= '<td>';
1098c2e8227SGreg Roach					ob_start();
1108c2e8227SGreg Roach					$chartController->printDescendency($person, 1, false);
1118c2e8227SGreg Roach					$content .= ob_get_clean();
1128c2e8227SGreg Roach					$content .= '</td>';
113a86dd8b1SGreg Roach					$content .= '<td>';
1148c2e8227SGreg Roach					ob_start();
1158c2e8227SGreg Roach					$chartController->printPersonPedigree($person, 1);
1168c2e8227SGreg Roach					$content .= ob_get_clean();
1178c2e8227SGreg Roach					$content .= '</td>';
118f679d3acSGreg Roach					$content .= '</tr></table>';
11902b6a1ebSGreg Roach					$content .= '<script>' . $chartController->setupJavascript() . '</script>';
1208c2e8227SGreg Roach					break;
1218c2e8227SGreg Roach				case 'treenav':
1229c6524dcSGreg Roach					$title = I18N::translate('Interactive tree of %s', $person->getFullName());
1238c2e8227SGreg Roach					$mod   = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
1248c2e8227SGreg Roach					$tv    = new TreeView;
125564ae2d7SGreg Roach					$content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
1268c2e8227SGreg Roach					$content .= '<script src="' . $mod->js() . '"></script>';
1278c2e8227SGreg Roach					list($html, $js) = $tv->drawViewport($person, 2);
1288c2e8227SGreg Roach					$content .= $html . '<script>' . $js . '</script>';
1298c2e8227SGreg Roach					break;
1308c2e8227SGreg Roach			}
1318c2e8227SGreg Roach		} else {
1326e7bf391SGreg Roach			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
1338c2e8227SGreg Roach		}
1348c2e8227SGreg Roach
1358c2e8227SGreg Roach		if ($template) {
136*397e599aSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
137*397e599aSGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
138*397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
139*397e599aSGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1409c6524dcSGreg Roach			} else {
1419c6524dcSGreg Roach				$config_url = '';
1429c6524dcSGreg Roach			}
1439c6524dcSGreg Roach
144225e381fSGreg Roach			return view('blocks/template', [
1459c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1469c6524dcSGreg Roach				'id'         => $block_id,
1479c6524dcSGreg Roach				'config_url' => $config_url,
148ea46934eSGreg Roach				'title'      => strip_tags($title),
1499c6524dcSGreg Roach				'content'    => $content,
1509c6524dcSGreg Roach			]);
1518c2e8227SGreg Roach		} else {
1528c2e8227SGreg Roach			return $content;
1538c2e8227SGreg Roach		}
1548c2e8227SGreg Roach	}
1558c2e8227SGreg Roach
1568c2e8227SGreg Roach	/** {@inheritdoc} */
157a9430be8SGreg Roach	public function loadAjax(): bool {
1588c2e8227SGreg Roach		return true;
1598c2e8227SGreg Roach	}
1608c2e8227SGreg Roach
1618c2e8227SGreg Roach	/** {@inheritdoc} */
162a9430be8SGreg Roach	public function isUserBlock(): bool {
1638c2e8227SGreg Roach		return true;
1648c2e8227SGreg Roach	}
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach	/** {@inheritdoc} */
167a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1688c2e8227SGreg Roach		return true;
1698c2e8227SGreg Roach	}
1708c2e8227SGreg Roach
17176692c8bSGreg Roach	/**
17276692c8bSGreg Roach	 * An HTML form to edit block settings
17376692c8bSGreg Roach	 *
17476692c8bSGreg Roach	 * @param int $block_id
175a9430be8SGreg Roach	 *
176988d5e9cSGreg Roach	 * @return void
17776692c8bSGreg Roach	 */
178be9a728cSGreg Roach	public function configureBlock($block_id) {
1798c2e8227SGreg Roach		global $WT_TREE, $controller;
1808c2e8227SGreg Roach
1818c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
1824b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
1838c2e8227SGreg Roach
1848c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
185e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
186e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
1878c2e8227SGreg Roach		}
1888c2e8227SGreg Roach
189e2a378d3SGreg Roach		$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
190e2a378d3SGreg Roach		$pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
1918c2e8227SGreg Roach
19213abd6f3SGreg Roach		$charts = [
193fddfbb14SGreg Roach			'pedigree'    => I18N::translate('Pedigree'),
194fddfbb14SGreg Roach			'descendants' => I18N::translate('Descendants'),
195fddfbb14SGreg Roach			'hourglass'   => I18N::translate('Hourglass chart'),
196fddfbb14SGreg Roach			'treenav'     => I18N::translate('Interactive tree'),
19713abd6f3SGreg Roach		];
198fddfbb14SGreg Roach		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
199fddfbb14SGreg Roach
2008c2e8227SGreg Roach		?>
20115d603e7SGreg Roach		<div class="form-group row">
20215d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="type">
203564ae2d7SGreg Roach				<?= I18N::translate('Chart type') ?>
20415d603e7SGreg Roach			</label>
20515d603e7SGreg Roach			<div class="col-sm-9">
20615d603e7SGreg Roach				<?= Bootstrap4::select($charts, $type, ['id' => 'type', 'name' => 'type']) ?>
20715d603e7SGreg Roach			</div>
20815d603e7SGreg Roach		</div>
20915d603e7SGreg Roach		<div class="form-group row">
21015d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="pid">
211054bfc98SGreg Roach				<label for="pid">
212564ae2d7SGreg Roach					<?= I18N::translate('Individual') ?>
213054bfc98SGreg Roach				</label>
21415d603e7SGreg Roach			</label>
21515d603e7SGreg Roach			<div class="col-sm-9">
21615d603e7SGreg Roach				<?= FunctionsEdit::formControlIndividual(Individual::getInstance($pid, $WT_TREE), ['id' => 'pid', 'name' => 'pid']) ?>
21715d603e7SGreg Roach			</div>
21815d603e7SGreg Roach		</div>
2198c2e8227SGreg Roach		<?php
2208c2e8227SGreg Roach	}
2218c2e8227SGreg Roach}
222