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