xref: /webtrees/app/Individual.php (revision 1afbbc50a65bb73de1d13dc18d669efe80119058)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
236ccdf4f0SGreg Roachuse Exception;
24a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
26852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
272e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2839ca88baSGreg Roachuse Illuminate\Support\Collection;
29886b77daSGreg Roachuse stdClass;
30a25f0a04SGreg Roach
31a25f0a04SGreg Roach/**
3276692c8bSGreg Roach * A GEDCOM individual (INDI) object.
33a25f0a04SGreg Roach */
34c1010edaSGreg Roachclass Individual extends GedcomRecord
35c1010edaSGreg Roach{
3616d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3716d6367aSGreg Roach
38852ede8cSGreg Roach    protected const ROUTE_NAME = IndividualPage::class;
39a25f0a04SGreg Roach
4076692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
4176692c8bSGreg Roach    public $generation;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
444686330aSGreg Roach    private $estimated_birth_date;
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach    /** @var Date The estimated date of death */
474686330aSGreg Roach    private $estimated_death_date;
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach    /**
50886b77daSGreg Roach     * A closure which will create a record from a database row.
51886b77daSGreg Roach     *
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)
1947c4add84SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_PATH_LENGTH);
1957c4add84SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF);
1967c4add84SGreg Roach
19754ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1984b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
199a25f0a04SGreg Roach        }
200a25f0a04SGreg Roach
201a25f0a04SGreg Roach        // No restriction found - show living people to members only:
2024b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
203a25f0a04SGreg Roach    }
204a25f0a04SGreg Roach
205a25f0a04SGreg Roach    /**
206a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
207a25f0a04SGreg Roach     *
208a25f0a04SGreg Roach     * @param Individual $target
209cbc1590aSGreg Roach     * @param int        $distance
210a25f0a04SGreg Roach     *
211cbc1590aSGreg Roach     * @return bool
212a25f0a04SGreg Roach     */
2138f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
214c1010edaSGreg Roach    {
215a25f0a04SGreg Roach        static $cache = null;
216a25f0a04SGreg Roach
2177c4add84SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $target->tree);
218a25f0a04SGreg Roach        if ($user_individual) {
219a25f0a04SGreg Roach            if (!$cache) {
22013abd6f3SGreg Roach                $cache = [
22113abd6f3SGreg Roach                    0 => [$user_individual],
22213abd6f3SGreg Roach                    1 => [],
22313abd6f3SGreg Roach                ];
2248d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
225dc124885SGreg Roach                    $family = $fact->target();
226e24444eeSGreg Roach                    if ($family instanceof Family) {
227a25f0a04SGreg Roach                        $cache[1][] = $family;
228a25f0a04SGreg Roach                    }
229a25f0a04SGreg Roach                }
230a25f0a04SGreg Roach            }
231a25f0a04SGreg Roach        } else {
232a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
233a25f0a04SGreg Roach            return true;
234a25f0a04SGreg Roach        }
235a25f0a04SGreg Roach
236a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
237a25f0a04SGreg Roach        $distance *= 2;
238a25f0a04SGreg Roach
239a25f0a04SGreg Roach        // Consider each path length in turn
240a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
241a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
242a25f0a04SGreg Roach                // We have already calculated all records with this length
243e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
244a25f0a04SGreg Roach                    return true;
245a25f0a04SGreg Roach                }
246a25f0a04SGreg Roach            } else {
247a25f0a04SGreg Roach                // Need to calculate these paths
24813abd6f3SGreg Roach                $cache[$n] = [];
249e364afe4SGreg Roach                if ($n % 2 === 0) {
250a25f0a04SGreg Roach                    // Add FAM->INDI links
251a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2528d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
253dc124885SGreg Roach                            $individual = $fact->target();
254a25f0a04SGreg Roach                            // Don’t backtrack
255e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
256a25f0a04SGreg Roach                                $cache[$n][] = $individual;
257a25f0a04SGreg Roach                            }
258a25f0a04SGreg Roach                        }
259a25f0a04SGreg Roach                    }
260a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
261a25f0a04SGreg Roach                        return true;
262a25f0a04SGreg Roach                    }
263a25f0a04SGreg Roach                } else {
264a25f0a04SGreg Roach                    // Add INDI->FAM links
265a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2668d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
267dc124885SGreg Roach                            $family = $fact->target();
268a25f0a04SGreg Roach                            // Don’t backtrack
269e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
270a25f0a04SGreg Roach                                $cache[$n][] = $family;
271a25f0a04SGreg Roach                            }
272a25f0a04SGreg Roach                        }
273a25f0a04SGreg Roach                    }
274a25f0a04SGreg Roach                }
275a25f0a04SGreg Roach            }
276a25f0a04SGreg Roach        }
277a25f0a04SGreg Roach
278a25f0a04SGreg Roach        return false;
279a25f0a04SGreg Roach    }
280a25f0a04SGreg Roach
28176692c8bSGreg Roach    /**
28276692c8bSGreg Roach     * Generate a private version of this record
28376692c8bSGreg Roach     *
28476692c8bSGreg Roach     * @param int $access_level
28576692c8bSGreg Roach     *
28676692c8bSGreg Roach     * @return string
28776692c8bSGreg Roach     */
2883c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
289c1010edaSGreg Roach    {
290acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
291a25f0a04SGreg Roach
292a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
293518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
294a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2958d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
296138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
297a25f0a04SGreg Roach            }
298a25f0a04SGreg Roach        }
299a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
3008d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
301a25f0a04SGreg Roach        foreach ($matches as $match) {
30224ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
303a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
304a25f0a04SGreg Roach                $rec .= $match[0];
305a25f0a04SGreg Roach            }
306a25f0a04SGreg Roach        }
307a25f0a04SGreg Roach        // Don’t privatize sex.
308a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
309a25f0a04SGreg Roach            $rec .= $match[0];
310a25f0a04SGreg Roach        }
311a25f0a04SGreg Roach
312a25f0a04SGreg Roach        return $rec;
313a25f0a04SGreg Roach    }
314a25f0a04SGreg Roach
31576692c8bSGreg Roach    /**
31676692c8bSGreg Roach     * Fetch data from the database
31776692c8bSGreg Roach     *
31876692c8bSGreg Roach     * @param string $xref
31976692c8bSGreg Roach     * @param int    $tree_id
32076692c8bSGreg Roach     *
321e364afe4SGreg Roach     * @return string|null
32276692c8bSGreg Roach     */
323e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
324c1010edaSGreg Roach    {
3252e5b4452SGreg Roach        return DB::table('individuals')
3262e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3272e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3282e5b4452SGreg Roach            ->value('i_gedcom');
329a25f0a04SGreg Roach    }
330a25f0a04SGreg Roach
331a25f0a04SGreg Roach    /**
332a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
333a25f0a04SGreg Roach     * If not known to be dead, then assume living.
334a25f0a04SGreg Roach     *
335cbc1590aSGreg Roach     * @return bool
336a25f0a04SGreg Roach     */
3378f53f488SRico Sonntag    public function isDead(): bool
338c1010edaSGreg Roach    {
339c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3404459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
341a25f0a04SGreg Roach
342a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3438d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
344a25f0a04SGreg Roach            return true;
345a25f0a04SGreg Roach        }
346a25f0a04SGreg Roach
347a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
348a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
349a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
350a25f0a04SGreg Roach                $date = new Date($date_match);
351269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
352a25f0a04SGreg Roach                    return true;
353a25f0a04SGreg Roach                }
354a25f0a04SGreg Roach            }
355a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
356a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
357a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
358a25f0a04SGreg Roach                return false;
359a25f0a04SGreg Roach            }
360a25f0a04SGreg Roach        }
361a25f0a04SGreg Roach
362a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
363a25f0a04SGreg Roach
364a25f0a04SGreg Roach        // Check parents (birth and adopted)
36539ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
36639ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
367a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
368a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
369a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
370a25f0a04SGreg Roach                    $date = new Date($date_match);
371269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
372a25f0a04SGreg Roach                        return true;
373a25f0a04SGreg Roach                    }
374a25f0a04SGreg Roach                }
375a25f0a04SGreg Roach            }
376a25f0a04SGreg Roach        }
377a25f0a04SGreg Roach
378a25f0a04SGreg Roach        // Check spouses
37939ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
380a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
381a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
382a25f0a04SGreg Roach                $date = new Date($date_match);
383a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
384269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
385a25f0a04SGreg Roach                    return true;
386a25f0a04SGreg Roach                }
387a25f0a04SGreg Roach            }
388a25f0a04SGreg Roach            // Check spouse dates
38939ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
390a25f0a04SGreg Roach            if ($spouse) {
391a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
392a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
393a25f0a04SGreg Roach                    $date = new Date($date_match);
394a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
395269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
396a25f0a04SGreg Roach                        return true;
397a25f0a04SGreg Roach                    }
398a25f0a04SGreg Roach                }
399a25f0a04SGreg Roach            }
400a25f0a04SGreg Roach            // Check child dates
40139ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
402a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
403a25f0a04SGreg Roach                // Assume children born after age of 15
404a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
405a25f0a04SGreg Roach                    $date = new Date($date_match);
406269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
407a25f0a04SGreg Roach                        return true;
408a25f0a04SGreg Roach                    }
409a25f0a04SGreg Roach                }
410a25f0a04SGreg Roach                // Check grandchildren
41139ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
41239ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
413a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
414a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
415a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
416a25f0a04SGreg Roach                            $date = new Date($date_match);
417269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
418a25f0a04SGreg Roach                                return true;
419a25f0a04SGreg Roach                            }
420a25f0a04SGreg Roach                        }
421a25f0a04SGreg Roach                    }
422a25f0a04SGreg Roach                }
423a25f0a04SGreg Roach            }
424a25f0a04SGreg Roach        }
425a25f0a04SGreg Roach
426a25f0a04SGreg Roach        return false;
427a25f0a04SGreg Roach    }
428a25f0a04SGreg Roach
429a25f0a04SGreg Roach    /**
430a25f0a04SGreg Roach     * Find the highlighted media object for an individual
431a25f0a04SGreg Roach     *
432e364afe4SGreg Roach     * @return MediaFile|null
433a25f0a04SGreg Roach     */
434e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
435c1010edaSGreg Roach    {
4368d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
437dc124885SGreg Roach            $media = $fact->target();
438a213a63cSGreg Roach            if ($media instanceof Media) {
4394a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
440a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4414a9f750fSGreg Roach                        return $media_file;
4424a9f750fSGreg Roach                    }
4434a9f750fSGreg Roach                }
444a25f0a04SGreg Roach            }
445a25f0a04SGreg Roach        }
446a25f0a04SGreg Roach
447a25f0a04SGreg Roach        return null;
448a25f0a04SGreg Roach    }
449a25f0a04SGreg Roach
450a25f0a04SGreg Roach    /**
451a25f0a04SGreg Roach     * Display the prefered image for this individual.
452a25f0a04SGreg Roach     * Use an icon if no image is available.
453a25f0a04SGreg Roach     *
454dce07401SGreg Roach     * @param int      $width      Pixels
455dce07401SGreg Roach     * @param int      $height     Pixels
456dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
457dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
458dce07401SGreg Roach     *
459a25f0a04SGreg Roach     * @return string
460a25f0a04SGreg Roach     */
4618f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
462c1010edaSGreg Roach    {
4634a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4644a9f750fSGreg Roach
4654a9f750fSGreg Roach        if ($media_file !== null) {
4664a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
467a25f0a04SGreg Roach        }
4684a9f750fSGreg Roach
4694a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
47039ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4714a9f750fSGreg Roach        }
4724a9f750fSGreg Roach
4734a9f750fSGreg Roach        return '';
474a25f0a04SGreg Roach    }
475a25f0a04SGreg Roach
476a25f0a04SGreg Roach    /**
477a25f0a04SGreg Roach     * Get the date of birth
478a25f0a04SGreg Roach     *
479a25f0a04SGreg Roach     * @return Date
480a25f0a04SGreg Roach     */
4818f53f488SRico Sonntag    public function getBirthDate(): Date
482c1010edaSGreg Roach    {
483a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
484a25f0a04SGreg Roach            if ($date->isOK()) {
485a25f0a04SGreg Roach                return $date;
486a25f0a04SGreg Roach            }
487a25f0a04SGreg Roach        }
488a25f0a04SGreg Roach
489a25f0a04SGreg Roach        return new Date('');
490a25f0a04SGreg Roach    }
491a25f0a04SGreg Roach
492a25f0a04SGreg Roach    /**
493a25f0a04SGreg Roach     * Get the place of birth
494a25f0a04SGreg Roach     *
49516d0b7f7SRico Sonntag     * @return Place
496a25f0a04SGreg Roach     */
4978f53f488SRico Sonntag    public function getBirthPlace(): Place
498c1010edaSGreg Roach    {
499a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
500a25f0a04SGreg Roach            return $place;
501a25f0a04SGreg Roach        }
502a25f0a04SGreg Roach
503b20ddbf9SGreg Roach        return new Place('', $this->tree);
504a25f0a04SGreg Roach    }
505a25f0a04SGreg Roach
506a25f0a04SGreg Roach    /**
507a25f0a04SGreg Roach     * Get the year of birth
508a25f0a04SGreg Roach     *
509a25f0a04SGreg Roach     * @return string the year of birth
510a25f0a04SGreg Roach     */
5118f53f488SRico Sonntag    public function getBirthYear(): string
512c1010edaSGreg Roach    {
513f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
514a25f0a04SGreg Roach    }
515a25f0a04SGreg Roach
516a25f0a04SGreg Roach    /**
517a25f0a04SGreg Roach     * Get the date of death
518a25f0a04SGreg Roach     *
519a25f0a04SGreg Roach     * @return Date
520a25f0a04SGreg Roach     */
5218f53f488SRico Sonntag    public function getDeathDate(): Date
522c1010edaSGreg Roach    {
523a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
524a25f0a04SGreg Roach            if ($date->isOK()) {
525a25f0a04SGreg Roach                return $date;
526a25f0a04SGreg Roach            }
527a25f0a04SGreg Roach        }
528a25f0a04SGreg Roach
529a25f0a04SGreg Roach        return new Date('');
530a25f0a04SGreg Roach    }
531a25f0a04SGreg Roach
532a25f0a04SGreg Roach    /**
533a25f0a04SGreg Roach     * Get the place of death
534a25f0a04SGreg Roach     *
53516d0b7f7SRico Sonntag     * @return Place
536a25f0a04SGreg Roach     */
5378f53f488SRico Sonntag    public function getDeathPlace(): Place
538c1010edaSGreg Roach    {
539a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
540a25f0a04SGreg Roach            return $place;
541a25f0a04SGreg Roach        }
542a25f0a04SGreg Roach
543b20ddbf9SGreg Roach        return new Place('', $this->tree);
544a25f0a04SGreg Roach    }
545a25f0a04SGreg Roach
546a25f0a04SGreg Roach    /**
547a25f0a04SGreg Roach     * get the death year
548a25f0a04SGreg Roach     *
549a25f0a04SGreg Roach     * @return string the year of death
550a25f0a04SGreg Roach     */
5518f53f488SRico Sonntag    public function getDeathYear(): string
552c1010edaSGreg Roach    {
553f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
554a25f0a04SGreg Roach    }
555a25f0a04SGreg Roach
556a25f0a04SGreg Roach    /**
557a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
55815d603e7SGreg Roach     * Provide the place and full date using a tooltip.
559a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
560a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
561a25f0a04SGreg Roach     *
562a25f0a04SGreg Roach     * @return string
563a25f0a04SGreg Roach     */
5648f53f488SRico Sonntag    public function getLifeSpan(): string
565c1010edaSGreg Roach    {
56615d603e7SGreg Roach        // Just the first part of the place name
567392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
568392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
56915d603e7SGreg Roach        // Remove markup from dates
57015d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
57115d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
57215d603e7SGreg Roach
573c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
574dacfe33cSGreg Roach        return I18N::translate(
575a25f0a04SGreg Roach            '%1$s–%2$s',
5762d4c1bedSGreg Roach            '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5772d4c1bedSGreg Roach            '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
578a25f0a04SGreg Roach        );
579a25f0a04SGreg Roach    }
580a25f0a04SGreg Roach
581a25f0a04SGreg Roach    /**
582a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
583a25f0a04SGreg Roach     *
584a25f0a04SGreg Roach     * @return Date[]
585a25f0a04SGreg Roach     */
5868f53f488SRico Sonntag    public function getAllBirthDates(): array
587c1010edaSGreg Roach    {
5888d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5898d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
590a25f0a04SGreg Roach            if ($tmp) {
591a25f0a04SGreg Roach                return $tmp;
592a25f0a04SGreg Roach            }
593a25f0a04SGreg Roach        }
594a25f0a04SGreg Roach
59513abd6f3SGreg Roach        return [];
596a25f0a04SGreg Roach    }
597a25f0a04SGreg Roach
598a25f0a04SGreg Roach    /**
599a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
600a25f0a04SGreg Roach     *
6014080d558SGreg Roach     * @return Place[]
602a25f0a04SGreg Roach     */
6038f53f488SRico Sonntag    public function getAllBirthPlaces(): array
604c1010edaSGreg Roach    {
6058d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
6068d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
60754c1ab5eSGreg Roach            if ($places !== []) {
6084080d558SGreg Roach                return $places;
609a25f0a04SGreg Roach            }
610a25f0a04SGreg Roach        }
611a25f0a04SGreg Roach
61213abd6f3SGreg Roach        return [];
613a25f0a04SGreg Roach    }
614a25f0a04SGreg Roach
615a25f0a04SGreg Roach    /**
616a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
617a25f0a04SGreg Roach     *
618a25f0a04SGreg Roach     * @return Date[]
619a25f0a04SGreg Roach     */
6208f53f488SRico Sonntag    public function getAllDeathDates(): array
621c1010edaSGreg Roach    {
6228d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6238d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
624a25f0a04SGreg Roach            if ($tmp) {
625a25f0a04SGreg Roach                return $tmp;
626a25f0a04SGreg Roach            }
627a25f0a04SGreg Roach        }
628a25f0a04SGreg Roach
62913abd6f3SGreg Roach        return [];
630a25f0a04SGreg Roach    }
631a25f0a04SGreg Roach
632a25f0a04SGreg Roach    /**
633a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
634a25f0a04SGreg Roach     *
6354080d558SGreg Roach     * @return Place[]
636a25f0a04SGreg Roach     */
6378f53f488SRico Sonntag    public function getAllDeathPlaces(): array
638c1010edaSGreg Roach    {
6398d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6408d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
64154c1ab5eSGreg Roach            if ($places !== []) {
6424080d558SGreg Roach                return $places;
643a25f0a04SGreg Roach            }
644a25f0a04SGreg Roach        }
645a25f0a04SGreg Roach
64613abd6f3SGreg Roach        return [];
647a25f0a04SGreg Roach    }
648a25f0a04SGreg Roach
649a25f0a04SGreg Roach    /**
650a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
651a25f0a04SGreg Roach     *
652a25f0a04SGreg Roach     * @return Date
653a25f0a04SGreg Roach     */
6548f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
655c1010edaSGreg Roach    {
6568f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
657a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
658a25f0a04SGreg Roach                if ($date->isOK()) {
6594686330aSGreg Roach                    $this->estimated_birth_date = $date;
660a25f0a04SGreg Roach                    break;
661a25f0a04SGreg Roach                }
662a25f0a04SGreg Roach            }
6638f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
66413abd6f3SGreg Roach                $min = [];
66513abd6f3SGreg Roach                $max = [];
666a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
667f5b60decSGreg Roach                if ($tmp->isOK()) {
668f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
669f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
670a25f0a04SGreg Roach                }
67139ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
672a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
673f5b60decSGreg Roach                    if ($tmp->isOK()) {
674f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
675f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
676a25f0a04SGreg Roach                    }
67739ca88baSGreg Roach                    $husband = $family->husband();
678e364afe4SGreg Roach                    if ($husband instanceof self) {
6792e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
680f5b60decSGreg Roach                        if ($tmp->isOK()) {
681f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
682f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
683a25f0a04SGreg Roach                        }
684a25f0a04SGreg Roach                    }
68539ca88baSGreg Roach                    $wife = $family->wife();
686e364afe4SGreg Roach                    if ($wife instanceof self) {
6872e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
688f5b60decSGreg Roach                        if ($tmp->isOK()) {
689f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
690f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
691a25f0a04SGreg Roach                        }
692a25f0a04SGreg Roach                    }
69339ca88baSGreg Roach                    foreach ($family->children() as $child) {
694a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
695f5b60decSGreg Roach                        if ($tmp->isOK()) {
696f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
697f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
698a25f0a04SGreg Roach                        }
699a25f0a04SGreg Roach                    }
700a25f0a04SGreg Roach                }
70139ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
702a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
703f5b60decSGreg Roach                    if ($tmp->isOK()) {
704f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
705f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
706a25f0a04SGreg Roach                    }
70739ca88baSGreg Roach                    $spouse = $family->spouse($this);
708a25f0a04SGreg Roach                    if ($spouse) {
709a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
710f5b60decSGreg Roach                        if ($tmp->isOK()) {
711f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
712f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
713a25f0a04SGreg Roach                        }
714a25f0a04SGreg Roach                    }
71539ca88baSGreg Roach                    foreach ($family->children() as $child) {
716a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
717f5b60decSGreg Roach                        if ($tmp->isOK()) {
718e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
719f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
720a25f0a04SGreg Roach                        }
721a25f0a04SGreg Roach                    }
722a25f0a04SGreg Roach                }
723a25f0a04SGreg Roach                if ($min && $max) {
72459f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
725a25f0a04SGreg Roach
72665e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7274686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
728a25f0a04SGreg Roach                } else {
7294686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
730a25f0a04SGreg Roach                }
731a25f0a04SGreg Roach            }
732a25f0a04SGreg Roach        }
733a25f0a04SGreg Roach
7344686330aSGreg Roach        return $this->estimated_birth_date;
735a25f0a04SGreg Roach    }
736a25f0a04SGreg Roach
737a25f0a04SGreg Roach    /**
738a25f0a04SGreg Roach     * Generate an estimated date of death.
739a25f0a04SGreg Roach     *
740a25f0a04SGreg Roach     * @return Date
741a25f0a04SGreg Roach     */
7428f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
743c1010edaSGreg Roach    {
7444686330aSGreg Roach        if ($this->estimated_death_date === null) {
745a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
746a25f0a04SGreg Roach                if ($date->isOK()) {
7474686330aSGreg Roach                    $this->estimated_death_date = $date;
748a25f0a04SGreg Roach                    break;
749a25f0a04SGreg Roach                }
750a25f0a04SGreg Roach            }
7514686330aSGreg Roach            if ($this->estimated_death_date === null) {
752f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
753c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7544686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
755a25f0a04SGreg Roach                } else {
7564686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
757a25f0a04SGreg Roach                }
758a25f0a04SGreg Roach            }
759a25f0a04SGreg Roach        }
760a25f0a04SGreg Roach
7614686330aSGreg Roach        return $this->estimated_death_date;
762a25f0a04SGreg Roach    }
763a25f0a04SGreg Roach
764a25f0a04SGreg Roach    /**
765a25f0a04SGreg Roach     * Get the sex - M F or U
766a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
767a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
768a25f0a04SGreg Roach     *
769a25f0a04SGreg Roach     * @return string
770a25f0a04SGreg Roach     */
771e364afe4SGreg Roach    public function sex(): string
772c1010edaSGreg Roach    {
773a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
774a25f0a04SGreg Roach            return $match[1];
775a25f0a04SGreg Roach        }
776b2ce94c6SRico Sonntag
777b2ce94c6SRico Sonntag        return 'U';
778a25f0a04SGreg Roach    }
779a25f0a04SGreg Roach
780a25f0a04SGreg Roach    /**
781a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
782a25f0a04SGreg Roach     *
783a25f0a04SGreg Roach     * @return string
784a25f0a04SGreg Roach     */
7858f53f488SRico Sonntag    public function getBoxStyle(): string
786c1010edaSGreg Roach    {
787c1010edaSGreg Roach        $tmp = [
788c1010edaSGreg Roach            'M' => '',
789c1010edaSGreg Roach            'F' => 'F',
790c1010edaSGreg Roach            'U' => 'NN',
791c1010edaSGreg Roach        ];
792a25f0a04SGreg Roach
79339ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
794a25f0a04SGreg Roach    }
795a25f0a04SGreg Roach
796a25f0a04SGreg Roach    /**
797a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
798a25f0a04SGreg Roach     *
799cbc1590aSGreg Roach     * @param int|null $access_level
800a25f0a04SGreg Roach     *
80154c7f8dfSGreg Roach     * @return Collection
802a25f0a04SGreg Roach     */
80339ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
804c1010edaSGreg Roach    {
8054b9ff166SGreg Roach        if ($access_level === null) {
8064b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8074b9ff166SGreg Roach        }
8084b9ff166SGreg Roach
809acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
810a25f0a04SGreg Roach
81139ca88baSGreg Roach        $families = new Collection();
8128d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
813dc124885SGreg Roach            $family = $fact->target();
814e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
81539ca88baSGreg Roach                $families->push($family);
816a25f0a04SGreg Roach            }
817a25f0a04SGreg Roach        }
818a25f0a04SGreg Roach
81939ca88baSGreg Roach        return new Collection($families);
820a25f0a04SGreg Roach    }
821a25f0a04SGreg Roach
822a25f0a04SGreg Roach    /**
823a25f0a04SGreg Roach     * Get the current spouse of this individual.
824a25f0a04SGreg Roach     *
825a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
826a25f0a04SGreg Roach     * in chronological order, and take the last one found.
827a25f0a04SGreg Roach     *
828a25f0a04SGreg Roach     * @return Individual|null
829a25f0a04SGreg Roach     */
830e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
831c1010edaSGreg Roach    {
83239ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
83339ca88baSGreg Roach
83439ca88baSGreg Roach        if ($family instanceof Family) {
83539ca88baSGreg Roach            return $family->spouse($this);
836a25f0a04SGreg Roach        }
837b2ce94c6SRico Sonntag
838b2ce94c6SRico Sonntag        return null;
839a25f0a04SGreg Roach    }
840a25f0a04SGreg Roach
841a25f0a04SGreg Roach    /**
842a25f0a04SGreg Roach     * Count the children belonging to this individual.
843a25f0a04SGreg Roach     *
844cbc1590aSGreg Roach     * @return int
845a25f0a04SGreg Roach     */
846e364afe4SGreg Roach    public function numberOfChildren(): int
847c1010edaSGreg Roach    {
8487d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8493dc7bbe9SGreg Roach            return (int) $match[1];
850b2ce94c6SRico Sonntag        }
851b2ce94c6SRico Sonntag
85213abd6f3SGreg Roach        $children = [];
85339ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
85439ca88baSGreg Roach            foreach ($fam->children() as $child) {
855c0935879SGreg Roach                $children[$child->xref()] = true;
856a25f0a04SGreg Roach            }
857a25f0a04SGreg Roach        }
858a25f0a04SGreg Roach
859a25f0a04SGreg Roach        return count($children);
860a25f0a04SGreg Roach    }
861a25f0a04SGreg Roach
862a25f0a04SGreg Roach    /**
863a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
864a25f0a04SGreg Roach     *
865cbc1590aSGreg Roach     * @param int|null $access_level
866a25f0a04SGreg Roach     *
86754c7f8dfSGreg Roach     * @return Collection
868a25f0a04SGreg Roach     */
86939ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
870c1010edaSGreg Roach    {
8714b9ff166SGreg Roach        if ($access_level === null) {
8724b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8734b9ff166SGreg Roach        }
8744b9ff166SGreg Roach
875acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
876a25f0a04SGreg Roach
87739ca88baSGreg Roach        $families = new Collection();
87839ca88baSGreg Roach
8798d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
880dc124885SGreg Roach            $family = $fact->target();
881e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
88239ca88baSGreg Roach                $families->push($family);
883a25f0a04SGreg Roach            }
884a25f0a04SGreg Roach        }
885a25f0a04SGreg Roach
886a25f0a04SGreg Roach        return $families;
887a25f0a04SGreg Roach    }
888a25f0a04SGreg Roach
889a25f0a04SGreg Roach    /**
890a25f0a04SGreg Roach     * Get a list of step-parent families.
891a25f0a04SGreg Roach     *
89254c7f8dfSGreg Roach     * @return Collection
893a25f0a04SGreg Roach     */
894820b62dfSGreg Roach    public function childStepFamilies(): Collection
895c1010edaSGreg Roach    {
89613abd6f3SGreg Roach        $step_families = [];
89739ca88baSGreg Roach        $families      = $this->childFamilies();
898a25f0a04SGreg Roach        foreach ($families as $family) {
89939ca88baSGreg Roach            $father = $family->husband();
900a25f0a04SGreg Roach            if ($father) {
90139ca88baSGreg Roach                foreach ($father->spouseFamilies() as $step_family) {
90239ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
903a25f0a04SGreg Roach                        $step_families[] = $step_family;
904a25f0a04SGreg Roach                    }
905a25f0a04SGreg Roach                }
906a25f0a04SGreg Roach            }
90739ca88baSGreg Roach            $mother = $family->wife();
908a25f0a04SGreg Roach            if ($mother) {
90939ca88baSGreg Roach                foreach ($mother->spouseFamilies() as $step_family) {
91039ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
911a25f0a04SGreg Roach                        $step_families[] = $step_family;
912a25f0a04SGreg Roach                    }
913a25f0a04SGreg Roach                }
914a25f0a04SGreg Roach            }
915a25f0a04SGreg Roach        }
916a25f0a04SGreg Roach
917820b62dfSGreg Roach        return new Collection($step_families);
918a25f0a04SGreg Roach    }
919a25f0a04SGreg Roach
920a25f0a04SGreg Roach    /**
921a25f0a04SGreg Roach     * Get a list of step-parent families.
922a25f0a04SGreg Roach     *
92354c7f8dfSGreg Roach     * @return Collection
924a25f0a04SGreg Roach     */
925820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
926c1010edaSGreg Roach    {
92713abd6f3SGreg Roach        $step_families = [];
92839ca88baSGreg Roach        $families      = $this->spouseFamilies();
929820b62dfSGreg Roach
930a25f0a04SGreg Roach        foreach ($families as $family) {
93139ca88baSGreg Roach            $spouse = $family->spouse($this);
932820b62dfSGreg Roach
933a25f0a04SGreg Roach            if ($spouse) {
93439ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
93539ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
936a25f0a04SGreg Roach                        $step_families[] = $step_family;
937a25f0a04SGreg Roach                    }
938a25f0a04SGreg Roach                }
939a25f0a04SGreg Roach            }
940a25f0a04SGreg Roach        }
941a25f0a04SGreg Roach
942820b62dfSGreg Roach        return new Collection($step_families);
943a25f0a04SGreg Roach    }
944a25f0a04SGreg Roach
945a25f0a04SGreg Roach    /**
946a25f0a04SGreg Roach     * A label for a parental family group
947a25f0a04SGreg Roach     *
948a25f0a04SGreg Roach     * @param Family $family
949a25f0a04SGreg Roach     *
950a25f0a04SGreg Roach     * @return string
951a25f0a04SGreg Roach     */
952820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
953c1010edaSGreg Roach    {
9547d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
955a25f0a04SGreg Roach            // A specified pedigree
956764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
957b2ce94c6SRico Sonntag        }
958b2ce94c6SRico Sonntag
959a25f0a04SGreg Roach        // Default (birth) pedigree
960764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
961a25f0a04SGreg Roach    }
962a25f0a04SGreg Roach
963a25f0a04SGreg Roach    /**
964a25f0a04SGreg Roach     * Create a label for a step family
965a25f0a04SGreg Roach     *
966a25f0a04SGreg Roach     * @param Family $step_family
967a25f0a04SGreg Roach     *
968a25f0a04SGreg Roach     * @return string
969a25f0a04SGreg Roach     */
9708f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
971c1010edaSGreg Roach    {
97239ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
973a25f0a04SGreg Roach            if ($family !== $step_family) {
974a25f0a04SGreg Roach                // Must be a step-family
97539ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
97639ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
977a25f0a04SGreg Roach                        if ($parent === $step_parent) {
978a25f0a04SGreg Roach                            // One common parent - must be a step family
979e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
980a25f0a04SGreg Roach                                // Father’s family with someone else
98139ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
982a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
98339ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
984b2ce94c6SRico Sonntag                                }
985b2ce94c6SRico Sonntag
986a25f0a04SGreg Roach                                /* I18N: A step-family. */
987bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
988a25f0a04SGreg Roach                            }
989b2ce94c6SRico Sonntag
990a25f0a04SGreg Roach                            // Mother’s family with someone else
99139ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
992a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
99339ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
994b2ce94c6SRico Sonntag                            }
995b2ce94c6SRico Sonntag
996a25f0a04SGreg Roach                            /* I18N: A step-family. */
997bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
998a25f0a04SGreg Roach                        }
999a25f0a04SGreg Roach                    }
1000a25f0a04SGreg Roach                }
1001a25f0a04SGreg Roach            }
1002a25f0a04SGreg Roach        }
1003a25f0a04SGreg Roach
1004a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1005a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1006a25f0a04SGreg Roach    }
1007a25f0a04SGreg Roach
1008225e381fSGreg Roach    /**
1009225e381fSGreg Roach     * Get the description for the family.
1010225e381fSGreg Roach     *
1011225e381fSGreg Roach     * For example, "XXX's family with new wife".
1012225e381fSGreg Roach     *
1013225e381fSGreg Roach     * @param Family $family
1014225e381fSGreg Roach     *
1015225e381fSGreg Roach     * @return string
1016225e381fSGreg Roach     */
1017e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
1018c1010edaSGreg Roach    {
101939ca88baSGreg Roach        $spouse = $family->spouse($this);
1020225e381fSGreg Roach        if ($spouse) {
1021225e381fSGreg Roach            /* I18N: %s is the spouse name */
102239ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1023225e381fSGreg Roach        }
1024b2ce94c6SRico Sonntag
102539ca88baSGreg Roach        return $family->fullName();
1026225e381fSGreg Roach    }
1027225e381fSGreg Roach
1028a25f0a04SGreg Roach    /**
1029a25f0a04SGreg Roach     * get primary parents names for this individual
1030a25f0a04SGreg Roach     *
1031a25f0a04SGreg Roach     * @param string $classname optional css class
1032a25f0a04SGreg Roach     * @param string $display   optional css style display
1033a25f0a04SGreg Roach     *
1034a25f0a04SGreg Roach     * @return string a div block with father & mother names
1035a25f0a04SGreg Roach     */
10368f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1037c1010edaSGreg Roach    {
1038*1afbbc50SGreg Roach        $fam = $this->childFamilies()->first();
1039a25f0a04SGreg Roach        if (!$fam) {
1040a25f0a04SGreg Roach            return '';
1041a25f0a04SGreg Roach        }
1042a25f0a04SGreg Roach        $txt = '<div';
1043a25f0a04SGreg Roach        if ($classname) {
10444c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1045a25f0a04SGreg Roach        }
1046a25f0a04SGreg Roach        if ($display) {
10474c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1048a25f0a04SGreg Roach        }
1049a25f0a04SGreg Roach        $txt .= '>';
105039ca88baSGreg Roach        $husb = $fam->husband();
1051a25f0a04SGreg Roach        if ($husb) {
1052a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1053a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1054a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1055a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1056a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
105739ca88baSGreg Roach            $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>';
1058a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1059a25f0a04SGreg Roach        }
106039ca88baSGreg Roach        $wife = $fam->wife();
1061a25f0a04SGreg Roach        if ($wife) {
1062a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1063a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1064a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1065a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1066a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
106739ca88baSGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->fullName());
1068a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1069a25f0a04SGreg Roach        }
1070a25f0a04SGreg Roach        $txt .= '</div>';
1071a25f0a04SGreg Roach
1072a25f0a04SGreg Roach        return $txt;
1073a25f0a04SGreg Roach    }
1074a25f0a04SGreg Roach
1075961ec755SGreg Roach    /**
1076961ec755SGreg Roach     * If this object has no name, what do we call it?
1077961ec755SGreg Roach     *
1078961ec755SGreg Roach     * @return string
1079961ec755SGreg Roach     */
10808f53f488SRico Sonntag    public function getFallBackName(): string
1081c1010edaSGreg Roach    {
1082a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1083a25f0a04SGreg Roach    }
1084a25f0a04SGreg Roach
1085a25f0a04SGreg Roach    /**
1086a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1087a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1088a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1089a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1090a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1091a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1092a25f0a04SGreg Roach     *
1093a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1094a25f0a04SGreg Roach     * 2 GIVN Robert
1095a25f0a04SGreg Roach     * 2 SPFX de
1096a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1097a25f0a04SGreg Roach     * 2 NICK The Bald
1098a25f0a04SGreg Roach     *
1099a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1100a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1101a25f0a04SGreg Roach     *
1102a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1103a25f0a04SGreg Roach     *
1104a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1105a25f0a04SGreg Roach     * or
1106a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1107a25f0a04SGreg Roach     * 2 GIVN Carlos
1108a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1109a25f0a04SGreg Roach     *
1110a25f0a04SGreg Roach     * @param string $type
1111a25f0a04SGreg Roach     * @param string $full
1112a25f0a04SGreg Roach     * @param string $gedcom
1113e364afe4SGreg Roach     *
1114e364afe4SGreg Roach     * @return void
1115a25f0a04SGreg Roach     */
1116e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1117c1010edaSGreg Roach    {
1118a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1119a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1120a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1121a25f0a04SGreg Roach
112276f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1123a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1124a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1125a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1126a25f0a04SGreg Roach
1127a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
112876f666f4SGreg Roach        if ($SURN !== '') {
1129a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1130a25f0a04SGreg Roach        } else {
113113abd6f3SGreg Roach            $SURNS = [];
1132a25f0a04SGreg Roach        }
113376f666f4SGreg Roach
1134a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1135a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1136a25f0a04SGreg Roach
1137a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1138a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1139a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1140a25f0a04SGreg Roach
1141a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
114276f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1143e364afe4SGreg Roach            $full .= '/';
1144a25f0a04SGreg Roach        }
1145a25f0a04SGreg Roach
1146a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1147a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1148a25f0a04SGreg Roach
1149a25f0a04SGreg Roach        // Extract the surname.
1150a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1151a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1152a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1153a25f0a04SGreg Roach        } else {
1154a25f0a04SGreg Roach            $surname = '';
1155a25f0a04SGreg Roach        }
1156a25f0a04SGreg Roach
1157a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1158a25f0a04SGreg Roach        if (!$SURNS) {
1159a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1160a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1161a25f0a04SGreg Roach                $SURNS = $matches[1];
1162a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1163a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1164a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1165a25f0a04SGreg Roach                }
1166a25f0a04SGreg Roach            } else {
1167a25f0a04SGreg Roach                // It is valid not to have a surname at all
116813abd6f3SGreg Roach                $SURNS = [''];
1169a25f0a04SGreg Roach            }
1170a25f0a04SGreg Roach        }
1171a25f0a04SGreg Roach
1172a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1173a25f0a04SGreg Roach        if (!$GIVN) {
1174a25f0a04SGreg Roach            $GIVN = preg_replace(
117513abd6f3SGreg Roach                [
1176c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1177c1010edaSGreg Roach                    // remove surname
1178c1010edaSGreg Roach                    '/ ?".+"/',
1179c1010edaSGreg Roach                    // remove nickname
1180c1010edaSGreg Roach                    '/ {2,}/',
1181c1010edaSGreg Roach                    // multiple spaces, caused by the above
1182c1010edaSGreg Roach                    '/^ | $/',
1183c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
118413abd6f3SGreg Roach                ],
118513abd6f3SGreg Roach                [
1186a25f0a04SGreg Roach                    ' ',
1187a25f0a04SGreg Roach                    ' ',
1188a25f0a04SGreg Roach                    ' ',
1189a25f0a04SGreg Roach                    '',
119013abd6f3SGreg Roach                ],
1191a25f0a04SGreg Roach                $full
1192a25f0a04SGreg Roach            );
1193a25f0a04SGreg Roach        }
1194a25f0a04SGreg Roach
1195a25f0a04SGreg Roach        // Add placeholder for unknown given name
1196a25f0a04SGreg Roach        if (!$GIVN) {
1197a25f0a04SGreg Roach            $GIVN = '@P.N.';
119873f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1199a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1200a25f0a04SGreg Roach        }
1201a25f0a04SGreg Roach
12027b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12037b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1204c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12057b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1206c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1207a25f0a04SGreg Roach        }
1208a25f0a04SGreg Roach
1209a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1210a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1211a25f0a04SGreg Roach        // $full is for display on-screen
1212a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1213a25f0a04SGreg Roach
1214a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1215ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1216ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1217c6f196c3SGreg Roach        // Format for display
1218d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1219acc34ea1SGreg Roach        // Localise quotation marks around the nickname
12200b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
12218d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12228d68cabeSGreg Roach        }, $full);
1223a25f0a04SGreg Roach
1224c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1225a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1226a25f0a04SGreg Roach
1227a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1228a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1229a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1230a25f0a04SGreg Roach
1231ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1232a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1233e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1234a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1235e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1236a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1237a25f0a04SGreg Roach            }
1238a25f0a04SGreg Roach
1239bdb3725aSGreg Roach            $this->getAllNames[] = [
1240a25f0a04SGreg Roach                'type'    => $type,
1241a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1242c1010edaSGreg Roach                'full'    => $full,
1243c1010edaSGreg Roach                // This is used for display
1244c1010edaSGreg Roach                'fullNN'  => $fullNN,
1245c1010edaSGreg Roach                // This goes into the database
1246c1010edaSGreg Roach                'surname' => $surname,
1247c1010edaSGreg Roach                // This goes into the database
1248c1010edaSGreg Roach                'givn'    => $GIVN,
1249c1010edaSGreg Roach                // This goes into the database
1250c1010edaSGreg Roach                'surn'    => $SURN,
1251c1010edaSGreg Roach                // This goes into the database
125213abd6f3SGreg Roach            ];
1253a25f0a04SGreg Roach        }
1254a25f0a04SGreg Roach    }
1255a25f0a04SGreg Roach
1256a25f0a04SGreg Roach    /**
125776692c8bSGreg Roach     * Extract names from the GEDCOM record.
1258c7ff4153SGreg Roach     *
1259c7ff4153SGreg Roach     * @return void
1260a25f0a04SGreg Roach     */
1261e364afe4SGreg Roach    public function extractNames(): void
1262c1010edaSGreg Roach    {
12638f53f488SRico Sonntag        $this->extractNamesFromFacts(
12648f53f488SRico Sonntag            1,
12658f53f488SRico Sonntag            'NAME',
126630158ae7SGreg Roach            $this->facts(
12678d0ebef0SGreg Roach                ['NAME'],
12688f53f488SRico Sonntag                false,
12698f53f488SRico Sonntag                Auth::accessLevel($this->tree),
12708f53f488SRico Sonntag                $this->canShowName()
12718f53f488SRico Sonntag            )
12728f53f488SRico Sonntag        );
1273a25f0a04SGreg Roach    }
1274a25f0a04SGreg Roach
1275a25f0a04SGreg Roach    /**
1276a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1277a25f0a04SGreg Roach     * selection items or favorites.
1278a25f0a04SGreg Roach     *
1279a25f0a04SGreg Roach     * @return string
1280a25f0a04SGreg Roach     */
12818f53f488SRico Sonntag    public function formatListDetails(): string
1282c1010edaSGreg Roach    {
1283a25f0a04SGreg Roach        return
12848d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
12858d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1286a25f0a04SGreg Roach    }
1287a25f0a04SGreg Roach}
1288