xref: /webtrees/app/Individual.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg 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
20a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
222e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
23a25f0a04SGreg Roach
24a25f0a04SGreg Roach/**
2576692c8bSGreg Roach * A GEDCOM individual (INDI) object.
26a25f0a04SGreg Roach */
27c1010edaSGreg Roachclass Individual extends GedcomRecord
28c1010edaSGreg Roach{
29a25f0a04SGreg Roach    const RECORD_TYPE = 'INDI';
30225e381fSGreg Roach    const ROUTE_NAME  = 'individual';
31a25f0a04SGreg Roach
3276692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3376692c8bSGreg Roach    public $generation;
34a25f0a04SGreg Roach
35a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
364686330aSGreg Roach    private $estimated_birth_date;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /** @var Date The estimated date of death */
394686330aSGreg Roach    private $estimated_death_date;
40a25f0a04SGreg Roach
41a25f0a04SGreg Roach    /**
42e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
43e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
44e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
45e71ef9d2SGreg Roach     *
46e71ef9d2SGreg Roach     * @param string      $xref
47e71ef9d2SGreg Roach     * @param Tree        $tree
48e71ef9d2SGreg Roach     * @param string|null $gedcom
49e71ef9d2SGreg Roach     *
50e71ef9d2SGreg Roach     * @throws \Exception
51e71ef9d2SGreg Roach     * @return Individual|null
52e71ef9d2SGreg Roach     */
5376f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
54c1010edaSGreg Roach    {
55e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
56e71ef9d2SGreg Roach
57e71ef9d2SGreg Roach        if ($record instanceof Individual) {
58e71ef9d2SGreg Roach            return $record;
59e71ef9d2SGreg Roach        }
60b2ce94c6SRico Sonntag
61b2ce94c6SRico Sonntag        return null;
62e71ef9d2SGreg Roach    }
63e71ef9d2SGreg Roach
64e71ef9d2SGreg Roach    /**
65395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
66395f0fe0SGreg Roach     * Typically when we load families and their members.
67395f0fe0SGreg Roach     *
68395f0fe0SGreg Roach     * @param Tree     $tree
694a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
7018d7a90dSGreg Roach     *
7118d7a90dSGreg Roach     * @return void
72395f0fe0SGreg Roach     */
732e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
74c1010edaSGreg Roach    {
752e5b4452SGreg Roach        $rows = DB::table('individuals')
762e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
772e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
782e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
792e5b4452SGreg Roach            ->get();
80395f0fe0SGreg Roach
81395f0fe0SGreg Roach        foreach ($rows as $row) {
82395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
83395f0fe0SGreg Roach        }
84395f0fe0SGreg Roach    }
85395f0fe0SGreg Roach
86395f0fe0SGreg Roach    /**
87a25f0a04SGreg Roach     * Can the name of this record be shown?
88a25f0a04SGreg Roach     *
8976692c8bSGreg Roach     * @param int|null $access_level
9076692c8bSGreg Roach     *
9176692c8bSGreg Roach     * @return bool
92a25f0a04SGreg Roach     */
9335584196SGreg Roach    public function canShowName(int $access_level = null): bool
94c1010edaSGreg Roach    {
954b9ff166SGreg Roach        if ($access_level === null) {
964b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
974b9ff166SGreg Roach        }
984b9ff166SGreg Roach
99518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
100a25f0a04SGreg Roach    }
101a25f0a04SGreg Roach
102a25f0a04SGreg Roach    /**
10376692c8bSGreg Roach     * Can this individual be shown?
104a25f0a04SGreg Roach     *
10576692c8bSGreg Roach     * @param int $access_level
10676692c8bSGreg Roach     *
10776692c8bSGreg Roach     * @return bool
108a25f0a04SGreg Roach     */
10935584196SGreg Roach    protected function canShowByType(int $access_level): bool
110c1010edaSGreg Roach    {
111a25f0a04SGreg Roach        // Dead people...
112518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
113a25f0a04SGreg Roach            $keep_alive             = false;
11454ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
115a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1168d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
117a25f0a04SGreg Roach                foreach ($matches as $match) {
118a25f0a04SGreg Roach                    $date = new Date($match[1]);
119a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
120a25f0a04SGreg Roach                        $keep_alive = true;
121a25f0a04SGreg Roach                        break;
122a25f0a04SGreg Roach                    }
123a25f0a04SGreg Roach                }
124a25f0a04SGreg Roach            }
12554ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
126a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1278d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
128a25f0a04SGreg Roach                foreach ($matches as $match) {
129a25f0a04SGreg Roach                    $date = new Date($match[1]);
130a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
131a25f0a04SGreg Roach                        $keep_alive = true;
132a25f0a04SGreg Roach                        break;
133a25f0a04SGreg Roach                    }
134a25f0a04SGreg Roach                }
135a25f0a04SGreg Roach            }
136a25f0a04SGreg Roach            if (!$keep_alive) {
137a25f0a04SGreg Roach                return true;
138a25f0a04SGreg Roach            }
139a25f0a04SGreg Roach        }
140a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
14154ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1424b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
14354ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1444b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
145a25f0a04SGreg Roach        }
146a25f0a04SGreg Roach
147a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1484b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
149a25f0a04SGreg Roach    }
150a25f0a04SGreg Roach
151a25f0a04SGreg Roach    /**
152a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
153a25f0a04SGreg Roach     *
154a25f0a04SGreg Roach     * @param Individual $target
155cbc1590aSGreg Roach     * @param int        $distance
156a25f0a04SGreg Roach     *
157cbc1590aSGreg Roach     * @return bool
158a25f0a04SGreg Roach     */
1598f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
160c1010edaSGreg Roach    {
161a25f0a04SGreg Roach        static $cache = null;
162a25f0a04SGreg Roach
16306ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
164a25f0a04SGreg Roach        if ($user_individual) {
165a25f0a04SGreg Roach            if (!$cache) {
16613abd6f3SGreg Roach                $cache = [
16713abd6f3SGreg Roach                    0 => [$user_individual],
16813abd6f3SGreg Roach                    1 => [],
16913abd6f3SGreg Roach                ];
1708d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
171dc124885SGreg Roach                    $family = $fact->target();
172e24444eeSGreg Roach                    if ($family instanceof Family) {
173a25f0a04SGreg Roach                        $cache[1][] = $family;
174a25f0a04SGreg Roach                    }
175a25f0a04SGreg Roach                }
176a25f0a04SGreg Roach            }
177a25f0a04SGreg Roach        } else {
178a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
179a25f0a04SGreg Roach            return true;
180a25f0a04SGreg Roach        }
181a25f0a04SGreg Roach
182a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
183a25f0a04SGreg Roach        $distance *= 2;
184a25f0a04SGreg Roach
185a25f0a04SGreg Roach        // Consider each path length in turn
186a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
187a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
188a25f0a04SGreg Roach                // We have already calculated all records with this length
189a25f0a04SGreg Roach                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
190a25f0a04SGreg Roach                    return true;
191a25f0a04SGreg Roach                }
192a25f0a04SGreg Roach            } else {
193a25f0a04SGreg Roach                // Need to calculate these paths
19413abd6f3SGreg Roach                $cache[$n] = [];
195a25f0a04SGreg Roach                if ($n % 2 == 0) {
196a25f0a04SGreg Roach                    // Add FAM->INDI links
197a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
1988d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
199dc124885SGreg Roach                            $individual = $fact->target();
200a25f0a04SGreg Roach                            // Don’t backtrack
201e24444eeSGreg Roach                            if ($individual instanceof Individual && !in_array($individual, $cache[$n - 2], true)) {
202a25f0a04SGreg Roach                                $cache[$n][] = $individual;
203a25f0a04SGreg Roach                            }
204a25f0a04SGreg Roach                        }
205a25f0a04SGreg Roach                    }
206a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
207a25f0a04SGreg Roach                        return true;
208a25f0a04SGreg Roach                    }
209a25f0a04SGreg Roach                } else {
210a25f0a04SGreg Roach                    // Add INDI->FAM links
211a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2128d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
213dc124885SGreg Roach                            $family = $fact->target();
214a25f0a04SGreg Roach                            // Don’t backtrack
215e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
216a25f0a04SGreg Roach                                $cache[$n][] = $family;
217a25f0a04SGreg Roach                            }
218a25f0a04SGreg Roach                        }
219a25f0a04SGreg Roach                    }
220a25f0a04SGreg Roach                }
221a25f0a04SGreg Roach            }
222a25f0a04SGreg Roach        }
223a25f0a04SGreg Roach
224a25f0a04SGreg Roach        return false;
225a25f0a04SGreg Roach    }
226a25f0a04SGreg Roach
22776692c8bSGreg Roach    /**
22876692c8bSGreg Roach     * Generate a private version of this record
22976692c8bSGreg Roach     *
23076692c8bSGreg Roach     * @param int $access_level
23176692c8bSGreg Roach     *
23276692c8bSGreg Roach     * @return string
23376692c8bSGreg Roach     */
2343c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
235c1010edaSGreg Roach    {
236acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
237a25f0a04SGreg Roach
238a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
239518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
240a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2418d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
242138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
243a25f0a04SGreg Roach            }
244a25f0a04SGreg Roach        }
245a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2468d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
247a25f0a04SGreg Roach        foreach ($matches as $match) {
24824ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
249a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
250a25f0a04SGreg Roach                $rec .= $match[0];
251a25f0a04SGreg Roach            }
252a25f0a04SGreg Roach        }
253a25f0a04SGreg Roach        // Don’t privatize sex.
254a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
255a25f0a04SGreg Roach            $rec .= $match[0];
256a25f0a04SGreg Roach        }
257a25f0a04SGreg Roach
258a25f0a04SGreg Roach        return $rec;
259a25f0a04SGreg Roach    }
260a25f0a04SGreg Roach
26176692c8bSGreg Roach    /**
26276692c8bSGreg Roach     * Fetch data from the database
26376692c8bSGreg Roach     *
26476692c8bSGreg Roach     * @param string $xref
26576692c8bSGreg Roach     * @param int    $tree_id
26676692c8bSGreg Roach     *
26776692c8bSGreg Roach     * @return null|string
26876692c8bSGreg Roach     */
26925e95e6eSGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
270c1010edaSGreg Roach    {
2712e5b4452SGreg Roach        return DB::table('individuals')
2722e5b4452SGreg Roach            ->where('i_id', '=', $xref)
2732e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
2742e5b4452SGreg Roach            ->value('i_gedcom');
275a25f0a04SGreg Roach    }
276a25f0a04SGreg Roach
277a25f0a04SGreg Roach    /**
278a25f0a04SGreg Roach     * Static helper function to sort an array of people by birth date
279a25f0a04SGreg Roach     *
280a25f0a04SGreg Roach     * @param Individual $x
281a25f0a04SGreg Roach     * @param Individual $y
282a25f0a04SGreg Roach     *
283cbc1590aSGreg Roach     * @return int
284a25f0a04SGreg Roach     */
2858f53f488SRico Sonntag    public static function compareBirthDate(Individual $x, Individual $y): int
286c1010edaSGreg Roach    {
287f5b60decSGreg Roach        return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
288a25f0a04SGreg Roach    }
289a25f0a04SGreg Roach
290a25f0a04SGreg Roach    /**
291a25f0a04SGreg Roach     * Static helper function to sort an array of people by death date
292a25f0a04SGreg Roach     *
293a25f0a04SGreg Roach     * @param Individual $x
294a25f0a04SGreg Roach     * @param Individual $y
295a25f0a04SGreg Roach     *
296cbc1590aSGreg Roach     * @return int
297a25f0a04SGreg Roach     */
2988f53f488SRico Sonntag    public static function compareDeathDate(Individual $x, Individual $y): int
299c1010edaSGreg Roach    {
300f5b60decSGreg Roach        return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
301a25f0a04SGreg Roach    }
302a25f0a04SGreg Roach
303a25f0a04SGreg Roach    /**
304a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
305a25f0a04SGreg Roach     * If not known to be dead, then assume living.
306a25f0a04SGreg Roach     *
307cbc1590aSGreg Roach     * @return bool
308a25f0a04SGreg Roach     */
3098f53f488SRico Sonntag    public function isDead(): bool
310c1010edaSGreg Roach    {
311c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
312a25f0a04SGreg Roach
313a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3148d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
315a25f0a04SGreg Roach            return true;
316a25f0a04SGreg Roach        }
317a25f0a04SGreg Roach
318a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
319a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
320a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
321a25f0a04SGreg Roach                $date = new Date($date_match);
322f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
323a25f0a04SGreg Roach                    return true;
324a25f0a04SGreg Roach                }
325a25f0a04SGreg Roach            }
326a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
327a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
328a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
329a25f0a04SGreg Roach                return false;
330a25f0a04SGreg Roach            }
331a25f0a04SGreg Roach        }
332a25f0a04SGreg Roach
333a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
334a25f0a04SGreg Roach
335a25f0a04SGreg Roach        // Check parents (birth and adopted)
3364b9ff166SGreg Roach        foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3374b9ff166SGreg Roach            foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
338a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
339a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
340a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
341a25f0a04SGreg Roach                    $date = new Date($date_match);
342f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
343a25f0a04SGreg Roach                        return true;
344a25f0a04SGreg Roach                    }
345a25f0a04SGreg Roach                }
346a25f0a04SGreg Roach            }
347a25f0a04SGreg Roach        }
348a25f0a04SGreg Roach
349a25f0a04SGreg Roach        // Check spouses
3504b9ff166SGreg Roach        foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
351a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
352a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
353a25f0a04SGreg Roach                $date = new Date($date_match);
354a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
355f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
356a25f0a04SGreg Roach                    return true;
357a25f0a04SGreg Roach                }
358a25f0a04SGreg Roach            }
359a25f0a04SGreg Roach            // Check spouse dates
36013e3123bSGreg Roach            $spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
361a25f0a04SGreg Roach            if ($spouse) {
362a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
363a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
364a25f0a04SGreg Roach                    $date = new Date($date_match);
365a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
366f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
367a25f0a04SGreg Roach                        return true;
368a25f0a04SGreg Roach                    }
369a25f0a04SGreg Roach                }
370a25f0a04SGreg Roach            }
371a25f0a04SGreg Roach            // Check child dates
3724b9ff166SGreg Roach            foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
373a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
374a25f0a04SGreg Roach                // Assume children born after age of 15
375a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
376a25f0a04SGreg Roach                    $date = new Date($date_match);
377f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
378a25f0a04SGreg Roach                        return true;
379a25f0a04SGreg Roach                    }
380a25f0a04SGreg Roach                }
381a25f0a04SGreg Roach                // Check grandchildren
3824b9ff166SGreg Roach                foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
3834b9ff166SGreg Roach                    foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
384a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
385a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
386a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
387a25f0a04SGreg Roach                            $date = new Date($date_match);
388f5b60decSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
389a25f0a04SGreg Roach                                return true;
390a25f0a04SGreg Roach                            }
391a25f0a04SGreg Roach                        }
392a25f0a04SGreg Roach                    }
393a25f0a04SGreg Roach                }
394a25f0a04SGreg Roach            }
395a25f0a04SGreg Roach        }
396a25f0a04SGreg Roach
397a25f0a04SGreg Roach        return false;
398a25f0a04SGreg Roach    }
399a25f0a04SGreg Roach
400a25f0a04SGreg Roach    /**
401a25f0a04SGreg Roach     * Find the highlighted media object for an individual
402a25f0a04SGreg Roach     *
4034a9f750fSGreg Roach     * @return null|MediaFile
404a25f0a04SGreg Roach     */
405c1010edaSGreg Roach    public function findHighlightedMediaFile()
406c1010edaSGreg Roach    {
4078d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
408dc124885SGreg Roach            $media = $fact->target();
409a213a63cSGreg Roach            if ($media instanceof Media) {
4104a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
411a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4124a9f750fSGreg Roach                        return $media_file;
4134a9f750fSGreg Roach                    }
4144a9f750fSGreg Roach                }
415a25f0a04SGreg Roach            }
416a25f0a04SGreg Roach        }
417a25f0a04SGreg Roach
418a25f0a04SGreg Roach        return null;
419a25f0a04SGreg Roach    }
420a25f0a04SGreg Roach
421a25f0a04SGreg Roach    /**
422a25f0a04SGreg Roach     * Display the prefered image for this individual.
423a25f0a04SGreg Roach     * Use an icon if no image is available.
424a25f0a04SGreg Roach     *
425dce07401SGreg Roach     * @param int      $width      Pixels
426dce07401SGreg Roach     * @param int      $height     Pixels
427dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
428dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
429dce07401SGreg Roach     *
430a25f0a04SGreg Roach     * @return string
431a25f0a04SGreg Roach     */
4328f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
433c1010edaSGreg Roach    {
4344a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4354a9f750fSGreg Roach
4364a9f750fSGreg Roach        if ($media_file !== null) {
4374a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
438a25f0a04SGreg Roach        }
4394a9f750fSGreg Roach
4404a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
4414a9f750fSGreg Roach            return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
4424a9f750fSGreg Roach        }
4434a9f750fSGreg Roach
4444a9f750fSGreg Roach        return '';
445a25f0a04SGreg Roach    }
446a25f0a04SGreg Roach
447a25f0a04SGreg Roach    /**
448a25f0a04SGreg Roach     * Get the date of birth
449a25f0a04SGreg Roach     *
450a25f0a04SGreg Roach     * @return Date
451a25f0a04SGreg Roach     */
4528f53f488SRico Sonntag    public function getBirthDate(): Date
453c1010edaSGreg Roach    {
454a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
455a25f0a04SGreg Roach            if ($date->isOK()) {
456a25f0a04SGreg Roach                return $date;
457a25f0a04SGreg Roach            }
458a25f0a04SGreg Roach        }
459a25f0a04SGreg Roach
460a25f0a04SGreg Roach        return new Date('');
461a25f0a04SGreg Roach    }
462a25f0a04SGreg Roach
463a25f0a04SGreg Roach    /**
464a25f0a04SGreg Roach     * Get the place of birth
465a25f0a04SGreg Roach     *
46616d0b7f7SRico Sonntag     * @return Place
467a25f0a04SGreg Roach     */
4688f53f488SRico Sonntag    public function getBirthPlace(): Place
469c1010edaSGreg Roach    {
470a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
471a25f0a04SGreg Roach            return $place;
472a25f0a04SGreg Roach        }
473a25f0a04SGreg Roach
474b20ddbf9SGreg Roach        return new Place('', $this->tree);
475a25f0a04SGreg Roach    }
476a25f0a04SGreg Roach
477a25f0a04SGreg Roach    /**
478a25f0a04SGreg Roach     * Get the year of birth
479a25f0a04SGreg Roach     *
480a25f0a04SGreg Roach     * @return string the year of birth
481a25f0a04SGreg Roach     */
4828f53f488SRico Sonntag    public function getBirthYear(): string
483c1010edaSGreg Roach    {
484f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
485a25f0a04SGreg Roach    }
486a25f0a04SGreg Roach
487a25f0a04SGreg Roach    /**
488a25f0a04SGreg Roach     * Get the date of death
489a25f0a04SGreg Roach     *
490a25f0a04SGreg Roach     * @return Date
491a25f0a04SGreg Roach     */
4928f53f488SRico Sonntag    public function getDeathDate(): Date
493c1010edaSGreg Roach    {
494a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
495a25f0a04SGreg Roach            if ($date->isOK()) {
496a25f0a04SGreg Roach                return $date;
497a25f0a04SGreg Roach            }
498a25f0a04SGreg Roach        }
499a25f0a04SGreg Roach
500a25f0a04SGreg Roach        return new Date('');
501a25f0a04SGreg Roach    }
502a25f0a04SGreg Roach
503a25f0a04SGreg Roach    /**
504a25f0a04SGreg Roach     * Get the place of death
505a25f0a04SGreg Roach     *
50616d0b7f7SRico Sonntag     * @return Place
507a25f0a04SGreg Roach     */
5088f53f488SRico Sonntag    public function getDeathPlace(): Place
509c1010edaSGreg Roach    {
510a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
511a25f0a04SGreg Roach            return $place;
512a25f0a04SGreg Roach        }
513a25f0a04SGreg Roach
514b20ddbf9SGreg Roach        return new Place('', $this->tree);
515a25f0a04SGreg Roach    }
516a25f0a04SGreg Roach
517a25f0a04SGreg Roach    /**
518a25f0a04SGreg Roach     * get the death year
519a25f0a04SGreg Roach     *
520a25f0a04SGreg Roach     * @return string the year of death
521a25f0a04SGreg Roach     */
5228f53f488SRico Sonntag    public function getDeathYear(): string
523c1010edaSGreg Roach    {
524f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
525a25f0a04SGreg Roach    }
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach    /**
528a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
52915d603e7SGreg Roach     * Provide the place and full date using a tooltip.
530a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
531a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
532a25f0a04SGreg Roach     *
533a25f0a04SGreg Roach     * @return string
534a25f0a04SGreg Roach     */
5358f53f488SRico Sonntag    public function getLifeSpan(): string
536c1010edaSGreg Roach    {
53715d603e7SGreg Roach        // Just the first part of the place name
5381e9c2c49SGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
5391e9c2c49SGreg Roach        $death_place = strip_tags($this->getDeathPlace()->getShortName());
54015d603e7SGreg Roach        // Remove markup from dates
54115d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
54215d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
54315d603e7SGreg Roach
544c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
545bbb76c12SGreg Roach        return
546c1010edaSGreg Roach            I18N::translate(
547a25f0a04SGreg Roach                '%1$s–%2$s',
5482d4c1bedSGreg Roach                '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5492d4c1bedSGreg Roach                '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
550a25f0a04SGreg Roach            );
551a25f0a04SGreg Roach    }
552a25f0a04SGreg Roach
553a25f0a04SGreg Roach    /**
554a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
555a25f0a04SGreg Roach     *
556a25f0a04SGreg Roach     * @return Date[]
557a25f0a04SGreg Roach     */
5588f53f488SRico Sonntag    public function getAllBirthDates(): array
559c1010edaSGreg Roach    {
5608d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5618d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
562a25f0a04SGreg Roach            if ($tmp) {
563a25f0a04SGreg Roach                return $tmp;
564a25f0a04SGreg Roach            }
565a25f0a04SGreg Roach        }
566a25f0a04SGreg Roach
56713abd6f3SGreg Roach        return [];
568a25f0a04SGreg Roach    }
569a25f0a04SGreg Roach
570a25f0a04SGreg Roach    /**
571a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
572a25f0a04SGreg Roach     *
5734080d558SGreg Roach     * @return Place[]
574a25f0a04SGreg Roach     */
5758f53f488SRico Sonntag    public function getAllBirthPlaces(): array
576c1010edaSGreg Roach    {
5778d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5788d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
5794080d558SGreg Roach            if (!empty($places)) {
5804080d558SGreg Roach                return $places;
581a25f0a04SGreg Roach            }
582a25f0a04SGreg Roach        }
583a25f0a04SGreg Roach
58413abd6f3SGreg Roach        return [];
585a25f0a04SGreg Roach    }
586a25f0a04SGreg Roach
587a25f0a04SGreg Roach    /**
588a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
589a25f0a04SGreg Roach     *
590a25f0a04SGreg Roach     * @return Date[]
591a25f0a04SGreg Roach     */
5928f53f488SRico Sonntag    public function getAllDeathDates(): array
593c1010edaSGreg Roach    {
5948d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
5958d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
596a25f0a04SGreg Roach            if ($tmp) {
597a25f0a04SGreg Roach                return $tmp;
598a25f0a04SGreg Roach            }
599a25f0a04SGreg Roach        }
600a25f0a04SGreg Roach
60113abd6f3SGreg Roach        return [];
602a25f0a04SGreg Roach    }
603a25f0a04SGreg Roach
604a25f0a04SGreg Roach    /**
605a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
606a25f0a04SGreg Roach     *
6074080d558SGreg Roach     * @return Place[]
608a25f0a04SGreg Roach     */
6098f53f488SRico Sonntag    public function getAllDeathPlaces(): array
610c1010edaSGreg Roach    {
6118d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6128d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6134080d558SGreg Roach            if (!empty($places)) {
6144080d558SGreg Roach                return $places;
615a25f0a04SGreg Roach            }
616a25f0a04SGreg Roach        }
617a25f0a04SGreg Roach
61813abd6f3SGreg Roach        return [];
619a25f0a04SGreg Roach    }
620a25f0a04SGreg Roach
621a25f0a04SGreg Roach    /**
622a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
623a25f0a04SGreg Roach     *
624a25f0a04SGreg Roach     * @return Date
625a25f0a04SGreg Roach     */
6268f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
627c1010edaSGreg Roach    {
6288f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
629a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
630a25f0a04SGreg Roach                if ($date->isOK()) {
6314686330aSGreg Roach                    $this->estimated_birth_date = $date;
632a25f0a04SGreg Roach                    break;
633a25f0a04SGreg Roach                }
634a25f0a04SGreg Roach            }
6358f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
63613abd6f3SGreg Roach                $min = [];
63713abd6f3SGreg Roach                $max = [];
638a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
639f5b60decSGreg Roach                if ($tmp->isOK()) {
640f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
641f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
642a25f0a04SGreg Roach                }
643a25f0a04SGreg Roach                foreach ($this->getChildFamilies() as $family) {
644a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
645f5b60decSGreg Roach                    if ($tmp->isOK()) {
646f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
647f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
648a25f0a04SGreg Roach                    }
6492e5b4452SGreg Roach                    $husband = $family->getHusband();
6502e5b4452SGreg Roach                    if ($husband instanceof Individual) {
6512e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
652f5b60decSGreg Roach                        if ($tmp->isOK()) {
653f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
654f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
655a25f0a04SGreg Roach                        }
656a25f0a04SGreg Roach                    }
6572e5b4452SGreg Roach                    $wife = $family->getWife();
6582e5b4452SGreg Roach                    if ($wife instanceof Individual) {
6592e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
660f5b60decSGreg Roach                        if ($tmp->isOK()) {
661f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
662f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
663a25f0a04SGreg Roach                        }
664a25f0a04SGreg Roach                    }
665a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
666a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
667f5b60decSGreg Roach                        if ($tmp->isOK()) {
668f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
669f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
670a25f0a04SGreg Roach                        }
671a25f0a04SGreg Roach                    }
672a25f0a04SGreg Roach                }
673a25f0a04SGreg Roach                foreach ($this->getSpouseFamilies() as $family) {
674a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
675f5b60decSGreg Roach                    if ($tmp->isOK()) {
676f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
677f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
678a25f0a04SGreg Roach                    }
679a25f0a04SGreg Roach                    $spouse = $family->getSpouse($this);
680a25f0a04SGreg Roach                    if ($spouse) {
681a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
682f5b60decSGreg Roach                        if ($tmp->isOK()) {
683f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
684f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
685a25f0a04SGreg Roach                        }
686a25f0a04SGreg Roach                    }
687a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
688a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
689f5b60decSGreg Roach                        if ($tmp->isOK()) {
690f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
691f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
692a25f0a04SGreg Roach                        }
693a25f0a04SGreg Roach                    }
694a25f0a04SGreg Roach                }
695a25f0a04SGreg Roach                if ($min && $max) {
69659f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
697a25f0a04SGreg Roach
69865e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
6994686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
700a25f0a04SGreg Roach                } else {
7014686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
702a25f0a04SGreg Roach                }
703a25f0a04SGreg Roach            }
704a25f0a04SGreg Roach        }
705a25f0a04SGreg Roach
7064686330aSGreg Roach        return $this->estimated_birth_date;
707a25f0a04SGreg Roach    }
708a25f0a04SGreg Roach
709a25f0a04SGreg Roach    /**
710a25f0a04SGreg Roach     * Generate an estimated date of death.
711a25f0a04SGreg Roach     *
712a25f0a04SGreg Roach     * @return Date
713a25f0a04SGreg Roach     */
7148f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
715c1010edaSGreg Roach    {
7164686330aSGreg Roach        if ($this->estimated_death_date === null) {
717a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
718a25f0a04SGreg Roach                if ($date->isOK()) {
7194686330aSGreg Roach                    $this->estimated_death_date = $date;
720a25f0a04SGreg Roach                    break;
721a25f0a04SGreg Roach                }
722a25f0a04SGreg Roach            }
7234686330aSGreg Roach            if ($this->estimated_death_date === null) {
724f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
725c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7264686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
727a25f0a04SGreg Roach                } else {
7284686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
729a25f0a04SGreg Roach                }
730a25f0a04SGreg Roach            }
731a25f0a04SGreg Roach        }
732a25f0a04SGreg Roach
7334686330aSGreg Roach        return $this->estimated_death_date;
734a25f0a04SGreg Roach    }
735a25f0a04SGreg Roach
736a25f0a04SGreg Roach    /**
737a25f0a04SGreg Roach     * Get the sex - M F or U
738a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
739a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
740a25f0a04SGreg Roach     *
741a25f0a04SGreg Roach     * @return string
742a25f0a04SGreg Roach     */
743c1010edaSGreg Roach    public function getSex()
744c1010edaSGreg Roach    {
745a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
746a25f0a04SGreg Roach            return $match[1];
747a25f0a04SGreg Roach        }
748b2ce94c6SRico Sonntag
749b2ce94c6SRico Sonntag        return 'U';
750a25f0a04SGreg Roach    }
751a25f0a04SGreg Roach
752a25f0a04SGreg Roach    /**
753a25f0a04SGreg Roach     * Get the individual’s sex image
754a25f0a04SGreg Roach     *
755a25f0a04SGreg Roach     * @param string $size
756a25f0a04SGreg Roach     *
757a25f0a04SGreg Roach     * @return string
758a25f0a04SGreg Roach     */
7598f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
760c1010edaSGreg Roach    {
761a25f0a04SGreg Roach        return self::sexImage($this->getSex(), $size);
762a25f0a04SGreg Roach    }
763a25f0a04SGreg Roach
764a25f0a04SGreg Roach    /**
765a25f0a04SGreg Roach     * Generate a sex icon/image
766a25f0a04SGreg Roach     *
767a25f0a04SGreg Roach     * @param string $sex
768a25f0a04SGreg Roach     * @param string $size
769a25f0a04SGreg Roach     *
770a25f0a04SGreg Roach     * @return string
771a25f0a04SGreg Roach     */
7728f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
773c1010edaSGreg Roach    {
774a25f0a04SGreg Roach        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
775a25f0a04SGreg Roach    }
776a25f0a04SGreg Roach
777a25f0a04SGreg Roach    /**
778a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
779a25f0a04SGreg Roach     *
780a25f0a04SGreg Roach     * @return string
781a25f0a04SGreg Roach     */
7828f53f488SRico Sonntag    public function getBoxStyle(): string
783c1010edaSGreg Roach    {
784c1010edaSGreg Roach        $tmp = [
785c1010edaSGreg Roach            'M' => '',
786c1010edaSGreg Roach            'F' => 'F',
787c1010edaSGreg Roach            'U' => 'NN',
788c1010edaSGreg Roach        ];
789a25f0a04SGreg Roach
790a25f0a04SGreg Roach        return 'person_box' . $tmp[$this->getSex()];
791a25f0a04SGreg Roach    }
792a25f0a04SGreg Roach
793a25f0a04SGreg Roach    /**
794a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
795a25f0a04SGreg Roach     *
796cbc1590aSGreg Roach     * @param int|null $access_level
797a25f0a04SGreg Roach     *
798a25f0a04SGreg Roach     * @return Family[]
799a25f0a04SGreg Roach     */
8008f53f488SRico Sonntag    public function getSpouseFamilies($access_level = null): array
801c1010edaSGreg Roach    {
8024b9ff166SGreg Roach        if ($access_level === null) {
8034b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8044b9ff166SGreg Roach        }
8054b9ff166SGreg Roach
806acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
807a25f0a04SGreg Roach
80813abd6f3SGreg Roach        $families = [];
8098d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
810dc124885SGreg Roach            $family = $fact->target();
811e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
812a25f0a04SGreg Roach                $families[] = $family;
813a25f0a04SGreg Roach            }
814a25f0a04SGreg Roach        }
815a25f0a04SGreg Roach
816a25f0a04SGreg Roach        return $families;
817a25f0a04SGreg Roach    }
818a25f0a04SGreg Roach
819a25f0a04SGreg Roach    /**
820a25f0a04SGreg Roach     * Get the current spouse of this individual.
821a25f0a04SGreg Roach     *
822a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
823a25f0a04SGreg Roach     * in chronological order, and take the last one found.
824a25f0a04SGreg Roach     *
825a25f0a04SGreg Roach     * @return Individual|null
826a25f0a04SGreg Roach     */
827c1010edaSGreg Roach    public function getCurrentSpouse()
828c1010edaSGreg Roach    {
829a25f0a04SGreg Roach        $tmp    = $this->getSpouseFamilies();
830a25f0a04SGreg Roach        $family = end($tmp);
831a25f0a04SGreg Roach        if ($family) {
832a25f0a04SGreg Roach            return $family->getSpouse($this);
833a25f0a04SGreg Roach        }
834b2ce94c6SRico Sonntag
835b2ce94c6SRico Sonntag        return null;
836a25f0a04SGreg Roach    }
837a25f0a04SGreg Roach
838a25f0a04SGreg Roach    /**
839a25f0a04SGreg Roach     * Count the children belonging to this individual.
840a25f0a04SGreg Roach     *
841cbc1590aSGreg Roach     * @return int
842a25f0a04SGreg Roach     */
843c1010edaSGreg Roach    public function getNumberOfChildren()
844c1010edaSGreg Roach    {
8457d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8463dc7bbe9SGreg Roach            return (int) $match[1];
847b2ce94c6SRico Sonntag        }
848b2ce94c6SRico Sonntag
84913abd6f3SGreg Roach        $children = [];
850a25f0a04SGreg Roach        foreach ($this->getSpouseFamilies() as $fam) {
851a25f0a04SGreg Roach            foreach ($fam->getChildren() as $child) {
852c0935879SGreg Roach                $children[$child->xref()] = true;
853a25f0a04SGreg Roach            }
854a25f0a04SGreg Roach        }
855a25f0a04SGreg Roach
856a25f0a04SGreg Roach        return count($children);
857a25f0a04SGreg Roach    }
858a25f0a04SGreg Roach
859a25f0a04SGreg Roach    /**
860a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
861a25f0a04SGreg Roach     *
862cbc1590aSGreg Roach     * @param int|null $access_level
863a25f0a04SGreg Roach     *
864a25f0a04SGreg Roach     * @return Family[]
865a25f0a04SGreg Roach     */
8668f53f488SRico Sonntag    public function getChildFamilies($access_level = null): array
867c1010edaSGreg Roach    {
8684b9ff166SGreg Roach        if ($access_level === null) {
8694b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8704b9ff166SGreg Roach        }
8714b9ff166SGreg Roach
872acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
873a25f0a04SGreg Roach
87413abd6f3SGreg Roach        $families = [];
8758d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
876dc124885SGreg Roach            $family = $fact->target();
877e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
878a25f0a04SGreg Roach                $families[] = $family;
879a25f0a04SGreg Roach            }
880a25f0a04SGreg Roach        }
881a25f0a04SGreg Roach
882a25f0a04SGreg Roach        return $families;
883a25f0a04SGreg Roach    }
884a25f0a04SGreg Roach
885a25f0a04SGreg Roach    /**
886a25f0a04SGreg Roach     * Get the preferred parents for this individual.
887a25f0a04SGreg Roach     *
888a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
889a25f0a04SGreg Roach     * The preferred family record is:
890a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
891a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
892a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
893a25f0a04SGreg Roach     * (d) the first one found
894a25f0a04SGreg Roach     *
895a25f0a04SGreg Roach     * @return Family|null
896a25f0a04SGreg Roach     */
897c1010edaSGreg Roach    public function getPrimaryChildFamily()
898c1010edaSGreg Roach    {
899a25f0a04SGreg Roach        $families = $this->getChildFamilies();
900a25f0a04SGreg Roach        switch (count($families)) {
901a25f0a04SGreg Roach            case 0:
902a25f0a04SGreg Roach                return null;
903a25f0a04SGreg Roach            case 1:
90415d603e7SGreg Roach                return $families[0];
905a25f0a04SGreg Roach            default:
906a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
907a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
90874e78bfaSJonathan Jaubart                foreach ($families as $fam) {
909c0935879SGreg Roach                    $famid = $fam->xref();
9107d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
911a25f0a04SGreg Roach                        return $fam;
912a25f0a04SGreg Roach                    }
913a25f0a04SGreg Roach                }
914a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
91574e78bfaSJonathan Jaubart                foreach ($families as $fam) {
916c0935879SGreg Roach                    $famid = $fam->xref();
9177d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
918a25f0a04SGreg Roach                        return $fam;
919a25f0a04SGreg Roach                    }
920a25f0a04SGreg Roach                }
921a25f0a04SGreg Roach                // c) records with no '2 PEDI'
92274e78bfaSJonathan Jaubart                foreach ($families as $fam) {
923c0935879SGreg Roach                    $famid = $fam->xref();
9247d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
925a25f0a04SGreg Roach                        return $fam;
926a25f0a04SGreg Roach                    }
927a25f0a04SGreg Roach                }
928a25f0a04SGreg Roach
929a25f0a04SGreg Roach                // d) any record
93015d603e7SGreg Roach                return $families[0];
931a25f0a04SGreg Roach        }
932a25f0a04SGreg Roach    }
933a25f0a04SGreg Roach
934a25f0a04SGreg Roach    /**
935a25f0a04SGreg Roach     * Get a list of step-parent families.
936a25f0a04SGreg Roach     *
937a25f0a04SGreg Roach     * @return Family[]
938a25f0a04SGreg Roach     */
9398f53f488SRico Sonntag    public function getChildStepFamilies(): array
940c1010edaSGreg Roach    {
94113abd6f3SGreg Roach        $step_families = [];
942a25f0a04SGreg Roach        $families      = $this->getChildFamilies();
943a25f0a04SGreg Roach        foreach ($families as $family) {
944a25f0a04SGreg Roach            $father = $family->getHusband();
945a25f0a04SGreg Roach            if ($father) {
946a25f0a04SGreg Roach                foreach ($father->getSpouseFamilies() as $step_family) {
947a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
948a25f0a04SGreg Roach                        $step_families[] = $step_family;
949a25f0a04SGreg Roach                    }
950a25f0a04SGreg Roach                }
951a25f0a04SGreg Roach            }
952a25f0a04SGreg Roach            $mother = $family->getWife();
953a25f0a04SGreg Roach            if ($mother) {
954a25f0a04SGreg Roach                foreach ($mother->getSpouseFamilies() as $step_family) {
955a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
956a25f0a04SGreg Roach                        $step_families[] = $step_family;
957a25f0a04SGreg Roach                    }
958a25f0a04SGreg Roach                }
959a25f0a04SGreg Roach            }
960a25f0a04SGreg Roach        }
961a25f0a04SGreg Roach
962a25f0a04SGreg Roach        return $step_families;
963a25f0a04SGreg Roach    }
964a25f0a04SGreg Roach
965a25f0a04SGreg Roach    /**
966a25f0a04SGreg Roach     * Get a list of step-parent families.
967a25f0a04SGreg Roach     *
968a25f0a04SGreg Roach     * @return Family[]
969a25f0a04SGreg Roach     */
9708f53f488SRico Sonntag    public function getSpouseStepFamilies(): array
971c1010edaSGreg Roach    {
97213abd6f3SGreg Roach        $step_families = [];
973a25f0a04SGreg Roach        $families      = $this->getSpouseFamilies();
974a25f0a04SGreg Roach        foreach ($families as $family) {
975a25f0a04SGreg Roach            $spouse = $family->getSpouse($this);
976a25f0a04SGreg Roach            if ($spouse) {
977a25f0a04SGreg Roach                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
978a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
979a25f0a04SGreg Roach                        $step_families[] = $step_family;
980a25f0a04SGreg Roach                    }
981a25f0a04SGreg Roach                }
982a25f0a04SGreg Roach            }
983a25f0a04SGreg Roach        }
984a25f0a04SGreg Roach
985a25f0a04SGreg Roach        return $step_families;
986a25f0a04SGreg Roach    }
987a25f0a04SGreg Roach
988a25f0a04SGreg Roach    /**
989a25f0a04SGreg Roach     * A label for a parental family group
990a25f0a04SGreg Roach     *
991a25f0a04SGreg Roach     * @param Family $family
992a25f0a04SGreg Roach     *
993a25f0a04SGreg Roach     * @return string
994a25f0a04SGreg Roach     */
995c1010edaSGreg Roach    public function getChildFamilyLabel(Family $family)
996c1010edaSGreg Roach    {
9977d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
998a25f0a04SGreg Roach            // A specified pedigree
999764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1000b2ce94c6SRico Sonntag        }
1001b2ce94c6SRico Sonntag
1002a25f0a04SGreg Roach        // Default (birth) pedigree
1003764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1004a25f0a04SGreg Roach    }
1005a25f0a04SGreg Roach
1006a25f0a04SGreg Roach    /**
1007a25f0a04SGreg Roach     * Create a label for a step family
1008a25f0a04SGreg Roach     *
1009a25f0a04SGreg Roach     * @param Family $step_family
1010a25f0a04SGreg Roach     *
1011a25f0a04SGreg Roach     * @return string
1012a25f0a04SGreg Roach     */
10138f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1014c1010edaSGreg Roach    {
1015a25f0a04SGreg Roach        foreach ($this->getChildFamilies() as $family) {
1016a25f0a04SGreg Roach            if ($family !== $step_family) {
1017a25f0a04SGreg Roach                // Must be a step-family
1018a25f0a04SGreg Roach                foreach ($family->getSpouses() as $parent) {
1019a25f0a04SGreg Roach                    foreach ($step_family->getSpouses() as $step_parent) {
1020a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1021a25f0a04SGreg Roach                            // One common parent - must be a step family
1022a25f0a04SGreg Roach                            if ($parent->getSex() == 'M') {
1023a25f0a04SGreg Roach                                // Father’s family with someone else
1024a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1025a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1026bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1027b2ce94c6SRico Sonntag                                }
1028b2ce94c6SRico Sonntag
1029a25f0a04SGreg Roach                                /* I18N: A step-family. */
1030bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1031a25f0a04SGreg Roach                            }
1032b2ce94c6SRico Sonntag
1033a25f0a04SGreg Roach                            // Mother’s family with someone else
1034a25f0a04SGreg Roach                            if ($step_family->getSpouse($step_parent)) {
1035a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
1036bbb76c12SGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1037b2ce94c6SRico Sonntag                            }
1038b2ce94c6SRico Sonntag
1039a25f0a04SGreg Roach                            /* I18N: A step-family. */
1040bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1041a25f0a04SGreg Roach                        }
1042a25f0a04SGreg Roach                    }
1043a25f0a04SGreg Roach                }
1044a25f0a04SGreg Roach            }
1045a25f0a04SGreg Roach        }
1046a25f0a04SGreg Roach
1047a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1048a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1049a25f0a04SGreg Roach    }
1050a25f0a04SGreg Roach
1051225e381fSGreg Roach    /**
1052225e381fSGreg Roach     * Get the description for the family.
1053225e381fSGreg Roach     *
1054225e381fSGreg Roach     * For example, "XXX's family with new wife".
1055225e381fSGreg Roach     *
1056225e381fSGreg Roach     * @param Family $family
1057225e381fSGreg Roach     *
1058225e381fSGreg Roach     * @return string
1059225e381fSGreg Roach     */
1060c1010edaSGreg Roach    public function getSpouseFamilyLabel(Family $family)
1061c1010edaSGreg Roach    {
1062225e381fSGreg Roach        $spouse = $family->getSpouse($this);
1063225e381fSGreg Roach        if ($spouse) {
1064225e381fSGreg Roach            /* I18N: %s is the spouse name */
1065bbb76c12SGreg Roach            return I18N::translate('Family with %s', $spouse->getFullName());
1066225e381fSGreg Roach        }
1067b2ce94c6SRico Sonntag
1068b2ce94c6SRico Sonntag        return $family->getFullName();
1069225e381fSGreg Roach    }
1070225e381fSGreg Roach
1071a25f0a04SGreg Roach    /**
1072a25f0a04SGreg Roach     * get primary parents names for this individual
1073a25f0a04SGreg Roach     *
1074a25f0a04SGreg Roach     * @param string $classname optional css class
1075a25f0a04SGreg Roach     * @param string $display   optional css style display
1076a25f0a04SGreg Roach     *
1077a25f0a04SGreg Roach     * @return string a div block with father & mother names
1078a25f0a04SGreg Roach     */
10798f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1080c1010edaSGreg Roach    {
1081a25f0a04SGreg Roach        $fam = $this->getPrimaryChildFamily();
1082a25f0a04SGreg Roach        if (!$fam) {
1083a25f0a04SGreg Roach            return '';
1084a25f0a04SGreg Roach        }
1085a25f0a04SGreg Roach        $txt = '<div';
1086a25f0a04SGreg Roach        if ($classname) {
10874c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1088a25f0a04SGreg Roach        }
1089a25f0a04SGreg Roach        if ($display) {
10904c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1091a25f0a04SGreg Roach        }
1092a25f0a04SGreg Roach        $txt .= '>';
1093a25f0a04SGreg Roach        $husb = $fam->getHusband();
1094a25f0a04SGreg Roach        if ($husb) {
1095a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1096a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1097a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1098a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1099a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
1100bbb76c12SGreg Roach            $txt .= I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1101a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1102a25f0a04SGreg Roach        }
1103a25f0a04SGreg Roach        $wife = $fam->getWife();
1104a25f0a04SGreg Roach        if ($wife) {
1105a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1106a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1107a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1108a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1109a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
1110bbb76c12SGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->getFullName());
1111a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1112a25f0a04SGreg Roach        }
1113a25f0a04SGreg Roach        $txt .= '</div>';
1114a25f0a04SGreg Roach
1115a25f0a04SGreg Roach        return $txt;
1116a25f0a04SGreg Roach    }
1117a25f0a04SGreg Roach
1118a25f0a04SGreg Roach    /** {@inheritdoc} */
11198f53f488SRico Sonntag    public function getFallBackName(): string
1120c1010edaSGreg Roach    {
1121a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1122a25f0a04SGreg Roach    }
1123a25f0a04SGreg Roach
1124a25f0a04SGreg Roach    /**
1125a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1126a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1127a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1128a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1129a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1130a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1131a25f0a04SGreg Roach     *
1132a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1133a25f0a04SGreg Roach     * 2 GIVN Robert
1134a25f0a04SGreg Roach     * 2 SPFX de
1135a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1136a25f0a04SGreg Roach     * 2 NICK The Bald
1137a25f0a04SGreg Roach     *
1138a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1139a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1140a25f0a04SGreg Roach     *
1141a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1142a25f0a04SGreg Roach     *
1143a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1144a25f0a04SGreg Roach     * or
1145a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1146a25f0a04SGreg Roach     * 2 GIVN Carlos
1147a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1148a25f0a04SGreg Roach     *
1149a25f0a04SGreg Roach     * @param string $type
1150a25f0a04SGreg Roach     * @param string $full
1151a25f0a04SGreg Roach     * @param string $gedcom
1152a25f0a04SGreg Roach     */
115376f666f4SGreg Roach    protected function addName(string $type, string $full, string $gedcom)
1154c1010edaSGreg Roach    {
1155a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1156a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1157a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1158a25f0a04SGreg Roach
115976f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1160a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1161a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1162a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1163a25f0a04SGreg Roach
1164a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
116576f666f4SGreg Roach        if ($SURN !== '') {
1166a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1167a25f0a04SGreg Roach        } else {
116813abd6f3SGreg Roach            $SURNS = [];
1169a25f0a04SGreg Roach        }
117076f666f4SGreg Roach
1171a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1172a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1173a25f0a04SGreg Roach
1174a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1175a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1176a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1177a25f0a04SGreg Roach
1178a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
117976f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1180a25f0a04SGreg Roach            $full = $full . '/';
1181a25f0a04SGreg Roach        }
1182a25f0a04SGreg Roach
1183a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1184a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1185a25f0a04SGreg Roach
1186a25f0a04SGreg Roach        // Extract the surname.
1187a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1188a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1189a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1190a25f0a04SGreg Roach        } else {
1191a25f0a04SGreg Roach            $surname = '';
1192a25f0a04SGreg Roach        }
1193a25f0a04SGreg Roach
1194a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1195a25f0a04SGreg Roach        if (!$SURNS) {
1196a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1197a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1198a25f0a04SGreg Roach                $SURNS = $matches[1];
1199a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1200a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1201a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1202a25f0a04SGreg Roach                }
1203a25f0a04SGreg Roach            } else {
1204a25f0a04SGreg Roach                // It is valid not to have a surname at all
120513abd6f3SGreg Roach                $SURNS = [''];
1206a25f0a04SGreg Roach            }
1207a25f0a04SGreg Roach        }
1208a25f0a04SGreg Roach
1209a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1210a25f0a04SGreg Roach        if (!$GIVN) {
1211a25f0a04SGreg Roach            $GIVN = preg_replace(
121213abd6f3SGreg Roach                [
1213c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1214c1010edaSGreg Roach                    // remove surname
1215c1010edaSGreg Roach                    '/ ?".+"/',
1216c1010edaSGreg Roach                    // remove nickname
1217c1010edaSGreg Roach                    '/ {2,}/',
1218c1010edaSGreg Roach                    // multiple spaces, caused by the above
1219c1010edaSGreg Roach                    '/^ | $/',
1220c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
122113abd6f3SGreg Roach                ],
122213abd6f3SGreg Roach                [
1223a25f0a04SGreg Roach                    ' ',
1224a25f0a04SGreg Roach                    ' ',
1225a25f0a04SGreg Roach                    ' ',
1226a25f0a04SGreg Roach                    '',
122713abd6f3SGreg Roach                ],
1228a25f0a04SGreg Roach                $full
1229a25f0a04SGreg Roach            );
1230a25f0a04SGreg Roach        }
1231a25f0a04SGreg Roach
1232a25f0a04SGreg Roach        // Add placeholder for unknown given name
1233a25f0a04SGreg Roach        if (!$GIVN) {
1234a25f0a04SGreg Roach            $GIVN = '@P.N.';
123573f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1236a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1237a25f0a04SGreg Roach        }
1238a25f0a04SGreg Roach
12397b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12407b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1241c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12427b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1243c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1244a25f0a04SGreg Roach        }
1245a25f0a04SGreg Roach
1246a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1247a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1248a25f0a04SGreg Roach        // $full is for display on-screen
1249a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1250a25f0a04SGreg Roach
1251a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1252ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1253ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1254c6f196c3SGreg Roach        // Format for display
1255d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1256acc34ea1SGreg Roach        // Localise quotation marks around the nickname
125718d7a90dSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function (array $matches): string {
12588d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12598d68cabeSGreg Roach        }, $full);
1260a25f0a04SGreg Roach
1261c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1262a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1263a25f0a04SGreg Roach
1264a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1265a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1266a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1267a25f0a04SGreg Roach
1268ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1269a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1270a25f0a04SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1271a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1272a25f0a04SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1273a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1274a25f0a04SGreg Roach            }
1275a25f0a04SGreg Roach
1276bdb3725aSGreg Roach            $this->getAllNames[] = [
1277a25f0a04SGreg Roach                'type'    => $type,
1278a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1279c1010edaSGreg Roach                'full'    => $full,
1280c1010edaSGreg Roach                // This is used for display
1281c1010edaSGreg Roach                'fullNN'  => $fullNN,
1282c1010edaSGreg Roach                // This goes into the database
1283c1010edaSGreg Roach                'surname' => $surname,
1284c1010edaSGreg Roach                // This goes into the database
1285c1010edaSGreg Roach                'givn'    => $GIVN,
1286c1010edaSGreg Roach                // This goes into the database
1287c1010edaSGreg Roach                'surn'    => $SURN,
1288c1010edaSGreg Roach                // This goes into the database
128913abd6f3SGreg Roach            ];
1290a25f0a04SGreg Roach        }
1291a25f0a04SGreg Roach    }
1292a25f0a04SGreg Roach
1293a25f0a04SGreg Roach    /**
129476692c8bSGreg Roach     * Extract names from the GEDCOM record.
1295c7ff4153SGreg Roach     *
1296c7ff4153SGreg Roach     * @return void
1297a25f0a04SGreg Roach     */
1298c1010edaSGreg Roach    public function extractNames()
1299c1010edaSGreg Roach    {
13008f53f488SRico Sonntag        $this->extractNamesFromFacts(
13018f53f488SRico Sonntag            1,
13028f53f488SRico Sonntag            'NAME',
130330158ae7SGreg Roach            $this->facts(
13048d0ebef0SGreg Roach                ['NAME'],
13058f53f488SRico Sonntag                false,
13068f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13078f53f488SRico Sonntag                $this->canShowName()
13088f53f488SRico Sonntag            )
13098f53f488SRico Sonntag        );
1310a25f0a04SGreg Roach    }
1311a25f0a04SGreg Roach
1312a25f0a04SGreg Roach    /**
1313a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1314a25f0a04SGreg Roach     * selection items or favorites.
1315a25f0a04SGreg Roach     *
1316a25f0a04SGreg Roach     * @return string
1317a25f0a04SGreg Roach     */
13188f53f488SRico Sonntag    public function formatListDetails(): string
1319c1010edaSGreg Roach    {
1320a25f0a04SGreg Roach        return
13218d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13228d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1323a25f0a04SGreg Roach    }
1324a25f0a04SGreg Roach}
1325