18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 225edf1a44SGreg Roachuse Fisharebest\Webtrees\Auth; 230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException; 240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 27168ff6f3Sric2016use Fisharebest\Webtrees\Menu; 28e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 295229eadeSGreg Roachuse Fisharebest\Webtrees\Tree; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 32f3874e19SGreg Roach 335229eadeSGreg Roachuse function assert; 348c2e8227SGreg Roach 358c2e8227SGreg Roach/** 368c2e8227SGreg Roach * Class InteractiveTreeModule 378c2e8227SGreg 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 388c2e8227SGreg Roach */ 3937eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface 40c1010edaSGreg Roach{ 4149a243cbSGreg Roach use ModuleChartTrait; 4249a243cbSGreg Roach use ModuleTabTrait; 4349a243cbSGreg Roach 44961ec755SGreg Roach /** 450cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 46961ec755SGreg Roach * 47961ec755SGreg Roach * @return string 48961ec755SGreg Roach */ 4949a243cbSGreg Roach public function title(): string 50c1010edaSGreg Roach { 51bbb76c12SGreg Roach /* I18N: Name of a module */ 52bbb76c12SGreg Roach return I18N::translate('Interactive tree'); 538c2e8227SGreg Roach } 548c2e8227SGreg Roach 55961ec755SGreg Roach /** 56961ec755SGreg Roach * A sentence describing what this module does. 57961ec755SGreg Roach * 58961ec755SGreg Roach * @return string 59961ec755SGreg Roach */ 6049a243cbSGreg Roach public function description(): string 61c1010edaSGreg Roach { 62bbb76c12SGreg Roach /* I18N: Description of the “Interactive tree” module */ 63bbb76c12SGreg Roach return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 648c2e8227SGreg Roach } 658c2e8227SGreg Roach 6649a243cbSGreg Roach /** 6749a243cbSGreg Roach * The default position for this tab. It can be changed in the control panel. 6849a243cbSGreg Roach * 6949a243cbSGreg Roach * @return int 7049a243cbSGreg Roach */ 71cbf4b7faSGreg Roach public function defaultTabOrder(): int 72cbf4b7faSGreg Roach { 73fb7a0427SGreg Roach return 7; 748c2e8227SGreg Roach } 758c2e8227SGreg Roach 763caaa4d2SGreg Roach /** 773caaa4d2SGreg Roach * Generate the HTML content of this tab. 783caaa4d2SGreg Roach * 793caaa4d2SGreg Roach * @param Individual $individual 803caaa4d2SGreg Roach * 813caaa4d2SGreg Roach * @return string 823caaa4d2SGreg Roach */ 839b34404bSGreg Roach public function getTabContent(Individual $individual): string 84c1010edaSGreg Roach { 85225e381fSGreg Roach $treeview = new TreeView('tvTab'); 865edf1a44SGreg Roach 8765e02381SGreg Roach [$html, $js] = $treeview->drawViewport($individual, 3); 888c2e8227SGreg Roach 895edf1a44SGreg Roach return view('modules/interactive-tree/tab', [ 90225e381fSGreg Roach 'html' => $html, 91225e381fSGreg Roach 'js' => $js, 92225e381fSGreg Roach ]); 938c2e8227SGreg Roach } 948c2e8227SGreg Roach 953caaa4d2SGreg Roach /** 963caaa4d2SGreg Roach * Is this tab empty? If so, we don't always need to display it. 973caaa4d2SGreg Roach * 983caaa4d2SGreg Roach * @param Individual $individual 993caaa4d2SGreg Roach * 1003caaa4d2SGreg Roach * @return bool 1013caaa4d2SGreg Roach */ 1029b34404bSGreg Roach public function hasTabContent(Individual $individual): bool 103c1010edaSGreg Roach { 10415d603e7SGreg Roach return true; 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach 1073caaa4d2SGreg Roach /** 1083caaa4d2SGreg Roach * A greyed out tab has no actual content, but may perhaps have 1093caaa4d2SGreg Roach * options to create content. 1103caaa4d2SGreg Roach * 1113caaa4d2SGreg Roach * @param Individual $individual 1123caaa4d2SGreg Roach * 1133caaa4d2SGreg Roach * @return bool 1143caaa4d2SGreg Roach */ 1159b34404bSGreg Roach public function isGrayedOut(Individual $individual): bool 116c1010edaSGreg Roach { 1178c2e8227SGreg Roach return false; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 1203caaa4d2SGreg Roach /** 1213caaa4d2SGreg Roach * Can this tab load asynchronously? 1223caaa4d2SGreg Roach * 1233caaa4d2SGreg Roach * @return bool 1243caaa4d2SGreg Roach */ 1259b34404bSGreg Roach public function canLoadAjax(): bool 126c1010edaSGreg Roach { 1278c2e8227SGreg Roach return true; 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach 130168ff6f3Sric2016 /** 131377a2979SGreg Roach * CSS class for the URL. 132377a2979SGreg Roach * 133377a2979SGreg Roach * @return string 134377a2979SGreg Roach */ 135377a2979SGreg Roach public function chartMenuClass(): string 136377a2979SGreg Roach { 137377a2979SGreg Roach return 'menu-chart-tree'; 138377a2979SGreg Roach } 139377a2979SGreg Roach 140377a2979SGreg Roach /** 1414eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1424eb71cfaSGreg Roach * 14360bc3e3fSGreg Roach * @param Individual $individual 14460bc3e3fSGreg Roach * 1454eb71cfaSGreg Roach * @return Menu|null 1464eb71cfaSGreg Roach */ 147377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 148c1010edaSGreg Roach { 149e6562982SGreg Roach return $this->chartMenu($individual); 150e6562982SGreg Roach } 151e6562982SGreg Roach 152e6562982SGreg Roach /** 153e6562982SGreg Roach * The title for a specific instance of this chart. 154e6562982SGreg Roach * 155e6562982SGreg Roach * @param Individual $individual 156e6562982SGreg Roach * 157e6562982SGreg Roach * @return string 158e6562982SGreg Roach */ 159e6562982SGreg Roach public function chartTitle(Individual $individual): string 160e6562982SGreg Roach { 161e6562982SGreg Roach /* I18N: %s is an individual’s name */ 16239ca88baSGreg Roach return I18N::translate('Interactive tree of %s', $individual->fullName()); 163e6562982SGreg Roach } 164e6562982SGreg Roach 165e6562982SGreg Roach /** 166e6562982SGreg Roach * The URL for this chart. 167e6562982SGreg Roach * 168e6562982SGreg Roach * @param Individual $individual 16959597b37SGreg Roach * @param mixed[] $parameters 170e6562982SGreg Roach * 171e6562982SGreg Roach * @return string 172e6562982SGreg Roach */ 173e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 174e6562982SGreg Roach { 175e6562982SGreg Roach return route('module', [ 17626684e68SGreg Roach 'module' => $this->name(), 1775edf1a44SGreg Roach 'action' => 'Chart', 178e6562982SGreg Roach 'xref' => $individual->xref(), 1799022ab66SGreg Roach 'tree' => $individual->tree()->name(), 180e6562982SGreg Roach ] + $parameters); 181e6562982SGreg Roach } 182e6562982SGreg Roach 183e6562982SGreg Roach /** 1846ccdf4f0SGreg Roach * @param ServerRequestInterface $request 18576692c8bSGreg Roach * 1866ccdf4f0SGreg Roach * @return ResponseInterface 18776692c8bSGreg Roach */ 18857ab2231SGreg Roach public function getChartAction(ServerRequestInterface $request): ResponseInterface 189c1010edaSGreg Roach { 19057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 19175964c75SGreg Roach assert($tree instanceof Tree); 1925229eadeSGreg Roach 193e106ff43SGreg Roach $xref = $request->getQueryParams()['xref']; 194e2ae4578SGreg Roach 195e2ae4578SGreg Roach $individual = Individual::getInstance($xref, $tree); 1963c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 197bfaf8159SGreg Roach 198ddeb3354SGreg Roach $user = $request->getAttribute('user'); 199ddeb3354SGreg Roach 2009867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 201bfaf8159SGreg Roach 2028c2e8227SGreg Roach $tv = new TreeView('tv'); 2038c2e8227SGreg Roach 20465e02381SGreg Roach [$html, $js] = $tv->drawViewport($individual, 4); 2058c2e8227SGreg Roach 206575707d1SGreg Roach return $this->viewResponse('modules/interactive-tree/page', [ 2075edf1a44SGreg Roach 'html' => $html, 2088840c547SGreg Roach 'individual' => $individual, 209e2ae4578SGreg Roach 'js' => $js, 21071378461SGreg Roach 'module' => $this->name(), 2115edf1a44SGreg Roach 'title' => $this->chartTitle($individual), 212e2ae4578SGreg Roach 'tree' => $tree, 213f8a18b14SGreg Roach ]); 2148c2e8227SGreg Roach } 2158c2e8227SGreg Roach 2160e519608SGreg Roach 2170e519608SGreg Roach /** 2180e519608SGreg Roach * @param ServerRequestInterface $request 2190e519608SGreg Roach * 2200e519608SGreg Roach * @return ResponseInterface 2210e519608SGreg Roach */ 2220e519608SGreg Roach public function postChartAction(ServerRequestInterface $request): ResponseInterface 2230e519608SGreg Roach { 2240e519608SGreg Roach $tree = $request->getAttribute('tree'); 22575964c75SGreg Roach assert($tree instanceof Tree); 2260e519608SGreg Roach 227*b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 228*b46c87bdSGreg Roach 2290e519608SGreg Roach return redirect(route('module', [ 2300e519608SGreg Roach 'module' => $this->name(), 2310e519608SGreg Roach 'action' => 'Chart', 2320e519608SGreg Roach 'tree' => $tree->name(), 233*b46c87bdSGreg Roach 'xref' => $params['xref'] ?? '', 2340e519608SGreg Roach ])); 2350e519608SGreg Roach } 2360e519608SGreg Roach 237e2ae4578SGreg Roach /** 2386ccdf4f0SGreg Roach * @param ServerRequestInterface $request 239e2ae4578SGreg Roach * 2406ccdf4f0SGreg Roach * @return ResponseInterface 241e2ae4578SGreg Roach */ 24257ab2231SGreg Roach public function getDetailsAction(ServerRequestInterface $request): ResponseInterface 243c1010edaSGreg Roach { 24457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2454ea62551SGreg Roach assert($tree instanceof Tree); 2464ea62551SGreg Roach 247e106ff43SGreg Roach $pid = $request->getQueryParams()['pid']; 248e2ae4578SGreg Roach $individual = Individual::getInstance($pid, $tree); 249e2ae4578SGreg Roach 2500bc54ba3SGreg Roach if ($individual === null) { 25159f2f229SGreg Roach throw new IndividualNotFoundException(); 2520bc54ba3SGreg Roach } 2530bc54ba3SGreg Roach 2540bc54ba3SGreg Roach if (!$individual->canShow()) { 25559f2f229SGreg Roach throw new IndividualAccessDeniedException(); 2560bc54ba3SGreg Roach } 2570bc54ba3SGreg Roach 258e106ff43SGreg Roach $instance = $request->getQueryParams()['instance']; 259e2ae4578SGreg Roach $treeview = new TreeView($instance); 260e2ae4578SGreg Roach 2616ccdf4f0SGreg Roach return response($treeview->getDetails($individual)); 2628c2e8227SGreg Roach } 2638c2e8227SGreg Roach 2648c2e8227SGreg Roach /** 2656ccdf4f0SGreg Roach * @param ServerRequestInterface $request 2663cf92ae2SGreg Roach * 2676ccdf4f0SGreg Roach * @return ResponseInterface 2688c2e8227SGreg Roach */ 26906f42609SGreg Roach public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface 270c1010edaSGreg Roach { 27157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2724ea62551SGreg Roach assert($tree instanceof Tree); 2734ea62551SGreg Roach 274e106ff43SGreg Roach $q = $request->getQueryParams()['q']; 275e106ff43SGreg Roach $instance = $request->getQueryParams()['instance']; 276e2ae4578SGreg Roach $treeview = new TreeView($instance); 2778c2e8227SGreg Roach 27806f42609SGreg Roach return response($treeview->getIndividuals($tree, $q)); 2798c2e8227SGreg Roach } 2808c2e8227SGreg Roach} 281