xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 0bc54ba3948a3d4d792d1f1ef4251483902552ce)
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
18*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
19*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
22168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
23e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
24e2ae4578SGreg Roachuse Fisharebest\Webtrees\Tree;
25e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Request;
26e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class InteractiveTreeModule
308c2e8227SGreg 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
318c2e8227SGreg Roach */
32168ff6f3Sric2016class InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface {
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getTitle() {
35e2ae4578SGreg Roach		return /* I18N: Name of a module */
36e2ae4578SGreg Roach			I18N::translate('Interactive tree');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
398c2e8227SGreg Roach	/** {@inheritdoc} */
408c2e8227SGreg Roach	public function getDescription() {
41e2ae4578SGreg Roach		return /* I18N: Description of the “Interactive tree” module */
42e2ae4578SGreg Roach			I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
438c2e8227SGreg Roach	}
448c2e8227SGreg Roach
458c2e8227SGreg Roach	/** {@inheritdoc} */
468c2e8227SGreg Roach	public function defaultTabOrder() {
478c2e8227SGreg Roach		return 68;
488c2e8227SGreg Roach	}
498c2e8227SGreg Roach
508c2e8227SGreg Roach	/** {@inheritdoc} */
51225e381fSGreg Roach	public function getTabContent(Individual $individual) {
52225e381fSGreg Roach		$treeview = new TreeView('tvTab');
53225e381fSGreg Roach		list($html, $js) = $treeview->drawViewport($individual, 3);
548c2e8227SGreg Roach
55a8cd57e1SGreg Roach		return view('modules/tree/tab', [
56225e381fSGreg Roach			'html'         => $html,
57225e381fSGreg Roach			'js'           => $js,
58d5691647SGreg Roach			'treeview_css' => $this->css(),
59d5691647SGreg Roach			'treeview_js'  => $this->js(),
60225e381fSGreg Roach		]);
618c2e8227SGreg Roach	}
628c2e8227SGreg Roach
63d5691647SGreg Roach	/**
64d5691647SGreg Roach	 * @return string
65d5691647SGreg Roach	 */
66d5691647SGreg Roach	public function css(): string {
67d5691647SGreg Roach		return WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
68d5691647SGreg Roach	}
69d5691647SGreg Roach
70d5691647SGreg Roach	/**
71d5691647SGreg Roach	 * @return string
72d5691647SGreg Roach	 */
73d5691647SGreg Roach	public function js(): string {
74d5691647SGreg Roach		return WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
75d5691647SGreg Roach	}
76d5691647SGreg Roach
778c2e8227SGreg Roach	/** {@inheritdoc} */
78225e381fSGreg Roach	public function hasTabContent(Individual $individual) {
7915d603e7SGreg Roach		return true;
808c2e8227SGreg Roach	}
818c2e8227SGreg Roach
828c2e8227SGreg Roach	/** {@inheritdoc} */
83225e381fSGreg Roach	public function isGrayedOut(Individual $individual) {
848c2e8227SGreg Roach		return false;
858c2e8227SGreg Roach	}
868c2e8227SGreg Roach
878c2e8227SGreg Roach	/** {@inheritdoc} */
888c2e8227SGreg Roach	public function canLoadAjax() {
898c2e8227SGreg Roach		return true;
908c2e8227SGreg Roach	}
918c2e8227SGreg Roach
92168ff6f3Sric2016	/**
93168ff6f3Sric2016	 * Return a menu item for this chart.
94168ff6f3Sric2016	 *
9560bc3e3fSGreg Roach	 * @param Individual $individual
9660bc3e3fSGreg Roach	 *
974eb71cfaSGreg Roach	 * @return Menu|null
98168ff6f3Sric2016	 */
99168ff6f3Sric2016	public function getChartMenu(Individual $individual) {
100168ff6f3Sric2016		return new Menu(
101168ff6f3Sric2016			$this->getTitle(),
102e2ae4578SGreg Roach			e(route('module', ['module' => $this->getName(), 'action' => 'Treeview', 'xref' => $individual->getXref(), 'ged' => $individual->getTree()->getName()])),
103168ff6f3Sric2016			'menu-chart-tree',
10413abd6f3SGreg Roach			['rel' => 'nofollow']
105168ff6f3Sric2016		);
106168ff6f3Sric2016	}
107168ff6f3Sric2016
1084eb71cfaSGreg Roach	/**
1094eb71cfaSGreg Roach	 * Return a menu item for this chart - for use in individual boxes.
1104eb71cfaSGreg Roach	 *
11160bc3e3fSGreg Roach	 * @param Individual $individual
11260bc3e3fSGreg Roach	 *
1134eb71cfaSGreg Roach	 * @return Menu|null
1144eb71cfaSGreg Roach	 */
115168ff6f3Sric2016	public function getBoxChartMenu(Individual $individual) {
116168ff6f3Sric2016		return $this->getChartMenu($individual);
117168ff6f3Sric2016	}
118168ff6f3Sric2016
11976692c8bSGreg Roach	/**
120e2ae4578SGreg Roach	 * @param Request $request
12176692c8bSGreg Roach	 *
122e2ae4578SGreg Roach	 * @return Response
12376692c8bSGreg Roach	 */
124e2ae4578SGreg Roach	public function getTreeviewAction(Request $request): Response {
125e2ae4578SGreg Roach		/** @var Tree $tree */
126e2ae4578SGreg Roach		$tree = $request->attributes->get('tree');
12724ec66ceSGreg Roach
128e2ae4578SGreg Roach		$xref = $request->get('xref');
129e2ae4578SGreg Roach
130e2ae4578SGreg Roach		$individual = Individual::getInstance($xref, $tree);
1318c2e8227SGreg Roach		$tv         = new TreeView('tv');
1328c2e8227SGreg Roach
133e2ae4578SGreg Roach		list($html, $js) = $tv->drawViewport($individual, 4);
1348c2e8227SGreg Roach
135e2ae4578SGreg Roach		$title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1368c2e8227SGreg Roach
137e2ae4578SGreg Roach		return $this->viewResponse('interactive-tree-page', [
138e2ae4578SGreg Roach			'title'      => $title,
1398840c547SGreg Roach			'individual' => $individual,
140e2ae4578SGreg Roach			'js'         => $js,
141f8a18b14SGreg Roach			'html'       => $html,
142e2ae4578SGreg Roach			'tree'       => $tree,
143f8a18b14SGreg Roach		]);
1448c2e8227SGreg Roach	}
1458c2e8227SGreg Roach
146e2ae4578SGreg Roach	/**
147e2ae4578SGreg Roach	 * @param Request $request
148e2ae4578SGreg Roach	 *
149e2ae4578SGreg Roach	 * @return Response
150e2ae4578SGreg Roach	 */
151e2ae4578SGreg Roach	public function getDetailsAction(Request $request): Response {
152e2ae4578SGreg Roach		/** @var Tree $tree */
153e2ae4578SGreg Roach		$tree = $request->attributes->get('tree');
1548c2e8227SGreg Roach
155e2ae4578SGreg Roach		$pid        = $request->get('pid', WT_REGEX_XREF);
156e2ae4578SGreg Roach		$individual = Individual::getInstance($pid, $tree);
157e2ae4578SGreg Roach
158*0bc54ba3SGreg Roach		if ($individual === null) {
159*0bc54ba3SGreg Roach			throw new IndividualNotFoundException;
160*0bc54ba3SGreg Roach		}
161*0bc54ba3SGreg Roach
162*0bc54ba3SGreg Roach		if (!$individual->canShow()) {
163*0bc54ba3SGreg Roach			throw new IndividualAccessDeniedException;
164*0bc54ba3SGreg Roach		}
165*0bc54ba3SGreg Roach
166e2ae4578SGreg Roach		$instance = $request->get('instance');
167e2ae4578SGreg Roach		$treeview = new TreeView($instance);
168e2ae4578SGreg Roach
169e2ae4578SGreg Roach		return new Response($treeview->getDetails($individual));
1708c2e8227SGreg Roach	}
1718c2e8227SGreg Roach
1728c2e8227SGreg Roach	/**
173e2ae4578SGreg Roach	 * @param Request $request
1743cf92ae2SGreg Roach	 *
175e2ae4578SGreg Roach	 * @return Response
1768c2e8227SGreg Roach	 */
177e2ae4578SGreg Roach	public function getPersonsAction(Request $request): Response {
178bc8b8f24SGreg Roach		/** @var Tree $tree */
179bc8b8f24SGreg Roach		$tree = $request->attributes->get('tree');
180bc8b8f24SGreg Roach
181e2ae4578SGreg Roach		$q  = $request->get('q');
182e2ae4578SGreg Roach		$instance = $request->get('instance');
183e2ae4578SGreg Roach		$treeview = new TreeView($instance);
1848c2e8227SGreg Roach
185bc8b8f24SGreg Roach		return new Response($treeview->getPersons($tree, $q));
1868c2e8227SGreg Roach	}
1878c2e8227SGreg Roach}
188