xref: /webtrees/app/Module/InteractiveTreeModule.php (revision bc8b8f243b1266586ff44b1bbdd6d4ac158baded)
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\I18N;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
20168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
21e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
22e2ae4578SGreg Roachuse Fisharebest\Webtrees\Tree;
23e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Request;
24e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
25e2ae4578SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class InteractiveTreeModule
298c2e8227SGreg 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
308c2e8227SGreg Roach */
31168ff6f3Sric2016class InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface {
328c2e8227SGreg Roach	/** {@inheritdoc} */
338c2e8227SGreg Roach	public function getTitle() {
34e2ae4578SGreg Roach		return /* I18N: Name of a module */
35e2ae4578SGreg Roach			I18N::translate('Interactive tree');
368c2e8227SGreg Roach	}
378c2e8227SGreg Roach
388c2e8227SGreg Roach	/** {@inheritdoc} */
398c2e8227SGreg Roach	public function getDescription() {
40e2ae4578SGreg Roach		return /* I18N: Description of the “Interactive tree” module */
41e2ae4578SGreg Roach			I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
428c2e8227SGreg Roach	}
438c2e8227SGreg Roach
448c2e8227SGreg Roach	/** {@inheritdoc} */
458c2e8227SGreg Roach	public function defaultTabOrder() {
468c2e8227SGreg Roach		return 68;
478c2e8227SGreg Roach	}
488c2e8227SGreg Roach
498c2e8227SGreg Roach	/** {@inheritdoc} */
50225e381fSGreg Roach	public function getTabContent(Individual $individual) {
51225e381fSGreg Roach		$treeview = new TreeView('tvTab');
52225e381fSGreg Roach		list($html, $js) = $treeview->drawViewport($individual, 3);
538c2e8227SGreg Roach
54a8cd57e1SGreg Roach		return view('modules/tree/tab', [
55225e381fSGreg Roach			'html'         => $html,
56225e381fSGreg Roach			'js'           => $js,
57d5691647SGreg Roach			'treeview_css' => $this->css(),
58d5691647SGreg Roach			'treeview_js'  => $this->js(),
59225e381fSGreg Roach		]);
608c2e8227SGreg Roach	}
618c2e8227SGreg Roach
62d5691647SGreg Roach	/**
63d5691647SGreg Roach	 * @return string
64d5691647SGreg Roach	 */
65d5691647SGreg Roach	public function css(): string {
66d5691647SGreg Roach		return WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
67d5691647SGreg Roach	}
68d5691647SGreg Roach
69d5691647SGreg Roach	/**
70d5691647SGreg Roach	 * @return string
71d5691647SGreg Roach	 */
72d5691647SGreg Roach	public function js(): string {
73d5691647SGreg Roach		return WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
74d5691647SGreg Roach	}
75d5691647SGreg Roach
768c2e8227SGreg Roach	/** {@inheritdoc} */
77225e381fSGreg Roach	public function hasTabContent(Individual $individual) {
7815d603e7SGreg Roach		return true;
798c2e8227SGreg Roach	}
808c2e8227SGreg Roach
818c2e8227SGreg Roach	/** {@inheritdoc} */
82225e381fSGreg Roach	public function isGrayedOut(Individual $individual) {
838c2e8227SGreg Roach		return false;
848c2e8227SGreg Roach	}
858c2e8227SGreg Roach
868c2e8227SGreg Roach	/** {@inheritdoc} */
878c2e8227SGreg Roach	public function canLoadAjax() {
888c2e8227SGreg Roach		return true;
898c2e8227SGreg Roach	}
908c2e8227SGreg Roach
91168ff6f3Sric2016	/**
92168ff6f3Sric2016	 * Return a menu item for this chart.
93168ff6f3Sric2016	 *
9460bc3e3fSGreg Roach	 * @param Individual $individual
9560bc3e3fSGreg Roach	 *
964eb71cfaSGreg Roach	 * @return Menu|null
97168ff6f3Sric2016	 */
98168ff6f3Sric2016	public function getChartMenu(Individual $individual) {
99168ff6f3Sric2016		return new Menu(
100168ff6f3Sric2016			$this->getTitle(),
101e2ae4578SGreg Roach			e(route('module', ['module' => $this->getName(), 'action' => 'Treeview', 'xref' => $individual->getXref(), 'ged' => $individual->getTree()->getName()])),
102168ff6f3Sric2016			'menu-chart-tree',
10313abd6f3SGreg Roach			['rel' => 'nofollow']
104168ff6f3Sric2016		);
105168ff6f3Sric2016	}
106168ff6f3Sric2016
1074eb71cfaSGreg Roach	/**
1084eb71cfaSGreg Roach	 * Return a menu item for this chart - for use in individual boxes.
1094eb71cfaSGreg Roach	 *
11060bc3e3fSGreg Roach	 * @param Individual $individual
11160bc3e3fSGreg Roach	 *
1124eb71cfaSGreg Roach	 * @return Menu|null
1134eb71cfaSGreg Roach	 */
114168ff6f3Sric2016	public function getBoxChartMenu(Individual $individual) {
115168ff6f3Sric2016		return $this->getChartMenu($individual);
116168ff6f3Sric2016	}
117168ff6f3Sric2016
11876692c8bSGreg Roach	/**
119e2ae4578SGreg Roach	 * @param Request $request
12076692c8bSGreg Roach	 *
121e2ae4578SGreg Roach	 * @return Response
12276692c8bSGreg Roach	 */
123e2ae4578SGreg Roach	public function getTreeviewAction(Request $request): Response {
124e2ae4578SGreg Roach		/** @var Tree $tree */
125e2ae4578SGreg Roach		$tree = $request->attributes->get('tree');
12624ec66ceSGreg Roach
127e2ae4578SGreg Roach		$xref = $request->get('xref');
128e2ae4578SGreg Roach
129e2ae4578SGreg Roach		$individual = Individual::getInstance($xref, $tree);
1308c2e8227SGreg Roach		$tv         = new TreeView('tv');
1318c2e8227SGreg Roach
132e2ae4578SGreg Roach		list($html, $js) = $tv->drawViewport($individual, 4);
1338c2e8227SGreg Roach
134e2ae4578SGreg Roach		$title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1358c2e8227SGreg Roach
136e2ae4578SGreg Roach		return $this->viewResponse('interactive-tree-page', [
137e2ae4578SGreg Roach			'title'      => $title,
1388840c547SGreg Roach			'individual' => $individual,
139e2ae4578SGreg Roach			'js'         => $js,
140f8a18b14SGreg Roach			'html'       => $html,
141e2ae4578SGreg Roach			'tree'       => $tree,
142f8a18b14SGreg Roach		]);
1438c2e8227SGreg Roach	}
1448c2e8227SGreg Roach
145e2ae4578SGreg Roach	/**
146e2ae4578SGreg Roach	 * @param Request $request
147e2ae4578SGreg Roach	 *
148e2ae4578SGreg Roach	 * @return Response
149e2ae4578SGreg Roach	 */
150e2ae4578SGreg Roach	public function getDetailsAction(Request $request): Response {
151e2ae4578SGreg Roach		/** @var Tree $tree */
152e2ae4578SGreg Roach		$tree = $request->attributes->get('tree');
1538c2e8227SGreg Roach
154e2ae4578SGreg Roach		$pid        = $request->get('pid', WT_REGEX_XREF);
155e2ae4578SGreg Roach		$individual = Individual::getInstance($pid, $tree);
156e2ae4578SGreg Roach
157e2ae4578SGreg Roach		if ($individual && $individual->canShow()) {
158e2ae4578SGreg Roach			$instance = $request->get('instance');
159e2ae4578SGreg Roach			$treeview = new TreeView($instance);
160e2ae4578SGreg Roach
161e2ae4578SGreg Roach			return new Response($treeview->getDetails($individual));
162e2ae4578SGreg Roach		} else {
163e2ae4578SGreg Roach			throw new NotFoundHttpException;
1648c2e8227SGreg Roach		}
1658c2e8227SGreg Roach	}
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach	/**
168e2ae4578SGreg Roach	 * @param Request $request
1693cf92ae2SGreg Roach	 *
170e2ae4578SGreg Roach	 * @return Response
1718c2e8227SGreg Roach	 */
172e2ae4578SGreg Roach	public function getPersonsAction(Request $request): Response {
173*bc8b8f24SGreg Roach		/** @var Tree $tree */
174*bc8b8f24SGreg Roach		$tree = $request->attributes->get('tree');
175*bc8b8f24SGreg Roach
176e2ae4578SGreg Roach		$q  = $request->get('q');
177e2ae4578SGreg Roach		$instance = $request->get('instance');
178e2ae4578SGreg Roach		$treeview = new TreeView($instance);
1798c2e8227SGreg Roach
180*bc8b8f24SGreg Roach		return new Response($treeview->getPersons($tree, $q));
1818c2e8227SGreg Roach	}
1828c2e8227SGreg Roach}
183