1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Closure; 23use Fig\Http\Message\RequestMethodInterface; 24use Fisharebest\Algorithm\Dijkstra; 25use Fisharebest\Webtrees\Auth; 26use Fisharebest\Webtrees\Contracts\UserInterface; 27use Fisharebest\Webtrees\DB; 28use Fisharebest\Webtrees\FlashMessages; 29use Fisharebest\Webtrees\GedcomRecord; 30use Fisharebest\Webtrees\I18N; 31use Fisharebest\Webtrees\Individual; 32use Fisharebest\Webtrees\Menu; 33use Fisharebest\Webtrees\Registry; 34use Fisharebest\Webtrees\Services\RelationshipService; 35use Fisharebest\Webtrees\Services\TreeService; 36use Fisharebest\Webtrees\Tree; 37use Fisharebest\Webtrees\Validator; 38use Illuminate\Database\Query\JoinClause; 39use Illuminate\Support\Collection; 40use Psr\Http\Message\ResponseInterface; 41use Psr\Http\Message\ServerRequestInterface; 42use Psr\Http\Server\RequestHandlerInterface; 43 44use function array_map; 45use function asset; 46use function count; 47use function current; 48use function e; 49use function implode; 50use function in_array; 51use function max; 52use function min; 53use function next; 54use function ob_get_clean; 55use function ob_start; 56use function preg_match; 57use function redirect; 58use function response; 59use function route; 60use function sort; 61use function view; 62 63/** 64 * Class RelationshipsChartModule 65 */ 66class RelationshipsChartModule extends AbstractModule implements ModuleChartInterface, ModuleConfigInterface, RequestHandlerInterface 67{ 68 use ModuleChartTrait; 69 use ModuleConfigTrait; 70 71 protected const ROUTE_URL = '/tree/{tree}/relationships-{ancestors}-{recursion}/{xref}{/xref2}'; 72 73 /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */ 74 public const UNLIMITED_RECURSION = 99; 75 76 /** By default new trees allow unlimited recursion */ 77 public const DEFAULT_RECURSION = '99'; 78 79 /** By default new trees search for all relationships (not via ancestors) */ 80 public const DEFAULT_ANCESTORS = '0'; 81 public const DEFAULT_PARAMETERS = [ 82 'ancestors' => self::DEFAULT_ANCESTORS, 83 'recursion' => self::DEFAULT_RECURSION, 84 ]; 85 86 private TreeService $tree_service; 87 88 private RelationshipService $relationship_service; 89 90 /** 91 * @param RelationshipService $relationship_service 92 * @param TreeService $tree_service 93 */ 94 public function __construct(RelationshipService $relationship_service, TreeService $tree_service) 95 { 96 $this->relationship_service = $relationship_service; 97 $this->tree_service = $tree_service; 98 } 99 100 /** 101 * Initialization. 102 * 103 * @return void 104 */ 105 public function boot(): void 106 { 107 Registry::routeFactory()->routeMap() 108 ->get(static::class, static::ROUTE_URL, $this) 109 ->allows(RequestMethodInterface::METHOD_POST) 110 ->tokens([ 111 'ancestors' => '\d+', 112 'recursion' => '\d+', 113 ]); 114 } 115 116 public function description(): string 117 { 118 /* I18N: Description of the “RelationshipsChart” module */ 119 return I18N::translate('A chart displaying relationships between two individuals.'); 120 } 121 122 /** 123 * Return a menu item for this chart - for use in individual boxes. 124 * 125 * @param Individual $individual 126 * 127 * @return Menu|null 128 */ 129 public function chartBoxMenu(Individual $individual): Menu|null 130 { 131 return $this->chartMenu($individual); 132 } 133 134 /** 135 * A main menu item for this chart. 136 * 137 * @param Individual $individual 138 * 139 * @return Menu 140 */ 141 public function chartMenu(Individual $individual): Menu 142 { 143 $my_xref = $individual->tree()->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 144 145 if ($my_xref !== '' && $my_xref !== $individual->xref()) { 146 $my_record = Registry::individualFactory()->make($my_xref, $individual->tree()); 147 148 if ($my_record instanceof Individual) { 149 return new Menu( 150 I18N::translate('Relationship to me'), 151 $this->chartUrl($my_record, ['xref2' => $individual->xref()]), 152 $this->chartMenuClass(), 153 $this->chartUrlAttributes() 154 ); 155 } 156 } 157 158 return new Menu( 159 $this->title(), 160 $this->chartUrl($individual), 161 $this->chartMenuClass(), 162 $this->chartUrlAttributes() 163 ); 164 } 165 166 /** 167 * CSS class for the URL. 168 * 169 * @return string 170 */ 171 public function chartMenuClass(): string 172 { 173 return 'menu-chart-relationship'; 174 } 175 176 /** 177 * How should this module be identified in the control panel, etc.? 178 * 179 * @return string 180 */ 181 public function title(): string 182 { 183 /* I18N: Name of a module/chart */ 184 return I18N::translate('Relationships'); 185 } 186 187 /** 188 * The URL for a page showing chart options. 189 * 190 * @param Individual $individual 191 * @param array<bool|int|string|array<string>|null> $parameters 192 * 193 * @return string 194 */ 195 public function chartUrl(Individual $individual, array $parameters = []): string 196 { 197 return route(static::class, [ 198 'xref' => $individual->xref(), 199 'tree' => $individual->tree()->name(), 200 ] + $parameters + self::DEFAULT_PARAMETERS); 201 } 202 203 /** 204 * @param ServerRequestInterface $request 205 * 206 * @return ResponseInterface 207 */ 208 public function handle(ServerRequestInterface $request): ResponseInterface 209 { 210 $tree = Validator::attributes($request)->tree(); 211 $xref = Validator::attributes($request)->isXref()->string('xref'); 212 $xref2 = Validator::attributes($request)->isXref()->string('xref2', ''); 213 $ajax = Validator::queryParams($request)->boolean('ajax', false); 214 $ancestors = (int) $request->getAttribute('ancestors'); 215 $recursion = (int) $request->getAttribute('recursion'); 216 $user = Validator::attributes($request)->user(); 217 218 // Convert POST requests into GET requests for pretty URLs. 219 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 220 return redirect(route(static::class, [ 221 'tree' => $tree->name(), 222 'ancestors' => Validator::parsedBody($request)->string('ancestors', ''), 223 'recursion' => Validator::parsedBody($request)->string('recursion', ''), 224 'xref' => Validator::parsedBody($request)->string('xref', ''), 225 'xref2' => Validator::parsedBody($request)->string('xref2', ''), 226 ])); 227 } 228 229 $individual1 = Registry::individualFactory()->make($xref, $tree); 230 $individual2 = Registry::individualFactory()->make($xref2, $tree); 231 232 $ancestors_only = (int) $tree->getPreference('RELATIONSHIP_ANCESTORS', static::DEFAULT_ANCESTORS); 233 $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION); 234 235 $recursion = min($recursion, $max_recursion); 236 237 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 238 239 if ($individual1 instanceof Individual) { 240 $individual1 = Auth::checkIndividualAccess($individual1, false, true); 241 } 242 243 if ($individual2 instanceof Individual) { 244 $individual2 = Auth::checkIndividualAccess($individual2, false, true); 245 } 246 247 if ($individual1 instanceof Individual && $individual2 instanceof Individual) { 248 if ($ajax) { 249 return $this->chart($individual1, $individual2, $recursion, $ancestors); 250 } 251 252 /* I18N: %s are individual’s names */ 253 $title = I18N::translate('Relationships between %1$s and %2$s', $individual1->fullName(), $individual2->fullName()); 254 $ajax_url = $this->chartUrl($individual1, [ 255 'ajax' => true, 256 'ancestors' => $ancestors, 257 'recursion' => $recursion, 258 'xref2' => $individual2->xref(), 259 ]); 260 } else { 261 $title = I18N::translate('Relationships'); 262 $ajax_url = ''; 263 } 264 265 return $this->viewResponse('modules/relationships-chart/page', [ 266 'ajax_url' => $ajax_url, 267 'ancestors' => $ancestors, 268 'ancestors_only' => $ancestors_only, 269 'ancestors_options' => $this->ancestorsOptions(), 270 'individual1' => $individual1, 271 'individual2' => $individual2, 272 'max_recursion' => $max_recursion, 273 'module' => $this->name(), 274 'recursion' => $recursion, 275 'recursion_options' => $this->recursionOptions($max_recursion), 276 'title' => $title, 277 'tree' => $tree, 278 ]); 279 } 280 281 /** 282 * @param Individual $individual1 283 * @param Individual $individual2 284 * @param int $recursion 285 * @param int $ancestors 286 * 287 * @return ResponseInterface 288 */ 289 public function chart(Individual $individual1, Individual $individual2, int $recursion, int $ancestors): ResponseInterface 290 { 291 $tree = $individual1->tree(); 292 293 $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION); 294 295 $recursion = min($recursion, $max_recursion); 296 297 $paths = $this->calculateRelationships($individual1, $individual2, $recursion, (bool) $ancestors); 298 299 ob_start(); 300 if (I18N::direction() === 'ltr') { 301 $diagonal1 = asset('css/images/dline.png'); 302 $diagonal2 = asset('css/images/dline2.png'); 303 } else { 304 $diagonal1 = asset('css/images/dline2.png'); 305 $diagonal2 = asset('css/images/dline.png'); 306 } 307 308 $num_paths = 0; 309 foreach ($paths as $path) { 310 // Extract the relationship names between pairs of individuals 311 $relationships = $this->oldStyleRelationshipPath($tree, $path); 312 if ($relationships === []) { 313 // Cannot see one of the families/individuals, due to privacy; 314 continue; 315 } 316 317 $nodes = Collection::make($path) 318 ->map(static function (string $xref, int $key) use ($tree): GedcomRecord { 319 if ($key % 2 === 0) { 320 return Registry::individualFactory()->make($xref, $tree); 321 } 322 323 return Registry::familyFactory()->make($xref, $tree); 324 }); 325 326 $relationship = $this->relationship_service->nameFromPath($nodes->all(), I18N::language()); 327 328 echo '<h3>', I18N::translate('Relationship: %s', $relationship), '</h3>'; 329 330 $num_paths++; 331 332 // Use a table/grid for layout. 333 $table = []; 334 // Current position in the grid. 335 $x = 0; 336 $y = 0; 337 // Extent of the grid. 338 $min_y = 0; 339 $max_y = 0; 340 $max_x = 0; 341 // For each node in the path. 342 foreach ($path as $n => $xref) { 343 if ($n % 2 === 1) { 344 switch ($relationships[$n]) { 345 case 'hus': 346 case 'wif': 347 case 'spo': 348 case 'bro': 349 case 'sis': 350 case 'sib': 351 $table[$x + 1][$y] = '<div style="background:url(' . e(asset('css/images/hline.png')) . ') repeat-x center; width: 94px; text-align: center"><div style="height: 32px;">' . $this->relationship_service->legacyNameAlgorithm($relationships[$n], Registry::individualFactory()->make($path[$n - 1], $tree), Registry::individualFactory()->make($path[$n + 1], $tree)) . '</div><div style="height: 32px;">' . view('icons/arrow-right') . '</div></div>'; 352 $x += 2; 353 break; 354 case 'son': 355 case 'dau': 356 case 'chi': 357 if ($n > 2 && preg_match('/fat|mot|par/', $relationships[$n - 2])) { 358 $table[$x + 1][$y - 1] = '<div style="background:url(' . $diagonal2 . '); width: 64px; height: 64px; text-align: center;"><div style="height: 32px; text-align: end;">' . $this->relationship_service->legacyNameAlgorithm($relationships[$n], Registry::individualFactory()->make($path[$n - 1], $tree), Registry::individualFactory()->make($path[$n + 1], $tree)) . '</div><div style="height: 32px; text-align: start;">' . view('icons/arrow-down') . '</div></div>'; 359 $x += 2; 360 } else { 361 $table[$x][$y - 1] = '<div style="background:url(' . e('"' . asset('css/images/vline.png') . '"') . ') repeat-y center; height: 64px; text-align: center;"><div style="display: inline-block; width:50%; line-height: 64px;">' . $this->relationship_service->legacyNameAlgorithm($relationships[$n], Registry::individualFactory()->make($path[$n - 1], $tree), Registry::individualFactory()->make($path[$n + 1], $tree)) . '</div><div style="display: inline-block; width:50%; line-height: 64px;">' . view('icons/arrow-down') . '</div></div>'; 362 } 363 $y -= 2; 364 break; 365 case 'fat': 366 case 'mot': 367 case 'par': 368 if ($n > 2 && preg_match('/son|dau|chi/', $relationships[$n - 2])) { 369 $table[$x + 1][$y + 1] = '<div style="background:url(' . $diagonal1 . '); background-position: top right; width: 64px; height: 64px; text-align: center;"><div style="height: 32px; text-align: start;">' . $this->relationship_service->legacyNameAlgorithm($relationships[$n], Registry::individualFactory()->make($path[$n - 1], $tree), Registry::individualFactory()->make($path[$n + 1], $tree)) . '</div><div style="height: 32px; text-align: end;">' . view('icons/arrow-down') . '</div></div>'; 370 $x += 2; 371 } else { 372 $table[$x][$y + 1] = '<div style="background:url(' . e('"' . asset('css/images/vline.png') . '"') . ') repeat-y center; height: 64px; text-align:center; "><div style="display: inline-block; width: 50%; line-height: 64px;">' . $this->relationship_service->legacyNameAlgorithm($relationships[$n], Registry::individualFactory()->make($path[$n - 1], $tree), Registry::individualFactory()->make($path[$n + 1], $tree)) . '</div><div style="display: inline-block; width: 50%; line-height: 32px">' . view('icons/arrow-up') . '</div></div>'; 373 } 374 $y += 2; 375 break; 376 } 377 $max_x = max($max_x, $x); 378 $min_y = min($min_y, $y); 379 $max_y = max($max_y, $y); 380 } else { 381 $individual = Registry::individualFactory()->make($xref, $tree); 382 $table[$x][$y] = view('chart-box', ['individual' => $individual]); 383 } 384 } 385 echo '<div class="wt-chart wt-chart-relationships">'; 386 echo '<table style="border-collapse: collapse; margin: 20px 50px;">'; 387 for ($y = $max_y; $y >= $min_y; --$y) { 388 echo '<tr>'; 389 for ($x = 0; $x <= $max_x; ++$x) { 390 echo '<td style="padding: 0;">'; 391 if (isset($table[$x][$y])) { 392 echo $table[$x][$y]; 393 } 394 echo '</td>'; 395 } 396 echo '</tr>'; 397 } 398 echo '</table>'; 399 echo '</div>'; 400 } 401 402 if (!$num_paths) { 403 echo '<p>', I18N::translate('No link between the two individuals could be found.'), '</p>'; 404 } 405 406 $html = ob_get_clean(); 407 408 return response($html); 409 } 410 411 /** 412 * @param ServerRequestInterface $request 413 * 414 * @return ResponseInterface 415 */ 416 public function getAdminAction(ServerRequestInterface $request): ResponseInterface 417 { 418 $this->layout = 'layouts/administration'; 419 420 return $this->viewResponse('modules/relationships-chart/config', [ 421 'all_trees' => $this->tree_service->all(), 422 'ancestors_options' => $this->ancestorsOptions(), 423 'default_ancestors' => self::DEFAULT_ANCESTORS, 424 'default_recursion' => self::DEFAULT_RECURSION, 425 'recursion_options' => $this->recursionConfigOptions(), 426 'title' => I18N::translate('Chart preferences') . ' — ' . $this->title(), 427 ]); 428 } 429 430 /** 431 * @param ServerRequestInterface $request 432 * 433 * @return ResponseInterface 434 */ 435 public function postAdminAction(ServerRequestInterface $request): ResponseInterface 436 { 437 foreach ($this->tree_service->all() as $tree) { 438 $recursion = Validator::parsedBody($request)->integer('relationship-recursion-' . $tree->id()); 439 $ancestors = Validator::parsedBody($request)->string('relationship-ancestors-' . $tree->id()); 440 441 $tree->setPreference('RELATIONSHIP_RECURSION', (string) $recursion); 442 $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors); 443 } 444 445 FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); 446 447 return redirect($this->getConfigLink()); 448 } 449 450 /** 451 * Possible options for the ancestors option 452 * 453 * @return array<int,string> 454 */ 455 private function ancestorsOptions(): array 456 { 457 return [ 458 0 => I18N::translate('Find any relationship'), 459 1 => I18N::translate('Find relationships via ancestors'), 460 ]; 461 } 462 463 /** 464 * Possible options for the recursion option 465 * 466 * @return array<int,string> 467 */ 468 private function recursionConfigOptions(): array 469 { 470 return [ 471 0 => I18N::translate('none'), 472 1 => I18N::number(1), 473 2 => I18N::number(2), 474 3 => I18N::number(3), 475 self::UNLIMITED_RECURSION => I18N::translate('unlimited'), 476 ]; 477 } 478 479 /** 480 * Calculate the shortest paths - or all paths - between two individuals. 481 * 482 * @param Individual $individual1 483 * @param Individual $individual2 484 * @param int $recursion How many levels of recursion to use 485 * @param bool $ancestor Restrict to relationships via a common ancestor 486 * 487 * @return array<array<string>> 488 */ 489 private function calculateRelationships( 490 Individual $individual1, 491 Individual $individual2, 492 int $recursion, 493 bool $ancestor = false 494 ): array { 495 $tree = $individual1->tree(); 496 497 $rows = DB::table('link') 498 ->where('l_file', '=', $tree->id()) 499 ->whereIn('l_type', ['FAMS', 'FAMC']) 500 ->select(['l_from', 'l_to']) 501 ->get(); 502 503 // Optionally restrict the graph to the ancestors of the individuals. 504 if ($ancestor) { 505 $ancestors = $this->allAncestors($individual1->xref(), $individual2->xref(), $tree->id()); 506 $exclude = $this->excludeFamilies($individual1->xref(), $individual2->xref(), $tree->id()); 507 } else { 508 $ancestors = []; 509 $exclude = []; 510 } 511 512 $graph = []; 513 514 foreach ($rows as $row) { 515 if ($ancestors === [] || in_array($row->l_from, $ancestors, true) && !in_array($row->l_to, $exclude, true)) { 516 $graph[$row->l_from][$row->l_to] = 1; 517 $graph[$row->l_to][$row->l_from] = 1; 518 } 519 } 520 521 $xref1 = $individual1->xref(); 522 $xref2 = $individual2->xref(); 523 $dijkstra = new Dijkstra($graph); 524 $paths = $dijkstra->shortestPaths($xref1, $xref2); 525 526 // Only process each exclusion list once; 527 $excluded = []; 528 529 $queue = []; 530 foreach ($paths as $path) { 531 // Insert the paths into the queue, with an exclusion list. 532 $queue[] = [ 533 'path' => $path, 534 'exclude' => [], 535 ]; 536 // While there are un-extended paths 537 for ($next = current($queue); $next !== false; $next = next($queue)) { 538 // For each family on the path 539 for ($n = count($next['path']) - 2; $n >= 1; $n -= 2) { 540 $exclude = $next['exclude']; 541 if (count($exclude) >= $recursion) { 542 continue; 543 } 544 $exclude[] = $next['path'][$n]; 545 sort($exclude); 546 $tmp = implode('-', $exclude); 547 if (in_array($tmp, $excluded, true)) { 548 continue; 549 } 550 551 $excluded[] = $tmp; 552 // Add any new path to the queue 553 foreach ($dijkstra->shortestPaths($xref1, $xref2, $exclude) as $new_path) { 554 $queue[] = [ 555 'path' => $new_path, 556 'exclude' => $exclude, 557 ]; 558 } 559 } 560 } 561 } 562 // Extract the paths from the queue. 563 $paths = []; 564 foreach ($queue as $next) { 565 // The Dijkstra library does not use strict types, and converts 566 // numeric array keys (XREFs) from strings to integers; 567 $path = array_map($this->stringMapper(), $next['path']); 568 569 // Remove duplicates 570 $paths[implode('-', $next['path'])] = $path; 571 } 572 573 return $paths; 574 } 575 576 /** 577 * Convert numeric values to strings 578 * 579 * @return Closure(int|string):string 580 */ 581 private function stringMapper(): Closure 582 { 583 return static fn ($xref) => (string) $xref; 584 } 585 586 /** 587 * Find all ancestors of a list of individuals 588 * 589 * @param string $xref1 590 * @param string $xref2 591 * @param int $tree_id 592 * 593 * @return array<string> 594 */ 595 private function allAncestors(string $xref1, string $xref2, int $tree_id): array 596 { 597 $ancestors = [ 598 $xref1, 599 $xref2, 600 ]; 601 602 $queue = [ 603 $xref1, 604 $xref2, 605 ]; 606 while ($queue !== []) { 607 $parents = DB::table('link AS l1') 608 ->join('link AS l2', static function (JoinClause $join): void { 609 $join 610 ->on('l1.l_to', '=', 'l2.l_to') 611 ->on('l1.l_file', '=', 'l2.l_file'); 612 }) 613 ->where('l1.l_file', '=', $tree_id) 614 ->where('l1.l_type', '=', 'FAMC') 615 ->where('l2.l_type', '=', 'FAMS') 616 ->whereIn('l1.l_from', $queue) 617 ->pluck('l2.l_from'); 618 619 $queue = []; 620 foreach ($parents as $parent) { 621 if (!in_array($parent, $ancestors, true)) { 622 $ancestors[] = $parent; 623 $queue[] = $parent; 624 } 625 } 626 } 627 628 return $ancestors; 629 } 630 631 /** 632 * Find all families of two individuals 633 * 634 * @param string $xref1 635 * @param string $xref2 636 * @param int $tree_id 637 * 638 * @return array<string> 639 */ 640 private function excludeFamilies(string $xref1, string $xref2, int $tree_id): array 641 { 642 return DB::table('link AS l1') 643 ->join('link AS l2', static function (JoinClause $join): void { 644 $join 645 ->on('l1.l_to', '=', 'l2.l_to') 646 ->on('l1.l_type', '=', 'l2.l_type') 647 ->on('l1.l_file', '=', 'l2.l_file'); 648 }) 649 ->where('l1.l_file', '=', $tree_id) 650 ->where('l1.l_type', '=', 'FAMS') 651 ->where('l1.l_from', '=', $xref1) 652 ->where('l2.l_from', '=', $xref2) 653 ->pluck('l1.l_to') 654 ->all(); 655 } 656 657 /** 658 * Convert a path (list of XREFs) to an "old-style" string of relationships. 659 * Return an empty array, if privacy rules prevent us viewing any node. 660 * 661 * @param Tree $tree 662 * @param array<string> $path Alternately Individual / Family 663 * 664 * @return array<string> 665 */ 666 private function oldStyleRelationshipPath(Tree $tree, array $path): array 667 { 668 $spouse_codes = [ 669 'M' => 'hus', 670 'F' => 'wif', 671 'U' => 'spo', 672 ]; 673 $parent_codes = [ 674 'M' => 'fat', 675 'F' => 'mot', 676 'U' => 'par', 677 ]; 678 $child_codes = [ 679 'M' => 'son', 680 'F' => 'dau', 681 'U' => 'chi', 682 ]; 683 $sibling_codes = [ 684 'M' => 'bro', 685 'F' => 'sis', 686 'U' => 'sib', 687 ]; 688 $relationships = []; 689 690 for ($i = 1, $count = count($path); $i < $count; $i += 2) { 691 $family = Registry::familyFactory()->make($path[$i], $tree); 692 $prev = Registry::individualFactory()->make($path[$i - 1], $tree); 693 $next = Registry::individualFactory()->make($path[$i + 1], $tree); 694 if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $prev->xref() . '@/', $family->gedcom(), $match)) { 695 $rel1 = $match[1]; 696 } else { 697 return []; 698 } 699 if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $next->xref() . '@/', $family->gedcom(), $match)) { 700 $rel2 = $match[1]; 701 } else { 702 return []; 703 } 704 if (($rel1 === 'HUSB' || $rel1 === 'WIFE') && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) { 705 $relationships[$i] = $spouse_codes[$next->sex()] ?? $spouse_codes['U']; 706 } elseif (($rel1 === 'HUSB' || $rel1 === 'WIFE') && $rel2 === 'CHIL') { 707 $relationships[$i] = $child_codes[$next->sex()] ?? $child_codes['U']; 708 } elseif ($rel1 === 'CHIL' && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) { 709 $relationships[$i] = $parent_codes[$next->sex()] ?? $parent_codes['U']; 710 } elseif ($rel1 === 'CHIL' && $rel2 === 'CHIL') { 711 $relationships[$i] = $sibling_codes[$next->sex()] ?? $sibling_codes['U']; 712 } 713 } 714 715 return $relationships; 716 } 717 718 /** 719 * Possible options for the recursion option 720 * 721 * @param int $max_recursion 722 * 723 * @return array<string> 724 */ 725 private function recursionOptions(int $max_recursion): array 726 { 727 if ($max_recursion === static::UNLIMITED_RECURSION) { 728 $text = I18N::translate('Find all possible relationships'); 729 } else { 730 $text = I18N::translate('Find other relationships'); 731 } 732 733 return [ 734 '0' => I18N::translate('Find the closest relationships'), 735 $max_recursion => $text, 736 ]; 737 } 738} 739