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