xref: /webtrees/app/Module/InteractiveTreeModule.php (revision a8cd57e1bd9bc297d89d2b7df423a89f32b96a3a)
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
54*a8cd57e1SGreg Roach		return view('modules/tree/tab', [
55225e381fSGreg Roach			'html'         => $html,
56225e381fSGreg Roach			'js'           => $js,
57e2ae4578SGreg Roach			'treeview_css' => WT_MODULES_DIR . $this->getName() . '/css/treeview.css',
58e2ae4578SGreg Roach			'treeview_js'  => WT_MODULES_DIR . $this->getName() . '/js/treeview.js',
59225e381fSGreg Roach		]);
608c2e8227SGreg Roach	}
618c2e8227SGreg Roach
628c2e8227SGreg Roach	/** {@inheritdoc} */
63225e381fSGreg Roach	public function hasTabContent(Individual $individual) {
6415d603e7SGreg Roach		return true;
658c2e8227SGreg Roach	}
668c2e8227SGreg Roach
678c2e8227SGreg Roach	/** {@inheritdoc} */
68225e381fSGreg Roach	public function isGrayedOut(Individual $individual) {
698c2e8227SGreg Roach		return false;
708c2e8227SGreg Roach	}
718c2e8227SGreg Roach
728c2e8227SGreg Roach	/** {@inheritdoc} */
738c2e8227SGreg Roach	public function canLoadAjax() {
748c2e8227SGreg Roach		return true;
758c2e8227SGreg Roach	}
768c2e8227SGreg Roach
77168ff6f3Sric2016	/**
78168ff6f3Sric2016	 * Return a menu item for this chart.
79168ff6f3Sric2016	 *
8060bc3e3fSGreg Roach	 * @param Individual $individual
8160bc3e3fSGreg Roach	 *
824eb71cfaSGreg Roach	 * @return Menu|null
83168ff6f3Sric2016	 */
84168ff6f3Sric2016	public function getChartMenu(Individual $individual) {
85168ff6f3Sric2016		return new Menu(
86168ff6f3Sric2016			$this->getTitle(),
87e2ae4578SGreg Roach			e(route('module', ['module' => $this->getName(), 'action' => 'Treeview', 'xref' => $individual->getXref(), 'ged' => $individual->getTree()->getName()])),
88168ff6f3Sric2016			'menu-chart-tree',
8913abd6f3SGreg Roach			['rel' => 'nofollow']
90168ff6f3Sric2016		);
91168ff6f3Sric2016	}
92168ff6f3Sric2016
934eb71cfaSGreg Roach	/**
944eb71cfaSGreg Roach	 * Return a menu item for this chart - for use in individual boxes.
954eb71cfaSGreg Roach	 *
9660bc3e3fSGreg Roach	 * @param Individual $individual
9760bc3e3fSGreg Roach	 *
984eb71cfaSGreg Roach	 * @return Menu|null
994eb71cfaSGreg Roach	 */
100168ff6f3Sric2016	public function getBoxChartMenu(Individual $individual) {
101168ff6f3Sric2016		return $this->getChartMenu($individual);
102168ff6f3Sric2016	}
103168ff6f3Sric2016
10476692c8bSGreg Roach	/**
105e2ae4578SGreg Roach	 * @param Request $request
10676692c8bSGreg Roach	 *
107e2ae4578SGreg Roach	 * @return Response
10876692c8bSGreg Roach	 */
109e2ae4578SGreg Roach	public function getTreeviewAction(Request $request): Response {
110e2ae4578SGreg Roach		/** @var Tree $tree */
111e2ae4578SGreg Roach		$tree = $request->attributes->get('tree');
11224ec66ceSGreg Roach
113e2ae4578SGreg Roach		$xref = $request->get('xref');
114e2ae4578SGreg Roach
115e2ae4578SGreg Roach		$individual = Individual::getInstance($xref, $tree);
1168c2e8227SGreg Roach		$tv         = new TreeView('tv');
1178c2e8227SGreg Roach
118e2ae4578SGreg Roach		list($html, $js) = $tv->drawViewport($individual, 4);
1198c2e8227SGreg Roach
120e2ae4578SGreg Roach		$title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1218c2e8227SGreg Roach
122e2ae4578SGreg Roach		return $this->viewResponse('interactive-tree-page', [
123e2ae4578SGreg Roach			'title'      => $title,
1248840c547SGreg Roach			'individual' => $individual,
125e2ae4578SGreg Roach			'js'         => $js,
126f8a18b14SGreg Roach			'html'       => $html,
127e2ae4578SGreg Roach			'tree'       => $tree,
128f8a18b14SGreg Roach		]);
1298c2e8227SGreg Roach	}
1308c2e8227SGreg Roach
131e2ae4578SGreg Roach	/**
132e2ae4578SGreg Roach	 * @param Request $request
133e2ae4578SGreg Roach	 *
134e2ae4578SGreg Roach	 * @return Response
135e2ae4578SGreg Roach	 */
136e2ae4578SGreg Roach	public function getDetailsAction(Request $request): Response {
137e2ae4578SGreg Roach		/** @var Tree $tree */
138e2ae4578SGreg Roach		$tree = $request->attributes->get('tree');
1398c2e8227SGreg Roach
140e2ae4578SGreg Roach		$pid        = $request->get('pid', WT_REGEX_XREF);
141e2ae4578SGreg Roach		$individual = Individual::getInstance($pid, $tree);
142e2ae4578SGreg Roach
143e2ae4578SGreg Roach		if ($individual && $individual->canShow()) {
144e2ae4578SGreg Roach			$instance = $request->get('instance');
145e2ae4578SGreg Roach			$treeview = new TreeView($instance);
146e2ae4578SGreg Roach
147e2ae4578SGreg Roach			return new Response($treeview->getDetails($individual));
148e2ae4578SGreg Roach		} else {
149e2ae4578SGreg Roach			throw new NotFoundHttpException;
1508c2e8227SGreg Roach		}
1518c2e8227SGreg Roach	}
1528c2e8227SGreg Roach
1538c2e8227SGreg Roach	/**
154e2ae4578SGreg Roach	 * @param Request $request
1553cf92ae2SGreg Roach	 *
156e2ae4578SGreg Roach	 * @return Response
1578c2e8227SGreg Roach	 */
158e2ae4578SGreg Roach	public function getPersonsAction(Request $request): Response {
159e2ae4578SGreg Roach		$q  = $request->get('q');
160e2ae4578SGreg Roach		$instance = $request->get('instance');
161e2ae4578SGreg Roach		$treeview = new TreeView($instance);
1628c2e8227SGreg Roach
163e2ae4578SGreg Roach		return new Response($treeview->getPersons($q));
1648c2e8227SGreg Roach	}
1658c2e8227SGreg Roach}
166