1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 5*1fe542e9SGreg Roach * Copyright (C) 2021 webtrees development team 6168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify 7168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by 8168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or 9168ff6f3Sric2016 * (at your option) any later version. 10168ff6f3Sric2016 * This program is distributed in the hope that it will be useful, 11168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13168ff6f3Sric2016 * GNU General Public License for more details. 14168ff6f3Sric2016 * You should have received a copy of the GNU General Public License 15168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16168ff6f3Sric2016 */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module; 21168ff6f3Sric2016 223cfcc809SGreg Roachuse Aura\Router\RouterContainer; 23185cbb4dSGreg Roachuse Closure; 243cfcc809SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 259b5537c3SGreg Roachuse Fisharebest\Algorithm\Dijkstra; 26168ff6f3Sric2016use Fisharebest\Webtrees\Auth; 27*1fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 2945ac604bSGreg Roachuse Fisharebest\Webtrees\FlashMessages; 309b5537c3SGreg Roachuse Fisharebest\Webtrees\Functions\Functions; 31168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 32168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 331e3273c9SGreg Roachuse Fisharebest\Webtrees\Menu; 343df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 3545ac604bSGreg Roachuse Fisharebest\Webtrees\Tree; 369b5537c3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 379b5537c3SGreg Roachuse Illuminate\Database\Query\JoinClause; 386ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 396ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 403cfcc809SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 413976b470SGreg Roach 429e18e23bSGreg Roachuse function app; 439e18e23bSGreg Roachuse function assert; 44ddeb3354SGreg Roachuse function is_string; 453cfcc809SGreg Roachuse function redirect; 463cfcc809SGreg Roachuse function route; 47f4ba05e3SGreg Roachuse function view; 48168ff6f3Sric2016 49168ff6f3Sric2016/** 50168ff6f3Sric2016 * Class RelationshipsChartModule 51168ff6f3Sric2016 */ 527a1b7425SGreg Roachclass RelationshipsChartModule extends AbstractModule implements ModuleChartInterface, ModuleConfigInterface, RequestHandlerInterface 53c1010edaSGreg Roach{ 5449a243cbSGreg Roach use ModuleChartTrait; 5549a243cbSGreg Roach use ModuleConfigTrait; 5649a243cbSGreg Roach 5772f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/relationships-{ancestors}-{recursion}/{xref}{/xref2}'; 583cfcc809SGreg Roach 591e3273c9SGreg Roach /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */ 6016d6367aSGreg Roach public const UNLIMITED_RECURSION = 99; 611e3273c9SGreg Roach 621e3273c9SGreg Roach /** By default new trees allow unlimited recursion */ 6316d6367aSGreg Roach public const DEFAULT_RECURSION = '99'; 6445ac604bSGreg Roach 65e0bd7dc9SGreg Roach /** By default new trees search for all relationships (not via ancestors) */ 6616d6367aSGreg Roach public const DEFAULT_ANCESTORS = '0'; 673cfcc809SGreg Roach public const DEFAULT_PARAMETERS = [ 683cfcc809SGreg Roach 'ancestors' => self::DEFAULT_ANCESTORS, 693cfcc809SGreg Roach 'recursion' => self::DEFAULT_RECURSION, 703cfcc809SGreg Roach ]; 713cfcc809SGreg Roach 723df1e584SGreg Roach /** @var TreeService */ 733df1e584SGreg Roach private $tree_service; 743df1e584SGreg Roach 753df1e584SGreg Roach /** 763df1e584SGreg Roach * @param TreeService $tree_service 773df1e584SGreg Roach */ 783df1e584SGreg Roach public function __construct(TreeService $tree_service) 793df1e584SGreg Roach { 803df1e584SGreg Roach $this->tree_service = $tree_service; 813df1e584SGreg Roach } 823df1e584SGreg Roach 833cfcc809SGreg Roach /** 843cfcc809SGreg Roach * Initialization. 853cfcc809SGreg Roach * 869e18e23bSGreg Roach * @return void 873cfcc809SGreg Roach */ 889e18e23bSGreg Roach public function boot(): void 893cfcc809SGreg Roach { 909e18e23bSGreg Roach $router_container = app(RouterContainer::class); 919e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 929e18e23bSGreg Roach 933cfcc809SGreg Roach $router_container->getMap() 9472f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 953cfcc809SGreg Roach ->allows(RequestMethodInterface::METHOD_POST) 963cfcc809SGreg Roach ->tokens([ 973cfcc809SGreg Roach 'ancestors' => '\d+', 983cfcc809SGreg Roach 'recursion' => '\d+', 993cfcc809SGreg Roach ]); 1003cfcc809SGreg Roach } 101e0bd7dc9SGreg Roach 102168ff6f3Sric2016 /** 103168ff6f3Sric2016 * A sentence describing what this module does. 104168ff6f3Sric2016 * 105168ff6f3Sric2016 * @return string 106168ff6f3Sric2016 */ 10749a243cbSGreg Roach public function description(): string 108c1010edaSGreg Roach { 109bbb76c12SGreg Roach /* I18N: Description of the “RelationshipsChart” module */ 110bbb76c12SGreg Roach return I18N::translate('A chart displaying relationships between two individuals.'); 111168ff6f3Sric2016 } 112168ff6f3Sric2016 113168ff6f3Sric2016 /** 1146ccdf4f0SGreg Roach * Return a menu item for this chart - for use in individual boxes. 1156ccdf4f0SGreg Roach * 1166ccdf4f0SGreg Roach * @param Individual $individual 1176ccdf4f0SGreg Roach * 1186ccdf4f0SGreg Roach * @return Menu|null 1196ccdf4f0SGreg Roach */ 1206ccdf4f0SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 1216ccdf4f0SGreg Roach { 1226ccdf4f0SGreg Roach return $this->chartMenu($individual); 1236ccdf4f0SGreg Roach } 1246ccdf4f0SGreg Roach 1256ccdf4f0SGreg Roach /** 126e6562982SGreg Roach * A main menu item for this chart. 127168ff6f3Sric2016 * 1288e69695bSGreg Roach * @param Individual $individual 1298e69695bSGreg Roach * 130e6562982SGreg Roach * @return Menu 131168ff6f3Sric2016 */ 132e6562982SGreg Roach public function chartMenu(Individual $individual): Menu 133c1010edaSGreg Roach { 134*1fe542e9SGreg Roach $gedcomid = $individual->tree()->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 135168ff6f3Sric2016 1363dcc812bSGreg Roach if ($gedcomid !== '' && $gedcomid !== $individual->xref()) { 137168ff6f3Sric2016 return new Menu( 138168ff6f3Sric2016 I18N::translate('Relationship to me'), 139e6562982SGreg Roach $this->chartUrl($individual, ['xref2' => $gedcomid]), 140377a2979SGreg Roach $this->chartMenuClass(), 141e6562982SGreg Roach $this->chartUrlAttributes() 142168ff6f3Sric2016 ); 143b2ce94c6SRico Sonntag } 144b2ce94c6SRico Sonntag 145168ff6f3Sric2016 return new Menu( 146e6562982SGreg Roach $this->title(), 147e6562982SGreg Roach $this->chartUrl($individual), 148377a2979SGreg Roach $this->chartMenuClass(), 149e6562982SGreg Roach $this->chartUrlAttributes() 150168ff6f3Sric2016 ); 151168ff6f3Sric2016 } 152168ff6f3Sric2016 1534eb71cfaSGreg Roach /** 154377a2979SGreg Roach * CSS class for the URL. 155377a2979SGreg Roach * 156377a2979SGreg Roach * @return string 157377a2979SGreg Roach */ 158377a2979SGreg Roach public function chartMenuClass(): string 159377a2979SGreg Roach { 160377a2979SGreg Roach return 'menu-chart-relationship'; 161377a2979SGreg Roach } 162377a2979SGreg Roach 163377a2979SGreg Roach /** 1646ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 1654eb71cfaSGreg Roach * 1666ccdf4f0SGreg Roach * @return string 1674eb71cfaSGreg Roach */ 1686ccdf4f0SGreg Roach public function title(): string 169c1010edaSGreg Roach { 1706ccdf4f0SGreg Roach /* I18N: Name of a module/chart */ 1716ccdf4f0SGreg Roach return I18N::translate('Relationships'); 172e6562982SGreg Roach } 173e6562982SGreg Roach 174e6562982SGreg Roach /** 1753cfcc809SGreg Roach * The URL for a page showing chart options. 17657ab2231SGreg Roach * 1773cfcc809SGreg Roach * @param Individual $individual 17859597b37SGreg Roach * @param mixed[] $parameters 17918d7a90dSGreg Roach * 1803cfcc809SGreg Roach * @return string 181e0bd7dc9SGreg Roach */ 1823cfcc809SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 183c1010edaSGreg Roach { 18472f04adfSGreg Roach return route(static::class, [ 1853cfcc809SGreg Roach 'xref' => $individual->xref(), 1863cfcc809SGreg Roach 'tree' => $individual->tree()->name(), 1873cfcc809SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 1881e3273c9SGreg Roach } 1899b5537c3SGreg Roach 1909b5537c3SGreg Roach /** 1916ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1926ccdf4f0SGreg Roach * 1936ccdf4f0SGreg Roach * @return ResponseInterface 1946ccdf4f0SGreg Roach */ 1953cfcc809SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 1966ccdf4f0SGreg Roach { 1974ea62551SGreg Roach $tree = $request->getAttribute('tree'); 1984ea62551SGreg Roach assert($tree instanceof Tree); 1994ea62551SGreg Roach 200ddeb3354SGreg Roach $xref = $request->getAttribute('xref'); 201ddeb3354SGreg Roach assert(is_string($xref)); 202ddeb3354SGreg Roach 203ddeb3354SGreg Roach $xref2 = $request->getAttribute('xref2') ?? ''; 204ddeb3354SGreg Roach 2053cfcc809SGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 2063cfcc809SGreg Roach $ancestors = (int) $request->getAttribute('ancestors'); 2073cfcc809SGreg Roach $recursion = (int) $request->getAttribute('recursion'); 20857ab2231SGreg Roach $user = $request->getAttribute('user'); 2099b5537c3SGreg Roach 2103cfcc809SGreg Roach // Convert POST requests into GET requests for pretty URLs. 2113cfcc809SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 212b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 213b46c87bdSGreg Roach 21472f04adfSGreg Roach return redirect(route(static::class, [ 215b46c87bdSGreg Roach 'ancestors' => $params['ancestors'], 216b46c87bdSGreg Roach 'recursion' => $params['recursion'], 2174ea62551SGreg Roach 'tree' => $tree->name(), 218b46c87bdSGreg Roach 'xref' => $params['xref'], 219b46c87bdSGreg Roach 'xref2' => $params['xref2'], 2203cfcc809SGreg Roach ])); 2213cfcc809SGreg Roach } 2229b5537c3SGreg Roach 2236b9cb339SGreg Roach $individual1 = Registry::individualFactory()->make($xref, $tree); 2246b9cb339SGreg Roach $individual2 = Registry::individualFactory()->make($xref2, $tree); 2259b5537c3SGreg Roach 2263dcc812bSGreg Roach $ancestors_only = (int) $tree->getPreference('RELATIONSHIP_ANCESTORS', static::DEFAULT_ANCESTORS); 2273dcc812bSGreg Roach $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION); 2289b5537c3SGreg Roach 2299b5537c3SGreg Roach $recursion = min($recursion, $max_recursion); 2309b5537c3SGreg Roach 2313dcc812bSGreg Roach if ($individual1 instanceof Individual) { 2323c3fd0a5SGreg Roach $individual1 = Auth::checkIndividualAccess($individual1, false, true); 2333dcc812bSGreg Roach } 2343dcc812bSGreg Roach 2353dcc812bSGreg Roach if ($individual2 instanceof Individual) { 2363c3fd0a5SGreg Roach $individual2 = Auth::checkIndividualAccess($individual2, false, true); 2378365f910SGreg Roach } 2383dcc812bSGreg Roach 239ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 2409867b2f0SGreg Roach 2419b5537c3SGreg Roach if ($individual1 instanceof Individual && $individual2 instanceof Individual) { 2420b93976aSGreg Roach if ($ajax === '1') { 243f866a2aeSGreg Roach return $this->chart($individual1, $individual2, $recursion, $ancestors); 244f866a2aeSGreg Roach } 245f866a2aeSGreg Roach 2469b5537c3SGreg Roach /* I18N: %s are individual’s names */ 24739ca88baSGreg Roach $title = I18N::translate('Relationships between %1$s and %2$s', $individual1->fullName(), $individual2->fullName()); 2489b5537c3SGreg Roach $ajax_url = $this->chartUrl($individual1, [ 2499b5537c3SGreg Roach 'ajax' => true, 2509b5537c3SGreg Roach 'ancestors' => $ancestors, 2513cfcc809SGreg Roach 'recursion' => $recursion, 2523cfcc809SGreg Roach 'xref2' => $individual2->xref(), 2539b5537c3SGreg Roach ]); 2543dcc812bSGreg Roach } else { 2553dcc812bSGreg Roach $title = I18N::translate('Relationships'); 256f866a2aeSGreg Roach $ajax_url = ''; 2573dcc812bSGreg Roach } 2589b5537c3SGreg Roach 2599b5537c3SGreg Roach return $this->viewResponse('modules/relationships-chart/page', [ 2609b5537c3SGreg Roach 'ajax_url' => $ajax_url, 2619b5537c3SGreg Roach 'ancestors' => $ancestors, 2629b5537c3SGreg Roach 'ancestors_only' => $ancestors_only, 2639b5537c3SGreg Roach 'ancestors_options' => $this->ancestorsOptions(), 2649b5537c3SGreg Roach 'individual1' => $individual1, 2659b5537c3SGreg Roach 'individual2' => $individual2, 2669b5537c3SGreg Roach 'max_recursion' => $max_recursion, 26771378461SGreg Roach 'module' => $this->name(), 2689b5537c3SGreg Roach 'recursion' => $recursion, 2699b5537c3SGreg Roach 'recursion_options' => $this->recursionOptions($max_recursion), 2709b5537c3SGreg Roach 'title' => $title, 2713cfcc809SGreg Roach 'tree' => $tree, 2729b5537c3SGreg Roach ]); 2739b5537c3SGreg Roach } 2749b5537c3SGreg Roach 2759b5537c3SGreg Roach /** 2763dcc812bSGreg Roach * @param Individual $individual1 2773dcc812bSGreg Roach * @param Individual $individual2 2783dcc812bSGreg Roach * @param int $recursion 2793dcc812bSGreg Roach * @param int $ancestors 2809b5537c3SGreg Roach * 2816ccdf4f0SGreg Roach * @return ResponseInterface 2829b5537c3SGreg Roach */ 2836ccdf4f0SGreg Roach public function chart(Individual $individual1, Individual $individual2, int $recursion, int $ancestors): ResponseInterface 2849b5537c3SGreg Roach { 2853dcc812bSGreg Roach $tree = $individual1->tree(); 2869b5537c3SGreg Roach 2873dcc812bSGreg Roach $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION); 2889b5537c3SGreg Roach 2899b5537c3SGreg Roach $recursion = min($recursion, $max_recursion); 2909b5537c3SGreg Roach 2919b5537c3SGreg Roach $paths = $this->calculateRelationships($individual1, $individual2, $recursion, (bool) $ancestors); 2929b5537c3SGreg Roach 2939b5537c3SGreg Roach ob_start(); 2949b5537c3SGreg Roach if (I18N::direction() === 'ltr') { 295e837ff07SGreg Roach $diagonal1 = asset('css/images/dline.png'); 296e837ff07SGreg Roach $diagonal2 = asset('css/images/dline2.png'); 2979b5537c3SGreg Roach } else { 298e837ff07SGreg Roach $diagonal1 = asset('css/images/dline2.png'); 299e837ff07SGreg Roach $diagonal2 = asset('css/images/dline.png'); 3009b5537c3SGreg Roach } 3019b5537c3SGreg Roach 3029b5537c3SGreg Roach $num_paths = 0; 3039b5537c3SGreg Roach foreach ($paths as $path) { 3049b5537c3SGreg Roach // Extract the relationship names between pairs of individuals 3059b5537c3SGreg Roach $relationships = $this->oldStyleRelationshipPath($tree, $path); 306075d1a05SGreg Roach if ($relationships === []) { 3079b5537c3SGreg Roach // Cannot see one of the families/individuals, due to privacy; 3089b5537c3SGreg Roach continue; 3099b5537c3SGreg Roach } 3109b5537c3SGreg Roach echo '<h3>', I18N::translate('Relationship: %s', Functions::getRelationshipNameFromPath(implode('', $relationships), $individual1, $individual2)), '</h3>'; 3119b5537c3SGreg Roach $num_paths++; 3129b5537c3SGreg Roach 3139b5537c3SGreg Roach // Use a table/grid for layout. 3149b5537c3SGreg Roach $table = []; 3159b5537c3SGreg Roach // Current position in the grid. 3169b5537c3SGreg Roach $x = 0; 3179b5537c3SGreg Roach $y = 0; 3189b5537c3SGreg Roach // Extent of the grid. 3199b5537c3SGreg Roach $min_y = 0; 3209b5537c3SGreg Roach $max_y = 0; 3219b5537c3SGreg Roach $max_x = 0; 3229b5537c3SGreg Roach // For each node in the path. 3239b5537c3SGreg Roach foreach ($path as $n => $xref) { 3249b5537c3SGreg Roach if ($n % 2 === 1) { 3259b5537c3SGreg Roach switch ($relationships[$n]) { 3269b5537c3SGreg Roach case 'hus': 3279b5537c3SGreg Roach case 'wif': 3289b5537c3SGreg Roach case 'spo': 3299b5537c3SGreg Roach case 'bro': 3309b5537c3SGreg Roach case 'sis': 3319b5537c3SGreg Roach case 'sib': 3326b9cb339SGreg Roach $table[$x + 1][$y] = '<div style="background:url(' . e(asset('css/images/hline.png')) . ') repeat-x center; width: 94px; text-align: center"><div class="hline-text" style="height: 32px;">' . Functions::getRelationshipNameFromPath($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>'; 3339b5537c3SGreg Roach $x += 2; 3349b5537c3SGreg Roach break; 3359b5537c3SGreg Roach case 'son': 3369b5537c3SGreg Roach case 'dau': 3379b5537c3SGreg Roach case 'chi': 3389b5537c3SGreg Roach if ($n > 2 && preg_match('/fat|mot|par/', $relationships[$n - 2])) { 3396b9cb339SGreg Roach $table[$x + 1][$y - 1] = '<div style="background:url(' . $diagonal2 . '); width: 64px; height: 64px; text-align: center;"><div style="height: 32px; text-align: end;">' . Functions::getRelationshipNameFromPath($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>'; 3409b5537c3SGreg Roach $x += 2; 3419b5537c3SGreg Roach } else { 3426b9cb339SGreg Roach $table[$x][$y - 1] = '<div style="background:url(' . e('"' . asset('css/images/vline.png') . '"') . ') repeat-y center; height: 64px; text-align: center;"><div class="vline-text" style="display: inline-block; width:50%; line-height: 64px;">' . Functions::getRelationshipNameFromPath($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>'; 3439b5537c3SGreg Roach } 3449b5537c3SGreg Roach $y -= 2; 3459b5537c3SGreg Roach break; 3469b5537c3SGreg Roach case 'fat': 3479b5537c3SGreg Roach case 'mot': 3489b5537c3SGreg Roach case 'par': 3499b5537c3SGreg Roach if ($n > 2 && preg_match('/son|dau|chi/', $relationships[$n - 2])) { 3506b9cb339SGreg Roach $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;">' . Functions::getRelationshipNameFromPath($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>'; 3519b5537c3SGreg Roach $x += 2; 3529b5537c3SGreg Roach } else { 3536b9cb339SGreg Roach $table[$x][$y + 1] = '<div style="background:url(' . e('"' . asset('css/images/vline.png') . '"') . ') repeat-y center; height: 64px; text-align:center; "><div class="vline-text" style="display: inline-block; width: 50%; line-height: 64px;">' . Functions::getRelationshipNameFromPath($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>'; 3549b5537c3SGreg Roach } 3559b5537c3SGreg Roach $y += 2; 3569b5537c3SGreg Roach break; 3579b5537c3SGreg Roach } 3589b5537c3SGreg Roach $max_x = max($max_x, $x); 3599b5537c3SGreg Roach $min_y = min($min_y, $y); 3609b5537c3SGreg Roach $max_y = max($max_y, $y); 3619b5537c3SGreg Roach } else { 3626b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 363f4ba05e3SGreg Roach $table[$x][$y] = view('chart-box', ['individual' => $individual]); 3649b5537c3SGreg Roach } 3659b5537c3SGreg Roach } 3664a2590a5SGreg Roach echo '<div class="wt-chart wt-chart-relationships">'; 3679b5537c3SGreg Roach echo '<table style="border-collapse: collapse; margin: 20px 50px;">'; 3689b5537c3SGreg Roach for ($y = $max_y; $y >= $min_y; --$y) { 3699b5537c3SGreg Roach echo '<tr>'; 3709b5537c3SGreg Roach for ($x = 0; $x <= $max_x; ++$x) { 3719b5537c3SGreg Roach echo '<td style="padding: 0;">'; 3729b5537c3SGreg Roach if (isset($table[$x][$y])) { 3739b5537c3SGreg Roach echo $table[$x][$y]; 3749b5537c3SGreg Roach } 3759b5537c3SGreg Roach echo '</td>'; 3769b5537c3SGreg Roach } 3779b5537c3SGreg Roach echo '</tr>'; 3789b5537c3SGreg Roach } 3799b5537c3SGreg Roach echo '</table>'; 3809b5537c3SGreg Roach echo '</div>'; 3819b5537c3SGreg Roach } 3829b5537c3SGreg Roach 3839b5537c3SGreg Roach if (!$num_paths) { 3849b5537c3SGreg Roach echo '<p>', I18N::translate('No link between the two individuals could be found.'), '</p>'; 3859b5537c3SGreg Roach } 3869b5537c3SGreg Roach 3879b5537c3SGreg Roach $html = ob_get_clean(); 3889b5537c3SGreg Roach 3896ccdf4f0SGreg Roach return response($html); 3909b5537c3SGreg Roach } 3919b5537c3SGreg Roach 3929b5537c3SGreg Roach /** 3933cfcc809SGreg Roach * @param ServerRequestInterface $request 3943cfcc809SGreg Roach * 3953cfcc809SGreg Roach * @return ResponseInterface 3963cfcc809SGreg Roach */ 3973cfcc809SGreg Roach public function getAdminAction(ServerRequestInterface $request): ResponseInterface 3983cfcc809SGreg Roach { 3993cfcc809SGreg Roach $this->layout = 'layouts/administration'; 4003cfcc809SGreg Roach 4013cfcc809SGreg Roach return $this->viewResponse('modules/relationships-chart/config', [ 4023df1e584SGreg Roach 'all_trees' => $this->tree_service->all(), 4033cfcc809SGreg Roach 'ancestors_options' => $this->ancestorsOptions(), 4043cfcc809SGreg Roach 'default_ancestors' => self::DEFAULT_ANCESTORS, 4053cfcc809SGreg Roach 'default_recursion' => self::DEFAULT_RECURSION, 4063cfcc809SGreg Roach 'recursion_options' => $this->recursionConfigOptions(), 4073cfcc809SGreg Roach 'title' => I18N::translate('Chart preferences') . ' — ' . $this->title(), 4083cfcc809SGreg Roach ]); 4093cfcc809SGreg Roach } 4103cfcc809SGreg Roach 4113cfcc809SGreg Roach /** 4123cfcc809SGreg Roach * @param ServerRequestInterface $request 4133cfcc809SGreg Roach * 4143cfcc809SGreg Roach * @return ResponseInterface 4153cfcc809SGreg Roach */ 4163cfcc809SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 4173cfcc809SGreg Roach { 418b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 419b46c87bdSGreg Roach 4203df1e584SGreg Roach foreach ($this->tree_service->all() as $tree) { 421b46c87bdSGreg Roach $recursion = $params['relationship-recursion-' . $tree->id()] ?? ''; 422b46c87bdSGreg Roach $ancestors = $params['relationship-ancestors-' . $tree->id()] ?? ''; 4233cfcc809SGreg Roach 4243cfcc809SGreg Roach $tree->setPreference('RELATIONSHIP_RECURSION', $recursion); 4253cfcc809SGreg Roach $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors); 4263cfcc809SGreg Roach } 4273cfcc809SGreg Roach 4283cfcc809SGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); 4293cfcc809SGreg Roach 4303cfcc809SGreg Roach return redirect($this->getConfigLink()); 4313cfcc809SGreg Roach } 4323cfcc809SGreg Roach 4333cfcc809SGreg Roach /** 4343cfcc809SGreg Roach * Possible options for the ancestors option 4353cfcc809SGreg Roach * 4363cfcc809SGreg Roach * @return string[] 4373cfcc809SGreg Roach */ 4383cfcc809SGreg Roach private function ancestorsOptions(): array 4393cfcc809SGreg Roach { 4403cfcc809SGreg Roach return [ 4413cfcc809SGreg Roach 0 => I18N::translate('Find any relationship'), 4423cfcc809SGreg Roach 1 => I18N::translate('Find relationships via ancestors'), 4433cfcc809SGreg Roach ]; 4443cfcc809SGreg Roach } 4453cfcc809SGreg Roach 4463cfcc809SGreg Roach /** 4473cfcc809SGreg Roach * Possible options for the recursion option 4483cfcc809SGreg Roach * 4493cfcc809SGreg Roach * @return string[] 4503cfcc809SGreg Roach */ 4513cfcc809SGreg Roach private function recursionConfigOptions(): array 4523cfcc809SGreg Roach { 4533cfcc809SGreg Roach return [ 4543cfcc809SGreg Roach 0 => I18N::translate('none'), 4553cfcc809SGreg Roach 1 => I18N::number(1), 4563cfcc809SGreg Roach 2 => I18N::number(2), 4573cfcc809SGreg Roach 3 => I18N::number(3), 4583cfcc809SGreg Roach self::UNLIMITED_RECURSION => I18N::translate('unlimited'), 4593cfcc809SGreg Roach ]; 4603cfcc809SGreg Roach } 4613cfcc809SGreg Roach 4623cfcc809SGreg Roach /** 4639b5537c3SGreg Roach * Calculate the shortest paths - or all paths - between two individuals. 4649b5537c3SGreg Roach * 4659b5537c3SGreg Roach * @param Individual $individual1 4669b5537c3SGreg Roach * @param Individual $individual2 4679b5537c3SGreg Roach * @param int $recursion How many levels of recursion to use 4689b5537c3SGreg Roach * @param bool $ancestor Restrict to relationships via a common ancestor 4699b5537c3SGreg Roach * 4709b5537c3SGreg Roach * @return string[][] 4719b5537c3SGreg Roach */ 4729b5537c3SGreg Roach private function calculateRelationships(Individual $individual1, Individual $individual2, $recursion, $ancestor = false): array 4739b5537c3SGreg Roach { 4743dcc812bSGreg Roach $tree = $individual1->tree(); 4753dcc812bSGreg Roach 4769b5537c3SGreg Roach $rows = DB::table('link') 4773dcc812bSGreg Roach ->where('l_file', '=', $tree->id()) 4789b5537c3SGreg Roach ->whereIn('l_type', ['FAMS', 'FAMC']) 4799b5537c3SGreg Roach ->select(['l_from', 'l_to']) 4809b5537c3SGreg Roach ->get(); 4819b5537c3SGreg Roach 4829b5537c3SGreg Roach // Optionally restrict the graph to the ancestors of the individuals. 4839b5537c3SGreg Roach if ($ancestor) { 4843dcc812bSGreg Roach $ancestors = $this->allAncestors($individual1->xref(), $individual2->xref(), $tree->id()); 4853dcc812bSGreg Roach $exclude = $this->excludeFamilies($individual1->xref(), $individual2->xref(), $tree->id()); 4869b5537c3SGreg Roach } else { 4879b5537c3SGreg Roach $ancestors = []; 4889b5537c3SGreg Roach $exclude = []; 4899b5537c3SGreg Roach } 4909b5537c3SGreg Roach 4919b5537c3SGreg Roach $graph = []; 4929b5537c3SGreg Roach 4939b5537c3SGreg Roach foreach ($rows as $row) { 494075d1a05SGreg Roach if ($ancestors === [] || in_array($row->l_from, $ancestors, true) && !in_array($row->l_to, $exclude, true)) { 4959b5537c3SGreg Roach $graph[$row->l_from][$row->l_to] = 1; 4969b5537c3SGreg Roach $graph[$row->l_to][$row->l_from] = 1; 4979b5537c3SGreg Roach } 4989b5537c3SGreg Roach } 4999b5537c3SGreg Roach 5009b5537c3SGreg Roach $xref1 = $individual1->xref(); 5019b5537c3SGreg Roach $xref2 = $individual2->xref(); 5029b5537c3SGreg Roach $dijkstra = new Dijkstra($graph); 5039b5537c3SGreg Roach $paths = $dijkstra->shortestPaths($xref1, $xref2); 5049b5537c3SGreg Roach 5059b5537c3SGreg Roach // Only process each exclusion list once; 5069b5537c3SGreg Roach $excluded = []; 5079b5537c3SGreg Roach 5089b5537c3SGreg Roach $queue = []; 5099b5537c3SGreg Roach foreach ($paths as $path) { 5109b5537c3SGreg Roach // Insert the paths into the queue, with an exclusion list. 5119b5537c3SGreg Roach $queue[] = [ 5129b5537c3SGreg Roach 'path' => $path, 5139b5537c3SGreg Roach 'exclude' => [], 5149b5537c3SGreg Roach ]; 5159b5537c3SGreg Roach // While there are un-extended paths 5169b5537c3SGreg Roach for ($next = current($queue); $next !== false; $next = next($queue)) { 5179b5537c3SGreg Roach // For each family on the path 5189b5537c3SGreg Roach for ($n = count($next['path']) - 2; $n >= 1; $n -= 2) { 5199b5537c3SGreg Roach $exclude = $next['exclude']; 5209b5537c3SGreg Roach if (count($exclude) >= $recursion) { 5219b5537c3SGreg Roach continue; 5229b5537c3SGreg Roach } 5239b5537c3SGreg Roach $exclude[] = $next['path'][$n]; 5249b5537c3SGreg Roach sort($exclude); 5259b5537c3SGreg Roach $tmp = implode('-', $exclude); 52622d65e5aSGreg Roach if (in_array($tmp, $excluded, true)) { 5279b5537c3SGreg Roach continue; 5289b5537c3SGreg Roach } 5299b5537c3SGreg Roach 5309b5537c3SGreg Roach $excluded[] = $tmp; 5319b5537c3SGreg Roach // Add any new path to the queue 5329b5537c3SGreg Roach foreach ($dijkstra->shortestPaths($xref1, $xref2, $exclude) as $new_path) { 5339b5537c3SGreg Roach $queue[] = [ 5349b5537c3SGreg Roach 'path' => $new_path, 5359b5537c3SGreg Roach 'exclude' => $exclude, 5369b5537c3SGreg Roach ]; 5379b5537c3SGreg Roach } 5389b5537c3SGreg Roach } 5399b5537c3SGreg Roach } 5409b5537c3SGreg Roach } 541185cbb4dSGreg Roach // Extract the paths from the queue. 5429b5537c3SGreg Roach $paths = []; 5439b5537c3SGreg Roach foreach ($queue as $next) { 544185cbb4dSGreg Roach // The Dijkstra library does not use strict types, and converts 545185cbb4dSGreg Roach // numeric array keys (XREFs) from strings to integers; 546185cbb4dSGreg Roach $path = array_map($this->stringMapper(), $next['path']); 547185cbb4dSGreg Roach 548185cbb4dSGreg Roach // Remove duplicates 549185cbb4dSGreg Roach $paths[implode('-', $next['path'])] = $path; 5509b5537c3SGreg Roach } 5519b5537c3SGreg Roach 5529b5537c3SGreg Roach return $paths; 5539b5537c3SGreg Roach } 5549b5537c3SGreg Roach 5559b5537c3SGreg Roach /** 556185cbb4dSGreg Roach * Convert numeric values to strings 557185cbb4dSGreg Roach * 558185cbb4dSGreg Roach * @return Closure 559185cbb4dSGreg Roach */ 560c3f3b628SGreg Roach private function stringMapper(): Closure 561c3f3b628SGreg Roach { 562185cbb4dSGreg Roach return static function ($xref) { 563185cbb4dSGreg Roach return (string) $xref; 564185cbb4dSGreg Roach }; 565185cbb4dSGreg Roach } 566185cbb4dSGreg Roach 567185cbb4dSGreg Roach /** 5689b5537c3SGreg Roach * Find all ancestors of a list of individuals 5699b5537c3SGreg Roach * 5709b5537c3SGreg Roach * @param string $xref1 5719b5537c3SGreg Roach * @param string $xref2 5729b5537c3SGreg Roach * @param int $tree_id 5739b5537c3SGreg Roach * 5749b5537c3SGreg Roach * @return string[] 5759b5537c3SGreg Roach */ 5769b5537c3SGreg Roach private function allAncestors($xref1, $xref2, $tree_id): array 5779b5537c3SGreg Roach { 5789b5537c3SGreg Roach $ancestors = [ 5799b5537c3SGreg Roach $xref1, 5809b5537c3SGreg Roach $xref2, 5819b5537c3SGreg Roach ]; 5829b5537c3SGreg Roach 5839b5537c3SGreg Roach $queue = [ 5849b5537c3SGreg Roach $xref1, 5859b5537c3SGreg Roach $xref2, 5869b5537c3SGreg Roach ]; 587075d1a05SGreg Roach while ($queue !== []) { 5889b5537c3SGreg Roach $parents = DB::table('link AS l1') 5890b5fd0a6SGreg Roach ->join('link AS l2', static function (JoinClause $join): void { 5909b5537c3SGreg Roach $join 5919b5537c3SGreg Roach ->on('l1.l_to', '=', 'l2.l_to') 5929b5537c3SGreg Roach ->on('l1.l_file', '=', 'l2.l_file'); 5939b5537c3SGreg Roach }) 5949b5537c3SGreg Roach ->where('l1.l_file', '=', $tree_id) 5959b5537c3SGreg Roach ->where('l1.l_type', '=', 'FAMC') 5969b5537c3SGreg Roach ->where('l2.l_type', '=', 'FAMS') 5979b5537c3SGreg Roach ->whereIn('l1.l_from', $queue) 5989b5537c3SGreg Roach ->pluck('l2.l_from'); 5999b5537c3SGreg Roach 6009b5537c3SGreg Roach $queue = []; 6019b5537c3SGreg Roach foreach ($parents as $parent) { 60222d65e5aSGreg Roach if (!in_array($parent, $ancestors, true)) { 6039b5537c3SGreg Roach $ancestors[] = $parent; 6049b5537c3SGreg Roach $queue[] = $parent; 6059b5537c3SGreg Roach } 6069b5537c3SGreg Roach } 6079b5537c3SGreg Roach } 6089b5537c3SGreg Roach 6099b5537c3SGreg Roach return $ancestors; 6109b5537c3SGreg Roach } 6119b5537c3SGreg Roach 6129b5537c3SGreg Roach /** 6139b5537c3SGreg Roach * Find all families of two individuals 6149b5537c3SGreg Roach * 6159b5537c3SGreg Roach * @param string $xref1 6169b5537c3SGreg Roach * @param string $xref2 6179b5537c3SGreg Roach * @param int $tree_id 6189b5537c3SGreg Roach * 6199b5537c3SGreg Roach * @return string[] 6209b5537c3SGreg Roach */ 6219b5537c3SGreg Roach private function excludeFamilies($xref1, $xref2, $tree_id): array 6229b5537c3SGreg Roach { 6239b5537c3SGreg Roach return DB::table('link AS l1') 6240b5fd0a6SGreg Roach ->join('link AS l2', static function (JoinClause $join): void { 6259b5537c3SGreg Roach $join 6269b5537c3SGreg Roach ->on('l1.l_to', '=', 'l2.l_to') 6279b5537c3SGreg Roach ->on('l1.l_type', '=', 'l2.l_type') 6289b5537c3SGreg Roach ->on('l1.l_file', '=', 'l2.l_file'); 6299b5537c3SGreg Roach }) 6309b5537c3SGreg Roach ->where('l1.l_file', '=', $tree_id) 6319b5537c3SGreg Roach ->where('l1.l_type', '=', 'FAMS') 6329b5537c3SGreg Roach ->where('l1.l_from', '=', $xref1) 6339b5537c3SGreg Roach ->where('l2.l_from', '=', $xref2) 6349b5537c3SGreg Roach ->pluck('l1.l_to') 6359b5537c3SGreg Roach ->all(); 6369b5537c3SGreg Roach } 6379b5537c3SGreg Roach 6389b5537c3SGreg Roach /** 6396ccdf4f0SGreg Roach * Convert a path (list of XREFs) to an "old-style" string of relationships. 6406ccdf4f0SGreg Roach * Return an empty array, if privacy rules prevent us viewing any node. 6416ccdf4f0SGreg Roach * 6426ccdf4f0SGreg Roach * @param Tree $tree 6436ccdf4f0SGreg Roach * @param string[] $path Alternately Individual / Family 6446ccdf4f0SGreg Roach * 6456ccdf4f0SGreg Roach * @return string[] 6466ccdf4f0SGreg Roach */ 6476ccdf4f0SGreg Roach private function oldStyleRelationshipPath(Tree $tree, array $path): array 6486ccdf4f0SGreg Roach { 6496ccdf4f0SGreg Roach $spouse_codes = [ 6506ccdf4f0SGreg Roach 'M' => 'hus', 6516ccdf4f0SGreg Roach 'F' => 'wif', 6526ccdf4f0SGreg Roach 'U' => 'spo', 6536ccdf4f0SGreg Roach ]; 6546ccdf4f0SGreg Roach $parent_codes = [ 6556ccdf4f0SGreg Roach 'M' => 'fat', 6566ccdf4f0SGreg Roach 'F' => 'mot', 6576ccdf4f0SGreg Roach 'U' => 'par', 6586ccdf4f0SGreg Roach ]; 6596ccdf4f0SGreg Roach $child_codes = [ 6606ccdf4f0SGreg Roach 'M' => 'son', 6616ccdf4f0SGreg Roach 'F' => 'dau', 6626ccdf4f0SGreg Roach 'U' => 'chi', 6636ccdf4f0SGreg Roach ]; 6646ccdf4f0SGreg Roach $sibling_codes = [ 6656ccdf4f0SGreg Roach 'M' => 'bro', 6666ccdf4f0SGreg Roach 'F' => 'sis', 6676ccdf4f0SGreg Roach 'U' => 'sib', 6686ccdf4f0SGreg Roach ]; 6696ccdf4f0SGreg Roach $relationships = []; 6706ccdf4f0SGreg Roach 6716ccdf4f0SGreg Roach for ($i = 1, $count = count($path); $i < $count; $i += 2) { 6726b9cb339SGreg Roach $family = Registry::familyFactory()->make($path[$i], $tree); 6736b9cb339SGreg Roach $prev = Registry::individualFactory()->make($path[$i - 1], $tree); 6746b9cb339SGreg Roach $next = Registry::individualFactory()->make($path[$i + 1], $tree); 6756ccdf4f0SGreg Roach if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $prev->xref() . '@/', $family->gedcom(), $match)) { 6766ccdf4f0SGreg Roach $rel1 = $match[1]; 6776ccdf4f0SGreg Roach } else { 6786ccdf4f0SGreg Roach return []; 6796ccdf4f0SGreg Roach } 6806ccdf4f0SGreg Roach if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $next->xref() . '@/', $family->gedcom(), $match)) { 6816ccdf4f0SGreg Roach $rel2 = $match[1]; 6826ccdf4f0SGreg Roach } else { 6836ccdf4f0SGreg Roach return []; 6846ccdf4f0SGreg Roach } 6856ccdf4f0SGreg Roach if (($rel1 === 'HUSB' || $rel1 === 'WIFE') && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) { 6866ccdf4f0SGreg Roach $relationships[$i] = $spouse_codes[$next->sex()]; 6876ccdf4f0SGreg Roach } elseif (($rel1 === 'HUSB' || $rel1 === 'WIFE') && $rel2 === 'CHIL') { 6886ccdf4f0SGreg Roach $relationships[$i] = $child_codes[$next->sex()]; 6896ccdf4f0SGreg Roach } elseif ($rel1 === 'CHIL' && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) { 6906ccdf4f0SGreg Roach $relationships[$i] = $parent_codes[$next->sex()]; 6916ccdf4f0SGreg Roach } elseif ($rel1 === 'CHIL' && $rel2 === 'CHIL') { 6926ccdf4f0SGreg Roach $relationships[$i] = $sibling_codes[$next->sex()]; 6936ccdf4f0SGreg Roach } 6946ccdf4f0SGreg Roach } 6956ccdf4f0SGreg Roach 6966ccdf4f0SGreg Roach return $relationships; 6976ccdf4f0SGreg Roach } 6986ccdf4f0SGreg Roach 6996ccdf4f0SGreg Roach /** 7009b5537c3SGreg Roach * Possible options for the recursion option 7019b5537c3SGreg Roach * 7029b5537c3SGreg Roach * @param int $max_recursion 7039b5537c3SGreg Roach * 7044db4b4a9SGreg Roach * @return string[] 7059b5537c3SGreg Roach */ 7069b5537c3SGreg Roach private function recursionOptions(int $max_recursion): array 7079b5537c3SGreg Roach { 7083dcc812bSGreg Roach if ($max_recursion === static::UNLIMITED_RECURSION) { 7099b5537c3SGreg Roach $text = I18N::translate('Find all possible relationships'); 7109b5537c3SGreg Roach } else { 7119b5537c3SGreg Roach $text = I18N::translate('Find other relationships'); 7129b5537c3SGreg Roach } 7139b5537c3SGreg Roach 7149b5537c3SGreg Roach return [ 7159b5537c3SGreg Roach '0' => I18N::translate('Find the closest relationships'), 7169b5537c3SGreg Roach $max_recursion => $text, 7179b5537c3SGreg Roach ]; 7189b5537c3SGreg Roach } 719168ff6f3Sric2016} 720