xref: /webtrees/app/Individual.php (revision d5ad3db07e8881039a1b485e037789531d265b51)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
236ccdf4f0SGreg Roachuse Exception;
24a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
26852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
272e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2839ca88baSGreg Roachuse Illuminate\Support\Collection;
29886b77daSGreg Roachuse stdClass;
30a25f0a04SGreg Roach
31a25f0a04SGreg Roach/**
3276692c8bSGreg Roach * A GEDCOM individual (INDI) object.
33a25f0a04SGreg Roach */
34c1010edaSGreg Roachclass Individual extends GedcomRecord
35c1010edaSGreg Roach{
3616d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3716d6367aSGreg Roach
38852ede8cSGreg Roach    protected const ROUTE_NAME = IndividualPage::class;
39a25f0a04SGreg Roach
4076692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
4176692c8bSGreg Roach    public $generation;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
444686330aSGreg Roach    private $estimated_birth_date;
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach    /** @var Date The estimated date of death */
474686330aSGreg Roach    private $estimated_death_date;
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach    /**
50886b77daSGreg Roach     * A closure which will create a record from a database row.
51886b77daSGreg Roach     *
52*d5ad3db0SGreg Roach     * @param Tree $tree
53*d5ad3db0SGreg Roach     *
54886b77daSGreg Roach     * @return Closure
55886b77daSGreg Roach     */
56*d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
57886b77daSGreg Roach    {
58*d5ad3db0SGreg Roach        return static function (stdClass $row) use ($tree): Individual {
59*d5ad3db0SGreg Roach            $individual = Individual::getInstance($row->i_id, $tree, $row->i_gedcom);
60*d5ad3db0SGreg Roach            assert($individual instanceof Individual);
61e84cf2deSGreg Roach
62*d5ad3db0SGreg Roach            // Some queries include the names table.
63*d5ad3db0SGreg Roach            // For these we must select the specified name.
64*d5ad3db0SGreg Roach            if (($row->n_num ?? null) !== null) {
65e0458bdcSGreg Roach                $individual = clone $individual;
66e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
67e0458bdcSGreg Roach
68e0458bdcSGreg Roach                return $individual;
69e84cf2deSGreg Roach            }
70e84cf2deSGreg Roach
71e84cf2deSGreg Roach            return $individual;
72886b77daSGreg Roach        };
73886b77daSGreg Roach    }
74886b77daSGreg Roach
75886b77daSGreg Roach    /**
76c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
77c156e8f5SGreg Roach     *
78c156e8f5SGreg Roach     * @return Closure
79c156e8f5SGreg Roach     */
80c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
81c156e8f5SGreg Roach    {
826c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
83c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
84c156e8f5SGreg Roach        };
85c156e8f5SGreg Roach    }
86c156e8f5SGreg Roach
87c156e8f5SGreg Roach    /**
88c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
89c156e8f5SGreg Roach     *
90c156e8f5SGreg Roach     * @return Closure
91c156e8f5SGreg Roach     */
92c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
93c156e8f5SGreg Roach    {
946c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
95c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
96c156e8f5SGreg Roach        };
97c156e8f5SGreg Roach    }
98c156e8f5SGreg Roach
99c156e8f5SGreg Roach    /**
100e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
101e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
102e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
103e71ef9d2SGreg Roach     *
104e71ef9d2SGreg Roach     * @param string      $xref
105e71ef9d2SGreg Roach     * @param Tree        $tree
106e71ef9d2SGreg Roach     * @param string|null $gedcom
107e71ef9d2SGreg Roach     *
1086ccdf4f0SGreg Roach     * @throws Exception
109e71ef9d2SGreg Roach     * @return Individual|null
110e71ef9d2SGreg Roach     */
111e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
112c1010edaSGreg Roach    {
113e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
114e71ef9d2SGreg Roach
115e364afe4SGreg Roach        if ($record instanceof self) {
116e71ef9d2SGreg Roach            return $record;
117e71ef9d2SGreg Roach        }
118b2ce94c6SRico Sonntag
119b2ce94c6SRico Sonntag        return null;
120e71ef9d2SGreg Roach    }
121e71ef9d2SGreg Roach
122e71ef9d2SGreg Roach    /**
123395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
124395f0fe0SGreg Roach     * Typically when we load families and their members.
125395f0fe0SGreg Roach     *
126395f0fe0SGreg Roach     * @param Tree     $tree
1274a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
12818d7a90dSGreg Roach     *
12918d7a90dSGreg Roach     * @return void
130395f0fe0SGreg Roach     */
1312e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
132c1010edaSGreg Roach    {
1332e5b4452SGreg Roach        $rows = DB::table('individuals')
1342e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1352e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1362e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1372e5b4452SGreg Roach            ->get();
138395f0fe0SGreg Roach
139395f0fe0SGreg Roach        foreach ($rows as $row) {
140395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
141395f0fe0SGreg Roach        }
142395f0fe0SGreg Roach    }
143395f0fe0SGreg Roach
144395f0fe0SGreg Roach    /**
145a25f0a04SGreg Roach     * Can the name of this record be shown?
146a25f0a04SGreg Roach     *
14776692c8bSGreg Roach     * @param int|null $access_level
14876692c8bSGreg Roach     *
14976692c8bSGreg Roach     * @return bool
150a25f0a04SGreg Roach     */
15135584196SGreg Roach    public function canShowName(int $access_level = null): bool
152c1010edaSGreg Roach    {
1534b9ff166SGreg Roach        if ($access_level === null) {
1544b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1554b9ff166SGreg Roach        }
1564b9ff166SGreg Roach
157518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
158a25f0a04SGreg Roach    }
159a25f0a04SGreg Roach
160a25f0a04SGreg Roach    /**
16176692c8bSGreg Roach     * Can this individual be shown?
162a25f0a04SGreg Roach     *
16376692c8bSGreg Roach     * @param int $access_level
16476692c8bSGreg Roach     *
16576692c8bSGreg Roach     * @return bool
166a25f0a04SGreg Roach     */
16735584196SGreg Roach    protected function canShowByType(int $access_level): bool
168c1010edaSGreg Roach    {
169a25f0a04SGreg Roach        // Dead people...
170518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
171a25f0a04SGreg Roach            $keep_alive             = false;
17254ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
173a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1748d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
175a25f0a04SGreg Roach                foreach ($matches as $match) {
176a25f0a04SGreg Roach                    $date = new Date($match[1]);
177a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
178a25f0a04SGreg Roach                        $keep_alive = true;
179a25f0a04SGreg Roach                        break;
180a25f0a04SGreg Roach                    }
181a25f0a04SGreg Roach                }
182a25f0a04SGreg Roach            }
18354ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
184a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1858d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
186a25f0a04SGreg Roach                foreach ($matches as $match) {
187a25f0a04SGreg Roach                    $date = new Date($match[1]);
188a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
189a25f0a04SGreg Roach                        $keep_alive = true;
190a25f0a04SGreg Roach                        break;
191a25f0a04SGreg Roach                    }
192a25f0a04SGreg Roach                }
193a25f0a04SGreg Roach            }
194a25f0a04SGreg Roach            if (!$keep_alive) {
195a25f0a04SGreg Roach                return true;
196a25f0a04SGreg Roach            }
197a25f0a04SGreg Roach        }
198a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
1997c4add84SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_PATH_LENGTH);
2007c4add84SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF);
2017c4add84SGreg Roach
20254ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
2034b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
204a25f0a04SGreg Roach        }
205a25f0a04SGreg Roach
206a25f0a04SGreg Roach        // No restriction found - show living people to members only:
2074b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
208a25f0a04SGreg Roach    }
209a25f0a04SGreg Roach
210a25f0a04SGreg Roach    /**
211a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
212a25f0a04SGreg Roach     *
213a25f0a04SGreg Roach     * @param Individual $target
214cbc1590aSGreg Roach     * @param int        $distance
215a25f0a04SGreg Roach     *
216cbc1590aSGreg Roach     * @return bool
217a25f0a04SGreg Roach     */
2188f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
219c1010edaSGreg Roach    {
220a25f0a04SGreg Roach        static $cache = null;
221a25f0a04SGreg Roach
2227c4add84SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $target->tree);
223a25f0a04SGreg Roach        if ($user_individual) {
224a25f0a04SGreg Roach            if (!$cache) {
22513abd6f3SGreg Roach                $cache = [
22613abd6f3SGreg Roach                    0 => [$user_individual],
22713abd6f3SGreg Roach                    1 => [],
22813abd6f3SGreg Roach                ];
2298d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
230dc124885SGreg Roach                    $family = $fact->target();
231e24444eeSGreg Roach                    if ($family instanceof Family) {
232a25f0a04SGreg Roach                        $cache[1][] = $family;
233a25f0a04SGreg Roach                    }
234a25f0a04SGreg Roach                }
235a25f0a04SGreg Roach            }
236a25f0a04SGreg Roach        } else {
237a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
238a25f0a04SGreg Roach            return true;
239a25f0a04SGreg Roach        }
240a25f0a04SGreg Roach
241a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
242a25f0a04SGreg Roach        $distance *= 2;
243a25f0a04SGreg Roach
244a25f0a04SGreg Roach        // Consider each path length in turn
245a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
246a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
247a25f0a04SGreg Roach                // We have already calculated all records with this length
248e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
249a25f0a04SGreg Roach                    return true;
250a25f0a04SGreg Roach                }
251a25f0a04SGreg Roach            } else {
252a25f0a04SGreg Roach                // Need to calculate these paths
25313abd6f3SGreg Roach                $cache[$n] = [];
254e364afe4SGreg Roach                if ($n % 2 === 0) {
255a25f0a04SGreg Roach                    // Add FAM->INDI links
256a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2578d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
258dc124885SGreg Roach                            $individual = $fact->target();
259a25f0a04SGreg Roach                            // Don’t backtrack
260e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
261a25f0a04SGreg Roach                                $cache[$n][] = $individual;
262a25f0a04SGreg Roach                            }
263a25f0a04SGreg Roach                        }
264a25f0a04SGreg Roach                    }
265a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
266a25f0a04SGreg Roach                        return true;
267a25f0a04SGreg Roach                    }
268a25f0a04SGreg Roach                } else {
269a25f0a04SGreg Roach                    // Add INDI->FAM links
270a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2718d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
272dc124885SGreg Roach                            $family = $fact->target();
273a25f0a04SGreg Roach                            // Don’t backtrack
274e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
275a25f0a04SGreg Roach                                $cache[$n][] = $family;
276a25f0a04SGreg Roach                            }
277a25f0a04SGreg Roach                        }
278a25f0a04SGreg Roach                    }
279a25f0a04SGreg Roach                }
280a25f0a04SGreg Roach            }
281a25f0a04SGreg Roach        }
282a25f0a04SGreg Roach
283a25f0a04SGreg Roach        return false;
284a25f0a04SGreg Roach    }
285a25f0a04SGreg Roach
28676692c8bSGreg Roach    /**
28776692c8bSGreg Roach     * Generate a private version of this record
28876692c8bSGreg Roach     *
28976692c8bSGreg Roach     * @param int $access_level
29076692c8bSGreg Roach     *
29176692c8bSGreg Roach     * @return string
29276692c8bSGreg Roach     */
2933c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
294c1010edaSGreg Roach    {
295acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
296a25f0a04SGreg Roach
297a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
298518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
299a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
3008d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
301138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
302a25f0a04SGreg Roach            }
303a25f0a04SGreg Roach        }
304a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
3058d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
306a25f0a04SGreg Roach        foreach ($matches as $match) {
30724ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
308a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
309a25f0a04SGreg Roach                $rec .= $match[0];
310a25f0a04SGreg Roach            }
311a25f0a04SGreg Roach        }
312a25f0a04SGreg Roach        // Don’t privatize sex.
313a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
314a25f0a04SGreg Roach            $rec .= $match[0];
315a25f0a04SGreg Roach        }
316a25f0a04SGreg Roach
317a25f0a04SGreg Roach        return $rec;
318a25f0a04SGreg Roach    }
319a25f0a04SGreg Roach
32076692c8bSGreg Roach    /**
32176692c8bSGreg Roach     * Fetch data from the database
32276692c8bSGreg Roach     *
32376692c8bSGreg Roach     * @param string $xref
32476692c8bSGreg Roach     * @param int    $tree_id
32576692c8bSGreg Roach     *
326e364afe4SGreg Roach     * @return string|null
32776692c8bSGreg Roach     */
328e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
329c1010edaSGreg Roach    {
3302e5b4452SGreg Roach        return DB::table('individuals')
3312e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3322e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3332e5b4452SGreg Roach            ->value('i_gedcom');
334a25f0a04SGreg Roach    }
335a25f0a04SGreg Roach
336a25f0a04SGreg Roach    /**
337a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
338a25f0a04SGreg Roach     * If not known to be dead, then assume living.
339a25f0a04SGreg Roach     *
340cbc1590aSGreg Roach     * @return bool
341a25f0a04SGreg Roach     */
3428f53f488SRico Sonntag    public function isDead(): bool
343c1010edaSGreg Roach    {
344c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3454459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
346a25f0a04SGreg Roach
347a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3488d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
349a25f0a04SGreg Roach            return true;
350a25f0a04SGreg Roach        }
351a25f0a04SGreg Roach
352a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
353a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
354a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
355a25f0a04SGreg Roach                $date = new Date($date_match);
356269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
357a25f0a04SGreg Roach                    return true;
358a25f0a04SGreg Roach                }
359a25f0a04SGreg Roach            }
360a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
361a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
362a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
363a25f0a04SGreg Roach                return false;
364a25f0a04SGreg Roach            }
365a25f0a04SGreg Roach        }
366a25f0a04SGreg Roach
367a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
368a25f0a04SGreg Roach
369a25f0a04SGreg Roach        // Check parents (birth and adopted)
37039ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
37139ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
372a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
373a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
374a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
375a25f0a04SGreg Roach                    $date = new Date($date_match);
376269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
377a25f0a04SGreg Roach                        return true;
378a25f0a04SGreg Roach                    }
379a25f0a04SGreg Roach                }
380a25f0a04SGreg Roach            }
381a25f0a04SGreg Roach        }
382a25f0a04SGreg Roach
383a25f0a04SGreg Roach        // Check spouses
38439ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
385a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
386a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
387a25f0a04SGreg Roach                $date = new Date($date_match);
388a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
389269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
390a25f0a04SGreg Roach                    return true;
391a25f0a04SGreg Roach                }
392a25f0a04SGreg Roach            }
393a25f0a04SGreg Roach            // Check spouse dates
39439ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
395a25f0a04SGreg Roach            if ($spouse) {
396a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
397a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
398a25f0a04SGreg Roach                    $date = new Date($date_match);
399a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
400269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
401a25f0a04SGreg Roach                        return true;
402a25f0a04SGreg Roach                    }
403a25f0a04SGreg Roach                }
404a25f0a04SGreg Roach            }
405a25f0a04SGreg Roach            // Check child dates
40639ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
407a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
408a25f0a04SGreg Roach                // Assume children born after age of 15
409a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
410a25f0a04SGreg Roach                    $date = new Date($date_match);
411269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
412a25f0a04SGreg Roach                        return true;
413a25f0a04SGreg Roach                    }
414a25f0a04SGreg Roach                }
415a25f0a04SGreg Roach                // Check grandchildren
41639ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
41739ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
418a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
419a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
420a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
421a25f0a04SGreg Roach                            $date = new Date($date_match);
422269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
423a25f0a04SGreg Roach                                return true;
424a25f0a04SGreg Roach                            }
425a25f0a04SGreg Roach                        }
426a25f0a04SGreg Roach                    }
427a25f0a04SGreg Roach                }
428a25f0a04SGreg Roach            }
429a25f0a04SGreg Roach        }
430a25f0a04SGreg Roach
431a25f0a04SGreg Roach        return false;
432a25f0a04SGreg Roach    }
433a25f0a04SGreg Roach
434a25f0a04SGreg Roach    /**
435a25f0a04SGreg Roach     * Find the highlighted media object for an individual
436a25f0a04SGreg Roach     *
437e364afe4SGreg Roach     * @return MediaFile|null
438a25f0a04SGreg Roach     */
439e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
440c1010edaSGreg Roach    {
4418d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
442dc124885SGreg Roach            $media = $fact->target();
443a213a63cSGreg Roach            if ($media instanceof Media) {
4444a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
445a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4464a9f750fSGreg Roach                        return $media_file;
4474a9f750fSGreg Roach                    }
4484a9f750fSGreg Roach                }
449a25f0a04SGreg Roach            }
450a25f0a04SGreg Roach        }
451a25f0a04SGreg Roach
452a25f0a04SGreg Roach        return null;
453a25f0a04SGreg Roach    }
454a25f0a04SGreg Roach
455a25f0a04SGreg Roach    /**
456a25f0a04SGreg Roach     * Display the prefered image for this individual.
457a25f0a04SGreg Roach     * Use an icon if no image is available.
458a25f0a04SGreg Roach     *
459dce07401SGreg Roach     * @param int      $width      Pixels
460dce07401SGreg Roach     * @param int      $height     Pixels
461dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
462dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
463dce07401SGreg Roach     *
464a25f0a04SGreg Roach     * @return string
465a25f0a04SGreg Roach     */
4668f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
467c1010edaSGreg Roach    {
4684a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4694a9f750fSGreg Roach
4704a9f750fSGreg Roach        if ($media_file !== null) {
4714a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
472a25f0a04SGreg Roach        }
4734a9f750fSGreg Roach
4744a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
47539ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4764a9f750fSGreg Roach        }
4774a9f750fSGreg Roach
4784a9f750fSGreg Roach        return '';
479a25f0a04SGreg Roach    }
480a25f0a04SGreg Roach
481a25f0a04SGreg Roach    /**
482a25f0a04SGreg Roach     * Get the date of birth
483a25f0a04SGreg Roach     *
484a25f0a04SGreg Roach     * @return Date
485a25f0a04SGreg Roach     */
4868f53f488SRico Sonntag    public function getBirthDate(): Date
487c1010edaSGreg Roach    {
488a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
489a25f0a04SGreg Roach            if ($date->isOK()) {
490a25f0a04SGreg Roach                return $date;
491a25f0a04SGreg Roach            }
492a25f0a04SGreg Roach        }
493a25f0a04SGreg Roach
494a25f0a04SGreg Roach        return new Date('');
495a25f0a04SGreg Roach    }
496a25f0a04SGreg Roach
497a25f0a04SGreg Roach    /**
498a25f0a04SGreg Roach     * Get the place of birth
499a25f0a04SGreg Roach     *
50016d0b7f7SRico Sonntag     * @return Place
501a25f0a04SGreg Roach     */
5028f53f488SRico Sonntag    public function getBirthPlace(): Place
503c1010edaSGreg Roach    {
504a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
505a25f0a04SGreg Roach            return $place;
506a25f0a04SGreg Roach        }
507a25f0a04SGreg Roach
508b20ddbf9SGreg Roach        return new Place('', $this->tree);
509a25f0a04SGreg Roach    }
510a25f0a04SGreg Roach
511a25f0a04SGreg Roach    /**
512a25f0a04SGreg Roach     * Get the year of birth
513a25f0a04SGreg Roach     *
514a25f0a04SGreg Roach     * @return string the year of birth
515a25f0a04SGreg Roach     */
5168f53f488SRico Sonntag    public function getBirthYear(): string
517c1010edaSGreg Roach    {
518f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
519a25f0a04SGreg Roach    }
520a25f0a04SGreg Roach
521a25f0a04SGreg Roach    /**
522a25f0a04SGreg Roach     * Get the date of death
523a25f0a04SGreg Roach     *
524a25f0a04SGreg Roach     * @return Date
525a25f0a04SGreg Roach     */
5268f53f488SRico Sonntag    public function getDeathDate(): Date
527c1010edaSGreg Roach    {
528a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
529a25f0a04SGreg Roach            if ($date->isOK()) {
530a25f0a04SGreg Roach                return $date;
531a25f0a04SGreg Roach            }
532a25f0a04SGreg Roach        }
533a25f0a04SGreg Roach
534a25f0a04SGreg Roach        return new Date('');
535a25f0a04SGreg Roach    }
536a25f0a04SGreg Roach
537a25f0a04SGreg Roach    /**
538a25f0a04SGreg Roach     * Get the place of death
539a25f0a04SGreg Roach     *
54016d0b7f7SRico Sonntag     * @return Place
541a25f0a04SGreg Roach     */
5428f53f488SRico Sonntag    public function getDeathPlace(): Place
543c1010edaSGreg Roach    {
544a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
545a25f0a04SGreg Roach            return $place;
546a25f0a04SGreg Roach        }
547a25f0a04SGreg Roach
548b20ddbf9SGreg Roach        return new Place('', $this->tree);
549a25f0a04SGreg Roach    }
550a25f0a04SGreg Roach
551a25f0a04SGreg Roach    /**
552a25f0a04SGreg Roach     * get the death year
553a25f0a04SGreg Roach     *
554a25f0a04SGreg Roach     * @return string the year of death
555a25f0a04SGreg Roach     */
5568f53f488SRico Sonntag    public function getDeathYear(): string
557c1010edaSGreg Roach    {
558f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
559a25f0a04SGreg Roach    }
560a25f0a04SGreg Roach
561a25f0a04SGreg Roach    /**
562a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
56315d603e7SGreg Roach     * Provide the place and full date using a tooltip.
564a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
565a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
566a25f0a04SGreg Roach     *
567a25f0a04SGreg Roach     * @return string
568a25f0a04SGreg Roach     */
5698f53f488SRico Sonntag    public function getLifeSpan(): string
570c1010edaSGreg Roach    {
57115d603e7SGreg Roach        // Just the first part of the place name
572392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
573392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
57415d603e7SGreg Roach        // Remove markup from dates
57515d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
57615d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
57715d603e7SGreg Roach
578c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
579dacfe33cSGreg Roach        return I18N::translate(
580a25f0a04SGreg Roach            '%1$s–%2$s',
5812d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5822d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
583a25f0a04SGreg Roach        );
584a25f0a04SGreg Roach    }
585a25f0a04SGreg Roach
586a25f0a04SGreg Roach    /**
587a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
588a25f0a04SGreg Roach     *
589a25f0a04SGreg Roach     * @return Date[]
590a25f0a04SGreg Roach     */
5918f53f488SRico Sonntag    public function getAllBirthDates(): array
592c1010edaSGreg Roach    {
5938d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5948d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
595a25f0a04SGreg Roach            if ($tmp) {
596a25f0a04SGreg Roach                return $tmp;
597a25f0a04SGreg Roach            }
598a25f0a04SGreg Roach        }
599a25f0a04SGreg Roach
60013abd6f3SGreg Roach        return [];
601a25f0a04SGreg Roach    }
602a25f0a04SGreg Roach
603a25f0a04SGreg Roach    /**
604a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
605a25f0a04SGreg Roach     *
6064080d558SGreg Roach     * @return Place[]
607a25f0a04SGreg Roach     */
6088f53f488SRico Sonntag    public function getAllBirthPlaces(): array
609c1010edaSGreg Roach    {
6108d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
6118d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
61254c1ab5eSGreg Roach            if ($places !== []) {
6134080d558SGreg Roach                return $places;
614a25f0a04SGreg Roach            }
615a25f0a04SGreg Roach        }
616a25f0a04SGreg Roach
61713abd6f3SGreg Roach        return [];
618a25f0a04SGreg Roach    }
619a25f0a04SGreg Roach
620a25f0a04SGreg Roach    /**
621a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
622a25f0a04SGreg Roach     *
623a25f0a04SGreg Roach     * @return Date[]
624a25f0a04SGreg Roach     */
6258f53f488SRico Sonntag    public function getAllDeathDates(): array
626c1010edaSGreg Roach    {
6278d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6288d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
629a25f0a04SGreg Roach            if ($tmp) {
630a25f0a04SGreg Roach                return $tmp;
631a25f0a04SGreg Roach            }
632a25f0a04SGreg Roach        }
633a25f0a04SGreg Roach
63413abd6f3SGreg Roach        return [];
635a25f0a04SGreg Roach    }
636a25f0a04SGreg Roach
637a25f0a04SGreg Roach    /**
638a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
639a25f0a04SGreg Roach     *
6404080d558SGreg Roach     * @return Place[]
641a25f0a04SGreg Roach     */
6428f53f488SRico Sonntag    public function getAllDeathPlaces(): array
643c1010edaSGreg Roach    {
6448d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6458d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
64654c1ab5eSGreg Roach            if ($places !== []) {
6474080d558SGreg Roach                return $places;
648a25f0a04SGreg Roach            }
649a25f0a04SGreg Roach        }
650a25f0a04SGreg Roach
65113abd6f3SGreg Roach        return [];
652a25f0a04SGreg Roach    }
653a25f0a04SGreg Roach
654a25f0a04SGreg Roach    /**
655a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
656a25f0a04SGreg Roach     *
657a25f0a04SGreg Roach     * @return Date
658a25f0a04SGreg Roach     */
6598f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
660c1010edaSGreg Roach    {
6618f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
662a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
663a25f0a04SGreg Roach                if ($date->isOK()) {
6644686330aSGreg Roach                    $this->estimated_birth_date = $date;
665a25f0a04SGreg Roach                    break;
666a25f0a04SGreg Roach                }
667a25f0a04SGreg Roach            }
6688f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
66913abd6f3SGreg Roach                $min = [];
67013abd6f3SGreg Roach                $max = [];
671a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
672f5b60decSGreg Roach                if ($tmp->isOK()) {
673f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
674f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
675a25f0a04SGreg Roach                }
67639ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
677a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
678f5b60decSGreg Roach                    if ($tmp->isOK()) {
679f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
680f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
681a25f0a04SGreg Roach                    }
68239ca88baSGreg Roach                    $husband = $family->husband();
683e364afe4SGreg Roach                    if ($husband instanceof self) {
6842e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
685f5b60decSGreg Roach                        if ($tmp->isOK()) {
686f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
687f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
688a25f0a04SGreg Roach                        }
689a25f0a04SGreg Roach                    }
69039ca88baSGreg Roach                    $wife = $family->wife();
691e364afe4SGreg Roach                    if ($wife instanceof self) {
6922e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
693f5b60decSGreg Roach                        if ($tmp->isOK()) {
694f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
695f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
696a25f0a04SGreg Roach                        }
697a25f0a04SGreg Roach                    }
69839ca88baSGreg Roach                    foreach ($family->children() as $child) {
699a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
700f5b60decSGreg Roach                        if ($tmp->isOK()) {
701f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
702f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
703a25f0a04SGreg Roach                        }
704a25f0a04SGreg Roach                    }
705a25f0a04SGreg Roach                }
70639ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
707a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
708f5b60decSGreg Roach                    if ($tmp->isOK()) {
709f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
710f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
711a25f0a04SGreg Roach                    }
71239ca88baSGreg Roach                    $spouse = $family->spouse($this);
713a25f0a04SGreg Roach                    if ($spouse) {
714a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
715f5b60decSGreg Roach                        if ($tmp->isOK()) {
716f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
717f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
718a25f0a04SGreg Roach                        }
719a25f0a04SGreg Roach                    }
72039ca88baSGreg Roach                    foreach ($family->children() as $child) {
721a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
722f5b60decSGreg Roach                        if ($tmp->isOK()) {
723e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
724f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
725a25f0a04SGreg Roach                        }
726a25f0a04SGreg Roach                    }
727a25f0a04SGreg Roach                }
728a25f0a04SGreg Roach                if ($min && $max) {
72959f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
730a25f0a04SGreg Roach
73165e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7324686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
733a25f0a04SGreg Roach                } else {
7344686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
735a25f0a04SGreg Roach                }
736a25f0a04SGreg Roach            }
737a25f0a04SGreg Roach        }
738a25f0a04SGreg Roach
7394686330aSGreg Roach        return $this->estimated_birth_date;
740a25f0a04SGreg Roach    }
741a25f0a04SGreg Roach
742a25f0a04SGreg Roach    /**
743a25f0a04SGreg Roach     * Generate an estimated date of death.
744a25f0a04SGreg Roach     *
745a25f0a04SGreg Roach     * @return Date
746a25f0a04SGreg Roach     */
7478f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
748c1010edaSGreg Roach    {
7494686330aSGreg Roach        if ($this->estimated_death_date === null) {
750a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
751a25f0a04SGreg Roach                if ($date->isOK()) {
7524686330aSGreg Roach                    $this->estimated_death_date = $date;
753a25f0a04SGreg Roach                    break;
754a25f0a04SGreg Roach                }
755a25f0a04SGreg Roach            }
7564686330aSGreg Roach            if ($this->estimated_death_date === null) {
757f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
758c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7594686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
760a25f0a04SGreg Roach                } else {
7614686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
762a25f0a04SGreg Roach                }
763a25f0a04SGreg Roach            }
764a25f0a04SGreg Roach        }
765a25f0a04SGreg Roach
7664686330aSGreg Roach        return $this->estimated_death_date;
767a25f0a04SGreg Roach    }
768a25f0a04SGreg Roach
769a25f0a04SGreg Roach    /**
770a25f0a04SGreg Roach     * Get the sex - M F or U
771a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
772a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
773a25f0a04SGreg Roach     *
774a25f0a04SGreg Roach     * @return string
775a25f0a04SGreg Roach     */
776e364afe4SGreg Roach    public function sex(): string
777c1010edaSGreg Roach    {
778a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
779a25f0a04SGreg Roach            return $match[1];
780a25f0a04SGreg Roach        }
781b2ce94c6SRico Sonntag
782b2ce94c6SRico Sonntag        return 'U';
783a25f0a04SGreg Roach    }
784a25f0a04SGreg Roach
785a25f0a04SGreg Roach    /**
786a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
787a25f0a04SGreg Roach     *
788a25f0a04SGreg Roach     * @return string
789a25f0a04SGreg Roach     */
7908f53f488SRico Sonntag    public function getBoxStyle(): string
791c1010edaSGreg Roach    {
792c1010edaSGreg Roach        $tmp = [
793c1010edaSGreg Roach            'M' => '',
794c1010edaSGreg Roach            'F' => 'F',
795c1010edaSGreg Roach            'U' => 'NN',
796c1010edaSGreg Roach        ];
797a25f0a04SGreg Roach
79839ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
799a25f0a04SGreg Roach    }
800a25f0a04SGreg Roach
801a25f0a04SGreg Roach    /**
802a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
803a25f0a04SGreg Roach     *
804cbc1590aSGreg Roach     * @param int|null $access_level
805a25f0a04SGreg Roach     *
80654c7f8dfSGreg Roach     * @return Collection
807a25f0a04SGreg Roach     */
80839ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
809c1010edaSGreg Roach    {
8104b9ff166SGreg Roach        if ($access_level === null) {
8114b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8124b9ff166SGreg Roach        }
8134b9ff166SGreg Roach
814acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
815a25f0a04SGreg Roach
81639ca88baSGreg Roach        $families = new Collection();
8178d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
818dc124885SGreg Roach            $family = $fact->target();
819e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
82039ca88baSGreg Roach                $families->push($family);
821a25f0a04SGreg Roach            }
822a25f0a04SGreg Roach        }
823a25f0a04SGreg Roach
82439ca88baSGreg Roach        return new Collection($families);
825a25f0a04SGreg Roach    }
826a25f0a04SGreg Roach
827a25f0a04SGreg Roach    /**
828a25f0a04SGreg Roach     * Get the current spouse of this individual.
829a25f0a04SGreg Roach     *
830a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
831a25f0a04SGreg Roach     * in chronological order, and take the last one found.
832a25f0a04SGreg Roach     *
833a25f0a04SGreg Roach     * @return Individual|null
834a25f0a04SGreg Roach     */
835e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
836c1010edaSGreg Roach    {
83739ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
83839ca88baSGreg Roach
83939ca88baSGreg Roach        if ($family instanceof Family) {
84039ca88baSGreg Roach            return $family->spouse($this);
841a25f0a04SGreg Roach        }
842b2ce94c6SRico Sonntag
843b2ce94c6SRico Sonntag        return null;
844a25f0a04SGreg Roach    }
845a25f0a04SGreg Roach
846a25f0a04SGreg Roach    /**
847a25f0a04SGreg Roach     * Count the children belonging to this individual.
848a25f0a04SGreg Roach     *
849cbc1590aSGreg Roach     * @return int
850a25f0a04SGreg Roach     */
851e364afe4SGreg Roach    public function numberOfChildren(): int
852c1010edaSGreg Roach    {
8537d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8543dc7bbe9SGreg Roach            return (int) $match[1];
855b2ce94c6SRico Sonntag        }
856b2ce94c6SRico Sonntag
85713abd6f3SGreg Roach        $children = [];
85839ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
85939ca88baSGreg Roach            foreach ($fam->children() as $child) {
860c0935879SGreg Roach                $children[$child->xref()] = true;
861a25f0a04SGreg Roach            }
862a25f0a04SGreg Roach        }
863a25f0a04SGreg Roach
864a25f0a04SGreg Roach        return count($children);
865a25f0a04SGreg Roach    }
866a25f0a04SGreg Roach
867a25f0a04SGreg Roach    /**
868a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
869a25f0a04SGreg Roach     *
870cbc1590aSGreg Roach     * @param int|null $access_level
871a25f0a04SGreg Roach     *
87254c7f8dfSGreg Roach     * @return Collection
873a25f0a04SGreg Roach     */
87439ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
875c1010edaSGreg Roach    {
8764b9ff166SGreg Roach        if ($access_level === null) {
8774b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8784b9ff166SGreg Roach        }
8794b9ff166SGreg Roach
880acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
881a25f0a04SGreg Roach
88239ca88baSGreg Roach        $families = new Collection();
88339ca88baSGreg Roach
8848d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
885dc124885SGreg Roach            $family = $fact->target();
886e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
88739ca88baSGreg Roach                $families->push($family);
888a25f0a04SGreg Roach            }
889a25f0a04SGreg Roach        }
890a25f0a04SGreg Roach
891a25f0a04SGreg Roach        return $families;
892a25f0a04SGreg Roach    }
893a25f0a04SGreg Roach
894a25f0a04SGreg Roach    /**
895a25f0a04SGreg Roach     * Get a list of step-parent families.
896a25f0a04SGreg Roach     *
89754c7f8dfSGreg Roach     * @return Collection
898a25f0a04SGreg Roach     */
899820b62dfSGreg Roach    public function childStepFamilies(): Collection
900c1010edaSGreg Roach    {
901ed5b6227SGreg Roach        $step_families = new Collection();
90239ca88baSGreg Roach        $families      = $this->childFamilies();
903a25f0a04SGreg Roach        foreach ($families as $family) {
904ed5b6227SGreg Roach            foreach ($family->spouses() as $parent) {
905ed5b6227SGreg Roach                foreach ($parent->spouseFamilies() as $step_family) {
90639ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
907ed5b6227SGreg Roach                        $step_families->add($step_family);
908a25f0a04SGreg Roach                    }
909a25f0a04SGreg Roach                }
910a25f0a04SGreg Roach            }
911a25f0a04SGreg Roach        }
912a25f0a04SGreg Roach
913ed5b6227SGreg Roach        return $step_families->unique();
914a25f0a04SGreg Roach    }
915a25f0a04SGreg Roach
916a25f0a04SGreg Roach    /**
917a25f0a04SGreg Roach     * Get a list of step-parent families.
918a25f0a04SGreg Roach     *
91954c7f8dfSGreg Roach     * @return Collection
920a25f0a04SGreg Roach     */
921820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
922c1010edaSGreg Roach    {
92313abd6f3SGreg Roach        $step_families = [];
92439ca88baSGreg Roach        $families      = $this->spouseFamilies();
925820b62dfSGreg Roach
926a25f0a04SGreg Roach        foreach ($families as $family) {
92739ca88baSGreg Roach            $spouse = $family->spouse($this);
928820b62dfSGreg Roach
929a25f0a04SGreg Roach            if ($spouse) {
93039ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
93139ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
932a25f0a04SGreg Roach                        $step_families[] = $step_family;
933a25f0a04SGreg Roach                    }
934a25f0a04SGreg Roach                }
935a25f0a04SGreg Roach            }
936a25f0a04SGreg Roach        }
937a25f0a04SGreg Roach
938820b62dfSGreg Roach        return new Collection($step_families);
939a25f0a04SGreg Roach    }
940a25f0a04SGreg Roach
941a25f0a04SGreg Roach    /**
942a25f0a04SGreg Roach     * A label for a parental family group
943a25f0a04SGreg Roach     *
944a25f0a04SGreg Roach     * @param Family $family
945a25f0a04SGreg Roach     *
946a25f0a04SGreg Roach     * @return string
947a25f0a04SGreg Roach     */
948820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
949c1010edaSGreg Roach    {
9507d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
951a25f0a04SGreg Roach            // A specified pedigree
952764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
953b2ce94c6SRico Sonntag        }
954b2ce94c6SRico Sonntag
955a25f0a04SGreg Roach        // Default (birth) pedigree
956764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
957a25f0a04SGreg Roach    }
958a25f0a04SGreg Roach
959a25f0a04SGreg Roach    /**
960a25f0a04SGreg Roach     * Create a label for a step family
961a25f0a04SGreg Roach     *
962a25f0a04SGreg Roach     * @param Family $step_family
963a25f0a04SGreg Roach     *
964a25f0a04SGreg Roach     * @return string
965a25f0a04SGreg Roach     */
9668f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
967c1010edaSGreg Roach    {
96839ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
969a25f0a04SGreg Roach            if ($family !== $step_family) {
970a25f0a04SGreg Roach                // Must be a step-family
97139ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
97239ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
973a25f0a04SGreg Roach                        if ($parent === $step_parent) {
974a25f0a04SGreg Roach                            // One common parent - must be a step family
975e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
976a25f0a04SGreg Roach                                // Father’s family with someone else
97739ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
978a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
97939ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
980b2ce94c6SRico Sonntag                                }
981b2ce94c6SRico Sonntag
982a25f0a04SGreg Roach                                /* I18N: A step-family. */
983bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
984a25f0a04SGreg Roach                            }
985b2ce94c6SRico Sonntag
986a25f0a04SGreg Roach                            // Mother’s family with someone else
98739ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
988a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
98939ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
990b2ce94c6SRico Sonntag                            }
991b2ce94c6SRico Sonntag
992a25f0a04SGreg Roach                            /* I18N: A step-family. */
993bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
994a25f0a04SGreg Roach                        }
995a25f0a04SGreg Roach                    }
996a25f0a04SGreg Roach                }
997a25f0a04SGreg Roach            }
998a25f0a04SGreg Roach        }
999a25f0a04SGreg Roach
1000a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1001a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1002a25f0a04SGreg Roach    }
1003a25f0a04SGreg Roach
1004225e381fSGreg Roach    /**
1005225e381fSGreg Roach     * Get the description for the family.
1006225e381fSGreg Roach     *
1007225e381fSGreg Roach     * For example, "XXX's family with new wife".
1008225e381fSGreg Roach     *
1009225e381fSGreg Roach     * @param Family $family
1010225e381fSGreg Roach     *
1011225e381fSGreg Roach     * @return string
1012225e381fSGreg Roach     */
1013e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
1014c1010edaSGreg Roach    {
101539ca88baSGreg Roach        $spouse = $family->spouse($this);
1016225e381fSGreg Roach        if ($spouse) {
1017225e381fSGreg Roach            /* I18N: %s is the spouse name */
101839ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1019225e381fSGreg Roach        }
1020b2ce94c6SRico Sonntag
102139ca88baSGreg Roach        return $family->fullName();
1022225e381fSGreg Roach    }
1023225e381fSGreg Roach
1024a25f0a04SGreg Roach    /**
1025a25f0a04SGreg Roach     * get primary parents names for this individual
1026a25f0a04SGreg Roach     *
1027a25f0a04SGreg Roach     * @param string $classname optional css class
1028a25f0a04SGreg Roach     * @param string $display   optional css style display
1029a25f0a04SGreg Roach     *
1030a25f0a04SGreg Roach     * @return string a div block with father & mother names
1031a25f0a04SGreg Roach     */
10328f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1033c1010edaSGreg Roach    {
10341afbbc50SGreg Roach        $fam = $this->childFamilies()->first();
1035a25f0a04SGreg Roach        if (!$fam) {
1036a25f0a04SGreg Roach            return '';
1037a25f0a04SGreg Roach        }
1038a25f0a04SGreg Roach        $txt = '<div';
1039a25f0a04SGreg Roach        if ($classname) {
10404c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1041a25f0a04SGreg Roach        }
1042a25f0a04SGreg Roach        if ($display) {
10434c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1044a25f0a04SGreg Roach        }
1045a25f0a04SGreg Roach        $txt .= '>';
104639ca88baSGreg Roach        $husb = $fam->husband();
1047a25f0a04SGreg Roach        if ($husb) {
1048a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1049a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1050a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1051a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1052a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
105339ca88baSGreg Roach            $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>';
1054a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1055a25f0a04SGreg Roach        }
105639ca88baSGreg Roach        $wife = $fam->wife();
1057a25f0a04SGreg Roach        if ($wife) {
1058a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1059a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1060a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1061a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1062a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
106339ca88baSGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->fullName());
1064a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1065a25f0a04SGreg Roach        }
1066a25f0a04SGreg Roach        $txt .= '</div>';
1067a25f0a04SGreg Roach
1068a25f0a04SGreg Roach        return $txt;
1069a25f0a04SGreg Roach    }
1070a25f0a04SGreg Roach
1071961ec755SGreg Roach    /**
1072961ec755SGreg Roach     * If this object has no name, what do we call it?
1073961ec755SGreg Roach     *
1074961ec755SGreg Roach     * @return string
1075961ec755SGreg Roach     */
10768f53f488SRico Sonntag    public function getFallBackName(): string
1077c1010edaSGreg Roach    {
1078a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1079a25f0a04SGreg Roach    }
1080a25f0a04SGreg Roach
1081a25f0a04SGreg Roach    /**
1082a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1083a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1084a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1085a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1086a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1087a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1088a25f0a04SGreg Roach     *
1089a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1090a25f0a04SGreg Roach     * 2 GIVN Robert
1091a25f0a04SGreg Roach     * 2 SPFX de
1092a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1093a25f0a04SGreg Roach     * 2 NICK The Bald
1094a25f0a04SGreg Roach     *
1095a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1096a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1097a25f0a04SGreg Roach     *
1098a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1099a25f0a04SGreg Roach     *
1100a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1101a25f0a04SGreg Roach     * or
1102a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1103a25f0a04SGreg Roach     * 2 GIVN Carlos
1104a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1105a25f0a04SGreg Roach     *
1106a25f0a04SGreg Roach     * @param string $type
1107a25f0a04SGreg Roach     * @param string $full
1108a25f0a04SGreg Roach     * @param string $gedcom
1109e364afe4SGreg Roach     *
1110e364afe4SGreg Roach     * @return void
1111a25f0a04SGreg Roach     */
1112e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1113c1010edaSGreg Roach    {
1114a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1115a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1116a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1117a25f0a04SGreg Roach
111876f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1119a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1120a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1121a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1122a25f0a04SGreg Roach
1123a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
112476f666f4SGreg Roach        if ($SURN !== '') {
1125a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1126a25f0a04SGreg Roach        } else {
112713abd6f3SGreg Roach            $SURNS = [];
1128a25f0a04SGreg Roach        }
112976f666f4SGreg Roach
1130a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1131a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1132a25f0a04SGreg Roach
1133a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1134a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1135a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1136a25f0a04SGreg Roach
1137a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
113876f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1139e364afe4SGreg Roach            $full .= '/';
1140a25f0a04SGreg Roach        }
1141a25f0a04SGreg Roach
1142a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1143a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1144a25f0a04SGreg Roach
1145a25f0a04SGreg Roach        // Extract the surname.
1146a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1147a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1148a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1149a25f0a04SGreg Roach        } else {
1150a25f0a04SGreg Roach            $surname = '';
1151a25f0a04SGreg Roach        }
1152a25f0a04SGreg Roach
1153a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1154a25f0a04SGreg Roach        if (!$SURNS) {
1155a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1156a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1157a25f0a04SGreg Roach                $SURNS = $matches[1];
1158a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1159a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1160a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1161a25f0a04SGreg Roach                }
1162a25f0a04SGreg Roach            } else {
1163a25f0a04SGreg Roach                // It is valid not to have a surname at all
116413abd6f3SGreg Roach                $SURNS = [''];
1165a25f0a04SGreg Roach            }
1166a25f0a04SGreg Roach        }
1167a25f0a04SGreg Roach
1168a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1169a25f0a04SGreg Roach        if (!$GIVN) {
1170a25f0a04SGreg Roach            $GIVN = preg_replace(
117113abd6f3SGreg Roach                [
1172c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1173c1010edaSGreg Roach                    // remove surname
1174c1010edaSGreg Roach                    '/ ?".+"/',
1175c1010edaSGreg Roach                    // remove nickname
1176c1010edaSGreg Roach                    '/ {2,}/',
1177c1010edaSGreg Roach                    // multiple spaces, caused by the above
1178c1010edaSGreg Roach                    '/^ | $/',
1179c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
118013abd6f3SGreg Roach                ],
118113abd6f3SGreg Roach                [
1182a25f0a04SGreg Roach                    ' ',
1183a25f0a04SGreg Roach                    ' ',
1184a25f0a04SGreg Roach                    ' ',
1185a25f0a04SGreg Roach                    '',
118613abd6f3SGreg Roach                ],
1187a25f0a04SGreg Roach                $full
1188a25f0a04SGreg Roach            );
1189a25f0a04SGreg Roach        }
1190a25f0a04SGreg Roach
1191a25f0a04SGreg Roach        // Add placeholder for unknown given name
1192a25f0a04SGreg Roach        if (!$GIVN) {
1193a25f0a04SGreg Roach            $GIVN = '@P.N.';
119473f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1195a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1196a25f0a04SGreg Roach        }
1197a25f0a04SGreg Roach
11987b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
11997b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1200c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12017b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1202c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1203a25f0a04SGreg Roach        }
1204a25f0a04SGreg Roach
1205a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1206a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1207a25f0a04SGreg Roach        // $full is for display on-screen
1208a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1209a25f0a04SGreg Roach
1210a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1211ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1212ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1213c6f196c3SGreg Roach        // Format for display
1214d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1215acc34ea1SGreg Roach        // Localise quotation marks around the nickname
12160b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
12178d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12188d68cabeSGreg Roach        }, $full);
1219a25f0a04SGreg Roach
1220c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1221a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1222a25f0a04SGreg Roach
1223a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1224a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1225a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1226a25f0a04SGreg Roach
1227ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1228a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1229e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1230a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1231e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1232a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1233a25f0a04SGreg Roach            }
1234a25f0a04SGreg Roach
1235bdb3725aSGreg Roach            $this->getAllNames[] = [
1236a25f0a04SGreg Roach                'type'    => $type,
1237a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1238c1010edaSGreg Roach                'full'    => $full,
1239c1010edaSGreg Roach                // This is used for display
1240c1010edaSGreg Roach                'fullNN'  => $fullNN,
1241c1010edaSGreg Roach                // This goes into the database
1242c1010edaSGreg Roach                'surname' => $surname,
1243c1010edaSGreg Roach                // This goes into the database
1244c1010edaSGreg Roach                'givn'    => $GIVN,
1245c1010edaSGreg Roach                // This goes into the database
1246c1010edaSGreg Roach                'surn'    => $SURN,
1247c1010edaSGreg Roach                // This goes into the database
124813abd6f3SGreg Roach            ];
1249a25f0a04SGreg Roach        }
1250a25f0a04SGreg Roach    }
1251a25f0a04SGreg Roach
1252a25f0a04SGreg Roach    /**
125376692c8bSGreg Roach     * Extract names from the GEDCOM record.
1254c7ff4153SGreg Roach     *
1255c7ff4153SGreg Roach     * @return void
1256a25f0a04SGreg Roach     */
1257e364afe4SGreg Roach    public function extractNames(): void
1258c1010edaSGreg Roach    {
12598f53f488SRico Sonntag        $this->extractNamesFromFacts(
12608f53f488SRico Sonntag            1,
12618f53f488SRico Sonntag            'NAME',
126230158ae7SGreg Roach            $this->facts(
12638d0ebef0SGreg Roach                ['NAME'],
12648f53f488SRico Sonntag                false,
12658f53f488SRico Sonntag                Auth::accessLevel($this->tree),
12668f53f488SRico Sonntag                $this->canShowName()
12678f53f488SRico Sonntag            )
12688f53f488SRico Sonntag        );
1269a25f0a04SGreg Roach    }
1270a25f0a04SGreg Roach
1271a25f0a04SGreg Roach    /**
1272a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1273a25f0a04SGreg Roach     * selection items or favorites.
1274a25f0a04SGreg Roach     *
1275a25f0a04SGreg Roach     * @return string
1276a25f0a04SGreg Roach     */
12778f53f488SRico Sonntag    public function formatListDetails(): string
1278c1010edaSGreg Roach    {
1279a25f0a04SGreg Roach        return
12808d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
12818d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1282a25f0a04SGreg Roach    }
1283a25f0a04SGreg Roach}
1284