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\Exceptions\IndividualAccessDeniedException; 21use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 22use Fisharebest\Webtrees\Gedcom; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Menu; 26use Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 27use Fisharebest\Webtrees\Tree; 28use Fisharebest\Webtrees\Webtrees; 29use Symfony\Component\HttpFoundation\Request; 30use Symfony\Component\HttpFoundation\Response; 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 ModuleInterface, ModuleChartInterface, ModuleTabInterface 37{ 38 use ModuleChartTrait; 39 use ModuleTabTrait; 40 41 /** {@inheritdoc} */ 42 public function title(): string 43 { 44 /* I18N: Name of a module */ 45 return I18N::translate('Interactive tree'); 46 } 47 48 /** {@inheritdoc} */ 49 public function description(): string 50 { 51 /* I18N: Description of the “Interactive tree” module */ 52 return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 53 } 54 55 /** 56 * The default position for this tab. It can be changed in the control panel. 57 * 58 * @return int 59 */ 60 public function defaultTabOrder(): int 61 { 62 return 90; 63 } 64 65 /** {@inheritdoc} */ 66 public function getTabContent(Individual $individual): string 67 { 68 $treeview = new TreeView('tvTab'); 69 [$html, $js] = $treeview->drawViewport($individual, 3); 70 71 return view('modules/tree/tab', [ 72 'html' => $html, 73 'js' => $js, 74 'treeview_css' => $this->css(), 75 'treeview_js' => $this->js(), 76 ]); 77 } 78 79 /** 80 * @return string 81 */ 82 public function css(): string 83 { 84 return Webtrees::MODULES_PATH . $this->getName() . '/css/treeview.css'; 85 } 86 87 /** 88 * @return string 89 */ 90 public function js(): string 91 { 92 return Webtrees::MODULES_PATH . $this->getName() . '/js/treeview.js'; 93 } 94 95 /** {@inheritdoc} */ 96 public function hasTabContent(Individual $individual): bool 97 { 98 return true; 99 } 100 101 /** {@inheritdoc} */ 102 public function isGrayedOut(Individual $individual): bool 103 { 104 return false; 105 } 106 107 /** {@inheritdoc} */ 108 public function canLoadAjax(): bool 109 { 110 return true; 111 } 112 113 /** 114 * Return a menu item for this chart - for use in individual boxes. 115 * 116 * @param Individual $individual 117 * 118 * @return Menu|null 119 */ 120 public function chartMenuIndividual(Individual $individual): ?Menu 121 { 122 return $this->chartMenu($individual); 123 } 124 125 /** 126 * The title for a specific instance of this chart. 127 * 128 * @param Individual $individual 129 * 130 * @return string 131 */ 132 public function chartTitle(Individual $individual): string 133 { 134 /* I18N: %s is an individual’s name */ 135 return I18N::translate('Interactive tree of %s', $individual->getFullName()); 136 } 137 138 /** 139 * The URL for this chart. 140 * 141 * @param Individual $individual 142 * @param string[] $parameters 143 * 144 * @return string 145 */ 146 public function chartUrl(Individual $individual, array $parameters = []): string 147 { 148 return route('module', [ 149 'module' => $this->getName(), 150 'action' => 'Treeview', 151 'xref' => $individual->xref(), 152 'ged' => $individual->tree()->name(), 153 ] + $parameters); 154 } 155 156 /** 157 * CSS class for the URL. 158 * 159 * @return string 160 */ 161 public function chartUrlClasss(): string 162 { 163 return 'menu-chart-tree'; 164 } 165 166 /** 167 * @param Request $request 168 * @param Tree $tree 169 * 170 * @return Response 171 */ 172 public function getTreeviewAction(Request $request, Tree $tree): Response 173 { 174 $xref = $request->get('xref', ''); 175 176 $individual = Individual::getInstance($xref, $tree); 177 178 if ($individual === null) { 179 throw new IndividualNotFoundException(); 180 } 181 182 if (!$individual->canShow()) { 183 throw new IndividualAccessDeniedException(); 184 } 185 186 $tv = new TreeView('tv'); 187 188 [$html, $js] = $tv->drawViewport($individual, 4); 189 190 $title = I18N::translate('Interactive tree of %s', $individual->getFullName()); 191 192 return $this->viewResponse('interactive-tree-page', [ 193 'title' => $title, 194 'individual' => $individual, 195 'js' => $js, 196 'html' => $html, 197 'tree' => $tree, 198 ]); 199 } 200 201 /** 202 * @param Request $request 203 * @param Tree $tree 204 * 205 * @return Response 206 */ 207 public function getDetailsAction(Request $request, Tree $tree): Response 208 { 209 $pid = $request->get('pid', Gedcom::REGEX_XREF); 210 $individual = Individual::getInstance($pid, $tree); 211 212 if ($individual === null) { 213 throw new IndividualNotFoundException(); 214 } 215 216 if (!$individual->canShow()) { 217 throw new IndividualAccessDeniedException(); 218 } 219 220 $instance = $request->get('instance', ''); 221 $treeview = new TreeView($instance); 222 223 return new Response($treeview->getDetails($individual)); 224 } 225 226 /** 227 * @param Request $request 228 * @param Tree $tree 229 * 230 * @return Response 231 */ 232 public function getPersonsAction(Request $request, Tree $tree): Response 233 { 234 $q = $request->get('q', ''); 235 $instance = $request->get('instance', ''); 236 $treeview = new TreeView($instance); 237 238 return new Response($treeview->getPersons($tree, $q)); 239 } 240} 241