xref: /webtrees/app/Individual.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
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
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     *
56d4ac8227SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::individualFactory()
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     *
96d4ac8227SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::individualFactory()
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)
1841fe542e9SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH);
1851fe542e9SGreg 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     */
203*24f2a3afSGreg Roach    private static function isRelated(Individual $target, int $distance): bool
204c1010edaSGreg Roach    {
205a25f0a04SGreg Roach        static $cache = null;
206a25f0a04SGreg Roach
2071fe542e9SGreg 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     */
435*24f2a3afSGreg Roach    public function displayImage(int $width, int $height, string $fit, array $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) {
563d240bb87SGreg Roach            $dates = $this->getAllEventDates([$event]);
564d240bb87SGreg Roach
565d240bb87SGreg Roach            if ($dates !== []) {
566d240bb87SGreg Roach                return $dates;
567a25f0a04SGreg Roach            }
568a25f0a04SGreg Roach        }
569a25f0a04SGreg Roach
57013abd6f3SGreg Roach        return [];
571a25f0a04SGreg Roach    }
572a25f0a04SGreg Roach
573a25f0a04SGreg Roach    /**
574a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
575a25f0a04SGreg Roach     *
5764080d558SGreg Roach     * @return Place[]
577a25f0a04SGreg Roach     */
5788f53f488SRico Sonntag    public function getAllBirthPlaces(): array
579c1010edaSGreg Roach    {
5808d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5818d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
582d240bb87SGreg Roach
58354c1ab5eSGreg Roach            if ($places !== []) {
5844080d558SGreg Roach                return $places;
585a25f0a04SGreg Roach            }
586a25f0a04SGreg Roach        }
587a25f0a04SGreg Roach
58813abd6f3SGreg Roach        return [];
589a25f0a04SGreg Roach    }
590a25f0a04SGreg Roach
591a25f0a04SGreg Roach    /**
592a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
593a25f0a04SGreg Roach     *
594a25f0a04SGreg Roach     * @return Date[]
595a25f0a04SGreg Roach     */
5968f53f488SRico Sonntag    public function getAllDeathDates(): array
597c1010edaSGreg Roach    {
5988d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
599d240bb87SGreg Roach            $dates = $this->getAllEventDates([$event]);
600d240bb87SGreg Roach
601d240bb87SGreg Roach            if ($dates !== []) {
602d240bb87SGreg Roach                return $dates;
603a25f0a04SGreg Roach            }
604a25f0a04SGreg Roach        }
605a25f0a04SGreg Roach
60613abd6f3SGreg Roach        return [];
607a25f0a04SGreg Roach    }
608a25f0a04SGreg Roach
609a25f0a04SGreg Roach    /**
610a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
611a25f0a04SGreg Roach     *
6124080d558SGreg Roach     * @return Place[]
613a25f0a04SGreg Roach     */
6148f53f488SRico Sonntag    public function getAllDeathPlaces(): array
615c1010edaSGreg Roach    {
6168d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6178d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
618d240bb87SGreg Roach
61954c1ab5eSGreg Roach            if ($places !== []) {
6204080d558SGreg Roach                return $places;
621a25f0a04SGreg Roach            }
622a25f0a04SGreg Roach        }
623a25f0a04SGreg Roach
62413abd6f3SGreg Roach        return [];
625a25f0a04SGreg Roach    }
626a25f0a04SGreg Roach
627a25f0a04SGreg Roach    /**
628a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
629a25f0a04SGreg Roach     *
630a25f0a04SGreg Roach     * @return Date
631a25f0a04SGreg Roach     */
6328f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
633c1010edaSGreg Roach    {
6348f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
635a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
636a25f0a04SGreg Roach                if ($date->isOK()) {
6374686330aSGreg Roach                    $this->estimated_birth_date = $date;
638a25f0a04SGreg Roach                    break;
639a25f0a04SGreg Roach                }
640a25f0a04SGreg Roach            }
6418f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
64213abd6f3SGreg Roach                $min = [];
64313abd6f3SGreg Roach                $max = [];
644a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
645f5b60decSGreg Roach                if ($tmp->isOK()) {
646f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
647f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
648a25f0a04SGreg Roach                }
64939ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
650a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
651f5b60decSGreg Roach                    if ($tmp->isOK()) {
652f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
653f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
654a25f0a04SGreg Roach                    }
65539ca88baSGreg Roach                    $husband = $family->husband();
656e364afe4SGreg Roach                    if ($husband instanceof self) {
6572e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
658f5b60decSGreg Roach                        if ($tmp->isOK()) {
659f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
660f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
661a25f0a04SGreg Roach                        }
662a25f0a04SGreg Roach                    }
66339ca88baSGreg Roach                    $wife = $family->wife();
664e364afe4SGreg Roach                    if ($wife instanceof self) {
6652e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
666f5b60decSGreg Roach                        if ($tmp->isOK()) {
667f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
668f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
669a25f0a04SGreg Roach                        }
670a25f0a04SGreg Roach                    }
67139ca88baSGreg Roach                    foreach ($family->children() as $child) {
672a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
673f5b60decSGreg Roach                        if ($tmp->isOK()) {
674f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
675f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
676a25f0a04SGreg Roach                        }
677a25f0a04SGreg Roach                    }
678a25f0a04SGreg Roach                }
67939ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
680a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
681f5b60decSGreg Roach                    if ($tmp->isOK()) {
682f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
683f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
684a25f0a04SGreg Roach                    }
68539ca88baSGreg Roach                    $spouse = $family->spouse($this);
686a25f0a04SGreg Roach                    if ($spouse) {
687a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
688f5b60decSGreg Roach                        if ($tmp->isOK()) {
689f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
690f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
691a25f0a04SGreg Roach                        }
692a25f0a04SGreg Roach                    }
69339ca88baSGreg Roach                    foreach ($family->children() as $child) {
694a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
695f5b60decSGreg Roach                        if ($tmp->isOK()) {
696e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
697f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
698a25f0a04SGreg Roach                        }
699a25f0a04SGreg Roach                    }
700a25f0a04SGreg Roach                }
701a25f0a04SGreg Roach                if ($min && $max) {
70259f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
703a25f0a04SGreg Roach
70465e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7054686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
706a25f0a04SGreg Roach                } else {
7074686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
708a25f0a04SGreg Roach                }
709a25f0a04SGreg Roach            }
710a25f0a04SGreg Roach        }
711a25f0a04SGreg Roach
7124686330aSGreg Roach        return $this->estimated_birth_date;
713a25f0a04SGreg Roach    }
714a25f0a04SGreg Roach
715a25f0a04SGreg Roach    /**
716a25f0a04SGreg Roach     * Generate an estimated date of death.
717a25f0a04SGreg Roach     *
718a25f0a04SGreg Roach     * @return Date
719a25f0a04SGreg Roach     */
7208f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
721c1010edaSGreg Roach    {
7224686330aSGreg Roach        if ($this->estimated_death_date === null) {
723a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
724a25f0a04SGreg Roach                if ($date->isOK()) {
7254686330aSGreg Roach                    $this->estimated_death_date = $date;
726a25f0a04SGreg Roach                    break;
727a25f0a04SGreg Roach                }
728a25f0a04SGreg Roach            }
7294686330aSGreg Roach            if ($this->estimated_death_date === null) {
730f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
731c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7324686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
733a25f0a04SGreg Roach                } else {
7344686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
735a25f0a04SGreg Roach                }
736a25f0a04SGreg Roach            }
737a25f0a04SGreg Roach        }
738a25f0a04SGreg Roach
7394686330aSGreg Roach        return $this->estimated_death_date;
740a25f0a04SGreg Roach    }
741a25f0a04SGreg Roach
742a25f0a04SGreg Roach    /**
743a25f0a04SGreg Roach     * Get the sex - M F or U
744a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
745a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
746a25f0a04SGreg Roach     *
747a25f0a04SGreg Roach     * @return string
748a25f0a04SGreg Roach     */
749e364afe4SGreg Roach    public function sex(): string
750c1010edaSGreg Roach    {
751a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
752a25f0a04SGreg Roach            return $match[1];
753a25f0a04SGreg Roach        }
754b2ce94c6SRico Sonntag
755b2ce94c6SRico Sonntag        return 'U';
756a25f0a04SGreg Roach    }
757a25f0a04SGreg Roach
758a25f0a04SGreg Roach    /**
759a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
760a25f0a04SGreg Roach     *
761a25f0a04SGreg Roach     * @return string
762a25f0a04SGreg Roach     */
7638f53f488SRico Sonntag    public function getBoxStyle(): string
764c1010edaSGreg Roach    {
765c1010edaSGreg Roach        $tmp = [
766c1010edaSGreg Roach            'M' => '',
767c1010edaSGreg Roach            'F' => 'F',
768c1010edaSGreg Roach            'U' => 'NN',
769c1010edaSGreg Roach        ];
770a25f0a04SGreg Roach
77139ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
772a25f0a04SGreg Roach    }
773a25f0a04SGreg Roach
774a25f0a04SGreg Roach    /**
775a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
776a25f0a04SGreg Roach     *
777cbc1590aSGreg Roach     * @param int|null $access_level
778a25f0a04SGreg Roach     *
779b5c8fd7eSGreg Roach     * @return Collection<Family>
780a25f0a04SGreg Roach     */
78139ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
782c1010edaSGreg Roach    {
783d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
784d9e083e7SGreg Roach
785d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
786d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
7874b9ff166SGreg Roach        }
7884b9ff166SGreg Roach
78939ca88baSGreg Roach        $families = new Collection();
790d9e083e7SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level) as $fact) {
791dc124885SGreg Roach            $family = $fact->target();
792d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
79339ca88baSGreg Roach                $families->push($family);
794a25f0a04SGreg Roach            }
795a25f0a04SGreg Roach        }
796a25f0a04SGreg Roach
79739ca88baSGreg Roach        return new Collection($families);
798a25f0a04SGreg Roach    }
799a25f0a04SGreg Roach
800a25f0a04SGreg Roach    /**
801a25f0a04SGreg Roach     * Get the current spouse of this individual.
802a25f0a04SGreg Roach     *
803a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
804a25f0a04SGreg Roach     * in chronological order, and take the last one found.
805a25f0a04SGreg Roach     *
806a25f0a04SGreg Roach     * @return Individual|null
807a25f0a04SGreg Roach     */
808e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
809c1010edaSGreg Roach    {
81039ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
81139ca88baSGreg Roach
81239ca88baSGreg Roach        if ($family instanceof Family) {
81339ca88baSGreg Roach            return $family->spouse($this);
814a25f0a04SGreg Roach        }
815b2ce94c6SRico Sonntag
816b2ce94c6SRico Sonntag        return null;
817a25f0a04SGreg Roach    }
818a25f0a04SGreg Roach
819a25f0a04SGreg Roach    /**
820a25f0a04SGreg Roach     * Count the children belonging to this individual.
821a25f0a04SGreg Roach     *
822cbc1590aSGreg Roach     * @return int
823a25f0a04SGreg Roach     */
824e364afe4SGreg Roach    public function numberOfChildren(): int
825c1010edaSGreg Roach    {
8267d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8273dc7bbe9SGreg Roach            return (int) $match[1];
828b2ce94c6SRico Sonntag        }
829b2ce94c6SRico Sonntag
83013abd6f3SGreg Roach        $children = [];
83139ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
83239ca88baSGreg Roach            foreach ($fam->children() as $child) {
833c0935879SGreg Roach                $children[$child->xref()] = true;
834a25f0a04SGreg Roach            }
835a25f0a04SGreg Roach        }
836a25f0a04SGreg Roach
837a25f0a04SGreg Roach        return count($children);
838a25f0a04SGreg Roach    }
839a25f0a04SGreg Roach
840a25f0a04SGreg Roach    /**
841a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
842a25f0a04SGreg Roach     *
843cbc1590aSGreg Roach     * @param int|null $access_level
844a25f0a04SGreg Roach     *
845b5c8fd7eSGreg Roach     * @return Collection<Family>
846a25f0a04SGreg Roach     */
84739ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
848c1010edaSGreg Roach    {
849d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
8504b9ff166SGreg Roach
851d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
852d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
853d9e083e7SGreg Roach        }
854a25f0a04SGreg Roach
85539ca88baSGreg Roach        $families = new Collection();
85639ca88baSGreg Roach
857d9e083e7SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level) as $fact) {
858dc124885SGreg Roach            $family = $fact->target();
859d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
86039ca88baSGreg Roach                $families->push($family);
861a25f0a04SGreg Roach            }
862a25f0a04SGreg Roach        }
863a25f0a04SGreg Roach
864a25f0a04SGreg Roach        return $families;
865a25f0a04SGreg Roach    }
866a25f0a04SGreg Roach
867a25f0a04SGreg Roach    /**
868a25f0a04SGreg Roach     * Get a list of step-parent families.
869a25f0a04SGreg Roach     *
870b5c8fd7eSGreg Roach     * @return Collection<Family>
871a25f0a04SGreg Roach     */
872820b62dfSGreg Roach    public function childStepFamilies(): Collection
873c1010edaSGreg Roach    {
874ed5b6227SGreg Roach        $step_families = new Collection();
87539ca88baSGreg Roach        $families      = $this->childFamilies();
876a25f0a04SGreg Roach        foreach ($families as $family) {
877ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
878ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
87939ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
880ed5b6227SGreg Roach                        $step_families->add($step_family);
881a25f0a04SGreg Roach                    }
882a25f0a04SGreg Roach                }
883a25f0a04SGreg Roach            }
884a25f0a04SGreg Roach        }
885a25f0a04SGreg Roach
8868c627a69SGreg Roach        return $step_families->uniqueStrict(static function (Family $family): string {
8878c627a69SGreg Roach            return $family->xref();
8888c627a69SGreg Roach        });
889a25f0a04SGreg Roach    }
890a25f0a04SGreg Roach
891a25f0a04SGreg Roach    /**
892a25f0a04SGreg Roach     * Get a list of step-parent families.
893a25f0a04SGreg Roach     *
894b5c8fd7eSGreg Roach     * @return Collection<Family>
895a25f0a04SGreg Roach     */
896820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
897c1010edaSGreg Roach    {
89813abd6f3SGreg Roach        $step_families = [];
89939ca88baSGreg Roach        $families      = $this->spouseFamilies();
900820b62dfSGreg Roach
901a25f0a04SGreg Roach        foreach ($families as $family) {
90239ca88baSGreg Roach            $spouse = $family->spouse($this);
903820b62dfSGreg Roach
904d823340dSGreg Roach            if ($spouse instanceof self) {
90539ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
90639ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
907a25f0a04SGreg Roach                        $step_families[] = $step_family;
908a25f0a04SGreg Roach                    }
909a25f0a04SGreg Roach                }
910a25f0a04SGreg Roach            }
911a25f0a04SGreg Roach        }
912a25f0a04SGreg Roach
913820b62dfSGreg Roach        return new Collection($step_families);
914a25f0a04SGreg Roach    }
915a25f0a04SGreg Roach
916a25f0a04SGreg Roach    /**
917a25f0a04SGreg Roach     * A label for a parental family group
918a25f0a04SGreg Roach     *
919a25f0a04SGreg Roach     * @param Family $family
920a25f0a04SGreg Roach     *
921a25f0a04SGreg Roach     * @return string
922a25f0a04SGreg Roach     */
923820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
924c1010edaSGreg Roach    {
9257d70e4a7SGreg Roach        preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match);
926b2ce94c6SRico Sonntag
9277d70e4a7SGreg Roach        $values = [
9287d70e4a7SGreg Roach            'birth'   => I18N::translate('Family with parents'),
9297d70e4a7SGreg Roach            'adopted' => I18N::translate('Family with adoptive parents'),
9307d70e4a7SGreg Roach            'foster'  => I18N::translate('Family with foster parents'),
9317d70e4a7SGreg Roach            'sealing' => /* I18N: “sealing” is a Mormon ceremony. */
9327d70e4a7SGreg Roach                I18N::translate('Family with sealing parents'),
9337d70e4a7SGreg Roach            'rada'    => /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */
9347d70e4a7SGreg Roach                I18N::translate('Family with rada parents'),
9357d70e4a7SGreg Roach        ];
9367d70e4a7SGreg Roach
9377d70e4a7SGreg Roach        return $values[$match[1] ?? 'birth'] ?? $values['birth'];
938a25f0a04SGreg Roach    }
939a25f0a04SGreg Roach
940a25f0a04SGreg Roach    /**
941a25f0a04SGreg Roach     * Create a label for a step family
942a25f0a04SGreg Roach     *
943a25f0a04SGreg Roach     * @param Family $step_family
944a25f0a04SGreg Roach     *
945a25f0a04SGreg Roach     * @return string
946a25f0a04SGreg Roach     */
9478f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
948c1010edaSGreg Roach    {
94939ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
950a25f0a04SGreg Roach            if ($family !== $step_family) {
951a25f0a04SGreg Roach                // Must be a step-family
95239ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
95339ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
954a25f0a04SGreg Roach                        if ($parent === $step_parent) {
955a25f0a04SGreg Roach                            // One common parent - must be a step family
956e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
957a25f0a04SGreg Roach                                // Father’s family with someone else
95839ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
959a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
96039ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
961b2ce94c6SRico Sonntag                                }
962b2ce94c6SRico Sonntag
963a25f0a04SGreg Roach                                /* I18N: A step-family. */
964bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
965a25f0a04SGreg Roach                            }
966b2ce94c6SRico Sonntag
967a25f0a04SGreg Roach                            // Mother’s family with someone else
96839ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
969a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
97039ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
971b2ce94c6SRico Sonntag                            }
972b2ce94c6SRico Sonntag
973a25f0a04SGreg Roach                            /* I18N: A step-family. */
974bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
975a25f0a04SGreg Roach                        }
976a25f0a04SGreg Roach                    }
977a25f0a04SGreg Roach                }
978a25f0a04SGreg Roach            }
979a25f0a04SGreg Roach        }
980a25f0a04SGreg Roach
981a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
982a25f0a04SGreg Roach        return I18N::translate('Family with parents');
983a25f0a04SGreg Roach    }
984a25f0a04SGreg Roach
985225e381fSGreg Roach    /**
986225e381fSGreg Roach     * Get the description for the family.
987225e381fSGreg Roach     *
988225e381fSGreg Roach     * For example, "XXX's family with new wife".
989225e381fSGreg Roach     *
990225e381fSGreg Roach     * @param Family $family
991225e381fSGreg Roach     *
992225e381fSGreg Roach     * @return string
993225e381fSGreg Roach     */
994e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
995c1010edaSGreg Roach    {
99639ca88baSGreg Roach        $spouse = $family->spouse($this);
997225e381fSGreg Roach        if ($spouse) {
998225e381fSGreg Roach            /* I18N: %s is the spouse name */
99939ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1000225e381fSGreg Roach        }
1001b2ce94c6SRico Sonntag
100239ca88baSGreg Roach        return $family->fullName();
1003225e381fSGreg Roach    }
1004225e381fSGreg Roach
1005a25f0a04SGreg Roach    /**
1006961ec755SGreg Roach     * If this object has no name, what do we call it?
1007961ec755SGreg Roach     *
1008961ec755SGreg Roach     * @return string
1009961ec755SGreg Roach     */
10108f53f488SRico Sonntag    public function getFallBackName(): string
1011c1010edaSGreg Roach    {
1012a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1013a25f0a04SGreg Roach    }
1014a25f0a04SGreg Roach
1015a25f0a04SGreg Roach    /**
1016a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1017a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1018a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1019a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1020a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1021a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1022a25f0a04SGreg Roach     *
1023a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1024a25f0a04SGreg Roach     * 2 GIVN Robert
1025a25f0a04SGreg Roach     * 2 SPFX de
1026a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1027a25f0a04SGreg Roach     * 2 NICK The Bald
1028a25f0a04SGreg Roach     *
1029a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1030a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1031a25f0a04SGreg Roach     *
1032a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1033a25f0a04SGreg Roach     *
1034a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1035a25f0a04SGreg Roach     * or
1036a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1037a25f0a04SGreg Roach     * 2 GIVN Carlos
1038a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1039a25f0a04SGreg Roach     *
1040a25f0a04SGreg Roach     * @param string $type
1041a25f0a04SGreg Roach     * @param string $full
1042a25f0a04SGreg Roach     * @param string $gedcom
1043e364afe4SGreg Roach     *
1044e364afe4SGreg Roach     * @return void
1045a25f0a04SGreg Roach     */
1046e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1047c1010edaSGreg Roach    {
1048a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1049a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1050a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1051a25f0a04SGreg Roach
105276f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1053a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1054a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1055a25f0a04SGreg Roach
1056a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
105776f666f4SGreg Roach        if ($SURN !== '') {
1058a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1059a25f0a04SGreg Roach        } else {
106013abd6f3SGreg Roach            $SURNS = [];
1061a25f0a04SGreg Roach        }
106276f666f4SGreg Roach
1063a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1064a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1065a25f0a04SGreg Roach
1066a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1067a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1068a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1069a25f0a04SGreg Roach
1070a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
107176f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1072e364afe4SGreg Roach            $full .= '/';
1073a25f0a04SGreg Roach        }
1074a25f0a04SGreg Roach
1075a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1076a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1077a25f0a04SGreg Roach
1078a25f0a04SGreg Roach        // Extract the surname.
1079a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1080a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1081a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1082a25f0a04SGreg Roach        } else {
1083a25f0a04SGreg Roach            $surname = '';
1084a25f0a04SGreg Roach        }
1085a25f0a04SGreg Roach
1086a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1087a25f0a04SGreg Roach        if (!$SURNS) {
1088a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1089a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1090a25f0a04SGreg Roach                $SURNS = $matches[1];
1091a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1092a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1093a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1094a25f0a04SGreg Roach                }
1095a25f0a04SGreg Roach            } else {
1096a25f0a04SGreg Roach                // It is valid not to have a surname at all
109713abd6f3SGreg Roach                $SURNS = [''];
1098a25f0a04SGreg Roach            }
1099a25f0a04SGreg Roach        }
1100a25f0a04SGreg Roach
1101a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1102a25f0a04SGreg Roach        if (!$GIVN) {
1103a25f0a04SGreg Roach            $GIVN = preg_replace(
110413abd6f3SGreg Roach                [
1105c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1106c1010edaSGreg Roach                    // remove surname
1107c1010edaSGreg Roach                    '/ ?".+"/',
1108c1010edaSGreg Roach                    // remove nickname
1109c1010edaSGreg Roach                    '/ {2,}/',
1110c1010edaSGreg Roach                    // multiple spaces, caused by the above
1111c1010edaSGreg Roach                    '/^ | $/',
1112c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
111313abd6f3SGreg Roach                ],
111413abd6f3SGreg Roach                [
1115a25f0a04SGreg Roach                    ' ',
1116a25f0a04SGreg Roach                    ' ',
1117a25f0a04SGreg Roach                    ' ',
1118a25f0a04SGreg Roach                    '',
111913abd6f3SGreg Roach                ],
1120a25f0a04SGreg Roach                $full
1121a25f0a04SGreg Roach            );
1122a25f0a04SGreg Roach        }
1123a25f0a04SGreg Roach
1124a25f0a04SGreg Roach        // Add placeholder for unknown given name
1125a25f0a04SGreg Roach        if (!$GIVN) {
1126d823340dSGreg Roach            $GIVN = self::PRAENOMEN_NESCIO;
112773f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1128a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1129a25f0a04SGreg Roach        }
1130a25f0a04SGreg Roach
1131a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1132a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1133a25f0a04SGreg Roach        // $full is for display on-screen
1134a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1135a25f0a04SGreg Roach
1136a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1137d823340dSGreg Roach        $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full);
1138d823340dSGreg Roach        $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full);
1139c6f196c3SGreg Roach        // Format for display
1140d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1141acc34ea1SGreg Roach        // Localise quotation marks around the nickname
11420b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
1143c652cdbdSGreg Roach            return '<q class="wt-nickname">' . $matches[1] . '</q>';
11448d68cabeSGreg Roach        }, $full);
1145a25f0a04SGreg Roach
1146c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1147a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1148a25f0a04SGreg Roach
1149a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1150a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1151a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1152a25f0a04SGreg Roach
1153ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1154a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1155e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1156a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1157e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1158a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1159a25f0a04SGreg Roach            }
1160a25f0a04SGreg Roach
1161bdb3725aSGreg Roach            $this->getAllNames[] = [
1162a25f0a04SGreg Roach                'type'    => $type,
1163a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1164c1010edaSGreg Roach                'full'    => $full,
1165c1010edaSGreg Roach                // This is used for display
1166c1010edaSGreg Roach                'fullNN'  => $fullNN,
1167c1010edaSGreg Roach                // This goes into the database
1168c1010edaSGreg Roach                'surname' => $surname,
1169c1010edaSGreg Roach                // This goes into the database
1170c1010edaSGreg Roach                'givn'    => $GIVN,
1171c1010edaSGreg Roach                // This goes into the database
1172c1010edaSGreg Roach                'surn'    => $SURN,
1173c1010edaSGreg Roach                // This goes into the database
117413abd6f3SGreg Roach            ];
1175a25f0a04SGreg Roach        }
1176a25f0a04SGreg Roach    }
1177a25f0a04SGreg Roach
1178a25f0a04SGreg Roach    /**
117976692c8bSGreg Roach     * Extract names from the GEDCOM record.
1180c7ff4153SGreg Roach     *
1181c7ff4153SGreg Roach     * @return void
1182a25f0a04SGreg Roach     */
1183e364afe4SGreg Roach    public function extractNames(): void
1184c1010edaSGreg Roach    {
1185d9e083e7SGreg Roach        $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree);
1186d9e083e7SGreg Roach
11878f53f488SRico Sonntag        $this->extractNamesFromFacts(
11888f53f488SRico Sonntag            1,
11898f53f488SRico Sonntag            'NAME',
1190d9e083e7SGreg Roach            $this->facts(['NAME'], false, $access_level)
11918f53f488SRico Sonntag        );
1192a25f0a04SGreg Roach    }
1193a25f0a04SGreg Roach
1194a25f0a04SGreg Roach    /**
1195a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1196a25f0a04SGreg Roach     * selection items or favorites.
1197a25f0a04SGreg Roach     *
1198a25f0a04SGreg Roach     * @return string
1199a25f0a04SGreg Roach     */
12008f53f488SRico Sonntag    public function formatListDetails(): string
1201c1010edaSGreg Roach    {
1202a25f0a04SGreg Roach        return
12038d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
12048d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1205a25f0a04SGreg Roach    }
12068091bfd1SGreg Roach
12078091bfd1SGreg Roach    /**
12088091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
12098091bfd1SGreg Roach     */
12108091bfd1SGreg Roach    public function lock(): void
12118091bfd1SGreg Roach    {
12128091bfd1SGreg Roach        DB::table('individuals')
12138091bfd1SGreg Roach            ->where('i_file', '=', $this->tree->id())
12148091bfd1SGreg Roach            ->where('i_id', '=', $this->xref())
12158091bfd1SGreg Roach            ->lockForUpdate()
12168091bfd1SGreg Roach            ->get();
12178091bfd1SGreg Roach    }
1218a25f0a04SGreg Roach}
1219