xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 1062a1429914c995339f502856821457aa975a5a)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*1062a142SGreg 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\Controller\ChartController;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
22168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
23e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
24f8a18b14SGreg Roachuse Fisharebest\Webtrees\View;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class InteractiveTreeModule
288c2e8227SGreg Roach * Tip : you could change the number of generations loaded before ajax calls both in individual page and in treeview page to optimize speed and server load
298c2e8227SGreg Roach */
30168ff6f3Sric2016class InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface {
318c2e8227SGreg Roach	/** {@inheritdoc} */
328c2e8227SGreg Roach	public function getTitle() {
338c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Interactive tree');
348c2e8227SGreg Roach	}
358c2e8227SGreg Roach
368c2e8227SGreg Roach	/** {@inheritdoc} */
378c2e8227SGreg Roach	public function getDescription() {
388c2e8227SGreg Roach		return /* I18N: Description of the “Interactive tree” module */ I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
398c2e8227SGreg Roach	}
408c2e8227SGreg Roach
418c2e8227SGreg Roach	/** {@inheritdoc} */
428c2e8227SGreg Roach	public function defaultTabOrder() {
438c2e8227SGreg Roach		return 68;
448c2e8227SGreg Roach	}
458c2e8227SGreg Roach
468c2e8227SGreg Roach	/** {@inheritdoc} */
478c2e8227SGreg Roach	public function getTabContent() {
488c2e8227SGreg Roach		global $controller;
498c2e8227SGreg Roach
508c2e8227SGreg Roach		$tv              = new TreeView('tvTab');
518c2e8227SGreg Roach		list($html, $js) = $tv->drawViewport($controller->record, 3);
52cbc1590aSGreg Roach
538c2e8227SGreg Roach		return
548c2e8227SGreg Roach			'<script src="' . $this->js() . '"></script>' .
558c2e8227SGreg Roach			'<script>' . $js . '</script>' .
568c2e8227SGreg Roach			$html;
578c2e8227SGreg Roach	}
588c2e8227SGreg Roach
598c2e8227SGreg Roach	/** {@inheritdoc} */
608c2e8227SGreg Roach	public function hasTabContent() {
6115d603e7SGreg Roach		return true;
628c2e8227SGreg Roach	}
638c2e8227SGreg Roach
648c2e8227SGreg Roach	/** {@inheritdoc} */
658c2e8227SGreg Roach	public function isGrayedOut() {
668c2e8227SGreg Roach		return false;
678c2e8227SGreg Roach	}
688c2e8227SGreg Roach
698c2e8227SGreg Roach	/** {@inheritdoc} */
708c2e8227SGreg Roach	public function canLoadAjax() {
718c2e8227SGreg Roach		return true;
728c2e8227SGreg Roach	}
738c2e8227SGreg Roach
74168ff6f3Sric2016	/**
75168ff6f3Sric2016	 * Return a menu item for this chart.
76168ff6f3Sric2016	 *
7760bc3e3fSGreg Roach	 * @param Individual $individual
7860bc3e3fSGreg Roach	 *
794eb71cfaSGreg Roach	 * @return Menu|null
80168ff6f3Sric2016	 */
81168ff6f3Sric2016	public function getChartMenu(Individual $individual) {
82168ff6f3Sric2016		return new Menu(
83168ff6f3Sric2016			$this->getTitle(),
844eb71cfaSGreg Roach			'module.php?mod=tree&amp;mod_action=treeview&amp;rootid=' . $individual->getXref() . '&amp;ged=' . $individual->getTree()->getNameUrl(),
85168ff6f3Sric2016			'menu-chart-tree',
8613abd6f3SGreg Roach			['rel' => 'nofollow']
87168ff6f3Sric2016		);
88168ff6f3Sric2016	}
89168ff6f3Sric2016
904eb71cfaSGreg Roach	/**
914eb71cfaSGreg Roach	 * Return a menu item for this chart - for use in individual boxes.
924eb71cfaSGreg Roach	 *
9360bc3e3fSGreg Roach	 * @param Individual $individual
9460bc3e3fSGreg Roach	 *
954eb71cfaSGreg Roach	 * @return Menu|null
964eb71cfaSGreg Roach	 */
97168ff6f3Sric2016	public function getBoxChartMenu(Individual $individual) {
98168ff6f3Sric2016		return $this->getChartMenu($individual);
99168ff6f3Sric2016	}
100168ff6f3Sric2016
1018c2e8227SGreg Roach	/** {@inheritdoc} */
1028c2e8227SGreg Roach	public function getPreLoadContent() {
10315d603e7SGreg Roach		// We cannot use $("head").append(<link rel="stylesheet" ...as jQuery is not loaded at this time
1048c2e8227SGreg Roach		return
1058c2e8227SGreg Roach			'<script>
1068c2e8227SGreg Roach			if (document.createStyleSheet) {
1078c2e8227SGreg Roach				document.createStyleSheet("' . $this->css() . '"); // For Internet Explorer
1088c2e8227SGreg Roach			} else {
1098c2e8227SGreg Roach				var newSheet=document.createElement("link");
1108c2e8227SGreg Roach				newSheet.setAttribute("rel","stylesheet");
1118c2e8227SGreg Roach				newSheet.setAttribute("type","text/css");
1128c2e8227SGreg Roach				newSheet.setAttribute("href","' . $this->css() . '");
1138c2e8227SGreg Roach				document.getElementsByTagName("head")[0].appendChild(newSheet);
1148c2e8227SGreg Roach			}
1158c2e8227SGreg Roach			</script>';
1168c2e8227SGreg Roach	}
1178c2e8227SGreg Roach
11876692c8bSGreg Roach	/**
11976692c8bSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
12076692c8bSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
12176692c8bSGreg Roach	 *
12276692c8bSGreg Roach	 * @param string $mod_action
12376692c8bSGreg Roach	 */
1248c2e8227SGreg Roach	public function modAction($mod_action) {
12524ec66ceSGreg Roach		global $controller, $WT_TREE;
12624ec66ceSGreg Roach
1278c2e8227SGreg Roach		switch ($mod_action) {
1288c2e8227SGreg Roach			case 'treeview':
1298c2e8227SGreg Roach				$controller = new ChartController;
1308c2e8227SGreg Roach				$tv         = new TreeView('tv');
1318c2e8227SGreg Roach
1328c2e8227SGreg Roach				$person = $controller->getSignificantIndividual();
1338c2e8227SGreg Roach
1348c2e8227SGreg Roach				list($html, $js) = $tv->drawViewport($person, 4);
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach				$controller
1378c2e8227SGreg Roach					->setPageTitle(I18N::translate('Interactive tree of %s', $person->getFullName()))
1388c2e8227SGreg Roach					->pageHeader()
1398c2e8227SGreg Roach					->addExternalJavascript($this->js())
1408c2e8227SGreg Roach					->addInlineJavascript($js)
1418c2e8227SGreg Roach					->addInlineJavascript('
1428c2e8227SGreg Roach					if (document.createStyleSheet) {
1438c2e8227SGreg Roach						document.createStyleSheet("' . $this->css() . '"); // For Internet Explorer
1448c2e8227SGreg Roach					} else {
14515d603e7SGreg Roach						$("head").append(\'<link rel="stylesheet" type="text/css" href="' . $this->css() . '">\');
1468c2e8227SGreg Roach					}
1478c2e8227SGreg Roach				');
148f8a18b14SGreg Roach
149f8a18b14SGreg Roach				echo View::make('interactive-tree-page', [
150f8a18b14SGreg Roach					'title'      => $controller->getPageTitle(),
151f8a18b14SGreg Roach					'individual' => $controller->root,
152f8a18b14SGreg Roach					'html'       => $html,
153f8a18b14SGreg Roach					//'css'        => $this->css(),
154f8a18b14SGreg Roach					//'js'         => $this->js(),
155f8a18b14SGreg Roach				]);
156f8a18b14SGreg Roach
1578c2e8227SGreg Roach				break;
1588c2e8227SGreg Roach
1598c2e8227SGreg Roach			case 'getDetails':
1608c2e8227SGreg Roach				header('Content-Type: text/html; charset=UTF-8');
1618c2e8227SGreg Roach				$pid        = Filter::get('pid', WT_REGEX_XREF);
1628c2e8227SGreg Roach				$i          = Filter::get('instance');
1638c2e8227SGreg Roach				$tv         = new TreeView($i);
16424ec66ceSGreg Roach				$individual = Individual::getInstance($pid, $WT_TREE);
1658c2e8227SGreg Roach				if ($individual) {
1668c2e8227SGreg Roach					echo $tv->getDetails($individual);
1678c2e8227SGreg Roach				}
1688c2e8227SGreg Roach				break;
1698c2e8227SGreg Roach
1708c2e8227SGreg Roach			case 'getPersons':
1718c2e8227SGreg Roach				header('Content-Type: text/html; charset=UTF-8');
1728c2e8227SGreg Roach				$q  = Filter::get('q');
1738c2e8227SGreg Roach				$i  = Filter::get('instance');
1748c2e8227SGreg Roach				$tv = new TreeView($i);
1758c2e8227SGreg Roach				echo $tv->getPersons($q);
1768c2e8227SGreg Roach				break;
1778c2e8227SGreg Roach
1788c2e8227SGreg Roach			default:
1798c2e8227SGreg Roach				http_response_code(404);
1808c2e8227SGreg Roach				break;
1818c2e8227SGreg Roach		}
1828c2e8227SGreg Roach	}
1838c2e8227SGreg Roach
1848c2e8227SGreg Roach	/**
18576692c8bSGreg Roach	 * URL for our style sheet.
1863cf92ae2SGreg Roach	 *
1878c2e8227SGreg Roach	 * @return string
1888c2e8227SGreg Roach	 */
1898c2e8227SGreg Roach	public function css() {
19005168efaSGreg Roach		return WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
1918c2e8227SGreg Roach	}
1928c2e8227SGreg Roach
1938c2e8227SGreg Roach	/**
19476692c8bSGreg Roach	 * URL for our JavaScript.
19576692c8bSGreg Roach	 *
1968c2e8227SGreg Roach	 * @return string
1978c2e8227SGreg Roach	 */
1988c2e8227SGreg Roach	public function js() {
19905168efaSGreg Roach		return WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
2008c2e8227SGreg Roach	}
2018c2e8227SGreg Roach}
202