1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 51fe542e9SGreg Roach * Copyright (C) 2021 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 22886b77daSGreg Roachuse Closure; 23a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar; 241fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 25852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 262e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2739ca88baSGreg Roachuse Illuminate\Support\Collection; 28a25f0a04SGreg Roach 297d70e4a7SGreg Roachuse function preg_match; 307d70e4a7SGreg Roach 31a25f0a04SGreg Roach/** 3276692c8bSGreg Roach * A GEDCOM individual (INDI) object. 33a25f0a04SGreg Roach */ 34c1010edaSGreg Roachclass Individual extends GedcomRecord 35c1010edaSGreg Roach{ 3616d6367aSGreg Roach public const RECORD_TYPE = 'INDI'; 3716d6367aSGreg Roach 388fb4e87cSGreg Roach // Placeholders to indicate unknown names 398fb4e87cSGreg Roach public const NOMEN_NESCIO = '@N.N.'; 408fb4e87cSGreg Roach public const PRAENOMEN_NESCIO = '@P.N.'; 418fb4e87cSGreg Roach 42852ede8cSGreg Roach protected const ROUTE_NAME = IndividualPage::class; 43a25f0a04SGreg Roach 4476692c8bSGreg Roach /** @var int used in some lists to keep track of this individual’s generation in that list */ 4576692c8bSGreg Roach public $generation; 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** @var Date The estimated date of birth */ 484686330aSGreg Roach private $estimated_birth_date; 49a25f0a04SGreg Roach 50a25f0a04SGreg Roach /** @var Date The estimated date of death */ 514686330aSGreg Roach private $estimated_death_date; 52a25f0a04SGreg Roach 53a25f0a04SGreg Roach /** 54c156e8f5SGreg Roach * A closure which will compare individuals by birth date. 55c156e8f5SGreg Roach * 56c156e8f5SGreg Roach * @return Closure 57c156e8f5SGreg Roach */ 58c156e8f5SGreg Roach public static function birthDateComparator(): Closure 59c156e8f5SGreg Roach { 606c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 61c156e8f5SGreg Roach return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 62c156e8f5SGreg Roach }; 63c156e8f5SGreg Roach } 64c156e8f5SGreg Roach 65c156e8f5SGreg Roach /** 66c156e8f5SGreg Roach * A closure which will compare individuals by death date. 67c156e8f5SGreg Roach * 68c156e8f5SGreg Roach * @return Closure 69c156e8f5SGreg Roach */ 70c156e8f5SGreg Roach public static function deathDateComparator(): Closure 71c156e8f5SGreg Roach { 726c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 7310e21aa9SGreg Roach return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate()); 74c156e8f5SGreg Roach }; 75c156e8f5SGreg Roach } 76c156e8f5SGreg Roach 77c156e8f5SGreg Roach /** 78395f0fe0SGreg Roach * Sometimes, we'll know in advance that we need to load a set of records. 79395f0fe0SGreg Roach * Typically when we load families and their members. 80395f0fe0SGreg Roach * 81395f0fe0SGreg Roach * @param Tree $tree 824a8aaa00SScrutinizer Auto-Fixer * @param string[] $xrefs 8318d7a90dSGreg Roach * 8418d7a90dSGreg Roach * @return void 85395f0fe0SGreg Roach */ 862e5b4452SGreg Roach public static function load(Tree $tree, array $xrefs): void 87c1010edaSGreg Roach { 882e5b4452SGreg Roach $rows = DB::table('individuals') 892e5b4452SGreg Roach ->where('i_file', '=', $tree->id()) 902e5b4452SGreg Roach ->whereIn('i_id', array_unique($xrefs)) 912e5b4452SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom']) 922e5b4452SGreg Roach ->get(); 93395f0fe0SGreg Roach 94395f0fe0SGreg Roach foreach ($rows as $row) { 956b9cb339SGreg Roach Registry::individualFactory()->make($row->xref, $tree, $row->gedcom); 96395f0fe0SGreg Roach } 97395f0fe0SGreg Roach } 98395f0fe0SGreg Roach 99395f0fe0SGreg Roach /** 100a25f0a04SGreg Roach * Can the name of this record be shown? 101a25f0a04SGreg Roach * 10276692c8bSGreg Roach * @param int|null $access_level 10376692c8bSGreg Roach * 10476692c8bSGreg Roach * @return bool 105a25f0a04SGreg Roach */ 10635584196SGreg Roach public function canShowName(int $access_level = null): bool 107c1010edaSGreg Roach { 108d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 1094b9ff166SGreg Roach 110518bbdc1SGreg Roach return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 111a25f0a04SGreg Roach } 112a25f0a04SGreg Roach 113a25f0a04SGreg Roach /** 11476692c8bSGreg Roach * Can this individual be shown? 115a25f0a04SGreg Roach * 11676692c8bSGreg Roach * @param int $access_level 11776692c8bSGreg Roach * 11876692c8bSGreg Roach * @return bool 119a25f0a04SGreg Roach */ 12035584196SGreg Roach protected function canShowByType(int $access_level): bool 121c1010edaSGreg Roach { 122a25f0a04SGreg Roach // Dead people... 123518bbdc1SGreg Roach if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 124a25f0a04SGreg Roach $keep_alive = false; 12554ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 126a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH) { 1278d0ebef0SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER); 128a25f0a04SGreg Roach foreach ($matches as $match) { 129a25f0a04SGreg Roach $date = new Date($match[1]); 130a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 131a25f0a04SGreg Roach $keep_alive = true; 132a25f0a04SGreg Roach break; 133a25f0a04SGreg Roach } 134a25f0a04SGreg Roach } 135a25f0a04SGreg Roach } 13654ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 137a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_DEATH) { 1388d0ebef0SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER); 139a25f0a04SGreg Roach foreach ($matches as $match) { 140a25f0a04SGreg Roach $date = new Date($match[1]); 141a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 142a25f0a04SGreg Roach $keep_alive = true; 143a25f0a04SGreg Roach break; 144a25f0a04SGreg Roach } 145a25f0a04SGreg Roach } 146a25f0a04SGreg Roach } 147a25f0a04SGreg Roach if (!$keep_alive) { 148a25f0a04SGreg Roach return true; 149a25f0a04SGreg Roach } 150a25f0a04SGreg Roach } 151a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 1521fe542e9SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH); 1531fe542e9SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 1547c4add84SGreg Roach 15554ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1564b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 157a25f0a04SGreg Roach } 158a25f0a04SGreg Roach 159a25f0a04SGreg Roach // No restriction found - show living people to members only: 1604b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 161a25f0a04SGreg Roach } 162a25f0a04SGreg Roach 163a25f0a04SGreg Roach /** 164a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 165a25f0a04SGreg Roach * 166a25f0a04SGreg Roach * @param Individual $target 167cbc1590aSGreg Roach * @param int $distance 168a25f0a04SGreg Roach * 169cbc1590aSGreg Roach * @return bool 170a25f0a04SGreg Roach */ 17124f2a3afSGreg Roach private static function isRelated(Individual $target, int $distance): bool 172c1010edaSGreg Roach { 173a25f0a04SGreg Roach static $cache = null; 174a25f0a04SGreg Roach 1751fe542e9SGreg Roach $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree); 176a25f0a04SGreg Roach if ($user_individual) { 177a25f0a04SGreg Roach if (!$cache) { 17813abd6f3SGreg Roach $cache = [ 17913abd6f3SGreg Roach 0 => [$user_individual], 18013abd6f3SGreg Roach 1 => [], 18113abd6f3SGreg Roach ]; 1828d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 183dc124885SGreg Roach $family = $fact->target(); 184e24444eeSGreg Roach if ($family instanceof Family) { 185a25f0a04SGreg Roach $cache[1][] = $family; 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach } 188a25f0a04SGreg Roach } 189a25f0a04SGreg Roach } else { 190a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 191a25f0a04SGreg Roach return true; 192a25f0a04SGreg Roach } 193a25f0a04SGreg Roach 194a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 195a25f0a04SGreg Roach $distance *= 2; 196a25f0a04SGreg Roach 197a25f0a04SGreg Roach // Consider each path length in turn 198a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 199a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 200a25f0a04SGreg Roach // We have already calculated all records with this length 201e364afe4SGreg Roach if ($n % 2 === 0 && in_array($target, $cache[$n], true)) { 202a25f0a04SGreg Roach return true; 203a25f0a04SGreg Roach } 204a25f0a04SGreg Roach } else { 205a25f0a04SGreg Roach // Need to calculate these paths 20613abd6f3SGreg Roach $cache[$n] = []; 207e364afe4SGreg Roach if ($n % 2 === 0) { 208a25f0a04SGreg Roach // Add FAM->INDI links 209a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 2108d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 211dc124885SGreg Roach $individual = $fact->target(); 212a25f0a04SGreg Roach // Don’t backtrack 213e364afe4SGreg Roach if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) { 214a25f0a04SGreg Roach $cache[$n][] = $individual; 215a25f0a04SGreg Roach } 216a25f0a04SGreg Roach } 217a25f0a04SGreg Roach } 218a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 219a25f0a04SGreg Roach return true; 220a25f0a04SGreg Roach } 221a25f0a04SGreg Roach } else { 222a25f0a04SGreg Roach // Add INDI->FAM links 223a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 2248d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 225dc124885SGreg Roach $family = $fact->target(); 226a25f0a04SGreg Roach // Don’t backtrack 227e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 228a25f0a04SGreg Roach $cache[$n][] = $family; 229a25f0a04SGreg Roach } 230a25f0a04SGreg Roach } 231a25f0a04SGreg Roach } 232a25f0a04SGreg Roach } 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach 236a25f0a04SGreg Roach return false; 237a25f0a04SGreg Roach } 238a25f0a04SGreg Roach 23976692c8bSGreg Roach /** 24076692c8bSGreg Roach * Generate a private version of this record 24176692c8bSGreg Roach * 24276692c8bSGreg Roach * @param int $access_level 24376692c8bSGreg Roach * 24476692c8bSGreg Roach * @return string 24576692c8bSGreg Roach */ 2463c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 247c1010edaSGreg Roach { 248acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 249a25f0a04SGreg Roach 250a25f0a04SGreg Roach $rec = '0 @' . $this->xref . '@ INDI'; 251518bbdc1SGreg Roach if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) { 252a25f0a04SGreg Roach // Show all the NAME tags, including subtags 2538d0ebef0SGreg Roach foreach ($this->facts(['NAME']) as $fact) { 254138ca96cSGreg Roach $rec .= "\n" . $fact->gedcom(); 255a25f0a04SGreg Roach } 256a25f0a04SGreg Roach } 257a25f0a04SGreg Roach // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data 2588d0ebef0SGreg Roach preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 259a25f0a04SGreg Roach foreach ($matches as $match) { 2606b9cb339SGreg Roach $rela = Registry::familyFactory()->make($match[1], $this->tree); 261a25f0a04SGreg Roach if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 262a25f0a04SGreg Roach $rec .= $match[0]; 263a25f0a04SGreg Roach } 264a25f0a04SGreg Roach } 265a25f0a04SGreg Roach // Don’t privatize sex. 266a25f0a04SGreg Roach if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) { 267a25f0a04SGreg Roach $rec .= $match[0]; 268a25f0a04SGreg Roach } 269a25f0a04SGreg Roach 270a25f0a04SGreg Roach return $rec; 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach 27376692c8bSGreg Roach /** 274a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 275a25f0a04SGreg Roach * If not known to be dead, then assume living. 276a25f0a04SGreg Roach * 277cbc1590aSGreg Roach * @return bool 278a25f0a04SGreg Roach */ 2798f53f488SRico Sonntag public function isDead(): bool 280c1010edaSGreg Roach { 281c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 2824459dc9aSGreg Roach $today_jd = Carbon::now()->julianDay(); 283a25f0a04SGreg Roach 284a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 2858d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 286a25f0a04SGreg Roach return true; 287a25f0a04SGreg Roach } 288a25f0a04SGreg Roach 289a25f0a04SGreg Roach // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 290a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 291a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 292a25f0a04SGreg Roach $date = new Date($date_match); 293269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) { 294a25f0a04SGreg Roach return true; 295a25f0a04SGreg Roach } 296a25f0a04SGreg Roach } 297a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 298a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 299a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 300a25f0a04SGreg Roach return false; 301a25f0a04SGreg Roach } 302a25f0a04SGreg Roach } 303a25f0a04SGreg Roach 304a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 305a25f0a04SGreg Roach 306a25f0a04SGreg Roach // Check parents (birth and adopted) 30739ca88baSGreg Roach foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) { 30839ca88baSGreg Roach foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) { 309a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 310a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 311a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 312a25f0a04SGreg Roach $date = new Date($date_match); 313269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) { 314a25f0a04SGreg Roach return true; 315a25f0a04SGreg Roach } 316a25f0a04SGreg Roach } 317a25f0a04SGreg Roach } 318a25f0a04SGreg Roach } 319a25f0a04SGreg Roach 320a25f0a04SGreg Roach // Check spouses 32139ca88baSGreg Roach foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) { 322a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 323a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 324a25f0a04SGreg Roach $date = new Date($date_match); 325a25f0a04SGreg Roach // Assume marriage occurs after age of 10 326269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) { 327a25f0a04SGreg Roach return true; 328a25f0a04SGreg Roach } 329a25f0a04SGreg Roach } 330a25f0a04SGreg Roach // Check spouse dates 33139ca88baSGreg Roach $spouse = $family->spouse($this, Auth::PRIV_HIDE); 332a25f0a04SGreg Roach if ($spouse) { 333a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 334a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 335a25f0a04SGreg Roach $date = new Date($date_match); 336a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 337269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) { 338a25f0a04SGreg Roach return true; 339a25f0a04SGreg Roach } 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach } 342a25f0a04SGreg Roach // Check child dates 34339ca88baSGreg Roach foreach ($family->children(Auth::PRIV_HIDE) as $child) { 344a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 345a25f0a04SGreg Roach // Assume children born after age of 15 346a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 347a25f0a04SGreg Roach $date = new Date($date_match); 348269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) { 349a25f0a04SGreg Roach return true; 350a25f0a04SGreg Roach } 351a25f0a04SGreg Roach } 352a25f0a04SGreg Roach // Check grandchildren 35339ca88baSGreg Roach foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) { 35439ca88baSGreg Roach foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) { 355a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 356a25f0a04SGreg Roach // Assume grandchildren born after age of 30 357a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 358a25f0a04SGreg Roach $date = new Date($date_match); 359269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) { 360a25f0a04SGreg Roach return true; 361a25f0a04SGreg Roach } 362a25f0a04SGreg Roach } 363a25f0a04SGreg Roach } 364a25f0a04SGreg Roach } 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach } 367a25f0a04SGreg Roach 368a25f0a04SGreg Roach return false; 369a25f0a04SGreg Roach } 370a25f0a04SGreg Roach 371a25f0a04SGreg Roach /** 372a25f0a04SGreg Roach * Find the highlighted media object for an individual 373a25f0a04SGreg Roach * 374e364afe4SGreg Roach * @return MediaFile|null 375a25f0a04SGreg Roach */ 376e364afe4SGreg Roach public function findHighlightedMediaFile(): ?MediaFile 377c1010edaSGreg Roach { 37892647683SGreg Roach $fact = $this->facts(['OBJE']) 37919d319e7SGreg Roach ->first(static function (Fact $fact): bool { 380dc124885SGreg Roach $media = $fact->target(); 38119d319e7SGreg Roach 38219d319e7SGreg Roach return $media instanceof Media && $media->firstImageFile() instanceof MediaFile; 38319d319e7SGreg Roach }); 38419d319e7SGreg Roach 38592647683SGreg Roach if ($fact instanceof Fact && $fact->target() instanceof Media) { 38692647683SGreg Roach return $fact->target()->firstImageFile(); 387a25f0a04SGreg Roach } 388a25f0a04SGreg Roach 389a25f0a04SGreg Roach return null; 390a25f0a04SGreg Roach } 391a25f0a04SGreg Roach 392a25f0a04SGreg Roach /** 393a25f0a04SGreg Roach * Display the prefered image for this individual. 394a25f0a04SGreg Roach * Use an icon if no image is available. 395a25f0a04SGreg Roach * 396dce07401SGreg Roach * @param int $width Pixels 397dce07401SGreg Roach * @param int $height Pixels 398dce07401SGreg Roach * @param string $fit "crop" or "contain" 399dce07401SGreg Roach * @param string[] $attributes Additional HTML attributes 400dce07401SGreg Roach * 401a25f0a04SGreg Roach * @return string 402a25f0a04SGreg Roach */ 40324f2a3afSGreg Roach public function displayImage(int $width, int $height, string $fit, array $attributes): string 404c1010edaSGreg Roach { 4054a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 4064a9f750fSGreg Roach 4074a9f750fSGreg Roach if ($media_file !== null) { 4084a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 409a25f0a04SGreg Roach } 4104a9f750fSGreg Roach 4114a9f750fSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE')) { 41239ca88baSGreg Roach return '<i class="icon-silhouette-' . $this->sex() . '"></i>'; 4134a9f750fSGreg Roach } 4144a9f750fSGreg Roach 4154a9f750fSGreg Roach return ''; 416a25f0a04SGreg Roach } 417a25f0a04SGreg Roach 418a25f0a04SGreg Roach /** 419a25f0a04SGreg Roach * Get the date of birth 420a25f0a04SGreg Roach * 421a25f0a04SGreg Roach * @return Date 422a25f0a04SGreg Roach */ 4238f53f488SRico Sonntag public function getBirthDate(): Date 424c1010edaSGreg Roach { 425a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 426a25f0a04SGreg Roach if ($date->isOK()) { 427a25f0a04SGreg Roach return $date; 428a25f0a04SGreg Roach } 429a25f0a04SGreg Roach } 430a25f0a04SGreg Roach 431a25f0a04SGreg Roach return new Date(''); 432a25f0a04SGreg Roach } 433a25f0a04SGreg Roach 434a25f0a04SGreg Roach /** 435a25f0a04SGreg Roach * Get the place of birth 436a25f0a04SGreg Roach * 43716d0b7f7SRico Sonntag * @return Place 438a25f0a04SGreg Roach */ 4398f53f488SRico Sonntag public function getBirthPlace(): Place 440c1010edaSGreg Roach { 441a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 442a25f0a04SGreg Roach return $place; 443a25f0a04SGreg Roach } 444a25f0a04SGreg Roach 445b20ddbf9SGreg Roach return new Place('', $this->tree); 446a25f0a04SGreg Roach } 447a25f0a04SGreg Roach 448a25f0a04SGreg Roach /** 449a25f0a04SGreg Roach * Get the date of death 450a25f0a04SGreg Roach * 451a25f0a04SGreg Roach * @return Date 452a25f0a04SGreg Roach */ 4538f53f488SRico Sonntag public function getDeathDate(): Date 454c1010edaSGreg Roach { 455a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 456a25f0a04SGreg Roach if ($date->isOK()) { 457a25f0a04SGreg Roach return $date; 458a25f0a04SGreg Roach } 459a25f0a04SGreg Roach } 460a25f0a04SGreg Roach 461a25f0a04SGreg Roach return new Date(''); 462a25f0a04SGreg Roach } 463a25f0a04SGreg Roach 464a25f0a04SGreg Roach /** 465a25f0a04SGreg Roach * Get the place of death 466a25f0a04SGreg Roach * 46716d0b7f7SRico Sonntag * @return Place 468a25f0a04SGreg Roach */ 4698f53f488SRico Sonntag public function getDeathPlace(): Place 470c1010edaSGreg Roach { 471a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 472a25f0a04SGreg Roach return $place; 473a25f0a04SGreg Roach } 474a25f0a04SGreg Roach 475b20ddbf9SGreg Roach return new Place('', $this->tree); 476a25f0a04SGreg Roach } 477a25f0a04SGreg Roach 478a25f0a04SGreg Roach /** 479a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 48015d603e7SGreg Roach * Provide the place and full date using a tooltip. 481a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 482a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 483a25f0a04SGreg Roach * 484a25f0a04SGreg Roach * @return string 485a25f0a04SGreg Roach */ 4865e6816beSGreg Roach public function lifespan(): string 487c1010edaSGreg Roach { 48853f5ca04SGreg Roach // Just the first part of the place name. 489392561bbSGreg Roach $birth_place = strip_tags($this->getBirthPlace()->shortName()); 490392561bbSGreg Roach $death_place = strip_tags($this->getDeathPlace()->shortName()); 49153f5ca04SGreg Roach 49253f5ca04SGreg Roach // Remove markup from dates. 49315d603e7SGreg Roach $birth_date = strip_tags($this->getBirthDate()->display()); 49415d603e7SGreg Roach $death_date = strip_tags($this->getDeathDate()->display()); 49515d603e7SGreg Roach 49653f5ca04SGreg Roach // Use minimum and maximum dates - to agree with the age calculations. 49753f5ca04SGreg Roach $birth_year = $this->getBirthDate()->minimumDate()->format('%Y'); 49853f5ca04SGreg Roach $death_year = $this->getDeathDate()->maximumDate()->format('%Y'); 49953f5ca04SGreg Roach 500c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 501dacfe33cSGreg Roach return I18N::translate( 502a25f0a04SGreg Roach '%1$s–%2$s', 50353f5ca04SGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $birth_year . '</span>', 50453f5ca04SGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $death_year . '</span>' 505a25f0a04SGreg Roach ); 506a25f0a04SGreg Roach } 507a25f0a04SGreg Roach 508a25f0a04SGreg Roach /** 509a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 510a25f0a04SGreg Roach * 511a25f0a04SGreg Roach * @return Date[] 512a25f0a04SGreg Roach */ 5138f53f488SRico Sonntag public function getAllBirthDates(): array 514c1010edaSGreg Roach { 5158d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 516d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 517d240bb87SGreg Roach 518d240bb87SGreg Roach if ($dates !== []) { 519d240bb87SGreg Roach return $dates; 520a25f0a04SGreg Roach } 521a25f0a04SGreg Roach } 522a25f0a04SGreg Roach 52313abd6f3SGreg Roach return []; 524a25f0a04SGreg Roach } 525a25f0a04SGreg Roach 526a25f0a04SGreg Roach /** 527a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 528a25f0a04SGreg Roach * 5294080d558SGreg Roach * @return Place[] 530a25f0a04SGreg Roach */ 5318f53f488SRico Sonntag public function getAllBirthPlaces(): array 532c1010edaSGreg Roach { 5338d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 5348d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 535d240bb87SGreg Roach 53654c1ab5eSGreg Roach if ($places !== []) { 5374080d558SGreg Roach return $places; 538a25f0a04SGreg Roach } 539a25f0a04SGreg Roach } 540a25f0a04SGreg Roach 54113abd6f3SGreg Roach return []; 542a25f0a04SGreg Roach } 543a25f0a04SGreg Roach 544a25f0a04SGreg Roach /** 545a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 546a25f0a04SGreg Roach * 547a25f0a04SGreg Roach * @return Date[] 548a25f0a04SGreg Roach */ 5498f53f488SRico Sonntag public function getAllDeathDates(): array 550c1010edaSGreg Roach { 5518d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 552d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 553d240bb87SGreg Roach 554d240bb87SGreg Roach if ($dates !== []) { 555d240bb87SGreg Roach return $dates; 556a25f0a04SGreg Roach } 557a25f0a04SGreg Roach } 558a25f0a04SGreg Roach 55913abd6f3SGreg Roach return []; 560a25f0a04SGreg Roach } 561a25f0a04SGreg Roach 562a25f0a04SGreg Roach /** 563a25f0a04SGreg Roach * Get all the death places - for the individual lists. 564a25f0a04SGreg Roach * 5654080d558SGreg Roach * @return Place[] 566a25f0a04SGreg Roach */ 5678f53f488SRico Sonntag public function getAllDeathPlaces(): array 568c1010edaSGreg Roach { 5698d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 5708d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 571d240bb87SGreg Roach 57254c1ab5eSGreg Roach if ($places !== []) { 5734080d558SGreg Roach return $places; 574a25f0a04SGreg Roach } 575a25f0a04SGreg Roach } 576a25f0a04SGreg Roach 57713abd6f3SGreg Roach return []; 578a25f0a04SGreg Roach } 579a25f0a04SGreg Roach 580a25f0a04SGreg Roach /** 581a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 582a25f0a04SGreg Roach * 583a25f0a04SGreg Roach * @return Date 584a25f0a04SGreg Roach */ 5858f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 586c1010edaSGreg Roach { 5878f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 588a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 589a25f0a04SGreg Roach if ($date->isOK()) { 5904686330aSGreg Roach $this->estimated_birth_date = $date; 591a25f0a04SGreg Roach break; 592a25f0a04SGreg Roach } 593a25f0a04SGreg Roach } 5948f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 59513abd6f3SGreg Roach $min = []; 59613abd6f3SGreg Roach $max = []; 597a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 598f5b60decSGreg Roach if ($tmp->isOK()) { 599f5b60decSGreg Roach $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365; 600f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 601a25f0a04SGreg Roach } 60239ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 603a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 604f5b60decSGreg Roach if ($tmp->isOK()) { 605f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 1; 606f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 607a25f0a04SGreg Roach } 60839ca88baSGreg Roach $husband = $family->husband(); 609e364afe4SGreg Roach if ($husband instanceof self) { 6102e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 611f5b60decSGreg Roach if ($tmp->isOK()) { 612f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 613f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 614a25f0a04SGreg Roach } 615a25f0a04SGreg Roach } 61639ca88baSGreg Roach $wife = $family->wife(); 617e364afe4SGreg Roach if ($wife instanceof self) { 6182e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 619f5b60decSGreg Roach if ($tmp->isOK()) { 620f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 621f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 622a25f0a04SGreg Roach } 623a25f0a04SGreg Roach } 62439ca88baSGreg Roach foreach ($family->children() as $child) { 625a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 626f5b60decSGreg Roach if ($tmp->isOK()) { 627f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 628f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 629a25f0a04SGreg Roach } 630a25f0a04SGreg Roach } 631a25f0a04SGreg Roach } 63239ca88baSGreg Roach foreach ($this->spouseFamilies() as $family) { 633a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 634f5b60decSGreg Roach if ($tmp->isOK()) { 635f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 636f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 637a25f0a04SGreg Roach } 63839ca88baSGreg Roach $spouse = $family->spouse($this); 639a25f0a04SGreg Roach if ($spouse) { 640a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 641f5b60decSGreg Roach if ($tmp->isOK()) { 642f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 643f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 644a25f0a04SGreg Roach } 645a25f0a04SGreg Roach } 64639ca88baSGreg Roach foreach ($family->children() as $child) { 647a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 648f5b60decSGreg Roach if ($tmp->isOK()) { 649e364afe4SGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65); 650f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 651a25f0a04SGreg Roach } 652a25f0a04SGreg Roach } 653a25f0a04SGreg Roach } 654a25f0a04SGreg Roach if ($min && $max) { 65559f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 656a25f0a04SGreg Roach 65765e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 6584686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 659a25f0a04SGreg Roach } else { 6604686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 661a25f0a04SGreg Roach } 662a25f0a04SGreg Roach } 663a25f0a04SGreg Roach } 664a25f0a04SGreg Roach 6654686330aSGreg Roach return $this->estimated_birth_date; 666a25f0a04SGreg Roach } 667a25f0a04SGreg Roach 668a25f0a04SGreg Roach /** 669a25f0a04SGreg Roach * Generate an estimated date of death. 670a25f0a04SGreg Roach * 671a25f0a04SGreg Roach * @return Date 672a25f0a04SGreg Roach */ 6738f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 674c1010edaSGreg Roach { 6754686330aSGreg Roach if ($this->estimated_death_date === null) { 676a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 677a25f0a04SGreg Roach if ($date->isOK()) { 6784686330aSGreg Roach $this->estimated_death_date = $date; 679a25f0a04SGreg Roach break; 680a25f0a04SGreg Roach } 681a25f0a04SGreg Roach } 6824686330aSGreg Roach if ($this->estimated_death_date === null) { 683f5b60decSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay()) { 684c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 6854686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 686a25f0a04SGreg Roach } else { 6874686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 688a25f0a04SGreg Roach } 689a25f0a04SGreg Roach } 690a25f0a04SGreg Roach } 691a25f0a04SGreg Roach 6924686330aSGreg Roach return $this->estimated_death_date; 693a25f0a04SGreg Roach } 694a25f0a04SGreg Roach 695a25f0a04SGreg Roach /** 696a25f0a04SGreg Roach * Get the sex - M F or U 697a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 698a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 699a25f0a04SGreg Roach * 700a25f0a04SGreg Roach * @return string 701a25f0a04SGreg Roach */ 702e364afe4SGreg Roach public function sex(): string 703c1010edaSGreg Roach { 704a25f0a04SGreg Roach if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) { 705a25f0a04SGreg Roach return $match[1]; 706a25f0a04SGreg Roach } 707b2ce94c6SRico Sonntag 708b2ce94c6SRico Sonntag return 'U'; 709a25f0a04SGreg Roach } 710a25f0a04SGreg Roach 711a25f0a04SGreg Roach /** 712a25f0a04SGreg Roach * Get a list of this individual’s spouse families 713a25f0a04SGreg Roach * 714cbc1590aSGreg Roach * @param int|null $access_level 715a25f0a04SGreg Roach * 716b5c8fd7eSGreg Roach * @return Collection<Family> 717a25f0a04SGreg Roach */ 71839ca88baSGreg Roach public function spouseFamilies($access_level = null): Collection 719c1010edaSGreg Roach { 720d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 721d9e083e7SGreg Roach 722d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 723d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 7244b9ff166SGreg Roach } 7254b9ff166SGreg Roach 72639ca88baSGreg Roach $families = new Collection(); 727d9e083e7SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level) as $fact) { 728dc124885SGreg Roach $family = $fact->target(); 729d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 73039ca88baSGreg Roach $families->push($family); 731a25f0a04SGreg Roach } 732a25f0a04SGreg Roach } 733a25f0a04SGreg Roach 73439ca88baSGreg Roach return new Collection($families); 735a25f0a04SGreg Roach } 736a25f0a04SGreg Roach 737a25f0a04SGreg Roach /** 738a25f0a04SGreg Roach * Get the current spouse of this individual. 739a25f0a04SGreg Roach * 740a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 741a25f0a04SGreg Roach * in chronological order, and take the last one found. 742a25f0a04SGreg Roach * 743a25f0a04SGreg Roach * @return Individual|null 744a25f0a04SGreg Roach */ 745e364afe4SGreg Roach public function getCurrentSpouse(): ?Individual 746c1010edaSGreg Roach { 74739ca88baSGreg Roach $family = $this->spouseFamilies()->last(); 74839ca88baSGreg Roach 74939ca88baSGreg Roach if ($family instanceof Family) { 75039ca88baSGreg Roach return $family->spouse($this); 751a25f0a04SGreg Roach } 752b2ce94c6SRico Sonntag 753b2ce94c6SRico Sonntag return null; 754a25f0a04SGreg Roach } 755a25f0a04SGreg Roach 756a25f0a04SGreg Roach /** 757a25f0a04SGreg Roach * Count the children belonging to this individual. 758a25f0a04SGreg Roach * 759cbc1590aSGreg Roach * @return int 760a25f0a04SGreg Roach */ 761e364afe4SGreg Roach public function numberOfChildren(): int 762c1010edaSGreg Roach { 7637d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 7643dc7bbe9SGreg Roach return (int) $match[1]; 765b2ce94c6SRico Sonntag } 766b2ce94c6SRico Sonntag 76713abd6f3SGreg Roach $children = []; 76839ca88baSGreg Roach foreach ($this->spouseFamilies() as $fam) { 76939ca88baSGreg Roach foreach ($fam->children() as $child) { 770c0935879SGreg Roach $children[$child->xref()] = true; 771a25f0a04SGreg Roach } 772a25f0a04SGreg Roach } 773a25f0a04SGreg Roach 774a25f0a04SGreg Roach return count($children); 775a25f0a04SGreg Roach } 776a25f0a04SGreg Roach 777a25f0a04SGreg Roach /** 778a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 779a25f0a04SGreg Roach * 780cbc1590aSGreg Roach * @param int|null $access_level 781a25f0a04SGreg Roach * 782b5c8fd7eSGreg Roach * @return Collection<Family> 783a25f0a04SGreg Roach */ 78439ca88baSGreg Roach public function childFamilies($access_level = null): Collection 785c1010edaSGreg Roach { 786d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 7874b9ff166SGreg Roach 788d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 789d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 790d9e083e7SGreg Roach } 791a25f0a04SGreg Roach 79239ca88baSGreg Roach $families = new Collection(); 79339ca88baSGreg Roach 794d9e083e7SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level) as $fact) { 795dc124885SGreg Roach $family = $fact->target(); 796d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 79739ca88baSGreg Roach $families->push($family); 798a25f0a04SGreg Roach } 799a25f0a04SGreg Roach } 800a25f0a04SGreg Roach 801a25f0a04SGreg Roach return $families; 802a25f0a04SGreg Roach } 803a25f0a04SGreg Roach 804a25f0a04SGreg Roach /** 805a25f0a04SGreg Roach * Get a list of step-parent families. 806a25f0a04SGreg Roach * 807b5c8fd7eSGreg Roach * @return Collection<Family> 808a25f0a04SGreg Roach */ 809820b62dfSGreg Roach public function childStepFamilies(): Collection 810c1010edaSGreg Roach { 811ed5b6227SGreg Roach $step_families = new Collection(); 81239ca88baSGreg Roach $families = $this->childFamilies(); 813a25f0a04SGreg Roach foreach ($families as $family) { 814ed5b6227SGreg Roach foreach ($family->spouses() as $parent) { 815ed5b6227SGreg Roach foreach ($parent->spouseFamilies() as $step_family) { 81639ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 817ed5b6227SGreg Roach $step_families->add($step_family); 818a25f0a04SGreg Roach } 819a25f0a04SGreg Roach } 820a25f0a04SGreg Roach } 821a25f0a04SGreg Roach } 822a25f0a04SGreg Roach 8238c627a69SGreg Roach return $step_families->uniqueStrict(static function (Family $family): string { 8248c627a69SGreg Roach return $family->xref(); 8258c627a69SGreg Roach }); 826a25f0a04SGreg Roach } 827a25f0a04SGreg Roach 828a25f0a04SGreg Roach /** 829a25f0a04SGreg Roach * Get a list of step-parent families. 830a25f0a04SGreg Roach * 831b5c8fd7eSGreg Roach * @return Collection<Family> 832a25f0a04SGreg Roach */ 833820b62dfSGreg Roach public function spouseStepFamilies(): Collection 834c1010edaSGreg Roach { 83513abd6f3SGreg Roach $step_families = []; 83639ca88baSGreg Roach $families = $this->spouseFamilies(); 837820b62dfSGreg Roach 838a25f0a04SGreg Roach foreach ($families as $family) { 83939ca88baSGreg Roach $spouse = $family->spouse($this); 840820b62dfSGreg Roach 841d823340dSGreg Roach if ($spouse instanceof self) { 84239ca88baSGreg Roach foreach ($family->spouse($this)->spouseFamilies() as $step_family) { 84339ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 844a25f0a04SGreg Roach $step_families[] = $step_family; 845a25f0a04SGreg Roach } 846a25f0a04SGreg Roach } 847a25f0a04SGreg Roach } 848a25f0a04SGreg Roach } 849a25f0a04SGreg Roach 850820b62dfSGreg Roach return new Collection($step_families); 851a25f0a04SGreg Roach } 852a25f0a04SGreg Roach 853a25f0a04SGreg Roach /** 854a25f0a04SGreg Roach * A label for a parental family group 855a25f0a04SGreg Roach * 856a25f0a04SGreg Roach * @param Family $family 857a25f0a04SGreg Roach * 858a25f0a04SGreg Roach * @return string 859a25f0a04SGreg Roach */ 860820b62dfSGreg Roach public function getChildFamilyLabel(Family $family): string 861c1010edaSGreg Roach { 8627d70e4a7SGreg Roach preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match); 863b2ce94c6SRico Sonntag 8647d70e4a7SGreg Roach $values = [ 8657d70e4a7SGreg Roach 'birth' => I18N::translate('Family with parents'), 8667d70e4a7SGreg Roach 'adopted' => I18N::translate('Family with adoptive parents'), 8677d70e4a7SGreg Roach 'foster' => I18N::translate('Family with foster parents'), 8687d70e4a7SGreg Roach 'sealing' => /* I18N: “sealing” is a Mormon ceremony. */ 8697d70e4a7SGreg Roach I18N::translate('Family with sealing parents'), 8707d70e4a7SGreg Roach 'rada' => /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */ 8717d70e4a7SGreg Roach I18N::translate('Family with rada parents'), 8727d70e4a7SGreg Roach ]; 8737d70e4a7SGreg Roach 8747d70e4a7SGreg Roach return $values[$match[1] ?? 'birth'] ?? $values['birth']; 875a25f0a04SGreg Roach } 876a25f0a04SGreg Roach 877a25f0a04SGreg Roach /** 878a25f0a04SGreg Roach * Create a label for a step family 879a25f0a04SGreg Roach * 880a25f0a04SGreg Roach * @param Family $step_family 881a25f0a04SGreg Roach * 882a25f0a04SGreg Roach * @return string 883a25f0a04SGreg Roach */ 8848f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 885c1010edaSGreg Roach { 88639ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 887a25f0a04SGreg Roach if ($family !== $step_family) { 888a25f0a04SGreg Roach // Must be a step-family 88939ca88baSGreg Roach foreach ($family->spouses() as $parent) { 89039ca88baSGreg Roach foreach ($step_family->spouses() as $step_parent) { 891a25f0a04SGreg Roach if ($parent === $step_parent) { 892a25f0a04SGreg Roach // One common parent - must be a step family 893e364afe4SGreg Roach if ($parent->sex() === 'M') { 894a25f0a04SGreg Roach // Father’s family with someone else 89539ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 896a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 89739ca88baSGreg Roach return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName()); 898b2ce94c6SRico Sonntag } 899b2ce94c6SRico Sonntag 900a25f0a04SGreg Roach /* I18N: A step-family. */ 901bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 902a25f0a04SGreg Roach } 903b2ce94c6SRico Sonntag 904a25f0a04SGreg Roach // Mother’s family with someone else 90539ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 906a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 90739ca88baSGreg Roach return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName()); 908b2ce94c6SRico Sonntag } 909b2ce94c6SRico Sonntag 910a25f0a04SGreg Roach /* I18N: A step-family. */ 911bbb76c12SGreg Roach return I18N::translate('Mother’s family with an unknown individual'); 912a25f0a04SGreg Roach } 913a25f0a04SGreg Roach } 914a25f0a04SGreg Roach } 915a25f0a04SGreg Roach } 916a25f0a04SGreg Roach } 917a25f0a04SGreg Roach 918a25f0a04SGreg Roach // Perahps same parents - but a different family record? 919a25f0a04SGreg Roach return I18N::translate('Family with parents'); 920a25f0a04SGreg Roach } 921a25f0a04SGreg Roach 922225e381fSGreg Roach /** 923225e381fSGreg Roach * Get the description for the family. 924225e381fSGreg Roach * 925225e381fSGreg Roach * For example, "XXX's family with new wife". 926225e381fSGreg Roach * 927225e381fSGreg Roach * @param Family $family 928225e381fSGreg Roach * 929225e381fSGreg Roach * @return string 930225e381fSGreg Roach */ 931e364afe4SGreg Roach public function getSpouseFamilyLabel(Family $family): string 932c1010edaSGreg Roach { 93339ca88baSGreg Roach $spouse = $family->spouse($this); 934225e381fSGreg Roach if ($spouse) { 935225e381fSGreg Roach /* I18N: %s is the spouse name */ 93639ca88baSGreg Roach return I18N::translate('Family with %s', $spouse->fullName()); 937225e381fSGreg Roach } 938b2ce94c6SRico Sonntag 93939ca88baSGreg Roach return $family->fullName(); 940225e381fSGreg Roach } 941225e381fSGreg Roach 942a25f0a04SGreg Roach /** 943961ec755SGreg Roach * If this object has no name, what do we call it? 944961ec755SGreg Roach * 945961ec755SGreg Roach * @return string 946961ec755SGreg Roach */ 9478f53f488SRico Sonntag public function getFallBackName(): string 948c1010edaSGreg Roach { 949a25f0a04SGreg Roach return '@P.N. /@N.N./'; 950a25f0a04SGreg Roach } 951a25f0a04SGreg Roach 952a25f0a04SGreg Roach /** 953a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 954a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 955a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 956a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 957a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 958a25f0a04SGreg Roach * recorded in the NAME field. e.g. 959a25f0a04SGreg Roach * 960a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 961a25f0a04SGreg Roach * 2 GIVN Robert 962a25f0a04SGreg Roach * 2 SPFX de 963a25f0a04SGreg Roach * 2 SURN CLITHEROW 964a25f0a04SGreg Roach * 2 NICK The Bald 965a25f0a04SGreg Roach * 966a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 967a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 968a25f0a04SGreg Roach * 969a25f0a04SGreg Roach * Handle multiple surnames, either as; 970a25f0a04SGreg Roach * 971a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 972a25f0a04SGreg Roach * or 973a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 974a25f0a04SGreg Roach * 2 GIVN Carlos 975a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 976a25f0a04SGreg Roach * 977a25f0a04SGreg Roach * @param string $type 97851928f9aSGreg Roach * @param string $value 979a25f0a04SGreg Roach * @param string $gedcom 980e364afe4SGreg Roach * 981e364afe4SGreg Roach * @return void 982a25f0a04SGreg Roach */ 98351928f9aSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 984c1010edaSGreg Roach { 985a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 986a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 987a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 988a25f0a04SGreg Roach 98976f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 990a25f0a04SGreg Roach $GIVN = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : ''; 991a25f0a04SGreg Roach $SURN = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : ''; 992a25f0a04SGreg Roach 993a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 99476f666f4SGreg Roach if ($SURN !== '') { 995a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 996a25f0a04SGreg Roach } else { 99713abd6f3SGreg Roach $SURNS = []; 998a25f0a04SGreg Roach } 99976f666f4SGreg Roach 1000a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 1001a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 1002a25f0a04SGreg Roach 1003a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1004a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 1005a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1006a25f0a04SGreg Roach 1007a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 100851928f9aSGreg Roach if (substr_count($value, '/') % 2 === 1) { 100951928f9aSGreg Roach $value .= '/'; 1010a25f0a04SGreg Roach } 1011a25f0a04SGreg Roach 1012a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 101351928f9aSGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $value); 1014a25f0a04SGreg Roach 1015a25f0a04SGreg Roach // Extract the surname. 1016a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 1017a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 1018a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 1019a25f0a04SGreg Roach } else { 1020a25f0a04SGreg Roach $surname = ''; 1021a25f0a04SGreg Roach } 1022a25f0a04SGreg Roach 1023a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 1024a25f0a04SGreg Roach if (!$SURNS) { 1025a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 1026a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 1027a25f0a04SGreg Roach $SURNS = $matches[1]; 1028a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 1029a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 1030a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 1031a25f0a04SGreg Roach } 1032a25f0a04SGreg Roach } else { 1033a25f0a04SGreg Roach // It is valid not to have a surname at all 103413abd6f3SGreg Roach $SURNS = ['']; 1035a25f0a04SGreg Roach } 1036a25f0a04SGreg Roach } 1037a25f0a04SGreg Roach 1038a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 1039a25f0a04SGreg Roach if (!$GIVN) { 1040a25f0a04SGreg Roach $GIVN = preg_replace( 104113abd6f3SGreg Roach [ 1042c1010edaSGreg Roach '/ ?\/.*\/ ?/', 1043c1010edaSGreg Roach // remove surname 1044c1010edaSGreg Roach '/ ?".+"/', 1045c1010edaSGreg Roach // remove nickname 1046c1010edaSGreg Roach '/ {2,}/', 1047c1010edaSGreg Roach // multiple spaces, caused by the above 1048c1010edaSGreg Roach '/^ | $/', 1049c1010edaSGreg Roach // leading/trailing spaces, caused by the above 105013abd6f3SGreg Roach ], 105113abd6f3SGreg Roach [ 1052a25f0a04SGreg Roach ' ', 1053a25f0a04SGreg Roach ' ', 1054a25f0a04SGreg Roach ' ', 1055a25f0a04SGreg Roach '', 105613abd6f3SGreg Roach ], 1057a25f0a04SGreg Roach $full 1058a25f0a04SGreg Roach ); 1059a25f0a04SGreg Roach } 1060a25f0a04SGreg Roach 1061a25f0a04SGreg Roach // Add placeholder for unknown given name 1062a25f0a04SGreg Roach if (!$GIVN) { 1063d823340dSGreg Roach $GIVN = self::PRAENOMEN_NESCIO; 106473f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1065a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1066a25f0a04SGreg Roach } 1067a25f0a04SGreg Roach 1068a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1069a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1070a25f0a04SGreg Roach // $full is for display on-screen 1071a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1072a25f0a04SGreg Roach 1073a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1074d823340dSGreg Roach $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full); 1075d823340dSGreg Roach $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full); 1076c6f196c3SGreg Roach // Format for display 1077d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1078acc34ea1SGreg Roach // Localise quotation marks around the nickname 10790b5fd0a6SGreg Roach $full = preg_replace_callback('/"([^&]*)"/', static function (array $matches): string { 1080c652cdbdSGreg Roach return '<q class="wt-nickname">' . $matches[1] . '</q>'; 10818d68cabeSGreg Roach }, $full); 1082a25f0a04SGreg Roach 1083c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1084*ee51991cSGreg Roach $full = preg_replace('/([^ >\x{200C}]*)\*/u', '<span class="starredname">\\1</span>', $full); 1085a25f0a04SGreg Roach 1086a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1087a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1088a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1089a25f0a04SGreg Roach 1090ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1091a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1092e364afe4SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) { 1093a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1094e364afe4SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) { 1095a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1096a25f0a04SGreg Roach } 1097a25f0a04SGreg Roach 1098bdb3725aSGreg Roach $this->getAllNames[] = [ 1099a25f0a04SGreg Roach 'type' => $type, 1100a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1101c1010edaSGreg Roach 'full' => $full, 1102c1010edaSGreg Roach // This is used for display 1103c1010edaSGreg Roach 'fullNN' => $fullNN, 1104c1010edaSGreg Roach // This goes into the database 1105c1010edaSGreg Roach 'surname' => $surname, 1106c1010edaSGreg Roach // This goes into the database 1107c1010edaSGreg Roach 'givn' => $GIVN, 1108c1010edaSGreg Roach // This goes into the database 1109c1010edaSGreg Roach 'surn' => $SURN, 1110c1010edaSGreg Roach // This goes into the database 111113abd6f3SGreg Roach ]; 1112a25f0a04SGreg Roach } 1113a25f0a04SGreg Roach } 1114a25f0a04SGreg Roach 1115a25f0a04SGreg Roach /** 111676692c8bSGreg Roach * Extract names from the GEDCOM record. 1117c7ff4153SGreg Roach * 1118c7ff4153SGreg Roach * @return void 1119a25f0a04SGreg Roach */ 1120e364afe4SGreg Roach public function extractNames(): void 1121c1010edaSGreg Roach { 1122d9e083e7SGreg Roach $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree); 1123d9e083e7SGreg Roach 11248f53f488SRico Sonntag $this->extractNamesFromFacts( 11258f53f488SRico Sonntag 1, 11268f53f488SRico Sonntag 'NAME', 1127d9e083e7SGreg Roach $this->facts(['NAME'], false, $access_level) 11288f53f488SRico Sonntag ); 1129a25f0a04SGreg Roach } 1130a25f0a04SGreg Roach 1131a25f0a04SGreg Roach /** 1132a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1133a25f0a04SGreg Roach * selection items or favorites. 1134a25f0a04SGreg Roach * 1135a25f0a04SGreg Roach * @return string 1136a25f0a04SGreg Roach */ 11378f53f488SRico Sonntag public function formatListDetails(): string 1138c1010edaSGreg Roach { 1139a25f0a04SGreg Roach return 11408d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 11418d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1142a25f0a04SGreg Roach } 11438091bfd1SGreg Roach 11448091bfd1SGreg Roach /** 11458091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11468091bfd1SGreg Roach */ 11478091bfd1SGreg Roach public function lock(): void 11488091bfd1SGreg Roach { 11498091bfd1SGreg Roach DB::table('individuals') 11508091bfd1SGreg Roach ->where('i_file', '=', $this->tree->id()) 11518091bfd1SGreg Roach ->where('i_id', '=', $this->xref()) 11528091bfd1SGreg Roach ->lockForUpdate() 11538091bfd1SGreg Roach ->get(); 11548091bfd1SGreg Roach } 1155a25f0a04SGreg Roach} 1156