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\FontAwesome; 22use Fisharebest\Webtrees\Functions\FunctionsCharts; 23use Fisharebest\Webtrees\Functions\FunctionsPrint; 24use Fisharebest\Webtrees\Gedcom; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Menu; 28use Fisharebest\Webtrees\Services\ChartService; 29use Fisharebest\Webtrees\Theme; 30use Fisharebest\Webtrees\Tree; 31use Illuminate\Support\Collection; 32use Symfony\Component\HttpFoundation\Request; 33use Symfony\Component\HttpFoundation\Response; 34 35/** 36 * Class AncestorsChartModule 37 */ 38class AncestorsChartModule extends AbstractModule implements ModuleInterface, ModuleChartInterface 39{ 40 use ModuleChartTrait; 41 42 // Chart styles 43 protected const CHART_STYLE_LIST = 'list'; 44 protected const CHART_STYLE_BOOKLET = 'booklet'; 45 protected const CHART_STYLE_INDIVIDUALS = 'individuals'; 46 protected const CHART_STYLE_FAMILIES = 'families'; 47 48 // Defaults 49 protected const DEFAULT_COUSINS = false; 50 protected const DEFAULT_STYLE = self::CHART_STYLE_LIST; 51 protected const DEFAULT_GENERATIONS = '3'; 52 protected const DEFAULT_MAXIMUM_GENERATIONS = '9'; 53 54 /** 55 * How should this module be labelled on tabs, menus, etc.? 56 * 57 * @return string 58 */ 59 public function title(): string 60 { 61 /* I18N: Name of a module/chart */ 62 return I18N::translate('Ancestors'); 63 } 64 65 /** 66 * A sentence describing what this module does. 67 * 68 * @return string 69 */ 70 public function description(): string 71 { 72 /* I18N: Description of the “AncestorsChart” module */ 73 return I18N::translate('A chart of an individual’s ancestors.'); 74 } 75 76 /** 77 * CSS class for the URL. 78 * 79 * @return string 80 */ 81 public function chartMenuClass(): string 82 { 83 return 'menu-chart-ancestry'; 84 } 85 86 /** 87 * Return a menu item for this chart - for use in individual boxes. 88 * 89 * @param Individual $individual 90 * 91 * @return Menu|null 92 */ 93 public function chartBoxMenu(Individual $individual): ?Menu 94 { 95 return $this->chartMenu($individual); 96 } 97 98 /** 99 * The title for a specific instance of this chart. 100 * 101 * @param Individual $individual 102 * 103 * @return string 104 */ 105 public function chartTitle(Individual $individual): string 106 { 107 /* I18N: %s is an individual’s name */ 108 return I18N::translate('Ancestors of %s', $individual->getFullName()); 109 } 110 111 /** 112 * A form to request the chart parameters. 113 * 114 * @param Request $request 115 * @param Tree $tree 116 * @param ChartService $chart_service 117 * 118 * @return Response 119 */ 120 public function getChartAction(Request $request, Tree $tree, ChartService $chart_service): Response 121 { 122 $ajax = (bool) $request->get('ajax'); 123 $xref = $request->get('xref', ''); 124 $individual = Individual::getInstance($xref, $tree); 125 126 Auth::checkIndividualAccess($individual); 127 128 $minimum_generations = 2; 129 $maximum_generations = (int) $tree->getPreference('MAX_PEDIGREE_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS); 130 $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS); 131 132 $show_cousins = (bool) $request->get('show_cousins', self::DEFAULT_COUSINS); 133 $chart_style = $request->get('chart_style', self::DEFAULT_STYLE); 134 $generations = (int) $request->get('generations', $default_generations); 135 136 $generations = min($generations, $maximum_generations); 137 $generations = max($generations, $minimum_generations); 138 139 if ($ajax) { 140 $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); 141 142 switch ($chart_style) { 143 default: 144 case self::CHART_STYLE_LIST: 145 return $this->ancestorsList($individual, $generations); 146 147 case self::CHART_STYLE_BOOKLET: 148 return $this->ancestorsBooklet($ancestors, $show_cousins); 149 150 case self::CHART_STYLE_INDIVIDUALS: 151 return $this->ancestorsIndividuals($tree, $ancestors); 152 153 case self::CHART_STYLE_FAMILIES: 154 return $this->ancestorsFamilies($tree, $ancestors); 155 } 156 } 157 158 $ajax_url = $this->chartUrl($individual, [ 159 'generations' => $generations, 160 'chart_style' => $chart_style, 161 'show_cousins' => $show_cousins, 162 'ajax' => true, 163 ]); 164 165 return $this->viewResponse('modules/ancestors-chart/page', [ 166 'ajax_url' => $ajax_url, 167 'chart_style' => $chart_style, 168 'chart_styles' => $this->chartStyles(), 169 'default_generations' => $default_generations, 170 'generations' => $generations, 171 'individual' => $individual, 172 'maximum_generations' => $maximum_generations, 173 'minimum_generations' => $minimum_generations, 174 'module_name' => $this->name(), 175 'show_cousins' => $show_cousins, 176 'title' => $this->chartTitle($individual), 177 ]); 178 } 179 180 /** 181 * Show a hierarchical list of ancestors 182 * 183 * @TODO replace ob_start() with views. 184 * 185 * @param Individual $individual 186 * @param int $generations 187 * 188 * @return Response 189 */ 190 protected function ancestorsList(Individual $individual, int $generations): Response 191 { 192 ob_start(); 193 194 $this->printChildAscendancy($individual, 1, $generations - 1); 195 196 $html = ob_get_clean(); 197 198 $html = '<ul class="chart_common">' . $html . '</ul>'; 199 200 return new Response($html); 201 } 202 203 /** 204 * print a child ascendancy 205 * 206 * @param Individual $individual 207 * @param int $sosa 208 * @param int $generations 209 * 210 * @return void 211 */ 212 protected function printChildAscendancy(Individual $individual, $sosa, $generations) 213 { 214 echo '<li class="wt-ancestors-chart-list-item">'; 215 echo '<table><tbody><tr><td>'; 216 if ($sosa === 1) { 217 echo '<img src="', Theme::theme()->parameter('image-spacer'), '" height="3" width="15"></td><td>'; 218 } else { 219 echo '<img src="', Theme::theme()->parameter('image-spacer'), '" height="3" width="2">'; 220 echo '<img src="', Theme::theme()->parameter('image-hline'), '" height="3" width="13"></td><td>'; 221 } 222 echo FunctionsPrint::printPedigreePerson($individual); 223 echo '</td><td>'; 224 if ($sosa > 1) { 225 echo FontAwesome::linkIcon('arrow-down', $this->chartTitle($individual), [ 226 'href' => $this->chartUrl($individual, [ 227 'generations' => $generations, 228 'chart_style' => self::CHART_STYLE_LIST, 229 ]) 230 ]); 231 } 232 echo '</td><td class="details1"> <span class="person_box' . ($sosa === 1 ? 'NN' : ($sosa % 2 ? 'F' : '')) . '">', I18N::number($sosa), '</span> '; 233 echo '</td><td class="details1"> ', FunctionsCharts::getSosaName($sosa), '</td>'; 234 echo '</tr></tbody></table>'; 235 236 // Parents 237 $family = $individual->getPrimaryChildFamily(); 238 if ($family && $generations > 0) { 239 // Marriage details 240 echo '<span class="details1">'; 241 echo '<img src="', Theme::theme()->parameter('image-spacer'), '" height="2" width="15"><a href="#" onclick="return expand_layer(\'sosa_', $sosa, '\');" class="top"><i id="sosa_', $sosa, '_img" class="icon-minus" title="', I18N::translate('View this family'), '"></i></a>'; 242 echo ' <span class="person_box">', I18N::number($sosa * 2), '</span> ', I18N::translate('and'); 243 echo ' <span class="person_boxF">', I18N::number($sosa * 2 + 1), '</span>'; 244 if ($family->canShow()) { 245 foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) { 246 echo ' <a href="', e($family->url()), '" class="details1">', $fact->summary(), '</a>'; 247 } 248 } 249 echo '</span>'; 250 echo '<ul class="wt-ancestors-chart-list" id="sosa_', $sosa, '">'; 251 if ($family->getHusband()) { 252 $this->printChildAscendancy($family->getHusband(), $sosa * 2, $generations - 1); 253 } 254 if ($family->getWife()) { 255 $this->printChildAscendancy($family->getWife(), $sosa * 2 + 1, $generations - 1); 256 } 257 echo '</ul>'; 258 } 259 echo '</li>'; 260 } 261 262 /** 263 * Show a tabular list of individual ancestors. 264 * 265 * @param Tree $tree 266 * @param Collection $ancestors 267 * 268 * @return Response 269 */ 270 protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): Response 271 { 272 $this->layout = 'layouts/ajax'; 273 274 return $this->viewResponse('lists/individuals-table', [ 275 'individuals' => $ancestors, 276 'sosa' => true, 277 'tree' => $tree, 278 ]); 279 } 280 281 /** 282 * Show a tabular list of individual ancestors. 283 * 284 * @param Tree $tree 285 * @param Collection $ancestors 286 * 287 * @return Response 288 */ 289 protected function ancestorsFamilies(Tree $tree, Collection $ancestors): Response 290 { 291 $this->layout = 'layouts/ajax'; 292 293 $families = []; 294 foreach ($ancestors as $individual) { 295 foreach ($individual->getChildFamilies() as $family) { 296 $families[$family->xref()] = $family; 297 } 298 } 299 300 return $this->viewResponse('lists/families-table', [ 301 'families' => $families, 302 'tree' => $tree, 303 ]); 304 } 305 306 /** 307 * Show a booklet view of ancestors 308 * 309 * @TODO replace ob_start() with views. 310 * 311 * @param Collection $ancestors 312 * @param bool $show_cousins 313 * 314 * @return Response 315 */ 316 protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): Response 317 { 318 ob_start(); 319 320 echo FunctionsPrint::printPedigreePerson($ancestors[1]); 321 foreach ($ancestors as $sosa => $individual) { 322 foreach ($individual->getChildFamilies() as $family) { 323 FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins); 324 } 325 } 326 327 $html = ob_get_clean(); 328 329 return new Response($html); 330 } 331 332 /** 333 * This chart can display its output in a number of styles 334 * 335 * @return array 336 */ 337 protected function chartStyles(): array 338 { 339 return [ 340 self::CHART_STYLE_LIST => I18N::translate('List'), 341 self::CHART_STYLE_BOOKLET => I18N::translate('Booklet'), 342 self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 343 self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 344 ]; 345 } 346} 347