xref: /webtrees/app/Individual.php (revision 8091bfd1be4ee831bc13b9f8b2b83d7a646242a6)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fb4e87cSGreg Roach * Copyright (C) 2020 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;
24852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
252e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2639ca88baSGreg Roachuse Illuminate\Support\Collection;
27a25f0a04SGreg Roach
287d70e4a7SGreg Roachuse function preg_match;
297d70e4a7SGreg Roach
30a25f0a04SGreg Roach/**
3176692c8bSGreg Roach * A GEDCOM individual (INDI) object.
32a25f0a04SGreg Roach */
33c1010edaSGreg Roachclass Individual extends GedcomRecord
34c1010edaSGreg Roach{
3516d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3616d6367aSGreg Roach
378fb4e87cSGreg Roach    // Placeholders to indicate unknown names
388fb4e87cSGreg Roach    public const NOMEN_NESCIO     = '@N.N.';
398fb4e87cSGreg Roach    public const PRAENOMEN_NESCIO = '@P.N.';
408fb4e87cSGreg Roach
41852ede8cSGreg Roach    protected const ROUTE_NAME = IndividualPage::class;
42a25f0a04SGreg Roach
4376692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
4476692c8bSGreg Roach    public $generation;
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
474686330aSGreg Roach    private $estimated_birth_date;
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach    /** @var Date The estimated date of death */
504686330aSGreg Roach    private $estimated_death_date;
51a25f0a04SGreg Roach
52a25f0a04SGreg Roach    /**
53886b77daSGreg Roach     * A closure which will create a record from a database row.
54886b77daSGreg Roach     *
55bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
56bb03c9f0SGreg Roach     *
57d5ad3db0SGreg Roach     * @param Tree $tree
58d5ad3db0SGreg Roach     *
59886b77daSGreg Roach     * @return Closure
60886b77daSGreg Roach     */
61d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
62886b77daSGreg Roach    {
636b9cb339SGreg Roach        return Registry::individualFactory()->mapper($tree);
64886b77daSGreg Roach    }
65886b77daSGreg Roach
66886b77daSGreg Roach    /**
67c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
68c156e8f5SGreg Roach     *
69c156e8f5SGreg Roach     * @return Closure
70c156e8f5SGreg Roach     */
71c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
72c156e8f5SGreg Roach    {
736c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
74c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
75c156e8f5SGreg Roach        };
76c156e8f5SGreg Roach    }
77c156e8f5SGreg Roach
78c156e8f5SGreg Roach    /**
79c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
80c156e8f5SGreg Roach     *
81c156e8f5SGreg Roach     * @return Closure
82c156e8f5SGreg Roach     */
83c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
84c156e8f5SGreg Roach    {
856c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
8610e21aa9SGreg Roach            return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
87c156e8f5SGreg Roach        };
88c156e8f5SGreg Roach    }
89c156e8f5SGreg Roach
90c156e8f5SGreg Roach    /**
91e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
92e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
93e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
94e71ef9d2SGreg Roach     *
95bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
96bb03c9f0SGreg Roach     *
97e71ef9d2SGreg Roach     * @param string      $xref
98e71ef9d2SGreg Roach     * @param Tree        $tree
99e71ef9d2SGreg Roach     * @param string|null $gedcom
100e71ef9d2SGreg Roach     *
101e71ef9d2SGreg Roach     * @return Individual|null
102e71ef9d2SGreg Roach     */
103ffc0a61fSGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Individual
104c1010edaSGreg Roach    {
1056b9cb339SGreg Roach        return Registry::individualFactory()->make($xref, $tree, $gedcom);
106e71ef9d2SGreg Roach    }
107e71ef9d2SGreg Roach
108e71ef9d2SGreg Roach    /**
109395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
110395f0fe0SGreg Roach     * Typically when we load families and their members.
111395f0fe0SGreg Roach     *
112395f0fe0SGreg Roach     * @param Tree     $tree
1134a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
11418d7a90dSGreg Roach     *
11518d7a90dSGreg Roach     * @return void
116395f0fe0SGreg Roach     */
1172e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
118c1010edaSGreg Roach    {
1192e5b4452SGreg Roach        $rows = DB::table('individuals')
1202e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1212e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1222e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1232e5b4452SGreg Roach            ->get();
124395f0fe0SGreg Roach
125395f0fe0SGreg Roach        foreach ($rows as $row) {
1266b9cb339SGreg Roach            Registry::individualFactory()->make($row->xref, $tree, $row->gedcom);
127395f0fe0SGreg Roach        }
128395f0fe0SGreg Roach    }
129395f0fe0SGreg Roach
130395f0fe0SGreg Roach    /**
131a25f0a04SGreg Roach     * Can the name of this record be shown?
132a25f0a04SGreg Roach     *
13376692c8bSGreg Roach     * @param int|null $access_level
13476692c8bSGreg Roach     *
13576692c8bSGreg Roach     * @return bool
136a25f0a04SGreg Roach     */
13735584196SGreg Roach    public function canShowName(int $access_level = null): bool
138c1010edaSGreg Roach    {
139d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
1404b9ff166SGreg Roach
141518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
142a25f0a04SGreg Roach    }
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach    /**
14576692c8bSGreg Roach     * Can this individual be shown?
146a25f0a04SGreg Roach     *
14776692c8bSGreg Roach     * @param int $access_level
14876692c8bSGreg Roach     *
14976692c8bSGreg Roach     * @return bool
150a25f0a04SGreg Roach     */
15135584196SGreg Roach    protected function canShowByType(int $access_level): bool
152c1010edaSGreg Roach    {
153a25f0a04SGreg Roach        // Dead people...
154518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
155a25f0a04SGreg Roach            $keep_alive             = false;
15654ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
157a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1588d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
159a25f0a04SGreg Roach                foreach ($matches as $match) {
160a25f0a04SGreg Roach                    $date = new Date($match[1]);
161a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
162a25f0a04SGreg Roach                        $keep_alive = true;
163a25f0a04SGreg Roach                        break;
164a25f0a04SGreg Roach                    }
165a25f0a04SGreg Roach                }
166a25f0a04SGreg Roach            }
16754ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
168a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1698d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
170a25f0a04SGreg Roach                foreach ($matches as $match) {
171a25f0a04SGreg Roach                    $date = new Date($match[1]);
172a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
173a25f0a04SGreg Roach                        $keep_alive = true;
174a25f0a04SGreg Roach                        break;
175a25f0a04SGreg Roach                    }
176a25f0a04SGreg Roach                }
177a25f0a04SGreg Roach            }
178a25f0a04SGreg Roach            if (!$keep_alive) {
179a25f0a04SGreg Roach                return true;
180a25f0a04SGreg Roach            }
181a25f0a04SGreg Roach        }
182a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
1837c4add84SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_PATH_LENGTH);
1847c4add84SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF);
1857c4add84SGreg Roach
18654ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1874b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
188a25f0a04SGreg Roach        }
189a25f0a04SGreg Roach
190a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1914b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
192a25f0a04SGreg Roach    }
193a25f0a04SGreg Roach
194a25f0a04SGreg Roach    /**
195a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
196a25f0a04SGreg Roach     *
197a25f0a04SGreg Roach     * @param Individual $target
198cbc1590aSGreg Roach     * @param int        $distance
199a25f0a04SGreg Roach     *
200cbc1590aSGreg Roach     * @return bool
201a25f0a04SGreg Roach     */
2028f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
203c1010edaSGreg Roach    {
204a25f0a04SGreg Roach        static $cache = null;
205a25f0a04SGreg Roach
2066b9cb339SGreg Roach        $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $target->tree);
207a25f0a04SGreg Roach        if ($user_individual) {
208a25f0a04SGreg Roach            if (!$cache) {
20913abd6f3SGreg Roach                $cache = [
21013abd6f3SGreg Roach                    0 => [$user_individual],
21113abd6f3SGreg Roach                    1 => [],
21213abd6f3SGreg Roach                ];
2138d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
214dc124885SGreg Roach                    $family = $fact->target();
215e24444eeSGreg Roach                    if ($family instanceof Family) {
216a25f0a04SGreg Roach                        $cache[1][] = $family;
217a25f0a04SGreg Roach                    }
218a25f0a04SGreg Roach                }
219a25f0a04SGreg Roach            }
220a25f0a04SGreg Roach        } else {
221a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
222a25f0a04SGreg Roach            return true;
223a25f0a04SGreg Roach        }
224a25f0a04SGreg Roach
225a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
226a25f0a04SGreg Roach        $distance *= 2;
227a25f0a04SGreg Roach
228a25f0a04SGreg Roach        // Consider each path length in turn
229a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
230a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
231a25f0a04SGreg Roach                // We have already calculated all records with this length
232e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
233a25f0a04SGreg Roach                    return true;
234a25f0a04SGreg Roach                }
235a25f0a04SGreg Roach            } else {
236a25f0a04SGreg Roach                // Need to calculate these paths
23713abd6f3SGreg Roach                $cache[$n] = [];
238e364afe4SGreg Roach                if ($n % 2 === 0) {
239a25f0a04SGreg Roach                    // Add FAM->INDI links
240a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2418d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
242dc124885SGreg Roach                            $individual = $fact->target();
243a25f0a04SGreg Roach                            // Don’t backtrack
244e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
245a25f0a04SGreg Roach                                $cache[$n][] = $individual;
246a25f0a04SGreg Roach                            }
247a25f0a04SGreg Roach                        }
248a25f0a04SGreg Roach                    }
249a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
250a25f0a04SGreg Roach                        return true;
251a25f0a04SGreg Roach                    }
252a25f0a04SGreg Roach                } else {
253a25f0a04SGreg Roach                    // Add INDI->FAM links
254a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2558d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
256dc124885SGreg Roach                            $family = $fact->target();
257a25f0a04SGreg Roach                            // Don’t backtrack
258e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
259a25f0a04SGreg Roach                                $cache[$n][] = $family;
260a25f0a04SGreg Roach                            }
261a25f0a04SGreg Roach                        }
262a25f0a04SGreg Roach                    }
263a25f0a04SGreg Roach                }
264a25f0a04SGreg Roach            }
265a25f0a04SGreg Roach        }
266a25f0a04SGreg Roach
267a25f0a04SGreg Roach        return false;
268a25f0a04SGreg Roach    }
269a25f0a04SGreg Roach
27076692c8bSGreg Roach    /**
27176692c8bSGreg Roach     * Generate a private version of this record
27276692c8bSGreg Roach     *
27376692c8bSGreg Roach     * @param int $access_level
27476692c8bSGreg Roach     *
27576692c8bSGreg Roach     * @return string
27676692c8bSGreg Roach     */
2773c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
278c1010edaSGreg Roach    {
279acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
280a25f0a04SGreg Roach
281a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
282518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
283a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2848d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
285138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
286a25f0a04SGreg Roach            }
287a25f0a04SGreg Roach        }
288a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2898d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
290a25f0a04SGreg Roach        foreach ($matches as $match) {
2916b9cb339SGreg Roach            $rela = Registry::familyFactory()->make($match[1], $this->tree);
292a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
293a25f0a04SGreg Roach                $rec .= $match[0];
294a25f0a04SGreg Roach            }
295a25f0a04SGreg Roach        }
296a25f0a04SGreg Roach        // Don’t privatize sex.
297a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
298a25f0a04SGreg Roach            $rec .= $match[0];
299a25f0a04SGreg Roach        }
300a25f0a04SGreg Roach
301a25f0a04SGreg Roach        return $rec;
302a25f0a04SGreg Roach    }
303a25f0a04SGreg Roach
30476692c8bSGreg Roach    /**
305a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
306a25f0a04SGreg Roach     * If not known to be dead, then assume living.
307a25f0a04SGreg Roach     *
308cbc1590aSGreg Roach     * @return bool
309a25f0a04SGreg Roach     */
3108f53f488SRico Sonntag    public function isDead(): bool
311c1010edaSGreg Roach    {
312c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3134459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
314a25f0a04SGreg Roach
315a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3168d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
317a25f0a04SGreg Roach            return true;
318a25f0a04SGreg Roach        }
319a25f0a04SGreg Roach
320a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
321a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
322a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
323a25f0a04SGreg Roach                $date = new Date($date_match);
324269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
325a25f0a04SGreg Roach                    return true;
326a25f0a04SGreg Roach                }
327a25f0a04SGreg Roach            }
328a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
329a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
330a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
331a25f0a04SGreg Roach                return false;
332a25f0a04SGreg Roach            }
333a25f0a04SGreg Roach        }
334a25f0a04SGreg Roach
335a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
336a25f0a04SGreg Roach
337a25f0a04SGreg Roach        // Check parents (birth and adopted)
33839ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
33939ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
340a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
341a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
342a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
343a25f0a04SGreg Roach                    $date = new Date($date_match);
344269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
345a25f0a04SGreg Roach                        return true;
346a25f0a04SGreg Roach                    }
347a25f0a04SGreg Roach                }
348a25f0a04SGreg Roach            }
349a25f0a04SGreg Roach        }
350a25f0a04SGreg Roach
351a25f0a04SGreg Roach        // Check spouses
35239ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
353a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
354a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
355a25f0a04SGreg Roach                $date = new Date($date_match);
356a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
357269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
358a25f0a04SGreg Roach                    return true;
359a25f0a04SGreg Roach                }
360a25f0a04SGreg Roach            }
361a25f0a04SGreg Roach            // Check spouse dates
36239ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
363a25f0a04SGreg Roach            if ($spouse) {
364a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
365a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
366a25f0a04SGreg Roach                    $date = new Date($date_match);
367a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
368269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
369a25f0a04SGreg Roach                        return true;
370a25f0a04SGreg Roach                    }
371a25f0a04SGreg Roach                }
372a25f0a04SGreg Roach            }
373a25f0a04SGreg Roach            // Check child dates
37439ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
375a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
376a25f0a04SGreg Roach                // Assume children born after age of 15
377a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
378a25f0a04SGreg Roach                    $date = new Date($date_match);
379269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
380a25f0a04SGreg Roach                        return true;
381a25f0a04SGreg Roach                    }
382a25f0a04SGreg Roach                }
383a25f0a04SGreg Roach                // Check grandchildren
38439ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
38539ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
386a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
387a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
388a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
389a25f0a04SGreg Roach                            $date = new Date($date_match);
390269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
391a25f0a04SGreg Roach                                return true;
392a25f0a04SGreg Roach                            }
393a25f0a04SGreg Roach                        }
394a25f0a04SGreg Roach                    }
395a25f0a04SGreg Roach                }
396a25f0a04SGreg Roach            }
397a25f0a04SGreg Roach        }
398a25f0a04SGreg Roach
399a25f0a04SGreg Roach        return false;
400a25f0a04SGreg Roach    }
401a25f0a04SGreg Roach
402a25f0a04SGreg Roach    /**
403a25f0a04SGreg Roach     * Find the highlighted media object for an individual
404a25f0a04SGreg Roach     *
405e364afe4SGreg Roach     * @return MediaFile|null
406a25f0a04SGreg Roach     */
407e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
408c1010edaSGreg Roach    {
40992647683SGreg Roach        $fact = $this->facts(['OBJE'])
41019d319e7SGreg Roach            ->first(static function (Fact $fact): bool {
411dc124885SGreg Roach                $media = $fact->target();
41219d319e7SGreg Roach
41319d319e7SGreg Roach                return $media instanceof Media && $media->firstImageFile() instanceof MediaFile;
41419d319e7SGreg Roach            });
41519d319e7SGreg Roach
41692647683SGreg Roach        if ($fact instanceof Fact && $fact->target() instanceof Media) {
41792647683SGreg Roach            return $fact->target()->firstImageFile();
418a25f0a04SGreg Roach        }
419a25f0a04SGreg Roach
420a25f0a04SGreg Roach        return null;
421a25f0a04SGreg Roach    }
422a25f0a04SGreg Roach
423a25f0a04SGreg Roach    /**
424a25f0a04SGreg Roach     * Display the prefered image for this individual.
425a25f0a04SGreg Roach     * Use an icon if no image is available.
426a25f0a04SGreg Roach     *
427dce07401SGreg Roach     * @param int      $width      Pixels
428dce07401SGreg Roach     * @param int      $height     Pixels
429dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
430dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
431dce07401SGreg Roach     *
432a25f0a04SGreg Roach     * @return string
433a25f0a04SGreg Roach     */
4348f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
435c1010edaSGreg Roach    {
4364a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4374a9f750fSGreg Roach
4384a9f750fSGreg Roach        if ($media_file !== null) {
4394a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
440a25f0a04SGreg Roach        }
4414a9f750fSGreg Roach
4424a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
44339ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4444a9f750fSGreg Roach        }
4454a9f750fSGreg Roach
4464a9f750fSGreg Roach        return '';
447a25f0a04SGreg Roach    }
448a25f0a04SGreg Roach
449a25f0a04SGreg Roach    /**
450a25f0a04SGreg Roach     * Get the date of birth
451a25f0a04SGreg Roach     *
452a25f0a04SGreg Roach     * @return Date
453a25f0a04SGreg Roach     */
4548f53f488SRico Sonntag    public function getBirthDate(): Date
455c1010edaSGreg Roach    {
456a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
457a25f0a04SGreg Roach            if ($date->isOK()) {
458a25f0a04SGreg Roach                return $date;
459a25f0a04SGreg Roach            }
460a25f0a04SGreg Roach        }
461a25f0a04SGreg Roach
462a25f0a04SGreg Roach        return new Date('');
463a25f0a04SGreg Roach    }
464a25f0a04SGreg Roach
465a25f0a04SGreg Roach    /**
466a25f0a04SGreg Roach     * Get the place of birth
467a25f0a04SGreg Roach     *
46816d0b7f7SRico Sonntag     * @return Place
469a25f0a04SGreg Roach     */
4708f53f488SRico Sonntag    public function getBirthPlace(): Place
471c1010edaSGreg Roach    {
472a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
473a25f0a04SGreg Roach            return $place;
474a25f0a04SGreg Roach        }
475a25f0a04SGreg Roach
476b20ddbf9SGreg Roach        return new Place('', $this->tree);
477a25f0a04SGreg Roach    }
478a25f0a04SGreg Roach
479a25f0a04SGreg Roach    /**
480a25f0a04SGreg Roach     * Get the year of birth
481a25f0a04SGreg Roach     *
482a25f0a04SGreg Roach     * @return string the year of birth
483a25f0a04SGreg Roach     */
4848f53f488SRico Sonntag    public function getBirthYear(): string
485c1010edaSGreg Roach    {
486f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
487a25f0a04SGreg Roach    }
488a25f0a04SGreg Roach
489a25f0a04SGreg Roach    /**
490a25f0a04SGreg Roach     * Get the date of death
491a25f0a04SGreg Roach     *
492a25f0a04SGreg Roach     * @return Date
493a25f0a04SGreg Roach     */
4948f53f488SRico Sonntag    public function getDeathDate(): Date
495c1010edaSGreg Roach    {
496a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
497a25f0a04SGreg Roach            if ($date->isOK()) {
498a25f0a04SGreg Roach                return $date;
499a25f0a04SGreg Roach            }
500a25f0a04SGreg Roach        }
501a25f0a04SGreg Roach
502a25f0a04SGreg Roach        return new Date('');
503a25f0a04SGreg Roach    }
504a25f0a04SGreg Roach
505a25f0a04SGreg Roach    /**
506a25f0a04SGreg Roach     * Get the place of death
507a25f0a04SGreg Roach     *
50816d0b7f7SRico Sonntag     * @return Place
509a25f0a04SGreg Roach     */
5108f53f488SRico Sonntag    public function getDeathPlace(): Place
511c1010edaSGreg Roach    {
512a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
513a25f0a04SGreg Roach            return $place;
514a25f0a04SGreg Roach        }
515a25f0a04SGreg Roach
516b20ddbf9SGreg Roach        return new Place('', $this->tree);
517a25f0a04SGreg Roach    }
518a25f0a04SGreg Roach
519a25f0a04SGreg Roach    /**
520a25f0a04SGreg Roach     * get the death year
521a25f0a04SGreg Roach     *
522a25f0a04SGreg Roach     * @return string the year of death
523a25f0a04SGreg Roach     */
5248f53f488SRico Sonntag    public function getDeathYear(): string
525c1010edaSGreg Roach    {
526f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
527a25f0a04SGreg Roach    }
528a25f0a04SGreg Roach
529a25f0a04SGreg Roach    /**
530a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
53115d603e7SGreg Roach     * Provide the place and full date using a tooltip.
532a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
533a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
534a25f0a04SGreg Roach     *
535a25f0a04SGreg Roach     * @return string
536a25f0a04SGreg Roach     */
5375e6816beSGreg Roach    public function lifespan(): string
538c1010edaSGreg Roach    {
53915d603e7SGreg Roach        // Just the first part of the place name
540392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
541392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
54215d603e7SGreg Roach        // Remove markup from dates
54315d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
54415d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
54515d603e7SGreg Roach
546c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
547dacfe33cSGreg Roach        return I18N::translate(
548a25f0a04SGreg Roach            '%1$s–%2$s',
5492d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5502d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
551a25f0a04SGreg Roach        );
552a25f0a04SGreg Roach    }
553a25f0a04SGreg Roach
554a25f0a04SGreg Roach    /**
555a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
556a25f0a04SGreg Roach     *
557a25f0a04SGreg Roach     * @return Date[]
558a25f0a04SGreg Roach     */
5598f53f488SRico Sonntag    public function getAllBirthDates(): array
560c1010edaSGreg Roach    {
5618d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5628d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
563a25f0a04SGreg Roach            if ($tmp) {
564a25f0a04SGreg Roach                return $tmp;
565a25f0a04SGreg Roach            }
566a25f0a04SGreg Roach        }
567a25f0a04SGreg Roach
56813abd6f3SGreg Roach        return [];
569a25f0a04SGreg Roach    }
570a25f0a04SGreg Roach
571a25f0a04SGreg Roach    /**
572a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
573a25f0a04SGreg Roach     *
5744080d558SGreg Roach     * @return Place[]
575a25f0a04SGreg Roach     */
5768f53f488SRico Sonntag    public function getAllBirthPlaces(): array
577c1010edaSGreg Roach    {
5788d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5798d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
58054c1ab5eSGreg Roach            if ($places !== []) {
5814080d558SGreg Roach                return $places;
582a25f0a04SGreg Roach            }
583a25f0a04SGreg Roach        }
584a25f0a04SGreg Roach
58513abd6f3SGreg Roach        return [];
586a25f0a04SGreg Roach    }
587a25f0a04SGreg Roach
588a25f0a04SGreg Roach    /**
589a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
590a25f0a04SGreg Roach     *
591a25f0a04SGreg Roach     * @return Date[]
592a25f0a04SGreg Roach     */
5938f53f488SRico Sonntag    public function getAllDeathDates(): array
594c1010edaSGreg Roach    {
5958d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
5968d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
597a25f0a04SGreg Roach            if ($tmp) {
598a25f0a04SGreg Roach                return $tmp;
599a25f0a04SGreg Roach            }
600a25f0a04SGreg Roach        }
601a25f0a04SGreg Roach
60213abd6f3SGreg Roach        return [];
603a25f0a04SGreg Roach    }
604a25f0a04SGreg Roach
605a25f0a04SGreg Roach    /**
606a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
607a25f0a04SGreg Roach     *
6084080d558SGreg Roach     * @return Place[]
609a25f0a04SGreg Roach     */
6108f53f488SRico Sonntag    public function getAllDeathPlaces(): array
611c1010edaSGreg Roach    {
6128d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6138d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
61454c1ab5eSGreg Roach            if ($places !== []) {
6154080d558SGreg Roach                return $places;
616a25f0a04SGreg Roach            }
617a25f0a04SGreg Roach        }
618a25f0a04SGreg Roach
61913abd6f3SGreg Roach        return [];
620a25f0a04SGreg Roach    }
621a25f0a04SGreg Roach
622a25f0a04SGreg Roach    /**
623a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
624a25f0a04SGreg Roach     *
625a25f0a04SGreg Roach     * @return Date
626a25f0a04SGreg Roach     */
6278f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
628c1010edaSGreg Roach    {
6298f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
630a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
631a25f0a04SGreg Roach                if ($date->isOK()) {
6324686330aSGreg Roach                    $this->estimated_birth_date = $date;
633a25f0a04SGreg Roach                    break;
634a25f0a04SGreg Roach                }
635a25f0a04SGreg Roach            }
6368f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
63713abd6f3SGreg Roach                $min = [];
63813abd6f3SGreg Roach                $max = [];
639a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
640f5b60decSGreg Roach                if ($tmp->isOK()) {
641f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
642f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
643a25f0a04SGreg Roach                }
64439ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
645a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
646f5b60decSGreg Roach                    if ($tmp->isOK()) {
647f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
648f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
649a25f0a04SGreg Roach                    }
65039ca88baSGreg Roach                    $husband = $family->husband();
651e364afe4SGreg Roach                    if ($husband instanceof self) {
6522e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
653f5b60decSGreg Roach                        if ($tmp->isOK()) {
654f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
655f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
656a25f0a04SGreg Roach                        }
657a25f0a04SGreg Roach                    }
65839ca88baSGreg Roach                    $wife = $family->wife();
659e364afe4SGreg Roach                    if ($wife instanceof self) {
6602e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
661f5b60decSGreg Roach                        if ($tmp->isOK()) {
662f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
663f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
664a25f0a04SGreg Roach                        }
665a25f0a04SGreg Roach                    }
66639ca88baSGreg Roach                    foreach ($family->children() as $child) {
667a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
668f5b60decSGreg Roach                        if ($tmp->isOK()) {
669f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
670f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
671a25f0a04SGreg Roach                        }
672a25f0a04SGreg Roach                    }
673a25f0a04SGreg Roach                }
67439ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
675a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
676f5b60decSGreg Roach                    if ($tmp->isOK()) {
677f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
678f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
679a25f0a04SGreg Roach                    }
68039ca88baSGreg Roach                    $spouse = $family->spouse($this);
681a25f0a04SGreg Roach                    if ($spouse) {
682a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
683f5b60decSGreg Roach                        if ($tmp->isOK()) {
684f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
685f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
686a25f0a04SGreg Roach                        }
687a25f0a04SGreg Roach                    }
68839ca88baSGreg Roach                    foreach ($family->children() as $child) {
689a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
690f5b60decSGreg Roach                        if ($tmp->isOK()) {
691e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
692f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
693a25f0a04SGreg Roach                        }
694a25f0a04SGreg Roach                    }
695a25f0a04SGreg Roach                }
696a25f0a04SGreg Roach                if ($min && $max) {
69759f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
698a25f0a04SGreg Roach
69965e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7004686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
701a25f0a04SGreg Roach                } else {
7024686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
703a25f0a04SGreg Roach                }
704a25f0a04SGreg Roach            }
705a25f0a04SGreg Roach        }
706a25f0a04SGreg Roach
7074686330aSGreg Roach        return $this->estimated_birth_date;
708a25f0a04SGreg Roach    }
709a25f0a04SGreg Roach
710a25f0a04SGreg Roach    /**
711a25f0a04SGreg Roach     * Generate an estimated date of death.
712a25f0a04SGreg Roach     *
713a25f0a04SGreg Roach     * @return Date
714a25f0a04SGreg Roach     */
7158f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
716c1010edaSGreg Roach    {
7174686330aSGreg Roach        if ($this->estimated_death_date === null) {
718a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
719a25f0a04SGreg Roach                if ($date->isOK()) {
7204686330aSGreg Roach                    $this->estimated_death_date = $date;
721a25f0a04SGreg Roach                    break;
722a25f0a04SGreg Roach                }
723a25f0a04SGreg Roach            }
7244686330aSGreg Roach            if ($this->estimated_death_date === null) {
725f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
726c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7274686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
728a25f0a04SGreg Roach                } else {
7294686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
730a25f0a04SGreg Roach                }
731a25f0a04SGreg Roach            }
732a25f0a04SGreg Roach        }
733a25f0a04SGreg Roach
7344686330aSGreg Roach        return $this->estimated_death_date;
735a25f0a04SGreg Roach    }
736a25f0a04SGreg Roach
737a25f0a04SGreg Roach    /**
738a25f0a04SGreg Roach     * Get the sex - M F or U
739a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
740a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
741a25f0a04SGreg Roach     *
742a25f0a04SGreg Roach     * @return string
743a25f0a04SGreg Roach     */
744e364afe4SGreg Roach    public function sex(): string
745c1010edaSGreg Roach    {
746a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
747a25f0a04SGreg Roach            return $match[1];
748a25f0a04SGreg Roach        }
749b2ce94c6SRico Sonntag
750b2ce94c6SRico Sonntag        return 'U';
751a25f0a04SGreg Roach    }
752a25f0a04SGreg Roach
753a25f0a04SGreg Roach    /**
754a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
755a25f0a04SGreg Roach     *
756a25f0a04SGreg Roach     * @return string
757a25f0a04SGreg Roach     */
7588f53f488SRico Sonntag    public function getBoxStyle(): string
759c1010edaSGreg Roach    {
760c1010edaSGreg Roach        $tmp = [
761c1010edaSGreg Roach            'M' => '',
762c1010edaSGreg Roach            'F' => 'F',
763c1010edaSGreg Roach            'U' => 'NN',
764c1010edaSGreg Roach        ];
765a25f0a04SGreg Roach
76639ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
767a25f0a04SGreg Roach    }
768a25f0a04SGreg Roach
769a25f0a04SGreg Roach    /**
770a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
771a25f0a04SGreg Roach     *
772cbc1590aSGreg Roach     * @param int|null $access_level
773a25f0a04SGreg Roach     *
774b5c8fd7eSGreg Roach     * @return Collection<Family>
775a25f0a04SGreg Roach     */
77639ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
777c1010edaSGreg Roach    {
778d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
779d9e083e7SGreg Roach
780d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
781d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
7824b9ff166SGreg Roach        }
7834b9ff166SGreg Roach
78439ca88baSGreg Roach        $families = new Collection();
785d9e083e7SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level) as $fact) {
786dc124885SGreg Roach            $family = $fact->target();
787d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
78839ca88baSGreg Roach                $families->push($family);
789a25f0a04SGreg Roach            }
790a25f0a04SGreg Roach        }
791a25f0a04SGreg Roach
79239ca88baSGreg Roach        return new Collection($families);
793a25f0a04SGreg Roach    }
794a25f0a04SGreg Roach
795a25f0a04SGreg Roach    /**
796a25f0a04SGreg Roach     * Get the current spouse of this individual.
797a25f0a04SGreg Roach     *
798a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
799a25f0a04SGreg Roach     * in chronological order, and take the last one found.
800a25f0a04SGreg Roach     *
801a25f0a04SGreg Roach     * @return Individual|null
802a25f0a04SGreg Roach     */
803e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
804c1010edaSGreg Roach    {
80539ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
80639ca88baSGreg Roach
80739ca88baSGreg Roach        if ($family instanceof Family) {
80839ca88baSGreg Roach            return $family->spouse($this);
809a25f0a04SGreg Roach        }
810b2ce94c6SRico Sonntag
811b2ce94c6SRico Sonntag        return null;
812a25f0a04SGreg Roach    }
813a25f0a04SGreg Roach
814a25f0a04SGreg Roach    /**
815a25f0a04SGreg Roach     * Count the children belonging to this individual.
816a25f0a04SGreg Roach     *
817cbc1590aSGreg Roach     * @return int
818a25f0a04SGreg Roach     */
819e364afe4SGreg Roach    public function numberOfChildren(): int
820c1010edaSGreg Roach    {
8217d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8223dc7bbe9SGreg Roach            return (int) $match[1];
823b2ce94c6SRico Sonntag        }
824b2ce94c6SRico Sonntag
82513abd6f3SGreg Roach        $children = [];
82639ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
82739ca88baSGreg Roach            foreach ($fam->children() as $child) {
828c0935879SGreg Roach                $children[$child->xref()] = true;
829a25f0a04SGreg Roach            }
830a25f0a04SGreg Roach        }
831a25f0a04SGreg Roach
832a25f0a04SGreg Roach        return count($children);
833a25f0a04SGreg Roach    }
834a25f0a04SGreg Roach
835a25f0a04SGreg Roach    /**
836a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
837a25f0a04SGreg Roach     *
838cbc1590aSGreg Roach     * @param int|null $access_level
839a25f0a04SGreg Roach     *
840b5c8fd7eSGreg Roach     * @return Collection<Family>
841a25f0a04SGreg Roach     */
84239ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
843c1010edaSGreg Roach    {
844d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
8454b9ff166SGreg Roach
846d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
847d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
848d9e083e7SGreg Roach        }
849a25f0a04SGreg Roach
85039ca88baSGreg Roach        $families = new Collection();
85139ca88baSGreg Roach
852d9e083e7SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level) as $fact) {
853dc124885SGreg Roach            $family = $fact->target();
854d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
85539ca88baSGreg Roach                $families->push($family);
856a25f0a04SGreg Roach            }
857a25f0a04SGreg Roach        }
858a25f0a04SGreg Roach
859a25f0a04SGreg Roach        return $families;
860a25f0a04SGreg Roach    }
861a25f0a04SGreg Roach
862a25f0a04SGreg Roach    /**
863a25f0a04SGreg Roach     * Get a list of step-parent families.
864a25f0a04SGreg Roach     *
865b5c8fd7eSGreg Roach     * @return Collection<Family>
866a25f0a04SGreg Roach     */
867820b62dfSGreg Roach    public function childStepFamilies(): Collection
868c1010edaSGreg Roach    {
869ed5b6227SGreg Roach        $step_families = new Collection();
87039ca88baSGreg Roach        $families      = $this->childFamilies();
871a25f0a04SGreg Roach        foreach ($families as $family) {
872ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
873ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
87439ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
875ed5b6227SGreg Roach                        $step_families->add($step_family);
876a25f0a04SGreg Roach                    }
877a25f0a04SGreg Roach                }
878a25f0a04SGreg Roach            }
879a25f0a04SGreg Roach        }
880a25f0a04SGreg Roach
8818c627a69SGreg Roach        return $step_families->uniqueStrict(static function (Family $family): string {
8828c627a69SGreg Roach            return $family->xref();
8838c627a69SGreg Roach        });
884a25f0a04SGreg Roach    }
885a25f0a04SGreg Roach
886a25f0a04SGreg Roach    /**
887a25f0a04SGreg Roach     * Get a list of step-parent families.
888a25f0a04SGreg Roach     *
889b5c8fd7eSGreg Roach     * @return Collection<Family>
890a25f0a04SGreg Roach     */
891820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
892c1010edaSGreg Roach    {
89313abd6f3SGreg Roach        $step_families = [];
89439ca88baSGreg Roach        $families      = $this->spouseFamilies();
895820b62dfSGreg Roach
896a25f0a04SGreg Roach        foreach ($families as $family) {
89739ca88baSGreg Roach            $spouse = $family->spouse($this);
898820b62dfSGreg Roach
899d823340dSGreg Roach            if ($spouse instanceof self) {
90039ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
90139ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
902a25f0a04SGreg Roach                        $step_families[] = $step_family;
903a25f0a04SGreg Roach                    }
904a25f0a04SGreg Roach                }
905a25f0a04SGreg Roach            }
906a25f0a04SGreg Roach        }
907a25f0a04SGreg Roach
908820b62dfSGreg Roach        return new Collection($step_families);
909a25f0a04SGreg Roach    }
910a25f0a04SGreg Roach
911a25f0a04SGreg Roach    /**
912a25f0a04SGreg Roach     * A label for a parental family group
913a25f0a04SGreg Roach     *
914a25f0a04SGreg Roach     * @param Family $family
915a25f0a04SGreg Roach     *
916a25f0a04SGreg Roach     * @return string
917a25f0a04SGreg Roach     */
918820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
919c1010edaSGreg Roach    {
9207d70e4a7SGreg Roach        preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match);
921b2ce94c6SRico Sonntag
9227d70e4a7SGreg Roach        $values = [
9237d70e4a7SGreg Roach            'birth'   => I18N::translate('Family with parents'),
9247d70e4a7SGreg Roach            'adopted' => I18N::translate('Family with adoptive parents'),
9257d70e4a7SGreg Roach            'foster'  => I18N::translate('Family with foster parents'),
9267d70e4a7SGreg Roach            'sealing' => /* I18N: “sealing” is a Mormon ceremony. */
9277d70e4a7SGreg Roach                I18N::translate('Family with sealing parents'),
9287d70e4a7SGreg Roach            'rada'    => /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */
9297d70e4a7SGreg Roach                I18N::translate('Family with rada parents'),
9307d70e4a7SGreg Roach        ];
9317d70e4a7SGreg Roach
9327d70e4a7SGreg Roach        return $values[$match[1] ?? 'birth'] ?? $values['birth'];
933a25f0a04SGreg Roach    }
934a25f0a04SGreg Roach
935a25f0a04SGreg Roach    /**
936a25f0a04SGreg Roach     * Create a label for a step family
937a25f0a04SGreg Roach     *
938a25f0a04SGreg Roach     * @param Family $step_family
939a25f0a04SGreg Roach     *
940a25f0a04SGreg Roach     * @return string
941a25f0a04SGreg Roach     */
9428f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
943c1010edaSGreg Roach    {
94439ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
945a25f0a04SGreg Roach            if ($family !== $step_family) {
946a25f0a04SGreg Roach                // Must be a step-family
94739ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
94839ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
949a25f0a04SGreg Roach                        if ($parent === $step_parent) {
950a25f0a04SGreg Roach                            // One common parent - must be a step family
951e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
952a25f0a04SGreg Roach                                // Father’s family with someone else
95339ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
954a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
95539ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
956b2ce94c6SRico Sonntag                                }
957b2ce94c6SRico Sonntag
958a25f0a04SGreg Roach                                /* I18N: A step-family. */
959bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
960a25f0a04SGreg Roach                            }
961b2ce94c6SRico Sonntag
962a25f0a04SGreg Roach                            // Mother’s family with someone else
96339ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
964a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
96539ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
966b2ce94c6SRico Sonntag                            }
967b2ce94c6SRico Sonntag
968a25f0a04SGreg Roach                            /* I18N: A step-family. */
969bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
970a25f0a04SGreg Roach                        }
971a25f0a04SGreg Roach                    }
972a25f0a04SGreg Roach                }
973a25f0a04SGreg Roach            }
974a25f0a04SGreg Roach        }
975a25f0a04SGreg Roach
976a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
977a25f0a04SGreg Roach        return I18N::translate('Family with parents');
978a25f0a04SGreg Roach    }
979a25f0a04SGreg Roach
980225e381fSGreg Roach    /**
981225e381fSGreg Roach     * Get the description for the family.
982225e381fSGreg Roach     *
983225e381fSGreg Roach     * For example, "XXX's family with new wife".
984225e381fSGreg Roach     *
985225e381fSGreg Roach     * @param Family $family
986225e381fSGreg Roach     *
987225e381fSGreg Roach     * @return string
988225e381fSGreg Roach     */
989e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
990c1010edaSGreg Roach    {
99139ca88baSGreg Roach        $spouse = $family->spouse($this);
992225e381fSGreg Roach        if ($spouse) {
993225e381fSGreg Roach            /* I18N: %s is the spouse name */
99439ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
995225e381fSGreg Roach        }
996b2ce94c6SRico Sonntag
99739ca88baSGreg Roach        return $family->fullName();
998225e381fSGreg Roach    }
999225e381fSGreg Roach
1000a25f0a04SGreg Roach    /**
1001961ec755SGreg Roach     * If this object has no name, what do we call it?
1002961ec755SGreg Roach     *
1003961ec755SGreg Roach     * @return string
1004961ec755SGreg Roach     */
10058f53f488SRico Sonntag    public function getFallBackName(): string
1006c1010edaSGreg Roach    {
1007a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1008a25f0a04SGreg Roach    }
1009a25f0a04SGreg Roach
1010a25f0a04SGreg Roach    /**
1011a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1012a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1013a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1014a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1015a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1016a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1017a25f0a04SGreg Roach     *
1018a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1019a25f0a04SGreg Roach     * 2 GIVN Robert
1020a25f0a04SGreg Roach     * 2 SPFX de
1021a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1022a25f0a04SGreg Roach     * 2 NICK The Bald
1023a25f0a04SGreg Roach     *
1024a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1025a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1026a25f0a04SGreg Roach     *
1027a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1028a25f0a04SGreg Roach     *
1029a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1030a25f0a04SGreg Roach     * or
1031a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1032a25f0a04SGreg Roach     * 2 GIVN Carlos
1033a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1034a25f0a04SGreg Roach     *
1035a25f0a04SGreg Roach     * @param string $type
1036a25f0a04SGreg Roach     * @param string $full
1037a25f0a04SGreg Roach     * @param string $gedcom
1038e364afe4SGreg Roach     *
1039e364afe4SGreg Roach     * @return void
1040a25f0a04SGreg Roach     */
1041e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1042c1010edaSGreg Roach    {
1043a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1044a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1045a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1046a25f0a04SGreg Roach
104776f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1048a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1049a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1050a25f0a04SGreg Roach
1051a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
105276f666f4SGreg Roach        if ($SURN !== '') {
1053a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1054a25f0a04SGreg Roach        } else {
105513abd6f3SGreg Roach            $SURNS = [];
1056a25f0a04SGreg Roach        }
105776f666f4SGreg Roach
1058a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1059a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1060a25f0a04SGreg Roach
1061a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1062a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1063a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1064a25f0a04SGreg Roach
1065a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
106676f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1067e364afe4SGreg Roach            $full .= '/';
1068a25f0a04SGreg Roach        }
1069a25f0a04SGreg Roach
1070a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1071a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1072a25f0a04SGreg Roach
1073a25f0a04SGreg Roach        // Extract the surname.
1074a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1075a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1076a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1077a25f0a04SGreg Roach        } else {
1078a25f0a04SGreg Roach            $surname = '';
1079a25f0a04SGreg Roach        }
1080a25f0a04SGreg Roach
1081a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1082a25f0a04SGreg Roach        if (!$SURNS) {
1083a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1084a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1085a25f0a04SGreg Roach                $SURNS = $matches[1];
1086a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1087a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1088a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1089a25f0a04SGreg Roach                }
1090a25f0a04SGreg Roach            } else {
1091a25f0a04SGreg Roach                // It is valid not to have a surname at all
109213abd6f3SGreg Roach                $SURNS = [''];
1093a25f0a04SGreg Roach            }
1094a25f0a04SGreg Roach        }
1095a25f0a04SGreg Roach
1096a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1097a25f0a04SGreg Roach        if (!$GIVN) {
1098a25f0a04SGreg Roach            $GIVN = preg_replace(
109913abd6f3SGreg Roach                [
1100c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1101c1010edaSGreg Roach                    // remove surname
1102c1010edaSGreg Roach                    '/ ?".+"/',
1103c1010edaSGreg Roach                    // remove nickname
1104c1010edaSGreg Roach                    '/ {2,}/',
1105c1010edaSGreg Roach                    // multiple spaces, caused by the above
1106c1010edaSGreg Roach                    '/^ | $/',
1107c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
110813abd6f3SGreg Roach                ],
110913abd6f3SGreg Roach                [
1110a25f0a04SGreg Roach                    ' ',
1111a25f0a04SGreg Roach                    ' ',
1112a25f0a04SGreg Roach                    ' ',
1113a25f0a04SGreg Roach                    '',
111413abd6f3SGreg Roach                ],
1115a25f0a04SGreg Roach                $full
1116a25f0a04SGreg Roach            );
1117a25f0a04SGreg Roach        }
1118a25f0a04SGreg Roach
1119a25f0a04SGreg Roach        // Add placeholder for unknown given name
1120a25f0a04SGreg Roach        if (!$GIVN) {
1121d823340dSGreg Roach            $GIVN = self::PRAENOMEN_NESCIO;
112273f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1123a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1124a25f0a04SGreg Roach        }
1125a25f0a04SGreg Roach
1126a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1127a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1128a25f0a04SGreg Roach        // $full is for display on-screen
1129a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1130a25f0a04SGreg Roach
1131a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1132d823340dSGreg Roach        $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full);
1133d823340dSGreg Roach        $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full);
1134c6f196c3SGreg Roach        // Format for display
1135d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1136acc34ea1SGreg Roach        // Localise quotation marks around the nickname
11370b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
1138c652cdbdSGreg Roach            return '<q class="wt-nickname">' . $matches[1] . '</q>';
11398d68cabeSGreg Roach        }, $full);
1140a25f0a04SGreg Roach
1141c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1142a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1143a25f0a04SGreg Roach
1144a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1145a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1146a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1147a25f0a04SGreg Roach
1148ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1149a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1150e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1151a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1152e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1153a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1154a25f0a04SGreg Roach            }
1155a25f0a04SGreg Roach
1156bdb3725aSGreg Roach            $this->getAllNames[] = [
1157a25f0a04SGreg Roach                'type'    => $type,
1158a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1159c1010edaSGreg Roach                'full'    => $full,
1160c1010edaSGreg Roach                // This is used for display
1161c1010edaSGreg Roach                'fullNN'  => $fullNN,
1162c1010edaSGreg Roach                // This goes into the database
1163c1010edaSGreg Roach                'surname' => $surname,
1164c1010edaSGreg Roach                // This goes into the database
1165c1010edaSGreg Roach                'givn'    => $GIVN,
1166c1010edaSGreg Roach                // This goes into the database
1167c1010edaSGreg Roach                'surn'    => $SURN,
1168c1010edaSGreg Roach                // This goes into the database
116913abd6f3SGreg Roach            ];
1170a25f0a04SGreg Roach        }
1171a25f0a04SGreg Roach    }
1172a25f0a04SGreg Roach
1173a25f0a04SGreg Roach    /**
117476692c8bSGreg Roach     * Extract names from the GEDCOM record.
1175c7ff4153SGreg Roach     *
1176c7ff4153SGreg Roach     * @return void
1177a25f0a04SGreg Roach     */
1178e364afe4SGreg Roach    public function extractNames(): void
1179c1010edaSGreg Roach    {
1180d9e083e7SGreg Roach        $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree);
1181d9e083e7SGreg Roach
11828f53f488SRico Sonntag        $this->extractNamesFromFacts(
11838f53f488SRico Sonntag            1,
11848f53f488SRico Sonntag            'NAME',
1185d9e083e7SGreg Roach            $this->facts(['NAME'], false, $access_level)
11868f53f488SRico Sonntag        );
1187a25f0a04SGreg Roach    }
1188a25f0a04SGreg Roach
1189a25f0a04SGreg Roach    /**
1190a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1191a25f0a04SGreg Roach     * selection items or favorites.
1192a25f0a04SGreg Roach     *
1193a25f0a04SGreg Roach     * @return string
1194a25f0a04SGreg Roach     */
11958f53f488SRico Sonntag    public function formatListDetails(): string
1196c1010edaSGreg Roach    {
1197a25f0a04SGreg Roach        return
11988d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
11998d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1200a25f0a04SGreg Roach    }
1201*8091bfd1SGreg Roach
1202*8091bfd1SGreg Roach    /**
1203*8091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
1204*8091bfd1SGreg Roach     */
1205*8091bfd1SGreg Roach    public function lock(): void
1206*8091bfd1SGreg Roach    {
1207*8091bfd1SGreg Roach        DB::table('individuals')
1208*8091bfd1SGreg Roach            ->where('i_file', '=', $this->tree->id())
1209*8091bfd1SGreg Roach            ->where('i_id', '=', $this->xref())
1210*8091bfd1SGreg Roach            ->lockForUpdate()
1211*8091bfd1SGreg Roach            ->get();
1212*8091bfd1SGreg Roach    }
1213a25f0a04SGreg Roach}
1214