1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\Tree; 29use Fisharebest\Webtrees\Validator; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32 33use function assert; 34 35/** 36 * Class InteractiveTreeModule 37 * 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 38 */ 39class InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface 40{ 41 use ModuleChartTrait; 42 use ModuleTabTrait; 43 44 /** 45 * How should this module be identified in the control panel, etc.? 46 * 47 * @return string 48 */ 49 public function title(): string 50 { 51 /* I18N: Name of a module */ 52 return I18N::translate('Interactive tree'); 53 } 54 55 /** 56 * A sentence describing what this module does. 57 * 58 * @return string 59 */ 60 public function description(): string 61 { 62 /* I18N: Description of the “Interactive tree” module */ 63 return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 64 } 65 66 /** 67 * The default position for this tab. It can be changed in the control panel. 68 * 69 * @return int 70 */ 71 public function defaultTabOrder(): int 72 { 73 return 7; 74 } 75 76 /** 77 * Generate the HTML content of this tab. 78 * 79 * @param Individual $individual 80 * 81 * @return string 82 */ 83 public function getTabContent(Individual $individual): string 84 { 85 $treeview = new TreeView('tvTab'); 86 87 [$html, $js] = $treeview->drawViewport($individual, 3); 88 89 return view('modules/interactive-tree/tab', [ 90 'html' => $html, 91 'js' => $js, 92 ]); 93 } 94 95 /** 96 * Is this tab empty? If so, we don't always need to display it. 97 * 98 * @param Individual $individual 99 * 100 * @return bool 101 */ 102 public function hasTabContent(Individual $individual): bool 103 { 104 return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty(); 105 } 106 107 /** 108 * A greyed out tab has no actual content, but may perhaps have 109 * options to create content. 110 * 111 * @param Individual $individual 112 * 113 * @return bool 114 */ 115 public function isGrayedOut(Individual $individual): bool 116 { 117 return false; 118 } 119 120 /** 121 * Can this tab load asynchronously? 122 * 123 * @return bool 124 */ 125 public function canLoadAjax(): bool 126 { 127 return true; 128 } 129 130 /** 131 * CSS class for the URL. 132 * 133 * @return string 134 */ 135 public function chartMenuClass(): string 136 { 137 return 'menu-chart-tree'; 138 } 139 140 /** 141 * Return a menu item for this chart - for use in individual boxes. 142 * 143 * @param Individual $individual 144 * 145 * @return Menu|null 146 */ 147 public function chartBoxMenu(Individual $individual): ?Menu 148 { 149 return $this->chartMenu($individual); 150 } 151 152 /** 153 * The title for a specific instance of this chart. 154 * 155 * @param Individual $individual 156 * 157 * @return string 158 */ 159 public function chartTitle(Individual $individual): string 160 { 161 /* I18N: %s is an individual’s name */ 162 return I18N::translate('Interactive tree of %s', $individual->fullName()); 163 } 164 165 /** 166 * The URL for this chart. 167 * 168 * @param Individual $individual 169 * @param array<bool|int|string|array|null> $parameters 170 * 171 * @return string 172 */ 173 public function chartUrl(Individual $individual, array $parameters = []): string 174 { 175 return route('module', [ 176 'module' => $this->name(), 177 'action' => 'Chart', 178 'xref' => $individual->xref(), 179 'tree' => $individual->tree()->name(), 180 ] + $parameters); 181 } 182 183 /** 184 * @param ServerRequestInterface $request 185 * 186 * @return ResponseInterface 187 */ 188 public function getChartAction(ServerRequestInterface $request): ResponseInterface 189 { 190 $tree = $request->getAttribute('tree'); 191 assert($tree instanceof Tree); 192 193 $xref = Validator::queryParams($request)->isXref()->requiredString('xref'); 194 195 $individual = Registry::individualFactory()->make($xref, $tree); 196 $individual = Auth::checkIndividualAccess($individual, false, true); 197 198 $user = $request->getAttribute('user'); 199 200 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 201 202 $tv = new TreeView('tv'); 203 204 [$html, $js] = $tv->drawViewport($individual, 4); 205 206 return $this->viewResponse('modules/interactive-tree/page', [ 207 'html' => $html, 208 'individual' => $individual, 209 'js' => $js, 210 'module' => $this->name(), 211 'title' => $this->chartTitle($individual), 212 'tree' => $tree, 213 ]); 214 } 215 216 217 /** 218 * @param ServerRequestInterface $request 219 * 220 * @return ResponseInterface 221 */ 222 public function postChartAction(ServerRequestInterface $request): ResponseInterface 223 { 224 $tree = $request->getAttribute('tree'); 225 assert($tree instanceof Tree); 226 227 $params = (array) $request->getParsedBody(); 228 229 return redirect(route('module', [ 230 'module' => $this->name(), 231 'action' => 'Chart', 232 'tree' => $tree->name(), 233 'xref' => $params['xref'] ?? '', 234 ])); 235 } 236 237 /** 238 * @param ServerRequestInterface $request 239 * 240 * @return ResponseInterface 241 */ 242 public function getDetailsAction(ServerRequestInterface $request): ResponseInterface 243 { 244 $tree = $request->getAttribute('tree'); 245 assert($tree instanceof Tree); 246 247 $pid = $request->getQueryParams()['pid']; 248 $individual = Registry::individualFactory()->make($pid, $tree); 249 250 $individual = Auth::checkIndividualAccess($individual); 251 252 $instance = $request->getQueryParams()['instance']; 253 $treeview = new TreeView($instance); 254 255 return response($treeview->getDetails($individual)); 256 } 257 258 /** 259 * @param ServerRequestInterface $request 260 * 261 * @return ResponseInterface 262 */ 263 public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface 264 { 265 $tree = $request->getAttribute('tree'); 266 assert($tree instanceof Tree); 267 268 $q = $request->getQueryParams()['q']; 269 $instance = $request->getQueryParams()['instance']; 270 $treeview = new TreeView($instance); 271 272 return response($treeview->getIndividuals($tree, $q)); 273 } 274} 275