xref: /webtrees/app/Individual.php (revision 59f2f229057fe08ec1b09bd435699190641eed6a)
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     */
51c1010edaSGreg Roach    public static function getInstance($xref, Tree $tree, $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        } else {
58e71ef9d2SGreg Roach            return null;
59e71ef9d2SGreg Roach        }
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
68395f0fe0SGreg Roach     */
69c1010edaSGreg Roach    public static function load(Tree $tree, array $xrefs)
70c1010edaSGreg Roach    {
7113abd6f3SGreg Roach        $args         = [
72395f0fe0SGreg Roach            'tree_id' => $tree->getTreeId(),
7313abd6f3SGreg Roach        ];
7413abd6f3SGreg Roach        $placeholders = [];
75395f0fe0SGreg Roach
76395f0fe0SGreg Roach        foreach (array_unique($xrefs) as $n => $xref) {
77395f0fe0SGreg Roach            if (!isset(self::$gedcom_record_cache[$tree->getTreeId()][$xref])) {
78f7247c73SGreg Roach                $placeholders[] = ':x' . $n;
79395f0fe0SGreg Roach                $args['x' . $n] = $xref;
80395f0fe0SGreg Roach            }
81395f0fe0SGreg Roach        }
82395f0fe0SGreg Roach
83f7247c73SGreg Roach        if (!empty($placeholders)) {
84395f0fe0SGreg Roach            $rows = Database::prepare(
85395f0fe0SGreg Roach                "SELECT i_id AS xref, i_gedcom AS gedcom" .
86395f0fe0SGreg Roach                " FROM `##individuals`" .
87f7247c73SGreg Roach                " WHERE i_file = :tree_id AND i_id IN (" . implode(',', $placeholders) . ")"
88395f0fe0SGreg Roach            )->execute(
89395f0fe0SGreg Roach                $args
90395f0fe0SGreg Roach            )->fetchAll();
91395f0fe0SGreg Roach
92395f0fe0SGreg Roach            foreach ($rows as $row) {
93395f0fe0SGreg Roach                self::getInstance($row->xref, $tree, $row->gedcom);
94395f0fe0SGreg Roach            }
95395f0fe0SGreg Roach        }
96395f0fe0SGreg Roach    }
97395f0fe0SGreg Roach
98395f0fe0SGreg Roach    /**
99a25f0a04SGreg Roach     * Can the name of this record be shown?
100a25f0a04SGreg Roach     *
10176692c8bSGreg Roach     * @param int|null $access_level
10276692c8bSGreg Roach     *
10376692c8bSGreg Roach     * @return bool
104a25f0a04SGreg Roach     */
105c1010edaSGreg Roach    public function canShowName($access_level = null)
106c1010edaSGreg Roach    {
1074b9ff166SGreg Roach        if ($access_level === null) {
1084b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
1094b9ff166SGreg Roach        }
1104b9ff166SGreg Roach
111518bbdc1SGreg Roach        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
112a25f0a04SGreg Roach    }
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach    /**
11576692c8bSGreg Roach     * Can this individual be shown?
116a25f0a04SGreg Roach     *
11776692c8bSGreg Roach     * @param int $access_level
11876692c8bSGreg Roach     *
11976692c8bSGreg Roach     * @return bool
120a25f0a04SGreg Roach     */
121c1010edaSGreg Roach    protected function canShowByType($access_level)
122c1010edaSGreg Roach    {
123a25f0a04SGreg Roach        // Dead people...
124518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
125a25f0a04SGreg Roach            $keep_alive             = false;
12654ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_BIRTH = (int)$this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
127a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_BIRTH) {
128a25f0a04SGreg Roach                preg_match_all('/\n1 (?:' . WT_EVENTS_BIRT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
129a25f0a04SGreg Roach                foreach ($matches as $match) {
130a25f0a04SGreg Roach                    $date = new Date($match[1]);
131a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
132a25f0a04SGreg Roach                        $keep_alive = true;
133a25f0a04SGreg Roach                        break;
134a25f0a04SGreg Roach                    }
135a25f0a04SGreg Roach                }
136a25f0a04SGreg Roach            }
13754ba7dc5SGreg Roach            $KEEP_ALIVE_YEARS_DEATH = (int)$this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
138a25f0a04SGreg Roach            if ($KEEP_ALIVE_YEARS_DEATH) {
139a25f0a04SGreg Roach                preg_match_all('/\n1 (?:' . WT_EVENTS_DEAT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
140a25f0a04SGreg Roach                foreach ($matches as $match) {
141a25f0a04SGreg Roach                    $date = new Date($match[1]);
142a25f0a04SGreg Roach                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
143a25f0a04SGreg Roach                        $keep_alive = true;
144a25f0a04SGreg Roach                        break;
145a25f0a04SGreg Roach                    }
146a25f0a04SGreg Roach                }
147a25f0a04SGreg Roach            }
148a25f0a04SGreg Roach            if (!$keep_alive) {
149a25f0a04SGreg Roach                return true;
150a25f0a04SGreg Roach            }
151a25f0a04SGreg Roach        }
152a25f0a04SGreg Roach        // Consider relationship privacy (unless an admin is applying download restrictions)
15354ba7dc5SGreg Roach        $user_path_length = (int)$this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1544b9ff166SGreg Roach        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
15554ba7dc5SGreg Roach        if ($gedcomid !== '' && $user_path_length > 0) {
1564b9ff166SGreg Roach            return self::isRelated($this, $user_path_length);
157a25f0a04SGreg Roach        }
158a25f0a04SGreg Roach
159a25f0a04SGreg Roach        // No restriction found - show living people to members only:
1604b9ff166SGreg Roach        return Auth::PRIV_USER >= $access_level;
161a25f0a04SGreg Roach    }
162a25f0a04SGreg Roach
163a25f0a04SGreg Roach    /**
164a25f0a04SGreg Roach     * For relationship privacy calculations - is this individual a close relative?
165a25f0a04SGreg Roach     *
166a25f0a04SGreg Roach     * @param Individual $target
167cbc1590aSGreg Roach     * @param int        $distance
168a25f0a04SGreg Roach     *
169cbc1590aSGreg Roach     * @return bool
170a25f0a04SGreg Roach     */
171c1010edaSGreg Roach    private static function isRelated(Individual $target, $distance)
172c1010edaSGreg Roach    {
173a25f0a04SGreg Roach        static $cache = null;
174a25f0a04SGreg Roach
17506ef8e02SGreg Roach        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
176a25f0a04SGreg Roach        if ($user_individual) {
177a25f0a04SGreg Roach            if (!$cache) {
17813abd6f3SGreg Roach                $cache = [
17913abd6f3SGreg Roach                    0 => [$user_individual],
18013abd6f3SGreg Roach                    1 => [],
18113abd6f3SGreg Roach                ];
1824b9ff166SGreg Roach                foreach ($user_individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
183a25f0a04SGreg Roach                    $family = $fact->getTarget();
184a25f0a04SGreg Roach                    if ($family) {
185a25f0a04SGreg Roach                        $cache[1][] = $family;
186a25f0a04SGreg Roach                    }
187a25f0a04SGreg Roach                }
188a25f0a04SGreg Roach            }
189a25f0a04SGreg Roach        } else {
190a25f0a04SGreg Roach            // No individual linked to this account? Cannot use relationship privacy.
191a25f0a04SGreg Roach            return true;
192a25f0a04SGreg Roach        }
193a25f0a04SGreg Roach
194a25f0a04SGreg Roach        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
195a25f0a04SGreg Roach        $distance *= 2;
196a25f0a04SGreg Roach
197a25f0a04SGreg Roach        // Consider each path length in turn
198a25f0a04SGreg Roach        for ($n = 0; $n <= $distance; ++$n) {
199a25f0a04SGreg Roach            if (array_key_exists($n, $cache)) {
200a25f0a04SGreg Roach                // We have already calculated all records with this length
201a25f0a04SGreg Roach                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
202a25f0a04SGreg Roach                    return true;
203a25f0a04SGreg Roach                }
204a25f0a04SGreg Roach            } else {
205a25f0a04SGreg Roach                // Need to calculate these paths
20613abd6f3SGreg Roach                $cache[$n] = [];
207a25f0a04SGreg Roach                if ($n % 2 == 0) {
208a25f0a04SGreg Roach                    // Add FAM->INDI links
209a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $family) {
2104b9ff166SGreg Roach                        foreach ($family->getFacts('HUSB|WIFE|CHIL', false, Auth::PRIV_HIDE) as $fact) {
211a25f0a04SGreg Roach                            $individual = $fact->getTarget();
212a25f0a04SGreg Roach                            // Don’t backtrack
213a25f0a04SGreg Roach                            if ($individual && !in_array($individual, $cache[$n - 2], true)) {
214a25f0a04SGreg Roach                                $cache[$n][] = $individual;
215a25f0a04SGreg Roach                            }
216a25f0a04SGreg Roach                        }
217a25f0a04SGreg Roach                    }
218a25f0a04SGreg Roach                    if (in_array($target, $cache[$n], true)) {
219a25f0a04SGreg Roach                        return true;
220a25f0a04SGreg Roach                    }
221a25f0a04SGreg Roach                } else {
222a25f0a04SGreg Roach                    // Add INDI->FAM links
223a25f0a04SGreg Roach                    foreach ($cache[$n - 1] as $individual) {
2244b9ff166SGreg Roach                        foreach ($individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
225a25f0a04SGreg Roach                            $family = $fact->getTarget();
226a25f0a04SGreg Roach                            // Don’t backtrack
227a25f0a04SGreg Roach                            if ($family && !in_array($family, $cache[$n - 2], true)) {
228a25f0a04SGreg Roach                                $cache[$n][] = $family;
229a25f0a04SGreg Roach                            }
230a25f0a04SGreg Roach                        }
231a25f0a04SGreg Roach                    }
232a25f0a04SGreg Roach                }
233a25f0a04SGreg Roach            }
234a25f0a04SGreg Roach        }
235a25f0a04SGreg Roach
236a25f0a04SGreg Roach        return false;
237a25f0a04SGreg Roach    }
238a25f0a04SGreg Roach
23976692c8bSGreg Roach    /**
24076692c8bSGreg Roach     * Generate a private version of this record
24176692c8bSGreg Roach     *
24276692c8bSGreg Roach     * @param int $access_level
24376692c8bSGreg Roach     *
24476692c8bSGreg Roach     * @return string
24576692c8bSGreg Roach     */
246c1010edaSGreg Roach    protected function createPrivateGedcomRecord($access_level)
247c1010edaSGreg Roach    {
248518bbdc1SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
249a25f0a04SGreg Roach
250a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ INDI';
251518bbdc1SGreg Roach        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
252a25f0a04SGreg Roach            // Show all the NAME tags, including subtags
253a25f0a04SGreg Roach            foreach ($this->getFacts('NAME') as $fact) {
254a25f0a04SGreg Roach                $rec .= "\n" . $fact->getGedcom();
255a25f0a04SGreg Roach            }
256a25f0a04SGreg Roach        }
257a25f0a04SGreg Roach        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
258a25f0a04SGreg Roach        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
259a25f0a04SGreg Roach        foreach ($matches as $match) {
26024ec66ceSGreg Roach            $rela = Family::getInstance($match[1], $this->tree);
261a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
262a25f0a04SGreg Roach                $rec .= $match[0];
263a25f0a04SGreg Roach            }
264a25f0a04SGreg Roach        }
265a25f0a04SGreg Roach        // Don’t privatize sex.
266a25f0a04SGreg Roach        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
267a25f0a04SGreg Roach            $rec .= $match[0];
268a25f0a04SGreg Roach        }
269a25f0a04SGreg Roach
270a25f0a04SGreg Roach        return $rec;
271a25f0a04SGreg Roach    }
272a25f0a04SGreg Roach
27376692c8bSGreg Roach    /**
27476692c8bSGreg Roach     * Fetch data from the database
27576692c8bSGreg Roach     *
27676692c8bSGreg Roach     * @param string $xref
27776692c8bSGreg Roach     * @param int    $tree_id
27876692c8bSGreg Roach     *
27976692c8bSGreg Roach     * @return null|string
28076692c8bSGreg Roach     */
281c1010edaSGreg Roach    protected static function fetchGedcomRecord($xref, $tree_id)
282c1010edaSGreg Roach    {
28364d9078aSGreg Roach        return Database::prepare(
28464d9078aSGreg Roach            "SELECT i_gedcom FROM `##individuals` WHERE i_id = :xref AND i_file = :tree_id"
28513abd6f3SGreg Roach        )->execute([
28664d9078aSGreg Roach            'xref'    => $xref,
28764d9078aSGreg Roach            'tree_id' => $tree_id,
28813abd6f3SGreg Roach        ])->fetchOne();
289a25f0a04SGreg Roach    }
290a25f0a04SGreg Roach
291a25f0a04SGreg Roach    /**
292a25f0a04SGreg Roach     * Static helper function to sort an array of people by birth date
293a25f0a04SGreg Roach     *
294a25f0a04SGreg Roach     * @param Individual $x
295a25f0a04SGreg Roach     * @param Individual $y
296a25f0a04SGreg Roach     *
297cbc1590aSGreg Roach     * @return int
298a25f0a04SGreg Roach     */
299c1010edaSGreg Roach    public static function compareBirthDate(Individual $x, Individual $y)
300c1010edaSGreg Roach    {
301f5b60decSGreg Roach        return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
302a25f0a04SGreg Roach    }
303a25f0a04SGreg Roach
304a25f0a04SGreg Roach    /**
305a25f0a04SGreg Roach     * Static helper function to sort an array of people by death date
306a25f0a04SGreg Roach     *
307a25f0a04SGreg Roach     * @param Individual $x
308a25f0a04SGreg Roach     * @param Individual $y
309a25f0a04SGreg Roach     *
310cbc1590aSGreg Roach     * @return int
311a25f0a04SGreg Roach     */
312c1010edaSGreg Roach    public static function compareDeathDate(Individual $x, Individual $y)
313c1010edaSGreg Roach    {
314f5b60decSGreg Roach        return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
315a25f0a04SGreg Roach    }
316a25f0a04SGreg Roach
317a25f0a04SGreg Roach    /**
318a25f0a04SGreg Roach     * Calculate whether this individual is living or dead.
319a25f0a04SGreg Roach     * If not known to be dead, then assume living.
320a25f0a04SGreg Roach     *
321cbc1590aSGreg Roach     * @return bool
322a25f0a04SGreg Roach     */
323c1010edaSGreg Roach    public function isDead()
324c1010edaSGreg Roach    {
325c4b3e5a2SGreg Roach        $MAX_ALIVE_AGE = (int)$this->tree->getPreference('MAX_ALIVE_AGE');
326a25f0a04SGreg Roach
327a25f0a04SGreg Roach        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
328a25f0a04SGreg Roach        if (preg_match('/\n1 (?:' . WT_EVENTS_DEAT . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
329a25f0a04SGreg Roach            return true;
330a25f0a04SGreg Roach        }
331a25f0a04SGreg Roach
332a25f0a04SGreg Roach        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
333a25f0a04SGreg Roach        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
334a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
335a25f0a04SGreg Roach                $date = new Date($date_match);
336f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
337a25f0a04SGreg Roach                    return true;
338a25f0a04SGreg Roach                }
339a25f0a04SGreg Roach            }
340a25f0a04SGreg Roach            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
341a25f0a04SGreg Roach            // If one of these is a birth, the individual must be alive.
342a25f0a04SGreg Roach            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
343a25f0a04SGreg Roach                return false;
344a25f0a04SGreg Roach            }
345a25f0a04SGreg Roach        }
346a25f0a04SGreg Roach
347a25f0a04SGreg Roach        // If we found no conclusive dates then check the dates of close relatives.
348a25f0a04SGreg Roach
349a25f0a04SGreg Roach        // Check parents (birth and adopted)
3504b9ff166SGreg Roach        foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3514b9ff166SGreg Roach            foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
352a25f0a04SGreg Roach                // Assume parents are no more than 45 years older than their children
353a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
354a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
355a25f0a04SGreg Roach                    $date = new Date($date_match);
356f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
357a25f0a04SGreg Roach                        return true;
358a25f0a04SGreg Roach                    }
359a25f0a04SGreg Roach                }
360a25f0a04SGreg Roach            }
361a25f0a04SGreg Roach        }
362a25f0a04SGreg Roach
363a25f0a04SGreg Roach        // Check spouses
3644b9ff166SGreg Roach        foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
365a25f0a04SGreg Roach            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
366a25f0a04SGreg Roach            foreach ($date_matches[1] as $date_match) {
367a25f0a04SGreg Roach                $date = new Date($date_match);
368a25f0a04SGreg Roach                // Assume marriage occurs after age of 10
369f5b60decSGreg Roach                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
370a25f0a04SGreg Roach                    return true;
371a25f0a04SGreg Roach                }
372a25f0a04SGreg Roach            }
373a25f0a04SGreg Roach            // Check spouse dates
37413e3123bSGreg Roach            $spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
375a25f0a04SGreg Roach            if ($spouse) {
376a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
377a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
378a25f0a04SGreg Roach                    $date = new Date($date_match);
379a25f0a04SGreg Roach                    // Assume max age difference between spouses of 40 years
380f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
381a25f0a04SGreg Roach                        return true;
382a25f0a04SGreg Roach                    }
383a25f0a04SGreg Roach                }
384a25f0a04SGreg Roach            }
385a25f0a04SGreg Roach            // Check child dates
3864b9ff166SGreg Roach            foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
387a25f0a04SGreg Roach                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
388a25f0a04SGreg Roach                // Assume children born after age of 15
389a25f0a04SGreg Roach                foreach ($date_matches[1] as $date_match) {
390a25f0a04SGreg Roach                    $date = new Date($date_match);
391f5b60decSGreg Roach                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
392a25f0a04SGreg Roach                        return true;
393a25f0a04SGreg Roach                    }
394a25f0a04SGreg Roach                }
395a25f0a04SGreg Roach                // Check grandchildren
3964b9ff166SGreg Roach                foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
3974b9ff166SGreg Roach                    foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
398a25f0a04SGreg Roach                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
399a25f0a04SGreg Roach                        // Assume grandchildren born after age of 30
400a25f0a04SGreg Roach                        foreach ($date_matches[1] as $date_match) {
401a25f0a04SGreg Roach                            $date = new Date($date_match);
402f5b60decSGreg Roach                            if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
403a25f0a04SGreg Roach                                return true;
404a25f0a04SGreg Roach                            }
405a25f0a04SGreg Roach                        }
406a25f0a04SGreg Roach                    }
407a25f0a04SGreg Roach                }
408a25f0a04SGreg Roach            }
409a25f0a04SGreg Roach        }
410a25f0a04SGreg Roach
411a25f0a04SGreg Roach        return false;
412a25f0a04SGreg Roach    }
413a25f0a04SGreg Roach
414a25f0a04SGreg Roach    /**
415a25f0a04SGreg Roach     * Find the highlighted media object for an individual
416a25f0a04SGreg Roach     *
4174a9f750fSGreg Roach     * @return null|MediaFile
418a25f0a04SGreg Roach     */
419c1010edaSGreg Roach    public function findHighlightedMediaFile()
420c1010edaSGreg Roach    {
421c4fdfd2dSGreg Roach        foreach ($this->getFacts('OBJE') as $fact) {
422c4fdfd2dSGreg Roach            $media = $fact->getTarget();
423a213a63cSGreg Roach            if ($media instanceof Media) {
4244a9f750fSGreg Roach                foreach ($media->mediaFiles() as $media_file) {
425a213a63cSGreg Roach                    if ($media_file->isImage() && !$media_file->isExternal()) {
4264a9f750fSGreg Roach                        return $media_file;
4274a9f750fSGreg Roach                    }
4284a9f750fSGreg Roach                }
429a25f0a04SGreg Roach            }
430a25f0a04SGreg Roach        }
431a25f0a04SGreg Roach
432a25f0a04SGreg Roach        return null;
433a25f0a04SGreg Roach    }
434a25f0a04SGreg Roach
435a25f0a04SGreg Roach    /**
436a25f0a04SGreg Roach     * Display the prefered image for this individual.
437a25f0a04SGreg Roach     * Use an icon if no image is available.
438a25f0a04SGreg Roach     *
439dce07401SGreg Roach     * @param int      $width      Pixels
440dce07401SGreg Roach     * @param int      $height     Pixels
441dce07401SGreg Roach     * @param string   $fit        "crop" or "contain"
442dce07401SGreg Roach     * @param string[] $attributes Additional HTML attributes
443dce07401SGreg Roach     *
444a25f0a04SGreg Roach     * @return string
445a25f0a04SGreg Roach     */
446c1010edaSGreg Roach    public function displayImage($width, $height, $fit, $attributes)
447c1010edaSGreg Roach    {
4484a9f750fSGreg Roach        $media_file = $this->findHighlightedMediaFile();
4494a9f750fSGreg Roach
4504a9f750fSGreg Roach        if ($media_file !== null) {
4514a9f750fSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
452a25f0a04SGreg Roach        }
4534a9f750fSGreg Roach
4544a9f750fSGreg Roach        if ($this->tree->getPreference('USE_SILHOUETTE')) {
4554a9f750fSGreg Roach            return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
4564a9f750fSGreg Roach        }
4574a9f750fSGreg Roach
4584a9f750fSGreg Roach        return '';
459a25f0a04SGreg Roach    }
460a25f0a04SGreg Roach
461a25f0a04SGreg Roach    /**
462a25f0a04SGreg Roach     * Get the date of birth
463a25f0a04SGreg Roach     *
464a25f0a04SGreg Roach     * @return Date
465a25f0a04SGreg Roach     */
466c1010edaSGreg Roach    public function getBirthDate()
467c1010edaSGreg Roach    {
468a25f0a04SGreg Roach        foreach ($this->getAllBirthDates() as $date) {
469a25f0a04SGreg Roach            if ($date->isOK()) {
470a25f0a04SGreg Roach                return $date;
471a25f0a04SGreg Roach            }
472a25f0a04SGreg Roach        }
473a25f0a04SGreg Roach
474a25f0a04SGreg Roach        return new Date('');
475a25f0a04SGreg Roach    }
476a25f0a04SGreg Roach
477a25f0a04SGreg Roach    /**
478a25f0a04SGreg Roach     * Get the place of birth
479a25f0a04SGreg Roach     *
48016d0b7f7SRico Sonntag     * @return Place
481a25f0a04SGreg Roach     */
482c1010edaSGreg Roach    public function getBirthPlace()
483c1010edaSGreg Roach    {
484a25f0a04SGreg Roach        foreach ($this->getAllBirthPlaces() as $place) {
485a25f0a04SGreg Roach            if ($place) {
486a25f0a04SGreg Roach                return $place;
487a25f0a04SGreg Roach            }
488a25f0a04SGreg Roach        }
489a25f0a04SGreg Roach
490b20ddbf9SGreg Roach        return new Place('', $this->tree);
491a25f0a04SGreg Roach    }
492a25f0a04SGreg Roach
493a25f0a04SGreg Roach    /**
494a25f0a04SGreg Roach     * Get the year of birth
495a25f0a04SGreg Roach     *
496a25f0a04SGreg Roach     * @return string the year of birth
497a25f0a04SGreg Roach     */
498c1010edaSGreg Roach    public function getBirthYear()
499c1010edaSGreg Roach    {
500f5b60decSGreg Roach        return $this->getBirthDate()->minimumDate()->format('%Y');
501a25f0a04SGreg Roach    }
502a25f0a04SGreg Roach
503a25f0a04SGreg Roach    /**
504a25f0a04SGreg Roach     * Get the date of death
505a25f0a04SGreg Roach     *
506a25f0a04SGreg Roach     * @return Date
507a25f0a04SGreg Roach     */
508c1010edaSGreg Roach    public function getDeathDate()
509c1010edaSGreg Roach    {
510a25f0a04SGreg Roach        foreach ($this->getAllDeathDates() as $date) {
511a25f0a04SGreg Roach            if ($date->isOK()) {
512a25f0a04SGreg Roach                return $date;
513a25f0a04SGreg Roach            }
514a25f0a04SGreg Roach        }
515a25f0a04SGreg Roach
516a25f0a04SGreg Roach        return new Date('');
517a25f0a04SGreg Roach    }
518a25f0a04SGreg Roach
519a25f0a04SGreg Roach    /**
520a25f0a04SGreg Roach     * Get the place of death
521a25f0a04SGreg Roach     *
52216d0b7f7SRico Sonntag     * @return Place
523a25f0a04SGreg Roach     */
524c1010edaSGreg Roach    public function getDeathPlace()
525c1010edaSGreg Roach    {
526a25f0a04SGreg Roach        foreach ($this->getAllDeathPlaces() as $place) {
527a25f0a04SGreg Roach            if ($place) {
528a25f0a04SGreg Roach                return $place;
529a25f0a04SGreg Roach            }
530a25f0a04SGreg Roach        }
531a25f0a04SGreg Roach
532b20ddbf9SGreg Roach        return new Place('', $this->tree);
533a25f0a04SGreg Roach    }
534a25f0a04SGreg Roach
535a25f0a04SGreg Roach    /**
536a25f0a04SGreg Roach     * get the death year
537a25f0a04SGreg Roach     *
538a25f0a04SGreg Roach     * @return string the year of death
539a25f0a04SGreg Roach     */
540c1010edaSGreg Roach    public function getDeathYear()
541c1010edaSGreg Roach    {
542f5b60decSGreg Roach        return $this->getDeathDate()->minimumDate()->format('%Y');
543a25f0a04SGreg Roach    }
544a25f0a04SGreg Roach
545a25f0a04SGreg Roach    /**
546a25f0a04SGreg Roach     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
54715d603e7SGreg Roach     * Provide the place and full date using a tooltip.
548a25f0a04SGreg Roach     * For consistent layout in charts, etc., show just a “–” when no dates are known.
549a25f0a04SGreg Roach     * Note that this is a (non-breaking) en-dash, and not a hyphen.
550a25f0a04SGreg Roach     *
551a25f0a04SGreg Roach     * @return string
552a25f0a04SGreg Roach     */
553c1010edaSGreg Roach    public function getLifeSpan()
554c1010edaSGreg Roach    {
55515d603e7SGreg Roach        // Just the first part of the place name
5561e9c2c49SGreg Roach        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
5571e9c2c49SGreg Roach        $death_place = strip_tags($this->getDeathPlace()->getShortName());
55815d603e7SGreg Roach        // Remove markup from dates
55915d603e7SGreg Roach        $birth_date = strip_tags($this->getBirthDate()->display());
56015d603e7SGreg Roach        $death_date = strip_tags($this->getDeathDate()->display());
56115d603e7SGreg Roach
562c1010edaSGreg Roach        /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
563bbb76c12SGreg Roach        return
564c1010edaSGreg Roach            I18N::translate(
565a25f0a04SGreg Roach                '%1$s–%2$s',
566d53324c9SGreg Roach                '<span title="' . e($birth_place) . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
567d53324c9SGreg Roach                '<span title="' . e($death_place) . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
568a25f0a04SGreg Roach            );
569a25f0a04SGreg Roach    }
570a25f0a04SGreg Roach
571a25f0a04SGreg Roach    /**
572a25f0a04SGreg Roach     * Get all the birth dates - for the individual lists.
573a25f0a04SGreg Roach     *
574a25f0a04SGreg Roach     * @return Date[]
575a25f0a04SGreg Roach     */
576c1010edaSGreg Roach    public function getAllBirthDates()
577c1010edaSGreg Roach    {
578a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
579a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
580a25f0a04SGreg Roach            if ($tmp) {
581a25f0a04SGreg Roach                return $tmp;
582a25f0a04SGreg Roach            }
583a25f0a04SGreg Roach        }
584a25f0a04SGreg Roach
58513abd6f3SGreg Roach        return [];
586a25f0a04SGreg Roach    }
587a25f0a04SGreg Roach
588a25f0a04SGreg Roach    /**
589a25f0a04SGreg Roach     * Gat all the birth places - for the individual lists.
590a25f0a04SGreg Roach     *
5914080d558SGreg Roach     * @return Place[]
592a25f0a04SGreg Roach     */
593c1010edaSGreg Roach    public function getAllBirthPlaces()
594c1010edaSGreg Roach    {
595a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
5964080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
5974080d558SGreg Roach            if (!empty($places)) {
5984080d558SGreg Roach                return $places;
599a25f0a04SGreg Roach            }
600a25f0a04SGreg Roach        }
601a25f0a04SGreg Roach
60213abd6f3SGreg Roach        return [];
603a25f0a04SGreg Roach    }
604a25f0a04SGreg Roach
605a25f0a04SGreg Roach    /**
606a25f0a04SGreg Roach     * Get all the death dates - for the individual lists.
607a25f0a04SGreg Roach     *
608a25f0a04SGreg Roach     * @return Date[]
609a25f0a04SGreg Roach     */
610c1010edaSGreg Roach    public function getAllDeathDates()
611c1010edaSGreg Roach    {
612a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
613a25f0a04SGreg Roach            $tmp = $this->getAllEventDates($event);
614a25f0a04SGreg Roach            if ($tmp) {
615a25f0a04SGreg Roach                return $tmp;
616a25f0a04SGreg Roach            }
617a25f0a04SGreg Roach        }
618a25f0a04SGreg Roach
61913abd6f3SGreg Roach        return [];
620a25f0a04SGreg Roach    }
621a25f0a04SGreg Roach
622a25f0a04SGreg Roach    /**
623a25f0a04SGreg Roach     * Get all the death places - for the individual lists.
624a25f0a04SGreg Roach     *
6254080d558SGreg Roach     * @return Place[]
626a25f0a04SGreg Roach     */
627c1010edaSGreg Roach    public function getAllDeathPlaces()
628c1010edaSGreg Roach    {
629a25f0a04SGreg Roach        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
6304080d558SGreg Roach            $places = $this->getAllEventPlaces($event);
6314080d558SGreg Roach            if (!empty($places)) {
6324080d558SGreg Roach                return $places;
633a25f0a04SGreg Roach            }
634a25f0a04SGreg Roach        }
635a25f0a04SGreg Roach
63613abd6f3SGreg Roach        return [];
637a25f0a04SGreg Roach    }
638a25f0a04SGreg Roach
639a25f0a04SGreg Roach    /**
640a25f0a04SGreg Roach     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
641a25f0a04SGreg Roach     *
642a25f0a04SGreg Roach     * @return Date
643a25f0a04SGreg Roach     */
644c1010edaSGreg Roach    public function getEstimatedBirthDate()
645c1010edaSGreg Roach    {
6464686330aSGreg Roach        if (is_null($this->estimated_birth_date)) {
647a25f0a04SGreg Roach            foreach ($this->getAllBirthDates() as $date) {
648a25f0a04SGreg Roach                if ($date->isOK()) {
6494686330aSGreg Roach                    $this->estimated_birth_date = $date;
650a25f0a04SGreg Roach                    break;
651a25f0a04SGreg Roach                }
652a25f0a04SGreg Roach            }
6534686330aSGreg Roach            if (is_null($this->estimated_birth_date)) {
65413abd6f3SGreg Roach                $min = [];
65513abd6f3SGreg Roach                $max = [];
656a25f0a04SGreg Roach                $tmp = $this->getDeathDate();
657f5b60decSGreg Roach                if ($tmp->isOK()) {
658f5b60decSGreg Roach                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
659f5b60decSGreg Roach                    $max[] = $tmp->maximumJulianDay();
660a25f0a04SGreg Roach                }
661a25f0a04SGreg Roach                foreach ($this->getChildFamilies() as $family) {
662a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
663f5b60decSGreg Roach                    if ($tmp->isOK()) {
664f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
665f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
666a25f0a04SGreg Roach                    }
667a25f0a04SGreg Roach                    if ($parent = $family->getHusband()) {
668a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
669f5b60decSGreg Roach                        if ($tmp->isOK()) {
670f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
671f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
672a25f0a04SGreg Roach                        }
673a25f0a04SGreg Roach                    }
674a25f0a04SGreg Roach                    if ($parent = $family->getWife()) {
675a25f0a04SGreg Roach                        $tmp = $parent->getBirthDate();
676f5b60decSGreg Roach                        if ($tmp->isOK()) {
677f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
678f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
679a25f0a04SGreg Roach                        }
680a25f0a04SGreg Roach                    }
681a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
682a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
683f5b60decSGreg Roach                        if ($tmp->isOK()) {
684f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
685f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
686a25f0a04SGreg Roach                        }
687a25f0a04SGreg Roach                    }
688a25f0a04SGreg Roach                }
689a25f0a04SGreg Roach                foreach ($this->getSpouseFamilies() as $family) {
690a25f0a04SGreg Roach                    $tmp = $family->getMarriageDate();
691f5b60decSGreg Roach                    if ($tmp->isOK()) {
692f5b60decSGreg Roach                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
693f5b60decSGreg Roach                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
694a25f0a04SGreg Roach                    }
695a25f0a04SGreg Roach                    $spouse = $family->getSpouse($this);
696a25f0a04SGreg Roach                    if ($spouse) {
697a25f0a04SGreg Roach                        $tmp = $spouse->getBirthDate();
698f5b60decSGreg Roach                        if ($tmp->isOK()) {
699f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
700f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
701a25f0a04SGreg Roach                        }
702a25f0a04SGreg Roach                    }
703a25f0a04SGreg Roach                    foreach ($family->getChildren() as $child) {
704a25f0a04SGreg Roach                        $tmp = $child->getBirthDate();
705f5b60decSGreg Roach                        if ($tmp->isOK()) {
706f5b60decSGreg Roach                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
707f5b60decSGreg Roach                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
708a25f0a04SGreg Roach                        }
709a25f0a04SGreg Roach                    }
710a25f0a04SGreg Roach                }
711a25f0a04SGreg Roach                if ($min && $max) {
712*59f2f229SGreg Roach                    $gregorian_calendar = new GregorianCalendar();
713a25f0a04SGreg Roach
714a25f0a04SGreg Roach                    list($year) = $gregorian_calendar->jdToYmd((int)((max($min) + min($max)) / 2));
7154686330aSGreg Roach                    $this->estimated_birth_date = new Date('EST ' . $year);
716a25f0a04SGreg Roach                } else {
7174686330aSGreg Roach                    $this->estimated_birth_date = new Date(''); // always return a date object
718a25f0a04SGreg Roach                }
719a25f0a04SGreg Roach            }
720a25f0a04SGreg Roach        }
721a25f0a04SGreg Roach
7224686330aSGreg Roach        return $this->estimated_birth_date;
723a25f0a04SGreg Roach    }
724a25f0a04SGreg Roach
725a25f0a04SGreg Roach    /**
726a25f0a04SGreg Roach     * Generate an estimated date of death.
727a25f0a04SGreg Roach     *
728a25f0a04SGreg Roach     * @return Date
729a25f0a04SGreg Roach     */
730c1010edaSGreg Roach    public function getEstimatedDeathDate()
731c1010edaSGreg Roach    {
7324686330aSGreg Roach        if ($this->estimated_death_date === null) {
733a25f0a04SGreg Roach            foreach ($this->getAllDeathDates() as $date) {
734a25f0a04SGreg Roach                if ($date->isOK()) {
7354686330aSGreg Roach                    $this->estimated_death_date = $date;
736a25f0a04SGreg Roach                    break;
737a25f0a04SGreg Roach                }
738a25f0a04SGreg Roach            }
7394686330aSGreg Roach            if ($this->estimated_death_date === null) {
740f5b60decSGreg Roach                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
741c4b3e5a2SGreg Roach                    $max_alive_age              = (int)$this->tree->getPreference('MAX_ALIVE_AGE');
7424686330aSGreg Roach                    $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
743a25f0a04SGreg Roach                } else {
7444686330aSGreg Roach                    $this->estimated_death_date = new Date(''); // always return a date object
745a25f0a04SGreg Roach                }
746a25f0a04SGreg Roach            }
747a25f0a04SGreg Roach        }
748a25f0a04SGreg Roach
7494686330aSGreg Roach        return $this->estimated_death_date;
750a25f0a04SGreg Roach    }
751a25f0a04SGreg Roach
752a25f0a04SGreg Roach    /**
753a25f0a04SGreg Roach     * Get the sex - M F or U
754a25f0a04SGreg Roach     * Use the un-privatised gedcom record. We call this function during
755a25f0a04SGreg Roach     * the privatize-gedcom function, and we are allowed to know this.
756a25f0a04SGreg Roach     *
757a25f0a04SGreg Roach     * @return string
758a25f0a04SGreg Roach     */
759c1010edaSGreg Roach    public function getSex()
760c1010edaSGreg Roach    {
761a25f0a04SGreg Roach        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
762a25f0a04SGreg Roach            return $match[1];
763a25f0a04SGreg Roach        } else {
764a25f0a04SGreg Roach            return 'U';
765a25f0a04SGreg Roach        }
766a25f0a04SGreg Roach    }
767a25f0a04SGreg Roach
768a25f0a04SGreg Roach    /**
769a25f0a04SGreg Roach     * Get the individual’s sex image
770a25f0a04SGreg Roach     *
771a25f0a04SGreg Roach     * @param string $size
772a25f0a04SGreg Roach     *
773a25f0a04SGreg Roach     * @return string
774a25f0a04SGreg Roach     */
775c1010edaSGreg Roach    public function getSexImage($size = 'small')
776c1010edaSGreg Roach    {
777a25f0a04SGreg Roach        return self::sexImage($this->getSex(), $size);
778a25f0a04SGreg Roach    }
779a25f0a04SGreg Roach
780a25f0a04SGreg Roach    /**
781a25f0a04SGreg Roach     * Generate a sex icon/image
782a25f0a04SGreg Roach     *
783a25f0a04SGreg Roach     * @param string $sex
784a25f0a04SGreg Roach     * @param string $size
785a25f0a04SGreg Roach     *
786a25f0a04SGreg Roach     * @return string
787a25f0a04SGreg Roach     */
788c1010edaSGreg Roach    public static function sexImage($sex, $size = 'small')
789c1010edaSGreg Roach    {
790a25f0a04SGreg Roach        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
791a25f0a04SGreg Roach    }
792a25f0a04SGreg Roach
793a25f0a04SGreg Roach    /**
794a25f0a04SGreg Roach     * Generate the CSS class to be used for drawing this individual
795a25f0a04SGreg Roach     *
796a25f0a04SGreg Roach     * @return string
797a25f0a04SGreg Roach     */
798c1010edaSGreg Roach    public function getBoxStyle()
799c1010edaSGreg Roach    {
800c1010edaSGreg Roach        $tmp = [
801c1010edaSGreg Roach            'M' => '',
802c1010edaSGreg Roach            'F' => 'F',
803c1010edaSGreg Roach            'U' => 'NN',
804c1010edaSGreg Roach        ];
805a25f0a04SGreg Roach
806a25f0a04SGreg Roach        return 'person_box' . $tmp[$this->getSex()];
807a25f0a04SGreg Roach    }
808a25f0a04SGreg Roach
809a25f0a04SGreg Roach    /**
810a25f0a04SGreg Roach     * Get a list of this individual’s spouse families
811a25f0a04SGreg Roach     *
812cbc1590aSGreg Roach     * @param int|null $access_level
813a25f0a04SGreg Roach     *
814a25f0a04SGreg Roach     * @return Family[]
815a25f0a04SGreg Roach     */
816c1010edaSGreg Roach    public function getSpouseFamilies($access_level = null)
817c1010edaSGreg Roach    {
8184b9ff166SGreg Roach        if ($access_level === null) {
8194b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8204b9ff166SGreg Roach        }
8214b9ff166SGreg Roach
822518bbdc1SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
823a25f0a04SGreg Roach
82413abd6f3SGreg Roach        $families = [];
825a25f0a04SGreg Roach        foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
826a25f0a04SGreg Roach            $family = $fact->getTarget();
827a25f0a04SGreg Roach            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
828a25f0a04SGreg Roach                $families[] = $family;
829a25f0a04SGreg Roach            }
830a25f0a04SGreg Roach        }
831a25f0a04SGreg Roach
832a25f0a04SGreg Roach        return $families;
833a25f0a04SGreg Roach    }
834a25f0a04SGreg Roach
835a25f0a04SGreg Roach    /**
836a25f0a04SGreg Roach     * Get the current spouse of this individual.
837a25f0a04SGreg Roach     *
838a25f0a04SGreg Roach     * Where an individual has multiple spouses, assume they are stored
839a25f0a04SGreg Roach     * in chronological order, and take the last one found.
840a25f0a04SGreg Roach     *
841a25f0a04SGreg Roach     * @return Individual|null
842a25f0a04SGreg Roach     */
843c1010edaSGreg Roach    public function getCurrentSpouse()
844c1010edaSGreg Roach    {
845a25f0a04SGreg Roach        $tmp    = $this->getSpouseFamilies();
846a25f0a04SGreg Roach        $family = end($tmp);
847a25f0a04SGreg Roach        if ($family) {
848a25f0a04SGreg Roach            return $family->getSpouse($this);
849a25f0a04SGreg Roach        } else {
850a25f0a04SGreg Roach            return null;
851a25f0a04SGreg Roach        }
852a25f0a04SGreg Roach    }
853a25f0a04SGreg Roach
854a25f0a04SGreg Roach    /**
855a25f0a04SGreg Roach     * Count the children belonging to this individual.
856a25f0a04SGreg Roach     *
857cbc1590aSGreg Roach     * @return int
858a25f0a04SGreg Roach     */
859c1010edaSGreg Roach    public function getNumberOfChildren()
860c1010edaSGreg Roach    {
861a25f0a04SGreg Roach        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
862a25f0a04SGreg Roach            return $match[1];
863a25f0a04SGreg Roach        } else {
86413abd6f3SGreg Roach            $children = [];
865a25f0a04SGreg Roach            foreach ($this->getSpouseFamilies() as $fam) {
866a25f0a04SGreg Roach                foreach ($fam->getChildren() as $child) {
867a25f0a04SGreg Roach                    $children[$child->getXref()] = true;
868a25f0a04SGreg Roach                }
869a25f0a04SGreg Roach            }
870a25f0a04SGreg Roach
871a25f0a04SGreg Roach            return count($children);
872a25f0a04SGreg Roach        }
873a25f0a04SGreg Roach    }
874a25f0a04SGreg Roach
875a25f0a04SGreg Roach    /**
876a25f0a04SGreg Roach     * Get a list of this individual’s child families (i.e. their parents).
877a25f0a04SGreg Roach     *
878cbc1590aSGreg Roach     * @param int|null $access_level
879a25f0a04SGreg Roach     *
880a25f0a04SGreg Roach     * @return Family[]
881a25f0a04SGreg Roach     */
882c1010edaSGreg Roach    public function getChildFamilies($access_level = null)
883c1010edaSGreg Roach    {
8844b9ff166SGreg Roach        if ($access_level === null) {
8854b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
8864b9ff166SGreg Roach        }
8874b9ff166SGreg Roach
888518bbdc1SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
889a25f0a04SGreg Roach
89013abd6f3SGreg Roach        $families = [];
891a25f0a04SGreg Roach        foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
892a25f0a04SGreg Roach            $family = $fact->getTarget();
893a25f0a04SGreg Roach            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
894a25f0a04SGreg Roach                $families[] = $family;
895a25f0a04SGreg Roach            }
896a25f0a04SGreg Roach        }
897a25f0a04SGreg Roach
898a25f0a04SGreg Roach        return $families;
899a25f0a04SGreg Roach    }
900a25f0a04SGreg Roach
901a25f0a04SGreg Roach    /**
902a25f0a04SGreg Roach     * Get the preferred parents for this individual.
903a25f0a04SGreg Roach     *
904a25f0a04SGreg Roach     * An individual may multiple parents (e.g. birth, adopted, disputed).
905a25f0a04SGreg Roach     * The preferred family record is:
906a25f0a04SGreg Roach     * (a) the first one with an explicit tag "_PRIMARY Y"
907a25f0a04SGreg Roach     * (b) the first one with a pedigree of "birth"
908a25f0a04SGreg Roach     * (c) the first one with no pedigree (default is "birth")
909a25f0a04SGreg Roach     * (d) the first one found
910a25f0a04SGreg Roach     *
911a25f0a04SGreg Roach     * @return Family|null
912a25f0a04SGreg Roach     */
913c1010edaSGreg Roach    public function getPrimaryChildFamily()
914c1010edaSGreg Roach    {
915a25f0a04SGreg Roach        $families = $this->getChildFamilies();
916a25f0a04SGreg Roach        switch (count($families)) {
917a25f0a04SGreg Roach            case 0:
918a25f0a04SGreg Roach                return null;
919a25f0a04SGreg Roach            case 1:
92015d603e7SGreg Roach                return $families[0];
921a25f0a04SGreg Roach            default:
922a25f0a04SGreg Roach                // If there is more than one FAMC record, choose the preferred parents:
923a25f0a04SGreg Roach                // a) records with '2 _PRIMARY'
92474e78bfaSJonathan Jaubart                foreach ($families as $fam) {
92574e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
926a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
927a25f0a04SGreg Roach                        return $fam;
928a25f0a04SGreg Roach                    }
929a25f0a04SGreg Roach                }
930a25f0a04SGreg Roach                // b) records with '2 PEDI birt'
93174e78bfaSJonathan Jaubart                foreach ($families as $fam) {
93274e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
933a25f0a04SGreg Roach                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
934a25f0a04SGreg Roach                        return $fam;
935a25f0a04SGreg Roach                    }
936a25f0a04SGreg Roach                }
937a25f0a04SGreg Roach                // c) records with no '2 PEDI'
93874e78bfaSJonathan Jaubart                foreach ($families as $fam) {
93974e78bfaSJonathan Jaubart                    $famid = $fam->getXref();
940a25f0a04SGreg Roach                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
941a25f0a04SGreg Roach                        return $fam;
942a25f0a04SGreg Roach                    }
943a25f0a04SGreg Roach                }
944a25f0a04SGreg Roach
945a25f0a04SGreg Roach                // d) any record
94615d603e7SGreg Roach                return $families[0];
947a25f0a04SGreg Roach        }
948a25f0a04SGreg Roach    }
949a25f0a04SGreg Roach
950a25f0a04SGreg Roach    /**
951a25f0a04SGreg Roach     * Get a list of step-parent families.
952a25f0a04SGreg Roach     *
953a25f0a04SGreg Roach     * @return Family[]
954a25f0a04SGreg Roach     */
955c1010edaSGreg Roach    public function getChildStepFamilies()
956c1010edaSGreg Roach    {
95713abd6f3SGreg Roach        $step_families = [];
958a25f0a04SGreg Roach        $families      = $this->getChildFamilies();
959a25f0a04SGreg Roach        foreach ($families as $family) {
960a25f0a04SGreg Roach            $father = $family->getHusband();
961a25f0a04SGreg Roach            if ($father) {
962a25f0a04SGreg Roach                foreach ($father->getSpouseFamilies() as $step_family) {
963a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
964a25f0a04SGreg Roach                        $step_families[] = $step_family;
965a25f0a04SGreg Roach                    }
966a25f0a04SGreg Roach                }
967a25f0a04SGreg Roach            }
968a25f0a04SGreg Roach            $mother = $family->getWife();
969a25f0a04SGreg Roach            if ($mother) {
970a25f0a04SGreg Roach                foreach ($mother->getSpouseFamilies() as $step_family) {
971a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
972a25f0a04SGreg Roach                        $step_families[] = $step_family;
973a25f0a04SGreg Roach                    }
974a25f0a04SGreg Roach                }
975a25f0a04SGreg Roach            }
976a25f0a04SGreg Roach        }
977a25f0a04SGreg Roach
978a25f0a04SGreg Roach        return $step_families;
979a25f0a04SGreg Roach    }
980a25f0a04SGreg Roach
981a25f0a04SGreg Roach    /**
982a25f0a04SGreg Roach     * Get a list of step-parent families.
983a25f0a04SGreg Roach     *
984a25f0a04SGreg Roach     * @return Family[]
985a25f0a04SGreg Roach     */
986c1010edaSGreg Roach    public function getSpouseStepFamilies()
987c1010edaSGreg Roach    {
98813abd6f3SGreg Roach        $step_families = [];
989a25f0a04SGreg Roach        $families      = $this->getSpouseFamilies();
990a25f0a04SGreg Roach        foreach ($families as $family) {
991a25f0a04SGreg Roach            $spouse = $family->getSpouse($this);
992a25f0a04SGreg Roach            if ($spouse) {
993a25f0a04SGreg Roach                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
994a25f0a04SGreg Roach                    if (!in_array($step_family, $families, true)) {
995a25f0a04SGreg Roach                        $step_families[] = $step_family;
996a25f0a04SGreg Roach                    }
997a25f0a04SGreg Roach                }
998a25f0a04SGreg Roach            }
999a25f0a04SGreg Roach        }
1000a25f0a04SGreg Roach
1001a25f0a04SGreg Roach        return $step_families;
1002a25f0a04SGreg Roach    }
1003a25f0a04SGreg Roach
1004a25f0a04SGreg Roach    /**
1005a25f0a04SGreg Roach     * A label for a parental family group
1006a25f0a04SGreg Roach     *
1007a25f0a04SGreg Roach     * @param Family $family
1008a25f0a04SGreg Roach     *
1009a25f0a04SGreg Roach     * @return string
1010a25f0a04SGreg Roach     */
1011c1010edaSGreg Roach    public function getChildFamilyLabel(Family $family)
1012c1010edaSGreg Roach    {
1013a25f0a04SGreg Roach        if (preg_match('/\n1 FAMC @' . $family->getXref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
1014a25f0a04SGreg Roach            // A specified pedigree
1015764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1016a25f0a04SGreg Roach        } else {
1017a25f0a04SGreg Roach            // Default (birth) pedigree
1018764a01d9SGreg Roach            return GedcomCodePedi::getChildFamilyLabel('');
1019a25f0a04SGreg Roach        }
1020a25f0a04SGreg Roach    }
1021a25f0a04SGreg Roach
1022a25f0a04SGreg Roach    /**
1023a25f0a04SGreg Roach     * Create a label for a step family
1024a25f0a04SGreg Roach     *
1025a25f0a04SGreg Roach     * @param Family $step_family
1026a25f0a04SGreg Roach     *
1027a25f0a04SGreg Roach     * @return string
1028a25f0a04SGreg Roach     */
1029c1010edaSGreg Roach    public function getStepFamilyLabel(Family $step_family)
1030c1010edaSGreg Roach    {
1031a25f0a04SGreg Roach        foreach ($this->getChildFamilies() as $family) {
1032a25f0a04SGreg Roach            if ($family !== $step_family) {
1033a25f0a04SGreg Roach                // Must be a step-family
1034a25f0a04SGreg Roach                foreach ($family->getSpouses() as $parent) {
1035a25f0a04SGreg Roach                    foreach ($step_family->getSpouses() as $step_parent) {
1036a25f0a04SGreg Roach                        if ($parent === $step_parent) {
1037a25f0a04SGreg Roach                            // One common parent - must be a step family
1038a25f0a04SGreg Roach                            if ($parent->getSex() == 'M') {
1039a25f0a04SGreg Roach                                // Father’s family with someone else
1040a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1041a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1042bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1043a25f0a04SGreg Roach                                } else {
1044a25f0a04SGreg Roach                                    /* I18N: A step-family. */
1045bbb76c12SGreg Roach                                    return I18N::translate('Father’s family with an unknown individual');
1046a25f0a04SGreg Roach                                }
1047a25f0a04SGreg Roach                            } else {
1048a25f0a04SGreg Roach                                // Mother’s family with someone else
1049a25f0a04SGreg Roach                                if ($step_family->getSpouse($step_parent)) {
1050a25f0a04SGreg Roach                                    /* I18N: A step-family. %s is an individual’s name */
1051bbb76c12SGreg Roach                                    return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1052a25f0a04SGreg Roach                                } else {
1053a25f0a04SGreg Roach                                    /* I18N: A step-family. */
1054bbb76c12SGreg Roach                                    return I18N::translate('Mother’s family with an unknown individual');
1055a25f0a04SGreg Roach                                }
1056a25f0a04SGreg Roach                            }
1057a25f0a04SGreg Roach                        }
1058a25f0a04SGreg Roach                    }
1059a25f0a04SGreg Roach                }
1060a25f0a04SGreg Roach            }
1061a25f0a04SGreg Roach        }
1062a25f0a04SGreg Roach
1063a25f0a04SGreg Roach        // Perahps same parents - but a different family record?
1064a25f0a04SGreg Roach        return I18N::translate('Family with parents');
1065a25f0a04SGreg Roach    }
1066a25f0a04SGreg Roach
1067225e381fSGreg Roach
1068225e381fSGreg Roach    /**
1069225e381fSGreg Roach     * Get the description for the family.
1070225e381fSGreg Roach     *
1071225e381fSGreg Roach     * For example, "XXX's family with new wife".
1072225e381fSGreg Roach     *
1073225e381fSGreg Roach     * @param Family $family
1074225e381fSGreg Roach     *
1075225e381fSGreg Roach     * @return string
1076225e381fSGreg Roach     */
1077c1010edaSGreg Roach    public function getSpouseFamilyLabel(Family $family)
1078c1010edaSGreg Roach    {
1079225e381fSGreg Roach        $spouse = $family->getSpouse($this);
1080225e381fSGreg Roach        if ($spouse) {
1081225e381fSGreg Roach            /* I18N: %s is the spouse name */
1082bbb76c12SGreg Roach            return I18N::translate('Family with %s', $spouse->getFullName());
1083225e381fSGreg Roach        } else {
1084225e381fSGreg Roach            return $family->getFullName();
1085225e381fSGreg Roach        }
1086225e381fSGreg Roach    }
1087225e381fSGreg Roach
1088a25f0a04SGreg Roach    /**
1089a25f0a04SGreg Roach     * get primary parents names for this individual
1090a25f0a04SGreg Roach     *
1091a25f0a04SGreg Roach     * @param string $classname optional css class
1092a25f0a04SGreg Roach     * @param string $display   optional css style display
1093a25f0a04SGreg Roach     *
1094a25f0a04SGreg Roach     * @return string a div block with father & mother names
1095a25f0a04SGreg Roach     */
1096c1010edaSGreg Roach    public function getPrimaryParentsNames($classname = '', $display = '')
1097c1010edaSGreg Roach    {
1098a25f0a04SGreg Roach        $fam = $this->getPrimaryChildFamily();
1099a25f0a04SGreg Roach        if (!$fam) {
1100a25f0a04SGreg Roach            return '';
1101a25f0a04SGreg Roach        }
1102a25f0a04SGreg Roach        $txt = '<div';
1103a25f0a04SGreg Roach        if ($classname) {
11044c621133SGreg Roach            $txt .= ' class="' . $classname . '"';
1105a25f0a04SGreg Roach        }
1106a25f0a04SGreg Roach        if ($display) {
11074c621133SGreg Roach            $txt .= ' style="display:' . $display . '"';
1108a25f0a04SGreg Roach        }
1109a25f0a04SGreg Roach        $txt  .= '>';
1110a25f0a04SGreg Roach        $husb = $fam->getHusband();
1111a25f0a04SGreg Roach        if ($husb) {
1112a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1113a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1114a25f0a04SGreg Roach            $primary = $husb->getPrimaryName();
1115a25f0a04SGreg Roach            $husb->setPrimaryName(null);
1116a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s father */
1117bbb76c12SGreg Roach            $txt .= I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1118a25f0a04SGreg Roach            $husb->setPrimaryName($primary);
1119a25f0a04SGreg Roach        }
1120a25f0a04SGreg Roach        $wife = $fam->getWife();
1121a25f0a04SGreg Roach        if ($wife) {
1122a25f0a04SGreg Roach            // Temporarily reset the 'prefered' display name, as we always
1123a25f0a04SGreg Roach            // want the default name, not the one selected for display on the indilist.
1124a25f0a04SGreg Roach            $primary = $wife->getPrimaryName();
1125a25f0a04SGreg Roach            $wife->setPrimaryName(null);
1126a25f0a04SGreg Roach            /* I18N: %s is the name of an individual’s mother */
1127bbb76c12SGreg Roach            $txt .= I18N::translate('Mother: %s', $wife->getFullName());
1128a25f0a04SGreg Roach            $wife->setPrimaryName($primary);
1129a25f0a04SGreg Roach        }
1130a25f0a04SGreg Roach        $txt .= '</div>';
1131a25f0a04SGreg Roach
1132a25f0a04SGreg Roach        return $txt;
1133a25f0a04SGreg Roach    }
1134a25f0a04SGreg Roach
1135a25f0a04SGreg Roach    /** {@inheritdoc} */
1136c1010edaSGreg Roach    public function getFallBackName()
1137c1010edaSGreg Roach    {
1138a25f0a04SGreg Roach        return '@P.N. /@N.N./';
1139a25f0a04SGreg Roach    }
1140a25f0a04SGreg Roach
1141a25f0a04SGreg Roach    /**
1142a25f0a04SGreg Roach     * Convert a name record into ‘full’ and ‘sort’ versions.
1143a25f0a04SGreg Roach     * Use the NAME field to generate the ‘full’ version, as the
1144a25f0a04SGreg Roach     * gedcom spec says that this is the individual’s name, as they would write it.
1145a25f0a04SGreg Roach     * Use the SURN field to generate the sortable names. Note that this field
1146a25f0a04SGreg Roach     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1147a25f0a04SGreg Roach     * recorded in the NAME field. e.g.
1148a25f0a04SGreg Roach     *
1149a25f0a04SGreg Roach     * 1 NAME Robert /de Gliderow/
1150a25f0a04SGreg Roach     * 2 GIVN Robert
1151a25f0a04SGreg Roach     * 2 SPFX de
1152a25f0a04SGreg Roach     * 2 SURN CLITHEROW
1153a25f0a04SGreg Roach     * 2 NICK The Bald
1154a25f0a04SGreg Roach     *
1155a25f0a04SGreg Roach     * full=>'Robert de Gliderow 'The Bald''
1156a25f0a04SGreg Roach     * sort=>'CLITHEROW, ROBERT'
1157a25f0a04SGreg Roach     *
1158a25f0a04SGreg Roach     * Handle multiple surnames, either as;
1159a25f0a04SGreg Roach     *
1160a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez/ y /Sante/
1161a25f0a04SGreg Roach     * or
1162a25f0a04SGreg Roach     * 1 NAME Carlos /Vasquez y Sante/
1163a25f0a04SGreg Roach     * 2 GIVN Carlos
1164a25f0a04SGreg Roach     * 2 SURN Vasquez,Sante
1165a25f0a04SGreg Roach     *
1166a25f0a04SGreg Roach     * @param string $type
1167a25f0a04SGreg Roach     * @param string $full
1168a25f0a04SGreg Roach     * @param string $gedcom
1169a25f0a04SGreg Roach     */
1170c1010edaSGreg Roach    protected function addName($type, $full, $gedcom)
1171c1010edaSGreg Roach    {
1172a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1173a25f0a04SGreg Roach        // Extract the structured name parts - use for "sortable" names and indexes
1174a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1175a25f0a04SGreg Roach
1176a25f0a04SGreg Roach        $sublevel = 1 + (int)$gedcom[0];
1177a25f0a04SGreg Roach        $NPFX     = preg_match("/\n{$sublevel} NPFX (.+)/", $gedcom, $match) ? $match[1] : '';
1178a25f0a04SGreg Roach        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1179a25f0a04SGreg Roach        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1180a25f0a04SGreg Roach        $NSFX     = preg_match("/\n{$sublevel} NSFX (.+)/", $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...
1184a25f0a04SGreg Roach        if ($SURN) {
1185a25f0a04SGreg Roach            $SURNS = preg_split('/ *, */', $SURN);
1186a25f0a04SGreg Roach        } else {
118713abd6f3SGreg Roach            $SURNS = [];
1188a25f0a04SGreg Roach        }
1189a25f0a04SGreg Roach        // ...so is GIVN - but nobody uses it like that
1190a25f0a04SGreg Roach        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1191a25f0a04SGreg Roach
1192a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1193a25f0a04SGreg Roach        // Extract the components from NAME - use for the "full" names
1194a25f0a04SGreg Roach        ////////////////////////////////////////////////////////////////////////////
1195a25f0a04SGreg Roach
1196a25f0a04SGreg Roach        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
1197a25f0a04SGreg Roach        if (substr_count($full, '/') % 2 == 1) {
1198a25f0a04SGreg Roach            $full = $full . '/';
1199a25f0a04SGreg Roach        }
1200a25f0a04SGreg Roach
1201a25f0a04SGreg Roach        // GEDCOM uses "//" to indicate an unknown surname
1202a25f0a04SGreg Roach        $full = preg_replace('/\/\//', '/@N.N./', $full);
1203a25f0a04SGreg Roach
1204a25f0a04SGreg Roach        // Extract the surname.
1205a25f0a04SGreg Roach        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1206a25f0a04SGreg Roach        if (preg_match('/\/.*\//', $full, $match)) {
1207a25f0a04SGreg Roach            $surname = str_replace('/', '', $match[0]);
1208a25f0a04SGreg Roach        } else {
1209a25f0a04SGreg Roach            $surname = '';
1210a25f0a04SGreg Roach        }
1211a25f0a04SGreg Roach
1212a25f0a04SGreg Roach        // If we don’t have a SURN record, extract it from the NAME
1213a25f0a04SGreg Roach        if (!$SURNS) {
1214a25f0a04SGreg Roach            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1215a25f0a04SGreg Roach                // There can be many surnames, each wrapped with '/'
1216a25f0a04SGreg Roach                $SURNS = $matches[1];
1217a25f0a04SGreg Roach                foreach ($SURNS as $n => $SURN) {
1218a25f0a04SGreg Roach                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1219a25f0a04SGreg Roach                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1220a25f0a04SGreg Roach                }
1221a25f0a04SGreg Roach            } else {
1222a25f0a04SGreg Roach                // It is valid not to have a surname at all
122313abd6f3SGreg Roach                $SURNS = [''];
1224a25f0a04SGreg Roach            }
1225a25f0a04SGreg Roach        }
1226a25f0a04SGreg Roach
1227a25f0a04SGreg Roach        // If we don’t have a GIVN record, extract it from the NAME
1228a25f0a04SGreg Roach        if (!$GIVN) {
1229a25f0a04SGreg Roach            $GIVN = preg_replace(
123013abd6f3SGreg Roach                [
1231c1010edaSGreg Roach                    '/ ?\/.*\/ ?/',
1232c1010edaSGreg Roach                    // remove surname
1233c1010edaSGreg Roach                    '/ ?".+"/',
1234c1010edaSGreg Roach                    // remove nickname
1235c1010edaSGreg Roach                    '/ {2,}/',
1236c1010edaSGreg Roach                    // multiple spaces, caused by the above
1237c1010edaSGreg Roach                    '/^ | $/',
1238c1010edaSGreg Roach                    // leading/trailing spaces, caused by the above
123913abd6f3SGreg Roach                ],
124013abd6f3SGreg Roach                [
1241a25f0a04SGreg Roach                    ' ',
1242a25f0a04SGreg Roach                    ' ',
1243a25f0a04SGreg Roach                    ' ',
1244a25f0a04SGreg Roach                    '',
124513abd6f3SGreg Roach                ],
1246a25f0a04SGreg Roach                $full
1247a25f0a04SGreg Roach            );
1248a25f0a04SGreg Roach        }
1249a25f0a04SGreg Roach
1250a25f0a04SGreg Roach        // Add placeholder for unknown given name
1251a25f0a04SGreg Roach        if (!$GIVN) {
1252a25f0a04SGreg Roach            $GIVN = '@P.N.';
1253a25f0a04SGreg Roach            $pos  = strpos($full, '/');
1254a25f0a04SGreg Roach            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1255a25f0a04SGreg Roach        }
1256a25f0a04SGreg Roach
12577b0e71c3SGreg Roach        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12587b0e71c3SGreg Roach        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1259c6f196c3SGreg Roach        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12607b0e71c3SGreg Roach            // A NICK field is present, but not included in the NAME.  Show it at the end.
1261c6f196c3SGreg Roach            $full .= ' "' . $NICK . '"';
1262a25f0a04SGreg Roach        }
1263a25f0a04SGreg Roach
1264a25f0a04SGreg Roach        // Remove slashes - they don’t get displayed
1265a25f0a04SGreg Roach        // $fullNN keeps the @N.N. placeholders, for the database
1266a25f0a04SGreg Roach        // $full is for display on-screen
1267a25f0a04SGreg Roach        $fullNN = str_replace('/', '', $full);
1268a25f0a04SGreg Roach
1269a25f0a04SGreg Roach        // Insert placeholders for any missing/unknown names
1270ad1a1cd2SGreg Roach        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1271ad1a1cd2SGreg Roach        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1272c6f196c3SGreg Roach        // Format for display
1273d53324c9SGreg Roach        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1274acc34ea1SGreg Roach        // Localise quotation marks around the nickname
12758d68cabeSGreg Roach        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function ($matches) {
12768d68cabeSGreg Roach            return I18N::translate('“%s”', $matches[1]);
12778d68cabeSGreg Roach        }, $full);
1278a25f0a04SGreg Roach
1279c6f196c3SGreg Roach        // A suffix of “*” indicates a preferred name
1280a25f0a04SGreg Roach        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1281a25f0a04SGreg Roach
1282a25f0a04SGreg Roach        // Remove prefered-name indicater - they don’t go in the database
1283a25f0a04SGreg Roach        $GIVN   = str_replace('*', '', $GIVN);
1284a25f0a04SGreg Roach        $fullNN = str_replace('*', '', $fullNN);
1285a25f0a04SGreg Roach
1286ffd703eaSGreg Roach        foreach ($SURNS as $SURN) {
1287a25f0a04SGreg Roach            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1288a25f0a04SGreg Roach            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1289a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1290a25f0a04SGreg Roach            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1291a25f0a04SGreg Roach                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1292a25f0a04SGreg Roach            }
1293a25f0a04SGreg Roach
1294bdb3725aSGreg Roach            $this->getAllNames[] = [
1295a25f0a04SGreg Roach                'type'    => $type,
1296a25f0a04SGreg Roach                'sort'    => $SURN . ',' . $GIVN,
1297c1010edaSGreg Roach                'full'    => $full,
1298c1010edaSGreg Roach                // This is used for display
1299c1010edaSGreg Roach                'fullNN'  => $fullNN,
1300c1010edaSGreg Roach                // This goes into the database
1301c1010edaSGreg Roach                'surname' => $surname,
1302c1010edaSGreg Roach                // This goes into the database
1303c1010edaSGreg Roach                'givn'    => $GIVN,
1304c1010edaSGreg Roach                // This goes into the database
1305c1010edaSGreg Roach                'surn'    => $SURN,
1306c1010edaSGreg Roach                // This goes into the database
130713abd6f3SGreg Roach            ];
1308a25f0a04SGreg Roach        }
1309a25f0a04SGreg Roach    }
1310a25f0a04SGreg Roach
1311a25f0a04SGreg Roach    /**
131276692c8bSGreg Roach     * Extract names from the GEDCOM record.
1313a25f0a04SGreg Roach     */
1314c1010edaSGreg Roach    public function extractNames()
1315c1010edaSGreg Roach    {
13164b9ff166SGreg Roach        $this->extractNamesFromFacts(1, 'NAME', $this->getFacts('NAME', false, Auth::accessLevel($this->tree), $this->canShowName()));
1317a25f0a04SGreg Roach    }
1318a25f0a04SGreg Roach
1319a25f0a04SGreg Roach    /**
1320a25f0a04SGreg Roach     * Extra info to display when displaying this record in a list of
1321a25f0a04SGreg Roach     * selection items or favorites.
1322a25f0a04SGreg Roach     *
1323a25f0a04SGreg Roach     * @return string
1324a25f0a04SGreg Roach     */
1325c1010edaSGreg Roach    public function formatListDetails()
1326c1010edaSGreg Roach    {
1327a25f0a04SGreg Roach        return
1328841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1329841014f1SGreg Roach            $this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1330a25f0a04SGreg Roach    }
1331a25f0a04SGreg Roach}
1332