xref: /webtrees/app/Individual.php (revision 19d319e7095703d7954023b55f1661a16d1edff3)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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;
236ccdf4f0SGreg Roachuse Exception;
24a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
26852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
272e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2839ca88baSGreg Roachuse Illuminate\Support\Collection;
29886b77daSGreg Roachuse stdClass;
30a25f0a04SGreg 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
38852ede8cSGreg Roach    protected const ROUTE_NAME = IndividualPage::class;
39a25f0a04SGreg Roach
4076692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
4176692c8bSGreg Roach    public $generation;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
444686330aSGreg Roach    private $estimated_birth_date;
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach    /** @var Date The estimated date of death */
474686330aSGreg Roach    private $estimated_death_date;
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach    /**
50886b77daSGreg Roach     * A closure which will create a record from a database row.
51886b77daSGreg Roach     *
52d5ad3db0SGreg Roach     * @param Tree $tree
53d5ad3db0SGreg Roach     *
54886b77daSGreg Roach     * @return Closure
55886b77daSGreg Roach     */
56d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
57886b77daSGreg Roach    {
58d5ad3db0SGreg Roach        return static function (stdClass $row) use ($tree): Individual {
59d5ad3db0SGreg Roach            $individual = Individual::getInstance($row->i_id, $tree, $row->i_gedcom);
60d5ad3db0SGreg Roach            assert($individual instanceof Individual);
61e84cf2deSGreg Roach
62d5ad3db0SGreg Roach            // Some queries include the names table.
63d5ad3db0SGreg Roach            // For these we must select the specified name.
64d5ad3db0SGreg Roach            if (($row->n_num ?? null) !== null) {
65e0458bdcSGreg Roach                $individual = clone $individual;
66e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
67e0458bdcSGreg Roach
68e0458bdcSGreg Roach                return $individual;
69e84cf2deSGreg Roach            }
70e84cf2deSGreg Roach
71e84cf2deSGreg Roach            return $individual;
72886b77daSGreg Roach        };
73886b77daSGreg Roach    }
74886b77daSGreg Roach
75886b77daSGreg Roach    /**
76c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
77c156e8f5SGreg Roach     *
78c156e8f5SGreg Roach     * @return Closure
79c156e8f5SGreg Roach     */
80c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
81c156e8f5SGreg Roach    {
826c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
83c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
84c156e8f5SGreg Roach        };
85c156e8f5SGreg Roach    }
86c156e8f5SGreg Roach
87c156e8f5SGreg Roach    /**
88c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
89c156e8f5SGreg Roach     *
90c156e8f5SGreg Roach     * @return Closure
91c156e8f5SGreg Roach     */
92c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
93c156e8f5SGreg Roach    {
946c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
95c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
96c156e8f5SGreg Roach        };
97c156e8f5SGreg Roach    }
98c156e8f5SGreg Roach
99c156e8f5SGreg Roach    /**
100e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
101e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
102e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
103e71ef9d2SGreg Roach     *
104e71ef9d2SGreg Roach     * @param string      $xref
105e71ef9d2SGreg Roach     * @param Tree        $tree
106e71ef9d2SGreg Roach     * @param string|null $gedcom
107e71ef9d2SGreg Roach     *
1086ccdf4f0SGreg Roach     * @throws Exception
109e71ef9d2SGreg Roach     * @return Individual|null
110e71ef9d2SGreg Roach     */
111ffc0a61fSGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Individual
112c1010edaSGreg Roach    {
113e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
114e71ef9d2SGreg Roach
115e364afe4SGreg Roach        if ($record instanceof self) {
116e71ef9d2SGreg Roach            return $record;
117e71ef9d2SGreg Roach        }
118b2ce94c6SRico Sonntag
119b2ce94c6SRico Sonntag        return null;
120e71ef9d2SGreg Roach    }
121e71ef9d2SGreg Roach
122e71ef9d2SGreg Roach    /**
123395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
124395f0fe0SGreg Roach     * Typically when we load families and their members.
125395f0fe0SGreg Roach     *
126395f0fe0SGreg Roach     * @param Tree     $tree
1274a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
12818d7a90dSGreg Roach     *
12918d7a90dSGreg Roach     * @return void
130395f0fe0SGreg Roach     */
1312e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
132c1010edaSGreg Roach    {
1332e5b4452SGreg Roach        $rows = DB::table('individuals')
1342e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1352e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1362e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1372e5b4452SGreg Roach            ->get();
138395f0fe0SGreg Roach
139395f0fe0SGreg Roach        foreach ($rows as $row) {
140395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
141395f0fe0SGreg Roach        }
142395f0fe0SGreg Roach    }
143395f0fe0SGreg Roach
144395f0fe0SGreg Roach    /**
145a25f0a04SGreg Roach     * Can the name of this record be shown?
146a25f0a04SGreg Roach     *
14776692c8bSGreg Roach     * @param int|null $access_level
14876692c8bSGreg Roach     *
14976692c8bSGreg Roach     * @return bool
150a25f0a04SGreg Roach     */
15135584196SGreg Roach    public function canShowName(int $access_level = null): bool
152c1010edaSGreg Roach    {
153d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
1544b9ff166SGreg Roach
155518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
156a25f0a04SGreg Roach    }
157a25f0a04SGreg Roach
158a25f0a04SGreg Roach    /**
15976692c8bSGreg Roach     * Can this individual be shown?
160a25f0a04SGreg Roach     *
16176692c8bSGreg Roach     * @param int $access_level
16276692c8bSGreg Roach     *
16376692c8bSGreg Roach     * @return bool
164a25f0a04SGreg Roach     */
16535584196SGreg Roach    protected function canShowByType(int $access_level): bool
166c1010edaSGreg Roach    {
167a25f0a04SGreg Roach        // Dead people...
168518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
169a25f0a04SGreg Roach            $keep_alive             = false;
17054ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
171a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1728d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
173a25f0a04SGreg Roach                foreach ($matches as $match) {
174a25f0a04SGreg Roach                    $date = new Date($match[1]);
175a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
176a25f0a04SGreg Roach                        $keep_alive = true;
177a25f0a04SGreg Roach                        break;
178a25f0a04SGreg Roach                    }
179a25f0a04SGreg Roach                }
180a25f0a04SGreg Roach            }
18154ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
182a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1838d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
184a25f0a04SGreg Roach                foreach ($matches as $match) {
185a25f0a04SGreg Roach                    $date = new Date($match[1]);
186a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
187a25f0a04SGreg Roach                        $keep_alive = true;
188a25f0a04SGreg Roach                        break;
189a25f0a04SGreg Roach                    }
190a25f0a04SGreg Roach                }
191a25f0a04SGreg Roach            }
192a25f0a04SGreg Roach            if (!$keep_alive) {
193a25f0a04SGreg Roach                return true;
194a25f0a04SGreg Roach            }
195a25f0a04SGreg Roach        }
196a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
1977c4add84SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_PATH_LENGTH);
1987c4add84SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF);
1997c4add84SGreg Roach
20054ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
2014b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
202a25f0a04SGreg Roach        }
203a25f0a04SGreg Roach
204a25f0a04SGreg Roach        // No restriction found - show living people to members only:
2054b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
206a25f0a04SGreg Roach    }
207a25f0a04SGreg Roach
208a25f0a04SGreg Roach    /**
209a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
210a25f0a04SGreg Roach     *
211a25f0a04SGreg Roach     * @param Individual $target
212cbc1590aSGreg Roach     * @param int        $distance
213a25f0a04SGreg Roach     *
214cbc1590aSGreg Roach     * @return bool
215a25f0a04SGreg Roach     */
2168f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
217c1010edaSGreg Roach    {
218a25f0a04SGreg Roach        static $cache = null;
219a25f0a04SGreg Roach
2207c4add84SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $target->tree);
221a25f0a04SGreg Roach        if ($user_individual) {
222a25f0a04SGreg Roach            if (!$cache) {
22313abd6f3SGreg Roach                $cache = [
22413abd6f3SGreg Roach                    0 => [$user_individual],
22513abd6f3SGreg Roach                    1 => [],
22613abd6f3SGreg Roach                ];
2278d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
228dc124885SGreg Roach                    $family = $fact->target();
229e24444eeSGreg Roach                    if ($family instanceof Family) {
230a25f0a04SGreg Roach                        $cache[1][] = $family;
231a25f0a04SGreg Roach                    }
232a25f0a04SGreg Roach                }
233a25f0a04SGreg Roach            }
234a25f0a04SGreg Roach        } else {
235a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
236a25f0a04SGreg Roach            return true;
237a25f0a04SGreg Roach        }
238a25f0a04SGreg Roach
239a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
240a25f0a04SGreg Roach        $distance *= 2;
241a25f0a04SGreg Roach
242a25f0a04SGreg Roach        // Consider each path length in turn
243a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
244a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
245a25f0a04SGreg Roach                // We have already calculated all records with this length
246e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
247a25f0a04SGreg Roach                    return true;
248a25f0a04SGreg Roach                }
249a25f0a04SGreg Roach            } else {
250a25f0a04SGreg Roach                // Need to calculate these paths
25113abd6f3SGreg Roach                $cache[$n] = [];
252e364afe4SGreg Roach                if ($n % 2 === 0) {
253a25f0a04SGreg Roach                    // Add FAM->INDI links
254a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2558d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
256dc124885SGreg Roach                            $individual = $fact->target();
257a25f0a04SGreg Roach                            // Don’t backtrack
258e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
259a25f0a04SGreg Roach                                $cache[$n][] = $individual;
260a25f0a04SGreg Roach                            }
261a25f0a04SGreg Roach                        }
262a25f0a04SGreg Roach                    }
263a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
264a25f0a04SGreg Roach                        return true;
265a25f0a04SGreg Roach                    }
266a25f0a04SGreg Roach                } else {
267a25f0a04SGreg Roach                    // Add INDI->FAM links
268a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2698d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
270dc124885SGreg Roach                            $family = $fact->target();
271a25f0a04SGreg Roach                            // Don’t backtrack
272e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
273a25f0a04SGreg Roach                                $cache[$n][] = $family;
274a25f0a04SGreg Roach                            }
275a25f0a04SGreg Roach                        }
276a25f0a04SGreg Roach                    }
277a25f0a04SGreg Roach                }
278a25f0a04SGreg Roach            }
279a25f0a04SGreg Roach        }
280a25f0a04SGreg Roach
281a25f0a04SGreg Roach        return false;
282a25f0a04SGreg Roach    }
283a25f0a04SGreg Roach
28476692c8bSGreg Roach    /**
28576692c8bSGreg Roach     * Generate a private version of this record
28676692c8bSGreg Roach     *
28776692c8bSGreg Roach     * @param int $access_level
28876692c8bSGreg Roach     *
28976692c8bSGreg Roach     * @return string
29076692c8bSGreg Roach     */
2913c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
292c1010edaSGreg Roach    {
293acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
294a25f0a04SGreg Roach
295a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
296518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
297a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2988d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
299138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
300a25f0a04SGreg Roach            }
301a25f0a04SGreg Roach        }
302a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
3038d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
304a25f0a04SGreg Roach        foreach ($matches as $match) {
30524ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
306a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
307a25f0a04SGreg Roach                $rec .= $match[0];
308a25f0a04SGreg Roach            }
309a25f0a04SGreg Roach        }
310a25f0a04SGreg Roach        // Don’t privatize sex.
311a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
312a25f0a04SGreg Roach            $rec .= $match[0];
313a25f0a04SGreg Roach        }
314a25f0a04SGreg Roach
315a25f0a04SGreg Roach        return $rec;
316a25f0a04SGreg Roach    }
317a25f0a04SGreg Roach
31876692c8bSGreg Roach    /**
31976692c8bSGreg Roach     * Fetch data from the database
32076692c8bSGreg Roach     *
32176692c8bSGreg Roach     * @param string $xref
32276692c8bSGreg Roach     * @param int    $tree_id
32376692c8bSGreg Roach     *
324e364afe4SGreg Roach     * @return string|null
32576692c8bSGreg Roach     */
326e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
327c1010edaSGreg Roach    {
3282e5b4452SGreg Roach        return DB::table('individuals')
3292e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3302e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3312e5b4452SGreg Roach            ->value('i_gedcom');
332a25f0a04SGreg Roach    }
333a25f0a04SGreg Roach
334a25f0a04SGreg Roach    /**
335a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
336a25f0a04SGreg Roach     * If not known to be dead, then assume living.
337a25f0a04SGreg Roach     *
338cbc1590aSGreg Roach     * @return bool
339a25f0a04SGreg Roach     */
3408f53f488SRico Sonntag    public function isDead(): bool
341c1010edaSGreg Roach    {
342c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3434459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
344a25f0a04SGreg Roach
345a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3468d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
347a25f0a04SGreg Roach            return true;
348a25f0a04SGreg Roach        }
349a25f0a04SGreg Roach
350a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
351a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
352a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
353a25f0a04SGreg Roach                $date = new Date($date_match);
354269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
355a25f0a04SGreg Roach                    return true;
356a25f0a04SGreg Roach                }
357a25f0a04SGreg Roach            }
358a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
359a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
360a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
361a25f0a04SGreg Roach                return false;
362a25f0a04SGreg Roach            }
363a25f0a04SGreg Roach        }
364a25f0a04SGreg Roach
365a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
366a25f0a04SGreg Roach
367a25f0a04SGreg Roach        // Check parents (birth and adopted)
36839ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
36939ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
370a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
371a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
372a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
373a25f0a04SGreg Roach                    $date = new Date($date_match);
374269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
375a25f0a04SGreg Roach                        return true;
376a25f0a04SGreg Roach                    }
377a25f0a04SGreg Roach                }
378a25f0a04SGreg Roach            }
379a25f0a04SGreg Roach        }
380a25f0a04SGreg Roach
381a25f0a04SGreg Roach        // Check spouses
38239ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
383a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
384a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
385a25f0a04SGreg Roach                $date = new Date($date_match);
386a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
387269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
388a25f0a04SGreg Roach                    return true;
389a25f0a04SGreg Roach                }
390a25f0a04SGreg Roach            }
391a25f0a04SGreg Roach            // Check spouse dates
39239ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
393a25f0a04SGreg Roach            if ($spouse) {
394a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
395a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
396a25f0a04SGreg Roach                    $date = new Date($date_match);
397a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
398269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
399a25f0a04SGreg Roach                        return true;
400a25f0a04SGreg Roach                    }
401a25f0a04SGreg Roach                }
402a25f0a04SGreg Roach            }
403a25f0a04SGreg Roach            // Check child dates
40439ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
405a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
406a25f0a04SGreg Roach                // Assume children born after age of 15
407a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
408a25f0a04SGreg Roach                    $date = new Date($date_match);
409269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
410a25f0a04SGreg Roach                        return true;
411a25f0a04SGreg Roach                    }
412a25f0a04SGreg Roach                }
413a25f0a04SGreg Roach                // Check grandchildren
41439ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
41539ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
416a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
417a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
418a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
419a25f0a04SGreg Roach                            $date = new Date($date_match);
420269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
421a25f0a04SGreg Roach                                return true;
422a25f0a04SGreg Roach                            }
423a25f0a04SGreg Roach                        }
424a25f0a04SGreg Roach                    }
425a25f0a04SGreg Roach                }
426a25f0a04SGreg Roach            }
427a25f0a04SGreg Roach        }
428a25f0a04SGreg Roach
429a25f0a04SGreg Roach        return false;
430a25f0a04SGreg Roach    }
431a25f0a04SGreg Roach
432a25f0a04SGreg Roach    /**
433a25f0a04SGreg Roach     * Find the highlighted media object for an individual
434a25f0a04SGreg Roach     *
435e364afe4SGreg Roach     * @return MediaFile|null
436a25f0a04SGreg Roach     */
437e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
438c1010edaSGreg Roach    {
439*19d319e7SGreg Roach        $media = $this->facts(['OBJE'])
440*19d319e7SGreg Roach            ->first(static function (Fact $fact): bool {
441dc124885SGreg Roach                $media = $fact->target();
442*19d319e7SGreg Roach
443*19d319e7SGreg Roach                return $media instanceof Media && $media->firstImageFile() instanceof MediaFile;
444*19d319e7SGreg Roach            });
445*19d319e7SGreg Roach
446a213a63cSGreg Roach        if ($media instanceof Media) {
447*19d319e7SGreg Roach            return $media->firstImageFile();
448a25f0a04SGreg Roach        }
449a25f0a04SGreg Roach
450a25f0a04SGreg Roach        return null;
451a25f0a04SGreg Roach    }
452a25f0a04SGreg Roach
453a25f0a04SGreg Roach    /**
454a25f0a04SGreg Roach     * Display the prefered image for this individual.
455a25f0a04SGreg Roach     * Use an icon if no image is available.
456a25f0a04SGreg Roach     *
457dce07401SGreg Roach     * @param int      $width      Pixels
458dce07401SGreg Roach     * @param int      $height     Pixels
459dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
460dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
461dce07401SGreg Roach     *
462a25f0a04SGreg Roach     * @return string
463a25f0a04SGreg Roach     */
4648f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
465c1010edaSGreg Roach    {
4664a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4674a9f750fSGreg Roach
4684a9f750fSGreg Roach        if ($media_file !== null) {
4694a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
470a25f0a04SGreg Roach        }
4714a9f750fSGreg Roach
4724a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
47339ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4744a9f750fSGreg Roach        }
4754a9f750fSGreg Roach
4764a9f750fSGreg Roach        return '';
477a25f0a04SGreg Roach    }
478a25f0a04SGreg Roach
479a25f0a04SGreg Roach    /**
480a25f0a04SGreg Roach     * Get the date of birth
481a25f0a04SGreg Roach     *
482a25f0a04SGreg Roach     * @return Date
483a25f0a04SGreg Roach     */
4848f53f488SRico Sonntag    public function getBirthDate(): Date
485c1010edaSGreg Roach    {
486a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
487a25f0a04SGreg Roach            if ($date->isOK()) {
488a25f0a04SGreg Roach                return $date;
489a25f0a04SGreg Roach            }
490a25f0a04SGreg Roach        }
491a25f0a04SGreg Roach
492a25f0a04SGreg Roach        return new Date('');
493a25f0a04SGreg Roach    }
494a25f0a04SGreg Roach
495a25f0a04SGreg Roach    /**
496a25f0a04SGreg Roach     * Get the place of birth
497a25f0a04SGreg Roach     *
49816d0b7f7SRico Sonntag     * @return Place
499a25f0a04SGreg Roach     */
5008f53f488SRico Sonntag    public function getBirthPlace(): Place
501c1010edaSGreg Roach    {
502a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
503a25f0a04SGreg Roach            return $place;
504a25f0a04SGreg Roach        }
505a25f0a04SGreg Roach
506b20ddbf9SGreg Roach        return new Place('', $this->tree);
507a25f0a04SGreg Roach    }
508a25f0a04SGreg Roach
509a25f0a04SGreg Roach    /**
510a25f0a04SGreg Roach     * Get the year of birth
511a25f0a04SGreg Roach     *
512a25f0a04SGreg Roach     * @return string the year of birth
513a25f0a04SGreg Roach     */
5148f53f488SRico Sonntag    public function getBirthYear(): string
515c1010edaSGreg Roach    {
516f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
517a25f0a04SGreg Roach    }
518a25f0a04SGreg Roach
519a25f0a04SGreg Roach    /**
520a25f0a04SGreg Roach     * Get the date of death
521a25f0a04SGreg Roach     *
522a25f0a04SGreg Roach     * @return Date
523a25f0a04SGreg Roach     */
5248f53f488SRico Sonntag    public function getDeathDate(): Date
525c1010edaSGreg Roach    {
526a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
527a25f0a04SGreg Roach            if ($date->isOK()) {
528a25f0a04SGreg Roach                return $date;
529a25f0a04SGreg Roach            }
530a25f0a04SGreg Roach        }
531a25f0a04SGreg Roach
532a25f0a04SGreg Roach        return new Date('');
533a25f0a04SGreg Roach    }
534a25f0a04SGreg Roach
535a25f0a04SGreg Roach    /**
536a25f0a04SGreg Roach     * Get the place of death
537a25f0a04SGreg Roach     *
53816d0b7f7SRico Sonntag     * @return Place
539a25f0a04SGreg Roach     */
5408f53f488SRico Sonntag    public function getDeathPlace(): Place
541c1010edaSGreg Roach    {
542a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
543a25f0a04SGreg Roach            return $place;
544a25f0a04SGreg Roach        }
545a25f0a04SGreg Roach
546b20ddbf9SGreg Roach        return new Place('', $this->tree);
547a25f0a04SGreg Roach    }
548a25f0a04SGreg Roach
549a25f0a04SGreg Roach    /**
550a25f0a04SGreg Roach     * get the death year
551a25f0a04SGreg Roach     *
552a25f0a04SGreg Roach     * @return string the year of death
553a25f0a04SGreg Roach     */
5548f53f488SRico Sonntag    public function getDeathYear(): string
555c1010edaSGreg Roach    {
556f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
557a25f0a04SGreg Roach    }
558a25f0a04SGreg Roach
559a25f0a04SGreg Roach    /**
560a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
56115d603e7SGreg Roach     * Provide the place and full date using a tooltip.
562a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
563a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
564a25f0a04SGreg Roach     *
565a25f0a04SGreg Roach     * @return string
566a25f0a04SGreg Roach     */
5675e6816beSGreg Roach    public function lifespan(): string
568c1010edaSGreg Roach    {
56915d603e7SGreg Roach        // Just the first part of the place name
570392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
571392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
57215d603e7SGreg Roach        // Remove markup from dates
57315d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
57415d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
57515d603e7SGreg Roach
576c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
577dacfe33cSGreg Roach        return I18N::translate(
578a25f0a04SGreg Roach            '%1$s–%2$s',
5792d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5802d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
581a25f0a04SGreg Roach        );
582a25f0a04SGreg Roach    }
583a25f0a04SGreg Roach
584a25f0a04SGreg Roach    /**
585a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
586a25f0a04SGreg Roach     *
587a25f0a04SGreg Roach     * @return Date[]
588a25f0a04SGreg Roach     */
5898f53f488SRico Sonntag    public function getAllBirthDates(): array
590c1010edaSGreg Roach    {
5918d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5928d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
593a25f0a04SGreg Roach            if ($tmp) {
594a25f0a04SGreg Roach                return $tmp;
595a25f0a04SGreg Roach            }
596a25f0a04SGreg Roach        }
597a25f0a04SGreg Roach
59813abd6f3SGreg Roach        return [];
599a25f0a04SGreg Roach    }
600a25f0a04SGreg Roach
601a25f0a04SGreg Roach    /**
602a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
603a25f0a04SGreg Roach     *
6044080d558SGreg Roach     * @return Place[]
605a25f0a04SGreg Roach     */
6068f53f488SRico Sonntag    public function getAllBirthPlaces(): array
607c1010edaSGreg Roach    {
6088d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
6098d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
61054c1ab5eSGreg Roach            if ($places !== []) {
6114080d558SGreg Roach                return $places;
612a25f0a04SGreg Roach            }
613a25f0a04SGreg Roach        }
614a25f0a04SGreg Roach
61513abd6f3SGreg Roach        return [];
616a25f0a04SGreg Roach    }
617a25f0a04SGreg Roach
618a25f0a04SGreg Roach    /**
619a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
620a25f0a04SGreg Roach     *
621a25f0a04SGreg Roach     * @return Date[]
622a25f0a04SGreg Roach     */
6238f53f488SRico Sonntag    public function getAllDeathDates(): array
624c1010edaSGreg Roach    {
6258d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6268d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
627a25f0a04SGreg Roach            if ($tmp) {
628a25f0a04SGreg Roach                return $tmp;
629a25f0a04SGreg Roach            }
630a25f0a04SGreg Roach        }
631a25f0a04SGreg Roach
63213abd6f3SGreg Roach        return [];
633a25f0a04SGreg Roach    }
634a25f0a04SGreg Roach
635a25f0a04SGreg Roach    /**
636a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
637a25f0a04SGreg Roach     *
6384080d558SGreg Roach     * @return Place[]
639a25f0a04SGreg Roach     */
6408f53f488SRico Sonntag    public function getAllDeathPlaces(): array
641c1010edaSGreg Roach    {
6428d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6438d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
64454c1ab5eSGreg Roach            if ($places !== []) {
6454080d558SGreg Roach                return $places;
646a25f0a04SGreg Roach            }
647a25f0a04SGreg Roach        }
648a25f0a04SGreg Roach
64913abd6f3SGreg Roach        return [];
650a25f0a04SGreg Roach    }
651a25f0a04SGreg Roach
652a25f0a04SGreg Roach    /**
653a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
654a25f0a04SGreg Roach     *
655a25f0a04SGreg Roach     * @return Date
656a25f0a04SGreg Roach     */
6578f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
658c1010edaSGreg Roach    {
6598f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
660a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
661a25f0a04SGreg Roach                if ($date->isOK()) {
6624686330aSGreg Roach                    $this->estimated_birth_date = $date;
663a25f0a04SGreg Roach                    break;
664a25f0a04SGreg Roach                }
665a25f0a04SGreg Roach            }
6668f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
66713abd6f3SGreg Roach                $min = [];
66813abd6f3SGreg Roach                $max = [];
669a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
670f5b60decSGreg Roach                if ($tmp->isOK()) {
671f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
672f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
673a25f0a04SGreg Roach                }
67439ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
675a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
676f5b60decSGreg Roach                    if ($tmp->isOK()) {
677f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
678f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
679a25f0a04SGreg Roach                    }
68039ca88baSGreg Roach                    $husband = $family->husband();
681e364afe4SGreg Roach                    if ($husband instanceof self) {
6822e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
683f5b60decSGreg Roach                        if ($tmp->isOK()) {
684f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
685f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
686a25f0a04SGreg Roach                        }
687a25f0a04SGreg Roach                    }
68839ca88baSGreg Roach                    $wife = $family->wife();
689e364afe4SGreg Roach                    if ($wife instanceof self) {
6902e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
691f5b60decSGreg Roach                        if ($tmp->isOK()) {
692f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
693f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
694a25f0a04SGreg Roach                        }
695a25f0a04SGreg Roach                    }
69639ca88baSGreg Roach                    foreach ($family->children() as $child) {
697a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
698f5b60decSGreg Roach                        if ($tmp->isOK()) {
699f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
700f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
701a25f0a04SGreg Roach                        }
702a25f0a04SGreg Roach                    }
703a25f0a04SGreg Roach                }
70439ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
705a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
706f5b60decSGreg Roach                    if ($tmp->isOK()) {
707f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
708f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
709a25f0a04SGreg Roach                    }
71039ca88baSGreg Roach                    $spouse = $family->spouse($this);
711a25f0a04SGreg Roach                    if ($spouse) {
712a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
713f5b60decSGreg Roach                        if ($tmp->isOK()) {
714f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
715f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
716a25f0a04SGreg Roach                        }
717a25f0a04SGreg Roach                    }
71839ca88baSGreg Roach                    foreach ($family->children() as $child) {
719a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
720f5b60decSGreg Roach                        if ($tmp->isOK()) {
721e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
722f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
723a25f0a04SGreg Roach                        }
724a25f0a04SGreg Roach                    }
725a25f0a04SGreg Roach                }
726a25f0a04SGreg Roach                if ($min && $max) {
72759f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
728a25f0a04SGreg Roach
72965e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7304686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
731a25f0a04SGreg Roach                } else {
7324686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
733a25f0a04SGreg Roach                }
734a25f0a04SGreg Roach            }
735a25f0a04SGreg Roach        }
736a25f0a04SGreg Roach
7374686330aSGreg Roach        return $this->estimated_birth_date;
738a25f0a04SGreg Roach    }
739a25f0a04SGreg Roach
740a25f0a04SGreg Roach    /**
741a25f0a04SGreg Roach     * Generate an estimated date of death.
742a25f0a04SGreg Roach     *
743a25f0a04SGreg Roach     * @return Date
744a25f0a04SGreg Roach     */
7458f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
746c1010edaSGreg Roach    {
7474686330aSGreg Roach        if ($this->estimated_death_date === null) {
748a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
749a25f0a04SGreg Roach                if ($date->isOK()) {
7504686330aSGreg Roach                    $this->estimated_death_date = $date;
751a25f0a04SGreg Roach                    break;
752a25f0a04SGreg Roach                }
753a25f0a04SGreg Roach            }
7544686330aSGreg Roach            if ($this->estimated_death_date === null) {
755f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
756c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7574686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
758a25f0a04SGreg Roach                } else {
7594686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
760a25f0a04SGreg Roach                }
761a25f0a04SGreg Roach            }
762a25f0a04SGreg Roach        }
763a25f0a04SGreg Roach
7644686330aSGreg Roach        return $this->estimated_death_date;
765a25f0a04SGreg Roach    }
766a25f0a04SGreg Roach
767a25f0a04SGreg Roach    /**
768a25f0a04SGreg Roach     * Get the sex - M F or U
769a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
770a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
771a25f0a04SGreg Roach     *
772a25f0a04SGreg Roach     * @return string
773a25f0a04SGreg Roach     */
774e364afe4SGreg Roach    public function sex(): string
775c1010edaSGreg Roach    {
776a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
777a25f0a04SGreg Roach            return $match[1];
778a25f0a04SGreg Roach        }
779b2ce94c6SRico Sonntag
780b2ce94c6SRico Sonntag        return 'U';
781a25f0a04SGreg Roach    }
782a25f0a04SGreg Roach
783a25f0a04SGreg Roach    /**
784a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
785a25f0a04SGreg Roach     *
786a25f0a04SGreg Roach     * @return string
787a25f0a04SGreg Roach     */
7888f53f488SRico Sonntag    public function getBoxStyle(): string
789c1010edaSGreg Roach    {
790c1010edaSGreg Roach        $tmp = [
791c1010edaSGreg Roach            'M' => '',
792c1010edaSGreg Roach            'F' => 'F',
793c1010edaSGreg Roach            'U' => 'NN',
794c1010edaSGreg Roach        ];
795a25f0a04SGreg Roach
79639ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
797a25f0a04SGreg Roach    }
798a25f0a04SGreg Roach
799a25f0a04SGreg Roach    /**
800a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
801a25f0a04SGreg Roach     *
802cbc1590aSGreg Roach     * @param int|null $access_level
803a25f0a04SGreg Roach     *
804b5c8fd7eSGreg Roach     * @return Collection<Family>
805a25f0a04SGreg Roach     */
80639ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
807c1010edaSGreg Roach    {
808d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
809d9e083e7SGreg Roach
810d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
811d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
8124b9ff166SGreg Roach        }
8134b9ff166SGreg Roach
81439ca88baSGreg Roach        $families = new Collection();
815d9e083e7SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level) as $fact) {
816dc124885SGreg Roach            $family = $fact->target();
817d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
81839ca88baSGreg Roach                $families->push($family);
819a25f0a04SGreg Roach            }
820a25f0a04SGreg Roach        }
821a25f0a04SGreg Roach
82239ca88baSGreg Roach        return new Collection($families);
823a25f0a04SGreg Roach    }
824a25f0a04SGreg Roach
825a25f0a04SGreg Roach    /**
826a25f0a04SGreg Roach     * Get the current spouse of this individual.
827a25f0a04SGreg Roach     *
828a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
829a25f0a04SGreg Roach     * in chronological order, and take the last one found.
830a25f0a04SGreg Roach     *
831a25f0a04SGreg Roach     * @return Individual|null
832a25f0a04SGreg Roach     */
833e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
834c1010edaSGreg Roach    {
83539ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
83639ca88baSGreg Roach
83739ca88baSGreg Roach        if ($family instanceof Family) {
83839ca88baSGreg Roach            return $family->spouse($this);
839a25f0a04SGreg Roach        }
840b2ce94c6SRico Sonntag
841b2ce94c6SRico Sonntag        return null;
842a25f0a04SGreg Roach    }
843a25f0a04SGreg Roach
844a25f0a04SGreg Roach    /**
845a25f0a04SGreg Roach     * Count the children belonging to this individual.
846a25f0a04SGreg Roach     *
847cbc1590aSGreg Roach     * @return int
848a25f0a04SGreg Roach     */
849e364afe4SGreg Roach    public function numberOfChildren(): int
850c1010edaSGreg Roach    {
8517d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8523dc7bbe9SGreg Roach            return (int) $match[1];
853b2ce94c6SRico Sonntag        }
854b2ce94c6SRico Sonntag
85513abd6f3SGreg Roach        $children = [];
85639ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
85739ca88baSGreg Roach            foreach ($fam->children() as $child) {
858c0935879SGreg Roach                $children[$child->xref()] = true;
859a25f0a04SGreg Roach            }
860a25f0a04SGreg Roach        }
861a25f0a04SGreg Roach
862a25f0a04SGreg Roach        return count($children);
863a25f0a04SGreg Roach    }
864a25f0a04SGreg Roach
865a25f0a04SGreg Roach    /**
866a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
867a25f0a04SGreg Roach     *
868cbc1590aSGreg Roach     * @param int|null $access_level
869a25f0a04SGreg Roach     *
870b5c8fd7eSGreg Roach     * @return Collection<Family>
871a25f0a04SGreg Roach     */
87239ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
873c1010edaSGreg Roach    {
874d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
8754b9ff166SGreg Roach
876d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
877d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
878d9e083e7SGreg Roach        }
879a25f0a04SGreg Roach
88039ca88baSGreg Roach        $families = new Collection();
88139ca88baSGreg Roach
882d9e083e7SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level) as $fact) {
883dc124885SGreg Roach            $family = $fact->target();
884d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
88539ca88baSGreg Roach                $families->push($family);
886a25f0a04SGreg Roach            }
887a25f0a04SGreg Roach        }
888a25f0a04SGreg Roach
889a25f0a04SGreg Roach        return $families;
890a25f0a04SGreg Roach    }
891a25f0a04SGreg Roach
892a25f0a04SGreg Roach    /**
893a25f0a04SGreg Roach     * Get a list of step-parent families.
894a25f0a04SGreg Roach     *
895b5c8fd7eSGreg Roach     * @return Collection<Family>
896a25f0a04SGreg Roach     */
897820b62dfSGreg Roach    public function childStepFamilies(): Collection
898c1010edaSGreg Roach    {
899ed5b6227SGreg Roach        $step_families = new Collection();
90039ca88baSGreg Roach        $families      = $this->childFamilies();
901a25f0a04SGreg Roach        foreach ($families as $family) {
902ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
903ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
90439ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
905ed5b6227SGreg Roach                        $step_families->add($step_family);
906a25f0a04SGreg Roach                    }
907a25f0a04SGreg Roach                }
908a25f0a04SGreg Roach            }
909a25f0a04SGreg Roach        }
910a25f0a04SGreg Roach
9118c627a69SGreg Roach        return $step_families->uniqueStrict(static function (Family $family): string {
9128c627a69SGreg Roach            return $family->xref();
9138c627a69SGreg Roach        });
914a25f0a04SGreg Roach    }
915a25f0a04SGreg Roach
916a25f0a04SGreg Roach    /**
917a25f0a04SGreg Roach     * Get a list of step-parent families.
918a25f0a04SGreg Roach     *
919b5c8fd7eSGreg Roach     * @return Collection<Family>
920a25f0a04SGreg Roach     */
921820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
922c1010edaSGreg Roach    {
92313abd6f3SGreg Roach        $step_families = [];
92439ca88baSGreg Roach        $families      = $this->spouseFamilies();
925820b62dfSGreg Roach
926a25f0a04SGreg Roach        foreach ($families as $family) {
92739ca88baSGreg Roach            $spouse = $family->spouse($this);
928820b62dfSGreg Roach
929b5c8fd7eSGreg Roach            if ($spouse instanceof Individual) {
93039ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
93139ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
932a25f0a04SGreg Roach                        $step_families[] = $step_family;
933a25f0a04SGreg Roach                    }
934a25f0a04SGreg Roach                }
935a25f0a04SGreg Roach            }
936a25f0a04SGreg Roach        }
937a25f0a04SGreg Roach
938820b62dfSGreg Roach        return new Collection($step_families);
939a25f0a04SGreg Roach    }
940a25f0a04SGreg Roach
941a25f0a04SGreg Roach    /**
942a25f0a04SGreg Roach     * A label for a parental family group
943a25f0a04SGreg Roach     *
944a25f0a04SGreg Roach     * @param Family $family
945a25f0a04SGreg Roach     *
946a25f0a04SGreg Roach     * @return string
947a25f0a04SGreg Roach     */
948820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
949c1010edaSGreg Roach    {
9507d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
951a25f0a04SGreg Roach            // A specified pedigree
952764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
953b2ce94c6SRico Sonntag        }
954b2ce94c6SRico Sonntag
955a25f0a04SGreg Roach        // Default (birth) pedigree
956764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
957a25f0a04SGreg Roach    }
958a25f0a04SGreg Roach
959a25f0a04SGreg Roach    /**
960a25f0a04SGreg Roach     * Create a label for a step family
961a25f0a04SGreg Roach     *
962a25f0a04SGreg Roach     * @param Family $step_family
963a25f0a04SGreg Roach     *
964a25f0a04SGreg Roach     * @return string
965a25f0a04SGreg Roach     */
9668f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
967c1010edaSGreg Roach    {
96839ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
969a25f0a04SGreg Roach            if ($family !== $step_family) {
970a25f0a04SGreg Roach                // Must be a step-family
97139ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
97239ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
973a25f0a04SGreg Roach                        if ($parent === $step_parent) {
974a25f0a04SGreg Roach                            // One common parent - must be a step family
975e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
976a25f0a04SGreg Roach                                // Father’s family with someone else
97739ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
978a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
97939ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
980b2ce94c6SRico Sonntag                                }
981b2ce94c6SRico Sonntag
982a25f0a04SGreg Roach                                /* I18N: A step-family. */
983bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
984a25f0a04SGreg Roach                            }
985b2ce94c6SRico Sonntag
986a25f0a04SGreg Roach                            // Mother’s family with someone else
98739ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
988a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
98939ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
990b2ce94c6SRico Sonntag                            }
991b2ce94c6SRico Sonntag
992a25f0a04SGreg Roach                            /* I18N: A step-family. */
993bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
994a25f0a04SGreg Roach                        }
995a25f0a04SGreg Roach                    }
996a25f0a04SGreg Roach                }
997a25f0a04SGreg Roach            }
998a25f0a04SGreg Roach        }
999a25f0a04SGreg Roach
1000a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1001a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1002a25f0a04SGreg Roach    }
1003a25f0a04SGreg Roach
1004225e381fSGreg Roach    /**
1005225e381fSGreg Roach     * Get the description for the family.
1006225e381fSGreg Roach     *
1007225e381fSGreg Roach     * For example, "XXX's family with new wife".
1008225e381fSGreg Roach     *
1009225e381fSGreg Roach     * @param Family $family
1010225e381fSGreg Roach     *
1011225e381fSGreg Roach     * @return string
1012225e381fSGreg Roach     */
1013e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
1014c1010edaSGreg Roach    {
101539ca88baSGreg Roach        $spouse = $family->spouse($this);
1016225e381fSGreg Roach        if ($spouse) {
1017225e381fSGreg Roach            /* I18N: %s is the spouse name */
101839ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1019225e381fSGreg Roach        }
1020b2ce94c6SRico Sonntag
102139ca88baSGreg Roach        return $family->fullName();
1022225e381fSGreg Roach    }
1023225e381fSGreg Roach
1024a25f0a04SGreg Roach    /**
1025961ec755SGreg Roach     * If this object has no name, what do we call it?
1026961ec755SGreg Roach     *
1027961ec755SGreg Roach     * @return string
1028961ec755SGreg Roach     */
10298f53f488SRico Sonntag    public function getFallBackName(): string
1030c1010edaSGreg Roach    {
1031a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1032a25f0a04SGreg Roach    }
1033a25f0a04SGreg Roach
1034a25f0a04SGreg Roach    /**
1035a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1036a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1037a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1038a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1039a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1040a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1041a25f0a04SGreg Roach     *
1042a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1043a25f0a04SGreg Roach     * 2 GIVN Robert
1044a25f0a04SGreg Roach     * 2 SPFX de
1045a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1046a25f0a04SGreg Roach     * 2 NICK The Bald
1047a25f0a04SGreg Roach     *
1048a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1049a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1050a25f0a04SGreg Roach     *
1051a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1052a25f0a04SGreg Roach     *
1053a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1054a25f0a04SGreg Roach     * or
1055a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1056a25f0a04SGreg Roach     * 2 GIVN Carlos
1057a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1058a25f0a04SGreg Roach     *
1059a25f0a04SGreg Roach     * @param string $type
1060a25f0a04SGreg Roach     * @param string $full
1061a25f0a04SGreg Roach     * @param string $gedcom
1062e364afe4SGreg Roach     *
1063e364afe4SGreg Roach     * @return void
1064a25f0a04SGreg Roach     */
1065e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1066c1010edaSGreg Roach    {
1067a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1068a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1069a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1070a25f0a04SGreg Roach
107176f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1072a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1073a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1074a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1075a25f0a04SGreg Roach
1076a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
107776f666f4SGreg Roach        if ($SURN !== '') {
1078a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1079a25f0a04SGreg Roach        } else {
108013abd6f3SGreg Roach            $SURNS = [];
1081a25f0a04SGreg Roach        }
108276f666f4SGreg Roach
1083a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1084a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1085a25f0a04SGreg Roach
1086a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1087a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1088a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1089a25f0a04SGreg Roach
1090a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
109176f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1092e364afe4SGreg Roach            $full .= '/';
1093a25f0a04SGreg Roach        }
1094a25f0a04SGreg Roach
1095a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1096a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1097a25f0a04SGreg Roach
1098a25f0a04SGreg Roach        // Extract the surname.
1099a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1100a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1101a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1102a25f0a04SGreg Roach        } else {
1103a25f0a04SGreg Roach            $surname = '';
1104a25f0a04SGreg Roach        }
1105a25f0a04SGreg Roach
1106a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1107a25f0a04SGreg Roach        if (!$SURNS) {
1108a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1109a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1110a25f0a04SGreg Roach                $SURNS = $matches[1];
1111a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1112a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1113a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1114a25f0a04SGreg Roach                }
1115a25f0a04SGreg Roach            } else {
1116a25f0a04SGreg Roach                // It is valid not to have a surname at all
111713abd6f3SGreg Roach                $SURNS = [''];
1118a25f0a04SGreg Roach            }
1119a25f0a04SGreg Roach        }
1120a25f0a04SGreg Roach
1121a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1122a25f0a04SGreg Roach        if (!$GIVN) {
1123a25f0a04SGreg Roach            $GIVN = preg_replace(
112413abd6f3SGreg Roach                [
1125c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1126c1010edaSGreg Roach                    // remove surname
1127c1010edaSGreg Roach                    '/ ?".+"/',
1128c1010edaSGreg Roach                    // remove nickname
1129c1010edaSGreg Roach                    '/ {2,}/',
1130c1010edaSGreg Roach                    // multiple spaces, caused by the above
1131c1010edaSGreg Roach                    '/^ | $/',
1132c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
113313abd6f3SGreg Roach                ],
113413abd6f3SGreg Roach                [
1135a25f0a04SGreg Roach                    ' ',
1136a25f0a04SGreg Roach                    ' ',
1137a25f0a04SGreg Roach                    ' ',
1138a25f0a04SGreg Roach                    '',
113913abd6f3SGreg Roach                ],
1140a25f0a04SGreg Roach                $full
1141a25f0a04SGreg Roach            );
1142a25f0a04SGreg Roach        }
1143a25f0a04SGreg Roach
1144a25f0a04SGreg Roach        // Add placeholder for unknown given name
1145a25f0a04SGreg Roach        if (!$GIVN) {
1146a25f0a04SGreg Roach            $GIVN = '@P.N.';
114773f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1148a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1149a25f0a04SGreg Roach        }
1150a25f0a04SGreg Roach
11517b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
11527b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1153c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
11547b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1155c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1156a25f0a04SGreg Roach        }
1157a25f0a04SGreg Roach
1158a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1159a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1160a25f0a04SGreg Roach        // $full is for display on-screen
1161a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1162a25f0a04SGreg Roach
1163a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1164ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1165ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1166c6f196c3SGreg Roach        // Format for display
1167d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1168acc34ea1SGreg Roach        // Localise quotation marks around the nickname
11690b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
1170c652cdbdSGreg Roach            return '<q class="wt-nickname">' . $matches[1] . '</q>';
11718d68cabeSGreg Roach        }, $full);
1172a25f0a04SGreg Roach
1173c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1174a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1175a25f0a04SGreg Roach
1176a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1177a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1178a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1179a25f0a04SGreg Roach
1180ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1181a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1182e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1183a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1184e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1185a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1186a25f0a04SGreg Roach            }
1187a25f0a04SGreg Roach
1188bdb3725aSGreg Roach            $this->getAllNames[] = [
1189a25f0a04SGreg Roach                'type'    => $type,
1190a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1191c1010edaSGreg Roach                'full'    => $full,
1192c1010edaSGreg Roach                // This is used for display
1193c1010edaSGreg Roach                'fullNN'  => $fullNN,
1194c1010edaSGreg Roach                // This goes into the database
1195c1010edaSGreg Roach                'surname' => $surname,
1196c1010edaSGreg Roach                // This goes into the database
1197c1010edaSGreg Roach                'givn'    => $GIVN,
1198c1010edaSGreg Roach                // This goes into the database
1199c1010edaSGreg Roach                'surn'    => $SURN,
1200c1010edaSGreg Roach                // This goes into the database
120113abd6f3SGreg Roach            ];
1202a25f0a04SGreg Roach        }
1203a25f0a04SGreg Roach    }
1204a25f0a04SGreg Roach
1205a25f0a04SGreg Roach    /**
120676692c8bSGreg Roach     * Extract names from the GEDCOM record.
1207c7ff4153SGreg Roach     *
1208c7ff4153SGreg Roach     * @return void
1209a25f0a04SGreg Roach     */
1210e364afe4SGreg Roach    public function extractNames(): void
1211c1010edaSGreg Roach    {
1212d9e083e7SGreg Roach        $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree);
1213d9e083e7SGreg Roach
12148f53f488SRico Sonntag        $this->extractNamesFromFacts(
12158f53f488SRico Sonntag            1,
12168f53f488SRico Sonntag            'NAME',
1217d9e083e7SGreg Roach            $this->facts(['NAME'], false, $access_level)
12188f53f488SRico Sonntag        );
1219a25f0a04SGreg Roach    }
1220a25f0a04SGreg Roach
1221a25f0a04SGreg Roach    /**
1222a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1223a25f0a04SGreg Roach     * selection items or favorites.
1224a25f0a04SGreg Roach     *
1225a25f0a04SGreg Roach     * @return string
1226a25f0a04SGreg Roach     */
12278f53f488SRico Sonntag    public function formatListDetails(): string
1228c1010edaSGreg Roach    {
1229a25f0a04SGreg Roach        return
12308d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
12318d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1232a25f0a04SGreg Roach    }
1233a25f0a04SGreg Roach}
1234