xref: /webtrees/app/Individual.php (revision bb03c9f048b83092098d5e46c2ab323ae7e2b314)
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;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
25852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
262e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2739ca88baSGreg Roachuse Illuminate\Support\Collection;
28a25f0a04SGreg Roach
29a25f0a04SGreg Roach/**
3076692c8bSGreg Roach * A GEDCOM individual (INDI) object.
31a25f0a04SGreg Roach */
32c1010edaSGreg Roachclass Individual extends GedcomRecord
33c1010edaSGreg Roach{
3416d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3516d6367aSGreg Roach
36852ede8cSGreg Roach    protected const ROUTE_NAME = IndividualPage::class;
37a25f0a04SGreg Roach
3876692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3976692c8bSGreg Roach    public $generation;
40a25f0a04SGreg Roach
41a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
424686330aSGreg Roach    private $estimated_birth_date;
43a25f0a04SGreg Roach
44a25f0a04SGreg Roach    /** @var Date The estimated date of death */
454686330aSGreg Roach    private $estimated_death_date;
46a25f0a04SGreg Roach
47a25f0a04SGreg Roach    /**
48886b77daSGreg Roach     * A closure which will create a record from a database row.
49886b77daSGreg Roach     *
50*bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
51*bb03c9f0SGreg 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    {
58*bb03c9f0SGreg Roach        return Factory::individual()->mapper($tree);
59886b77daSGreg Roach    }
60886b77daSGreg Roach
61886b77daSGreg Roach    /**
62c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
63c156e8f5SGreg Roach     *
64c156e8f5SGreg Roach     * @return Closure
65c156e8f5SGreg Roach     */
66c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
67c156e8f5SGreg Roach    {
686c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
69c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
70c156e8f5SGreg Roach        };
71c156e8f5SGreg Roach    }
72c156e8f5SGreg Roach
73c156e8f5SGreg Roach    /**
74c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
75c156e8f5SGreg Roach     *
76c156e8f5SGreg Roach     * @return Closure
77c156e8f5SGreg Roach     */
78c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
79c156e8f5SGreg Roach    {
806c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
81c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
82c156e8f5SGreg Roach        };
83c156e8f5SGreg Roach    }
84c156e8f5SGreg Roach
85c156e8f5SGreg Roach    /**
86e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
87e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
88e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
89e71ef9d2SGreg Roach     *
90*bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::individual()
91*bb03c9f0SGreg Roach     *
92e71ef9d2SGreg Roach     * @param string      $xref
93e71ef9d2SGreg Roach     * @param Tree        $tree
94e71ef9d2SGreg Roach     * @param string|null $gedcom
95e71ef9d2SGreg Roach     *
96e71ef9d2SGreg Roach     * @return Individual|null
97e71ef9d2SGreg Roach     */
98ffc0a61fSGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Individual
99c1010edaSGreg Roach    {
100*bb03c9f0SGreg Roach        return Factory::individual()->make($xref, $tree, $gedcom);
101e71ef9d2SGreg Roach    }
102e71ef9d2SGreg Roach
103e71ef9d2SGreg Roach    /**
104395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
105395f0fe0SGreg Roach     * Typically when we load families and their members.
106395f0fe0SGreg Roach     *
107395f0fe0SGreg Roach     * @param Tree     $tree
1084a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
10918d7a90dSGreg Roach     *
11018d7a90dSGreg Roach     * @return void
111395f0fe0SGreg Roach     */
1122e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
113c1010edaSGreg Roach    {
1142e5b4452SGreg Roach        $rows = DB::table('individuals')
1152e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1162e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1172e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1182e5b4452SGreg Roach            ->get();
119395f0fe0SGreg Roach
120395f0fe0SGreg Roach        foreach ($rows as $row) {
121*bb03c9f0SGreg Roach            Factory::individual()->make($row->xref, $tree, $row->gedcom);
122395f0fe0SGreg Roach        }
123395f0fe0SGreg Roach    }
124395f0fe0SGreg Roach
125395f0fe0SGreg Roach    /**
126a25f0a04SGreg Roach     * Can the name of this record be shown?
127a25f0a04SGreg Roach     *
12876692c8bSGreg Roach     * @param int|null $access_level
12976692c8bSGreg Roach     *
13076692c8bSGreg Roach     * @return bool
131a25f0a04SGreg Roach     */
13235584196SGreg Roach    public function canShowName(int $access_level = null): bool
133c1010edaSGreg Roach    {
134d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
1354b9ff166SGreg Roach
136518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
137a25f0a04SGreg Roach    }
138a25f0a04SGreg Roach
139a25f0a04SGreg Roach    /**
14076692c8bSGreg Roach     * Can this individual be shown?
141a25f0a04SGreg Roach     *
14276692c8bSGreg Roach     * @param int $access_level
14376692c8bSGreg Roach     *
14476692c8bSGreg Roach     * @return bool
145a25f0a04SGreg Roach     */
14635584196SGreg Roach    protected function canShowByType(int $access_level): bool
147c1010edaSGreg Roach    {
148a25f0a04SGreg Roach        // Dead people...
149518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
150a25f0a04SGreg Roach            $keep_alive             = false;
15154ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
152a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1538d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
154a25f0a04SGreg Roach                foreach ($matches as $match) {
155a25f0a04SGreg Roach                    $date = new Date($match[1]);
156a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
157a25f0a04SGreg Roach                        $keep_alive = true;
158a25f0a04SGreg Roach                        break;
159a25f0a04SGreg Roach                    }
160a25f0a04SGreg Roach                }
161a25f0a04SGreg Roach            }
16254ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
163a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1648d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
165a25f0a04SGreg Roach                foreach ($matches as $match) {
166a25f0a04SGreg Roach                    $date = new Date($match[1]);
167a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
168a25f0a04SGreg Roach                        $keep_alive = true;
169a25f0a04SGreg Roach                        break;
170a25f0a04SGreg Roach                    }
171a25f0a04SGreg Roach                }
172a25f0a04SGreg Roach            }
173a25f0a04SGreg Roach            if (!$keep_alive) {
174a25f0a04SGreg Roach                return true;
175a25f0a04SGreg Roach            }
176a25f0a04SGreg Roach        }
177a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
1787c4add84SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_PATH_LENGTH);
1797c4add84SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF);
1807c4add84SGreg Roach
18154ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1824b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
183a25f0a04SGreg Roach        }
184a25f0a04SGreg Roach
185a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1864b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
187a25f0a04SGreg Roach    }
188a25f0a04SGreg Roach
189a25f0a04SGreg Roach    /**
190a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
191a25f0a04SGreg Roach     *
192a25f0a04SGreg Roach     * @param Individual $target
193cbc1590aSGreg Roach     * @param int        $distance
194a25f0a04SGreg Roach     *
195cbc1590aSGreg Roach     * @return bool
196a25f0a04SGreg Roach     */
1978f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
198c1010edaSGreg Roach    {
199a25f0a04SGreg Roach        static $cache = null;
200a25f0a04SGreg Roach
201*bb03c9f0SGreg Roach        $user_individual = Factory::individual()->make($target->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $target->tree);
202a25f0a04SGreg Roach        if ($user_individual) {
203a25f0a04SGreg Roach            if (!$cache) {
20413abd6f3SGreg Roach                $cache = [
20513abd6f3SGreg Roach                    0 => [$user_individual],
20613abd6f3SGreg Roach                    1 => [],
20713abd6f3SGreg Roach                ];
2088d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
209dc124885SGreg Roach                    $family = $fact->target();
210e24444eeSGreg Roach                    if ($family instanceof Family) {
211a25f0a04SGreg Roach                        $cache[1][] = $family;
212a25f0a04SGreg Roach                    }
213a25f0a04SGreg Roach                }
214a25f0a04SGreg Roach            }
215a25f0a04SGreg Roach        } else {
216a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
217a25f0a04SGreg Roach            return true;
218a25f0a04SGreg Roach        }
219a25f0a04SGreg Roach
220a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
221a25f0a04SGreg Roach        $distance *= 2;
222a25f0a04SGreg Roach
223a25f0a04SGreg Roach        // Consider each path length in turn
224a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
225a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
226a25f0a04SGreg Roach                // We have already calculated all records with this length
227e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
228a25f0a04SGreg Roach                    return true;
229a25f0a04SGreg Roach                }
230a25f0a04SGreg Roach            } else {
231a25f0a04SGreg Roach                // Need to calculate these paths
23213abd6f3SGreg Roach                $cache[$n] = [];
233e364afe4SGreg Roach                if ($n % 2 === 0) {
234a25f0a04SGreg Roach                    // Add FAM->INDI links
235a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2368d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
237dc124885SGreg Roach                            $individual = $fact->target();
238a25f0a04SGreg Roach                            // Don’t backtrack
239e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
240a25f0a04SGreg Roach                                $cache[$n][] = $individual;
241a25f0a04SGreg Roach                            }
242a25f0a04SGreg Roach                        }
243a25f0a04SGreg Roach                    }
244a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
245a25f0a04SGreg Roach                        return true;
246a25f0a04SGreg Roach                    }
247a25f0a04SGreg Roach                } else {
248a25f0a04SGreg Roach                    // Add INDI->FAM links
249a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2508d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
251dc124885SGreg Roach                            $family = $fact->target();
252a25f0a04SGreg Roach                            // Don’t backtrack
253e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
254a25f0a04SGreg Roach                                $cache[$n][] = $family;
255a25f0a04SGreg Roach                            }
256a25f0a04SGreg Roach                        }
257a25f0a04SGreg Roach                    }
258a25f0a04SGreg Roach                }
259a25f0a04SGreg Roach            }
260a25f0a04SGreg Roach        }
261a25f0a04SGreg Roach
262a25f0a04SGreg Roach        return false;
263a25f0a04SGreg Roach    }
264a25f0a04SGreg Roach
26576692c8bSGreg Roach    /**
26676692c8bSGreg Roach     * Generate a private version of this record
26776692c8bSGreg Roach     *
26876692c8bSGreg Roach     * @param int $access_level
26976692c8bSGreg Roach     *
27076692c8bSGreg Roach     * @return string
27176692c8bSGreg Roach     */
2723c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
273c1010edaSGreg Roach    {
274acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
275a25f0a04SGreg Roach
276a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
277518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
278a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2798d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
280138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
281a25f0a04SGreg Roach            }
282a25f0a04SGreg Roach        }
283a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2848d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
285a25f0a04SGreg Roach        foreach ($matches as $match) {
286*bb03c9f0SGreg Roach            $rela = Factory::family()->make($match[1], $this->tree);
287a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
288a25f0a04SGreg Roach                $rec .= $match[0];
289a25f0a04SGreg Roach            }
290a25f0a04SGreg Roach        }
291a25f0a04SGreg Roach        // Don’t privatize sex.
292a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
293a25f0a04SGreg Roach            $rec .= $match[0];
294a25f0a04SGreg Roach        }
295a25f0a04SGreg Roach
296a25f0a04SGreg Roach        return $rec;
297a25f0a04SGreg Roach    }
298a25f0a04SGreg Roach
29976692c8bSGreg Roach    /**
300a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
301a25f0a04SGreg Roach     * If not known to be dead, then assume living.
302a25f0a04SGreg Roach     *
303cbc1590aSGreg Roach     * @return bool
304a25f0a04SGreg Roach     */
3058f53f488SRico Sonntag    public function isDead(): bool
306c1010edaSGreg Roach    {
307c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3084459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
309a25f0a04SGreg Roach
310a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3118d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
312a25f0a04SGreg Roach            return true;
313a25f0a04SGreg Roach        }
314a25f0a04SGreg Roach
315a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
316a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
317a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
318a25f0a04SGreg Roach                $date = new Date($date_match);
319269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
320a25f0a04SGreg Roach                    return true;
321a25f0a04SGreg Roach                }
322a25f0a04SGreg Roach            }
323a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
324a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
325a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
326a25f0a04SGreg Roach                return false;
327a25f0a04SGreg Roach            }
328a25f0a04SGreg Roach        }
329a25f0a04SGreg Roach
330a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
331a25f0a04SGreg Roach
332a25f0a04SGreg Roach        // Check parents (birth and adopted)
33339ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
33439ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
335a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
336a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
337a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
338a25f0a04SGreg Roach                    $date = new Date($date_match);
339269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
340a25f0a04SGreg Roach                        return true;
341a25f0a04SGreg Roach                    }
342a25f0a04SGreg Roach                }
343a25f0a04SGreg Roach            }
344a25f0a04SGreg Roach        }
345a25f0a04SGreg Roach
346a25f0a04SGreg Roach        // Check spouses
34739ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
348a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
349a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
350a25f0a04SGreg Roach                $date = new Date($date_match);
351a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
352269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
353a25f0a04SGreg Roach                    return true;
354a25f0a04SGreg Roach                }
355a25f0a04SGreg Roach            }
356a25f0a04SGreg Roach            // Check spouse dates
35739ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
358a25f0a04SGreg Roach            if ($spouse) {
359a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
360a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
361a25f0a04SGreg Roach                    $date = new Date($date_match);
362a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
363269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
364a25f0a04SGreg Roach                        return true;
365a25f0a04SGreg Roach                    }
366a25f0a04SGreg Roach                }
367a25f0a04SGreg Roach            }
368a25f0a04SGreg Roach            // Check child dates
36939ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
370a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
371a25f0a04SGreg Roach                // Assume children born after age of 15
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 - 15)) {
375a25f0a04SGreg Roach                        return true;
376a25f0a04SGreg Roach                    }
377a25f0a04SGreg Roach                }
378a25f0a04SGreg Roach                // Check grandchildren
37939ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
38039ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
381a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
382a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
383a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
384a25f0a04SGreg Roach                            $date = new Date($date_match);
385269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
386a25f0a04SGreg Roach                                return true;
387a25f0a04SGreg Roach                            }
388a25f0a04SGreg Roach                        }
389a25f0a04SGreg Roach                    }
390a25f0a04SGreg Roach                }
391a25f0a04SGreg Roach            }
392a25f0a04SGreg Roach        }
393a25f0a04SGreg Roach
394a25f0a04SGreg Roach        return false;
395a25f0a04SGreg Roach    }
396a25f0a04SGreg Roach
397a25f0a04SGreg Roach    /**
398a25f0a04SGreg Roach     * Find the highlighted media object for an individual
399a25f0a04SGreg Roach     *
400e364afe4SGreg Roach     * @return MediaFile|null
401a25f0a04SGreg Roach     */
402e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
403c1010edaSGreg Roach    {
40492647683SGreg Roach        $fact = $this->facts(['OBJE'])
40519d319e7SGreg Roach            ->first(static function (Fact $fact): bool {
406dc124885SGreg Roach                $media = $fact->target();
40719d319e7SGreg Roach
40819d319e7SGreg Roach                return $media instanceof Media && $media->firstImageFile() instanceof MediaFile;
40919d319e7SGreg Roach            });
41019d319e7SGreg Roach
41192647683SGreg Roach        if ($fact instanceof Fact && $fact->target() instanceof Media) {
41292647683SGreg Roach            return $fact->target()->firstImageFile();
413a25f0a04SGreg Roach        }
414a25f0a04SGreg Roach
415a25f0a04SGreg Roach        return null;
416a25f0a04SGreg Roach    }
417a25f0a04SGreg Roach
418a25f0a04SGreg Roach    /**
419a25f0a04SGreg Roach     * Display the prefered image for this individual.
420a25f0a04SGreg Roach     * Use an icon if no image is available.
421a25f0a04SGreg Roach     *
422dce07401SGreg Roach     * @param int      $width      Pixels
423dce07401SGreg Roach     * @param int      $height     Pixels
424dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
425dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
426dce07401SGreg Roach     *
427a25f0a04SGreg Roach     * @return string
428a25f0a04SGreg Roach     */
4298f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
430c1010edaSGreg Roach    {
4314a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4324a9f750fSGreg Roach
4334a9f750fSGreg Roach        if ($media_file !== null) {
4344a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
435a25f0a04SGreg Roach        }
4364a9f750fSGreg Roach
4374a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
43839ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4394a9f750fSGreg Roach        }
4404a9f750fSGreg Roach
4414a9f750fSGreg Roach        return '';
442a25f0a04SGreg Roach    }
443a25f0a04SGreg Roach
444a25f0a04SGreg Roach    /**
445a25f0a04SGreg Roach     * Get the date of birth
446a25f0a04SGreg Roach     *
447a25f0a04SGreg Roach     * @return Date
448a25f0a04SGreg Roach     */
4498f53f488SRico Sonntag    public function getBirthDate(): Date
450c1010edaSGreg Roach    {
451a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
452a25f0a04SGreg Roach            if ($date->isOK()) {
453a25f0a04SGreg Roach                return $date;
454a25f0a04SGreg Roach            }
455a25f0a04SGreg Roach        }
456a25f0a04SGreg Roach
457a25f0a04SGreg Roach        return new Date('');
458a25f0a04SGreg Roach    }
459a25f0a04SGreg Roach
460a25f0a04SGreg Roach    /**
461a25f0a04SGreg Roach     * Get the place of birth
462a25f0a04SGreg Roach     *
46316d0b7f7SRico Sonntag     * @return Place
464a25f0a04SGreg Roach     */
4658f53f488SRico Sonntag    public function getBirthPlace(): Place
466c1010edaSGreg Roach    {
467a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
468a25f0a04SGreg Roach            return $place;
469a25f0a04SGreg Roach        }
470a25f0a04SGreg Roach
471b20ddbf9SGreg Roach        return new Place('', $this->tree);
472a25f0a04SGreg Roach    }
473a25f0a04SGreg Roach
474a25f0a04SGreg Roach    /**
475a25f0a04SGreg Roach     * Get the year of birth
476a25f0a04SGreg Roach     *
477a25f0a04SGreg Roach     * @return string the year of birth
478a25f0a04SGreg Roach     */
4798f53f488SRico Sonntag    public function getBirthYear(): string
480c1010edaSGreg Roach    {
481f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
482a25f0a04SGreg Roach    }
483a25f0a04SGreg Roach
484a25f0a04SGreg Roach    /**
485a25f0a04SGreg Roach     * Get the date of death
486a25f0a04SGreg Roach     *
487a25f0a04SGreg Roach     * @return Date
488a25f0a04SGreg Roach     */
4898f53f488SRico Sonntag    public function getDeathDate(): Date
490c1010edaSGreg Roach    {
491a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
492a25f0a04SGreg Roach            if ($date->isOK()) {
493a25f0a04SGreg Roach                return $date;
494a25f0a04SGreg Roach            }
495a25f0a04SGreg Roach        }
496a25f0a04SGreg Roach
497a25f0a04SGreg Roach        return new Date('');
498a25f0a04SGreg Roach    }
499a25f0a04SGreg Roach
500a25f0a04SGreg Roach    /**
501a25f0a04SGreg Roach     * Get the place of death
502a25f0a04SGreg Roach     *
50316d0b7f7SRico Sonntag     * @return Place
504a25f0a04SGreg Roach     */
5058f53f488SRico Sonntag    public function getDeathPlace(): Place
506c1010edaSGreg Roach    {
507a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
508a25f0a04SGreg Roach            return $place;
509a25f0a04SGreg Roach        }
510a25f0a04SGreg Roach
511b20ddbf9SGreg Roach        return new Place('', $this->tree);
512a25f0a04SGreg Roach    }
513a25f0a04SGreg Roach
514a25f0a04SGreg Roach    /**
515a25f0a04SGreg Roach     * get the death year
516a25f0a04SGreg Roach     *
517a25f0a04SGreg Roach     * @return string the year of death
518a25f0a04SGreg Roach     */
5198f53f488SRico Sonntag    public function getDeathYear(): string
520c1010edaSGreg Roach    {
521f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
522a25f0a04SGreg Roach    }
523a25f0a04SGreg Roach
524a25f0a04SGreg Roach    /**
525a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
52615d603e7SGreg Roach     * Provide the place and full date using a tooltip.
527a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
528a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
529a25f0a04SGreg Roach     *
530a25f0a04SGreg Roach     * @return string
531a25f0a04SGreg Roach     */
5325e6816beSGreg Roach    public function lifespan(): string
533c1010edaSGreg Roach    {
53415d603e7SGreg Roach        // Just the first part of the place name
535392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
536392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
53715d603e7SGreg Roach        // Remove markup from dates
53815d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
53915d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
54015d603e7SGreg Roach
541c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
542dacfe33cSGreg Roach        return I18N::translate(
543a25f0a04SGreg Roach            '%1$s–%2$s',
5442d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5452d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
546a25f0a04SGreg Roach        );
547a25f0a04SGreg Roach    }
548a25f0a04SGreg Roach
549a25f0a04SGreg Roach    /**
550a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
551a25f0a04SGreg Roach     *
552a25f0a04SGreg Roach     * @return Date[]
553a25f0a04SGreg Roach     */
5548f53f488SRico Sonntag    public function getAllBirthDates(): array
555c1010edaSGreg Roach    {
5568d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5578d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
558a25f0a04SGreg Roach            if ($tmp) {
559a25f0a04SGreg Roach                return $tmp;
560a25f0a04SGreg Roach            }
561a25f0a04SGreg Roach        }
562a25f0a04SGreg Roach
56313abd6f3SGreg Roach        return [];
564a25f0a04SGreg Roach    }
565a25f0a04SGreg Roach
566a25f0a04SGreg Roach    /**
567a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
568a25f0a04SGreg Roach     *
5694080d558SGreg Roach     * @return Place[]
570a25f0a04SGreg Roach     */
5718f53f488SRico Sonntag    public function getAllBirthPlaces(): array
572c1010edaSGreg Roach    {
5738d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5748d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
57554c1ab5eSGreg Roach            if ($places !== []) {
5764080d558SGreg Roach                return $places;
577a25f0a04SGreg Roach            }
578a25f0a04SGreg Roach        }
579a25f0a04SGreg Roach
58013abd6f3SGreg Roach        return [];
581a25f0a04SGreg Roach    }
582a25f0a04SGreg Roach
583a25f0a04SGreg Roach    /**
584a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
585a25f0a04SGreg Roach     *
586a25f0a04SGreg Roach     * @return Date[]
587a25f0a04SGreg Roach     */
5888f53f488SRico Sonntag    public function getAllDeathDates(): array
589c1010edaSGreg Roach    {
5908d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
5918d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
592a25f0a04SGreg Roach            if ($tmp) {
593a25f0a04SGreg Roach                return $tmp;
594a25f0a04SGreg Roach            }
595a25f0a04SGreg Roach        }
596a25f0a04SGreg Roach
59713abd6f3SGreg Roach        return [];
598a25f0a04SGreg Roach    }
599a25f0a04SGreg Roach
600a25f0a04SGreg Roach    /**
601a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
602a25f0a04SGreg Roach     *
6034080d558SGreg Roach     * @return Place[]
604a25f0a04SGreg Roach     */
6058f53f488SRico Sonntag    public function getAllDeathPlaces(): array
606c1010edaSGreg Roach    {
6078d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6088d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
60954c1ab5eSGreg Roach            if ($places !== []) {
6104080d558SGreg Roach                return $places;
611a25f0a04SGreg Roach            }
612a25f0a04SGreg Roach        }
613a25f0a04SGreg Roach
61413abd6f3SGreg Roach        return [];
615a25f0a04SGreg Roach    }
616a25f0a04SGreg Roach
617a25f0a04SGreg Roach    /**
618a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
619a25f0a04SGreg Roach     *
620a25f0a04SGreg Roach     * @return Date
621a25f0a04SGreg Roach     */
6228f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
623c1010edaSGreg Roach    {
6248f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
625a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
626a25f0a04SGreg Roach                if ($date->isOK()) {
6274686330aSGreg Roach                    $this->estimated_birth_date = $date;
628a25f0a04SGreg Roach                    break;
629a25f0a04SGreg Roach                }
630a25f0a04SGreg Roach            }
6318f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
63213abd6f3SGreg Roach                $min = [];
63313abd6f3SGreg Roach                $max = [];
634a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
635f5b60decSGreg Roach                if ($tmp->isOK()) {
636f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
637f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
638a25f0a04SGreg Roach                }
63939ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
640a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
641f5b60decSGreg Roach                    if ($tmp->isOK()) {
642f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
643f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
644a25f0a04SGreg Roach                    }
64539ca88baSGreg Roach                    $husband = $family->husband();
646e364afe4SGreg Roach                    if ($husband instanceof self) {
6472e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
648f5b60decSGreg Roach                        if ($tmp->isOK()) {
649f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
650f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
651a25f0a04SGreg Roach                        }
652a25f0a04SGreg Roach                    }
65339ca88baSGreg Roach                    $wife = $family->wife();
654e364afe4SGreg Roach                    if ($wife instanceof self) {
6552e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
656f5b60decSGreg Roach                        if ($tmp->isOK()) {
657f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
658f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
659a25f0a04SGreg Roach                        }
660a25f0a04SGreg Roach                    }
66139ca88baSGreg Roach                    foreach ($family->children() as $child) {
662a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
663f5b60decSGreg Roach                        if ($tmp->isOK()) {
664f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
665f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
666a25f0a04SGreg Roach                        }
667a25f0a04SGreg Roach                    }
668a25f0a04SGreg Roach                }
66939ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
670a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
671f5b60decSGreg Roach                    if ($tmp->isOK()) {
672f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
673f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
674a25f0a04SGreg Roach                    }
67539ca88baSGreg Roach                    $spouse = $family->spouse($this);
676a25f0a04SGreg Roach                    if ($spouse) {
677a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
678f5b60decSGreg Roach                        if ($tmp->isOK()) {
679f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
680f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
681a25f0a04SGreg Roach                        }
682a25f0a04SGreg Roach                    }
68339ca88baSGreg Roach                    foreach ($family->children() as $child) {
684a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
685f5b60decSGreg Roach                        if ($tmp->isOK()) {
686e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
687f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
688a25f0a04SGreg Roach                        }
689a25f0a04SGreg Roach                    }
690a25f0a04SGreg Roach                }
691a25f0a04SGreg Roach                if ($min && $max) {
69259f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
693a25f0a04SGreg Roach
69465e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
6954686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
696a25f0a04SGreg Roach                } else {
6974686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
698a25f0a04SGreg Roach                }
699a25f0a04SGreg Roach            }
700a25f0a04SGreg Roach        }
701a25f0a04SGreg Roach
7024686330aSGreg Roach        return $this->estimated_birth_date;
703a25f0a04SGreg Roach    }
704a25f0a04SGreg Roach
705a25f0a04SGreg Roach    /**
706a25f0a04SGreg Roach     * Generate an estimated date of death.
707a25f0a04SGreg Roach     *
708a25f0a04SGreg Roach     * @return Date
709a25f0a04SGreg Roach     */
7108f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
711c1010edaSGreg Roach    {
7124686330aSGreg Roach        if ($this->estimated_death_date === null) {
713a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
714a25f0a04SGreg Roach                if ($date->isOK()) {
7154686330aSGreg Roach                    $this->estimated_death_date = $date;
716a25f0a04SGreg Roach                    break;
717a25f0a04SGreg Roach                }
718a25f0a04SGreg Roach            }
7194686330aSGreg Roach            if ($this->estimated_death_date === null) {
720f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
721c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7224686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
723a25f0a04SGreg Roach                } else {
7244686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
725a25f0a04SGreg Roach                }
726a25f0a04SGreg Roach            }
727a25f0a04SGreg Roach        }
728a25f0a04SGreg Roach
7294686330aSGreg Roach        return $this->estimated_death_date;
730a25f0a04SGreg Roach    }
731a25f0a04SGreg Roach
732a25f0a04SGreg Roach    /**
733a25f0a04SGreg Roach     * Get the sex - M F or U
734a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
735a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
736a25f0a04SGreg Roach     *
737a25f0a04SGreg Roach     * @return string
738a25f0a04SGreg Roach     */
739e364afe4SGreg Roach    public function sex(): string
740c1010edaSGreg Roach    {
741a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
742a25f0a04SGreg Roach            return $match[1];
743a25f0a04SGreg Roach        }
744b2ce94c6SRico Sonntag
745b2ce94c6SRico Sonntag        return 'U';
746a25f0a04SGreg Roach    }
747a25f0a04SGreg Roach
748a25f0a04SGreg Roach    /**
749a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
750a25f0a04SGreg Roach     *
751a25f0a04SGreg Roach     * @return string
752a25f0a04SGreg Roach     */
7538f53f488SRico Sonntag    public function getBoxStyle(): string
754c1010edaSGreg Roach    {
755c1010edaSGreg Roach        $tmp = [
756c1010edaSGreg Roach            'M' => '',
757c1010edaSGreg Roach            'F' => 'F',
758c1010edaSGreg Roach            'U' => 'NN',
759c1010edaSGreg Roach        ];
760a25f0a04SGreg Roach
76139ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
762a25f0a04SGreg Roach    }
763a25f0a04SGreg Roach
764a25f0a04SGreg Roach    /**
765a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
766a25f0a04SGreg Roach     *
767cbc1590aSGreg Roach     * @param int|null $access_level
768a25f0a04SGreg Roach     *
769b5c8fd7eSGreg Roach     * @return Collection<Family>
770a25f0a04SGreg Roach     */
77139ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
772c1010edaSGreg Roach    {
773d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
774d9e083e7SGreg Roach
775d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
776d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
7774b9ff166SGreg Roach        }
7784b9ff166SGreg Roach
77939ca88baSGreg Roach        $families = new Collection();
780d9e083e7SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level) as $fact) {
781dc124885SGreg Roach            $family = $fact->target();
782d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
78339ca88baSGreg Roach                $families->push($family);
784a25f0a04SGreg Roach            }
785a25f0a04SGreg Roach        }
786a25f0a04SGreg Roach
78739ca88baSGreg Roach        return new Collection($families);
788a25f0a04SGreg Roach    }
789a25f0a04SGreg Roach
790a25f0a04SGreg Roach    /**
791a25f0a04SGreg Roach     * Get the current spouse of this individual.
792a25f0a04SGreg Roach     *
793a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
794a25f0a04SGreg Roach     * in chronological order, and take the last one found.
795a25f0a04SGreg Roach     *
796a25f0a04SGreg Roach     * @return Individual|null
797a25f0a04SGreg Roach     */
798e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
799c1010edaSGreg Roach    {
80039ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
80139ca88baSGreg Roach
80239ca88baSGreg Roach        if ($family instanceof Family) {
80339ca88baSGreg Roach            return $family->spouse($this);
804a25f0a04SGreg Roach        }
805b2ce94c6SRico Sonntag
806b2ce94c6SRico Sonntag        return null;
807a25f0a04SGreg Roach    }
808a25f0a04SGreg Roach
809a25f0a04SGreg Roach    /**
810a25f0a04SGreg Roach     * Count the children belonging to this individual.
811a25f0a04SGreg Roach     *
812cbc1590aSGreg Roach     * @return int
813a25f0a04SGreg Roach     */
814e364afe4SGreg Roach    public function numberOfChildren(): int
815c1010edaSGreg Roach    {
8167d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8173dc7bbe9SGreg Roach            return (int) $match[1];
818b2ce94c6SRico Sonntag        }
819b2ce94c6SRico Sonntag
82013abd6f3SGreg Roach        $children = [];
82139ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
82239ca88baSGreg Roach            foreach ($fam->children() as $child) {
823c0935879SGreg Roach                $children[$child->xref()] = true;
824a25f0a04SGreg Roach            }
825a25f0a04SGreg Roach        }
826a25f0a04SGreg Roach
827a25f0a04SGreg Roach        return count($children);
828a25f0a04SGreg Roach    }
829a25f0a04SGreg Roach
830a25f0a04SGreg Roach    /**
831a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
832a25f0a04SGreg Roach     *
833cbc1590aSGreg Roach     * @param int|null $access_level
834a25f0a04SGreg Roach     *
835b5c8fd7eSGreg Roach     * @return Collection<Family>
836a25f0a04SGreg Roach     */
83739ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
838c1010edaSGreg Roach    {
839d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
8404b9ff166SGreg Roach
841d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
842d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
843d9e083e7SGreg Roach        }
844a25f0a04SGreg Roach
84539ca88baSGreg Roach        $families = new Collection();
84639ca88baSGreg Roach
847d9e083e7SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level) as $fact) {
848dc124885SGreg Roach            $family = $fact->target();
849d9e083e7SGreg Roach            if ($family instanceof Family && $family->canShow($access_level)) {
85039ca88baSGreg Roach                $families->push($family);
851a25f0a04SGreg Roach            }
852a25f0a04SGreg Roach        }
853a25f0a04SGreg Roach
854a25f0a04SGreg Roach        return $families;
855a25f0a04SGreg Roach    }
856a25f0a04SGreg Roach
857a25f0a04SGreg Roach    /**
858a25f0a04SGreg Roach     * Get a list of step-parent families.
859a25f0a04SGreg Roach     *
860b5c8fd7eSGreg Roach     * @return Collection<Family>
861a25f0a04SGreg Roach     */
862820b62dfSGreg Roach    public function childStepFamilies(): Collection
863c1010edaSGreg Roach    {
864ed5b6227SGreg Roach        $step_families = new Collection();
86539ca88baSGreg Roach        $families      = $this->childFamilies();
866a25f0a04SGreg Roach        foreach ($families as $family) {
867ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
868ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
86939ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
870ed5b6227SGreg Roach                        $step_families->add($step_family);
871a25f0a04SGreg Roach                    }
872a25f0a04SGreg Roach                }
873a25f0a04SGreg Roach            }
874a25f0a04SGreg Roach        }
875a25f0a04SGreg Roach
8768c627a69SGreg Roach        return $step_families->uniqueStrict(static function (Family $family): string {
8778c627a69SGreg Roach            return $family->xref();
8788c627a69SGreg Roach        });
879a25f0a04SGreg Roach    }
880a25f0a04SGreg Roach
881a25f0a04SGreg Roach    /**
882a25f0a04SGreg Roach     * Get a list of step-parent families.
883a25f0a04SGreg Roach     *
884b5c8fd7eSGreg Roach     * @return Collection<Family>
885a25f0a04SGreg Roach     */
886820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
887c1010edaSGreg Roach    {
88813abd6f3SGreg Roach        $step_families = [];
88939ca88baSGreg Roach        $families      = $this->spouseFamilies();
890820b62dfSGreg Roach
891a25f0a04SGreg Roach        foreach ($families as $family) {
89239ca88baSGreg Roach            $spouse = $family->spouse($this);
893820b62dfSGreg Roach
894b5c8fd7eSGreg Roach            if ($spouse instanceof Individual) {
89539ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
89639ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
897a25f0a04SGreg Roach                        $step_families[] = $step_family;
898a25f0a04SGreg Roach                    }
899a25f0a04SGreg Roach                }
900a25f0a04SGreg Roach            }
901a25f0a04SGreg Roach        }
902a25f0a04SGreg Roach
903820b62dfSGreg Roach        return new Collection($step_families);
904a25f0a04SGreg Roach    }
905a25f0a04SGreg Roach
906a25f0a04SGreg Roach    /**
907a25f0a04SGreg Roach     * A label for a parental family group
908a25f0a04SGreg Roach     *
909a25f0a04SGreg Roach     * @param Family $family
910a25f0a04SGreg Roach     *
911a25f0a04SGreg Roach     * @return string
912a25f0a04SGreg Roach     */
913820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
914c1010edaSGreg Roach    {
9157d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
916a25f0a04SGreg Roach            // A specified pedigree
917764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
918b2ce94c6SRico Sonntag        }
919b2ce94c6SRico Sonntag
920a25f0a04SGreg Roach        // Default (birth) pedigree
921764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
922a25f0a04SGreg Roach    }
923a25f0a04SGreg Roach
924a25f0a04SGreg Roach    /**
925a25f0a04SGreg Roach     * Create a label for a step family
926a25f0a04SGreg Roach     *
927a25f0a04SGreg Roach     * @param Family $step_family
928a25f0a04SGreg Roach     *
929a25f0a04SGreg Roach     * @return string
930a25f0a04SGreg Roach     */
9318f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
932c1010edaSGreg Roach    {
93339ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
934a25f0a04SGreg Roach            if ($family !== $step_family) {
935a25f0a04SGreg Roach                // Must be a step-family
93639ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
93739ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
938a25f0a04SGreg Roach                        if ($parent === $step_parent) {
939a25f0a04SGreg Roach                            // One common parent - must be a step family
940e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
941a25f0a04SGreg Roach                                // Father’s family with someone else
94239ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
943a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
94439ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
945b2ce94c6SRico Sonntag                                }
946b2ce94c6SRico Sonntag
947a25f0a04SGreg Roach                                /* I18N: A step-family. */
948bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
949a25f0a04SGreg Roach                            }
950b2ce94c6SRico Sonntag
951a25f0a04SGreg Roach                            // Mother’s family with someone else
95239ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
953a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
95439ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
955b2ce94c6SRico Sonntag                            }
956b2ce94c6SRico Sonntag
957a25f0a04SGreg Roach                            /* I18N: A step-family. */
958bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
959a25f0a04SGreg Roach                        }
960a25f0a04SGreg Roach                    }
961a25f0a04SGreg Roach                }
962a25f0a04SGreg Roach            }
963a25f0a04SGreg Roach        }
964a25f0a04SGreg Roach
965a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
966a25f0a04SGreg Roach        return I18N::translate('Family with parents');
967a25f0a04SGreg Roach    }
968a25f0a04SGreg Roach
969225e381fSGreg Roach    /**
970225e381fSGreg Roach     * Get the description for the family.
971225e381fSGreg Roach     *
972225e381fSGreg Roach     * For example, "XXX's family with new wife".
973225e381fSGreg Roach     *
974225e381fSGreg Roach     * @param Family $family
975225e381fSGreg Roach     *
976225e381fSGreg Roach     * @return string
977225e381fSGreg Roach     */
978e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
979c1010edaSGreg Roach    {
98039ca88baSGreg Roach        $spouse = $family->spouse($this);
981225e381fSGreg Roach        if ($spouse) {
982225e381fSGreg Roach            /* I18N: %s is the spouse name */
98339ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
984225e381fSGreg Roach        }
985b2ce94c6SRico Sonntag
98639ca88baSGreg Roach        return $family->fullName();
987225e381fSGreg Roach    }
988225e381fSGreg Roach
989a25f0a04SGreg Roach    /**
990961ec755SGreg Roach     * If this object has no name, what do we call it?
991961ec755SGreg Roach     *
992961ec755SGreg Roach     * @return string
993961ec755SGreg Roach     */
9948f53f488SRico Sonntag    public function getFallBackName(): string
995c1010edaSGreg Roach    {
996a25f0a04SGreg Roach        return '@P.N. /@N.N./';
997a25f0a04SGreg Roach    }
998a25f0a04SGreg Roach
999a25f0a04SGreg Roach    /**
1000a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1001a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1002a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1003a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1004a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1005a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1006a25f0a04SGreg Roach     *
1007a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1008a25f0a04SGreg Roach     * 2 GIVN Robert
1009a25f0a04SGreg Roach     * 2 SPFX de
1010a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1011a25f0a04SGreg Roach     * 2 NICK The Bald
1012a25f0a04SGreg Roach     *
1013a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1014a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1015a25f0a04SGreg Roach     *
1016a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1017a25f0a04SGreg Roach     *
1018a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1019a25f0a04SGreg Roach     * or
1020a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1021a25f0a04SGreg Roach     * 2 GIVN Carlos
1022a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1023a25f0a04SGreg Roach     *
1024a25f0a04SGreg Roach     * @param string $type
1025a25f0a04SGreg Roach     * @param string $full
1026a25f0a04SGreg Roach     * @param string $gedcom
1027e364afe4SGreg Roach     *
1028e364afe4SGreg Roach     * @return void
1029a25f0a04SGreg Roach     */
1030e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1031c1010edaSGreg Roach    {
1032a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1033a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1034a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1035a25f0a04SGreg Roach
103676f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1037a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1038a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1039a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1040a25f0a04SGreg Roach
1041a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
104276f666f4SGreg Roach        if ($SURN !== '') {
1043a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1044a25f0a04SGreg Roach        } else {
104513abd6f3SGreg Roach            $SURNS = [];
1046a25f0a04SGreg Roach        }
104776f666f4SGreg Roach
1048a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1049a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1050a25f0a04SGreg Roach
1051a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1052a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1053a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1054a25f0a04SGreg Roach
1055a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
105676f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1057e364afe4SGreg Roach            $full .= '/';
1058a25f0a04SGreg Roach        }
1059a25f0a04SGreg Roach
1060a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1061a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1062a25f0a04SGreg Roach
1063a25f0a04SGreg Roach        // Extract the surname.
1064a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1065a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1066a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1067a25f0a04SGreg Roach        } else {
1068a25f0a04SGreg Roach            $surname = '';
1069a25f0a04SGreg Roach        }
1070a25f0a04SGreg Roach
1071a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1072a25f0a04SGreg Roach        if (!$SURNS) {
1073a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1074a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1075a25f0a04SGreg Roach                $SURNS = $matches[1];
1076a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1077a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1078a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1079a25f0a04SGreg Roach                }
1080a25f0a04SGreg Roach            } else {
1081a25f0a04SGreg Roach                // It is valid not to have a surname at all
108213abd6f3SGreg Roach                $SURNS = [''];
1083a25f0a04SGreg Roach            }
1084a25f0a04SGreg Roach        }
1085a25f0a04SGreg Roach
1086a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1087a25f0a04SGreg Roach        if (!$GIVN) {
1088a25f0a04SGreg Roach            $GIVN = preg_replace(
108913abd6f3SGreg Roach                [
1090c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1091c1010edaSGreg Roach                    // remove surname
1092c1010edaSGreg Roach                    '/ ?".+"/',
1093c1010edaSGreg Roach                    // remove nickname
1094c1010edaSGreg Roach                    '/ {2,}/',
1095c1010edaSGreg Roach                    // multiple spaces, caused by the above
1096c1010edaSGreg Roach                    '/^ | $/',
1097c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
109813abd6f3SGreg Roach                ],
109913abd6f3SGreg Roach                [
1100a25f0a04SGreg Roach                    ' ',
1101a25f0a04SGreg Roach                    ' ',
1102a25f0a04SGreg Roach                    ' ',
1103a25f0a04SGreg Roach                    '',
110413abd6f3SGreg Roach                ],
1105a25f0a04SGreg Roach                $full
1106a25f0a04SGreg Roach            );
1107a25f0a04SGreg Roach        }
1108a25f0a04SGreg Roach
1109a25f0a04SGreg Roach        // Add placeholder for unknown given name
1110a25f0a04SGreg Roach        if (!$GIVN) {
1111a25f0a04SGreg Roach            $GIVN = '@P.N.';
111273f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1113a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1114a25f0a04SGreg Roach        }
1115a25f0a04SGreg Roach
11167b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
11177b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1118c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
11197b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1120c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1121a25f0a04SGreg Roach        }
1122a25f0a04SGreg Roach
1123a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1124a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1125a25f0a04SGreg Roach        // $full is for display on-screen
1126a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1127a25f0a04SGreg Roach
1128a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1129ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1130ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1131c6f196c3SGreg Roach        // Format for display
1132d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1133acc34ea1SGreg Roach        // Localise quotation marks around the nickname
11340b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
1135c652cdbdSGreg Roach            return '<q class="wt-nickname">' . $matches[1] . '</q>';
11368d68cabeSGreg Roach        }, $full);
1137a25f0a04SGreg Roach
1138c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1139a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1140a25f0a04SGreg Roach
1141a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1142a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1143a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1144a25f0a04SGreg Roach
1145ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1146a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1147e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1148a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1149e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1150a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1151a25f0a04SGreg Roach            }
1152a25f0a04SGreg Roach
1153bdb3725aSGreg Roach            $this->getAllNames[] = [
1154a25f0a04SGreg Roach                'type'    => $type,
1155a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1156c1010edaSGreg Roach                'full'    => $full,
1157c1010edaSGreg Roach                // This is used for display
1158c1010edaSGreg Roach                'fullNN'  => $fullNN,
1159c1010edaSGreg Roach                // This goes into the database
1160c1010edaSGreg Roach                'surname' => $surname,
1161c1010edaSGreg Roach                // This goes into the database
1162c1010edaSGreg Roach                'givn'    => $GIVN,
1163c1010edaSGreg Roach                // This goes into the database
1164c1010edaSGreg Roach                'surn'    => $SURN,
1165c1010edaSGreg Roach                // This goes into the database
116613abd6f3SGreg Roach            ];
1167a25f0a04SGreg Roach        }
1168a25f0a04SGreg Roach    }
1169a25f0a04SGreg Roach
1170a25f0a04SGreg Roach    /**
117176692c8bSGreg Roach     * Extract names from the GEDCOM record.
1172c7ff4153SGreg Roach     *
1173c7ff4153SGreg Roach     * @return void
1174a25f0a04SGreg Roach     */
1175e364afe4SGreg Roach    public function extractNames(): void
1176c1010edaSGreg Roach    {
1177d9e083e7SGreg Roach        $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree);
1178d9e083e7SGreg Roach
11798f53f488SRico Sonntag        $this->extractNamesFromFacts(
11808f53f488SRico Sonntag            1,
11818f53f488SRico Sonntag            'NAME',
1182d9e083e7SGreg Roach            $this->facts(['NAME'], false, $access_level)
11838f53f488SRico Sonntag        );
1184a25f0a04SGreg Roach    }
1185a25f0a04SGreg Roach
1186a25f0a04SGreg Roach    /**
1187a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1188a25f0a04SGreg Roach     * selection items or favorites.
1189a25f0a04SGreg Roach     *
1190a25f0a04SGreg Roach     * @return string
1191a25f0a04SGreg Roach     */
11928f53f488SRico Sonntag    public function formatListDetails(): string
1193c1010edaSGreg Roach    {
1194a25f0a04SGreg Roach        return
11958d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
11968d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1197a25f0a04SGreg Roach    }
1198a25f0a04SGreg Roach}
1199