xref: /webtrees/app/Individual.php (revision 4459dc9a6d0c27769f8135175f3569e4fa287451)
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;
2439ca88baSGreg Roachuse Illuminate\Support\Collection;
25886b77daSGreg Roachuse stdClass;
26a25f0a04SGreg Roach
27a25f0a04SGreg Roach/**
2876692c8bSGreg Roach * A GEDCOM individual (INDI) object.
29a25f0a04SGreg Roach */
30c1010edaSGreg Roachclass Individual extends GedcomRecord
31c1010edaSGreg Roach{
3216d6367aSGreg Roach    public const RECORD_TYPE = 'INDI';
3316d6367aSGreg Roach
3416d6367aSGreg Roach    protected const ROUTE_NAME = 'individual';
35a25f0a04SGreg Roach
3676692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3776692c8bSGreg Roach    public $generation;
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
404686330aSGreg Roach    private $estimated_birth_date;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach    /** @var Date The estimated date of death */
434686330aSGreg Roach    private $estimated_death_date;
44a25f0a04SGreg Roach
45a25f0a04SGreg Roach    /**
46886b77daSGreg Roach     * A closure which will create a record from a database row.
47886b77daSGreg Roach     *
48886b77daSGreg Roach     * @return Closure
49886b77daSGreg Roach     */
50c0804649SGreg Roach    public static function rowMapper(): Closure
51886b77daSGreg Roach    {
52c0804649SGreg Roach        return function (stdClass $row): Individual {
53a7a24840SGreg Roach            $individual = Individual::getInstance($row->i_id, Tree::findById((int) $row->i_file), $row->i_gedcom);
54e84cf2deSGreg Roach
55e84cf2deSGreg Roach            if ($row->n_num ?? null) {
56e84cf2deSGreg Roach                $individual->setPrimaryName($row->n_num);
57e84cf2deSGreg Roach            }
58e84cf2deSGreg Roach
59e84cf2deSGreg Roach            return $individual;
60886b77daSGreg Roach        };
61886b77daSGreg Roach    }
62886b77daSGreg Roach
63886b77daSGreg Roach    /**
64c156e8f5SGreg Roach     * A closure which will compare individuals by birth date.
65c156e8f5SGreg Roach     *
66c156e8f5SGreg Roach     * @return Closure
67c156e8f5SGreg Roach     */
68c156e8f5SGreg Roach    public static function birthDateComparator(): Closure
69c156e8f5SGreg Roach    {
70a9199cb2SGreg Roach        return function (Individual $x, Individual $y): int {
71c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
72c156e8f5SGreg Roach        };
73c156e8f5SGreg Roach    }
74c156e8f5SGreg Roach
75c156e8f5SGreg Roach    /**
76c156e8f5SGreg Roach     * A closure which will compare individuals by death date.
77c156e8f5SGreg Roach     *
78c156e8f5SGreg Roach     * @return Closure
79c156e8f5SGreg Roach     */
80c156e8f5SGreg Roach    public static function deathDateComparator(): Closure
81c156e8f5SGreg Roach    {
82a9199cb2SGreg Roach        return function (Individual $x, Individual $y): int {
83c156e8f5SGreg Roach            return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
84c156e8f5SGreg Roach        };
85c156e8f5SGreg Roach    }
86c156e8f5SGreg Roach
87c156e8f5SGreg Roach    /**
88e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
89e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
90e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
91e71ef9d2SGreg Roach     *
92e71ef9d2SGreg Roach     * @param string      $xref
93e71ef9d2SGreg Roach     * @param Tree        $tree
94e71ef9d2SGreg Roach     * @param string|null $gedcom
95e71ef9d2SGreg Roach     *
96e71ef9d2SGreg Roach     * @throws \Exception
97e71ef9d2SGreg Roach     * @return Individual|null
98e71ef9d2SGreg Roach     */
9976f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
100c1010edaSGreg Roach    {
101e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
102e71ef9d2SGreg Roach
103e71ef9d2SGreg Roach        if ($record instanceof Individual) {
104e71ef9d2SGreg Roach            return $record;
105e71ef9d2SGreg Roach        }
106b2ce94c6SRico Sonntag
107b2ce94c6SRico Sonntag        return null;
108e71ef9d2SGreg Roach    }
109e71ef9d2SGreg Roach
110e71ef9d2SGreg Roach    /**
111395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
112395f0fe0SGreg Roach     * Typically when we load families and their members.
113395f0fe0SGreg Roach     *
114395f0fe0SGreg Roach     * @param Tree     $tree
1154a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
11618d7a90dSGreg Roach     *
11718d7a90dSGreg Roach     * @return void
118395f0fe0SGreg Roach     */
1192e5b4452SGreg Roach    public static function load(Tree $tree, array $xrefs): void
120c1010edaSGreg Roach    {
1212e5b4452SGreg Roach        $rows = DB::table('individuals')
1222e5b4452SGreg Roach            ->where('i_file', '=', $tree->id())
1232e5b4452SGreg Roach            ->whereIn('i_id', array_unique($xrefs))
1242e5b4452SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
1252e5b4452SGreg Roach            ->get();
126395f0fe0SGreg Roach
127395f0fe0SGreg Roach        foreach ($rows as $row) {
128395f0fe0SGreg Roach            self::getInstance($row->xref, $tree, $row->gedcom);
129395f0fe0SGreg Roach        }
130395f0fe0SGreg Roach    }
131395f0fe0SGreg Roach
132395f0fe0SGreg Roach    /**
133a25f0a04SGreg Roach     * Can the name of this record be shown?
134a25f0a04SGreg Roach     *
13576692c8bSGreg Roach     * @param int|null $access_level
13676692c8bSGreg Roach     *
13776692c8bSGreg Roach     * @return bool
138a25f0a04SGreg Roach     */
13935584196SGreg Roach    public function canShowName(int $access_level = null): bool
140c1010edaSGreg Roach    {
1414b9ff166SGreg Roach        if ($access_level === null) {
1424b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1434b9ff166SGreg Roach        }
1444b9ff166SGreg Roach
145518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
146a25f0a04SGreg Roach    }
147a25f0a04SGreg Roach
148a25f0a04SGreg Roach    /**
14976692c8bSGreg Roach     * Can this individual be shown?
150a25f0a04SGreg Roach     *
15176692c8bSGreg Roach     * @param int $access_level
15276692c8bSGreg Roach     *
15376692c8bSGreg Roach     * @return bool
154a25f0a04SGreg Roach     */
15535584196SGreg Roach    protected function canShowByType(int $access_level): bool
156c1010edaSGreg Roach    {
157a25f0a04SGreg Roach        // Dead people...
158518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
159a25f0a04SGreg Roach            $keep_alive             = false;
16054ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
161a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
1628d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
163a25f0a04SGreg Roach                foreach ($matches as $match) {
164a25f0a04SGreg Roach                    $date = new Date($match[1]);
165a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
166a25f0a04SGreg Roach                        $keep_alive = true;
167a25f0a04SGreg Roach                        break;
168a25f0a04SGreg Roach                    }
169a25f0a04SGreg Roach                }
170a25f0a04SGreg Roach            }
17154ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
172a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
1738d0ebef0SGreg Roach                preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
174a25f0a04SGreg Roach                foreach ($matches as $match) {
175a25f0a04SGreg Roach                    $date = new Date($match[1]);
176a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
177a25f0a04SGreg Roach                        $keep_alive = true;
178a25f0a04SGreg Roach                        break;
179a25f0a04SGreg Roach                    }
180a25f0a04SGreg Roach                }
181a25f0a04SGreg Roach            }
182a25f0a04SGreg Roach            if (!$keep_alive) {
183a25f0a04SGreg Roach                return true;
184a25f0a04SGreg Roach            }
185a25f0a04SGreg Roach        }
186a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
18754ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1884b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
18954ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1904b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
191a25f0a04SGreg Roach        }
192a25f0a04SGreg Roach
193a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1944b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
195a25f0a04SGreg Roach    }
196a25f0a04SGreg Roach
197a25f0a04SGreg Roach    /**
198a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
199a25f0a04SGreg Roach     *
200a25f0a04SGreg Roach     * @param Individual $target
201cbc1590aSGreg Roach     * @param int        $distance
202a25f0a04SGreg Roach     *
203cbc1590aSGreg Roach     * @return bool
204a25f0a04SGreg Roach     */
2058f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
206c1010edaSGreg Roach    {
207a25f0a04SGreg Roach        static $cache = null;
208a25f0a04SGreg Roach
20906ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
210a25f0a04SGreg Roach        if ($user_individual) {
211a25f0a04SGreg Roach            if (!$cache) {
21213abd6f3SGreg Roach                $cache = [
21313abd6f3SGreg Roach                    0 => [$user_individual],
21413abd6f3SGreg Roach                    1 => [],
21513abd6f3SGreg Roach                ];
2168d0ebef0SGreg Roach                foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
217dc124885SGreg Roach                    $family = $fact->target();
218e24444eeSGreg Roach                    if ($family instanceof Family) {
219a25f0a04SGreg Roach                        $cache[1][] = $family;
220a25f0a04SGreg Roach                    }
221a25f0a04SGreg Roach                }
222a25f0a04SGreg Roach            }
223a25f0a04SGreg Roach        } else {
224a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
225a25f0a04SGreg Roach            return true;
226a25f0a04SGreg Roach        }
227a25f0a04SGreg Roach
228a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
229a25f0a04SGreg Roach        $distance *= 2;
230a25f0a04SGreg Roach
231a25f0a04SGreg Roach        // Consider each path length in turn
232a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
233a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
234a25f0a04SGreg Roach                // We have already calculated all records with this length
235a25f0a04SGreg Roach                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
236a25f0a04SGreg Roach                    return true;
237a25f0a04SGreg Roach                }
238a25f0a04SGreg Roach            } else {
239a25f0a04SGreg Roach                // Need to calculate these paths
24013abd6f3SGreg Roach                $cache[$n] = [];
241a25f0a04SGreg Roach                if ($n % 2 == 0) {
242a25f0a04SGreg Roach                    // Add FAM->INDI links
243a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2448d0ebef0SGreg Roach                        foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) {
245dc124885SGreg Roach                            $individual = $fact->target();
246a25f0a04SGreg Roach                            // Don’t backtrack
247e24444eeSGreg Roach                            if ($individual instanceof Individual && !in_array($individual, $cache[$n - 2], true)) {
248a25f0a04SGreg Roach                                $cache[$n][] = $individual;
249a25f0a04SGreg Roach                            }
250a25f0a04SGreg Roach                        }
251a25f0a04SGreg Roach                    }
252a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
253a25f0a04SGreg Roach                        return true;
254a25f0a04SGreg Roach                    }
255a25f0a04SGreg Roach                } else {
256a25f0a04SGreg Roach                    // Add INDI->FAM links
257a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2588d0ebef0SGreg Roach                        foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) {
259dc124885SGreg Roach                            $family = $fact->target();
260a25f0a04SGreg Roach                            // Don’t backtrack
261e24444eeSGreg Roach                            if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) {
262a25f0a04SGreg Roach                                $cache[$n][] = $family;
263a25f0a04SGreg Roach                            }
264a25f0a04SGreg Roach                        }
265a25f0a04SGreg Roach                    }
266a25f0a04SGreg Roach                }
267a25f0a04SGreg Roach            }
268a25f0a04SGreg Roach        }
269a25f0a04SGreg Roach
270a25f0a04SGreg Roach        return false;
271a25f0a04SGreg Roach    }
272a25f0a04SGreg Roach
27376692c8bSGreg Roach    /**
27476692c8bSGreg Roach     * Generate a private version of this record
27576692c8bSGreg Roach     *
27676692c8bSGreg Roach     * @param int $access_level
27776692c8bSGreg Roach     *
27876692c8bSGreg Roach     * @return string
27976692c8bSGreg Roach     */
2803c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
281c1010edaSGreg Roach    {
282acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
283a25f0a04SGreg Roach
284a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
285518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
286a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
2878d0ebef0SGreg Roach            foreach ($this->facts(['NAME']) as $fact) {
288138ca96cSGreg Roach                $rec .= "\n" . $fact->gedcom();
289a25f0a04SGreg Roach            }
290a25f0a04SGreg Roach        }
291a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
2928d0ebef0SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
293a25f0a04SGreg Roach        foreach ($matches as $match) {
29424ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
295a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
296a25f0a04SGreg Roach                $rec .= $match[0];
297a25f0a04SGreg Roach            }
298a25f0a04SGreg Roach        }
299a25f0a04SGreg Roach        // Don’t privatize sex.
300a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
301a25f0a04SGreg Roach            $rec .= $match[0];
302a25f0a04SGreg Roach        }
303a25f0a04SGreg Roach
304a25f0a04SGreg Roach        return $rec;
305a25f0a04SGreg Roach    }
306a25f0a04SGreg Roach
30776692c8bSGreg Roach    /**
30876692c8bSGreg Roach     * Fetch data from the database
30976692c8bSGreg Roach     *
31076692c8bSGreg Roach     * @param string $xref
31176692c8bSGreg Roach     * @param int    $tree_id
31276692c8bSGreg Roach     *
31376692c8bSGreg Roach     * @return null|string
31476692c8bSGreg Roach     */
31525e95e6eSGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
316c1010edaSGreg Roach    {
3172e5b4452SGreg Roach        return DB::table('individuals')
3182e5b4452SGreg Roach            ->where('i_id', '=', $xref)
3192e5b4452SGreg Roach            ->where('i_file', '=', $tree_id)
3202e5b4452SGreg Roach            ->value('i_gedcom');
321a25f0a04SGreg Roach    }
322a25f0a04SGreg Roach
323a25f0a04SGreg Roach    /**
324a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
325a25f0a04SGreg Roach     * If not known to be dead, then assume living.
326a25f0a04SGreg Roach     *
327cbc1590aSGreg Roach     * @return bool
328a25f0a04SGreg Roach     */
3298f53f488SRico Sonntag    public function isDead(): bool
330c1010edaSGreg Roach    {
331c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
332*4459dc9aSGreg Roach        $today_jd      = Carbon::now()->julianDay();
333a25f0a04SGreg Roach
334a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
3358d0ebef0SGreg Roach        if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
336a25f0a04SGreg Roach            return true;
337a25f0a04SGreg Roach        }
338a25f0a04SGreg Roach
339a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
340a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
341a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
342a25f0a04SGreg Roach                $date = new Date($date_match);
343269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) {
344a25f0a04SGreg Roach                    return true;
345a25f0a04SGreg Roach                }
346a25f0a04SGreg Roach            }
347a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
348a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
349a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
350a25f0a04SGreg Roach                return false;
351a25f0a04SGreg Roach            }
352a25f0a04SGreg Roach        }
353a25f0a04SGreg Roach
354a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
355a25f0a04SGreg Roach
356a25f0a04SGreg Roach        // Check parents (birth and adopted)
35739ca88baSGreg Roach        foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) {
35839ca88baSGreg Roach            foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) {
359a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
360a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
361a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
362a25f0a04SGreg Roach                    $date = new Date($date_match);
363269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) {
364a25f0a04SGreg Roach                        return true;
365a25f0a04SGreg Roach                    }
366a25f0a04SGreg Roach                }
367a25f0a04SGreg Roach            }
368a25f0a04SGreg Roach        }
369a25f0a04SGreg Roach
370a25f0a04SGreg Roach        // Check spouses
37139ca88baSGreg Roach        foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) {
372a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
373a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
374a25f0a04SGreg Roach                $date = new Date($date_match);
375a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
376269fd10dSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) {
377a25f0a04SGreg Roach                    return true;
378a25f0a04SGreg Roach                }
379a25f0a04SGreg Roach            }
380a25f0a04SGreg Roach            // Check spouse dates
38139ca88baSGreg Roach            $spouse = $family->spouse($this, Auth::PRIV_HIDE);
382a25f0a04SGreg Roach            if ($spouse) {
383a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
384a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
385a25f0a04SGreg Roach                    $date = new Date($date_match);
386a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
387269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) {
388a25f0a04SGreg Roach                        return true;
389a25f0a04SGreg Roach                    }
390a25f0a04SGreg Roach                }
391a25f0a04SGreg Roach            }
392a25f0a04SGreg Roach            // Check child dates
39339ca88baSGreg Roach            foreach ($family->children(Auth::PRIV_HIDE) as $child) {
394a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
395a25f0a04SGreg Roach                // Assume children born after age of 15
396a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
397a25f0a04SGreg Roach                    $date = new Date($date_match);
398269fd10dSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) {
399a25f0a04SGreg Roach                        return true;
400a25f0a04SGreg Roach                    }
401a25f0a04SGreg Roach                }
402a25f0a04SGreg Roach                // Check grandchildren
40339ca88baSGreg Roach                foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) {
40439ca88baSGreg Roach                    foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) {
405a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
406a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
407a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
408a25f0a04SGreg Roach                            $date = new Date($date_match);
409269fd10dSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) {
410a25f0a04SGreg Roach                                return true;
411a25f0a04SGreg Roach                            }
412a25f0a04SGreg Roach                        }
413a25f0a04SGreg Roach                    }
414a25f0a04SGreg Roach                }
415a25f0a04SGreg Roach            }
416a25f0a04SGreg Roach        }
417a25f0a04SGreg Roach
418a25f0a04SGreg Roach        return false;
419a25f0a04SGreg Roach    }
420a25f0a04SGreg Roach
421a25f0a04SGreg Roach    /**
422a25f0a04SGreg Roach     * Find the highlighted media object for an individual
423a25f0a04SGreg Roach     *
4244a9f750fSGreg Roach     * @return null|MediaFile
425a25f0a04SGreg Roach     */
426c1010edaSGreg Roach    public function findHighlightedMediaFile()
427c1010edaSGreg Roach    {
4288d0ebef0SGreg Roach        foreach ($this->facts(['OBJE']) as $fact) {
429dc124885SGreg Roach            $media = $fact->target();
430a213a63cSGreg Roach            if ($media instanceof Media) {
4314a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
432a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4334a9f750fSGreg Roach                        return $media_file;
4344a9f750fSGreg Roach                    }
4354a9f750fSGreg Roach                }
436a25f0a04SGreg Roach            }
437a25f0a04SGreg Roach        }
438a25f0a04SGreg Roach
439a25f0a04SGreg Roach        return null;
440a25f0a04SGreg Roach    }
441a25f0a04SGreg Roach
442a25f0a04SGreg Roach    /**
443a25f0a04SGreg Roach     * Display the prefered image for this individual.
444a25f0a04SGreg Roach     * Use an icon if no image is available.
445a25f0a04SGreg Roach     *
446dce07401SGreg Roach     * @param int      $width      Pixels
447dce07401SGreg Roach     * @param int      $height     Pixels
448dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
449dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
450dce07401SGreg Roach     *
451a25f0a04SGreg Roach     * @return string
452a25f0a04SGreg Roach     */
4538f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
454c1010edaSGreg Roach    {
4554a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4564a9f750fSGreg Roach
4574a9f750fSGreg Roach        if ($media_file !== null) {
4584a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
459a25f0a04SGreg Roach        }
4604a9f750fSGreg Roach
4614a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
46239ca88baSGreg Roach            return '<i class="icon-silhouette-' . $this->sex() . '"></i>';
4634a9f750fSGreg Roach        }
4644a9f750fSGreg Roach
4654a9f750fSGreg Roach        return '';
466a25f0a04SGreg Roach    }
467a25f0a04SGreg Roach
468a25f0a04SGreg Roach    /**
469a25f0a04SGreg Roach     * Get the date of birth
470a25f0a04SGreg Roach     *
471a25f0a04SGreg Roach     * @return Date
472a25f0a04SGreg Roach     */
4738f53f488SRico Sonntag    public function getBirthDate(): Date
474c1010edaSGreg Roach    {
475a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
476a25f0a04SGreg Roach            if ($date->isOK()) {
477a25f0a04SGreg Roach                return $date;
478a25f0a04SGreg Roach            }
479a25f0a04SGreg Roach        }
480a25f0a04SGreg Roach
481a25f0a04SGreg Roach        return new Date('');
482a25f0a04SGreg Roach    }
483a25f0a04SGreg Roach
484a25f0a04SGreg Roach    /**
485a25f0a04SGreg Roach     * Get the place of birth
486a25f0a04SGreg Roach     *
48716d0b7f7SRico Sonntag     * @return Place
488a25f0a04SGreg Roach     */
4898f53f488SRico Sonntag    public function getBirthPlace(): Place
490c1010edaSGreg Roach    {
491a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
492a25f0a04SGreg Roach            return $place;
493a25f0a04SGreg Roach        }
494a25f0a04SGreg Roach
495b20ddbf9SGreg Roach        return new Place('', $this->tree);
496a25f0a04SGreg Roach    }
497a25f0a04SGreg Roach
498a25f0a04SGreg Roach    /**
499a25f0a04SGreg Roach     * Get the year of birth
500a25f0a04SGreg Roach     *
501a25f0a04SGreg Roach     * @return string the year of birth
502a25f0a04SGreg Roach     */
5038f53f488SRico Sonntag    public function getBirthYear(): string
504c1010edaSGreg Roach    {
505f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
506a25f0a04SGreg Roach    }
507a25f0a04SGreg Roach
508a25f0a04SGreg Roach    /**
509a25f0a04SGreg Roach     * Get the date of death
510a25f0a04SGreg Roach     *
511a25f0a04SGreg Roach     * @return Date
512a25f0a04SGreg Roach     */
5138f53f488SRico Sonntag    public function getDeathDate(): Date
514c1010edaSGreg Roach    {
515a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
516a25f0a04SGreg Roach            if ($date->isOK()) {
517a25f0a04SGreg Roach                return $date;
518a25f0a04SGreg Roach            }
519a25f0a04SGreg Roach        }
520a25f0a04SGreg Roach
521a25f0a04SGreg Roach        return new Date('');
522a25f0a04SGreg Roach    }
523a25f0a04SGreg Roach
524a25f0a04SGreg Roach    /**
525a25f0a04SGreg Roach     * Get the place of death
526a25f0a04SGreg Roach     *
52716d0b7f7SRico Sonntag     * @return Place
528a25f0a04SGreg Roach     */
5298f53f488SRico Sonntag    public function getDeathPlace(): Place
530c1010edaSGreg Roach    {
531a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
532a25f0a04SGreg Roach            return $place;
533a25f0a04SGreg Roach        }
534a25f0a04SGreg Roach
535b20ddbf9SGreg Roach        return new Place('', $this->tree);
536a25f0a04SGreg Roach    }
537a25f0a04SGreg Roach
538a25f0a04SGreg Roach    /**
539a25f0a04SGreg Roach     * get the death year
540a25f0a04SGreg Roach     *
541a25f0a04SGreg Roach     * @return string the year of death
542a25f0a04SGreg Roach     */
5438f53f488SRico Sonntag    public function getDeathYear(): string
544c1010edaSGreg Roach    {
545f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
546a25f0a04SGreg Roach    }
547a25f0a04SGreg Roach
548a25f0a04SGreg Roach    /**
549a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
55015d603e7SGreg Roach     * Provide the place and full date using a tooltip.
551a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
552a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
553a25f0a04SGreg Roach     *
554a25f0a04SGreg Roach     * @return string
555a25f0a04SGreg Roach     */
5568f53f488SRico Sonntag    public function getLifeSpan(): string
557c1010edaSGreg Roach    {
55815d603e7SGreg Roach        // Just the first part of the place name
559392561bbSGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->shortName());
560392561bbSGreg Roach        $death_place = strip_tags($this->getDeathPlace()->shortName());
56115d603e7SGreg Roach        // Remove markup from dates
56215d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56315d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56415d603e7SGreg Roach
565c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
566bbb76c12SGreg Roach        return
567c1010edaSGreg Roach            I18N::translate(
568a25f0a04SGreg Roach                '%1$s–%2$s',
5692d4c1bedSGreg Roach                '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
5702d4c1bedSGreg Roach                '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
571a25f0a04SGreg Roach            );
572a25f0a04SGreg Roach    }
573a25f0a04SGreg Roach
574a25f0a04SGreg Roach    /**
575a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
576a25f0a04SGreg Roach     *
577a25f0a04SGreg Roach     * @return Date[]
578a25f0a04SGreg Roach     */
5798f53f488SRico Sonntag    public function getAllBirthDates(): array
580c1010edaSGreg Roach    {
5818d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5828d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
583a25f0a04SGreg Roach            if ($tmp) {
584a25f0a04SGreg Roach                return $tmp;
585a25f0a04SGreg Roach            }
586a25f0a04SGreg Roach        }
587a25f0a04SGreg Roach
58813abd6f3SGreg Roach        return [];
589a25f0a04SGreg Roach    }
590a25f0a04SGreg Roach
591a25f0a04SGreg Roach    /**
592a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
593a25f0a04SGreg Roach     *
5944080d558SGreg Roach     * @return Place[]
595a25f0a04SGreg Roach     */
5968f53f488SRico Sonntag    public function getAllBirthPlaces(): array
597c1010edaSGreg Roach    {
5988d0ebef0SGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $event) {
5998d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6004080d558SGreg Roach            if (!empty($places)) {
6014080d558SGreg Roach                return $places;
602a25f0a04SGreg Roach            }
603a25f0a04SGreg Roach        }
604a25f0a04SGreg Roach
60513abd6f3SGreg Roach        return [];
606a25f0a04SGreg Roach    }
607a25f0a04SGreg Roach
608a25f0a04SGreg Roach    /**
609a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
610a25f0a04SGreg Roach     *
611a25f0a04SGreg Roach     * @return Date[]
612a25f0a04SGreg Roach     */
6138f53f488SRico Sonntag    public function getAllDeathDates(): array
614c1010edaSGreg Roach    {
6158d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6168d0ebef0SGreg Roach            $tmp = $this->getAllEventDates([$event]);
617a25f0a04SGreg Roach            if ($tmp) {
618a25f0a04SGreg Roach                return $tmp;
619a25f0a04SGreg Roach            }
620a25f0a04SGreg Roach        }
621a25f0a04SGreg Roach
62213abd6f3SGreg Roach        return [];
623a25f0a04SGreg Roach    }
624a25f0a04SGreg Roach
625a25f0a04SGreg Roach    /**
626a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
627a25f0a04SGreg Roach     *
6284080d558SGreg Roach     * @return Place[]
629a25f0a04SGreg Roach     */
6308f53f488SRico Sonntag    public function getAllDeathPlaces(): array
631c1010edaSGreg Roach    {
6328d0ebef0SGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
6338d0ebef0SGreg Roach            $places = $this->getAllEventPlaces([$event]);
6344080d558SGreg Roach            if (!empty($places)) {
6354080d558SGreg Roach                return $places;
636a25f0a04SGreg Roach            }
637a25f0a04SGreg Roach        }
638a25f0a04SGreg Roach
63913abd6f3SGreg Roach        return [];
640a25f0a04SGreg Roach    }
641a25f0a04SGreg Roach
642a25f0a04SGreg Roach    /**
643a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
644a25f0a04SGreg Roach     *
645a25f0a04SGreg Roach     * @return Date
646a25f0a04SGreg Roach     */
6478f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
648c1010edaSGreg Roach    {
6498f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
650a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
651a25f0a04SGreg Roach                if ($date->isOK()) {
6524686330aSGreg Roach                    $this->estimated_birth_date = $date;
653a25f0a04SGreg Roach                    break;
654a25f0a04SGreg Roach                }
655a25f0a04SGreg Roach            }
6568f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
65713abd6f3SGreg Roach                $min = [];
65813abd6f3SGreg Roach                $max = [];
659a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
660f5b60decSGreg Roach                if ($tmp->isOK()) {
661f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
662f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
663a25f0a04SGreg Roach                }
66439ca88baSGreg Roach                foreach ($this->childFamilies() as $family) {
665a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
666f5b60decSGreg Roach                    if ($tmp->isOK()) {
667f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
668f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
669a25f0a04SGreg Roach                    }
67039ca88baSGreg Roach                    $husband = $family->husband();
6712e5b4452SGreg Roach                    if ($husband instanceof Individual) {
6722e5b4452SGreg Roach                        $tmp = $husband->getBirthDate();
673f5b60decSGreg Roach                        if ($tmp->isOK()) {
674f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
675f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
676a25f0a04SGreg Roach                        }
677a25f0a04SGreg Roach                    }
67839ca88baSGreg Roach                    $wife = $family->wife();
6792e5b4452SGreg Roach                    if ($wife instanceof Individual) {
6802e5b4452SGreg Roach                        $tmp = $wife->getBirthDate();
681f5b60decSGreg Roach                        if ($tmp->isOK()) {
682f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
683f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
684a25f0a04SGreg Roach                        }
685a25f0a04SGreg Roach                    }
68639ca88baSGreg Roach                    foreach ($family->children() as $child) {
687a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
688f5b60decSGreg Roach                        if ($tmp->isOK()) {
689f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
690f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
691a25f0a04SGreg Roach                        }
692a25f0a04SGreg Roach                    }
693a25f0a04SGreg Roach                }
69439ca88baSGreg Roach                foreach ($this->spouseFamilies() as $family) {
695a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
696f5b60decSGreg Roach                    if ($tmp->isOK()) {
697f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
698f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
699a25f0a04SGreg Roach                    }
70039ca88baSGreg Roach                    $spouse = $family->spouse($this);
701a25f0a04SGreg Roach                    if ($spouse) {
702a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
703f5b60decSGreg Roach                        if ($tmp->isOK()) {
704f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
705f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
706a25f0a04SGreg Roach                        }
707a25f0a04SGreg Roach                    }
70839ca88baSGreg Roach                    foreach ($family->children() as $child) {
709a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
710f5b60decSGreg Roach                        if ($tmp->isOK()) {
71139ca88baSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() == 'F' ? 45 : 65);
712f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
713a25f0a04SGreg Roach                        }
714a25f0a04SGreg Roach                    }
715a25f0a04SGreg Roach                }
716a25f0a04SGreg Roach                if ($min && $max) {
71759f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
718a25f0a04SGreg Roach
71965e02381SGreg Roach                    [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2));
7204686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
721a25f0a04SGreg Roach                } else {
7224686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
723a25f0a04SGreg Roach                }
724a25f0a04SGreg Roach            }
725a25f0a04SGreg Roach        }
726a25f0a04SGreg Roach
7274686330aSGreg Roach        return $this->estimated_birth_date;
728a25f0a04SGreg Roach    }
729a25f0a04SGreg Roach
730a25f0a04SGreg Roach    /**
731a25f0a04SGreg Roach     * Generate an estimated date of death.
732a25f0a04SGreg Roach     *
733a25f0a04SGreg Roach     * @return Date
734a25f0a04SGreg Roach     */
7358f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
736c1010edaSGreg Roach    {
7374686330aSGreg Roach        if ($this->estimated_death_date === null) {
738a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
739a25f0a04SGreg Roach                if ($date->isOK()) {
7404686330aSGreg Roach                    $this->estimated_death_date = $date;
741a25f0a04SGreg Roach                    break;
742a25f0a04SGreg Roach                }
743a25f0a04SGreg Roach            }
7444686330aSGreg Roach            if ($this->estimated_death_date === null) {
745f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
746c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7474686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
748a25f0a04SGreg Roach                } else {
7494686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
750a25f0a04SGreg Roach                }
751a25f0a04SGreg Roach            }
752a25f0a04SGreg Roach        }
753a25f0a04SGreg Roach
7544686330aSGreg Roach        return $this->estimated_death_date;
755a25f0a04SGreg Roach    }
756a25f0a04SGreg Roach
757a25f0a04SGreg Roach    /**
758a25f0a04SGreg Roach     * Get the sex - M F or U
759a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
760a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
761a25f0a04SGreg Roach     *
762a25f0a04SGreg Roach     * @return string
763a25f0a04SGreg Roach     */
76439ca88baSGreg Roach    public function sex()
765c1010edaSGreg Roach    {
766a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
767a25f0a04SGreg Roach            return $match[1];
768a25f0a04SGreg Roach        }
769b2ce94c6SRico Sonntag
770b2ce94c6SRico Sonntag        return 'U';
771a25f0a04SGreg Roach    }
772a25f0a04SGreg Roach
773a25f0a04SGreg Roach    /**
774a25f0a04SGreg Roach     * Get the individual’s sex image
775a25f0a04SGreg Roach     *
776a25f0a04SGreg Roach     * @param string $size
777a25f0a04SGreg Roach     *
778a25f0a04SGreg Roach     * @return string
779a25f0a04SGreg Roach     */
7808f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
781c1010edaSGreg Roach    {
78239ca88baSGreg Roach        return self::sexImage($this->sex(), $size);
783a25f0a04SGreg Roach    }
784a25f0a04SGreg Roach
785a25f0a04SGreg Roach    /**
786a25f0a04SGreg Roach     * Generate a sex icon/image
787a25f0a04SGreg Roach     *
788a25f0a04SGreg Roach     * @param string $sex
789a25f0a04SGreg Roach     * @param string $size
790a25f0a04SGreg Roach     *
791a25f0a04SGreg Roach     * @return string
792a25f0a04SGreg Roach     */
7938f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
794c1010edaSGreg Roach    {
795242a7862SGreg Roach        $image = view('icons/sex-' . $sex);
796242a7862SGreg Roach
797242a7862SGreg Roach        if ($size === 'small') {
798242a7862SGreg Roach            $image = '<small>' . $image . '</small>';
799242a7862SGreg Roach        }
800242a7862SGreg Roach
801242a7862SGreg Roach        return $image;
802a25f0a04SGreg Roach    }
803a25f0a04SGreg Roach
804a25f0a04SGreg Roach    /**
805a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
806a25f0a04SGreg Roach     *
807a25f0a04SGreg Roach     * @return string
808a25f0a04SGreg Roach     */
8098f53f488SRico Sonntag    public function getBoxStyle(): string
810c1010edaSGreg Roach    {
811c1010edaSGreg Roach        $tmp = [
812c1010edaSGreg Roach            'M' => '',
813c1010edaSGreg Roach            'F' => 'F',
814c1010edaSGreg Roach            'U' => 'NN',
815c1010edaSGreg Roach        ];
816a25f0a04SGreg Roach
81739ca88baSGreg Roach        return 'person_box' . $tmp[$this->sex()];
818a25f0a04SGreg Roach    }
819a25f0a04SGreg Roach
820a25f0a04SGreg Roach    /**
821a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
822a25f0a04SGreg Roach     *
823cbc1590aSGreg Roach     * @param int|null $access_level
824a25f0a04SGreg Roach     *
82539ca88baSGreg Roach     * @return Collection|Family[]
826a25f0a04SGreg Roach     */
82739ca88baSGreg Roach    public function spouseFamilies($access_level = null): Collection
828c1010edaSGreg Roach    {
8294b9ff166SGreg Roach        if ($access_level === null) {
8304b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8314b9ff166SGreg Roach        }
8324b9ff166SGreg Roach
833acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
834a25f0a04SGreg Roach
83539ca88baSGreg Roach        $families = new Collection();
8368d0ebef0SGreg Roach        foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
837dc124885SGreg Roach            $family = $fact->target();
838e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
83939ca88baSGreg Roach                $families->push($family);
840a25f0a04SGreg Roach            }
841a25f0a04SGreg Roach        }
842a25f0a04SGreg Roach
84339ca88baSGreg Roach        return new Collection($families);
844a25f0a04SGreg Roach    }
845a25f0a04SGreg Roach
846a25f0a04SGreg Roach    /**
847a25f0a04SGreg Roach     * Get the current spouse of this individual.
848a25f0a04SGreg Roach     *
849a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
850a25f0a04SGreg Roach     * in chronological order, and take the last one found.
851a25f0a04SGreg Roach     *
852a25f0a04SGreg Roach     * @return Individual|null
853a25f0a04SGreg Roach     */
854c1010edaSGreg Roach    public function getCurrentSpouse()
855c1010edaSGreg Roach    {
85639ca88baSGreg Roach        $family = $this->spouseFamilies()->last();
85739ca88baSGreg Roach
85839ca88baSGreg Roach        if ($family instanceof Family) {
85939ca88baSGreg Roach            return $family->spouse($this);
860a25f0a04SGreg Roach        }
861b2ce94c6SRico Sonntag
862b2ce94c6SRico Sonntag        return null;
863a25f0a04SGreg Roach    }
864a25f0a04SGreg Roach
865a25f0a04SGreg Roach    /**
866a25f0a04SGreg Roach     * Count the children belonging to this individual.
867a25f0a04SGreg Roach     *
868cbc1590aSGreg Roach     * @return int
869a25f0a04SGreg Roach     */
87039ca88baSGreg Roach    public function numberOfChildren()
871c1010edaSGreg Roach    {
8727d0db648SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) {
8733dc7bbe9SGreg Roach            return (int) $match[1];
874b2ce94c6SRico Sonntag        }
875b2ce94c6SRico Sonntag
87613abd6f3SGreg Roach        $children = [];
87739ca88baSGreg Roach        foreach ($this->spouseFamilies() as $fam) {
87839ca88baSGreg Roach            foreach ($fam->children() as $child) {
879c0935879SGreg Roach                $children[$child->xref()] = true;
880a25f0a04SGreg Roach            }
881a25f0a04SGreg Roach        }
882a25f0a04SGreg Roach
883a25f0a04SGreg Roach        return count($children);
884a25f0a04SGreg Roach    }
885a25f0a04SGreg Roach
886a25f0a04SGreg Roach    /**
887a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
888a25f0a04SGreg Roach     *
889cbc1590aSGreg Roach     * @param int|null $access_level
890a25f0a04SGreg Roach     *
89139ca88baSGreg Roach     * @return Collection|Family[]
892a25f0a04SGreg Roach     */
89339ca88baSGreg Roach    public function childFamilies($access_level = null): Collection
894c1010edaSGreg Roach    {
8954b9ff166SGreg Roach        if ($access_level === null) {
8964b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8974b9ff166SGreg Roach        }
8984b9ff166SGreg Roach
899acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
900a25f0a04SGreg Roach
90139ca88baSGreg Roach        $families = new Collection();
90239ca88baSGreg Roach
9038d0ebef0SGreg Roach        foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
904dc124885SGreg Roach            $family = $fact->target();
905e24444eeSGreg Roach            if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
90639ca88baSGreg Roach                $families->push($family);
907a25f0a04SGreg Roach            }
908a25f0a04SGreg Roach        }
909a25f0a04SGreg Roach
910a25f0a04SGreg Roach        return $families;
911a25f0a04SGreg Roach    }
912a25f0a04SGreg Roach
913a25f0a04SGreg Roach    /**
914a25f0a04SGreg Roach     * Get the preferred parents for this individual.
915a25f0a04SGreg Roach     *
916a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
917a25f0a04SGreg Roach     * The preferred family record is:
918a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
919a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
920a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
921a25f0a04SGreg Roach     * (d) the first one found
922a25f0a04SGreg Roach     *
923a25f0a04SGreg Roach     * @return Family|null
924a25f0a04SGreg Roach     */
92539ca88baSGreg Roach    public function primaryChildFamily()
926c1010edaSGreg Roach    {
92739ca88baSGreg Roach        $families = $this->childFamilies();
928a25f0a04SGreg Roach        switch (count($families)) {
929a25f0a04SGreg Roach            case 0:
930a25f0a04SGreg Roach                return null;
931a25f0a04SGreg Roach            case 1:
93215d603e7SGreg Roach                return $families[0];
933a25f0a04SGreg Roach            default:
934a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
935a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
93674e78bfaSJonathan Jaubart                foreach ($families as $fam) {
937c0935879SGreg Roach                    $famid = $fam->xref();
9387d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
939a25f0a04SGreg Roach                        return $fam;
940a25f0a04SGreg Roach                    }
941a25f0a04SGreg Roach                }
942a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
94374e78bfaSJonathan Jaubart                foreach ($families as $fam) {
944c0935879SGreg Roach                    $famid = $fam->xref();
9457d0db648SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
946a25f0a04SGreg Roach                        return $fam;
947a25f0a04SGreg Roach                    }
948a25f0a04SGreg Roach                }
949a25f0a04SGreg Roach                // c) records with no '2 PEDI'
95074e78bfaSJonathan Jaubart                foreach ($families as $fam) {
951c0935879SGreg Roach                    $famid = $fam->xref();
9527d0db648SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
953a25f0a04SGreg Roach                        return $fam;
954a25f0a04SGreg Roach                    }
955a25f0a04SGreg Roach                }
956a25f0a04SGreg Roach
957a25f0a04SGreg Roach                // d) any record
95815d603e7SGreg Roach                return $families[0];
959a25f0a04SGreg Roach        }
960a25f0a04SGreg Roach    }
961a25f0a04SGreg Roach
962a25f0a04SGreg Roach    /**
963a25f0a04SGreg Roach     * Get a list of step-parent families.
964a25f0a04SGreg Roach     *
965820b62dfSGreg Roach     * @return Collection|Family[]
966a25f0a04SGreg Roach     */
967820b62dfSGreg Roach    public function childStepFamilies(): Collection
968c1010edaSGreg Roach    {
96913abd6f3SGreg Roach        $step_families = [];
97039ca88baSGreg Roach        $families      = $this->childFamilies();
971a25f0a04SGreg Roach        foreach ($families as $family) {
97239ca88baSGreg Roach            $father = $family->husband();
973a25f0a04SGreg Roach            if ($father) {
97439ca88baSGreg Roach                foreach ($father->spouseFamilies() as $step_family) {
97539ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
976a25f0a04SGreg Roach                        $step_families[] = $step_family;
977a25f0a04SGreg Roach                    }
978a25f0a04SGreg Roach                }
979a25f0a04SGreg Roach            }
98039ca88baSGreg Roach            $mother = $family->wife();
981a25f0a04SGreg Roach            if ($mother) {
98239ca88baSGreg Roach                foreach ($mother->spouseFamilies() as $step_family) {
98339ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
984a25f0a04SGreg Roach                        $step_families[] = $step_family;
985a25f0a04SGreg Roach                    }
986a25f0a04SGreg Roach                }
987a25f0a04SGreg Roach            }
988a25f0a04SGreg Roach        }
989a25f0a04SGreg Roach
990820b62dfSGreg Roach        return new Collection($step_families);
991a25f0a04SGreg Roach    }
992a25f0a04SGreg Roach
993a25f0a04SGreg Roach    /**
994a25f0a04SGreg Roach     * Get a list of step-parent families.
995a25f0a04SGreg Roach     *
996820b62dfSGreg Roach     * @return Collection|Family[]
997a25f0a04SGreg Roach     */
998820b62dfSGreg Roach    public function spouseStepFamilies(): Collection
999c1010edaSGreg Roach    {
100013abd6f3SGreg Roach        $step_families = [];
100139ca88baSGreg Roach        $families      = $this->spouseFamilies();
1002820b62dfSGreg Roach
1003a25f0a04SGreg Roach        foreach ($families as $family) {
100439ca88baSGreg Roach            $spouse = $family->spouse($this);
1005820b62dfSGreg Roach
1006a25f0a04SGreg Roach            if ($spouse) {
100739ca88baSGreg Roach                foreach ($family->spouse($this)->spouseFamilies() as $step_family) {
100839ca88baSGreg Roach                    if (!$families->containsStrict($step_family)) {
1009a25f0a04SGreg Roach                        $step_families[] = $step_family;
1010a25f0a04SGreg Roach                    }
1011a25f0a04SGreg Roach                }
1012a25f0a04SGreg Roach            }
1013a25f0a04SGreg Roach        }
1014a25f0a04SGreg Roach
1015820b62dfSGreg Roach        return new Collection($step_families);
1016a25f0a04SGreg Roach    }
1017a25f0a04SGreg Roach
1018a25f0a04SGreg Roach    /**
1019a25f0a04SGreg Roach     * A label for a parental family group
1020a25f0a04SGreg Roach     *
1021a25f0a04SGreg Roach     * @param Family $family
1022a25f0a04SGreg Roach     *
1023a25f0a04SGreg Roach     * @return string
1024a25f0a04SGreg Roach     */
1025820b62dfSGreg Roach    public function getChildFamilyLabel(Family $family): string
1026c1010edaSGreg Roach    {
10277d0db648SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) {
1028a25f0a04SGreg Roach            // A specified pedigree
1029764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1030b2ce94c6SRico Sonntag        }
1031b2ce94c6SRico Sonntag
1032a25f0a04SGreg Roach        // Default (birth) pedigree
1033764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1034a25f0a04SGreg Roach    }
1035a25f0a04SGreg Roach
1036a25f0a04SGreg Roach    /**
1037a25f0a04SGreg Roach     * Create a label for a step family
1038a25f0a04SGreg Roach     *
1039a25f0a04SGreg Roach     * @param Family $step_family
1040a25f0a04SGreg Roach     *
1041a25f0a04SGreg Roach     * @return string
1042a25f0a04SGreg Roach     */
10438f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1044c1010edaSGreg Roach    {
104539ca88baSGreg Roach        foreach ($this->childFamilies() as $family) {
1046a25f0a04SGreg Roach            if ($family !== $step_family) {
1047a25f0a04SGreg Roach                // Must be a step-family
104839ca88baSGreg Roach                foreach ($family->spouses() as $parent) {
104939ca88baSGreg Roach                    foreach ($step_family->spouses() as $step_parent) {
1050a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1051a25f0a04SGreg Roach                            // One common parent - must be a step family
105239ca88baSGreg Roach                            if ($parent->sex() == 'M') {
1053a25f0a04SGreg Roach                                // Father’s family with someone else
105439ca88baSGreg Roach                                if ($step_family->spouse($step_parent)) {
1055a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
105639ca88baSGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName());
1057b2ce94c6SRico Sonntag                                }
1058b2ce94c6SRico Sonntag
1059a25f0a04SGreg Roach                                /* I18N: A step-family. */
1060bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1061a25f0a04SGreg Roach                            }
1062b2ce94c6SRico Sonntag
1063a25f0a04SGreg Roach                            // Mother’s family with someone else
106439ca88baSGreg Roach                            if ($step_family->spouse($step_parent)) {
1065a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
106639ca88baSGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName());
1067b2ce94c6SRico Sonntag                            }
1068b2ce94c6SRico Sonntag
1069a25f0a04SGreg Roach                            /* I18N: A step-family. */
1070bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1071a25f0a04SGreg Roach                        }
1072a25f0a04SGreg Roach                    }
1073a25f0a04SGreg Roach                }
1074a25f0a04SGreg Roach            }
1075a25f0a04SGreg Roach        }
1076a25f0a04SGreg Roach
1077a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1078a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1079a25f0a04SGreg Roach    }
1080a25f0a04SGreg Roach
1081225e381fSGreg Roach    /**
1082225e381fSGreg Roach     * Get the description for the family.
1083225e381fSGreg Roach     *
1084225e381fSGreg Roach     * For example, "XXX's family with new wife".
1085225e381fSGreg Roach     *
1086225e381fSGreg Roach     * @param Family $family
1087225e381fSGreg Roach     *
1088225e381fSGreg Roach     * @return string
1089225e381fSGreg Roach     */
1090c1010edaSGreg Roach    public function getSpouseFamilyLabel(Family $family)
1091c1010edaSGreg Roach    {
109239ca88baSGreg Roach        $spouse = $family->spouse($this);
1093225e381fSGreg Roach        if ($spouse) {
1094225e381fSGreg Roach            /* I18N: %s is the spouse name */
109539ca88baSGreg Roach            return I18N::translate('Family with %s', $spouse->fullName());
1096225e381fSGreg Roach        }
1097b2ce94c6SRico Sonntag
109839ca88baSGreg Roach        return $family->fullName();
1099225e381fSGreg Roach    }
1100225e381fSGreg Roach
1101a25f0a04SGreg Roach    /**
1102a25f0a04SGreg Roach     * get primary parents names for this individual
1103a25f0a04SGreg Roach     *
1104a25f0a04SGreg Roach     * @param string $classname optional css class
1105a25f0a04SGreg Roach     * @param string $display   optional css style display
1106a25f0a04SGreg Roach     *
1107a25f0a04SGreg Roach     * @return string a div block with father & mother names
1108a25f0a04SGreg Roach     */
11098f53f488SRico Sonntag    public function getPrimaryParentsNames($classname = '', $display = ''): string
1110c1010edaSGreg Roach    {
111139ca88baSGreg Roach        $fam = $this->primaryChildFamily();
1112a25f0a04SGreg Roach        if (!$fam) {
1113a25f0a04SGreg Roach            return '';
1114a25f0a04SGreg Roach        }
1115a25f0a04SGreg Roach        $txt = '<div';
1116a25f0a04SGreg Roach        if ($classname) {
11174c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1118a25f0a04SGreg Roach        }
1119a25f0a04SGreg Roach        if ($display) {
11204c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1121a25f0a04SGreg Roach        }
1122a25f0a04SGreg Roach        $txt .= '>';
112339ca88baSGreg Roach        $husb = $fam->husband();
1124a25f0a04SGreg Roach        if ($husb) {
1125a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1126a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1127a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1128a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1129a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
113039ca88baSGreg Roach            $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>';
1131a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1132a25f0a04SGreg Roach        }
113339ca88baSGreg Roach        $wife = $fam->wife();
1134a25f0a04SGreg Roach        if ($wife) {
1135a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1136a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1137a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1138a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1139a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
114039ca88baSGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->fullName());
1141a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1142a25f0a04SGreg Roach        }
1143a25f0a04SGreg Roach        $txt .= '</div>';
1144a25f0a04SGreg Roach
1145a25f0a04SGreg Roach        return $txt;
1146a25f0a04SGreg Roach    }
1147a25f0a04SGreg Roach
1148961ec755SGreg Roach    /**
1149961ec755SGreg Roach     * If this object has no name, what do we call it?
1150961ec755SGreg Roach     *
1151961ec755SGreg Roach     * @return string
1152961ec755SGreg Roach     */
11538f53f488SRico Sonntag    public function getFallBackName(): string
1154c1010edaSGreg Roach    {
1155a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1156a25f0a04SGreg Roach    }
1157a25f0a04SGreg Roach
1158a25f0a04SGreg Roach    /**
1159a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1160a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1161a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1162a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1163a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1164a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1165a25f0a04SGreg Roach     *
1166a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1167a25f0a04SGreg Roach     * 2 GIVN Robert
1168a25f0a04SGreg Roach     * 2 SPFX de
1169a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1170a25f0a04SGreg Roach     * 2 NICK The Bald
1171a25f0a04SGreg Roach     *
1172a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1173a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1174a25f0a04SGreg Roach     *
1175a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1176a25f0a04SGreg Roach     *
1177a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1178a25f0a04SGreg Roach     * or
1179a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1180a25f0a04SGreg Roach     * 2 GIVN Carlos
1181a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1182a25f0a04SGreg Roach     *
1183a25f0a04SGreg Roach     * @param string $type
1184a25f0a04SGreg Roach     * @param string $full
1185a25f0a04SGreg Roach     * @param string $gedcom
1186a25f0a04SGreg Roach     */
118776f666f4SGreg Roach    protected function addName(string $type, string $full, string $gedcom)
1188c1010edaSGreg Roach    {
1189a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1190a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1191a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1192a25f0a04SGreg Roach
119376f666f4SGreg Roach        $sublevel = 1 + (int) substr($gedcom, 0, 1);
1194a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1195a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1196a25f0a04SGreg Roach        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1197a25f0a04SGreg Roach
1198a25f0a04SGreg Roach        // SURN is an comma-separated list of surnames...
119976f666f4SGreg Roach        if ($SURN !== '') {
1200a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1201a25f0a04SGreg Roach        } else {
120213abd6f3SGreg Roach            $SURNS = [];
1203a25f0a04SGreg Roach        }
120476f666f4SGreg Roach
1205a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1206a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1207a25f0a04SGreg Roach
1208a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1209a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1210a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1211a25f0a04SGreg Roach
1212a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
121376f666f4SGreg Roach        if (substr_count($full, '/') % 2 === 1) {
1214a25f0a04SGreg Roach            $full = $full . '/';
1215a25f0a04SGreg Roach        }
1216a25f0a04SGreg Roach
1217a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1218a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1219a25f0a04SGreg Roach
1220a25f0a04SGreg Roach        // Extract the surname.
1221a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1222a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1223a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1224a25f0a04SGreg Roach        } else {
1225a25f0a04SGreg Roach            $surname = '';
1226a25f0a04SGreg Roach        }
1227a25f0a04SGreg Roach
1228a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1229a25f0a04SGreg Roach        if (!$SURNS) {
1230a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1231a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1232a25f0a04SGreg Roach                $SURNS = $matches[1];
1233a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1234a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1235a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1236a25f0a04SGreg Roach                }
1237a25f0a04SGreg Roach            } else {
1238a25f0a04SGreg Roach                // It is valid not to have a surname at all
123913abd6f3SGreg Roach                $SURNS = [''];
1240a25f0a04SGreg Roach            }
1241a25f0a04SGreg Roach        }
1242a25f0a04SGreg Roach
1243a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1244a25f0a04SGreg Roach        if (!$GIVN) {
1245a25f0a04SGreg Roach            $GIVN = preg_replace(
124613abd6f3SGreg Roach                [
1247c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1248c1010edaSGreg Roach                    // remove surname
1249c1010edaSGreg Roach                    '/ ?".+"/',
1250c1010edaSGreg Roach                    // remove nickname
1251c1010edaSGreg Roach                    '/ {2,}/',
1252c1010edaSGreg Roach                    // multiple spaces, caused by the above
1253c1010edaSGreg Roach                    '/^ | $/',
1254c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
125513abd6f3SGreg Roach                ],
125613abd6f3SGreg Roach                [
1257a25f0a04SGreg Roach                    ' ',
1258a25f0a04SGreg Roach                    ' ',
1259a25f0a04SGreg Roach                    ' ',
1260a25f0a04SGreg Roach                    '',
126113abd6f3SGreg Roach                ],
1262a25f0a04SGreg Roach                $full
1263a25f0a04SGreg Roach            );
1264a25f0a04SGreg Roach        }
1265a25f0a04SGreg Roach
1266a25f0a04SGreg Roach        // Add placeholder for unknown given name
1267a25f0a04SGreg Roach        if (!$GIVN) {
1268a25f0a04SGreg Roach            $GIVN = '@P.N.';
126973f4f553SGreg Roach            $pos  = (int) strpos($full, '/');
1270a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1271a25f0a04SGreg Roach        }
1272a25f0a04SGreg Roach
12737b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12747b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1275c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12767b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1277c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1278a25f0a04SGreg Roach        }
1279a25f0a04SGreg Roach
1280a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1281a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1282a25f0a04SGreg Roach        // $full is for display on-screen
1283a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1284a25f0a04SGreg Roach
1285a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1286ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1287ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1288c6f196c3SGreg Roach        // Format for display
1289d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1290acc34ea1SGreg Roach        // Localise quotation marks around the nickname
129118d7a90dSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function (array $matches): string {
12928d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12938d68cabeSGreg Roach        }, $full);
1294a25f0a04SGreg Roach
1295c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1296a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1297a25f0a04SGreg Roach
1298a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1299a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1300a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1301a25f0a04SGreg Roach
1302ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1303a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1304a25f0a04SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1305a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1306a25f0a04SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1307a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1308a25f0a04SGreg Roach            }
1309a25f0a04SGreg Roach
1310bdb3725aSGreg Roach            $this->getAllNames[] = [
1311a25f0a04SGreg Roach                'type'    => $type,
1312a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1313c1010edaSGreg Roach                'full'    => $full,
1314c1010edaSGreg Roach                // This is used for display
1315c1010edaSGreg Roach                'fullNN'  => $fullNN,
1316c1010edaSGreg Roach                // This goes into the database
1317c1010edaSGreg Roach                'surname' => $surname,
1318c1010edaSGreg Roach                // This goes into the database
1319c1010edaSGreg Roach                'givn'    => $GIVN,
1320c1010edaSGreg Roach                // This goes into the database
1321c1010edaSGreg Roach                'surn'    => $SURN,
1322c1010edaSGreg Roach                // This goes into the database
132313abd6f3SGreg Roach            ];
1324a25f0a04SGreg Roach        }
1325a25f0a04SGreg Roach    }
1326a25f0a04SGreg Roach
1327a25f0a04SGreg Roach    /**
132876692c8bSGreg Roach     * Extract names from the GEDCOM record.
1329c7ff4153SGreg Roach     *
1330c7ff4153SGreg Roach     * @return void
1331a25f0a04SGreg Roach     */
1332c1010edaSGreg Roach    public function extractNames()
1333c1010edaSGreg Roach    {
13348f53f488SRico Sonntag        $this->extractNamesFromFacts(
13358f53f488SRico Sonntag            1,
13368f53f488SRico Sonntag            'NAME',
133730158ae7SGreg Roach            $this->facts(
13388d0ebef0SGreg Roach                ['NAME'],
13398f53f488SRico Sonntag                false,
13408f53f488SRico Sonntag                Auth::accessLevel($this->tree),
13418f53f488SRico Sonntag                $this->canShowName()
13428f53f488SRico Sonntag            )
13438f53f488SRico Sonntag        );
1344a25f0a04SGreg Roach    }
1345a25f0a04SGreg Roach
1346a25f0a04SGreg Roach    /**
1347a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1348a25f0a04SGreg Roach     * selection items or favorites.
1349a25f0a04SGreg Roach     *
1350a25f0a04SGreg Roach     * @return string
1351a25f0a04SGreg Roach     */
13528f53f488SRico Sonntag    public function formatListDetails(): string
1353c1010edaSGreg Roach    {
1354a25f0a04SGreg Roach        return
13558d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) .
13568d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1);
1357a25f0a04SGreg Roach    }
1358a25f0a04SGreg Roach}
1359