xref: /webtrees/app/Individual.php (revision 16d6367af0fc87302889bd9d5831cab67c1a54f0)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
222e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
23a25f0a04SGreg Roach
24a25f0a04SGreg Roach/**
2576692c8bSGreg Roach * A GEDCOM individual (INDI) object.
26a25f0a04SGreg Roach */
27c1010edaSGreg Roachclass Individual extends GedcomRecord
28c1010edaSGreg Roach{
29*16d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
30*16d6367aSGreg Roach
31*16d6367aSGreg Roach    protected const ROUTE_NAME  = 'individual';
32a25f0a04SGreg Roach
3376692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3476692c8bSGreg Roach    public $generation;
35a25f0a04SGreg Roach
36a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
374686330aSGreg Roach    private $estimated_birth_date;
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach    /** @var Date The estimated date of death */
404686330aSGreg Roach    private $estimated_death_date;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach    /**
43e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
44e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
45e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
46e71ef9d2SGreg Roach     *
47e71ef9d2SGreg Roach     * @param string      $xref
48e71ef9d2SGreg Roach     * @param Tree        $tree
49e71ef9d2SGreg Roach     * @param string|null $gedcom
50e71ef9d2SGreg Roach     *
51e71ef9d2SGreg Roach     * @throws \Exception
52e71ef9d2SGreg Roach     * @return Individual|null
53e71ef9d2SGreg Roach     */
5476f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
55c1010edaSGreg Roach    {
56e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
57e71ef9d2SGreg Roach
58e71ef9d2SGreg Roach        if ($record instanceof Individual) {
59e71ef9d2SGreg Roach            return $record;
60e71ef9d2SGreg Roach        }
61b2ce94c6SRico Sonntag
62b2ce94c6SRico Sonntag        return null;
63e71ef9d2SGreg Roach    }
64e71ef9d2SGreg Roach
65e71ef9d2SGreg Roach    /**
66395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
67395f0fe0SGreg Roach     * Typically when we load families and their members.
68395f0fe0SGreg Roach     *
69395f0fe0SGreg Roach     * @param Tree     $tree
704a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
7118d7a90dSGreg Roach     *
7218d7a90dSGreg Roach     * @return void
73395f0fe0SGreg Roach     */
742e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
75c1010edaSGreg Roach    {
762e5b4452SGreg Roach        $rows = DB::table('individuals')
772e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
782e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
792e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
802e5b4452SGreg Roach            ->get();
81395f0fe0SGreg Roach
82395f0fe0SGreg Roach        foreach ($rows as $row) {
83395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
84395f0fe0SGreg Roach        }
85395f0fe0SGreg Roach    }
86395f0fe0SGreg Roach
87395f0fe0SGreg Roach    /**
88a25f0a04SGreg Roach     * Can the name of this record be shown?
89a25f0a04SGreg Roach     *
9076692c8bSGreg Roach     * @param int|null $access_level
9176692c8bSGreg Roach     *
9276692c8bSGreg Roach     * @return bool
93a25f0a04SGreg Roach     */
9435584196SGreg Roach    public function canShowName(int $access_level = null): bool
95c1010edaSGreg Roach    {
964b9ff166SGreg Roach        if ($access_level === null) {
974b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
984b9ff166SGreg Roach        }
994b9ff166SGreg Roach
100518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
101a25f0a04SGreg Roach    }
102a25f0a04SGreg Roach
103a25f0a04SGreg Roach    /**
10476692c8bSGreg Roach     * Can this individual be shown?
105a25f0a04SGreg Roach     *
10676692c8bSGreg Roach     * @param int $access_level
10776692c8bSGreg Roach     *
10876692c8bSGreg Roach     * @return bool
109a25f0a04SGreg Roach     */
11035584196SGreg Roach    protected function canShowByType(int $access_level): bool
111c1010edaSGreg Roach    {
112a25f0a04SGreg Roach        // Dead people...
113518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
114a25f0a04SGreg Roach            $keep_alive             = false;
11554ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
116a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1178d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
118a25f0a04SGreg Roach                foreach ($matches as $match) {
119a25f0a04SGreg Roach                    $date = new Date($match[1]);
120a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
121a25f0a04SGreg Roach                        $keep_alive = true;
122a25f0a04SGreg Roach                        break;
123a25f0a04SGreg Roach                    }
124a25f0a04SGreg Roach                }
125a25f0a04SGreg Roach            }
12654ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
127a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1288d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
129a25f0a04SGreg Roach                foreach ($matches as $match) {
130a25f0a04SGreg Roach                    $date = new Date($match[1]);
131a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
132a25f0a04SGreg Roach                        $keep_alive = true;
133a25f0a04SGreg Roach                        break;
134a25f0a04SGreg Roach                    }
135a25f0a04SGreg Roach                }
136a25f0a04SGreg Roach            }
137a25f0a04SGreg Roach            if (!$keep_alive) {
138a25f0a04SGreg Roach                return true;
139a25f0a04SGreg Roach            }
140a25f0a04SGreg Roach        }
141a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
14254ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1434b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
14454ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1454b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
146a25f0a04SGreg Roach        }
147a25f0a04SGreg Roach
148a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1494b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
150a25f0a04SGreg Roach    }
151a25f0a04SGreg Roach
152a25f0a04SGreg Roach    /**
153a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
154a25f0a04SGreg Roach     *
155a25f0a04SGreg Roach     * @param Individual $target
156cbc1590aSGreg Roach     * @param int        $distance
157a25f0a04SGreg Roach     *
158cbc1590aSGreg Roach     * @return bool
159a25f0a04SGreg Roach     */
1608f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
161c1010edaSGreg Roach    {
162a25f0a04SGreg Roach        static $cache = null;
163a25f0a04SGreg Roach
16406ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
165a25f0a04SGreg Roach        if ($user_individual) {
166a25f0a04SGreg Roach            if (!$cache) {
16713abd6f3SGreg Roach                $cache = [
16813abd6f3SGreg Roach                    0 => [$user_individual],
16913abd6f3SGreg Roach                    1 => [],
17013abd6f3SGreg Roach                ];
1718d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
172dc124885SGreg Roach                    $family = $fact->target();
173e24444eeSGreg Roach                    if ($family instanceof Family) {
174a25f0a04SGreg Roach                        $cache[1][] = $family;
175a25f0a04SGreg Roach                    }
176a25f0a04SGreg Roach                }
177a25f0a04SGreg Roach            }
178a25f0a04SGreg Roach        } else {
179a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
180a25f0a04SGreg Roach            return true;
181a25f0a04SGreg Roach        }
182a25f0a04SGreg Roach
183a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
184a25f0a04SGreg Roach        $distance *= 2;
185a25f0a04SGreg Roach
186a25f0a04SGreg Roach        // Consider each path length in turn
187a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
188a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
189a25f0a04SGreg Roach                // We have already calculated all records with this length
190a25f0a04SGreg Roach                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
191a25f0a04SGreg Roach                    return true;
192a25f0a04SGreg Roach                }
193a25f0a04SGreg Roach            } else {
194a25f0a04SGreg Roach                // Need to calculate these paths
19513abd6f3SGreg Roach                $cache[$n] = [];
196a25f0a04SGreg Roach                if ($n % 2 == 0) {
197a25f0a04SGreg Roach                    // Add FAM->INDI links
198a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
1998d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
200dc124885SGreg Roach                            $individual = $fact->target();
201a25f0a04SGreg Roach                            // Don’t backtrack
202e24444eeSGreg Roach                            if ($individual instanceof Individual && !in_array($individual, $cache[$n - 2], true)) {
203a25f0a04SGreg Roach                                $cache[$n][] = $individual;
204a25f0a04SGreg Roach                            }
205a25f0a04SGreg Roach                        }
206a25f0a04SGreg Roach                    }
207a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
208a25f0a04SGreg Roach                        return true;
209a25f0a04SGreg Roach                    }
210a25f0a04SGreg Roach                } else {
211a25f0a04SGreg Roach                    // Add INDI->FAM links
212a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2138d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
214dc124885SGreg Roach                            $family = $fact->target();
215a25f0a04SGreg Roach                            // Don’t backtrack
216e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
217a25f0a04SGreg Roach                                $cache[$n][] = $family;
218a25f0a04SGreg Roach                            }
219a25f0a04SGreg Roach                        }
220a25f0a04SGreg Roach                    }
221a25f0a04SGreg Roach                }
222a25f0a04SGreg Roach            }
223a25f0a04SGreg Roach        }
224a25f0a04SGreg Roach
225a25f0a04SGreg Roach        return false;
226a25f0a04SGreg Roach    }
227a25f0a04SGreg Roach
22876692c8bSGreg Roach    /**
22976692c8bSGreg Roach     * Generate a private version of this record
23076692c8bSGreg Roach     *
23176692c8bSGreg Roach     * @param int $access_level
23276692c8bSGreg Roach     *
23376692c8bSGreg Roach     * @return string
23476692c8bSGreg Roach     */
2353c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
236c1010edaSGreg Roach    {
237acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
238a25f0a04SGreg Roach
239a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
240518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
241a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2428d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
243138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
244a25f0a04SGreg Roach            }
245a25f0a04SGreg Roach        }
246a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2478d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
248a25f0a04SGreg Roach        foreach ($matches as $match) {
24924ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
250a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
251a25f0a04SGreg Roach                $rec .= $match[0];
252a25f0a04SGreg Roach            }
253a25f0a04SGreg Roach        }
254a25f0a04SGreg Roach        // Don’t privatize sex.
255a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
256a25f0a04SGreg Roach            $rec .= $match[0];
257a25f0a04SGreg Roach        }
258a25f0a04SGreg Roach
259a25f0a04SGreg Roach        return $rec;
260a25f0a04SGreg Roach    }
261a25f0a04SGreg Roach
26276692c8bSGreg Roach    /**
26376692c8bSGreg Roach     * Fetch data from the database
26476692c8bSGreg Roach     *
26576692c8bSGreg Roach     * @param string $xref
26676692c8bSGreg Roach     * @param int    $tree_id
26776692c8bSGreg Roach     *
26876692c8bSGreg Roach     * @return null|string
26976692c8bSGreg Roach     */
27025e95e6eSGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
271c1010edaSGreg Roach    {
2722e5b4452SGreg Roach        return DB::table('individuals')
2732e5b4452SGreg Roach            ->where('i_id', '=', $xref)
2742e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
2752e5b4452SGreg Roach            ->value('i_gedcom');
276a25f0a04SGreg Roach    }
277a25f0a04SGreg Roach
278a25f0a04SGreg Roach    /**
279a25f0a04SGreg Roach     * Static helper function to sort an array of people by birth date
280a25f0a04SGreg Roach     *
281a25f0a04SGreg Roach     * @param Individual $x
282a25f0a04SGreg Roach     * @param Individual $y
283a25f0a04SGreg Roach     *
284cbc1590aSGreg Roach     * @return int
285a25f0a04SGreg Roach     */
2868f53f488SRico Sonntag    public static function compareBirthDate(Individual $x, Individual $y): int
287c1010edaSGreg Roach    {
288f5b60decSGreg Roach        return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
289a25f0a04SGreg Roach    }
290a25f0a04SGreg Roach
291a25f0a04SGreg Roach    /**
292a25f0a04SGreg Roach     * Static helper function to sort an array of people by death date
293a25f0a04SGreg Roach     *
294a25f0a04SGreg Roach     * @param Individual $x
295a25f0a04SGreg Roach     * @param Individual $y
296a25f0a04SGreg Roach     *
297cbc1590aSGreg Roach     * @return int
298a25f0a04SGreg Roach     */
2998f53f488SRico Sonntag    public static function compareDeathDate(Individual $x, Individual $y): int
300c1010edaSGreg Roach    {
301f5b60decSGreg Roach        return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
302a25f0a04SGreg Roach    }
303a25f0a04SGreg Roach
304a25f0a04SGreg Roach    /**
305a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
306a25f0a04SGreg Roach     * If not known to be dead, then assume living.
307a25f0a04SGreg Roach     *
308cbc1590aSGreg Roach     * @return bool
309a25f0a04SGreg Roach     */
3108f53f488SRico Sonntag    public function isDead(): bool
311c1010edaSGreg Roach    {
312c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
313a25f0a04SGreg Roach
314a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3158d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
316a25f0a04SGreg Roach            return true;
317a25f0a04SGreg Roach        }
318a25f0a04SGreg Roach
319a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
320a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
321a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
322a25f0a04SGreg Roach                $date = new Date($date_match);
323f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
324a25f0a04SGreg Roach                    return true;
325a25f0a04SGreg Roach                }
326a25f0a04SGreg Roach            }
327a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
328a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
329a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
330a25f0a04SGreg Roach                return false;
331a25f0a04SGreg Roach            }
332a25f0a04SGreg Roach        }
333a25f0a04SGreg Roach
334a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
335a25f0a04SGreg Roach
336a25f0a04SGreg Roach        // Check parents (birth and adopted)
3374b9ff166SGreg Roach        foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3384b9ff166SGreg Roach            foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
339a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
340a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
341a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
342a25f0a04SGreg Roach                    $date = new Date($date_match);
343f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
344a25f0a04SGreg Roach                        return true;
345a25f0a04SGreg Roach                    }
346a25f0a04SGreg Roach                }
347a25f0a04SGreg Roach            }
348a25f0a04SGreg Roach        }
349a25f0a04SGreg Roach
350a25f0a04SGreg Roach        // Check spouses
3514b9ff166SGreg Roach        foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
352a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
353a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
354a25f0a04SGreg Roach                $date = new Date($date_match);
355a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
356f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
357a25f0a04SGreg Roach                    return true;
358a25f0a04SGreg Roach                }
359a25f0a04SGreg Roach            }
360a25f0a04SGreg Roach            // Check spouse dates
36113e3123bSGreg Roach            $spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
362a25f0a04SGreg Roach            if ($spouse) {
363a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
364a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
365a25f0a04SGreg Roach                    $date = new Date($date_match);
366a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
367f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
368a25f0a04SGreg Roach                        return true;
369a25f0a04SGreg Roach                    }
370a25f0a04SGreg Roach                }
371a25f0a04SGreg Roach            }
372a25f0a04SGreg Roach            // Check child dates
3734b9ff166SGreg Roach            foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
374a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
375a25f0a04SGreg Roach                // Assume children born after age of 15
376a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
377a25f0a04SGreg Roach                    $date = new Date($date_match);
378f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
379a25f0a04SGreg Roach                        return true;
380a25f0a04SGreg Roach                    }
381a25f0a04SGreg Roach                }
382a25f0a04SGreg Roach                // Check grandchildren
3834b9ff166SGreg Roach                foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
3844b9ff166SGreg Roach                    foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
385a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
386a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
387a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
388a25f0a04SGreg Roach                            $date = new Date($date_match);
389f5b60decSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
390a25f0a04SGreg Roach                                return true;
391a25f0a04SGreg Roach                            }
392a25f0a04SGreg Roach                        }
393a25f0a04SGreg Roach                    }
394a25f0a04SGreg Roach                }
395a25f0a04SGreg Roach            }
396a25f0a04SGreg Roach        }
397a25f0a04SGreg Roach
398a25f0a04SGreg Roach        return false;
399a25f0a04SGreg Roach    }
400a25f0a04SGreg Roach
401a25f0a04SGreg Roach    /**
402a25f0a04SGreg Roach     * Find the highlighted media object for an individual
403a25f0a04SGreg Roach     *
4044a9f750fSGreg Roach     * @return null|MediaFile
405a25f0a04SGreg Roach     */
406c1010edaSGreg Roach    public function findHighlightedMediaFile()
407c1010edaSGreg Roach    {
4088d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
409dc124885SGreg Roach            $media = $fact->target();
410a213a63cSGreg Roach            if ($media instanceof Media) {
4114a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
412a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4134a9f750fSGreg Roach                        return $media_file;
4144a9f750fSGreg Roach                    }
4154a9f750fSGreg Roach                }
416a25f0a04SGreg Roach            }
417a25f0a04SGreg Roach        }
418a25f0a04SGreg Roach
419a25f0a04SGreg Roach        return null;
420a25f0a04SGreg Roach    }
421a25f0a04SGreg Roach
422a25f0a04SGreg Roach    /**
423a25f0a04SGreg Roach     * Display the prefered image for this individual.
424a25f0a04SGreg Roach     * Use an icon if no image is available.
425a25f0a04SGreg Roach     *
426dce07401SGreg Roach     * @param int      $width      Pixels
427dce07401SGreg Roach     * @param int      $height     Pixels
428dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
429dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
430dce07401SGreg Roach     *
431a25f0a04SGreg Roach     * @return string
432a25f0a04SGreg Roach     */
4338f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
434c1010edaSGreg Roach    {
4354a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4364a9f750fSGreg Roach
4374a9f750fSGreg Roach        if ($media_file !== null) {
4384a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
439a25f0a04SGreg Roach        }
4404a9f750fSGreg Roach
4414a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
4424a9f750fSGreg Roach            return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
4434a9f750fSGreg Roach        }
4444a9f750fSGreg Roach
4454a9f750fSGreg Roach        return '';
446a25f0a04SGreg Roach    }
447a25f0a04SGreg Roach
448a25f0a04SGreg Roach    /**
449a25f0a04SGreg Roach     * Get the date of birth
450a25f0a04SGreg Roach     *
451a25f0a04SGreg Roach     * @return Date
452a25f0a04SGreg Roach     */
4538f53f488SRico Sonntag    public function getBirthDate(): Date
454c1010edaSGreg Roach    {
455a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
456a25f0a04SGreg Roach            if ($date->isOK()) {
457a25f0a04SGreg Roach                return $date;
458a25f0a04SGreg Roach            }
459a25f0a04SGreg Roach        }
460a25f0a04SGreg Roach
461a25f0a04SGreg Roach        return new Date('');
462a25f0a04SGreg Roach    }
463a25f0a04SGreg Roach
464a25f0a04SGreg Roach    /**
465a25f0a04SGreg Roach     * Get the place of birth
466a25f0a04SGreg Roach     *
46716d0b7f7SRico Sonntag     * @return Place
468a25f0a04SGreg Roach     */
4698f53f488SRico Sonntag    public function getBirthPlace(): Place
470c1010edaSGreg Roach    {
471a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
472a25f0a04SGreg Roach            return $place;
473a25f0a04SGreg Roach        }
474a25f0a04SGreg Roach
475b20ddbf9SGreg Roach        return new Place('', $this->tree);
476a25f0a04SGreg Roach    }
477a25f0a04SGreg Roach
478a25f0a04SGreg Roach    /**
479a25f0a04SGreg Roach     * Get the year of birth
480a25f0a04SGreg Roach     *
481a25f0a04SGreg Roach     * @return string the year of birth
482a25f0a04SGreg Roach     */
4838f53f488SRico Sonntag    public function getBirthYear(): string
484c1010edaSGreg Roach    {
485f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
486a25f0a04SGreg Roach    }
487a25f0a04SGreg Roach
488a25f0a04SGreg Roach    /**
489a25f0a04SGreg Roach     * Get the date of death
490a25f0a04SGreg Roach     *
491a25f0a04SGreg Roach     * @return Date
492a25f0a04SGreg Roach     */
4938f53f488SRico Sonntag    public function getDeathDate(): Date
494c1010edaSGreg Roach    {
495a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
496a25f0a04SGreg Roach            if ($date->isOK()) {
497a25f0a04SGreg Roach                return $date;
498a25f0a04SGreg Roach            }
499a25f0a04SGreg Roach        }
500a25f0a04SGreg Roach
501a25f0a04SGreg Roach        return new Date('');
502a25f0a04SGreg Roach    }
503a25f0a04SGreg Roach
504a25f0a04SGreg Roach    /**
505a25f0a04SGreg Roach     * Get the place of death
506a25f0a04SGreg Roach     *
50716d0b7f7SRico Sonntag     * @return Place
508a25f0a04SGreg Roach     */
5098f53f488SRico Sonntag    public function getDeathPlace(): Place
510c1010edaSGreg Roach    {
511a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
512a25f0a04SGreg Roach            return $place;
513a25f0a04SGreg Roach        }
514a25f0a04SGreg Roach
515b20ddbf9SGreg Roach        return new Place('', $this->tree);
516a25f0a04SGreg Roach    }
517a25f0a04SGreg Roach
518a25f0a04SGreg Roach    /**
519a25f0a04SGreg Roach     * get the death year
520a25f0a04SGreg Roach     *
521a25f0a04SGreg Roach     * @return string the year of death
522a25f0a04SGreg Roach     */
5238f53f488SRico Sonntag    public function getDeathYear(): string
524c1010edaSGreg Roach    {
525f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
526a25f0a04SGreg Roach    }
527a25f0a04SGreg Roach
528a25f0a04SGreg Roach    /**
529a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
53015d603e7SGreg Roach     * Provide the place and full date using a tooltip.
531a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
532a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
533a25f0a04SGreg Roach     *
534a25f0a04SGreg Roach     * @return string
535a25f0a04SGreg Roach     */
5368f53f488SRico Sonntag    public function getLifeSpan(): string
537c1010edaSGreg Roach    {
53815d603e7SGreg Roach        // Just the first part of the place name
5391e9c2c49SGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
5401e9c2c49SGreg Roach        $death_place = strip_tags($this->getDeathPlace()->getShortName());
54115d603e7SGreg Roach        // Remove markup from dates
54215d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
54315d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
54415d603e7SGreg Roach
545c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
546bbb76c12SGreg Roach        return
547c1010edaSGreg Roach            I18N::translate(
548a25f0a04SGreg Roach                '%1$s–%2$s',
5492d4c1bedSGreg Roach                '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5502d4c1bedSGreg Roach                '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
551a25f0a04SGreg Roach            );
552a25f0a04SGreg Roach    }
553a25f0a04SGreg Roach
554a25f0a04SGreg Roach    /**
555a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
556a25f0a04SGreg Roach     *
557a25f0a04SGreg Roach     * @return Date[]
558a25f0a04SGreg Roach     */
5598f53f488SRico Sonntag    public function getAllBirthDates(): array
560c1010edaSGreg Roach    {
5618d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5628d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
563a25f0a04SGreg Roach            if ($tmp) {
564a25f0a04SGreg Roach                return $tmp;
565a25f0a04SGreg Roach            }
566a25f0a04SGreg Roach        }
567a25f0a04SGreg Roach
56813abd6f3SGreg Roach        return [];
569a25f0a04SGreg Roach    }
570a25f0a04SGreg Roach
571a25f0a04SGreg Roach    /**
572a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
573a25f0a04SGreg Roach     *
5744080d558SGreg Roach     * @return Place[]
575a25f0a04SGreg Roach     */
5768f53f488SRico Sonntag    public function getAllBirthPlaces(): array
577c1010edaSGreg Roach    {
5788d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5798d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
5804080d558SGreg Roach            if (!empty($places)) {
5814080d558SGreg Roach                return $places;
582a25f0a04SGreg Roach            }
583a25f0a04SGreg Roach        }
584a25f0a04SGreg Roach
58513abd6f3SGreg Roach        return [];
586a25f0a04SGreg Roach    }
587a25f0a04SGreg Roach
588a25f0a04SGreg Roach    /**
589a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
590a25f0a04SGreg Roach     *
591a25f0a04SGreg Roach     * @return Date[]
592a25f0a04SGreg Roach     */
5938f53f488SRico Sonntag    public function getAllDeathDates(): array
594c1010edaSGreg Roach    {
5958d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
5968d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
597a25f0a04SGreg Roach            if ($tmp) {
598a25f0a04SGreg Roach                return $tmp;
599a25f0a04SGreg Roach            }
600a25f0a04SGreg Roach        }
601a25f0a04SGreg Roach
60213abd6f3SGreg Roach        return [];
603a25f0a04SGreg Roach    }
604a25f0a04SGreg Roach
605a25f0a04SGreg Roach    /**
606a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
607a25f0a04SGreg Roach     *
6084080d558SGreg Roach     * @return Place[]
609a25f0a04SGreg Roach     */
6108f53f488SRico Sonntag    public function getAllDeathPlaces(): array
611c1010edaSGreg Roach    {
6128d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6138d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6144080d558SGreg Roach            if (!empty($places)) {
6154080d558SGreg Roach                return $places;
616a25f0a04SGreg Roach            }
617a25f0a04SGreg Roach        }
618a25f0a04SGreg Roach
61913abd6f3SGreg Roach        return [];
620a25f0a04SGreg Roach    }
621a25f0a04SGreg Roach
622a25f0a04SGreg Roach    /**
623a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
624a25f0a04SGreg Roach     *
625a25f0a04SGreg Roach     * @return Date
626a25f0a04SGreg Roach     */
6278f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
628c1010edaSGreg Roach    {
6298f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
630a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
631a25f0a04SGreg Roach                if ($date->isOK()) {
6324686330aSGreg Roach                    $this->estimated_birth_date = $date;
633a25f0a04SGreg Roach                    break;
634a25f0a04SGreg Roach                }
635a25f0a04SGreg Roach            }
6368f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
63713abd6f3SGreg Roach                $min = [];
63813abd6f3SGreg Roach                $max = [];
639a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
640f5b60decSGreg Roach                if ($tmp->isOK()) {
641f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
642f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
643a25f0a04SGreg Roach                }
644a25f0a04SGreg Roach                foreach ($this->getChildFamilies() as $family) {
645a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
646f5b60decSGreg Roach                    if ($tmp->isOK()) {
647f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
648f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
649a25f0a04SGreg Roach                    }
6502e5b4452SGreg Roach                    $husband = $family->getHusband();
6512e5b4452SGreg Roach                    if ($husband instanceof Individual) {
6522e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
653f5b60decSGreg Roach                        if ($tmp->isOK()) {
654f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
655f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
656a25f0a04SGreg Roach                        }
657a25f0a04SGreg Roach                    }
6582e5b4452SGreg Roach                    $wife = $family->getWife();
6592e5b4452SGreg Roach                    if ($wife instanceof Individual) {
6602e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
661f5b60decSGreg Roach                        if ($tmp->isOK()) {
662f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
663f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
664a25f0a04SGreg Roach                        }
665a25f0a04SGreg Roach                    }
666a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
667a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
668f5b60decSGreg Roach                        if ($tmp->isOK()) {
669f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
670f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
671a25f0a04SGreg Roach                        }
672a25f0a04SGreg Roach                    }
673a25f0a04SGreg Roach                }
674a25f0a04SGreg Roach                foreach ($this->getSpouseFamilies() as $family) {
675a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
676f5b60decSGreg Roach                    if ($tmp->isOK()) {
677f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
678f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
679a25f0a04SGreg Roach                    }
680a25f0a04SGreg Roach                    $spouse = $family->getSpouse($this);
681a25f0a04SGreg Roach                    if ($spouse) {
682a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
683f5b60decSGreg Roach                        if ($tmp->isOK()) {
684f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
685f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
686a25f0a04SGreg Roach                        }
687a25f0a04SGreg Roach                    }
688a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
689a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
690f5b60decSGreg Roach                        if ($tmp->isOK()) {
691f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
692f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
693a25f0a04SGreg Roach                        }
694a25f0a04SGreg Roach                    }
695a25f0a04SGreg Roach                }
696a25f0a04SGreg Roach                if ($min && $max) {
69759f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
698a25f0a04SGreg Roach
69965e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7004686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
701a25f0a04SGreg Roach                } else {
7024686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
703a25f0a04SGreg Roach                }
704a25f0a04SGreg Roach            }
705a25f0a04SGreg Roach        }
706a25f0a04SGreg Roach
7074686330aSGreg Roach        return $this->estimated_birth_date;
708a25f0a04SGreg Roach    }
709a25f0a04SGreg Roach
710a25f0a04SGreg Roach    /**
711a25f0a04SGreg Roach     * Generate an estimated date of death.
712a25f0a04SGreg Roach     *
713a25f0a04SGreg Roach     * @return Date
714a25f0a04SGreg Roach     */
7158f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
716c1010edaSGreg Roach    {
7174686330aSGreg Roach        if ($this->estimated_death_date === null) {
718a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
719a25f0a04SGreg Roach                if ($date->isOK()) {
7204686330aSGreg Roach                    $this->estimated_death_date = $date;
721a25f0a04SGreg Roach                    break;
722a25f0a04SGreg Roach                }
723a25f0a04SGreg Roach            }
7244686330aSGreg Roach            if ($this->estimated_death_date === null) {
725f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
726c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7274686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
728a25f0a04SGreg Roach                } else {
7294686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
730a25f0a04SGreg Roach                }
731a25f0a04SGreg Roach            }
732a25f0a04SGreg Roach        }
733a25f0a04SGreg Roach
7344686330aSGreg Roach        return $this->estimated_death_date;
735a25f0a04SGreg Roach    }
736a25f0a04SGreg Roach
737a25f0a04SGreg Roach    /**
738a25f0a04SGreg Roach     * Get the sex - M F or U
739a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
740a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
741a25f0a04SGreg Roach     *
742a25f0a04SGreg Roach     * @return string
743a25f0a04SGreg Roach     */
744c1010edaSGreg Roach    public function getSex()
745c1010edaSGreg Roach    {
746a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
747a25f0a04SGreg Roach            return $match[1];
748a25f0a04SGreg Roach        }
749b2ce94c6SRico Sonntag
750b2ce94c6SRico Sonntag        return 'U';
751a25f0a04SGreg Roach    }
752a25f0a04SGreg Roach
753a25f0a04SGreg Roach    /**
754a25f0a04SGreg Roach     * Get the individual’s sex image
755a25f0a04SGreg Roach     *
756a25f0a04SGreg Roach     * @param string $size
757a25f0a04SGreg Roach     *
758a25f0a04SGreg Roach     * @return string
759a25f0a04SGreg Roach     */
7608f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
761c1010edaSGreg Roach    {
762a25f0a04SGreg Roach        return self::sexImage($this->getSex(), $size);
763a25f0a04SGreg Roach    }
764a25f0a04SGreg Roach
765a25f0a04SGreg Roach    /**
766a25f0a04SGreg Roach     * Generate a sex icon/image
767a25f0a04SGreg Roach     *
768a25f0a04SGreg Roach     * @param string $sex
769a25f0a04SGreg Roach     * @param string $size
770a25f0a04SGreg Roach     *
771a25f0a04SGreg Roach     * @return string
772a25f0a04SGreg Roach     */
7738f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
774c1010edaSGreg Roach    {
775a25f0a04SGreg Roach        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
776a25f0a04SGreg Roach    }
777a25f0a04SGreg Roach
778a25f0a04SGreg Roach    /**
779a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
780a25f0a04SGreg Roach     *
781a25f0a04SGreg Roach     * @return string
782a25f0a04SGreg Roach     */
7838f53f488SRico Sonntag    public function getBoxStyle(): string
784c1010edaSGreg Roach    {
785c1010edaSGreg Roach        $tmp = [
786c1010edaSGreg Roach            'M' => '',
787c1010edaSGreg Roach            'F' => 'F',
788c1010edaSGreg Roach            'U' => 'NN',
789c1010edaSGreg Roach        ];
790a25f0a04SGreg Roach
791a25f0a04SGreg Roach        return 'person_box' . $tmp[$this->getSex()];
792a25f0a04SGreg Roach    }
793a25f0a04SGreg Roach
794a25f0a04SGreg Roach    /**
795a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
796a25f0a04SGreg Roach     *
797cbc1590aSGreg Roach     * @param int|null $access_level
798a25f0a04SGreg Roach     *
799a25f0a04SGreg Roach     * @return Family[]
800a25f0a04SGreg Roach     */
8018f53f488SRico Sonntag    public function getSpouseFamilies($access_level = null): array
802c1010edaSGreg Roach    {
8034b9ff166SGreg Roach        if ($access_level === null) {
8044b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8054b9ff166SGreg Roach        }
8064b9ff166SGreg Roach
807acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
808a25f0a04SGreg Roach
80913abd6f3SGreg Roach        $families = [];
8108d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
811dc124885SGreg Roach            $family = $fact->target();
812e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
813a25f0a04SGreg Roach                $families[] = $family;
814a25f0a04SGreg Roach            }
815a25f0a04SGreg Roach        }
816a25f0a04SGreg Roach
817a25f0a04SGreg Roach        return $families;
818a25f0a04SGreg Roach    }
819a25f0a04SGreg Roach
820a25f0a04SGreg Roach    /**
821a25f0a04SGreg Roach     * Get the current spouse of this individual.
822a25f0a04SGreg Roach     *
823a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
824a25f0a04SGreg Roach     * in chronological order, and take the last one found.
825a25f0a04SGreg Roach     *
826a25f0a04SGreg Roach     * @return Individual|null
827a25f0a04SGreg Roach     */
828c1010edaSGreg Roach    public function getCurrentSpouse()
829c1010edaSGreg Roach    {
830a25f0a04SGreg Roach        $tmp    = $this->getSpouseFamilies();
831a25f0a04SGreg Roach        $family = end($tmp);
832a25f0a04SGreg Roach        if ($family) {
833a25f0a04SGreg Roach            return $family->getSpouse($this);
834a25f0a04SGreg Roach        }
835b2ce94c6SRico Sonntag
836b2ce94c6SRico Sonntag        return null;
837a25f0a04SGreg Roach    }
838a25f0a04SGreg Roach
839a25f0a04SGreg Roach    /**
840a25f0a04SGreg Roach     * Count the children belonging to this individual.
841a25f0a04SGreg Roach     *
842cbc1590aSGreg Roach     * @return int
843a25f0a04SGreg Roach     */
844c1010edaSGreg Roach    public function getNumberOfChildren()
845c1010edaSGreg Roach    {
8467d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8473dc7bbe9SGreg Roach            return (int) $match[1];
848b2ce94c6SRico Sonntag        }
849b2ce94c6SRico Sonntag
85013abd6f3SGreg Roach        $children = [];
851a25f0a04SGreg Roach        foreach ($this->getSpouseFamilies() as $fam) {
852a25f0a04SGreg Roach            foreach ($fam->getChildren() as $child) {
853c0935879SGreg Roach                $children[$child->xref()] = true;
854a25f0a04SGreg Roach            }
855a25f0a04SGreg Roach        }
856a25f0a04SGreg Roach
857a25f0a04SGreg Roach        return count($children);
858a25f0a04SGreg Roach    }
859a25f0a04SGreg Roach
860a25f0a04SGreg Roach    /**
861a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
862a25f0a04SGreg Roach     *
863cbc1590aSGreg Roach     * @param int|null $access_level
864a25f0a04SGreg Roach     *
865a25f0a04SGreg Roach     * @return Family[]
866a25f0a04SGreg Roach     */
8678f53f488SRico Sonntag    public function getChildFamilies($access_level = null): array
868c1010edaSGreg Roach    {
8694b9ff166SGreg Roach        if ($access_level === null) {
8704b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8714b9ff166SGreg Roach        }
8724b9ff166SGreg Roach
873acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
874a25f0a04SGreg Roach
87513abd6f3SGreg Roach        $families = [];
8768d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
877dc124885SGreg Roach            $family = $fact->target();
878e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
879a25f0a04SGreg Roach                $families[] = $family;
880a25f0a04SGreg Roach            }
881a25f0a04SGreg Roach        }
882a25f0a04SGreg Roach
883a25f0a04SGreg Roach        return $families;
884a25f0a04SGreg Roach    }
885a25f0a04SGreg Roach
886a25f0a04SGreg Roach    /**
887a25f0a04SGreg Roach     * Get the preferred parents for this individual.
888a25f0a04SGreg Roach     *
889a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
890a25f0a04SGreg Roach     * The preferred family record is:
891a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
892a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
893a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
894a25f0a04SGreg Roach     * (d) the first one found
895a25f0a04SGreg Roach     *
896a25f0a04SGreg Roach     * @return Family|null
897a25f0a04SGreg Roach     */
898c1010edaSGreg Roach    public function getPrimaryChildFamily()
899c1010edaSGreg Roach    {
900a25f0a04SGreg Roach        $families = $this->getChildFamilies();
901a25f0a04SGreg Roach        switch (count($families)) {
902a25f0a04SGreg Roach            case 0:
903a25f0a04SGreg Roach                return null;
904a25f0a04SGreg Roach            case 1:
90515d603e7SGreg Roach                return $families[0];
906a25f0a04SGreg Roach            default:
907a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
908a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
90974e78bfaSJonathan Jaubart                foreach ($families as $fam) {
910c0935879SGreg Roach                    $famid = $fam->xref();
9117d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
912a25f0a04SGreg Roach                        return $fam;
913a25f0a04SGreg Roach                    }
914a25f0a04SGreg Roach                }
915a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
91674e78bfaSJonathan Jaubart                foreach ($families as $fam) {
917c0935879SGreg Roach                    $famid = $fam->xref();
9187d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
919a25f0a04SGreg Roach                        return $fam;
920a25f0a04SGreg Roach                    }
921a25f0a04SGreg Roach                }
922a25f0a04SGreg Roach                // c) records with no '2 PEDI'
92374e78bfaSJonathan Jaubart                foreach ($families as $fam) {
924c0935879SGreg Roach                    $famid = $fam->xref();
9257d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
926a25f0a04SGreg Roach                        return $fam;
927a25f0a04SGreg Roach                    }
928a25f0a04SGreg Roach                }
929a25f0a04SGreg Roach
930a25f0a04SGreg Roach                // d) any record
93115d603e7SGreg Roach                return $families[0];
932a25f0a04SGreg Roach        }
933a25f0a04SGreg Roach    }
934a25f0a04SGreg Roach
935a25f0a04SGreg Roach    /**
936a25f0a04SGreg Roach     * Get a list of step-parent families.
937a25f0a04SGreg Roach     *
938a25f0a04SGreg Roach     * @return Family[]
939a25f0a04SGreg Roach     */
9408f53f488SRico Sonntag    public function getChildStepFamilies(): array
941c1010edaSGreg Roach    {
94213abd6f3SGreg Roach        $step_families = [];
943a25f0a04SGreg Roach        $families      = $this->getChildFamilies();
944a25f0a04SGreg Roach        foreach ($families as $family) {
945a25f0a04SGreg Roach            $father = $family->getHusband();
946a25f0a04SGreg Roach            if ($father) {
947a25f0a04SGreg Roach                foreach ($father->getSpouseFamilies() as $step_family) {
948a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
949a25f0a04SGreg Roach                        $step_families[] = $step_family;
950a25f0a04SGreg Roach                    }
951a25f0a04SGreg Roach                }
952a25f0a04SGreg Roach            }
953a25f0a04SGreg Roach            $mother = $family->getWife();
954a25f0a04SGreg Roach            if ($mother) {
955a25f0a04SGreg Roach                foreach ($mother->getSpouseFamilies() as $step_family) {
956a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
957a25f0a04SGreg Roach                        $step_families[] = $step_family;
958a25f0a04SGreg Roach                    }
959a25f0a04SGreg Roach                }
960a25f0a04SGreg Roach            }
961a25f0a04SGreg Roach        }
962a25f0a04SGreg Roach
963a25f0a04SGreg Roach        return $step_families;
964a25f0a04SGreg Roach    }
965a25f0a04SGreg Roach
966a25f0a04SGreg Roach    /**
967a25f0a04SGreg Roach     * Get a list of step-parent families.
968a25f0a04SGreg Roach     *
969a25f0a04SGreg Roach     * @return Family[]
970a25f0a04SGreg Roach     */
9718f53f488SRico Sonntag    public function getSpouseStepFamilies(): array
972c1010edaSGreg Roach    {
97313abd6f3SGreg Roach        $step_families = [];
974a25f0a04SGreg Roach        $families      = $this->getSpouseFamilies();
975a25f0a04SGreg Roach        foreach ($families as $family) {
976a25f0a04SGreg Roach            $spouse = $family->getSpouse($this);
977a25f0a04SGreg Roach            if ($spouse) {
978a25f0a04SGreg Roach                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
979a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
980a25f0a04SGreg Roach                        $step_families[] = $step_family;
981a25f0a04SGreg Roach                    }
982a25f0a04SGreg Roach                }
983a25f0a04SGreg Roach            }
984a25f0a04SGreg Roach        }
985a25f0a04SGreg Roach
986a25f0a04SGreg Roach        return $step_families;
987a25f0a04SGreg Roach    }
988a25f0a04SGreg Roach
989a25f0a04SGreg Roach    /**
990a25f0a04SGreg Roach     * A label for a parental family group
991a25f0a04SGreg Roach     *
992a25f0a04SGreg Roach     * @param Family $family
993a25f0a04SGreg Roach     *
994a25f0a04SGreg Roach     * @return string
995a25f0a04SGreg Roach     */
996c1010edaSGreg Roach    public function getChildFamilyLabel(Family $family)
997c1010edaSGreg Roach    {
9987d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
999a25f0a04SGreg Roach            // A specified pedigree
1000764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1001b2ce94c6SRico Sonntag        }
1002b2ce94c6SRico Sonntag
1003a25f0a04SGreg Roach        // Default (birth) pedigree
1004764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1005a25f0a04SGreg Roach    }
1006a25f0a04SGreg Roach
1007a25f0a04SGreg Roach    /**
1008a25f0a04SGreg Roach     * Create a label for a step family
1009a25f0a04SGreg Roach     *
1010a25f0a04SGreg Roach     * @param Family $step_family
1011a25f0a04SGreg Roach     *
1012a25f0a04SGreg Roach     * @return string
1013a25f0a04SGreg Roach     */
10148f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1015c1010edaSGreg Roach    {
1016a25f0a04SGreg Roach        foreach ($this->getChildFamilies() as $family) {
1017a25f0a04SGreg Roach            if ($family !== $step_family) {
1018a25f0a04SGreg Roach                // Must be a step-family
1019a25f0a04SGreg Roach                foreach ($family->getSpouses() as $parent) {
1020a25f0a04SGreg Roach                    foreach ($step_family->getSpouses() as $step_parent) {
1021a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1022a25f0a04SGreg Roach                            // One common parent - must be a step family
1023a25f0a04SGreg Roach                            if ($parent->getSex() == 'M') {
1024a25f0a04SGreg Roach                                // Father’s family with someone else
1025a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1026a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1027bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1028b2ce94c6SRico Sonntag                                }
1029b2ce94c6SRico Sonntag
1030a25f0a04SGreg Roach                                /* I18N: A step-family. */
1031bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1032a25f0a04SGreg Roach                            }
1033b2ce94c6SRico Sonntag
1034a25f0a04SGreg Roach                            // Mother’s family with someone else
1035a25f0a04SGreg Roach                            if ($step_family->getSpouse($step_parent)) {
1036a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
1037bbb76c12SGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1038b2ce94c6SRico Sonntag                            }
1039b2ce94c6SRico Sonntag
1040a25f0a04SGreg Roach                            /* I18N: A step-family. */
1041bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1042a25f0a04SGreg Roach                        }
1043a25f0a04SGreg Roach                    }
1044a25f0a04SGreg Roach                }
1045a25f0a04SGreg Roach            }
1046a25f0a04SGreg Roach        }
1047a25f0a04SGreg Roach
1048a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1049a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1050a25f0a04SGreg Roach    }
1051a25f0a04SGreg Roach
1052225e381fSGreg Roach    /**
1053225e381fSGreg Roach     * Get the description for the family.
1054225e381fSGreg Roach     *
1055225e381fSGreg Roach     * For example, "XXX's family with new wife".
1056225e381fSGreg Roach     *
1057225e381fSGreg Roach     * @param Family $family
1058225e381fSGreg Roach     *
1059225e381fSGreg Roach     * @return string
1060225e381fSGreg Roach     */
1061c1010edaSGreg Roach    public function getSpouseFamilyLabel(Family $family)
1062c1010edaSGreg Roach    {
1063225e381fSGreg Roach        $spouse = $family->getSpouse($this);
1064225e381fSGreg Roach        if ($spouse) {
1065225e381fSGreg Roach            /* I18N: %s is the spouse name */
1066bbb76c12SGreg Roach            return I18N::translate('Family with %s', $spouse->getFullName());
1067225e381fSGreg Roach        }
1068b2ce94c6SRico Sonntag
1069b2ce94c6SRico Sonntag        return $family->getFullName();
1070225e381fSGreg Roach    }
1071225e381fSGreg Roach
1072a25f0a04SGreg Roach    /**
1073a25f0a04SGreg Roach     * get primary parents names for this individual
1074a25f0a04SGreg Roach     *
1075a25f0a04SGreg Roach     * @param string $classname optional css class
1076a25f0a04SGreg Roach     * @param string $display   optional css style display
1077a25f0a04SGreg Roach     *
1078a25f0a04SGreg Roach     * @return string a div block with father & mother names
1079a25f0a04SGreg Roach     */
10808f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1081c1010edaSGreg Roach    {
1082a25f0a04SGreg Roach        $fam = $this->getPrimaryChildFamily();
1083a25f0a04SGreg Roach        if (!$fam) {
1084a25f0a04SGreg Roach            return '';
1085a25f0a04SGreg Roach        }
1086a25f0a04SGreg Roach        $txt = '<div';
1087a25f0a04SGreg Roach        if ($classname) {
10884c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1089a25f0a04SGreg Roach        }
1090a25f0a04SGreg Roach        if ($display) {
10914c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1092a25f0a04SGreg Roach        }
1093a25f0a04SGreg Roach        $txt .= '>';
1094a25f0a04SGreg Roach        $husb = $fam->getHusband();
1095a25f0a04SGreg Roach        if ($husb) {
1096a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1097a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1098a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1099a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1100a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
1101bbb76c12SGreg Roach            $txt .= I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1102a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1103a25f0a04SGreg Roach        }
1104a25f0a04SGreg Roach        $wife = $fam->getWife();
1105a25f0a04SGreg Roach        if ($wife) {
1106a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1107a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1108a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1109a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1110a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
1111bbb76c12SGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->getFullName());
1112a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1113a25f0a04SGreg Roach        }
1114a25f0a04SGreg Roach        $txt .= '</div>';
1115a25f0a04SGreg Roach
1116a25f0a04SGreg Roach        return $txt;
1117a25f0a04SGreg Roach    }
1118a25f0a04SGreg Roach
1119a25f0a04SGreg Roach    /** {@inheritdoc} */
11208f53f488SRico Sonntag    public function getFallBackName(): string
1121c1010edaSGreg Roach    {
1122a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1123a25f0a04SGreg Roach    }
1124a25f0a04SGreg Roach
1125a25f0a04SGreg Roach    /**
1126a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1127a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1128a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1129a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1130a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1131a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1132a25f0a04SGreg Roach     *
1133a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1134a25f0a04SGreg Roach     * 2 GIVN Robert
1135a25f0a04SGreg Roach     * 2 SPFX de
1136a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1137a25f0a04SGreg Roach     * 2 NICK The Bald
1138a25f0a04SGreg Roach     *
1139a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1140a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1141a25f0a04SGreg Roach     *
1142a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1143a25f0a04SGreg Roach     *
1144a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1145a25f0a04SGreg Roach     * or
1146a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1147a25f0a04SGreg Roach     * 2 GIVN Carlos
1148a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1149a25f0a04SGreg Roach     *
1150a25f0a04SGreg Roach     * @param string $type
1151a25f0a04SGreg Roach     * @param string $full
1152a25f0a04SGreg Roach     * @param string $gedcom
1153a25f0a04SGreg Roach     */
115476f666f4SGreg Roach    protected function addName(string $type, string $full, string $gedcom)
1155c1010edaSGreg Roach    {
1156a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1157a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1158a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1159a25f0a04SGreg Roach
116076f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1161a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1162a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1163a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1164a25f0a04SGreg Roach
1165a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
116676f666f4SGreg Roach        if ($SURN !== '') {
1167a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1168a25f0a04SGreg Roach        } else {
116913abd6f3SGreg Roach            $SURNS = [];
1170a25f0a04SGreg Roach        }
117176f666f4SGreg Roach
1172a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1173a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1174a25f0a04SGreg Roach
1175a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1176a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1177a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1178a25f0a04SGreg Roach
1179a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
118076f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1181a25f0a04SGreg Roach            $full = $full . '/';
1182a25f0a04SGreg Roach        }
1183a25f0a04SGreg Roach
1184a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1185a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1186a25f0a04SGreg Roach
1187a25f0a04SGreg Roach        // Extract the surname.
1188a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1189a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1190a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1191a25f0a04SGreg Roach        } else {
1192a25f0a04SGreg Roach            $surname = '';
1193a25f0a04SGreg Roach        }
1194a25f0a04SGreg Roach
1195a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1196a25f0a04SGreg Roach        if (!$SURNS) {
1197a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1198a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1199a25f0a04SGreg Roach                $SURNS = $matches[1];
1200a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1201a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1202a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1203a25f0a04SGreg Roach                }
1204a25f0a04SGreg Roach            } else {
1205a25f0a04SGreg Roach                // It is valid not to have a surname at all
120613abd6f3SGreg Roach                $SURNS = [''];
1207a25f0a04SGreg Roach            }
1208a25f0a04SGreg Roach        }
1209a25f0a04SGreg Roach
1210a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1211a25f0a04SGreg Roach        if (!$GIVN) {
1212a25f0a04SGreg Roach            $GIVN = preg_replace(
121313abd6f3SGreg Roach                [
1214c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1215c1010edaSGreg Roach                    // remove surname
1216c1010edaSGreg Roach                    '/ ?".+"/',
1217c1010edaSGreg Roach                    // remove nickname
1218c1010edaSGreg Roach                    '/ {2,}/',
1219c1010edaSGreg Roach                    // multiple spaces, caused by the above
1220c1010edaSGreg Roach                    '/^ | $/',
1221c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
122213abd6f3SGreg Roach                ],
122313abd6f3SGreg Roach                [
1224a25f0a04SGreg Roach                    ' ',
1225a25f0a04SGreg Roach                    ' ',
1226a25f0a04SGreg Roach                    ' ',
1227a25f0a04SGreg Roach                    '',
122813abd6f3SGreg Roach                ],
1229a25f0a04SGreg Roach                $full
1230a25f0a04SGreg Roach            );
1231a25f0a04SGreg Roach        }
1232a25f0a04SGreg Roach
1233a25f0a04SGreg Roach        // Add placeholder for unknown given name
1234a25f0a04SGreg Roach        if (!$GIVN) {
1235a25f0a04SGreg Roach            $GIVN = '@P.N.';
123673f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1237a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1238a25f0a04SGreg Roach        }
1239a25f0a04SGreg Roach
12407b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12417b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1242c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12437b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1244c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1245a25f0a04SGreg Roach        }
1246a25f0a04SGreg Roach
1247a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1248a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1249a25f0a04SGreg Roach        // $full is for display on-screen
1250a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1251a25f0a04SGreg Roach
1252a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1253ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1254ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1255c6f196c3SGreg Roach        // Format for display
1256d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1257acc34ea1SGreg Roach        // Localise quotation marks around the nickname
125818d7a90dSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function (array $matches): string {
12598d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12608d68cabeSGreg Roach        }, $full);
1261a25f0a04SGreg Roach
1262c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1263a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1264a25f0a04SGreg Roach
1265a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1266a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1267a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1268a25f0a04SGreg Roach
1269ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1270a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1271a25f0a04SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1272a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1273a25f0a04SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1274a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1275a25f0a04SGreg Roach            }
1276a25f0a04SGreg Roach
1277bdb3725aSGreg Roach            $this->getAllNames[] = [
1278a25f0a04SGreg Roach                'type'    => $type,
1279a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1280c1010edaSGreg Roach                'full'    => $full,
1281c1010edaSGreg Roach                // This is used for display
1282c1010edaSGreg Roach                'fullNN'  => $fullNN,
1283c1010edaSGreg Roach                // This goes into the database
1284c1010edaSGreg Roach                'surname' => $surname,
1285c1010edaSGreg Roach                // This goes into the database
1286c1010edaSGreg Roach                'givn'    => $GIVN,
1287c1010edaSGreg Roach                // This goes into the database
1288c1010edaSGreg Roach                'surn'    => $SURN,
1289c1010edaSGreg Roach                // This goes into the database
129013abd6f3SGreg Roach            ];
1291a25f0a04SGreg Roach        }
1292a25f0a04SGreg Roach    }
1293a25f0a04SGreg Roach
1294a25f0a04SGreg Roach    /**
129576692c8bSGreg Roach     * Extract names from the GEDCOM record.
1296c7ff4153SGreg Roach     *
1297c7ff4153SGreg Roach     * @return void
1298a25f0a04SGreg Roach     */
1299c1010edaSGreg Roach    public function extractNames()
1300c1010edaSGreg Roach    {
13018f53f488SRico Sonntag        $this->extractNamesFromFacts(
13028f53f488SRico Sonntag            1,
13038f53f488SRico Sonntag            'NAME',
130430158ae7SGreg Roach            $this->facts(
13058d0ebef0SGreg Roach                ['NAME'],
13068f53f488SRico Sonntag                false,
13078f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13088f53f488SRico Sonntag                $this->canShowName()
13098f53f488SRico Sonntag            )
13108f53f488SRico Sonntag        );
1311a25f0a04SGreg Roach    }
1312a25f0a04SGreg Roach
1313a25f0a04SGreg Roach    /**
1314a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1315a25f0a04SGreg Roach     * selection items or favorites.
1316a25f0a04SGreg Roach     *
1317a25f0a04SGreg Roach     * @return string
1318a25f0a04SGreg Roach     */
13198f53f488SRico Sonntag    public function formatListDetails(): string
1320c1010edaSGreg Roach    {
1321a25f0a04SGreg Roach        return
13228d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13238d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1324a25f0a04SGreg Roach    }
1325a25f0a04SGreg Roach}
1326