xref: /webtrees/app/Individual.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
16*e7f56f2aSGreg Roachdeclare(strict_types=1);
17*e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
22a25f0a04SGreg Roach
23a25f0a04SGreg Roach/**
2476692c8bSGreg Roach * A GEDCOM individual (INDI) object.
25a25f0a04SGreg Roach */
26c1010edaSGreg Roachclass Individual extends GedcomRecord
27c1010edaSGreg Roach{
28a25f0a04SGreg Roach    const RECORD_TYPE = 'INDI';
29225e381fSGreg Roach    const ROUTE_NAME  = 'individual';
30a25f0a04SGreg Roach
3176692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3276692c8bSGreg Roach    public $generation;
33a25f0a04SGreg Roach
34a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
354686330aSGreg Roach    private $estimated_birth_date;
36a25f0a04SGreg Roach
37a25f0a04SGreg Roach    /** @var Date The estimated date of death */
384686330aSGreg Roach    private $estimated_death_date;
39a25f0a04SGreg Roach
40a25f0a04SGreg Roach    /**
41e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
42e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
43e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
44e71ef9d2SGreg Roach     *
45e71ef9d2SGreg Roach     * @param string      $xref
46e71ef9d2SGreg Roach     * @param Tree        $tree
47e71ef9d2SGreg Roach     * @param string|null $gedcom
48e71ef9d2SGreg Roach     *
49e71ef9d2SGreg Roach     * @throws \Exception
50e71ef9d2SGreg Roach     *
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     */
73c1010edaSGreg Roach    public static function load(Tree $tree, array $xrefs)
74c1010edaSGreg Roach    {
7513abd6f3SGreg Roach        $args         = [
76395f0fe0SGreg Roach            'tree_id' => $tree->getTreeId(),
7713abd6f3SGreg Roach        ];
7813abd6f3SGreg Roach        $placeholders = [];
79395f0fe0SGreg Roach
80395f0fe0SGreg Roach        foreach (array_unique($xrefs) as $n => $xref) {
81395f0fe0SGreg Roach            if (!isset(self::$gedcom_record_cache[$tree->getTreeId()][$xref])) {
82f7247c73SGreg Roach                $placeholders[] = ':x' . $n;
83395f0fe0SGreg Roach                $args['x' . $n] = $xref;
84395f0fe0SGreg Roach            }
85395f0fe0SGreg Roach        }
86395f0fe0SGreg Roach
87f7247c73SGreg Roach        if (!empty($placeholders)) {
88395f0fe0SGreg Roach            $rows = Database::prepare(
89395f0fe0SGreg Roach                "SELECT i_id AS xref, i_gedcom AS gedcom" .
90395f0fe0SGreg Roach                " FROM `##individuals`" .
91f7247c73SGreg Roach                " WHERE i_file = :tree_id AND i_id IN (" . implode(',', $placeholders) . ")"
92395f0fe0SGreg Roach            )->execute(
93395f0fe0SGreg Roach                $args
94395f0fe0SGreg Roach            )->fetchAll();
95395f0fe0SGreg Roach
96395f0fe0SGreg Roach            foreach ($rows as $row) {
97395f0fe0SGreg Roach                self::getInstance($row->xref, $tree, $row->gedcom);
98395f0fe0SGreg Roach            }
99395f0fe0SGreg Roach        }
100395f0fe0SGreg Roach    }
101395f0fe0SGreg Roach
102395f0fe0SGreg Roach    /**
103a25f0a04SGreg Roach     * Can the name of this record be shown?
104a25f0a04SGreg Roach     *
10576692c8bSGreg Roach     * @param int|null $access_level
10676692c8bSGreg Roach     *
10776692c8bSGreg Roach     * @return bool
108a25f0a04SGreg Roach     */
10935584196SGreg Roach    public function canShowName(int $access_level = null): bool
110c1010edaSGreg Roach    {
1114b9ff166SGreg Roach        if ($access_level === null) {
1124b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1134b9ff166SGreg Roach        }
1144b9ff166SGreg Roach
115518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
116a25f0a04SGreg Roach    }
117a25f0a04SGreg Roach
118a25f0a04SGreg Roach    /**
11976692c8bSGreg Roach     * Can this individual be shown?
120a25f0a04SGreg Roach     *
12176692c8bSGreg Roach     * @param int $access_level
12276692c8bSGreg Roach     *
12376692c8bSGreg Roach     * @return bool
124a25f0a04SGreg Roach     */
12535584196SGreg Roach    protected function canShowByType(int $access_level): bool
126c1010edaSGreg Roach    {
127a25f0a04SGreg Roach        // Dead people...
128518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
129a25f0a04SGreg Roach            $keep_alive             = false;
13054ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
131a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
132a25f0a04SGreg Roach                preg_match_all('/\n1 (?:' . WT_EVENTS_BIRT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
133a25f0a04SGreg Roach                foreach ($matches as $match) {
134a25f0a04SGreg Roach                    $date = new Date($match[1]);
135a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
136a25f0a04SGreg Roach                        $keep_alive = true;
137a25f0a04SGreg Roach                        break;
138a25f0a04SGreg Roach                    }
139a25f0a04SGreg Roach                }
140a25f0a04SGreg Roach            }
14154ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
142a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
143a25f0a04SGreg Roach                preg_match_all('/\n1 (?:' . WT_EVENTS_DEAT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
144a25f0a04SGreg Roach                foreach ($matches as $match) {
145a25f0a04SGreg Roach                    $date = new Date($match[1]);
146a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
147a25f0a04SGreg Roach                        $keep_alive = true;
148a25f0a04SGreg Roach                        break;
149a25f0a04SGreg Roach                    }
150a25f0a04SGreg Roach                }
151a25f0a04SGreg Roach            }
152a25f0a04SGreg Roach            if (!$keep_alive) {
153a25f0a04SGreg Roach                return true;
154a25f0a04SGreg Roach            }
155a25f0a04SGreg Roach        }
156a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
15754ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1584b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
15954ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1604b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
161a25f0a04SGreg Roach        }
162a25f0a04SGreg Roach
163a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1644b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
165a25f0a04SGreg Roach    }
166a25f0a04SGreg Roach
167a25f0a04SGreg Roach    /**
168a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
169a25f0a04SGreg Roach     *
170a25f0a04SGreg Roach     * @param Individual $target
171cbc1590aSGreg Roach     * @param int        $distance
172a25f0a04SGreg Roach     *
173cbc1590aSGreg Roach     * @return bool
174a25f0a04SGreg Roach     */
1758f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
176c1010edaSGreg Roach    {
177a25f0a04SGreg Roach        static $cache = null;
178a25f0a04SGreg Roach
17906ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
180a25f0a04SGreg Roach        if ($user_individual) {
181a25f0a04SGreg Roach            if (!$cache) {
18213abd6f3SGreg Roach                $cache = [
18313abd6f3SGreg Roach                    0 => [$user_individual],
18413abd6f3SGreg Roach                    1 => [],
18513abd6f3SGreg Roach                ];
1864b9ff166SGreg Roach                foreach ($user_individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
187a25f0a04SGreg Roach                    $family = $fact->getTarget();
188a25f0a04SGreg Roach                    if ($family) {
189a25f0a04SGreg Roach                        $cache[1][] = $family;
190a25f0a04SGreg Roach                    }
191a25f0a04SGreg Roach                }
192a25f0a04SGreg Roach            }
193a25f0a04SGreg Roach        } else {
194a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
195a25f0a04SGreg Roach            return true;
196a25f0a04SGreg Roach        }
197a25f0a04SGreg Roach
198a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
199a25f0a04SGreg Roach        $distance *= 2;
200a25f0a04SGreg Roach
201a25f0a04SGreg Roach        // Consider each path length in turn
202a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
203a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
204a25f0a04SGreg Roach                // We have already calculated all records with this length
205a25f0a04SGreg Roach                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
206a25f0a04SGreg Roach                    return true;
207a25f0a04SGreg Roach                }
208a25f0a04SGreg Roach            } else {
209a25f0a04SGreg Roach                // Need to calculate these paths
21013abd6f3SGreg Roach                $cache[$n] = [];
211a25f0a04SGreg Roach                if ($n % 2 == 0) {
212a25f0a04SGreg Roach                    // Add FAM->INDI links
213a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2144b9ff166SGreg Roach                        foreach ($family->getFacts('HUSB|WIFE|CHIL', false, Auth::PRIV_HIDE) as $fact) {
215a25f0a04SGreg Roach                            $individual = $fact->getTarget();
216a25f0a04SGreg Roach                            // Don’t backtrack
217a25f0a04SGreg Roach                            if ($individual && !in_array($individual, $cache[$n - 2], true)) {
218a25f0a04SGreg Roach                                $cache[$n][] = $individual;
219a25f0a04SGreg Roach                            }
220a25f0a04SGreg Roach                        }
221a25f0a04SGreg Roach                    }
222a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
223a25f0a04SGreg Roach                        return true;
224a25f0a04SGreg Roach                    }
225a25f0a04SGreg Roach                } else {
226a25f0a04SGreg Roach                    // Add INDI->FAM links
227a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2284b9ff166SGreg Roach                        foreach ($individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
229a25f0a04SGreg Roach                            $family = $fact->getTarget();
230a25f0a04SGreg Roach                            // Don’t backtrack
231a25f0a04SGreg Roach                            if ($family && !in_array($family, $cache[$n - 2], true)) {
232a25f0a04SGreg Roach                                $cache[$n][] = $family;
233a25f0a04SGreg Roach                            }
234a25f0a04SGreg Roach                        }
235a25f0a04SGreg Roach                    }
236a25f0a04SGreg Roach                }
237a25f0a04SGreg Roach            }
238a25f0a04SGreg Roach        }
239a25f0a04SGreg Roach
240a25f0a04SGreg Roach        return false;
241a25f0a04SGreg Roach    }
242a25f0a04SGreg Roach
24376692c8bSGreg Roach    /**
24476692c8bSGreg Roach     * Generate a private version of this record
24576692c8bSGreg Roach     *
24676692c8bSGreg Roach     * @param int $access_level
24776692c8bSGreg Roach     *
24876692c8bSGreg Roach     * @return string
24976692c8bSGreg Roach     */
2503c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
251c1010edaSGreg Roach    {
252acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
253a25f0a04SGreg Roach
254a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
255518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
256a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
257a25f0a04SGreg Roach            foreach ($this->getFacts('NAME') as $fact) {
258a25f0a04SGreg Roach                $rec .= "\n" . $fact->getGedcom();
259a25f0a04SGreg Roach            }
260a25f0a04SGreg Roach        }
261a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
262a25f0a04SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
263a25f0a04SGreg Roach        foreach ($matches as $match) {
26424ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
265a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
266a25f0a04SGreg Roach                $rec .= $match[0];
267a25f0a04SGreg Roach            }
268a25f0a04SGreg Roach        }
269a25f0a04SGreg Roach        // Don’t privatize sex.
270a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
271a25f0a04SGreg Roach            $rec .= $match[0];
272a25f0a04SGreg Roach        }
273a25f0a04SGreg Roach
274a25f0a04SGreg Roach        return $rec;
275a25f0a04SGreg Roach    }
276a25f0a04SGreg Roach
27776692c8bSGreg Roach    /**
27876692c8bSGreg Roach     * Fetch data from the database
27976692c8bSGreg Roach     *
28076692c8bSGreg Roach     * @param string $xref
28176692c8bSGreg Roach     * @param int    $tree_id
28276692c8bSGreg Roach     *
28376692c8bSGreg Roach     * @return null|string
28476692c8bSGreg Roach     */
28525e95e6eSGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
286c1010edaSGreg Roach    {
28764d9078aSGreg Roach        return Database::prepare(
28864d9078aSGreg Roach            "SELECT i_gedcom FROM `##individuals` WHERE i_id = :xref AND i_file = :tree_id"
28913abd6f3SGreg Roach        )->execute([
29064d9078aSGreg Roach            'xref'    => $xref,
29164d9078aSGreg Roach            'tree_id' => $tree_id,
29213abd6f3SGreg Roach        ])->fetchOne();
293a25f0a04SGreg Roach    }
294a25f0a04SGreg Roach
295a25f0a04SGreg Roach    /**
296a25f0a04SGreg Roach     * Static helper function to sort an array of people by birth date
297a25f0a04SGreg Roach     *
298a25f0a04SGreg Roach     * @param Individual $x
299a25f0a04SGreg Roach     * @param Individual $y
300a25f0a04SGreg Roach     *
301cbc1590aSGreg Roach     * @return int
302a25f0a04SGreg Roach     */
3038f53f488SRico Sonntag    public static function compareBirthDate(Individual $x, Individual $y): int
304c1010edaSGreg Roach    {
305f5b60decSGreg Roach        return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
306a25f0a04SGreg Roach    }
307a25f0a04SGreg Roach
308a25f0a04SGreg Roach    /**
309a25f0a04SGreg Roach     * Static helper function to sort an array of people by death date
310a25f0a04SGreg Roach     *
311a25f0a04SGreg Roach     * @param Individual $x
312a25f0a04SGreg Roach     * @param Individual $y
313a25f0a04SGreg Roach     *
314cbc1590aSGreg Roach     * @return int
315a25f0a04SGreg Roach     */
3168f53f488SRico Sonntag    public static function compareDeathDate(Individual $x, Individual $y): int
317c1010edaSGreg Roach    {
318f5b60decSGreg Roach        return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
319a25f0a04SGreg Roach    }
320a25f0a04SGreg Roach
321a25f0a04SGreg Roach    /**
322a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
323a25f0a04SGreg Roach     * If not known to be dead, then assume living.
324a25f0a04SGreg Roach     *
325cbc1590aSGreg Roach     * @return bool
326a25f0a04SGreg Roach     */
3278f53f488SRico Sonntag    public function isDead(): bool
328c1010edaSGreg Roach    {
329c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
330a25f0a04SGreg Roach
331a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
332a25f0a04SGreg Roach        if (preg_match('/\n1 (?:' . WT_EVENTS_DEAT . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
333a25f0a04SGreg Roach            return true;
334a25f0a04SGreg Roach        }
335a25f0a04SGreg Roach
336a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
337a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
338a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
339a25f0a04SGreg Roach                $date = new Date($date_match);
340f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
341a25f0a04SGreg Roach                    return true;
342a25f0a04SGreg Roach                }
343a25f0a04SGreg Roach            }
344a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
345a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
346a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
347a25f0a04SGreg Roach                return false;
348a25f0a04SGreg Roach            }
349a25f0a04SGreg Roach        }
350a25f0a04SGreg Roach
351a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
352a25f0a04SGreg Roach
353a25f0a04SGreg Roach        // Check parents (birth and adopted)
3544b9ff166SGreg Roach        foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3554b9ff166SGreg Roach            foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
356a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
357a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
358a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
359a25f0a04SGreg Roach                    $date = new Date($date_match);
360f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
361a25f0a04SGreg Roach                        return true;
362a25f0a04SGreg Roach                    }
363a25f0a04SGreg Roach                }
364a25f0a04SGreg Roach            }
365a25f0a04SGreg Roach        }
366a25f0a04SGreg Roach
367a25f0a04SGreg Roach        // Check spouses
3684b9ff166SGreg Roach        foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
369a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
370a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
371a25f0a04SGreg Roach                $date = new Date($date_match);
372a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
373f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
374a25f0a04SGreg Roach                    return true;
375a25f0a04SGreg Roach                }
376a25f0a04SGreg Roach            }
377a25f0a04SGreg Roach            // Check spouse dates
37813e3123bSGreg Roach            $spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
379a25f0a04SGreg Roach            if ($spouse) {
380a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
381a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
382a25f0a04SGreg Roach                    $date = new Date($date_match);
383a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
384f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
385a25f0a04SGreg Roach                        return true;
386a25f0a04SGreg Roach                    }
387a25f0a04SGreg Roach                }
388a25f0a04SGreg Roach            }
389a25f0a04SGreg Roach            // Check child dates
3904b9ff166SGreg Roach            foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
391a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
392a25f0a04SGreg Roach                // Assume children born after age of 15
393a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
394a25f0a04SGreg Roach                    $date = new Date($date_match);
395f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
396a25f0a04SGreg Roach                        return true;
397a25f0a04SGreg Roach                    }
398a25f0a04SGreg Roach                }
399a25f0a04SGreg Roach                // Check grandchildren
4004b9ff166SGreg Roach                foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
4014b9ff166SGreg Roach                    foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
402a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
403a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
404a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
405a25f0a04SGreg Roach                            $date = new Date($date_match);
406f5b60decSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
407a25f0a04SGreg Roach                                return true;
408a25f0a04SGreg Roach                            }
409a25f0a04SGreg Roach                        }
410a25f0a04SGreg Roach                    }
411a25f0a04SGreg Roach                }
412a25f0a04SGreg Roach            }
413a25f0a04SGreg Roach        }
414a25f0a04SGreg Roach
415a25f0a04SGreg Roach        return false;
416a25f0a04SGreg Roach    }
417a25f0a04SGreg Roach
418a25f0a04SGreg Roach    /**
419a25f0a04SGreg Roach     * Find the highlighted media object for an individual
420a25f0a04SGreg Roach     *
4214a9f750fSGreg Roach     * @return null|MediaFile
422a25f0a04SGreg Roach     */
423c1010edaSGreg Roach    public function findHighlightedMediaFile()
424c1010edaSGreg Roach    {
425c4fdfd2dSGreg Roach        foreach ($this->getFacts('OBJE') as $fact) {
426c4fdfd2dSGreg Roach            $media = $fact->getTarget();
427a213a63cSGreg Roach            if ($media instanceof Media) {
4284a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
429a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4304a9f750fSGreg Roach                        return $media_file;
4314a9f750fSGreg Roach                    }
4324a9f750fSGreg Roach                }
433a25f0a04SGreg Roach            }
434a25f0a04SGreg Roach        }
435a25f0a04SGreg Roach
436a25f0a04SGreg Roach        return null;
437a25f0a04SGreg Roach    }
438a25f0a04SGreg Roach
439a25f0a04SGreg Roach    /**
440a25f0a04SGreg Roach     * Display the prefered image for this individual.
441a25f0a04SGreg Roach     * Use an icon if no image is available.
442a25f0a04SGreg Roach     *
443dce07401SGreg Roach     * @param int      $width      Pixels
444dce07401SGreg Roach     * @param int      $height     Pixels
445dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
446dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
447dce07401SGreg Roach     *
448a25f0a04SGreg Roach     * @return string
449a25f0a04SGreg Roach     */
4508f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
451c1010edaSGreg Roach    {
4524a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4534a9f750fSGreg Roach
4544a9f750fSGreg Roach        if ($media_file !== null) {
4554a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
456a25f0a04SGreg Roach        }
4574a9f750fSGreg Roach
4584a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
4594a9f750fSGreg Roach            return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
4604a9f750fSGreg Roach        }
4614a9f750fSGreg Roach
4624a9f750fSGreg Roach        return '';
463a25f0a04SGreg Roach    }
464a25f0a04SGreg Roach
465a25f0a04SGreg Roach    /**
466a25f0a04SGreg Roach     * Get the date of birth
467a25f0a04SGreg Roach     *
468a25f0a04SGreg Roach     * @return Date
469a25f0a04SGreg Roach     */
4708f53f488SRico Sonntag    public function getBirthDate(): Date
471c1010edaSGreg Roach    {
472a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
473a25f0a04SGreg Roach            if ($date->isOK()) {
474a25f0a04SGreg Roach                return $date;
475a25f0a04SGreg Roach            }
476a25f0a04SGreg Roach        }
477a25f0a04SGreg Roach
478a25f0a04SGreg Roach        return new Date('');
479a25f0a04SGreg Roach    }
480a25f0a04SGreg Roach
481a25f0a04SGreg Roach    /**
482a25f0a04SGreg Roach     * Get the place of birth
483a25f0a04SGreg Roach     *
48416d0b7f7SRico Sonntag     * @return Place
485a25f0a04SGreg Roach     */
4868f53f488SRico Sonntag    public function getBirthPlace(): Place
487c1010edaSGreg Roach    {
488a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
489a25f0a04SGreg Roach            if ($place) {
490a25f0a04SGreg Roach                return $place;
491a25f0a04SGreg Roach            }
492a25f0a04SGreg Roach        }
493a25f0a04SGreg Roach
494b20ddbf9SGreg Roach        return new Place('', $this->tree);
495a25f0a04SGreg Roach    }
496a25f0a04SGreg Roach
497a25f0a04SGreg Roach    /**
498a25f0a04SGreg Roach     * Get the year of birth
499a25f0a04SGreg Roach     *
500a25f0a04SGreg Roach     * @return string the year of birth
501a25f0a04SGreg Roach     */
5028f53f488SRico Sonntag    public function getBirthYear(): string
503c1010edaSGreg Roach    {
504f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
505a25f0a04SGreg Roach    }
506a25f0a04SGreg Roach
507a25f0a04SGreg Roach    /**
508a25f0a04SGreg Roach     * Get the date of death
509a25f0a04SGreg Roach     *
510a25f0a04SGreg Roach     * @return Date
511a25f0a04SGreg Roach     */
5128f53f488SRico Sonntag    public function getDeathDate(): Date
513c1010edaSGreg Roach    {
514a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
515a25f0a04SGreg Roach            if ($date->isOK()) {
516a25f0a04SGreg Roach                return $date;
517a25f0a04SGreg Roach            }
518a25f0a04SGreg Roach        }
519a25f0a04SGreg Roach
520a25f0a04SGreg Roach        return new Date('');
521a25f0a04SGreg Roach    }
522a25f0a04SGreg Roach
523a25f0a04SGreg Roach    /**
524a25f0a04SGreg Roach     * Get the place of death
525a25f0a04SGreg Roach     *
52616d0b7f7SRico Sonntag     * @return Place
527a25f0a04SGreg Roach     */
5288f53f488SRico Sonntag    public function getDeathPlace(): Place
529c1010edaSGreg Roach    {
530a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
531a25f0a04SGreg Roach            if ($place) {
532a25f0a04SGreg Roach                return $place;
533a25f0a04SGreg Roach            }
534a25f0a04SGreg Roach        }
535a25f0a04SGreg Roach
536b20ddbf9SGreg Roach        return new Place('', $this->tree);
537a25f0a04SGreg Roach    }
538a25f0a04SGreg Roach
539a25f0a04SGreg Roach    /**
540a25f0a04SGreg Roach     * get the death year
541a25f0a04SGreg Roach     *
542a25f0a04SGreg Roach     * @return string the year of death
543a25f0a04SGreg Roach     */
5448f53f488SRico Sonntag    public function getDeathYear(): string
545c1010edaSGreg Roach    {
546f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
547a25f0a04SGreg Roach    }
548a25f0a04SGreg Roach
549a25f0a04SGreg Roach    /**
550a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
55115d603e7SGreg Roach     * Provide the place and full date using a tooltip.
552a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
553a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
554a25f0a04SGreg Roach     *
555a25f0a04SGreg Roach     * @return string
556a25f0a04SGreg Roach     */
5578f53f488SRico Sonntag    public function getLifeSpan(): string
558c1010edaSGreg Roach    {
55915d603e7SGreg Roach        // Just the first part of the place name
5601e9c2c49SGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
5611e9c2c49SGreg Roach        $death_place = strip_tags($this->getDeathPlace()->getShortName());
56215d603e7SGreg Roach        // Remove markup from dates
56315d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56415d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56515d603e7SGreg Roach
566c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
567bbb76c12SGreg Roach        return
568c1010edaSGreg Roach            I18N::translate(
569a25f0a04SGreg Roach                '%1$s–%2$s',
570d53324c9SGreg Roach                '<span title="' . e($birth_place) . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
571d53324c9SGreg Roach                '<span title="' . e($death_place) . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
572a25f0a04SGreg Roach            );
573a25f0a04SGreg Roach    }
574a25f0a04SGreg Roach
575a25f0a04SGreg Roach    /**
576a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
577a25f0a04SGreg Roach     *
578a25f0a04SGreg Roach     * @return Date[]
579a25f0a04SGreg Roach     */
5808f53f488SRico Sonntag    public function getAllBirthDates(): array
581c1010edaSGreg Roach    {
582a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
583a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
584a25f0a04SGreg Roach            if ($tmp) {
585a25f0a04SGreg Roach                return $tmp;
586a25f0a04SGreg Roach            }
587a25f0a04SGreg Roach        }
588a25f0a04SGreg Roach
58913abd6f3SGreg Roach        return [];
590a25f0a04SGreg Roach    }
591a25f0a04SGreg Roach
592a25f0a04SGreg Roach    /**
593a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
594a25f0a04SGreg Roach     *
5954080d558SGreg Roach     * @return Place[]
596a25f0a04SGreg Roach     */
5978f53f488SRico Sonntag    public function getAllBirthPlaces(): array
598c1010edaSGreg Roach    {
599a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
6004080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
6014080d558SGreg Roach            if (!empty($places)) {
6024080d558SGreg Roach                return $places;
603a25f0a04SGreg Roach            }
604a25f0a04SGreg Roach        }
605a25f0a04SGreg Roach
60613abd6f3SGreg Roach        return [];
607a25f0a04SGreg Roach    }
608a25f0a04SGreg Roach
609a25f0a04SGreg Roach    /**
610a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
611a25f0a04SGreg Roach     *
612a25f0a04SGreg Roach     * @return Date[]
613a25f0a04SGreg Roach     */
6148f53f488SRico Sonntag    public function getAllDeathDates(): array
615c1010edaSGreg Roach    {
616a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
617a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
618a25f0a04SGreg Roach            if ($tmp) {
619a25f0a04SGreg Roach                return $tmp;
620a25f0a04SGreg Roach            }
621a25f0a04SGreg Roach        }
622a25f0a04SGreg Roach
62313abd6f3SGreg Roach        return [];
624a25f0a04SGreg Roach    }
625a25f0a04SGreg Roach
626a25f0a04SGreg Roach    /**
627a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
628a25f0a04SGreg Roach     *
6294080d558SGreg Roach     * @return Place[]
630a25f0a04SGreg Roach     */
6318f53f488SRico Sonntag    public function getAllDeathPlaces(): array
632c1010edaSGreg Roach    {
633a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
6344080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
6354080d558SGreg Roach            if (!empty($places)) {
6364080d558SGreg Roach                return $places;
637a25f0a04SGreg Roach            }
638a25f0a04SGreg Roach        }
639a25f0a04SGreg Roach
64013abd6f3SGreg Roach        return [];
641a25f0a04SGreg Roach    }
642a25f0a04SGreg Roach
643a25f0a04SGreg Roach    /**
644a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
645a25f0a04SGreg Roach     *
646a25f0a04SGreg Roach     * @return Date
647a25f0a04SGreg Roach     */
6488f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
649c1010edaSGreg Roach    {
6508f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
651a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
652a25f0a04SGreg Roach                if ($date->isOK()) {
6534686330aSGreg Roach                    $this->estimated_birth_date = $date;
654a25f0a04SGreg Roach                    break;
655a25f0a04SGreg Roach                }
656a25f0a04SGreg Roach            }
6578f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
65813abd6f3SGreg Roach                $min = [];
65913abd6f3SGreg Roach                $max = [];
660a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
661f5b60decSGreg Roach                if ($tmp->isOK()) {
662f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
663f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
664a25f0a04SGreg Roach                }
665a25f0a04SGreg Roach                foreach ($this->getChildFamilies() as $family) {
666a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
667f5b60decSGreg Roach                    if ($tmp->isOK()) {
668f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
669f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
670a25f0a04SGreg Roach                    }
671a25f0a04SGreg Roach                    if ($parent = $family->getHusband()) {
672a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
673f5b60decSGreg Roach                        if ($tmp->isOK()) {
674f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
675f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
676a25f0a04SGreg Roach                        }
677a25f0a04SGreg Roach                    }
678a25f0a04SGreg Roach                    if ($parent = $family->getWife()) {
679a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
680f5b60decSGreg Roach                        if ($tmp->isOK()) {
681f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
682f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
683a25f0a04SGreg Roach                        }
684a25f0a04SGreg Roach                    }
685a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
686a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
687f5b60decSGreg Roach                        if ($tmp->isOK()) {
688f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
689f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
690a25f0a04SGreg Roach                        }
691a25f0a04SGreg Roach                    }
692a25f0a04SGreg Roach                }
693a25f0a04SGreg Roach                foreach ($this->getSpouseFamilies() as $family) {
694a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
695f5b60decSGreg Roach                    if ($tmp->isOK()) {
696f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
697f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
698a25f0a04SGreg Roach                    }
699a25f0a04SGreg Roach                    $spouse = $family->getSpouse($this);
700a25f0a04SGreg Roach                    if ($spouse) {
701a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
702f5b60decSGreg Roach                        if ($tmp->isOK()) {
703f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
704f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
705a25f0a04SGreg Roach                        }
706a25f0a04SGreg Roach                    }
707a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
708a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
709f5b60decSGreg Roach                        if ($tmp->isOK()) {
710f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
711f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
712a25f0a04SGreg Roach                        }
713a25f0a04SGreg Roach                    }
714a25f0a04SGreg Roach                }
715a25f0a04SGreg Roach                if ($min && $max) {
71659f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
717a25f0a04SGreg Roach
718cdaafeeeSGreg Roach                    list($year) = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7194686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
720a25f0a04SGreg Roach                } else {
7214686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
722a25f0a04SGreg Roach                }
723a25f0a04SGreg Roach            }
724a25f0a04SGreg Roach        }
725a25f0a04SGreg Roach
7264686330aSGreg Roach        return $this->estimated_birth_date;
727a25f0a04SGreg Roach    }
728a25f0a04SGreg Roach
729a25f0a04SGreg Roach    /**
730a25f0a04SGreg Roach     * Generate an estimated date of death.
731a25f0a04SGreg Roach     *
732a25f0a04SGreg Roach     * @return Date
733a25f0a04SGreg Roach     */
7348f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
735c1010edaSGreg Roach    {
7364686330aSGreg Roach        if ($this->estimated_death_date === null) {
737a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
738a25f0a04SGreg Roach                if ($date->isOK()) {
7394686330aSGreg Roach                    $this->estimated_death_date = $date;
740a25f0a04SGreg Roach                    break;
741a25f0a04SGreg Roach                }
742a25f0a04SGreg Roach            }
7434686330aSGreg Roach            if ($this->estimated_death_date === null) {
744f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
745c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7464686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
747a25f0a04SGreg Roach                } else {
7484686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
749a25f0a04SGreg Roach                }
750a25f0a04SGreg Roach            }
751a25f0a04SGreg Roach        }
752a25f0a04SGreg Roach
7534686330aSGreg Roach        return $this->estimated_death_date;
754a25f0a04SGreg Roach    }
755a25f0a04SGreg Roach
756a25f0a04SGreg Roach    /**
757a25f0a04SGreg Roach     * Get the sex - M F or U
758a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
759a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
760a25f0a04SGreg Roach     *
761a25f0a04SGreg Roach     * @return string
762a25f0a04SGreg Roach     */
763c1010edaSGreg Roach    public function getSex()
764c1010edaSGreg Roach    {
765a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
766a25f0a04SGreg Roach            return $match[1];
767a25f0a04SGreg Roach        }
768b2ce94c6SRico Sonntag
769b2ce94c6SRico Sonntag        return 'U';
770a25f0a04SGreg Roach    }
771a25f0a04SGreg Roach
772a25f0a04SGreg Roach    /**
773a25f0a04SGreg Roach     * Get the individual’s sex image
774a25f0a04SGreg Roach     *
775a25f0a04SGreg Roach     * @param string $size
776a25f0a04SGreg Roach     *
777a25f0a04SGreg Roach     * @return string
778a25f0a04SGreg Roach     */
7798f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
780c1010edaSGreg Roach    {
781a25f0a04SGreg Roach        return self::sexImage($this->getSex(), $size);
782a25f0a04SGreg Roach    }
783a25f0a04SGreg Roach
784a25f0a04SGreg Roach    /**
785a25f0a04SGreg Roach     * Generate a sex icon/image
786a25f0a04SGreg Roach     *
787a25f0a04SGreg Roach     * @param string $sex
788a25f0a04SGreg Roach     * @param string $size
789a25f0a04SGreg Roach     *
790a25f0a04SGreg Roach     * @return string
791a25f0a04SGreg Roach     */
7928f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
793c1010edaSGreg Roach    {
794a25f0a04SGreg Roach        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
795a25f0a04SGreg Roach    }
796a25f0a04SGreg Roach
797a25f0a04SGreg Roach    /**
798a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
799a25f0a04SGreg Roach     *
800a25f0a04SGreg Roach     * @return string
801a25f0a04SGreg Roach     */
8028f53f488SRico Sonntag    public function getBoxStyle(): string
803c1010edaSGreg Roach    {
804c1010edaSGreg Roach        $tmp = [
805c1010edaSGreg Roach            'M' => '',
806c1010edaSGreg Roach            'F' => 'F',
807c1010edaSGreg Roach            'U' => 'NN',
808c1010edaSGreg Roach        ];
809a25f0a04SGreg Roach
810a25f0a04SGreg Roach        return 'person_box' . $tmp[$this->getSex()];
811a25f0a04SGreg Roach    }
812a25f0a04SGreg Roach
813a25f0a04SGreg Roach    /**
814a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
815a25f0a04SGreg Roach     *
816cbc1590aSGreg Roach     * @param int|null $access_level
817a25f0a04SGreg Roach     *
818a25f0a04SGreg Roach     * @return Family[]
819a25f0a04SGreg Roach     */
8208f53f488SRico Sonntag    public function getSpouseFamilies($access_level = null): array
821c1010edaSGreg Roach    {
8224b9ff166SGreg Roach        if ($access_level === null) {
8234b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8244b9ff166SGreg Roach        }
8254b9ff166SGreg Roach
826acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
827a25f0a04SGreg Roach
82813abd6f3SGreg Roach        $families = [];
829a25f0a04SGreg Roach        foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
830a25f0a04SGreg Roach            $family = $fact->getTarget();
831a25f0a04SGreg Roach            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
832a25f0a04SGreg Roach                $families[] = $family;
833a25f0a04SGreg Roach            }
834a25f0a04SGreg Roach        }
835a25f0a04SGreg Roach
836a25f0a04SGreg Roach        return $families;
837a25f0a04SGreg Roach    }
838a25f0a04SGreg Roach
839a25f0a04SGreg Roach    /**
840a25f0a04SGreg Roach     * Get the current spouse of this individual.
841a25f0a04SGreg Roach     *
842a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
843a25f0a04SGreg Roach     * in chronological order, and take the last one found.
844a25f0a04SGreg Roach     *
845a25f0a04SGreg Roach     * @return Individual|null
846a25f0a04SGreg Roach     */
847c1010edaSGreg Roach    public function getCurrentSpouse()
848c1010edaSGreg Roach    {
849a25f0a04SGreg Roach        $tmp    = $this->getSpouseFamilies();
850a25f0a04SGreg Roach        $family = end($tmp);
851a25f0a04SGreg Roach        if ($family) {
852a25f0a04SGreg Roach            return $family->getSpouse($this);
853a25f0a04SGreg Roach        }
854b2ce94c6SRico Sonntag
855b2ce94c6SRico Sonntag        return null;
856a25f0a04SGreg Roach    }
857a25f0a04SGreg Roach
858a25f0a04SGreg Roach    /**
859a25f0a04SGreg Roach     * Count the children belonging to this individual.
860a25f0a04SGreg Roach     *
861cbc1590aSGreg Roach     * @return int
862a25f0a04SGreg Roach     */
863c1010edaSGreg Roach    public function getNumberOfChildren()
864c1010edaSGreg Roach    {
865a25f0a04SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
866a25f0a04SGreg Roach            return $match[1];
867b2ce94c6SRico Sonntag        }
868b2ce94c6SRico Sonntag
86913abd6f3SGreg Roach        $children = [];
870a25f0a04SGreg Roach        foreach ($this->getSpouseFamilies() as $fam) {
871a25f0a04SGreg Roach            foreach ($fam->getChildren() as $child) {
872a25f0a04SGreg Roach                $children[$child->getXref()] = true;
873a25f0a04SGreg Roach            }
874a25f0a04SGreg Roach        }
875a25f0a04SGreg Roach
876a25f0a04SGreg Roach        return count($children);
877a25f0a04SGreg Roach    }
878a25f0a04SGreg Roach
879a25f0a04SGreg Roach    /**
880a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
881a25f0a04SGreg Roach     *
882cbc1590aSGreg Roach     * @param int|null $access_level
883a25f0a04SGreg Roach     *
884a25f0a04SGreg Roach     * @return Family[]
885a25f0a04SGreg Roach     */
8868f53f488SRico Sonntag    public function getChildFamilies($access_level = null): array
887c1010edaSGreg Roach    {
8884b9ff166SGreg Roach        if ($access_level === null) {
8894b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8904b9ff166SGreg Roach        }
8914b9ff166SGreg Roach
892acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
893a25f0a04SGreg Roach
89413abd6f3SGreg Roach        $families = [];
895a25f0a04SGreg Roach        foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
896a25f0a04SGreg Roach            $family = $fact->getTarget();
897a25f0a04SGreg Roach            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
898a25f0a04SGreg Roach                $families[] = $family;
899a25f0a04SGreg Roach            }
900a25f0a04SGreg Roach        }
901a25f0a04SGreg Roach
902a25f0a04SGreg Roach        return $families;
903a25f0a04SGreg Roach    }
904a25f0a04SGreg Roach
905a25f0a04SGreg Roach    /**
906a25f0a04SGreg Roach     * Get the preferred parents for this individual.
907a25f0a04SGreg Roach     *
908a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
909a25f0a04SGreg Roach     * The preferred family record is:
910a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
911a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
912a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
913a25f0a04SGreg Roach     * (d) the first one found
914a25f0a04SGreg Roach     *
915a25f0a04SGreg Roach     * @return Family|null
916a25f0a04SGreg Roach     */
917c1010edaSGreg Roach    public function getPrimaryChildFamily()
918c1010edaSGreg Roach    {
919a25f0a04SGreg Roach        $families = $this->getChildFamilies();
920a25f0a04SGreg Roach        switch (count($families)) {
921a25f0a04SGreg Roach            case 0:
922a25f0a04SGreg Roach                return null;
923a25f0a04SGreg Roach            case 1:
92415d603e7SGreg Roach                return $families[0];
925a25f0a04SGreg Roach            default:
926a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
927a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
92874e78bfaSJonathan Jaubart                foreach ($families as $fam) {
92974e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
930a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
931a25f0a04SGreg Roach                        return $fam;
932a25f0a04SGreg Roach                    }
933a25f0a04SGreg Roach                }
934a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
93574e78bfaSJonathan Jaubart                foreach ($families as $fam) {
93674e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
937a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
938a25f0a04SGreg Roach                        return $fam;
939a25f0a04SGreg Roach                    }
940a25f0a04SGreg Roach                }
941a25f0a04SGreg Roach                // c) records with no '2 PEDI'
94274e78bfaSJonathan Jaubart                foreach ($families as $fam) {
94374e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
944a25f0a04SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
945a25f0a04SGreg Roach                        return $fam;
946a25f0a04SGreg Roach                    }
947a25f0a04SGreg Roach                }
948a25f0a04SGreg Roach
949a25f0a04SGreg Roach                // d) any record
95015d603e7SGreg Roach                return $families[0];
951a25f0a04SGreg Roach        }
952a25f0a04SGreg Roach    }
953a25f0a04SGreg Roach
954a25f0a04SGreg Roach    /**
955a25f0a04SGreg Roach     * Get a list of step-parent families.
956a25f0a04SGreg Roach     *
957a25f0a04SGreg Roach     * @return Family[]
958a25f0a04SGreg Roach     */
9598f53f488SRico Sonntag    public function getChildStepFamilies(): array
960c1010edaSGreg Roach    {
96113abd6f3SGreg Roach        $step_families = [];
962a25f0a04SGreg Roach        $families      = $this->getChildFamilies();
963a25f0a04SGreg Roach        foreach ($families as $family) {
964a25f0a04SGreg Roach            $father = $family->getHusband();
965a25f0a04SGreg Roach            if ($father) {
966a25f0a04SGreg Roach                foreach ($father->getSpouseFamilies() as $step_family) {
967a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
968a25f0a04SGreg Roach                        $step_families[] = $step_family;
969a25f0a04SGreg Roach                    }
970a25f0a04SGreg Roach                }
971a25f0a04SGreg Roach            }
972a25f0a04SGreg Roach            $mother = $family->getWife();
973a25f0a04SGreg Roach            if ($mother) {
974a25f0a04SGreg Roach                foreach ($mother->getSpouseFamilies() as $step_family) {
975a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
976a25f0a04SGreg Roach                        $step_families[] = $step_family;
977a25f0a04SGreg Roach                    }
978a25f0a04SGreg Roach                }
979a25f0a04SGreg Roach            }
980a25f0a04SGreg Roach        }
981a25f0a04SGreg Roach
982a25f0a04SGreg Roach        return $step_families;
983a25f0a04SGreg Roach    }
984a25f0a04SGreg Roach
985a25f0a04SGreg Roach    /**
986a25f0a04SGreg Roach     * Get a list of step-parent families.
987a25f0a04SGreg Roach     *
988a25f0a04SGreg Roach     * @return Family[]
989a25f0a04SGreg Roach     */
9908f53f488SRico Sonntag    public function getSpouseStepFamilies(): array
991c1010edaSGreg Roach    {
99213abd6f3SGreg Roach        $step_families = [];
993a25f0a04SGreg Roach        $families      = $this->getSpouseFamilies();
994a25f0a04SGreg Roach        foreach ($families as $family) {
995a25f0a04SGreg Roach            $spouse = $family->getSpouse($this);
996a25f0a04SGreg Roach            if ($spouse) {
997a25f0a04SGreg Roach                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
998a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
999a25f0a04SGreg Roach                        $step_families[] = $step_family;
1000a25f0a04SGreg Roach                    }
1001a25f0a04SGreg Roach                }
1002a25f0a04SGreg Roach            }
1003a25f0a04SGreg Roach        }
1004a25f0a04SGreg Roach
1005a25f0a04SGreg Roach        return $step_families;
1006a25f0a04SGreg Roach    }
1007a25f0a04SGreg Roach
1008a25f0a04SGreg Roach    /**
1009a25f0a04SGreg Roach     * A label for a parental family group
1010a25f0a04SGreg Roach     *
1011a25f0a04SGreg Roach     * @param Family $family
1012a25f0a04SGreg Roach     *
1013a25f0a04SGreg Roach     * @return string
1014a25f0a04SGreg Roach     */
1015c1010edaSGreg Roach    public function getChildFamilyLabel(Family $family)
1016c1010edaSGreg Roach    {
1017a25f0a04SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->getXref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
1018a25f0a04SGreg Roach            // A specified pedigree
1019764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1020b2ce94c6SRico Sonntag        }
1021b2ce94c6SRico Sonntag
1022a25f0a04SGreg Roach        // Default (birth) pedigree
1023764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1024a25f0a04SGreg Roach    }
1025a25f0a04SGreg Roach
1026a25f0a04SGreg Roach    /**
1027a25f0a04SGreg Roach     * Create a label for a step family
1028a25f0a04SGreg Roach     *
1029a25f0a04SGreg Roach     * @param Family $step_family
1030a25f0a04SGreg Roach     *
1031a25f0a04SGreg Roach     * @return string
1032a25f0a04SGreg Roach     */
10338f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1034c1010edaSGreg Roach    {
1035a25f0a04SGreg Roach        foreach ($this->getChildFamilies() as $family) {
1036a25f0a04SGreg Roach            if ($family !== $step_family) {
1037a25f0a04SGreg Roach                // Must be a step-family
1038a25f0a04SGreg Roach                foreach ($family->getSpouses() as $parent) {
1039a25f0a04SGreg Roach                    foreach ($step_family->getSpouses() as $step_parent) {
1040a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1041a25f0a04SGreg Roach                            // One common parent - must be a step family
1042a25f0a04SGreg Roach                            if ($parent->getSex() == 'M') {
1043a25f0a04SGreg Roach                                // Father’s family with someone else
1044a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1045a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1046bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1047b2ce94c6SRico Sonntag                                }
1048b2ce94c6SRico Sonntag
1049a25f0a04SGreg Roach                                /* I18N: A step-family. */
1050bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1051a25f0a04SGreg Roach                            }
1052b2ce94c6SRico Sonntag
1053a25f0a04SGreg Roach                            // Mother’s family with someone else
1054a25f0a04SGreg Roach                            if ($step_family->getSpouse($step_parent)) {
1055a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
1056bbb76c12SGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1057b2ce94c6SRico Sonntag                            }
1058b2ce94c6SRico Sonntag
1059a25f0a04SGreg Roach                            /* I18N: A step-family. */
1060bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1061a25f0a04SGreg Roach                        }
1062a25f0a04SGreg Roach                    }
1063a25f0a04SGreg Roach                }
1064a25f0a04SGreg Roach            }
1065a25f0a04SGreg Roach        }
1066a25f0a04SGreg Roach
1067a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1068a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1069a25f0a04SGreg Roach    }
1070a25f0a04SGreg Roach
1071225e381fSGreg Roach
1072225e381fSGreg Roach    /**
1073225e381fSGreg Roach     * Get the description for the family.
1074225e381fSGreg Roach     *
1075225e381fSGreg Roach     * For example, "XXX's family with new wife".
1076225e381fSGreg Roach     *
1077225e381fSGreg Roach     * @param Family $family
1078225e381fSGreg Roach     *
1079225e381fSGreg Roach     * @return string
1080225e381fSGreg Roach     */
1081c1010edaSGreg Roach    public function getSpouseFamilyLabel(Family $family)
1082c1010edaSGreg Roach    {
1083225e381fSGreg Roach        $spouse = $family->getSpouse($this);
1084225e381fSGreg Roach        if ($spouse) {
1085225e381fSGreg Roach            /* I18N: %s is the spouse name */
1086bbb76c12SGreg Roach            return I18N::translate('Family with %s', $spouse->getFullName());
1087225e381fSGreg Roach        }
1088b2ce94c6SRico Sonntag
1089b2ce94c6SRico Sonntag        return $family->getFullName();
1090225e381fSGreg Roach    }
1091225e381fSGreg Roach
1092a25f0a04SGreg Roach    /**
1093a25f0a04SGreg Roach     * get primary parents names for this individual
1094a25f0a04SGreg Roach     *
1095a25f0a04SGreg Roach     * @param string $classname optional css class
1096a25f0a04SGreg Roach     * @param string $display   optional css style display
1097a25f0a04SGreg Roach     *
1098a25f0a04SGreg Roach     * @return string a div block with father & mother names
1099a25f0a04SGreg Roach     */
11008f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1101c1010edaSGreg Roach    {
1102a25f0a04SGreg Roach        $fam = $this->getPrimaryChildFamily();
1103a25f0a04SGreg Roach        if (!$fam) {
1104a25f0a04SGreg Roach            return '';
1105a25f0a04SGreg Roach        }
1106a25f0a04SGreg Roach        $txt = '<div';
1107a25f0a04SGreg Roach        if ($classname) {
11084c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1109a25f0a04SGreg Roach        }
1110a25f0a04SGreg Roach        if ($display) {
11114c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1112a25f0a04SGreg Roach        }
1113a25f0a04SGreg Roach        $txt .= '>';
1114a25f0a04SGreg Roach        $husb = $fam->getHusband();
1115a25f0a04SGreg Roach        if ($husb) {
1116a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1117a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1118a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1119a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1120a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
1121bbb76c12SGreg Roach            $txt .= I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1122a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1123a25f0a04SGreg Roach        }
1124a25f0a04SGreg Roach        $wife = $fam->getWife();
1125a25f0a04SGreg Roach        if ($wife) {
1126a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1127a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1128a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1129a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1130a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
1131bbb76c12SGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->getFullName());
1132a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1133a25f0a04SGreg Roach        }
1134a25f0a04SGreg Roach        $txt .= '</div>';
1135a25f0a04SGreg Roach
1136a25f0a04SGreg Roach        return $txt;
1137a25f0a04SGreg Roach    }
1138a25f0a04SGreg Roach
1139a25f0a04SGreg Roach    /** {@inheritdoc} */
11408f53f488SRico Sonntag    public function getFallBackName(): string
1141c1010edaSGreg Roach    {
1142a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1143a25f0a04SGreg Roach    }
1144a25f0a04SGreg Roach
1145a25f0a04SGreg Roach    /**
1146a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1147a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1148a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1149a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1150a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1151a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1152a25f0a04SGreg Roach     *
1153a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1154a25f0a04SGreg Roach     * 2 GIVN Robert
1155a25f0a04SGreg Roach     * 2 SPFX de
1156a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1157a25f0a04SGreg Roach     * 2 NICK The Bald
1158a25f0a04SGreg Roach     *
1159a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1160a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1161a25f0a04SGreg Roach     *
1162a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1163a25f0a04SGreg Roach     *
1164a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1165a25f0a04SGreg Roach     * or
1166a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1167a25f0a04SGreg Roach     * 2 GIVN Carlos
1168a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1169a25f0a04SGreg Roach     *
1170a25f0a04SGreg Roach     * @param string $type
1171a25f0a04SGreg Roach     * @param string $full
1172a25f0a04SGreg Roach     * @param string $gedcom
1173a25f0a04SGreg Roach     */
117476f666f4SGreg Roach    protected function addName(string $type, string $full, string $gedcom)
1175c1010edaSGreg Roach    {
1176a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1177a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1178a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1179a25f0a04SGreg Roach
118076f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1181a25f0a04SGreg Roach        $GIVN = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1182a25f0a04SGreg Roach        $SURN = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1183a25f0a04SGreg Roach        $NICK = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1184a25f0a04SGreg Roach
1185a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
118676f666f4SGreg Roach        if ($SURN !== '') {
1187a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1188a25f0a04SGreg Roach        } else {
118913abd6f3SGreg Roach            $SURNS = [];
1190a25f0a04SGreg Roach        }
119176f666f4SGreg Roach
1192a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1193a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1194a25f0a04SGreg Roach
1195a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1196a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1197a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1198a25f0a04SGreg Roach
1199a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
120076f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1201a25f0a04SGreg Roach            $full = $full . '/';
1202a25f0a04SGreg Roach        }
1203a25f0a04SGreg Roach
1204a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1205a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1206a25f0a04SGreg Roach
1207a25f0a04SGreg Roach        // Extract the surname.
1208a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1209a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1210a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1211a25f0a04SGreg Roach        } else {
1212a25f0a04SGreg Roach            $surname = '';
1213a25f0a04SGreg Roach        }
1214a25f0a04SGreg Roach
1215a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1216a25f0a04SGreg Roach        if (!$SURNS) {
1217a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1218a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1219a25f0a04SGreg Roach                $SURNS = $matches[1];
1220a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1221a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1222a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1223a25f0a04SGreg Roach                }
1224a25f0a04SGreg Roach            } else {
1225a25f0a04SGreg Roach                // It is valid not to have a surname at all
122613abd6f3SGreg Roach                $SURNS = [''];
1227a25f0a04SGreg Roach            }
1228a25f0a04SGreg Roach        }
1229a25f0a04SGreg Roach
1230a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1231a25f0a04SGreg Roach        if (!$GIVN) {
1232a25f0a04SGreg Roach            $GIVN = preg_replace(
123313abd6f3SGreg Roach                [
1234c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1235c1010edaSGreg Roach                    // remove surname
1236c1010edaSGreg Roach                    '/ ?".+"/',
1237c1010edaSGreg Roach                    // remove nickname
1238c1010edaSGreg Roach                    '/ {2,}/',
1239c1010edaSGreg Roach                    // multiple spaces, caused by the above
1240c1010edaSGreg Roach                    '/^ | $/',
1241c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
124213abd6f3SGreg Roach                ],
124313abd6f3SGreg Roach                [
1244a25f0a04SGreg Roach                    ' ',
1245a25f0a04SGreg Roach                    ' ',
1246a25f0a04SGreg Roach                    ' ',
1247a25f0a04SGreg Roach                    '',
124813abd6f3SGreg Roach                ],
1249a25f0a04SGreg Roach                $full
1250a25f0a04SGreg Roach            );
1251a25f0a04SGreg Roach        }
1252a25f0a04SGreg Roach
1253a25f0a04SGreg Roach        // Add placeholder for unknown given name
1254a25f0a04SGreg Roach        if (!$GIVN) {
1255a25f0a04SGreg Roach            $GIVN = '@P.N.';
1256a25f0a04SGreg Roach            $pos  = strpos($full, '/');
1257a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1258a25f0a04SGreg Roach        }
1259a25f0a04SGreg Roach
12607b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12617b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1262c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12637b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1264c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1265a25f0a04SGreg Roach        }
1266a25f0a04SGreg Roach
1267a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1268a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1269a25f0a04SGreg Roach        // $full is for display on-screen
1270a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1271a25f0a04SGreg Roach
1272a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1273ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1274ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1275c6f196c3SGreg Roach        // Format for display
1276d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1277acc34ea1SGreg Roach        // Localise quotation marks around the nickname
127818d7a90dSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function (array $matches): string {
12798d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12808d68cabeSGreg Roach        }, $full);
1281a25f0a04SGreg Roach
1282c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1283a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1284a25f0a04SGreg Roach
1285a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1286a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1287a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1288a25f0a04SGreg Roach
1289ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1290a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1291a25f0a04SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1292a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1293a25f0a04SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1294a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1295a25f0a04SGreg Roach            }
1296a25f0a04SGreg Roach
1297bdb3725aSGreg Roach            $this->getAllNames[] = [
1298a25f0a04SGreg Roach                'type'    => $type,
1299a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1300c1010edaSGreg Roach                'full'    => $full,
1301c1010edaSGreg Roach                // This is used for display
1302c1010edaSGreg Roach                'fullNN'  => $fullNN,
1303c1010edaSGreg Roach                // This goes into the database
1304c1010edaSGreg Roach                'surname' => $surname,
1305c1010edaSGreg Roach                // This goes into the database
1306c1010edaSGreg Roach                'givn'    => $GIVN,
1307c1010edaSGreg Roach                // This goes into the database
1308c1010edaSGreg Roach                'surn'    => $SURN,
1309c1010edaSGreg Roach                // This goes into the database
131013abd6f3SGreg Roach            ];
1311a25f0a04SGreg Roach        }
1312a25f0a04SGreg Roach    }
1313a25f0a04SGreg Roach
1314a25f0a04SGreg Roach    /**
131576692c8bSGreg Roach     * Extract names from the GEDCOM record.
1316c7ff4153SGreg Roach     *
1317c7ff4153SGreg Roach     * @return void
1318a25f0a04SGreg Roach     */
1319c1010edaSGreg Roach    public function extractNames()
1320c1010edaSGreg Roach    {
13218f53f488SRico Sonntag        $this->extractNamesFromFacts(
13228f53f488SRico Sonntag            1,
13238f53f488SRico Sonntag            'NAME',
13248f53f488SRico Sonntag            $this->getFacts(
13258f53f488SRico Sonntag                'NAME',
13268f53f488SRico Sonntag                false,
13278f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13288f53f488SRico Sonntag                $this->canShowName()
13298f53f488SRico Sonntag            )
13308f53f488SRico Sonntag        );
1331a25f0a04SGreg Roach    }
1332a25f0a04SGreg Roach
1333a25f0a04SGreg Roach    /**
1334a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1335a25f0a04SGreg Roach     * selection items or favorites.
1336a25f0a04SGreg Roach     *
1337a25f0a04SGreg Roach     * @return string
1338a25f0a04SGreg Roach     */
13398f53f488SRico Sonntag    public function formatListDetails(): string
1340c1010edaSGreg Roach    {
1341a25f0a04SGreg Roach        return
1342841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1343841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1344a25f0a04SGreg Roach    }
1345a25f0a04SGreg Roach}
1346