xref: /webtrees/app/Individual.php (revision 36779af1bd0601de7819554b13a393f6edb92507)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
51fe542e9SGreg Roach * Copyright (C) 2021 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * 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/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
23a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
241fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
25852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
262e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2739ca88baSGreg Roachuse Illuminate\Support\Collection;
28a25f0a04SGreg Roach
297d70e4a7SGreg Roachuse function preg_match;
307d70e4a7SGreg Roach
31a25f0a04SGreg Roach/**
3276692c8bSGreg Roach * A GEDCOM individual (INDI) object.
33a25f0a04SGreg Roach */
34c1010edaSGreg Roachclass Individual extends GedcomRecord
35c1010edaSGreg Roach{
3616d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3716d6367aSGreg Roach
388fb4e87cSGreg Roach    // Placeholders to indicate unknown names
398fb4e87cSGreg Roach    public const NOMEN_NESCIO     = '@N.N.';
408fb4e87cSGreg Roach    public const PRAENOMEN_NESCIO = '@P.N.';
418fb4e87cSGreg Roach
42852ede8cSGreg Roach    protected const ROUTE_NAME = IndividualPage::class;
43a25f0a04SGreg Roach
447fa97a69SGreg Roach    /** Used in some lists to keep track of this individual’s generation in that list */
457fa97a69SGreg Roach    public ?int $generation = null;
46a25f0a04SGreg Roach
477fa97a69SGreg Roach    private ?Date $estimated_birth_date = null;
48a25f0a04SGreg Roach
497fa97a69SGreg Roach    private ?Date $estimated_death_date = null;
50a25f0a04SGreg Roach
51a25f0a04SGreg Roach    /**
52c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
53c156e8f5SGreg Roach     *
54c156e8f5SGreg Roach     * @return Closure
55c156e8f5SGreg Roach     */
56c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
57c156e8f5SGreg Roach    {
586c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
59c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
60c156e8f5SGreg Roach        };
61c156e8f5SGreg Roach    }
62c156e8f5SGreg Roach
63c156e8f5SGreg Roach    /**
64c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
65c156e8f5SGreg Roach     *
66c156e8f5SGreg Roach     * @return Closure
67c156e8f5SGreg Roach     */
68c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
69c156e8f5SGreg Roach    {
706c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
7110e21aa9SGreg Roach            return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
72c156e8f5SGreg Roach        };
73c156e8f5SGreg Roach    }
74c156e8f5SGreg Roach
75c156e8f5SGreg Roach    /**
76a25f0a04SGreg Roach     * Can the name of this record be shown?
77a25f0a04SGreg Roach     *
7876692c8bSGreg Roach     * @param int|null $access_level
7976692c8bSGreg Roach     *
8076692c8bSGreg Roach     * @return bool
81a25f0a04SGreg Roach     */
8235584196SGreg Roach    public function canShowName(int $access_level = null): bool
83c1010edaSGreg Roach    {
84d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
854b9ff166SGreg Roach
86f56b86d2SGreg Roach        return (int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
87a25f0a04SGreg Roach    }
88a25f0a04SGreg Roach
89a25f0a04SGreg Roach    /**
9076692c8bSGreg Roach     * Can this individual be shown?
91a25f0a04SGreg Roach     *
9276692c8bSGreg Roach     * @param int $access_level
9376692c8bSGreg Roach     *
9476692c8bSGreg Roach     * @return bool
95a25f0a04SGreg Roach     */
9635584196SGreg Roach    protected function canShowByType(int $access_level): bool
97c1010edaSGreg Roach    {
98a25f0a04SGreg Roach        // Dead people...
99f56b86d2SGreg Roach        if ((int) $this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
100a25f0a04SGreg Roach            $keep_alive             = false;
10154ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
102a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
103dce4f3a4SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER);
104a25f0a04SGreg Roach                foreach ($matches as $match) {
105a25f0a04SGreg Roach                    $date = new Date($match[1]);
106a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
107a25f0a04SGreg Roach                        $keep_alive = true;
108a25f0a04SGreg Roach                        break;
109a25f0a04SGreg Roach                    }
110a25f0a04SGreg Roach                }
111a25f0a04SGreg Roach            }
11254ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
113a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
114dce4f3a4SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER);
115a25f0a04SGreg Roach                foreach ($matches as $match) {
116a25f0a04SGreg Roach                    $date = new Date($match[1]);
117a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
118a25f0a04SGreg Roach                        $keep_alive = true;
119a25f0a04SGreg Roach                        break;
120a25f0a04SGreg Roach                    }
121a25f0a04SGreg Roach                }
122a25f0a04SGreg Roach            }
123a25f0a04SGreg Roach            if (!$keep_alive) {
124a25f0a04SGreg Roach                return true;
125a25f0a04SGreg Roach            }
126a25f0a04SGreg Roach        }
127a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
1281fe542e9SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH);
1291fe542e9SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
1307c4add84SGreg Roach
13154ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1324b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
133a25f0a04SGreg Roach        }
134a25f0a04SGreg Roach
135a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1364b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
137a25f0a04SGreg Roach    }
138a25f0a04SGreg Roach
139a25f0a04SGreg Roach    /**
140a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
141a25f0a04SGreg Roach     *
142a25f0a04SGreg Roach     * @param Individual $target
143cbc1590aSGreg Roach     * @param int        $distance
144a25f0a04SGreg Roach     *
145cbc1590aSGreg Roach     * @return bool
146a25f0a04SGreg Roach     */
14724f2a3afSGreg Roach    private static function isRelated(Individual $target, int $distance): bool
148c1010edaSGreg Roach    {
149a25f0a04SGreg Roach        static $cache = null;
150a25f0a04SGreg Roach
1511fe542e9SGreg Roach        $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree);
152a25f0a04SGreg Roach        if ($user_individual) {
153a25f0a04SGreg Roach            if (!$cache) {
15413abd6f3SGreg Roach                $cache = [
15513abd6f3SGreg Roach                    0 => [$user_individual],
15613abd6f3SGreg Roach                    1 => [],
15713abd6f3SGreg Roach                ];
1588d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
159dc124885SGreg Roach                    $family = $fact->target();
160e24444eeSGreg Roach                    if ($family instanceof Family) {
161a25f0a04SGreg Roach                        $cache[1][] = $family;
162a25f0a04SGreg Roach                    }
163a25f0a04SGreg Roach                }
164a25f0a04SGreg Roach            }
165a25f0a04SGreg Roach        } else {
166a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
167a25f0a04SGreg Roach            return true;
168a25f0a04SGreg Roach        }
169a25f0a04SGreg Roach
170a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
171a25f0a04SGreg Roach        $distance *= 2;
172a25f0a04SGreg Roach
173a25f0a04SGreg Roach        // Consider each path length in turn
174a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
175a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
176a25f0a04SGreg Roach                // We have already calculated all records with this length
177e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
178a25f0a04SGreg Roach                    return true;
179a25f0a04SGreg Roach                }
180a25f0a04SGreg Roach            } else {
181a25f0a04SGreg Roach                // Need to calculate these paths
18213abd6f3SGreg Roach                $cache[$n] = [];
183e364afe4SGreg Roach                if ($n % 2 === 0) {
184a25f0a04SGreg Roach                    // Add FAM->INDI links
185a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
1868d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
187dc124885SGreg Roach                            $individual = $fact->target();
188a25f0a04SGreg Roach                            // Don’t backtrack
189e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
190a25f0a04SGreg Roach                                $cache[$n][] = $individual;
191a25f0a04SGreg Roach                            }
192a25f0a04SGreg Roach                        }
193a25f0a04SGreg Roach                    }
194a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
195a25f0a04SGreg Roach                        return true;
196a25f0a04SGreg Roach                    }
197a25f0a04SGreg Roach                } else {
198a25f0a04SGreg Roach                    // Add INDI->FAM links
199a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2008d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
201dc124885SGreg Roach                            $family = $fact->target();
202a25f0a04SGreg Roach                            // Don’t backtrack
203e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
204a25f0a04SGreg Roach                                $cache[$n][] = $family;
205a25f0a04SGreg Roach                            }
206a25f0a04SGreg Roach                        }
207a25f0a04SGreg Roach                    }
208a25f0a04SGreg Roach                }
209a25f0a04SGreg Roach            }
210a25f0a04SGreg Roach        }
211a25f0a04SGreg Roach
212a25f0a04SGreg Roach        return false;
213a25f0a04SGreg Roach    }
214a25f0a04SGreg Roach
21576692c8bSGreg Roach    /**
21676692c8bSGreg Roach     * Generate a private version of this record
21776692c8bSGreg Roach     *
21876692c8bSGreg Roach     * @param int $access_level
21976692c8bSGreg Roach     *
22076692c8bSGreg Roach     * @return string
22176692c8bSGreg Roach     */
2223c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
223c1010edaSGreg Roach    {
224acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
225a25f0a04SGreg Roach
226a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
227f56b86d2SGreg Roach        if ((int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
228a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2298d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
230138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
231a25f0a04SGreg Roach            }
232a25f0a04SGreg Roach        }
233a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2348d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
235a25f0a04SGreg Roach        foreach ($matches as $match) {
2366b9cb339SGreg Roach            $rela = Registry::familyFactory()->make($match[1], $this->tree);
237a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
238a25f0a04SGreg Roach                $rec .= $match[0];
239a25f0a04SGreg Roach            }
240a25f0a04SGreg Roach        }
241a25f0a04SGreg Roach        // Don’t privatize sex.
242a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
243a25f0a04SGreg Roach            $rec .= $match[0];
244a25f0a04SGreg Roach        }
245a25f0a04SGreg Roach
246a25f0a04SGreg Roach        return $rec;
247a25f0a04SGreg Roach    }
248a25f0a04SGreg Roach
24976692c8bSGreg Roach    /**
250a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
251a25f0a04SGreg Roach     * If not known to be dead, then assume living.
252a25f0a04SGreg Roach     *
253cbc1590aSGreg Roach     * @return bool
254a25f0a04SGreg Roach     */
2558f53f488SRico Sonntag    public function isDead(): bool
256c1010edaSGreg Roach    {
257c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
2584459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
259a25f0a04SGreg Roach
260a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
2618d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
262a25f0a04SGreg Roach            return true;
263a25f0a04SGreg Roach        }
264a25f0a04SGreg Roach
265a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
266a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
267a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
268a25f0a04SGreg Roach                $date = new Date($date_match);
269269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
270a25f0a04SGreg Roach                    return true;
271a25f0a04SGreg Roach                }
272a25f0a04SGreg Roach            }
273a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
274a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
275a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
276a25f0a04SGreg Roach                return false;
277a25f0a04SGreg Roach            }
278a25f0a04SGreg Roach        }
279a25f0a04SGreg Roach
280a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
281a25f0a04SGreg Roach
282a25f0a04SGreg Roach        // Check parents (birth and adopted)
28339ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
28439ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
285a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
286a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
287a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
288a25f0a04SGreg Roach                    $date = new Date($date_match);
289269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
290a25f0a04SGreg Roach                        return true;
291a25f0a04SGreg Roach                    }
292a25f0a04SGreg Roach                }
293a25f0a04SGreg Roach            }
294a25f0a04SGreg Roach        }
295a25f0a04SGreg Roach
296a25f0a04SGreg Roach        // Check spouses
29739ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
298a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
299a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
300a25f0a04SGreg Roach                $date = new Date($date_match);
301a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
302269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
303a25f0a04SGreg Roach                    return true;
304a25f0a04SGreg Roach                }
305a25f0a04SGreg Roach            }
306a25f0a04SGreg Roach            // Check spouse dates
30739ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
308a25f0a04SGreg Roach            if ($spouse) {
309a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
310a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
311a25f0a04SGreg Roach                    $date = new Date($date_match);
312a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
313269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
314a25f0a04SGreg Roach                        return true;
315a25f0a04SGreg Roach                    }
316a25f0a04SGreg Roach                }
317a25f0a04SGreg Roach            }
318a25f0a04SGreg Roach            // Check child dates
31939ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
320a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
321a25f0a04SGreg Roach                // Assume children born after age of 15
322a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
323a25f0a04SGreg Roach                    $date = new Date($date_match);
324269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
325a25f0a04SGreg Roach                        return true;
326a25f0a04SGreg Roach                    }
327a25f0a04SGreg Roach                }
328a25f0a04SGreg Roach                // Check grandchildren
32939ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
33039ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
331a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
332a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
333a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
334a25f0a04SGreg Roach                            $date = new Date($date_match);
335269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
336a25f0a04SGreg Roach                                return true;
337a25f0a04SGreg Roach                            }
338a25f0a04SGreg Roach                        }
339a25f0a04SGreg Roach                    }
340a25f0a04SGreg Roach                }
341a25f0a04SGreg Roach            }
342a25f0a04SGreg Roach        }
343a25f0a04SGreg Roach
344a25f0a04SGreg Roach        return false;
345a25f0a04SGreg Roach    }
346a25f0a04SGreg Roach
347a25f0a04SGreg Roach    /**
348a25f0a04SGreg Roach     * Find the highlighted media object for an individual
349a25f0a04SGreg Roach     *
350e364afe4SGreg Roach     * @return MediaFile|null
351a25f0a04SGreg Roach     */
352e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
353c1010edaSGreg Roach    {
35492647683SGreg Roach        $fact = $this->facts(['OBJE'])
35519d319e7SGreg Roach            ->first(static function (Fact $fact): bool {
356dc124885SGreg Roach                $media = $fact->target();
35719d319e7SGreg Roach
35819d319e7SGreg Roach                return $media instanceof Media && $media->firstImageFile() instanceof MediaFile;
35919d319e7SGreg Roach            });
36019d319e7SGreg Roach
36192647683SGreg Roach        if ($fact instanceof Fact && $fact->target() instanceof Media) {
36292647683SGreg Roach            return $fact->target()->firstImageFile();
363a25f0a04SGreg Roach        }
364a25f0a04SGreg Roach
365a25f0a04SGreg Roach        return null;
366a25f0a04SGreg Roach    }
367a25f0a04SGreg Roach
368a25f0a04SGreg Roach    /**
3697b0d562eSGreg Roach     * Display the preferred image for this individual.
370a25f0a04SGreg Roach     * Use an icon if no image is available.
371a25f0a04SGreg Roach     *
372dce07401SGreg Roach     * @param int           $width      Pixels
373dce07401SGreg Roach     * @param int           $height     Pixels
374dce07401SGreg Roach     * @param string        $fit        "crop" or "contain"
37509482a55SGreg Roach     * @param array<string> $attributes Additional HTML attributes
376dce07401SGreg Roach     *
377a25f0a04SGreg Roach     * @return string
378a25f0a04SGreg Roach     */
37924f2a3afSGreg Roach    public function displayImage(int $width, int $height, string $fit, array $attributes): string
380c1010edaSGreg Roach    {
3814a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
3824a9f750fSGreg Roach
3834a9f750fSGreg Roach        if ($media_file !== null) {
3844a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
385a25f0a04SGreg Roach        }
3864a9f750fSGreg Roach
3874a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
3881baf69deSGreg Roach            return '<i class="icon-silhouette icon-silhouette-' . strtolower($this->sex()) . '"></i>';
3894a9f750fSGreg Roach        }
3904a9f750fSGreg Roach
3914a9f750fSGreg Roach        return '';
392a25f0a04SGreg Roach    }
393a25f0a04SGreg Roach
394a25f0a04SGreg Roach    /**
395a25f0a04SGreg Roach     * Get the date of birth
396a25f0a04SGreg Roach     *
397a25f0a04SGreg Roach     * @return Date
398a25f0a04SGreg Roach     */
3998f53f488SRico Sonntag    public function getBirthDate(): Date
400c1010edaSGreg Roach    {
401a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
402a25f0a04SGreg Roach            if ($date->isOK()) {
403a25f0a04SGreg Roach                return $date;
404a25f0a04SGreg Roach            }
405a25f0a04SGreg Roach        }
406a25f0a04SGreg Roach
407a25f0a04SGreg Roach        return new Date('');
408a25f0a04SGreg Roach    }
409a25f0a04SGreg Roach
410a25f0a04SGreg Roach    /**
411a25f0a04SGreg Roach     * Get the place of birth
412a25f0a04SGreg Roach     *
41316d0b7f7SRico Sonntag     * @return Place
414a25f0a04SGreg Roach     */
4158f53f488SRico Sonntag    public function getBirthPlace(): Place
416c1010edaSGreg Roach    {
417a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
418a25f0a04SGreg Roach            return $place;
419a25f0a04SGreg Roach        }
420a25f0a04SGreg Roach
421b20ddbf9SGreg Roach        return new Place('', $this->tree);
422a25f0a04SGreg Roach    }
423a25f0a04SGreg Roach
424a25f0a04SGreg Roach    /**
425a25f0a04SGreg Roach     * Get the date of death
426a25f0a04SGreg Roach     *
427a25f0a04SGreg Roach     * @return Date
428a25f0a04SGreg Roach     */
4298f53f488SRico Sonntag    public function getDeathDate(): Date
430c1010edaSGreg Roach    {
431a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
432a25f0a04SGreg Roach            if ($date->isOK()) {
433a25f0a04SGreg Roach                return $date;
434a25f0a04SGreg Roach            }
435a25f0a04SGreg Roach        }
436a25f0a04SGreg Roach
437a25f0a04SGreg Roach        return new Date('');
438a25f0a04SGreg Roach    }
439a25f0a04SGreg Roach
440a25f0a04SGreg Roach    /**
441a25f0a04SGreg Roach     * Get the place of death
442a25f0a04SGreg Roach     *
44316d0b7f7SRico Sonntag     * @return Place
444a25f0a04SGreg Roach     */
4458f53f488SRico Sonntag    public function getDeathPlace(): Place
446c1010edaSGreg Roach    {
447a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
448a25f0a04SGreg Roach            return $place;
449a25f0a04SGreg Roach        }
450a25f0a04SGreg Roach
451b20ddbf9SGreg Roach        return new Place('', $this->tree);
452a25f0a04SGreg Roach    }
453a25f0a04SGreg Roach
454a25f0a04SGreg Roach    /**
455a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
45615d603e7SGreg Roach     * Provide the place and full date using a tooltip.
457a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
458a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
459a25f0a04SGreg Roach     *
460a25f0a04SGreg Roach     * @return string
461a25f0a04SGreg Roach     */
4625e6816beSGreg Roach    public function lifespan(): string
463c1010edaSGreg Roach    {
46453f5ca04SGreg Roach        // Just the first part of the place name.
465392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
466392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
46753f5ca04SGreg Roach
46881b9dc5dSGreg Roach        // Remove markup from dates.  Use UTF_FSI / UTF_PDI instead of <bdi></bdi>, as
46981b9dc5dSGreg Roach        // we cannot use HTML markup in title attributes.
47081b9dc5dSGreg Roach        $birth_date = "\u{2068}" . strip_tags($this->getBirthDate()->display()) . "\u{2069}";
47181b9dc5dSGreg Roach        $death_date = "\u{2068}" . strip_tags($this->getDeathDate()->display()) . "\u{2069}";
47215d603e7SGreg Roach
47353f5ca04SGreg Roach        // Use minimum and maximum dates - to agree with the age calculations.
47453f5ca04SGreg Roach        $birth_year = $this->getBirthDate()->minimumDate()->format('%Y');
47553f5ca04SGreg Roach        $death_year = $this->getDeathDate()->maximumDate()->format('%Y');
47653f5ca04SGreg Roach
477c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
478dacfe33cSGreg Roach        return I18N::translate(
479a25f0a04SGreg Roach            '%1$s–%2$s',
48053f5ca04SGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $birth_year . '</span>',
48153f5ca04SGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $death_year . '</span>'
482a25f0a04SGreg Roach        );
483a25f0a04SGreg Roach    }
484a25f0a04SGreg Roach
485a25f0a04SGreg Roach    /**
486a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
487a25f0a04SGreg Roach     *
48809482a55SGreg Roach     * @return array<Date>
489a25f0a04SGreg Roach     */
4908f53f488SRico Sonntag    public function getAllBirthDates(): array
491c1010edaSGreg Roach    {
4928d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
493d240bb87SGreg Roach            $dates = $this->getAllEventDates([$event]);
494d240bb87SGreg Roach
495d240bb87SGreg Roach            if ($dates !== []) {
496d240bb87SGreg Roach                return $dates;
497a25f0a04SGreg Roach            }
498a25f0a04SGreg Roach        }
499a25f0a04SGreg Roach
50013abd6f3SGreg Roach        return [];
501a25f0a04SGreg Roach    }
502a25f0a04SGreg Roach
503a25f0a04SGreg Roach    /**
504a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
505a25f0a04SGreg Roach     *
50609482a55SGreg Roach     * @return array<Place>
507a25f0a04SGreg Roach     */
5088f53f488SRico Sonntag    public function getAllBirthPlaces(): array
509c1010edaSGreg Roach    {
5108d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5118d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
512d240bb87SGreg Roach
51354c1ab5eSGreg Roach            if ($places !== []) {
5144080d558SGreg Roach                return $places;
515a25f0a04SGreg Roach            }
516a25f0a04SGreg Roach        }
517a25f0a04SGreg Roach
51813abd6f3SGreg Roach        return [];
519a25f0a04SGreg Roach    }
520a25f0a04SGreg Roach
521a25f0a04SGreg Roach    /**
522a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
523a25f0a04SGreg Roach     *
52409482a55SGreg Roach     * @return array<Date>
525a25f0a04SGreg Roach     */
5268f53f488SRico Sonntag    public function getAllDeathDates(): array
527c1010edaSGreg Roach    {
5288d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
529d240bb87SGreg Roach            $dates = $this->getAllEventDates([$event]);
530d240bb87SGreg Roach
531d240bb87SGreg Roach            if ($dates !== []) {
532d240bb87SGreg Roach                return $dates;
533a25f0a04SGreg Roach            }
534a25f0a04SGreg Roach        }
535a25f0a04SGreg Roach
53613abd6f3SGreg Roach        return [];
537a25f0a04SGreg Roach    }
538a25f0a04SGreg Roach
539a25f0a04SGreg Roach    /**
540a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
541a25f0a04SGreg Roach     *
54209482a55SGreg Roach     * @return array<Place>
543a25f0a04SGreg Roach     */
5448f53f488SRico Sonntag    public function getAllDeathPlaces(): array
545c1010edaSGreg Roach    {
5468d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
5478d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
548d240bb87SGreg Roach
54954c1ab5eSGreg Roach            if ($places !== []) {
5504080d558SGreg Roach                return $places;
551a25f0a04SGreg Roach            }
552a25f0a04SGreg Roach        }
553a25f0a04SGreg Roach
55413abd6f3SGreg Roach        return [];
555a25f0a04SGreg Roach    }
556a25f0a04SGreg Roach
557a25f0a04SGreg Roach    /**
558a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
559a25f0a04SGreg Roach     *
560a25f0a04SGreg Roach     * @return Date
561a25f0a04SGreg Roach     */
5628f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
563c1010edaSGreg Roach    {
5648f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
565a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
566a25f0a04SGreg Roach                if ($date->isOK()) {
5674686330aSGreg Roach                    $this->estimated_birth_date = $date;
568a25f0a04SGreg Roach                    break;
569a25f0a04SGreg Roach                }
570a25f0a04SGreg Roach            }
5718f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
57213abd6f3SGreg Roach                $min = [];
57313abd6f3SGreg Roach                $max = [];
574a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
575f5b60decSGreg Roach                if ($tmp->isOK()) {
576f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
577f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
578a25f0a04SGreg Roach                }
57939ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
580a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
581f5b60decSGreg Roach                    if ($tmp->isOK()) {
582f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
583f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
584a25f0a04SGreg Roach                    }
58539ca88baSGreg Roach                    $husband = $family->husband();
586e364afe4SGreg Roach                    if ($husband instanceof self) {
5872e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
588f5b60decSGreg Roach                        if ($tmp->isOK()) {
589f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
590f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
591a25f0a04SGreg Roach                        }
592a25f0a04SGreg Roach                    }
59339ca88baSGreg Roach                    $wife = $family->wife();
594e364afe4SGreg Roach                    if ($wife instanceof self) {
5952e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
596f5b60decSGreg Roach                        if ($tmp->isOK()) {
597f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
598f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
599a25f0a04SGreg Roach                        }
600a25f0a04SGreg Roach                    }
60139ca88baSGreg Roach                    foreach ($family->children() as $child) {
602a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
603f5b60decSGreg Roach                        if ($tmp->isOK()) {
604f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
605f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
606a25f0a04SGreg Roach                        }
607a25f0a04SGreg Roach                    }
608a25f0a04SGreg Roach                }
60939ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
610a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
611f5b60decSGreg Roach                    if ($tmp->isOK()) {
612f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
613f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
614a25f0a04SGreg Roach                    }
61539ca88baSGreg Roach                    $spouse = $family->spouse($this);
616a25f0a04SGreg Roach                    if ($spouse) {
617a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
618f5b60decSGreg Roach                        if ($tmp->isOK()) {
619f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
620f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
621a25f0a04SGreg Roach                        }
622a25f0a04SGreg Roach                    }
62339ca88baSGreg Roach                    foreach ($family->children() as $child) {
624a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
625f5b60decSGreg Roach                        if ($tmp->isOK()) {
626e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
627f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
628a25f0a04SGreg Roach                        }
629a25f0a04SGreg Roach                    }
630a25f0a04SGreg Roach                }
631a25f0a04SGreg Roach                if ($min && $max) {
63259f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
633a25f0a04SGreg Roach
63465e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
6354686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
636a25f0a04SGreg Roach                } else {
6374686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
638a25f0a04SGreg Roach                }
639a25f0a04SGreg Roach            }
640a25f0a04SGreg Roach        }
641a25f0a04SGreg Roach
6424686330aSGreg Roach        return $this->estimated_birth_date;
643a25f0a04SGreg Roach    }
644a25f0a04SGreg Roach
645a25f0a04SGreg Roach    /**
646a25f0a04SGreg Roach     * Generate an estimated date of death.
647a25f0a04SGreg Roach     *
648a25f0a04SGreg Roach     * @return Date
649a25f0a04SGreg Roach     */
6508f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
651c1010edaSGreg Roach    {
6524686330aSGreg Roach        if ($this->estimated_death_date === null) {
653a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
654a25f0a04SGreg Roach                if ($date->isOK()) {
6554686330aSGreg Roach                    $this->estimated_death_date = $date;
656a25f0a04SGreg Roach                    break;
657a25f0a04SGreg Roach                }
658a25f0a04SGreg Roach            }
6594686330aSGreg Roach            if ($this->estimated_death_date === null) {
660f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
661c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
6624686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
663a25f0a04SGreg Roach                } else {
6644686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
665a25f0a04SGreg Roach                }
666a25f0a04SGreg Roach            }
667a25f0a04SGreg Roach        }
668a25f0a04SGreg Roach
6694686330aSGreg Roach        return $this->estimated_death_date;
670a25f0a04SGreg Roach    }
671a25f0a04SGreg Roach
672a25f0a04SGreg Roach    /**
673a25f0a04SGreg Roach     * Get the sex - M F or U
674a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
675a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
676a25f0a04SGreg Roach     *
677a25f0a04SGreg Roach     * @return string
678a25f0a04SGreg Roach     */
679e364afe4SGreg Roach    public function sex(): string
680c1010edaSGreg Roach    {
6811baf69deSGreg Roach        if (preg_match('/\n1 SEX ([MFX])/', $this->gedcom . $this->pending, $match)) {
682a25f0a04SGreg Roach            return $match[1];
683a25f0a04SGreg Roach        }
684b2ce94c6SRico Sonntag
685b2ce94c6SRico Sonntag        return 'U';
686a25f0a04SGreg Roach    }
687a25f0a04SGreg Roach
688a25f0a04SGreg Roach    /**
689a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
690a25f0a04SGreg Roach     *
691cbc1590aSGreg Roach     * @param int|null $access_level
692a25f0a04SGreg Roach     *
693*36779af1SGreg Roach     * @return Collection<int,Family>
694a25f0a04SGreg Roach     */
69573d58381SGreg Roach    public function spouseFamilies(int $access_level = null): Collection
696c1010edaSGreg Roach    {
697d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
698d9e083e7SGreg Roach
699d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
700d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
7014b9ff166SGreg Roach        }
7024b9ff166SGreg Roach
70339ca88baSGreg Roach        $families = new Collection();
704d9e083e7SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level) as $fact) {
705dc124885SGreg Roach            $family = $fact->target();
706d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
70739ca88baSGreg Roach                $families->push($family);
708a25f0a04SGreg Roach            }
709a25f0a04SGreg Roach        }
710a25f0a04SGreg Roach
71139ca88baSGreg Roach        return new Collection($families);
712a25f0a04SGreg Roach    }
713a25f0a04SGreg Roach
714a25f0a04SGreg Roach    /**
715a25f0a04SGreg Roach     * Get the current spouse of this individual.
716a25f0a04SGreg Roach     *
717a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
718a25f0a04SGreg Roach     * in chronological order, and take the last one found.
719a25f0a04SGreg Roach     *
720a25f0a04SGreg Roach     * @return Individual|null
721a25f0a04SGreg Roach     */
722e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
723c1010edaSGreg Roach    {
72439ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
72539ca88baSGreg Roach
72639ca88baSGreg Roach        if ($family instanceof Family) {
72739ca88baSGreg Roach            return $family->spouse($this);
728a25f0a04SGreg Roach        }
729b2ce94c6SRico Sonntag
730b2ce94c6SRico Sonntag        return null;
731a25f0a04SGreg Roach    }
732a25f0a04SGreg Roach
733a25f0a04SGreg Roach    /**
734a25f0a04SGreg Roach     * Count the children belonging to this individual.
735a25f0a04SGreg Roach     *
736cbc1590aSGreg Roach     * @return int
737a25f0a04SGreg Roach     */
738e364afe4SGreg Roach    public function numberOfChildren(): int
739c1010edaSGreg Roach    {
7407d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
7413dc7bbe9SGreg Roach            return (int) $match[1];
742b2ce94c6SRico Sonntag        }
743b2ce94c6SRico Sonntag
74413abd6f3SGreg Roach        $children = [];
74539ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
74639ca88baSGreg Roach            foreach ($fam->children() as $child) {
747c0935879SGreg Roach                $children[$child->xref()] = true;
748a25f0a04SGreg Roach            }
749a25f0a04SGreg Roach        }
750a25f0a04SGreg Roach
751a25f0a04SGreg Roach        return count($children);
752a25f0a04SGreg Roach    }
753a25f0a04SGreg Roach
754a25f0a04SGreg Roach    /**
755a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
756a25f0a04SGreg Roach     *
757cbc1590aSGreg Roach     * @param int|null $access_level
758a25f0a04SGreg Roach     *
759*36779af1SGreg Roach     * @return Collection<int,Family>
760a25f0a04SGreg Roach     */
76173d58381SGreg Roach    public function childFamilies(int $access_level = null): Collection
762c1010edaSGreg Roach    {
763d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
7644b9ff166SGreg Roach
765d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
766d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
767d9e083e7SGreg Roach        }
768a25f0a04SGreg Roach
76939ca88baSGreg Roach        $families = new Collection();
77039ca88baSGreg Roach
771d9e083e7SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level) as $fact) {
772dc124885SGreg Roach            $family = $fact->target();
773d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
77439ca88baSGreg Roach                $families->push($family);
775a25f0a04SGreg Roach            }
776a25f0a04SGreg Roach        }
777a25f0a04SGreg Roach
778a25f0a04SGreg Roach        return $families;
779a25f0a04SGreg Roach    }
780a25f0a04SGreg Roach
781a25f0a04SGreg Roach    /**
782a25f0a04SGreg Roach     * Get a list of step-parent families.
783a25f0a04SGreg Roach     *
784*36779af1SGreg Roach     * @return Collection<int,Family>
785a25f0a04SGreg Roach     */
786820b62dfSGreg Roach    public function childStepFamilies(): Collection
787c1010edaSGreg Roach    {
788ed5b6227SGreg Roach        $step_families = new Collection();
78939ca88baSGreg Roach        $families      = $this->childFamilies();
790a25f0a04SGreg Roach        foreach ($families as $family) {
791ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
792ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
79339ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
794ed5b6227SGreg Roach                        $step_families->add($step_family);
795a25f0a04SGreg Roach                    }
796a25f0a04SGreg Roach                }
797a25f0a04SGreg Roach            }
798a25f0a04SGreg Roach        }
799a25f0a04SGreg Roach
8008c627a69SGreg Roach        return $step_families->uniqueStrict(static function (Family $family): string {
8018c627a69SGreg Roach            return $family->xref();
8028c627a69SGreg Roach        });
803a25f0a04SGreg Roach    }
804a25f0a04SGreg Roach
805a25f0a04SGreg Roach    /**
806a25f0a04SGreg Roach     * Get a list of step-parent families.
807a25f0a04SGreg Roach     *
808*36779af1SGreg Roach     * @return Collection<int,Family>
809a25f0a04SGreg Roach     */
810820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
811c1010edaSGreg Roach    {
81213abd6f3SGreg Roach        $step_families = [];
81339ca88baSGreg Roach        $families      = $this->spouseFamilies();
814820b62dfSGreg Roach
815a25f0a04SGreg Roach        foreach ($families as $family) {
81639ca88baSGreg Roach            $spouse = $family->spouse($this);
817820b62dfSGreg Roach
818d823340dSGreg Roach            if ($spouse instanceof self) {
81939ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
82039ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
821a25f0a04SGreg Roach                        $step_families[] = $step_family;
822a25f0a04SGreg Roach                    }
823a25f0a04SGreg Roach                }
824a25f0a04SGreg Roach            }
825a25f0a04SGreg Roach        }
826a25f0a04SGreg Roach
827820b62dfSGreg Roach        return new Collection($step_families);
828a25f0a04SGreg Roach    }
829a25f0a04SGreg Roach
830a25f0a04SGreg Roach    /**
831a25f0a04SGreg Roach     * A label for a parental family group
832a25f0a04SGreg Roach     *
833a25f0a04SGreg Roach     * @param Family $family
834a25f0a04SGreg Roach     *
835a25f0a04SGreg Roach     * @return string
836a25f0a04SGreg Roach     */
837820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
838c1010edaSGreg Roach    {
8397d70e4a7SGreg Roach        preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match);
840b2ce94c6SRico Sonntag
8417d70e4a7SGreg Roach        $values = [
8427d70e4a7SGreg Roach            'birth'   => I18N::translate('Family with parents'),
8437d70e4a7SGreg Roach            'adopted' => I18N::translate('Family with adoptive parents'),
8447d70e4a7SGreg Roach            'foster'  => I18N::translate('Family with foster parents'),
8457d70e4a7SGreg Roach            'sealing' => /* I18N: “sealing” is a Mormon ceremony. */
8467d70e4a7SGreg Roach                I18N::translate('Family with sealing parents'),
8477d70e4a7SGreg Roach            'rada'    => /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */
8487d70e4a7SGreg Roach                I18N::translate('Family with rada parents'),
8497d70e4a7SGreg Roach        ];
8507d70e4a7SGreg Roach
8517d70e4a7SGreg Roach        return $values[$match[1] ?? 'birth'] ?? $values['birth'];
852a25f0a04SGreg Roach    }
853a25f0a04SGreg Roach
854a25f0a04SGreg Roach    /**
855a25f0a04SGreg Roach     * Create a label for a step family
856a25f0a04SGreg Roach     *
857a25f0a04SGreg Roach     * @param Family $step_family
858a25f0a04SGreg Roach     *
859a25f0a04SGreg Roach     * @return string
860a25f0a04SGreg Roach     */
8618f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
862c1010edaSGreg Roach    {
86339ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
864a25f0a04SGreg Roach            if ($family !== $step_family) {
865a25f0a04SGreg Roach                // Must be a step-family
86639ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
86739ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
868a25f0a04SGreg Roach                        if ($parent === $step_parent) {
869a25f0a04SGreg Roach                            // One common parent - must be a step family
870e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
871a25f0a04SGreg Roach                                // Father’s family with someone else
87239ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
873a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
87439ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
875b2ce94c6SRico Sonntag                                }
876b2ce94c6SRico Sonntag
877a25f0a04SGreg Roach                                /* I18N: A step-family. */
878bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
879a25f0a04SGreg Roach                            }
880b2ce94c6SRico Sonntag
881a25f0a04SGreg Roach                            // Mother’s family with someone else
88239ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
883a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
88439ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
885b2ce94c6SRico Sonntag                            }
886b2ce94c6SRico Sonntag
887a25f0a04SGreg Roach                            /* I18N: A step-family. */
888bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
889a25f0a04SGreg Roach                        }
890a25f0a04SGreg Roach                    }
891a25f0a04SGreg Roach                }
892a25f0a04SGreg Roach            }
893a25f0a04SGreg Roach        }
894a25f0a04SGreg Roach
895a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
896a25f0a04SGreg Roach        return I18N::translate('Family with parents');
897a25f0a04SGreg Roach    }
898a25f0a04SGreg Roach
899225e381fSGreg Roach    /**
900225e381fSGreg Roach     * Get the description for the family.
901225e381fSGreg Roach     *
902225e381fSGreg Roach     * For example, "XXX's family with new wife".
903225e381fSGreg Roach     *
904225e381fSGreg Roach     * @param Family $family
905225e381fSGreg Roach     *
906225e381fSGreg Roach     * @return string
907225e381fSGreg Roach     */
908e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
909c1010edaSGreg Roach    {
91039ca88baSGreg Roach        $spouse = $family->spouse($this);
911225e381fSGreg Roach        if ($spouse) {
912225e381fSGreg Roach            /* I18N: %s is the spouse name */
91339ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
914225e381fSGreg Roach        }
915b2ce94c6SRico Sonntag
91639ca88baSGreg Roach        return $family->fullName();
917225e381fSGreg Roach    }
918225e381fSGreg Roach
919a25f0a04SGreg Roach    /**
920961ec755SGreg Roach     * If this object has no name, what do we call it?
921961ec755SGreg Roach     *
922961ec755SGreg Roach     * @return string
923961ec755SGreg Roach     */
9248f53f488SRico Sonntag    public function getFallBackName(): string
925c1010edaSGreg Roach    {
926a25f0a04SGreg Roach        return '@P.N. /@N.N./';
927a25f0a04SGreg Roach    }
928a25f0a04SGreg Roach
929a25f0a04SGreg Roach    /**
930a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
931a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
932a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
933a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
934a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
935a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
936a25f0a04SGreg Roach     *
937a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
938a25f0a04SGreg Roach     * 2 GIVN Robert
939a25f0a04SGreg Roach     * 2 SPFX de
940a25f0a04SGreg Roach     * 2 SURN CLITHEROW
941a25f0a04SGreg Roach     * 2 NICK The Bald
942a25f0a04SGreg Roach     *
943a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
944a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
945a25f0a04SGreg Roach     *
946a25f0a04SGreg Roach     * Handle multiple surnames, either as;
947a25f0a04SGreg Roach     *
948a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
949a25f0a04SGreg Roach     * or
950a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
951a25f0a04SGreg Roach     * 2 GIVN Carlos
952a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
953a25f0a04SGreg Roach     *
954a25f0a04SGreg Roach     * @param string $type
95551928f9aSGreg Roach     * @param string $value
956a25f0a04SGreg Roach     * @param string $gedcom
957e364afe4SGreg Roach     *
958e364afe4SGreg Roach     * @return void
959a25f0a04SGreg Roach     */
96051928f9aSGreg Roach    protected function addName(string $type, string $value, string $gedcom): void
961c1010edaSGreg Roach    {
962a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
963a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
964a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
965a25f0a04SGreg Roach
96676f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
967b6ac34efSGreg Roach        $GIVN     = preg_match('/\n' . $sublevel . ' GIVN (.+)/', $gedcom, $match) ? $match[1] : '';
968b6ac34efSGreg Roach        $SURN     = preg_match('/\n' . $sublevel . ' SURN (.+)/', $gedcom, $match) ? $match[1] : '';
969a25f0a04SGreg Roach
970a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
97176f666f4SGreg Roach        if ($SURN !== '') {
972a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
973a25f0a04SGreg Roach        } else {
97413abd6f3SGreg Roach            $SURNS = [];
975a25f0a04SGreg Roach        }
97676f666f4SGreg Roach
977a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
978a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
979a25f0a04SGreg Roach
980a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
981a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
982a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
983a25f0a04SGreg Roach
984a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
98551928f9aSGreg Roach        if (substr_count($value, '/') % 2 === 1) {
98651928f9aSGreg Roach            $value .= '/';
987a25f0a04SGreg Roach        }
988a25f0a04SGreg Roach
989a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
99051928f9aSGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $value);
991a25f0a04SGreg Roach
992a25f0a04SGreg Roach        // Extract the surname.
993a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
994a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
995a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
996a25f0a04SGreg Roach        } else {
997a25f0a04SGreg Roach            $surname = '';
998a25f0a04SGreg Roach        }
999a25f0a04SGreg Roach
1000a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1001a25f0a04SGreg Roach        if (!$SURNS) {
1002a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1003a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1004a25f0a04SGreg Roach                $SURNS = $matches[1];
1005a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1006a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1007a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1008a25f0a04SGreg Roach                }
1009a25f0a04SGreg Roach            } else {
1010a25f0a04SGreg Roach                // It is valid not to have a surname at all
101113abd6f3SGreg Roach                $SURNS = [''];
1012a25f0a04SGreg Roach            }
1013a25f0a04SGreg Roach        }
1014a25f0a04SGreg Roach
1015a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1016a25f0a04SGreg Roach        if (!$GIVN) {
1017c1010edaSGreg Roach            // remove surname
1018c72b7fa4SGreg Roach            $GIVN = preg_replace('/ ?\/.*\/ ?/', ' ', $full);
1019c1010edaSGreg Roach            // remove nickname
1020c72b7fa4SGreg Roach            $GIVN = preg_replace('/ ?".+"/', ' ', $GIVN);
1021c1010edaSGreg Roach            // multiple spaces, caused by the above
1022c72b7fa4SGreg Roach            $GIVN = preg_replace('/ {2,}/', ' ', $GIVN);
1023c1010edaSGreg Roach            // leading/trailing spaces, caused by the above
1024c72b7fa4SGreg Roach            $GIVN = preg_replace('/^ | $/', '', $GIVN);
1025a25f0a04SGreg Roach        }
1026a25f0a04SGreg Roach
1027a25f0a04SGreg Roach        // Add placeholder for unknown given name
1028a25f0a04SGreg Roach        if (!$GIVN) {
1029d823340dSGreg Roach            $GIVN = self::PRAENOMEN_NESCIO;
103073f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1031a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1032a25f0a04SGreg Roach        }
1033a25f0a04SGreg Roach
1034a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1035a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1036a25f0a04SGreg Roach        // $full is for display on-screen
1037a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1038a25f0a04SGreg Roach
1039a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1040d823340dSGreg Roach        $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full);
1041d823340dSGreg Roach        $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full);
1042c6f196c3SGreg Roach        // Format for display
1043d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1044acc34ea1SGreg Roach        // Localise quotation marks around the nickname
10450b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
1046c652cdbdSGreg Roach            return '<q class="wt-nickname">' . $matches[1] . '</q>';
10478d68cabeSGreg Roach        }, $full);
1048a25f0a04SGreg Roach
1049c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1050ee51991cSGreg Roach        $full = preg_replace('/([^ >\x{200C}]*)\*/u', '<span class="starredname">\\1</span>', $full);
1051a25f0a04SGreg Roach
1052a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1053a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1054a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1055a25f0a04SGreg Roach
1056ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1057a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1058e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1059a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1060e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1061a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1062a25f0a04SGreg Roach            }
1063a25f0a04SGreg Roach
1064bdb3725aSGreg Roach            $this->getAllNames[] = [
1065a25f0a04SGreg Roach                'type'    => $type,
1066a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1067c1010edaSGreg Roach                'full'    => $full,
1068c1010edaSGreg Roach                // This is used for display
1069c1010edaSGreg Roach                'fullNN'  => $fullNN,
1070c1010edaSGreg Roach                // This goes into the database
1071c1010edaSGreg Roach                'surname' => $surname,
1072c1010edaSGreg Roach                // This goes into the database
1073c1010edaSGreg Roach                'givn'    => $GIVN,
1074c1010edaSGreg Roach                // This goes into the database
1075c1010edaSGreg Roach                'surn'    => $SURN,
1076c1010edaSGreg Roach                // This goes into the database
107713abd6f3SGreg Roach            ];
1078a25f0a04SGreg Roach        }
1079a25f0a04SGreg Roach    }
1080a25f0a04SGreg Roach
1081a25f0a04SGreg Roach    /**
108276692c8bSGreg Roach     * Extract names from the GEDCOM record.
1083c7ff4153SGreg Roach     *
1084c7ff4153SGreg Roach     * @return void
1085a25f0a04SGreg Roach     */
1086e364afe4SGreg Roach    public function extractNames(): void
1087c1010edaSGreg Roach    {
1088d9e083e7SGreg Roach        $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree);
1089d9e083e7SGreg Roach
10908f53f488SRico Sonntag        $this->extractNamesFromFacts(
10918f53f488SRico Sonntag            1,
10928f53f488SRico Sonntag            'NAME',
1093d9e083e7SGreg Roach            $this->facts(['NAME'], false, $access_level)
10948f53f488SRico Sonntag        );
1095a25f0a04SGreg Roach    }
1096a25f0a04SGreg Roach
1097a25f0a04SGreg Roach    /**
1098a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1099a25f0a04SGreg Roach     * selection items or favorites.
1100a25f0a04SGreg Roach     *
1101a25f0a04SGreg Roach     * @return string
1102a25f0a04SGreg Roach     */
11038f53f488SRico Sonntag    public function formatListDetails(): string
1104c1010edaSGreg Roach    {
1105a25f0a04SGreg Roach        return
11068d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
11078d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1108a25f0a04SGreg Roach    }
11098091bfd1SGreg Roach
11108091bfd1SGreg Roach    /**
11118091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
11128091bfd1SGreg Roach     */
11138091bfd1SGreg Roach    public function lock(): void
11148091bfd1SGreg Roach    {
11158091bfd1SGreg Roach        DB::table('individuals')
11168091bfd1SGreg Roach            ->where('i_file', '=', $this->tree->id())
11178091bfd1SGreg Roach            ->where('i_id', '=', $this->xref())
11188091bfd1SGreg Roach            ->lockForUpdate()
11198091bfd1SGreg Roach            ->get();
11208091bfd1SGreg Roach    }
1121a25f0a04SGreg Roach}
1122