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