xref: /webtrees/app/Module/ChartsBlockModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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;
19*15d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Controller\HourglassController;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
22*15d603e7SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class ChartsBlockModule
328c2e8227SGreg Roach */
33e2a378d3SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface {
348c2e8227SGreg Roach	/** {@inheritdoc} */
358c2e8227SGreg Roach	public function getTitle() {
368c2e8227SGreg Roach		return /* I18N: Name of a module/block */ I18N::translate('Charts');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
398c2e8227SGreg Roach	/** {@inheritdoc} */
408c2e8227SGreg Roach	public function getDescription() {
418c2e8227SGreg Roach		return /* I18N: Description of the “Charts” module */ I18N::translate('An alternative way to display charts.');
428c2e8227SGreg Roach	}
438c2e8227SGreg Roach
4476692c8bSGreg Roach	/**
4576692c8bSGreg Roach	 * Generate the HTML content of this block.
4676692c8bSGreg Roach	 *
4776692c8bSGreg Roach	 * @param int      $block_id
4876692c8bSGreg Roach	 * @param bool     $template
49727f238cSGreg Roach	 * @param string[] $cfg
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @return string
5276692c8bSGreg Roach	 */
5313abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
548c2e8227SGreg Roach		global $WT_TREE, $ctype, $controller;
558c2e8227SGreg Roach
568c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
574b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
588c2e8227SGreg Roach
59e2a378d3SGreg Roach		$type    = $this->getBlockSetting($block_id, 'type', 'pedigree');
60e2a378d3SGreg Roach		$pid     = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
618c2e8227SGreg Roach
62*15d603e7SGreg Roach		foreach (['type', 'pid'] as $name) {
638c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
648c2e8227SGreg Roach				$$name = $cfg[$name];
658c2e8227SGreg Roach			}
668c2e8227SGreg Roach		}
678c2e8227SGreg Roach
6824ec66ceSGreg Roach		$person = Individual::getInstance($pid, $WT_TREE);
698c2e8227SGreg Roach		if (!$person) {
708c2e8227SGreg Roach			$pid = $PEDIGREE_ROOT_ID;
71e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'pid', $pid);
7224ec66ceSGreg Roach			$person = Individual::getInstance($pid, $WT_TREE);
738c2e8227SGreg Roach		}
748c2e8227SGreg Roach
758c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
768c2e8227SGreg Roach		$class = $this->getName() . '_block';
774b9ff166SGreg Roach		if ($ctype == 'gedcom' && Auth::isManager($WT_TREE) || $ctype == 'user' && Auth::check()) {
78*15d603e7SGreg Roach			$title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' ';
798c2e8227SGreg Roach		} else {
808c2e8227SGreg Roach			$title = '';
818c2e8227SGreg Roach		}
828c2e8227SGreg Roach
838c2e8227SGreg Roach		if ($person) {
84f679d3acSGreg Roach			$content = '';
858c2e8227SGreg Roach			switch ($type) {
868c2e8227SGreg Roach			case 'pedigree':
878c2e8227SGreg Roach				$title .= I18N::translate('Pedigree of %s', $person->getFullName());
88*15d603e7SGreg Roach				$chartController = new HourglassController($person->getXref());
898c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
90f679d3acSGreg Roach				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
91a86dd8b1SGreg Roach				$content .= '<td>';
928c2e8227SGreg Roach				ob_start();
93*15d603e7SGreg Roach				FunctionsPrint::printPedigreePerson($person);
948c2e8227SGreg Roach				$content .= ob_get_clean();
958c2e8227SGreg Roach				$content .= '</td>';
96a86dd8b1SGreg Roach				$content .= '<td>';
978c2e8227SGreg Roach				ob_start();
988c2e8227SGreg Roach				$chartController->printPersonPedigree($person, 1);
998c2e8227SGreg Roach				$content .= ob_get_clean();
1008c2e8227SGreg Roach				$content .= '</td>';
101f679d3acSGreg Roach				$content .= '</tr></table>';
1028c2e8227SGreg Roach				break;
1038c2e8227SGreg Roach			case 'descendants':
1048c2e8227SGreg Roach				$title .= I18N::translate('Descendants of %s', $person->getFullName());
105*15d603e7SGreg Roach				$chartController = new HourglassController($person->getXref());
1068c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
1078c2e8227SGreg Roach				ob_start();
1088c2e8227SGreg Roach				$chartController->printDescendency($person, 1, false);
1098c2e8227SGreg Roach				$content .= ob_get_clean();
1108c2e8227SGreg Roach				break;
1118c2e8227SGreg Roach			case 'hourglass':
1128c2e8227SGreg Roach				$title .= I18N::translate('Hourglass chart of %s', $person->getFullName());
113*15d603e7SGreg Roach				$chartController = new HourglassController($person->getXref());
1148c2e8227SGreg Roach				$controller->addInlineJavascript($chartController->setupJavascript());
115f679d3acSGreg Roach				$content .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
116a86dd8b1SGreg Roach				$content .= '<td>';
1178c2e8227SGreg Roach				ob_start();
1188c2e8227SGreg Roach				$chartController->printDescendency($person, 1, false);
1198c2e8227SGreg Roach				$content .= ob_get_clean();
1208c2e8227SGreg Roach				$content .= '</td>';
121a86dd8b1SGreg Roach				$content .= '<td>';
1228c2e8227SGreg Roach				ob_start();
1238c2e8227SGreg Roach				$chartController->printPersonPedigree($person, 1);
1248c2e8227SGreg Roach				$content .= ob_get_clean();
1258c2e8227SGreg Roach				$content .= '</td>';
126f679d3acSGreg Roach				$content .= '</tr></table>';
1278c2e8227SGreg Roach				break;
1288c2e8227SGreg Roach			case 'treenav':
1298c2e8227SGreg Roach				$title .= I18N::translate('Interactive tree of %s', $person->getFullName());
1308c2e8227SGreg Roach				$mod = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
1318c2e8227SGreg Roach				$tv  = new TreeView;
132564ae2d7SGreg Roach				$content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
1338c2e8227SGreg Roach				$content .= '<script src="' . $mod->js() . '"></script>';
1348c2e8227SGreg Roach				list($html, $js) = $tv->drawViewport($person, 2);
1358c2e8227SGreg Roach				$content .= $html . '<script>' . $js . '</script>';
1368c2e8227SGreg Roach				break;
1378c2e8227SGreg Roach			}
1388c2e8227SGreg Roach		} else {
1396e7bf391SGreg Roach			$content = I18N::translate('You must select an individual and a chart type in the block preferences');
1408c2e8227SGreg Roach		}
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach		if ($template) {
1438c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1448c2e8227SGreg Roach		} else {
1458c2e8227SGreg Roach			return $content;
1468c2e8227SGreg Roach		}
1478c2e8227SGreg Roach	}
1488c2e8227SGreg Roach
1498c2e8227SGreg Roach	/** {@inheritdoc} */
1508c2e8227SGreg Roach	public function loadAjax() {
1518c2e8227SGreg Roach		return true;
1528c2e8227SGreg Roach	}
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach	/** {@inheritdoc} */
1558c2e8227SGreg Roach	public function isUserBlock() {
1568c2e8227SGreg Roach		return true;
1578c2e8227SGreg Roach	}
1588c2e8227SGreg Roach
1598c2e8227SGreg Roach	/** {@inheritdoc} */
1608c2e8227SGreg Roach	public function isGedcomBlock() {
1618c2e8227SGreg Roach		return true;
1628c2e8227SGreg Roach	}
1638c2e8227SGreg Roach
16476692c8bSGreg Roach	/**
16576692c8bSGreg Roach	 * An HTML form to edit block settings
16676692c8bSGreg Roach	 *
16776692c8bSGreg Roach	 * @param int $block_id
16876692c8bSGreg Roach	 */
1698c2e8227SGreg Roach	public function configureBlock($block_id) {
1708c2e8227SGreg Roach		global $WT_TREE, $controller;
1718c2e8227SGreg Roach
1728c2e8227SGreg Roach		$PEDIGREE_ROOT_ID = $WT_TREE->getPreference('PEDIGREE_ROOT_ID');
1734b9ff166SGreg Roach		$gedcomid         = $WT_TREE->getUserPreference(Auth::user(), 'gedcomid');
1748c2e8227SGreg Roach
1758c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
176e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree'));
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF));
1788c2e8227SGreg Roach		}
1798c2e8227SGreg Roach
180e2a378d3SGreg Roach		$type    = $this->getBlockSetting($block_id, 'type', 'pedigree');
181e2a378d3SGreg Roach		$pid     = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ? $gedcomid : $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
1828c2e8227SGreg Roach
18313abd6f3SGreg Roach		$charts = [
184fddfbb14SGreg Roach			'pedigree'    => I18N::translate('Pedigree'),
185fddfbb14SGreg Roach			'descendants' => I18N::translate('Descendants'),
186fddfbb14SGreg Roach			'hourglass'   => I18N::translate('Hourglass chart'),
187fddfbb14SGreg Roach			'treenav'     => I18N::translate('Interactive tree'),
18813abd6f3SGreg Roach		];
189fddfbb14SGreg Roach		uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
190fddfbb14SGreg Roach
1918c2e8227SGreg Roach		?>
192*15d603e7SGreg Roach		<div class="form-group row">
193*15d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="type">
194564ae2d7SGreg Roach				<?= I18N::translate('Chart type') ?>
195*15d603e7SGreg Roach			</label>
196*15d603e7SGreg Roach			<div class="col-sm-9">
197*15d603e7SGreg Roach				<?= Bootstrap4::select($charts, $type, ['id' => 'type', 'name' => 'type']) ?>
198*15d603e7SGreg Roach			</div>
199*15d603e7SGreg Roach		</div>
200*15d603e7SGreg Roach		<div class="form-group row">
201*15d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="pid">
202054bfc98SGreg Roach				<label for="pid">
203564ae2d7SGreg Roach					<?= I18N::translate('Individual') ?>
204054bfc98SGreg Roach				</label>
205*15d603e7SGreg Roach			</label>
206*15d603e7SGreg Roach			<div class="col-sm-9">
207*15d603e7SGreg Roach				<?= FunctionsEdit::formControlIndividual(Individual::getInstance($pid, $WT_TREE), ['id' => 'pid', 'name' => 'pid']) ?>
208*15d603e7SGreg Roach			</div>
209*15d603e7SGreg Roach		</div>
2108c2e8227SGreg Roach		<?php
2118c2e8227SGreg Roach	}
2128c2e8227SGreg Roach}
213