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() 37 { 38 return /* I18N: Name of a module */ 39 I18N::translate('Interactive tree'); 40 } 41 42 /** {@inheritdoc} */ 43 public function getDescription() 44 { 45 return /* I18N: Description of the “Interactive tree” module */ 46 I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.'); 47 } 48 49 /** {@inheritdoc} */ 50 public function defaultTabOrder() 51 { 52 return 68; 53 } 54 55 /** {@inheritdoc} */ 56 public function getTabContent(Individual $individual) 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) 87 { 88 return true; 89 } 90 91 /** {@inheritdoc} */ 92 public function isGrayedOut(Individual $individual) 93 { 94 return false; 95 } 96 97 /** {@inheritdoc} */ 98 public function canLoadAjax() 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 * 140 * @return Response 141 */ 142 public function getTreeviewAction(Request $request): Response 143 { 144 /** @var Tree $tree */ 145 $tree = $request->attributes->get('tree'); 146 147 $xref = $request->get('xref'); 148 149 $individual = Individual::getInstance($xref, $tree); 150 151 if ($individual === null) { 152 throw new IndividualNotFoundException; 153 } 154 155 if (!$individual->canShow()) { 156 throw new IndividualAccessDeniedException; 157 } 158 159 $tv = new TreeView('tv'); 160 161 list($html, $js) = $tv->drawViewport($individual, 4); 162 163 $title = I18N::translate('Interactive tree of %s', $individual->getFullName()); 164 165 return $this->viewResponse('interactive-tree-page', [ 166 'title' => $title, 167 'individual' => $individual, 168 'js' => $js, 169 'html' => $html, 170 'tree' => $tree, 171 ]); 172 } 173 174 /** 175 * @param Request $request 176 * 177 * @return Response 178 */ 179 public function getDetailsAction(Request $request): Response 180 { 181 /** @var Tree $tree */ 182 $tree = $request->attributes->get('tree'); 183 184 $pid = $request->get('pid', WT_REGEX_XREF); 185 $individual = Individual::getInstance($pid, $tree); 186 187 if ($individual === null) { 188 throw new IndividualNotFoundException; 189 } 190 191 if (!$individual->canShow()) { 192 throw new IndividualAccessDeniedException; 193 } 194 195 $instance = $request->get('instance'); 196 $treeview = new TreeView($instance); 197 198 return new Response($treeview->getDetails($individual)); 199 } 200 201 /** 202 * @param Request $request 203 * 204 * @return Response 205 */ 206 public function getPersonsAction(Request $request): Response 207 { 208 /** @var Tree $tree */ 209 $tree = $request->attributes->get('tree'); 210 211 $q = $request->get('q'); 212 $instance = $request->get('instance'); 213 $treeview = new TreeView($instance); 214 215 return new Response($treeview->getPersons($tree, $q)); 216 } 217} 218