xref: /webtrees/app/Individual.php (revision dacfe33c2987794981799d66f9ae998a42f6de3a)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
236ccdf4f0SGreg Roachuse Exception;
24a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
262e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2739ca88baSGreg Roachuse Illuminate\Support\Collection;
28886b77daSGreg Roachuse stdClass;
29a25f0a04SGreg 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
3716d6367aSGreg Roach    protected const ROUTE_NAME = 'individual';
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     *
51886b77daSGreg Roach     * @return Closure
52886b77daSGreg Roach     */
53c0804649SGreg Roach    public static function rowMapper(): Closure
54886b77daSGreg Roach    {
556c2179e2SGreg Roach        return static function (stdClass $row): Individual {
56a7a24840SGreg Roach            $individual = Individual::getInstance($row->i_id, Tree::findById((int) $row->i_file), $row->i_gedcom);
57e84cf2deSGreg Roach
58e84cf2deSGreg Roach            if ($row->n_num ?? null) {
59e0458bdcSGreg Roach                $individual = clone $individual;
60e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
61e0458bdcSGreg Roach
62e0458bdcSGreg Roach                return $individual;
63e84cf2deSGreg Roach            }
64e84cf2deSGreg Roach
65e84cf2deSGreg Roach            return $individual;
66886b77daSGreg Roach        };
67886b77daSGreg Roach    }
68886b77daSGreg Roach
69886b77daSGreg Roach    /**
70c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
71c156e8f5SGreg Roach     *
72c156e8f5SGreg Roach     * @return Closure
73c156e8f5SGreg Roach     */
74c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
75c156e8f5SGreg Roach    {
766c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
77c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
78c156e8f5SGreg Roach        };
79c156e8f5SGreg Roach    }
80c156e8f5SGreg Roach
81c156e8f5SGreg Roach    /**
82c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
83c156e8f5SGreg Roach     *
84c156e8f5SGreg Roach     * @return Closure
85c156e8f5SGreg Roach     */
86c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
87c156e8f5SGreg Roach    {
886c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
89c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
90c156e8f5SGreg Roach        };
91c156e8f5SGreg Roach    }
92c156e8f5SGreg Roach
93c156e8f5SGreg Roach    /**
94e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
95e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
96e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
97e71ef9d2SGreg Roach     *
98e71ef9d2SGreg Roach     * @param string      $xref
99e71ef9d2SGreg Roach     * @param Tree        $tree
100e71ef9d2SGreg Roach     * @param string|null $gedcom
101e71ef9d2SGreg Roach     *
1026ccdf4f0SGreg Roach     * @throws Exception
103e71ef9d2SGreg Roach     * @return Individual|null
104e71ef9d2SGreg Roach     */
105e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
106c1010edaSGreg Roach    {
107e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
108e71ef9d2SGreg Roach
109e364afe4SGreg Roach        if ($record instanceof self) {
110e71ef9d2SGreg Roach            return $record;
111e71ef9d2SGreg Roach        }
112b2ce94c6SRico Sonntag
113b2ce94c6SRico Sonntag        return null;
114e71ef9d2SGreg Roach    }
115e71ef9d2SGreg Roach
116e71ef9d2SGreg Roach    /**
117395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
118395f0fe0SGreg Roach     * Typically when we load families and their members.
119395f0fe0SGreg Roach     *
120395f0fe0SGreg Roach     * @param Tree     $tree
1214a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
12218d7a90dSGreg Roach     *
12318d7a90dSGreg Roach     * @return void
124395f0fe0SGreg Roach     */
1252e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
126c1010edaSGreg Roach    {
1272e5b4452SGreg Roach        $rows = DB::table('individuals')
1282e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1292e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1302e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1312e5b4452SGreg Roach            ->get();
132395f0fe0SGreg Roach
133395f0fe0SGreg Roach        foreach ($rows as $row) {
134395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
135395f0fe0SGreg Roach        }
136395f0fe0SGreg Roach    }
137395f0fe0SGreg Roach
138395f0fe0SGreg Roach    /**
139a25f0a04SGreg Roach     * Can the name of this record be shown?
140a25f0a04SGreg Roach     *
14176692c8bSGreg Roach     * @param int|null $access_level
14276692c8bSGreg Roach     *
14376692c8bSGreg Roach     * @return bool
144a25f0a04SGreg Roach     */
14535584196SGreg Roach    public function canShowName(int $access_level = null): bool
146c1010edaSGreg Roach    {
1474b9ff166SGreg Roach        if ($access_level === null) {
1484b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1494b9ff166SGreg Roach        }
1504b9ff166SGreg Roach
151518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
152a25f0a04SGreg Roach    }
153a25f0a04SGreg Roach
154a25f0a04SGreg Roach    /**
15576692c8bSGreg Roach     * Can this individual be shown?
156a25f0a04SGreg Roach     *
15776692c8bSGreg Roach     * @param int $access_level
15876692c8bSGreg Roach     *
15976692c8bSGreg Roach     * @return bool
160a25f0a04SGreg Roach     */
16135584196SGreg Roach    protected function canShowByType(int $access_level): bool
162c1010edaSGreg Roach    {
163a25f0a04SGreg Roach        // Dead people...
164518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
165a25f0a04SGreg Roach            $keep_alive             = false;
16654ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
167a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1688d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
169a25f0a04SGreg Roach                foreach ($matches as $match) {
170a25f0a04SGreg Roach                    $date = new Date($match[1]);
171a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
172a25f0a04SGreg Roach                        $keep_alive = true;
173a25f0a04SGreg Roach                        break;
174a25f0a04SGreg Roach                    }
175a25f0a04SGreg Roach                }
176a25f0a04SGreg Roach            }
17754ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
178a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1798d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
180a25f0a04SGreg Roach                foreach ($matches as $match) {
181a25f0a04SGreg Roach                    $date = new Date($match[1]);
182a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
183a25f0a04SGreg Roach                        $keep_alive = true;
184a25f0a04SGreg Roach                        break;
185a25f0a04SGreg Roach                    }
186a25f0a04SGreg Roach                }
187a25f0a04SGreg Roach            }
188a25f0a04SGreg Roach            if (!$keep_alive) {
189a25f0a04SGreg Roach                return true;
190a25f0a04SGreg Roach            }
191a25f0a04SGreg Roach        }
192a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
19354ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1944b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
19554ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1964b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
197a25f0a04SGreg Roach        }
198a25f0a04SGreg Roach
199a25f0a04SGreg Roach        // No restriction found - show living people to members only:
2004b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
201a25f0a04SGreg Roach    }
202a25f0a04SGreg Roach
203a25f0a04SGreg Roach    /**
204a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
205a25f0a04SGreg Roach     *
206a25f0a04SGreg Roach     * @param Individual $target
207cbc1590aSGreg Roach     * @param int        $distance
208a25f0a04SGreg Roach     *
209cbc1590aSGreg Roach     * @return bool
210a25f0a04SGreg Roach     */
2118f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
212c1010edaSGreg Roach    {
213a25f0a04SGreg Roach        static $cache = null;
214a25f0a04SGreg Roach
21506ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
216a25f0a04SGreg Roach        if ($user_individual) {
217a25f0a04SGreg Roach            if (!$cache) {
21813abd6f3SGreg Roach                $cache = [
21913abd6f3SGreg Roach                    0 => [$user_individual],
22013abd6f3SGreg Roach                    1 => [],
22113abd6f3SGreg Roach                ];
2228d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
223dc124885SGreg Roach                    $family = $fact->target();
224e24444eeSGreg Roach                    if ($family instanceof Family) {
225a25f0a04SGreg Roach                        $cache[1][] = $family;
226a25f0a04SGreg Roach                    }
227a25f0a04SGreg Roach                }
228a25f0a04SGreg Roach            }
229a25f0a04SGreg Roach        } else {
230a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
231a25f0a04SGreg Roach            return true;
232a25f0a04SGreg Roach        }
233a25f0a04SGreg Roach
234a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
235a25f0a04SGreg Roach        $distance *= 2;
236a25f0a04SGreg Roach
237a25f0a04SGreg Roach        // Consider each path length in turn
238a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
239a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
240a25f0a04SGreg Roach                // We have already calculated all records with this length
241e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
242a25f0a04SGreg Roach                    return true;
243a25f0a04SGreg Roach                }
244a25f0a04SGreg Roach            } else {
245a25f0a04SGreg Roach                // Need to calculate these paths
24613abd6f3SGreg Roach                $cache[$n] = [];
247e364afe4SGreg Roach                if ($n % 2 === 0) {
248a25f0a04SGreg Roach                    // Add FAM->INDI links
249a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2508d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
251dc124885SGreg Roach                            $individual = $fact->target();
252a25f0a04SGreg Roach                            // Don’t backtrack
253e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
254a25f0a04SGreg Roach                                $cache[$n][] = $individual;
255a25f0a04SGreg Roach                            }
256a25f0a04SGreg Roach                        }
257a25f0a04SGreg Roach                    }
258a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
259a25f0a04SGreg Roach                        return true;
260a25f0a04SGreg Roach                    }
261a25f0a04SGreg Roach                } else {
262a25f0a04SGreg Roach                    // Add INDI->FAM links
263a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2648d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
265dc124885SGreg Roach                            $family = $fact->target();
266a25f0a04SGreg Roach                            // Don’t backtrack
267e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
268a25f0a04SGreg Roach                                $cache[$n][] = $family;
269a25f0a04SGreg Roach                            }
270a25f0a04SGreg Roach                        }
271a25f0a04SGreg Roach                    }
272a25f0a04SGreg Roach                }
273a25f0a04SGreg Roach            }
274a25f0a04SGreg Roach        }
275a25f0a04SGreg Roach
276a25f0a04SGreg Roach        return false;
277a25f0a04SGreg Roach    }
278a25f0a04SGreg Roach
27976692c8bSGreg Roach    /**
28076692c8bSGreg Roach     * Generate a private version of this record
28176692c8bSGreg Roach     *
28276692c8bSGreg Roach     * @param int $access_level
28376692c8bSGreg Roach     *
28476692c8bSGreg Roach     * @return string
28576692c8bSGreg Roach     */
2863c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
287c1010edaSGreg Roach    {
288acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
289a25f0a04SGreg Roach
290a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
291518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
292a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2938d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
294138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
295a25f0a04SGreg Roach            }
296a25f0a04SGreg Roach        }
297a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2988d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
299a25f0a04SGreg Roach        foreach ($matches as $match) {
30024ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
301a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
302a25f0a04SGreg Roach                $rec .= $match[0];
303a25f0a04SGreg Roach            }
304a25f0a04SGreg Roach        }
305a25f0a04SGreg Roach        // Don’t privatize sex.
306a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
307a25f0a04SGreg Roach            $rec .= $match[0];
308a25f0a04SGreg Roach        }
309a25f0a04SGreg Roach
310a25f0a04SGreg Roach        return $rec;
311a25f0a04SGreg Roach    }
312a25f0a04SGreg Roach
31376692c8bSGreg Roach    /**
31476692c8bSGreg Roach     * Fetch data from the database
31576692c8bSGreg Roach     *
31676692c8bSGreg Roach     * @param string $xref
31776692c8bSGreg Roach     * @param int    $tree_id
31876692c8bSGreg Roach     *
319e364afe4SGreg Roach     * @return string|null
32076692c8bSGreg Roach     */
321e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
322c1010edaSGreg Roach    {
3232e5b4452SGreg Roach        return DB::table('individuals')
3242e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3252e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3262e5b4452SGreg Roach            ->value('i_gedcom');
327a25f0a04SGreg Roach    }
328a25f0a04SGreg Roach
329a25f0a04SGreg Roach    /**
330a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
331a25f0a04SGreg Roach     * If not known to be dead, then assume living.
332a25f0a04SGreg Roach     *
333cbc1590aSGreg Roach     * @return bool
334a25f0a04SGreg Roach     */
3358f53f488SRico Sonntag    public function isDead(): bool
336c1010edaSGreg Roach    {
337c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3384459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
339a25f0a04SGreg Roach
340a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3418d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
342a25f0a04SGreg Roach            return true;
343a25f0a04SGreg Roach        }
344a25f0a04SGreg Roach
345a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
346a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
347a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
348a25f0a04SGreg Roach                $date = new Date($date_match);
349269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
350a25f0a04SGreg Roach                    return true;
351a25f0a04SGreg Roach                }
352a25f0a04SGreg Roach            }
353a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
354a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
355a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
356a25f0a04SGreg Roach                return false;
357a25f0a04SGreg Roach            }
358a25f0a04SGreg Roach        }
359a25f0a04SGreg Roach
360a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
361a25f0a04SGreg Roach
362a25f0a04SGreg Roach        // Check parents (birth and adopted)
36339ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
36439ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
365a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
366a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
367a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
368a25f0a04SGreg Roach                    $date = new Date($date_match);
369269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
370a25f0a04SGreg Roach                        return true;
371a25f0a04SGreg Roach                    }
372a25f0a04SGreg Roach                }
373a25f0a04SGreg Roach            }
374a25f0a04SGreg Roach        }
375a25f0a04SGreg Roach
376a25f0a04SGreg Roach        // Check spouses
37739ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
378a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
379a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
380a25f0a04SGreg Roach                $date = new Date($date_match);
381a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
382269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
383a25f0a04SGreg Roach                    return true;
384a25f0a04SGreg Roach                }
385a25f0a04SGreg Roach            }
386a25f0a04SGreg Roach            // Check spouse dates
38739ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
388a25f0a04SGreg Roach            if ($spouse) {
389a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
390a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
391a25f0a04SGreg Roach                    $date = new Date($date_match);
392a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
393269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
394a25f0a04SGreg Roach                        return true;
395a25f0a04SGreg Roach                    }
396a25f0a04SGreg Roach                }
397a25f0a04SGreg Roach            }
398a25f0a04SGreg Roach            // Check child dates
39939ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
400a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
401a25f0a04SGreg Roach                // Assume children born after age of 15
402a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
403a25f0a04SGreg Roach                    $date = new Date($date_match);
404269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
405a25f0a04SGreg Roach                        return true;
406a25f0a04SGreg Roach                    }
407a25f0a04SGreg Roach                }
408a25f0a04SGreg Roach                // Check grandchildren
40939ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
41039ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
411a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
412a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
413a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
414a25f0a04SGreg Roach                            $date = new Date($date_match);
415269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
416a25f0a04SGreg Roach                                return true;
417a25f0a04SGreg Roach                            }
418a25f0a04SGreg Roach                        }
419a25f0a04SGreg Roach                    }
420a25f0a04SGreg Roach                }
421a25f0a04SGreg Roach            }
422a25f0a04SGreg Roach        }
423a25f0a04SGreg Roach
424a25f0a04SGreg Roach        return false;
425a25f0a04SGreg Roach    }
426a25f0a04SGreg Roach
427a25f0a04SGreg Roach    /**
428a25f0a04SGreg Roach     * Find the highlighted media object for an individual
429a25f0a04SGreg Roach     *
430e364afe4SGreg Roach     * @return MediaFile|null
431a25f0a04SGreg Roach     */
432e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
433c1010edaSGreg Roach    {
4348d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
435dc124885SGreg Roach            $media = $fact->target();
436a213a63cSGreg Roach            if ($media instanceof Media) {
4374a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
438a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4394a9f750fSGreg Roach                        return $media_file;
4404a9f750fSGreg Roach                    }
4414a9f750fSGreg Roach                }
442a25f0a04SGreg Roach            }
443a25f0a04SGreg Roach        }
444a25f0a04SGreg Roach
445a25f0a04SGreg Roach        return null;
446a25f0a04SGreg Roach    }
447a25f0a04SGreg Roach
448a25f0a04SGreg Roach    /**
449a25f0a04SGreg Roach     * Display the prefered image for this individual.
450a25f0a04SGreg Roach     * Use an icon if no image is available.
451a25f0a04SGreg Roach     *
452dce07401SGreg Roach     * @param int      $width      Pixels
453dce07401SGreg Roach     * @param int      $height     Pixels
454dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
455dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
456dce07401SGreg Roach     *
457a25f0a04SGreg Roach     * @return string
458a25f0a04SGreg Roach     */
4598f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
460c1010edaSGreg Roach    {
4614a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4624a9f750fSGreg Roach
4634a9f750fSGreg Roach        if ($media_file !== null) {
4644a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
465a25f0a04SGreg Roach        }
4664a9f750fSGreg Roach
4674a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
46839ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4694a9f750fSGreg Roach        }
4704a9f750fSGreg Roach
4714a9f750fSGreg Roach        return '';
472a25f0a04SGreg Roach    }
473a25f0a04SGreg Roach
474a25f0a04SGreg Roach    /**
475a25f0a04SGreg Roach     * Get the date of birth
476a25f0a04SGreg Roach     *
477a25f0a04SGreg Roach     * @return Date
478a25f0a04SGreg Roach     */
4798f53f488SRico Sonntag    public function getBirthDate(): Date
480c1010edaSGreg Roach    {
481a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
482a25f0a04SGreg Roach            if ($date->isOK()) {
483a25f0a04SGreg Roach                return $date;
484a25f0a04SGreg Roach            }
485a25f0a04SGreg Roach        }
486a25f0a04SGreg Roach
487a25f0a04SGreg Roach        return new Date('');
488a25f0a04SGreg Roach    }
489a25f0a04SGreg Roach
490a25f0a04SGreg Roach    /**
491a25f0a04SGreg Roach     * Get the place of birth
492a25f0a04SGreg Roach     *
49316d0b7f7SRico Sonntag     * @return Place
494a25f0a04SGreg Roach     */
4958f53f488SRico Sonntag    public function getBirthPlace(): Place
496c1010edaSGreg Roach    {
497a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
498a25f0a04SGreg Roach            return $place;
499a25f0a04SGreg Roach        }
500a25f0a04SGreg Roach
501b20ddbf9SGreg Roach        return new Place('', $this->tree);
502a25f0a04SGreg Roach    }
503a25f0a04SGreg Roach
504a25f0a04SGreg Roach    /**
505a25f0a04SGreg Roach     * Get the year of birth
506a25f0a04SGreg Roach     *
507a25f0a04SGreg Roach     * @return string the year of birth
508a25f0a04SGreg Roach     */
5098f53f488SRico Sonntag    public function getBirthYear(): string
510c1010edaSGreg Roach    {
511f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
512a25f0a04SGreg Roach    }
513a25f0a04SGreg Roach
514a25f0a04SGreg Roach    /**
515a25f0a04SGreg Roach     * Get the date of death
516a25f0a04SGreg Roach     *
517a25f0a04SGreg Roach     * @return Date
518a25f0a04SGreg Roach     */
5198f53f488SRico Sonntag    public function getDeathDate(): Date
520c1010edaSGreg Roach    {
521a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
522a25f0a04SGreg Roach            if ($date->isOK()) {
523a25f0a04SGreg Roach                return $date;
524a25f0a04SGreg Roach            }
525a25f0a04SGreg Roach        }
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach        return new Date('');
528a25f0a04SGreg Roach    }
529a25f0a04SGreg Roach
530a25f0a04SGreg Roach    /**
531a25f0a04SGreg Roach     * Get the place of death
532a25f0a04SGreg Roach     *
53316d0b7f7SRico Sonntag     * @return Place
534a25f0a04SGreg Roach     */
5358f53f488SRico Sonntag    public function getDeathPlace(): Place
536c1010edaSGreg Roach    {
537a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
538a25f0a04SGreg Roach            return $place;
539a25f0a04SGreg Roach        }
540a25f0a04SGreg Roach
541b20ddbf9SGreg Roach        return new Place('', $this->tree);
542a25f0a04SGreg Roach    }
543a25f0a04SGreg Roach
544a25f0a04SGreg Roach    /**
545a25f0a04SGreg Roach     * get the death year
546a25f0a04SGreg Roach     *
547a25f0a04SGreg Roach     * @return string the year of death
548a25f0a04SGreg Roach     */
5498f53f488SRico Sonntag    public function getDeathYear(): string
550c1010edaSGreg Roach    {
551f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
552a25f0a04SGreg Roach    }
553a25f0a04SGreg Roach
554a25f0a04SGreg Roach    /**
555a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
55615d603e7SGreg Roach     * Provide the place and full date using a tooltip.
557a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
558a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
559a25f0a04SGreg Roach     *
560a25f0a04SGreg Roach     * @return string
561a25f0a04SGreg Roach     */
5628f53f488SRico Sonntag    public function getLifeSpan(): string
563c1010edaSGreg Roach    {
56415d603e7SGreg Roach        // Just the first part of the place name
565392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
566392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
56715d603e7SGreg Roach        // Remove markup from dates
56815d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56915d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
57015d603e7SGreg Roach
571c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
572*dacfe33cSGreg Roach        return I18N::translate(
573a25f0a04SGreg Roach            '%1$s–%2$s',
5742d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5752d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
576a25f0a04SGreg Roach        );
577a25f0a04SGreg Roach    }
578a25f0a04SGreg Roach
579a25f0a04SGreg Roach    /**
580a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
581a25f0a04SGreg Roach     *
582a25f0a04SGreg Roach     * @return Date[]
583a25f0a04SGreg Roach     */
5848f53f488SRico Sonntag    public function getAllBirthDates(): array
585c1010edaSGreg Roach    {
5868d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5878d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
588a25f0a04SGreg Roach            if ($tmp) {
589a25f0a04SGreg Roach                return $tmp;
590a25f0a04SGreg Roach            }
591a25f0a04SGreg Roach        }
592a25f0a04SGreg Roach
59313abd6f3SGreg Roach        return [];
594a25f0a04SGreg Roach    }
595a25f0a04SGreg Roach
596a25f0a04SGreg Roach    /**
597a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
598a25f0a04SGreg Roach     *
5994080d558SGreg Roach     * @return Place[]
600a25f0a04SGreg Roach     */
6018f53f488SRico Sonntag    public function getAllBirthPlaces(): array
602c1010edaSGreg Roach    {
6038d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
6048d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
60554c1ab5eSGreg Roach            if ($places !== []) {
6064080d558SGreg Roach                return $places;
607a25f0a04SGreg Roach            }
608a25f0a04SGreg Roach        }
609a25f0a04SGreg Roach
61013abd6f3SGreg Roach        return [];
611a25f0a04SGreg Roach    }
612a25f0a04SGreg Roach
613a25f0a04SGreg Roach    /**
614a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
615a25f0a04SGreg Roach     *
616a25f0a04SGreg Roach     * @return Date[]
617a25f0a04SGreg Roach     */
6188f53f488SRico Sonntag    public function getAllDeathDates(): array
619c1010edaSGreg Roach    {
6208d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6218d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
622a25f0a04SGreg Roach            if ($tmp) {
623a25f0a04SGreg Roach                return $tmp;
624a25f0a04SGreg Roach            }
625a25f0a04SGreg Roach        }
626a25f0a04SGreg Roach
62713abd6f3SGreg Roach        return [];
628a25f0a04SGreg Roach    }
629a25f0a04SGreg Roach
630a25f0a04SGreg Roach    /**
631a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
632a25f0a04SGreg Roach     *
6334080d558SGreg Roach     * @return Place[]
634a25f0a04SGreg Roach     */
6358f53f488SRico Sonntag    public function getAllDeathPlaces(): array
636c1010edaSGreg Roach    {
6378d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6388d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
63954c1ab5eSGreg Roach            if ($places !== []) {
6404080d558SGreg Roach                return $places;
641a25f0a04SGreg Roach            }
642a25f0a04SGreg Roach        }
643a25f0a04SGreg Roach
64413abd6f3SGreg Roach        return [];
645a25f0a04SGreg Roach    }
646a25f0a04SGreg Roach
647a25f0a04SGreg Roach    /**
648a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
649a25f0a04SGreg Roach     *
650a25f0a04SGreg Roach     * @return Date
651a25f0a04SGreg Roach     */
6528f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
653c1010edaSGreg Roach    {
6548f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
655a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
656a25f0a04SGreg Roach                if ($date->isOK()) {
6574686330aSGreg Roach                    $this->estimated_birth_date = $date;
658a25f0a04SGreg Roach                    break;
659a25f0a04SGreg Roach                }
660a25f0a04SGreg Roach            }
6618f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
66213abd6f3SGreg Roach                $min = [];
66313abd6f3SGreg Roach                $max = [];
664a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
665f5b60decSGreg Roach                if ($tmp->isOK()) {
666f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
667f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
668a25f0a04SGreg Roach                }
66939ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
670a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
671f5b60decSGreg Roach                    if ($tmp->isOK()) {
672f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
673f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
674a25f0a04SGreg Roach                    }
67539ca88baSGreg Roach                    $husband = $family->husband();
676e364afe4SGreg Roach                    if ($husband instanceof self) {
6772e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
678f5b60decSGreg Roach                        if ($tmp->isOK()) {
679f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
680f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
681a25f0a04SGreg Roach                        }
682a25f0a04SGreg Roach                    }
68339ca88baSGreg Roach                    $wife = $family->wife();
684e364afe4SGreg Roach                    if ($wife instanceof self) {
6852e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
686f5b60decSGreg Roach                        if ($tmp->isOK()) {
687f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
688f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
689a25f0a04SGreg Roach                        }
690a25f0a04SGreg Roach                    }
69139ca88baSGreg Roach                    foreach ($family->children() as $child) {
692a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
693f5b60decSGreg Roach                        if ($tmp->isOK()) {
694f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
695f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
696a25f0a04SGreg Roach                        }
697a25f0a04SGreg Roach                    }
698a25f0a04SGreg Roach                }
69939ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
700a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
701f5b60decSGreg Roach                    if ($tmp->isOK()) {
702f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
703f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
704a25f0a04SGreg Roach                    }
70539ca88baSGreg Roach                    $spouse = $family->spouse($this);
706a25f0a04SGreg Roach                    if ($spouse) {
707a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
708f5b60decSGreg Roach                        if ($tmp->isOK()) {
709f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
710f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
711a25f0a04SGreg Roach                        }
712a25f0a04SGreg Roach                    }
71339ca88baSGreg Roach                    foreach ($family->children() as $child) {
714a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
715f5b60decSGreg Roach                        if ($tmp->isOK()) {
716e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
717f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
718a25f0a04SGreg Roach                        }
719a25f0a04SGreg Roach                    }
720a25f0a04SGreg Roach                }
721a25f0a04SGreg Roach                if ($min && $max) {
72259f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
723a25f0a04SGreg Roach
72465e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7254686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
726a25f0a04SGreg Roach                } else {
7274686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
728a25f0a04SGreg Roach                }
729a25f0a04SGreg Roach            }
730a25f0a04SGreg Roach        }
731a25f0a04SGreg Roach
7324686330aSGreg Roach        return $this->estimated_birth_date;
733a25f0a04SGreg Roach    }
734a25f0a04SGreg Roach
735a25f0a04SGreg Roach    /**
736a25f0a04SGreg Roach     * Generate an estimated date of death.
737a25f0a04SGreg Roach     *
738a25f0a04SGreg Roach     * @return Date
739a25f0a04SGreg Roach     */
7408f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
741c1010edaSGreg Roach    {
7424686330aSGreg Roach        if ($this->estimated_death_date === null) {
743a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
744a25f0a04SGreg Roach                if ($date->isOK()) {
7454686330aSGreg Roach                    $this->estimated_death_date = $date;
746a25f0a04SGreg Roach                    break;
747a25f0a04SGreg Roach                }
748a25f0a04SGreg Roach            }
7494686330aSGreg Roach            if ($this->estimated_death_date === null) {
750f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
751c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7524686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
753a25f0a04SGreg Roach                } else {
7544686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
755a25f0a04SGreg Roach                }
756a25f0a04SGreg Roach            }
757a25f0a04SGreg Roach        }
758a25f0a04SGreg Roach
7594686330aSGreg Roach        return $this->estimated_death_date;
760a25f0a04SGreg Roach    }
761a25f0a04SGreg Roach
762a25f0a04SGreg Roach    /**
763a25f0a04SGreg Roach     * Get the sex - M F or U
764a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
765a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
766a25f0a04SGreg Roach     *
767a25f0a04SGreg Roach     * @return string
768a25f0a04SGreg Roach     */
769e364afe4SGreg Roach    public function sex(): string
770c1010edaSGreg Roach    {
771a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
772a25f0a04SGreg Roach            return $match[1];
773a25f0a04SGreg Roach        }
774b2ce94c6SRico Sonntag
775b2ce94c6SRico Sonntag        return 'U';
776a25f0a04SGreg Roach    }
777a25f0a04SGreg Roach
778a25f0a04SGreg Roach    /**
779a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
780a25f0a04SGreg Roach     *
781a25f0a04SGreg Roach     * @return string
782a25f0a04SGreg Roach     */
7838f53f488SRico Sonntag    public function getBoxStyle(): string
784c1010edaSGreg Roach    {
785c1010edaSGreg Roach        $tmp = [
786c1010edaSGreg Roach            'M' => '',
787c1010edaSGreg Roach            'F' => 'F',
788c1010edaSGreg Roach            'U' => 'NN',
789c1010edaSGreg Roach        ];
790a25f0a04SGreg Roach
79139ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
792a25f0a04SGreg Roach    }
793a25f0a04SGreg Roach
794a25f0a04SGreg Roach    /**
795a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
796a25f0a04SGreg Roach     *
797cbc1590aSGreg Roach     * @param int|null $access_level
798a25f0a04SGreg Roach     *
79954c7f8dfSGreg Roach     * @return Collection
800a25f0a04SGreg Roach     */
80139ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
802c1010edaSGreg Roach    {
8034b9ff166SGreg Roach        if ($access_level === null) {
8044b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8054b9ff166SGreg Roach        }
8064b9ff166SGreg Roach
807acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
808a25f0a04SGreg Roach
80939ca88baSGreg Roach        $families = new Collection();
8108d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
811dc124885SGreg Roach            $family = $fact->target();
812e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
81339ca88baSGreg Roach                $families->push($family);
814a25f0a04SGreg Roach            }
815a25f0a04SGreg Roach        }
816a25f0a04SGreg Roach
81739ca88baSGreg Roach        return new Collection($families);
818a25f0a04SGreg Roach    }
819a25f0a04SGreg Roach
820a25f0a04SGreg Roach    /**
821a25f0a04SGreg Roach     * Get the current spouse of this individual.
822a25f0a04SGreg Roach     *
823a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
824a25f0a04SGreg Roach     * in chronological order, and take the last one found.
825a25f0a04SGreg Roach     *
826a25f0a04SGreg Roach     * @return Individual|null
827a25f0a04SGreg Roach     */
828e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
829c1010edaSGreg Roach    {
83039ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
83139ca88baSGreg Roach
83239ca88baSGreg Roach        if ($family instanceof Family) {
83339ca88baSGreg Roach            return $family->spouse($this);
834a25f0a04SGreg Roach        }
835b2ce94c6SRico Sonntag
836b2ce94c6SRico Sonntag        return null;
837a25f0a04SGreg Roach    }
838a25f0a04SGreg Roach
839a25f0a04SGreg Roach    /**
840a25f0a04SGreg Roach     * Count the children belonging to this individual.
841a25f0a04SGreg Roach     *
842cbc1590aSGreg Roach     * @return int
843a25f0a04SGreg Roach     */
844e364afe4SGreg Roach    public function numberOfChildren(): int
845c1010edaSGreg Roach    {
8467d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8473dc7bbe9SGreg Roach            return (int) $match[1];
848b2ce94c6SRico Sonntag        }
849b2ce94c6SRico Sonntag
85013abd6f3SGreg Roach        $children = [];
85139ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
85239ca88baSGreg Roach            foreach ($fam->children() as $child) {
853c0935879SGreg Roach                $children[$child->xref()] = true;
854a25f0a04SGreg Roach            }
855a25f0a04SGreg Roach        }
856a25f0a04SGreg Roach
857a25f0a04SGreg Roach        return count($children);
858a25f0a04SGreg Roach    }
859a25f0a04SGreg Roach
860a25f0a04SGreg Roach    /**
861a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
862a25f0a04SGreg Roach     *
863cbc1590aSGreg Roach     * @param int|null $access_level
864a25f0a04SGreg Roach     *
86554c7f8dfSGreg Roach     * @return Collection
866a25f0a04SGreg Roach     */
86739ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
868c1010edaSGreg Roach    {
8694b9ff166SGreg Roach        if ($access_level === null) {
8704b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8714b9ff166SGreg Roach        }
8724b9ff166SGreg Roach
873acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
874a25f0a04SGreg Roach
87539ca88baSGreg Roach        $families = new Collection();
87639ca88baSGreg Roach
8778d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
878dc124885SGreg Roach            $family = $fact->target();
879e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
88039ca88baSGreg Roach                $families->push($family);
881a25f0a04SGreg Roach            }
882a25f0a04SGreg Roach        }
883a25f0a04SGreg Roach
884a25f0a04SGreg Roach        return $families;
885a25f0a04SGreg Roach    }
886a25f0a04SGreg Roach
887a25f0a04SGreg Roach    /**
888a25f0a04SGreg Roach     * Get the preferred parents for this individual.
889a25f0a04SGreg Roach     *
890a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
891a25f0a04SGreg Roach     * The preferred family record is:
892a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
893a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
894a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
895a25f0a04SGreg Roach     * (d) the first one found
896a25f0a04SGreg Roach     *
897a25f0a04SGreg Roach     * @return Family|null
898a25f0a04SGreg Roach     */
899e364afe4SGreg Roach    public function primaryChildFamily(): ?Family
900c1010edaSGreg Roach    {
90139ca88baSGreg Roach        $families = $this->childFamilies();
9023b092cb5SGreg Roach        switch ($families->count()) {
903a25f0a04SGreg Roach            case 0:
904a25f0a04SGreg Roach                return null;
905a25f0a04SGreg Roach            case 1:
90615d603e7SGreg Roach                return $families[0];
907a25f0a04SGreg Roach            default:
908a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
909a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
91074e78bfaSJonathan Jaubart                foreach ($families as $fam) {
911c0935879SGreg Roach                    $famid = $fam->xref();
9127d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
913a25f0a04SGreg Roach                        return $fam;
914a25f0a04SGreg Roach                    }
915a25f0a04SGreg Roach                }
916a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
91774e78bfaSJonathan Jaubart                foreach ($families as $fam) {
918c0935879SGreg Roach                    $famid = $fam->xref();
9197d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
920a25f0a04SGreg Roach                        return $fam;
921a25f0a04SGreg Roach                    }
922a25f0a04SGreg Roach                }
923a25f0a04SGreg Roach                // c) records with no '2 PEDI'
92474e78bfaSJonathan Jaubart                foreach ($families as $fam) {
925c0935879SGreg Roach                    $famid = $fam->xref();
9267d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
927a25f0a04SGreg Roach                        return $fam;
928a25f0a04SGreg Roach                    }
929a25f0a04SGreg Roach                }
930a25f0a04SGreg Roach
931a25f0a04SGreg Roach                // d) any record
93215d603e7SGreg Roach                return $families[0];
933a25f0a04SGreg Roach        }
934a25f0a04SGreg Roach    }
935a25f0a04SGreg Roach
936a25f0a04SGreg Roach    /**
937a25f0a04SGreg Roach     * Get a list of step-parent families.
938a25f0a04SGreg Roach     *
93954c7f8dfSGreg Roach     * @return Collection
940a25f0a04SGreg Roach     */
941820b62dfSGreg Roach    public function childStepFamilies(): Collection
942c1010edaSGreg Roach    {
94313abd6f3SGreg Roach        $step_families = [];
94439ca88baSGreg Roach        $families      = $this->childFamilies();
945a25f0a04SGreg Roach        foreach ($families as $family) {
94639ca88baSGreg Roach            $father = $family->husband();
947a25f0a04SGreg Roach            if ($father) {
94839ca88baSGreg Roach                foreach ($father->spouseFamilies() as $step_family) {
94939ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
950a25f0a04SGreg Roach                        $step_families[] = $step_family;
951a25f0a04SGreg Roach                    }
952a25f0a04SGreg Roach                }
953a25f0a04SGreg Roach            }
95439ca88baSGreg Roach            $mother = $family->wife();
955a25f0a04SGreg Roach            if ($mother) {
95639ca88baSGreg Roach                foreach ($mother->spouseFamilies() as $step_family) {
95739ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
958a25f0a04SGreg Roach                        $step_families[] = $step_family;
959a25f0a04SGreg Roach                    }
960a25f0a04SGreg Roach                }
961a25f0a04SGreg Roach            }
962a25f0a04SGreg Roach        }
963a25f0a04SGreg Roach
964820b62dfSGreg Roach        return new Collection($step_families);
965a25f0a04SGreg Roach    }
966a25f0a04SGreg Roach
967a25f0a04SGreg Roach    /**
968a25f0a04SGreg Roach     * Get a list of step-parent families.
969a25f0a04SGreg Roach     *
97054c7f8dfSGreg Roach     * @return Collection
971a25f0a04SGreg Roach     */
972820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
973c1010edaSGreg Roach    {
97413abd6f3SGreg Roach        $step_families = [];
97539ca88baSGreg Roach        $families      = $this->spouseFamilies();
976820b62dfSGreg Roach
977a25f0a04SGreg Roach        foreach ($families as $family) {
97839ca88baSGreg Roach            $spouse = $family->spouse($this);
979820b62dfSGreg Roach
980a25f0a04SGreg Roach            if ($spouse) {
98139ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
98239ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
983a25f0a04SGreg Roach                        $step_families[] = $step_family;
984a25f0a04SGreg Roach                    }
985a25f0a04SGreg Roach                }
986a25f0a04SGreg Roach            }
987a25f0a04SGreg Roach        }
988a25f0a04SGreg Roach
989820b62dfSGreg Roach        return new Collection($step_families);
990a25f0a04SGreg Roach    }
991a25f0a04SGreg Roach
992a25f0a04SGreg Roach    /**
993a25f0a04SGreg Roach     * A label for a parental family group
994a25f0a04SGreg Roach     *
995a25f0a04SGreg Roach     * @param Family $family
996a25f0a04SGreg Roach     *
997a25f0a04SGreg Roach     * @return string
998a25f0a04SGreg Roach     */
999820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
1000c1010edaSGreg Roach    {
10017d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
1002a25f0a04SGreg Roach            // A specified pedigree
1003764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1004b2ce94c6SRico Sonntag        }
1005b2ce94c6SRico Sonntag
1006a25f0a04SGreg Roach        // Default (birth) pedigree
1007764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1008a25f0a04SGreg Roach    }
1009a25f0a04SGreg Roach
1010a25f0a04SGreg Roach    /**
1011a25f0a04SGreg Roach     * Create a label for a step family
1012a25f0a04SGreg Roach     *
1013a25f0a04SGreg Roach     * @param Family $step_family
1014a25f0a04SGreg Roach     *
1015a25f0a04SGreg Roach     * @return string
1016a25f0a04SGreg Roach     */
10178f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1018c1010edaSGreg Roach    {
101939ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
1020a25f0a04SGreg Roach            if ($family !== $step_family) {
1021a25f0a04SGreg Roach                // Must be a step-family
102239ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
102339ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
1024a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1025a25f0a04SGreg Roach                            // One common parent - must be a step family
1026e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
1027a25f0a04SGreg Roach                                // Father’s family with someone else
102839ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
1029a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
103039ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
1031b2ce94c6SRico Sonntag                                }
1032b2ce94c6SRico Sonntag
1033a25f0a04SGreg Roach                                /* I18N: A step-family. */
1034bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1035a25f0a04SGreg Roach                            }
1036b2ce94c6SRico Sonntag
1037a25f0a04SGreg Roach                            // Mother’s family with someone else
103839ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
1039a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
104039ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
1041b2ce94c6SRico Sonntag                            }
1042b2ce94c6SRico Sonntag
1043a25f0a04SGreg Roach                            /* I18N: A step-family. */
1044bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1045a25f0a04SGreg Roach                        }
1046a25f0a04SGreg Roach                    }
1047a25f0a04SGreg Roach                }
1048a25f0a04SGreg Roach            }
1049a25f0a04SGreg Roach        }
1050a25f0a04SGreg Roach
1051a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1052a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1053a25f0a04SGreg Roach    }
1054a25f0a04SGreg Roach
1055225e381fSGreg Roach    /**
1056225e381fSGreg Roach     * Get the description for the family.
1057225e381fSGreg Roach     *
1058225e381fSGreg Roach     * For example, "XXX's family with new wife".
1059225e381fSGreg Roach     *
1060225e381fSGreg Roach     * @param Family $family
1061225e381fSGreg Roach     *
1062225e381fSGreg Roach     * @return string
1063225e381fSGreg Roach     */
1064e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
1065c1010edaSGreg Roach    {
106639ca88baSGreg Roach        $spouse = $family->spouse($this);
1067225e381fSGreg Roach        if ($spouse) {
1068225e381fSGreg Roach            /* I18N: %s is the spouse name */
106939ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1070225e381fSGreg Roach        }
1071b2ce94c6SRico Sonntag
107239ca88baSGreg Roach        return $family->fullName();
1073225e381fSGreg Roach    }
1074225e381fSGreg Roach
1075a25f0a04SGreg Roach    /**
1076a25f0a04SGreg Roach     * get primary parents names for this individual
1077a25f0a04SGreg Roach     *
1078a25f0a04SGreg Roach     * @param string $classname optional css class
1079a25f0a04SGreg Roach     * @param string $display   optional css style display
1080a25f0a04SGreg Roach     *
1081a25f0a04SGreg Roach     * @return string a div block with father & mother names
1082a25f0a04SGreg Roach     */
10838f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1084c1010edaSGreg Roach    {
108539ca88baSGreg Roach        $fam = $this->primaryChildFamily();
1086a25f0a04SGreg Roach        if (!$fam) {
1087a25f0a04SGreg Roach            return '';
1088a25f0a04SGreg Roach        }
1089a25f0a04SGreg Roach        $txt = '<div';
1090a25f0a04SGreg Roach        if ($classname) {
10914c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1092a25f0a04SGreg Roach        }
1093a25f0a04SGreg Roach        if ($display) {
10944c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1095a25f0a04SGreg Roach        }
1096a25f0a04SGreg Roach        $txt .= '>';
109739ca88baSGreg Roach        $husb = $fam->husband();
1098a25f0a04SGreg Roach        if ($husb) {
1099a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1100a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1101a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1102a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1103a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
110439ca88baSGreg Roach            $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>';
1105a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1106a25f0a04SGreg Roach        }
110739ca88baSGreg Roach        $wife = $fam->wife();
1108a25f0a04SGreg Roach        if ($wife) {
1109a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1110a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1111a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1112a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1113a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
111439ca88baSGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->fullName());
1115a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1116a25f0a04SGreg Roach        }
1117a25f0a04SGreg Roach        $txt .= '</div>';
1118a25f0a04SGreg Roach
1119a25f0a04SGreg Roach        return $txt;
1120a25f0a04SGreg Roach    }
1121a25f0a04SGreg Roach
1122961ec755SGreg Roach    /**
1123961ec755SGreg Roach     * If this object has no name, what do we call it?
1124961ec755SGreg Roach     *
1125961ec755SGreg Roach     * @return string
1126961ec755SGreg Roach     */
11278f53f488SRico Sonntag    public function getFallBackName(): string
1128c1010edaSGreg Roach    {
1129a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1130a25f0a04SGreg Roach    }
1131a25f0a04SGreg Roach
1132a25f0a04SGreg Roach    /**
1133a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1134a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1135a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1136a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1137a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1138a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1139a25f0a04SGreg Roach     *
1140a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1141a25f0a04SGreg Roach     * 2 GIVN Robert
1142a25f0a04SGreg Roach     * 2 SPFX de
1143a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1144a25f0a04SGreg Roach     * 2 NICK The Bald
1145a25f0a04SGreg Roach     *
1146a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1147a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1148a25f0a04SGreg Roach     *
1149a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1150a25f0a04SGreg Roach     *
1151a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1152a25f0a04SGreg Roach     * or
1153a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1154a25f0a04SGreg Roach     * 2 GIVN Carlos
1155a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1156a25f0a04SGreg Roach     *
1157a25f0a04SGreg Roach     * @param string $type
1158a25f0a04SGreg Roach     * @param string $full
1159a25f0a04SGreg Roach     * @param string $gedcom
1160e364afe4SGreg Roach     *
1161e364afe4SGreg Roach     * @return void
1162a25f0a04SGreg Roach     */
1163e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1164c1010edaSGreg Roach    {
1165a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1166a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1167a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1168a25f0a04SGreg Roach
116976f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1170a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1171a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1172a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1173a25f0a04SGreg Roach
1174a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
117576f666f4SGreg Roach        if ($SURN !== '') {
1176a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1177a25f0a04SGreg Roach        } else {
117813abd6f3SGreg Roach            $SURNS = [];
1179a25f0a04SGreg Roach        }
118076f666f4SGreg Roach
1181a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1182a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1183a25f0a04SGreg Roach
1184a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1185a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1186a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1187a25f0a04SGreg Roach
1188a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
118976f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1190e364afe4SGreg Roach            $full .= '/';
1191a25f0a04SGreg Roach        }
1192a25f0a04SGreg Roach
1193a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1194a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1195a25f0a04SGreg Roach
1196a25f0a04SGreg Roach        // Extract the surname.
1197a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1198a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1199a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1200a25f0a04SGreg Roach        } else {
1201a25f0a04SGreg Roach            $surname = '';
1202a25f0a04SGreg Roach        }
1203a25f0a04SGreg Roach
1204a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1205a25f0a04SGreg Roach        if (!$SURNS) {
1206a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1207a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1208a25f0a04SGreg Roach                $SURNS = $matches[1];
1209a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1210a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1211a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1212a25f0a04SGreg Roach                }
1213a25f0a04SGreg Roach            } else {
1214a25f0a04SGreg Roach                // It is valid not to have a surname at all
121513abd6f3SGreg Roach                $SURNS = [''];
1216a25f0a04SGreg Roach            }
1217a25f0a04SGreg Roach        }
1218a25f0a04SGreg Roach
1219a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1220a25f0a04SGreg Roach        if (!$GIVN) {
1221a25f0a04SGreg Roach            $GIVN = preg_replace(
122213abd6f3SGreg Roach                [
1223c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1224c1010edaSGreg Roach                    // remove surname
1225c1010edaSGreg Roach                    '/ ?".+"/',
1226c1010edaSGreg Roach                    // remove nickname
1227c1010edaSGreg Roach                    '/ {2,}/',
1228c1010edaSGreg Roach                    // multiple spaces, caused by the above
1229c1010edaSGreg Roach                    '/^ | $/',
1230c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
123113abd6f3SGreg Roach                ],
123213abd6f3SGreg Roach                [
1233a25f0a04SGreg Roach                    ' ',
1234a25f0a04SGreg Roach                    ' ',
1235a25f0a04SGreg Roach                    ' ',
1236a25f0a04SGreg Roach                    '',
123713abd6f3SGreg Roach                ],
1238a25f0a04SGreg Roach                $full
1239a25f0a04SGreg Roach            );
1240a25f0a04SGreg Roach        }
1241a25f0a04SGreg Roach
1242a25f0a04SGreg Roach        // Add placeholder for unknown given name
1243a25f0a04SGreg Roach        if (!$GIVN) {
1244a25f0a04SGreg Roach            $GIVN = '@P.N.';
124573f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1246a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1247a25f0a04SGreg Roach        }
1248a25f0a04SGreg Roach
12497b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12507b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1251c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12527b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1253c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1254a25f0a04SGreg Roach        }
1255a25f0a04SGreg Roach
1256a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1257a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1258a25f0a04SGreg Roach        // $full is for display on-screen
1259a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1260a25f0a04SGreg Roach
1261a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1262ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1263ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1264c6f196c3SGreg Roach        // Format for display
1265d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1266acc34ea1SGreg Roach        // Localise quotation marks around the nickname
12670b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
12688d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12698d68cabeSGreg Roach        }, $full);
1270a25f0a04SGreg Roach
1271c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1272a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1273a25f0a04SGreg Roach
1274a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1275a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1276a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1277a25f0a04SGreg Roach
1278ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1279a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1280e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1281a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1282e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1283a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1284a25f0a04SGreg Roach            }
1285a25f0a04SGreg Roach
1286bdb3725aSGreg Roach            $this->getAllNames[] = [
1287a25f0a04SGreg Roach                'type'    => $type,
1288a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1289c1010edaSGreg Roach                'full'    => $full,
1290c1010edaSGreg Roach                // This is used for display
1291c1010edaSGreg Roach                'fullNN'  => $fullNN,
1292c1010edaSGreg Roach                // This goes into the database
1293c1010edaSGreg Roach                'surname' => $surname,
1294c1010edaSGreg Roach                // This goes into the database
1295c1010edaSGreg Roach                'givn'    => $GIVN,
1296c1010edaSGreg Roach                // This goes into the database
1297c1010edaSGreg Roach                'surn'    => $SURN,
1298c1010edaSGreg Roach                // This goes into the database
129913abd6f3SGreg Roach            ];
1300a25f0a04SGreg Roach        }
1301a25f0a04SGreg Roach    }
1302a25f0a04SGreg Roach
1303a25f0a04SGreg Roach    /**
130476692c8bSGreg Roach     * Extract names from the GEDCOM record.
1305c7ff4153SGreg Roach     *
1306c7ff4153SGreg Roach     * @return void
1307a25f0a04SGreg Roach     */
1308e364afe4SGreg Roach    public function extractNames(): void
1309c1010edaSGreg Roach    {
13108f53f488SRico Sonntag        $this->extractNamesFromFacts(
13118f53f488SRico Sonntag            1,
13128f53f488SRico Sonntag            'NAME',
131330158ae7SGreg Roach            $this->facts(
13148d0ebef0SGreg Roach                ['NAME'],
13158f53f488SRico Sonntag                false,
13168f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13178f53f488SRico Sonntag                $this->canShowName()
13188f53f488SRico Sonntag            )
13198f53f488SRico Sonntag        );
1320a25f0a04SGreg Roach    }
1321a25f0a04SGreg Roach
1322a25f0a04SGreg Roach    /**
1323a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1324a25f0a04SGreg Roach     * selection items or favorites.
1325a25f0a04SGreg Roach     *
1326a25f0a04SGreg Roach     * @return string
1327a25f0a04SGreg Roach     */
13288f53f488SRico Sonntag    public function formatListDetails(): string
1329c1010edaSGreg Roach    {
1330a25f0a04SGreg Roach        return
13318d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13328d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1333a25f0a04SGreg Roach    }
1334a25f0a04SGreg Roach}
1335