xref: /webtrees/app/Individual.php (revision d69397b0fb0a7e71220076299d45dc2ac034b754)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees;
17
18use Fisharebest\ExtCalendar\GregorianCalendar;
19use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
20
21/**
22 * A GEDCOM individual (INDI) object.
23 */
24class Individual extends GedcomRecord
25{
26    const RECORD_TYPE = 'INDI';
27    const ROUTE_NAME  = 'individual';
28
29    /** @var int used in some lists to keep track of this individual’s generation in that list */
30    public $generation;
31
32    /** @var Date The estimated date of birth */
33    private $_getEstimatedBirthDate;
34
35    /** @var Date The estimated date of death */
36    private $_getEstimatedDeathDate;
37
38    /**
39     * Get an instance of an individual object. For single records,
40     * we just receive the XREF. For bulk records (such as lists
41     * and search results) we can receive the GEDCOM data as well.
42     *
43     * @param string      $xref
44     * @param Tree        $tree
45     * @param string|null $gedcom
46     *
47     * @throws \Exception
48     *
49     * @return Individual|null
50     */
51    public static function getInstance($xref, Tree $tree, $gedcom = null)
52    {
53        $record = parent::getInstance($xref, $tree, $gedcom);
54
55        if ($record instanceof Individual) {
56            return $record;
57        } else {
58            return null;
59        }
60    }
61
62    /**
63     * Sometimes, we'll know in advance that we need to load a set of records.
64     * Typically when we load families and their members.
65     *
66     * @param Tree     $tree
67     * @param string[] $xrefs
68     */
69    public static function load(Tree $tree, array $xrefs)
70    {
71        $args         = [
72            'tree_id' => $tree->getTreeId(),
73        ];
74        $placeholders = [];
75
76        foreach (array_unique($xrefs) as $n => $xref) {
77            if (!isset(self::$gedcom_record_cache[$tree->getTreeId()][$xref])) {
78                $placeholders[] = ':x' . $n;
79                $args['x' . $n] = $xref;
80            }
81        }
82
83        if (!empty($placeholders)) {
84            $rows = Database::prepare(
85                "SELECT i_id AS xref, i_gedcom AS gedcom" .
86                " FROM `##individuals`" .
87                " WHERE i_file = :tree_id AND i_id IN (" . implode(',', $placeholders) . ")"
88            )->execute(
89                $args
90            )->fetchAll();
91
92            foreach ($rows as $row) {
93                self::getInstance($row->xref, $tree, $row->gedcom);
94            }
95        }
96    }
97
98    /**
99     * Can the name of this record be shown?
100     *
101     * @param int|null $access_level
102     *
103     * @return bool
104     */
105    public function canShowName($access_level = null)
106    {
107        if ($access_level === null) {
108            $access_level = Auth::accessLevel($this->tree);
109        }
110
111        return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
112    }
113
114    /**
115     * Can this individual be shown?
116     *
117     * @param int $access_level
118     *
119     * @return bool
120     */
121    protected function canShowByType($access_level)
122    {
123        // Dead people...
124        if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
125            $keep_alive             = false;
126            $KEEP_ALIVE_YEARS_BIRTH = (int)$this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
127            if ($KEEP_ALIVE_YEARS_BIRTH) {
128                preg_match_all('/\n1 (?:' . WT_EVENTS_BIRT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
129                foreach ($matches as $match) {
130                    $date = new Date($match[1]);
131                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
132                        $keep_alive = true;
133                        break;
134                    }
135                }
136            }
137            $KEEP_ALIVE_YEARS_DEATH = (int)$this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
138            if ($KEEP_ALIVE_YEARS_DEATH) {
139                preg_match_all('/\n1 (?:' . WT_EVENTS_DEAT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
140                foreach ($matches as $match) {
141                    $date = new Date($match[1]);
142                    if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
143                        $keep_alive = true;
144                        break;
145                    }
146                }
147            }
148            if (!$keep_alive) {
149                return true;
150            }
151        }
152        // Consider relationship privacy (unless an admin is applying download restrictions)
153        $user_path_length = (int)$this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
154        $gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
155        if ($gedcomid !== '' && $user_path_length > 0) {
156            return self::isRelated($this, $user_path_length);
157        }
158
159        // No restriction found - show living people to members only:
160        return Auth::PRIV_USER >= $access_level;
161    }
162
163    /**
164     * For relationship privacy calculations - is this individual a close relative?
165     *
166     * @param Individual $target
167     * @param int        $distance
168     *
169     * @return bool
170     */
171    private static function isRelated(Individual $target, $distance)
172    {
173        static $cache = null;
174
175        $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
176        if ($user_individual) {
177            if (!$cache) {
178                $cache = [
179                    0 => [$user_individual],
180                    1 => [],
181                ];
182                foreach ($user_individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
183                    $family = $fact->getTarget();
184                    if ($family) {
185                        $cache[1][] = $family;
186                    }
187                }
188            }
189        } else {
190            // No individual linked to this account? Cannot use relationship privacy.
191            return true;
192        }
193
194        // Double the distance, as we count the INDI-FAM and FAM-INDI links separately
195        $distance *= 2;
196
197        // Consider each path length in turn
198        for ($n = 0; $n <= $distance; ++$n) {
199            if (array_key_exists($n, $cache)) {
200                // We have already calculated all records with this length
201                if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
202                    return true;
203                }
204            } else {
205                // Need to calculate these paths
206                $cache[$n] = [];
207                if ($n % 2 == 0) {
208                    // Add FAM->INDI links
209                    foreach ($cache[$n - 1] as $family) {
210                        foreach ($family->getFacts('HUSB|WIFE|CHIL', false, Auth::PRIV_HIDE) as $fact) {
211                            $individual = $fact->getTarget();
212                            // Don’t backtrack
213                            if ($individual && !in_array($individual, $cache[$n - 2], true)) {
214                                $cache[$n][] = $individual;
215                            }
216                        }
217                    }
218                    if (in_array($target, $cache[$n], true)) {
219                        return true;
220                    }
221                } else {
222                    // Add INDI->FAM links
223                    foreach ($cache[$n - 1] as $individual) {
224                        foreach ($individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
225                            $family = $fact->getTarget();
226                            // Don’t backtrack
227                            if ($family && !in_array($family, $cache[$n - 2], true)) {
228                                $cache[$n][] = $family;
229                            }
230                        }
231                    }
232                }
233            }
234        }
235
236        return false;
237    }
238
239    /**
240     * Generate a private version of this record
241     *
242     * @param int $access_level
243     *
244     * @return string
245     */
246    protected function createPrivateGedcomRecord($access_level)
247    {
248        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
249
250        $rec = '0 @' . $this->xref . '@ INDI';
251        if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
252            // Show all the NAME tags, including subtags
253            foreach ($this->getFacts('NAME') as $fact) {
254                $rec .= "\n" . $fact->getGedcom();
255            }
256        }
257        // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
258        preg_match_all('/\n1 (?:FAMC|FAMS) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
259        foreach ($matches as $match) {
260            $rela = Family::getInstance($match[1], $this->tree);
261            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
262                $rec .= $match[0];
263            }
264        }
265        // Don’t privatize sex.
266        if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
267            $rec .= $match[0];
268        }
269
270        return $rec;
271    }
272
273    /**
274     * Fetch data from the database
275     *
276     * @param string $xref
277     * @param int    $tree_id
278     *
279     * @return null|string
280     */
281    protected static function fetchGedcomRecord($xref, $tree_id)
282    {
283        return Database::prepare(
284            "SELECT i_gedcom FROM `##individuals` WHERE i_id = :xref AND i_file = :tree_id"
285        )->execute([
286            'xref'    => $xref,
287            'tree_id' => $tree_id,
288        ])->fetchOne();
289    }
290
291    /**
292     * Static helper function to sort an array of people by birth date
293     *
294     * @param Individual $x
295     * @param Individual $y
296     *
297     * @return int
298     */
299    public static function compareBirthDate(Individual $x, Individual $y)
300    {
301        return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
302    }
303
304    /**
305     * Static helper function to sort an array of people by death date
306     *
307     * @param Individual $x
308     * @param Individual $y
309     *
310     * @return int
311     */
312    public static function compareDeathDate(Individual $x, Individual $y)
313    {
314        return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
315    }
316
317    /**
318     * Calculate whether this individual is living or dead.
319     * If not known to be dead, then assume living.
320     *
321     * @return bool
322     */
323    public function isDead()
324    {
325        $MAX_ALIVE_AGE = (int)$this->tree->getPreference('MAX_ALIVE_AGE');
326
327        // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
328        if (preg_match('/\n1 (?:' . WT_EVENTS_DEAT . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
329            return true;
330        }
331
332        // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
333        if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
334            foreach ($date_matches[1] as $date_match) {
335                $date = new Date($date_match);
336                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
337                    return true;
338                }
339            }
340            // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
341            // If one of these is a birth, the individual must be alive.
342            if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
343                return false;
344            }
345        }
346
347        // If we found no conclusive dates then check the dates of close relatives.
348
349        // Check parents (birth and adopted)
350        foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
351            foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
352                // Assume parents are no more than 45 years older than their children
353                preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
354                foreach ($date_matches[1] as $date_match) {
355                    $date = new Date($date_match);
356                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
357                        return true;
358                    }
359                }
360            }
361        }
362
363        // Check spouses
364        foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
365            preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
366            foreach ($date_matches[1] as $date_match) {
367                $date = new Date($date_match);
368                // Assume marriage occurs after age of 10
369                if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
370                    return true;
371                }
372            }
373            // Check spouse dates
374            $spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
375            if ($spouse) {
376                preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
377                foreach ($date_matches[1] as $date_match) {
378                    $date = new Date($date_match);
379                    // Assume max age difference between spouses of 40 years
380                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
381                        return true;
382                    }
383                }
384            }
385            // Check child dates
386            foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
387                preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
388                // Assume children born after age of 15
389                foreach ($date_matches[1] as $date_match) {
390                    $date = new Date($date_match);
391                    if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
392                        return true;
393                    }
394                }
395                // Check grandchildren
396                foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
397                    foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
398                        preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
399                        // Assume grandchildren born after age of 30
400                        foreach ($date_matches[1] as $date_match) {
401                            $date = new Date($date_match);
402                            if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
403                                return true;
404                            }
405                        }
406                    }
407                }
408            }
409        }
410
411        return false;
412    }
413
414    /**
415     * Find the highlighted media object for an individual
416     *
417     * @return null|MediaFile
418     */
419    public function findHighlightedMediaFile()
420    {
421        foreach ($this->getFacts('OBJE') as $fact) {
422            $media = $fact->getTarget();
423            if ($media instanceof Media) {
424                foreach ($media->mediaFiles() as $media_file) {
425                    if ($media_file->isImage() && !$media_file->isExternal()) {
426                        return $media_file;
427                    }
428                }
429            }
430        }
431
432        return null;
433    }
434
435    /**
436     * Display the prefered image for this individual.
437     * Use an icon if no image is available.
438     *
439     * @param int      $width      Pixels
440     * @param int      $height     Pixels
441     * @param string   $fit        "crop" or "contain"
442     * @param string[] $attributes Additional HTML attributes
443     *
444     * @return string
445     */
446    public function displayImage($width, $height, $fit, $attributes)
447    {
448        $media_file = $this->findHighlightedMediaFile();
449
450        if ($media_file !== null) {
451            return $media_file->displayImage($width, $height, $fit, $attributes);
452        }
453
454        if ($this->tree->getPreference('USE_SILHOUETTE')) {
455            return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
456        }
457
458        return '';
459    }
460
461    /**
462     * Get the date of birth
463     *
464     * @return Date
465     */
466    public function getBirthDate()
467    {
468        foreach ($this->getAllBirthDates() as $date) {
469            if ($date->isOK()) {
470                return $date;
471            }
472        }
473
474        return new Date('');
475    }
476
477    /**
478     * Get the place of birth
479     *
480     * @return Place
481     */
482    public function getBirthPlace()
483    {
484        foreach ($this->getAllBirthPlaces() as $place) {
485            if ($place) {
486                return $place;
487            }
488        }
489
490        return new Place('', $this->tree);
491    }
492
493    /**
494     * Get the year of birth
495     *
496     * @return string the year of birth
497     */
498    public function getBirthYear()
499    {
500        return $this->getBirthDate()->minimumDate()->format('%Y');
501    }
502
503    /**
504     * Get the date of death
505     *
506     * @return Date
507     */
508    public function getDeathDate()
509    {
510        foreach ($this->getAllDeathDates() as $date) {
511            if ($date->isOK()) {
512                return $date;
513            }
514        }
515
516        return new Date('');
517    }
518
519    /**
520     * Get the place of death
521     *
522     * @return Place
523     */
524    public function getDeathPlace()
525    {
526        foreach ($this->getAllDeathPlaces() as $place) {
527            if ($place) {
528                return $place;
529            }
530        }
531
532        return new Place('', $this->tree);
533    }
534
535    /**
536     * get the death year
537     *
538     * @return string the year of death
539     */
540    public function getDeathYear()
541    {
542        return $this->getDeathDate()->minimumDate()->format('%Y');
543    }
544
545    /**
546     * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
547     * Provide the place and full date using a tooltip.
548     * For consistent layout in charts, etc., show just a “–” when no dates are known.
549     * Note that this is a (non-breaking) en-dash, and not a hyphen.
550     *
551     * @return string
552     */
553    public function getLifeSpan()
554    {
555        // Just the first part of the place name
556        $birth_place = strip_tags($this->getBirthPlace()->getShortName());
557        $death_place = strip_tags($this->getDeathPlace()->getShortName());
558        // Remove markup from dates
559        $birth_date = strip_tags($this->getBirthDate()->display());
560        $death_date = strip_tags($this->getDeathDate()->display());
561
562        return
563            /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */
564            I18N::translate(
565                '%1$s–%2$s',
566                '<span title="' . e($birth_place) . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
567                '<span title="' . e($death_place) . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
568            );
569    }
570
571    /**
572     * Get all the birth dates - for the individual lists.
573     *
574     * @return Date[]
575     */
576    public function getAllBirthDates()
577    {
578        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
579            $tmp = $this->getAllEventDates($event);
580            if ($tmp) {
581                return $tmp;
582            }
583        }
584
585        return [];
586    }
587
588    /**
589     * Gat all the birth places - for the individual lists.
590     *
591     * @return Place[]
592     */
593    public function getAllBirthPlaces()
594    {
595        foreach (explode('|', WT_EVENTS_BIRT) as $event) {
596            $places = $this->getAllEventPlaces($event);
597            if (!empty($places)) {
598                return $places;
599            }
600        }
601
602        return [];
603    }
604
605    /**
606     * Get all the death dates - for the individual lists.
607     *
608     * @return Date[]
609     */
610    public function getAllDeathDates()
611    {
612        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
613            $tmp = $this->getAllEventDates($event);
614            if ($tmp) {
615                return $tmp;
616            }
617        }
618
619        return [];
620    }
621
622    /**
623     * Get all the death places - for the individual lists.
624     *
625     * @return Place[]
626     */
627    public function getAllDeathPlaces()
628    {
629        foreach (explode('|', WT_EVENTS_DEAT) as $event) {
630            $places = $this->getAllEventPlaces($event);
631            if (!empty($places)) {
632                return $places;
633            }
634        }
635
636        return [];
637    }
638
639    /**
640     * Generate an estimate for the date of birth, based on dates of parents/children/spouses
641     *
642     * @return Date
643     */
644    public function getEstimatedBirthDate()
645    {
646        if (is_null($this->_getEstimatedBirthDate)) {
647            foreach ($this->getAllBirthDates() as $date) {
648                if ($date->isOK()) {
649                    $this->_getEstimatedBirthDate = $date;
650                    break;
651                }
652            }
653            if (is_null($this->_getEstimatedBirthDate)) {
654                $min = [];
655                $max = [];
656                $tmp = $this->getDeathDate();
657                if ($tmp->isOK()) {
658                    $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
659                    $max[] = $tmp->maximumJulianDay();
660                }
661                foreach ($this->getChildFamilies() as $family) {
662                    $tmp = $family->getMarriageDate();
663                    if ($tmp->isOK()) {
664                        $min[] = $tmp->maximumJulianDay() - 365 * 1;
665                        $max[] = $tmp->minimumJulianDay() + 365 * 30;
666                    }
667                    if ($parent = $family->getHusband()) {
668                        $tmp = $parent->getBirthDate();
669                        if ($tmp->isOK()) {
670                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
671                            $max[] = $tmp->minimumJulianDay() + 365 * 65;
672                        }
673                    }
674                    if ($parent = $family->getWife()) {
675                        $tmp = $parent->getBirthDate();
676                        if ($tmp->isOK()) {
677                            $min[] = $tmp->maximumJulianDay() + 365 * 15;
678                            $max[] = $tmp->minimumJulianDay() + 365 * 45;
679                        }
680                    }
681                    foreach ($family->getChildren() as $child) {
682                        $tmp = $child->getBirthDate();
683                        if ($tmp->isOK()) {
684                            $min[] = $tmp->maximumJulianDay() - 365 * 30;
685                            $max[] = $tmp->minimumJulianDay() + 365 * 30;
686                        }
687                    }
688                }
689                foreach ($this->getSpouseFamilies() as $family) {
690                    $tmp = $family->getMarriageDate();
691                    if ($tmp->isOK()) {
692                        $min[] = $tmp->maximumJulianDay() - 365 * 45;
693                        $max[] = $tmp->minimumJulianDay() - 365 * 15;
694                    }
695                    $spouse = $family->getSpouse($this);
696                    if ($spouse) {
697                        $tmp = $spouse->getBirthDate();
698                        if ($tmp->isOK()) {
699                            $min[] = $tmp->maximumJulianDay() - 365 * 25;
700                            $max[] = $tmp->minimumJulianDay() + 365 * 25;
701                        }
702                    }
703                    foreach ($family->getChildren() as $child) {
704                        $tmp = $child->getBirthDate();
705                        if ($tmp->isOK()) {
706                            $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
707                            $max[] = $tmp->minimumJulianDay() - 365 * 15;
708                        }
709                    }
710                }
711                if ($min && $max) {
712                    $gregorian_calendar = new GregorianCalendar;
713
714                    list($year) = $gregorian_calendar->jdToYmd((int)((max($min) + min($max)) / 2));
715                    $this->_getEstimatedBirthDate = new Date('EST ' . $year);
716                } else {
717                    $this->_getEstimatedBirthDate = new Date(''); // always return a date object
718                }
719            }
720        }
721
722        return $this->_getEstimatedBirthDate;
723    }
724
725    /**
726     * Generate an estimated date of death.
727     *
728     * @return Date
729     */
730    public function getEstimatedDeathDate()
731    {
732        if ($this->_getEstimatedDeathDate === null) {
733            foreach ($this->getAllDeathDates() as $date) {
734                if ($date->isOK()) {
735                    $this->_getEstimatedDeathDate = $date;
736                    break;
737                }
738            }
739            if ($this->_getEstimatedDeathDate === null) {
740                if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
741                    $max_alive_age                = (int)$this->tree->getPreference('MAX_ALIVE_AGE');
742                    $this->_getEstimatedDeathDate = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
743                } else {
744                    $this->_getEstimatedDeathDate = new Date(''); // always return a date object
745                }
746            }
747        }
748
749        return $this->_getEstimatedDeathDate;
750    }
751
752    /**
753     * Get the sex - M F or U
754     * Use the un-privatised gedcom record. We call this function during
755     * the privatize-gedcom function, and we are allowed to know this.
756     *
757     * @return string
758     */
759    public function getSex()
760    {
761        if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
762            return $match[1];
763        } else {
764            return 'U';
765        }
766    }
767
768    /**
769     * Get the individual’s sex image
770     *
771     * @param string $size
772     *
773     * @return string
774     */
775    public function getSexImage($size = 'small')
776    {
777        return self::sexImage($this->getSex(), $size);
778    }
779
780    /**
781     * Generate a sex icon/image
782     *
783     * @param string $sex
784     * @param string $size
785     *
786     * @return string
787     */
788    public static function sexImage($sex, $size = 'small')
789    {
790        return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
791    }
792
793    /**
794     * Generate the CSS class to be used for drawing this individual
795     *
796     * @return string
797     */
798    public function getBoxStyle()
799    {
800        $tmp = [
801            'M' => '',
802            'F' => 'F',
803            'U' => 'NN',
804        ];
805
806        return 'person_box' . $tmp[$this->getSex()];
807    }
808
809    /**
810     * Get a list of this individual’s spouse families
811     *
812     * @param int|null $access_level
813     *
814     * @return Family[]
815     */
816    public function getSpouseFamilies($access_level = null)
817    {
818        if ($access_level === null) {
819            $access_level = Auth::accessLevel($this->tree);
820        }
821
822        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
823
824        $families = [];
825        foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
826            $family = $fact->getTarget();
827            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
828                $families[] = $family;
829            }
830        }
831
832        return $families;
833    }
834
835    /**
836     * Get the current spouse of this individual.
837     *
838     * Where an individual has multiple spouses, assume they are stored
839     * in chronological order, and take the last one found.
840     *
841     * @return Individual|null
842     */
843    public function getCurrentSpouse()
844    {
845        $tmp    = $this->getSpouseFamilies();
846        $family = end($tmp);
847        if ($family) {
848            return $family->getSpouse($this);
849        } else {
850            return null;
851        }
852    }
853
854    /**
855     * Count the children belonging to this individual.
856     *
857     * @return int
858     */
859    public function getNumberOfChildren()
860    {
861        if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
862            return $match[1];
863        } else {
864            $children = [];
865            foreach ($this->getSpouseFamilies() as $fam) {
866                foreach ($fam->getChildren() as $child) {
867                    $children[$child->getXref()] = true;
868                }
869            }
870
871            return count($children);
872        }
873    }
874
875    /**
876     * Get a list of this individual’s child families (i.e. their parents).
877     *
878     * @param int|null $access_level
879     *
880     * @return Family[]
881     */
882    public function getChildFamilies($access_level = null)
883    {
884        if ($access_level === null) {
885            $access_level = Auth::accessLevel($this->tree);
886        }
887
888        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
889
890        $families = [];
891        foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
892            $family = $fact->getTarget();
893            if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
894                $families[] = $family;
895            }
896        }
897
898        return $families;
899    }
900
901    /**
902     * Get the preferred parents for this individual.
903     *
904     * An individual may multiple parents (e.g. birth, adopted, disputed).
905     * The preferred family record is:
906     * (a) the first one with an explicit tag "_PRIMARY Y"
907     * (b) the first one with a pedigree of "birth"
908     * (c) the first one with no pedigree (default is "birth")
909     * (d) the first one found
910     *
911     * @return Family|null
912     */
913    public function getPrimaryChildFamily()
914    {
915        $families = $this->getChildFamilies();
916        switch (count($families)) {
917            case 0:
918                return null;
919            case 1:
920                return $families[0];
921            default:
922                // If there is more than one FAMC record, choose the preferred parents:
923                // a) records with '2 _PRIMARY'
924                foreach ($families as $fam) {
925                    $famid = $fam->getXref();
926                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
927                        return $fam;
928                    }
929                }
930                // b) records with '2 PEDI birt'
931                foreach ($families as $fam) {
932                    $famid = $fam->getXref();
933                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
934                        return $fam;
935                    }
936                }
937                // c) records with no '2 PEDI'
938                foreach ($families as $fam) {
939                    $famid = $fam->getXref();
940                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
941                        return $fam;
942                    }
943                }
944
945                // d) any record
946                return $families[0];
947        }
948    }
949
950    /**
951     * Get a list of step-parent families.
952     *
953     * @return Family[]
954     */
955    public function getChildStepFamilies()
956    {
957        $step_families = [];
958        $families      = $this->getChildFamilies();
959        foreach ($families as $family) {
960            $father = $family->getHusband();
961            if ($father) {
962                foreach ($father->getSpouseFamilies() as $step_family) {
963                    if (!in_array($step_family, $families, true)) {
964                        $step_families[] = $step_family;
965                    }
966                }
967            }
968            $mother = $family->getWife();
969            if ($mother) {
970                foreach ($mother->getSpouseFamilies() as $step_family) {
971                    if (!in_array($step_family, $families, true)) {
972                        $step_families[] = $step_family;
973                    }
974                }
975            }
976        }
977
978        return $step_families;
979    }
980
981    /**
982     * Get a list of step-parent families.
983     *
984     * @return Family[]
985     */
986    public function getSpouseStepFamilies()
987    {
988        $step_families = [];
989        $families      = $this->getSpouseFamilies();
990        foreach ($families as $family) {
991            $spouse = $family->getSpouse($this);
992            if ($spouse) {
993                foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
994                    if (!in_array($step_family, $families, true)) {
995                        $step_families[] = $step_family;
996                    }
997                }
998            }
999        }
1000
1001        return $step_families;
1002    }
1003
1004    /**
1005     * A label for a parental family group
1006     *
1007     * @param Family $family
1008     *
1009     * @return string
1010     */
1011    public function getChildFamilyLabel(Family $family)
1012    {
1013        if (preg_match('/\n1 FAMC @' . $family->getXref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
1014            // A specified pedigree
1015            return GedcomCodePedi::getChildFamilyLabel($match[1]);
1016        } else {
1017            // Default (birth) pedigree
1018            return GedcomCodePedi::getChildFamilyLabel('');
1019        }
1020    }
1021
1022    /**
1023     * Create a label for a step family
1024     *
1025     * @param Family $step_family
1026     *
1027     * @return string
1028     */
1029    public function getStepFamilyLabel(Family $step_family)
1030    {
1031        foreach ($this->getChildFamilies() as $family) {
1032            if ($family !== $step_family) {
1033                // Must be a step-family
1034                foreach ($family->getSpouses() as $parent) {
1035                    foreach ($step_family->getSpouses() as $step_parent) {
1036                        if ($parent === $step_parent) {
1037                            // One common parent - must be a step family
1038                            if ($parent->getSex() == 'M') {
1039                                // Father’s family with someone else
1040                                if ($step_family->getSpouse($step_parent)) {
1041                                    return
1042                                        /* I18N: A step-family. %s is an individual’s name */
1043                                        I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1044                                } else {
1045                                    return
1046                                        /* I18N: A step-family. */
1047                                        I18N::translate('Father’s family with an unknown individual');
1048                                }
1049                            } else {
1050                                // Mother’s family with someone else
1051                                if ($step_family->getSpouse($step_parent)) {
1052                                    return
1053                                        /* I18N: A step-family. %s is an individual’s name */
1054                                        I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1055                                } else {
1056                                    return
1057                                        /* I18N: A step-family. */
1058                                        I18N::translate('Mother’s family with an unknown individual');
1059                                }
1060                            }
1061                        }
1062                    }
1063                }
1064            }
1065        }
1066
1067        // Perahps same parents - but a different family record?
1068        return I18N::translate('Family with parents');
1069    }
1070
1071
1072    /**
1073     * Get the description for the family.
1074     *
1075     * For example, "XXX's family with new wife".
1076     *
1077     * @param Family $family
1078     *
1079     * @return string
1080     */
1081    public function getSpouseFamilyLabel(Family $family)
1082    {
1083        $spouse = $family->getSpouse($this);
1084        if ($spouse) {
1085            return
1086                /* I18N: %s is the spouse name */
1087                I18N::translate('Family with %s', $spouse->getFullName());
1088        } else {
1089            return $family->getFullName();
1090        }
1091    }
1092
1093    /**
1094     * get primary parents names for this individual
1095     *
1096     * @param string $classname optional css class
1097     * @param string $display   optional css style display
1098     *
1099     * @return string a div block with father & mother names
1100     */
1101    public function getPrimaryParentsNames($classname = '', $display = '')
1102    {
1103        $fam = $this->getPrimaryChildFamily();
1104        if (!$fam) {
1105            return '';
1106        }
1107        $txt = '<div';
1108        if ($classname) {
1109            $txt .= ' class="' . $classname . '"';
1110        }
1111        if ($display) {
1112            $txt .= ' style="display:' . $display . '"';
1113        }
1114        $txt  .= '>';
1115        $husb = $fam->getHusband();
1116        if ($husb) {
1117            // Temporarily reset the 'prefered' display name, as we always
1118            // want the default name, not the one selected for display on the indilist.
1119            $primary = $husb->getPrimaryName();
1120            $husb->setPrimaryName(null);
1121            $txt .=
1122                /* I18N: %s is the name of an individual’s father */
1123                I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1124            $husb->setPrimaryName($primary);
1125        }
1126        $wife = $fam->getWife();
1127        if ($wife) {
1128            // Temporarily reset the 'prefered' display name, as we always
1129            // want the default name, not the one selected for display on the indilist.
1130            $primary = $wife->getPrimaryName();
1131            $wife->setPrimaryName(null);
1132            $txt .=
1133                /* I18N: %s is the name of an individual’s mother */
1134                I18N::translate('Mother: %s', $wife->getFullName());
1135            $wife->setPrimaryName($primary);
1136        }
1137        $txt .= '</div>';
1138
1139        return $txt;
1140    }
1141
1142    /** {@inheritdoc} */
1143    public function getFallBackName()
1144    {
1145        return '@P.N. /@N.N./';
1146    }
1147
1148    /**
1149     * Convert a name record into ‘full’ and ‘sort’ versions.
1150     * Use the NAME field to generate the ‘full’ version, as the
1151     * gedcom spec says that this is the individual’s name, as they would write it.
1152     * Use the SURN field to generate the sortable names. Note that this field
1153     * may also be used for the ‘true’ surname, perhaps spelt differently to that
1154     * recorded in the NAME field. e.g.
1155     *
1156     * 1 NAME Robert /de Gliderow/
1157     * 2 GIVN Robert
1158     * 2 SPFX de
1159     * 2 SURN CLITHEROW
1160     * 2 NICK The Bald
1161     *
1162     * full=>'Robert de Gliderow 'The Bald''
1163     * sort=>'CLITHEROW, ROBERT'
1164     *
1165     * Handle multiple surnames, either as;
1166     *
1167     * 1 NAME Carlos /Vasquez/ y /Sante/
1168     * or
1169     * 1 NAME Carlos /Vasquez y Sante/
1170     * 2 GIVN Carlos
1171     * 2 SURN Vasquez,Sante
1172     *
1173     * @param string $type
1174     * @param string $full
1175     * @param string $gedcom
1176     */
1177    protected function addName($type, $full, $gedcom)
1178    {
1179        ////////////////////////////////////////////////////////////////////////////
1180        // Extract the structured name parts - use for "sortable" names and indexes
1181        ////////////////////////////////////////////////////////////////////////////
1182
1183        $sublevel = 1 + (int)$gedcom[0];
1184        $NPFX     = preg_match("/\n{$sublevel} NPFX (.+)/", $gedcom, $match) ? $match[1] : '';
1185        $GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1186        $SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1187        $NSFX     = preg_match("/\n{$sublevel} NSFX (.+)/", $gedcom, $match) ? $match[1] : '';
1188        $NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1189
1190        // SURN is an comma-separated list of surnames...
1191        if ($SURN) {
1192            $SURNS = preg_split('/ *, */', $SURN);
1193        } else {
1194            $SURNS = [];
1195        }
1196        // ...so is GIVN - but nobody uses it like that
1197        $GIVN = str_replace('/ *, */', ' ', $GIVN);
1198
1199        ////////////////////////////////////////////////////////////////////////////
1200        // Extract the components from NAME - use for the "full" names
1201        ////////////////////////////////////////////////////////////////////////////
1202
1203        // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
1204        if (substr_count($full, '/') % 2 == 1) {
1205            $full = $full . '/';
1206        }
1207
1208        // GEDCOM uses "//" to indicate an unknown surname
1209        $full = preg_replace('/\/\//', '/@N.N./', $full);
1210
1211        // Extract the surname.
1212        // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1213        if (preg_match('/\/.*\//', $full, $match)) {
1214            $surname = str_replace('/', '', $match[0]);
1215        } else {
1216            $surname = '';
1217        }
1218
1219        // If we don’t have a SURN record, extract it from the NAME
1220        if (!$SURNS) {
1221            if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1222                // There can be many surnames, each wrapped with '/'
1223                $SURNS = $matches[1];
1224                foreach ($SURNS as $n => $SURN) {
1225                    // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1226                    $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1227                }
1228            } else {
1229                // It is valid not to have a surname at all
1230                $SURNS = [''];
1231            }
1232        }
1233
1234        // If we don’t have a GIVN record, extract it from the NAME
1235        if (!$GIVN) {
1236            $GIVN = preg_replace(
1237                [
1238                    '/ ?\/.*\/ ?/',
1239                    // remove surname
1240                    '/ ?".+"/',
1241                    // remove nickname
1242                    '/ {2,}/',
1243                    // multiple spaces, caused by the above
1244                    '/^ | $/',
1245                    // leading/trailing spaces, caused by the above
1246                ],
1247                [
1248                    ' ',
1249                    ' ',
1250                    ' ',
1251                    '',
1252                ],
1253                $full
1254            );
1255        }
1256
1257        // Add placeholder for unknown given name
1258        if (!$GIVN) {
1259            $GIVN = '@P.N.';
1260            $pos  = strpos($full, '/');
1261            $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1262        }
1263
1264        // GEDCOM 5.5.1 nicknames should be specificied in a NICK field
1265        // GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1266        if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
1267            // A NICK field is present, but not included in the NAME.  Show it at the end.
1268            $full .= ' "' . $NICK . '"';
1269        }
1270
1271        // Remove slashes - they don’t get displayed
1272        // $fullNN keeps the @N.N. placeholders, for the database
1273        // $full is for display on-screen
1274        $fullNN = str_replace('/', '', $full);
1275
1276        // Insert placeholders for any missing/unknown names
1277        $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1278        $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1279        // Format for display
1280        $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1281        // Localise quotation marks around the nickname
1282        $full = preg_replace_callback('/&quot;([^&]*)&quot;/', function ($matches) {
1283            return I18N::translate('“%s”', $matches[1]);
1284        }, $full);
1285
1286        // A suffix of “*” indicates a preferred name
1287        $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1288
1289        // Remove prefered-name indicater - they don’t go in the database
1290        $GIVN   = str_replace('*', '', $GIVN);
1291        $fullNN = str_replace('*', '', $fullNN);
1292
1293        foreach ($SURNS as $SURN) {
1294            // Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1295            if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1296                $SURN = substr_replace($SURN, 'Mac', 0, 2);
1297            } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1298                $SURN = substr_replace($SURN, 'Mac', 0, 4);
1299            }
1300
1301            $this->_getAllNames[] = [
1302                'type'    => $type,
1303                'sort'    => $SURN . ',' . $GIVN,
1304                'full'    => $full,
1305                // This is used for display
1306                'fullNN'  => $fullNN,
1307                // This goes into the database
1308                'surname' => $surname,
1309                // This goes into the database
1310                'givn'    => $GIVN,
1311                // This goes into the database
1312                'surn'    => $SURN,
1313                // This goes into the database
1314            ];
1315        }
1316    }
1317
1318    /**
1319     * Extract names from the GEDCOM record.
1320     */
1321    public function extractNames()
1322    {
1323        $this->extractNamesFromFacts(1, 'NAME', $this->getFacts('NAME', false, Auth::accessLevel($this->tree), $this->canShowName()));
1324    }
1325
1326    /**
1327     * Extra info to display when displaying this record in a list of
1328     * selection items or favorites.
1329     *
1330     * @return string
1331     */
1332    public function formatListDetails()
1333    {
1334        return
1335            $this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1336            $this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1337    }
1338}
1339