1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException; 19use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 20use Fisharebest\Webtrees\I18N; 21use Fisharebest\Webtrees\Individual; 22use Fisharebest\Webtrees\Menu; 23use Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 24use Fisharebest\Webtrees\Tree; 25use Symfony\Component\HttpFoundation\Request; 26use Symfony\Component\HttpFoundation\Response; 27use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 28 29/** 30 * Class InteractiveTreeModule 31 * 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 32 */ 33class InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface 34{ 35 /** {@inheritdoc} */ 36 public function getTitle(): string 37 { 38 /* I18N: Name of a module */ 39 return I18N::translate('Interactive tree'); 40 } 41 42 /** {@inheritdoc} */ 43 public function getDescription(): string 44 { 45 /* I18N: Description of the “Interactive tree” module */ 46 return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 47 } 48 49 /** {@inheritdoc} */ 50 public function defaultTabOrder(): int 51 { 52 return 68; 53 } 54 55 /** {@inheritdoc} */ 56 public function getTabContent(Individual $individual): string 57 { 58 $treeview = new TreeView('tvTab'); 59 list($html, $js) = $treeview->drawViewport($individual, 3); 60 61 return view('modules/tree/tab', [ 62 'html' => $html, 63 'js' => $js, 64 'treeview_css' => $this->css(), 65 'treeview_js' => $this->js(), 66 ]); 67 } 68 69 /** 70 * @return string 71 */ 72 public function css(): string 73 { 74 return WT_MODULES_DIR . $this->getName() . '/css/treeview.css'; 75 } 76 77 /** 78 * @return string 79 */ 80 public function js(): string 81 { 82 return WT_MODULES_DIR . $this->getName() . '/js/treeview.js'; 83 } 84 85 /** {@inheritdoc} */ 86 public function hasTabContent(Individual $individual): bool 87 { 88 return true; 89 } 90 91 /** {@inheritdoc} */ 92 public function isGrayedOut(Individual $individual): bool 93 { 94 return false; 95 } 96 97 /** {@inheritdoc} */ 98 public function canLoadAjax(): bool 99 { 100 return true; 101 } 102 103 /** 104 * Return a menu item for this chart. 105 * 106 * @param Individual $individual 107 * 108 * @return Menu|null 109 */ 110 public function getChartMenu(Individual $individual) 111 { 112 return new Menu( 113 $this->getTitle(), 114 route('module', [ 115 'module' => $this->getName(), 116 'action' => 'Treeview', 117 'xref' => $individual->getXref(), 118 'ged' => $individual->getTree()->getName(), 119 ]), 120 'menu-chart-tree', 121 ['rel' => 'nofollow'] 122 ); 123 } 124 125 /** 126 * Return a menu item for this chart - for use in individual boxes. 127 * 128 * @param Individual $individual 129 * 130 * @return Menu|null 131 */ 132 public function getBoxChartMenu(Individual $individual) 133 { 134 return $this->getChartMenu($individual); 135 } 136 137 /** 138 * @param Request $request 139 * @param Tree $tree 140 * 141 * @return Response 142 */ 143 public function getTreeviewAction(Request $request, Tree $tree): Response 144 { 145 $xref = $request->get('xref'); 146 147 $individual = Individual::getInstance($xref, $tree); 148 149 if ($individual === null) { 150 throw new IndividualNotFoundException(); 151 } 152 153 if (!$individual->canShow()) { 154 throw new IndividualAccessDeniedException(); 155 } 156 157 $tv = new TreeView('tv'); 158 159 list($html, $js) = $tv->drawViewport($individual, 4); 160 161 $title = I18N::translate('Interactive tree of %s', $individual->getFullName()); 162 163 return $this->viewResponse('interactive-tree-page', [ 164 'title' => $title, 165 'individual' => $individual, 166 'js' => $js, 167 'html' => $html, 168 'tree' => $tree, 169 ]); 170 } 171 172 /** 173 * @param Request $request 174 * @param Tree $tree 175 * 176 * @return Response 177 */ 178 public function getDetailsAction(Request $request, Tree $tree): Response 179 { 180 $pid = $request->get('pid', WT_REGEX_XREF); 181 $individual = Individual::getInstance($pid, $tree); 182 183 if ($individual === null) { 184 throw new IndividualNotFoundException(); 185 } 186 187 if (!$individual->canShow()) { 188 throw new IndividualAccessDeniedException(); 189 } 190 191 $instance = $request->get('instance'); 192 $treeview = new TreeView($instance); 193 194 return new Response($treeview->getDetails($individual)); 195 } 196 197 /** 198 * @param Request $request 199 * @param Tree $tree 200 * 201 * @return Response 202 */ 203 public function getPersonsAction(Request $request, Tree $tree): Response 204 { 205 $q = $request->get('q'); 206 $instance = $request->get('instance'); 207 $treeview = new TreeView($instance); 208 209 return new Response($treeview->getPersons($tree, $q)); 210 } 211} 212