xref: /webtrees/app/Module/BranchesListModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1667992b6aSRichard Cissee */
17fcfa147eSGreg Roach
1867992b6aSRichard Cisseedeclare(strict_types=1);
1967992b6aSRichard Cissee
2067992b6aSRichard Cisseenamespace Fisharebest\Webtrees\Module;
2167992b6aSRichard Cissee
2206a438b4SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
23808ea2d4SGreg Roachuse Fisharebest\Webtrees\Auth;
2406a438b4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
256f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
267c29ac65SGreg Roachuse Fisharebest\Webtrees\Elements\PedigreeLinkageType;
2706a438b4SGreg Roachuse Fisharebest\Webtrees\Family;
2806a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2967992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
3006a438b4SGreg Roachuse Fisharebest\Webtrees\Individual;
3109482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
3267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
3306a438b4SGreg Roachuse Fisharebest\Webtrees\Soundex;
3467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
35b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
3606a438b4SGreg Roachuse Illuminate\Database\Query\Builder;
3706a438b4SGreg Roachuse Illuminate\Database\Query\JoinClause;
386ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
396ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
4006a438b4SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
41f3874e19SGreg Roach
4206a438b4SGreg Roachuse function array_search;
4306a438b4SGreg Roachuse function e;
4406a438b4SGreg Roachuse function explode;
4506a438b4SGreg Roachuse function in_array;
4606a438b4SGreg Roachuse function is_int;
4706a438b4SGreg Roachuse function key;
4806a438b4SGreg Roachuse function log;
4906a438b4SGreg Roachuse function next;
50808ea2d4SGreg Roachuse function redirect;
51808ea2d4SGreg Roachuse function route;
5206a438b4SGreg Roachuse function strip_tags;
5306a438b4SGreg Roachuse function stripos;
54382922aaSGreg Roachuse function strtolower;
5506a438b4SGreg Roachuse function usort;
5606a438b4SGreg Roachuse function view;
5767992b6aSRichard Cissee
5867992b6aSRichard Cissee/**
5967992b6aSRichard Cissee * Class BranchesListModule
6067992b6aSRichard Cissee */
6106a438b4SGreg Roachclass BranchesListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
6267992b6aSRichard Cissee{
6367992b6aSRichard Cissee    use ModuleListTrait;
6467992b6aSRichard Cissee
6506a438b4SGreg Roach    protected const ROUTE_URL = '/tree/{tree}/branches{/surname}';
6606a438b4SGreg Roach
6743f2f523SGreg Roach    private ModuleService $module_service;
6806a438b4SGreg Roach
6906a438b4SGreg Roach    /**
7006a438b4SGreg Roach     * @param ModuleService $module_service
7106a438b4SGreg Roach     */
7206a438b4SGreg Roach    public function __construct(ModuleService $module_service)
7306a438b4SGreg Roach    {
7406a438b4SGreg Roach        $this->module_service = $module_service;
7506a438b4SGreg Roach    }
7606a438b4SGreg Roach
7706a438b4SGreg Roach    /**
7806a438b4SGreg Roach     * Initialization.
7906a438b4SGreg Roach     *
8006a438b4SGreg Roach     * @return void
8106a438b4SGreg Roach     */
8206a438b4SGreg Roach    public function boot(): void
8306a438b4SGreg Roach    {
84158900c2SGreg Roach        Registry::routeFactory()->routeMap()
8506a438b4SGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
8606a438b4SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
8706a438b4SGreg Roach    }
8806a438b4SGreg Roach
8967992b6aSRichard Cissee    /**
900cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
9167992b6aSRichard Cissee     *
9267992b6aSRichard Cissee     * @return string
9367992b6aSRichard Cissee     */
9467992b6aSRichard Cissee    public function title(): string
9567992b6aSRichard Cissee    {
9667992b6aSRichard Cissee        /* I18N: Name of a module/list */
9767992b6aSRichard Cissee        return I18N::translate('Branches');
9867992b6aSRichard Cissee    }
9967992b6aSRichard Cissee
10067992b6aSRichard Cissee    public function description(): string
10167992b6aSRichard Cissee    {
102b5e8e56bSGreg Roach        /* I18N: Description of the “Branches” module */
10367992b6aSRichard Cissee        return I18N::translate('A list of branches of a family.');
10467992b6aSRichard Cissee    }
10567992b6aSRichard Cissee
10667992b6aSRichard Cissee    /**
10767992b6aSRichard Cissee     * CSS class for the URL.
10867992b6aSRichard Cissee     *
10967992b6aSRichard Cissee     * @return string
11067992b6aSRichard Cissee     */
11167992b6aSRichard Cissee    public function listMenuClass(): string
11267992b6aSRichard Cissee    {
11367992b6aSRichard Cissee        return 'menu-branches';
11467992b6aSRichard Cissee    }
11567992b6aSRichard Cissee
1164db4b4a9SGreg Roach    /**
1174db4b4a9SGreg Roach     * @param Tree                                      $tree
11876d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
1194db4b4a9SGreg Roach     *
1204db4b4a9SGreg Roach     * @return string
1214db4b4a9SGreg Roach     */
12267992b6aSRichard Cissee    public function listUrl(Tree $tree, array $parameters = []): string
12367992b6aSRichard Cissee    {
124d35568b4SGreg Roach        $request = Registry::container()->get(ServerRequestInterface::class);
125b55cbc6bSGreg Roach        $xref    = Validator::attributes($request)->isXref()->string('xref', '');
12606a438b4SGreg Roach
12706a438b4SGreg Roach        if ($xref !== '') {
1286b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($xref, $tree);
12906a438b4SGreg Roach
13006a438b4SGreg Roach            if ($individual instanceof Individual && $individual->canShow()) {
1313529c469SGreg Roach                $parameters['surname'] ??= $individual->getAllNames()[0]['surn'] ?? null;
13206a438b4SGreg Roach            }
13367992b6aSRichard Cissee        }
13467992b6aSRichard Cissee
13506a438b4SGreg Roach        $parameters['tree'] = $tree->name();
1365229eadeSGreg Roach
13706a438b4SGreg Roach        return route(static::class, $parameters);
13867992b6aSRichard Cissee    }
13967992b6aSRichard Cissee
1404db4b4a9SGreg Roach    /**
14124f2a3afSGreg Roach     * @return array<string>
1424db4b4a9SGreg Roach     */
14367992b6aSRichard Cissee    public function listUrlAttributes(): array
14467992b6aSRichard Cissee    {
14567992b6aSRichard Cissee        return [];
14667992b6aSRichard Cissee    }
14706a438b4SGreg Roach
14806a438b4SGreg Roach    /**
14906a438b4SGreg Roach     * @param ServerRequestInterface $request
15006a438b4SGreg Roach     *
15106a438b4SGreg Roach     * @return ResponseInterface
15206a438b4SGreg Roach     */
15306a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
15406a438b4SGreg Roach    {
155b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
156b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
15706a438b4SGreg Roach
15806a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
15906a438b4SGreg Roach
16006a438b4SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
16106a438b4SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
162748dbe15SGreg Roach            return redirect($this->listUrl($tree, [
163748dbe15SGreg Roach                'soundex_dm'  => Validator::parsedBody($request)->boolean('soundex_dm', false),
164748dbe15SGreg Roach                'soundex_std' => Validator::parsedBody($request)->boolean('soundex_std', false),
165748dbe15SGreg Roach                'surname'     => Validator::parsedBody($request)->string('surname'),
166748dbe15SGreg Roach            ]));
16706a438b4SGreg Roach        }
16806a438b4SGreg Roach
169748dbe15SGreg Roach        $surname     = Validator::attributes($request)->string('surname', '');
170748dbe15SGreg Roach        $soundex_std = Validator::queryParams($request)->boolean('soundex_std', false);
171748dbe15SGreg Roach        $soundex_dm  = Validator::queryParams($request)->boolean('soundex_dm', false);
172b55cbc6bSGreg Roach        $ajax        = Validator::queryParams($request)->boolean('ajax', false);
17306a438b4SGreg Roach
174b55cbc6bSGreg Roach        if ($ajax) {
17506a438b4SGreg Roach            $this->layout = 'layouts/ajax';
17606a438b4SGreg Roach
17706a438b4SGreg Roach            // Highlight direct-line ancestors of this individual.
1781fe542e9SGreg Roach            $xref = $tree->getUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF);
1796b9cb339SGreg Roach            $self = Registry::individualFactory()->make($xref, $tree);
18006a438b4SGreg Roach
18106a438b4SGreg Roach            if ($surname !== '') {
18206a438b4SGreg Roach                $individuals = $this->loadIndividuals($tree, $surname, $soundex_dm, $soundex_std);
18306a438b4SGreg Roach            } else {
18406a438b4SGreg Roach                $individuals = [];
18506a438b4SGreg Roach            }
18606a438b4SGreg Roach
18706a438b4SGreg Roach            if ($self instanceof Individual) {
18806a438b4SGreg Roach                $ancestors = $this->allAncestors($self);
18906a438b4SGreg Roach            } else {
19006a438b4SGreg Roach                $ancestors = [];
19106a438b4SGreg Roach            }
19206a438b4SGreg Roach
19306a438b4SGreg Roach            return $this->viewResponse('modules/branches/list', [
19406a438b4SGreg Roach                'branches' => $this->getPatriarchsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std),
19506a438b4SGreg Roach            ]);
19606a438b4SGreg Roach        }
19706a438b4SGreg Roach
19806a438b4SGreg Roach        if ($surname !== '') {
19906a438b4SGreg Roach            /* I18N: %s is a surname */
20006a438b4SGreg Roach            $title = I18N::translate('Branches of the %s family', e($surname));
20106a438b4SGreg Roach
202748dbe15SGreg Roach            $ajax_url = $this->listUrl($tree, [
203748dbe15SGreg Roach                'ajax'        => true,
204748dbe15SGreg Roach                'soundex_dm'  => $soundex_dm,
205748dbe15SGreg Roach                'soundex_std' => $soundex_std,
206748dbe15SGreg Roach                'surname'     => $surname,
207748dbe15SGreg Roach            ]);
20806a438b4SGreg Roach        } else {
20906a438b4SGreg Roach            /* I18N: Branches of a family tree */
21006a438b4SGreg Roach            $title = I18N::translate('Branches');
21106a438b4SGreg Roach
21206a438b4SGreg Roach            $ajax_url = '';
21306a438b4SGreg Roach        }
21406a438b4SGreg Roach
21506a438b4SGreg Roach        return $this->viewResponse('branches-page', [
21606a438b4SGreg Roach            'ajax_url'    => $ajax_url,
21706a438b4SGreg Roach            'soundex_dm'  => $soundex_dm,
21806a438b4SGreg Roach            'soundex_std' => $soundex_std,
21906a438b4SGreg Roach            'surname'     => $surname,
22006a438b4SGreg Roach            'title'       => $title,
22106a438b4SGreg Roach            'tree'        => $tree,
22206a438b4SGreg Roach        ]);
22306a438b4SGreg Roach    }
22406a438b4SGreg Roach
22506a438b4SGreg Roach    /**
22606a438b4SGreg Roach     * Find all ancestors of an individual, indexed by the Sosa-Stradonitz number.
22706a438b4SGreg Roach     *
22806a438b4SGreg Roach     * @param Individual $individual
22906a438b4SGreg Roach     *
23009482a55SGreg Roach     * @return array<Individual>
23106a438b4SGreg Roach     */
23206a438b4SGreg Roach    private function allAncestors(Individual $individual): array
23306a438b4SGreg Roach    {
23406a438b4SGreg Roach        $ancestors = [
23506a438b4SGreg Roach            1 => $individual,
23606a438b4SGreg Roach        ];
23706a438b4SGreg Roach
23806a438b4SGreg Roach        do {
23906a438b4SGreg Roach            $sosa = key($ancestors);
24006a438b4SGreg Roach
24106a438b4SGreg Roach            $family = $ancestors[$sosa]->childFamilies()->first();
24206a438b4SGreg Roach
24306a438b4SGreg Roach            if ($family !== null) {
24406a438b4SGreg Roach                if ($family->husband() !== null) {
24506a438b4SGreg Roach                    $ancestors[$sosa * 2] = $family->husband();
24606a438b4SGreg Roach                }
24706a438b4SGreg Roach                if ($family->wife() !== null) {
24806a438b4SGreg Roach                    $ancestors[$sosa * 2 + 1] = $family->wife();
24906a438b4SGreg Roach                }
25006a438b4SGreg Roach            }
25106a438b4SGreg Roach        } while (next($ancestors));
25206a438b4SGreg Roach
25306a438b4SGreg Roach        return $ancestors;
25406a438b4SGreg Roach    }
25506a438b4SGreg Roach
25606a438b4SGreg Roach    /**
25706a438b4SGreg Roach     * Fetch all individuals with a matching surname
25806a438b4SGreg Roach     *
25906a438b4SGreg Roach     * @param Tree   $tree
26006a438b4SGreg Roach     * @param string $surname
26106a438b4SGreg Roach     * @param bool   $soundex_dm
26206a438b4SGreg Roach     * @param bool   $soundex_std
26306a438b4SGreg Roach     *
26409482a55SGreg Roach     * @return array<Individual>
26506a438b4SGreg Roach     */
26606a438b4SGreg Roach    private function loadIndividuals(Tree $tree, string $surname, bool $soundex_dm, bool $soundex_std): array
26706a438b4SGreg Roach    {
26806a438b4SGreg Roach        $individuals = DB::table('individuals')
26906a438b4SGreg Roach            ->join('name', static function (JoinClause $join): void {
27006a438b4SGreg Roach                $join
27106a438b4SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
27206a438b4SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
27306a438b4SGreg Roach            })
27406a438b4SGreg Roach            ->where('i_file', '=', $tree->id())
27506a438b4SGreg Roach            ->where('n_type', '<>', '_MARNM')
27606a438b4SGreg Roach            ->where(static function (Builder $query) use ($surname, $soundex_dm, $soundex_std): void {
27706a438b4SGreg Roach                $query
27806a438b4SGreg Roach                    ->where('n_surn', '=', $surname)
27906a438b4SGreg Roach                    ->orWhere('n_surname', '=', $surname);
28006a438b4SGreg Roach
28106a438b4SGreg Roach                if ($soundex_std) {
28206a438b4SGreg Roach                    $sdx = Soundex::russell($surname);
28306a438b4SGreg Roach                    if ($sdx !== '') {
28406a438b4SGreg Roach                        foreach (explode(':', $sdx) as $value) {
28506a438b4SGreg Roach                            $query->orWhere('n_soundex_surn_std', 'LIKE', '%' . $value . '%');
28606a438b4SGreg Roach                        }
28706a438b4SGreg Roach                    }
28806a438b4SGreg Roach                }
28906a438b4SGreg Roach
29006a438b4SGreg Roach                if ($soundex_dm) {
29106a438b4SGreg Roach                    $sdx = Soundex::daitchMokotoff($surname);
29206a438b4SGreg Roach                    if ($sdx !== '') {
29306a438b4SGreg Roach                        foreach (explode(':', $sdx) as $value) {
29406a438b4SGreg Roach                            $query->orWhere('n_soundex_surn_dm', 'LIKE', '%' . $value . '%');
29506a438b4SGreg Roach                        }
29606a438b4SGreg Roach                    }
29706a438b4SGreg Roach                }
29806a438b4SGreg Roach            })
29906a438b4SGreg Roach            ->distinct()
3000aed7de4SGreg Roach            ->select(['individuals.*'])
30106a438b4SGreg Roach            ->get()
3026b9cb339SGreg Roach            ->map(Registry::individualFactory()->mapper($tree))
30306a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter())
30406a438b4SGreg Roach            ->all();
30506a438b4SGreg Roach
30606a438b4SGreg Roach        usort($individuals, Individual::birthDateComparator());
30706a438b4SGreg Roach
30806a438b4SGreg Roach        return $individuals;
30906a438b4SGreg Roach    }
31006a438b4SGreg Roach
31106a438b4SGreg Roach    /**
31206a438b4SGreg Roach     * For each individual with no ancestors, list their descendants.
31306a438b4SGreg Roach     *
31406a438b4SGreg Roach     * @param Tree              $tree
31509482a55SGreg Roach     * @param array<Individual> $individuals
31609482a55SGreg Roach     * @param array<Individual> $ancestors
31706a438b4SGreg Roach     * @param string            $surname
31806a438b4SGreg Roach     * @param bool              $soundex_dm
31906a438b4SGreg Roach     * @param bool              $soundex_std
32006a438b4SGreg Roach     *
32106a438b4SGreg Roach     * @return string
32206a438b4SGreg Roach     */
32306a438b4SGreg Roach    private function getPatriarchsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std): string
32406a438b4SGreg Roach    {
32506a438b4SGreg Roach        $html = '';
32606a438b4SGreg Roach        foreach ($individuals as $individual) {
32706a438b4SGreg Roach            foreach ($individual->childFamilies() as $family) {
32806a438b4SGreg Roach                foreach ($family->spouses() as $parent) {
32906a438b4SGreg Roach                    if (in_array($parent, $individuals, true)) {
33006a438b4SGreg Roach                        continue 3;
33106a438b4SGreg Roach                    }
33206a438b4SGreg Roach                }
33306a438b4SGreg Roach            }
33406a438b4SGreg Roach            $html .= $this->getDescendantsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std, $individual, null);
33506a438b4SGreg Roach        }
33606a438b4SGreg Roach
33706a438b4SGreg Roach        return $html;
33806a438b4SGreg Roach    }
33906a438b4SGreg Roach
34006a438b4SGreg Roach    /**
34106a438b4SGreg Roach     * Generate a recursive list of descendants of an individual.
34206a438b4SGreg Roach     * If parents are specified, we can also show the pedigree (adopted, etc.).
34306a438b4SGreg Roach     *
34406a438b4SGreg Roach     * @param Tree              $tree
34509482a55SGreg Roach     * @param array<Individual> $individuals
34609482a55SGreg Roach     * @param array<Individual> $ancestors
34706a438b4SGreg Roach     * @param string            $surname
34806a438b4SGreg Roach     * @param bool              $soundex_dm
34906a438b4SGreg Roach     * @param bool              $soundex_std
35006a438b4SGreg Roach     * @param Individual        $individual
35106a438b4SGreg Roach     * @param Family|null       $parents
35206a438b4SGreg Roach     *
35306a438b4SGreg Roach     * @return string
35406a438b4SGreg Roach     */
3552c6f1bd5SGreg Roach    private function getDescendantsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std, Individual $individual, Family|null $parents = null): string
35606a438b4SGreg Roach    {
357*f25fc0f9SGreg Roach        $module = $this->module_service
358*f25fc0f9SGreg Roach            ->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
359*f25fc0f9SGreg Roach            ->first(static fn (ModuleInterface $module) => $module instanceof RelationshipsChartModule);
36006a438b4SGreg Roach
36106a438b4SGreg Roach        // A person has many names. Select the one that matches the searched surname
36206a438b4SGreg Roach        $person_name = '';
36306a438b4SGreg Roach        foreach ($individual->getAllNames() as $name) {
36406a438b4SGreg Roach            [$surn1] = explode(',', $name['sort']);
36506a438b4SGreg Roach            if ($this->surnamesMatch($surn1, $surname, $soundex_std, $soundex_dm)) {
36606a438b4SGreg Roach                $person_name = $name['full'];
36706a438b4SGreg Roach                break;
36806a438b4SGreg Roach            }
36906a438b4SGreg Roach        }
37006a438b4SGreg Roach
37106a438b4SGreg Roach        // No matching name? Typically children with a different surname. The branch stops here.
37206a438b4SGreg Roach        if ($person_name === '') {
373f773eef0SGreg Roach            return '<li title="' . strip_tags($individual->fullName()) . '" class="wt-branch-split"><small>' . view('icons/sex', ['sex' => $individual->sex()]) . '</small>…</li>';
37406a438b4SGreg Roach        }
37506a438b4SGreg Roach
37606a438b4SGreg Roach        // Is this individual one of our ancestors?
37706a438b4SGreg Roach        $sosa = array_search($individual, $ancestors, true);
37806a438b4SGreg Roach        if (is_int($sosa) && $module instanceof RelationshipsChartModule) {
37906a438b4SGreg Roach            $sosa_class = 'search_hit';
380382922aaSGreg Roach            $sosa_html  = ' <a class="small wt-chart-box-' . strtolower($individual->sex()) . '" href="' . e($module->chartUrl($individual, ['xref2' => $ancestors[1]->xref()])) . '" rel="nofollow" title="' . I18N::translate('Relationship') . '">' . I18N::number($sosa) . '</a>' . self::sosaGeneration($sosa);
38106a438b4SGreg Roach        } else {
38206a438b4SGreg Roach            $sosa_class = '';
38306a438b4SGreg Roach            $sosa_html  = '';
38406a438b4SGreg Roach        }
38506a438b4SGreg Roach
38606a438b4SGreg Roach        // Generate HTML for this individual, and all their descendants
38706a438b4SGreg 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;
38806a438b4SGreg Roach
38906a438b4SGreg Roach        // If this is not a birth pedigree (e.g. an adoption), highlight it
390b6ec1ccfSGreg Roach        if ($parents instanceof Family) {
39106a438b4SGreg Roach            foreach ($individual->facts(['FAMC']) as $fact) {
39206a438b4SGreg Roach                if ($fact->target() === $parents) {
39306a438b4SGreg Roach                    $pedi = $fact->attribute('PEDI');
394fd307386SGreg Roach
39588a03560SGreg Roach                    if ($pedi !== '' && $pedi !== PedigreeLinkageType::VALUE_BIRTH) {
396c2ed51d1SGreg Roach                        $pedigree  = Registry::elementFactory()->make('INDI:FAMC:PEDI')->value($pedi, $tree);
397fd307386SGreg Roach                        $indi_html = '<span class="red">' . $pedigree . '</span> ' . $indi_html;
398fd307386SGreg Roach                    }
39906a438b4SGreg Roach                    break;
40006a438b4SGreg Roach                }
40106a438b4SGreg Roach            }
40206a438b4SGreg Roach        }
40306a438b4SGreg Roach
40406a438b4SGreg Roach        // spouses and children
40506a438b4SGreg Roach        $spouse_families = $individual->spouseFamilies()
40606a438b4SGreg Roach            ->sort(Family::marriageDateComparator());
40706a438b4SGreg Roach
40806a438b4SGreg Roach        if ($spouse_families->isNotEmpty()) {
40906a438b4SGreg Roach            $fam_html = '';
41006a438b4SGreg Roach            foreach ($spouse_families as $family) {
41106a438b4SGreg Roach                $fam_html .= $indi_html; // Repeat the individual details for each spouse.
41206a438b4SGreg Roach
41306a438b4SGreg Roach                $spouse = $family->spouse($individual);
41406a438b4SGreg Roach                if ($spouse instanceof Individual) {
41506a438b4SGreg Roach                    $sosa = array_search($spouse, $ancestors, true);
41606a438b4SGreg Roach                    if (is_int($sosa) && $module instanceof RelationshipsChartModule) {
41706a438b4SGreg Roach                        $sosa_class = 'search_hit';
418382922aaSGreg Roach                        $sosa_html  = ' <a class="small wt-chart-box-' . strtolower($spouse->sex()) . '" href="' . e($module->chartUrl($spouse, ['xref2' => $ancestors[1]->xref()])) . '" rel="nofollow" title="' . I18N::translate('Relationship') . '">' . I18N::number($sosa) . '</a>' . self::sosaGeneration($sosa);
41906a438b4SGreg Roach                    } else {
42006a438b4SGreg Roach                        $sosa_class = '';
42106a438b4SGreg Roach                        $sosa_html  = '';
42206a438b4SGreg Roach                    }
42306a438b4SGreg Roach                    $marriage_year = $family->getMarriageYear();
42406a438b4SGreg Roach                    if ($marriage_year) {
42506a438b4SGreg Roach                        $fam_html .= ' <a href="' . e($family->url()) . '" title="' . strip_tags($family->getMarriageDate()->display()) . '"><i class="icon-rings"></i>' . $marriage_year . '</a>';
42606a438b4SGreg Roach                    } elseif ($family->facts(['MARR'])->isNotEmpty()) {
42706a438b4SGreg Roach                        $fam_html .= ' <a href="' . e($family->url()) . '" title="' . I18N::translate('Marriage') . '"><i class="icon-rings"></i></a>';
42806a438b4SGreg Roach                    } else {
42906a438b4SGreg Roach                        $fam_html .= ' <a href="' . e($family->url()) . '" title="' . I18N::translate('Not married') . '"><i class="icon-rings"></i></a>';
43006a438b4SGreg Roach                    }
43106a438b4SGreg 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;
43206a438b4SGreg Roach                }
43306a438b4SGreg Roach
43406a438b4SGreg Roach                $fam_html .= '<ol>';
43506a438b4SGreg Roach                foreach ($family->children() as $child) {
43606a438b4SGreg Roach                    $fam_html .= $this->getDescendantsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std, $child, $family);
43706a438b4SGreg Roach                }
43806a438b4SGreg Roach                $fam_html .= '</ol>';
43906a438b4SGreg Roach            }
44006a438b4SGreg Roach
44106a438b4SGreg Roach            return '<li>' . $fam_html . '</li>';
44206a438b4SGreg Roach        }
44306a438b4SGreg Roach
44406a438b4SGreg Roach        // No spouses - just show the individual
44506a438b4SGreg Roach        return '<li>' . $indi_html . '</li>';
44606a438b4SGreg Roach    }
44706a438b4SGreg Roach
44806a438b4SGreg Roach    /**
44906a438b4SGreg Roach     * Do two surnames match?
45006a438b4SGreg Roach     *
45106a438b4SGreg Roach     * @param string $surname1
45206a438b4SGreg Roach     * @param string $surname2
45306a438b4SGreg Roach     * @param bool   $soundex_std
45406a438b4SGreg Roach     * @param bool   $soundex_dm
45506a438b4SGreg Roach     *
45606a438b4SGreg Roach     * @return bool
45706a438b4SGreg Roach     */
45806a438b4SGreg Roach    private function surnamesMatch(string $surname1, string $surname2, bool $soundex_std, bool $soundex_dm): bool
45906a438b4SGreg Roach    {
46006a438b4SGreg Roach        // One name sounds like another?
46106a438b4SGreg Roach        if ($soundex_std && Soundex::compare(Soundex::russell($surname1), Soundex::russell($surname2))) {
46206a438b4SGreg Roach            return true;
46306a438b4SGreg Roach        }
46406a438b4SGreg Roach        if ($soundex_dm && Soundex::compare(Soundex::daitchMokotoff($surname1), Soundex::daitchMokotoff($surname2))) {
46506a438b4SGreg Roach            return true;
46606a438b4SGreg Roach        }
46706a438b4SGreg Roach
46806a438b4SGreg Roach        // One is a substring of the other.  e.g. Halen / Van Halen
46906a438b4SGreg Roach        return stripos($surname1, $surname2) !== false || stripos($surname2, $surname1) !== false;
47006a438b4SGreg Roach    }
47106a438b4SGreg Roach
47206a438b4SGreg Roach    /**
47306a438b4SGreg Roach     * Convert a SOSA number into a generation number. e.g. 8 = great-grandfather = 3 generations
47406a438b4SGreg Roach     *
47506a438b4SGreg Roach     * @param int $sosa
47606a438b4SGreg Roach     *
47706a438b4SGreg Roach     * @return string
47806a438b4SGreg Roach     */
47924f2a3afSGreg Roach    private static function sosaGeneration(int $sosa): string
48006a438b4SGreg Roach    {
48106a438b4SGreg Roach        $generation = (int) log($sosa, 2) + 1;
48206a438b4SGreg Roach
48306a438b4SGreg Roach        return '<sup title="' . I18N::translate('Generation') . '">' . $generation . '</sup>';
48406a438b4SGreg Roach    }
48567992b6aSRichard Cissee}
486