xref: /webtrees/app/Individual.php (revision a7a24840b43d2ed5c53b60c55f1f09e1cc2f01f9)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20886b77daSGreg Roachuse Closure;
21a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
232e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
24886b77daSGreg Roachuse stdClass;
25a25f0a04SGreg Roach
26a25f0a04SGreg Roach/**
2776692c8bSGreg Roach * A GEDCOM individual (INDI) object.
28a25f0a04SGreg Roach */
29c1010edaSGreg Roachclass Individual extends GedcomRecord
30c1010edaSGreg Roach{
3116d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3216d6367aSGreg Roach
3316d6367aSGreg Roach    protected const ROUTE_NAME = 'individual';
34a25f0a04SGreg Roach
3576692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3676692c8bSGreg Roach    public $generation;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
394686330aSGreg Roach    private $estimated_birth_date;
40a25f0a04SGreg Roach
41a25f0a04SGreg Roach    /** @var Date The estimated date of death */
424686330aSGreg Roach    private $estimated_death_date;
43a25f0a04SGreg Roach
44a25f0a04SGreg Roach    /**
45886b77daSGreg Roach     * A closure which will create a record from a database row.
46886b77daSGreg Roach     *
47886b77daSGreg Roach     * @return Closure
48886b77daSGreg Roach     */
49c0804649SGreg Roach    public static function rowMapper(): Closure
50886b77daSGreg Roach    {
51c0804649SGreg Roach        return function (stdClass $row): Individual {
52*a7a24840SGreg Roach            $individual = Individual::getInstance($row->i_id, Tree::findById((int) $row->i_file), $row->i_gedcom);
53e84cf2deSGreg Roach
54e84cf2deSGreg Roach            if ($row->n_num ?? null) {
55e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
56e84cf2deSGreg Roach            }
57e84cf2deSGreg Roach
58e84cf2deSGreg Roach            return $individual;
59886b77daSGreg Roach        };
60886b77daSGreg Roach    }
61886b77daSGreg Roach
62886b77daSGreg Roach    /**
63c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
64c156e8f5SGreg Roach     *
65c156e8f5SGreg Roach     * @return Closure
66c156e8f5SGreg Roach     */
67c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
68c156e8f5SGreg Roach    {
69a9199cb2SGreg Roach        return function (Individual $x, Individual $y): int {
70c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
71c156e8f5SGreg Roach        };
72c156e8f5SGreg Roach    }
73c156e8f5SGreg Roach
74c156e8f5SGreg Roach    /**
75c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
76c156e8f5SGreg Roach     *
77c156e8f5SGreg Roach     * @return Closure
78c156e8f5SGreg Roach     */
79c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
80c156e8f5SGreg Roach    {
81a9199cb2SGreg Roach        return function (Individual $x, Individual $y): int {
82c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
83c156e8f5SGreg Roach        };
84c156e8f5SGreg Roach    }
85c156e8f5SGreg Roach
86c156e8f5SGreg Roach    /**
87e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
88e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
89e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
90e71ef9d2SGreg Roach     *
91e71ef9d2SGreg Roach     * @param string      $xref
92e71ef9d2SGreg Roach     * @param Tree        $tree
93e71ef9d2SGreg Roach     * @param string|null $gedcom
94e71ef9d2SGreg Roach     *
95e71ef9d2SGreg Roach     * @throws \Exception
96e71ef9d2SGreg Roach     * @return Individual|null
97e71ef9d2SGreg Roach     */
9876f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
99c1010edaSGreg Roach    {
100e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
101e71ef9d2SGreg Roach
102e71ef9d2SGreg Roach        if ($record instanceof Individual) {
103e71ef9d2SGreg Roach            return $record;
104e71ef9d2SGreg Roach        }
105b2ce94c6SRico Sonntag
106b2ce94c6SRico Sonntag        return null;
107e71ef9d2SGreg Roach    }
108e71ef9d2SGreg Roach
109e71ef9d2SGreg Roach    /**
110395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
111395f0fe0SGreg Roach     * Typically when we load families and their members.
112395f0fe0SGreg Roach     *
113395f0fe0SGreg Roach     * @param Tree     $tree
1144a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
11518d7a90dSGreg Roach     *
11618d7a90dSGreg Roach     * @return void
117395f0fe0SGreg Roach     */
1182e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
119c1010edaSGreg Roach    {
1202e5b4452SGreg Roach        $rows = DB::table('individuals')
1212e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1222e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1232e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1242e5b4452SGreg Roach            ->get();
125395f0fe0SGreg Roach
126395f0fe0SGreg Roach        foreach ($rows as $row) {
127395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
128395f0fe0SGreg Roach        }
129395f0fe0SGreg Roach    }
130395f0fe0SGreg Roach
131395f0fe0SGreg Roach    /**
132a25f0a04SGreg Roach     * Can the name of this record be shown?
133a25f0a04SGreg Roach     *
13476692c8bSGreg Roach     * @param int|null $access_level
13576692c8bSGreg Roach     *
13676692c8bSGreg Roach     * @return bool
137a25f0a04SGreg Roach     */
13835584196SGreg Roach    public function canShowName(int $access_level = null): bool
139c1010edaSGreg Roach    {
1404b9ff166SGreg Roach        if ($access_level === null) {
1414b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1424b9ff166SGreg Roach        }
1434b9ff166SGreg Roach
144518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
145a25f0a04SGreg Roach    }
146a25f0a04SGreg Roach
147a25f0a04SGreg Roach    /**
14876692c8bSGreg Roach     * Can this individual be shown?
149a25f0a04SGreg Roach     *
15076692c8bSGreg Roach     * @param int $access_level
15176692c8bSGreg Roach     *
15276692c8bSGreg Roach     * @return bool
153a25f0a04SGreg Roach     */
15435584196SGreg Roach    protected function canShowByType(int $access_level): bool
155c1010edaSGreg Roach    {
156a25f0a04SGreg Roach        // Dead people...
157518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
158a25f0a04SGreg Roach            $keep_alive             = false;
15954ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
160a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1618d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
162a25f0a04SGreg Roach                foreach ($matches as $match) {
163a25f0a04SGreg Roach                    $date = new Date($match[1]);
164a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
165a25f0a04SGreg Roach                        $keep_alive = true;
166a25f0a04SGreg Roach                        break;
167a25f0a04SGreg Roach                    }
168a25f0a04SGreg Roach                }
169a25f0a04SGreg Roach            }
17054ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
171a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1728d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
173a25f0a04SGreg Roach                foreach ($matches as $match) {
174a25f0a04SGreg Roach                    $date = new Date($match[1]);
175a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
176a25f0a04SGreg Roach                        $keep_alive = true;
177a25f0a04SGreg Roach                        break;
178a25f0a04SGreg Roach                    }
179a25f0a04SGreg Roach                }
180a25f0a04SGreg Roach            }
181a25f0a04SGreg Roach            if (!$keep_alive) {
182a25f0a04SGreg Roach                return true;
183a25f0a04SGreg Roach            }
184a25f0a04SGreg Roach        }
185a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
18654ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1874b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
18854ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1894b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
190a25f0a04SGreg Roach        }
191a25f0a04SGreg Roach
192a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1934b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
194a25f0a04SGreg Roach    }
195a25f0a04SGreg Roach
196a25f0a04SGreg Roach    /**
197a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
198a25f0a04SGreg Roach     *
199a25f0a04SGreg Roach     * @param Individual $target
200cbc1590aSGreg Roach     * @param int        $distance
201a25f0a04SGreg Roach     *
202cbc1590aSGreg Roach     * @return bool
203a25f0a04SGreg Roach     */
2048f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
205c1010edaSGreg Roach    {
206a25f0a04SGreg Roach        static $cache = null;
207a25f0a04SGreg Roach
20806ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
209a25f0a04SGreg Roach        if ($user_individual) {
210a25f0a04SGreg Roach            if (!$cache) {
21113abd6f3SGreg Roach                $cache = [
21213abd6f3SGreg Roach                    0 => [$user_individual],
21313abd6f3SGreg Roach                    1 => [],
21413abd6f3SGreg Roach                ];
2158d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
216dc124885SGreg Roach                    $family = $fact->target();
217e24444eeSGreg Roach                    if ($family instanceof Family) {
218a25f0a04SGreg Roach                        $cache[1][] = $family;
219a25f0a04SGreg Roach                    }
220a25f0a04SGreg Roach                }
221a25f0a04SGreg Roach            }
222a25f0a04SGreg Roach        } else {
223a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
224a25f0a04SGreg Roach            return true;
225a25f0a04SGreg Roach        }
226a25f0a04SGreg Roach
227a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
228a25f0a04SGreg Roach        $distance *= 2;
229a25f0a04SGreg Roach
230a25f0a04SGreg Roach        // Consider each path length in turn
231a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
232a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
233a25f0a04SGreg Roach                // We have already calculated all records with this length
234a25f0a04SGreg Roach                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
235a25f0a04SGreg Roach                    return true;
236a25f0a04SGreg Roach                }
237a25f0a04SGreg Roach            } else {
238a25f0a04SGreg Roach                // Need to calculate these paths
23913abd6f3SGreg Roach                $cache[$n] = [];
240a25f0a04SGreg Roach                if ($n % 2 == 0) {
241a25f0a04SGreg Roach                    // Add FAM->INDI links
242a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2438d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
244dc124885SGreg Roach                            $individual = $fact->target();
245a25f0a04SGreg Roach                            // Don’t backtrack
246e24444eeSGreg Roach                            if ($individual instanceof Individual && !in_array($individual, $cache[$n - 2], true)) {
247a25f0a04SGreg Roach                                $cache[$n][] = $individual;
248a25f0a04SGreg Roach                            }
249a25f0a04SGreg Roach                        }
250a25f0a04SGreg Roach                    }
251a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
252a25f0a04SGreg Roach                        return true;
253a25f0a04SGreg Roach                    }
254a25f0a04SGreg Roach                } else {
255a25f0a04SGreg Roach                    // Add INDI->FAM links
256a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2578d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
258dc124885SGreg Roach                            $family = $fact->target();
259a25f0a04SGreg Roach                            // Don’t backtrack
260e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
261a25f0a04SGreg Roach                                $cache[$n][] = $family;
262a25f0a04SGreg Roach                            }
263a25f0a04SGreg Roach                        }
264a25f0a04SGreg Roach                    }
265a25f0a04SGreg Roach                }
266a25f0a04SGreg Roach            }
267a25f0a04SGreg Roach        }
268a25f0a04SGreg Roach
269a25f0a04SGreg Roach        return false;
270a25f0a04SGreg Roach    }
271a25f0a04SGreg Roach
27276692c8bSGreg Roach    /**
27376692c8bSGreg Roach     * Generate a private version of this record
27476692c8bSGreg Roach     *
27576692c8bSGreg Roach     * @param int $access_level
27676692c8bSGreg Roach     *
27776692c8bSGreg Roach     * @return string
27876692c8bSGreg Roach     */
2793c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
280c1010edaSGreg Roach    {
281acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
282a25f0a04SGreg Roach
283a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
284518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
285a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2868d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
287138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
288a25f0a04SGreg Roach            }
289a25f0a04SGreg Roach        }
290a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2918d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
292a25f0a04SGreg Roach        foreach ($matches as $match) {
29324ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
294a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
295a25f0a04SGreg Roach                $rec .= $match[0];
296a25f0a04SGreg Roach            }
297a25f0a04SGreg Roach        }
298a25f0a04SGreg Roach        // Don’t privatize sex.
299a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
300a25f0a04SGreg Roach            $rec .= $match[0];
301a25f0a04SGreg Roach        }
302a25f0a04SGreg Roach
303a25f0a04SGreg Roach        return $rec;
304a25f0a04SGreg Roach    }
305a25f0a04SGreg Roach
30676692c8bSGreg Roach    /**
30776692c8bSGreg Roach     * Fetch data from the database
30876692c8bSGreg Roach     *
30976692c8bSGreg Roach     * @param string $xref
31076692c8bSGreg Roach     * @param int    $tree_id
31176692c8bSGreg Roach     *
31276692c8bSGreg Roach     * @return null|string
31376692c8bSGreg Roach     */
31425e95e6eSGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
315c1010edaSGreg Roach    {
3162e5b4452SGreg Roach        return DB::table('individuals')
3172e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3182e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3192e5b4452SGreg Roach            ->value('i_gedcom');
320a25f0a04SGreg Roach    }
321a25f0a04SGreg Roach
322a25f0a04SGreg Roach    /**
323a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
324a25f0a04SGreg Roach     * If not known to be dead, then assume living.
325a25f0a04SGreg Roach     *
326cbc1590aSGreg Roach     * @return bool
327a25f0a04SGreg Roach     */
3288f53f488SRico Sonntag    public function isDead(): bool
329c1010edaSGreg Roach    {
330c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
331a25f0a04SGreg Roach
332a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3338d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
334a25f0a04SGreg Roach            return true;
335a25f0a04SGreg Roach        }
336a25f0a04SGreg Roach
337a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
338a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
339a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
340a25f0a04SGreg Roach                $date = new Date($date_match);
341f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
342a25f0a04SGreg Roach                    return true;
343a25f0a04SGreg Roach                }
344a25f0a04SGreg Roach            }
345a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
346a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
347a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
348a25f0a04SGreg Roach                return false;
349a25f0a04SGreg Roach            }
350a25f0a04SGreg Roach        }
351a25f0a04SGreg Roach
352a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
353a25f0a04SGreg Roach
354a25f0a04SGreg Roach        // Check parents (birth and adopted)
3554b9ff166SGreg Roach        foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3564b9ff166SGreg Roach            foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
357a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
358a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
359a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
360a25f0a04SGreg Roach                    $date = new Date($date_match);
361f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
362a25f0a04SGreg Roach                        return true;
363a25f0a04SGreg Roach                    }
364a25f0a04SGreg Roach                }
365a25f0a04SGreg Roach            }
366a25f0a04SGreg Roach        }
367a25f0a04SGreg Roach
368a25f0a04SGreg Roach        // Check spouses
3694b9ff166SGreg Roach        foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
370a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
371a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
372a25f0a04SGreg Roach                $date = new Date($date_match);
373a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
374f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
375a25f0a04SGreg Roach                    return true;
376a25f0a04SGreg Roach                }
377a25f0a04SGreg Roach            }
378a25f0a04SGreg Roach            // Check spouse dates
37913e3123bSGreg Roach            $spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
380a25f0a04SGreg Roach            if ($spouse) {
381a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
382a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
383a25f0a04SGreg Roach                    $date = new Date($date_match);
384a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
385f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
386a25f0a04SGreg Roach                        return true;
387a25f0a04SGreg Roach                    }
388a25f0a04SGreg Roach                }
389a25f0a04SGreg Roach            }
390a25f0a04SGreg Roach            // Check child dates
3914b9ff166SGreg Roach            foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
392a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
393a25f0a04SGreg Roach                // Assume children born after age of 15
394a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
395a25f0a04SGreg Roach                    $date = new Date($date_match);
396f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
397a25f0a04SGreg Roach                        return true;
398a25f0a04SGreg Roach                    }
399a25f0a04SGreg Roach                }
400a25f0a04SGreg Roach                // Check grandchildren
4014b9ff166SGreg Roach                foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
4024b9ff166SGreg Roach                    foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
403a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
404a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
405a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
406a25f0a04SGreg Roach                            $date = new Date($date_match);
407f5b60decSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
408a25f0a04SGreg Roach                                return true;
409a25f0a04SGreg Roach                            }
410a25f0a04SGreg Roach                        }
411a25f0a04SGreg Roach                    }
412a25f0a04SGreg Roach                }
413a25f0a04SGreg Roach            }
414a25f0a04SGreg Roach        }
415a25f0a04SGreg Roach
416a25f0a04SGreg Roach        return false;
417a25f0a04SGreg Roach    }
418a25f0a04SGreg Roach
419a25f0a04SGreg Roach    /**
420a25f0a04SGreg Roach     * Find the highlighted media object for an individual
421a25f0a04SGreg Roach     *
4224a9f750fSGreg Roach     * @return null|MediaFile
423a25f0a04SGreg Roach     */
424c1010edaSGreg Roach    public function findHighlightedMediaFile()
425c1010edaSGreg Roach    {
4268d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
427dc124885SGreg Roach            $media = $fact->target();
428a213a63cSGreg Roach            if ($media instanceof Media) {
4294a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
430a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4314a9f750fSGreg Roach                        return $media_file;
4324a9f750fSGreg Roach                    }
4334a9f750fSGreg Roach                }
434a25f0a04SGreg Roach            }
435a25f0a04SGreg Roach        }
436a25f0a04SGreg Roach
437a25f0a04SGreg Roach        return null;
438a25f0a04SGreg Roach    }
439a25f0a04SGreg Roach
440a25f0a04SGreg Roach    /**
441a25f0a04SGreg Roach     * Display the prefered image for this individual.
442a25f0a04SGreg Roach     * Use an icon if no image is available.
443a25f0a04SGreg Roach     *
444dce07401SGreg Roach     * @param int      $width      Pixels
445dce07401SGreg Roach     * @param int      $height     Pixels
446dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
447dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
448dce07401SGreg Roach     *
449a25f0a04SGreg Roach     * @return string
450a25f0a04SGreg Roach     */
4518f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
452c1010edaSGreg Roach    {
4534a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4544a9f750fSGreg Roach
4554a9f750fSGreg Roach        if ($media_file !== null) {
4564a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
457a25f0a04SGreg Roach        }
4584a9f750fSGreg Roach
4594a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
4604a9f750fSGreg Roach            return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
4614a9f750fSGreg Roach        }
4624a9f750fSGreg Roach
4634a9f750fSGreg Roach        return '';
464a25f0a04SGreg Roach    }
465a25f0a04SGreg Roach
466a25f0a04SGreg Roach    /**
467a25f0a04SGreg Roach     * Get the date of birth
468a25f0a04SGreg Roach     *
469a25f0a04SGreg Roach     * @return Date
470a25f0a04SGreg Roach     */
4718f53f488SRico Sonntag    public function getBirthDate(): Date
472c1010edaSGreg Roach    {
473a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
474a25f0a04SGreg Roach            if ($date->isOK()) {
475a25f0a04SGreg Roach                return $date;
476a25f0a04SGreg Roach            }
477a25f0a04SGreg Roach        }
478a25f0a04SGreg Roach
479a25f0a04SGreg Roach        return new Date('');
480a25f0a04SGreg Roach    }
481a25f0a04SGreg Roach
482a25f0a04SGreg Roach    /**
483a25f0a04SGreg Roach     * Get the place of birth
484a25f0a04SGreg Roach     *
48516d0b7f7SRico Sonntag     * @return Place
486a25f0a04SGreg Roach     */
4878f53f488SRico Sonntag    public function getBirthPlace(): Place
488c1010edaSGreg Roach    {
489a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
490a25f0a04SGreg Roach            return $place;
491a25f0a04SGreg Roach        }
492a25f0a04SGreg Roach
493b20ddbf9SGreg Roach        return new Place('', $this->tree);
494a25f0a04SGreg Roach    }
495a25f0a04SGreg Roach
496a25f0a04SGreg Roach    /**
497a25f0a04SGreg Roach     * Get the year of birth
498a25f0a04SGreg Roach     *
499a25f0a04SGreg Roach     * @return string the year of birth
500a25f0a04SGreg Roach     */
5018f53f488SRico Sonntag    public function getBirthYear(): string
502c1010edaSGreg Roach    {
503f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
504a25f0a04SGreg Roach    }
505a25f0a04SGreg Roach
506a25f0a04SGreg Roach    /**
507a25f0a04SGreg Roach     * Get the date of death
508a25f0a04SGreg Roach     *
509a25f0a04SGreg Roach     * @return Date
510a25f0a04SGreg Roach     */
5118f53f488SRico Sonntag    public function getDeathDate(): Date
512c1010edaSGreg Roach    {
513a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
514a25f0a04SGreg Roach            if ($date->isOK()) {
515a25f0a04SGreg Roach                return $date;
516a25f0a04SGreg Roach            }
517a25f0a04SGreg Roach        }
518a25f0a04SGreg Roach
519a25f0a04SGreg Roach        return new Date('');
520a25f0a04SGreg Roach    }
521a25f0a04SGreg Roach
522a25f0a04SGreg Roach    /**
523a25f0a04SGreg Roach     * Get the place of death
524a25f0a04SGreg Roach     *
52516d0b7f7SRico Sonntag     * @return Place
526a25f0a04SGreg Roach     */
5278f53f488SRico Sonntag    public function getDeathPlace(): Place
528c1010edaSGreg Roach    {
529a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
530a25f0a04SGreg Roach            return $place;
531a25f0a04SGreg Roach        }
532a25f0a04SGreg Roach
533b20ddbf9SGreg Roach        return new Place('', $this->tree);
534a25f0a04SGreg Roach    }
535a25f0a04SGreg Roach
536a25f0a04SGreg Roach    /**
537a25f0a04SGreg Roach     * get the death year
538a25f0a04SGreg Roach     *
539a25f0a04SGreg Roach     * @return string the year of death
540a25f0a04SGreg Roach     */
5418f53f488SRico Sonntag    public function getDeathYear(): string
542c1010edaSGreg Roach    {
543f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
544a25f0a04SGreg Roach    }
545a25f0a04SGreg Roach
546a25f0a04SGreg Roach    /**
547a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
54815d603e7SGreg Roach     * Provide the place and full date using a tooltip.
549a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
550a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
551a25f0a04SGreg Roach     *
552a25f0a04SGreg Roach     * @return string
553a25f0a04SGreg Roach     */
5548f53f488SRico Sonntag    public function getLifeSpan(): string
555c1010edaSGreg Roach    {
55615d603e7SGreg Roach        // Just the first part of the place name
5571e9c2c49SGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
5581e9c2c49SGreg Roach        $death_place = strip_tags($this->getDeathPlace()->getShortName());
55915d603e7SGreg Roach        // Remove markup from dates
56015d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56115d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56215d603e7SGreg Roach
563c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
564bbb76c12SGreg Roach        return
565c1010edaSGreg Roach            I18N::translate(
566a25f0a04SGreg Roach                '%1$s–%2$s',
5672d4c1bedSGreg Roach                '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5682d4c1bedSGreg Roach                '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
569a25f0a04SGreg Roach            );
570a25f0a04SGreg Roach    }
571a25f0a04SGreg Roach
572a25f0a04SGreg Roach    /**
573a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
574a25f0a04SGreg Roach     *
575a25f0a04SGreg Roach     * @return Date[]
576a25f0a04SGreg Roach     */
5778f53f488SRico Sonntag    public function getAllBirthDates(): array
578c1010edaSGreg Roach    {
5798d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5808d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
581a25f0a04SGreg Roach            if ($tmp) {
582a25f0a04SGreg Roach                return $tmp;
583a25f0a04SGreg Roach            }
584a25f0a04SGreg Roach        }
585a25f0a04SGreg Roach
58613abd6f3SGreg Roach        return [];
587a25f0a04SGreg Roach    }
588a25f0a04SGreg Roach
589a25f0a04SGreg Roach    /**
590a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
591a25f0a04SGreg Roach     *
5924080d558SGreg Roach     * @return Place[]
593a25f0a04SGreg Roach     */
5948f53f488SRico Sonntag    public function getAllBirthPlaces(): array
595c1010edaSGreg Roach    {
5968d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5978d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
5984080d558SGreg Roach            if (!empty($places)) {
5994080d558SGreg Roach                return $places;
600a25f0a04SGreg Roach            }
601a25f0a04SGreg Roach        }
602a25f0a04SGreg Roach
60313abd6f3SGreg Roach        return [];
604a25f0a04SGreg Roach    }
605a25f0a04SGreg Roach
606a25f0a04SGreg Roach    /**
607a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
608a25f0a04SGreg Roach     *
609a25f0a04SGreg Roach     * @return Date[]
610a25f0a04SGreg Roach     */
6118f53f488SRico Sonntag    public function getAllDeathDates(): array
612c1010edaSGreg Roach    {
6138d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6148d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
615a25f0a04SGreg Roach            if ($tmp) {
616a25f0a04SGreg Roach                return $tmp;
617a25f0a04SGreg Roach            }
618a25f0a04SGreg Roach        }
619a25f0a04SGreg Roach
62013abd6f3SGreg Roach        return [];
621a25f0a04SGreg Roach    }
622a25f0a04SGreg Roach
623a25f0a04SGreg Roach    /**
624a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
625a25f0a04SGreg Roach     *
6264080d558SGreg Roach     * @return Place[]
627a25f0a04SGreg Roach     */
6288f53f488SRico Sonntag    public function getAllDeathPlaces(): array
629c1010edaSGreg Roach    {
6308d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6318d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6324080d558SGreg Roach            if (!empty($places)) {
6334080d558SGreg Roach                return $places;
634a25f0a04SGreg Roach            }
635a25f0a04SGreg Roach        }
636a25f0a04SGreg Roach
63713abd6f3SGreg Roach        return [];
638a25f0a04SGreg Roach    }
639a25f0a04SGreg Roach
640a25f0a04SGreg Roach    /**
641a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
642a25f0a04SGreg Roach     *
643a25f0a04SGreg Roach     * @return Date
644a25f0a04SGreg Roach     */
6458f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
646c1010edaSGreg Roach    {
6478f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
648a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
649a25f0a04SGreg Roach                if ($date->isOK()) {
6504686330aSGreg Roach                    $this->estimated_birth_date = $date;
651a25f0a04SGreg Roach                    break;
652a25f0a04SGreg Roach                }
653a25f0a04SGreg Roach            }
6548f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
65513abd6f3SGreg Roach                $min = [];
65613abd6f3SGreg Roach                $max = [];
657a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
658f5b60decSGreg Roach                if ($tmp->isOK()) {
659f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
660f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
661a25f0a04SGreg Roach                }
662a25f0a04SGreg Roach                foreach ($this->getChildFamilies() as $family) {
663a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
664f5b60decSGreg Roach                    if ($tmp->isOK()) {
665f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
666f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
667a25f0a04SGreg Roach                    }
6682e5b4452SGreg Roach                    $husband = $family->getHusband();
6692e5b4452SGreg Roach                    if ($husband instanceof Individual) {
6702e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
671f5b60decSGreg Roach                        if ($tmp->isOK()) {
672f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
673f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
674a25f0a04SGreg Roach                        }
675a25f0a04SGreg Roach                    }
6762e5b4452SGreg Roach                    $wife = $family->getWife();
6772e5b4452SGreg Roach                    if ($wife instanceof Individual) {
6782e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
679f5b60decSGreg Roach                        if ($tmp->isOK()) {
680f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
681f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
682a25f0a04SGreg Roach                        }
683a25f0a04SGreg Roach                    }
684a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
685a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
686f5b60decSGreg Roach                        if ($tmp->isOK()) {
687f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
688f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
689a25f0a04SGreg Roach                        }
690a25f0a04SGreg Roach                    }
691a25f0a04SGreg Roach                }
692a25f0a04SGreg Roach                foreach ($this->getSpouseFamilies() as $family) {
693a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
694f5b60decSGreg Roach                    if ($tmp->isOK()) {
695f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
696f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
697a25f0a04SGreg Roach                    }
698a25f0a04SGreg Roach                    $spouse = $family->getSpouse($this);
699a25f0a04SGreg Roach                    if ($spouse) {
700a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
701f5b60decSGreg Roach                        if ($tmp->isOK()) {
702f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
703f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
704a25f0a04SGreg Roach                        }
705a25f0a04SGreg Roach                    }
706a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
707a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
708f5b60decSGreg Roach                        if ($tmp->isOK()) {
709f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
710f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
711a25f0a04SGreg Roach                        }
712a25f0a04SGreg Roach                    }
713a25f0a04SGreg Roach                }
714a25f0a04SGreg Roach                if ($min && $max) {
71559f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
716a25f0a04SGreg Roach
71765e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7184686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
719a25f0a04SGreg Roach                } else {
7204686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
721a25f0a04SGreg Roach                }
722a25f0a04SGreg Roach            }
723a25f0a04SGreg Roach        }
724a25f0a04SGreg Roach
7254686330aSGreg Roach        return $this->estimated_birth_date;
726a25f0a04SGreg Roach    }
727a25f0a04SGreg Roach
728a25f0a04SGreg Roach    /**
729a25f0a04SGreg Roach     * Generate an estimated date of death.
730a25f0a04SGreg Roach     *
731a25f0a04SGreg Roach     * @return Date
732a25f0a04SGreg Roach     */
7338f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
734c1010edaSGreg Roach    {
7354686330aSGreg Roach        if ($this->estimated_death_date === null) {
736a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
737a25f0a04SGreg Roach                if ($date->isOK()) {
7384686330aSGreg Roach                    $this->estimated_death_date = $date;
739a25f0a04SGreg Roach                    break;
740a25f0a04SGreg Roach                }
741a25f0a04SGreg Roach            }
7424686330aSGreg Roach            if ($this->estimated_death_date === null) {
743f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
744c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7454686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
746a25f0a04SGreg Roach                } else {
7474686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
748a25f0a04SGreg Roach                }
749a25f0a04SGreg Roach            }
750a25f0a04SGreg Roach        }
751a25f0a04SGreg Roach
7524686330aSGreg Roach        return $this->estimated_death_date;
753a25f0a04SGreg Roach    }
754a25f0a04SGreg Roach
755a25f0a04SGreg Roach    /**
756a25f0a04SGreg Roach     * Get the sex - M F or U
757a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
758a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
759a25f0a04SGreg Roach     *
760a25f0a04SGreg Roach     * @return string
761a25f0a04SGreg Roach     */
762c1010edaSGreg Roach    public function getSex()
763c1010edaSGreg Roach    {
764a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
765a25f0a04SGreg Roach            return $match[1];
766a25f0a04SGreg Roach        }
767b2ce94c6SRico Sonntag
768b2ce94c6SRico Sonntag        return 'U';
769a25f0a04SGreg Roach    }
770a25f0a04SGreg Roach
771a25f0a04SGreg Roach    /**
772a25f0a04SGreg Roach     * Get the individual’s sex image
773a25f0a04SGreg Roach     *
774a25f0a04SGreg Roach     * @param string $size
775a25f0a04SGreg Roach     *
776a25f0a04SGreg Roach     * @return string
777a25f0a04SGreg Roach     */
7788f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
779c1010edaSGreg Roach    {
780a25f0a04SGreg Roach        return self::sexImage($this->getSex(), $size);
781a25f0a04SGreg Roach    }
782a25f0a04SGreg Roach
783a25f0a04SGreg Roach    /**
784a25f0a04SGreg Roach     * Generate a sex icon/image
785a25f0a04SGreg Roach     *
786a25f0a04SGreg Roach     * @param string $sex
787a25f0a04SGreg Roach     * @param string $size
788a25f0a04SGreg Roach     *
789a25f0a04SGreg Roach     * @return string
790a25f0a04SGreg Roach     */
7918f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
792c1010edaSGreg Roach    {
793a25f0a04SGreg Roach        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
794a25f0a04SGreg Roach    }
795a25f0a04SGreg Roach
796a25f0a04SGreg Roach    /**
797a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
798a25f0a04SGreg Roach     *
799a25f0a04SGreg Roach     * @return string
800a25f0a04SGreg Roach     */
8018f53f488SRico Sonntag    public function getBoxStyle(): string
802c1010edaSGreg Roach    {
803c1010edaSGreg Roach        $tmp = [
804c1010edaSGreg Roach            'M' => '',
805c1010edaSGreg Roach            'F' => 'F',
806c1010edaSGreg Roach            'U' => 'NN',
807c1010edaSGreg Roach        ];
808a25f0a04SGreg Roach
809a25f0a04SGreg Roach        return 'person_box' . $tmp[$this->getSex()];
810a25f0a04SGreg Roach    }
811a25f0a04SGreg Roach
812a25f0a04SGreg Roach    /**
813a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
814a25f0a04SGreg Roach     *
815cbc1590aSGreg Roach     * @param int|null $access_level
816a25f0a04SGreg Roach     *
817a25f0a04SGreg Roach     * @return Family[]
818a25f0a04SGreg Roach     */
8198f53f488SRico Sonntag    public function getSpouseFamilies($access_level = null): array
820c1010edaSGreg Roach    {
8214b9ff166SGreg Roach        if ($access_level === null) {
8224b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8234b9ff166SGreg Roach        }
8244b9ff166SGreg Roach
825acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
826a25f0a04SGreg Roach
82713abd6f3SGreg Roach        $families = [];
8288d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
829dc124885SGreg Roach            $family = $fact->target();
830e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
831a25f0a04SGreg Roach                $families[] = $family;
832a25f0a04SGreg Roach            }
833a25f0a04SGreg Roach        }
834a25f0a04SGreg Roach
835a25f0a04SGreg Roach        return $families;
836a25f0a04SGreg Roach    }
837a25f0a04SGreg Roach
838a25f0a04SGreg Roach    /**
839a25f0a04SGreg Roach     * Get the current spouse of this individual.
840a25f0a04SGreg Roach     *
841a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
842a25f0a04SGreg Roach     * in chronological order, and take the last one found.
843a25f0a04SGreg Roach     *
844a25f0a04SGreg Roach     * @return Individual|null
845a25f0a04SGreg Roach     */
846c1010edaSGreg Roach    public function getCurrentSpouse()
847c1010edaSGreg Roach    {
848a25f0a04SGreg Roach        $tmp    = $this->getSpouseFamilies();
849a25f0a04SGreg Roach        $family = end($tmp);
850a25f0a04SGreg Roach        if ($family) {
851a25f0a04SGreg Roach            return $family->getSpouse($this);
852a25f0a04SGreg Roach        }
853b2ce94c6SRico Sonntag
854b2ce94c6SRico Sonntag        return null;
855a25f0a04SGreg Roach    }
856a25f0a04SGreg Roach
857a25f0a04SGreg Roach    /**
858a25f0a04SGreg Roach     * Count the children belonging to this individual.
859a25f0a04SGreg Roach     *
860cbc1590aSGreg Roach     * @return int
861a25f0a04SGreg Roach     */
862c1010edaSGreg Roach    public function getNumberOfChildren()
863c1010edaSGreg Roach    {
8647d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8653dc7bbe9SGreg Roach            return (int) $match[1];
866b2ce94c6SRico Sonntag        }
867b2ce94c6SRico Sonntag
86813abd6f3SGreg Roach        $children = [];
869a25f0a04SGreg Roach        foreach ($this->getSpouseFamilies() as $fam) {
870a25f0a04SGreg Roach            foreach ($fam->getChildren() as $child) {
871c0935879SGreg Roach                $children[$child->xref()] = true;
872a25f0a04SGreg Roach            }
873a25f0a04SGreg Roach        }
874a25f0a04SGreg Roach
875a25f0a04SGreg Roach        return count($children);
876a25f0a04SGreg Roach    }
877a25f0a04SGreg Roach
878a25f0a04SGreg Roach    /**
879a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
880a25f0a04SGreg Roach     *
881cbc1590aSGreg Roach     * @param int|null $access_level
882a25f0a04SGreg Roach     *
883a25f0a04SGreg Roach     * @return Family[]
884a25f0a04SGreg Roach     */
8858f53f488SRico Sonntag    public function getChildFamilies($access_level = null): array
886c1010edaSGreg Roach    {
8874b9ff166SGreg Roach        if ($access_level === null) {
8884b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8894b9ff166SGreg Roach        }
8904b9ff166SGreg Roach
891acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
892a25f0a04SGreg Roach
89313abd6f3SGreg Roach        $families = [];
8948d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
895dc124885SGreg Roach            $family = $fact->target();
896e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
897a25f0a04SGreg Roach                $families[] = $family;
898a25f0a04SGreg Roach            }
899a25f0a04SGreg Roach        }
900a25f0a04SGreg Roach
901a25f0a04SGreg Roach        return $families;
902a25f0a04SGreg Roach    }
903a25f0a04SGreg Roach
904a25f0a04SGreg Roach    /**
905a25f0a04SGreg Roach     * Get the preferred parents for this individual.
906a25f0a04SGreg Roach     *
907a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
908a25f0a04SGreg Roach     * The preferred family record is:
909a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
910a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
911a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
912a25f0a04SGreg Roach     * (d) the first one found
913a25f0a04SGreg Roach     *
914a25f0a04SGreg Roach     * @return Family|null
915a25f0a04SGreg Roach     */
916c1010edaSGreg Roach    public function getPrimaryChildFamily()
917c1010edaSGreg Roach    {
918a25f0a04SGreg Roach        $families = $this->getChildFamilies();
919a25f0a04SGreg Roach        switch (count($families)) {
920a25f0a04SGreg Roach            case 0:
921a25f0a04SGreg Roach                return null;
922a25f0a04SGreg Roach            case 1:
92315d603e7SGreg Roach                return $families[0];
924a25f0a04SGreg Roach            default:
925a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
926a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
92774e78bfaSJonathan Jaubart                foreach ($families as $fam) {
928c0935879SGreg Roach                    $famid = $fam->xref();
9297d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
930a25f0a04SGreg Roach                        return $fam;
931a25f0a04SGreg Roach                    }
932a25f0a04SGreg Roach                }
933a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
93474e78bfaSJonathan Jaubart                foreach ($families as $fam) {
935c0935879SGreg Roach                    $famid = $fam->xref();
9367d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
937a25f0a04SGreg Roach                        return $fam;
938a25f0a04SGreg Roach                    }
939a25f0a04SGreg Roach                }
940a25f0a04SGreg Roach                // c) records with no '2 PEDI'
94174e78bfaSJonathan Jaubart                foreach ($families as $fam) {
942c0935879SGreg Roach                    $famid = $fam->xref();
9437d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
944a25f0a04SGreg Roach                        return $fam;
945a25f0a04SGreg Roach                    }
946a25f0a04SGreg Roach                }
947a25f0a04SGreg Roach
948a25f0a04SGreg Roach                // d) any record
94915d603e7SGreg Roach                return $families[0];
950a25f0a04SGreg Roach        }
951a25f0a04SGreg Roach    }
952a25f0a04SGreg Roach
953a25f0a04SGreg Roach    /**
954a25f0a04SGreg Roach     * Get a list of step-parent families.
955a25f0a04SGreg Roach     *
956a25f0a04SGreg Roach     * @return Family[]
957a25f0a04SGreg Roach     */
9588f53f488SRico Sonntag    public function getChildStepFamilies(): array
959c1010edaSGreg Roach    {
96013abd6f3SGreg Roach        $step_families = [];
961a25f0a04SGreg Roach        $families      = $this->getChildFamilies();
962a25f0a04SGreg Roach        foreach ($families as $family) {
963a25f0a04SGreg Roach            $father = $family->getHusband();
964a25f0a04SGreg Roach            if ($father) {
965a25f0a04SGreg Roach                foreach ($father->getSpouseFamilies() as $step_family) {
966a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
967a25f0a04SGreg Roach                        $step_families[] = $step_family;
968a25f0a04SGreg Roach                    }
969a25f0a04SGreg Roach                }
970a25f0a04SGreg Roach            }
971a25f0a04SGreg Roach            $mother = $family->getWife();
972a25f0a04SGreg Roach            if ($mother) {
973a25f0a04SGreg Roach                foreach ($mother->getSpouseFamilies() as $step_family) {
974a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
975a25f0a04SGreg Roach                        $step_families[] = $step_family;
976a25f0a04SGreg Roach                    }
977a25f0a04SGreg Roach                }
978a25f0a04SGreg Roach            }
979a25f0a04SGreg Roach        }
980a25f0a04SGreg Roach
981a25f0a04SGreg Roach        return $step_families;
982a25f0a04SGreg Roach    }
983a25f0a04SGreg Roach
984a25f0a04SGreg Roach    /**
985a25f0a04SGreg Roach     * Get a list of step-parent families.
986a25f0a04SGreg Roach     *
987a25f0a04SGreg Roach     * @return Family[]
988a25f0a04SGreg Roach     */
9898f53f488SRico Sonntag    public function getSpouseStepFamilies(): array
990c1010edaSGreg Roach    {
99113abd6f3SGreg Roach        $step_families = [];
992a25f0a04SGreg Roach        $families      = $this->getSpouseFamilies();
993a25f0a04SGreg Roach        foreach ($families as $family) {
994a25f0a04SGreg Roach            $spouse = $family->getSpouse($this);
995a25f0a04SGreg Roach            if ($spouse) {
996a25f0a04SGreg Roach                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
997a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
998a25f0a04SGreg Roach                        $step_families[] = $step_family;
999a25f0a04SGreg Roach                    }
1000a25f0a04SGreg Roach                }
1001a25f0a04SGreg Roach            }
1002a25f0a04SGreg Roach        }
1003a25f0a04SGreg Roach
1004a25f0a04SGreg Roach        return $step_families;
1005a25f0a04SGreg Roach    }
1006a25f0a04SGreg Roach
1007a25f0a04SGreg Roach    /**
1008a25f0a04SGreg Roach     * A label for a parental family group
1009a25f0a04SGreg Roach     *
1010a25f0a04SGreg Roach     * @param Family $family
1011a25f0a04SGreg Roach     *
1012a25f0a04SGreg Roach     * @return string
1013a25f0a04SGreg Roach     */
1014c1010edaSGreg Roach    public function getChildFamilyLabel(Family $family)
1015c1010edaSGreg Roach    {
10167d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
1017a25f0a04SGreg Roach            // A specified pedigree
1018764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1019b2ce94c6SRico Sonntag        }
1020b2ce94c6SRico Sonntag
1021a25f0a04SGreg Roach        // Default (birth) pedigree
1022764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1023a25f0a04SGreg Roach    }
1024a25f0a04SGreg Roach
1025a25f0a04SGreg Roach    /**
1026a25f0a04SGreg Roach     * Create a label for a step family
1027a25f0a04SGreg Roach     *
1028a25f0a04SGreg Roach     * @param Family $step_family
1029a25f0a04SGreg Roach     *
1030a25f0a04SGreg Roach     * @return string
1031a25f0a04SGreg Roach     */
10328f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1033c1010edaSGreg Roach    {
1034a25f0a04SGreg Roach        foreach ($this->getChildFamilies() as $family) {
1035a25f0a04SGreg Roach            if ($family !== $step_family) {
1036a25f0a04SGreg Roach                // Must be a step-family
1037a25f0a04SGreg Roach                foreach ($family->getSpouses() as $parent) {
1038a25f0a04SGreg Roach                    foreach ($step_family->getSpouses() as $step_parent) {
1039a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1040a25f0a04SGreg Roach                            // One common parent - must be a step family
1041a25f0a04SGreg Roach                            if ($parent->getSex() == 'M') {
1042a25f0a04SGreg Roach                                // Father’s family with someone else
1043a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1044a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1045bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1046b2ce94c6SRico Sonntag                                }
1047b2ce94c6SRico Sonntag
1048a25f0a04SGreg Roach                                /* I18N: A step-family. */
1049bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1050a25f0a04SGreg Roach                            }
1051b2ce94c6SRico Sonntag
1052a25f0a04SGreg Roach                            // Mother’s family with someone else
1053a25f0a04SGreg Roach                            if ($step_family->getSpouse($step_parent)) {
1054a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
1055bbb76c12SGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1056b2ce94c6SRico Sonntag                            }
1057b2ce94c6SRico Sonntag
1058a25f0a04SGreg Roach                            /* I18N: A step-family. */
1059bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1060a25f0a04SGreg Roach                        }
1061a25f0a04SGreg Roach                    }
1062a25f0a04SGreg Roach                }
1063a25f0a04SGreg Roach            }
1064a25f0a04SGreg Roach        }
1065a25f0a04SGreg Roach
1066a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1067a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1068a25f0a04SGreg Roach    }
1069a25f0a04SGreg Roach
1070225e381fSGreg Roach    /**
1071225e381fSGreg Roach     * Get the description for the family.
1072225e381fSGreg Roach     *
1073225e381fSGreg Roach     * For example, "XXX's family with new wife".
1074225e381fSGreg Roach     *
1075225e381fSGreg Roach     * @param Family $family
1076225e381fSGreg Roach     *
1077225e381fSGreg Roach     * @return string
1078225e381fSGreg Roach     */
1079c1010edaSGreg Roach    public function getSpouseFamilyLabel(Family $family)
1080c1010edaSGreg Roach    {
1081225e381fSGreg Roach        $spouse = $family->getSpouse($this);
1082225e381fSGreg Roach        if ($spouse) {
1083225e381fSGreg Roach            /* I18N: %s is the spouse name */
1084bbb76c12SGreg Roach            return I18N::translate('Family with %s', $spouse->getFullName());
1085225e381fSGreg Roach        }
1086b2ce94c6SRico Sonntag
1087b2ce94c6SRico Sonntag        return $family->getFullName();
1088225e381fSGreg Roach    }
1089225e381fSGreg Roach
1090a25f0a04SGreg Roach    /**
1091a25f0a04SGreg Roach     * get primary parents names for this individual
1092a25f0a04SGreg Roach     *
1093a25f0a04SGreg Roach     * @param string $classname optional css class
1094a25f0a04SGreg Roach     * @param string $display   optional css style display
1095a25f0a04SGreg Roach     *
1096a25f0a04SGreg Roach     * @return string a div block with father & mother names
1097a25f0a04SGreg Roach     */
10988f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1099c1010edaSGreg Roach    {
1100a25f0a04SGreg Roach        $fam = $this->getPrimaryChildFamily();
1101a25f0a04SGreg Roach        if (!$fam) {
1102a25f0a04SGreg Roach            return '';
1103a25f0a04SGreg Roach        }
1104a25f0a04SGreg Roach        $txt = '<div';
1105a25f0a04SGreg Roach        if ($classname) {
11064c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1107a25f0a04SGreg Roach        }
1108a25f0a04SGreg Roach        if ($display) {
11094c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1110a25f0a04SGreg Roach        }
1111a25f0a04SGreg Roach        $txt .= '>';
1112a25f0a04SGreg Roach        $husb = $fam->getHusband();
1113a25f0a04SGreg Roach        if ($husb) {
1114a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1115a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1116a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1117a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1118a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
1119bbb76c12SGreg Roach            $txt .= I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1120a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1121a25f0a04SGreg Roach        }
1122a25f0a04SGreg Roach        $wife = $fam->getWife();
1123a25f0a04SGreg Roach        if ($wife) {
1124a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1125a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1126a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1127a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1128a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
1129bbb76c12SGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->getFullName());
1130a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1131a25f0a04SGreg Roach        }
1132a25f0a04SGreg Roach        $txt .= '</div>';
1133a25f0a04SGreg Roach
1134a25f0a04SGreg Roach        return $txt;
1135a25f0a04SGreg Roach    }
1136a25f0a04SGreg Roach
1137a25f0a04SGreg Roach    /** {@inheritdoc} */
11388f53f488SRico Sonntag    public function getFallBackName(): string
1139c1010edaSGreg Roach    {
1140a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1141a25f0a04SGreg Roach    }
1142a25f0a04SGreg Roach
1143a25f0a04SGreg Roach    /**
1144a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1145a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1146a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1147a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1148a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1149a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1150a25f0a04SGreg Roach     *
1151a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1152a25f0a04SGreg Roach     * 2 GIVN Robert
1153a25f0a04SGreg Roach     * 2 SPFX de
1154a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1155a25f0a04SGreg Roach     * 2 NICK The Bald
1156a25f0a04SGreg Roach     *
1157a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1158a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1159a25f0a04SGreg Roach     *
1160a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1161a25f0a04SGreg Roach     *
1162a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1163a25f0a04SGreg Roach     * or
1164a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1165a25f0a04SGreg Roach     * 2 GIVN Carlos
1166a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1167a25f0a04SGreg Roach     *
1168a25f0a04SGreg Roach     * @param string $type
1169a25f0a04SGreg Roach     * @param string $full
1170a25f0a04SGreg Roach     * @param string $gedcom
1171a25f0a04SGreg Roach     */
117276f666f4SGreg Roach    protected function addName(string $type, string $full, string $gedcom)
1173c1010edaSGreg Roach    {
1174a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1175a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1176a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1177a25f0a04SGreg Roach
117876f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1179a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1180a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1181a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1182a25f0a04SGreg Roach
1183a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
118476f666f4SGreg Roach        if ($SURN !== '') {
1185a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1186a25f0a04SGreg Roach        } else {
118713abd6f3SGreg Roach            $SURNS = [];
1188a25f0a04SGreg Roach        }
118976f666f4SGreg Roach
1190a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1191a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1192a25f0a04SGreg Roach
1193a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1194a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1195a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1196a25f0a04SGreg Roach
1197a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
119876f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1199a25f0a04SGreg Roach            $full = $full . '/';
1200a25f0a04SGreg Roach        }
1201a25f0a04SGreg Roach
1202a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1203a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1204a25f0a04SGreg Roach
1205a25f0a04SGreg Roach        // Extract the surname.
1206a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1207a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1208a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1209a25f0a04SGreg Roach        } else {
1210a25f0a04SGreg Roach            $surname = '';
1211a25f0a04SGreg Roach        }
1212a25f0a04SGreg Roach
1213a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1214a25f0a04SGreg Roach        if (!$SURNS) {
1215a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1216a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1217a25f0a04SGreg Roach                $SURNS = $matches[1];
1218a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1219a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1220a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1221a25f0a04SGreg Roach                }
1222a25f0a04SGreg Roach            } else {
1223a25f0a04SGreg Roach                // It is valid not to have a surname at all
122413abd6f3SGreg Roach                $SURNS = [''];
1225a25f0a04SGreg Roach            }
1226a25f0a04SGreg Roach        }
1227a25f0a04SGreg Roach
1228a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1229a25f0a04SGreg Roach        if (!$GIVN) {
1230a25f0a04SGreg Roach            $GIVN = preg_replace(
123113abd6f3SGreg Roach                [
1232c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1233c1010edaSGreg Roach                    // remove surname
1234c1010edaSGreg Roach                    '/ ?".+"/',
1235c1010edaSGreg Roach                    // remove nickname
1236c1010edaSGreg Roach                    '/ {2,}/',
1237c1010edaSGreg Roach                    // multiple spaces, caused by the above
1238c1010edaSGreg Roach                    '/^ | $/',
1239c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
124013abd6f3SGreg Roach                ],
124113abd6f3SGreg Roach                [
1242a25f0a04SGreg Roach                    ' ',
1243a25f0a04SGreg Roach                    ' ',
1244a25f0a04SGreg Roach                    ' ',
1245a25f0a04SGreg Roach                    '',
124613abd6f3SGreg Roach                ],
1247a25f0a04SGreg Roach                $full
1248a25f0a04SGreg Roach            );
1249a25f0a04SGreg Roach        }
1250a25f0a04SGreg Roach
1251a25f0a04SGreg Roach        // Add placeholder for unknown given name
1252a25f0a04SGreg Roach        if (!$GIVN) {
1253a25f0a04SGreg Roach            $GIVN = '@P.N.';
125473f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1255a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1256a25f0a04SGreg Roach        }
1257a25f0a04SGreg Roach
12587b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12597b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1260c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12617b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1262c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1263a25f0a04SGreg Roach        }
1264a25f0a04SGreg Roach
1265a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1266a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1267a25f0a04SGreg Roach        // $full is for display on-screen
1268a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1269a25f0a04SGreg Roach
1270a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1271ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1272ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1273c6f196c3SGreg Roach        // Format for display
1274d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1275acc34ea1SGreg Roach        // Localise quotation marks around the nickname
127618d7a90dSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function (array $matches): string {
12778d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12788d68cabeSGreg Roach        }, $full);
1279a25f0a04SGreg Roach
1280c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1281a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1282a25f0a04SGreg Roach
1283a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1284a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1285a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1286a25f0a04SGreg Roach
1287ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1288a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1289a25f0a04SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1290a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1291a25f0a04SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1292a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1293a25f0a04SGreg Roach            }
1294a25f0a04SGreg Roach
1295bdb3725aSGreg Roach            $this->getAllNames[] = [
1296a25f0a04SGreg Roach                'type'    => $type,
1297a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1298c1010edaSGreg Roach                'full'    => $full,
1299c1010edaSGreg Roach                // This is used for display
1300c1010edaSGreg Roach                'fullNN'  => $fullNN,
1301c1010edaSGreg Roach                // This goes into the database
1302c1010edaSGreg Roach                'surname' => $surname,
1303c1010edaSGreg Roach                // This goes into the database
1304c1010edaSGreg Roach                'givn'    => $GIVN,
1305c1010edaSGreg Roach                // This goes into the database
1306c1010edaSGreg Roach                'surn'    => $SURN,
1307c1010edaSGreg Roach                // This goes into the database
130813abd6f3SGreg Roach            ];
1309a25f0a04SGreg Roach        }
1310a25f0a04SGreg Roach    }
1311a25f0a04SGreg Roach
1312a25f0a04SGreg Roach    /**
131376692c8bSGreg Roach     * Extract names from the GEDCOM record.
1314c7ff4153SGreg Roach     *
1315c7ff4153SGreg Roach     * @return void
1316a25f0a04SGreg Roach     */
1317c1010edaSGreg Roach    public function extractNames()
1318c1010edaSGreg Roach    {
13198f53f488SRico Sonntag        $this->extractNamesFromFacts(
13208f53f488SRico Sonntag            1,
13218f53f488SRico Sonntag            'NAME',
132230158ae7SGreg Roach            $this->facts(
13238d0ebef0SGreg Roach                ['NAME'],
13248f53f488SRico Sonntag                false,
13258f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13268f53f488SRico Sonntag                $this->canShowName()
13278f53f488SRico Sonntag            )
13288f53f488SRico Sonntag        );
1329a25f0a04SGreg Roach    }
1330a25f0a04SGreg Roach
1331a25f0a04SGreg Roach    /**
1332a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1333a25f0a04SGreg Roach     * selection items or favorites.
1334a25f0a04SGreg Roach     *
1335a25f0a04SGreg Roach     * @return string
1336a25f0a04SGreg Roach     */
13378f53f488SRico Sonntag    public function formatListDetails(): string
1338c1010edaSGreg Roach    {
1339a25f0a04SGreg Roach        return
13408d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13418d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1342a25f0a04SGreg Roach    }
1343a25f0a04SGreg Roach}
1344