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\Contracts\UserInterface; 22use Fisharebest\Webtrees\Functions\FunctionsPrint; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Menu; 26use Fisharebest\Webtrees\Tree; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29use stdClass; 30use function view; 31 32/** 33 * Class FamilyBookChartModule 34 */ 35class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface 36{ 37 use ModuleChartTrait; 38 39 // Defaults 40 private const DEFAULT_GENERATIONS = '2'; 41 private const DEFAULT_DESCENDANT_GENERATIONS = '5'; 42 private const DEFAULT_MAXIMUM_GENERATIONS = '9'; 43 44 // Limits 45 public const MINIMUM_GENERATIONS = 2; 46 public const MAXIMUM_GENERATIONS = 10; 47 48 /** @var stdClass */ 49 private $box; 50 51 /** @var bool */ 52 private $show_spouse; 53 54 /** @var int */ 55 private $descent; 56 57 /** @var int */ 58 private $bhalfheight; 59 60 /** @var int */ 61 private $generations; 62 63 /** @var int */ 64 private $dgenerations; 65 66 /** 67 * How should this module be identified in the control panel, etc.? 68 * 69 * @return string 70 */ 71 public function title(): string 72 { 73 /* I18N: Name of a module/chart */ 74 return I18N::translate('Family book'); 75 } 76 77 /** 78 * A sentence describing what this module does. 79 * 80 * @return string 81 */ 82 public function description(): string 83 { 84 /* I18N: Description of the “FamilyBookChart” module */ 85 return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.'); 86 } 87 88 /** 89 * CSS class for the URL. 90 * 91 * @return string 92 */ 93 public function chartMenuClass(): string 94 { 95 return 'menu-chart-familybook'; 96 } 97 98 /** 99 * Return a menu item for this chart - for use in individual boxes. 100 * 101 * @param Individual $individual 102 * 103 * @return Menu|null 104 */ 105 public function chartBoxMenu(Individual $individual): ?Menu 106 { 107 return $this->chartMenu($individual); 108 } 109 110 /** 111 * The title for a specific instance of this chart. 112 * 113 * @param Individual $individual 114 * 115 * @return string 116 */ 117 public function chartTitle(Individual $individual): string 118 { 119 /* I18N: %s is an individual’s name */ 120 return I18N::translate('Family book of %s', $individual->fullName()); 121 } 122 123 /** 124 * A form to request the chart parameters. 125 * 126 * @param ServerRequestInterface $request 127 * @param Tree $tree 128 * @param UserInterface $user 129 * 130 * @return ResponseInterface 131 */ 132 public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface 133 { 134 $ajax = (bool) $request->get('ajax'); 135 $xref = $request->get('xref', ''); 136 $individual = Individual::getInstance($xref, $tree); 137 138 Auth::checkIndividualAccess($individual); 139 Auth::checkComponentAccess($this, 'chart', $tree, $user); 140 141 $show_spouse = (bool) $request->get('show_spouse'); 142 $generations = (int) $request->get('generations', self::DEFAULT_GENERATIONS); 143 $generations = min($generations, self::MAXIMUM_GENERATIONS); 144 $generations = max($generations, self::MINIMUM_GENERATIONS); 145 146 // Generations of ancestors/descendants in each mini-tree. 147 $book_size = (int) $request->get('book_size', 2); 148 $book_size = min($book_size, 5); 149 $book_size = max($book_size, 2); 150 151 if ($ajax) { 152 return $this->chart($individual, $generations, $book_size, $show_spouse); 153 } 154 155 $ajax_url = $this->chartUrl($individual, [ 156 'ajax' => true, 157 'book_size' => $book_size, 158 'generations' => $generations, 159 'show_spouse' => $show_spouse, 160 ]); 161 162 return $this->viewResponse('modules/family-book-chart/page', [ 163 'ajax_url' => $ajax_url, 164 'book_size' => $book_size, 165 'generations' => $generations, 166 'individual' => $individual, 167 'maximum_generations' => self::MAXIMUM_GENERATIONS, 168 'minimum_generations' => self::MINIMUM_GENERATIONS, 169 'module_name' => $this->name(), 170 'show_spouse' => $show_spouse, 171 'title' => $this->chartTitle($individual), 172 ]); 173 } 174 175 /** 176 * @param Individual $individual 177 * @param int $generations 178 * @param int $book_size 179 * @param bool $show_spouse 180 * 181 * @return ResponseInterface 182 */ 183 public function chart(Individual $individual, int $generations, int $book_size, bool $show_spouse): ResponseInterface 184 { 185 $this->box = (object) [ 186 'width' => app(ModuleThemeInterface::class)->parameter('chart-box-x'), 187 'height' => app(ModuleThemeInterface::class)->parameter('chart-box-y'), 188 ]; 189 190 $this->show_spouse = $show_spouse; 191 $this->descent = $generations; 192 $this->generations = $book_size; 193 194 $this->bhalfheight = $this->box->height / 2; 195 $this->dgenerations = $this->maxDescendencyGenerations($individual, 0); 196 197 if ($this->dgenerations < 1) { 198 $this->dgenerations = 1; 199 } 200 201 // @TODO - this is just a wrapper around the old code. 202 ob_start(); 203 $this->printFamilyBook($individual, $generations); 204 $html = ob_get_clean(); 205 206 return response($html); 207 } 208 209 /** 210 * Prints descendency of passed in person 211 * 212 * @param int $generation 213 * @param Individual|null $person 214 * 215 * @return float 216 */ 217 private function printDescendency($generation, Individual $person = null): float 218 { 219 if ($generation > $this->dgenerations) { 220 return 0; 221 } 222 223 echo '<table cellspacing="0" cellpadding="0" border="0" ><tr><td>'; 224 $numkids = 0.0; 225 226 // Load children 227 $children = []; 228 if ($person instanceof Individual) { 229 // Count is position from center to left, dgenerations is number of generations 230 if ($generation < $this->dgenerations) { 231 // All children, from all partners 232 foreach ($person->spouseFamilies() as $family) { 233 foreach ($family->children() as $child) { 234 $children[] = $child; 235 } 236 } 237 } 238 } 239 if ($generation < $this->dgenerations) { 240 if (!empty($children)) { 241 // real people 242 echo '<table cellspacing="0" cellpadding="0" border="0" >'; 243 foreach ($children as $i => $child) { 244 echo '<tr><td>'; 245 $kids = $this->printDescendency($generation + 1, $child); 246 $numkids += $kids; 247 echo '</td>'; 248 // Print the lines 249 if (count($children) > 1) { 250 if ($i === 0) { 251 // Adjust for the first column on left 252 $h = round((($this->box->height * $kids) + 8) / 2); // Assumes border = 1 and padding = 3 253 // Adjust for other vertical columns 254 if ($kids > 1) { 255 $h = ($kids - 1) * 4 + $h; 256 } 257 echo '<td class="align-bottom">', 258 '<img id="vline_', $child->xref(), '" src="', e(asset('css/images/vline.png')), '" width="3" height="', $h - 4, '"></td>'; 259 } elseif ($i === count($children) - 1) { 260 // Adjust for the first column on left 261 $h = round((($this->box->height * $kids) + 8) / 2); 262 // Adjust for other vertical columns 263 if ($kids > 1) { 264 $h = ($kids - 1) * 4 + $h; 265 } 266 echo '<td class="align-top">', 267 '<img class="bvertline" width="3" id="vline_', $child->xref(), '" src="', e(asset('css/images/vline.png')), '" height="', $h - 2, '"></td>'; 268 } else { 269 echo '<td class="align-bottomm"style="background: url(', e(asset('css/images/vline.png')), ');">', 270 '<img class="spacer" width="3" src="', e(asset('css/images/spacer.png')), '"></td>'; 271 } 272 } 273 echo '</tr>'; 274 } 275 echo '</table>'; 276 } else { 277 // Hidden/empty boxes - to preserve the layout 278 echo '<table cellspacing="0" cellpadding="0" border="0" ><tr><td>'; 279 $numkids += $this->printDescendency($generation + 1, null); 280 echo '</td></tr></table>'; 281 } 282 echo '</td>'; 283 echo '<td>'; 284 } 285 286 if ($numkids === 0.0) { 287 $numkids = 1; 288 } 289 echo '<table cellspacing="0" cellpadding="0" border="0" ><tr><td>'; 290 if ($person instanceof Individual) { 291 echo view('chart-box', ['individual' => $person]); 292 echo '</td><td>', 293 '<img class="linef1" src="', e(asset('css/images/hline.png')), '" width="8" height="3">'; 294 } else { 295 echo '<div style="width:', $this->box->width + 19, 'px; height:', $this->box->height + 8, 'px;"></div>', 296 '</td><td>'; 297 } 298 299 // Print the spouse 300 if ($generation === 1 && $person instanceof Individual) { 301 if ($this->show_spouse) { 302 foreach ($person->spouseFamilies() as $family) { 303 $spouse = $family->spouse($person); 304 echo '</td></tr><tr><td>'; 305 echo view('chart-box', ['individual' => $spouse]); 306 $numkids += 0.95; 307 echo '</td><td>'; 308 } 309 } 310 } 311 echo '</td></tr></table>'; 312 echo '</td></tr>'; 313 echo '</table>'; 314 315 return $numkids; 316 } 317 318 /** 319 * Prints pedigree of the person passed in 320 * 321 * @param Individual $person 322 * @param int $count 323 * 324 * @return void 325 */ 326 private function printPersonPedigree($person, $count): void 327 { 328 if ($count >= $this->generations) { 329 return; 330 } 331 332 $genoffset = $this->generations; // handle pedigree n generations lines 333 //-- calculate how tall the lines should be 334 $lh = $this->bhalfheight * (2 ** ($genoffset - $count - 1)); 335 // 336 //Prints empty table columns for children w/o parents up to the max generation 337 //This allows vertical line spacing to be consistent 338 if ($person->childFamilies()->isEmpty()) { 339 echo '<table cellspacing="0" cellpadding="0" border="0" >'; 340 echo '<div class="wt-chart-box"></div>'; 341 342 //-- recursively get the father’s family 343 $this->printPersonPedigree($person, $count + 1); 344 echo '</td><td></tr>'; 345 echo '<div class="wt-chart-box"></div>'; 346 347 //-- recursively get the mother’s family 348 $this->printPersonPedigree($person, $count + 1); 349 echo '</td><td></tr></table>'; 350 } 351 352 // Empty box section done, now for regular pedigree 353 foreach ($person->childFamilies() as $family) { 354 echo '<table cellspacing="0" cellpadding="0" border="0" ><tr><td class="align-bottom">'; 355 // Determine line height for two or more spouces 356 // And then adjust the vertical line for the root person only 357 $famcount = 0; 358 if ($this->show_spouse) { 359 // count number of spouses 360 $famcount += $person->spouseFamilies()->count(); 361 } 362 $savlh = $lh; // Save current line height 363 if ($count == 1 && $genoffset <= $famcount) { 364 $linefactor = 0; 365 // genoffset of 2 needs no adjustment 366 if ($genoffset > 2) { 367 $tblheight = $this->box->height + 8; 368 if ($genoffset == 3) { 369 if ($famcount == 3) { 370 $linefactor = $tblheight / 2; 371 } elseif ($famcount > 3) { 372 $linefactor = $tblheight; 373 } 374 } 375 if ($genoffset == 4) { 376 if ($famcount == 4) { 377 $linefactor = $tblheight; 378 } elseif ($famcount > 4) { 379 $linefactor = ($famcount - $genoffset) * ($tblheight * 1.5); 380 } 381 } 382 if ($genoffset == 5) { 383 if ($famcount == 5) { 384 $linefactor = 0; 385 } elseif ($famcount > 5) { 386 $linefactor = $tblheight * ($famcount - $genoffset); 387 } 388 } 389 } 390 $lh = (($famcount - 1) * $this->box->height - $linefactor); 391 if ($genoffset > 5) { 392 $lh = $savlh; 393 } 394 } 395 echo '<img class="line3 pvline" src="', e(asset('css/images/vline.png')), '" width="3" height="', $lh, '"></td>', 396 '<td>', 397 '<img class="linef2" src="', e(asset('css/images/hline.png')), '" height="3"></td>', 398 '<td>'; 399 $lh = $savlh; // restore original line height 400 //-- print the father box 401 echo view('chart-box', ['individual' => $family->husband()]); 402 echo '</td>'; 403 if ($family->husband()) { 404 echo '<td>'; 405 //-- recursively get the father’s family 406 $this->printPersonPedigree($family->husband(), $count + 1); 407 echo '</td>'; 408 } else { 409 echo '<td>'; 410 if ($genoffset > $count) { 411 echo '<table cellspacing="0" cellpadding="0" border="0" >'; 412 for ($i = 1; $i < ((2 ** ($genoffset - $count)) / 2); $i++) { 413 echo '<div class="wt-chart-box"></div>'; 414 echo '</tr>'; 415 } 416 echo '</table>'; 417 } 418 } 419 echo '</tr><tr>', 420 '<td class="align-top"><img class="pvline" alt="" role="presentation" src="', e(asset('css/images/vline.png')), '" width="3" height="', $lh, '"></td>', 421 '<td><img class="linef3" alt="" role="presentation" src="', e(asset('css/images/hline.png')), '" height="3"></td>', 422 '<td>'; 423 //-- print the mother box 424 echo view('chart-box', ['individual' => $family->wife()]); 425 echo '</td>'; 426 if ($family->wife()) { 427 echo '<td>'; 428 //-- recursively print the mother’s family 429 $this->printPersonPedigree($family->wife(), $count + 1); 430 echo '</td>'; 431 } else { 432 echo '<td>'; 433 if ($count < $genoffset - 1) { 434 echo '<table cellspacing="0" cellpadding="0" border="0" >'; 435 for ($i = 1; $i < ((2 ** (($genoffset - 1) - $count)) / 2) + 1; $i++) { 436 echo '<div class="wt-chart-box"></div>'; 437 echo '</tr>'; 438 echo '<div class="wt-chart-box"></div>'; 439 echo '</tr>'; 440 } 441 echo '</table>'; 442 } 443 } 444 echo '</tr>', 445 '</table>'; 446 break; 447 } 448 } 449 450 /** 451 * Calculates number of generations a person has 452 * 453 * @param Individual $individual 454 * @param int $depth 455 * 456 * @return int 457 */ 458 private function maxDescendencyGenerations(Individual $individual, $depth): int 459 { 460 if ($depth > $this->generations) { 461 return $depth; 462 } 463 $maxdc = $depth; 464 foreach ($individual->spouseFamilies() as $family) { 465 foreach ($family->children() as $child) { 466 $dc = $this->maxDescendencyGenerations($child, $depth + 1); 467 if ($dc >= $this->generations) { 468 return $dc; 469 } 470 if ($dc > $maxdc) { 471 $maxdc = $dc; 472 } 473 } 474 } 475 $maxdc++; 476 if ($maxdc == 1) { 477 $maxdc++; 478 } 479 480 return $maxdc; 481 } 482 483 /** 484 * Print a “Family Book” for an individual 485 * 486 * @param Individual $person 487 * @param int $descent_steps 488 * 489 * @return void 490 */ 491 private function printFamilyBook(Individual $person, $descent_steps): void 492 { 493 if ($descent_steps == 0) { 494 return; 495 } 496 497 echo 498 '<h3>', 499 /* I18N: %s is an individual’s name */ 500 I18N::translate('Family of %s', $person->fullName()), 501 '</h3>', 502 '<table cellspacing="0" cellpadding="0" border="0" ><tr><td class="align-middle">'; 503 $this->dgenerations = $this->generations; 504 $this->printDescendency(1, $person); 505 echo '</td><td class="align-middle">'; 506 $this->printPersonPedigree($person, 1); 507 echo '</td></tr></table><br><br><hr class="wt-family-break"><br><br>'; 508 foreach ($person->spouseFamilies() as $family) { 509 foreach ($family->children() as $child) { 510 $this->printFamilyBook($child, $descent_steps - 1); 511 } 512 } 513 } 514} 515