1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Menu; 26use Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Validator; 29use Psr\Http\Message\ResponseInterface; 30use Psr\Http\Message\ServerRequestInterface; 31 32/** 33 * Class InteractiveTreeModule 34 * 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 35 */ 36class InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface 37{ 38 use ModuleChartTrait; 39 use ModuleTabTrait; 40 41 /** 42 * How should this module be identified in the control panel, etc.? 43 * 44 * @return string 45 */ 46 public function title(): string 47 { 48 /* I18N: Name of a module */ 49 return I18N::translate('Interactive tree'); 50 } 51 52 /** 53 * A sentence describing what this module does. 54 * 55 * @return string 56 */ 57 public function description(): string 58 { 59 /* I18N: Description of the “Interactive tree” module */ 60 return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 61 } 62 63 /** 64 * The default position for this tab. It can be changed in the control panel. 65 * 66 * @return int 67 */ 68 public function defaultTabOrder(): int 69 { 70 return 7; 71 } 72 73 /** 74 * Generate the HTML content of this tab. 75 * 76 * @param Individual $individual 77 * 78 * @return string 79 */ 80 public function getTabContent(Individual $individual): string 81 { 82 $treeview = new TreeView('tvTab'); 83 84 [$html, $js] = $treeview->drawViewport($individual, 3); 85 86 return view('modules/interactive-tree/tab', [ 87 'html' => $html, 88 'js' => $js, 89 ]); 90 } 91 92 /** 93 * Is this tab empty? If so, we don't always need to display it. 94 * 95 * @param Individual $individual 96 * 97 * @return bool 98 */ 99 public function hasTabContent(Individual $individual): bool 100 { 101 return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty(); 102 } 103 104 /** 105 * A greyed out tab has no actual content, but may perhaps have 106 * options to create content. 107 * 108 * @param Individual $individual 109 * 110 * @return bool 111 */ 112 public function isGrayedOut(Individual $individual): bool 113 { 114 return false; 115 } 116 117 /** 118 * Can this tab load asynchronously? 119 * 120 * @return bool 121 */ 122 public function canLoadAjax(): bool 123 { 124 return true; 125 } 126 127 /** 128 * CSS class for the URL. 129 * 130 * @return string 131 */ 132 public function chartMenuClass(): string 133 { 134 return 'menu-chart-tree'; 135 } 136 137 /** 138 * Return a menu item for this chart - for use in individual boxes. 139 * 140 * @param Individual $individual 141 * 142 * @return Menu|null 143 */ 144 public function chartBoxMenu(Individual $individual): ?Menu 145 { 146 return $this->chartMenu($individual); 147 } 148 149 /** 150 * The title for a specific instance of this chart. 151 * 152 * @param Individual $individual 153 * 154 * @return string 155 */ 156 public function chartTitle(Individual $individual): string 157 { 158 /* I18N: %s is an individual’s name */ 159 return I18N::translate('Interactive tree of %s', $individual->fullName()); 160 } 161 162 /** 163 * The URL for this chart. 164 * 165 * @param Individual $individual 166 * @param array<bool|int|string|array<string>|null> $parameters 167 * 168 * @return string 169 */ 170 public function chartUrl(Individual $individual, array $parameters = []): string 171 { 172 return route('module', [ 173 'module' => $this->name(), 174 'action' => 'Chart', 175 'xref' => $individual->xref(), 176 'tree' => $individual->tree()->name(), 177 ] + $parameters); 178 } 179 180 /** 181 * @param ServerRequestInterface $request 182 * 183 * @return ResponseInterface 184 */ 185 public function getChartAction(ServerRequestInterface $request): ResponseInterface 186 { 187 $tree = Validator::attributes($request)->tree(); 188 $user = Validator::attributes($request)->user(); 189 $xref = Validator::queryParams($request)->isXref()->string('xref'); 190 191 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 192 193 $individual = Registry::individualFactory()->make($xref, $tree); 194 $individual = Auth::checkIndividualAccess($individual, false, true); 195 196 $tv = new TreeView('tv'); 197 198 [$html, $js] = $tv->drawViewport($individual, 4); 199 200 return $this->viewResponse('modules/interactive-tree/page', [ 201 'html' => $html, 202 'individual' => $individual, 203 'js' => $js, 204 'module' => $this->name(), 205 'title' => $this->chartTitle($individual), 206 'tree' => $tree, 207 ]); 208 } 209 210 211 /** 212 * @param ServerRequestInterface $request 213 * 214 * @return ResponseInterface 215 */ 216 public function postChartAction(ServerRequestInterface $request): ResponseInterface 217 { 218 return redirect(route('module', [ 219 'module' => $this->name(), 220 'action' => 'Chart', 221 'tree' => Validator::attributes($request)->tree()->name(), 222 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 223 ])); 224 } 225 226 /** 227 * @param ServerRequestInterface $request 228 * 229 * @return ResponseInterface 230 */ 231 public function getDetailsAction(ServerRequestInterface $request): ResponseInterface 232 { 233 $tree = Validator::attributes($request)->tree(); 234 $pid = Validator::queryParams($request)->string('pid'); 235 $individual = Registry::individualFactory()->make($pid, $tree); 236 $individual = Auth::checkIndividualAccess($individual); 237 $instance = Validator::queryParams($request)->string('instance'); 238 $treeview = new TreeView($instance); 239 240 return response($treeview->getDetails($individual)); 241 } 242 243 /** 244 * @param ServerRequestInterface $request 245 * 246 * @return ResponseInterface 247 */ 248 public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface 249 { 250 $tree = Validator::attributes($request)->tree(); 251 $q = Validator::queryParams($request)->string('q'); 252 $instance = Validator::queryParams($request)->string('instance'); 253 $treeview = new TreeView($instance); 254 255 return response($treeview->getIndividuals($tree, $q)); 256 } 257} 258