1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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; 25665e281aSGreg Roachuse Fisharebest\Webtrees\Elements\PedigreeLinkageType; 26852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 2739ca88baSGreg Roachuse Illuminate\Support\Collection; 28a25f0a04SGreg Roach 2910e06497SGreg Roachuse function array_key_exists; 3010e06497SGreg Roachuse function count; 3110e06497SGreg Roachuse function in_array; 327d70e4a7SGreg Roachuse function preg_match; 337d70e4a7SGreg Roach 34a25f0a04SGreg Roach/** 3576692c8bSGreg Roach * A GEDCOM individual (INDI) object. 36a25f0a04SGreg Roach */ 37c1010edaSGreg Roachclass Individual extends GedcomRecord 38c1010edaSGreg Roach{ 3916d6367aSGreg Roach public const RECORD_TYPE = 'INDI'; 4016d6367aSGreg Roach 418fb4e87cSGreg Roach // Placeholders to indicate unknown names 428fb4e87cSGreg Roach public const NOMEN_NESCIO = '@N.N.'; 438fb4e87cSGreg Roach public const PRAENOMEN_NESCIO = '@P.N.'; 448fb4e87cSGreg Roach 45852ede8cSGreg Roach protected const ROUTE_NAME = IndividualPage::class; 46a25f0a04SGreg Roach 477fa97a69SGreg Roach /** Used in some lists to keep track of this individual’s generation in that list */ 487fa97a69SGreg Roach public ?int $generation = null; 49a25f0a04SGreg Roach 507fa97a69SGreg Roach private ?Date $estimated_birth_date = null; 51a25f0a04SGreg Roach 527fa97a69SGreg Roach private ?Date $estimated_death_date = null; 53a25f0a04SGreg Roach 54a25f0a04SGreg Roach /** 55c156e8f5SGreg Roach * A closure which will compare individuals by birth date. 56c156e8f5SGreg Roach * 57c6921a17SGreg Roach * @return Closure(Individual,Individual):int 58c156e8f5SGreg Roach */ 59c156e8f5SGreg Roach public static function birthDateComparator(): Closure 60c156e8f5SGreg Roach { 61*f25fc0f9SGreg Roach return static fn(Individual $x, Individual $y): int => Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 62c156e8f5SGreg Roach } 63c156e8f5SGreg Roach 64c156e8f5SGreg Roach /** 65c156e8f5SGreg Roach * A closure which will compare individuals by death date. 66c156e8f5SGreg Roach * 67c6921a17SGreg Roach * @return Closure(Individual,Individual):int 68c156e8f5SGreg Roach */ 69c156e8f5SGreg Roach public static function deathDateComparator(): Closure 70c156e8f5SGreg Roach { 71*f25fc0f9SGreg Roach return static fn(Individual $x, Individual $y): int => Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate()); 72c156e8f5SGreg Roach } 73c156e8f5SGreg Roach 74c156e8f5SGreg Roach /** 75a25f0a04SGreg Roach * Can the name of this record be shown? 76a25f0a04SGreg Roach * 7776692c8bSGreg Roach * @param int|null $access_level 7876692c8bSGreg Roach * 7976692c8bSGreg Roach * @return bool 80a25f0a04SGreg Roach */ 812c6f1bd5SGreg Roach public function canShowName(int|null $access_level = null): bool 82c1010edaSGreg Roach { 833529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 844b9ff166SGreg Roach 85f56b86d2SGreg Roach return (int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 86a25f0a04SGreg Roach } 87a25f0a04SGreg Roach 88a25f0a04SGreg Roach /** 8976692c8bSGreg Roach * Can this individual be shown? 90a25f0a04SGreg Roach * 9176692c8bSGreg Roach * @param int $access_level 9276692c8bSGreg Roach * 9376692c8bSGreg Roach * @return bool 94a25f0a04SGreg Roach */ 9535584196SGreg Roach protected function canShowByType(int $access_level): bool 96c1010edaSGreg Roach { 97a25f0a04SGreg Roach // Dead people... 98f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 99a25f0a04SGreg Roach $keep_alive = false; 10054ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 101b6ec1ccfSGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH !== 0) { 102dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 103a25f0a04SGreg Roach foreach ($matches as $match) { 104a25f0a04SGreg Roach $date = new Date($match[1]); 105a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 106a25f0a04SGreg Roach $keep_alive = true; 107a25f0a04SGreg Roach break; 108a25f0a04SGreg Roach } 109a25f0a04SGreg Roach } 110a25f0a04SGreg Roach } 11154ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 112b6ec1ccfSGreg Roach if ($KEEP_ALIVE_YEARS_DEATH !== 0) { 113dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 114a25f0a04SGreg Roach foreach ($matches as $match) { 115a25f0a04SGreg Roach $date = new Date($match[1]); 116a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 117a25f0a04SGreg Roach $keep_alive = true; 118a25f0a04SGreg Roach break; 119a25f0a04SGreg Roach } 120a25f0a04SGreg Roach } 121a25f0a04SGreg Roach } 122a25f0a04SGreg Roach if (!$keep_alive) { 123a25f0a04SGreg Roach return true; 124a25f0a04SGreg Roach } 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 1271fe542e9SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH); 1281fe542e9SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 1297c4add84SGreg Roach 13054ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1314b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 132a25f0a04SGreg Roach } 133a25f0a04SGreg Roach 134a25f0a04SGreg Roach // No restriction found - show living people to members only: 1354b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 136a25f0a04SGreg Roach } 137a25f0a04SGreg Roach 138a25f0a04SGreg Roach /** 139a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 140a25f0a04SGreg Roach * 141a25f0a04SGreg Roach * @param Individual $target 142cbc1590aSGreg Roach * @param int $distance 143a25f0a04SGreg Roach * 144cbc1590aSGreg Roach * @return bool 145a25f0a04SGreg Roach */ 14624f2a3afSGreg Roach private static function isRelated(Individual $target, int $distance): bool 147c1010edaSGreg Roach { 148a25f0a04SGreg Roach static $cache = null; 149a25f0a04SGreg Roach 1501fe542e9SGreg Roach $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree); 151b6ec1ccfSGreg Roach if ($user_individual instanceof Individual) { 152a25f0a04SGreg Roach if (!$cache) { 15313abd6f3SGreg Roach $cache = [ 15413abd6f3SGreg Roach 0 => [$user_individual], 15513abd6f3SGreg Roach 1 => [], 15613abd6f3SGreg Roach ]; 1578d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 158dc124885SGreg Roach $family = $fact->target(); 159e24444eeSGreg Roach if ($family instanceof Family) { 160a25f0a04SGreg Roach $cache[1][] = $family; 161a25f0a04SGreg Roach } 162a25f0a04SGreg Roach } 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach } else { 165a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 166a25f0a04SGreg Roach return true; 167a25f0a04SGreg Roach } 168a25f0a04SGreg Roach 169a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 170a25f0a04SGreg Roach $distance *= 2; 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach // Consider each path length in turn 173a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 174a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 175a25f0a04SGreg Roach // We have already calculated all records with this length 176e364afe4SGreg Roach if ($n % 2 === 0 && in_array($target, $cache[$n], true)) { 177a25f0a04SGreg Roach return true; 178a25f0a04SGreg Roach } 179a25f0a04SGreg Roach } else { 180a25f0a04SGreg Roach // Need to calculate these paths 18113abd6f3SGreg Roach $cache[$n] = []; 182e364afe4SGreg Roach if ($n % 2 === 0) { 183a25f0a04SGreg Roach // Add FAM->INDI links 184a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 1858d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 186dc124885SGreg Roach $individual = $fact->target(); 187a25f0a04SGreg Roach // Don’t backtrack 188e364afe4SGreg Roach if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) { 189a25f0a04SGreg Roach $cache[$n][] = $individual; 190a25f0a04SGreg Roach } 191a25f0a04SGreg Roach } 192a25f0a04SGreg Roach } 193a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 194a25f0a04SGreg Roach return true; 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach } else { 197a25f0a04SGreg Roach // Add INDI->FAM links 198a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 1998d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 200dc124885SGreg Roach $family = $fact->target(); 201a25f0a04SGreg Roach // Don’t backtrack 202e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 203a25f0a04SGreg Roach $cache[$n][] = $family; 204a25f0a04SGreg Roach } 205a25f0a04SGreg Roach } 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach } 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach return false; 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 21476692c8bSGreg Roach /** 21576692c8bSGreg Roach * Generate a private version of this record 21676692c8bSGreg Roach * 21776692c8bSGreg Roach * @param int $access_level 21876692c8bSGreg Roach * 21976692c8bSGreg Roach * @return string 22076692c8bSGreg Roach */ 2213c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 222c1010edaSGreg Roach { 223acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 224a25f0a04SGreg Roach 225a25f0a04SGreg Roach $rec = '0 @' . $this->xref . '@ INDI'; 226f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) { 227a25f0a04SGreg Roach // Show all the NAME tags, including subtags 2288d0ebef0SGreg Roach foreach ($this->facts(['NAME']) as $fact) { 229138ca96cSGreg Roach $rec .= "\n" . $fact->gedcom(); 230a25f0a04SGreg Roach } 231a25f0a04SGreg Roach } 232a25f0a04SGreg Roach // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data 2338d0ebef0SGreg Roach preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 234a25f0a04SGreg Roach foreach ($matches as $match) { 2356b9cb339SGreg Roach $rela = Registry::familyFactory()->make($match[1], $this->tree); 236a25f0a04SGreg Roach if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 237a25f0a04SGreg Roach $rec .= $match[0]; 238a25f0a04SGreg Roach } 239a25f0a04SGreg Roach } 240a25f0a04SGreg Roach // Don’t privatize sex. 241a25f0a04SGreg Roach if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) { 242a25f0a04SGreg Roach $rec .= $match[0]; 243a25f0a04SGreg Roach } 244a25f0a04SGreg Roach 245a25f0a04SGreg Roach return $rec; 246a25f0a04SGreg Roach } 247a25f0a04SGreg Roach 24876692c8bSGreg Roach /** 249a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 250a25f0a04SGreg Roach * If not known to be dead, then assume living. 251a25f0a04SGreg Roach * 252cbc1590aSGreg Roach * @return bool 253a25f0a04SGreg Roach */ 2548f53f488SRico Sonntag public function isDead(): bool 255c1010edaSGreg Roach { 256c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 257d97083feSGreg Roach $today_jd = Registry::timestampFactory()->now()->julianDay(); 258a25f0a04SGreg Roach 259a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 2608d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 261a25f0a04SGreg Roach return true; 262a25f0a04SGreg Roach } 263a25f0a04SGreg Roach 264e63974caSStefan Weil // If any event occurred more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 265a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 266a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 267a25f0a04SGreg Roach $date = new Date($date_match); 268269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) { 269a25f0a04SGreg Roach return true; 270a25f0a04SGreg Roach } 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 273a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 274a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 275a25f0a04SGreg Roach return false; 276a25f0a04SGreg Roach } 277a25f0a04SGreg Roach } 278a25f0a04SGreg Roach 279a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 280a25f0a04SGreg Roach 281a25f0a04SGreg Roach // Check parents (birth and adopted) 28239ca88baSGreg Roach foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) { 28339ca88baSGreg Roach foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) { 284a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 285a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 286a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 287a25f0a04SGreg Roach $date = new Date($date_match); 288269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) { 289a25f0a04SGreg Roach return true; 290a25f0a04SGreg Roach } 291a25f0a04SGreg Roach } 292a25f0a04SGreg Roach } 293a25f0a04SGreg Roach } 294a25f0a04SGreg Roach 295a25f0a04SGreg Roach // Check spouses 29639ca88baSGreg Roach foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) { 297a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 298a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 299a25f0a04SGreg Roach $date = new Date($date_match); 300a25f0a04SGreg Roach // Assume marriage occurs after age of 10 301269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) { 302a25f0a04SGreg Roach return true; 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach // Check spouse dates 30639ca88baSGreg Roach $spouse = $family->spouse($this, Auth::PRIV_HIDE); 307a25f0a04SGreg Roach if ($spouse) { 308a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 309a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 310a25f0a04SGreg Roach $date = new Date($date_match); 311a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 312269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) { 313a25f0a04SGreg Roach return true; 314a25f0a04SGreg Roach } 315a25f0a04SGreg Roach } 316a25f0a04SGreg Roach } 317a25f0a04SGreg Roach // Check child dates 31839ca88baSGreg Roach foreach ($family->children(Auth::PRIV_HIDE) as $child) { 319a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 320a25f0a04SGreg Roach // Assume children born after age of 15 321a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 322a25f0a04SGreg Roach $date = new Date($date_match); 323269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) { 324a25f0a04SGreg Roach return true; 325a25f0a04SGreg Roach } 326a25f0a04SGreg Roach } 327a25f0a04SGreg Roach // Check grandchildren 32839ca88baSGreg Roach foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) { 32939ca88baSGreg Roach foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) { 330a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 331a25f0a04SGreg Roach // Assume grandchildren born after age of 30 332a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 333a25f0a04SGreg Roach $date = new Date($date_match); 334269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) { 335a25f0a04SGreg Roach return true; 336a25f0a04SGreg Roach } 337a25f0a04SGreg Roach } 338a25f0a04SGreg Roach } 339a25f0a04SGreg Roach } 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach } 342a25f0a04SGreg Roach 343a25f0a04SGreg Roach return false; 344a25f0a04SGreg Roach } 345a25f0a04SGreg Roach 346a25f0a04SGreg Roach /** 347a25f0a04SGreg Roach * Find the highlighted media object for an individual 348a25f0a04SGreg Roach * 349e364afe4SGreg Roach * @return MediaFile|null 350a25f0a04SGreg Roach */ 351e364afe4SGreg Roach public function findHighlightedMediaFile(): ?MediaFile 352c1010edaSGreg Roach { 35392647683SGreg Roach $fact = $this->facts(['OBJE']) 35419d319e7SGreg Roach ->first(static function (Fact $fact): bool { 355dc124885SGreg Roach $media = $fact->target(); 35619d319e7SGreg Roach 35719d319e7SGreg Roach return $media instanceof Media && $media->firstImageFile() instanceof MediaFile; 35819d319e7SGreg Roach }); 35919d319e7SGreg Roach 36092647683SGreg Roach if ($fact instanceof Fact && $fact->target() instanceof Media) { 36192647683SGreg Roach return $fact->target()->firstImageFile(); 362a25f0a04SGreg Roach } 363a25f0a04SGreg Roach 364a25f0a04SGreg Roach return null; 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach 367a25f0a04SGreg Roach /** 3687b0d562eSGreg Roach * Display the preferred image for this individual. 369a25f0a04SGreg Roach * Use an icon if no image is available. 370a25f0a04SGreg Roach * 371dce07401SGreg Roach * @param int $width Pixels 372dce07401SGreg Roach * @param int $height Pixels 373dce07401SGreg Roach * @param string $fit "crop" or "contain" 37409482a55SGreg Roach * @param array<string> $attributes Additional HTML attributes 375dce07401SGreg Roach * 376a25f0a04SGreg Roach * @return string 377a25f0a04SGreg Roach */ 37824f2a3afSGreg Roach public function displayImage(int $width, int $height, string $fit, array $attributes): string 379c1010edaSGreg Roach { 3804a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 3814a9f750fSGreg Roach 3824a9f750fSGreg Roach if ($media_file !== null) { 3834a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 384a25f0a04SGreg Roach } 3854a9f750fSGreg Roach 386b6ec1ccfSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE') === '1') { 38768b631f1SGreg Roach return '<i class="icon-silhouette icon-silhouette-' . strtolower($this->sex()) . ' wt-icon-flip-rtl"></i>'; 3884a9f750fSGreg Roach } 3894a9f750fSGreg Roach 3904a9f750fSGreg Roach return ''; 391a25f0a04SGreg Roach } 392a25f0a04SGreg Roach 393a25f0a04SGreg Roach /** 394a25f0a04SGreg Roach * Get the date of birth 395a25f0a04SGreg Roach * 396a25f0a04SGreg Roach * @return Date 397a25f0a04SGreg Roach */ 3988f53f488SRico Sonntag public function getBirthDate(): Date 399c1010edaSGreg Roach { 400a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 401a25f0a04SGreg Roach if ($date->isOK()) { 402a25f0a04SGreg Roach return $date; 403a25f0a04SGreg Roach } 404a25f0a04SGreg Roach } 405a25f0a04SGreg Roach 406a25f0a04SGreg Roach return new Date(''); 407a25f0a04SGreg Roach } 408a25f0a04SGreg Roach 409a25f0a04SGreg Roach /** 410a25f0a04SGreg Roach * Get the place of birth 411a25f0a04SGreg Roach * 41216d0b7f7SRico Sonntag * @return Place 413a25f0a04SGreg Roach */ 4148f53f488SRico Sonntag public function getBirthPlace(): Place 415c1010edaSGreg Roach { 416a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 417a25f0a04SGreg Roach return $place; 418a25f0a04SGreg Roach } 419a25f0a04SGreg Roach 420b20ddbf9SGreg Roach return new Place('', $this->tree); 421a25f0a04SGreg Roach } 422a25f0a04SGreg Roach 423a25f0a04SGreg Roach /** 424a25f0a04SGreg Roach * Get the date of death 425a25f0a04SGreg Roach * 426a25f0a04SGreg Roach * @return Date 427a25f0a04SGreg Roach */ 4288f53f488SRico Sonntag public function getDeathDate(): Date 429c1010edaSGreg Roach { 430a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 431a25f0a04SGreg Roach if ($date->isOK()) { 432a25f0a04SGreg Roach return $date; 433a25f0a04SGreg Roach } 434a25f0a04SGreg Roach } 435a25f0a04SGreg Roach 436a25f0a04SGreg Roach return new Date(''); 437a25f0a04SGreg Roach } 438a25f0a04SGreg Roach 439a25f0a04SGreg Roach /** 440a25f0a04SGreg Roach * Get the place of death 441a25f0a04SGreg Roach * 44216d0b7f7SRico Sonntag * @return Place 443a25f0a04SGreg Roach */ 4448f53f488SRico Sonntag public function getDeathPlace(): Place 445c1010edaSGreg Roach { 446a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 447a25f0a04SGreg Roach return $place; 448a25f0a04SGreg Roach } 449a25f0a04SGreg Roach 450b20ddbf9SGreg Roach return new Place('', $this->tree); 451a25f0a04SGreg Roach } 452a25f0a04SGreg Roach 453a25f0a04SGreg Roach /** 454a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 45515d603e7SGreg Roach * Provide the place and full date using a tooltip. 456a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 457a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 458a25f0a04SGreg Roach * 459a25f0a04SGreg Roach * @return string 460a25f0a04SGreg Roach */ 4615e6816beSGreg Roach public function lifespan(): string 462c1010edaSGreg Roach { 46353f5ca04SGreg Roach // Just the first part of the place name. 464392561bbSGreg Roach $birth_place = strip_tags($this->getBirthPlace()->shortName()); 465392561bbSGreg Roach $death_place = strip_tags($this->getDeathPlace()->shortName()); 46653f5ca04SGreg Roach 46781b9dc5dSGreg Roach // Remove markup from dates. Use UTF_FSI / UTF_PDI instead of <bdi></bdi>, as 46881b9dc5dSGreg Roach // we cannot use HTML markup in title attributes. 46981b9dc5dSGreg Roach $birth_date = "\u{2068}" . strip_tags($this->getBirthDate()->display()) . "\u{2069}"; 47081b9dc5dSGreg Roach $death_date = "\u{2068}" . strip_tags($this->getDeathDate()->display()) . "\u{2069}"; 47115d603e7SGreg Roach 47253f5ca04SGreg Roach // Use minimum and maximum dates - to agree with the age calculations. 47353f5ca04SGreg Roach $birth_year = $this->getBirthDate()->minimumDate()->format('%Y'); 47453f5ca04SGreg Roach $death_year = $this->getDeathDate()->maximumDate()->format('%Y'); 47553f5ca04SGreg Roach 476f94b830fSGreg Roach if ($birth_year === '') { 477f94b830fSGreg Roach $birth_year = I18N::translate('…'); 478f94b830fSGreg Roach } 479f94b830fSGreg Roach 4807ecbeefdSGreg Roach if ($death_year === '' && $this->isDead()) { 4817ecbeefdSGreg Roach $death_year = I18N::translate('…'); 4827ecbeefdSGreg Roach } 4837ecbeefdSGreg Roach 484c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 485dacfe33cSGreg Roach return I18N::translate( 486a25f0a04SGreg Roach '%1$s–%2$s', 48753f5ca04SGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $birth_year . '</span>', 48853f5ca04SGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $death_year . '</span>' 489a25f0a04SGreg Roach ); 490a25f0a04SGreg Roach } 491a25f0a04SGreg Roach 492a25f0a04SGreg Roach /** 493a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 494a25f0a04SGreg Roach * 49509482a55SGreg Roach * @return array<Date> 496a25f0a04SGreg Roach */ 4978f53f488SRico Sonntag public function getAllBirthDates(): array 498c1010edaSGreg Roach { 4998d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 500d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 501d240bb87SGreg Roach 502d240bb87SGreg Roach if ($dates !== []) { 503d240bb87SGreg Roach return $dates; 504a25f0a04SGreg Roach } 505a25f0a04SGreg Roach } 506a25f0a04SGreg Roach 50713abd6f3SGreg Roach return []; 508a25f0a04SGreg Roach } 509a25f0a04SGreg Roach 510a25f0a04SGreg Roach /** 511a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 512a25f0a04SGreg Roach * 51309482a55SGreg Roach * @return array<Place> 514a25f0a04SGreg Roach */ 5158f53f488SRico Sonntag public function getAllBirthPlaces(): array 516c1010edaSGreg Roach { 5178d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 5188d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 519d240bb87SGreg Roach 52054c1ab5eSGreg Roach if ($places !== []) { 5214080d558SGreg Roach return $places; 522a25f0a04SGreg Roach } 523a25f0a04SGreg Roach } 524a25f0a04SGreg Roach 52513abd6f3SGreg Roach return []; 526a25f0a04SGreg Roach } 527a25f0a04SGreg Roach 528a25f0a04SGreg Roach /** 529a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 530a25f0a04SGreg Roach * 53109482a55SGreg Roach * @return array<Date> 532a25f0a04SGreg Roach */ 5338f53f488SRico Sonntag public function getAllDeathDates(): array 534c1010edaSGreg Roach { 5358d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 536d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 537d240bb87SGreg Roach 538d240bb87SGreg Roach if ($dates !== []) { 539d240bb87SGreg Roach return $dates; 540a25f0a04SGreg Roach } 541a25f0a04SGreg Roach } 542a25f0a04SGreg Roach 54313abd6f3SGreg Roach return []; 544a25f0a04SGreg Roach } 545a25f0a04SGreg Roach 546a25f0a04SGreg Roach /** 547a25f0a04SGreg Roach * Get all the death places - for the individual lists. 548a25f0a04SGreg Roach * 54909482a55SGreg Roach * @return array<Place> 550a25f0a04SGreg Roach */ 5518f53f488SRico Sonntag public function getAllDeathPlaces(): array 552c1010edaSGreg Roach { 5538d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 5548d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 555d240bb87SGreg Roach 55654c1ab5eSGreg Roach if ($places !== []) { 5574080d558SGreg Roach return $places; 558a25f0a04SGreg Roach } 559a25f0a04SGreg Roach } 560a25f0a04SGreg Roach 56113abd6f3SGreg Roach return []; 562a25f0a04SGreg Roach } 563a25f0a04SGreg Roach 564a25f0a04SGreg Roach /** 565a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 566a25f0a04SGreg Roach * 567a25f0a04SGreg Roach * @return Date 568a25f0a04SGreg Roach */ 5698f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 570c1010edaSGreg Roach { 5718f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 572a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 573a25f0a04SGreg Roach if ($date->isOK()) { 5744686330aSGreg Roach $this->estimated_birth_date = $date; 575a25f0a04SGreg Roach break; 576a25f0a04SGreg Roach } 577a25f0a04SGreg Roach } 5788f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 57913abd6f3SGreg Roach $min = []; 58013abd6f3SGreg Roach $max = []; 581a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 582f5b60decSGreg Roach if ($tmp->isOK()) { 5836bd19c8cSGreg Roach $min[] = $tmp->minimumJulianDay() - 365 * (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 584f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 585a25f0a04SGreg Roach } 58639ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 587a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 588f5b60decSGreg Roach if ($tmp->isOK()) { 5896bd19c8cSGreg Roach $min[] = $tmp->maximumJulianDay() - 365; 590f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 591a25f0a04SGreg Roach } 59239ca88baSGreg Roach $husband = $family->husband(); 593e364afe4SGreg Roach if ($husband instanceof self) { 5942e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 595f5b60decSGreg Roach if ($tmp->isOK()) { 596f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 597f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 598a25f0a04SGreg Roach } 599a25f0a04SGreg Roach } 60039ca88baSGreg Roach $wife = $family->wife(); 601e364afe4SGreg Roach if ($wife instanceof self) { 6022e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 603f5b60decSGreg Roach if ($tmp->isOK()) { 604f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 605f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 606a25f0a04SGreg Roach } 607a25f0a04SGreg Roach } 60839ca88baSGreg Roach foreach ($family->children() as $child) { 609a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 610f5b60decSGreg Roach if ($tmp->isOK()) { 611f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 612f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 613a25f0a04SGreg Roach } 614a25f0a04SGreg Roach } 615a25f0a04SGreg Roach } 61639ca88baSGreg Roach foreach ($this->spouseFamilies() as $family) { 617a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 618f5b60decSGreg Roach if ($tmp->isOK()) { 619f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 620f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 621a25f0a04SGreg Roach } 62239ca88baSGreg Roach $spouse = $family->spouse($this); 623a25f0a04SGreg Roach if ($spouse) { 624a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 625f5b60decSGreg Roach if ($tmp->isOK()) { 626f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 627f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 628a25f0a04SGreg Roach } 629a25f0a04SGreg Roach } 63039ca88baSGreg Roach foreach ($family->children() as $child) { 631a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 632f5b60decSGreg Roach if ($tmp->isOK()) { 633e364afe4SGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65); 634f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 635a25f0a04SGreg Roach } 636a25f0a04SGreg Roach } 637a25f0a04SGreg Roach } 638a25f0a04SGreg Roach if ($min && $max) { 63959f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 640a25f0a04SGreg Roach 64165e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 6424686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 643a25f0a04SGreg Roach } else { 6444686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 645a25f0a04SGreg Roach } 646a25f0a04SGreg Roach } 647a25f0a04SGreg Roach } 648a25f0a04SGreg Roach 6494686330aSGreg Roach return $this->estimated_birth_date; 650a25f0a04SGreg Roach } 651a25f0a04SGreg Roach 652a25f0a04SGreg Roach /** 653a25f0a04SGreg Roach * Generate an estimated date of death. 654a25f0a04SGreg Roach * 655a25f0a04SGreg Roach * @return Date 656a25f0a04SGreg Roach */ 6578f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 658c1010edaSGreg Roach { 6594686330aSGreg Roach if ($this->estimated_death_date === null) { 660a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 661a25f0a04SGreg Roach if ($date->isOK()) { 6624686330aSGreg Roach $this->estimated_death_date = $date; 663a25f0a04SGreg Roach break; 664a25f0a04SGreg Roach } 665a25f0a04SGreg Roach } 6664686330aSGreg Roach if ($this->estimated_death_date === null) { 667b6ec1ccfSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay() !== 0) { 668c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 6694686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 670a25f0a04SGreg Roach } else { 6714686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 672a25f0a04SGreg Roach } 673a25f0a04SGreg Roach } 674a25f0a04SGreg Roach } 675a25f0a04SGreg Roach 6764686330aSGreg Roach return $this->estimated_death_date; 677a25f0a04SGreg Roach } 678a25f0a04SGreg Roach 679a25f0a04SGreg Roach /** 680a25f0a04SGreg Roach * Get the sex - M F or U 681a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 682a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 683a25f0a04SGreg Roach * 684a25f0a04SGreg Roach * @return string 685a25f0a04SGreg Roach */ 686e364afe4SGreg Roach public function sex(): string 687c1010edaSGreg Roach { 6881baf69deSGreg Roach if (preg_match('/\n1 SEX ([MFX])/', $this->gedcom . $this->pending, $match)) { 689a25f0a04SGreg Roach return $match[1]; 690a25f0a04SGreg Roach } 691b2ce94c6SRico Sonntag 692b2ce94c6SRico Sonntag return 'U'; 693a25f0a04SGreg Roach } 694a25f0a04SGreg Roach 695a25f0a04SGreg Roach /** 696a25f0a04SGreg Roach * Get a list of this individual’s spouse families 697a25f0a04SGreg Roach * 698cbc1590aSGreg Roach * @param int|null $access_level 699a25f0a04SGreg Roach * 70036779af1SGreg Roach * @return Collection<int,Family> 701a25f0a04SGreg Roach */ 7022c6f1bd5SGreg Roach public function spouseFamilies(int|null $access_level = null): Collection 703c1010edaSGreg Roach { 7043529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 705d9e083e7SGreg Roach 706d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 707d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 7084b9ff166SGreg Roach } 7094b9ff166SGreg Roach 71039ca88baSGreg Roach $families = new Collection(); 711d9e083e7SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level) as $fact) { 712dc124885SGreg Roach $family = $fact->target(); 713d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 71439ca88baSGreg Roach $families->push($family); 715a25f0a04SGreg Roach } 716a25f0a04SGreg Roach } 717a25f0a04SGreg Roach 71839ca88baSGreg Roach return new Collection($families); 719a25f0a04SGreg Roach } 720a25f0a04SGreg Roach 721a25f0a04SGreg Roach /** 722a25f0a04SGreg Roach * Get the current spouse of this individual. 723a25f0a04SGreg Roach * 724a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 725a25f0a04SGreg Roach * in chronological order, and take the last one found. 726a25f0a04SGreg Roach * 727a25f0a04SGreg Roach * @return Individual|null 728a25f0a04SGreg Roach */ 729e364afe4SGreg Roach public function getCurrentSpouse(): ?Individual 730c1010edaSGreg Roach { 73139ca88baSGreg Roach $family = $this->spouseFamilies()->last(); 73239ca88baSGreg Roach 73339ca88baSGreg Roach if ($family instanceof Family) { 73439ca88baSGreg Roach return $family->spouse($this); 735a25f0a04SGreg Roach } 736b2ce94c6SRico Sonntag 737b2ce94c6SRico Sonntag return null; 738a25f0a04SGreg Roach } 739a25f0a04SGreg Roach 740a25f0a04SGreg Roach /** 741a25f0a04SGreg Roach * Count the children belonging to this individual. 742a25f0a04SGreg Roach * 743cbc1590aSGreg Roach * @return int 744a25f0a04SGreg Roach */ 745e364afe4SGreg Roach public function numberOfChildren(): int 746c1010edaSGreg Roach { 7477d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 7483dc7bbe9SGreg Roach return (int) $match[1]; 749b2ce94c6SRico Sonntag } 750b2ce94c6SRico Sonntag 75113abd6f3SGreg Roach $children = []; 75239ca88baSGreg Roach foreach ($this->spouseFamilies() as $fam) { 75339ca88baSGreg Roach foreach ($fam->children() as $child) { 754c0935879SGreg Roach $children[$child->xref()] = true; 755a25f0a04SGreg Roach } 756a25f0a04SGreg Roach } 757a25f0a04SGreg Roach 758a25f0a04SGreg Roach return count($children); 759a25f0a04SGreg Roach } 760a25f0a04SGreg Roach 761a25f0a04SGreg Roach /** 762a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 763a25f0a04SGreg Roach * 764cbc1590aSGreg Roach * @param int|null $access_level 765a25f0a04SGreg Roach * 76636779af1SGreg Roach * @return Collection<int,Family> 767a25f0a04SGreg Roach */ 7682c6f1bd5SGreg Roach public function childFamilies(int|null $access_level = null): Collection 769c1010edaSGreg Roach { 7703529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 7714b9ff166SGreg Roach 772d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 773d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 774d9e083e7SGreg Roach } 775a25f0a04SGreg Roach 77639ca88baSGreg Roach $families = new Collection(); 77739ca88baSGreg Roach 778d9e083e7SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level) as $fact) { 779dc124885SGreg Roach $family = $fact->target(); 780d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 78139ca88baSGreg Roach $families->push($family); 782a25f0a04SGreg Roach } 783a25f0a04SGreg Roach } 784a25f0a04SGreg Roach 785a25f0a04SGreg Roach return $families; 786a25f0a04SGreg Roach } 787a25f0a04SGreg Roach 788a25f0a04SGreg Roach /** 789a25f0a04SGreg Roach * Get a list of step-parent families. 790a25f0a04SGreg Roach * 79136779af1SGreg Roach * @return Collection<int,Family> 792a25f0a04SGreg Roach */ 793820b62dfSGreg Roach public function childStepFamilies(): Collection 794c1010edaSGreg Roach { 795ed5b6227SGreg Roach $step_families = new Collection(); 79639ca88baSGreg Roach $families = $this->childFamilies(); 797a25f0a04SGreg Roach foreach ($families as $family) { 798ed5b6227SGreg Roach foreach ($family->spouses() as $parent) { 799ed5b6227SGreg Roach foreach ($parent->spouseFamilies() as $step_family) { 80039ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 801ed5b6227SGreg Roach $step_families->add($step_family); 802a25f0a04SGreg Roach } 803a25f0a04SGreg Roach } 804a25f0a04SGreg Roach } 805a25f0a04SGreg Roach } 806a25f0a04SGreg Roach 807*f25fc0f9SGreg Roach return $step_families->uniqueStrict(static fn(Family $family): string => $family->xref()); 808a25f0a04SGreg Roach } 809a25f0a04SGreg Roach 810a25f0a04SGreg Roach /** 811a25f0a04SGreg Roach * Get a list of step-parent families. 812a25f0a04SGreg Roach * 81336779af1SGreg Roach * @return Collection<int,Family> 814a25f0a04SGreg Roach */ 815820b62dfSGreg Roach public function spouseStepFamilies(): Collection 816c1010edaSGreg Roach { 81713abd6f3SGreg Roach $step_families = []; 81839ca88baSGreg Roach $families = $this->spouseFamilies(); 819820b62dfSGreg Roach 820a25f0a04SGreg Roach foreach ($families as $family) { 82139ca88baSGreg Roach $spouse = $family->spouse($this); 822820b62dfSGreg Roach 823d823340dSGreg Roach if ($spouse instanceof self) { 82439ca88baSGreg Roach foreach ($family->spouse($this)->spouseFamilies() as $step_family) { 82539ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 826a25f0a04SGreg Roach $step_families[] = $step_family; 827a25f0a04SGreg Roach } 828a25f0a04SGreg Roach } 829a25f0a04SGreg Roach } 830a25f0a04SGreg Roach } 831a25f0a04SGreg Roach 832820b62dfSGreg Roach return new Collection($step_families); 833a25f0a04SGreg Roach } 834a25f0a04SGreg Roach 835a25f0a04SGreg Roach /** 836a25f0a04SGreg Roach * A label for a parental family group 837a25f0a04SGreg Roach * 838a25f0a04SGreg Roach * @param Family $family 839a25f0a04SGreg Roach * 840a25f0a04SGreg Roach * @return string 841a25f0a04SGreg Roach */ 842820b62dfSGreg Roach public function getChildFamilyLabel(Family $family): string 843c1010edaSGreg Roach { 8440e7e67a6SGreg Roach $fact = $this->facts(['FAMC'])->first(static fn (Fact $fact): bool => $fact->target() === $family); 8450e7e67a6SGreg Roach 8460e7e67a6SGreg Roach if ($fact instanceof Fact) { 8470e7e67a6SGreg Roach $pedigree = $fact->attribute('PEDI'); 8480e7e67a6SGreg Roach } else { 8490e7e67a6SGreg Roach $pedigree = ''; 8500e7e67a6SGreg Roach } 851b2ce94c6SRico Sonntag 8527d70e4a7SGreg Roach $values = [ 85388a03560SGreg Roach PedigreeLinkageType::VALUE_BIRTH => I18N::translate('Family with parents'), 85488a03560SGreg Roach PedigreeLinkageType::VALUE_ADOPTED => I18N::translate('Family with adoptive parents'), 85588a03560SGreg Roach PedigreeLinkageType::VALUE_FOSTER => I18N::translate('Family with foster parents'), 856665e281aSGreg Roach /* I18N: “sealing” is a Mormon ceremony. */ 85788a03560SGreg Roach PedigreeLinkageType::VALUE_SEALING => I18N::translate('Family with sealing parents'), 858665e281aSGreg Roach /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */ 85988a03560SGreg Roach PedigreeLinkageType::VALUE_RADA => I18N::translate('Family with rada parents'), 8607d70e4a7SGreg Roach ]; 8617d70e4a7SGreg Roach 86288a03560SGreg Roach return $values[$pedigree] ?? $values[PedigreeLinkageType::VALUE_BIRTH]; 863a25f0a04SGreg Roach } 864a25f0a04SGreg Roach 865a25f0a04SGreg Roach /** 866a25f0a04SGreg Roach * Create a label for a step family 867a25f0a04SGreg Roach * 868a25f0a04SGreg Roach * @param Family $step_family 869a25f0a04SGreg Roach * 870a25f0a04SGreg Roach * @return string 871a25f0a04SGreg Roach */ 8728f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 873c1010edaSGreg Roach { 87439ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 875a25f0a04SGreg Roach if ($family !== $step_family) { 876a25f0a04SGreg Roach // Must be a step-family 87739ca88baSGreg Roach foreach ($family->spouses() as $parent) { 87839ca88baSGreg Roach foreach ($step_family->spouses() as $step_parent) { 879a25f0a04SGreg Roach if ($parent === $step_parent) { 880a25f0a04SGreg Roach // One common parent - must be a step family 881e364afe4SGreg Roach if ($parent->sex() === 'M') { 882a25f0a04SGreg Roach // Father’s family with someone else 883b6ec1ccfSGreg Roach if ($step_family->spouse($step_parent) instanceof Individual) { 884a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 88539ca88baSGreg Roach return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName()); 886b2ce94c6SRico Sonntag } 887b2ce94c6SRico Sonntag 888a25f0a04SGreg Roach /* I18N: A step-family. */ 889bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 890a25f0a04SGreg Roach } 891b2ce94c6SRico Sonntag 892a25f0a04SGreg Roach // Mother’s family with someone else 893b6ec1ccfSGreg Roach if ($step_family->spouse($step_parent) instanceof Individual) { 894a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 89539ca88baSGreg Roach return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName()); 896b2ce94c6SRico Sonntag } 897b2ce94c6SRico Sonntag 898a25f0a04SGreg Roach /* I18N: A step-family. */ 899bbb76c12SGreg Roach return I18N::translate('Mother’s family with an unknown individual'); 900a25f0a04SGreg Roach } 901a25f0a04SGreg Roach } 902a25f0a04SGreg Roach } 903a25f0a04SGreg Roach } 904a25f0a04SGreg Roach } 905a25f0a04SGreg Roach 906a25f0a04SGreg Roach // Perahps same parents - but a different family record? 907a25f0a04SGreg Roach return I18N::translate('Family with parents'); 908a25f0a04SGreg Roach } 909a25f0a04SGreg Roach 910225e381fSGreg Roach /** 911225e381fSGreg Roach * Get the description for the family. 912225e381fSGreg Roach * 913225e381fSGreg Roach * For example, "XXX's family with new wife". 914225e381fSGreg Roach * 915225e381fSGreg Roach * @param Family $family 916225e381fSGreg Roach * 917225e381fSGreg Roach * @return string 918225e381fSGreg Roach */ 919e364afe4SGreg Roach public function getSpouseFamilyLabel(Family $family): string 920c1010edaSGreg Roach { 92139ca88baSGreg Roach $spouse = $family->spouse($this); 922b6ec1ccfSGreg Roach 923b6ec1ccfSGreg Roach if ($spouse instanceof Individual) { 924225e381fSGreg Roach /* I18N: %s is the spouse name */ 92539ca88baSGreg Roach return I18N::translate('Family with %s', $spouse->fullName()); 926225e381fSGreg Roach } 927b2ce94c6SRico Sonntag 92839ca88baSGreg Roach return $family->fullName(); 929225e381fSGreg Roach } 930225e381fSGreg Roach 931a25f0a04SGreg Roach /** 932961ec755SGreg Roach * If this object has no name, what do we call it? 933961ec755SGreg Roach * 934961ec755SGreg Roach * @return string 935961ec755SGreg Roach */ 9368f53f488SRico Sonntag public function getFallBackName(): string 937c1010edaSGreg Roach { 938a25f0a04SGreg Roach return '@P.N. /@N.N./'; 939a25f0a04SGreg Roach } 940a25f0a04SGreg Roach 941a25f0a04SGreg Roach /** 942a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 943a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 944a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 945a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 946a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 947a25f0a04SGreg Roach * recorded in the NAME field. e.g. 948a25f0a04SGreg Roach * 949a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 950a25f0a04SGreg Roach * 2 GIVN Robert 951a25f0a04SGreg Roach * 2 SPFX de 952a25f0a04SGreg Roach * 2 SURN CLITHEROW 953a25f0a04SGreg Roach * 2 NICK The Bald 954a25f0a04SGreg Roach * 955a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 956a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 957a25f0a04SGreg Roach * 958a25f0a04SGreg Roach * Handle multiple surnames, either as; 959a25f0a04SGreg Roach * 960a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 961a25f0a04SGreg Roach * or 962a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 963a25f0a04SGreg Roach * 2 GIVN Carlos 964a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 965a25f0a04SGreg Roach * 966a25f0a04SGreg Roach * @param string $type 96751928f9aSGreg Roach * @param string $value 968a25f0a04SGreg Roach * @param string $gedcom 969e364afe4SGreg Roach * 970e364afe4SGreg Roach * @return void 971a25f0a04SGreg Roach */ 97251928f9aSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 973c1010edaSGreg Roach { 974a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 975a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 976a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 977a25f0a04SGreg Roach 97876f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 979ef475b14SGreg Roach $GIVN = preg_match('/\n' . $sublevel . ' GIVN (.+)/', $gedcom, $match) === 1 ? $match[1] : ''; 980ef475b14SGreg Roach $SURN = preg_match('/\n' . $sublevel . ' SURN (.+)/', $gedcom, $match) === 1 ? $match[1] : ''; 981a25f0a04SGreg Roach 982a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 98376f666f4SGreg Roach if ($SURN !== '') { 984a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 985a25f0a04SGreg Roach } else { 98613abd6f3SGreg Roach $SURNS = []; 987a25f0a04SGreg Roach } 98876f666f4SGreg Roach 989a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 990a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 991a25f0a04SGreg Roach 992a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 993a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 994a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 995a25f0a04SGreg Roach 996a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 99751928f9aSGreg Roach if (substr_count($value, '/') % 2 === 1) { 99851928f9aSGreg Roach $value .= '/'; 999a25f0a04SGreg Roach } 1000a25f0a04SGreg Roach 1001a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 100251928f9aSGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $value); 1003a25f0a04SGreg Roach 1004a25f0a04SGreg Roach // Extract the surname. 1005a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 1006a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 1007a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 1008a25f0a04SGreg Roach } else { 1009a25f0a04SGreg Roach $surname = ''; 1010a25f0a04SGreg Roach } 1011a25f0a04SGreg Roach 1012a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 1013a25f0a04SGreg Roach if (!$SURNS) { 1014a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 1015a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 1016a25f0a04SGreg Roach $SURNS = $matches[1]; 1017a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 1018a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 1019a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 1020a25f0a04SGreg Roach } 1021a25f0a04SGreg Roach } else { 1022a25f0a04SGreg Roach // It is valid not to have a surname at all 102313abd6f3SGreg Roach $SURNS = ['']; 1024a25f0a04SGreg Roach } 1025a25f0a04SGreg Roach } 1026a25f0a04SGreg Roach 1027a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 1028a25f0a04SGreg Roach if (!$GIVN) { 1029c1010edaSGreg Roach // remove surname 1030c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?\/.*\/ ?/', ' ', $full); 1031c1010edaSGreg Roach // remove nickname 1032c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?".+"/', ' ', $GIVN); 1033c1010edaSGreg Roach // multiple spaces, caused by the above 1034c72b7fa4SGreg Roach $GIVN = preg_replace('/ {2,}/', ' ', $GIVN); 1035c1010edaSGreg Roach // leading/trailing spaces, caused by the above 1036c72b7fa4SGreg Roach $GIVN = preg_replace('/^ | $/', '', $GIVN); 1037a25f0a04SGreg Roach } 1038a25f0a04SGreg Roach 1039a25f0a04SGreg Roach // Add placeholder for unknown given name 1040a25f0a04SGreg Roach if (!$GIVN) { 1041d823340dSGreg Roach $GIVN = self::PRAENOMEN_NESCIO; 104273f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1043a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1044a25f0a04SGreg Roach } 1045a25f0a04SGreg Roach 1046a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1047a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1048a25f0a04SGreg Roach // $full is for display on-screen 1049a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1050a25f0a04SGreg Roach 1051a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1052d823340dSGreg Roach $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full); 1053d823340dSGreg Roach $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full); 1054c6f196c3SGreg Roach // Format for display 1055d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1056acc34ea1SGreg Roach // Localise quotation marks around the nickname 1057*f25fc0f9SGreg Roach $full = preg_replace_callback('/"([^&]*)"/', static fn(array $matches): string => '<q class="wt-nickname">' . $matches[1] . '</q>', $full); 1058a25f0a04SGreg Roach 1059c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1060ee51991cSGreg Roach $full = preg_replace('/([^ >\x{200C}]*)\*/u', '<span class="starredname">\\1</span>', $full); 1061a25f0a04SGreg Roach 1062a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1063a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1064a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1065a25f0a04SGreg Roach 1066ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1067a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1068e364afe4SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) { 1069a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1070e364afe4SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) { 1071a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1072a25f0a04SGreg Roach } 1073a25f0a04SGreg Roach 1074bdb3725aSGreg Roach $this->getAllNames[] = [ 1075a25f0a04SGreg Roach 'type' => $type, 1076a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1077c1010edaSGreg Roach 'full' => $full, 1078c1010edaSGreg Roach // This is used for display 1079c1010edaSGreg Roach 'fullNN' => $fullNN, 1080c1010edaSGreg Roach // This goes into the database 1081c1010edaSGreg Roach 'surname' => $surname, 1082c1010edaSGreg Roach // This goes into the database 1083c1010edaSGreg Roach 'givn' => $GIVN, 1084c1010edaSGreg Roach // This goes into the database 1085c1010edaSGreg Roach 'surn' => $SURN, 1086c1010edaSGreg Roach // This goes into the database 108713abd6f3SGreg Roach ]; 1088a25f0a04SGreg Roach } 1089a25f0a04SGreg Roach } 1090a25f0a04SGreg Roach 1091a25f0a04SGreg Roach /** 109276692c8bSGreg Roach * Extract names from the GEDCOM record. 1093c7ff4153SGreg Roach * 1094c7ff4153SGreg Roach * @return void 1095a25f0a04SGreg Roach */ 1096e364afe4SGreg Roach public function extractNames(): void 1097c1010edaSGreg Roach { 1098d9e083e7SGreg Roach $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree); 1099d9e083e7SGreg Roach 11008f53f488SRico Sonntag $this->extractNamesFromFacts( 11018f53f488SRico Sonntag 1, 11028f53f488SRico Sonntag 'NAME', 1103d9e083e7SGreg Roach $this->facts(['NAME'], false, $access_level) 11048f53f488SRico Sonntag ); 1105a25f0a04SGreg Roach } 1106a25f0a04SGreg Roach 1107a25f0a04SGreg Roach /** 1108a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1109a25f0a04SGreg Roach * selection items or favorites. 1110a25f0a04SGreg Roach * 1111a25f0a04SGreg Roach * @return string 1112a25f0a04SGreg Roach */ 11138f53f488SRico Sonntag public function formatListDetails(): string 1114c1010edaSGreg Roach { 1115a25f0a04SGreg Roach return 11168d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 11178d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1118a25f0a04SGreg Roach } 11198091bfd1SGreg Roach 11208091bfd1SGreg Roach /** 11218091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11228091bfd1SGreg Roach */ 11238091bfd1SGreg Roach public function lock(): void 11248091bfd1SGreg Roach { 11258091bfd1SGreg Roach DB::table('individuals') 11268091bfd1SGreg Roach ->where('i_file', '=', $this->tree->id()) 11278091bfd1SGreg Roach ->where('i_id', '=', $this->xref()) 11288091bfd1SGreg Roach ->lockForUpdate() 11298091bfd1SGreg Roach ->get(); 11308091bfd1SGreg Roach } 1131a25f0a04SGreg Roach} 1132