xref: /webtrees/app/Individual.php (revision 6b9cb339562c8aa4681c7eff2bf3bdf401cc9edd)
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;
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
37852ede8cSGreg Roach    protected const ROUTE_NAME = IndividualPage::class;
38a25f0a04SGreg Roach
3976692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
4076692c8bSGreg Roach    public $generation;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
434686330aSGreg Roach    private $estimated_birth_date;
44a25f0a04SGreg Roach
45a25f0a04SGreg Roach    /** @var Date The estimated date of death */
464686330aSGreg Roach    private $estimated_death_date;
47a25f0a04SGreg Roach
48a25f0a04SGreg Roach    /**
49886b77daSGreg Roach     * A closure which will create a record from a database row.
50886b77daSGreg Roach     *
51bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
52bb03c9f0SGreg Roach     *
53d5ad3db0SGreg Roach     * @param Tree $tree
54d5ad3db0SGreg Roach     *
55886b77daSGreg Roach     * @return Closure
56886b77daSGreg Roach     */
57d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
58886b77daSGreg Roach    {
59*6b9cb339SGreg Roach        return Registry::individualFactory()->mapper($tree);
60886b77daSGreg Roach    }
61886b77daSGreg Roach
62886b77daSGreg Roach    /**
63c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
64c156e8f5SGreg Roach     *
65c156e8f5SGreg Roach     * @return Closure
66c156e8f5SGreg Roach     */
67c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
68c156e8f5SGreg Roach    {
696c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
70c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
71c156e8f5SGreg Roach        };
72c156e8f5SGreg Roach    }
73c156e8f5SGreg Roach
74c156e8f5SGreg Roach    /**
75c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
76c156e8f5SGreg Roach     *
77c156e8f5SGreg Roach     * @return Closure
78c156e8f5SGreg Roach     */
79c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
80c156e8f5SGreg Roach    {
816c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
82c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
83c156e8f5SGreg Roach        };
84c156e8f5SGreg Roach    }
85c156e8f5SGreg Roach
86c156e8f5SGreg Roach    /**
87e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
88e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
89e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
90e71ef9d2SGreg Roach     *
91bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
92bb03c9f0SGreg Roach     *
93e71ef9d2SGreg Roach     * @param string      $xref
94e71ef9d2SGreg Roach     * @param Tree        $tree
95e71ef9d2SGreg Roach     * @param string|null $gedcom
96e71ef9d2SGreg Roach     *
97e71ef9d2SGreg Roach     * @return Individual|null
98e71ef9d2SGreg Roach     */
99ffc0a61fSGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Individual
100c1010edaSGreg Roach    {
101*6b9cb339SGreg Roach        return Registry::individualFactory()->make($xref, $tree, $gedcom);
102e71ef9d2SGreg Roach    }
103e71ef9d2SGreg Roach
104e71ef9d2SGreg Roach    /**
105395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
106395f0fe0SGreg Roach     * Typically when we load families and their members.
107395f0fe0SGreg Roach     *
108395f0fe0SGreg Roach     * @param Tree     $tree
1094a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
11018d7a90dSGreg Roach     *
11118d7a90dSGreg Roach     * @return void
112395f0fe0SGreg Roach     */
1132e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
114c1010edaSGreg Roach    {
1152e5b4452SGreg Roach        $rows = DB::table('individuals')
1162e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1172e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1182e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1192e5b4452SGreg Roach            ->get();
120395f0fe0SGreg Roach
121395f0fe0SGreg Roach        foreach ($rows as $row) {
122*6b9cb339SGreg Roach            Registry::individualFactory()->make($row->xref, $tree, $row->gedcom);
123395f0fe0SGreg Roach        }
124395f0fe0SGreg Roach    }
125395f0fe0SGreg Roach
126395f0fe0SGreg Roach    /**
127a25f0a04SGreg Roach     * Can the name of this record be shown?
128a25f0a04SGreg Roach     *
12976692c8bSGreg Roach     * @param int|null $access_level
13076692c8bSGreg Roach     *
13176692c8bSGreg Roach     * @return bool
132a25f0a04SGreg Roach     */
13335584196SGreg Roach    public function canShowName(int $access_level = null): bool
134c1010edaSGreg Roach    {
135d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
1364b9ff166SGreg Roach
137518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
138a25f0a04SGreg Roach    }
139a25f0a04SGreg Roach
140a25f0a04SGreg Roach    /**
14176692c8bSGreg Roach     * Can this individual be shown?
142a25f0a04SGreg Roach     *
14376692c8bSGreg Roach     * @param int $access_level
14476692c8bSGreg Roach     *
14576692c8bSGreg Roach     * @return bool
146a25f0a04SGreg Roach     */
14735584196SGreg Roach    protected function canShowByType(int $access_level): bool
148c1010edaSGreg Roach    {
149a25f0a04SGreg Roach        // Dead people...
150518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
151a25f0a04SGreg Roach            $keep_alive             = false;
15254ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
153a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1548d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
155a25f0a04SGreg Roach                foreach ($matches as $match) {
156a25f0a04SGreg Roach                    $date = new Date($match[1]);
157a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
158a25f0a04SGreg Roach                        $keep_alive = true;
159a25f0a04SGreg Roach                        break;
160a25f0a04SGreg Roach                    }
161a25f0a04SGreg Roach                }
162a25f0a04SGreg Roach            }
16354ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
164a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1658d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
166a25f0a04SGreg Roach                foreach ($matches as $match) {
167a25f0a04SGreg Roach                    $date = new Date($match[1]);
168a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
169a25f0a04SGreg Roach                        $keep_alive = true;
170a25f0a04SGreg Roach                        break;
171a25f0a04SGreg Roach                    }
172a25f0a04SGreg Roach                }
173a25f0a04SGreg Roach            }
174a25f0a04SGreg Roach            if (!$keep_alive) {
175a25f0a04SGreg Roach                return true;
176a25f0a04SGreg Roach            }
177a25f0a04SGreg Roach        }
178a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
1797c4add84SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_PATH_LENGTH);
1807c4add84SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF);
1817c4add84SGreg Roach
18254ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1834b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
184a25f0a04SGreg Roach        }
185a25f0a04SGreg Roach
186a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1874b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
188a25f0a04SGreg Roach    }
189a25f0a04SGreg Roach
190a25f0a04SGreg Roach    /**
191a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
192a25f0a04SGreg Roach     *
193a25f0a04SGreg Roach     * @param Individual $target
194cbc1590aSGreg Roach     * @param int        $distance
195a25f0a04SGreg Roach     *
196cbc1590aSGreg Roach     * @return bool
197a25f0a04SGreg Roach     */
1988f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
199c1010edaSGreg Roach    {
200a25f0a04SGreg Roach        static $cache = null;
201a25f0a04SGreg Roach
202*6b9cb339SGreg Roach        $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $target->tree);
203a25f0a04SGreg Roach        if ($user_individual) {
204a25f0a04SGreg Roach            if (!$cache) {
20513abd6f3SGreg Roach                $cache = [
20613abd6f3SGreg Roach                    0 => [$user_individual],
20713abd6f3SGreg Roach                    1 => [],
20813abd6f3SGreg Roach                ];
2098d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
210dc124885SGreg Roach                    $family = $fact->target();
211e24444eeSGreg Roach                    if ($family instanceof Family) {
212a25f0a04SGreg Roach                        $cache[1][] = $family;
213a25f0a04SGreg Roach                    }
214a25f0a04SGreg Roach                }
215a25f0a04SGreg Roach            }
216a25f0a04SGreg Roach        } else {
217a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
218a25f0a04SGreg Roach            return true;
219a25f0a04SGreg Roach        }
220a25f0a04SGreg Roach
221a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
222a25f0a04SGreg Roach        $distance *= 2;
223a25f0a04SGreg Roach
224a25f0a04SGreg Roach        // Consider each path length in turn
225a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
226a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
227a25f0a04SGreg Roach                // We have already calculated all records with this length
228e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
229a25f0a04SGreg Roach                    return true;
230a25f0a04SGreg Roach                }
231a25f0a04SGreg Roach            } else {
232a25f0a04SGreg Roach                // Need to calculate these paths
23313abd6f3SGreg Roach                $cache[$n] = [];
234e364afe4SGreg Roach                if ($n % 2 === 0) {
235a25f0a04SGreg Roach                    // Add FAM->INDI links
236a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2378d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
238dc124885SGreg Roach                            $individual = $fact->target();
239a25f0a04SGreg Roach                            // Don’t backtrack
240e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
241a25f0a04SGreg Roach                                $cache[$n][] = $individual;
242a25f0a04SGreg Roach                            }
243a25f0a04SGreg Roach                        }
244a25f0a04SGreg Roach                    }
245a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
246a25f0a04SGreg Roach                        return true;
247a25f0a04SGreg Roach                    }
248a25f0a04SGreg Roach                } else {
249a25f0a04SGreg Roach                    // Add INDI->FAM links
250a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2518d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
252dc124885SGreg Roach                            $family = $fact->target();
253a25f0a04SGreg Roach                            // Don’t backtrack
254e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
255a25f0a04SGreg Roach                                $cache[$n][] = $family;
256a25f0a04SGreg Roach                            }
257a25f0a04SGreg Roach                        }
258a25f0a04SGreg Roach                    }
259a25f0a04SGreg Roach                }
260a25f0a04SGreg Roach            }
261a25f0a04SGreg Roach        }
262a25f0a04SGreg Roach
263a25f0a04SGreg Roach        return false;
264a25f0a04SGreg Roach    }
265a25f0a04SGreg Roach
26676692c8bSGreg Roach    /**
26776692c8bSGreg Roach     * Generate a private version of this record
26876692c8bSGreg Roach     *
26976692c8bSGreg Roach     * @param int $access_level
27076692c8bSGreg Roach     *
27176692c8bSGreg Roach     * @return string
27276692c8bSGreg Roach     */
2733c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
274c1010edaSGreg Roach    {
275acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
276a25f0a04SGreg Roach
277a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
278518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
279a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2808d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
281138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
282a25f0a04SGreg Roach            }
283a25f0a04SGreg Roach        }
284a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2858d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
286a25f0a04SGreg Roach        foreach ($matches as $match) {
287*6b9cb339SGreg Roach            $rela = Registry::familyFactory()->make($match[1], $this->tree);
288a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
289a25f0a04SGreg Roach                $rec .= $match[0];
290a25f0a04SGreg Roach            }
291a25f0a04SGreg Roach        }
292a25f0a04SGreg Roach        // Don’t privatize sex.
293a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
294a25f0a04SGreg Roach            $rec .= $match[0];
295a25f0a04SGreg Roach        }
296a25f0a04SGreg Roach
297a25f0a04SGreg Roach        return $rec;
298a25f0a04SGreg Roach    }
299a25f0a04SGreg Roach
30076692c8bSGreg Roach    /**
301a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
302a25f0a04SGreg Roach     * If not known to be dead, then assume living.
303a25f0a04SGreg Roach     *
304cbc1590aSGreg Roach     * @return bool
305a25f0a04SGreg Roach     */
3068f53f488SRico Sonntag    public function isDead(): bool
307c1010edaSGreg Roach    {
308c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3094459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
310a25f0a04SGreg Roach
311a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3128d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
313a25f0a04SGreg Roach            return true;
314a25f0a04SGreg Roach        }
315a25f0a04SGreg Roach
316a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
317a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
318a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
319a25f0a04SGreg Roach                $date = new Date($date_match);
320269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
321a25f0a04SGreg Roach                    return true;
322a25f0a04SGreg Roach                }
323a25f0a04SGreg Roach            }
324a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
325a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
326a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
327a25f0a04SGreg Roach                return false;
328a25f0a04SGreg Roach            }
329a25f0a04SGreg Roach        }
330a25f0a04SGreg Roach
331a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
332a25f0a04SGreg Roach
333a25f0a04SGreg Roach        // Check parents (birth and adopted)
33439ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
33539ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
336a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
337a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
338a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
339a25f0a04SGreg Roach                    $date = new Date($date_match);
340269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
341a25f0a04SGreg Roach                        return true;
342a25f0a04SGreg Roach                    }
343a25f0a04SGreg Roach                }
344a25f0a04SGreg Roach            }
345a25f0a04SGreg Roach        }
346a25f0a04SGreg Roach
347a25f0a04SGreg Roach        // Check spouses
34839ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
349a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
350a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
351a25f0a04SGreg Roach                $date = new Date($date_match);
352a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
353269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
354a25f0a04SGreg Roach                    return true;
355a25f0a04SGreg Roach                }
356a25f0a04SGreg Roach            }
357a25f0a04SGreg Roach            // Check spouse dates
35839ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
359a25f0a04SGreg Roach            if ($spouse) {
360a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
361a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
362a25f0a04SGreg Roach                    $date = new Date($date_match);
363a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
364269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
365a25f0a04SGreg Roach                        return true;
366a25f0a04SGreg Roach                    }
367a25f0a04SGreg Roach                }
368a25f0a04SGreg Roach            }
369a25f0a04SGreg Roach            // Check child dates
37039ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
371a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
372a25f0a04SGreg Roach                // Assume children born after age of 15
373a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
374a25f0a04SGreg Roach                    $date = new Date($date_match);
375269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
376a25f0a04SGreg Roach                        return true;
377a25f0a04SGreg Roach                    }
378a25f0a04SGreg Roach                }
379a25f0a04SGreg Roach                // Check grandchildren
38039ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
38139ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
382a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
383a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
384a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
385a25f0a04SGreg Roach                            $date = new Date($date_match);
386269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
387a25f0a04SGreg Roach                                return true;
388a25f0a04SGreg Roach                            }
389a25f0a04SGreg Roach                        }
390a25f0a04SGreg Roach                    }
391a25f0a04SGreg Roach                }
392a25f0a04SGreg Roach            }
393a25f0a04SGreg Roach        }
394a25f0a04SGreg Roach
395a25f0a04SGreg Roach        return false;
396a25f0a04SGreg Roach    }
397a25f0a04SGreg Roach
398a25f0a04SGreg Roach    /**
399a25f0a04SGreg Roach     * Find the highlighted media object for an individual
400a25f0a04SGreg Roach     *
401e364afe4SGreg Roach     * @return MediaFile|null
402a25f0a04SGreg Roach     */
403e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
404c1010edaSGreg Roach    {
40592647683SGreg Roach        $fact = $this->facts(['OBJE'])
40619d319e7SGreg Roach            ->first(static function (Fact $fact): bool {
407dc124885SGreg Roach                $media = $fact->target();
40819d319e7SGreg Roach
40919d319e7SGreg Roach                return $media instanceof Media && $media->firstImageFile() instanceof MediaFile;
41019d319e7SGreg Roach            });
41119d319e7SGreg Roach
41292647683SGreg Roach        if ($fact instanceof Fact && $fact->target() instanceof Media) {
41392647683SGreg Roach            return $fact->target()->firstImageFile();
414a25f0a04SGreg Roach        }
415a25f0a04SGreg Roach
416a25f0a04SGreg Roach        return null;
417a25f0a04SGreg Roach    }
418a25f0a04SGreg Roach
419a25f0a04SGreg Roach    /**
420a25f0a04SGreg Roach     * Display the prefered image for this individual.
421a25f0a04SGreg Roach     * Use an icon if no image is available.
422a25f0a04SGreg Roach     *
423dce07401SGreg Roach     * @param int      $width      Pixels
424dce07401SGreg Roach     * @param int      $height     Pixels
425dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
426dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
427dce07401SGreg Roach     *
428a25f0a04SGreg Roach     * @return string
429a25f0a04SGreg Roach     */
4308f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
431c1010edaSGreg Roach    {
4324a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4334a9f750fSGreg Roach
4344a9f750fSGreg Roach        if ($media_file !== null) {
4354a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
436a25f0a04SGreg Roach        }
4374a9f750fSGreg Roach
4384a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
43939ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4404a9f750fSGreg Roach        }
4414a9f750fSGreg Roach
4424a9f750fSGreg Roach        return '';
443a25f0a04SGreg Roach    }
444a25f0a04SGreg Roach
445a25f0a04SGreg Roach    /**
446a25f0a04SGreg Roach     * Get the date of birth
447a25f0a04SGreg Roach     *
448a25f0a04SGreg Roach     * @return Date
449a25f0a04SGreg Roach     */
4508f53f488SRico Sonntag    public function getBirthDate(): Date
451c1010edaSGreg Roach    {
452a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
453a25f0a04SGreg Roach            if ($date->isOK()) {
454a25f0a04SGreg Roach                return $date;
455a25f0a04SGreg Roach            }
456a25f0a04SGreg Roach        }
457a25f0a04SGreg Roach
458a25f0a04SGreg Roach        return new Date('');
459a25f0a04SGreg Roach    }
460a25f0a04SGreg Roach
461a25f0a04SGreg Roach    /**
462a25f0a04SGreg Roach     * Get the place of birth
463a25f0a04SGreg Roach     *
46416d0b7f7SRico Sonntag     * @return Place
465a25f0a04SGreg Roach     */
4668f53f488SRico Sonntag    public function getBirthPlace(): Place
467c1010edaSGreg Roach    {
468a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
469a25f0a04SGreg Roach            return $place;
470a25f0a04SGreg Roach        }
471a25f0a04SGreg Roach
472b20ddbf9SGreg Roach        return new Place('', $this->tree);
473a25f0a04SGreg Roach    }
474a25f0a04SGreg Roach
475a25f0a04SGreg Roach    /**
476a25f0a04SGreg Roach     * Get the year of birth
477a25f0a04SGreg Roach     *
478a25f0a04SGreg Roach     * @return string the year of birth
479a25f0a04SGreg Roach     */
4808f53f488SRico Sonntag    public function getBirthYear(): string
481c1010edaSGreg Roach    {
482f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
483a25f0a04SGreg Roach    }
484a25f0a04SGreg Roach
485a25f0a04SGreg Roach    /**
486a25f0a04SGreg Roach     * Get the date of death
487a25f0a04SGreg Roach     *
488a25f0a04SGreg Roach     * @return Date
489a25f0a04SGreg Roach     */
4908f53f488SRico Sonntag    public function getDeathDate(): Date
491c1010edaSGreg Roach    {
492a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
493a25f0a04SGreg Roach            if ($date->isOK()) {
494a25f0a04SGreg Roach                return $date;
495a25f0a04SGreg Roach            }
496a25f0a04SGreg Roach        }
497a25f0a04SGreg Roach
498a25f0a04SGreg Roach        return new Date('');
499a25f0a04SGreg Roach    }
500a25f0a04SGreg Roach
501a25f0a04SGreg Roach    /**
502a25f0a04SGreg Roach     * Get the place of death
503a25f0a04SGreg Roach     *
50416d0b7f7SRico Sonntag     * @return Place
505a25f0a04SGreg Roach     */
5068f53f488SRico Sonntag    public function getDeathPlace(): Place
507c1010edaSGreg Roach    {
508a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
509a25f0a04SGreg Roach            return $place;
510a25f0a04SGreg Roach        }
511a25f0a04SGreg Roach
512b20ddbf9SGreg Roach        return new Place('', $this->tree);
513a25f0a04SGreg Roach    }
514a25f0a04SGreg Roach
515a25f0a04SGreg Roach    /**
516a25f0a04SGreg Roach     * get the death year
517a25f0a04SGreg Roach     *
518a25f0a04SGreg Roach     * @return string the year of death
519a25f0a04SGreg Roach     */
5208f53f488SRico Sonntag    public function getDeathYear(): string
521c1010edaSGreg Roach    {
522f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
523a25f0a04SGreg Roach    }
524a25f0a04SGreg Roach
525a25f0a04SGreg Roach    /**
526a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
52715d603e7SGreg Roach     * Provide the place and full date using a tooltip.
528a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
529a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
530a25f0a04SGreg Roach     *
531a25f0a04SGreg Roach     * @return string
532a25f0a04SGreg Roach     */
5335e6816beSGreg Roach    public function lifespan(): string
534c1010edaSGreg Roach    {
53515d603e7SGreg Roach        // Just the first part of the place name
536392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
537392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
53815d603e7SGreg Roach        // Remove markup from dates
53915d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
54015d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
54115d603e7SGreg Roach
542c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
543dacfe33cSGreg Roach        return I18N::translate(
544a25f0a04SGreg Roach            '%1$s–%2$s',
5452d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5462d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
547a25f0a04SGreg Roach        );
548a25f0a04SGreg Roach    }
549a25f0a04SGreg Roach
550a25f0a04SGreg Roach    /**
551a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
552a25f0a04SGreg Roach     *
553a25f0a04SGreg Roach     * @return Date[]
554a25f0a04SGreg Roach     */
5558f53f488SRico Sonntag    public function getAllBirthDates(): array
556c1010edaSGreg Roach    {
5578d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5588d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
559a25f0a04SGreg Roach            if ($tmp) {
560a25f0a04SGreg Roach                return $tmp;
561a25f0a04SGreg Roach            }
562a25f0a04SGreg Roach        }
563a25f0a04SGreg Roach
56413abd6f3SGreg Roach        return [];
565a25f0a04SGreg Roach    }
566a25f0a04SGreg Roach
567a25f0a04SGreg Roach    /**
568a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
569a25f0a04SGreg Roach     *
5704080d558SGreg Roach     * @return Place[]
571a25f0a04SGreg Roach     */
5728f53f488SRico Sonntag    public function getAllBirthPlaces(): array
573c1010edaSGreg Roach    {
5748d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5758d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
57654c1ab5eSGreg Roach            if ($places !== []) {
5774080d558SGreg Roach                return $places;
578a25f0a04SGreg Roach            }
579a25f0a04SGreg Roach        }
580a25f0a04SGreg Roach
58113abd6f3SGreg Roach        return [];
582a25f0a04SGreg Roach    }
583a25f0a04SGreg Roach
584a25f0a04SGreg Roach    /**
585a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
586a25f0a04SGreg Roach     *
587a25f0a04SGreg Roach     * @return Date[]
588a25f0a04SGreg Roach     */
5898f53f488SRico Sonntag    public function getAllDeathDates(): array
590c1010edaSGreg Roach    {
5918d0ebef0SGreg Roach        foreach (Gedcom::DEATH_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     * Get all the death places - for the individual lists.
603a25f0a04SGreg Roach     *
6044080d558SGreg Roach     * @return Place[]
605a25f0a04SGreg Roach     */
6068f53f488SRico Sonntag    public function getAllDeathPlaces(): array
607c1010edaSGreg Roach    {
6088d0ebef0SGreg Roach        foreach (Gedcom::DEATH_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     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
620a25f0a04SGreg Roach     *
621a25f0a04SGreg Roach     * @return Date
622a25f0a04SGreg Roach     */
6238f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
624c1010edaSGreg Roach    {
6258f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
626a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
627a25f0a04SGreg Roach                if ($date->isOK()) {
6284686330aSGreg Roach                    $this->estimated_birth_date = $date;
629a25f0a04SGreg Roach                    break;
630a25f0a04SGreg Roach                }
631a25f0a04SGreg Roach            }
6328f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
63313abd6f3SGreg Roach                $min = [];
63413abd6f3SGreg Roach                $max = [];
635a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
636f5b60decSGreg Roach                if ($tmp->isOK()) {
637f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
638f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
639a25f0a04SGreg Roach                }
64039ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
641a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
642f5b60decSGreg Roach                    if ($tmp->isOK()) {
643f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
644f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
645a25f0a04SGreg Roach                    }
64639ca88baSGreg Roach                    $husband = $family->husband();
647e364afe4SGreg Roach                    if ($husband instanceof self) {
6482e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
649f5b60decSGreg Roach                        if ($tmp->isOK()) {
650f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
651f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
652a25f0a04SGreg Roach                        }
653a25f0a04SGreg Roach                    }
65439ca88baSGreg Roach                    $wife = $family->wife();
655e364afe4SGreg Roach                    if ($wife instanceof self) {
6562e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
657f5b60decSGreg Roach                        if ($tmp->isOK()) {
658f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
659f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
660a25f0a04SGreg Roach                        }
661a25f0a04SGreg Roach                    }
66239ca88baSGreg Roach                    foreach ($family->children() as $child) {
663a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
664f5b60decSGreg Roach                        if ($tmp->isOK()) {
665f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
666f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
667a25f0a04SGreg Roach                        }
668a25f0a04SGreg Roach                    }
669a25f0a04SGreg Roach                }
67039ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
671a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
672f5b60decSGreg Roach                    if ($tmp->isOK()) {
673f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
674f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
675a25f0a04SGreg Roach                    }
67639ca88baSGreg Roach                    $spouse = $family->spouse($this);
677a25f0a04SGreg Roach                    if ($spouse) {
678a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
679f5b60decSGreg Roach                        if ($tmp->isOK()) {
680f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
681f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
682a25f0a04SGreg Roach                        }
683a25f0a04SGreg Roach                    }
68439ca88baSGreg Roach                    foreach ($family->children() as $child) {
685a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
686f5b60decSGreg Roach                        if ($tmp->isOK()) {
687e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
688f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
689a25f0a04SGreg Roach                        }
690a25f0a04SGreg Roach                    }
691a25f0a04SGreg Roach                }
692a25f0a04SGreg Roach                if ($min && $max) {
69359f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
694a25f0a04SGreg Roach
69565e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
6964686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
697a25f0a04SGreg Roach                } else {
6984686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
699a25f0a04SGreg Roach                }
700a25f0a04SGreg Roach            }
701a25f0a04SGreg Roach        }
702a25f0a04SGreg Roach
7034686330aSGreg Roach        return $this->estimated_birth_date;
704a25f0a04SGreg Roach    }
705a25f0a04SGreg Roach
706a25f0a04SGreg Roach    /**
707a25f0a04SGreg Roach     * Generate an estimated date of death.
708a25f0a04SGreg Roach     *
709a25f0a04SGreg Roach     * @return Date
710a25f0a04SGreg Roach     */
7118f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
712c1010edaSGreg Roach    {
7134686330aSGreg Roach        if ($this->estimated_death_date === null) {
714a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
715a25f0a04SGreg Roach                if ($date->isOK()) {
7164686330aSGreg Roach                    $this->estimated_death_date = $date;
717a25f0a04SGreg Roach                    break;
718a25f0a04SGreg Roach                }
719a25f0a04SGreg Roach            }
7204686330aSGreg Roach            if ($this->estimated_death_date === null) {
721f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
722c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7234686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
724a25f0a04SGreg Roach                } else {
7254686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
726a25f0a04SGreg Roach                }
727a25f0a04SGreg Roach            }
728a25f0a04SGreg Roach        }
729a25f0a04SGreg Roach
7304686330aSGreg Roach        return $this->estimated_death_date;
731a25f0a04SGreg Roach    }
732a25f0a04SGreg Roach
733a25f0a04SGreg Roach    /**
734a25f0a04SGreg Roach     * Get the sex - M F or U
735a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
736a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
737a25f0a04SGreg Roach     *
738a25f0a04SGreg Roach     * @return string
739a25f0a04SGreg Roach     */
740e364afe4SGreg Roach    public function sex(): string
741c1010edaSGreg Roach    {
742a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
743a25f0a04SGreg Roach            return $match[1];
744a25f0a04SGreg Roach        }
745b2ce94c6SRico Sonntag
746b2ce94c6SRico Sonntag        return 'U';
747a25f0a04SGreg Roach    }
748a25f0a04SGreg Roach
749a25f0a04SGreg Roach    /**
750a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
751a25f0a04SGreg Roach     *
752a25f0a04SGreg Roach     * @return string
753a25f0a04SGreg Roach     */
7548f53f488SRico Sonntag    public function getBoxStyle(): string
755c1010edaSGreg Roach    {
756c1010edaSGreg Roach        $tmp = [
757c1010edaSGreg Roach            'M' => '',
758c1010edaSGreg Roach            'F' => 'F',
759c1010edaSGreg Roach            'U' => 'NN',
760c1010edaSGreg Roach        ];
761a25f0a04SGreg Roach
76239ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
763a25f0a04SGreg Roach    }
764a25f0a04SGreg Roach
765a25f0a04SGreg Roach    /**
766a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
767a25f0a04SGreg Roach     *
768cbc1590aSGreg Roach     * @param int|null $access_level
769a25f0a04SGreg Roach     *
770b5c8fd7eSGreg Roach     * @return Collection<Family>
771a25f0a04SGreg Roach     */
77239ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
773c1010edaSGreg Roach    {
774d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
775d9e083e7SGreg Roach
776d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
777d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
7784b9ff166SGreg Roach        }
7794b9ff166SGreg Roach
78039ca88baSGreg Roach        $families = new Collection();
781d9e083e7SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level) as $fact) {
782dc124885SGreg Roach            $family = $fact->target();
783d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
78439ca88baSGreg Roach                $families->push($family);
785a25f0a04SGreg Roach            }
786a25f0a04SGreg Roach        }
787a25f0a04SGreg Roach
78839ca88baSGreg Roach        return new Collection($families);
789a25f0a04SGreg Roach    }
790a25f0a04SGreg Roach
791a25f0a04SGreg Roach    /**
792a25f0a04SGreg Roach     * Get the current spouse of this individual.
793a25f0a04SGreg Roach     *
794a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
795a25f0a04SGreg Roach     * in chronological order, and take the last one found.
796a25f0a04SGreg Roach     *
797a25f0a04SGreg Roach     * @return Individual|null
798a25f0a04SGreg Roach     */
799e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
800c1010edaSGreg Roach    {
80139ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
80239ca88baSGreg Roach
80339ca88baSGreg Roach        if ($family instanceof Family) {
80439ca88baSGreg Roach            return $family->spouse($this);
805a25f0a04SGreg Roach        }
806b2ce94c6SRico Sonntag
807b2ce94c6SRico Sonntag        return null;
808a25f0a04SGreg Roach    }
809a25f0a04SGreg Roach
810a25f0a04SGreg Roach    /**
811a25f0a04SGreg Roach     * Count the children belonging to this individual.
812a25f0a04SGreg Roach     *
813cbc1590aSGreg Roach     * @return int
814a25f0a04SGreg Roach     */
815e364afe4SGreg Roach    public function numberOfChildren(): int
816c1010edaSGreg Roach    {
8177d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8183dc7bbe9SGreg Roach            return (int) $match[1];
819b2ce94c6SRico Sonntag        }
820b2ce94c6SRico Sonntag
82113abd6f3SGreg Roach        $children = [];
82239ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
82339ca88baSGreg Roach            foreach ($fam->children() as $child) {
824c0935879SGreg Roach                $children[$child->xref()] = true;
825a25f0a04SGreg Roach            }
826a25f0a04SGreg Roach        }
827a25f0a04SGreg Roach
828a25f0a04SGreg Roach        return count($children);
829a25f0a04SGreg Roach    }
830a25f0a04SGreg Roach
831a25f0a04SGreg Roach    /**
832a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
833a25f0a04SGreg Roach     *
834cbc1590aSGreg Roach     * @param int|null $access_level
835a25f0a04SGreg Roach     *
836b5c8fd7eSGreg Roach     * @return Collection<Family>
837a25f0a04SGreg Roach     */
83839ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
839c1010edaSGreg Roach    {
840d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
8414b9ff166SGreg Roach
842d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
843d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
844d9e083e7SGreg Roach        }
845a25f0a04SGreg Roach
84639ca88baSGreg Roach        $families = new Collection();
84739ca88baSGreg Roach
848d9e083e7SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level) as $fact) {
849dc124885SGreg Roach            $family = $fact->target();
850d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
85139ca88baSGreg Roach                $families->push($family);
852a25f0a04SGreg Roach            }
853a25f0a04SGreg Roach        }
854a25f0a04SGreg Roach
855a25f0a04SGreg Roach        return $families;
856a25f0a04SGreg Roach    }
857a25f0a04SGreg Roach
858a25f0a04SGreg Roach    /**
859a25f0a04SGreg Roach     * Get a list of step-parent families.
860a25f0a04SGreg Roach     *
861b5c8fd7eSGreg Roach     * @return Collection<Family>
862a25f0a04SGreg Roach     */
863820b62dfSGreg Roach    public function childStepFamilies(): Collection
864c1010edaSGreg Roach    {
865ed5b6227SGreg Roach        $step_families = new Collection();
86639ca88baSGreg Roach        $families      = $this->childFamilies();
867a25f0a04SGreg Roach        foreach ($families as $family) {
868ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
869ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
87039ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
871ed5b6227SGreg Roach                        $step_families->add($step_family);
872a25f0a04SGreg Roach                    }
873a25f0a04SGreg Roach                }
874a25f0a04SGreg Roach            }
875a25f0a04SGreg Roach        }
876a25f0a04SGreg Roach
8778c627a69SGreg Roach        return $step_families->uniqueStrict(static function (Family $family): string {
8788c627a69SGreg Roach            return $family->xref();
8798c627a69SGreg Roach        });
880a25f0a04SGreg Roach    }
881a25f0a04SGreg Roach
882a25f0a04SGreg Roach    /**
883a25f0a04SGreg Roach     * Get a list of step-parent families.
884a25f0a04SGreg Roach     *
885b5c8fd7eSGreg Roach     * @return Collection<Family>
886a25f0a04SGreg Roach     */
887820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
888c1010edaSGreg Roach    {
88913abd6f3SGreg Roach        $step_families = [];
89039ca88baSGreg Roach        $families      = $this->spouseFamilies();
891820b62dfSGreg Roach
892a25f0a04SGreg Roach        foreach ($families as $family) {
89339ca88baSGreg Roach            $spouse = $family->spouse($this);
894820b62dfSGreg Roach
895b5c8fd7eSGreg Roach            if ($spouse instanceof Individual) {
89639ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
89739ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
898a25f0a04SGreg Roach                        $step_families[] = $step_family;
899a25f0a04SGreg Roach                    }
900a25f0a04SGreg Roach                }
901a25f0a04SGreg Roach            }
902a25f0a04SGreg Roach        }
903a25f0a04SGreg Roach
904820b62dfSGreg Roach        return new Collection($step_families);
905a25f0a04SGreg Roach    }
906a25f0a04SGreg Roach
907a25f0a04SGreg Roach    /**
908a25f0a04SGreg Roach     * A label for a parental family group
909a25f0a04SGreg Roach     *
910a25f0a04SGreg Roach     * @param Family $family
911a25f0a04SGreg Roach     *
912a25f0a04SGreg Roach     * @return string
913a25f0a04SGreg Roach     */
914820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
915c1010edaSGreg Roach    {
9167d70e4a7SGreg Roach        preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match);
917b2ce94c6SRico Sonntag
9187d70e4a7SGreg Roach        $values = [
9197d70e4a7SGreg Roach            'birth'   => I18N::translate('Family with parents'),
9207d70e4a7SGreg Roach            'adopted' => I18N::translate('Family with adoptive parents'),
9217d70e4a7SGreg Roach            'foster'  => I18N::translate('Family with foster parents'),
9227d70e4a7SGreg Roach            'sealing' => /* I18N: “sealing” is a Mormon ceremony. */
9237d70e4a7SGreg Roach                I18N::translate('Family with sealing parents'),
9247d70e4a7SGreg Roach            'rada'    => /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */
9257d70e4a7SGreg Roach                I18N::translate('Family with rada parents'),
9267d70e4a7SGreg Roach        ];
9277d70e4a7SGreg Roach
9287d70e4a7SGreg Roach        return $values[$match[1] ?? 'birth'] ?? $values['birth'];
929a25f0a04SGreg Roach    }
930a25f0a04SGreg Roach
931a25f0a04SGreg Roach    /**
932a25f0a04SGreg Roach     * Create a label for a step family
933a25f0a04SGreg Roach     *
934a25f0a04SGreg Roach     * @param Family $step_family
935a25f0a04SGreg Roach     *
936a25f0a04SGreg Roach     * @return string
937a25f0a04SGreg Roach     */
9388f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
939c1010edaSGreg Roach    {
94039ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
941a25f0a04SGreg Roach            if ($family !== $step_family) {
942a25f0a04SGreg Roach                // Must be a step-family
94339ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
94439ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
945a25f0a04SGreg Roach                        if ($parent === $step_parent) {
946a25f0a04SGreg Roach                            // One common parent - must be a step family
947e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
948a25f0a04SGreg Roach                                // Father’s family with someone else
94939ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
950a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
95139ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
952b2ce94c6SRico Sonntag                                }
953b2ce94c6SRico Sonntag
954a25f0a04SGreg Roach                                /* I18N: A step-family. */
955bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
956a25f0a04SGreg Roach                            }
957b2ce94c6SRico Sonntag
958a25f0a04SGreg Roach                            // Mother’s family with someone else
95939ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
960a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
96139ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
962b2ce94c6SRico Sonntag                            }
963b2ce94c6SRico Sonntag
964a25f0a04SGreg Roach                            /* I18N: A step-family. */
965bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
966a25f0a04SGreg Roach                        }
967a25f0a04SGreg Roach                    }
968a25f0a04SGreg Roach                }
969a25f0a04SGreg Roach            }
970a25f0a04SGreg Roach        }
971a25f0a04SGreg Roach
972a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
973a25f0a04SGreg Roach        return I18N::translate('Family with parents');
974a25f0a04SGreg Roach    }
975a25f0a04SGreg Roach
976225e381fSGreg Roach    /**
977225e381fSGreg Roach     * Get the description for the family.
978225e381fSGreg Roach     *
979225e381fSGreg Roach     * For example, "XXX's family with new wife".
980225e381fSGreg Roach     *
981225e381fSGreg Roach     * @param Family $family
982225e381fSGreg Roach     *
983225e381fSGreg Roach     * @return string
984225e381fSGreg Roach     */
985e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
986c1010edaSGreg Roach    {
98739ca88baSGreg Roach        $spouse = $family->spouse($this);
988225e381fSGreg Roach        if ($spouse) {
989225e381fSGreg Roach            /* I18N: %s is the spouse name */
99039ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
991225e381fSGreg Roach        }
992b2ce94c6SRico Sonntag
99339ca88baSGreg Roach        return $family->fullName();
994225e381fSGreg Roach    }
995225e381fSGreg Roach
996a25f0a04SGreg Roach    /**
997961ec755SGreg Roach     * If this object has no name, what do we call it?
998961ec755SGreg Roach     *
999961ec755SGreg Roach     * @return string
1000961ec755SGreg Roach     */
10018f53f488SRico Sonntag    public function getFallBackName(): string
1002c1010edaSGreg Roach    {
1003a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1004a25f0a04SGreg Roach    }
1005a25f0a04SGreg Roach
1006a25f0a04SGreg Roach    /**
1007a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1008a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1009a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1010a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1011a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1012a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1013a25f0a04SGreg Roach     *
1014a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1015a25f0a04SGreg Roach     * 2 GIVN Robert
1016a25f0a04SGreg Roach     * 2 SPFX de
1017a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1018a25f0a04SGreg Roach     * 2 NICK The Bald
1019a25f0a04SGreg Roach     *
1020a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1021a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1022a25f0a04SGreg Roach     *
1023a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1024a25f0a04SGreg Roach     *
1025a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1026a25f0a04SGreg Roach     * or
1027a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1028a25f0a04SGreg Roach     * 2 GIVN Carlos
1029a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1030a25f0a04SGreg Roach     *
1031a25f0a04SGreg Roach     * @param string $type
1032a25f0a04SGreg Roach     * @param string $full
1033a25f0a04SGreg Roach     * @param string $gedcom
1034e364afe4SGreg Roach     *
1035e364afe4SGreg Roach     * @return void
1036a25f0a04SGreg Roach     */
1037e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1038c1010edaSGreg Roach    {
1039a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1040a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1041a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1042a25f0a04SGreg Roach
104376f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1044a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1045a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1046a25f0a04SGreg Roach
1047a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
104876f666f4SGreg Roach        if ($SURN !== '') {
1049a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1050a25f0a04SGreg Roach        } else {
105113abd6f3SGreg Roach            $SURNS = [];
1052a25f0a04SGreg Roach        }
105376f666f4SGreg Roach
1054a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1055a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1056a25f0a04SGreg Roach
1057a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1058a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1059a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1060a25f0a04SGreg Roach
1061a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
106276f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1063e364afe4SGreg Roach            $full .= '/';
1064a25f0a04SGreg Roach        }
1065a25f0a04SGreg Roach
1066a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1067a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1068a25f0a04SGreg Roach
1069a25f0a04SGreg Roach        // Extract the surname.
1070a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1071a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1072a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1073a25f0a04SGreg Roach        } else {
1074a25f0a04SGreg Roach            $surname = '';
1075a25f0a04SGreg Roach        }
1076a25f0a04SGreg Roach
1077a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1078a25f0a04SGreg Roach        if (!$SURNS) {
1079a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1080a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1081a25f0a04SGreg Roach                $SURNS = $matches[1];
1082a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1083a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1084a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1085a25f0a04SGreg Roach                }
1086a25f0a04SGreg Roach            } else {
1087a25f0a04SGreg Roach                // It is valid not to have a surname at all
108813abd6f3SGreg Roach                $SURNS = [''];
1089a25f0a04SGreg Roach            }
1090a25f0a04SGreg Roach        }
1091a25f0a04SGreg Roach
1092a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1093a25f0a04SGreg Roach        if (!$GIVN) {
1094a25f0a04SGreg Roach            $GIVN = preg_replace(
109513abd6f3SGreg Roach                [
1096c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1097c1010edaSGreg Roach                    // remove surname
1098c1010edaSGreg Roach                    '/ ?".+"/',
1099c1010edaSGreg Roach                    // remove nickname
1100c1010edaSGreg Roach                    '/ {2,}/',
1101c1010edaSGreg Roach                    // multiple spaces, caused by the above
1102c1010edaSGreg Roach                    '/^ | $/',
1103c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
110413abd6f3SGreg Roach                ],
110513abd6f3SGreg Roach                [
1106a25f0a04SGreg Roach                    ' ',
1107a25f0a04SGreg Roach                    ' ',
1108a25f0a04SGreg Roach                    ' ',
1109a25f0a04SGreg Roach                    '',
111013abd6f3SGreg Roach                ],
1111a25f0a04SGreg Roach                $full
1112a25f0a04SGreg Roach            );
1113a25f0a04SGreg Roach        }
1114a25f0a04SGreg Roach
1115a25f0a04SGreg Roach        // Add placeholder for unknown given name
1116a25f0a04SGreg Roach        if (!$GIVN) {
1117a25f0a04SGreg Roach            $GIVN = '@P.N.';
111873f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1119a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1120a25f0a04SGreg Roach        }
1121a25f0a04SGreg Roach
1122a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1123a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1124a25f0a04SGreg Roach        // $full is for display on-screen
1125a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1126a25f0a04SGreg Roach
1127a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1128ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1129ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1130c6f196c3SGreg Roach        // Format for display
1131d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1132acc34ea1SGreg Roach        // Localise quotation marks around the nickname
11330b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
1134c652cdbdSGreg Roach            return '<q class="wt-nickname">' . $matches[1] . '</q>';
11358d68cabeSGreg Roach        }, $full);
1136a25f0a04SGreg Roach
1137c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1138a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1139a25f0a04SGreg Roach
1140a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1141a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1142a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1143a25f0a04SGreg Roach
1144ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1145a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1146e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1147a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1148e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1149a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1150a25f0a04SGreg Roach            }
1151a25f0a04SGreg Roach
1152bdb3725aSGreg Roach            $this->getAllNames[] = [
1153a25f0a04SGreg Roach                'type'    => $type,
1154a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1155c1010edaSGreg Roach                'full'    => $full,
1156c1010edaSGreg Roach                // This is used for display
1157c1010edaSGreg Roach                'fullNN'  => $fullNN,
1158c1010edaSGreg Roach                // This goes into the database
1159c1010edaSGreg Roach                'surname' => $surname,
1160c1010edaSGreg Roach                // This goes into the database
1161c1010edaSGreg Roach                'givn'    => $GIVN,
1162c1010edaSGreg Roach                // This goes into the database
1163c1010edaSGreg Roach                'surn'    => $SURN,
1164c1010edaSGreg Roach                // This goes into the database
116513abd6f3SGreg Roach            ];
1166a25f0a04SGreg Roach        }
1167a25f0a04SGreg Roach    }
1168a25f0a04SGreg Roach
1169a25f0a04SGreg Roach    /**
117076692c8bSGreg Roach     * Extract names from the GEDCOM record.
1171c7ff4153SGreg Roach     *
1172c7ff4153SGreg Roach     * @return void
1173a25f0a04SGreg Roach     */
1174e364afe4SGreg Roach    public function extractNames(): void
1175c1010edaSGreg Roach    {
1176d9e083e7SGreg Roach        $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree);
1177d9e083e7SGreg Roach
11788f53f488SRico Sonntag        $this->extractNamesFromFacts(
11798f53f488SRico Sonntag            1,
11808f53f488SRico Sonntag            'NAME',
1181d9e083e7SGreg Roach            $this->facts(['NAME'], false, $access_level)
11828f53f488SRico Sonntag        );
1183a25f0a04SGreg Roach    }
1184a25f0a04SGreg Roach
1185a25f0a04SGreg Roach    /**
1186a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1187a25f0a04SGreg Roach     * selection items or favorites.
1188a25f0a04SGreg Roach     *
1189a25f0a04SGreg Roach     * @return string
1190a25f0a04SGreg Roach     */
11918f53f488SRico Sonntag    public function formatListDetails(): string
1192c1010edaSGreg Roach    {
1193a25f0a04SGreg Roach        return
11948d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
11958d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1196a25f0a04SGreg Roach    }
1197a25f0a04SGreg Roach}
1198