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\Family; 22use Fisharebest\Webtrees\FontAwesome; 23use Fisharebest\Webtrees\Functions\FunctionsCharts; 24use Fisharebest\Webtrees\Functions\FunctionsPrint; 25use Fisharebest\Webtrees\Gedcom; 26use Fisharebest\Webtrees\GedcomTag; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Individual; 29use Fisharebest\Webtrees\Menu; 30use Fisharebest\Webtrees\Services\ChartService; 31use Fisharebest\Webtrees\Tree; 32use Fisharebest\Webtrees\User; 33use Illuminate\Support\Collection; 34use Ramsey\Uuid\Uuid; 35use Symfony\Component\HttpFoundation\Request; 36use Symfony\Component\HttpFoundation\Response; 37 38/** 39 * Class DescendancyChartModule 40 */ 41class DescendancyChartModule extends AbstractModule implements ModuleChartInterface 42{ 43 use ModuleChartTrait; 44 45 // Chart styles 46 public const CHART_STYLE_LIST = 0; 47 public const CHART_STYLE_BOOKLET = 1; 48 public const CHART_STYLE_INDIVIDUALS = 2; 49 public const CHART_STYLE_FAMILIES = 3; 50 51 // Defaults 52 public const DEFAULT_STYLE = self::CHART_STYLE_LIST; 53 public const DEFAULT_GENERATIONS = '3'; 54 public const DEFAULT_MAXIMUM_GENERATIONS = '9'; 55 56 /** @var int[] */ 57 protected $dabo_num = []; 58 59 /** @var string[] */ 60 protected $dabo_sex = []; 61 62 /** 63 * How should this module be labelled on tabs, menus, etc.? 64 * 65 * @return string 66 */ 67 public function title(): string 68 { 69 /* I18N: Name of a module/chart */ 70 return I18N::translate('Descendants'); 71 } 72 73 /** 74 * A sentence describing what this module does. 75 * 76 * @return string 77 */ 78 public function description(): string 79 { 80 /* I18N: Description of the “DescendancyChart” module */ 81 return I18N::translate('A chart of an individual’s descendants.'); 82 } 83 84 /** 85 * CSS class for the URL. 86 * 87 * @return string 88 */ 89 public function chartMenuClass(): string 90 { 91 return 'menu-chart-descendants'; 92 } 93 94 /** 95 * Return a menu item for this chart - for use in individual boxes. 96 * 97 * @param Individual $individual 98 * 99 * @return Menu|null 100 */ 101 public function chartBoxMenu(Individual $individual): ?Menu 102 { 103 return $this->chartMenu($individual); 104 } 105 106 /** 107 * The title for a specific instance of this chart. 108 * 109 * @param Individual $individual 110 * 111 * @return string 112 */ 113 public function chartTitle(Individual $individual): string 114 { 115 /* I18N: %s is an individual’s name */ 116 return I18N::translate('Descendants of %s', $individual->getFullName()); 117 } 118 119 /** 120 * A form to request the chart parameters. 121 * 122 * @param Request $request 123 * @param Tree $tree 124 * @param User $user 125 * @param ChartService $chart_service 126 * 127 * @return Response 128 */ 129 public function getChartAction(Request $request, Tree $tree, User $user, ChartService $chart_service): Response 130 { 131 $ajax = (bool) $request->get('ajax'); 132 $xref = $request->get('xref', ''); 133 $individual = Individual::getInstance($xref, $tree); 134 135 Auth::checkIndividualAccess($individual); 136 Auth::checkComponentAccess($this, 'chart', $tree, $user); 137 138 $minimum_generations = 2; 139 $maximum_generations = (int) $tree->getPreference('MAX_DESCENDANCY_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS); 140 $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS); 141 142 $chart_style = (int) $request->get('chart_style', self::DEFAULT_STYLE); 143 $generations = (int) $request->get('generations', $default_generations); 144 145 $generations = min($generations, $maximum_generations); 146 $generations = max($generations, $minimum_generations); 147 148 if ($ajax) { 149 return $this->chart($request, $tree, $chart_service); 150 } 151 152 $ajax_url = $this->chartUrl($individual, [ 153 'chart_style' => $chart_style, 154 'generations' => $generations, 155 'ajax' => true, 156 ]); 157 158 return $this->viewResponse('modules/descendancy_chart/page', [ 159 'ajax_url' => $ajax_url, 160 'chart_style' => $chart_style, 161 'chart_styles' => $this->chartStyles(), 162 'default_generations' => $default_generations, 163 'generations' => $generations, 164 'individual' => $individual, 165 'maximum_generations' => $maximum_generations, 166 'minimum_generations' => $minimum_generations, 167 'module_name' => $this->name(), 168 'title' => $this->chartTitle($individual), 169 ]); 170 } 171 172 /** 173 * @param Request $request 174 * @param Tree $tree 175 * @param ChartService $chart_service 176 * 177 * @return Response 178 */ 179 public function chart(Request $request, Tree $tree, ChartService $chart_service): Response 180 { 181 $this->layout = 'layouts/ajax'; 182 183 $xref = $request->get('xref', ''); 184 $individual = Individual::getInstance($xref, $tree); 185 186 Auth::checkIndividualAccess($individual); 187 188 $minimum_generations = 2; 189 $maximum_generations = (int) $tree->getPreference('MAX_PEDIGREE_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS); 190 $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS); 191 192 $chart_style = (int) $request->get('chart_style', self::DEFAULT_STYLE); 193 $generations = (int) $request->get('generations', $default_generations); 194 195 $generations = min($generations, $maximum_generations); 196 $generations = max($generations, $minimum_generations); 197 198 switch ($chart_style) { 199 case self::CHART_STYLE_LIST: 200 default: 201 return $this->descendantsList($individual, $generations); 202 203 case self::CHART_STYLE_BOOKLET: 204 return $this->descendantsBooklet($individual, $generations); 205 206 case self::CHART_STYLE_INDIVIDUALS: 207 $individuals = $chart_service->descendants($individual, $generations - 1); 208 209 return $this->descendantsIndividuals($tree, $individuals); 210 211 case self::CHART_STYLE_FAMILIES: 212 $families = $chart_service->descendantFamilies($individual, $generations - 1); 213 214 return $this->descendantsFamilies($tree, $families); 215 } 216 } 217 218 /** 219 * Show a hierarchical list of descendants 220 * 221 * @TODO replace ob_start() with views. 222 * 223 * @param Individual $individual 224 * @param int $generations 225 * 226 * @return Response 227 */ 228 private function descendantsList(Individual $individual, int $generations): Response 229 { 230 ob_start(); 231 232 echo '<ul class="chart_common">'; 233 $this->printChildDescendancy($individual, $generations, $generations); 234 echo '</ul>'; 235 236 $html = ob_get_clean(); 237 238 return new Response($html); 239 } 240 241 /** 242 * print a child descendancy 243 * 244 * @param Individual $person 245 * @param int $depth the descendancy depth to show 246 * @param int $generations 247 * 248 * @return void 249 */ 250 private function printChildDescendancy(Individual $person, $depth, int $generations) 251 { 252 echo '<li>'; 253 echo '<table><tr><td>'; 254 if ($depth == $generations) { 255 echo '<img alt="" role="presentation" src="' . app()->make(ModuleThemeInterface::class)->parameter('image-spacer') . '" height="3" width="15"></td><td>'; 256 } else { 257 echo '<img src="' . app()->make(ModuleThemeInterface::class)->parameter('image-spacer') . '" height="3" width="3">'; 258 echo '<img src="' . app()->make(ModuleThemeInterface::class)->parameter('image-hline') . '" height="3" width="', 12, '"></td><td>'; 259 } 260 echo FunctionsPrint::printPedigreePerson($person); 261 echo '</td>'; 262 263 // check if child has parents and add an arrow 264 echo '<td></td>'; 265 echo '<td>'; 266 foreach ($person->getChildFamilies() as $cfamily) { 267 foreach ($cfamily->getSpouses() as $parent) { 268 echo FontAwesome::linkIcon('arrow-up', I18N::translate('Start at parents'), ['href' => route('descendants', ['ged' => $parent->tree()->name(), 269 'xref' => $parent->xref(), 270 'generations' => $generations, 271 ]), 272 ]); 273 // only show the arrow for one of the parents 274 break; 275 } 276 } 277 278 // d'Aboville child number 279 $level = $generations - $depth; 280 echo '<br><br> '; 281 echo '<span dir="ltr">'; //needed so that RTL languages will display this properly 282 if (!isset($this->dabo_num[$level])) { 283 $this->dabo_num[$level] = 0; 284 } 285 $this->dabo_num[$level]++; 286 $this->dabo_num[$level + 1] = 0; 287 $this->dabo_sex[$level] = $person->getSex(); 288 for ($i = 0; $i <= $level; $i++) { 289 $isf = $this->dabo_sex[$i]; 290 if ($isf === 'M') { 291 $isf = ''; 292 } 293 if ($isf === 'U') { 294 $isf = 'NN'; 295 } 296 echo '<span class="person_box' . $isf . '"> ' . $this->dabo_num[$i] . ' </span>'; 297 if ($i < $level) { 298 echo '.'; 299 } 300 } 301 echo '</span>'; 302 echo '</td></tr>'; 303 echo '</table>'; 304 echo '</li>'; 305 306 // loop for each spouse 307 foreach ($person->getSpouseFamilies() as $family) { 308 $this->printFamilyDescendancy($person, $family, $depth, $generations); 309 } 310 } 311 312 /** 313 * print a family descendancy 314 * 315 * @param Individual $person 316 * @param Family $family 317 * @param int $depth the descendancy depth to show 318 * @param int $generations 319 * 320 * @return void 321 */ 322 private function printFamilyDescendancy(Individual $person, Family $family, int $depth, int $generations) 323 { 324 $uid = Uuid::uuid4()->toString(); // create a unique ID 325 // print marriage info 326 echo '<li>'; 327 echo '<img src="', app()->make(ModuleThemeInterface::class)->parameter('image-spacer'), '" height="2" width="', 19, '">'; 328 echo '<span class="details1">'; 329 echo '<a href="#" onclick="expand_layer(\'' . $uid . '\'); return false;" class="top"><i id="' . $uid . '_img" class="icon-minus" title="' . I18N::translate('View this family') . '"></i></a>'; 330 if ($family->canShow()) { 331 foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) { 332 echo ' <a href="', e($family->url()), '" class="details1">', $fact->summary(), '</a>'; 333 } 334 } 335 echo '</span>'; 336 337 // print spouse 338 $spouse = $family->getSpouse($person); 339 echo '<ul class="generations" id="' . $uid . '">'; 340 echo '<li>'; 341 echo '<table><tr><td>'; 342 echo FunctionsPrint::printPedigreePerson($spouse); 343 echo '</td>'; 344 345 // check if spouse has parents and add an arrow 346 echo '<td></td>'; 347 echo '<td>'; 348 if ($spouse) { 349 foreach ($spouse->getChildFamilies() as $cfamily) { 350 foreach ($cfamily->getSpouses() as $parent) { 351 echo FontAwesome::linkIcon('arrow-up', I18N::translate('Start at parents'), ['href' => route('descendants', ['ged' => $parent->tree()->name(), 352 'xref' => $parent->xref(), 353 'generations' => $generations, 354 ]), 355 ]); 356 // only show the arrow for one of the parents 357 break; 358 } 359 } 360 } 361 echo '<br><br> '; 362 echo '</td></tr>'; 363 364 // children 365 $children = $family->getChildren(); 366 echo '<tr><td colspan="3" class="details1" > '; 367 if (!empty($children)) { 368 echo GedcomTag::getLabel('NCHI') . ': ' . count($children); 369 } else { 370 // Distinguish between no children (NCHI 0) and no recorded 371 // children (no CHIL records) 372 if (strpos($family->gedcom(), '\n1 NCHI 0') !== false) { 373 echo GedcomTag::getLabel('NCHI') . ': ' . count($children); 374 } else { 375 echo I18N::translate('No children'); 376 } 377 } 378 echo '</td></tr></table>'; 379 echo '</li>'; 380 if ($depth > 1) { 381 foreach ($children as $child) { 382 $this->printChildDescendancy($child, $depth - 1, $generations); 383 } 384 } 385 echo '</ul>'; 386 echo '</li>'; 387 } 388 389 /** 390 * Show a tabular list of individual descendants. 391 * 392 * @param Tree $tree 393 * @param Collection $individuals 394 * 395 * @return Response 396 */ 397 private function descendantsIndividuals(Tree $tree, Collection $individuals): Response 398 { 399 $this->layout = 'layouts/ajax'; 400 401 return $this->viewResponse('lists/individuals-table', [ 402 'individuals' => $individuals, 403 'sosa' => false, 404 'tree' => $tree, 405 ]); 406 } 407 408 /** 409 * Show a tabular list of individual descendants. 410 * 411 * @param Tree $tree 412 * @param Collection $families 413 * 414 * @return Response 415 */ 416 private function descendantsFamilies(Tree $tree, Collection $families): Response 417 { 418 $this->layout = 'layouts/ajax'; 419 420 return $this->viewResponse('lists/families-table', [ 421 'families' => $families, 422 'tree' => $tree, 423 ]); 424 } 425 426 /** 427 * Show a booklet view of descendants 428 * 429 * @TODO replace ob_start() with views. 430 * 431 * @param Individual $individual 432 * @param int $generations 433 * 434 * @return Response 435 */ 436 private function descendantsBooklet(Individual $individual, int $generations): Response 437 { 438 ob_start(); 439 440 $this->printChildFamily($individual, $generations); 441 442 $html = ob_get_clean(); 443 444 return new Response($html); 445 } 446 447 448 /** 449 * Print a child family 450 * 451 * @param Individual $individual 452 * @param int $depth - the descendancy depth to show 453 * @param string $daboville - d'Aboville number 454 * @param string $gpid 455 * 456 * @return void 457 */ 458 private function printChildFamily(Individual $individual, $depth, $daboville = '1.', $gpid = '') 459 { 460 if ($depth < 2) { 461 return; 462 } 463 464 $i = 1; 465 466 foreach ($individual->getSpouseFamilies() as $family) { 467 FunctionsCharts::printSosaFamily($family, '', -1, $daboville, $individual->xref(), $gpid, false); 468 foreach ($family->getChildren() as $child) { 469 $this->printChildFamily($child, $depth - 1, $daboville . ($i++) . '.', $individual->xref()); 470 } 471 } 472 } 473 474 /** 475 * This chart can display its output in a number of styles 476 * 477 * @return array 478 */ 479 private function chartStyles(): array 480 { 481 return [ 482 self::CHART_STYLE_LIST => I18N::translate('List'), 483 self::CHART_STYLE_BOOKLET => I18N::translate('Booklet'), 484 self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 485 self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 486 ]; 487 } 488} 489