xref: /webtrees/app/Individual.php (revision c09358794665832df009408551d8543ccb6e3783)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg 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         = [
7672cf66d4SGreg Roach            'tree_id' => $tree->id(),
7713abd6f3SGreg Roach        ];
7813abd6f3SGreg Roach        $placeholders = [];
79395f0fe0SGreg Roach
80395f0fe0SGreg Roach        foreach (array_unique($xrefs) as $n => $xref) {
8172cf66d4SGreg Roach            if (!isset(self::$gedcom_record_cache[$tree->id()][$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) {
187dc124885SGreg Roach                    $family = $fact->target();
188e24444eeSGreg Roach                    if ($family instanceof 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) {
215dc124885SGreg Roach                            $individual = $fact->target();
216a25f0a04SGreg Roach                            // Don’t backtrack
217e24444eeSGreg Roach                            if ($individual instanceof 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) {
229dc124885SGreg Roach                            $family = $fact->target();
230a25f0a04SGreg Roach                            // Don’t backtrack
231e24444eeSGreg Roach                            if ($family instanceof 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) {
258138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
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) {
426dc124885SGreg Roach            $media = $fact->target();
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            return $place;
490a25f0a04SGreg Roach        }
491a25f0a04SGreg Roach
492b20ddbf9SGreg Roach        return new Place('', $this->tree);
493a25f0a04SGreg Roach    }
494a25f0a04SGreg Roach
495a25f0a04SGreg Roach    /**
496a25f0a04SGreg Roach     * Get the year of birth
497a25f0a04SGreg Roach     *
498a25f0a04SGreg Roach     * @return string the year of birth
499a25f0a04SGreg Roach     */
5008f53f488SRico Sonntag    public function getBirthYear(): string
501c1010edaSGreg Roach    {
502f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
503a25f0a04SGreg Roach    }
504a25f0a04SGreg Roach
505a25f0a04SGreg Roach    /**
506a25f0a04SGreg Roach     * Get the date of death
507a25f0a04SGreg Roach     *
508a25f0a04SGreg Roach     * @return Date
509a25f0a04SGreg Roach     */
5108f53f488SRico Sonntag    public function getDeathDate(): Date
511c1010edaSGreg Roach    {
512a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
513a25f0a04SGreg Roach            if ($date->isOK()) {
514a25f0a04SGreg Roach                return $date;
515a25f0a04SGreg Roach            }
516a25f0a04SGreg Roach        }
517a25f0a04SGreg Roach
518a25f0a04SGreg Roach        return new Date('');
519a25f0a04SGreg Roach    }
520a25f0a04SGreg Roach
521a25f0a04SGreg Roach    /**
522a25f0a04SGreg Roach     * Get the place of death
523a25f0a04SGreg Roach     *
52416d0b7f7SRico Sonntag     * @return Place
525a25f0a04SGreg Roach     */
5268f53f488SRico Sonntag    public function getDeathPlace(): Place
527c1010edaSGreg Roach    {
528a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
529a25f0a04SGreg Roach            return $place;
530a25f0a04SGreg Roach        }
531a25f0a04SGreg Roach
532b20ddbf9SGreg Roach        return new Place('', $this->tree);
533a25f0a04SGreg Roach    }
534a25f0a04SGreg Roach
535a25f0a04SGreg Roach    /**
536a25f0a04SGreg Roach     * get the death year
537a25f0a04SGreg Roach     *
538a25f0a04SGreg Roach     * @return string the year of death
539a25f0a04SGreg Roach     */
5408f53f488SRico Sonntag    public function getDeathYear(): string
541c1010edaSGreg Roach    {
542f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
543a25f0a04SGreg Roach    }
544a25f0a04SGreg Roach
545a25f0a04SGreg Roach    /**
546a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
54715d603e7SGreg Roach     * Provide the place and full date using a tooltip.
548a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
549a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
550a25f0a04SGreg Roach     *
551a25f0a04SGreg Roach     * @return string
552a25f0a04SGreg Roach     */
5538f53f488SRico Sonntag    public function getLifeSpan(): string
554c1010edaSGreg Roach    {
55515d603e7SGreg Roach        // Just the first part of the place name
5561e9c2c49SGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
5571e9c2c49SGreg Roach        $death_place = strip_tags($this->getDeathPlace()->getShortName());
55815d603e7SGreg Roach        // Remove markup from dates
55915d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56015d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56115d603e7SGreg Roach
562c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
563bbb76c12SGreg Roach        return
564c1010edaSGreg Roach            I18N::translate(
565a25f0a04SGreg Roach                '%1$s–%2$s',
566d53324c9SGreg Roach                '<span title="' . e($birth_place) . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
567d53324c9SGreg Roach                '<span title="' . e($death_place) . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
568a25f0a04SGreg Roach            );
569a25f0a04SGreg Roach    }
570a25f0a04SGreg Roach
571a25f0a04SGreg Roach    /**
572a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
573a25f0a04SGreg Roach     *
574a25f0a04SGreg Roach     * @return Date[]
575a25f0a04SGreg Roach     */
5768f53f488SRico Sonntag    public function getAllBirthDates(): array
577c1010edaSGreg Roach    {
578a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
579a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
580a25f0a04SGreg Roach            if ($tmp) {
581a25f0a04SGreg Roach                return $tmp;
582a25f0a04SGreg Roach            }
583a25f0a04SGreg Roach        }
584a25f0a04SGreg Roach
58513abd6f3SGreg Roach        return [];
586a25f0a04SGreg Roach    }
587a25f0a04SGreg Roach
588a25f0a04SGreg Roach    /**
589a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
590a25f0a04SGreg Roach     *
5914080d558SGreg Roach     * @return Place[]
592a25f0a04SGreg Roach     */
5938f53f488SRico Sonntag    public function getAllBirthPlaces(): array
594c1010edaSGreg Roach    {
595a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
5964080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
5974080d558SGreg Roach            if (!empty($places)) {
5984080d558SGreg Roach                return $places;
599a25f0a04SGreg Roach            }
600a25f0a04SGreg Roach        }
601a25f0a04SGreg Roach
60213abd6f3SGreg Roach        return [];
603a25f0a04SGreg Roach    }
604a25f0a04SGreg Roach
605a25f0a04SGreg Roach    /**
606a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
607a25f0a04SGreg Roach     *
608a25f0a04SGreg Roach     * @return Date[]
609a25f0a04SGreg Roach     */
6108f53f488SRico Sonntag    public function getAllDeathDates(): array
611c1010edaSGreg Roach    {
612a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
613a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
614a25f0a04SGreg Roach            if ($tmp) {
615a25f0a04SGreg Roach                return $tmp;
616a25f0a04SGreg Roach            }
617a25f0a04SGreg Roach        }
618a25f0a04SGreg Roach
61913abd6f3SGreg Roach        return [];
620a25f0a04SGreg Roach    }
621a25f0a04SGreg Roach
622a25f0a04SGreg Roach    /**
623a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
624a25f0a04SGreg Roach     *
6254080d558SGreg Roach     * @return Place[]
626a25f0a04SGreg Roach     */
6278f53f488SRico Sonntag    public function getAllDeathPlaces(): array
628c1010edaSGreg Roach    {
629a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
6304080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
6314080d558SGreg Roach            if (!empty($places)) {
6324080d558SGreg Roach                return $places;
633a25f0a04SGreg Roach            }
634a25f0a04SGreg Roach        }
635a25f0a04SGreg Roach
63613abd6f3SGreg Roach        return [];
637a25f0a04SGreg Roach    }
638a25f0a04SGreg Roach
639a25f0a04SGreg Roach    /**
640a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
641a25f0a04SGreg Roach     *
642a25f0a04SGreg Roach     * @return Date
643a25f0a04SGreg Roach     */
6448f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
645c1010edaSGreg Roach    {
6468f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
647a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
648a25f0a04SGreg Roach                if ($date->isOK()) {
6494686330aSGreg Roach                    $this->estimated_birth_date = $date;
650a25f0a04SGreg Roach                    break;
651a25f0a04SGreg Roach                }
652a25f0a04SGreg Roach            }
6538f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
65413abd6f3SGreg Roach                $min = [];
65513abd6f3SGreg Roach                $max = [];
656a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
657f5b60decSGreg Roach                if ($tmp->isOK()) {
658f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
659f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
660a25f0a04SGreg Roach                }
661a25f0a04SGreg Roach                foreach ($this->getChildFamilies() as $family) {
662a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
663f5b60decSGreg Roach                    if ($tmp->isOK()) {
664f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
665f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
666a25f0a04SGreg Roach                    }
667a25f0a04SGreg Roach                    if ($parent = $family->getHusband()) {
668a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
669f5b60decSGreg Roach                        if ($tmp->isOK()) {
670f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
671f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
672a25f0a04SGreg Roach                        }
673a25f0a04SGreg Roach                    }
674a25f0a04SGreg Roach                    if ($parent = $family->getWife()) {
675a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
676f5b60decSGreg Roach                        if ($tmp->isOK()) {
677f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
678f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
679a25f0a04SGreg Roach                        }
680a25f0a04SGreg Roach                    }
681a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
682a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
683f5b60decSGreg Roach                        if ($tmp->isOK()) {
684f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
685f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
686a25f0a04SGreg Roach                        }
687a25f0a04SGreg Roach                    }
688a25f0a04SGreg Roach                }
689a25f0a04SGreg Roach                foreach ($this->getSpouseFamilies() as $family) {
690a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
691f5b60decSGreg Roach                    if ($tmp->isOK()) {
692f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
693f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
694a25f0a04SGreg Roach                    }
695a25f0a04SGreg Roach                    $spouse = $family->getSpouse($this);
696a25f0a04SGreg Roach                    if ($spouse) {
697a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
698f5b60decSGreg Roach                        if ($tmp->isOK()) {
699f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
700f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
701a25f0a04SGreg Roach                        }
702a25f0a04SGreg Roach                    }
703a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
704a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
705f5b60decSGreg Roach                        if ($tmp->isOK()) {
706f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
707f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
708a25f0a04SGreg Roach                        }
709a25f0a04SGreg Roach                    }
710a25f0a04SGreg Roach                }
711a25f0a04SGreg Roach                if ($min && $max) {
71259f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
713a25f0a04SGreg Roach
714cdaafeeeSGreg Roach                    list($year) = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7154686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
716a25f0a04SGreg Roach                } else {
7174686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
718a25f0a04SGreg Roach                }
719a25f0a04SGreg Roach            }
720a25f0a04SGreg Roach        }
721a25f0a04SGreg Roach
7224686330aSGreg Roach        return $this->estimated_birth_date;
723a25f0a04SGreg Roach    }
724a25f0a04SGreg Roach
725a25f0a04SGreg Roach    /**
726a25f0a04SGreg Roach     * Generate an estimated date of death.
727a25f0a04SGreg Roach     *
728a25f0a04SGreg Roach     * @return Date
729a25f0a04SGreg Roach     */
7308f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
731c1010edaSGreg Roach    {
7324686330aSGreg Roach        if ($this->estimated_death_date === null) {
733a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
734a25f0a04SGreg Roach                if ($date->isOK()) {
7354686330aSGreg Roach                    $this->estimated_death_date = $date;
736a25f0a04SGreg Roach                    break;
737a25f0a04SGreg Roach                }
738a25f0a04SGreg Roach            }
7394686330aSGreg Roach            if ($this->estimated_death_date === null) {
740f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
741c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7424686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
743a25f0a04SGreg Roach                } else {
7444686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
745a25f0a04SGreg Roach                }
746a25f0a04SGreg Roach            }
747a25f0a04SGreg Roach        }
748a25f0a04SGreg Roach
7494686330aSGreg Roach        return $this->estimated_death_date;
750a25f0a04SGreg Roach    }
751a25f0a04SGreg Roach
752a25f0a04SGreg Roach    /**
753a25f0a04SGreg Roach     * Get the sex - M F or U
754a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
755a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
756a25f0a04SGreg Roach     *
757a25f0a04SGreg Roach     * @return string
758a25f0a04SGreg Roach     */
759c1010edaSGreg Roach    public function getSex()
760c1010edaSGreg Roach    {
761a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
762a25f0a04SGreg Roach            return $match[1];
763a25f0a04SGreg Roach        }
764b2ce94c6SRico Sonntag
765b2ce94c6SRico Sonntag        return 'U';
766a25f0a04SGreg Roach    }
767a25f0a04SGreg Roach
768a25f0a04SGreg Roach    /**
769a25f0a04SGreg Roach     * Get the individual’s sex image
770a25f0a04SGreg Roach     *
771a25f0a04SGreg Roach     * @param string $size
772a25f0a04SGreg Roach     *
773a25f0a04SGreg Roach     * @return string
774a25f0a04SGreg Roach     */
7758f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
776c1010edaSGreg Roach    {
777a25f0a04SGreg Roach        return self::sexImage($this->getSex(), $size);
778a25f0a04SGreg Roach    }
779a25f0a04SGreg Roach
780a25f0a04SGreg Roach    /**
781a25f0a04SGreg Roach     * Generate a sex icon/image
782a25f0a04SGreg Roach     *
783a25f0a04SGreg Roach     * @param string $sex
784a25f0a04SGreg Roach     * @param string $size
785a25f0a04SGreg Roach     *
786a25f0a04SGreg Roach     * @return string
787a25f0a04SGreg Roach     */
7888f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
789c1010edaSGreg Roach    {
790a25f0a04SGreg Roach        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
791a25f0a04SGreg Roach    }
792a25f0a04SGreg Roach
793a25f0a04SGreg Roach    /**
794a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
795a25f0a04SGreg Roach     *
796a25f0a04SGreg Roach     * @return string
797a25f0a04SGreg Roach     */
7988f53f488SRico Sonntag    public function getBoxStyle(): string
799c1010edaSGreg Roach    {
800c1010edaSGreg Roach        $tmp = [
801c1010edaSGreg Roach            'M' => '',
802c1010edaSGreg Roach            'F' => 'F',
803c1010edaSGreg Roach            'U' => 'NN',
804c1010edaSGreg Roach        ];
805a25f0a04SGreg Roach
806a25f0a04SGreg Roach        return 'person_box' . $tmp[$this->getSex()];
807a25f0a04SGreg Roach    }
808a25f0a04SGreg Roach
809a25f0a04SGreg Roach    /**
810a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
811a25f0a04SGreg Roach     *
812cbc1590aSGreg Roach     * @param int|null $access_level
813a25f0a04SGreg Roach     *
814a25f0a04SGreg Roach     * @return Family[]
815a25f0a04SGreg Roach     */
8168f53f488SRico Sonntag    public function getSpouseFamilies($access_level = null): array
817c1010edaSGreg Roach    {
8184b9ff166SGreg Roach        if ($access_level === null) {
8194b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8204b9ff166SGreg Roach        }
8214b9ff166SGreg Roach
822acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
823a25f0a04SGreg Roach
82413abd6f3SGreg Roach        $families = [];
825a25f0a04SGreg Roach        foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
826dc124885SGreg Roach            $family = $fact->target();
827e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
828a25f0a04SGreg Roach                $families[] = $family;
829a25f0a04SGreg Roach            }
830a25f0a04SGreg Roach        }
831a25f0a04SGreg Roach
832a25f0a04SGreg Roach        return $families;
833a25f0a04SGreg Roach    }
834a25f0a04SGreg Roach
835a25f0a04SGreg Roach    /**
836a25f0a04SGreg Roach     * Get the current spouse of this individual.
837a25f0a04SGreg Roach     *
838a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
839a25f0a04SGreg Roach     * in chronological order, and take the last one found.
840a25f0a04SGreg Roach     *
841a25f0a04SGreg Roach     * @return Individual|null
842a25f0a04SGreg Roach     */
843c1010edaSGreg Roach    public function getCurrentSpouse()
844c1010edaSGreg Roach    {
845a25f0a04SGreg Roach        $tmp    = $this->getSpouseFamilies();
846a25f0a04SGreg Roach        $family = end($tmp);
847a25f0a04SGreg Roach        if ($family) {
848a25f0a04SGreg Roach            return $family->getSpouse($this);
849a25f0a04SGreg Roach        }
850b2ce94c6SRico Sonntag
851b2ce94c6SRico Sonntag        return null;
852a25f0a04SGreg Roach    }
853a25f0a04SGreg Roach
854a25f0a04SGreg Roach    /**
855a25f0a04SGreg Roach     * Count the children belonging to this individual.
856a25f0a04SGreg Roach     *
857cbc1590aSGreg Roach     * @return int
858a25f0a04SGreg Roach     */
859c1010edaSGreg Roach    public function getNumberOfChildren()
860c1010edaSGreg Roach    {
861a25f0a04SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
8623dc7bbe9SGreg Roach            return (int) $match[1];
863b2ce94c6SRico Sonntag        }
864b2ce94c6SRico Sonntag
86513abd6f3SGreg Roach        $children = [];
866a25f0a04SGreg Roach        foreach ($this->getSpouseFamilies() as $fam) {
867a25f0a04SGreg Roach            foreach ($fam->getChildren() as $child) {
868*c0935879SGreg Roach                $children[$child->xref()] = true;
869a25f0a04SGreg Roach            }
870a25f0a04SGreg Roach        }
871a25f0a04SGreg Roach
872a25f0a04SGreg Roach        return count($children);
873a25f0a04SGreg Roach    }
874a25f0a04SGreg Roach
875a25f0a04SGreg Roach    /**
876a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
877a25f0a04SGreg Roach     *
878cbc1590aSGreg Roach     * @param int|null $access_level
879a25f0a04SGreg Roach     *
880a25f0a04SGreg Roach     * @return Family[]
881a25f0a04SGreg Roach     */
8828f53f488SRico Sonntag    public function getChildFamilies($access_level = null): array
883c1010edaSGreg Roach    {
8844b9ff166SGreg Roach        if ($access_level === null) {
8854b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8864b9ff166SGreg Roach        }
8874b9ff166SGreg Roach
888acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
889a25f0a04SGreg Roach
89013abd6f3SGreg Roach        $families = [];
891a25f0a04SGreg Roach        foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
892dc124885SGreg Roach            $family = $fact->target();
893e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
894a25f0a04SGreg Roach                $families[] = $family;
895a25f0a04SGreg Roach            }
896a25f0a04SGreg Roach        }
897a25f0a04SGreg Roach
898a25f0a04SGreg Roach        return $families;
899a25f0a04SGreg Roach    }
900a25f0a04SGreg Roach
901a25f0a04SGreg Roach    /**
902a25f0a04SGreg Roach     * Get the preferred parents for this individual.
903a25f0a04SGreg Roach     *
904a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
905a25f0a04SGreg Roach     * The preferred family record is:
906a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
907a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
908a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
909a25f0a04SGreg Roach     * (d) the first one found
910a25f0a04SGreg Roach     *
911a25f0a04SGreg Roach     * @return Family|null
912a25f0a04SGreg Roach     */
913c1010edaSGreg Roach    public function getPrimaryChildFamily()
914c1010edaSGreg Roach    {
915a25f0a04SGreg Roach        $families = $this->getChildFamilies();
916a25f0a04SGreg Roach        switch (count($families)) {
917a25f0a04SGreg Roach            case 0:
918a25f0a04SGreg Roach                return null;
919a25f0a04SGreg Roach            case 1:
92015d603e7SGreg Roach                return $families[0];
921a25f0a04SGreg Roach            default:
922a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
923a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
92474e78bfaSJonathan Jaubart                foreach ($families as $fam) {
925*c0935879SGreg Roach                    $famid = $fam->xref();
926a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
927a25f0a04SGreg Roach                        return $fam;
928a25f0a04SGreg Roach                    }
929a25f0a04SGreg Roach                }
930a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
93174e78bfaSJonathan Jaubart                foreach ($families as $fam) {
932*c0935879SGreg Roach                    $famid = $fam->xref();
933a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
934a25f0a04SGreg Roach                        return $fam;
935a25f0a04SGreg Roach                    }
936a25f0a04SGreg Roach                }
937a25f0a04SGreg Roach                // c) records with no '2 PEDI'
93874e78bfaSJonathan Jaubart                foreach ($families as $fam) {
939*c0935879SGreg Roach                    $famid = $fam->xref();
940a25f0a04SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
941a25f0a04SGreg Roach                        return $fam;
942a25f0a04SGreg Roach                    }
943a25f0a04SGreg Roach                }
944a25f0a04SGreg Roach
945a25f0a04SGreg Roach                // d) any record
94615d603e7SGreg Roach                return $families[0];
947a25f0a04SGreg Roach        }
948a25f0a04SGreg Roach    }
949a25f0a04SGreg Roach
950a25f0a04SGreg Roach    /**
951a25f0a04SGreg Roach     * Get a list of step-parent families.
952a25f0a04SGreg Roach     *
953a25f0a04SGreg Roach     * @return Family[]
954a25f0a04SGreg Roach     */
9558f53f488SRico Sonntag    public function getChildStepFamilies(): array
956c1010edaSGreg Roach    {
95713abd6f3SGreg Roach        $step_families = [];
958a25f0a04SGreg Roach        $families      = $this->getChildFamilies();
959a25f0a04SGreg Roach        foreach ($families as $family) {
960a25f0a04SGreg Roach            $father = $family->getHusband();
961a25f0a04SGreg Roach            if ($father) {
962a25f0a04SGreg Roach                foreach ($father->getSpouseFamilies() as $step_family) {
963a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
964a25f0a04SGreg Roach                        $step_families[] = $step_family;
965a25f0a04SGreg Roach                    }
966a25f0a04SGreg Roach                }
967a25f0a04SGreg Roach            }
968a25f0a04SGreg Roach            $mother = $family->getWife();
969a25f0a04SGreg Roach            if ($mother) {
970a25f0a04SGreg Roach                foreach ($mother->getSpouseFamilies() as $step_family) {
971a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
972a25f0a04SGreg Roach                        $step_families[] = $step_family;
973a25f0a04SGreg Roach                    }
974a25f0a04SGreg Roach                }
975a25f0a04SGreg Roach            }
976a25f0a04SGreg Roach        }
977a25f0a04SGreg Roach
978a25f0a04SGreg Roach        return $step_families;
979a25f0a04SGreg Roach    }
980a25f0a04SGreg Roach
981a25f0a04SGreg Roach    /**
982a25f0a04SGreg Roach     * Get a list of step-parent families.
983a25f0a04SGreg Roach     *
984a25f0a04SGreg Roach     * @return Family[]
985a25f0a04SGreg Roach     */
9868f53f488SRico Sonntag    public function getSpouseStepFamilies(): array
987c1010edaSGreg Roach    {
98813abd6f3SGreg Roach        $step_families = [];
989a25f0a04SGreg Roach        $families      = $this->getSpouseFamilies();
990a25f0a04SGreg Roach        foreach ($families as $family) {
991a25f0a04SGreg Roach            $spouse = $family->getSpouse($this);
992a25f0a04SGreg Roach            if ($spouse) {
993a25f0a04SGreg Roach                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
994a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
995a25f0a04SGreg Roach                        $step_families[] = $step_family;
996a25f0a04SGreg Roach                    }
997a25f0a04SGreg Roach                }
998a25f0a04SGreg Roach            }
999a25f0a04SGreg Roach        }
1000a25f0a04SGreg Roach
1001a25f0a04SGreg Roach        return $step_families;
1002a25f0a04SGreg Roach    }
1003a25f0a04SGreg Roach
1004a25f0a04SGreg Roach    /**
1005a25f0a04SGreg Roach     * A label for a parental family group
1006a25f0a04SGreg Roach     *
1007a25f0a04SGreg Roach     * @param Family $family
1008a25f0a04SGreg Roach     *
1009a25f0a04SGreg Roach     * @return string
1010a25f0a04SGreg Roach     */
1011c1010edaSGreg Roach    public function getChildFamilyLabel(Family $family)
1012c1010edaSGreg Roach    {
1013*c0935879SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
1014a25f0a04SGreg Roach            // A specified pedigree
1015764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1016b2ce94c6SRico Sonntag        }
1017b2ce94c6SRico Sonntag
1018a25f0a04SGreg Roach        // Default (birth) pedigree
1019764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1020a25f0a04SGreg Roach    }
1021a25f0a04SGreg Roach
1022a25f0a04SGreg Roach    /**
1023a25f0a04SGreg Roach     * Create a label for a step family
1024a25f0a04SGreg Roach     *
1025a25f0a04SGreg Roach     * @param Family $step_family
1026a25f0a04SGreg Roach     *
1027a25f0a04SGreg Roach     * @return string
1028a25f0a04SGreg Roach     */
10298f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1030c1010edaSGreg Roach    {
1031a25f0a04SGreg Roach        foreach ($this->getChildFamilies() as $family) {
1032a25f0a04SGreg Roach            if ($family !== $step_family) {
1033a25f0a04SGreg Roach                // Must be a step-family
1034a25f0a04SGreg Roach                foreach ($family->getSpouses() as $parent) {
1035a25f0a04SGreg Roach                    foreach ($step_family->getSpouses() as $step_parent) {
1036a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1037a25f0a04SGreg Roach                            // One common parent - must be a step family
1038a25f0a04SGreg Roach                            if ($parent->getSex() == 'M') {
1039a25f0a04SGreg Roach                                // Father’s family with someone else
1040a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1041a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1042bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1043b2ce94c6SRico Sonntag                                }
1044b2ce94c6SRico Sonntag
1045a25f0a04SGreg Roach                                /* I18N: A step-family. */
1046bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1047a25f0a04SGreg Roach                            }
1048b2ce94c6SRico Sonntag
1049a25f0a04SGreg Roach                            // Mother’s family with someone else
1050a25f0a04SGreg Roach                            if ($step_family->getSpouse($step_parent)) {
1051a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
1052bbb76c12SGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1053b2ce94c6SRico Sonntag                            }
1054b2ce94c6SRico Sonntag
1055a25f0a04SGreg Roach                            /* I18N: A step-family. */
1056bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1057a25f0a04SGreg Roach                        }
1058a25f0a04SGreg Roach                    }
1059a25f0a04SGreg Roach                }
1060a25f0a04SGreg Roach            }
1061a25f0a04SGreg Roach        }
1062a25f0a04SGreg Roach
1063a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1064a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1065a25f0a04SGreg Roach    }
1066a25f0a04SGreg Roach
1067225e381fSGreg Roach
1068225e381fSGreg Roach    /**
1069225e381fSGreg Roach     * Get the description for the family.
1070225e381fSGreg Roach     *
1071225e381fSGreg Roach     * For example, "XXX's family with new wife".
1072225e381fSGreg Roach     *
1073225e381fSGreg Roach     * @param Family $family
1074225e381fSGreg Roach     *
1075225e381fSGreg Roach     * @return string
1076225e381fSGreg Roach     */
1077c1010edaSGreg Roach    public function getSpouseFamilyLabel(Family $family)
1078c1010edaSGreg Roach    {
1079225e381fSGreg Roach        $spouse = $family->getSpouse($this);
1080225e381fSGreg Roach        if ($spouse) {
1081225e381fSGreg Roach            /* I18N: %s is the spouse name */
1082bbb76c12SGreg Roach            return I18N::translate('Family with %s', $spouse->getFullName());
1083225e381fSGreg Roach        }
1084b2ce94c6SRico Sonntag
1085b2ce94c6SRico Sonntag        return $family->getFullName();
1086225e381fSGreg Roach    }
1087225e381fSGreg Roach
1088a25f0a04SGreg Roach    /**
1089a25f0a04SGreg Roach     * get primary parents names for this individual
1090a25f0a04SGreg Roach     *
1091a25f0a04SGreg Roach     * @param string $classname optional css class
1092a25f0a04SGreg Roach     * @param string $display   optional css style display
1093a25f0a04SGreg Roach     *
1094a25f0a04SGreg Roach     * @return string a div block with father & mother names
1095a25f0a04SGreg Roach     */
10968f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1097c1010edaSGreg Roach    {
1098a25f0a04SGreg Roach        $fam = $this->getPrimaryChildFamily();
1099a25f0a04SGreg Roach        if (!$fam) {
1100a25f0a04SGreg Roach            return '';
1101a25f0a04SGreg Roach        }
1102a25f0a04SGreg Roach        $txt = '<div';
1103a25f0a04SGreg Roach        if ($classname) {
11044c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1105a25f0a04SGreg Roach        }
1106a25f0a04SGreg Roach        if ($display) {
11074c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1108a25f0a04SGreg Roach        }
1109a25f0a04SGreg Roach        $txt .= '>';
1110a25f0a04SGreg Roach        $husb = $fam->getHusband();
1111a25f0a04SGreg Roach        if ($husb) {
1112a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1113a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1114a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1115a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1116a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
1117bbb76c12SGreg Roach            $txt .= I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1118a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1119a25f0a04SGreg Roach        }
1120a25f0a04SGreg Roach        $wife = $fam->getWife();
1121a25f0a04SGreg Roach        if ($wife) {
1122a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1123a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1124a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1125a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1126a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
1127bbb76c12SGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->getFullName());
1128a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1129a25f0a04SGreg Roach        }
1130a25f0a04SGreg Roach        $txt .= '</div>';
1131a25f0a04SGreg Roach
1132a25f0a04SGreg Roach        return $txt;
1133a25f0a04SGreg Roach    }
1134a25f0a04SGreg Roach
1135a25f0a04SGreg Roach    /** {@inheritdoc} */
11368f53f488SRico Sonntag    public function getFallBackName(): string
1137c1010edaSGreg Roach    {
1138a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1139a25f0a04SGreg Roach    }
1140a25f0a04SGreg Roach
1141a25f0a04SGreg Roach    /**
1142a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1143a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1144a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1145a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1146a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1147a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1148a25f0a04SGreg Roach     *
1149a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1150a25f0a04SGreg Roach     * 2 GIVN Robert
1151a25f0a04SGreg Roach     * 2 SPFX de
1152a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1153a25f0a04SGreg Roach     * 2 NICK The Bald
1154a25f0a04SGreg Roach     *
1155a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1156a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1157a25f0a04SGreg Roach     *
1158a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1159a25f0a04SGreg Roach     *
1160a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1161a25f0a04SGreg Roach     * or
1162a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1163a25f0a04SGreg Roach     * 2 GIVN Carlos
1164a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1165a25f0a04SGreg Roach     *
1166a25f0a04SGreg Roach     * @param string $type
1167a25f0a04SGreg Roach     * @param string $full
1168a25f0a04SGreg Roach     * @param string $gedcom
1169a25f0a04SGreg Roach     */
117076f666f4SGreg Roach    protected function addName(string $type, string $full, string $gedcom)
1171c1010edaSGreg Roach    {
1172a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1173a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1174a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1175a25f0a04SGreg Roach
117676f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1177a25f0a04SGreg Roach        $GIVN = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1178a25f0a04SGreg Roach        $SURN = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1179a25f0a04SGreg Roach        $NICK = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1180a25f0a04SGreg Roach
1181a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
118276f666f4SGreg Roach        if ($SURN !== '') {
1183a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1184a25f0a04SGreg Roach        } else {
118513abd6f3SGreg Roach            $SURNS = [];
1186a25f0a04SGreg Roach        }
118776f666f4SGreg Roach
1188a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1189a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1190a25f0a04SGreg Roach
1191a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1192a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1193a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1194a25f0a04SGreg Roach
1195a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
119676f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1197a25f0a04SGreg Roach            $full = $full . '/';
1198a25f0a04SGreg Roach        }
1199a25f0a04SGreg Roach
1200a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1201a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1202a25f0a04SGreg Roach
1203a25f0a04SGreg Roach        // Extract the surname.
1204a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1205a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1206a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1207a25f0a04SGreg Roach        } else {
1208a25f0a04SGreg Roach            $surname = '';
1209a25f0a04SGreg Roach        }
1210a25f0a04SGreg Roach
1211a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1212a25f0a04SGreg Roach        if (!$SURNS) {
1213a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1214a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1215a25f0a04SGreg Roach                $SURNS = $matches[1];
1216a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1217a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1218a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1219a25f0a04SGreg Roach                }
1220a25f0a04SGreg Roach            } else {
1221a25f0a04SGreg Roach                // It is valid not to have a surname at all
122213abd6f3SGreg Roach                $SURNS = [''];
1223a25f0a04SGreg Roach            }
1224a25f0a04SGreg Roach        }
1225a25f0a04SGreg Roach
1226a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1227a25f0a04SGreg Roach        if (!$GIVN) {
1228a25f0a04SGreg Roach            $GIVN = preg_replace(
122913abd6f3SGreg Roach                [
1230c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1231c1010edaSGreg Roach                    // remove surname
1232c1010edaSGreg Roach                    '/ ?".+"/',
1233c1010edaSGreg Roach                    // remove nickname
1234c1010edaSGreg Roach                    '/ {2,}/',
1235c1010edaSGreg Roach                    // multiple spaces, caused by the above
1236c1010edaSGreg Roach                    '/^ | $/',
1237c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
123813abd6f3SGreg Roach                ],
123913abd6f3SGreg Roach                [
1240a25f0a04SGreg Roach                    ' ',
1241a25f0a04SGreg Roach                    ' ',
1242a25f0a04SGreg Roach                    ' ',
1243a25f0a04SGreg Roach                    '',
124413abd6f3SGreg Roach                ],
1245a25f0a04SGreg Roach                $full
1246a25f0a04SGreg Roach            );
1247a25f0a04SGreg Roach        }
1248a25f0a04SGreg Roach
1249a25f0a04SGreg Roach        // Add placeholder for unknown given name
1250a25f0a04SGreg Roach        if (!$GIVN) {
1251a25f0a04SGreg Roach            $GIVN = '@P.N.';
125273f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1253a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1254a25f0a04SGreg Roach        }
1255a25f0a04SGreg Roach
12567b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12577b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1258c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12597b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1260c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1261a25f0a04SGreg Roach        }
1262a25f0a04SGreg Roach
1263a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1264a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1265a25f0a04SGreg Roach        // $full is for display on-screen
1266a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1267a25f0a04SGreg Roach
1268a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1269ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1270ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1271c6f196c3SGreg Roach        // Format for display
1272d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1273acc34ea1SGreg Roach        // Localise quotation marks around the nickname
127418d7a90dSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function (array $matches): string {
12758d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12768d68cabeSGreg Roach        }, $full);
1277a25f0a04SGreg Roach
1278c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1279a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1280a25f0a04SGreg Roach
1281a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1282a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1283a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1284a25f0a04SGreg Roach
1285ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1286a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1287a25f0a04SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1288a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1289a25f0a04SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1290a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1291a25f0a04SGreg Roach            }
1292a25f0a04SGreg Roach
1293bdb3725aSGreg Roach            $this->getAllNames[] = [
1294a25f0a04SGreg Roach                'type'    => $type,
1295a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1296c1010edaSGreg Roach                'full'    => $full,
1297c1010edaSGreg Roach                // This is used for display
1298c1010edaSGreg Roach                'fullNN'  => $fullNN,
1299c1010edaSGreg Roach                // This goes into the database
1300c1010edaSGreg Roach                'surname' => $surname,
1301c1010edaSGreg Roach                // This goes into the database
1302c1010edaSGreg Roach                'givn'    => $GIVN,
1303c1010edaSGreg Roach                // This goes into the database
1304c1010edaSGreg Roach                'surn'    => $SURN,
1305c1010edaSGreg Roach                // This goes into the database
130613abd6f3SGreg Roach            ];
1307a25f0a04SGreg Roach        }
1308a25f0a04SGreg Roach    }
1309a25f0a04SGreg Roach
1310a25f0a04SGreg Roach    /**
131176692c8bSGreg Roach     * Extract names from the GEDCOM record.
1312c7ff4153SGreg Roach     *
1313c7ff4153SGreg Roach     * @return void
1314a25f0a04SGreg Roach     */
1315c1010edaSGreg Roach    public function extractNames()
1316c1010edaSGreg Roach    {
13178f53f488SRico Sonntag        $this->extractNamesFromFacts(
13188f53f488SRico Sonntag            1,
13198f53f488SRico Sonntag            'NAME',
13208f53f488SRico Sonntag            $this->getFacts(
13218f53f488SRico Sonntag                'NAME',
13228f53f488SRico Sonntag                false,
13238f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13248f53f488SRico Sonntag                $this->canShowName()
13258f53f488SRico Sonntag            )
13268f53f488SRico Sonntag        );
1327a25f0a04SGreg Roach    }
1328a25f0a04SGreg Roach
1329a25f0a04SGreg Roach    /**
1330a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1331a25f0a04SGreg Roach     * selection items or favorites.
1332a25f0a04SGreg Roach     *
1333a25f0a04SGreg Roach     * @return string
1334a25f0a04SGreg Roach     */
13358f53f488SRico Sonntag    public function formatListDetails(): string
1336c1010edaSGreg Roach    {
1337a25f0a04SGreg Roach        return
1338841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1339841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1340a25f0a04SGreg Roach    }
1341a25f0a04SGreg Roach}
1342