xref: /webtrees/app/Module/RelationshipsChartModule.php (revision 6fcafd028e511367c3fcdbfaa31aa05a85bf85c0)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
51fe542e9SGreg 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
271fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2845ac604bSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
299b5537c3SGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
30*6fcafd02SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
31168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
32168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
331e3273c9SGreg Roachuse Fisharebest\Webtrees\Menu;
34*6fcafd02SGreg Roachuse Fisharebest\Webtrees\Registry;
35*6fcafd02SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
36*6fcafd02SGreg Roachuse Fisharebest\Webtrees\Services\RelationshipService;
373df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
3845ac604bSGreg Roachuse Fisharebest\Webtrees\Tree;
399b5537c3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
409b5537c3SGreg Roachuse Illuminate\Database\Query\JoinClause;
41*6fcafd02SGreg Roachuse Illuminate\Support\Collection;
426ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
436ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
443cfcc809SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
453976b470SGreg Roach
469e18e23bSGreg Roachuse function app;
479e18e23bSGreg Roachuse function assert;
48ddeb3354SGreg Roachuse function is_string;
493cfcc809SGreg Roachuse function redirect;
503cfcc809SGreg Roachuse function route;
51f4ba05e3SGreg Roachuse function view;
52168ff6f3Sric2016
53168ff6f3Sric2016/**
54168ff6f3Sric2016 * Class RelationshipsChartModule
55168ff6f3Sric2016 */
567a1b7425SGreg Roachclass RelationshipsChartModule extends AbstractModule implements ModuleChartInterface, ModuleConfigInterface, RequestHandlerInterface
57c1010edaSGreg Roach{
5849a243cbSGreg Roach    use ModuleChartTrait;
5949a243cbSGreg Roach    use ModuleConfigTrait;
6049a243cbSGreg Roach
6172f04adfSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/relationships-{ancestors}-{recursion}/{xref}{/xref2}';
623cfcc809SGreg Roach
631e3273c9SGreg Roach    /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */
6416d6367aSGreg Roach    public const UNLIMITED_RECURSION = 99;
651e3273c9SGreg Roach
661e3273c9SGreg Roach    /** By default new trees allow unlimited recursion */
6716d6367aSGreg Roach    public const DEFAULT_RECURSION = '99';
6845ac604bSGreg Roach
69e0bd7dc9SGreg Roach    /** By default new trees search for all relationships (not via ancestors) */
7016d6367aSGreg Roach    public const DEFAULT_ANCESTORS  = '0';
713cfcc809SGreg Roach    public const DEFAULT_PARAMETERS = [
723cfcc809SGreg Roach        'ancestors' => self::DEFAULT_ANCESTORS,
733cfcc809SGreg Roach        'recursion' => self::DEFAULT_RECURSION,
743cfcc809SGreg Roach    ];
753cfcc809SGreg Roach
76*6fcafd02SGreg Roach    private TreeService $tree_service;
77*6fcafd02SGreg Roach
78*6fcafd02SGreg Roach    private RelationshipService $relationship_service;
793df1e584SGreg Roach
803df1e584SGreg Roach    /**
81*6fcafd02SGreg Roach     * @param RelationshipService $relationship_service
823df1e584SGreg Roach     * @param TreeService         $tree_service
833df1e584SGreg Roach     */
84*6fcafd02SGreg Roach    public function __construct(RelationshipService $relationship_service, TreeService $tree_service)
853df1e584SGreg Roach    {
86*6fcafd02SGreg Roach        $this->relationship_service = $relationship_service;
873df1e584SGreg Roach        $this->tree_service         = $tree_service;
883df1e584SGreg Roach    }
893df1e584SGreg Roach
903cfcc809SGreg Roach    /**
913cfcc809SGreg Roach     * Initialization.
923cfcc809SGreg Roach     *
939e18e23bSGreg Roach     * @return void
943cfcc809SGreg Roach     */
959e18e23bSGreg Roach    public function boot(): void
963cfcc809SGreg Roach    {
979e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
989e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
999e18e23bSGreg Roach
1003cfcc809SGreg Roach        $router_container->getMap()
10172f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
1023cfcc809SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
1033cfcc809SGreg Roach            ->tokens([
1043cfcc809SGreg Roach                'ancestors' => '\d+',
1053cfcc809SGreg Roach                'recursion' => '\d+',
1063cfcc809SGreg Roach            ]);
1073cfcc809SGreg Roach    }
108e0bd7dc9SGreg Roach
109168ff6f3Sric2016    /**
110168ff6f3Sric2016     * A sentence describing what this module does.
111168ff6f3Sric2016     *
112168ff6f3Sric2016     * @return string
113168ff6f3Sric2016     */
11449a243cbSGreg Roach    public function description(): string
115c1010edaSGreg Roach    {
116bbb76c12SGreg Roach        /* I18N: Description of the “RelationshipsChart” module */
117bbb76c12SGreg Roach        return I18N::translate('A chart displaying relationships between two individuals.');
118168ff6f3Sric2016    }
119168ff6f3Sric2016
120168ff6f3Sric2016    /**
1216ccdf4f0SGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1226ccdf4f0SGreg Roach     *
1236ccdf4f0SGreg Roach     * @param Individual $individual
1246ccdf4f0SGreg Roach     *
1256ccdf4f0SGreg Roach     * @return Menu|null
1266ccdf4f0SGreg Roach     */
1276ccdf4f0SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
1286ccdf4f0SGreg Roach    {
1296ccdf4f0SGreg Roach        return $this->chartMenu($individual);
1306ccdf4f0SGreg Roach    }
1316ccdf4f0SGreg Roach
1326ccdf4f0SGreg Roach    /**
133e6562982SGreg Roach     * A main menu item for this chart.
134168ff6f3Sric2016     *
1358e69695bSGreg Roach     * @param Individual $individual
1368e69695bSGreg Roach     *
137e6562982SGreg Roach     * @return Menu
138168ff6f3Sric2016     */
139e6562982SGreg Roach    public function chartMenu(Individual $individual): Menu
140c1010edaSGreg Roach    {
141a0c3c04bSGreg Roach        $my_xref = $individual->tree()->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
142168ff6f3Sric2016
143a0c3c04bSGreg Roach        if ($my_xref !== '' && $my_xref !== $individual->xref()) {
144a0c3c04bSGreg Roach            $my_record = Registry::individualFactory()->make($my_xref, $individual->tree());
145a0c3c04bSGreg Roach
1462491533aSGreg Roach            if ($my_record instanceof Individual) {
147168ff6f3Sric2016                return new Menu(
148168ff6f3Sric2016                    I18N::translate('Relationship to me'),
149a0c3c04bSGreg Roach                    $this->chartUrl($my_record, ['xref2' => $individual->xref()]),
150377a2979SGreg Roach                    $this->chartMenuClass(),
151e6562982SGreg Roach                    $this->chartUrlAttributes()
152168ff6f3Sric2016                );
153b2ce94c6SRico Sonntag            }
1542491533aSGreg Roach        }
155b2ce94c6SRico Sonntag
156168ff6f3Sric2016        return new Menu(
157e6562982SGreg Roach            $this->title(),
158e6562982SGreg Roach            $this->chartUrl($individual),
159377a2979SGreg Roach            $this->chartMenuClass(),
160e6562982SGreg Roach            $this->chartUrlAttributes()
161168ff6f3Sric2016        );
162168ff6f3Sric2016    }
163168ff6f3Sric2016
1644eb71cfaSGreg Roach    /**
165377a2979SGreg Roach     * CSS class for the URL.
166377a2979SGreg Roach     *
167377a2979SGreg Roach     * @return string
168377a2979SGreg Roach     */
169377a2979SGreg Roach    public function chartMenuClass(): string
170377a2979SGreg Roach    {
171377a2979SGreg Roach        return 'menu-chart-relationship';
172377a2979SGreg Roach    }
173377a2979SGreg Roach
174377a2979SGreg Roach    /**
1756ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1764eb71cfaSGreg Roach     *
1776ccdf4f0SGreg Roach     * @return string
1784eb71cfaSGreg Roach     */
1796ccdf4f0SGreg Roach    public function title(): string
180c1010edaSGreg Roach    {
1816ccdf4f0SGreg Roach        /* I18N: Name of a module/chart */
1826ccdf4f0SGreg Roach        return I18N::translate('Relationships');
183e6562982SGreg Roach    }
184e6562982SGreg Roach
185e6562982SGreg Roach    /**
1863cfcc809SGreg Roach     * The URL for a page showing chart options.
18757ab2231SGreg Roach     *
1883cfcc809SGreg Roach     * @param Individual $individual
18959597b37SGreg Roach     * @param mixed[]    $parameters
19018d7a90dSGreg Roach     *
1913cfcc809SGreg Roach     * @return string
192e0bd7dc9SGreg Roach     */
1933cfcc809SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
194c1010edaSGreg Roach    {
19572f04adfSGreg Roach        return route(static::class, [
1963cfcc809SGreg Roach                'xref' => $individual->xref(),
1973cfcc809SGreg Roach                'tree' => $individual->tree()->name(),
1983cfcc809SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
1991e3273c9SGreg Roach    }
2009b5537c3SGreg Roach
2019b5537c3SGreg Roach    /**
2026ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
2036ccdf4f0SGreg Roach     *
2046ccdf4f0SGreg Roach     * @return ResponseInterface
2056ccdf4f0SGreg Roach     */
2063cfcc809SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
2076ccdf4f0SGreg Roach    {
2084ea62551SGreg Roach        $tree = $request->getAttribute('tree');
2094ea62551SGreg Roach        assert($tree instanceof Tree);
2104ea62551SGreg Roach
211ddeb3354SGreg Roach        $xref = $request->getAttribute('xref');
212ddeb3354SGreg Roach        assert(is_string($xref));
213ddeb3354SGreg Roach
214ddeb3354SGreg Roach        $xref2 = $request->getAttribute('xref2') ?? '';
215ddeb3354SGreg Roach
2163cfcc809SGreg Roach        $ajax      = $request->getQueryParams()['ajax'] ?? '';
2173cfcc809SGreg Roach        $ancestors = (int) $request->getAttribute('ancestors');
2183cfcc809SGreg Roach        $recursion = (int) $request->getAttribute('recursion');
21957ab2231SGreg Roach        $user      = $request->getAttribute('user');
2209b5537c3SGreg Roach
2213cfcc809SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
2223cfcc809SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
223b46c87bdSGreg Roach            $params = (array) $request->getParsedBody();
224b46c87bdSGreg Roach
22572f04adfSGreg Roach            return redirect(route(static::class, [
226b46c87bdSGreg Roach                'ancestors' => $params['ancestors'],
227b46c87bdSGreg Roach                'recursion' => $params['recursion'],
2284ea62551SGreg Roach                'tree'      => $tree->name(),
229b46c87bdSGreg Roach                'xref'      => $params['xref'],
230b46c87bdSGreg Roach                'xref2'     => $params['xref2'],
2313cfcc809SGreg Roach            ]));
2323cfcc809SGreg Roach        }
2339b5537c3SGreg Roach
2346b9cb339SGreg Roach        $individual1 = Registry::individualFactory()->make($xref, $tree);
2356b9cb339SGreg Roach        $individual2 = Registry::individualFactory()->make($xref2, $tree);
2369b5537c3SGreg Roach
2373dcc812bSGreg Roach        $ancestors_only = (int) $tree->getPreference('RELATIONSHIP_ANCESTORS', static::DEFAULT_ANCESTORS);
2383dcc812bSGreg Roach        $max_recursion  = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION);
2399b5537c3SGreg Roach
2409b5537c3SGreg Roach        $recursion = min($recursion, $max_recursion);
2419b5537c3SGreg Roach
2423dcc812bSGreg Roach        if ($individual1 instanceof Individual) {
2433c3fd0a5SGreg Roach            $individual1 = Auth::checkIndividualAccess($individual1, false, true);
2443dcc812bSGreg Roach        }
2453dcc812bSGreg Roach
2463dcc812bSGreg Roach        if ($individual2 instanceof Individual) {
2473c3fd0a5SGreg Roach            $individual2 = Auth::checkIndividualAccess($individual2, false, true);
2488365f910SGreg Roach        }
2493dcc812bSGreg Roach
250ef483801SGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
2519867b2f0SGreg Roach
2529b5537c3SGreg Roach        if ($individual1 instanceof Individual && $individual2 instanceof Individual) {
2530b93976aSGreg Roach            if ($ajax === '1') {
254f866a2aeSGreg Roach                return $this->chart($individual1, $individual2, $recursion, $ancestors);
255f866a2aeSGreg Roach            }
256f866a2aeSGreg Roach
2579b5537c3SGreg Roach            /* I18N: %s are individual’s names */
25839ca88baSGreg Roach            $title    = I18N::translate('Relationships between %1$s and %2$s', $individual1->fullName(), $individual2->fullName());
2599b5537c3SGreg Roach            $ajax_url = $this->chartUrl($individual1, [
2609b5537c3SGreg Roach                'ajax'      => true,
2619b5537c3SGreg Roach                'ancestors' => $ancestors,
2623cfcc809SGreg Roach                'recursion' => $recursion,
2633cfcc809SGreg Roach                'xref2'     => $individual2->xref(),
2649b5537c3SGreg Roach            ]);
2653dcc812bSGreg Roach        } else {
2663dcc812bSGreg Roach            $title    = I18N::translate('Relationships');
267f866a2aeSGreg Roach            $ajax_url = '';
2683dcc812bSGreg Roach        }
2699b5537c3SGreg Roach
2709b5537c3SGreg Roach        return $this->viewResponse('modules/relationships-chart/page', [
2719b5537c3SGreg Roach            'ajax_url'          => $ajax_url,
2729b5537c3SGreg Roach            'ancestors'         => $ancestors,
2739b5537c3SGreg Roach            'ancestors_only'    => $ancestors_only,
2749b5537c3SGreg Roach            'ancestors_options' => $this->ancestorsOptions(),
2759b5537c3SGreg Roach            'individual1'       => $individual1,
2769b5537c3SGreg Roach            'individual2'       => $individual2,
2779b5537c3SGreg Roach            'max_recursion'     => $max_recursion,
27871378461SGreg Roach            'module'            => $this->name(),
2799b5537c3SGreg Roach            'recursion'         => $recursion,
2809b5537c3SGreg Roach            'recursion_options' => $this->recursionOptions($max_recursion),
2819b5537c3SGreg Roach            'title'             => $title,
2823cfcc809SGreg Roach            'tree'              => $tree,
2839b5537c3SGreg Roach        ]);
2849b5537c3SGreg Roach    }
2859b5537c3SGreg Roach
2869b5537c3SGreg Roach    /**
2873dcc812bSGreg Roach     * @param Individual $individual1
2883dcc812bSGreg Roach     * @param Individual $individual2
2893dcc812bSGreg Roach     * @param int        $recursion
2903dcc812bSGreg Roach     * @param int        $ancestors
2919b5537c3SGreg Roach     *
2926ccdf4f0SGreg Roach     * @return ResponseInterface
2939b5537c3SGreg Roach     */
2946ccdf4f0SGreg Roach    public function chart(Individual $individual1, Individual $individual2, int $recursion, int $ancestors): ResponseInterface
2959b5537c3SGreg Roach    {
2963dcc812bSGreg Roach        $tree = $individual1->tree();
2979b5537c3SGreg Roach
2983dcc812bSGreg Roach        $max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION);
2999b5537c3SGreg Roach
3009b5537c3SGreg Roach        $recursion = min($recursion, $max_recursion);
3019b5537c3SGreg Roach
3029b5537c3SGreg Roach        $paths = $this->calculateRelationships($individual1, $individual2, $recursion, (bool) $ancestors);
3039b5537c3SGreg Roach
3049b5537c3SGreg Roach        ob_start();
3059b5537c3SGreg Roach        if (I18N::direction() === 'ltr') {
306e837ff07SGreg Roach            $diagonal1 = asset('css/images/dline.png');
307e837ff07SGreg Roach            $diagonal2 = asset('css/images/dline2.png');
3089b5537c3SGreg Roach        } else {
309e837ff07SGreg Roach            $diagonal1 = asset('css/images/dline2.png');
310e837ff07SGreg Roach            $diagonal2 = asset('css/images/dline.png');
3119b5537c3SGreg Roach        }
3129b5537c3SGreg Roach
3139b5537c3SGreg Roach        $num_paths = 0;
3149b5537c3SGreg Roach        foreach ($paths as $path) {
3159b5537c3SGreg Roach            // Extract the relationship names between pairs of individuals
3169b5537c3SGreg Roach            $relationships = $this->oldStyleRelationshipPath($tree, $path);
317075d1a05SGreg Roach            if ($relationships === []) {
3189b5537c3SGreg Roach                // Cannot see one of the families/individuals, due to privacy;
3199b5537c3SGreg Roach                continue;
3209b5537c3SGreg Roach            }
321*6fcafd02SGreg Roach
322*6fcafd02SGreg Roach            $nodes = Collection::make($path)
323*6fcafd02SGreg Roach                ->map(static function (string $xref, int $key) use ($tree): GedcomRecord {
324*6fcafd02SGreg Roach                    if ($key % 2 === 0) {
325*6fcafd02SGreg Roach                        return Registry::individualFactory()->make($xref, $tree);
326*6fcafd02SGreg Roach                    }
327*6fcafd02SGreg Roach
328*6fcafd02SGreg Roach                    return  Registry::familyFactory()->make($xref, $tree);
329*6fcafd02SGreg Roach                });
330*6fcafd02SGreg Roach
331*6fcafd02SGreg Roach            $language = app(ModuleService::class)
332*6fcafd02SGreg Roach                ->findByInterface(ModuleLanguageInterface::class, true)
333*6fcafd02SGreg Roach                ->first(fn (ModuleLanguageInterface $language): bool => $language->locale()->languageTag() === I18N::languageTag());
334*6fcafd02SGreg Roach
335*6fcafd02SGreg Roach
336*6fcafd02SGreg Roach
337*6fcafd02SGreg Roach            echo '<h3>', I18N::translate('Relationship: %s', $this->relationship_service->nameFromPath($nodes->all(), $language)), '</h3>';
3389b5537c3SGreg Roach            $num_paths++;
3399b5537c3SGreg Roach
3409b5537c3SGreg Roach            // Use a table/grid for layout.
3419b5537c3SGreg Roach            $table = [];
3429b5537c3SGreg Roach            // Current position in the grid.
3439b5537c3SGreg Roach            $x = 0;
3449b5537c3SGreg Roach            $y = 0;
3459b5537c3SGreg Roach            // Extent of the grid.
3469b5537c3SGreg Roach            $min_y = 0;
3479b5537c3SGreg Roach            $max_y = 0;
3489b5537c3SGreg Roach            $max_x = 0;
3499b5537c3SGreg Roach            // For each node in the path.
3509b5537c3SGreg Roach            foreach ($path as $n => $xref) {
3519b5537c3SGreg Roach                if ($n % 2 === 1) {
3529b5537c3SGreg Roach                    switch ($relationships[$n]) {
3539b5537c3SGreg Roach                        case 'hus':
3549b5537c3SGreg Roach                        case 'wif':
3559b5537c3SGreg Roach                        case 'spo':
3569b5537c3SGreg Roach                        case 'bro':
3579b5537c3SGreg Roach                        case 'sis':
3589b5537c3SGreg Roach                        case 'sib':
359*6fcafd02SGreg 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;">' . app(RelationshipService::class)->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>';
3609b5537c3SGreg Roach                            $x += 2;
3619b5537c3SGreg Roach                            break;
3629b5537c3SGreg Roach                        case 'son':
3639b5537c3SGreg Roach                        case 'dau':
3649b5537c3SGreg Roach                        case 'chi':
3659b5537c3SGreg Roach                            if ($n > 2 && preg_match('/fat|mot|par/', $relationships[$n - 2])) {
366*6fcafd02SGreg 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;">' . app(RelationshipService::class)->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>';
3679b5537c3SGreg Roach                                $x += 2;
3689b5537c3SGreg Roach                            } else {
369*6fcafd02SGreg 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;">' . app(RelationshipService::class)->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>';
3709b5537c3SGreg Roach                            }
3719b5537c3SGreg Roach                            $y -= 2;
3729b5537c3SGreg Roach                            break;
3739b5537c3SGreg Roach                        case 'fat':
3749b5537c3SGreg Roach                        case 'mot':
3759b5537c3SGreg Roach                        case 'par':
3769b5537c3SGreg Roach                            if ($n > 2 && preg_match('/son|dau|chi/', $relationships[$n - 2])) {
377*6fcafd02SGreg 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;">' . app(RelationshipService::class)->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>';
3789b5537c3SGreg Roach                                $x += 2;
3799b5537c3SGreg Roach                            } else {
380*6fcafd02SGreg 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;">' . app(RelationshipService::class)->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>';
3819b5537c3SGreg Roach                            }
3829b5537c3SGreg Roach                            $y += 2;
3839b5537c3SGreg Roach                            break;
3849b5537c3SGreg Roach                    }
3859b5537c3SGreg Roach                    $max_x = max($max_x, $x);
3869b5537c3SGreg Roach                    $min_y = min($min_y, $y);
3879b5537c3SGreg Roach                    $max_y = max($max_y, $y);
3889b5537c3SGreg Roach                } else {
3896b9cb339SGreg Roach                    $individual    = Registry::individualFactory()->make($xref, $tree);
390f4ba05e3SGreg Roach                    $table[$x][$y] = view('chart-box', ['individual' => $individual]);
3919b5537c3SGreg Roach                }
3929b5537c3SGreg Roach            }
3934a2590a5SGreg Roach            echo '<div class="wt-chart wt-chart-relationships">';
3949b5537c3SGreg Roach            echo '<table style="border-collapse: collapse; margin: 20px 50px;">';
3959b5537c3SGreg Roach            for ($y = $max_y; $y >= $min_y; --$y) {
3969b5537c3SGreg Roach                echo '<tr>';
3979b5537c3SGreg Roach                for ($x = 0; $x <= $max_x; ++$x) {
3989b5537c3SGreg Roach                    echo '<td style="padding: 0;">';
3999b5537c3SGreg Roach                    if (isset($table[$x][$y])) {
4009b5537c3SGreg Roach                        echo $table[$x][$y];
4019b5537c3SGreg Roach                    }
4029b5537c3SGreg Roach                    echo '</td>';
4039b5537c3SGreg Roach                }
4049b5537c3SGreg Roach                echo '</tr>';
4059b5537c3SGreg Roach            }
4069b5537c3SGreg Roach            echo '</table>';
4079b5537c3SGreg Roach            echo '</div>';
4089b5537c3SGreg Roach        }
4099b5537c3SGreg Roach
4109b5537c3SGreg Roach        if (!$num_paths) {
4119b5537c3SGreg Roach            echo '<p>', I18N::translate('No link between the two individuals could be found.'), '</p>';
4129b5537c3SGreg Roach        }
4139b5537c3SGreg Roach
4149b5537c3SGreg Roach        $html = ob_get_clean();
4159b5537c3SGreg Roach
4166ccdf4f0SGreg Roach        return response($html);
4179b5537c3SGreg Roach    }
4189b5537c3SGreg Roach
4199b5537c3SGreg Roach    /**
4203cfcc809SGreg Roach     * @param ServerRequestInterface $request
4213cfcc809SGreg Roach     *
4223cfcc809SGreg Roach     * @return ResponseInterface
4233cfcc809SGreg Roach     */
4243cfcc809SGreg Roach    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
4253cfcc809SGreg Roach    {
4263cfcc809SGreg Roach        $this->layout = 'layouts/administration';
4273cfcc809SGreg Roach
4283cfcc809SGreg Roach        return $this->viewResponse('modules/relationships-chart/config', [
4293df1e584SGreg Roach            'all_trees'         => $this->tree_service->all(),
4303cfcc809SGreg Roach            'ancestors_options' => $this->ancestorsOptions(),
4313cfcc809SGreg Roach            'default_ancestors' => self::DEFAULT_ANCESTORS,
4323cfcc809SGreg Roach            'default_recursion' => self::DEFAULT_RECURSION,
4333cfcc809SGreg Roach            'recursion_options' => $this->recursionConfigOptions(),
4343cfcc809SGreg Roach            'title'             => I18N::translate('Chart preferences') . ' — ' . $this->title(),
4353cfcc809SGreg Roach        ]);
4363cfcc809SGreg Roach    }
4373cfcc809SGreg Roach
4383cfcc809SGreg Roach    /**
4393cfcc809SGreg Roach     * @param ServerRequestInterface $request
4403cfcc809SGreg Roach     *
4413cfcc809SGreg Roach     * @return ResponseInterface
4423cfcc809SGreg Roach     */
4433cfcc809SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
4443cfcc809SGreg Roach    {
445b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
446b46c87bdSGreg Roach
4473df1e584SGreg Roach        foreach ($this->tree_service->all() as $tree) {
448b46c87bdSGreg Roach            $recursion = $params['relationship-recursion-' . $tree->id()] ?? '';
449b46c87bdSGreg Roach            $ancestors = $params['relationship-ancestors-' . $tree->id()] ?? '';
4503cfcc809SGreg Roach
4513cfcc809SGreg Roach            $tree->setPreference('RELATIONSHIP_RECURSION', $recursion);
4523cfcc809SGreg Roach            $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors);
4533cfcc809SGreg Roach        }
4543cfcc809SGreg Roach
4553cfcc809SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
4563cfcc809SGreg Roach
4573cfcc809SGreg Roach        return redirect($this->getConfigLink());
4583cfcc809SGreg Roach    }
4593cfcc809SGreg Roach
4603cfcc809SGreg Roach    /**
4613cfcc809SGreg Roach     * Possible options for the ancestors option
4623cfcc809SGreg Roach     *
4637c2c99faSGreg Roach     * @return array<int,string>
4643cfcc809SGreg Roach     */
4653cfcc809SGreg Roach    private function ancestorsOptions(): array
4663cfcc809SGreg Roach    {
4673cfcc809SGreg Roach        return [
4683cfcc809SGreg Roach            0 => I18N::translate('Find any relationship'),
4693cfcc809SGreg Roach            1 => I18N::translate('Find relationships via ancestors'),
4703cfcc809SGreg Roach        ];
4713cfcc809SGreg Roach    }
4723cfcc809SGreg Roach
4733cfcc809SGreg Roach    /**
4743cfcc809SGreg Roach     * Possible options for the recursion option
4753cfcc809SGreg Roach     *
4767c2c99faSGreg Roach     * @return array<int,string>
4773cfcc809SGreg Roach     */
4783cfcc809SGreg Roach    private function recursionConfigOptions(): array
4793cfcc809SGreg Roach    {
4803cfcc809SGreg Roach        return [
4813cfcc809SGreg Roach            0                         => I18N::translate('none'),
4823cfcc809SGreg Roach            1                         => I18N::number(1),
4833cfcc809SGreg Roach            2                         => I18N::number(2),
4843cfcc809SGreg Roach            3                         => I18N::number(3),
4853cfcc809SGreg Roach            self::UNLIMITED_RECURSION => I18N::translate('unlimited'),
4863cfcc809SGreg Roach        ];
4873cfcc809SGreg Roach    }
4883cfcc809SGreg Roach
4893cfcc809SGreg Roach    /**
4909b5537c3SGreg Roach     * Calculate the shortest paths - or all paths - between two individuals.
4919b5537c3SGreg Roach     *
4929b5537c3SGreg Roach     * @param Individual $individual1
4939b5537c3SGreg Roach     * @param Individual $individual2
4949b5537c3SGreg Roach     * @param int        $recursion How many levels of recursion to use
4959b5537c3SGreg Roach     * @param bool       $ancestor  Restrict to relationships via a common ancestor
4969b5537c3SGreg Roach     *
49724f2a3afSGreg Roach     * @return array<array<string>>
4989b5537c3SGreg Roach     */
49924f2a3afSGreg Roach    private function calculateRelationships(
50024f2a3afSGreg Roach        Individual $individual1,
50124f2a3afSGreg Roach        Individual $individual2,
50224f2a3afSGreg Roach        int $recursion,
50324f2a3afSGreg Roach        bool $ancestor = false
50424f2a3afSGreg Roach    ): array {
5053dcc812bSGreg Roach        $tree = $individual1->tree();
5063dcc812bSGreg Roach
5079b5537c3SGreg Roach        $rows = DB::table('link')
5083dcc812bSGreg Roach            ->where('l_file', '=', $tree->id())
5099b5537c3SGreg Roach            ->whereIn('l_type', ['FAMS', 'FAMC'])
5109b5537c3SGreg Roach            ->select(['l_from', 'l_to'])
5119b5537c3SGreg Roach            ->get();
5129b5537c3SGreg Roach
5139b5537c3SGreg Roach        // Optionally restrict the graph to the ancestors of the individuals.
5149b5537c3SGreg Roach        if ($ancestor) {
5153dcc812bSGreg Roach            $ancestors = $this->allAncestors($individual1->xref(), $individual2->xref(), $tree->id());
5163dcc812bSGreg Roach            $exclude   = $this->excludeFamilies($individual1->xref(), $individual2->xref(), $tree->id());
5179b5537c3SGreg Roach        } else {
5189b5537c3SGreg Roach            $ancestors = [];
5199b5537c3SGreg Roach            $exclude   = [];
5209b5537c3SGreg Roach        }
5219b5537c3SGreg Roach
5229b5537c3SGreg Roach        $graph = [];
5239b5537c3SGreg Roach
5249b5537c3SGreg Roach        foreach ($rows as $row) {
525075d1a05SGreg Roach            if ($ancestors === [] || in_array($row->l_from, $ancestors, true) && !in_array($row->l_to, $exclude, true)) {
5269b5537c3SGreg Roach                $graph[$row->l_from][$row->l_to] = 1;
5279b5537c3SGreg Roach                $graph[$row->l_to][$row->l_from] = 1;
5289b5537c3SGreg Roach            }
5299b5537c3SGreg Roach        }
5309b5537c3SGreg Roach
5319b5537c3SGreg Roach        $xref1    = $individual1->xref();
5329b5537c3SGreg Roach        $xref2    = $individual2->xref();
5339b5537c3SGreg Roach        $dijkstra = new Dijkstra($graph);
5349b5537c3SGreg Roach        $paths    = $dijkstra->shortestPaths($xref1, $xref2);
5359b5537c3SGreg Roach
5369b5537c3SGreg Roach        // Only process each exclusion list once;
5379b5537c3SGreg Roach        $excluded = [];
5389b5537c3SGreg Roach
5399b5537c3SGreg Roach        $queue = [];
5409b5537c3SGreg Roach        foreach ($paths as $path) {
5419b5537c3SGreg Roach            // Insert the paths into the queue, with an exclusion list.
5429b5537c3SGreg Roach            $queue[] = [
5439b5537c3SGreg Roach                'path'    => $path,
5449b5537c3SGreg Roach                'exclude' => [],
5459b5537c3SGreg Roach            ];
5469b5537c3SGreg Roach            // While there are un-extended paths
5479b5537c3SGreg Roach            for ($next = current($queue); $next !== false; $next = next($queue)) {
5489b5537c3SGreg Roach                // For each family on the path
5499b5537c3SGreg Roach                for ($n = count($next['path']) - 2; $n >= 1; $n -= 2) {
5509b5537c3SGreg Roach                    $exclude = $next['exclude'];
5519b5537c3SGreg Roach                    if (count($exclude) >= $recursion) {
5529b5537c3SGreg Roach                        continue;
5539b5537c3SGreg Roach                    }
5549b5537c3SGreg Roach                    $exclude[] = $next['path'][$n];
5559b5537c3SGreg Roach                    sort($exclude);
5569b5537c3SGreg Roach                    $tmp = implode('-', $exclude);
55722d65e5aSGreg Roach                    if (in_array($tmp, $excluded, true)) {
5589b5537c3SGreg Roach                        continue;
5599b5537c3SGreg Roach                    }
5609b5537c3SGreg Roach
5619b5537c3SGreg Roach                    $excluded[] = $tmp;
5629b5537c3SGreg Roach                    // Add any new path to the queue
5639b5537c3SGreg Roach                    foreach ($dijkstra->shortestPaths($xref1, $xref2, $exclude) as $new_path) {
5649b5537c3SGreg Roach                        $queue[] = [
5659b5537c3SGreg Roach                            'path'    => $new_path,
5669b5537c3SGreg Roach                            'exclude' => $exclude,
5679b5537c3SGreg Roach                        ];
5689b5537c3SGreg Roach                    }
5699b5537c3SGreg Roach                }
5709b5537c3SGreg Roach            }
5719b5537c3SGreg Roach        }
572185cbb4dSGreg Roach        // Extract the paths from the queue.
5739b5537c3SGreg Roach        $paths = [];
5749b5537c3SGreg Roach        foreach ($queue as $next) {
575185cbb4dSGreg Roach            // The Dijkstra library does not use strict types, and converts
576185cbb4dSGreg Roach            // numeric array keys (XREFs) from strings to integers;
577185cbb4dSGreg Roach            $path = array_map($this->stringMapper(), $next['path']);
578185cbb4dSGreg Roach
579185cbb4dSGreg Roach            // Remove duplicates
580185cbb4dSGreg Roach            $paths[implode('-', $next['path'])] = $path;
5819b5537c3SGreg Roach        }
5829b5537c3SGreg Roach
5839b5537c3SGreg Roach        return $paths;
5849b5537c3SGreg Roach    }
5859b5537c3SGreg Roach
5869b5537c3SGreg Roach    /**
587185cbb4dSGreg Roach     * Convert numeric values to strings
588185cbb4dSGreg Roach     *
589185cbb4dSGreg Roach     * @return Closure
590185cbb4dSGreg Roach     */
591c3f3b628SGreg Roach    private function stringMapper(): Closure
592c3f3b628SGreg Roach    {
593185cbb4dSGreg Roach        return static function ($xref) {
594185cbb4dSGreg Roach            return (string) $xref;
595185cbb4dSGreg Roach        };
596185cbb4dSGreg Roach    }
597185cbb4dSGreg Roach
598185cbb4dSGreg Roach    /**
5999b5537c3SGreg Roach     * Find all ancestors of a list of individuals
6009b5537c3SGreg Roach     *
6019b5537c3SGreg Roach     * @param string $xref1
6029b5537c3SGreg Roach     * @param string $xref2
6039b5537c3SGreg Roach     * @param int    $tree_id
6049b5537c3SGreg Roach     *
60524f2a3afSGreg Roach     * @return array<string>
6069b5537c3SGreg Roach     */
60724f2a3afSGreg Roach    private function allAncestors(string $xref1, string $xref2, int $tree_id): array
6089b5537c3SGreg Roach    {
6099b5537c3SGreg Roach        $ancestors = [
6109b5537c3SGreg Roach            $xref1,
6119b5537c3SGreg Roach            $xref2,
6129b5537c3SGreg Roach        ];
6139b5537c3SGreg Roach
6149b5537c3SGreg Roach        $queue = [
6159b5537c3SGreg Roach            $xref1,
6169b5537c3SGreg Roach            $xref2,
6179b5537c3SGreg Roach        ];
618075d1a05SGreg Roach        while ($queue !== []) {
6199b5537c3SGreg Roach            $parents = DB::table('link AS l1')
6200b5fd0a6SGreg Roach                ->join('link AS l2', static function (JoinClause $join): void {
6219b5537c3SGreg Roach                    $join
6229b5537c3SGreg Roach                        ->on('l1.l_to', '=', 'l2.l_to')
6239b5537c3SGreg Roach                        ->on('l1.l_file', '=', 'l2.l_file');
6249b5537c3SGreg Roach                })
6259b5537c3SGreg Roach                ->where('l1.l_file', '=', $tree_id)
6269b5537c3SGreg Roach                ->where('l1.l_type', '=', 'FAMC')
6279b5537c3SGreg Roach                ->where('l2.l_type', '=', 'FAMS')
6289b5537c3SGreg Roach                ->whereIn('l1.l_from', $queue)
6299b5537c3SGreg Roach                ->pluck('l2.l_from');
6309b5537c3SGreg Roach
6319b5537c3SGreg Roach            $queue = [];
6329b5537c3SGreg Roach            foreach ($parents as $parent) {
63322d65e5aSGreg Roach                if (!in_array($parent, $ancestors, true)) {
6349b5537c3SGreg Roach                    $ancestors[] = $parent;
6359b5537c3SGreg Roach                    $queue[]     = $parent;
6369b5537c3SGreg Roach                }
6379b5537c3SGreg Roach            }
6389b5537c3SGreg Roach        }
6399b5537c3SGreg Roach
6409b5537c3SGreg Roach        return $ancestors;
6419b5537c3SGreg Roach    }
6429b5537c3SGreg Roach
6439b5537c3SGreg Roach    /**
6449b5537c3SGreg Roach     * Find all families of two individuals
6459b5537c3SGreg Roach     *
6469b5537c3SGreg Roach     * @param string $xref1
6479b5537c3SGreg Roach     * @param string $xref2
6489b5537c3SGreg Roach     * @param int    $tree_id
6499b5537c3SGreg Roach     *
65024f2a3afSGreg Roach     * @return array<string>
6519b5537c3SGreg Roach     */
65224f2a3afSGreg Roach    private function excludeFamilies(string $xref1, string $xref2, int $tree_id): array
6539b5537c3SGreg Roach    {
6549b5537c3SGreg Roach        return DB::table('link AS l1')
6550b5fd0a6SGreg Roach            ->join('link AS l2', static function (JoinClause $join): void {
6569b5537c3SGreg Roach                $join
6579b5537c3SGreg Roach                    ->on('l1.l_to', '=', 'l2.l_to')
6589b5537c3SGreg Roach                    ->on('l1.l_type', '=', 'l2.l_type')
6599b5537c3SGreg Roach                    ->on('l1.l_file', '=', 'l2.l_file');
6609b5537c3SGreg Roach            })
6619b5537c3SGreg Roach            ->where('l1.l_file', '=', $tree_id)
6629b5537c3SGreg Roach            ->where('l1.l_type', '=', 'FAMS')
6639b5537c3SGreg Roach            ->where('l1.l_from', '=', $xref1)
6649b5537c3SGreg Roach            ->where('l2.l_from', '=', $xref2)
6659b5537c3SGreg Roach            ->pluck('l1.l_to')
6669b5537c3SGreg Roach            ->all();
6679b5537c3SGreg Roach    }
6689b5537c3SGreg Roach
6699b5537c3SGreg Roach    /**
6706ccdf4f0SGreg Roach     * Convert a path (list of XREFs) to an "old-style" string of relationships.
6716ccdf4f0SGreg Roach     * Return an empty array, if privacy rules prevent us viewing any node.
6726ccdf4f0SGreg Roach     *
6736ccdf4f0SGreg Roach     * @param Tree     $tree
6746ccdf4f0SGreg Roach     * @param string[] $path Alternately Individual / Family
6756ccdf4f0SGreg Roach     *
67624f2a3afSGreg Roach     * @return array<string>
6776ccdf4f0SGreg Roach     */
6786ccdf4f0SGreg Roach    private function oldStyleRelationshipPath(Tree $tree, array $path): array
6796ccdf4f0SGreg Roach    {
6806ccdf4f0SGreg Roach        $spouse_codes = [
6816ccdf4f0SGreg Roach            'M' => 'hus',
6826ccdf4f0SGreg Roach            'F' => 'wif',
6836ccdf4f0SGreg Roach            'U' => 'spo',
6846ccdf4f0SGreg Roach        ];
6856ccdf4f0SGreg Roach        $parent_codes = [
6866ccdf4f0SGreg Roach            'M' => 'fat',
6876ccdf4f0SGreg Roach            'F' => 'mot',
6886ccdf4f0SGreg Roach            'U' => 'par',
6896ccdf4f0SGreg Roach        ];
6906ccdf4f0SGreg Roach        $child_codes = [
6916ccdf4f0SGreg Roach            'M' => 'son',
6926ccdf4f0SGreg Roach            'F' => 'dau',
6936ccdf4f0SGreg Roach            'U' => 'chi',
6946ccdf4f0SGreg Roach        ];
6956ccdf4f0SGreg Roach        $sibling_codes = [
6966ccdf4f0SGreg Roach            'M' => 'bro',
6976ccdf4f0SGreg Roach            'F' => 'sis',
6986ccdf4f0SGreg Roach            'U' => 'sib',
6996ccdf4f0SGreg Roach        ];
7006ccdf4f0SGreg Roach        $relationships = [];
7016ccdf4f0SGreg Roach
7026ccdf4f0SGreg Roach        for ($i = 1, $count = count($path); $i < $count; $i += 2) {
7036b9cb339SGreg Roach            $family = Registry::familyFactory()->make($path[$i], $tree);
7046b9cb339SGreg Roach            $prev   = Registry::individualFactory()->make($path[$i - 1], $tree);
7056b9cb339SGreg Roach            $next   = Registry::individualFactory()->make($path[$i + 1], $tree);
7066ccdf4f0SGreg Roach            if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $prev->xref() . '@/', $family->gedcom(), $match)) {
7076ccdf4f0SGreg Roach                $rel1 = $match[1];
7086ccdf4f0SGreg Roach            } else {
7096ccdf4f0SGreg Roach                return [];
7106ccdf4f0SGreg Roach            }
7116ccdf4f0SGreg Roach            if (preg_match('/\n\d (HUSB|WIFE|CHIL) @' . $next->xref() . '@/', $family->gedcom(), $match)) {
7126ccdf4f0SGreg Roach                $rel2 = $match[1];
7136ccdf4f0SGreg Roach            } else {
7146ccdf4f0SGreg Roach                return [];
7156ccdf4f0SGreg Roach            }
7166ccdf4f0SGreg Roach            if (($rel1 === 'HUSB' || $rel1 === 'WIFE') && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) {
7176ccdf4f0SGreg Roach                $relationships[$i] = $spouse_codes[$next->sex()];
7186ccdf4f0SGreg Roach            } elseif (($rel1 === 'HUSB' || $rel1 === 'WIFE') && $rel2 === 'CHIL') {
7196ccdf4f0SGreg Roach                $relationships[$i] = $child_codes[$next->sex()];
7206ccdf4f0SGreg Roach            } elseif ($rel1 === 'CHIL' && ($rel2 === 'HUSB' || $rel2 === 'WIFE')) {
7216ccdf4f0SGreg Roach                $relationships[$i] = $parent_codes[$next->sex()];
7226ccdf4f0SGreg Roach            } elseif ($rel1 === 'CHIL' && $rel2 === 'CHIL') {
7236ccdf4f0SGreg Roach                $relationships[$i] = $sibling_codes[$next->sex()];
7246ccdf4f0SGreg Roach            }
7256ccdf4f0SGreg Roach        }
7266ccdf4f0SGreg Roach
7276ccdf4f0SGreg Roach        return $relationships;
7286ccdf4f0SGreg Roach    }
7296ccdf4f0SGreg Roach
7306ccdf4f0SGreg Roach    /**
7319b5537c3SGreg Roach     * Possible options for the recursion option
7329b5537c3SGreg Roach     *
7339b5537c3SGreg Roach     * @param int $max_recursion
7349b5537c3SGreg Roach     *
73524f2a3afSGreg Roach     * @return array<string>
7369b5537c3SGreg Roach     */
7379b5537c3SGreg Roach    private function recursionOptions(int $max_recursion): array
7389b5537c3SGreg Roach    {
7393dcc812bSGreg Roach        if ($max_recursion === static::UNLIMITED_RECURSION) {
7409b5537c3SGreg Roach            $text = I18N::translate('Find all possible relationships');
7419b5537c3SGreg Roach        } else {
7429b5537c3SGreg Roach            $text = I18N::translate('Find other relationships');
7439b5537c3SGreg Roach        }
7449b5537c3SGreg Roach
7459b5537c3SGreg Roach        return [
7469b5537c3SGreg Roach            '0'            => I18N::translate('Find the closest relationships'),
7479b5537c3SGreg Roach            $max_recursion => $text,
7489b5537c3SGreg Roach        ];
7499b5537c3SGreg Roach    }
750168ff6f3Sric2016}
751