xref: /webtrees/app/Module/RelationshipsChartModule.php (revision 0b93976a9c83f1ad374620df2dc12a210d5be076)
1168ff6f3Sric2016<?php
2168ff6f3Sric2016/**
3168ff6f3Sric2016 * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
6168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
7168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
8168ff6f3Sric2016 * (at your option) any later version.
9168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
10168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12168ff6f3Sric2016 * GNU General Public License for more details.
13168ff6f3Sric2016 * You should have received a copy of the GNU General Public License
14168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15168ff6f3Sric2016 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
18168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
19168ff6f3Sric2016
209b5537c3SGreg Roachuse Fisharebest\Algorithm\Dijkstra;
21168ff6f3Sric2016use Fisharebest\Webtrees\Auth;
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
239b5537c3SGreg Roachuse Fisharebest\Webtrees\Family;
2445ac604bSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
259b5537c3SGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
26168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
27168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
281e3273c9SGreg Roachuse Fisharebest\Webtrees\Menu;
2945ac604bSGreg Roachuse Fisharebest\Webtrees\Tree;
309b5537c3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
319b5537c3SGreg Roachuse Illuminate\Database\Query\JoinClause;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34f4ba05e3SGreg Roachuse function view;
35168ff6f3Sric2016
36168ff6f3Sric2016/**
37168ff6f3Sric2016 * Class RelationshipsChartModule
38168ff6f3Sric2016 */
3937eb8894SGreg Roachclass RelationshipsChartModule extends AbstractModule implements ModuleChartInterface, ModuleConfigInterface
40c1010edaSGreg Roach{
4149a243cbSGreg Roach    use ModuleChartTrait;
4249a243cbSGreg Roach    use ModuleConfigTrait;
4349a243cbSGreg Roach
441e3273c9SGreg Roach    /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */
4516d6367aSGreg Roach    public const UNLIMITED_RECURSION = 99;
461e3273c9SGreg Roach
471e3273c9SGreg Roach    /** By default new trees allow unlimited recursion */
4816d6367aSGreg Roach    public const DEFAULT_RECURSION = '99';
4945ac604bSGreg Roach
50e0bd7dc9SGreg Roach    /** By default new trees search for all relationships (not via ancestors) */
5116d6367aSGreg Roach    public const DEFAULT_ANCESTORS = '0';
52e0bd7dc9SGreg Roach
53168ff6f3Sric2016    /**
54168ff6f3Sric2016     * A sentence describing what this module does.
55168ff6f3Sric2016     *
56168ff6f3Sric2016     * @return string
57168ff6f3Sric2016     */
5849a243cbSGreg Roach    public function description(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Description of the “RelationshipsChart” module */
61bbb76c12SGreg Roach        return I18N::translate('A chart displaying relationships between two individuals.');
62168ff6f3Sric2016    }
63168ff6f3Sric2016
64168ff6f3Sric2016    /**
656ccdf4f0SGreg Roach     * Return a menu item for this chart - for use in individual boxes.
666ccdf4f0SGreg Roach     *
676ccdf4f0SGreg Roach     * @param Individual $individual
686ccdf4f0SGreg Roach     *
696ccdf4f0SGreg Roach     * @return Menu|null
706ccdf4f0SGreg Roach     */
716ccdf4f0SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
726ccdf4f0SGreg Roach    {
736ccdf4f0SGreg Roach        return $this->chartMenu($individual);
746ccdf4f0SGreg Roach    }
756ccdf4f0SGreg Roach
766ccdf4f0SGreg Roach    /**
77e6562982SGreg Roach     * A main menu item for this chart.
78168ff6f3Sric2016     *
798e69695bSGreg Roach     * @param Individual $individual
808e69695bSGreg Roach     *
81e6562982SGreg Roach     * @return Menu
82168ff6f3Sric2016     */
83e6562982SGreg Roach    public function chartMenu(Individual $individual): Menu
84c1010edaSGreg Roach    {
85e6562982SGreg Roach        $gedcomid = $individual->tree()->getUserPreference(Auth::user(), 'gedcomid');
86168ff6f3Sric2016
873dcc812bSGreg Roach        if ($gedcomid !== '' && $gedcomid !== $individual->xref()) {
88168ff6f3Sric2016            return new Menu(
89168ff6f3Sric2016                I18N::translate('Relationship to me'),
90e6562982SGreg Roach                $this->chartUrl($individual, ['xref2' => $gedcomid]),
91377a2979SGreg Roach                $this->chartMenuClass(),
92e6562982SGreg Roach                $this->chartUrlAttributes()
93168ff6f3Sric2016            );
94b2ce94c6SRico Sonntag        }
95b2ce94c6SRico Sonntag
96168ff6f3Sric2016        return new Menu(
97e6562982SGreg Roach            $this->title(),
98e6562982SGreg Roach            $this->chartUrl($individual),
99377a2979SGreg Roach            $this->chartMenuClass(),
100e6562982SGreg Roach            $this->chartUrlAttributes()
101168ff6f3Sric2016        );
102168ff6f3Sric2016    }
103168ff6f3Sric2016
1044eb71cfaSGreg Roach    /**
105377a2979SGreg Roach     * CSS class for the URL.
106377a2979SGreg Roach     *
107377a2979SGreg Roach     * @return string
108377a2979SGreg Roach     */
109377a2979SGreg Roach    public function chartMenuClass(): string
110377a2979SGreg Roach    {
111377a2979SGreg Roach        return 'menu-chart-relationship';
112377a2979SGreg Roach    }
113377a2979SGreg Roach
114377a2979SGreg Roach    /**
1156ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1164eb71cfaSGreg Roach     *
1176ccdf4f0SGreg Roach     * @return string
1184eb71cfaSGreg Roach     */
1196ccdf4f0SGreg Roach    public function title(): string
120c1010edaSGreg Roach    {
1216ccdf4f0SGreg Roach        /* I18N: Name of a module/chart */
1226ccdf4f0SGreg Roach        return I18N::translate('Relationships');
123e6562982SGreg Roach    }
124e6562982SGreg Roach
125e6562982SGreg Roach    /**
1266ccdf4f0SGreg Roach     * @return ResponseInterface
127291c1b19SGreg Roach     */
1286ccdf4f0SGreg Roach    public function getAdminAction(): ResponseInterface
129c1010edaSGreg Roach    {
130291c1b19SGreg Roach        $this->layout = 'layouts/administration';
131291c1b19SGreg Roach
1329b5537c3SGreg Roach        return $this->viewResponse('modules/relationships-chart/config', [
133291c1b19SGreg Roach            'all_trees'         => Tree::getAll(),
134291c1b19SGreg Roach            'ancestors_options' => $this->ancestorsOptions(),
135291c1b19SGreg Roach            'default_ancestors' => self::DEFAULT_ANCESTORS,
136291c1b19SGreg Roach            'default_recursion' => self::DEFAULT_RECURSION,
1379b5537c3SGreg Roach            'recursion_options' => $this->recursionConfigOptions(),
13849a243cbSGreg Roach            'title'             => I18N::translate('Chart preferences') . ' — ' . $this->title(),
139291c1b19SGreg Roach        ]);
140291c1b19SGreg Roach    }
141291c1b19SGreg Roach
142291c1b19SGreg Roach    /**
143e0bd7dc9SGreg Roach     * Possible options for the ancestors option
14418d7a90dSGreg Roach     *
14518d7a90dSGreg Roach     * @return string[]
146e0bd7dc9SGreg Roach     */
14718d7a90dSGreg Roach    private function ancestorsOptions(): array
148c1010edaSGreg Roach    {
14913abd6f3SGreg Roach        return [
150e0bd7dc9SGreg Roach            0 => I18N::translate('Find any relationship'),
151e0bd7dc9SGreg Roach            1 => I18N::translate('Find relationships via ancestors'),
15213abd6f3SGreg Roach        ];
153e0bd7dc9SGreg Roach    }
154e0bd7dc9SGreg Roach
155e0bd7dc9SGreg Roach    /**
1561e3273c9SGreg Roach     * Possible options for the recursion option
15718d7a90dSGreg Roach     *
15818d7a90dSGreg Roach     * @return string[]
1591e3273c9SGreg Roach     */
1609b5537c3SGreg Roach    private function recursionConfigOptions(): array
161c1010edaSGreg Roach    {
16213abd6f3SGreg Roach        return [
1631e3273c9SGreg Roach            0                         => I18N::translate('none'),
1641e3273c9SGreg Roach            1                         => I18N::number(1),
1651e3273c9SGreg Roach            2                         => I18N::number(2),
1661e3273c9SGreg Roach            3                         => I18N::number(3),
167e0bd7dc9SGreg Roach            self::UNLIMITED_RECURSION => I18N::translate('unlimited'),
16813abd6f3SGreg Roach        ];
1691e3273c9SGreg Roach    }
1709b5537c3SGreg Roach
1719b5537c3SGreg Roach    /**
1726ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1736ccdf4f0SGreg Roach     *
1746ccdf4f0SGreg Roach     * @return ResponseInterface
1756ccdf4f0SGreg Roach     */
1766ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
1776ccdf4f0SGreg Roach    {
1786ccdf4f0SGreg Roach        foreach (Tree::getAll() as $tree) {
179*0b93976aSGreg Roach            $recursion = $request->getQueryParams()['relationship-recursion-' . $tree->id()] ?? '';
180*0b93976aSGreg Roach            $ancestors = $request->getQueryParams()['relationship-ancestors-' . $tree->id()] ?? '';
1816ccdf4f0SGreg Roach
1826ccdf4f0SGreg Roach            $tree->setPreference('RELATIONSHIP_RECURSION', $recursion);
1836ccdf4f0SGreg Roach            $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors);
1846ccdf4f0SGreg Roach        }
1856ccdf4f0SGreg Roach
1866ccdf4f0SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
1876ccdf4f0SGreg Roach
1886ccdf4f0SGreg Roach        return redirect($this->getConfigLink());
1896ccdf4f0SGreg Roach    }
1906ccdf4f0SGreg Roach
1916ccdf4f0SGreg Roach    /**
1929b5537c3SGreg Roach     * A form to request the chart parameters.
1939b5537c3SGreg Roach     *
1946ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1959b5537c3SGreg Roach     * @param Tree                   $tree
196e5a6b4d4SGreg Roach     * @param UserInterface          $user
1979b5537c3SGreg Roach     *
1986ccdf4f0SGreg Roach     * @return ResponseInterface
1999b5537c3SGreg Roach     */
2006ccdf4f0SGreg Roach    public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
2019b5537c3SGreg Roach    {
202*0b93976aSGreg Roach        $ajax = $request->getQueryParams()['ajax'] ?? '';
2039b5537c3SGreg Roach
204*0b93976aSGreg Roach        $xref  = $request->getQueryParams()['xref'] ?? '';
205*0b93976aSGreg Roach        $xref2 = $request->getQueryParams()['xref2'] ?? '';
2069b5537c3SGreg Roach
2073dcc812bSGreg Roach        $individual1 = Individual::getInstance($xref, $tree);
2089b5537c3SGreg Roach        $individual2 = Individual::getInstance($xref2, $tree);
2099b5537c3SGreg Roach
210*0b93976aSGreg Roach        $recursion = (int) ($request->getQueryParams()['recursion'] ?? 0);
211*0b93976aSGreg Roach        $ancestors = (int) ($request->getQueryParams()['ancestors'] ?? 0);
2129b5537c3SGreg Roach
2133dcc812bSGreg Roach        $ancestors_only = (int) $tree->getPreference('RELATIONSHIP_ANCESTORS', static::DEFAULT_ANCESTORS);
2143dcc812bSGreg Roach        $max_recursion  = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION);
2159b5537c3SGreg Roach
2169b5537c3SGreg Roach        $recursion = min($recursion, $max_recursion);
2179b5537c3SGreg Roach
2183dcc812bSGreg Roach        if ($individual1 instanceof Individual) {
2193dcc812bSGreg Roach            Auth::checkIndividualAccess($individual1);
2203dcc812bSGreg Roach        }
2213dcc812bSGreg Roach
2223dcc812bSGreg Roach        if ($individual2 instanceof Individual) {
2233dcc812bSGreg Roach            Auth::checkIndividualAccess($individual2);
2243dcc812bSGreg Roach        }
2253dcc812bSGreg Roach
2269867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
2279867b2f0SGreg Roach
2289b5537c3SGreg Roach        if ($individual1 instanceof Individual && $individual2 instanceof Individual) {
229*0b93976aSGreg Roach            if ($ajax === '1') {
230f866a2aeSGreg Roach                return $this->chart($individual1, $individual2, $recursion, $ancestors);
231f866a2aeSGreg Roach            }
232f866a2aeSGreg Roach
2339b5537c3SGreg Roach            /* I18N: %s are individual’s names */
23439ca88baSGreg Roach            $title = I18N::translate('Relationships between %1$s and %2$s', $individual1->fullName(), $individual2->fullName());
235f866a2aeSGreg Roach
2369b5537c3SGreg Roach            $ajax_url = $this->chartUrl($individual1, [
2379b5537c3SGreg Roach                'ajax'      => true,
238f866a2aeSGreg Roach                'xref2'     => $individual2->xref(),
2399b5537c3SGreg Roach                'recursion' => $recursion,
2409b5537c3SGreg Roach                'ancestors' => $ancestors,
2419b5537c3SGreg Roach            ]);
2423dcc812bSGreg Roach        } else {
2433dcc812bSGreg Roach            $title = I18N::translate('Relationships');
2443dcc812bSGreg Roach
245f866a2aeSGreg Roach            $ajax_url = '';
2463dcc812bSGreg Roach        }
2479b5537c3SGreg Roach
2489b5537c3SGreg Roach        return $this->viewResponse('modules/relationships-chart/page', [
2499b5537c3SGreg Roach            'ajax_url'          => $ajax_url,
2509b5537c3SGreg Roach            'ancestors'         => $ancestors,
2519b5537c3SGreg Roach            'ancestors_only'    => $ancestors_only,
2529b5537c3SGreg Roach            'ancestors_options' => $this->ancestorsOptions(),
2539b5537c3SGreg Roach            'individual1'       => $individual1,
2549b5537c3SGreg Roach            'individual2'       => $individual2,
2559b5537c3SGreg Roach            'max_recursion'     => $max_recursion,
2569b5537c3SGreg Roach            'module_name'       => $this->name(),
2579b5537c3SGreg Roach            'recursion'         => $recursion,
2589b5537c3SGreg Roach            'recursion_options' => $this->recursionOptions($max_recursion),
2599b5537c3SGreg Roach            'title'             => $title,
2609b5537c3SGreg Roach        ]);
2619b5537c3SGreg Roach    }
2629b5537c3SGreg Roach
2639b5537c3SGreg Roach    /**
2643dcc812bSGreg Roach     * @param Individual $individual1
2653dcc812bSGreg Roach     * @param Individual $individual2
2663dcc812bSGreg Roach     * @param int        $recursion
2673dcc812bSGreg Roach     * @param int        $ancestors
2689b5537c3SGreg Roach     *
2696ccdf4f0SGreg Roach     * @return ResponseInterface
2709b5537c3SGreg Roach     */
2716ccdf4f0SGreg Roach    public function chart(Individual $individual1, Individual $individual2, int $recursion, int $ancestors): ResponseInterface
2729b5537c3SGreg Roach    {
2733dcc812bSGreg Roach        $tree = $individual1->tree();
2749b5537c3SGreg Roach
2753dcc812bSGreg Roach        $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION);
2769b5537c3SGreg Roach
2779b5537c3SGreg Roach        $recursion = min($recursion, $max_recursion);
2789b5537c3SGreg Roach
2799b5537c3SGreg Roach        $paths = $this->calculateRelationships($individual1, $individual2, $recursion, (bool) $ancestors);
2809b5537c3SGreg Roach
2819b5537c3SGreg Roach        // @TODO - convert to views
2829b5537c3SGreg Roach        ob_start();
2839b5537c3SGreg Roach        if (I18N::direction() === 'ltr') {
284e837ff07SGreg Roach            $diagonal1 = asset('css/images/dline.png');
285e837ff07SGreg Roach            $diagonal2 = asset('css/images/dline2.png');
2869b5537c3SGreg Roach        } else {
287e837ff07SGreg Roach            $diagonal1 = asset('css/images/dline2.png');
288e837ff07SGreg Roach            $diagonal2 = asset('css/images/dline.png');
2899b5537c3SGreg Roach        }
2909b5537c3SGreg Roach
2919b5537c3SGreg Roach        $num_paths = 0;
2929b5537c3SGreg Roach        foreach ($paths as $path) {
2939b5537c3SGreg Roach            // Extract the relationship names between pairs of individuals
2949b5537c3SGreg Roach            $relationships = $this->oldStyleRelationshipPath($tree, $path);
2959b5537c3SGreg Roach            if (empty($relationships)) {
2969b5537c3SGreg Roach                // Cannot see one of the families/individuals, due to privacy;
2979b5537c3SGreg Roach                continue;
2989b5537c3SGreg Roach            }
2999b5537c3SGreg Roach            echo '<h3>', I18N::translate('Relationship: %s', Functions::getRelationshipNameFromPath(implode('', $relationships), $individual1, $individual2)), '</h3>';
3009b5537c3SGreg Roach            $num_paths++;
3019b5537c3SGreg Roach
3029b5537c3SGreg Roach            // Use a table/grid for layout.
3039b5537c3SGreg Roach            $table = [];
3049b5537c3SGreg Roach            // Current position in the grid.
3059b5537c3SGreg Roach            $x = 0;
3069b5537c3SGreg Roach            $y = 0;
3079b5537c3SGreg Roach            // Extent of the grid.
3089b5537c3SGreg Roach            $min_y = 0;
3099b5537c3SGreg Roach            $max_y = 0;
3109b5537c3SGreg Roach            $max_x = 0;
3119b5537c3SGreg Roach            // For each node in the path.
3129b5537c3SGreg Roach            foreach ($path as $n => $xref) {
3139b5537c3SGreg Roach                if ($n % 2 === 1) {
3149b5537c3SGreg Roach                    switch ($relationships[$n]) {
3159b5537c3SGreg Roach                        case 'hus':
3169b5537c3SGreg Roach                        case 'wif':
3179b5537c3SGreg Roach                        case 'spo':
3189b5537c3SGreg Roach                        case 'bro':
3199b5537c3SGreg Roach                        case 'sis':
3209b5537c3SGreg Roach                        case 'sib':
32139b853a7SGreg 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], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="height: 32px;">' . view('icons/arrow-right') . '</div></div>';
3229b5537c3SGreg Roach                            $x                 += 2;
3239b5537c3SGreg Roach                            break;
3249b5537c3SGreg Roach                        case 'son':
3259b5537c3SGreg Roach                        case 'dau':
3269b5537c3SGreg Roach                        case 'chi':
3279b5537c3SGreg Roach                            if ($n > 2 && preg_match('/fat|mot|par/', $relationships[$n - 2])) {
328d993d560SGreg 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], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="height: 32px; text-align: start;">' . view('icons/arrow-down') . '</div></div>';
3299b5537c3SGreg Roach                                $x                     += 2;
3309b5537c3SGreg Roach                            } else {
3314ac9dd10SGreg 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], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="display: inline-block; width:50%; line-height: 64px;">' . view('icons/arrow-down') . '</div></div>';
3329b5537c3SGreg Roach                            }
3339b5537c3SGreg Roach                            $y -= 2;
3349b5537c3SGreg Roach                            break;
3359b5537c3SGreg Roach                        case 'fat':
3369b5537c3SGreg Roach                        case 'mot':
3379b5537c3SGreg Roach                        case 'par':
3389b5537c3SGreg Roach                            if ($n > 2 && preg_match('/son|dau|chi/', $relationships[$n - 2])) {
339d993d560SGreg 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], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="height: 32px; text-align: end;">' . view('icons/arrow-down') . '</div></div>';
3409b5537c3SGreg Roach                                $x                     += 2;
3419b5537c3SGreg Roach                            } else {
3424ac9dd10SGreg 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], Individual::getInstance($path[$n - 1], $tree), Individual::getInstance($path[$n + 1], $tree)) . '</div><div style="display: inline-block; width: 50%; line-height: 32px">' . view('icons/arrow-up') . '</div></div>';
3439b5537c3SGreg Roach                            }
3449b5537c3SGreg Roach                            $y += 2;
3459b5537c3SGreg Roach                            break;
3469b5537c3SGreg Roach                    }
3479b5537c3SGreg Roach                    $max_x = max($max_x, $x);
3489b5537c3SGreg Roach                    $min_y = min($min_y, $y);
3499b5537c3SGreg Roach                    $max_y = max($max_y, $y);
3509b5537c3SGreg Roach                } else {
3519b5537c3SGreg Roach                    $individual    = Individual::getInstance($xref, $tree);
352f4ba05e3SGreg Roach                    $table[$x][$y] = view('chart-box', ['individual' => $individual]);
3539b5537c3SGreg Roach                }
3549b5537c3SGreg Roach            }
3554a2590a5SGreg Roach            echo '<div class="wt-chart wt-chart-relationships">';
3569b5537c3SGreg Roach            echo '<table style="border-collapse: collapse; margin: 20px 50px;">';
3579b5537c3SGreg Roach            for ($y = $max_y; $y >= $min_y; --$y) {
3589b5537c3SGreg Roach                echo '<tr>';
3599b5537c3SGreg Roach                for ($x = 0; $x <= $max_x; ++$x) {
3609b5537c3SGreg Roach                    echo '<td style="padding: 0;">';
3619b5537c3SGreg Roach                    if (isset($table[$x][$y])) {
3629b5537c3SGreg Roach                        echo $table[$x][$y];
3639b5537c3SGreg Roach                    }
3649b5537c3SGreg Roach                    echo '</td>';
3659b5537c3SGreg Roach                }
3669b5537c3SGreg Roach                echo '</tr>';
3679b5537c3SGreg Roach            }
3689b5537c3SGreg Roach            echo '</table>';
3699b5537c3SGreg Roach            echo '</div>';
3709b5537c3SGreg Roach        }
3719b5537c3SGreg Roach
3729b5537c3SGreg Roach        if (!$num_paths) {
3739b5537c3SGreg Roach            echo '<p>', I18N::translate('No link between the two individuals could be found.'), '</p>';
3749b5537c3SGreg Roach        }
3759b5537c3SGreg Roach
3769b5537c3SGreg Roach        $html = ob_get_clean();
3779b5537c3SGreg Roach
3786ccdf4f0SGreg Roach        return response($html);
3799b5537c3SGreg Roach    }
3809b5537c3SGreg Roach
3819b5537c3SGreg Roach    /**
3829b5537c3SGreg Roach     * Calculate the shortest paths - or all paths - between two individuals.
3839b5537c3SGreg Roach     *
3849b5537c3SGreg Roach     * @param Individual $individual1
3859b5537c3SGreg Roach     * @param Individual $individual2
3869b5537c3SGreg Roach     * @param int        $recursion How many levels of recursion to use
3879b5537c3SGreg Roach     * @param bool       $ancestor  Restrict to relationships via a common ancestor
3889b5537c3SGreg Roach     *
3899b5537c3SGreg Roach     * @return string[][]
3909b5537c3SGreg Roach     */
3919b5537c3SGreg Roach    private function calculateRelationships(Individual $individual1, Individual $individual2, $recursion, $ancestor = false): array
3929b5537c3SGreg Roach    {
3933dcc812bSGreg Roach        $tree = $individual1->tree();
3943dcc812bSGreg Roach
3959b5537c3SGreg Roach        $rows = DB::table('link')
3963dcc812bSGreg Roach            ->where('l_file', '=', $tree->id())
3979b5537c3SGreg Roach            ->whereIn('l_type', ['FAMS', 'FAMC'])
3989b5537c3SGreg Roach            ->select(['l_from', 'l_to'])
3999b5537c3SGreg Roach            ->get();
4009b5537c3SGreg Roach
4019b5537c3SGreg Roach        // Optionally restrict the graph to the ancestors of the individuals.
4029b5537c3SGreg Roach        if ($ancestor) {
4033dcc812bSGreg Roach            $ancestors = $this->allAncestors($individual1->xref(), $individual2->xref(), $tree->id());
4043dcc812bSGreg Roach            $exclude   = $this->excludeFamilies($individual1->xref(), $individual2->xref(), $tree->id());
4059b5537c3SGreg Roach        } else {
4069b5537c3SGreg Roach            $ancestors = [];
4079b5537c3SGreg Roach            $exclude   = [];
4089b5537c3SGreg Roach        }
4099b5537c3SGreg Roach
4109b5537c3SGreg Roach        $graph = [];
4119b5537c3SGreg Roach
4129b5537c3SGreg Roach        foreach ($rows as $row) {
41322d65e5aSGreg Roach            if (empty($ancestors) || in_array($row->l_from, $ancestors, true) && !in_array($row->l_to, $exclude, true)) {
4149b5537c3SGreg Roach                $graph[$row->l_from][$row->l_to] = 1;
4159b5537c3SGreg Roach                $graph[$row->l_to][$row->l_from] = 1;
4169b5537c3SGreg Roach            }
4179b5537c3SGreg Roach        }
4189b5537c3SGreg Roach
4199b5537c3SGreg Roach        $xref1    = $individual1->xref();
4209b5537c3SGreg Roach        $xref2    = $individual2->xref();
4219b5537c3SGreg Roach        $dijkstra = new Dijkstra($graph);
4229b5537c3SGreg Roach        $paths    = $dijkstra->shortestPaths($xref1, $xref2);
4239b5537c3SGreg Roach
4249b5537c3SGreg Roach        // Only process each exclusion list once;
4259b5537c3SGreg Roach        $excluded = [];
4269b5537c3SGreg Roach
4279b5537c3SGreg Roach        $queue = [];
4289b5537c3SGreg Roach        foreach ($paths as $path) {
4299b5537c3SGreg Roach            // Insert the paths into the queue, with an exclusion list.
4309b5537c3SGreg Roach            $queue[] = [
4319b5537c3SGreg Roach                'path'    => $path,
4329b5537c3SGreg Roach                'exclude' => [],
4339b5537c3SGreg Roach            ];
4349b5537c3SGreg Roach            // While there are un-extended paths
4359b5537c3SGreg Roach            for ($next = current($queue); $next !== false; $next = next($queue)) {
4369b5537c3SGreg Roach                // For each family on the path
4379b5537c3SGreg Roach                for ($n = count($next['path']) - 2; $n >= 1; $n -= 2) {
4389b5537c3SGreg Roach                    $exclude = $next['exclude'];
4399b5537c3SGreg Roach                    if (count($exclude) >= $recursion) {
4409b5537c3SGreg Roach                        continue;
4419b5537c3SGreg Roach                    }
4429b5537c3SGreg Roach                    $exclude[] = $next['path'][$n];
4439b5537c3SGreg Roach                    sort($exclude);
4449b5537c3SGreg Roach                    $tmp = implode('-', $exclude);
44522d65e5aSGreg Roach                    if (in_array($tmp, $excluded, true)) {
4469b5537c3SGreg Roach                        continue;
4479b5537c3SGreg Roach                    }
4489b5537c3SGreg Roach
4499b5537c3SGreg Roach                    $excluded[] = $tmp;
4509b5537c3SGreg Roach                    // Add any new path to the queue
4519b5537c3SGreg Roach                    foreach ($dijkstra->shortestPaths($xref1, $xref2, $exclude) as $new_path) {
4529b5537c3SGreg Roach                        $queue[] = [
4539b5537c3SGreg Roach                            'path'    => $new_path,
4549b5537c3SGreg Roach                            'exclude' => $exclude,
4559b5537c3SGreg Roach                        ];
4569b5537c3SGreg Roach                    }
4579b5537c3SGreg Roach                }
4589b5537c3SGreg Roach            }
4599b5537c3SGreg Roach        }
4609b5537c3SGreg Roach        // Extract the paths from the queue, removing duplicates.
4619b5537c3SGreg Roach        $paths = [];
4629b5537c3SGreg Roach        foreach ($queue as $next) {
4639b5537c3SGreg Roach            $paths[implode('-', $next['path'])] = $next['path'];
4649b5537c3SGreg Roach        }
4659b5537c3SGreg Roach
4669b5537c3SGreg Roach        return $paths;
4679b5537c3SGreg Roach    }
4689b5537c3SGreg Roach
4699b5537c3SGreg Roach    /**
4709b5537c3SGreg Roach     * Find all ancestors of a list of individuals
4719b5537c3SGreg Roach     *
4729b5537c3SGreg Roach     * @param string $xref1
4739b5537c3SGreg Roach     * @param string $xref2
4749b5537c3SGreg Roach     * @param int    $tree_id
4759b5537c3SGreg Roach     *
4769b5537c3SGreg Roach     * @return string[]
4779b5537c3SGreg Roach     */
4789b5537c3SGreg Roach    private function allAncestors($xref1, $xref2, $tree_id): array
4799b5537c3SGreg Roach    {
4809b5537c3SGreg Roach        $ancestors = [
4819b5537c3SGreg Roach            $xref1,
4829b5537c3SGreg Roach            $xref2,
4839b5537c3SGreg Roach        ];
4849b5537c3SGreg Roach
4859b5537c3SGreg Roach        $queue = [
4869b5537c3SGreg Roach            $xref1,
4879b5537c3SGreg Roach            $xref2,
4889b5537c3SGreg Roach        ];
4899b5537c3SGreg Roach        while (!empty($queue)) {
4909b5537c3SGreg Roach            $parents = DB::table('link AS l1')
4910b5fd0a6SGreg Roach                ->join('link AS l2', static function (JoinClause $join): void {
4929b5537c3SGreg Roach                    $join
4939b5537c3SGreg Roach                        ->on('l1.l_to', '=', 'l2.l_to')
4949b5537c3SGreg Roach                        ->on('l1.l_file', '=', 'l2.l_file');
4959b5537c3SGreg Roach                })
4969b5537c3SGreg Roach                ->where('l1.l_file', '=', $tree_id)
4979b5537c3SGreg Roach                ->where('l1.l_type', '=', 'FAMC')
4989b5537c3SGreg Roach                ->where('l2.l_type', '=', 'FAMS')
4999b5537c3SGreg Roach                ->whereIn('l1.l_from', $queue)
5009b5537c3SGreg Roach                ->pluck('l2.l_from');
5019b5537c3SGreg Roach
5029b5537c3SGreg Roach            $queue = [];
5039b5537c3SGreg Roach            foreach ($parents as $parent) {
50422d65e5aSGreg Roach                if (!in_array($parent, $ancestors, true)) {
5059b5537c3SGreg Roach                    $ancestors[] = $parent;
5069b5537c3SGreg Roach                    $queue[]     = $parent;
5079b5537c3SGreg Roach                }
5089b5537c3SGreg Roach            }
5099b5537c3SGreg Roach        }
5109b5537c3SGreg Roach
5119b5537c3SGreg Roach        return $ancestors;
5129b5537c3SGreg Roach    }
5139b5537c3SGreg Roach
5149b5537c3SGreg Roach    /**
5159b5537c3SGreg Roach     * Find all families of two individuals
5169b5537c3SGreg Roach     *
5179b5537c3SGreg Roach     * @param string $xref1
5189b5537c3SGreg Roach     * @param string $xref2
5199b5537c3SGreg Roach     * @param int    $tree_id
5209b5537c3SGreg Roach     *
5219b5537c3SGreg Roach     * @return string[]
5229b5537c3SGreg Roach     */
5239b5537c3SGreg Roach    private function excludeFamilies($xref1, $xref2, $tree_id): array
5249b5537c3SGreg Roach    {
5259b5537c3SGreg Roach        return DB::table('link AS l1')
5260b5fd0a6SGreg Roach            ->join('link AS l2', static function (JoinClause $join): void {
5279b5537c3SGreg Roach                $join
5289b5537c3SGreg Roach                    ->on('l1.l_to', '=', 'l2.l_to')
5299b5537c3SGreg Roach                    ->on('l1.l_type', '=', 'l2.l_type')
5309b5537c3SGreg Roach                    ->on('l1.l_file', '=', 'l2.l_file');
5319b5537c3SGreg Roach            })
5329b5537c3SGreg Roach            ->where('l1.l_file', '=', $tree_id)
5339b5537c3SGreg Roach            ->where('l1.l_type', '=', 'FAMS')
5349b5537c3SGreg Roach            ->where('l1.l_from', '=', $xref1)
5359b5537c3SGreg Roach            ->where('l2.l_from', '=', $xref2)
5369b5537c3SGreg Roach            ->pluck('l1.l_to')
5379b5537c3SGreg Roach            ->all();
5389b5537c3SGreg Roach    }
5399b5537c3SGreg Roach
5409b5537c3SGreg Roach    /**
5416ccdf4f0SGreg Roach     * Convert a path (list of XREFs) to an "old-style" string of relationships.
5426ccdf4f0SGreg Roach     * Return an empty array, if privacy rules prevent us viewing any node.
5436ccdf4f0SGreg Roach     *
5446ccdf4f0SGreg Roach     * @param Tree     $tree
5456ccdf4f0SGreg Roach     * @param string[] $path Alternately Individual / Family
5466ccdf4f0SGreg Roach     *
5476ccdf4f0SGreg Roach     * @return string[]
5486ccdf4f0SGreg Roach     */
5496ccdf4f0SGreg Roach    private function oldStyleRelationshipPath(Tree $tree, array $path): array
5506ccdf4f0SGreg Roach    {
5516ccdf4f0SGreg Roach        $spouse_codes  = [
5526ccdf4f0SGreg Roach            'M' => 'hus',
5536ccdf4f0SGreg Roach            'F' => 'wif',
5546ccdf4f0SGreg Roach            'U' => 'spo',
5556ccdf4f0SGreg Roach        ];
5566ccdf4f0SGreg Roach        $parent_codes  = [
5576ccdf4f0SGreg Roach            'M' => 'fat',
5586ccdf4f0SGreg Roach            'F' => 'mot',
5596ccdf4f0SGreg Roach            'U' => 'par',
5606ccdf4f0SGreg Roach        ];
5616ccdf4f0SGreg Roach        $child_codes   = [
5626ccdf4f0SGreg Roach            'M' => 'son',
5636ccdf4f0SGreg Roach            'F' => 'dau',
5646ccdf4f0SGreg Roach            'U' => 'chi',
5656ccdf4f0SGreg Roach        ];
5666ccdf4f0SGreg Roach        $sibling_codes = [
5676ccdf4f0SGreg Roach            'M' => 'bro',
5686ccdf4f0SGreg Roach            'F' => 'sis',
5696ccdf4f0SGreg Roach            'U' => 'sib',
5706ccdf4f0SGreg Roach        ];
5716ccdf4f0SGreg Roach        $relationships = [];
5726ccdf4f0SGreg Roach
5736ccdf4f0SGreg Roach        for ($i = 1, $count = count($path); $i < $count; $i += 2) {
5746ccdf4f0SGreg Roach            $family = Family::getInstance($path[$i], $tree);
5756ccdf4f0SGreg Roach            $prev   = Individual::getInstance($path[$i - 1], $tree);
5766ccdf4f0SGreg Roach            $next   = Individual::getInstance($path[$i + 1], $tree);
5776ccdf4f0SGreg Roach            if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $prev->xref() . '@/', $family->gedcom(), $match)) {
5786ccdf4f0SGreg Roach                $rel1 = $match[1];
5796ccdf4f0SGreg Roach            } else {
5806ccdf4f0SGreg Roach                return [];
5816ccdf4f0SGreg Roach            }
5826ccdf4f0SGreg Roach            if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $next->xref() . '@/', $family->gedcom(), $match)) {
5836ccdf4f0SGreg Roach                $rel2 = $match[1];
5846ccdf4f0SGreg Roach            } else {
5856ccdf4f0SGreg Roach                return [];
5866ccdf4f0SGreg Roach            }
5876ccdf4f0SGreg Roach            if (($rel1 === 'HUSB' || $rel1 === 'WIFE') && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) {
5886ccdf4f0SGreg Roach                $relationships[$i] = $spouse_codes[$next->sex()];
5896ccdf4f0SGreg Roach            } elseif (($rel1 === 'HUSB' || $rel1 === 'WIFE') && $rel2 === 'CHIL') {
5906ccdf4f0SGreg Roach                $relationships[$i] = $child_codes[$next->sex()];
5916ccdf4f0SGreg Roach            } elseif ($rel1 === 'CHIL' && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) {
5926ccdf4f0SGreg Roach                $relationships[$i] = $parent_codes[$next->sex()];
5936ccdf4f0SGreg Roach            } elseif ($rel1 === 'CHIL' && $rel2 === 'CHIL') {
5946ccdf4f0SGreg Roach                $relationships[$i] = $sibling_codes[$next->sex()];
5956ccdf4f0SGreg Roach            }
5966ccdf4f0SGreg Roach        }
5976ccdf4f0SGreg Roach
5986ccdf4f0SGreg Roach        return $relationships;
5996ccdf4f0SGreg Roach    }
6006ccdf4f0SGreg Roach
6016ccdf4f0SGreg Roach    /**
6029b5537c3SGreg Roach     * Possible options for the recursion option
6039b5537c3SGreg Roach     *
6049b5537c3SGreg Roach     * @param int $max_recursion
6059b5537c3SGreg Roach     *
6064db4b4a9SGreg Roach     * @return string[]
6079b5537c3SGreg Roach     */
6089b5537c3SGreg Roach    private function recursionOptions(int $max_recursion): array
6099b5537c3SGreg Roach    {
6103dcc812bSGreg Roach        if ($max_recursion === static::UNLIMITED_RECURSION) {
6119b5537c3SGreg Roach            $text = I18N::translate('Find all possible relationships');
6129b5537c3SGreg Roach        } else {
6139b5537c3SGreg Roach            $text = I18N::translate('Find other relationships');
6149b5537c3SGreg Roach        }
6159b5537c3SGreg Roach
6169b5537c3SGreg Roach        return [
6179b5537c3SGreg Roach            '0'            => I18N::translate('Find the closest relationships'),
6189b5537c3SGreg Roach            $max_recursion => $text,
6199b5537c3SGreg Roach        ];
6209b5537c3SGreg Roach    }
621168ff6f3Sric2016}
622