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. 115 * 116 * @param Individual $individual 117 * 118 * @return Menu|null 119 */ 120 public function getChartMenu(Individual $individual): ?Menu 121 { 122 return new Menu( 123 $this->title(), 124 route('module', [ 125 'module' => $this->getName(), 126 'action' => 'Treeview', 127 'xref' => $individual->xref(), 128 'ged' => $individual->tree()->name(), 129 ]), 130 'menu-chart-tree', 131 ['rel' => 'nofollow'] 132 ); 133 } 134 135 /** 136 * Return a menu item for this chart - for use in individual boxes. 137 * 138 * @param Individual $individual 139 * 140 * @return Menu|null 141 */ 142 public function getBoxChartMenu(Individual $individual): ?Menu 143 { 144 return $this->getChartMenu($individual); 145 } 146 147 /** 148 * @param Request $request 149 * @param Tree $tree 150 * 151 * @return Response 152 */ 153 public function getTreeviewAction(Request $request, Tree $tree): Response 154 { 155 $xref = $request->get('xref', ''); 156 157 $individual = Individual::getInstance($xref, $tree); 158 159 if ($individual === null) { 160 throw new IndividualNotFoundException(); 161 } 162 163 if (!$individual->canShow()) { 164 throw new IndividualAccessDeniedException(); 165 } 166 167 $tv = new TreeView('tv'); 168 169 [$html, $js] = $tv->drawViewport($individual, 4); 170 171 $title = I18N::translate('Interactive tree of %s', $individual->getFullName()); 172 173 return $this->viewResponse('interactive-tree-page', [ 174 'title' => $title, 175 'individual' => $individual, 176 'js' => $js, 177 'html' => $html, 178 'tree' => $tree, 179 ]); 180 } 181 182 /** 183 * @param Request $request 184 * @param Tree $tree 185 * 186 * @return Response 187 */ 188 public function getDetailsAction(Request $request, Tree $tree): Response 189 { 190 $pid = $request->get('pid', Gedcom::REGEX_XREF); 191 $individual = Individual::getInstance($pid, $tree); 192 193 if ($individual === null) { 194 throw new IndividualNotFoundException(); 195 } 196 197 if (!$individual->canShow()) { 198 throw new IndividualAccessDeniedException(); 199 } 200 201 $instance = $request->get('instance', ''); 202 $treeview = new TreeView($instance); 203 204 return new Response($treeview->getDetails($individual)); 205 } 206 207 /** 208 * @param Request $request 209 * @param Tree $tree 210 * 211 * @return Response 212 */ 213 public function getPersonsAction(Request $request, Tree $tree): Response 214 { 215 $q = $request->get('q', ''); 216 $instance = $request->get('instance', ''); 217 $treeview = new TreeView($instance); 218 219 return new Response($treeview->getPersons($tree, $q)); 220 } 221} 222