xref: /webtrees/app/Individual.php (revision 6c2179e2012ee7bb08bc76647556f957e4eeaa05)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20886b77daSGreg Roachuse Closure;
216ccdf4f0SGreg Roachuse Exception;
22a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
242e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2539ca88baSGreg Roachuse Illuminate\Support\Collection;
26886b77daSGreg Roachuse stdClass;
27a25f0a04SGreg Roach
28a25f0a04SGreg Roach/**
2976692c8bSGreg Roach * A GEDCOM individual (INDI) object.
30a25f0a04SGreg Roach */
31c1010edaSGreg Roachclass Individual extends GedcomRecord
32c1010edaSGreg Roach{
3316d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3416d6367aSGreg Roach
3516d6367aSGreg Roach    protected const ROUTE_NAME = 'individual';
36a25f0a04SGreg Roach
3776692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3876692c8bSGreg Roach    public $generation;
39a25f0a04SGreg Roach
40a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
414686330aSGreg Roach    private $estimated_birth_date;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach    /** @var Date The estimated date of death */
444686330aSGreg Roach    private $estimated_death_date;
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach    /**
47886b77daSGreg Roach     * A closure which will create a record from a database row.
48886b77daSGreg Roach     *
49886b77daSGreg Roach     * @return Closure
50886b77daSGreg Roach     */
51c0804649SGreg Roach    public static function rowMapper(): Closure
52886b77daSGreg Roach    {
53*6c2179e2SGreg Roach        return static function (stdClass $row): Individual {
54a7a24840SGreg Roach            $individual = Individual::getInstance($row->i_id, Tree::findById((int) $row->i_file), $row->i_gedcom);
55e84cf2deSGreg Roach
56e84cf2deSGreg Roach            if ($row->n_num ?? null) {
57e0458bdcSGreg Roach                $individual = clone $individual;
58e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
59e0458bdcSGreg Roach
60e0458bdcSGreg Roach                return $individual;
61e84cf2deSGreg Roach            }
62e84cf2deSGreg Roach
63e84cf2deSGreg Roach            return $individual;
64886b77daSGreg Roach        };
65886b77daSGreg Roach    }
66886b77daSGreg Roach
67886b77daSGreg Roach    /**
68c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
69c156e8f5SGreg Roach     *
70c156e8f5SGreg Roach     * @return Closure
71c156e8f5SGreg Roach     */
72c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
73c156e8f5SGreg Roach    {
74*6c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
75c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
76c156e8f5SGreg Roach        };
77c156e8f5SGreg Roach    }
78c156e8f5SGreg Roach
79c156e8f5SGreg Roach    /**
80c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
81c156e8f5SGreg Roach     *
82c156e8f5SGreg Roach     * @return Closure
83c156e8f5SGreg Roach     */
84c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
85c156e8f5SGreg Roach    {
86*6c2179e2SGreg Roach        return static function (Individual $x, Individual $y): int {
87c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
88c156e8f5SGreg Roach        };
89c156e8f5SGreg Roach    }
90c156e8f5SGreg Roach
91c156e8f5SGreg Roach    /**
92e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
93e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
94e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
95e71ef9d2SGreg Roach     *
96e71ef9d2SGreg Roach     * @param string      $xref
97e71ef9d2SGreg Roach     * @param Tree        $tree
98e71ef9d2SGreg Roach     * @param string|null $gedcom
99e71ef9d2SGreg Roach     *
1006ccdf4f0SGreg Roach     * @throws Exception
101e71ef9d2SGreg Roach     * @return Individual|null
102e71ef9d2SGreg Roach     */
103e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
104c1010edaSGreg Roach    {
105e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
106e71ef9d2SGreg Roach
107e364afe4SGreg Roach        if ($record instanceof self) {
108e71ef9d2SGreg Roach            return $record;
109e71ef9d2SGreg Roach        }
110b2ce94c6SRico Sonntag
111b2ce94c6SRico Sonntag        return null;
112e71ef9d2SGreg Roach    }
113e71ef9d2SGreg Roach
114e71ef9d2SGreg Roach    /**
115395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
116395f0fe0SGreg Roach     * Typically when we load families and their members.
117395f0fe0SGreg Roach     *
118395f0fe0SGreg Roach     * @param Tree     $tree
1194a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
12018d7a90dSGreg Roach     *
12118d7a90dSGreg Roach     * @return void
122395f0fe0SGreg Roach     */
1232e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
124c1010edaSGreg Roach    {
1252e5b4452SGreg Roach        $rows = DB::table('individuals')
1262e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1272e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1282e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1292e5b4452SGreg Roach            ->get();
130395f0fe0SGreg Roach
131395f0fe0SGreg Roach        foreach ($rows as $row) {
132395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
133395f0fe0SGreg Roach        }
134395f0fe0SGreg Roach    }
135395f0fe0SGreg Roach
136395f0fe0SGreg Roach    /**
137a25f0a04SGreg Roach     * Can the name of this record be shown?
138a25f0a04SGreg Roach     *
13976692c8bSGreg Roach     * @param int|null $access_level
14076692c8bSGreg Roach     *
14176692c8bSGreg Roach     * @return bool
142a25f0a04SGreg Roach     */
14335584196SGreg Roach    public function canShowName(int $access_level = null): bool
144c1010edaSGreg Roach    {
1454b9ff166SGreg Roach        if ($access_level === null) {
1464b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1474b9ff166SGreg Roach        }
1484b9ff166SGreg Roach
149518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
150a25f0a04SGreg Roach    }
151a25f0a04SGreg Roach
152a25f0a04SGreg Roach    /**
15376692c8bSGreg Roach     * Can this individual be shown?
154a25f0a04SGreg Roach     *
15576692c8bSGreg Roach     * @param int $access_level
15676692c8bSGreg Roach     *
15776692c8bSGreg Roach     * @return bool
158a25f0a04SGreg Roach     */
15935584196SGreg Roach    protected function canShowByType(int $access_level): bool
160c1010edaSGreg Roach    {
161a25f0a04SGreg Roach        // Dead people...
162518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
163a25f0a04SGreg Roach            $keep_alive             = false;
16454ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
165a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1668d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
167a25f0a04SGreg Roach                foreach ($matches as $match) {
168a25f0a04SGreg Roach                    $date = new Date($match[1]);
169a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
170a25f0a04SGreg Roach                        $keep_alive = true;
171a25f0a04SGreg Roach                        break;
172a25f0a04SGreg Roach                    }
173a25f0a04SGreg Roach                }
174a25f0a04SGreg Roach            }
17554ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
176a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1778d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
178a25f0a04SGreg Roach                foreach ($matches as $match) {
179a25f0a04SGreg Roach                    $date = new Date($match[1]);
180a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
181a25f0a04SGreg Roach                        $keep_alive = true;
182a25f0a04SGreg Roach                        break;
183a25f0a04SGreg Roach                    }
184a25f0a04SGreg Roach                }
185a25f0a04SGreg Roach            }
186a25f0a04SGreg Roach            if (!$keep_alive) {
187a25f0a04SGreg Roach                return true;
188a25f0a04SGreg Roach            }
189a25f0a04SGreg Roach        }
190a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
19154ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1924b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
19354ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1944b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
195a25f0a04SGreg Roach        }
196a25f0a04SGreg Roach
197a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1984b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
199a25f0a04SGreg Roach    }
200a25f0a04SGreg Roach
201a25f0a04SGreg Roach    /**
202a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
203a25f0a04SGreg Roach     *
204a25f0a04SGreg Roach     * @param Individual $target
205cbc1590aSGreg Roach     * @param int        $distance
206a25f0a04SGreg Roach     *
207cbc1590aSGreg Roach     * @return bool
208a25f0a04SGreg Roach     */
2098f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
210c1010edaSGreg Roach    {
211a25f0a04SGreg Roach        static $cache = null;
212a25f0a04SGreg Roach
21306ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
214a25f0a04SGreg Roach        if ($user_individual) {
215a25f0a04SGreg Roach            if (!$cache) {
21613abd6f3SGreg Roach                $cache = [
21713abd6f3SGreg Roach                    0 => [$user_individual],
21813abd6f3SGreg Roach                    1 => [],
21913abd6f3SGreg Roach                ];
2208d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
221dc124885SGreg Roach                    $family = $fact->target();
222e24444eeSGreg Roach                    if ($family instanceof Family) {
223a25f0a04SGreg Roach                        $cache[1][] = $family;
224a25f0a04SGreg Roach                    }
225a25f0a04SGreg Roach                }
226a25f0a04SGreg Roach            }
227a25f0a04SGreg Roach        } else {
228a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
229a25f0a04SGreg Roach            return true;
230a25f0a04SGreg Roach        }
231a25f0a04SGreg Roach
232a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
233a25f0a04SGreg Roach        $distance *= 2;
234a25f0a04SGreg Roach
235a25f0a04SGreg Roach        // Consider each path length in turn
236a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
237a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
238a25f0a04SGreg Roach                // We have already calculated all records with this length
239e364afe4SGreg Roach                if ($n % 2 === 0 && in_array($target, $cache[$n], true)) {
240a25f0a04SGreg Roach                    return true;
241a25f0a04SGreg Roach                }
242a25f0a04SGreg Roach            } else {
243a25f0a04SGreg Roach                // Need to calculate these paths
24413abd6f3SGreg Roach                $cache[$n] = [];
245e364afe4SGreg Roach                if ($n % 2 === 0) {
246a25f0a04SGreg Roach                    // Add FAM->INDI links
247a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2488d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
249dc124885SGreg Roach                            $individual = $fact->target();
250a25f0a04SGreg Roach                            // Don’t backtrack
251e364afe4SGreg Roach                            if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) {
252a25f0a04SGreg Roach                                $cache[$n][] = $individual;
253a25f0a04SGreg Roach                            }
254a25f0a04SGreg Roach                        }
255a25f0a04SGreg Roach                    }
256a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
257a25f0a04SGreg Roach                        return true;
258a25f0a04SGreg Roach                    }
259a25f0a04SGreg Roach                } else {
260a25f0a04SGreg Roach                    // Add INDI->FAM links
261a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2628d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
263dc124885SGreg Roach                            $family = $fact->target();
264a25f0a04SGreg Roach                            // Don’t backtrack
265e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
266a25f0a04SGreg Roach                                $cache[$n][] = $family;
267a25f0a04SGreg Roach                            }
268a25f0a04SGreg Roach                        }
269a25f0a04SGreg Roach                    }
270a25f0a04SGreg Roach                }
271a25f0a04SGreg Roach            }
272a25f0a04SGreg Roach        }
273a25f0a04SGreg Roach
274a25f0a04SGreg Roach        return false;
275a25f0a04SGreg Roach    }
276a25f0a04SGreg Roach
27776692c8bSGreg Roach    /**
27876692c8bSGreg Roach     * Generate a private version of this record
27976692c8bSGreg Roach     *
28076692c8bSGreg Roach     * @param int $access_level
28176692c8bSGreg Roach     *
28276692c8bSGreg Roach     * @return string
28376692c8bSGreg Roach     */
2843c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
285c1010edaSGreg Roach    {
286acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
287a25f0a04SGreg Roach
288a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
289518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
290a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2918d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
292138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
293a25f0a04SGreg Roach            }
294a25f0a04SGreg Roach        }
295a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2968d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
297a25f0a04SGreg Roach        foreach ($matches as $match) {
29824ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
299a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
300a25f0a04SGreg Roach                $rec .= $match[0];
301a25f0a04SGreg Roach            }
302a25f0a04SGreg Roach        }
303a25f0a04SGreg Roach        // Don’t privatize sex.
304a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
305a25f0a04SGreg Roach            $rec .= $match[0];
306a25f0a04SGreg Roach        }
307a25f0a04SGreg Roach
308a25f0a04SGreg Roach        return $rec;
309a25f0a04SGreg Roach    }
310a25f0a04SGreg Roach
31176692c8bSGreg Roach    /**
31276692c8bSGreg Roach     * Fetch data from the database
31376692c8bSGreg Roach     *
31476692c8bSGreg Roach     * @param string $xref
31576692c8bSGreg Roach     * @param int    $tree_id
31676692c8bSGreg Roach     *
317e364afe4SGreg Roach     * @return string|null
31876692c8bSGreg Roach     */
319e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
320c1010edaSGreg Roach    {
3212e5b4452SGreg Roach        return DB::table('individuals')
3222e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3232e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3242e5b4452SGreg Roach            ->value('i_gedcom');
325a25f0a04SGreg Roach    }
326a25f0a04SGreg Roach
327a25f0a04SGreg Roach    /**
328a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
329a25f0a04SGreg Roach     * If not known to be dead, then assume living.
330a25f0a04SGreg Roach     *
331cbc1590aSGreg Roach     * @return bool
332a25f0a04SGreg Roach     */
3338f53f488SRico Sonntag    public function isDead(): bool
334c1010edaSGreg Roach    {
335c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
3364459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
337a25f0a04SGreg Roach
338a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3398d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
340a25f0a04SGreg Roach            return true;
341a25f0a04SGreg Roach        }
342a25f0a04SGreg Roach
343a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
344a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
345a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
346a25f0a04SGreg Roach                $date = new Date($date_match);
347269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
348a25f0a04SGreg Roach                    return true;
349a25f0a04SGreg Roach                }
350a25f0a04SGreg Roach            }
351a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
352a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
353a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
354a25f0a04SGreg Roach                return false;
355a25f0a04SGreg Roach            }
356a25f0a04SGreg Roach        }
357a25f0a04SGreg Roach
358a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
359a25f0a04SGreg Roach
360a25f0a04SGreg Roach        // Check parents (birth and adopted)
36139ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
36239ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
363a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
364a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
365a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
366a25f0a04SGreg Roach                    $date = new Date($date_match);
367269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
368a25f0a04SGreg Roach                        return true;
369a25f0a04SGreg Roach                    }
370a25f0a04SGreg Roach                }
371a25f0a04SGreg Roach            }
372a25f0a04SGreg Roach        }
373a25f0a04SGreg Roach
374a25f0a04SGreg Roach        // Check spouses
37539ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
376a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
377a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
378a25f0a04SGreg Roach                $date = new Date($date_match);
379a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
380269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
381a25f0a04SGreg Roach                    return true;
382a25f0a04SGreg Roach                }
383a25f0a04SGreg Roach            }
384a25f0a04SGreg Roach            // Check spouse dates
38539ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
386a25f0a04SGreg Roach            if ($spouse) {
387a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
388a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
389a25f0a04SGreg Roach                    $date = new Date($date_match);
390a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
391269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
392a25f0a04SGreg Roach                        return true;
393a25f0a04SGreg Roach                    }
394a25f0a04SGreg Roach                }
395a25f0a04SGreg Roach            }
396a25f0a04SGreg Roach            // Check child dates
39739ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
398a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
399a25f0a04SGreg Roach                // Assume children born after age of 15
400a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
401a25f0a04SGreg Roach                    $date = new Date($date_match);
402269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
403a25f0a04SGreg Roach                        return true;
404a25f0a04SGreg Roach                    }
405a25f0a04SGreg Roach                }
406a25f0a04SGreg Roach                // Check grandchildren
40739ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
40839ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
409a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
410a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
411a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
412a25f0a04SGreg Roach                            $date = new Date($date_match);
413269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
414a25f0a04SGreg Roach                                return true;
415a25f0a04SGreg Roach                            }
416a25f0a04SGreg Roach                        }
417a25f0a04SGreg Roach                    }
418a25f0a04SGreg Roach                }
419a25f0a04SGreg Roach            }
420a25f0a04SGreg Roach        }
421a25f0a04SGreg Roach
422a25f0a04SGreg Roach        return false;
423a25f0a04SGreg Roach    }
424a25f0a04SGreg Roach
425a25f0a04SGreg Roach    /**
426a25f0a04SGreg Roach     * Find the highlighted media object for an individual
427a25f0a04SGreg Roach     *
428e364afe4SGreg Roach     * @return MediaFile|null
429a25f0a04SGreg Roach     */
430e364afe4SGreg Roach    public function findHighlightedMediaFile(): ?MediaFile
431c1010edaSGreg Roach    {
4328d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
433dc124885SGreg Roach            $media = $fact->target();
434a213a63cSGreg Roach            if ($media instanceof Media) {
4354a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
436a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4374a9f750fSGreg Roach                        return $media_file;
4384a9f750fSGreg Roach                    }
4394a9f750fSGreg Roach                }
440a25f0a04SGreg Roach            }
441a25f0a04SGreg Roach        }
442a25f0a04SGreg Roach
443a25f0a04SGreg Roach        return null;
444a25f0a04SGreg Roach    }
445a25f0a04SGreg Roach
446a25f0a04SGreg Roach    /**
447a25f0a04SGreg Roach     * Display the prefered image for this individual.
448a25f0a04SGreg Roach     * Use an icon if no image is available.
449a25f0a04SGreg Roach     *
450dce07401SGreg Roach     * @param int      $width      Pixels
451dce07401SGreg Roach     * @param int      $height     Pixels
452dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
453dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
454dce07401SGreg Roach     *
455a25f0a04SGreg Roach     * @return string
456a25f0a04SGreg Roach     */
4578f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
458c1010edaSGreg Roach    {
4594a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4604a9f750fSGreg Roach
4614a9f750fSGreg Roach        if ($media_file !== null) {
4624a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
463a25f0a04SGreg Roach        }
4644a9f750fSGreg Roach
4654a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
46639ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4674a9f750fSGreg Roach        }
4684a9f750fSGreg Roach
4694a9f750fSGreg Roach        return '';
470a25f0a04SGreg Roach    }
471a25f0a04SGreg Roach
472a25f0a04SGreg Roach    /**
473a25f0a04SGreg Roach     * Get the date of birth
474a25f0a04SGreg Roach     *
475a25f0a04SGreg Roach     * @return Date
476a25f0a04SGreg Roach     */
4778f53f488SRico Sonntag    public function getBirthDate(): Date
478c1010edaSGreg Roach    {
479a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
480a25f0a04SGreg Roach            if ($date->isOK()) {
481a25f0a04SGreg Roach                return $date;
482a25f0a04SGreg Roach            }
483a25f0a04SGreg Roach        }
484a25f0a04SGreg Roach
485a25f0a04SGreg Roach        return new Date('');
486a25f0a04SGreg Roach    }
487a25f0a04SGreg Roach
488a25f0a04SGreg Roach    /**
489a25f0a04SGreg Roach     * Get the place of birth
490a25f0a04SGreg Roach     *
49116d0b7f7SRico Sonntag     * @return Place
492a25f0a04SGreg Roach     */
4938f53f488SRico Sonntag    public function getBirthPlace(): Place
494c1010edaSGreg Roach    {
495a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
496a25f0a04SGreg Roach            return $place;
497a25f0a04SGreg Roach        }
498a25f0a04SGreg Roach
499b20ddbf9SGreg Roach        return new Place('', $this->tree);
500a25f0a04SGreg Roach    }
501a25f0a04SGreg Roach
502a25f0a04SGreg Roach    /**
503a25f0a04SGreg Roach     * Get the year of birth
504a25f0a04SGreg Roach     *
505a25f0a04SGreg Roach     * @return string the year of birth
506a25f0a04SGreg Roach     */
5078f53f488SRico Sonntag    public function getBirthYear(): string
508c1010edaSGreg Roach    {
509f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
510a25f0a04SGreg Roach    }
511a25f0a04SGreg Roach
512a25f0a04SGreg Roach    /**
513a25f0a04SGreg Roach     * Get the date of death
514a25f0a04SGreg Roach     *
515a25f0a04SGreg Roach     * @return Date
516a25f0a04SGreg Roach     */
5178f53f488SRico Sonntag    public function getDeathDate(): Date
518c1010edaSGreg Roach    {
519a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
520a25f0a04SGreg Roach            if ($date->isOK()) {
521a25f0a04SGreg Roach                return $date;
522a25f0a04SGreg Roach            }
523a25f0a04SGreg Roach        }
524a25f0a04SGreg Roach
525a25f0a04SGreg Roach        return new Date('');
526a25f0a04SGreg Roach    }
527a25f0a04SGreg Roach
528a25f0a04SGreg Roach    /**
529a25f0a04SGreg Roach     * Get the place of death
530a25f0a04SGreg Roach     *
53116d0b7f7SRico Sonntag     * @return Place
532a25f0a04SGreg Roach     */
5338f53f488SRico Sonntag    public function getDeathPlace(): Place
534c1010edaSGreg Roach    {
535a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
536a25f0a04SGreg Roach            return $place;
537a25f0a04SGreg Roach        }
538a25f0a04SGreg Roach
539b20ddbf9SGreg Roach        return new Place('', $this->tree);
540a25f0a04SGreg Roach    }
541a25f0a04SGreg Roach
542a25f0a04SGreg Roach    /**
543a25f0a04SGreg Roach     * get the death year
544a25f0a04SGreg Roach     *
545a25f0a04SGreg Roach     * @return string the year of death
546a25f0a04SGreg Roach     */
5478f53f488SRico Sonntag    public function getDeathYear(): string
548c1010edaSGreg Roach    {
549f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
550a25f0a04SGreg Roach    }
551a25f0a04SGreg Roach
552a25f0a04SGreg Roach    /**
553a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
55415d603e7SGreg Roach     * Provide the place and full date using a tooltip.
555a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
556a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
557a25f0a04SGreg Roach     *
558a25f0a04SGreg Roach     * @return string
559a25f0a04SGreg Roach     */
5608f53f488SRico Sonntag    public function getLifeSpan(): string
561c1010edaSGreg Roach    {
56215d603e7SGreg Roach        // Just the first part of the place name
563392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
564392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
56515d603e7SGreg Roach        // Remove markup from dates
56615d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56715d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56815d603e7SGreg Roach
569c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
570bbb76c12SGreg Roach        return
571c1010edaSGreg Roach            I18N::translate(
572a25f0a04SGreg Roach                '%1$s–%2$s',
5732d4c1bedSGreg Roach                '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5742d4c1bedSGreg Roach                '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
575a25f0a04SGreg Roach            );
576a25f0a04SGreg Roach    }
577a25f0a04SGreg Roach
578a25f0a04SGreg Roach    /**
579a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
580a25f0a04SGreg Roach     *
581a25f0a04SGreg Roach     * @return Date[]
582a25f0a04SGreg Roach     */
5838f53f488SRico Sonntag    public function getAllBirthDates(): array
584c1010edaSGreg Roach    {
5858d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5868d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
587a25f0a04SGreg Roach            if ($tmp) {
588a25f0a04SGreg Roach                return $tmp;
589a25f0a04SGreg Roach            }
590a25f0a04SGreg Roach        }
591a25f0a04SGreg Roach
59213abd6f3SGreg Roach        return [];
593a25f0a04SGreg Roach    }
594a25f0a04SGreg Roach
595a25f0a04SGreg Roach    /**
596a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
597a25f0a04SGreg Roach     *
5984080d558SGreg Roach     * @return Place[]
599a25f0a04SGreg Roach     */
6008f53f488SRico Sonntag    public function getAllBirthPlaces(): array
601c1010edaSGreg Roach    {
6028d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
6038d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6044080d558SGreg Roach            if (!empty($places)) {
6054080d558SGreg Roach                return $places;
606a25f0a04SGreg Roach            }
607a25f0a04SGreg Roach        }
608a25f0a04SGreg Roach
60913abd6f3SGreg Roach        return [];
610a25f0a04SGreg Roach    }
611a25f0a04SGreg Roach
612a25f0a04SGreg Roach    /**
613a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
614a25f0a04SGreg Roach     *
615a25f0a04SGreg Roach     * @return Date[]
616a25f0a04SGreg Roach     */
6178f53f488SRico Sonntag    public function getAllDeathDates(): array
618c1010edaSGreg Roach    {
6198d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6208d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
621a25f0a04SGreg Roach            if ($tmp) {
622a25f0a04SGreg Roach                return $tmp;
623a25f0a04SGreg Roach            }
624a25f0a04SGreg Roach        }
625a25f0a04SGreg Roach
62613abd6f3SGreg Roach        return [];
627a25f0a04SGreg Roach    }
628a25f0a04SGreg Roach
629a25f0a04SGreg Roach    /**
630a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
631a25f0a04SGreg Roach     *
6324080d558SGreg Roach     * @return Place[]
633a25f0a04SGreg Roach     */
6348f53f488SRico Sonntag    public function getAllDeathPlaces(): array
635c1010edaSGreg Roach    {
6368d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6378d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6384080d558SGreg Roach            if (!empty($places)) {
6394080d558SGreg Roach                return $places;
640a25f0a04SGreg Roach            }
641a25f0a04SGreg Roach        }
642a25f0a04SGreg Roach
64313abd6f3SGreg Roach        return [];
644a25f0a04SGreg Roach    }
645a25f0a04SGreg Roach
646a25f0a04SGreg Roach    /**
647a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
648a25f0a04SGreg Roach     *
649a25f0a04SGreg Roach     * @return Date
650a25f0a04SGreg Roach     */
6518f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
652c1010edaSGreg Roach    {
6538f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
654a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
655a25f0a04SGreg Roach                if ($date->isOK()) {
6564686330aSGreg Roach                    $this->estimated_birth_date = $date;
657a25f0a04SGreg Roach                    break;
658a25f0a04SGreg Roach                }
659a25f0a04SGreg Roach            }
6608f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
66113abd6f3SGreg Roach                $min = [];
66213abd6f3SGreg Roach                $max = [];
663a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
664f5b60decSGreg Roach                if ($tmp->isOK()) {
665f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
666f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
667a25f0a04SGreg Roach                }
66839ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
669a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
670f5b60decSGreg Roach                    if ($tmp->isOK()) {
671f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
672f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
673a25f0a04SGreg Roach                    }
67439ca88baSGreg Roach                    $husband = $family->husband();
675e364afe4SGreg Roach                    if ($husband instanceof self) {
6762e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
677f5b60decSGreg Roach                        if ($tmp->isOK()) {
678f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
679f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
680a25f0a04SGreg Roach                        }
681a25f0a04SGreg Roach                    }
68239ca88baSGreg Roach                    $wife = $family->wife();
683e364afe4SGreg Roach                    if ($wife instanceof self) {
6842e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
685f5b60decSGreg Roach                        if ($tmp->isOK()) {
686f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
687f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
688a25f0a04SGreg Roach                        }
689a25f0a04SGreg Roach                    }
69039ca88baSGreg Roach                    foreach ($family->children() as $child) {
691a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
692f5b60decSGreg Roach                        if ($tmp->isOK()) {
693f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
694f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
695a25f0a04SGreg Roach                        }
696a25f0a04SGreg Roach                    }
697a25f0a04SGreg Roach                }
69839ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
699a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
700f5b60decSGreg Roach                    if ($tmp->isOK()) {
701f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
702f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
703a25f0a04SGreg Roach                    }
70439ca88baSGreg Roach                    $spouse = $family->spouse($this);
705a25f0a04SGreg Roach                    if ($spouse) {
706a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
707f5b60decSGreg Roach                        if ($tmp->isOK()) {
708f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
709f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
710a25f0a04SGreg Roach                        }
711a25f0a04SGreg Roach                    }
71239ca88baSGreg Roach                    foreach ($family->children() as $child) {
713a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
714f5b60decSGreg Roach                        if ($tmp->isOK()) {
715e364afe4SGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65);
716f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
717a25f0a04SGreg Roach                        }
718a25f0a04SGreg Roach                    }
719a25f0a04SGreg Roach                }
720a25f0a04SGreg Roach                if ($min && $max) {
72159f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
722a25f0a04SGreg Roach
72365e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7244686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
725a25f0a04SGreg Roach                } else {
7264686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
727a25f0a04SGreg Roach                }
728a25f0a04SGreg Roach            }
729a25f0a04SGreg Roach        }
730a25f0a04SGreg Roach
7314686330aSGreg Roach        return $this->estimated_birth_date;
732a25f0a04SGreg Roach    }
733a25f0a04SGreg Roach
734a25f0a04SGreg Roach    /**
735a25f0a04SGreg Roach     * Generate an estimated date of death.
736a25f0a04SGreg Roach     *
737a25f0a04SGreg Roach     * @return Date
738a25f0a04SGreg Roach     */
7398f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
740c1010edaSGreg Roach    {
7414686330aSGreg Roach        if ($this->estimated_death_date === null) {
742a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
743a25f0a04SGreg Roach                if ($date->isOK()) {
7444686330aSGreg Roach                    $this->estimated_death_date = $date;
745a25f0a04SGreg Roach                    break;
746a25f0a04SGreg Roach                }
747a25f0a04SGreg Roach            }
7484686330aSGreg Roach            if ($this->estimated_death_date === null) {
749f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
750c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7514686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
752a25f0a04SGreg Roach                } else {
7534686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
754a25f0a04SGreg Roach                }
755a25f0a04SGreg Roach            }
756a25f0a04SGreg Roach        }
757a25f0a04SGreg Roach
7584686330aSGreg Roach        return $this->estimated_death_date;
759a25f0a04SGreg Roach    }
760a25f0a04SGreg Roach
761a25f0a04SGreg Roach    /**
762a25f0a04SGreg Roach     * Get the sex - M F or U
763a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
764a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
765a25f0a04SGreg Roach     *
766a25f0a04SGreg Roach     * @return string
767a25f0a04SGreg Roach     */
768e364afe4SGreg Roach    public function sex(): string
769c1010edaSGreg Roach    {
770a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
771a25f0a04SGreg Roach            return $match[1];
772a25f0a04SGreg Roach        }
773b2ce94c6SRico Sonntag
774b2ce94c6SRico Sonntag        return 'U';
775a25f0a04SGreg Roach    }
776a25f0a04SGreg Roach
777a25f0a04SGreg Roach    /**
778a25f0a04SGreg Roach     * Get the individual’s sex image
779a25f0a04SGreg Roach     *
780a25f0a04SGreg Roach     * @param string $size
781a25f0a04SGreg Roach     *
782a25f0a04SGreg Roach     * @return string
783a25f0a04SGreg Roach     */
7848f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
785c1010edaSGreg Roach    {
78639ca88baSGreg Roach        return self::sexImage($this->sex(), $size);
787a25f0a04SGreg Roach    }
788a25f0a04SGreg Roach
789a25f0a04SGreg Roach    /**
790a25f0a04SGreg Roach     * Generate a sex icon/image
791a25f0a04SGreg Roach     *
792a25f0a04SGreg Roach     * @param string $sex
793a25f0a04SGreg Roach     * @param string $size
794a25f0a04SGreg Roach     *
795a25f0a04SGreg Roach     * @return string
796a25f0a04SGreg Roach     */
7978f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
798c1010edaSGreg Roach    {
799242a7862SGreg Roach        $image = view('icons/sex-' . $sex);
800242a7862SGreg Roach
801242a7862SGreg Roach        if ($size === 'small') {
802242a7862SGreg Roach            $image = '<small>' . $image . '</small>';
803242a7862SGreg Roach        }
804242a7862SGreg Roach
805242a7862SGreg Roach        return $image;
806a25f0a04SGreg Roach    }
807a25f0a04SGreg Roach
808a25f0a04SGreg Roach    /**
809a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
810a25f0a04SGreg Roach     *
811a25f0a04SGreg Roach     * @return string
812a25f0a04SGreg Roach     */
8138f53f488SRico Sonntag    public function getBoxStyle(): string
814c1010edaSGreg Roach    {
815c1010edaSGreg Roach        $tmp = [
816c1010edaSGreg Roach            'M' => '',
817c1010edaSGreg Roach            'F' => 'F',
818c1010edaSGreg Roach            'U' => 'NN',
819c1010edaSGreg Roach        ];
820a25f0a04SGreg Roach
82139ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
822a25f0a04SGreg Roach    }
823a25f0a04SGreg Roach
824a25f0a04SGreg Roach    /**
825a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
826a25f0a04SGreg Roach     *
827cbc1590aSGreg Roach     * @param int|null $access_level
828a25f0a04SGreg Roach     *
82954c7f8dfSGreg Roach     * @return Collection
83054c7f8dfSGreg Roach     * @return Family[]
831a25f0a04SGreg Roach     */
83239ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
833c1010edaSGreg Roach    {
8344b9ff166SGreg Roach        if ($access_level === null) {
8354b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8364b9ff166SGreg Roach        }
8374b9ff166SGreg Roach
838acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
839a25f0a04SGreg Roach
84039ca88baSGreg Roach        $families = new Collection();
8418d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
842dc124885SGreg Roach            $family = $fact->target();
843e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
84439ca88baSGreg Roach                $families->push($family);
845a25f0a04SGreg Roach            }
846a25f0a04SGreg Roach        }
847a25f0a04SGreg Roach
84839ca88baSGreg Roach        return new Collection($families);
849a25f0a04SGreg Roach    }
850a25f0a04SGreg Roach
851a25f0a04SGreg Roach    /**
852a25f0a04SGreg Roach     * Get the current spouse of this individual.
853a25f0a04SGreg Roach     *
854a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
855a25f0a04SGreg Roach     * in chronological order, and take the last one found.
856a25f0a04SGreg Roach     *
857a25f0a04SGreg Roach     * @return Individual|null
858a25f0a04SGreg Roach     */
859e364afe4SGreg Roach    public function getCurrentSpouse(): ?Individual
860c1010edaSGreg Roach    {
86139ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
86239ca88baSGreg Roach
86339ca88baSGreg Roach        if ($family instanceof Family) {
86439ca88baSGreg Roach            return $family->spouse($this);
865a25f0a04SGreg Roach        }
866b2ce94c6SRico Sonntag
867b2ce94c6SRico Sonntag        return null;
868a25f0a04SGreg Roach    }
869a25f0a04SGreg Roach
870a25f0a04SGreg Roach    /**
871a25f0a04SGreg Roach     * Count the children belonging to this individual.
872a25f0a04SGreg Roach     *
873cbc1590aSGreg Roach     * @return int
874a25f0a04SGreg Roach     */
875e364afe4SGreg Roach    public function numberOfChildren(): int
876c1010edaSGreg Roach    {
8777d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8783dc7bbe9SGreg Roach            return (int) $match[1];
879b2ce94c6SRico Sonntag        }
880b2ce94c6SRico Sonntag
88113abd6f3SGreg Roach        $children = [];
88239ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
88339ca88baSGreg Roach            foreach ($fam->children() as $child) {
884c0935879SGreg Roach                $children[$child->xref()] = true;
885a25f0a04SGreg Roach            }
886a25f0a04SGreg Roach        }
887a25f0a04SGreg Roach
888a25f0a04SGreg Roach        return count($children);
889a25f0a04SGreg Roach    }
890a25f0a04SGreg Roach
891a25f0a04SGreg Roach    /**
892a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
893a25f0a04SGreg Roach     *
894cbc1590aSGreg Roach     * @param int|null $access_level
895a25f0a04SGreg Roach     *
89654c7f8dfSGreg Roach     * @return Collection
89754c7f8dfSGreg Roach     * @return Family[]
898a25f0a04SGreg Roach     */
89939ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
900c1010edaSGreg Roach    {
9014b9ff166SGreg Roach        if ($access_level === null) {
9024b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
9034b9ff166SGreg Roach        }
9044b9ff166SGreg Roach
905acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
906a25f0a04SGreg Roach
90739ca88baSGreg Roach        $families = new Collection();
90839ca88baSGreg Roach
9098d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
910dc124885SGreg Roach            $family = $fact->target();
911e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
91239ca88baSGreg Roach                $families->push($family);
913a25f0a04SGreg Roach            }
914a25f0a04SGreg Roach        }
915a25f0a04SGreg Roach
916a25f0a04SGreg Roach        return $families;
917a25f0a04SGreg Roach    }
918a25f0a04SGreg Roach
919a25f0a04SGreg Roach    /**
920a25f0a04SGreg Roach     * Get the preferred parents for this individual.
921a25f0a04SGreg Roach     *
922a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
923a25f0a04SGreg Roach     * The preferred family record is:
924a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
925a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
926a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
927a25f0a04SGreg Roach     * (d) the first one found
928a25f0a04SGreg Roach     *
929a25f0a04SGreg Roach     * @return Family|null
930a25f0a04SGreg Roach     */
931e364afe4SGreg Roach    public function primaryChildFamily(): ?Family
932c1010edaSGreg Roach    {
93339ca88baSGreg Roach        $families = $this->childFamilies();
9343b092cb5SGreg Roach        switch ($families->count()) {
935a25f0a04SGreg Roach            case 0:
936a25f0a04SGreg Roach                return null;
937a25f0a04SGreg Roach            case 1:
93815d603e7SGreg Roach                return $families[0];
939a25f0a04SGreg Roach            default:
940a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
941a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
94274e78bfaSJonathan Jaubart                foreach ($families as $fam) {
943c0935879SGreg Roach                    $famid = $fam->xref();
9447d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
945a25f0a04SGreg Roach                        return $fam;
946a25f0a04SGreg Roach                    }
947a25f0a04SGreg Roach                }
948a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
94974e78bfaSJonathan Jaubart                foreach ($families as $fam) {
950c0935879SGreg Roach                    $famid = $fam->xref();
9517d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
952a25f0a04SGreg Roach                        return $fam;
953a25f0a04SGreg Roach                    }
954a25f0a04SGreg Roach                }
955a25f0a04SGreg Roach                // c) records with no '2 PEDI'
95674e78bfaSJonathan Jaubart                foreach ($families as $fam) {
957c0935879SGreg Roach                    $famid = $fam->xref();
9587d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
959a25f0a04SGreg Roach                        return $fam;
960a25f0a04SGreg Roach                    }
961a25f0a04SGreg Roach                }
962a25f0a04SGreg Roach
963a25f0a04SGreg Roach                // d) any record
96415d603e7SGreg Roach                return $families[0];
965a25f0a04SGreg Roach        }
966a25f0a04SGreg Roach    }
967a25f0a04SGreg Roach
968a25f0a04SGreg Roach    /**
969a25f0a04SGreg Roach     * Get a list of step-parent families.
970a25f0a04SGreg Roach     *
97154c7f8dfSGreg Roach     * @return Collection
97254c7f8dfSGreg Roach     * @return Family[]
973a25f0a04SGreg Roach     */
974820b62dfSGreg Roach    public function childStepFamilies(): Collection
975c1010edaSGreg Roach    {
97613abd6f3SGreg Roach        $step_families = [];
97739ca88baSGreg Roach        $families      = $this->childFamilies();
978a25f0a04SGreg Roach        foreach ($families as $family) {
97939ca88baSGreg Roach            $father = $family->husband();
980a25f0a04SGreg Roach            if ($father) {
98139ca88baSGreg Roach                foreach ($father->spouseFamilies() as $step_family) {
98239ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
983a25f0a04SGreg Roach                        $step_families[] = $step_family;
984a25f0a04SGreg Roach                    }
985a25f0a04SGreg Roach                }
986a25f0a04SGreg Roach            }
98739ca88baSGreg Roach            $mother = $family->wife();
988a25f0a04SGreg Roach            if ($mother) {
98939ca88baSGreg Roach                foreach ($mother->spouseFamilies() as $step_family) {
99039ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
991a25f0a04SGreg Roach                        $step_families[] = $step_family;
992a25f0a04SGreg Roach                    }
993a25f0a04SGreg Roach                }
994a25f0a04SGreg Roach            }
995a25f0a04SGreg Roach        }
996a25f0a04SGreg Roach
997820b62dfSGreg Roach        return new Collection($step_families);
998a25f0a04SGreg Roach    }
999a25f0a04SGreg Roach
1000a25f0a04SGreg Roach    /**
1001a25f0a04SGreg Roach     * Get a list of step-parent families.
1002a25f0a04SGreg Roach     *
100354c7f8dfSGreg Roach     * @return Collection
100454c7f8dfSGreg Roach     * @return Family[]
1005a25f0a04SGreg Roach     */
1006820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
1007c1010edaSGreg Roach    {
100813abd6f3SGreg Roach        $step_families = [];
100939ca88baSGreg Roach        $families      = $this->spouseFamilies();
1010820b62dfSGreg Roach
1011a25f0a04SGreg Roach        foreach ($families as $family) {
101239ca88baSGreg Roach            $spouse = $family->spouse($this);
1013820b62dfSGreg Roach
1014a25f0a04SGreg Roach            if ($spouse) {
101539ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
101639ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
1017a25f0a04SGreg Roach                        $step_families[] = $step_family;
1018a25f0a04SGreg Roach                    }
1019a25f0a04SGreg Roach                }
1020a25f0a04SGreg Roach            }
1021a25f0a04SGreg Roach        }
1022a25f0a04SGreg Roach
1023820b62dfSGreg Roach        return new Collection($step_families);
1024a25f0a04SGreg Roach    }
1025a25f0a04SGreg Roach
1026a25f0a04SGreg Roach    /**
1027a25f0a04SGreg Roach     * A label for a parental family group
1028a25f0a04SGreg Roach     *
1029a25f0a04SGreg Roach     * @param Family $family
1030a25f0a04SGreg Roach     *
1031a25f0a04SGreg Roach     * @return string
1032a25f0a04SGreg Roach     */
1033820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
1034c1010edaSGreg Roach    {
10357d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
1036a25f0a04SGreg Roach            // A specified pedigree
1037764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1038b2ce94c6SRico Sonntag        }
1039b2ce94c6SRico Sonntag
1040a25f0a04SGreg Roach        // Default (birth) pedigree
1041764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1042a25f0a04SGreg Roach    }
1043a25f0a04SGreg Roach
1044a25f0a04SGreg Roach    /**
1045a25f0a04SGreg Roach     * Create a label for a step family
1046a25f0a04SGreg Roach     *
1047a25f0a04SGreg Roach     * @param Family $step_family
1048a25f0a04SGreg Roach     *
1049a25f0a04SGreg Roach     * @return string
1050a25f0a04SGreg Roach     */
10518f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1052c1010edaSGreg Roach    {
105339ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
1054a25f0a04SGreg Roach            if ($family !== $step_family) {
1055a25f0a04SGreg Roach                // Must be a step-family
105639ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
105739ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
1058a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1059a25f0a04SGreg Roach                            // One common parent - must be a step family
1060e364afe4SGreg Roach                            if ($parent->sex() === 'M') {
1061a25f0a04SGreg Roach                                // Father’s family with someone else
106239ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
1063a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
106439ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
1065b2ce94c6SRico Sonntag                                }
1066b2ce94c6SRico Sonntag
1067a25f0a04SGreg Roach                                /* I18N: A step-family. */
1068bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1069a25f0a04SGreg Roach                            }
1070b2ce94c6SRico Sonntag
1071a25f0a04SGreg Roach                            // Mother’s family with someone else
107239ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
1073a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
107439ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
1075b2ce94c6SRico Sonntag                            }
1076b2ce94c6SRico Sonntag
1077a25f0a04SGreg Roach                            /* I18N: A step-family. */
1078bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1079a25f0a04SGreg Roach                        }
1080a25f0a04SGreg Roach                    }
1081a25f0a04SGreg Roach                }
1082a25f0a04SGreg Roach            }
1083a25f0a04SGreg Roach        }
1084a25f0a04SGreg Roach
1085a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1086a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1087a25f0a04SGreg Roach    }
1088a25f0a04SGreg Roach
1089225e381fSGreg Roach    /**
1090225e381fSGreg Roach     * Get the description for the family.
1091225e381fSGreg Roach     *
1092225e381fSGreg Roach     * For example, "XXX's family with new wife".
1093225e381fSGreg Roach     *
1094225e381fSGreg Roach     * @param Family $family
1095225e381fSGreg Roach     *
1096225e381fSGreg Roach     * @return string
1097225e381fSGreg Roach     */
1098e364afe4SGreg Roach    public function getSpouseFamilyLabel(Family $family): string
1099c1010edaSGreg Roach    {
110039ca88baSGreg Roach        $spouse = $family->spouse($this);
1101225e381fSGreg Roach        if ($spouse) {
1102225e381fSGreg Roach            /* I18N: %s is the spouse name */
110339ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1104225e381fSGreg Roach        }
1105b2ce94c6SRico Sonntag
110639ca88baSGreg Roach        return $family->fullName();
1107225e381fSGreg Roach    }
1108225e381fSGreg Roach
1109a25f0a04SGreg Roach    /**
1110a25f0a04SGreg Roach     * get primary parents names for this individual
1111a25f0a04SGreg Roach     *
1112a25f0a04SGreg Roach     * @param string $classname optional css class
1113a25f0a04SGreg Roach     * @param string $display   optional css style display
1114a25f0a04SGreg Roach     *
1115a25f0a04SGreg Roach     * @return string a div block with father & mother names
1116a25f0a04SGreg Roach     */
11178f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1118c1010edaSGreg Roach    {
111939ca88baSGreg Roach        $fam = $this->primaryChildFamily();
1120a25f0a04SGreg Roach        if (!$fam) {
1121a25f0a04SGreg Roach            return '';
1122a25f0a04SGreg Roach        }
1123a25f0a04SGreg Roach        $txt = '<div';
1124a25f0a04SGreg Roach        if ($classname) {
11254c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1126a25f0a04SGreg Roach        }
1127a25f0a04SGreg Roach        if ($display) {
11284c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1129a25f0a04SGreg Roach        }
1130a25f0a04SGreg Roach        $txt .= '>';
113139ca88baSGreg Roach        $husb = $fam->husband();
1132a25f0a04SGreg Roach        if ($husb) {
1133a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1134a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1135a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1136a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1137a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
113839ca88baSGreg Roach            $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>';
1139a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1140a25f0a04SGreg Roach        }
114139ca88baSGreg Roach        $wife = $fam->wife();
1142a25f0a04SGreg Roach        if ($wife) {
1143a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1144a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1145a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1146a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1147a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
114839ca88baSGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->fullName());
1149a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1150a25f0a04SGreg Roach        }
1151a25f0a04SGreg Roach        $txt .= '</div>';
1152a25f0a04SGreg Roach
1153a25f0a04SGreg Roach        return $txt;
1154a25f0a04SGreg Roach    }
1155a25f0a04SGreg Roach
1156961ec755SGreg Roach    /**
1157961ec755SGreg Roach     * If this object has no name, what do we call it?
1158961ec755SGreg Roach     *
1159961ec755SGreg Roach     * @return string
1160961ec755SGreg Roach     */
11618f53f488SRico Sonntag    public function getFallBackName(): string
1162c1010edaSGreg Roach    {
1163a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1164a25f0a04SGreg Roach    }
1165a25f0a04SGreg Roach
1166a25f0a04SGreg Roach    /**
1167a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1168a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1169a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1170a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1171a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1172a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1173a25f0a04SGreg Roach     *
1174a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1175a25f0a04SGreg Roach     * 2 GIVN Robert
1176a25f0a04SGreg Roach     * 2 SPFX de
1177a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1178a25f0a04SGreg Roach     * 2 NICK The Bald
1179a25f0a04SGreg Roach     *
1180a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1181a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1182a25f0a04SGreg Roach     *
1183a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1184a25f0a04SGreg Roach     *
1185a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1186a25f0a04SGreg Roach     * or
1187a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1188a25f0a04SGreg Roach     * 2 GIVN Carlos
1189a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1190a25f0a04SGreg Roach     *
1191a25f0a04SGreg Roach     * @param string $type
1192a25f0a04SGreg Roach     * @param string $full
1193a25f0a04SGreg Roach     * @param string $gedcom
1194e364afe4SGreg Roach     *
1195e364afe4SGreg Roach     * @return void
1196a25f0a04SGreg Roach     */
1197e364afe4SGreg Roach    protected function addName(string $type, string $full, string $gedcom): void
1198c1010edaSGreg Roach    {
1199a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1200a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1201a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1202a25f0a04SGreg Roach
120376f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1204a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1205a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1206a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1207a25f0a04SGreg Roach
1208a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
120976f666f4SGreg Roach        if ($SURN !== '') {
1210a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1211a25f0a04SGreg Roach        } else {
121213abd6f3SGreg Roach            $SURNS = [];
1213a25f0a04SGreg Roach        }
121476f666f4SGreg Roach
1215a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1216a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1217a25f0a04SGreg Roach
1218a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1219a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1220a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1221a25f0a04SGreg Roach
1222a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
122376f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1224e364afe4SGreg Roach            $full .= '/';
1225a25f0a04SGreg Roach        }
1226a25f0a04SGreg Roach
1227a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1228a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1229a25f0a04SGreg Roach
1230a25f0a04SGreg Roach        // Extract the surname.
1231a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1232a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1233a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1234a25f0a04SGreg Roach        } else {
1235a25f0a04SGreg Roach            $surname = '';
1236a25f0a04SGreg Roach        }
1237a25f0a04SGreg Roach
1238a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1239a25f0a04SGreg Roach        if (!$SURNS) {
1240a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1241a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1242a25f0a04SGreg Roach                $SURNS = $matches[1];
1243a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1244a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1245a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1246a25f0a04SGreg Roach                }
1247a25f0a04SGreg Roach            } else {
1248a25f0a04SGreg Roach                // It is valid not to have a surname at all
124913abd6f3SGreg Roach                $SURNS = [''];
1250a25f0a04SGreg Roach            }
1251a25f0a04SGreg Roach        }
1252a25f0a04SGreg Roach
1253a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1254a25f0a04SGreg Roach        if (!$GIVN) {
1255a25f0a04SGreg Roach            $GIVN = preg_replace(
125613abd6f3SGreg Roach                [
1257c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1258c1010edaSGreg Roach                    // remove surname
1259c1010edaSGreg Roach                    '/ ?".+"/',
1260c1010edaSGreg Roach                    // remove nickname
1261c1010edaSGreg Roach                    '/ {2,}/',
1262c1010edaSGreg Roach                    // multiple spaces, caused by the above
1263c1010edaSGreg Roach                    '/^ | $/',
1264c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
126513abd6f3SGreg Roach                ],
126613abd6f3SGreg Roach                [
1267a25f0a04SGreg Roach                    ' ',
1268a25f0a04SGreg Roach                    ' ',
1269a25f0a04SGreg Roach                    ' ',
1270a25f0a04SGreg Roach                    '',
127113abd6f3SGreg Roach                ],
1272a25f0a04SGreg Roach                $full
1273a25f0a04SGreg Roach            );
1274a25f0a04SGreg Roach        }
1275a25f0a04SGreg Roach
1276a25f0a04SGreg Roach        // Add placeholder for unknown given name
1277a25f0a04SGreg Roach        if (!$GIVN) {
1278a25f0a04SGreg Roach            $GIVN = '@P.N.';
127973f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1280a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1281a25f0a04SGreg Roach        }
1282a25f0a04SGreg Roach
12837b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12847b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1285c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12867b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1287c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1288a25f0a04SGreg Roach        }
1289a25f0a04SGreg Roach
1290a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1291a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1292a25f0a04SGreg Roach        // $full is for display on-screen
1293a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1294a25f0a04SGreg Roach
1295a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1296ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1297ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1298c6f196c3SGreg Roach        // Format for display
1299d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1300acc34ea1SGreg Roach        // Localise quotation marks around the nickname
13010b5fd0a6SGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', static function (array $matches): string {
13028d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
13038d68cabeSGreg Roach        }, $full);
1304a25f0a04SGreg Roach
1305c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1306a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1307a25f0a04SGreg Roach
1308a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1309a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1310a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1311a25f0a04SGreg Roach
1312ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1313a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1314e364afe4SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) {
1315a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1316e364afe4SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) {
1317a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1318a25f0a04SGreg Roach            }
1319a25f0a04SGreg Roach
1320bdb3725aSGreg Roach            $this->getAllNames[] = [
1321a25f0a04SGreg Roach                'type'    => $type,
1322a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1323c1010edaSGreg Roach                'full'    => $full,
1324c1010edaSGreg Roach                // This is used for display
1325c1010edaSGreg Roach                'fullNN'  => $fullNN,
1326c1010edaSGreg Roach                // This goes into the database
1327c1010edaSGreg Roach                'surname' => $surname,
1328c1010edaSGreg Roach                // This goes into the database
1329c1010edaSGreg Roach                'givn'    => $GIVN,
1330c1010edaSGreg Roach                // This goes into the database
1331c1010edaSGreg Roach                'surn'    => $SURN,
1332c1010edaSGreg Roach                // This goes into the database
133313abd6f3SGreg Roach            ];
1334a25f0a04SGreg Roach        }
1335a25f0a04SGreg Roach    }
1336a25f0a04SGreg Roach
1337a25f0a04SGreg Roach    /**
133876692c8bSGreg Roach     * Extract names from the GEDCOM record.
1339c7ff4153SGreg Roach     *
1340c7ff4153SGreg Roach     * @return void
1341a25f0a04SGreg Roach     */
1342e364afe4SGreg Roach    public function extractNames(): void
1343c1010edaSGreg Roach    {
13448f53f488SRico Sonntag        $this->extractNamesFromFacts(
13458f53f488SRico Sonntag            1,
13468f53f488SRico Sonntag            'NAME',
134730158ae7SGreg Roach            $this->facts(
13488d0ebef0SGreg Roach                ['NAME'],
13498f53f488SRico Sonntag                false,
13508f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13518f53f488SRico Sonntag                $this->canShowName()
13528f53f488SRico Sonntag            )
13538f53f488SRico Sonntag        );
1354a25f0a04SGreg Roach    }
1355a25f0a04SGreg Roach
1356a25f0a04SGreg Roach    /**
1357a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1358a25f0a04SGreg Roach     * selection items or favorites.
1359a25f0a04SGreg Roach     *
1360a25f0a04SGreg Roach     * @return string
1361a25f0a04SGreg Roach     */
13628f53f488SRico Sonntag    public function formatListDetails(): string
1363c1010edaSGreg Roach    {
1364a25f0a04SGreg Roach        return
13658d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13668d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1367a25f0a04SGreg Roach    }
1368a25f0a04SGreg Roach}
1369