18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 25168ff6f3Sric2016use Fisharebest\Webtrees\Menu; 26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 2781b729d3SGreg Roachuse Fisharebest\Webtrees\Registry; 28ad40cb91SGreg Roachuse Fisharebest\Webtrees\Validator; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 31f3874e19SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class InteractiveTreeModule 348c2e8227SGreg 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 358c2e8227SGreg Roach */ 3637eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleChartTrait; 3949a243cbSGreg Roach use ModuleTabTrait; 4049a243cbSGreg Roach 41961ec755SGreg Roach /** 420cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 43961ec755SGreg Roach * 44961ec755SGreg Roach * @return string 45961ec755SGreg Roach */ 4649a243cbSGreg Roach public function title(): string 47c1010edaSGreg Roach { 48bbb76c12SGreg Roach /* I18N: Name of a module */ 49bbb76c12SGreg Roach return I18N::translate('Interactive tree'); 508c2e8227SGreg Roach } 518c2e8227SGreg Roach 52961ec755SGreg Roach /** 53961ec755SGreg Roach * A sentence describing what this module does. 54961ec755SGreg Roach * 55961ec755SGreg Roach * @return string 56961ec755SGreg Roach */ 5749a243cbSGreg Roach public function description(): string 58c1010edaSGreg Roach { 59bbb76c12SGreg Roach /* I18N: Description of the “Interactive tree” module */ 60bbb76c12SGreg Roach return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 618c2e8227SGreg Roach } 628c2e8227SGreg Roach 6349a243cbSGreg Roach /** 6449a243cbSGreg Roach * The default position for this tab. It can be changed in the control panel. 6549a243cbSGreg Roach * 6649a243cbSGreg Roach * @return int 6749a243cbSGreg Roach */ 68cbf4b7faSGreg Roach public function defaultTabOrder(): int 69cbf4b7faSGreg Roach { 70fb7a0427SGreg Roach return 7; 718c2e8227SGreg Roach } 728c2e8227SGreg Roach 733caaa4d2SGreg Roach /** 743caaa4d2SGreg Roach * Generate the HTML content of this tab. 753caaa4d2SGreg Roach * 763caaa4d2SGreg Roach * @param Individual $individual 773caaa4d2SGreg Roach * 783caaa4d2SGreg Roach * @return string 793caaa4d2SGreg Roach */ 809b34404bSGreg Roach public function getTabContent(Individual $individual): string 81c1010edaSGreg Roach { 82225e381fSGreg Roach $treeview = new TreeView('tvTab'); 835edf1a44SGreg Roach 8465e02381SGreg Roach [$html, $js] = $treeview->drawViewport($individual, 3); 858c2e8227SGreg Roach 865edf1a44SGreg Roach return view('modules/interactive-tree/tab', [ 87225e381fSGreg Roach 'html' => $html, 88225e381fSGreg Roach 'js' => $js, 89225e381fSGreg Roach ]); 908c2e8227SGreg Roach } 918c2e8227SGreg Roach 923caaa4d2SGreg Roach /** 933caaa4d2SGreg Roach * Is this tab empty? If so, we don't always need to display it. 943caaa4d2SGreg Roach * 953caaa4d2SGreg Roach * @param Individual $individual 963caaa4d2SGreg Roach * 973caaa4d2SGreg Roach * @return bool 983caaa4d2SGreg Roach */ 999b34404bSGreg Roach public function hasTabContent(Individual $individual): bool 100c1010edaSGreg Roach { 10176092b6cSGreg Roach return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty(); 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach 1043caaa4d2SGreg Roach /** 1053caaa4d2SGreg Roach * A greyed out tab has no actual content, but may perhaps have 1063caaa4d2SGreg Roach * options to create content. 1073caaa4d2SGreg Roach * 1083caaa4d2SGreg Roach * @param Individual $individual 1093caaa4d2SGreg Roach * 1103caaa4d2SGreg Roach * @return bool 1113caaa4d2SGreg Roach */ 1129b34404bSGreg Roach public function isGrayedOut(Individual $individual): bool 113c1010edaSGreg Roach { 1148c2e8227SGreg Roach return false; 1158c2e8227SGreg Roach } 1168c2e8227SGreg Roach 1173caaa4d2SGreg Roach /** 1183caaa4d2SGreg Roach * Can this tab load asynchronously? 1193caaa4d2SGreg Roach * 1203caaa4d2SGreg Roach * @return bool 1213caaa4d2SGreg Roach */ 1229b34404bSGreg Roach public function canLoadAjax(): bool 123c1010edaSGreg Roach { 1248c2e8227SGreg Roach return true; 1258c2e8227SGreg Roach } 1268c2e8227SGreg Roach 127168ff6f3Sric2016 /** 128377a2979SGreg Roach * CSS class for the URL. 129377a2979SGreg Roach * 130377a2979SGreg Roach * @return string 131377a2979SGreg Roach */ 132377a2979SGreg Roach public function chartMenuClass(): string 133377a2979SGreg Roach { 134377a2979SGreg Roach return 'menu-chart-tree'; 135377a2979SGreg Roach } 136377a2979SGreg Roach 137377a2979SGreg Roach /** 1384eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1394eb71cfaSGreg Roach * 14060bc3e3fSGreg Roach * @param Individual $individual 14160bc3e3fSGreg Roach * 1424eb71cfaSGreg Roach * @return Menu|null 1434eb71cfaSGreg Roach */ 144377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 145c1010edaSGreg Roach { 146e6562982SGreg Roach return $this->chartMenu($individual); 147e6562982SGreg Roach } 148e6562982SGreg Roach 149e6562982SGreg Roach /** 150e6562982SGreg Roach * The title for a specific instance of this chart. 151e6562982SGreg Roach * 152e6562982SGreg Roach * @param Individual $individual 153e6562982SGreg Roach * 154e6562982SGreg Roach * @return string 155e6562982SGreg Roach */ 156e6562982SGreg Roach public function chartTitle(Individual $individual): string 157e6562982SGreg Roach { 158e6562982SGreg Roach /* I18N: %s is an individual’s name */ 15939ca88baSGreg Roach return I18N::translate('Interactive tree of %s', $individual->fullName()); 160e6562982SGreg Roach } 161e6562982SGreg Roach 162e6562982SGreg Roach /** 163e6562982SGreg Roach * The URL for this chart. 164e6562982SGreg Roach * 165e6562982SGreg Roach * @param Individual $individual 16676d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 167e6562982SGreg Roach * 168e6562982SGreg Roach * @return string 169e6562982SGreg Roach */ 170e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 171e6562982SGreg Roach { 172e6562982SGreg Roach return route('module', [ 17326684e68SGreg Roach 'module' => $this->name(), 1745edf1a44SGreg Roach 'action' => 'Chart', 175e6562982SGreg Roach 'xref' => $individual->xref(), 1769022ab66SGreg Roach 'tree' => $individual->tree()->name(), 177e6562982SGreg Roach ] + $parameters); 178e6562982SGreg Roach } 179e6562982SGreg Roach 180e6562982SGreg Roach /** 1816ccdf4f0SGreg Roach * @param ServerRequestInterface $request 18276692c8bSGreg Roach * 1836ccdf4f0SGreg Roach * @return ResponseInterface 18476692c8bSGreg Roach */ 18557ab2231SGreg Roach public function getChartAction(ServerRequestInterface $request): ResponseInterface 186c1010edaSGreg Roach { 187b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 188b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 189b55cbc6bSGreg Roach $xref = Validator::queryParams($request)->isXref()->string('xref'); 1905229eadeSGreg Roach 191b55cbc6bSGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 192e2ae4578SGreg Roach 1936b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 1943c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 195bfaf8159SGreg Roach 1968c2e8227SGreg Roach $tv = new TreeView('tv'); 1978c2e8227SGreg Roach 19865e02381SGreg Roach [$html, $js] = $tv->drawViewport($individual, 4); 1998c2e8227SGreg Roach 200575707d1SGreg Roach return $this->viewResponse('modules/interactive-tree/page', [ 2015edf1a44SGreg Roach 'html' => $html, 2028840c547SGreg Roach 'individual' => $individual, 203e2ae4578SGreg Roach 'js' => $js, 20471378461SGreg Roach 'module' => $this->name(), 2055edf1a44SGreg Roach 'title' => $this->chartTitle($individual), 206e2ae4578SGreg Roach 'tree' => $tree, 207f8a18b14SGreg Roach ]); 2088c2e8227SGreg Roach } 2098c2e8227SGreg Roach 2100e519608SGreg Roach 2110e519608SGreg Roach /** 2120e519608SGreg Roach * @param ServerRequestInterface $request 2130e519608SGreg Roach * 2140e519608SGreg Roach * @return ResponseInterface 2150e519608SGreg Roach */ 2160e519608SGreg Roach public function postChartAction(ServerRequestInterface $request): ResponseInterface 2170e519608SGreg Roach { 2180e519608SGreg Roach return redirect(route('module', [ 2190e519608SGreg Roach 'module' => $this->name(), 2200e519608SGreg Roach 'action' => 'Chart', 221748dbe15SGreg Roach 'tree' => Validator::attributes($request)->tree()->name(), 222748dbe15SGreg Roach 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 2230e519608SGreg Roach ])); 2240e519608SGreg Roach } 2250e519608SGreg Roach 226e2ae4578SGreg Roach /** 2276ccdf4f0SGreg Roach * @param ServerRequestInterface $request 228e2ae4578SGreg Roach * 2296ccdf4f0SGreg Roach * @return ResponseInterface 230e2ae4578SGreg Roach */ 23157ab2231SGreg Roach public function getDetailsAction(ServerRequestInterface $request): ResponseInterface 232c1010edaSGreg Roach { 233b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 234748dbe15SGreg Roach $pid = Validator::queryParams($request)->string('pid'); 2356b9cb339SGreg Roach $individual = Registry::individualFactory()->make($pid, $tree); 23681b729d3SGreg Roach $individual = Auth::checkIndividualAccess($individual); 237748dbe15SGreg Roach $instance = Validator::queryParams($request)->string('instance'); 238e2ae4578SGreg Roach $treeview = new TreeView($instance); 239e2ae4578SGreg Roach 2406ccdf4f0SGreg Roach return response($treeview->getDetails($individual)); 2418c2e8227SGreg Roach } 2428c2e8227SGreg Roach 2438c2e8227SGreg Roach /** 2446ccdf4f0SGreg Roach * @param ServerRequestInterface $request 2453cf92ae2SGreg Roach * 2466ccdf4f0SGreg Roach * @return ResponseInterface 2478c2e8227SGreg Roach */ 24806f42609SGreg Roach public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface 249c1010edaSGreg Roach { 250b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 251748dbe15SGreg Roach $q = Validator::queryParams($request)->string('q'); 252748dbe15SGreg Roach $instance = Validator::queryParams($request)->string('instance'); 253e2ae4578SGreg Roach $treeview = new TreeView($instance); 2548c2e8227SGreg Roach 25506f42609SGreg Roach return response($treeview->getIndividuals($tree, $q)); 2568c2e8227SGreg Roach } 2578c2e8227SGreg Roach} 258