xref: /webtrees/app/Individual.php (revision 18d7a90d8a3b33b218801c0b68eb1a5140d7b4e7)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
2276692c8bSGreg Roach * A GEDCOM individual (INDI) object.
23a25f0a04SGreg Roach */
24c1010edaSGreg Roachclass Individual extends GedcomRecord
25c1010edaSGreg Roach{
26a25f0a04SGreg Roach    const RECORD_TYPE = 'INDI';
27225e381fSGreg Roach    const ROUTE_NAME  = 'individual';
28a25f0a04SGreg Roach
2976692c8bSGreg Roach    /** @var int used in some lists to keep track of this individual’s generation in that list */
3076692c8bSGreg Roach    public $generation;
31a25f0a04SGreg Roach
32a25f0a04SGreg Roach    /** @var Date The estimated date of birth */
334686330aSGreg Roach    private $estimated_birth_date;
34a25f0a04SGreg Roach
35a25f0a04SGreg Roach    /** @var Date The estimated date of death */
364686330aSGreg Roach    private $estimated_death_date;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /**
39e71ef9d2SGreg Roach     * Get an instance of an individual object. For single records,
40e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
41e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
42e71ef9d2SGreg Roach     *
43e71ef9d2SGreg Roach     * @param string      $xref
44e71ef9d2SGreg Roach     * @param Tree        $tree
45e71ef9d2SGreg Roach     * @param string|null $gedcom
46e71ef9d2SGreg Roach     *
47e71ef9d2SGreg Roach     * @throws \Exception
48e71ef9d2SGreg Roach     *
49e71ef9d2SGreg Roach     * @return Individual|null
50e71ef9d2SGreg Roach     */
5176f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
52c1010edaSGreg Roach    {
53e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
54e71ef9d2SGreg Roach
55e71ef9d2SGreg Roach        if ($record instanceof Individual) {
56e71ef9d2SGreg Roach            return $record;
57e71ef9d2SGreg Roach        }
58b2ce94c6SRico Sonntag
59b2ce94c6SRico Sonntag        return null;
60e71ef9d2SGreg Roach    }
61e71ef9d2SGreg Roach
62e71ef9d2SGreg Roach    /**
63395f0fe0SGreg Roach     * Sometimes, we'll know in advance that we need to load a set of records.
64395f0fe0SGreg Roach     * Typically when we load families and their members.
65395f0fe0SGreg Roach     *
66395f0fe0SGreg Roach     * @param Tree     $tree
674a8aaa00SScrutinizer Auto-Fixer     * @param string[] $xrefs
68*18d7a90dSGreg Roach     *
69*18d7a90dSGreg Roach     * @return void
70395f0fe0SGreg Roach     */
71c1010edaSGreg Roach    public static function load(Tree $tree, array $xrefs)
72c1010edaSGreg Roach    {
7313abd6f3SGreg Roach        $args         = [
74395f0fe0SGreg Roach            'tree_id' => $tree->getTreeId(),
7513abd6f3SGreg Roach        ];
7613abd6f3SGreg Roach        $placeholders = [];
77395f0fe0SGreg Roach
78395f0fe0SGreg Roach        foreach (array_unique($xrefs) as $n => $xref) {
79395f0fe0SGreg Roach            if (!isset(self::$gedcom_record_cache[$tree->getTreeId()][$xref])) {
80f7247c73SGreg Roach                $placeholders[] = ':x' . $n;
81395f0fe0SGreg Roach                $args['x' . $n] = $xref;
82395f0fe0SGreg Roach            }
83395f0fe0SGreg Roach        }
84395f0fe0SGreg Roach
85f7247c73SGreg Roach        if (!empty($placeholders)) {
86395f0fe0SGreg Roach            $rows = Database::prepare(
87395f0fe0SGreg Roach                "SELECT i_id AS xref, i_gedcom AS gedcom" .
88395f0fe0SGreg Roach                " FROM `##individuals`" .
89f7247c73SGreg Roach                " WHERE i_file = :tree_id AND i_id IN (" . implode(',', $placeholders) . ")"
90395f0fe0SGreg Roach            )->execute(
91395f0fe0SGreg Roach                $args
92395f0fe0SGreg Roach            )->fetchAll();
93395f0fe0SGreg Roach
94395f0fe0SGreg Roach            foreach ($rows as $row) {
95395f0fe0SGreg Roach                self::getInstance($row->xref, $tree, $row->gedcom);
96395f0fe0SGreg Roach            }
97395f0fe0SGreg Roach        }
98395f0fe0SGreg Roach    }
99395f0fe0SGreg Roach
100395f0fe0SGreg Roach    /**
101a25f0a04SGreg Roach     * Can the name of this record be shown?
102a25f0a04SGreg Roach     *
10376692c8bSGreg Roach     * @param int|null $access_level
10476692c8bSGreg Roach     *
10576692c8bSGreg Roach     * @return bool
106a25f0a04SGreg Roach     */
10735584196SGreg Roach    public function canShowName(int $access_level = null): bool
108c1010edaSGreg Roach    {
1094b9ff166SGreg Roach        if ($access_level === null) {
1104b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1114b9ff166SGreg Roach        }
1124b9ff166SGreg Roach
113518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
114a25f0a04SGreg Roach    }
115a25f0a04SGreg Roach
116a25f0a04SGreg Roach    /**
11776692c8bSGreg Roach     * Can this individual be shown?
118a25f0a04SGreg Roach     *
11976692c8bSGreg Roach     * @param int $access_level
12076692c8bSGreg Roach     *
12176692c8bSGreg Roach     * @return bool
122a25f0a04SGreg Roach     */
12335584196SGreg Roach    protected function canShowByType(int $access_level): bool
124c1010edaSGreg Roach    {
125a25f0a04SGreg Roach        // Dead people...
126518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
127a25f0a04SGreg Roach            $keep_alive             = false;
12854ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
129a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
130a25f0a04SGreg Roach                preg_match_all('/\n1 (?:' . WT_EVENTS_BIRT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
131a25f0a04SGreg Roach                foreach ($matches as $match) {
132a25f0a04SGreg Roach                    $date = new Date($match[1]);
133a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
134a25f0a04SGreg Roach                        $keep_alive = true;
135a25f0a04SGreg Roach                        break;
136a25f0a04SGreg Roach                    }
137a25f0a04SGreg Roach                }
138a25f0a04SGreg Roach            }
13954ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
140a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
141a25f0a04SGreg Roach                preg_match_all('/\n1 (?:' . WT_EVENTS_DEAT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
142a25f0a04SGreg Roach                foreach ($matches as $match) {
143a25f0a04SGreg Roach                    $date = new Date($match[1]);
144a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
145a25f0a04SGreg Roach                        $keep_alive = true;
146a25f0a04SGreg Roach                        break;
147a25f0a04SGreg Roach                    }
148a25f0a04SGreg Roach                }
149a25f0a04SGreg Roach            }
150a25f0a04SGreg Roach            if (!$keep_alive) {
151a25f0a04SGreg Roach                return true;
152a25f0a04SGreg Roach            }
153a25f0a04SGreg Roach        }
154a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
15554ba7dc5SGreg Roach        $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1564b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
15754ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1584b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
159a25f0a04SGreg Roach        }
160a25f0a04SGreg Roach
161a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1624b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
163a25f0a04SGreg Roach    }
164a25f0a04SGreg Roach
165a25f0a04SGreg Roach    /**
166a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
167a25f0a04SGreg Roach     *
168a25f0a04SGreg Roach     * @param Individual $target
169cbc1590aSGreg Roach     * @param int        $distance
170a25f0a04SGreg Roach     *
171cbc1590aSGreg Roach     * @return bool
172a25f0a04SGreg Roach     */
1738f53f488SRico Sonntag    private static function isRelated(Individual $target, $distance): bool
174c1010edaSGreg Roach    {
175a25f0a04SGreg Roach        static $cache = null;
176a25f0a04SGreg Roach
17706ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
178a25f0a04SGreg Roach        if ($user_individual) {
179a25f0a04SGreg Roach            if (!$cache) {
18013abd6f3SGreg Roach                $cache = [
18113abd6f3SGreg Roach                    0 => [$user_individual],
18213abd6f3SGreg Roach                    1 => [],
18313abd6f3SGreg Roach                ];
1844b9ff166SGreg Roach                foreach ($user_individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
185a25f0a04SGreg Roach                    $family = $fact->getTarget();
186a25f0a04SGreg Roach                    if ($family) {
187a25f0a04SGreg Roach                        $cache[1][] = $family;
188a25f0a04SGreg Roach                    }
189a25f0a04SGreg Roach                }
190a25f0a04SGreg Roach            }
191a25f0a04SGreg Roach        } else {
192a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
193a25f0a04SGreg Roach            return true;
194a25f0a04SGreg Roach        }
195a25f0a04SGreg Roach
196a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
197a25f0a04SGreg Roach        $distance *= 2;
198a25f0a04SGreg Roach
199a25f0a04SGreg Roach        // Consider each path length in turn
200a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
201a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
202a25f0a04SGreg Roach                // We have already calculated all records with this length
203a25f0a04SGreg Roach                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
204a25f0a04SGreg Roach                    return true;
205a25f0a04SGreg Roach                }
206a25f0a04SGreg Roach            } else {
207a25f0a04SGreg Roach                // Need to calculate these paths
20813abd6f3SGreg Roach                $cache[$n] = [];
209a25f0a04SGreg Roach                if ($n % 2 == 0) {
210a25f0a04SGreg Roach                    // Add FAM->INDI links
211a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2124b9ff166SGreg Roach                        foreach ($family->getFacts('HUSB|WIFE|CHIL', false, Auth::PRIV_HIDE) as $fact) {
213a25f0a04SGreg Roach                            $individual = $fact->getTarget();
214a25f0a04SGreg Roach                            // Don’t backtrack
215a25f0a04SGreg Roach                            if ($individual && !in_array($individual, $cache[$n - 2], true)) {
216a25f0a04SGreg Roach                                $cache[$n][] = $individual;
217a25f0a04SGreg Roach                            }
218a25f0a04SGreg Roach                        }
219a25f0a04SGreg Roach                    }
220a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
221a25f0a04SGreg Roach                        return true;
222a25f0a04SGreg Roach                    }
223a25f0a04SGreg Roach                } else {
224a25f0a04SGreg Roach                    // Add INDI->FAM links
225a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2264b9ff166SGreg Roach                        foreach ($individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
227a25f0a04SGreg Roach                            $family = $fact->getTarget();
228a25f0a04SGreg Roach                            // Don’t backtrack
229a25f0a04SGreg Roach                            if ($family && !in_array($family, $cache[$n - 2], true)) {
230a25f0a04SGreg Roach                                $cache[$n][] = $family;
231a25f0a04SGreg Roach                            }
232a25f0a04SGreg Roach                        }
233a25f0a04SGreg Roach                    }
234a25f0a04SGreg Roach                }
235a25f0a04SGreg Roach            }
236a25f0a04SGreg Roach        }
237a25f0a04SGreg Roach
238a25f0a04SGreg Roach        return false;
239a25f0a04SGreg Roach    }
240a25f0a04SGreg Roach
24176692c8bSGreg Roach    /**
24276692c8bSGreg Roach     * Generate a private version of this record
24376692c8bSGreg Roach     *
24476692c8bSGreg Roach     * @param int $access_level
24576692c8bSGreg Roach     *
24676692c8bSGreg Roach     * @return string
24776692c8bSGreg Roach     */
2483c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
249c1010edaSGreg Roach    {
250acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
251a25f0a04SGreg Roach
252a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
253518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
254a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
255a25f0a04SGreg Roach            foreach ($this->getFacts('NAME') as $fact) {
256a25f0a04SGreg Roach                $rec .= "\n" . $fact->getGedcom();
257a25f0a04SGreg Roach            }
258a25f0a04SGreg Roach        }
259a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
260a25f0a04SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
261a25f0a04SGreg Roach        foreach ($matches as $match) {
26224ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
263a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
264a25f0a04SGreg Roach                $rec .= $match[0];
265a25f0a04SGreg Roach            }
266a25f0a04SGreg Roach        }
267a25f0a04SGreg Roach        // Don’t privatize sex.
268a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
269a25f0a04SGreg Roach            $rec .= $match[0];
270a25f0a04SGreg Roach        }
271a25f0a04SGreg Roach
272a25f0a04SGreg Roach        return $rec;
273a25f0a04SGreg Roach    }
274a25f0a04SGreg Roach
27576692c8bSGreg Roach    /**
27676692c8bSGreg Roach     * Fetch data from the database
27776692c8bSGreg Roach     *
27876692c8bSGreg Roach     * @param string $xref
27976692c8bSGreg Roach     * @param int    $tree_id
28076692c8bSGreg Roach     *
28176692c8bSGreg Roach     * @return null|string
28276692c8bSGreg Roach     */
28325e95e6eSGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
284c1010edaSGreg Roach    {
28564d9078aSGreg Roach        return Database::prepare(
28664d9078aSGreg Roach            "SELECT i_gedcom FROM `##individuals` WHERE i_id = :xref AND i_file = :tree_id"
28713abd6f3SGreg Roach        )->execute([
28864d9078aSGreg Roach            'xref'    => $xref,
28964d9078aSGreg Roach            'tree_id' => $tree_id,
29013abd6f3SGreg Roach        ])->fetchOne();
291a25f0a04SGreg Roach    }
292a25f0a04SGreg Roach
293a25f0a04SGreg Roach    /**
294a25f0a04SGreg Roach     * Static helper function to sort an array of people by birth date
295a25f0a04SGreg Roach     *
296a25f0a04SGreg Roach     * @param Individual $x
297a25f0a04SGreg Roach     * @param Individual $y
298a25f0a04SGreg Roach     *
299cbc1590aSGreg Roach     * @return int
300a25f0a04SGreg Roach     */
3018f53f488SRico Sonntag    public static function compareBirthDate(Individual $x, Individual $y): int
302c1010edaSGreg Roach    {
303f5b60decSGreg Roach        return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
304a25f0a04SGreg Roach    }
305a25f0a04SGreg Roach
306a25f0a04SGreg Roach    /**
307a25f0a04SGreg Roach     * Static helper function to sort an array of people by death date
308a25f0a04SGreg Roach     *
309a25f0a04SGreg Roach     * @param Individual $x
310a25f0a04SGreg Roach     * @param Individual $y
311a25f0a04SGreg Roach     *
312cbc1590aSGreg Roach     * @return int
313a25f0a04SGreg Roach     */
3148f53f488SRico Sonntag    public static function compareDeathDate(Individual $x, Individual $y): int
315c1010edaSGreg Roach    {
316f5b60decSGreg Roach        return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
317a25f0a04SGreg Roach    }
318a25f0a04SGreg Roach
319a25f0a04SGreg Roach    /**
320a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
321a25f0a04SGreg Roach     * If not known to be dead, then assume living.
322a25f0a04SGreg Roach     *
323cbc1590aSGreg Roach     * @return bool
324a25f0a04SGreg Roach     */
3258f53f488SRico Sonntag    public function isDead(): bool
326c1010edaSGreg Roach    {
327c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
328a25f0a04SGreg Roach
329a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
330a25f0a04SGreg Roach        if (preg_match('/\n1 (?:' . WT_EVENTS_DEAT . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
331a25f0a04SGreg Roach            return true;
332a25f0a04SGreg Roach        }
333a25f0a04SGreg Roach
334a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
335a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
336a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
337a25f0a04SGreg Roach                $date = new Date($date_match);
338f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
339a25f0a04SGreg Roach                    return true;
340a25f0a04SGreg Roach                }
341a25f0a04SGreg Roach            }
342a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
343a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
344a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
345a25f0a04SGreg Roach                return false;
346a25f0a04SGreg Roach            }
347a25f0a04SGreg Roach        }
348a25f0a04SGreg Roach
349a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
350a25f0a04SGreg Roach
351a25f0a04SGreg Roach        // Check parents (birth and adopted)
3524b9ff166SGreg Roach        foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3534b9ff166SGreg Roach            foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
354a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
355a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
356a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
357a25f0a04SGreg Roach                    $date = new Date($date_match);
358f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
359a25f0a04SGreg Roach                        return true;
360a25f0a04SGreg Roach                    }
361a25f0a04SGreg Roach                }
362a25f0a04SGreg Roach            }
363a25f0a04SGreg Roach        }
364a25f0a04SGreg Roach
365a25f0a04SGreg Roach        // Check spouses
3664b9ff166SGreg Roach        foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
367a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
368a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
369a25f0a04SGreg Roach                $date = new Date($date_match);
370a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
371f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
372a25f0a04SGreg Roach                    return true;
373a25f0a04SGreg Roach                }
374a25f0a04SGreg Roach            }
375a25f0a04SGreg Roach            // Check spouse dates
37613e3123bSGreg Roach            $spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
377a25f0a04SGreg Roach            if ($spouse) {
378a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
379a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
380a25f0a04SGreg Roach                    $date = new Date($date_match);
381a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
382f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
383a25f0a04SGreg Roach                        return true;
384a25f0a04SGreg Roach                    }
385a25f0a04SGreg Roach                }
386a25f0a04SGreg Roach            }
387a25f0a04SGreg Roach            // Check child dates
3884b9ff166SGreg Roach            foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
389a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
390a25f0a04SGreg Roach                // Assume children born after age of 15
391a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
392a25f0a04SGreg Roach                    $date = new Date($date_match);
393f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
394a25f0a04SGreg Roach                        return true;
395a25f0a04SGreg Roach                    }
396a25f0a04SGreg Roach                }
397a25f0a04SGreg Roach                // Check grandchildren
3984b9ff166SGreg Roach                foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
3994b9ff166SGreg Roach                    foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
400a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
401a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
402a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
403a25f0a04SGreg Roach                            $date = new Date($date_match);
404f5b60decSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
405a25f0a04SGreg Roach                                return true;
406a25f0a04SGreg Roach                            }
407a25f0a04SGreg Roach                        }
408a25f0a04SGreg Roach                    }
409a25f0a04SGreg Roach                }
410a25f0a04SGreg Roach            }
411a25f0a04SGreg Roach        }
412a25f0a04SGreg Roach
413a25f0a04SGreg Roach        return false;
414a25f0a04SGreg Roach    }
415a25f0a04SGreg Roach
416a25f0a04SGreg Roach    /**
417a25f0a04SGreg Roach     * Find the highlighted media object for an individual
418a25f0a04SGreg Roach     *
4194a9f750fSGreg Roach     * @return null|MediaFile
420a25f0a04SGreg Roach     */
421c1010edaSGreg Roach    public function findHighlightedMediaFile()
422c1010edaSGreg Roach    {
423c4fdfd2dSGreg Roach        foreach ($this->getFacts('OBJE') as $fact) {
424c4fdfd2dSGreg Roach            $media = $fact->getTarget();
425a213a63cSGreg Roach            if ($media instanceof Media) {
4264a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
427a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4284a9f750fSGreg Roach                        return $media_file;
4294a9f750fSGreg Roach                    }
4304a9f750fSGreg Roach                }
431a25f0a04SGreg Roach            }
432a25f0a04SGreg Roach        }
433a25f0a04SGreg Roach
434a25f0a04SGreg Roach        return null;
435a25f0a04SGreg Roach    }
436a25f0a04SGreg Roach
437a25f0a04SGreg Roach    /**
438a25f0a04SGreg Roach     * Display the prefered image for this individual.
439a25f0a04SGreg Roach     * Use an icon if no image is available.
440a25f0a04SGreg Roach     *
441dce07401SGreg Roach     * @param int      $width      Pixels
442dce07401SGreg Roach     * @param int      $height     Pixels
443dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
444dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
445dce07401SGreg Roach     *
446a25f0a04SGreg Roach     * @return string
447a25f0a04SGreg Roach     */
4488f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes): string
449c1010edaSGreg Roach    {
4504a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4514a9f750fSGreg Roach
4524a9f750fSGreg Roach        if ($media_file !== null) {
4534a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
454a25f0a04SGreg Roach        }
4554a9f750fSGreg Roach
4564a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
4574a9f750fSGreg Roach            return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
4584a9f750fSGreg Roach        }
4594a9f750fSGreg Roach
4604a9f750fSGreg Roach        return '';
461a25f0a04SGreg Roach    }
462a25f0a04SGreg Roach
463a25f0a04SGreg Roach    /**
464a25f0a04SGreg Roach     * Get the date of birth
465a25f0a04SGreg Roach     *
466a25f0a04SGreg Roach     * @return Date
467a25f0a04SGreg Roach     */
4688f53f488SRico Sonntag    public function getBirthDate(): Date
469c1010edaSGreg Roach    {
470a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
471a25f0a04SGreg Roach            if ($date->isOK()) {
472a25f0a04SGreg Roach                return $date;
473a25f0a04SGreg Roach            }
474a25f0a04SGreg Roach        }
475a25f0a04SGreg Roach
476a25f0a04SGreg Roach        return new Date('');
477a25f0a04SGreg Roach    }
478a25f0a04SGreg Roach
479a25f0a04SGreg Roach    /**
480a25f0a04SGreg Roach     * Get the place of birth
481a25f0a04SGreg Roach     *
48216d0b7f7SRico Sonntag     * @return Place
483a25f0a04SGreg Roach     */
4848f53f488SRico Sonntag    public function getBirthPlace(): Place
485c1010edaSGreg Roach    {
486a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
487a25f0a04SGreg Roach            if ($place) {
488a25f0a04SGreg Roach                return $place;
489a25f0a04SGreg Roach            }
490a25f0a04SGreg Roach        }
491a25f0a04SGreg Roach
492b20ddbf9SGreg Roach        return new Place('', $this->tree);
493a25f0a04SGreg Roach    }
494a25f0a04SGreg Roach
495a25f0a04SGreg Roach    /**
496a25f0a04SGreg Roach     * Get the year of birth
497a25f0a04SGreg Roach     *
498a25f0a04SGreg Roach     * @return string the year of birth
499a25f0a04SGreg Roach     */
5008f53f488SRico Sonntag    public function getBirthYear(): string
501c1010edaSGreg Roach    {
502f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
503a25f0a04SGreg Roach    }
504a25f0a04SGreg Roach
505a25f0a04SGreg Roach    /**
506a25f0a04SGreg Roach     * Get the date of death
507a25f0a04SGreg Roach     *
508a25f0a04SGreg Roach     * @return Date
509a25f0a04SGreg Roach     */
5108f53f488SRico Sonntag    public function getDeathDate(): Date
511c1010edaSGreg Roach    {
512a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
513a25f0a04SGreg Roach            if ($date->isOK()) {
514a25f0a04SGreg Roach                return $date;
515a25f0a04SGreg Roach            }
516a25f0a04SGreg Roach        }
517a25f0a04SGreg Roach
518a25f0a04SGreg Roach        return new Date('');
519a25f0a04SGreg Roach    }
520a25f0a04SGreg Roach
521a25f0a04SGreg Roach    /**
522a25f0a04SGreg Roach     * Get the place of death
523a25f0a04SGreg Roach     *
52416d0b7f7SRico Sonntag     * @return Place
525a25f0a04SGreg Roach     */
5268f53f488SRico Sonntag    public function getDeathPlace(): Place
527c1010edaSGreg Roach    {
528a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
529a25f0a04SGreg Roach            if ($place) {
530a25f0a04SGreg Roach                return $place;
531a25f0a04SGreg Roach            }
532a25f0a04SGreg Roach        }
533a25f0a04SGreg Roach
534b20ddbf9SGreg Roach        return new Place('', $this->tree);
535a25f0a04SGreg Roach    }
536a25f0a04SGreg Roach
537a25f0a04SGreg Roach    /**
538a25f0a04SGreg Roach     * get the death year
539a25f0a04SGreg Roach     *
540a25f0a04SGreg Roach     * @return string the year of death
541a25f0a04SGreg Roach     */
5428f53f488SRico Sonntag    public function getDeathYear(): string
543c1010edaSGreg Roach    {
544f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
545a25f0a04SGreg Roach    }
546a25f0a04SGreg Roach
547a25f0a04SGreg Roach    /**
548a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
54915d603e7SGreg Roach     * Provide the place and full date using a tooltip.
550a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
551a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
552a25f0a04SGreg Roach     *
553a25f0a04SGreg Roach     * @return string
554a25f0a04SGreg Roach     */
5558f53f488SRico Sonntag    public function getLifeSpan(): string
556c1010edaSGreg Roach    {
55715d603e7SGreg Roach        // Just the first part of the place name
5581e9c2c49SGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
5591e9c2c49SGreg Roach        $death_place = strip_tags($this->getDeathPlace()->getShortName());
56015d603e7SGreg Roach        // Remove markup from dates
56115d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56215d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56315d603e7SGreg Roach
564c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
565bbb76c12SGreg Roach        return
566c1010edaSGreg Roach            I18N::translate(
567a25f0a04SGreg Roach                '%1$s–%2$s',
568d53324c9SGreg Roach                '<span title="' . e($birth_place) . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
569d53324c9SGreg Roach                '<span title="' . e($death_place) . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
570a25f0a04SGreg Roach            );
571a25f0a04SGreg Roach    }
572a25f0a04SGreg Roach
573a25f0a04SGreg Roach    /**
574a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
575a25f0a04SGreg Roach     *
576a25f0a04SGreg Roach     * @return Date[]
577a25f0a04SGreg Roach     */
5788f53f488SRico Sonntag    public function getAllBirthDates(): array
579c1010edaSGreg Roach    {
580a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
581a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
582a25f0a04SGreg Roach            if ($tmp) {
583a25f0a04SGreg Roach                return $tmp;
584a25f0a04SGreg Roach            }
585a25f0a04SGreg Roach        }
586a25f0a04SGreg Roach
58713abd6f3SGreg Roach        return [];
588a25f0a04SGreg Roach    }
589a25f0a04SGreg Roach
590a25f0a04SGreg Roach    /**
591a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
592a25f0a04SGreg Roach     *
5934080d558SGreg Roach     * @return Place[]
594a25f0a04SGreg Roach     */
5958f53f488SRico Sonntag    public function getAllBirthPlaces(): array
596c1010edaSGreg Roach    {
597a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
5984080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
5994080d558SGreg Roach            if (!empty($places)) {
6004080d558SGreg Roach                return $places;
601a25f0a04SGreg Roach            }
602a25f0a04SGreg Roach        }
603a25f0a04SGreg Roach
60413abd6f3SGreg Roach        return [];
605a25f0a04SGreg Roach    }
606a25f0a04SGreg Roach
607a25f0a04SGreg Roach    /**
608a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
609a25f0a04SGreg Roach     *
610a25f0a04SGreg Roach     * @return Date[]
611a25f0a04SGreg Roach     */
6128f53f488SRico Sonntag    public function getAllDeathDates(): array
613c1010edaSGreg Roach    {
614a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
615a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
616a25f0a04SGreg Roach            if ($tmp) {
617a25f0a04SGreg Roach                return $tmp;
618a25f0a04SGreg Roach            }
619a25f0a04SGreg Roach        }
620a25f0a04SGreg Roach
62113abd6f3SGreg Roach        return [];
622a25f0a04SGreg Roach    }
623a25f0a04SGreg Roach
624a25f0a04SGreg Roach    /**
625a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
626a25f0a04SGreg Roach     *
6274080d558SGreg Roach     * @return Place[]
628a25f0a04SGreg Roach     */
6298f53f488SRico Sonntag    public function getAllDeathPlaces(): array
630c1010edaSGreg Roach    {
631a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
6324080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
6334080d558SGreg Roach            if (!empty($places)) {
6344080d558SGreg Roach                return $places;
635a25f0a04SGreg Roach            }
636a25f0a04SGreg Roach        }
637a25f0a04SGreg Roach
63813abd6f3SGreg Roach        return [];
639a25f0a04SGreg Roach    }
640a25f0a04SGreg Roach
641a25f0a04SGreg Roach    /**
642a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
643a25f0a04SGreg Roach     *
644a25f0a04SGreg Roach     * @return Date
645a25f0a04SGreg Roach     */
6468f53f488SRico Sonntag    public function getEstimatedBirthDate(): Date
647c1010edaSGreg Roach    {
6488f038c36SRico Sonntag        if ($this->estimated_birth_date === null) {
649a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
650a25f0a04SGreg Roach                if ($date->isOK()) {
6514686330aSGreg Roach                    $this->estimated_birth_date = $date;
652a25f0a04SGreg Roach                    break;
653a25f0a04SGreg Roach                }
654a25f0a04SGreg Roach            }
6558f038c36SRico Sonntag            if ($this->estimated_birth_date === null) {
65613abd6f3SGreg Roach                $min = [];
65713abd6f3SGreg Roach                $max = [];
658a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
659f5b60decSGreg Roach                if ($tmp->isOK()) {
660f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
661f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
662a25f0a04SGreg Roach                }
663a25f0a04SGreg Roach                foreach ($this->getChildFamilies() as $family) {
664a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
665f5b60decSGreg Roach                    if ($tmp->isOK()) {
666f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
667f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
668a25f0a04SGreg Roach                    }
669a25f0a04SGreg Roach                    if ($parent = $family->getHusband()) {
670a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
671f5b60decSGreg Roach                        if ($tmp->isOK()) {
672f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
673f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
674a25f0a04SGreg Roach                        }
675a25f0a04SGreg Roach                    }
676a25f0a04SGreg Roach                    if ($parent = $family->getWife()) {
677a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
678f5b60decSGreg Roach                        if ($tmp->isOK()) {
679f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
680f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
681a25f0a04SGreg Roach                        }
682a25f0a04SGreg Roach                    }
683a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
684a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
685f5b60decSGreg Roach                        if ($tmp->isOK()) {
686f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
687f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
688a25f0a04SGreg Roach                        }
689a25f0a04SGreg Roach                    }
690a25f0a04SGreg Roach                }
691a25f0a04SGreg Roach                foreach ($this->getSpouseFamilies() as $family) {
692a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
693f5b60decSGreg Roach                    if ($tmp->isOK()) {
694f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
695f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
696a25f0a04SGreg Roach                    }
697a25f0a04SGreg Roach                    $spouse = $family->getSpouse($this);
698a25f0a04SGreg Roach                    if ($spouse) {
699a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
700f5b60decSGreg Roach                        if ($tmp->isOK()) {
701f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
702f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
703a25f0a04SGreg Roach                        }
704a25f0a04SGreg Roach                    }
705a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
706a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
707f5b60decSGreg Roach                        if ($tmp->isOK()) {
708f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
709f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
710a25f0a04SGreg Roach                        }
711a25f0a04SGreg Roach                    }
712a25f0a04SGreg Roach                }
713a25f0a04SGreg Roach                if ($min && $max) {
71459f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
715a25f0a04SGreg Roach
716a25f0a04SGreg Roach                    list($year) = $gregorian_calendar->jdToYmd((int) ((max($min) + min($max)) / 2));
7174686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
718a25f0a04SGreg Roach                } else {
7194686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
720a25f0a04SGreg Roach                }
721a25f0a04SGreg Roach            }
722a25f0a04SGreg Roach        }
723a25f0a04SGreg Roach
7244686330aSGreg Roach        return $this->estimated_birth_date;
725a25f0a04SGreg Roach    }
726a25f0a04SGreg Roach
727a25f0a04SGreg Roach    /**
728a25f0a04SGreg Roach     * Generate an estimated date of death.
729a25f0a04SGreg Roach     *
730a25f0a04SGreg Roach     * @return Date
731a25f0a04SGreg Roach     */
7328f53f488SRico Sonntag    public function getEstimatedDeathDate(): Date
733c1010edaSGreg Roach    {
7344686330aSGreg Roach        if ($this->estimated_death_date === null) {
735a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
736a25f0a04SGreg Roach                if ($date->isOK()) {
7374686330aSGreg Roach                    $this->estimated_death_date = $date;
738a25f0a04SGreg Roach                    break;
739a25f0a04SGreg Roach                }
740a25f0a04SGreg Roach            }
7414686330aSGreg Roach            if ($this->estimated_death_date === null) {
742f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
743c4b3e5a2SGreg Roach                    $max_alive_age              = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
7444686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
745a25f0a04SGreg Roach                } else {
7464686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
747a25f0a04SGreg Roach                }
748a25f0a04SGreg Roach            }
749a25f0a04SGreg Roach        }
750a25f0a04SGreg Roach
7514686330aSGreg Roach        return $this->estimated_death_date;
752a25f0a04SGreg Roach    }
753a25f0a04SGreg Roach
754a25f0a04SGreg Roach    /**
755a25f0a04SGreg Roach     * Get the sex - M F or U
756a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
757a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
758a25f0a04SGreg Roach     *
759a25f0a04SGreg Roach     * @return string
760a25f0a04SGreg Roach     */
761c1010edaSGreg Roach    public function getSex()
762c1010edaSGreg Roach    {
763a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
764a25f0a04SGreg Roach            return $match[1];
765a25f0a04SGreg Roach        }
766b2ce94c6SRico Sonntag
767b2ce94c6SRico Sonntag        return 'U';
768a25f0a04SGreg Roach    }
769a25f0a04SGreg Roach
770a25f0a04SGreg Roach    /**
771a25f0a04SGreg Roach     * Get the individual’s sex image
772a25f0a04SGreg Roach     *
773a25f0a04SGreg Roach     * @param string $size
774a25f0a04SGreg Roach     *
775a25f0a04SGreg Roach     * @return string
776a25f0a04SGreg Roach     */
7778f53f488SRico Sonntag    public function getSexImage($size = 'small'): string
778c1010edaSGreg Roach    {
779a25f0a04SGreg Roach        return self::sexImage($this->getSex(), $size);
780a25f0a04SGreg Roach    }
781a25f0a04SGreg Roach
782a25f0a04SGreg Roach    /**
783a25f0a04SGreg Roach     * Generate a sex icon/image
784a25f0a04SGreg Roach     *
785a25f0a04SGreg Roach     * @param string $sex
786a25f0a04SGreg Roach     * @param string $size
787a25f0a04SGreg Roach     *
788a25f0a04SGreg Roach     * @return string
789a25f0a04SGreg Roach     */
7908f53f488SRico Sonntag    public static function sexImage($sex, $size = 'small'): string
791c1010edaSGreg Roach    {
792a25f0a04SGreg Roach        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
793a25f0a04SGreg Roach    }
794a25f0a04SGreg Roach
795a25f0a04SGreg Roach    /**
796a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
797a25f0a04SGreg Roach     *
798a25f0a04SGreg Roach     * @return string
799a25f0a04SGreg Roach     */
8008f53f488SRico Sonntag    public function getBoxStyle(): string
801c1010edaSGreg Roach    {
802c1010edaSGreg Roach        $tmp = [
803c1010edaSGreg Roach            'M' => '',
804c1010edaSGreg Roach            'F' => 'F',
805c1010edaSGreg Roach            'U' => 'NN',
806c1010edaSGreg Roach        ];
807a25f0a04SGreg Roach
808a25f0a04SGreg Roach        return 'person_box' . $tmp[$this->getSex()];
809a25f0a04SGreg Roach    }
810a25f0a04SGreg Roach
811a25f0a04SGreg Roach    /**
812a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
813a25f0a04SGreg Roach     *
814cbc1590aSGreg Roach     * @param int|null $access_level
815a25f0a04SGreg Roach     *
816a25f0a04SGreg Roach     * @return Family[]
817a25f0a04SGreg Roach     */
8188f53f488SRico Sonntag    public function getSpouseFamilies($access_level = null): array
819c1010edaSGreg Roach    {
8204b9ff166SGreg Roach        if ($access_level === null) {
8214b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8224b9ff166SGreg Roach        }
8234b9ff166SGreg Roach
824acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
825a25f0a04SGreg Roach
82613abd6f3SGreg Roach        $families = [];
827a25f0a04SGreg Roach        foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
828a25f0a04SGreg Roach            $family = $fact->getTarget();
829a25f0a04SGreg Roach            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
830a25f0a04SGreg Roach                $families[] = $family;
831a25f0a04SGreg Roach            }
832a25f0a04SGreg Roach        }
833a25f0a04SGreg Roach
834a25f0a04SGreg Roach        return $families;
835a25f0a04SGreg Roach    }
836a25f0a04SGreg Roach
837a25f0a04SGreg Roach    /**
838a25f0a04SGreg Roach     * Get the current spouse of this individual.
839a25f0a04SGreg Roach     *
840a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
841a25f0a04SGreg Roach     * in chronological order, and take the last one found.
842a25f0a04SGreg Roach     *
843a25f0a04SGreg Roach     * @return Individual|null
844a25f0a04SGreg Roach     */
845c1010edaSGreg Roach    public function getCurrentSpouse()
846c1010edaSGreg Roach    {
847a25f0a04SGreg Roach        $tmp    = $this->getSpouseFamilies();
848a25f0a04SGreg Roach        $family = end($tmp);
849a25f0a04SGreg Roach        if ($family) {
850a25f0a04SGreg Roach            return $family->getSpouse($this);
851a25f0a04SGreg Roach        }
852b2ce94c6SRico Sonntag
853b2ce94c6SRico Sonntag        return null;
854a25f0a04SGreg Roach    }
855a25f0a04SGreg Roach
856a25f0a04SGreg Roach    /**
857a25f0a04SGreg Roach     * Count the children belonging to this individual.
858a25f0a04SGreg Roach     *
859cbc1590aSGreg Roach     * @return int
860a25f0a04SGreg Roach     */
861c1010edaSGreg Roach    public function getNumberOfChildren()
862c1010edaSGreg Roach    {
863a25f0a04SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
864a25f0a04SGreg Roach            return $match[1];
865b2ce94c6SRico Sonntag        }
866b2ce94c6SRico Sonntag
86713abd6f3SGreg Roach        $children = [];
868a25f0a04SGreg Roach        foreach ($this->getSpouseFamilies() as $fam) {
869a25f0a04SGreg Roach            foreach ($fam->getChildren() as $child) {
870a25f0a04SGreg Roach                $children[$child->getXref()] = true;
871a25f0a04SGreg Roach            }
872a25f0a04SGreg Roach        }
873a25f0a04SGreg Roach
874a25f0a04SGreg Roach        return count($children);
875a25f0a04SGreg Roach    }
876a25f0a04SGreg Roach
877a25f0a04SGreg Roach    /**
878a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
879a25f0a04SGreg Roach     *
880cbc1590aSGreg Roach     * @param int|null $access_level
881a25f0a04SGreg Roach     *
882a25f0a04SGreg Roach     * @return Family[]
883a25f0a04SGreg Roach     */
8848f53f488SRico Sonntag    public function getChildFamilies($access_level = null): array
885c1010edaSGreg Roach    {
8864b9ff166SGreg Roach        if ($access_level === null) {
8874b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8884b9ff166SGreg Roach        }
8894b9ff166SGreg Roach
890acf76a54SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
891a25f0a04SGreg Roach
89213abd6f3SGreg Roach        $families = [];
893a25f0a04SGreg Roach        foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
894a25f0a04SGreg Roach            $family = $fact->getTarget();
895a25f0a04SGreg Roach            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
896a25f0a04SGreg Roach                $families[] = $family;
897a25f0a04SGreg Roach            }
898a25f0a04SGreg Roach        }
899a25f0a04SGreg Roach
900a25f0a04SGreg Roach        return $families;
901a25f0a04SGreg Roach    }
902a25f0a04SGreg Roach
903a25f0a04SGreg Roach    /**
904a25f0a04SGreg Roach     * Get the preferred parents for this individual.
905a25f0a04SGreg Roach     *
906a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
907a25f0a04SGreg Roach     * The preferred family record is:
908a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
909a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
910a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
911a25f0a04SGreg Roach     * (d) the first one found
912a25f0a04SGreg Roach     *
913a25f0a04SGreg Roach     * @return Family|null
914a25f0a04SGreg Roach     */
915c1010edaSGreg Roach    public function getPrimaryChildFamily()
916c1010edaSGreg Roach    {
917a25f0a04SGreg Roach        $families = $this->getChildFamilies();
918a25f0a04SGreg Roach        switch (count($families)) {
919a25f0a04SGreg Roach            case 0:
920a25f0a04SGreg Roach                return null;
921a25f0a04SGreg Roach            case 1:
92215d603e7SGreg Roach                return $families[0];
923a25f0a04SGreg Roach            default:
924a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
925a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
92674e78bfaSJonathan Jaubart                foreach ($families as $fam) {
92774e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
928a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
929a25f0a04SGreg Roach                        return $fam;
930a25f0a04SGreg Roach                    }
931a25f0a04SGreg Roach                }
932a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
93374e78bfaSJonathan Jaubart                foreach ($families as $fam) {
93474e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
935a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
936a25f0a04SGreg Roach                        return $fam;
937a25f0a04SGreg Roach                    }
938a25f0a04SGreg Roach                }
939a25f0a04SGreg Roach                // c) records with no '2 PEDI'
94074e78bfaSJonathan Jaubart                foreach ($families as $fam) {
94174e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
942a25f0a04SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
943a25f0a04SGreg Roach                        return $fam;
944a25f0a04SGreg Roach                    }
945a25f0a04SGreg Roach                }
946a25f0a04SGreg Roach
947a25f0a04SGreg Roach                // d) any record
94815d603e7SGreg Roach                return $families[0];
949a25f0a04SGreg Roach        }
950a25f0a04SGreg Roach    }
951a25f0a04SGreg Roach
952a25f0a04SGreg Roach    /**
953a25f0a04SGreg Roach     * Get a list of step-parent families.
954a25f0a04SGreg Roach     *
955a25f0a04SGreg Roach     * @return Family[]
956a25f0a04SGreg Roach     */
9578f53f488SRico Sonntag    public function getChildStepFamilies(): array
958c1010edaSGreg Roach    {
95913abd6f3SGreg Roach        $step_families = [];
960a25f0a04SGreg Roach        $families      = $this->getChildFamilies();
961a25f0a04SGreg Roach        foreach ($families as $family) {
962a25f0a04SGreg Roach            $father = $family->getHusband();
963a25f0a04SGreg Roach            if ($father) {
964a25f0a04SGreg Roach                foreach ($father->getSpouseFamilies() as $step_family) {
965a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
966a25f0a04SGreg Roach                        $step_families[] = $step_family;
967a25f0a04SGreg Roach                    }
968a25f0a04SGreg Roach                }
969a25f0a04SGreg Roach            }
970a25f0a04SGreg Roach            $mother = $family->getWife();
971a25f0a04SGreg Roach            if ($mother) {
972a25f0a04SGreg Roach                foreach ($mother->getSpouseFamilies() as $step_family) {
973a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
974a25f0a04SGreg Roach                        $step_families[] = $step_family;
975a25f0a04SGreg Roach                    }
976a25f0a04SGreg Roach                }
977a25f0a04SGreg Roach            }
978a25f0a04SGreg Roach        }
979a25f0a04SGreg Roach
980a25f0a04SGreg Roach        return $step_families;
981a25f0a04SGreg Roach    }
982a25f0a04SGreg Roach
983a25f0a04SGreg Roach    /**
984a25f0a04SGreg Roach     * Get a list of step-parent families.
985a25f0a04SGreg Roach     *
986a25f0a04SGreg Roach     * @return Family[]
987a25f0a04SGreg Roach     */
9888f53f488SRico Sonntag    public function getSpouseStepFamilies(): array
989c1010edaSGreg Roach    {
99013abd6f3SGreg Roach        $step_families = [];
991a25f0a04SGreg Roach        $families      = $this->getSpouseFamilies();
992a25f0a04SGreg Roach        foreach ($families as $family) {
993a25f0a04SGreg Roach            $spouse = $family->getSpouse($this);
994a25f0a04SGreg Roach            if ($spouse) {
995a25f0a04SGreg Roach                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
996a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
997a25f0a04SGreg Roach                        $step_families[] = $step_family;
998a25f0a04SGreg Roach                    }
999a25f0a04SGreg Roach                }
1000a25f0a04SGreg Roach            }
1001a25f0a04SGreg Roach        }
1002a25f0a04SGreg Roach
1003a25f0a04SGreg Roach        return $step_families;
1004a25f0a04SGreg Roach    }
1005a25f0a04SGreg Roach
1006a25f0a04SGreg Roach    /**
1007a25f0a04SGreg Roach     * A label for a parental family group
1008a25f0a04SGreg Roach     *
1009a25f0a04SGreg Roach     * @param Family $family
1010a25f0a04SGreg Roach     *
1011a25f0a04SGreg Roach     * @return string
1012a25f0a04SGreg Roach     */
1013c1010edaSGreg Roach    public function getChildFamilyLabel(Family $family)
1014c1010edaSGreg Roach    {
1015a25f0a04SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->getXref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
1016a25f0a04SGreg Roach            // A specified pedigree
1017764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1018b2ce94c6SRico Sonntag        }
1019b2ce94c6SRico Sonntag
1020a25f0a04SGreg Roach        // Default (birth) pedigree
1021764a01d9SGreg Roach        return GedcomCodePedi::getChildFamilyLabel('');
1022a25f0a04SGreg Roach    }
1023a25f0a04SGreg Roach
1024a25f0a04SGreg Roach    /**
1025a25f0a04SGreg Roach     * Create a label for a step family
1026a25f0a04SGreg Roach     *
1027a25f0a04SGreg Roach     * @param Family $step_family
1028a25f0a04SGreg Roach     *
1029a25f0a04SGreg Roach     * @return string
1030a25f0a04SGreg Roach     */
10318f53f488SRico Sonntag    public function getStepFamilyLabel(Family $step_family): string
1032c1010edaSGreg Roach    {
1033a25f0a04SGreg Roach        foreach ($this->getChildFamilies() as $family) {
1034a25f0a04SGreg Roach            if ($family !== $step_family) {
1035a25f0a04SGreg Roach                // Must be a step-family
1036a25f0a04SGreg Roach                foreach ($family->getSpouses() as $parent) {
1037a25f0a04SGreg Roach                    foreach ($step_family->getSpouses() as $step_parent) {
1038a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1039a25f0a04SGreg Roach                            // One common parent - must be a step family
1040a25f0a04SGreg Roach                            if ($parent->getSex() == 'M') {
1041a25f0a04SGreg Roach                                // Father’s family with someone else
1042a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1043a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1044bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1045b2ce94c6SRico Sonntag                                }
1046b2ce94c6SRico Sonntag
1047a25f0a04SGreg Roach                                /* I18N: A step-family. */
1048bbb76c12SGreg Roach                                return I18N::translate('Father’s family with an unknown individual');
1049a25f0a04SGreg Roach                            }
1050b2ce94c6SRico Sonntag
1051a25f0a04SGreg Roach                            // Mother’s family with someone else
1052a25f0a04SGreg Roach                            if ($step_family->getSpouse($step_parent)) {
1053a25f0a04SGreg Roach                                /* I18N: A step-family. %s is an individual’s name */
1054bbb76c12SGreg Roach                                return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1055b2ce94c6SRico Sonntag                            }
1056b2ce94c6SRico Sonntag
1057a25f0a04SGreg Roach                            /* I18N: A step-family. */
1058bbb76c12SGreg Roach                            return I18N::translate('Mother’s family with an unknown individual');
1059a25f0a04SGreg Roach                        }
1060a25f0a04SGreg Roach                    }
1061a25f0a04SGreg Roach                }
1062a25f0a04SGreg Roach            }
1063a25f0a04SGreg Roach        }
1064a25f0a04SGreg Roach
1065a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1066a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1067a25f0a04SGreg Roach    }
1068a25f0a04SGreg Roach
1069225e381fSGreg 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.';
1254a25f0a04SGreg Roach            $pos  = 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
1276*18d7a90dSGreg 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',
13228f53f488SRico Sonntag            $this->getFacts(
13238f53f488SRico Sonntag                '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
1340841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1341841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1342a25f0a04SGreg Roach    }
1343a25f0a04SGreg Roach}
1344