xref: /webtrees/app/Module/BranchesListModule.php (revision fd30738620ed1bd71f926c9c64253ff30909ca72)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
506a438b4SGreg Roach * Copyright (C) 2020 webtrees development team
667992b6aSRichard Cissee * This program is free software: you can redistribute it and/or modify
767992b6aSRichard Cissee * it under the terms of the GNU General Public License as published by
867992b6aSRichard Cissee * the Free Software Foundation, either version 3 of the License, or
967992b6aSRichard Cissee * (at your option) any later version.
1067992b6aSRichard Cissee * This program is distributed in the hope that it will be useful,
1167992b6aSRichard Cissee * but WITHOUT ANY WARRANTY; without even the implied warranty of
1267992b6aSRichard Cissee * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1367992b6aSRichard Cissee * GNU General Public License for more details.
1467992b6aSRichard Cissee * You should have received a copy of the GNU General Public License
1567992b6aSRichard Cissee * along with this program. If not, see <http://www.gnu.org/licenses/>.
1667992b6aSRichard Cissee */
17fcfa147eSGreg Roach
1867992b6aSRichard Cisseedeclare(strict_types=1);
1967992b6aSRichard Cissee
2067992b6aSRichard Cisseenamespace Fisharebest\Webtrees\Module;
2167992b6aSRichard Cissee
2206a438b4SGreg Roachuse Aura\Router\RouterContainer;
2306a438b4SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
24808ea2d4SGreg Roachuse Fisharebest\Webtrees\Auth;
2506a438b4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2606a438b4SGreg Roachuse Fisharebest\Webtrees\Factory;
2706a438b4SGreg Roachuse Fisharebest\Webtrees\Family;
2806a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
2906a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
3067992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
3106a438b4SGreg Roachuse Fisharebest\Webtrees\Individual;
3267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
3306a438b4SGreg Roachuse Fisharebest\Webtrees\Soundex;
3467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
3506a438b4SGreg Roachuse Fisharebest\Webtrees\User;
3606a438b4SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3706a438b4SGreg Roachuse Illuminate\Database\Query\Builder;
3806a438b4SGreg Roachuse Illuminate\Database\Query\JoinClause;
396ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
406ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
4106a438b4SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
42f3874e19SGreg Roach
4306a438b4SGreg Roachuse function app;
4406a438b4SGreg Roachuse function array_search;
455229eadeSGreg Roachuse function assert;
4606a438b4SGreg Roachuse function e;
4706a438b4SGreg Roachuse function explode;
4806a438b4SGreg Roachuse function in_array;
4906a438b4SGreg Roachuse function is_int;
5006a438b4SGreg Roachuse function key;
5106a438b4SGreg Roachuse function log;
5206a438b4SGreg Roachuse function next;
53808ea2d4SGreg Roachuse function redirect;
54808ea2d4SGreg Roachuse function route;
5506a438b4SGreg Roachuse function strip_tags;
5606a438b4SGreg Roachuse function stripos;
5706a438b4SGreg Roachuse function usort;
5806a438b4SGreg Roachuse function view;
5967992b6aSRichard Cissee
6067992b6aSRichard Cissee/**
6167992b6aSRichard Cissee * Class BranchesListModule
6267992b6aSRichard Cissee */
6306a438b4SGreg Roachclass BranchesListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
6467992b6aSRichard Cissee{
6567992b6aSRichard Cissee    use ModuleListTrait;
6667992b6aSRichard Cissee
6706a438b4SGreg Roach    protected const ROUTE_URL  = '/tree/{tree}/branches{/surname}';
6806a438b4SGreg Roach
6906a438b4SGreg Roach    /** @var ModuleService */
7006a438b4SGreg Roach    protected $module_service;
7106a438b4SGreg Roach
7206a438b4SGreg Roach    /**
7306a438b4SGreg Roach     * BranchesListModule constructor.
7406a438b4SGreg Roach     *
7506a438b4SGreg Roach     * @param ModuleService $module_service
7606a438b4SGreg Roach     */
7706a438b4SGreg Roach    public function __construct(ModuleService $module_service)
7806a438b4SGreg Roach    {
7906a438b4SGreg Roach        $this->module_service = $module_service;
8006a438b4SGreg Roach    }
8106a438b4SGreg Roach
8206a438b4SGreg Roach    /**
8306a438b4SGreg Roach     * Initialization.
8406a438b4SGreg Roach     *
8506a438b4SGreg Roach     * @return void
8606a438b4SGreg Roach     */
8706a438b4SGreg Roach    public function boot(): void
8806a438b4SGreg Roach    {
8906a438b4SGreg Roach        $router_container = app(RouterContainer::class);
9006a438b4SGreg Roach        assert($router_container instanceof RouterContainer);
9106a438b4SGreg Roach
9206a438b4SGreg Roach        $router_container->getMap()
9306a438b4SGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
9406a438b4SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
9506a438b4SGreg Roach    }
9606a438b4SGreg Roach
9767992b6aSRichard Cissee    /**
980cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
9967992b6aSRichard Cissee     *
10067992b6aSRichard Cissee     * @return string
10167992b6aSRichard Cissee     */
10267992b6aSRichard Cissee    public function title(): string
10367992b6aSRichard Cissee    {
10467992b6aSRichard Cissee        /* I18N: Name of a module/list */
10567992b6aSRichard Cissee        return I18N::translate('Branches');
10667992b6aSRichard Cissee    }
10767992b6aSRichard Cissee
10867992b6aSRichard Cissee    /**
10967992b6aSRichard Cissee     * A sentence describing what this module does.
11067992b6aSRichard Cissee     *
11167992b6aSRichard Cissee     * @return string
11267992b6aSRichard Cissee     */
11367992b6aSRichard Cissee    public function description(): string
11467992b6aSRichard Cissee    {
115b5e8e56bSGreg Roach        /* I18N: Description of the “Branches” module */
11667992b6aSRichard Cissee        return I18N::translate('A list of branches of a family.');
11767992b6aSRichard Cissee    }
11867992b6aSRichard Cissee
11967992b6aSRichard Cissee    /**
12067992b6aSRichard Cissee     * CSS class for the URL.
12167992b6aSRichard Cissee     *
12267992b6aSRichard Cissee     * @return string
12367992b6aSRichard Cissee     */
12467992b6aSRichard Cissee    public function listMenuClass(): string
12567992b6aSRichard Cissee    {
12667992b6aSRichard Cissee        return 'menu-branches';
12767992b6aSRichard Cissee    }
12867992b6aSRichard Cissee
1294db4b4a9SGreg Roach    /**
1304db4b4a9SGreg Roach     * @param Tree    $tree
13159597b37SGreg Roach     * @param mixed[] $parameters
1324db4b4a9SGreg Roach     *
1334db4b4a9SGreg Roach     * @return string
1344db4b4a9SGreg Roach     */
13567992b6aSRichard Cissee    public function listUrl(Tree $tree, array $parameters = []): string
13667992b6aSRichard Cissee    {
13706a438b4SGreg Roach        $xref = app(ServerRequestInterface::class)->getAttribute('xref', '');
13806a438b4SGreg Roach
13906a438b4SGreg Roach        if ($xref !== '') {
14006a438b4SGreg Roach            $individual = Factory::individual()->make($xref, $tree);
14106a438b4SGreg Roach
14206a438b4SGreg Roach            if ($individual instanceof Individual && $individual->canShow()) {
14306a438b4SGreg Roach                $parameters['surname'] = $parameters['surname'] ?? $individual->getAllNames()[0]['surn'] ?? null;
14406a438b4SGreg Roach            }
14567992b6aSRichard Cissee        }
14667992b6aSRichard Cissee
14706a438b4SGreg Roach        $parameters['tree'] = $tree->name();
1485229eadeSGreg Roach
14906a438b4SGreg Roach        return route(static::class, $parameters);
15067992b6aSRichard Cissee    }
15167992b6aSRichard Cissee
1524db4b4a9SGreg Roach    /**
1534db4b4a9SGreg Roach     * @return string[]
1544db4b4a9SGreg Roach     */
15567992b6aSRichard Cissee    public function listUrlAttributes(): array
15667992b6aSRichard Cissee    {
15767992b6aSRichard Cissee        return [];
15867992b6aSRichard Cissee    }
15906a438b4SGreg Roach
16006a438b4SGreg Roach    /**
16106a438b4SGreg Roach     * Handle URLs generated by older versions of webtrees
16206a438b4SGreg Roach     *
16306a438b4SGreg Roach     * @param ServerRequestInterface $request
16406a438b4SGreg Roach     *
16506a438b4SGreg Roach     * @return ResponseInterface
16606a438b4SGreg Roach     */
16706a438b4SGreg Roach    public function getPageAction(ServerRequestInterface $request): ResponseInterface
16806a438b4SGreg Roach    {
16906a438b4SGreg Roach        return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams()));
17006a438b4SGreg Roach    }
17106a438b4SGreg Roach
17206a438b4SGreg Roach    /**
17306a438b4SGreg Roach     * @param ServerRequestInterface $request
17406a438b4SGreg Roach     *
17506a438b4SGreg Roach     * @return ResponseInterface
17606a438b4SGreg Roach     */
17706a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
17806a438b4SGreg Roach    {
17906a438b4SGreg Roach        $tree = $request->getAttribute('tree');
18006a438b4SGreg Roach        assert($tree instanceof Tree);
18106a438b4SGreg Roach
18206a438b4SGreg Roach        $user = $request->getAttribute('user');
18306a438b4SGreg Roach        assert($user instanceof UserInterface);
18406a438b4SGreg Roach
18506a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
18606a438b4SGreg Roach
18706a438b4SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
18806a438b4SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
18906a438b4SGreg Roach            return redirect($this->listUrl($tree, (array) $request->getParsedBody()));
19006a438b4SGreg Roach        }
19106a438b4SGreg Roach
19206a438b4SGreg Roach        $surname = (string) $request->getAttribute('surname');
19306a438b4SGreg Roach
19406a438b4SGreg Roach        $params      = $request->getQueryParams();
19506a438b4SGreg Roach        $ajax        = $params['ajax'] ?? '';
19606a438b4SGreg Roach        $soundex_std = (bool) ($params['soundex_std'] ?? false);
19706a438b4SGreg Roach        $soundex_dm  = (bool) ($params['soundex_dm'] ?? false);
19806a438b4SGreg Roach
19906a438b4SGreg Roach        if ($ajax === '1') {
20006a438b4SGreg Roach            $this->layout = 'layouts/ajax';
20106a438b4SGreg Roach
20206a438b4SGreg Roach            // Highlight direct-line ancestors of this individual.
20306a438b4SGreg Roach            $xref = $tree->getUserPreference($user, User::PREF_TREE_ACCOUNT_XREF);
20406a438b4SGreg Roach            $self = Factory::individual()->make($xref, $tree);
20506a438b4SGreg Roach
20606a438b4SGreg Roach            if ($surname !== '') {
20706a438b4SGreg Roach                $individuals = $this->loadIndividuals($tree, $surname, $soundex_dm, $soundex_std);
20806a438b4SGreg Roach            } else {
20906a438b4SGreg Roach                $individuals = [];
21006a438b4SGreg Roach            }
21106a438b4SGreg Roach
21206a438b4SGreg Roach            if ($self instanceof Individual) {
21306a438b4SGreg Roach                $ancestors = $this->allAncestors($self);
21406a438b4SGreg Roach            } else {
21506a438b4SGreg Roach                $ancestors = [];
21606a438b4SGreg Roach            }
21706a438b4SGreg Roach
21806a438b4SGreg Roach            return $this->viewResponse('modules/branches/list', [
21906a438b4SGreg Roach                'branches' => $this->getPatriarchsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std),
22006a438b4SGreg Roach            ]);
22106a438b4SGreg Roach        }
22206a438b4SGreg Roach
22306a438b4SGreg Roach        if ($surname !== '') {
22406a438b4SGreg Roach            /* I18N: %s is a surname */
22506a438b4SGreg Roach            $title = I18N::translate('Branches of the %s family', e($surname));
22606a438b4SGreg Roach
22706a438b4SGreg Roach            $ajax_url = $this->listUrl($tree, $params + ['ajax' => true, 'surname' => $surname]);
22806a438b4SGreg Roach        } else {
22906a438b4SGreg Roach            /* I18N: Branches of a family tree */
23006a438b4SGreg Roach            $title = I18N::translate('Branches');
23106a438b4SGreg Roach
23206a438b4SGreg Roach            $ajax_url = '';
23306a438b4SGreg Roach        }
23406a438b4SGreg Roach
23506a438b4SGreg Roach        return $this->viewResponse('branches-page', [
23606a438b4SGreg Roach            'ajax_url'    => $ajax_url,
23706a438b4SGreg Roach            'soundex_dm'  => $soundex_dm,
23806a438b4SGreg Roach            'soundex_std' => $soundex_std,
23906a438b4SGreg Roach            'surname'     => $surname,
24006a438b4SGreg Roach            'title'       => $title,
24106a438b4SGreg Roach            'tree'        => $tree,
24206a438b4SGreg Roach        ]);
24306a438b4SGreg Roach    }
24406a438b4SGreg Roach
24506a438b4SGreg Roach    /**
24606a438b4SGreg Roach     * Find all ancestors of an individual, indexed by the Sosa-Stradonitz number.
24706a438b4SGreg Roach     *
24806a438b4SGreg Roach     * @param Individual $individual
24906a438b4SGreg Roach     *
25006a438b4SGreg Roach     * @return Individual[]
25106a438b4SGreg Roach     */
25206a438b4SGreg Roach    private function allAncestors(Individual $individual): array
25306a438b4SGreg Roach    {
25406a438b4SGreg Roach        $ancestors = [
25506a438b4SGreg Roach            1 => $individual,
25606a438b4SGreg Roach        ];
25706a438b4SGreg Roach
25806a438b4SGreg Roach        do {
25906a438b4SGreg Roach            $sosa = key($ancestors);
26006a438b4SGreg Roach
26106a438b4SGreg Roach            $family = $ancestors[$sosa]->childFamilies()->first();
26206a438b4SGreg Roach
26306a438b4SGreg Roach            if ($family !== null) {
26406a438b4SGreg Roach                if ($family->husband() !== null) {
26506a438b4SGreg Roach                    $ancestors[$sosa * 2] = $family->husband();
26606a438b4SGreg Roach                }
26706a438b4SGreg Roach                if ($family->wife() !== null) {
26806a438b4SGreg Roach                    $ancestors[$sosa * 2 + 1] = $family->wife();
26906a438b4SGreg Roach                }
27006a438b4SGreg Roach            }
27106a438b4SGreg Roach        } while (next($ancestors));
27206a438b4SGreg Roach
27306a438b4SGreg Roach        return $ancestors;
27406a438b4SGreg Roach    }
27506a438b4SGreg Roach
27606a438b4SGreg Roach    /**
27706a438b4SGreg Roach     * Fetch all individuals with a matching surname
27806a438b4SGreg Roach     *
27906a438b4SGreg Roach     * @param Tree   $tree
28006a438b4SGreg Roach     * @param string $surname
28106a438b4SGreg Roach     * @param bool   $soundex_dm
28206a438b4SGreg Roach     * @param bool   $soundex_std
28306a438b4SGreg Roach     *
28406a438b4SGreg Roach     * @return Individual[]
28506a438b4SGreg Roach     */
28606a438b4SGreg Roach    private function loadIndividuals(Tree $tree, string $surname, bool $soundex_dm, bool $soundex_std): array
28706a438b4SGreg Roach    {
28806a438b4SGreg Roach        $individuals = DB::table('individuals')
28906a438b4SGreg Roach            ->join('name', static function (JoinClause $join): void {
29006a438b4SGreg Roach                $join
29106a438b4SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
29206a438b4SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
29306a438b4SGreg Roach            })
29406a438b4SGreg Roach            ->where('i_file', '=', $tree->id())
29506a438b4SGreg Roach            ->where('n_type', '<>', '_MARNM')
29606a438b4SGreg Roach            ->where(static function (Builder $query) use ($surname, $soundex_dm, $soundex_std): void {
29706a438b4SGreg Roach                $query
29806a438b4SGreg Roach                    ->where('n_surn', '=', $surname)
29906a438b4SGreg Roach                    ->orWhere('n_surname', '=', $surname);
30006a438b4SGreg Roach
30106a438b4SGreg Roach                if ($soundex_std) {
30206a438b4SGreg Roach                    $sdx = Soundex::russell($surname);
30306a438b4SGreg Roach                    if ($sdx !== '') {
30406a438b4SGreg Roach                        foreach (explode(':', $sdx) as $value) {
30506a438b4SGreg Roach                            $query->orWhere('n_soundex_surn_std', 'LIKE', '%' . $value . '%');
30606a438b4SGreg Roach                        }
30706a438b4SGreg Roach                    }
30806a438b4SGreg Roach                }
30906a438b4SGreg Roach
31006a438b4SGreg Roach                if ($soundex_dm) {
31106a438b4SGreg Roach                    $sdx = Soundex::daitchMokotoff($surname);
31206a438b4SGreg Roach                    if ($sdx !== '') {
31306a438b4SGreg Roach                        foreach (explode(':', $sdx) as $value) {
31406a438b4SGreg Roach                            $query->orWhere('n_soundex_surn_dm', 'LIKE', '%' . $value . '%');
31506a438b4SGreg Roach                        }
31606a438b4SGreg Roach                    }
31706a438b4SGreg Roach                }
31806a438b4SGreg Roach            })
31906a438b4SGreg Roach            ->select(['individuals.*'])
32006a438b4SGreg Roach            ->distinct()
32106a438b4SGreg Roach            ->get()
32206a438b4SGreg Roach            ->map(Factory::individual()->mapper($tree))
32306a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter())
32406a438b4SGreg Roach            ->all();
32506a438b4SGreg Roach
32606a438b4SGreg Roach        usort($individuals, Individual::birthDateComparator());
32706a438b4SGreg Roach
32806a438b4SGreg Roach        return $individuals;
32906a438b4SGreg Roach    }
33006a438b4SGreg Roach
33106a438b4SGreg Roach    /**
33206a438b4SGreg Roach     * For each individual with no ancestors, list their descendants.
33306a438b4SGreg Roach     *
33406a438b4SGreg Roach     * @param Tree         $tree
33506a438b4SGreg Roach     * @param Individual[] $individuals
33606a438b4SGreg Roach     * @param Individual[] $ancestors
33706a438b4SGreg Roach     * @param string       $surname
33806a438b4SGreg Roach     * @param bool         $soundex_dm
33906a438b4SGreg Roach     * @param bool         $soundex_std
34006a438b4SGreg Roach     *
34106a438b4SGreg Roach     * @return string
34206a438b4SGreg Roach     */
34306a438b4SGreg Roach    private function getPatriarchsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std): string
34406a438b4SGreg Roach    {
34506a438b4SGreg Roach        $html = '';
34606a438b4SGreg Roach        foreach ($individuals as $individual) {
34706a438b4SGreg Roach            foreach ($individual->childFamilies() as $family) {
34806a438b4SGreg Roach                foreach ($family->spouses() as $parent) {
34906a438b4SGreg Roach                    if (in_array($parent, $individuals, true)) {
35006a438b4SGreg Roach                        continue 3;
35106a438b4SGreg Roach                    }
35206a438b4SGreg Roach                }
35306a438b4SGreg Roach            }
35406a438b4SGreg Roach            $html .= $this->getDescendantsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std, $individual, null);
35506a438b4SGreg Roach        }
35606a438b4SGreg Roach
35706a438b4SGreg Roach        return $html;
35806a438b4SGreg Roach    }
35906a438b4SGreg Roach
36006a438b4SGreg Roach    /**
36106a438b4SGreg Roach     * Generate a recursive list of descendants of an individual.
36206a438b4SGreg Roach     * If parents are specified, we can also show the pedigree (adopted, etc.).
36306a438b4SGreg Roach     *
36406a438b4SGreg Roach     * @param Tree         $tree
36506a438b4SGreg Roach     * @param Individual[] $individuals
36606a438b4SGreg Roach     * @param Individual[] $ancestors
36706a438b4SGreg Roach     * @param string       $surname
36806a438b4SGreg Roach     * @param bool         $soundex_dm
36906a438b4SGreg Roach     * @param bool         $soundex_std
37006a438b4SGreg Roach     * @param Individual   $individual
37106a438b4SGreg Roach     * @param Family|null  $parents
37206a438b4SGreg Roach     *
37306a438b4SGreg Roach     * @return string
37406a438b4SGreg Roach     */
37506a438b4SGreg Roach    private function getDescendantsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std, Individual $individual, Family $parents = null): string
37606a438b4SGreg Roach    {
37706a438b4SGreg Roach        $module = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module) {
37806a438b4SGreg Roach            return $module instanceof RelationshipsChartModule;
37906a438b4SGreg Roach        });
38006a438b4SGreg Roach
38106a438b4SGreg Roach        // A person has many names. Select the one that matches the searched surname
38206a438b4SGreg Roach        $person_name = '';
38306a438b4SGreg Roach        foreach ($individual->getAllNames() as $name) {
38406a438b4SGreg Roach            [$surn1] = explode(',', $name['sort']);
38506a438b4SGreg Roach            if ($this->surnamesMatch($surn1, $surname, $soundex_std, $soundex_dm)) {
38606a438b4SGreg Roach                $person_name = $name['full'];
38706a438b4SGreg Roach                break;
38806a438b4SGreg Roach            }
38906a438b4SGreg Roach        }
39006a438b4SGreg Roach
39106a438b4SGreg Roach        // No matching name? Typically children with a different surname. The branch stops here.
39206a438b4SGreg Roach        if ($person_name === '') {
39306a438b4SGreg Roach            return '<li title="' . strip_tags($individual->fullName()) . '"><small>' . view('icons/sex', ['sex' => $individual->sex()]) . '</small>…</li>';
39406a438b4SGreg Roach        }
39506a438b4SGreg Roach
39606a438b4SGreg Roach        // Is this individual one of our ancestors?
39706a438b4SGreg Roach        $sosa = array_search($individual, $ancestors, true);
39806a438b4SGreg Roach        if (is_int($sosa) && $module instanceof RelationshipsChartModule) {
39906a438b4SGreg Roach            $sosa_class = 'search_hit';
40006a438b4SGreg Roach            $sosa_html  = '<a class="small ' . $individual->getBoxStyle() . '" href="' . e($module->chartUrl($individual, ['xref2' => $individuals[1]->xref()])) . '" rel="nofollow" title="' . I18N::translate('Relationships') . '">' . I18N::number($sosa) . '</a>' . self::sosaGeneration($sosa);
40106a438b4SGreg Roach        } else {
40206a438b4SGreg Roach            $sosa_class = '';
40306a438b4SGreg Roach            $sosa_html  = '';
40406a438b4SGreg Roach        }
40506a438b4SGreg Roach
40606a438b4SGreg Roach        // Generate HTML for this individual, and all their descendants
40706a438b4SGreg Roach        $indi_html = '<small>' . view('icons/sex', ['sex' => $individual->sex()]) . '</small><a class="' . $sosa_class . '" href="' . e($individual->url()) . '">' . $person_name . '</a> ' . $individual->lifespan() . $sosa_html;
40806a438b4SGreg Roach
40906a438b4SGreg Roach        // If this is not a birth pedigree (e.g. an adoption), highlight it
41006a438b4SGreg Roach        if ($parents) {
41106a438b4SGreg Roach            foreach ($individual->facts(['FAMC']) as $fact) {
41206a438b4SGreg Roach                if ($fact->target() === $parents) {
41306a438b4SGreg Roach                    $pedi = $fact->attribute('PEDI');
414*fd307386SGreg Roach
415*fd307386SGreg Roach                    if ($pedi !== '' && $pedi !== 'birth') {
416*fd307386SGreg Roach                        $pedigree  = GedcomCodePedi::getValue($pedi, $individual);
417*fd307386SGreg Roach                        $indi_html = '<span class="red">' . $pedigree . '</span> ' . $indi_html;
418*fd307386SGreg Roach                    }
41906a438b4SGreg Roach                    break;
42006a438b4SGreg Roach                }
42106a438b4SGreg Roach            }
42206a438b4SGreg Roach        }
42306a438b4SGreg Roach
42406a438b4SGreg Roach        // spouses and children
42506a438b4SGreg Roach        $spouse_families = $individual->spouseFamilies()
42606a438b4SGreg Roach            ->sort(Family::marriageDateComparator());
42706a438b4SGreg Roach
42806a438b4SGreg Roach        if ($spouse_families->isNotEmpty()) {
42906a438b4SGreg Roach            $fam_html = '';
43006a438b4SGreg Roach            foreach ($spouse_families as $family) {
43106a438b4SGreg Roach                $fam_html .= $indi_html; // Repeat the individual details for each spouse.
43206a438b4SGreg Roach
43306a438b4SGreg Roach                $spouse = $family->spouse($individual);
43406a438b4SGreg Roach                if ($spouse instanceof Individual) {
43506a438b4SGreg Roach                    $sosa = array_search($spouse, $ancestors, true);
43606a438b4SGreg Roach                    if (is_int($sosa) && $module instanceof RelationshipsChartModule) {
43706a438b4SGreg Roach                        $sosa_class = 'search_hit';
43806a438b4SGreg Roach                        $sosa_html  = '<a class="small ' . $spouse->getBoxStyle() . '" href="' . e($module->chartUrl($individual, ['xref2' => $individuals[1]->xref()])) . '" rel="nofollow" title="' . I18N::translate('Relationships') . '">' . I18N::number($sosa) . '</a>' . self::sosaGeneration($sosa);
43906a438b4SGreg Roach                    } else {
44006a438b4SGreg Roach                        $sosa_class = '';
44106a438b4SGreg Roach                        $sosa_html  = '';
44206a438b4SGreg Roach                    }
44306a438b4SGreg Roach                    $marriage_year = $family->getMarriageYear();
44406a438b4SGreg Roach                    if ($marriage_year) {
44506a438b4SGreg Roach                        $fam_html .= ' <a href="' . e($family->url()) . '" title="' . strip_tags($family->getMarriageDate()->display()) . '"><i class="icon-rings"></i>' . $marriage_year . '</a>';
44606a438b4SGreg Roach                    } elseif ($family->facts(['MARR'])->isNotEmpty()) {
44706a438b4SGreg Roach                        $fam_html .= ' <a href="' . e($family->url()) . '" title="' . I18N::translate('Marriage') . '"><i class="icon-rings"></i></a>';
44806a438b4SGreg Roach                    } else {
44906a438b4SGreg Roach                        $fam_html .= ' <a href="' . e($family->url()) . '" title="' . I18N::translate('Not married') . '"><i class="icon-rings"></i></a>';
45006a438b4SGreg Roach                    }
45106a438b4SGreg Roach                    $fam_html .= ' <small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small><a class="' . $sosa_class . '" href="' . e($spouse->url()) . '">' . $spouse->fullName() . '</a> ' . $spouse->lifespan() . ' ' . $sosa_html;
45206a438b4SGreg Roach                }
45306a438b4SGreg Roach
45406a438b4SGreg Roach                $fam_html .= '<ol>';
45506a438b4SGreg Roach                foreach ($family->children() as $child) {
45606a438b4SGreg Roach                    $fam_html .= $this->getDescendantsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std, $child, $family);
45706a438b4SGreg Roach                }
45806a438b4SGreg Roach                $fam_html .= '</ol>';
45906a438b4SGreg Roach            }
46006a438b4SGreg Roach
46106a438b4SGreg Roach            return '<li>' . $fam_html . '</li>';
46206a438b4SGreg Roach        }
46306a438b4SGreg Roach
46406a438b4SGreg Roach        // No spouses - just show the individual
46506a438b4SGreg Roach        return '<li>' . $indi_html . '</li>';
46606a438b4SGreg Roach    }
46706a438b4SGreg Roach
46806a438b4SGreg Roach    /**
46906a438b4SGreg Roach     * Do two surnames match?
47006a438b4SGreg Roach     *
47106a438b4SGreg Roach     * @param string $surname1
47206a438b4SGreg Roach     * @param string $surname2
47306a438b4SGreg Roach     * @param bool   $soundex_std
47406a438b4SGreg Roach     * @param bool   $soundex_dm
47506a438b4SGreg Roach     *
47606a438b4SGreg Roach     * @return bool
47706a438b4SGreg Roach     */
47806a438b4SGreg Roach    private function surnamesMatch(string $surname1, string $surname2, bool $soundex_std, bool $soundex_dm): bool
47906a438b4SGreg Roach    {
48006a438b4SGreg Roach        // One name sounds like another?
48106a438b4SGreg Roach        if ($soundex_std && Soundex::compare(Soundex::russell($surname1), Soundex::russell($surname2))) {
48206a438b4SGreg Roach            return true;
48306a438b4SGreg Roach        }
48406a438b4SGreg Roach        if ($soundex_dm && Soundex::compare(Soundex::daitchMokotoff($surname1), Soundex::daitchMokotoff($surname2))) {
48506a438b4SGreg Roach            return true;
48606a438b4SGreg Roach        }
48706a438b4SGreg Roach
48806a438b4SGreg Roach        // One is a substring of the other.  e.g. Halen / Van Halen
48906a438b4SGreg Roach        return stripos($surname1, $surname2) !== false || stripos($surname2, $surname1) !== false;
49006a438b4SGreg Roach    }
49106a438b4SGreg Roach
49206a438b4SGreg Roach    /**
49306a438b4SGreg Roach     * Convert a SOSA number into a generation number. e.g. 8 = great-grandfather = 3 generations
49406a438b4SGreg Roach     *
49506a438b4SGreg Roach     * @param int $sosa
49606a438b4SGreg Roach     *
49706a438b4SGreg Roach     * @return string
49806a438b4SGreg Roach     */
49906a438b4SGreg Roach    private static function sosaGeneration($sosa): string
50006a438b4SGreg Roach    {
50106a438b4SGreg Roach        $generation = (int) log($sosa, 2) + 1;
50206a438b4SGreg Roach
50306a438b4SGreg Roach        return '<sup title="' . I18N::translate('Generation') . '">' . $generation . '</sup>';
50406a438b4SGreg Roach    }
50567992b6aSRichard Cissee}
506