xref: /webtrees/app/Individual.php (revision 1fe542e96f8f7eedeebc278fae1e0ab0d9e74d95)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*1fe542e9SGreg 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://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;
24*1fe542e9SGreg 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
4476692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
4576692c8bSGreg Roach    public $generation;
46a25f0a04SGreg Roach
47a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
484686330aSGreg Roach    private $estimated_birth_date;
49a25f0a04SGreg Roach
50a25f0a04SGreg Roach    /** @var Date The estimated date of death */
514686330aSGreg Roach    private $estimated_death_date;
52a25f0a04SGreg Roach
53a25f0a04SGreg Roach    /**
54886b77daSGreg Roach     * A closure which will create a record from a database row.
55886b77daSGreg Roach     *
56bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
57bb03c9f0SGreg Roach     *
58d5ad3db0SGreg Roach     * @param Tree $tree
59d5ad3db0SGreg Roach     *
60886b77daSGreg Roach     * @return Closure
61886b77daSGreg Roach     */
62d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
63886b77daSGreg Roach    {
646b9cb339SGreg Roach        return Registry::individualFactory()->mapper($tree);
65886b77daSGreg Roach    }
66886b77daSGreg Roach
67886b77daSGreg Roach    /**
68c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
69c156e8f5SGreg Roach     *
70c156e8f5SGreg Roach     * @return Closure
71c156e8f5SGreg Roach     */
72c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
73c156e8f5SGreg Roach    {
746c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
75c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
76c156e8f5SGreg Roach        };
77c156e8f5SGreg Roach    }
78c156e8f5SGreg Roach
79c156e8f5SGreg Roach    /**
80c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
81c156e8f5SGreg Roach     *
82c156e8f5SGreg Roach     * @return Closure
83c156e8f5SGreg Roach     */
84c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
85c156e8f5SGreg Roach    {
866c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
8710e21aa9SGreg Roach            return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
88c156e8f5SGreg Roach        };
89c156e8f5SGreg Roach    }
90c156e8f5SGreg Roach
91c156e8f5SGreg Roach    /**
92e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
93e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
94e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
95e71ef9d2SGreg Roach     *
96bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
97bb03c9f0SGreg Roach     *
98e71ef9d2SGreg Roach     * @param string      $xref
99e71ef9d2SGreg Roach     * @param Tree        $tree
100e71ef9d2SGreg Roach     * @param string|null $gedcom
101e71ef9d2SGreg Roach     *
102e71ef9d2SGreg Roach     * @return Individual|null
103e71ef9d2SGreg Roach     */
104ffc0a61fSGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Individual
105c1010edaSGreg Roach    {
1066b9cb339SGreg Roach        return Registry::individualFactory()->make($xref, $tree, $gedcom);
107e71ef9d2SGreg Roach    }
108e71ef9d2SGreg Roach
109e71ef9d2SGreg Roach    /**
110395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
111395f0fe0SGreg Roach     * Typically when we load families and their members.
112395f0fe0SGreg Roach     *
113395f0fe0SGreg Roach     * @param Tree     $tree
1144a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
11518d7a90dSGreg Roach     *
11618d7a90dSGreg Roach     * @return void
117395f0fe0SGreg Roach     */
1182e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
119c1010edaSGreg Roach    {
1202e5b4452SGreg Roach        $rows = DB::table('individuals')
1212e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1222e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1232e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1242e5b4452SGreg Roach            ->get();
125395f0fe0SGreg Roach
126395f0fe0SGreg Roach        foreach ($rows as $row) {
1276b9cb339SGreg Roach            Registry::individualFactory()->make($row->xref, $tree, $row->gedcom);
128395f0fe0SGreg Roach        }
129395f0fe0SGreg Roach    }
130395f0fe0SGreg Roach
131395f0fe0SGreg Roach    /**
132a25f0a04SGreg Roach     * Can the name of this record be shown?
133a25f0a04SGreg Roach     *
13476692c8bSGreg Roach     * @param int|null $access_level
13576692c8bSGreg Roach     *
13676692c8bSGreg Roach     * @return bool
137a25f0a04SGreg Roach     */
13835584196SGreg Roach    public function canShowName(int $access_level = null): bool
139c1010edaSGreg Roach    {
140d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
1414b9ff166SGreg Roach
142518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
143a25f0a04SGreg Roach    }
144a25f0a04SGreg Roach
145a25f0a04SGreg Roach    /**
14676692c8bSGreg Roach     * Can this individual be shown?
147a25f0a04SGreg Roach     *
14876692c8bSGreg Roach     * @param int $access_level
14976692c8bSGreg Roach     *
15076692c8bSGreg Roach     * @return bool
151a25f0a04SGreg Roach     */
15235584196SGreg Roach    protected function canShowByType(int $access_level): bool
153c1010edaSGreg Roach    {
154a25f0a04SGreg Roach        // Dead people...
155518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
156a25f0a04SGreg Roach            $keep_alive             = false;
15754ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
158a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1598d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
160a25f0a04SGreg Roach                foreach ($matches as $match) {
161a25f0a04SGreg Roach                    $date = new Date($match[1]);
162a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
163a25f0a04SGreg Roach                        $keep_alive = true;
164a25f0a04SGreg Roach                        break;
165a25f0a04SGreg Roach                    }
166a25f0a04SGreg Roach                }
167a25f0a04SGreg Roach            }
16854ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
169a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1708d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
171a25f0a04SGreg Roach                foreach ($matches as $match) {
172a25f0a04SGreg Roach                    $date = new Date($match[1]);
173a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
174a25f0a04SGreg Roach                        $keep_alive = true;
175a25f0a04SGreg Roach                        break;
176a25f0a04SGreg Roach                    }
177a25f0a04SGreg Roach                }
178a25f0a04SGreg Roach            }
179a25f0a04SGreg Roach            if (!$keep_alive) {
180a25f0a04SGreg Roach                return true;
181a25f0a04SGreg Roach            }
182a25f0a04SGreg Roach        }
183a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
184*1fe542e9SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH);
185*1fe542e9SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
1867c4add84SGreg Roach
18754ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1884b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
189a25f0a04SGreg Roach        }
190a25f0a04SGreg Roach
191a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1924b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
193a25f0a04SGreg Roach    }
194a25f0a04SGreg Roach
195a25f0a04SGreg Roach    /**
196a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
197a25f0a04SGreg Roach     *
198a25f0a04SGreg Roach     * @param Individual $target
199cbc1590aSGreg Roach     * @param int        $distance
200a25f0a04SGreg Roach     *
201cbc1590aSGreg Roach     * @return bool
202a25f0a04SGreg Roach     */
2038f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
204c1010edaSGreg Roach    {
205a25f0a04SGreg Roach        static $cache = null;
206a25f0a04SGreg Roach
207*1fe542e9SGreg Roach        $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree);
208a25f0a04SGreg Roach        if ($user_individual) {
209a25f0a04SGreg Roach            if (!$cache) {
21013abd6f3SGreg Roach                $cache = [
21113abd6f3SGreg Roach                    0 => [$user_individual],
21213abd6f3SGreg Roach                    1 => [],
21313abd6f3SGreg Roach                ];
2148d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
215dc124885SGreg Roach                    $family = $fact->target();
216e24444eeSGreg Roach                    if ($family instanceof Family) {
217a25f0a04SGreg Roach                        $cache[1][] = $family;
218a25f0a04SGreg Roach                    }
219a25f0a04SGreg Roach                }
220a25f0a04SGreg Roach            }
221a25f0a04SGreg Roach        } else {
222a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
223a25f0a04SGreg Roach            return true;
224a25f0a04SGreg Roach        }
225a25f0a04SGreg Roach
226a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
227a25f0a04SGreg Roach        $distance *= 2;
228a25f0a04SGreg Roach
229a25f0a04SGreg Roach        // Consider each path length in turn
230a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
231a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
232a25f0a04SGreg Roach                // We have already calculated all records with this length
233e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
234a25f0a04SGreg Roach                    return true;
235a25f0a04SGreg Roach                }
236a25f0a04SGreg Roach            } else {
237a25f0a04SGreg Roach                // Need to calculate these paths
23813abd6f3SGreg Roach                $cache[$n] = [];
239e364afe4SGreg Roach                if ($n % 2 === 0) {
240a25f0a04SGreg Roach                    // Add FAM->INDI links
241a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2428d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
243dc124885SGreg Roach                            $individual = $fact->target();
244a25f0a04SGreg Roach                            // Don’t backtrack
245e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
246a25f0a04SGreg Roach                                $cache[$n][] = $individual;
247a25f0a04SGreg Roach                            }
248a25f0a04SGreg Roach                        }
249a25f0a04SGreg Roach                    }
250a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
251a25f0a04SGreg Roach                        return true;
252a25f0a04SGreg Roach                    }
253a25f0a04SGreg Roach                } else {
254a25f0a04SGreg Roach                    // Add INDI->FAM links
255a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2568d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
257dc124885SGreg Roach                            $family = $fact->target();
258a25f0a04SGreg Roach                            // Don’t backtrack
259e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
260a25f0a04SGreg Roach                                $cache[$n][] = $family;
261a25f0a04SGreg Roach                            }
262a25f0a04SGreg Roach                        }
263a25f0a04SGreg Roach                    }
264a25f0a04SGreg Roach                }
265a25f0a04SGreg Roach            }
266a25f0a04SGreg Roach        }
267a25f0a04SGreg Roach
268a25f0a04SGreg Roach        return false;
269a25f0a04SGreg Roach    }
270a25f0a04SGreg Roach
27176692c8bSGreg Roach    /**
27276692c8bSGreg Roach     * Generate a private version of this record
27376692c8bSGreg Roach     *
27476692c8bSGreg Roach     * @param int $access_level
27576692c8bSGreg Roach     *
27676692c8bSGreg Roach     * @return string
27776692c8bSGreg Roach     */
2783c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
279c1010edaSGreg Roach    {
280acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
281a25f0a04SGreg Roach
282a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
283518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
284a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2858d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
286138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
287a25f0a04SGreg Roach            }
288a25f0a04SGreg Roach        }
289a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2908d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
291a25f0a04SGreg Roach        foreach ($matches as $match) {
2926b9cb339SGreg Roach            $rela = Registry::familyFactory()->make($match[1], $this->tree);
293a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
294a25f0a04SGreg Roach                $rec .= $match[0];
295a25f0a04SGreg Roach            }
296a25f0a04SGreg Roach        }
297a25f0a04SGreg Roach        // Don’t privatize sex.
298a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
299a25f0a04SGreg Roach            $rec .= $match[0];
300a25f0a04SGreg Roach        }
301a25f0a04SGreg Roach
302a25f0a04SGreg Roach        return $rec;
303a25f0a04SGreg Roach    }
304a25f0a04SGreg Roach
30576692c8bSGreg Roach    /**
306a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
307a25f0a04SGreg Roach     * If not known to be dead, then assume living.
308a25f0a04SGreg Roach     *
309cbc1590aSGreg Roach     * @return bool
310a25f0a04SGreg Roach     */
3118f53f488SRico Sonntag    public function isDead(): bool
312c1010edaSGreg Roach    {
313c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3144459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
315a25f0a04SGreg Roach
316a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3178d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
318a25f0a04SGreg Roach            return true;
319a25f0a04SGreg Roach        }
320a25f0a04SGreg Roach
321a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
322a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
323a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
324a25f0a04SGreg Roach                $date = new Date($date_match);
325269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
326a25f0a04SGreg Roach                    return true;
327a25f0a04SGreg Roach                }
328a25f0a04SGreg Roach            }
329a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
330a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
331a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
332a25f0a04SGreg Roach                return false;
333a25f0a04SGreg Roach            }
334a25f0a04SGreg Roach        }
335a25f0a04SGreg Roach
336a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
337a25f0a04SGreg Roach
338a25f0a04SGreg Roach        // Check parents (birth and adopted)
33939ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
34039ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
341a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
342a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
343a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
344a25f0a04SGreg Roach                    $date = new Date($date_match);
345269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
346a25f0a04SGreg Roach                        return true;
347a25f0a04SGreg Roach                    }
348a25f0a04SGreg Roach                }
349a25f0a04SGreg Roach            }
350a25f0a04SGreg Roach        }
351a25f0a04SGreg Roach
352a25f0a04SGreg Roach        // Check spouses
35339ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
354a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
355a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
356a25f0a04SGreg Roach                $date = new Date($date_match);
357a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
358269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
359a25f0a04SGreg Roach                    return true;
360a25f0a04SGreg Roach                }
361a25f0a04SGreg Roach            }
362a25f0a04SGreg Roach            // Check spouse dates
36339ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
364a25f0a04SGreg Roach            if ($spouse) {
365a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
366a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
367a25f0a04SGreg Roach                    $date = new Date($date_match);
368a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
369269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
370a25f0a04SGreg Roach                        return true;
371a25f0a04SGreg Roach                    }
372a25f0a04SGreg Roach                }
373a25f0a04SGreg Roach            }
374a25f0a04SGreg Roach            // Check child dates
37539ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
376a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
377a25f0a04SGreg Roach                // Assume children born after age of 15
378a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
379a25f0a04SGreg Roach                    $date = new Date($date_match);
380269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
381a25f0a04SGreg Roach                        return true;
382a25f0a04SGreg Roach                    }
383a25f0a04SGreg Roach                }
384a25f0a04SGreg Roach                // Check grandchildren
38539ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
38639ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
387a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
388a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
389a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
390a25f0a04SGreg Roach                            $date = new Date($date_match);
391269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
392a25f0a04SGreg Roach                                return true;
393a25f0a04SGreg Roach                            }
394a25f0a04SGreg Roach                        }
395a25f0a04SGreg Roach                    }
396a25f0a04SGreg Roach                }
397a25f0a04SGreg Roach            }
398a25f0a04SGreg Roach        }
399a25f0a04SGreg Roach
400a25f0a04SGreg Roach        return false;
401a25f0a04SGreg Roach    }
402a25f0a04SGreg Roach
403a25f0a04SGreg Roach    /**
404a25f0a04SGreg Roach     * Find the highlighted media object for an individual
405a25f0a04SGreg Roach     *
406e364afe4SGreg Roach     * @return MediaFile|null
407a25f0a04SGreg Roach     */
408e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
409c1010edaSGreg Roach    {
41092647683SGreg Roach        $fact = $this->facts(['OBJE'])
41119d319e7SGreg Roach            ->first(static function (Fact $fact): bool {
412dc124885SGreg Roach                $media = $fact->target();
41319d319e7SGreg Roach
41419d319e7SGreg Roach                return $media instanceof Media && $media->firstImageFile() instanceof MediaFile;
41519d319e7SGreg Roach            });
41619d319e7SGreg Roach
41792647683SGreg Roach        if ($fact instanceof Fact && $fact->target() instanceof Media) {
41892647683SGreg Roach            return $fact->target()->firstImageFile();
419a25f0a04SGreg Roach        }
420a25f0a04SGreg Roach
421a25f0a04SGreg Roach        return null;
422a25f0a04SGreg Roach    }
423a25f0a04SGreg Roach
424a25f0a04SGreg Roach    /**
425a25f0a04SGreg Roach     * Display the prefered image for this individual.
426a25f0a04SGreg Roach     * Use an icon if no image is available.
427a25f0a04SGreg Roach     *
428dce07401SGreg Roach     * @param int      $width      Pixels
429dce07401SGreg Roach     * @param int      $height     Pixels
430dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
431dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
432dce07401SGreg Roach     *
433a25f0a04SGreg Roach     * @return string
434a25f0a04SGreg Roach     */
4358f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
436c1010edaSGreg Roach    {
4374a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4384a9f750fSGreg Roach
4394a9f750fSGreg Roach        if ($media_file !== null) {
4404a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
441a25f0a04SGreg Roach        }
4424a9f750fSGreg Roach
4434a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
44439ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4454a9f750fSGreg Roach        }
4464a9f750fSGreg Roach
4474a9f750fSGreg Roach        return '';
448a25f0a04SGreg Roach    }
449a25f0a04SGreg Roach
450a25f0a04SGreg Roach    /**
451a25f0a04SGreg Roach     * Get the date of birth
452a25f0a04SGreg Roach     *
453a25f0a04SGreg Roach     * @return Date
454a25f0a04SGreg Roach     */
4558f53f488SRico Sonntag    public function getBirthDate(): Date
456c1010edaSGreg Roach    {
457a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
458a25f0a04SGreg Roach            if ($date->isOK()) {
459a25f0a04SGreg Roach                return $date;
460a25f0a04SGreg Roach            }
461a25f0a04SGreg Roach        }
462a25f0a04SGreg Roach
463a25f0a04SGreg Roach        return new Date('');
464a25f0a04SGreg Roach    }
465a25f0a04SGreg Roach
466a25f0a04SGreg Roach    /**
467a25f0a04SGreg Roach     * Get the place of birth
468a25f0a04SGreg Roach     *
46916d0b7f7SRico Sonntag     * @return Place
470a25f0a04SGreg Roach     */
4718f53f488SRico Sonntag    public function getBirthPlace(): Place
472c1010edaSGreg Roach    {
473a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
474a25f0a04SGreg Roach            return $place;
475a25f0a04SGreg Roach        }
476a25f0a04SGreg Roach
477b20ddbf9SGreg Roach        return new Place('', $this->tree);
478a25f0a04SGreg Roach    }
479a25f0a04SGreg Roach
480a25f0a04SGreg Roach    /**
481a25f0a04SGreg Roach     * Get the year of birth
482a25f0a04SGreg Roach     *
483a25f0a04SGreg Roach     * @return string the year of birth
484a25f0a04SGreg Roach     */
4858f53f488SRico Sonntag    public function getBirthYear(): string
486c1010edaSGreg Roach    {
487f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
488a25f0a04SGreg Roach    }
489a25f0a04SGreg Roach
490a25f0a04SGreg Roach    /**
491a25f0a04SGreg Roach     * Get the date of death
492a25f0a04SGreg Roach     *
493a25f0a04SGreg Roach     * @return Date
494a25f0a04SGreg Roach     */
4958f53f488SRico Sonntag    public function getDeathDate(): Date
496c1010edaSGreg Roach    {
497a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
498a25f0a04SGreg Roach            if ($date->isOK()) {
499a25f0a04SGreg Roach                return $date;
500a25f0a04SGreg Roach            }
501a25f0a04SGreg Roach        }
502a25f0a04SGreg Roach
503a25f0a04SGreg Roach        return new Date('');
504a25f0a04SGreg Roach    }
505a25f0a04SGreg Roach
506a25f0a04SGreg Roach    /**
507a25f0a04SGreg Roach     * Get the place of death
508a25f0a04SGreg Roach     *
50916d0b7f7SRico Sonntag     * @return Place
510a25f0a04SGreg Roach     */
5118f53f488SRico Sonntag    public function getDeathPlace(): Place
512c1010edaSGreg Roach    {
513a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
514a25f0a04SGreg Roach            return $place;
515a25f0a04SGreg Roach        }
516a25f0a04SGreg Roach
517b20ddbf9SGreg Roach        return new Place('', $this->tree);
518a25f0a04SGreg Roach    }
519a25f0a04SGreg Roach
520a25f0a04SGreg Roach    /**
521a25f0a04SGreg Roach     * get the death year
522a25f0a04SGreg Roach     *
523a25f0a04SGreg Roach     * @return string the year of death
524a25f0a04SGreg Roach     */
5258f53f488SRico Sonntag    public function getDeathYear(): string
526c1010edaSGreg Roach    {
527f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
528a25f0a04SGreg Roach    }
529a25f0a04SGreg Roach
530a25f0a04SGreg Roach    /**
531a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
53215d603e7SGreg Roach     * Provide the place and full date using a tooltip.
533a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
534a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
535a25f0a04SGreg Roach     *
536a25f0a04SGreg Roach     * @return string
537a25f0a04SGreg Roach     */
5385e6816beSGreg Roach    public function lifespan(): string
539c1010edaSGreg Roach    {
54015d603e7SGreg Roach        // Just the first part of the place name
541392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
542392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
54315d603e7SGreg Roach        // Remove markup from dates
54415d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
54515d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
54615d603e7SGreg Roach
547c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
548dacfe33cSGreg Roach        return I18N::translate(
549a25f0a04SGreg Roach            '%1$s–%2$s',
5502d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5512d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
552a25f0a04SGreg Roach        );
553a25f0a04SGreg Roach    }
554a25f0a04SGreg Roach
555a25f0a04SGreg Roach    /**
556a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
557a25f0a04SGreg Roach     *
558a25f0a04SGreg Roach     * @return Date[]
559a25f0a04SGreg Roach     */
5608f53f488SRico Sonntag    public function getAllBirthDates(): array
561c1010edaSGreg Roach    {
5628d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5638d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
564a25f0a04SGreg Roach            if ($tmp) {
565a25f0a04SGreg Roach                return $tmp;
566a25f0a04SGreg Roach            }
567a25f0a04SGreg Roach        }
568a25f0a04SGreg Roach
56913abd6f3SGreg Roach        return [];
570a25f0a04SGreg Roach    }
571a25f0a04SGreg Roach
572a25f0a04SGreg Roach    /**
573a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
574a25f0a04SGreg Roach     *
5754080d558SGreg Roach     * @return Place[]
576a25f0a04SGreg Roach     */
5778f53f488SRico Sonntag    public function getAllBirthPlaces(): array
578c1010edaSGreg Roach    {
5798d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5808d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
58154c1ab5eSGreg Roach            if ($places !== []) {
5824080d558SGreg Roach                return $places;
583a25f0a04SGreg Roach            }
584a25f0a04SGreg Roach        }
585a25f0a04SGreg Roach
58613abd6f3SGreg Roach        return [];
587a25f0a04SGreg Roach    }
588a25f0a04SGreg Roach
589a25f0a04SGreg Roach    /**
590a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
591a25f0a04SGreg Roach     *
592a25f0a04SGreg Roach     * @return Date[]
593a25f0a04SGreg Roach     */
5948f53f488SRico Sonntag    public function getAllDeathDates(): array
595c1010edaSGreg Roach    {
5968d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
5978d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
598a25f0a04SGreg Roach            if ($tmp) {
599a25f0a04SGreg Roach                return $tmp;
600a25f0a04SGreg Roach            }
601a25f0a04SGreg Roach        }
602a25f0a04SGreg Roach
60313abd6f3SGreg Roach        return [];
604a25f0a04SGreg Roach    }
605a25f0a04SGreg Roach
606a25f0a04SGreg Roach    /**
607a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
608a25f0a04SGreg Roach     *
6094080d558SGreg Roach     * @return Place[]
610a25f0a04SGreg Roach     */
6118f53f488SRico Sonntag    public function getAllDeathPlaces(): array
612c1010edaSGreg Roach    {
6138d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6148d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
61554c1ab5eSGreg Roach            if ($places !== []) {
6164080d558SGreg Roach                return $places;
617a25f0a04SGreg Roach            }
618a25f0a04SGreg Roach        }
619a25f0a04SGreg Roach
62013abd6f3SGreg Roach        return [];
621a25f0a04SGreg Roach    }
622a25f0a04SGreg Roach
623a25f0a04SGreg Roach    /**
624a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
625a25f0a04SGreg Roach     *
626a25f0a04SGreg Roach     * @return Date
627a25f0a04SGreg Roach     */
6288f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
629c1010edaSGreg Roach    {
6308f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
631a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
632a25f0a04SGreg Roach                if ($date->isOK()) {
6334686330aSGreg Roach                    $this->estimated_birth_date = $date;
634a25f0a04SGreg Roach                    break;
635a25f0a04SGreg Roach                }
636a25f0a04SGreg Roach            }
6378f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
63813abd6f3SGreg Roach                $min = [];
63913abd6f3SGreg Roach                $max = [];
640a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
641f5b60decSGreg Roach                if ($tmp->isOK()) {
642f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
643f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
644a25f0a04SGreg Roach                }
64539ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
646a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
647f5b60decSGreg Roach                    if ($tmp->isOK()) {
648f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
649f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
650a25f0a04SGreg Roach                    }
65139ca88baSGreg Roach                    $husband = $family->husband();
652e364afe4SGreg Roach                    if ($husband instanceof self) {
6532e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
654f5b60decSGreg Roach                        if ($tmp->isOK()) {
655f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
656f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
657a25f0a04SGreg Roach                        }
658a25f0a04SGreg Roach                    }
65939ca88baSGreg Roach                    $wife = $family->wife();
660e364afe4SGreg Roach                    if ($wife instanceof self) {
6612e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
662f5b60decSGreg Roach                        if ($tmp->isOK()) {
663f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
664f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
665a25f0a04SGreg Roach                        }
666a25f0a04SGreg Roach                    }
66739ca88baSGreg Roach                    foreach ($family->children() as $child) {
668a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
669f5b60decSGreg Roach                        if ($tmp->isOK()) {
670f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
671f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
672a25f0a04SGreg Roach                        }
673a25f0a04SGreg Roach                    }
674a25f0a04SGreg Roach                }
67539ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
676a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
677f5b60decSGreg Roach                    if ($tmp->isOK()) {
678f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
679f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
680a25f0a04SGreg Roach                    }
68139ca88baSGreg Roach                    $spouse = $family->spouse($this);
682a25f0a04SGreg Roach                    if ($spouse) {
683a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
684f5b60decSGreg Roach                        if ($tmp->isOK()) {
685f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
686f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
687a25f0a04SGreg Roach                        }
688a25f0a04SGreg Roach                    }
68939ca88baSGreg Roach                    foreach ($family->children() as $child) {
690a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
691f5b60decSGreg Roach                        if ($tmp->isOK()) {
692e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
693f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
694a25f0a04SGreg Roach                        }
695a25f0a04SGreg Roach                    }
696a25f0a04SGreg Roach                }
697a25f0a04SGreg Roach                if ($min && $max) {
69859f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
699a25f0a04SGreg Roach
70065e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7014686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
702a25f0a04SGreg Roach                } else {
7034686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
704a25f0a04SGreg Roach                }
705a25f0a04SGreg Roach            }
706a25f0a04SGreg Roach        }
707a25f0a04SGreg Roach
7084686330aSGreg Roach        return $this->estimated_birth_date;
709a25f0a04SGreg Roach    }
710a25f0a04SGreg Roach
711a25f0a04SGreg Roach    /**
712a25f0a04SGreg Roach     * Generate an estimated date of death.
713a25f0a04SGreg Roach     *
714a25f0a04SGreg Roach     * @return Date
715a25f0a04SGreg Roach     */
7168f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
717c1010edaSGreg Roach    {
7184686330aSGreg Roach        if ($this->estimated_death_date === null) {
719a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
720a25f0a04SGreg Roach                if ($date->isOK()) {
7214686330aSGreg Roach                    $this->estimated_death_date = $date;
722a25f0a04SGreg Roach                    break;
723a25f0a04SGreg Roach                }
724a25f0a04SGreg Roach            }
7254686330aSGreg Roach            if ($this->estimated_death_date === null) {
726f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
727c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7284686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
729a25f0a04SGreg Roach                } else {
7304686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
731a25f0a04SGreg Roach                }
732a25f0a04SGreg Roach            }
733a25f0a04SGreg Roach        }
734a25f0a04SGreg Roach
7354686330aSGreg Roach        return $this->estimated_death_date;
736a25f0a04SGreg Roach    }
737a25f0a04SGreg Roach
738a25f0a04SGreg Roach    /**
739a25f0a04SGreg Roach     * Get the sex - M F or U
740a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
741a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
742a25f0a04SGreg Roach     *
743a25f0a04SGreg Roach     * @return string
744a25f0a04SGreg Roach     */
745e364afe4SGreg Roach    public function sex(): string
746c1010edaSGreg Roach    {
747a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
748a25f0a04SGreg Roach            return $match[1];
749a25f0a04SGreg Roach        }
750b2ce94c6SRico Sonntag
751b2ce94c6SRico Sonntag        return 'U';
752a25f0a04SGreg Roach    }
753a25f0a04SGreg Roach
754a25f0a04SGreg Roach    /**
755a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
756a25f0a04SGreg Roach     *
757a25f0a04SGreg Roach     * @return string
758a25f0a04SGreg Roach     */
7598f53f488SRico Sonntag    public function getBoxStyle(): string
760c1010edaSGreg Roach    {
761c1010edaSGreg Roach        $tmp = [
762c1010edaSGreg Roach            'M' => '',
763c1010edaSGreg Roach            'F' => 'F',
764c1010edaSGreg Roach            'U' => 'NN',
765c1010edaSGreg Roach        ];
766a25f0a04SGreg Roach
76739ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
768a25f0a04SGreg Roach    }
769a25f0a04SGreg Roach
770a25f0a04SGreg Roach    /**
771a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
772a25f0a04SGreg Roach     *
773cbc1590aSGreg Roach     * @param int|null $access_level
774a25f0a04SGreg Roach     *
775b5c8fd7eSGreg Roach     * @return Collection<Family>
776a25f0a04SGreg Roach     */
77739ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
778c1010edaSGreg Roach    {
779d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
780d9e083e7SGreg Roach
781d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
782d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
7834b9ff166SGreg Roach        }
7844b9ff166SGreg Roach
78539ca88baSGreg Roach        $families = new Collection();
786d9e083e7SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level) as $fact) {
787dc124885SGreg Roach            $family = $fact->target();
788d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
78939ca88baSGreg Roach                $families->push($family);
790a25f0a04SGreg Roach            }
791a25f0a04SGreg Roach        }
792a25f0a04SGreg Roach
79339ca88baSGreg Roach        return new Collection($families);
794a25f0a04SGreg Roach    }
795a25f0a04SGreg Roach
796a25f0a04SGreg Roach    /**
797a25f0a04SGreg Roach     * Get the current spouse of this individual.
798a25f0a04SGreg Roach     *
799a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
800a25f0a04SGreg Roach     * in chronological order, and take the last one found.
801a25f0a04SGreg Roach     *
802a25f0a04SGreg Roach     * @return Individual|null
803a25f0a04SGreg Roach     */
804e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
805c1010edaSGreg Roach    {
80639ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
80739ca88baSGreg Roach
80839ca88baSGreg Roach        if ($family instanceof Family) {
80939ca88baSGreg Roach            return $family->spouse($this);
810a25f0a04SGreg Roach        }
811b2ce94c6SRico Sonntag
812b2ce94c6SRico Sonntag        return null;
813a25f0a04SGreg Roach    }
814a25f0a04SGreg Roach
815a25f0a04SGreg Roach    /**
816a25f0a04SGreg Roach     * Count the children belonging to this individual.
817a25f0a04SGreg Roach     *
818cbc1590aSGreg Roach     * @return int
819a25f0a04SGreg Roach     */
820e364afe4SGreg Roach    public function numberOfChildren(): int
821c1010edaSGreg Roach    {
8227d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8233dc7bbe9SGreg Roach            return (int) $match[1];
824b2ce94c6SRico Sonntag        }
825b2ce94c6SRico Sonntag
82613abd6f3SGreg Roach        $children = [];
82739ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
82839ca88baSGreg Roach            foreach ($fam->children() as $child) {
829c0935879SGreg Roach                $children[$child->xref()] = true;
830a25f0a04SGreg Roach            }
831a25f0a04SGreg Roach        }
832a25f0a04SGreg Roach
833a25f0a04SGreg Roach        return count($children);
834a25f0a04SGreg Roach    }
835a25f0a04SGreg Roach
836a25f0a04SGreg Roach    /**
837a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
838a25f0a04SGreg Roach     *
839cbc1590aSGreg Roach     * @param int|null $access_level
840a25f0a04SGreg Roach     *
841b5c8fd7eSGreg Roach     * @return Collection<Family>
842a25f0a04SGreg Roach     */
84339ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
844c1010edaSGreg Roach    {
845d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
8464b9ff166SGreg Roach
847d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
848d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
849d9e083e7SGreg Roach        }
850a25f0a04SGreg Roach
85139ca88baSGreg Roach        $families = new Collection();
85239ca88baSGreg Roach
853d9e083e7SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level) as $fact) {
854dc124885SGreg Roach            $family = $fact->target();
855d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
85639ca88baSGreg Roach                $families->push($family);
857a25f0a04SGreg Roach            }
858a25f0a04SGreg Roach        }
859a25f0a04SGreg Roach
860a25f0a04SGreg Roach        return $families;
861a25f0a04SGreg Roach    }
862a25f0a04SGreg Roach
863a25f0a04SGreg Roach    /**
864a25f0a04SGreg Roach     * Get a list of step-parent families.
865a25f0a04SGreg Roach     *
866b5c8fd7eSGreg Roach     * @return Collection<Family>
867a25f0a04SGreg Roach     */
868820b62dfSGreg Roach    public function childStepFamilies(): Collection
869c1010edaSGreg Roach    {
870ed5b6227SGreg Roach        $step_families = new Collection();
87139ca88baSGreg Roach        $families      = $this->childFamilies();
872a25f0a04SGreg Roach        foreach ($families as $family) {
873ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
874ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
87539ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
876ed5b6227SGreg Roach                        $step_families->add($step_family);
877a25f0a04SGreg Roach                    }
878a25f0a04SGreg Roach                }
879a25f0a04SGreg Roach            }
880a25f0a04SGreg Roach        }
881a25f0a04SGreg Roach
8828c627a69SGreg Roach        return $step_families->uniqueStrict(static function (Family $family): string {
8838c627a69SGreg Roach            return $family->xref();
8848c627a69SGreg Roach        });
885a25f0a04SGreg Roach    }
886a25f0a04SGreg Roach
887a25f0a04SGreg Roach    /**
888a25f0a04SGreg Roach     * Get a list of step-parent families.
889a25f0a04SGreg Roach     *
890b5c8fd7eSGreg Roach     * @return Collection<Family>
891a25f0a04SGreg Roach     */
892820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
893c1010edaSGreg Roach    {
89413abd6f3SGreg Roach        $step_families = [];
89539ca88baSGreg Roach        $families      = $this->spouseFamilies();
896820b62dfSGreg Roach
897a25f0a04SGreg Roach        foreach ($families as $family) {
89839ca88baSGreg Roach            $spouse = $family->spouse($this);
899820b62dfSGreg Roach
900d823340dSGreg Roach            if ($spouse instanceof self) {
90139ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
90239ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
903a25f0a04SGreg Roach                        $step_families[] = $step_family;
904a25f0a04SGreg Roach                    }
905a25f0a04SGreg Roach                }
906a25f0a04SGreg Roach            }
907a25f0a04SGreg Roach        }
908a25f0a04SGreg Roach
909820b62dfSGreg Roach        return new Collection($step_families);
910a25f0a04SGreg Roach    }
911a25f0a04SGreg Roach
912a25f0a04SGreg Roach    /**
913a25f0a04SGreg Roach     * A label for a parental family group
914a25f0a04SGreg Roach     *
915a25f0a04SGreg Roach     * @param Family $family
916a25f0a04SGreg Roach     *
917a25f0a04SGreg Roach     * @return string
918a25f0a04SGreg Roach     */
919820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
920c1010edaSGreg Roach    {
9217d70e4a7SGreg Roach        preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match);
922b2ce94c6SRico Sonntag
9237d70e4a7SGreg Roach        $values = [
9247d70e4a7SGreg Roach            'birth'   => I18N::translate('Family with parents'),
9257d70e4a7SGreg Roach            'adopted' => I18N::translate('Family with adoptive parents'),
9267d70e4a7SGreg Roach            'foster'  => I18N::translate('Family with foster parents'),
9277d70e4a7SGreg Roach            'sealing' => /* I18N: “sealing” is a Mormon ceremony. */
9287d70e4a7SGreg Roach                I18N::translate('Family with sealing parents'),
9297d70e4a7SGreg Roach            'rada'    => /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */
9307d70e4a7SGreg Roach                I18N::translate('Family with rada parents'),
9317d70e4a7SGreg Roach        ];
9327d70e4a7SGreg Roach
9337d70e4a7SGreg Roach        return $values[$match[1] ?? 'birth'] ?? $values['birth'];
934a25f0a04SGreg Roach    }
935a25f0a04SGreg Roach
936a25f0a04SGreg Roach    /**
937a25f0a04SGreg Roach     * Create a label for a step family
938a25f0a04SGreg Roach     *
939a25f0a04SGreg Roach     * @param Family $step_family
940a25f0a04SGreg Roach     *
941a25f0a04SGreg Roach     * @return string
942a25f0a04SGreg Roach     */
9438f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
944c1010edaSGreg Roach    {
94539ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
946a25f0a04SGreg Roach            if ($family !== $step_family) {
947a25f0a04SGreg Roach                // Must be a step-family
94839ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
94939ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
950a25f0a04SGreg Roach                        if ($parent === $step_parent) {
951a25f0a04SGreg Roach                            // One common parent - must be a step family
952e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
953a25f0a04SGreg Roach                                // Father’s family with someone else
95439ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
955a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
95639ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
957b2ce94c6SRico Sonntag                                }
958b2ce94c6SRico Sonntag
959a25f0a04SGreg Roach                                /* I18N: A step-family. */
960bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
961a25f0a04SGreg Roach                            }
962b2ce94c6SRico Sonntag
963a25f0a04SGreg Roach                            // Mother’s family with someone else
96439ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
965a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
96639ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
967b2ce94c6SRico Sonntag                            }
968b2ce94c6SRico Sonntag
969a25f0a04SGreg Roach                            /* I18N: A step-family. */
970bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
971a25f0a04SGreg Roach                        }
972a25f0a04SGreg Roach                    }
973a25f0a04SGreg Roach                }
974a25f0a04SGreg Roach            }
975a25f0a04SGreg Roach        }
976a25f0a04SGreg Roach
977a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
978a25f0a04SGreg Roach        return I18N::translate('Family with parents');
979a25f0a04SGreg Roach    }
980a25f0a04SGreg Roach
981225e381fSGreg Roach    /**
982225e381fSGreg Roach     * Get the description for the family.
983225e381fSGreg Roach     *
984225e381fSGreg Roach     * For example, "XXX's family with new wife".
985225e381fSGreg Roach     *
986225e381fSGreg Roach     * @param Family $family
987225e381fSGreg Roach     *
988225e381fSGreg Roach     * @return string
989225e381fSGreg Roach     */
990e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
991c1010edaSGreg Roach    {
99239ca88baSGreg Roach        $spouse = $family->spouse($this);
993225e381fSGreg Roach        if ($spouse) {
994225e381fSGreg Roach            /* I18N: %s is the spouse name */
99539ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
996225e381fSGreg Roach        }
997b2ce94c6SRico Sonntag
99839ca88baSGreg Roach        return $family->fullName();
999225e381fSGreg Roach    }
1000225e381fSGreg Roach
1001a25f0a04SGreg Roach    /**
1002961ec755SGreg Roach     * If this object has no name, what do we call it?
1003961ec755SGreg Roach     *
1004961ec755SGreg Roach     * @return string
1005961ec755SGreg Roach     */
10068f53f488SRico Sonntag    public function getFallBackName(): string
1007c1010edaSGreg Roach    {
1008a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1009a25f0a04SGreg Roach    }
1010a25f0a04SGreg Roach
1011a25f0a04SGreg Roach    /**
1012a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1013a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1014a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1015a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1016a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1017a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1018a25f0a04SGreg Roach     *
1019a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1020a25f0a04SGreg Roach     * 2 GIVN Robert
1021a25f0a04SGreg Roach     * 2 SPFX de
1022a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1023a25f0a04SGreg Roach     * 2 NICK The Bald
1024a25f0a04SGreg Roach     *
1025a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1026a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1027a25f0a04SGreg Roach     *
1028a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1029a25f0a04SGreg Roach     *
1030a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1031a25f0a04SGreg Roach     * or
1032a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1033a25f0a04SGreg Roach     * 2 GIVN Carlos
1034a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1035a25f0a04SGreg Roach     *
1036a25f0a04SGreg Roach     * @param string $type
1037a25f0a04SGreg Roach     * @param string $full
1038a25f0a04SGreg Roach     * @param string $gedcom
1039e364afe4SGreg Roach     *
1040e364afe4SGreg Roach     * @return void
1041a25f0a04SGreg Roach     */
1042e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1043c1010edaSGreg Roach    {
1044a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1045a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1046a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1047a25f0a04SGreg Roach
104876f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1049a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1050a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1051a25f0a04SGreg Roach
1052a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
105376f666f4SGreg Roach        if ($SURN !== '') {
1054a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1055a25f0a04SGreg Roach        } else {
105613abd6f3SGreg Roach            $SURNS = [];
1057a25f0a04SGreg Roach        }
105876f666f4SGreg Roach
1059a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1060a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1061a25f0a04SGreg Roach
1062a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1063a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1064a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1065a25f0a04SGreg Roach
1066a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
106776f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1068e364afe4SGreg Roach            $full .= '/';
1069a25f0a04SGreg Roach        }
1070a25f0a04SGreg Roach
1071a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1072a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1073a25f0a04SGreg Roach
1074a25f0a04SGreg Roach        // Extract the surname.
1075a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1076a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1077a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1078a25f0a04SGreg Roach        } else {
1079a25f0a04SGreg Roach            $surname = '';
1080a25f0a04SGreg Roach        }
1081a25f0a04SGreg Roach
1082a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1083a25f0a04SGreg Roach        if (!$SURNS) {
1084a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1085a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1086a25f0a04SGreg Roach                $SURNS = $matches[1];
1087a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1088a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1089a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1090a25f0a04SGreg Roach                }
1091a25f0a04SGreg Roach            } else {
1092a25f0a04SGreg Roach                // It is valid not to have a surname at all
109313abd6f3SGreg Roach                $SURNS = [''];
1094a25f0a04SGreg Roach            }
1095a25f0a04SGreg Roach        }
1096a25f0a04SGreg Roach
1097a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1098a25f0a04SGreg Roach        if (!$GIVN) {
1099a25f0a04SGreg Roach            $GIVN = preg_replace(
110013abd6f3SGreg Roach                [
1101c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1102c1010edaSGreg Roach                    // remove surname
1103c1010edaSGreg Roach                    '/ ?".+"/',
1104c1010edaSGreg Roach                    // remove nickname
1105c1010edaSGreg Roach                    '/ {2,}/',
1106c1010edaSGreg Roach                    // multiple spaces, caused by the above
1107c1010edaSGreg Roach                    '/^ | $/',
1108c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
110913abd6f3SGreg Roach                ],
111013abd6f3SGreg Roach                [
1111a25f0a04SGreg Roach                    ' ',
1112a25f0a04SGreg Roach                    ' ',
1113a25f0a04SGreg Roach                    ' ',
1114a25f0a04SGreg Roach                    '',
111513abd6f3SGreg Roach                ],
1116a25f0a04SGreg Roach                $full
1117a25f0a04SGreg Roach            );
1118a25f0a04SGreg Roach        }
1119a25f0a04SGreg Roach
1120a25f0a04SGreg Roach        // Add placeholder for unknown given name
1121a25f0a04SGreg Roach        if (!$GIVN) {
1122d823340dSGreg Roach            $GIVN = self::PRAENOMEN_NESCIO;
112373f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1124a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1125a25f0a04SGreg Roach        }
1126a25f0a04SGreg Roach
1127a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1128a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1129a25f0a04SGreg Roach        // $full is for display on-screen
1130a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1131a25f0a04SGreg Roach
1132a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1133d823340dSGreg Roach        $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full);
1134d823340dSGreg Roach        $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full);
1135c6f196c3SGreg Roach        // Format for display
1136d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1137acc34ea1SGreg Roach        // Localise quotation marks around the nickname
11380b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
1139c652cdbdSGreg Roach            return '<q class="wt-nickname">' . $matches[1] . '</q>';
11408d68cabeSGreg Roach        }, $full);
1141a25f0a04SGreg Roach
1142c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1143a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1144a25f0a04SGreg Roach
1145a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1146a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1147a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1148a25f0a04SGreg Roach
1149ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1150a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1151e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1152a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1153e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1154a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1155a25f0a04SGreg Roach            }
1156a25f0a04SGreg Roach
1157bdb3725aSGreg Roach            $this->getAllNames[] = [
1158a25f0a04SGreg Roach                'type'    => $type,
1159a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1160c1010edaSGreg Roach                'full'    => $full,
1161c1010edaSGreg Roach                // This is used for display
1162c1010edaSGreg Roach                'fullNN'  => $fullNN,
1163c1010edaSGreg Roach                // This goes into the database
1164c1010edaSGreg Roach                'surname' => $surname,
1165c1010edaSGreg Roach                // This goes into the database
1166c1010edaSGreg Roach                'givn'    => $GIVN,
1167c1010edaSGreg Roach                // This goes into the database
1168c1010edaSGreg Roach                'surn'    => $SURN,
1169c1010edaSGreg Roach                // This goes into the database
117013abd6f3SGreg Roach            ];
1171a25f0a04SGreg Roach        }
1172a25f0a04SGreg Roach    }
1173a25f0a04SGreg Roach
1174a25f0a04SGreg Roach    /**
117576692c8bSGreg Roach     * Extract names from the GEDCOM record.
1176c7ff4153SGreg Roach     *
1177c7ff4153SGreg Roach     * @return void
1178a25f0a04SGreg Roach     */
1179e364afe4SGreg Roach    public function extractNames(): void
1180c1010edaSGreg Roach    {
1181d9e083e7SGreg Roach        $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree);
1182d9e083e7SGreg Roach
11838f53f488SRico Sonntag        $this->extractNamesFromFacts(
11848f53f488SRico Sonntag            1,
11858f53f488SRico Sonntag            'NAME',
1186d9e083e7SGreg Roach            $this->facts(['NAME'], false, $access_level)
11878f53f488SRico Sonntag        );
1188a25f0a04SGreg Roach    }
1189a25f0a04SGreg Roach
1190a25f0a04SGreg Roach    /**
1191a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1192a25f0a04SGreg Roach     * selection items or favorites.
1193a25f0a04SGreg Roach     *
1194a25f0a04SGreg Roach     * @return string
1195a25f0a04SGreg Roach     */
11968f53f488SRico Sonntag    public function formatListDetails(): string
1197c1010edaSGreg Roach    {
1198a25f0a04SGreg Roach        return
11998d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
12008d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1201a25f0a04SGreg Roach    }
12028091bfd1SGreg Roach
12038091bfd1SGreg Roach    /**
12048091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
12058091bfd1SGreg Roach     */
12068091bfd1SGreg Roach    public function lock(): void
12078091bfd1SGreg Roach    {
12088091bfd1SGreg Roach        DB::table('individuals')
12098091bfd1SGreg Roach            ->where('i_file', '=', $this->tree->id())
12108091bfd1SGreg Roach            ->where('i_id', '=', $this->xref())
12118091bfd1SGreg Roach            ->lockForUpdate()
12128091bfd1SGreg Roach            ->get();
12138091bfd1SGreg Roach    }
1214a25f0a04SGreg Roach}
1215