xref: /webtrees/app/Individual.php (revision 852ede8c85e501a9df6f3c9d31e5933428e18cc2)
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;
26*852ede8cSGreg 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
38*852ede8cSGreg 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     *
52886b77daSGreg Roach     * @return Closure
53886b77daSGreg Roach     */
54c0804649SGreg Roach    public static function rowMapper(): Closure
55886b77daSGreg Roach    {
566c2179e2SGreg Roach        return static function (stdClass $row): Individual {
57a7a24840SGreg Roach            $individual = Individual::getInstance($row->i_id, Tree::findById((int) $row->i_file), $row->i_gedcom);
58e84cf2deSGreg Roach
59e84cf2deSGreg Roach            if ($row->n_num ?? null) {
60e0458bdcSGreg Roach                $individual = clone $individual;
61e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
62e0458bdcSGreg Roach
63e0458bdcSGreg Roach                return $individual;
64e84cf2deSGreg Roach            }
65e84cf2deSGreg Roach
66e84cf2deSGreg Roach            return $individual;
67886b77daSGreg Roach        };
68886b77daSGreg Roach    }
69886b77daSGreg Roach
70886b77daSGreg Roach    /**
71c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
72c156e8f5SGreg Roach     *
73c156e8f5SGreg Roach     * @return Closure
74c156e8f5SGreg Roach     */
75c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
76c156e8f5SGreg Roach    {
776c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
78c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
79c156e8f5SGreg Roach        };
80c156e8f5SGreg Roach    }
81c156e8f5SGreg Roach
82c156e8f5SGreg Roach    /**
83c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
84c156e8f5SGreg Roach     *
85c156e8f5SGreg Roach     * @return Closure
86c156e8f5SGreg Roach     */
87c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
88c156e8f5SGreg Roach    {
896c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
90c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
91c156e8f5SGreg Roach        };
92c156e8f5SGreg Roach    }
93c156e8f5SGreg Roach
94c156e8f5SGreg Roach    /**
95e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
96e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
97e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
98e71ef9d2SGreg Roach     *
99e71ef9d2SGreg Roach     * @param string      $xref
100e71ef9d2SGreg Roach     * @param Tree        $tree
101e71ef9d2SGreg Roach     * @param string|null $gedcom
102e71ef9d2SGreg Roach     *
1036ccdf4f0SGreg Roach     * @throws Exception
104e71ef9d2SGreg Roach     * @return Individual|null
105e71ef9d2SGreg Roach     */
106e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
107c1010edaSGreg Roach    {
108e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
109e71ef9d2SGreg Roach
110e364afe4SGreg Roach        if ($record instanceof self) {
111e71ef9d2SGreg Roach            return $record;
112e71ef9d2SGreg Roach        }
113b2ce94c6SRico Sonntag
114b2ce94c6SRico Sonntag        return null;
115e71ef9d2SGreg Roach    }
116e71ef9d2SGreg Roach
117e71ef9d2SGreg Roach    /**
118395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
119395f0fe0SGreg Roach     * Typically when we load families and their members.
120395f0fe0SGreg Roach     *
121395f0fe0SGreg Roach     * @param Tree     $tree
1224a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
12318d7a90dSGreg Roach     *
12418d7a90dSGreg Roach     * @return void
125395f0fe0SGreg Roach     */
1262e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
127c1010edaSGreg Roach    {
1282e5b4452SGreg Roach        $rows = DB::table('individuals')
1292e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1302e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1312e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1322e5b4452SGreg Roach            ->get();
133395f0fe0SGreg Roach
134395f0fe0SGreg Roach        foreach ($rows as $row) {
135395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
136395f0fe0SGreg Roach        }
137395f0fe0SGreg Roach    }
138395f0fe0SGreg Roach
139395f0fe0SGreg Roach    /**
140a25f0a04SGreg Roach     * Can the name of this record be shown?
141a25f0a04SGreg Roach     *
14276692c8bSGreg Roach     * @param int|null $access_level
14376692c8bSGreg Roach     *
14476692c8bSGreg Roach     * @return bool
145a25f0a04SGreg Roach     */
14635584196SGreg Roach    public function canShowName(int $access_level = null): bool
147c1010edaSGreg Roach    {
1484b9ff166SGreg Roach        if ($access_level === null) {
1494b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1504b9ff166SGreg Roach        }
1514b9ff166SGreg Roach
152518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
153a25f0a04SGreg Roach    }
154a25f0a04SGreg Roach
155a25f0a04SGreg Roach    /**
15676692c8bSGreg Roach     * Can this individual be shown?
157a25f0a04SGreg Roach     *
15876692c8bSGreg Roach     * @param int $access_level
15976692c8bSGreg Roach     *
16076692c8bSGreg Roach     * @return bool
161a25f0a04SGreg Roach     */
16235584196SGreg Roach    protected function canShowByType(int $access_level): bool
163c1010edaSGreg Roach    {
164a25f0a04SGreg Roach        // Dead people...
165518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
166a25f0a04SGreg Roach            $keep_alive             = false;
16754ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
168a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1698d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
170a25f0a04SGreg Roach                foreach ($matches as $match) {
171a25f0a04SGreg Roach                    $date = new Date($match[1]);
172a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
173a25f0a04SGreg Roach                        $keep_alive = true;
174a25f0a04SGreg Roach                        break;
175a25f0a04SGreg Roach                    }
176a25f0a04SGreg Roach                }
177a25f0a04SGreg Roach            }
17854ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
179a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1808d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
181a25f0a04SGreg Roach                foreach ($matches as $match) {
182a25f0a04SGreg Roach                    $date = new Date($match[1]);
183a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
184a25f0a04SGreg Roach                        $keep_alive = true;
185a25f0a04SGreg Roach                        break;
186a25f0a04SGreg Roach                    }
187a25f0a04SGreg Roach                }
188a25f0a04SGreg Roach            }
189a25f0a04SGreg Roach            if (!$keep_alive) {
190a25f0a04SGreg Roach                return true;
191a25f0a04SGreg Roach            }
192a25f0a04SGreg Roach        }
193a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
19454ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1954b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
19654ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1974b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
198a25f0a04SGreg Roach        }
199a25f0a04SGreg Roach
200a25f0a04SGreg Roach        // No restriction found - show living people to members only:
2014b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
202a25f0a04SGreg Roach    }
203a25f0a04SGreg Roach
204a25f0a04SGreg Roach    /**
205a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
206a25f0a04SGreg Roach     *
207a25f0a04SGreg Roach     * @param Individual $target
208cbc1590aSGreg Roach     * @param int        $distance
209a25f0a04SGreg Roach     *
210cbc1590aSGreg Roach     * @return bool
211a25f0a04SGreg Roach     */
2128f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
213c1010edaSGreg Roach    {
214a25f0a04SGreg Roach        static $cache = null;
215a25f0a04SGreg Roach
21606ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
217a25f0a04SGreg Roach        if ($user_individual) {
218a25f0a04SGreg Roach            if (!$cache) {
21913abd6f3SGreg Roach                $cache = [
22013abd6f3SGreg Roach                    0 => [$user_individual],
22113abd6f3SGreg Roach                    1 => [],
22213abd6f3SGreg Roach                ];
2238d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
224dc124885SGreg Roach                    $family = $fact->target();
225e24444eeSGreg Roach                    if ($family instanceof Family) {
226a25f0a04SGreg Roach                        $cache[1][] = $family;
227a25f0a04SGreg Roach                    }
228a25f0a04SGreg Roach                }
229a25f0a04SGreg Roach            }
230a25f0a04SGreg Roach        } else {
231a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
232a25f0a04SGreg Roach            return true;
233a25f0a04SGreg Roach        }
234a25f0a04SGreg Roach
235a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
236a25f0a04SGreg Roach        $distance *= 2;
237a25f0a04SGreg Roach
238a25f0a04SGreg Roach        // Consider each path length in turn
239a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
240a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
241a25f0a04SGreg Roach                // We have already calculated all records with this length
242e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
243a25f0a04SGreg Roach                    return true;
244a25f0a04SGreg Roach                }
245a25f0a04SGreg Roach            } else {
246a25f0a04SGreg Roach                // Need to calculate these paths
24713abd6f3SGreg Roach                $cache[$n] = [];
248e364afe4SGreg Roach                if ($n % 2 === 0) {
249a25f0a04SGreg Roach                    // Add FAM->INDI links
250a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2518d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
252dc124885SGreg Roach                            $individual = $fact->target();
253a25f0a04SGreg Roach                            // Don’t backtrack
254e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
255a25f0a04SGreg Roach                                $cache[$n][] = $individual;
256a25f0a04SGreg Roach                            }
257a25f0a04SGreg Roach                        }
258a25f0a04SGreg Roach                    }
259a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
260a25f0a04SGreg Roach                        return true;
261a25f0a04SGreg Roach                    }
262a25f0a04SGreg Roach                } else {
263a25f0a04SGreg Roach                    // Add INDI->FAM links
264a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2658d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
266dc124885SGreg Roach                            $family = $fact->target();
267a25f0a04SGreg Roach                            // Don’t backtrack
268e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
269a25f0a04SGreg Roach                                $cache[$n][] = $family;
270a25f0a04SGreg Roach                            }
271a25f0a04SGreg Roach                        }
272a25f0a04SGreg Roach                    }
273a25f0a04SGreg Roach                }
274a25f0a04SGreg Roach            }
275a25f0a04SGreg Roach        }
276a25f0a04SGreg Roach
277a25f0a04SGreg Roach        return false;
278a25f0a04SGreg Roach    }
279a25f0a04SGreg Roach
28076692c8bSGreg Roach    /**
28176692c8bSGreg Roach     * Generate a private version of this record
28276692c8bSGreg Roach     *
28376692c8bSGreg Roach     * @param int $access_level
28476692c8bSGreg Roach     *
28576692c8bSGreg Roach     * @return string
28676692c8bSGreg Roach     */
2873c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
288c1010edaSGreg Roach    {
289acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
290a25f0a04SGreg Roach
291a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
292518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
293a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2948d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
295138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
296a25f0a04SGreg Roach            }
297a25f0a04SGreg Roach        }
298a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2998d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
300a25f0a04SGreg Roach        foreach ($matches as $match) {
30124ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
302a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
303a25f0a04SGreg Roach                $rec .= $match[0];
304a25f0a04SGreg Roach            }
305a25f0a04SGreg Roach        }
306a25f0a04SGreg Roach        // Don’t privatize sex.
307a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
308a25f0a04SGreg Roach            $rec .= $match[0];
309a25f0a04SGreg Roach        }
310a25f0a04SGreg Roach
311a25f0a04SGreg Roach        return $rec;
312a25f0a04SGreg Roach    }
313a25f0a04SGreg Roach
31476692c8bSGreg Roach    /**
31576692c8bSGreg Roach     * Fetch data from the database
31676692c8bSGreg Roach     *
31776692c8bSGreg Roach     * @param string $xref
31876692c8bSGreg Roach     * @param int    $tree_id
31976692c8bSGreg Roach     *
320e364afe4SGreg Roach     * @return string|null
32176692c8bSGreg Roach     */
322e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
323c1010edaSGreg Roach    {
3242e5b4452SGreg Roach        return DB::table('individuals')
3252e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3262e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3272e5b4452SGreg Roach            ->value('i_gedcom');
328a25f0a04SGreg Roach    }
329a25f0a04SGreg Roach
330a25f0a04SGreg Roach    /**
331a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
332a25f0a04SGreg Roach     * If not known to be dead, then assume living.
333a25f0a04SGreg Roach     *
334cbc1590aSGreg Roach     * @return bool
335a25f0a04SGreg Roach     */
3368f53f488SRico Sonntag    public function isDead(): bool
337c1010edaSGreg Roach    {
338c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3394459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
340a25f0a04SGreg Roach
341a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3428d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
343a25f0a04SGreg Roach            return true;
344a25f0a04SGreg Roach        }
345a25f0a04SGreg Roach
346a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
347a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
348a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
349a25f0a04SGreg Roach                $date = new Date($date_match);
350269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
351a25f0a04SGreg Roach                    return true;
352a25f0a04SGreg Roach                }
353a25f0a04SGreg Roach            }
354a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
355a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
356a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
357a25f0a04SGreg Roach                return false;
358a25f0a04SGreg Roach            }
359a25f0a04SGreg Roach        }
360a25f0a04SGreg Roach
361a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
362a25f0a04SGreg Roach
363a25f0a04SGreg Roach        // Check parents (birth and adopted)
36439ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
36539ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
366a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
367a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
368a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
369a25f0a04SGreg Roach                    $date = new Date($date_match);
370269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
371a25f0a04SGreg Roach                        return true;
372a25f0a04SGreg Roach                    }
373a25f0a04SGreg Roach                }
374a25f0a04SGreg Roach            }
375a25f0a04SGreg Roach        }
376a25f0a04SGreg Roach
377a25f0a04SGreg Roach        // Check spouses
37839ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
379a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
380a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
381a25f0a04SGreg Roach                $date = new Date($date_match);
382a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
383269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
384a25f0a04SGreg Roach                    return true;
385a25f0a04SGreg Roach                }
386a25f0a04SGreg Roach            }
387a25f0a04SGreg Roach            // Check spouse dates
38839ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
389a25f0a04SGreg Roach            if ($spouse) {
390a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
391a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
392a25f0a04SGreg Roach                    $date = new Date($date_match);
393a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
394269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
395a25f0a04SGreg Roach                        return true;
396a25f0a04SGreg Roach                    }
397a25f0a04SGreg Roach                }
398a25f0a04SGreg Roach            }
399a25f0a04SGreg Roach            // Check child dates
40039ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
401a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
402a25f0a04SGreg Roach                // Assume children born after age of 15
403a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
404a25f0a04SGreg Roach                    $date = new Date($date_match);
405269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
406a25f0a04SGreg Roach                        return true;
407a25f0a04SGreg Roach                    }
408a25f0a04SGreg Roach                }
409a25f0a04SGreg Roach                // Check grandchildren
41039ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
41139ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
412a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
413a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
414a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
415a25f0a04SGreg Roach                            $date = new Date($date_match);
416269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
417a25f0a04SGreg Roach                                return true;
418a25f0a04SGreg Roach                            }
419a25f0a04SGreg Roach                        }
420a25f0a04SGreg Roach                    }
421a25f0a04SGreg Roach                }
422a25f0a04SGreg Roach            }
423a25f0a04SGreg Roach        }
424a25f0a04SGreg Roach
425a25f0a04SGreg Roach        return false;
426a25f0a04SGreg Roach    }
427a25f0a04SGreg Roach
428a25f0a04SGreg Roach    /**
429a25f0a04SGreg Roach     * Find the highlighted media object for an individual
430a25f0a04SGreg Roach     *
431e364afe4SGreg Roach     * @return MediaFile|null
432a25f0a04SGreg Roach     */
433e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
434c1010edaSGreg Roach    {
4358d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
436dc124885SGreg Roach            $media = $fact->target();
437a213a63cSGreg Roach            if ($media instanceof Media) {
4384a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
439a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4404a9f750fSGreg Roach                        return $media_file;
4414a9f750fSGreg Roach                    }
4424a9f750fSGreg Roach                }
443a25f0a04SGreg Roach            }
444a25f0a04SGreg Roach        }
445a25f0a04SGreg Roach
446a25f0a04SGreg Roach        return null;
447a25f0a04SGreg Roach    }
448a25f0a04SGreg Roach
449a25f0a04SGreg Roach    /**
450a25f0a04SGreg Roach     * Display the prefered image for this individual.
451a25f0a04SGreg Roach     * Use an icon if no image is available.
452a25f0a04SGreg Roach     *
453dce07401SGreg Roach     * @param int      $width      Pixels
454dce07401SGreg Roach     * @param int      $height     Pixels
455dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
456dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
457dce07401SGreg Roach     *
458a25f0a04SGreg Roach     * @return string
459a25f0a04SGreg Roach     */
4608f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
461c1010edaSGreg Roach    {
4624a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4634a9f750fSGreg Roach
4644a9f750fSGreg Roach        if ($media_file !== null) {
4654a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
466a25f0a04SGreg Roach        }
4674a9f750fSGreg Roach
4684a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
46939ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4704a9f750fSGreg Roach        }
4714a9f750fSGreg Roach
4724a9f750fSGreg Roach        return '';
473a25f0a04SGreg Roach    }
474a25f0a04SGreg Roach
475a25f0a04SGreg Roach    /**
476a25f0a04SGreg Roach     * Get the date of birth
477a25f0a04SGreg Roach     *
478a25f0a04SGreg Roach     * @return Date
479a25f0a04SGreg Roach     */
4808f53f488SRico Sonntag    public function getBirthDate(): Date
481c1010edaSGreg Roach    {
482a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
483a25f0a04SGreg Roach            if ($date->isOK()) {
484a25f0a04SGreg Roach                return $date;
485a25f0a04SGreg Roach            }
486a25f0a04SGreg Roach        }
487a25f0a04SGreg Roach
488a25f0a04SGreg Roach        return new Date('');
489a25f0a04SGreg Roach    }
490a25f0a04SGreg Roach
491a25f0a04SGreg Roach    /**
492a25f0a04SGreg Roach     * Get the place of birth
493a25f0a04SGreg Roach     *
49416d0b7f7SRico Sonntag     * @return Place
495a25f0a04SGreg Roach     */
4968f53f488SRico Sonntag    public function getBirthPlace(): Place
497c1010edaSGreg Roach    {
498a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
499a25f0a04SGreg Roach            return $place;
500a25f0a04SGreg Roach        }
501a25f0a04SGreg Roach
502b20ddbf9SGreg Roach        return new Place('', $this->tree);
503a25f0a04SGreg Roach    }
504a25f0a04SGreg Roach
505a25f0a04SGreg Roach    /**
506a25f0a04SGreg Roach     * Get the year of birth
507a25f0a04SGreg Roach     *
508a25f0a04SGreg Roach     * @return string the year of birth
509a25f0a04SGreg Roach     */
5108f53f488SRico Sonntag    public function getBirthYear(): string
511c1010edaSGreg Roach    {
512f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
513a25f0a04SGreg Roach    }
514a25f0a04SGreg Roach
515a25f0a04SGreg Roach    /**
516a25f0a04SGreg Roach     * Get the date of death
517a25f0a04SGreg Roach     *
518a25f0a04SGreg Roach     * @return Date
519a25f0a04SGreg Roach     */
5208f53f488SRico Sonntag    public function getDeathDate(): Date
521c1010edaSGreg Roach    {
522a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
523a25f0a04SGreg Roach            if ($date->isOK()) {
524a25f0a04SGreg Roach                return $date;
525a25f0a04SGreg Roach            }
526a25f0a04SGreg Roach        }
527a25f0a04SGreg Roach
528a25f0a04SGreg Roach        return new Date('');
529a25f0a04SGreg Roach    }
530a25f0a04SGreg Roach
531a25f0a04SGreg Roach    /**
532a25f0a04SGreg Roach     * Get the place of death
533a25f0a04SGreg Roach     *
53416d0b7f7SRico Sonntag     * @return Place
535a25f0a04SGreg Roach     */
5368f53f488SRico Sonntag    public function getDeathPlace(): Place
537c1010edaSGreg Roach    {
538a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
539a25f0a04SGreg Roach            return $place;
540a25f0a04SGreg Roach        }
541a25f0a04SGreg Roach
542b20ddbf9SGreg Roach        return new Place('', $this->tree);
543a25f0a04SGreg Roach    }
544a25f0a04SGreg Roach
545a25f0a04SGreg Roach    /**
546a25f0a04SGreg Roach     * get the death year
547a25f0a04SGreg Roach     *
548a25f0a04SGreg Roach     * @return string the year of death
549a25f0a04SGreg Roach     */
5508f53f488SRico Sonntag    public function getDeathYear(): string
551c1010edaSGreg Roach    {
552f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
553a25f0a04SGreg Roach    }
554a25f0a04SGreg Roach
555a25f0a04SGreg Roach    /**
556a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
55715d603e7SGreg Roach     * Provide the place and full date using a tooltip.
558a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
559a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
560a25f0a04SGreg Roach     *
561a25f0a04SGreg Roach     * @return string
562a25f0a04SGreg Roach     */
5638f53f488SRico Sonntag    public function getLifeSpan(): string
564c1010edaSGreg Roach    {
56515d603e7SGreg Roach        // Just the first part of the place name
566392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
567392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
56815d603e7SGreg Roach        // Remove markup from dates
56915d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
57015d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
57115d603e7SGreg Roach
572c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
573dacfe33cSGreg Roach        return I18N::translate(
574a25f0a04SGreg Roach            '%1$s–%2$s',
5752d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5762d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
577a25f0a04SGreg Roach        );
578a25f0a04SGreg Roach    }
579a25f0a04SGreg Roach
580a25f0a04SGreg Roach    /**
581a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
582a25f0a04SGreg Roach     *
583a25f0a04SGreg Roach     * @return Date[]
584a25f0a04SGreg Roach     */
5858f53f488SRico Sonntag    public function getAllBirthDates(): array
586c1010edaSGreg Roach    {
5878d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5888d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
589a25f0a04SGreg Roach            if ($tmp) {
590a25f0a04SGreg Roach                return $tmp;
591a25f0a04SGreg Roach            }
592a25f0a04SGreg Roach        }
593a25f0a04SGreg Roach
59413abd6f3SGreg Roach        return [];
595a25f0a04SGreg Roach    }
596a25f0a04SGreg Roach
597a25f0a04SGreg Roach    /**
598a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
599a25f0a04SGreg Roach     *
6004080d558SGreg Roach     * @return Place[]
601a25f0a04SGreg Roach     */
6028f53f488SRico Sonntag    public function getAllBirthPlaces(): array
603c1010edaSGreg Roach    {
6048d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
6058d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
60654c1ab5eSGreg Roach            if ($places !== []) {
6074080d558SGreg Roach                return $places;
608a25f0a04SGreg Roach            }
609a25f0a04SGreg Roach        }
610a25f0a04SGreg Roach
61113abd6f3SGreg Roach        return [];
612a25f0a04SGreg Roach    }
613a25f0a04SGreg Roach
614a25f0a04SGreg Roach    /**
615a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
616a25f0a04SGreg Roach     *
617a25f0a04SGreg Roach     * @return Date[]
618a25f0a04SGreg Roach     */
6198f53f488SRico Sonntag    public function getAllDeathDates(): array
620c1010edaSGreg Roach    {
6218d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6228d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
623a25f0a04SGreg Roach            if ($tmp) {
624a25f0a04SGreg Roach                return $tmp;
625a25f0a04SGreg Roach            }
626a25f0a04SGreg Roach        }
627a25f0a04SGreg Roach
62813abd6f3SGreg Roach        return [];
629a25f0a04SGreg Roach    }
630a25f0a04SGreg Roach
631a25f0a04SGreg Roach    /**
632a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
633a25f0a04SGreg Roach     *
6344080d558SGreg Roach     * @return Place[]
635a25f0a04SGreg Roach     */
6368f53f488SRico Sonntag    public function getAllDeathPlaces(): array
637c1010edaSGreg Roach    {
6388d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6398d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
64054c1ab5eSGreg Roach            if ($places !== []) {
6414080d558SGreg Roach                return $places;
642a25f0a04SGreg Roach            }
643a25f0a04SGreg Roach        }
644a25f0a04SGreg Roach
64513abd6f3SGreg Roach        return [];
646a25f0a04SGreg Roach    }
647a25f0a04SGreg Roach
648a25f0a04SGreg Roach    /**
649a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
650a25f0a04SGreg Roach     *
651a25f0a04SGreg Roach     * @return Date
652a25f0a04SGreg Roach     */
6538f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
654c1010edaSGreg Roach    {
6558f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
656a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
657a25f0a04SGreg Roach                if ($date->isOK()) {
6584686330aSGreg Roach                    $this->estimated_birth_date = $date;
659a25f0a04SGreg Roach                    break;
660a25f0a04SGreg Roach                }
661a25f0a04SGreg Roach            }
6628f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
66313abd6f3SGreg Roach                $min = [];
66413abd6f3SGreg Roach                $max = [];
665a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
666f5b60decSGreg Roach                if ($tmp->isOK()) {
667f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
668f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
669a25f0a04SGreg Roach                }
67039ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
671a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
672f5b60decSGreg Roach                    if ($tmp->isOK()) {
673f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
674f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
675a25f0a04SGreg Roach                    }
67639ca88baSGreg Roach                    $husband = $family->husband();
677e364afe4SGreg Roach                    if ($husband instanceof self) {
6782e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
679f5b60decSGreg Roach                        if ($tmp->isOK()) {
680f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
681f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
682a25f0a04SGreg Roach                        }
683a25f0a04SGreg Roach                    }
68439ca88baSGreg Roach                    $wife = $family->wife();
685e364afe4SGreg Roach                    if ($wife instanceof self) {
6862e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
687f5b60decSGreg Roach                        if ($tmp->isOK()) {
688f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
689f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
690a25f0a04SGreg Roach                        }
691a25f0a04SGreg Roach                    }
69239ca88baSGreg Roach                    foreach ($family->children() as $child) {
693a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
694f5b60decSGreg Roach                        if ($tmp->isOK()) {
695f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
696f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
697a25f0a04SGreg Roach                        }
698a25f0a04SGreg Roach                    }
699a25f0a04SGreg Roach                }
70039ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
701a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
702f5b60decSGreg Roach                    if ($tmp->isOK()) {
703f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
704f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
705a25f0a04SGreg Roach                    }
70639ca88baSGreg Roach                    $spouse = $family->spouse($this);
707a25f0a04SGreg Roach                    if ($spouse) {
708a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
709f5b60decSGreg Roach                        if ($tmp->isOK()) {
710f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
711f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
712a25f0a04SGreg Roach                        }
713a25f0a04SGreg Roach                    }
71439ca88baSGreg Roach                    foreach ($family->children() as $child) {
715a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
716f5b60decSGreg Roach                        if ($tmp->isOK()) {
717e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
718f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
719a25f0a04SGreg Roach                        }
720a25f0a04SGreg Roach                    }
721a25f0a04SGreg Roach                }
722a25f0a04SGreg Roach                if ($min && $max) {
72359f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
724a25f0a04SGreg Roach
72565e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7264686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
727a25f0a04SGreg Roach                } else {
7284686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
729a25f0a04SGreg Roach                }
730a25f0a04SGreg Roach            }
731a25f0a04SGreg Roach        }
732a25f0a04SGreg Roach
7334686330aSGreg Roach        return $this->estimated_birth_date;
734a25f0a04SGreg Roach    }
735a25f0a04SGreg Roach
736a25f0a04SGreg Roach    /**
737a25f0a04SGreg Roach     * Generate an estimated date of death.
738a25f0a04SGreg Roach     *
739a25f0a04SGreg Roach     * @return Date
740a25f0a04SGreg Roach     */
7418f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
742c1010edaSGreg Roach    {
7434686330aSGreg Roach        if ($this->estimated_death_date === null) {
744a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
745a25f0a04SGreg Roach                if ($date->isOK()) {
7464686330aSGreg Roach                    $this->estimated_death_date = $date;
747a25f0a04SGreg Roach                    break;
748a25f0a04SGreg Roach                }
749a25f0a04SGreg Roach            }
7504686330aSGreg Roach            if ($this->estimated_death_date === null) {
751f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
752c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7534686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
754a25f0a04SGreg Roach                } else {
7554686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
756a25f0a04SGreg Roach                }
757a25f0a04SGreg Roach            }
758a25f0a04SGreg Roach        }
759a25f0a04SGreg Roach
7604686330aSGreg Roach        return $this->estimated_death_date;
761a25f0a04SGreg Roach    }
762a25f0a04SGreg Roach
763a25f0a04SGreg Roach    /**
764a25f0a04SGreg Roach     * Get the sex - M F or U
765a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
766a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
767a25f0a04SGreg Roach     *
768a25f0a04SGreg Roach     * @return string
769a25f0a04SGreg Roach     */
770e364afe4SGreg Roach    public function sex(): string
771c1010edaSGreg Roach    {
772a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
773a25f0a04SGreg Roach            return $match[1];
774a25f0a04SGreg Roach        }
775b2ce94c6SRico Sonntag
776b2ce94c6SRico Sonntag        return 'U';
777a25f0a04SGreg Roach    }
778a25f0a04SGreg Roach
779a25f0a04SGreg Roach    /**
780a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
781a25f0a04SGreg Roach     *
782a25f0a04SGreg Roach     * @return string
783a25f0a04SGreg Roach     */
7848f53f488SRico Sonntag    public function getBoxStyle(): string
785c1010edaSGreg Roach    {
786c1010edaSGreg Roach        $tmp = [
787c1010edaSGreg Roach            'M' => '',
788c1010edaSGreg Roach            'F' => 'F',
789c1010edaSGreg Roach            'U' => 'NN',
790c1010edaSGreg Roach        ];
791a25f0a04SGreg Roach
79239ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
793a25f0a04SGreg Roach    }
794a25f0a04SGreg Roach
795a25f0a04SGreg Roach    /**
796a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
797a25f0a04SGreg Roach     *
798cbc1590aSGreg Roach     * @param int|null $access_level
799a25f0a04SGreg Roach     *
80054c7f8dfSGreg Roach     * @return Collection
801a25f0a04SGreg Roach     */
80239ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
803c1010edaSGreg Roach    {
8044b9ff166SGreg Roach        if ($access_level === null) {
8054b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8064b9ff166SGreg Roach        }
8074b9ff166SGreg Roach
808acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
809a25f0a04SGreg Roach
81039ca88baSGreg Roach        $families = new Collection();
8118d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
812dc124885SGreg Roach            $family = $fact->target();
813e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
81439ca88baSGreg Roach                $families->push($family);
815a25f0a04SGreg Roach            }
816a25f0a04SGreg Roach        }
817a25f0a04SGreg Roach
81839ca88baSGreg Roach        return new Collection($families);
819a25f0a04SGreg Roach    }
820a25f0a04SGreg Roach
821a25f0a04SGreg Roach    /**
822a25f0a04SGreg Roach     * Get the current spouse of this individual.
823a25f0a04SGreg Roach     *
824a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
825a25f0a04SGreg Roach     * in chronological order, and take the last one found.
826a25f0a04SGreg Roach     *
827a25f0a04SGreg Roach     * @return Individual|null
828a25f0a04SGreg Roach     */
829e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
830c1010edaSGreg Roach    {
83139ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
83239ca88baSGreg Roach
83339ca88baSGreg Roach        if ($family instanceof Family) {
83439ca88baSGreg Roach            return $family->spouse($this);
835a25f0a04SGreg Roach        }
836b2ce94c6SRico Sonntag
837b2ce94c6SRico Sonntag        return null;
838a25f0a04SGreg Roach    }
839a25f0a04SGreg Roach
840a25f0a04SGreg Roach    /**
841a25f0a04SGreg Roach     * Count the children belonging to this individual.
842a25f0a04SGreg Roach     *
843cbc1590aSGreg Roach     * @return int
844a25f0a04SGreg Roach     */
845e364afe4SGreg Roach    public function numberOfChildren(): int
846c1010edaSGreg Roach    {
8477d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8483dc7bbe9SGreg Roach            return (int) $match[1];
849b2ce94c6SRico Sonntag        }
850b2ce94c6SRico Sonntag
85113abd6f3SGreg Roach        $children = [];
85239ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
85339ca88baSGreg Roach            foreach ($fam->children() as $child) {
854c0935879SGreg Roach                $children[$child->xref()] = true;
855a25f0a04SGreg Roach            }
856a25f0a04SGreg Roach        }
857a25f0a04SGreg Roach
858a25f0a04SGreg Roach        return count($children);
859a25f0a04SGreg Roach    }
860a25f0a04SGreg Roach
861a25f0a04SGreg Roach    /**
862a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
863a25f0a04SGreg Roach     *
864cbc1590aSGreg Roach     * @param int|null $access_level
865a25f0a04SGreg Roach     *
86654c7f8dfSGreg Roach     * @return Collection
867a25f0a04SGreg Roach     */
86839ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
869c1010edaSGreg Roach    {
8704b9ff166SGreg Roach        if ($access_level === null) {
8714b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8724b9ff166SGreg Roach        }
8734b9ff166SGreg Roach
874acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
875a25f0a04SGreg Roach
87639ca88baSGreg Roach        $families = new Collection();
87739ca88baSGreg Roach
8788d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
879dc124885SGreg Roach            $family = $fact->target();
880e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
88139ca88baSGreg Roach                $families->push($family);
882a25f0a04SGreg Roach            }
883a25f0a04SGreg Roach        }
884a25f0a04SGreg Roach
885a25f0a04SGreg Roach        return $families;
886a25f0a04SGreg Roach    }
887a25f0a04SGreg Roach
888a25f0a04SGreg Roach    /**
889a25f0a04SGreg Roach     * Get the preferred parents for this individual.
890a25f0a04SGreg Roach     *
891a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
892a25f0a04SGreg Roach     * The preferred family record is:
893a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
894a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
895a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
896a25f0a04SGreg Roach     * (d) the first one found
897a25f0a04SGreg Roach     *
898a25f0a04SGreg Roach     * @return Family|null
899a25f0a04SGreg Roach     */
900e364afe4SGreg Roach    public function primaryChildFamily(): ?Family
901c1010edaSGreg Roach    {
90239ca88baSGreg Roach        $families = $this->childFamilies();
9033b092cb5SGreg Roach        switch ($families->count()) {
904a25f0a04SGreg Roach            case 0:
905a25f0a04SGreg Roach                return null;
906a25f0a04SGreg Roach            case 1:
90715d603e7SGreg Roach                return $families[0];
908a25f0a04SGreg Roach            default:
909a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
910a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
91174e78bfaSJonathan Jaubart                foreach ($families as $fam) {
912c0935879SGreg Roach                    $famid = $fam->xref();
9137d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
914a25f0a04SGreg Roach                        return $fam;
915a25f0a04SGreg Roach                    }
916a25f0a04SGreg Roach                }
917a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
91874e78bfaSJonathan Jaubart                foreach ($families as $fam) {
919c0935879SGreg Roach                    $famid = $fam->xref();
9207d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
921a25f0a04SGreg Roach                        return $fam;
922a25f0a04SGreg Roach                    }
923a25f0a04SGreg Roach                }
924a25f0a04SGreg Roach                // c) records with no '2 PEDI'
92574e78bfaSJonathan Jaubart                foreach ($families as $fam) {
926c0935879SGreg Roach                    $famid = $fam->xref();
9277d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
928a25f0a04SGreg Roach                        return $fam;
929a25f0a04SGreg Roach                    }
930a25f0a04SGreg Roach                }
931a25f0a04SGreg Roach
932a25f0a04SGreg Roach                // d) any record
93315d603e7SGreg Roach                return $families[0];
934a25f0a04SGreg Roach        }
935a25f0a04SGreg Roach    }
936a25f0a04SGreg Roach
937a25f0a04SGreg Roach    /**
938a25f0a04SGreg Roach     * Get a list of step-parent families.
939a25f0a04SGreg Roach     *
94054c7f8dfSGreg Roach     * @return Collection
941a25f0a04SGreg Roach     */
942820b62dfSGreg Roach    public function childStepFamilies(): Collection
943c1010edaSGreg Roach    {
94413abd6f3SGreg Roach        $step_families = [];
94539ca88baSGreg Roach        $families      = $this->childFamilies();
946a25f0a04SGreg Roach        foreach ($families as $family) {
94739ca88baSGreg Roach            $father = $family->husband();
948a25f0a04SGreg Roach            if ($father) {
94939ca88baSGreg Roach                foreach ($father->spouseFamilies() as $step_family) {
95039ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
951a25f0a04SGreg Roach                        $step_families[] = $step_family;
952a25f0a04SGreg Roach                    }
953a25f0a04SGreg Roach                }
954a25f0a04SGreg Roach            }
95539ca88baSGreg Roach            $mother = $family->wife();
956a25f0a04SGreg Roach            if ($mother) {
95739ca88baSGreg Roach                foreach ($mother->spouseFamilies() as $step_family) {
95839ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
959a25f0a04SGreg Roach                        $step_families[] = $step_family;
960a25f0a04SGreg Roach                    }
961a25f0a04SGreg Roach                }
962a25f0a04SGreg Roach            }
963a25f0a04SGreg Roach        }
964a25f0a04SGreg Roach
965820b62dfSGreg Roach        return new Collection($step_families);
966a25f0a04SGreg Roach    }
967a25f0a04SGreg Roach
968a25f0a04SGreg Roach    /**
969a25f0a04SGreg Roach     * Get a list of step-parent families.
970a25f0a04SGreg Roach     *
97154c7f8dfSGreg Roach     * @return Collection
972a25f0a04SGreg Roach     */
973820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
974c1010edaSGreg Roach    {
97513abd6f3SGreg Roach        $step_families = [];
97639ca88baSGreg Roach        $families      = $this->spouseFamilies();
977820b62dfSGreg Roach
978a25f0a04SGreg Roach        foreach ($families as $family) {
97939ca88baSGreg Roach            $spouse = $family->spouse($this);
980820b62dfSGreg Roach
981a25f0a04SGreg Roach            if ($spouse) {
98239ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
98339ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
984a25f0a04SGreg Roach                        $step_families[] = $step_family;
985a25f0a04SGreg Roach                    }
986a25f0a04SGreg Roach                }
987a25f0a04SGreg Roach            }
988a25f0a04SGreg Roach        }
989a25f0a04SGreg Roach
990820b62dfSGreg Roach        return new Collection($step_families);
991a25f0a04SGreg Roach    }
992a25f0a04SGreg Roach
993a25f0a04SGreg Roach    /**
994a25f0a04SGreg Roach     * A label for a parental family group
995a25f0a04SGreg Roach     *
996a25f0a04SGreg Roach     * @param Family $family
997a25f0a04SGreg Roach     *
998a25f0a04SGreg Roach     * @return string
999a25f0a04SGreg Roach     */
1000820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
1001c1010edaSGreg Roach    {
10027d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
1003a25f0a04SGreg Roach            // A specified pedigree
1004764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1005b2ce94c6SRico Sonntag        }
1006b2ce94c6SRico Sonntag
1007a25f0a04SGreg Roach        // Default (birth) pedigree
1008764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1009a25f0a04SGreg Roach    }
1010a25f0a04SGreg Roach
1011a25f0a04SGreg Roach    /**
1012a25f0a04SGreg Roach     * Create a label for a step family
1013a25f0a04SGreg Roach     *
1014a25f0a04SGreg Roach     * @param Family $step_family
1015a25f0a04SGreg Roach     *
1016a25f0a04SGreg Roach     * @return string
1017a25f0a04SGreg Roach     */
10188f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1019c1010edaSGreg Roach    {
102039ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
1021a25f0a04SGreg Roach            if ($family !== $step_family) {
1022a25f0a04SGreg Roach                // Must be a step-family
102339ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
102439ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
1025a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1026a25f0a04SGreg Roach                            // One common parent - must be a step family
1027e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
1028a25f0a04SGreg Roach                                // Father’s family with someone else
102939ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
1030a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
103139ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
1032b2ce94c6SRico Sonntag                                }
1033b2ce94c6SRico Sonntag
1034a25f0a04SGreg Roach                                /* I18N: A step-family. */
1035bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1036a25f0a04SGreg Roach                            }
1037b2ce94c6SRico Sonntag
1038a25f0a04SGreg Roach                            // Mother’s family with someone else
103939ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
1040a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
104139ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
1042b2ce94c6SRico Sonntag                            }
1043b2ce94c6SRico Sonntag
1044a25f0a04SGreg Roach                            /* I18N: A step-family. */
1045bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1046a25f0a04SGreg Roach                        }
1047a25f0a04SGreg Roach                    }
1048a25f0a04SGreg Roach                }
1049a25f0a04SGreg Roach            }
1050a25f0a04SGreg Roach        }
1051a25f0a04SGreg Roach
1052a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1053a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1054a25f0a04SGreg Roach    }
1055a25f0a04SGreg Roach
1056225e381fSGreg Roach    /**
1057225e381fSGreg Roach     * Get the description for the family.
1058225e381fSGreg Roach     *
1059225e381fSGreg Roach     * For example, "XXX's family with new wife".
1060225e381fSGreg Roach     *
1061225e381fSGreg Roach     * @param Family $family
1062225e381fSGreg Roach     *
1063225e381fSGreg Roach     * @return string
1064225e381fSGreg Roach     */
1065e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
1066c1010edaSGreg Roach    {
106739ca88baSGreg Roach        $spouse = $family->spouse($this);
1068225e381fSGreg Roach        if ($spouse) {
1069225e381fSGreg Roach            /* I18N: %s is the spouse name */
107039ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1071225e381fSGreg Roach        }
1072b2ce94c6SRico Sonntag
107339ca88baSGreg Roach        return $family->fullName();
1074225e381fSGreg Roach    }
1075225e381fSGreg Roach
1076a25f0a04SGreg Roach    /**
1077a25f0a04SGreg Roach     * get primary parents names for this individual
1078a25f0a04SGreg Roach     *
1079a25f0a04SGreg Roach     * @param string $classname optional css class
1080a25f0a04SGreg Roach     * @param string $display   optional css style display
1081a25f0a04SGreg Roach     *
1082a25f0a04SGreg Roach     * @return string a div block with father & mother names
1083a25f0a04SGreg Roach     */
10848f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1085c1010edaSGreg Roach    {
108639ca88baSGreg Roach        $fam = $this->primaryChildFamily();
1087a25f0a04SGreg Roach        if (!$fam) {
1088a25f0a04SGreg Roach            return '';
1089a25f0a04SGreg Roach        }
1090a25f0a04SGreg Roach        $txt = '<div';
1091a25f0a04SGreg Roach        if ($classname) {
10924c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1093a25f0a04SGreg Roach        }
1094a25f0a04SGreg Roach        if ($display) {
10954c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1096a25f0a04SGreg Roach        }
1097a25f0a04SGreg Roach        $txt .= '>';
109839ca88baSGreg Roach        $husb = $fam->husband();
1099a25f0a04SGreg Roach        if ($husb) {
1100a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1101a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1102a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1103a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1104a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
110539ca88baSGreg Roach            $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>';
1106a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1107a25f0a04SGreg Roach        }
110839ca88baSGreg Roach        $wife = $fam->wife();
1109a25f0a04SGreg Roach        if ($wife) {
1110a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1111a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1112a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1113a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1114a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
111539ca88baSGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->fullName());
1116a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1117a25f0a04SGreg Roach        }
1118a25f0a04SGreg Roach        $txt .= '</div>';
1119a25f0a04SGreg Roach
1120a25f0a04SGreg Roach        return $txt;
1121a25f0a04SGreg Roach    }
1122a25f0a04SGreg Roach
1123961ec755SGreg Roach    /**
1124961ec755SGreg Roach     * If this object has no name, what do we call it?
1125961ec755SGreg Roach     *
1126961ec755SGreg Roach     * @return string
1127961ec755SGreg Roach     */
11288f53f488SRico Sonntag    public function getFallBackName(): string
1129c1010edaSGreg Roach    {
1130a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1131a25f0a04SGreg Roach    }
1132a25f0a04SGreg Roach
1133a25f0a04SGreg Roach    /**
1134a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1135a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1136a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1137a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1138a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1139a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1140a25f0a04SGreg Roach     *
1141a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1142a25f0a04SGreg Roach     * 2 GIVN Robert
1143a25f0a04SGreg Roach     * 2 SPFX de
1144a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1145a25f0a04SGreg Roach     * 2 NICK The Bald
1146a25f0a04SGreg Roach     *
1147a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1148a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1149a25f0a04SGreg Roach     *
1150a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1151a25f0a04SGreg Roach     *
1152a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1153a25f0a04SGreg Roach     * or
1154a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1155a25f0a04SGreg Roach     * 2 GIVN Carlos
1156a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1157a25f0a04SGreg Roach     *
1158a25f0a04SGreg Roach     * @param string $type
1159a25f0a04SGreg Roach     * @param string $full
1160a25f0a04SGreg Roach     * @param string $gedcom
1161e364afe4SGreg Roach     *
1162e364afe4SGreg Roach     * @return void
1163a25f0a04SGreg Roach     */
1164e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1165c1010edaSGreg Roach    {
1166a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1167a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1168a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1169a25f0a04SGreg Roach
117076f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1171a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1172a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1173a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1174a25f0a04SGreg Roach
1175a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
117676f666f4SGreg Roach        if ($SURN !== '') {
1177a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1178a25f0a04SGreg Roach        } else {
117913abd6f3SGreg Roach            $SURNS = [];
1180a25f0a04SGreg Roach        }
118176f666f4SGreg Roach
1182a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1183a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1184a25f0a04SGreg Roach
1185a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1186a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1187a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1188a25f0a04SGreg Roach
1189a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
119076f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1191e364afe4SGreg Roach            $full .= '/';
1192a25f0a04SGreg Roach        }
1193a25f0a04SGreg Roach
1194a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1195a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1196a25f0a04SGreg Roach
1197a25f0a04SGreg Roach        // Extract the surname.
1198a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1199a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1200a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1201a25f0a04SGreg Roach        } else {
1202a25f0a04SGreg Roach            $surname = '';
1203a25f0a04SGreg Roach        }
1204a25f0a04SGreg Roach
1205a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1206a25f0a04SGreg Roach        if (!$SURNS) {
1207a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1208a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1209a25f0a04SGreg Roach                $SURNS = $matches[1];
1210a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1211a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1212a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1213a25f0a04SGreg Roach                }
1214a25f0a04SGreg Roach            } else {
1215a25f0a04SGreg Roach                // It is valid not to have a surname at all
121613abd6f3SGreg Roach                $SURNS = [''];
1217a25f0a04SGreg Roach            }
1218a25f0a04SGreg Roach        }
1219a25f0a04SGreg Roach
1220a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1221a25f0a04SGreg Roach        if (!$GIVN) {
1222a25f0a04SGreg Roach            $GIVN = preg_replace(
122313abd6f3SGreg Roach                [
1224c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1225c1010edaSGreg Roach                    // remove surname
1226c1010edaSGreg Roach                    '/ ?".+"/',
1227c1010edaSGreg Roach                    // remove nickname
1228c1010edaSGreg Roach                    '/ {2,}/',
1229c1010edaSGreg Roach                    // multiple spaces, caused by the above
1230c1010edaSGreg Roach                    '/^ | $/',
1231c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
123213abd6f3SGreg Roach                ],
123313abd6f3SGreg Roach                [
1234a25f0a04SGreg Roach                    ' ',
1235a25f0a04SGreg Roach                    ' ',
1236a25f0a04SGreg Roach                    ' ',
1237a25f0a04SGreg Roach                    '',
123813abd6f3SGreg Roach                ],
1239a25f0a04SGreg Roach                $full
1240a25f0a04SGreg Roach            );
1241a25f0a04SGreg Roach        }
1242a25f0a04SGreg Roach
1243a25f0a04SGreg Roach        // Add placeholder for unknown given name
1244a25f0a04SGreg Roach        if (!$GIVN) {
1245a25f0a04SGreg Roach            $GIVN = '@P.N.';
124673f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1247a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1248a25f0a04SGreg Roach        }
1249a25f0a04SGreg Roach
12507b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12517b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1252c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12537b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1254c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1255a25f0a04SGreg Roach        }
1256a25f0a04SGreg Roach
1257a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1258a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1259a25f0a04SGreg Roach        // $full is for display on-screen
1260a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1261a25f0a04SGreg Roach
1262a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1263ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1264ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1265c6f196c3SGreg Roach        // Format for display
1266d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1267acc34ea1SGreg Roach        // Localise quotation marks around the nickname
12680b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
12698d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12708d68cabeSGreg Roach        }, $full);
1271a25f0a04SGreg Roach
1272c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1273a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1274a25f0a04SGreg Roach
1275a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1276a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1277a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1278a25f0a04SGreg Roach
1279ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1280a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1281e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1282a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1283e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1284a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1285a25f0a04SGreg Roach            }
1286a25f0a04SGreg Roach
1287bdb3725aSGreg Roach            $this->getAllNames[] = [
1288a25f0a04SGreg Roach                'type'    => $type,
1289a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1290c1010edaSGreg Roach                'full'    => $full,
1291c1010edaSGreg Roach                // This is used for display
1292c1010edaSGreg Roach                'fullNN'  => $fullNN,
1293c1010edaSGreg Roach                // This goes into the database
1294c1010edaSGreg Roach                'surname' => $surname,
1295c1010edaSGreg Roach                // This goes into the database
1296c1010edaSGreg Roach                'givn'    => $GIVN,
1297c1010edaSGreg Roach                // This goes into the database
1298c1010edaSGreg Roach                'surn'    => $SURN,
1299c1010edaSGreg Roach                // This goes into the database
130013abd6f3SGreg Roach            ];
1301a25f0a04SGreg Roach        }
1302a25f0a04SGreg Roach    }
1303a25f0a04SGreg Roach
1304a25f0a04SGreg Roach    /**
130576692c8bSGreg Roach     * Extract names from the GEDCOM record.
1306c7ff4153SGreg Roach     *
1307c7ff4153SGreg Roach     * @return void
1308a25f0a04SGreg Roach     */
1309e364afe4SGreg Roach    public function extractNames(): void
1310c1010edaSGreg Roach    {
13118f53f488SRico Sonntag        $this->extractNamesFromFacts(
13128f53f488SRico Sonntag            1,
13138f53f488SRico Sonntag            'NAME',
131430158ae7SGreg Roach            $this->facts(
13158d0ebef0SGreg Roach                ['NAME'],
13168f53f488SRico Sonntag                false,
13178f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13188f53f488SRico Sonntag                $this->canShowName()
13198f53f488SRico Sonntag            )
13208f53f488SRico Sonntag        );
1321a25f0a04SGreg Roach    }
1322a25f0a04SGreg Roach
1323a25f0a04SGreg Roach    /**
1324a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1325a25f0a04SGreg Roach     * selection items or favorites.
1326a25f0a04SGreg Roach     *
1327a25f0a04SGreg Roach     * @return string
1328a25f0a04SGreg Roach     */
13298f53f488SRico Sonntag    public function formatListDetails(): string
1330c1010edaSGreg Roach    {
1331a25f0a04SGreg Roach        return
13328d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13338d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1334a25f0a04SGreg Roach    }
1335a25f0a04SGreg Roach}
1336