xref: /webtrees/app/Module/InteractiveTreeModule.php (revision bba27599b02e0b6f90f62348bb4fa32bd74056d0)
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\I18N;
19use Fisharebest\Webtrees\Individual;
20use Fisharebest\Webtrees\Menu;
21use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
22use Fisharebest\Webtrees\Tree;
23use Symfony\Component\HttpFoundation\Request;
24use Symfony\Component\HttpFoundation\Response;
25use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
27/**
28 * Class InteractiveTreeModule
29 * 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
30 */
31class InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface {
32	/** {@inheritdoc} */
33	public function getTitle() {
34		return /* I18N: Name of a module */
35			I18N::translate('Interactive tree');
36	}
37
38	/** {@inheritdoc} */
39	public function getDescription() {
40		return /* I18N: Description of the “Interactive tree” module */
41			I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
42	}
43
44	/** {@inheritdoc} */
45	public function defaultTabOrder() {
46		return 68;
47	}
48
49	/** {@inheritdoc} */
50	public function getTabContent(Individual $individual) {
51		$treeview = new TreeView('tvTab');
52		list($html, $js) = $treeview->drawViewport($individual, 3);
53
54		return view('tabs/treeview', [
55			'html'         => $html,
56			'js'           => $js,
57			'treeview_css' => WT_MODULES_DIR . $this->getName() . '/css/treeview.css',
58			'treeview_js'  => WT_MODULES_DIR . $this->getName() . '/js/treeview.js',
59		]);
60	}
61
62	/** {@inheritdoc} */
63	public function hasTabContent(Individual $individual) {
64		return true;
65	}
66
67	/** {@inheritdoc} */
68	public function isGrayedOut(Individual $individual) {
69		return false;
70	}
71
72	/** {@inheritdoc} */
73	public function canLoadAjax() {
74		return true;
75	}
76
77	/**
78	 * Return a menu item for this chart.
79	 *
80	 * @param Individual $individual
81	 *
82	 * @return Menu|null
83	 */
84	public function getChartMenu(Individual $individual) {
85		return new Menu(
86			$this->getTitle(),
87			e(route('module', ['module' => $this->getName(), 'action' => 'Treeview', 'xref' => $individual->getXref(), 'ged' => $individual->getTree()->getName()])),
88			'menu-chart-tree',
89			['rel' => 'nofollow']
90		);
91	}
92
93	/**
94	 * Return a menu item for this chart - for use in individual boxes.
95	 *
96	 * @param Individual $individual
97	 *
98	 * @return Menu|null
99	 */
100	public function getBoxChartMenu(Individual $individual) {
101		return $this->getChartMenu($individual);
102	}
103
104	/**
105	 * @param Request $request
106	 *
107	 * @return Response
108	 */
109	public function getTreeviewAction(Request $request): Response {
110		/** @var Tree $tree */
111		$tree = $request->attributes->get('tree');
112
113		$xref = $request->get('xref');
114
115		$individual = Individual::getInstance($xref, $tree);
116		$tv         = new TreeView('tv');
117
118		list($html, $js) = $tv->drawViewport($individual, 4);
119
120		$title = I18N::translate('Interactive tree of %s', $individual->getFullName());
121
122		return $this->viewResponse('interactive-tree-page', [
123			'title'      => $title,
124			'individual' => $individual,
125			'js'         => $js,
126			'html'       => $html,
127			'tree'       => $tree,
128		]);
129	}
130
131	/**
132	 * @param Request $request
133	 *
134	 * @return Response
135	 */
136	public function getDetailsAction(Request $request): Response {
137		/** @var Tree $tree */
138		$tree = $request->attributes->get('tree');
139
140		$pid        = $request->get('pid', WT_REGEX_XREF);
141		$individual = Individual::getInstance($pid, $tree);
142
143		if ($individual && $individual->canShow()) {
144			$instance = $request->get('instance');
145			$treeview = new TreeView($instance);
146
147			return new Response($treeview->getDetails($individual));
148		} else {
149			throw new NotFoundHttpException;
150		}
151	}
152
153	/**
154	 * @param Request $request
155	 *
156	 * @return Response
157	 */
158	public function getPersonsAction(Request $request): Response {
159		$q  = $request->get('q');
160		$instance = $request->get('instance');
161		$treeview = new TreeView($instance);
162
163		return new Response($treeview->getPersons($q));
164	}
165}
166