18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 285229eadeSGreg Roachuse Fisharebest\Webtrees\Tree; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 31f3874e19SGreg Roach 325229eadeSGreg Roachuse function assert; 338c2e8227SGreg Roach 348c2e8227SGreg Roach/** 358c2e8227SGreg Roach * Class InteractiveTreeModule 368c2e8227SGreg 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 378c2e8227SGreg Roach */ 3837eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface 39c1010edaSGreg Roach{ 4049a243cbSGreg Roach use ModuleChartTrait; 4149a243cbSGreg Roach use ModuleTabTrait; 4249a243cbSGreg Roach 43961ec755SGreg Roach /** 440cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 45961ec755SGreg Roach * 46961ec755SGreg Roach * @return string 47961ec755SGreg Roach */ 4849a243cbSGreg Roach public function title(): string 49c1010edaSGreg Roach { 50bbb76c12SGreg Roach /* I18N: Name of a module */ 51bbb76c12SGreg Roach return I18N::translate('Interactive tree'); 528c2e8227SGreg Roach } 538c2e8227SGreg Roach 54961ec755SGreg Roach /** 55961ec755SGreg Roach * A sentence describing what this module does. 56961ec755SGreg Roach * 57961ec755SGreg Roach * @return string 58961ec755SGreg Roach */ 5949a243cbSGreg Roach public function description(): string 60c1010edaSGreg Roach { 61bbb76c12SGreg Roach /* I18N: Description of the “Interactive tree” module */ 62bbb76c12SGreg Roach return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 6549a243cbSGreg Roach /** 6649a243cbSGreg Roach * The default position for this tab. It can be changed in the control panel. 6749a243cbSGreg Roach * 6849a243cbSGreg Roach * @return int 6949a243cbSGreg Roach */ 70cbf4b7faSGreg Roach public function defaultTabOrder(): int 71cbf4b7faSGreg Roach { 72fb7a0427SGreg Roach return 7; 738c2e8227SGreg Roach } 748c2e8227SGreg Roach 753caaa4d2SGreg Roach /** 763caaa4d2SGreg Roach * Generate the HTML content of this tab. 773caaa4d2SGreg Roach * 783caaa4d2SGreg Roach * @param Individual $individual 793caaa4d2SGreg Roach * 803caaa4d2SGreg Roach * @return string 813caaa4d2SGreg Roach */ 829b34404bSGreg Roach public function getTabContent(Individual $individual): string 83c1010edaSGreg Roach { 84225e381fSGreg Roach $treeview = new TreeView('tvTab'); 855edf1a44SGreg Roach 8665e02381SGreg Roach [$html, $js] = $treeview->drawViewport($individual, 3); 878c2e8227SGreg Roach 885edf1a44SGreg Roach return view('modules/interactive-tree/tab', [ 89225e381fSGreg Roach 'html' => $html, 90225e381fSGreg Roach 'js' => $js, 91225e381fSGreg Roach ]); 928c2e8227SGreg Roach } 938c2e8227SGreg Roach 943caaa4d2SGreg Roach /** 953caaa4d2SGreg Roach * Is this tab empty? If so, we don't always need to display it. 963caaa4d2SGreg Roach * 973caaa4d2SGreg Roach * @param Individual $individual 983caaa4d2SGreg Roach * 993caaa4d2SGreg Roach * @return bool 1003caaa4d2SGreg Roach */ 1019b34404bSGreg Roach public function hasTabContent(Individual $individual): bool 102c1010edaSGreg Roach { 10376092b6cSGreg Roach return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty(); 1048c2e8227SGreg Roach } 1058c2e8227SGreg Roach 1063caaa4d2SGreg Roach /** 1073caaa4d2SGreg Roach * A greyed out tab has no actual content, but may perhaps have 1083caaa4d2SGreg Roach * options to create content. 1093caaa4d2SGreg Roach * 1103caaa4d2SGreg Roach * @param Individual $individual 1113caaa4d2SGreg Roach * 1123caaa4d2SGreg Roach * @return bool 1133caaa4d2SGreg Roach */ 1149b34404bSGreg Roach public function isGrayedOut(Individual $individual): bool 115c1010edaSGreg Roach { 1168c2e8227SGreg Roach return false; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 1193caaa4d2SGreg Roach /** 1203caaa4d2SGreg Roach * Can this tab load asynchronously? 1213caaa4d2SGreg Roach * 1223caaa4d2SGreg Roach * @return bool 1233caaa4d2SGreg Roach */ 1249b34404bSGreg Roach public function canLoadAjax(): bool 125c1010edaSGreg Roach { 1268c2e8227SGreg Roach return true; 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach 129168ff6f3Sric2016 /** 130377a2979SGreg Roach * CSS class for the URL. 131377a2979SGreg Roach * 132377a2979SGreg Roach * @return string 133377a2979SGreg Roach */ 134377a2979SGreg Roach public function chartMenuClass(): string 135377a2979SGreg Roach { 136377a2979SGreg Roach return 'menu-chart-tree'; 137377a2979SGreg Roach } 138377a2979SGreg Roach 139377a2979SGreg Roach /** 1404eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1414eb71cfaSGreg Roach * 14260bc3e3fSGreg Roach * @param Individual $individual 14360bc3e3fSGreg Roach * 1444eb71cfaSGreg Roach * @return Menu|null 1454eb71cfaSGreg Roach */ 146377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 147c1010edaSGreg Roach { 148e6562982SGreg Roach return $this->chartMenu($individual); 149e6562982SGreg Roach } 150e6562982SGreg Roach 151e6562982SGreg Roach /** 152e6562982SGreg Roach * The title for a specific instance of this chart. 153e6562982SGreg Roach * 154e6562982SGreg Roach * @param Individual $individual 155e6562982SGreg Roach * 156e6562982SGreg Roach * @return string 157e6562982SGreg Roach */ 158e6562982SGreg Roach public function chartTitle(Individual $individual): string 159e6562982SGreg Roach { 160e6562982SGreg Roach /* I18N: %s is an individual’s name */ 16139ca88baSGreg Roach return I18N::translate('Interactive tree of %s', $individual->fullName()); 162e6562982SGreg Roach } 163e6562982SGreg Roach 164e6562982SGreg Roach /** 165e6562982SGreg Roach * The URL for this chart. 166e6562982SGreg Roach * 167e6562982SGreg Roach * @param Individual $individual 168*09482a55SGreg Roach * @param array<bool|int|string|array|null> $parameters 169e6562982SGreg Roach * 170e6562982SGreg Roach * @return string 171e6562982SGreg Roach */ 172e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 173e6562982SGreg Roach { 174e6562982SGreg Roach return route('module', [ 17526684e68SGreg Roach 'module' => $this->name(), 1765edf1a44SGreg Roach 'action' => 'Chart', 177e6562982SGreg Roach 'xref' => $individual->xref(), 1789022ab66SGreg Roach 'tree' => $individual->tree()->name(), 179e6562982SGreg Roach ] + $parameters); 180e6562982SGreg Roach } 181e6562982SGreg Roach 182e6562982SGreg Roach /** 1836ccdf4f0SGreg Roach * @param ServerRequestInterface $request 18476692c8bSGreg Roach * 1856ccdf4f0SGreg Roach * @return ResponseInterface 18676692c8bSGreg Roach */ 18757ab2231SGreg Roach public function getChartAction(ServerRequestInterface $request): ResponseInterface 188c1010edaSGreg Roach { 18957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 19075964c75SGreg Roach assert($tree instanceof Tree); 1915229eadeSGreg Roach 192e106ff43SGreg Roach $xref = $request->getQueryParams()['xref']; 193e2ae4578SGreg Roach 1946b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 1953c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 196bfaf8159SGreg Roach 197ddeb3354SGreg Roach $user = $request->getAttribute('user'); 198ddeb3354SGreg Roach 199ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 200bfaf8159SGreg Roach 2018c2e8227SGreg Roach $tv = new TreeView('tv'); 2028c2e8227SGreg Roach 20365e02381SGreg Roach [$html, $js] = $tv->drawViewport($individual, 4); 2048c2e8227SGreg Roach 205575707d1SGreg Roach return $this->viewResponse('modules/interactive-tree/page', [ 2065edf1a44SGreg Roach 'html' => $html, 2078840c547SGreg Roach 'individual' => $individual, 208e2ae4578SGreg Roach 'js' => $js, 20971378461SGreg Roach 'module' => $this->name(), 2105edf1a44SGreg Roach 'title' => $this->chartTitle($individual), 211e2ae4578SGreg Roach 'tree' => $tree, 212f8a18b14SGreg Roach ]); 2138c2e8227SGreg Roach } 2148c2e8227SGreg Roach 2150e519608SGreg Roach 2160e519608SGreg Roach /** 2170e519608SGreg Roach * @param ServerRequestInterface $request 2180e519608SGreg Roach * 2190e519608SGreg Roach * @return ResponseInterface 2200e519608SGreg Roach */ 2210e519608SGreg Roach public function postChartAction(ServerRequestInterface $request): ResponseInterface 2220e519608SGreg Roach { 2230e519608SGreg Roach $tree = $request->getAttribute('tree'); 22475964c75SGreg Roach assert($tree instanceof Tree); 2250e519608SGreg Roach 226b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 227b46c87bdSGreg Roach 2280e519608SGreg Roach return redirect(route('module', [ 2290e519608SGreg Roach 'module' => $this->name(), 2300e519608SGreg Roach 'action' => 'Chart', 2310e519608SGreg Roach 'tree' => $tree->name(), 232b46c87bdSGreg Roach 'xref' => $params['xref'] ?? '', 2330e519608SGreg Roach ])); 2340e519608SGreg Roach } 2350e519608SGreg Roach 236e2ae4578SGreg Roach /** 2376ccdf4f0SGreg Roach * @param ServerRequestInterface $request 238e2ae4578SGreg Roach * 2396ccdf4f0SGreg Roach * @return ResponseInterface 240e2ae4578SGreg Roach */ 24157ab2231SGreg Roach public function getDetailsAction(ServerRequestInterface $request): ResponseInterface 242c1010edaSGreg Roach { 24357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2444ea62551SGreg Roach assert($tree instanceof Tree); 2454ea62551SGreg Roach 246e106ff43SGreg Roach $pid = $request->getQueryParams()['pid']; 2476b9cb339SGreg Roach $individual = Registry::individualFactory()->make($pid, $tree); 248e2ae4578SGreg Roach 24981b729d3SGreg Roach $individual = Auth::checkIndividualAccess($individual); 2500bc54ba3SGreg Roach 251e106ff43SGreg Roach $instance = $request->getQueryParams()['instance']; 252e2ae4578SGreg Roach $treeview = new TreeView($instance); 253e2ae4578SGreg Roach 2546ccdf4f0SGreg Roach return response($treeview->getDetails($individual)); 2558c2e8227SGreg Roach } 2568c2e8227SGreg Roach 2578c2e8227SGreg Roach /** 2586ccdf4f0SGreg Roach * @param ServerRequestInterface $request 2593cf92ae2SGreg Roach * 2606ccdf4f0SGreg Roach * @return ResponseInterface 2618c2e8227SGreg Roach */ 26206f42609SGreg Roach public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface 263c1010edaSGreg Roach { 26457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2654ea62551SGreg Roach assert($tree instanceof Tree); 2664ea62551SGreg Roach 267e106ff43SGreg Roach $q = $request->getQueryParams()['q']; 268e106ff43SGreg Roach $instance = $request->getQueryParams()['instance']; 269e2ae4578SGreg Roach $treeview = new TreeView($instance); 2708c2e8227SGreg Roach 27106f42609SGreg Roach return response($treeview->getIndividuals($tree, $q)); 2728c2e8227SGreg Roach } 2738c2e8227SGreg Roach} 274