xref: /webtrees/app/Individual.php (revision e0458bdc6fe08e8f4a459f05e98f137641a91ad1)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20886b77daSGreg Roachuse Closure;
21a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
232e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2439ca88baSGreg Roachuse Illuminate\Support\Collection;
25886b77daSGreg Roachuse stdClass;
26a25f0a04SGreg Roach
27a25f0a04SGreg Roach/**
2876692c8bSGreg Roach * A GEDCOM individual (INDI) object.
29a25f0a04SGreg Roach */
30c1010edaSGreg Roachclass Individual extends GedcomRecord
31c1010edaSGreg Roach{
3216d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3316d6367aSGreg Roach
3416d6367aSGreg Roach    protected const ROUTE_NAME = 'individual';
35a25f0a04SGreg Roach
3676692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3776692c8bSGreg Roach    public $generation;
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
404686330aSGreg Roach    private $estimated_birth_date;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach    /** @var Date The estimated date of death */
434686330aSGreg Roach    private $estimated_death_date;
44a25f0a04SGreg Roach
45a25f0a04SGreg Roach    /**
46886b77daSGreg Roach     * A closure which will create a record from a database row.
47886b77daSGreg Roach     *
48886b77daSGreg Roach     * @return Closure
49886b77daSGreg Roach     */
50c0804649SGreg Roach    public static function rowMapper(): Closure
51886b77daSGreg Roach    {
52c0804649SGreg Roach        return function (stdClass $row): Individual {
53a7a24840SGreg Roach            $individual = Individual::getInstance($row->i_id, Tree::findById((int) $row->i_file), $row->i_gedcom);
54e84cf2deSGreg Roach
55e84cf2deSGreg Roach            if ($row->n_num ?? null) {
56*e0458bdcSGreg Roach                $individual = clone $individual;
57e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
58*e0458bdcSGreg Roach
59*e0458bdcSGreg Roach                return $individual;
60e84cf2deSGreg Roach            }
61e84cf2deSGreg Roach
62e84cf2deSGreg Roach            return $individual;
63886b77daSGreg Roach        };
64886b77daSGreg Roach    }
65886b77daSGreg Roach
66886b77daSGreg Roach    /**
67c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
68c156e8f5SGreg Roach     *
69c156e8f5SGreg Roach     * @return Closure
70c156e8f5SGreg Roach     */
71c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
72c156e8f5SGreg Roach    {
73a9199cb2SGreg Roach        return function (Individual $x, Individual $y): int {
74c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
75c156e8f5SGreg Roach        };
76c156e8f5SGreg Roach    }
77c156e8f5SGreg Roach
78c156e8f5SGreg Roach    /**
79c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
80c156e8f5SGreg Roach     *
81c156e8f5SGreg Roach     * @return Closure
82c156e8f5SGreg Roach     */
83c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
84c156e8f5SGreg Roach    {
85a9199cb2SGreg Roach        return function (Individual $x, Individual $y): int {
86c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
87c156e8f5SGreg Roach        };
88c156e8f5SGreg Roach    }
89c156e8f5SGreg Roach
90c156e8f5SGreg Roach    /**
91e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
92e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
93e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
94e71ef9d2SGreg Roach     *
95e71ef9d2SGreg Roach     * @param string      $xref
96e71ef9d2SGreg Roach     * @param Tree        $tree
97e71ef9d2SGreg Roach     * @param string|null $gedcom
98e71ef9d2SGreg Roach     *
99e71ef9d2SGreg Roach     * @throws \Exception
100e71ef9d2SGreg Roach     * @return Individual|null
101e71ef9d2SGreg Roach     */
102e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
103c1010edaSGreg Roach    {
104e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
105e71ef9d2SGreg Roach
106e364afe4SGreg Roach        if ($record instanceof self) {
107e71ef9d2SGreg Roach            return $record;
108e71ef9d2SGreg Roach        }
109b2ce94c6SRico Sonntag
110b2ce94c6SRico Sonntag        return null;
111e71ef9d2SGreg Roach    }
112e71ef9d2SGreg Roach
113e71ef9d2SGreg Roach    /**
114395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
115395f0fe0SGreg Roach     * Typically when we load families and their members.
116395f0fe0SGreg Roach     *
117395f0fe0SGreg Roach     * @param Tree     $tree
1184a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
11918d7a90dSGreg Roach     *
12018d7a90dSGreg Roach     * @return void
121395f0fe0SGreg Roach     */
1222e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
123c1010edaSGreg Roach    {
1242e5b4452SGreg Roach        $rows = DB::table('individuals')
1252e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1262e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1272e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1282e5b4452SGreg Roach            ->get();
129395f0fe0SGreg Roach
130395f0fe0SGreg Roach        foreach ($rows as $row) {
131395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
132395f0fe0SGreg Roach        }
133395f0fe0SGreg Roach    }
134395f0fe0SGreg Roach
135395f0fe0SGreg Roach    /**
136a25f0a04SGreg Roach     * Can the name of this record be shown?
137a25f0a04SGreg Roach     *
13876692c8bSGreg Roach     * @param int|null $access_level
13976692c8bSGreg Roach     *
14076692c8bSGreg Roach     * @return bool
141a25f0a04SGreg Roach     */
14235584196SGreg Roach    public function canShowName(int $access_level = null): bool
143c1010edaSGreg Roach    {
1444b9ff166SGreg Roach        if ($access_level === null) {
1454b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1464b9ff166SGreg Roach        }
1474b9ff166SGreg Roach
148518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
149a25f0a04SGreg Roach    }
150a25f0a04SGreg Roach
151a25f0a04SGreg Roach    /**
15276692c8bSGreg Roach     * Can this individual be shown?
153a25f0a04SGreg Roach     *
15476692c8bSGreg Roach     * @param int $access_level
15576692c8bSGreg Roach     *
15676692c8bSGreg Roach     * @return bool
157a25f0a04SGreg Roach     */
15835584196SGreg Roach    protected function canShowByType(int $access_level): bool
159c1010edaSGreg Roach    {
160a25f0a04SGreg Roach        // Dead people...
161518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
162a25f0a04SGreg Roach            $keep_alive             = false;
16354ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
164a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1658d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
166a25f0a04SGreg Roach                foreach ($matches as $match) {
167a25f0a04SGreg Roach                    $date = new Date($match[1]);
168a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
169a25f0a04SGreg Roach                        $keep_alive = true;
170a25f0a04SGreg Roach                        break;
171a25f0a04SGreg Roach                    }
172a25f0a04SGreg Roach                }
173a25f0a04SGreg Roach            }
17454ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
175a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1768d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
177a25f0a04SGreg Roach                foreach ($matches as $match) {
178a25f0a04SGreg Roach                    $date = new Date($match[1]);
179a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
180a25f0a04SGreg Roach                        $keep_alive = true;
181a25f0a04SGreg Roach                        break;
182a25f0a04SGreg Roach                    }
183a25f0a04SGreg Roach                }
184a25f0a04SGreg Roach            }
185a25f0a04SGreg Roach            if (!$keep_alive) {
186a25f0a04SGreg Roach                return true;
187a25f0a04SGreg Roach            }
188a25f0a04SGreg Roach        }
189a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
19054ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1914b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
19254ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1934b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
194a25f0a04SGreg Roach        }
195a25f0a04SGreg Roach
196a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1974b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
198a25f0a04SGreg Roach    }
199a25f0a04SGreg Roach
200a25f0a04SGreg Roach    /**
201a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
202a25f0a04SGreg Roach     *
203a25f0a04SGreg Roach     * @param Individual $target
204cbc1590aSGreg Roach     * @param int        $distance
205a25f0a04SGreg Roach     *
206cbc1590aSGreg Roach     * @return bool
207a25f0a04SGreg Roach     */
2088f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
209c1010edaSGreg Roach    {
210a25f0a04SGreg Roach        static $cache = null;
211a25f0a04SGreg Roach
21206ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
213a25f0a04SGreg Roach        if ($user_individual) {
214a25f0a04SGreg Roach            if (!$cache) {
21513abd6f3SGreg Roach                $cache = [
21613abd6f3SGreg Roach                    0 => [$user_individual],
21713abd6f3SGreg Roach                    1 => [],
21813abd6f3SGreg Roach                ];
2198d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
220dc124885SGreg Roach                    $family = $fact->target();
221e24444eeSGreg Roach                    if ($family instanceof Family) {
222a25f0a04SGreg Roach                        $cache[1][] = $family;
223a25f0a04SGreg Roach                    }
224a25f0a04SGreg Roach                }
225a25f0a04SGreg Roach            }
226a25f0a04SGreg Roach        } else {
227a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
228a25f0a04SGreg Roach            return true;
229a25f0a04SGreg Roach        }
230a25f0a04SGreg Roach
231a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
232a25f0a04SGreg Roach        $distance *= 2;
233a25f0a04SGreg Roach
234a25f0a04SGreg Roach        // Consider each path length in turn
235a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
236a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
237a25f0a04SGreg Roach                // We have already calculated all records with this length
238e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
239a25f0a04SGreg Roach                    return true;
240a25f0a04SGreg Roach                }
241a25f0a04SGreg Roach            } else {
242a25f0a04SGreg Roach                // Need to calculate these paths
24313abd6f3SGreg Roach                $cache[$n] = [];
244e364afe4SGreg Roach                if ($n % 2 === 0) {
245a25f0a04SGreg Roach                    // Add FAM->INDI links
246a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2478d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
248dc124885SGreg Roach                            $individual = $fact->target();
249a25f0a04SGreg Roach                            // Don’t backtrack
250e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
251a25f0a04SGreg Roach                                $cache[$n][] = $individual;
252a25f0a04SGreg Roach                            }
253a25f0a04SGreg Roach                        }
254a25f0a04SGreg Roach                    }
255a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
256a25f0a04SGreg Roach                        return true;
257a25f0a04SGreg Roach                    }
258a25f0a04SGreg Roach                } else {
259a25f0a04SGreg Roach                    // Add INDI->FAM links
260a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2618d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
262dc124885SGreg Roach                            $family = $fact->target();
263a25f0a04SGreg Roach                            // Don’t backtrack
264e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
265a25f0a04SGreg Roach                                $cache[$n][] = $family;
266a25f0a04SGreg Roach                            }
267a25f0a04SGreg Roach                        }
268a25f0a04SGreg Roach                    }
269a25f0a04SGreg Roach                }
270a25f0a04SGreg Roach            }
271a25f0a04SGreg Roach        }
272a25f0a04SGreg Roach
273a25f0a04SGreg Roach        return false;
274a25f0a04SGreg Roach    }
275a25f0a04SGreg Roach
27676692c8bSGreg Roach    /**
27776692c8bSGreg Roach     * Generate a private version of this record
27876692c8bSGreg Roach     *
27976692c8bSGreg Roach     * @param int $access_level
28076692c8bSGreg Roach     *
28176692c8bSGreg Roach     * @return string
28276692c8bSGreg Roach     */
2833c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
284c1010edaSGreg Roach    {
285acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
286a25f0a04SGreg Roach
287a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
288518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
289a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2908d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
291138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
292a25f0a04SGreg Roach            }
293a25f0a04SGreg Roach        }
294a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2958d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
296a25f0a04SGreg Roach        foreach ($matches as $match) {
29724ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
298a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
299a25f0a04SGreg Roach                $rec .= $match[0];
300a25f0a04SGreg Roach            }
301a25f0a04SGreg Roach        }
302a25f0a04SGreg Roach        // Don’t privatize sex.
303a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
304a25f0a04SGreg Roach            $rec .= $match[0];
305a25f0a04SGreg Roach        }
306a25f0a04SGreg Roach
307a25f0a04SGreg Roach        return $rec;
308a25f0a04SGreg Roach    }
309a25f0a04SGreg Roach
31076692c8bSGreg Roach    /**
31176692c8bSGreg Roach     * Fetch data from the database
31276692c8bSGreg Roach     *
31376692c8bSGreg Roach     * @param string $xref
31476692c8bSGreg Roach     * @param int    $tree_id
31576692c8bSGreg Roach     *
316e364afe4SGreg Roach     * @return string|null
31776692c8bSGreg Roach     */
318e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
319c1010edaSGreg Roach    {
3202e5b4452SGreg Roach        return DB::table('individuals')
3212e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3222e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3232e5b4452SGreg Roach            ->value('i_gedcom');
324a25f0a04SGreg Roach    }
325a25f0a04SGreg Roach
326a25f0a04SGreg Roach    /**
327a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
328a25f0a04SGreg Roach     * If not known to be dead, then assume living.
329a25f0a04SGreg Roach     *
330cbc1590aSGreg Roach     * @return bool
331a25f0a04SGreg Roach     */
3328f53f488SRico Sonntag    public function isDead(): bool
333c1010edaSGreg Roach    {
334c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3354459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
336a25f0a04SGreg Roach
337a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3388d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
339a25f0a04SGreg Roach            return true;
340a25f0a04SGreg Roach        }
341a25f0a04SGreg Roach
342a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
343a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
344a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
345a25f0a04SGreg Roach                $date = new Date($date_match);
346269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
347a25f0a04SGreg Roach                    return true;
348a25f0a04SGreg Roach                }
349a25f0a04SGreg Roach            }
350a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
351a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
352a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
353a25f0a04SGreg Roach                return false;
354a25f0a04SGreg Roach            }
355a25f0a04SGreg Roach        }
356a25f0a04SGreg Roach
357a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
358a25f0a04SGreg Roach
359a25f0a04SGreg Roach        // Check parents (birth and adopted)
36039ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
36139ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
362a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
363a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
364a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
365a25f0a04SGreg Roach                    $date = new Date($date_match);
366269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
367a25f0a04SGreg Roach                        return true;
368a25f0a04SGreg Roach                    }
369a25f0a04SGreg Roach                }
370a25f0a04SGreg Roach            }
371a25f0a04SGreg Roach        }
372a25f0a04SGreg Roach
373a25f0a04SGreg Roach        // Check spouses
37439ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
375a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
376a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
377a25f0a04SGreg Roach                $date = new Date($date_match);
378a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
379269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
380a25f0a04SGreg Roach                    return true;
381a25f0a04SGreg Roach                }
382a25f0a04SGreg Roach            }
383a25f0a04SGreg Roach            // Check spouse dates
38439ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
385a25f0a04SGreg Roach            if ($spouse) {
386a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
387a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
388a25f0a04SGreg Roach                    $date = new Date($date_match);
389a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
390269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
391a25f0a04SGreg Roach                        return true;
392a25f0a04SGreg Roach                    }
393a25f0a04SGreg Roach                }
394a25f0a04SGreg Roach            }
395a25f0a04SGreg Roach            // Check child dates
39639ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
397a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
398a25f0a04SGreg Roach                // Assume children born after age of 15
399a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
400a25f0a04SGreg Roach                    $date = new Date($date_match);
401269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
402a25f0a04SGreg Roach                        return true;
403a25f0a04SGreg Roach                    }
404a25f0a04SGreg Roach                }
405a25f0a04SGreg Roach                // Check grandchildren
40639ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
40739ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
408a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
409a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
410a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
411a25f0a04SGreg Roach                            $date = new Date($date_match);
412269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
413a25f0a04SGreg Roach                                return true;
414a25f0a04SGreg Roach                            }
415a25f0a04SGreg Roach                        }
416a25f0a04SGreg Roach                    }
417a25f0a04SGreg Roach                }
418a25f0a04SGreg Roach            }
419a25f0a04SGreg Roach        }
420a25f0a04SGreg Roach
421a25f0a04SGreg Roach        return false;
422a25f0a04SGreg Roach    }
423a25f0a04SGreg Roach
424a25f0a04SGreg Roach    /**
425a25f0a04SGreg Roach     * Find the highlighted media object for an individual
426a25f0a04SGreg Roach     *
427e364afe4SGreg Roach     * @return MediaFile|null
428a25f0a04SGreg Roach     */
429e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
430c1010edaSGreg Roach    {
4318d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
432dc124885SGreg Roach            $media = $fact->target();
433a213a63cSGreg Roach            if ($media instanceof Media) {
4344a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
435a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4364a9f750fSGreg Roach                        return $media_file;
4374a9f750fSGreg Roach                    }
4384a9f750fSGreg Roach                }
439a25f0a04SGreg Roach            }
440a25f0a04SGreg Roach        }
441a25f0a04SGreg Roach
442a25f0a04SGreg Roach        return null;
443a25f0a04SGreg Roach    }
444a25f0a04SGreg Roach
445a25f0a04SGreg Roach    /**
446a25f0a04SGreg Roach     * Display the prefered image for this individual.
447a25f0a04SGreg Roach     * Use an icon if no image is available.
448a25f0a04SGreg Roach     *
449dce07401SGreg Roach     * @param int      $width      Pixels
450dce07401SGreg Roach     * @param int      $height     Pixels
451dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
452dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
453dce07401SGreg Roach     *
454a25f0a04SGreg Roach     * @return string
455a25f0a04SGreg Roach     */
4568f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
457c1010edaSGreg Roach    {
4584a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4594a9f750fSGreg Roach
4604a9f750fSGreg Roach        if ($media_file !== null) {
4614a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
462a25f0a04SGreg Roach        }
4634a9f750fSGreg Roach
4644a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
46539ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4664a9f750fSGreg Roach        }
4674a9f750fSGreg Roach
4684a9f750fSGreg Roach        return '';
469a25f0a04SGreg Roach    }
470a25f0a04SGreg Roach
471a25f0a04SGreg Roach    /**
472a25f0a04SGreg Roach     * Get the date of birth
473a25f0a04SGreg Roach     *
474a25f0a04SGreg Roach     * @return Date
475a25f0a04SGreg Roach     */
4768f53f488SRico Sonntag    public function getBirthDate(): Date
477c1010edaSGreg Roach    {
478a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
479a25f0a04SGreg Roach            if ($date->isOK()) {
480a25f0a04SGreg Roach                return $date;
481a25f0a04SGreg Roach            }
482a25f0a04SGreg Roach        }
483a25f0a04SGreg Roach
484a25f0a04SGreg Roach        return new Date('');
485a25f0a04SGreg Roach    }
486a25f0a04SGreg Roach
487a25f0a04SGreg Roach    /**
488a25f0a04SGreg Roach     * Get the place of birth
489a25f0a04SGreg Roach     *
49016d0b7f7SRico Sonntag     * @return Place
491a25f0a04SGreg Roach     */
4928f53f488SRico Sonntag    public function getBirthPlace(): Place
493c1010edaSGreg Roach    {
494a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
495a25f0a04SGreg Roach            return $place;
496a25f0a04SGreg Roach        }
497a25f0a04SGreg Roach
498b20ddbf9SGreg Roach        return new Place('', $this->tree);
499a25f0a04SGreg Roach    }
500a25f0a04SGreg Roach
501a25f0a04SGreg Roach    /**
502a25f0a04SGreg Roach     * Get the year of birth
503a25f0a04SGreg Roach     *
504a25f0a04SGreg Roach     * @return string the year of birth
505a25f0a04SGreg Roach     */
5068f53f488SRico Sonntag    public function getBirthYear(): string
507c1010edaSGreg Roach    {
508f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
509a25f0a04SGreg Roach    }
510a25f0a04SGreg Roach
511a25f0a04SGreg Roach    /**
512a25f0a04SGreg Roach     * Get the date of death
513a25f0a04SGreg Roach     *
514a25f0a04SGreg Roach     * @return Date
515a25f0a04SGreg Roach     */
5168f53f488SRico Sonntag    public function getDeathDate(): Date
517c1010edaSGreg Roach    {
518a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
519a25f0a04SGreg Roach            if ($date->isOK()) {
520a25f0a04SGreg Roach                return $date;
521a25f0a04SGreg Roach            }
522a25f0a04SGreg Roach        }
523a25f0a04SGreg Roach
524a25f0a04SGreg Roach        return new Date('');
525a25f0a04SGreg Roach    }
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach    /**
528a25f0a04SGreg Roach     * Get the place of death
529a25f0a04SGreg Roach     *
53016d0b7f7SRico Sonntag     * @return Place
531a25f0a04SGreg Roach     */
5328f53f488SRico Sonntag    public function getDeathPlace(): Place
533c1010edaSGreg Roach    {
534a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
535a25f0a04SGreg Roach            return $place;
536a25f0a04SGreg Roach        }
537a25f0a04SGreg Roach
538b20ddbf9SGreg Roach        return new Place('', $this->tree);
539a25f0a04SGreg Roach    }
540a25f0a04SGreg Roach
541a25f0a04SGreg Roach    /**
542a25f0a04SGreg Roach     * get the death year
543a25f0a04SGreg Roach     *
544a25f0a04SGreg Roach     * @return string the year of death
545a25f0a04SGreg Roach     */
5468f53f488SRico Sonntag    public function getDeathYear(): string
547c1010edaSGreg Roach    {
548f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
549a25f0a04SGreg Roach    }
550a25f0a04SGreg Roach
551a25f0a04SGreg Roach    /**
552a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
55315d603e7SGreg Roach     * Provide the place and full date using a tooltip.
554a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
555a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
556a25f0a04SGreg Roach     *
557a25f0a04SGreg Roach     * @return string
558a25f0a04SGreg Roach     */
5598f53f488SRico Sonntag    public function getLifeSpan(): string
560c1010edaSGreg Roach    {
56115d603e7SGreg Roach        // Just the first part of the place name
562392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
563392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
56415d603e7SGreg Roach        // Remove markup from dates
56515d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56615d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56715d603e7SGreg Roach
568c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
569bbb76c12SGreg Roach        return
570c1010edaSGreg Roach            I18N::translate(
571a25f0a04SGreg Roach                '%1$s–%2$s',
5722d4c1bedSGreg Roach                '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5732d4c1bedSGreg Roach                '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
574a25f0a04SGreg Roach            );
575a25f0a04SGreg Roach    }
576a25f0a04SGreg Roach
577a25f0a04SGreg Roach    /**
578a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
579a25f0a04SGreg Roach     *
580a25f0a04SGreg Roach     * @return Date[]
581a25f0a04SGreg Roach     */
5828f53f488SRico Sonntag    public function getAllBirthDates(): array
583c1010edaSGreg Roach    {
5848d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5858d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
586a25f0a04SGreg Roach            if ($tmp) {
587a25f0a04SGreg Roach                return $tmp;
588a25f0a04SGreg Roach            }
589a25f0a04SGreg Roach        }
590a25f0a04SGreg Roach
59113abd6f3SGreg Roach        return [];
592a25f0a04SGreg Roach    }
593a25f0a04SGreg Roach
594a25f0a04SGreg Roach    /**
595a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
596a25f0a04SGreg Roach     *
5974080d558SGreg Roach     * @return Place[]
598a25f0a04SGreg Roach     */
5998f53f488SRico Sonntag    public function getAllBirthPlaces(): array
600c1010edaSGreg Roach    {
6018d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
6028d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6034080d558SGreg Roach            if (!empty($places)) {
6044080d558SGreg Roach                return $places;
605a25f0a04SGreg Roach            }
606a25f0a04SGreg Roach        }
607a25f0a04SGreg Roach
60813abd6f3SGreg Roach        return [];
609a25f0a04SGreg Roach    }
610a25f0a04SGreg Roach
611a25f0a04SGreg Roach    /**
612a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
613a25f0a04SGreg Roach     *
614a25f0a04SGreg Roach     * @return Date[]
615a25f0a04SGreg Roach     */
6168f53f488SRico Sonntag    public function getAllDeathDates(): array
617c1010edaSGreg Roach    {
6188d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6198d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
620a25f0a04SGreg Roach            if ($tmp) {
621a25f0a04SGreg Roach                return $tmp;
622a25f0a04SGreg Roach            }
623a25f0a04SGreg Roach        }
624a25f0a04SGreg Roach
62513abd6f3SGreg Roach        return [];
626a25f0a04SGreg Roach    }
627a25f0a04SGreg Roach
628a25f0a04SGreg Roach    /**
629a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
630a25f0a04SGreg Roach     *
6314080d558SGreg Roach     * @return Place[]
632a25f0a04SGreg Roach     */
6338f53f488SRico Sonntag    public function getAllDeathPlaces(): array
634c1010edaSGreg Roach    {
6358d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6368d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6374080d558SGreg Roach            if (!empty($places)) {
6384080d558SGreg Roach                return $places;
639a25f0a04SGreg Roach            }
640a25f0a04SGreg Roach        }
641a25f0a04SGreg Roach
64213abd6f3SGreg Roach        return [];
643a25f0a04SGreg Roach    }
644a25f0a04SGreg Roach
645a25f0a04SGreg Roach    /**
646a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
647a25f0a04SGreg Roach     *
648a25f0a04SGreg Roach     * @return Date
649a25f0a04SGreg Roach     */
6508f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
651c1010edaSGreg Roach    {
6528f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
653a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
654a25f0a04SGreg Roach                if ($date->isOK()) {
6554686330aSGreg Roach                    $this->estimated_birth_date = $date;
656a25f0a04SGreg Roach                    break;
657a25f0a04SGreg Roach                }
658a25f0a04SGreg Roach            }
6598f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
66013abd6f3SGreg Roach                $min = [];
66113abd6f3SGreg Roach                $max = [];
662a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
663f5b60decSGreg Roach                if ($tmp->isOK()) {
664f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
665f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
666a25f0a04SGreg Roach                }
66739ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
668a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
669f5b60decSGreg Roach                    if ($tmp->isOK()) {
670f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
671f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
672a25f0a04SGreg Roach                    }
67339ca88baSGreg Roach                    $husband = $family->husband();
674e364afe4SGreg Roach                    if ($husband instanceof self) {
6752e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
676f5b60decSGreg Roach                        if ($tmp->isOK()) {
677f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
678f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
679a25f0a04SGreg Roach                        }
680a25f0a04SGreg Roach                    }
68139ca88baSGreg Roach                    $wife = $family->wife();
682e364afe4SGreg Roach                    if ($wife instanceof self) {
6832e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
684f5b60decSGreg Roach                        if ($tmp->isOK()) {
685f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
686f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
687a25f0a04SGreg Roach                        }
688a25f0a04SGreg Roach                    }
68939ca88baSGreg Roach                    foreach ($family->children() as $child) {
690a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
691f5b60decSGreg Roach                        if ($tmp->isOK()) {
692f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
693f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
694a25f0a04SGreg Roach                        }
695a25f0a04SGreg Roach                    }
696a25f0a04SGreg Roach                }
69739ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
698a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
699f5b60decSGreg Roach                    if ($tmp->isOK()) {
700f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
701f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
702a25f0a04SGreg Roach                    }
70339ca88baSGreg Roach                    $spouse = $family->spouse($this);
704a25f0a04SGreg Roach                    if ($spouse) {
705a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
706f5b60decSGreg Roach                        if ($tmp->isOK()) {
707f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
708f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
709a25f0a04SGreg Roach                        }
710a25f0a04SGreg Roach                    }
71139ca88baSGreg Roach                    foreach ($family->children() as $child) {
712a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
713f5b60decSGreg Roach                        if ($tmp->isOK()) {
714e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
715f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
716a25f0a04SGreg Roach                        }
717a25f0a04SGreg Roach                    }
718a25f0a04SGreg Roach                }
719a25f0a04SGreg Roach                if ($min && $max) {
72059f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
721a25f0a04SGreg Roach
72265e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7234686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
724a25f0a04SGreg Roach                } else {
7254686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
726a25f0a04SGreg Roach                }
727a25f0a04SGreg Roach            }
728a25f0a04SGreg Roach        }
729a25f0a04SGreg Roach
7304686330aSGreg Roach        return $this->estimated_birth_date;
731a25f0a04SGreg Roach    }
732a25f0a04SGreg Roach
733a25f0a04SGreg Roach    /**
734a25f0a04SGreg Roach     * Generate an estimated date of death.
735a25f0a04SGreg Roach     *
736a25f0a04SGreg Roach     * @return Date
737a25f0a04SGreg Roach     */
7388f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
739c1010edaSGreg Roach    {
7404686330aSGreg Roach        if ($this->estimated_death_date === null) {
741a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
742a25f0a04SGreg Roach                if ($date->isOK()) {
7434686330aSGreg Roach                    $this->estimated_death_date = $date;
744a25f0a04SGreg Roach                    break;
745a25f0a04SGreg Roach                }
746a25f0a04SGreg Roach            }
7474686330aSGreg Roach            if ($this->estimated_death_date === null) {
748f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
749c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7504686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
751a25f0a04SGreg Roach                } else {
7524686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
753a25f0a04SGreg Roach                }
754a25f0a04SGreg Roach            }
755a25f0a04SGreg Roach        }
756a25f0a04SGreg Roach
7574686330aSGreg Roach        return $this->estimated_death_date;
758a25f0a04SGreg Roach    }
759a25f0a04SGreg Roach
760a25f0a04SGreg Roach    /**
761a25f0a04SGreg Roach     * Get the sex - M F or U
762a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
763a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
764a25f0a04SGreg Roach     *
765a25f0a04SGreg Roach     * @return string
766a25f0a04SGreg Roach     */
767e364afe4SGreg Roach    public function sex(): string
768c1010edaSGreg Roach    {
769a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
770a25f0a04SGreg Roach            return $match[1];
771a25f0a04SGreg Roach        }
772b2ce94c6SRico Sonntag
773b2ce94c6SRico Sonntag        return 'U';
774a25f0a04SGreg Roach    }
775a25f0a04SGreg Roach
776a25f0a04SGreg Roach    /**
777a25f0a04SGreg Roach     * Get the individual’s sex image
778a25f0a04SGreg Roach     *
779a25f0a04SGreg Roach     * @param string $size
780a25f0a04SGreg Roach     *
781a25f0a04SGreg Roach     * @return string
782a25f0a04SGreg Roach     */
7838f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
784c1010edaSGreg Roach    {
78539ca88baSGreg Roach        return self::sexImage($this->sex(), $size);
786a25f0a04SGreg Roach    }
787a25f0a04SGreg Roach
788a25f0a04SGreg Roach    /**
789a25f0a04SGreg Roach     * Generate a sex icon/image
790a25f0a04SGreg Roach     *
791a25f0a04SGreg Roach     * @param string $sex
792a25f0a04SGreg Roach     * @param string $size
793a25f0a04SGreg Roach     *
794a25f0a04SGreg Roach     * @return string
795a25f0a04SGreg Roach     */
7968f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
797c1010edaSGreg Roach    {
798242a7862SGreg Roach        $image = view('icons/sex-' . $sex);
799242a7862SGreg Roach
800242a7862SGreg Roach        if ($size === 'small') {
801242a7862SGreg Roach            $image = '<small>' . $image . '</small>';
802242a7862SGreg Roach        }
803242a7862SGreg Roach
804242a7862SGreg Roach        return $image;
805a25f0a04SGreg Roach    }
806a25f0a04SGreg Roach
807a25f0a04SGreg Roach    /**
808a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
809a25f0a04SGreg Roach     *
810a25f0a04SGreg Roach     * @return string
811a25f0a04SGreg Roach     */
8128f53f488SRico Sonntag    public function getBoxStyle(): string
813c1010edaSGreg Roach    {
814c1010edaSGreg Roach        $tmp = [
815c1010edaSGreg Roach            'M' => '',
816c1010edaSGreg Roach            'F' => 'F',
817c1010edaSGreg Roach            'U' => 'NN',
818c1010edaSGreg Roach        ];
819a25f0a04SGreg Roach
82039ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
821a25f0a04SGreg Roach    }
822a25f0a04SGreg Roach
823a25f0a04SGreg Roach    /**
824a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
825a25f0a04SGreg Roach     *
826cbc1590aSGreg Roach     * @param int|null $access_level
827a25f0a04SGreg Roach     *
82854c7f8dfSGreg Roach     * @return Collection
82954c7f8dfSGreg Roach     * @return Family[]
830a25f0a04SGreg Roach     */
83139ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
832c1010edaSGreg Roach    {
8334b9ff166SGreg Roach        if ($access_level === null) {
8344b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8354b9ff166SGreg Roach        }
8364b9ff166SGreg Roach
837acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
838a25f0a04SGreg Roach
83939ca88baSGreg Roach        $families = new Collection();
8408d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
841dc124885SGreg Roach            $family = $fact->target();
842e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
84339ca88baSGreg Roach                $families->push($family);
844a25f0a04SGreg Roach            }
845a25f0a04SGreg Roach        }
846a25f0a04SGreg Roach
84739ca88baSGreg Roach        return new Collection($families);
848a25f0a04SGreg Roach    }
849a25f0a04SGreg Roach
850a25f0a04SGreg Roach    /**
851a25f0a04SGreg Roach     * Get the current spouse of this individual.
852a25f0a04SGreg Roach     *
853a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
854a25f0a04SGreg Roach     * in chronological order, and take the last one found.
855a25f0a04SGreg Roach     *
856a25f0a04SGreg Roach     * @return Individual|null
857a25f0a04SGreg Roach     */
858e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
859c1010edaSGreg Roach    {
86039ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
86139ca88baSGreg Roach
86239ca88baSGreg Roach        if ($family instanceof Family) {
86339ca88baSGreg Roach            return $family->spouse($this);
864a25f0a04SGreg Roach        }
865b2ce94c6SRico Sonntag
866b2ce94c6SRico Sonntag        return null;
867a25f0a04SGreg Roach    }
868a25f0a04SGreg Roach
869a25f0a04SGreg Roach    /**
870a25f0a04SGreg Roach     * Count the children belonging to this individual.
871a25f0a04SGreg Roach     *
872cbc1590aSGreg Roach     * @return int
873a25f0a04SGreg Roach     */
874e364afe4SGreg Roach    public function numberOfChildren(): int
875c1010edaSGreg Roach    {
8767d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8773dc7bbe9SGreg Roach            return (int) $match[1];
878b2ce94c6SRico Sonntag        }
879b2ce94c6SRico Sonntag
88013abd6f3SGreg Roach        $children = [];
88139ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
88239ca88baSGreg Roach            foreach ($fam->children() as $child) {
883c0935879SGreg Roach                $children[$child->xref()] = true;
884a25f0a04SGreg Roach            }
885a25f0a04SGreg Roach        }
886a25f0a04SGreg Roach
887a25f0a04SGreg Roach        return count($children);
888a25f0a04SGreg Roach    }
889a25f0a04SGreg Roach
890a25f0a04SGreg Roach    /**
891a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
892a25f0a04SGreg Roach     *
893cbc1590aSGreg Roach     * @param int|null $access_level
894a25f0a04SGreg Roach     *
89554c7f8dfSGreg Roach     * @return Collection
89654c7f8dfSGreg Roach     * @return Family[]
897a25f0a04SGreg Roach     */
89839ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
899c1010edaSGreg Roach    {
9004b9ff166SGreg Roach        if ($access_level === null) {
9014b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
9024b9ff166SGreg Roach        }
9034b9ff166SGreg Roach
904acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
905a25f0a04SGreg Roach
90639ca88baSGreg Roach        $families = new Collection();
90739ca88baSGreg Roach
9088d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
909dc124885SGreg Roach            $family = $fact->target();
910e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
91139ca88baSGreg Roach                $families->push($family);
912a25f0a04SGreg Roach            }
913a25f0a04SGreg Roach        }
914a25f0a04SGreg Roach
915a25f0a04SGreg Roach        return $families;
916a25f0a04SGreg Roach    }
917a25f0a04SGreg Roach
918a25f0a04SGreg Roach    /**
919a25f0a04SGreg Roach     * Get the preferred parents for this individual.
920a25f0a04SGreg Roach     *
921a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
922a25f0a04SGreg Roach     * The preferred family record is:
923a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
924a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
925a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
926a25f0a04SGreg Roach     * (d) the first one found
927a25f0a04SGreg Roach     *
928a25f0a04SGreg Roach     * @return Family|null
929a25f0a04SGreg Roach     */
930e364afe4SGreg Roach    public function primaryChildFamily(): ?Family
931c1010edaSGreg Roach    {
93239ca88baSGreg Roach        $families = $this->childFamilies();
9333b092cb5SGreg Roach        switch ($families->count()) {
934a25f0a04SGreg Roach            case 0:
935a25f0a04SGreg Roach                return null;
936a25f0a04SGreg Roach            case 1:
93715d603e7SGreg Roach                return $families[0];
938a25f0a04SGreg Roach            default:
939a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
940a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
94174e78bfaSJonathan Jaubart                foreach ($families as $fam) {
942c0935879SGreg Roach                    $famid = $fam->xref();
9437d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
944a25f0a04SGreg Roach                        return $fam;
945a25f0a04SGreg Roach                    }
946a25f0a04SGreg Roach                }
947a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
94874e78bfaSJonathan Jaubart                foreach ($families as $fam) {
949c0935879SGreg Roach                    $famid = $fam->xref();
9507d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
951a25f0a04SGreg Roach                        return $fam;
952a25f0a04SGreg Roach                    }
953a25f0a04SGreg Roach                }
954a25f0a04SGreg Roach                // c) records with no '2 PEDI'
95574e78bfaSJonathan Jaubart                foreach ($families as $fam) {
956c0935879SGreg Roach                    $famid = $fam->xref();
9577d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
958a25f0a04SGreg Roach                        return $fam;
959a25f0a04SGreg Roach                    }
960a25f0a04SGreg Roach                }
961a25f0a04SGreg Roach
962a25f0a04SGreg Roach                // d) any record
96315d603e7SGreg Roach                return $families[0];
964a25f0a04SGreg Roach        }
965a25f0a04SGreg Roach    }
966a25f0a04SGreg Roach
967a25f0a04SGreg Roach    /**
968a25f0a04SGreg Roach     * Get a list of step-parent families.
969a25f0a04SGreg Roach     *
97054c7f8dfSGreg Roach     * @return Collection
97154c7f8dfSGreg Roach     * @return Family[]
972a25f0a04SGreg Roach     */
973820b62dfSGreg Roach    public function childStepFamilies(): Collection
974c1010edaSGreg Roach    {
97513abd6f3SGreg Roach        $step_families = [];
97639ca88baSGreg Roach        $families      = $this->childFamilies();
977a25f0a04SGreg Roach        foreach ($families as $family) {
97839ca88baSGreg Roach            $father = $family->husband();
979a25f0a04SGreg Roach            if ($father) {
98039ca88baSGreg Roach                foreach ($father->spouseFamilies() as $step_family) {
98139ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
982a25f0a04SGreg Roach                        $step_families[] = $step_family;
983a25f0a04SGreg Roach                    }
984a25f0a04SGreg Roach                }
985a25f0a04SGreg Roach            }
98639ca88baSGreg Roach            $mother = $family->wife();
987a25f0a04SGreg Roach            if ($mother) {
98839ca88baSGreg Roach                foreach ($mother->spouseFamilies() as $step_family) {
98939ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
990a25f0a04SGreg Roach                        $step_families[] = $step_family;
991a25f0a04SGreg Roach                    }
992a25f0a04SGreg Roach                }
993a25f0a04SGreg Roach            }
994a25f0a04SGreg Roach        }
995a25f0a04SGreg Roach
996820b62dfSGreg Roach        return new Collection($step_families);
997a25f0a04SGreg Roach    }
998a25f0a04SGreg Roach
999a25f0a04SGreg Roach    /**
1000a25f0a04SGreg Roach     * Get a list of step-parent families.
1001a25f0a04SGreg Roach     *
100254c7f8dfSGreg Roach     * @return Collection
100354c7f8dfSGreg Roach     * @return Family[]
1004a25f0a04SGreg Roach     */
1005820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
1006c1010edaSGreg Roach    {
100713abd6f3SGreg Roach        $step_families = [];
100839ca88baSGreg Roach        $families      = $this->spouseFamilies();
1009820b62dfSGreg Roach
1010a25f0a04SGreg Roach        foreach ($families as $family) {
101139ca88baSGreg Roach            $spouse = $family->spouse($this);
1012820b62dfSGreg Roach
1013a25f0a04SGreg Roach            if ($spouse) {
101439ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
101539ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
1016a25f0a04SGreg Roach                        $step_families[] = $step_family;
1017a25f0a04SGreg Roach                    }
1018a25f0a04SGreg Roach                }
1019a25f0a04SGreg Roach            }
1020a25f0a04SGreg Roach        }
1021a25f0a04SGreg Roach
1022820b62dfSGreg Roach        return new Collection($step_families);
1023a25f0a04SGreg Roach    }
1024a25f0a04SGreg Roach
1025a25f0a04SGreg Roach    /**
1026a25f0a04SGreg Roach     * A label for a parental family group
1027a25f0a04SGreg Roach     *
1028a25f0a04SGreg Roach     * @param Family $family
1029a25f0a04SGreg Roach     *
1030a25f0a04SGreg Roach     * @return string
1031a25f0a04SGreg Roach     */
1032820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
1033c1010edaSGreg Roach    {
10347d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
1035a25f0a04SGreg Roach            // A specified pedigree
1036764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1037b2ce94c6SRico Sonntag        }
1038b2ce94c6SRico Sonntag
1039a25f0a04SGreg Roach        // Default (birth) pedigree
1040764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1041a25f0a04SGreg Roach    }
1042a25f0a04SGreg Roach
1043a25f0a04SGreg Roach    /**
1044a25f0a04SGreg Roach     * Create a label for a step family
1045a25f0a04SGreg Roach     *
1046a25f0a04SGreg Roach     * @param Family $step_family
1047a25f0a04SGreg Roach     *
1048a25f0a04SGreg Roach     * @return string
1049a25f0a04SGreg Roach     */
10508f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1051c1010edaSGreg Roach    {
105239ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
1053a25f0a04SGreg Roach            if ($family !== $step_family) {
1054a25f0a04SGreg Roach                // Must be a step-family
105539ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
105639ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
1057a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1058a25f0a04SGreg Roach                            // One common parent - must be a step family
1059e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
1060a25f0a04SGreg Roach                                // Father’s family with someone else
106139ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
1062a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
106339ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
1064b2ce94c6SRico Sonntag                                }
1065b2ce94c6SRico Sonntag
1066a25f0a04SGreg Roach                                /* I18N: A step-family. */
1067bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1068a25f0a04SGreg Roach                            }
1069b2ce94c6SRico Sonntag
1070a25f0a04SGreg Roach                            // Mother’s family with someone else
107139ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
1072a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
107339ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
1074b2ce94c6SRico Sonntag                            }
1075b2ce94c6SRico Sonntag
1076a25f0a04SGreg Roach                            /* I18N: A step-family. */
1077bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1078a25f0a04SGreg Roach                        }
1079a25f0a04SGreg Roach                    }
1080a25f0a04SGreg Roach                }
1081a25f0a04SGreg Roach            }
1082a25f0a04SGreg Roach        }
1083a25f0a04SGreg Roach
1084a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1085a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1086a25f0a04SGreg Roach    }
1087a25f0a04SGreg Roach
1088225e381fSGreg Roach    /**
1089225e381fSGreg Roach     * Get the description for the family.
1090225e381fSGreg Roach     *
1091225e381fSGreg Roach     * For example, "XXX's family with new wife".
1092225e381fSGreg Roach     *
1093225e381fSGreg Roach     * @param Family $family
1094225e381fSGreg Roach     *
1095225e381fSGreg Roach     * @return string
1096225e381fSGreg Roach     */
1097e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
1098c1010edaSGreg Roach    {
109939ca88baSGreg Roach        $spouse = $family->spouse($this);
1100225e381fSGreg Roach        if ($spouse) {
1101225e381fSGreg Roach            /* I18N: %s is the spouse name */
110239ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1103225e381fSGreg Roach        }
1104b2ce94c6SRico Sonntag
110539ca88baSGreg Roach        return $family->fullName();
1106225e381fSGreg Roach    }
1107225e381fSGreg Roach
1108a25f0a04SGreg Roach    /**
1109a25f0a04SGreg Roach     * get primary parents names for this individual
1110a25f0a04SGreg Roach     *
1111a25f0a04SGreg Roach     * @param string $classname optional css class
1112a25f0a04SGreg Roach     * @param string $display   optional css style display
1113a25f0a04SGreg Roach     *
1114a25f0a04SGreg Roach     * @return string a div block with father & mother names
1115a25f0a04SGreg Roach     */
11168f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1117c1010edaSGreg Roach    {
111839ca88baSGreg Roach        $fam = $this->primaryChildFamily();
1119a25f0a04SGreg Roach        if (!$fam) {
1120a25f0a04SGreg Roach            return '';
1121a25f0a04SGreg Roach        }
1122a25f0a04SGreg Roach        $txt = '<div';
1123a25f0a04SGreg Roach        if ($classname) {
11244c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1125a25f0a04SGreg Roach        }
1126a25f0a04SGreg Roach        if ($display) {
11274c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1128a25f0a04SGreg Roach        }
1129a25f0a04SGreg Roach        $txt .= '>';
113039ca88baSGreg Roach        $husb = $fam->husband();
1131a25f0a04SGreg Roach        if ($husb) {
1132a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1133a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1134a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1135a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1136a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
113739ca88baSGreg Roach            $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>';
1138a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1139a25f0a04SGreg Roach        }
114039ca88baSGreg Roach        $wife = $fam->wife();
1141a25f0a04SGreg Roach        if ($wife) {
1142a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1143a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1144a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1145a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1146a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
114739ca88baSGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->fullName());
1148a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1149a25f0a04SGreg Roach        }
1150a25f0a04SGreg Roach        $txt .= '</div>';
1151a25f0a04SGreg Roach
1152a25f0a04SGreg Roach        return $txt;
1153a25f0a04SGreg Roach    }
1154a25f0a04SGreg Roach
1155961ec755SGreg Roach    /**
1156961ec755SGreg Roach     * If this object has no name, what do we call it?
1157961ec755SGreg Roach     *
1158961ec755SGreg Roach     * @return string
1159961ec755SGreg Roach     */
11608f53f488SRico Sonntag    public function getFallBackName(): string
1161c1010edaSGreg Roach    {
1162a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1163a25f0a04SGreg Roach    }
1164a25f0a04SGreg Roach
1165a25f0a04SGreg Roach    /**
1166a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1167a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1168a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1169a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1170a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1171a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1172a25f0a04SGreg Roach     *
1173a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1174a25f0a04SGreg Roach     * 2 GIVN Robert
1175a25f0a04SGreg Roach     * 2 SPFX de
1176a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1177a25f0a04SGreg Roach     * 2 NICK The Bald
1178a25f0a04SGreg Roach     *
1179a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1180a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1181a25f0a04SGreg Roach     *
1182a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1183a25f0a04SGreg Roach     *
1184a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1185a25f0a04SGreg Roach     * or
1186a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1187a25f0a04SGreg Roach     * 2 GIVN Carlos
1188a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1189a25f0a04SGreg Roach     *
1190a25f0a04SGreg Roach     * @param string $type
1191a25f0a04SGreg Roach     * @param string $full
1192a25f0a04SGreg Roach     * @param string $gedcom
1193e364afe4SGreg Roach     *
1194e364afe4SGreg Roach     * @return void
1195a25f0a04SGreg Roach     */
1196e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1197c1010edaSGreg Roach    {
1198a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1199a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1200a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1201a25f0a04SGreg Roach
120276f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1203a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1204a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1205a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1206a25f0a04SGreg Roach
1207a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
120876f666f4SGreg Roach        if ($SURN !== '') {
1209a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1210a25f0a04SGreg Roach        } else {
121113abd6f3SGreg Roach            $SURNS = [];
1212a25f0a04SGreg Roach        }
121376f666f4SGreg Roach
1214a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1215a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1216a25f0a04SGreg Roach
1217a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1218a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1219a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1220a25f0a04SGreg Roach
1221a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
122276f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1223e364afe4SGreg Roach            $full .= '/';
1224a25f0a04SGreg Roach        }
1225a25f0a04SGreg Roach
1226a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1227a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1228a25f0a04SGreg Roach
1229a25f0a04SGreg Roach        // Extract the surname.
1230a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1231a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1232a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1233a25f0a04SGreg Roach        } else {
1234a25f0a04SGreg Roach            $surname = '';
1235a25f0a04SGreg Roach        }
1236a25f0a04SGreg Roach
1237a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1238a25f0a04SGreg Roach        if (!$SURNS) {
1239a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1240a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1241a25f0a04SGreg Roach                $SURNS = $matches[1];
1242a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1243a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1244a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1245a25f0a04SGreg Roach                }
1246a25f0a04SGreg Roach            } else {
1247a25f0a04SGreg Roach                // It is valid not to have a surname at all
124813abd6f3SGreg Roach                $SURNS = [''];
1249a25f0a04SGreg Roach            }
1250a25f0a04SGreg Roach        }
1251a25f0a04SGreg Roach
1252a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1253a25f0a04SGreg Roach        if (!$GIVN) {
1254a25f0a04SGreg Roach            $GIVN = preg_replace(
125513abd6f3SGreg Roach                [
1256c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1257c1010edaSGreg Roach                    // remove surname
1258c1010edaSGreg Roach                    '/ ?".+"/',
1259c1010edaSGreg Roach                    // remove nickname
1260c1010edaSGreg Roach                    '/ {2,}/',
1261c1010edaSGreg Roach                    // multiple spaces, caused by the above
1262c1010edaSGreg Roach                    '/^ | $/',
1263c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
126413abd6f3SGreg Roach                ],
126513abd6f3SGreg Roach                [
1266a25f0a04SGreg Roach                    ' ',
1267a25f0a04SGreg Roach                    ' ',
1268a25f0a04SGreg Roach                    ' ',
1269a25f0a04SGreg Roach                    '',
127013abd6f3SGreg Roach                ],
1271a25f0a04SGreg Roach                $full
1272a25f0a04SGreg Roach            );
1273a25f0a04SGreg Roach        }
1274a25f0a04SGreg Roach
1275a25f0a04SGreg Roach        // Add placeholder for unknown given name
1276a25f0a04SGreg Roach        if (!$GIVN) {
1277a25f0a04SGreg Roach            $GIVN = '@P.N.';
127873f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1279a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1280a25f0a04SGreg Roach        }
1281a25f0a04SGreg Roach
12827b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12837b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1284c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12857b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1286c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1287a25f0a04SGreg Roach        }
1288a25f0a04SGreg Roach
1289a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1290a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1291a25f0a04SGreg Roach        // $full is for display on-screen
1292a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1293a25f0a04SGreg Roach
1294a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1295ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1296ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1297c6f196c3SGreg Roach        // Format for display
1298d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1299acc34ea1SGreg Roach        // Localise quotation marks around the nickname
130018d7a90dSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function (array $matches): string {
13018d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
13028d68cabeSGreg Roach        }, $full);
1303a25f0a04SGreg Roach
1304c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1305a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1306a25f0a04SGreg Roach
1307a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1308a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1309a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1310a25f0a04SGreg Roach
1311ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1312a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1313e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1314a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1315e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1316a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1317a25f0a04SGreg Roach            }
1318a25f0a04SGreg Roach
1319bdb3725aSGreg Roach            $this->getAllNames[] = [
1320a25f0a04SGreg Roach                'type'    => $type,
1321a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1322c1010edaSGreg Roach                'full'    => $full,
1323c1010edaSGreg Roach                // This is used for display
1324c1010edaSGreg Roach                'fullNN'  => $fullNN,
1325c1010edaSGreg Roach                // This goes into the database
1326c1010edaSGreg Roach                'surname' => $surname,
1327c1010edaSGreg Roach                // This goes into the database
1328c1010edaSGreg Roach                'givn'    => $GIVN,
1329c1010edaSGreg Roach                // This goes into the database
1330c1010edaSGreg Roach                'surn'    => $SURN,
1331c1010edaSGreg Roach                // This goes into the database
133213abd6f3SGreg Roach            ];
1333a25f0a04SGreg Roach        }
1334a25f0a04SGreg Roach    }
1335a25f0a04SGreg Roach
1336a25f0a04SGreg Roach    /**
133776692c8bSGreg Roach     * Extract names from the GEDCOM record.
1338c7ff4153SGreg Roach     *
1339c7ff4153SGreg Roach     * @return void
1340a25f0a04SGreg Roach     */
1341e364afe4SGreg Roach    public function extractNames(): void
1342c1010edaSGreg Roach    {
13438f53f488SRico Sonntag        $this->extractNamesFromFacts(
13448f53f488SRico Sonntag            1,
13458f53f488SRico Sonntag            'NAME',
134630158ae7SGreg Roach            $this->facts(
13478d0ebef0SGreg Roach                ['NAME'],
13488f53f488SRico Sonntag                false,
13498f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13508f53f488SRico Sonntag                $this->canShowName()
13518f53f488SRico Sonntag            )
13528f53f488SRico Sonntag        );
1353a25f0a04SGreg Roach    }
1354a25f0a04SGreg Roach
1355a25f0a04SGreg Roach    /**
1356a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1357a25f0a04SGreg Roach     * selection items or favorites.
1358a25f0a04SGreg Roach     *
1359a25f0a04SGreg Roach     * @return string
1360a25f0a04SGreg Roach     */
13618f53f488SRico Sonntag    public function formatListDetails(): string
1362c1010edaSGreg Roach    {
1363a25f0a04SGreg Roach        return
13648d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13658d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1366a25f0a04SGreg Roach    }
1367a25f0a04SGreg Roach}
1368