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 { 616c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 62c156e8f5SGreg Roach return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 63c156e8f5SGreg Roach }; 64c156e8f5SGreg Roach } 65c156e8f5SGreg Roach 66c156e8f5SGreg Roach /** 67c156e8f5SGreg Roach * A closure which will compare individuals by death date. 68c156e8f5SGreg Roach * 69c6921a17SGreg Roach * @return Closure(Individual,Individual):int 70c156e8f5SGreg Roach */ 71c156e8f5SGreg Roach public static function deathDateComparator(): Closure 72c156e8f5SGreg Roach { 736c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 7410e21aa9SGreg Roach return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate()); 75c156e8f5SGreg Roach }; 76c156e8f5SGreg Roach } 77c156e8f5SGreg Roach 78c156e8f5SGreg Roach /** 79a25f0a04SGreg Roach * Can the name of this record be shown? 80a25f0a04SGreg Roach * 8176692c8bSGreg Roach * @param int|null $access_level 8276692c8bSGreg Roach * 8376692c8bSGreg Roach * @return bool 84a25f0a04SGreg Roach */ 8535584196SGreg Roach public function canShowName(int $access_level = null): bool 86c1010edaSGreg Roach { 873529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 884b9ff166SGreg Roach 89f56b86d2SGreg Roach return (int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 90a25f0a04SGreg Roach } 91a25f0a04SGreg Roach 92a25f0a04SGreg Roach /** 9376692c8bSGreg Roach * Can this individual be shown? 94a25f0a04SGreg Roach * 9576692c8bSGreg Roach * @param int $access_level 9676692c8bSGreg Roach * 9776692c8bSGreg Roach * @return bool 98a25f0a04SGreg Roach */ 9935584196SGreg Roach protected function canShowByType(int $access_level): bool 100c1010edaSGreg Roach { 101a25f0a04SGreg Roach // Dead people... 102f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 103a25f0a04SGreg Roach $keep_alive = false; 10454ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 105b6ec1ccfSGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH !== 0) { 106dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 107a25f0a04SGreg Roach foreach ($matches as $match) { 108a25f0a04SGreg Roach $date = new Date($match[1]); 109a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 110a25f0a04SGreg Roach $keep_alive = true; 111a25f0a04SGreg Roach break; 112a25f0a04SGreg Roach } 113a25f0a04SGreg Roach } 114a25f0a04SGreg Roach } 11554ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 116b6ec1ccfSGreg Roach if ($KEEP_ALIVE_YEARS_DEATH !== 0) { 117dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 118a25f0a04SGreg Roach foreach ($matches as $match) { 119a25f0a04SGreg Roach $date = new Date($match[1]); 120a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 121a25f0a04SGreg Roach $keep_alive = true; 122a25f0a04SGreg Roach break; 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach } 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach if (!$keep_alive) { 127a25f0a04SGreg Roach return true; 128a25f0a04SGreg Roach } 129a25f0a04SGreg Roach } 130a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 1311fe542e9SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH); 1321fe542e9SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 1337c4add84SGreg Roach 13454ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1354b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 136a25f0a04SGreg Roach } 137a25f0a04SGreg Roach 138a25f0a04SGreg Roach // No restriction found - show living people to members only: 1394b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 140a25f0a04SGreg Roach } 141a25f0a04SGreg Roach 142a25f0a04SGreg Roach /** 143a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 144a25f0a04SGreg Roach * 145a25f0a04SGreg Roach * @param Individual $target 146cbc1590aSGreg Roach * @param int $distance 147a25f0a04SGreg Roach * 148cbc1590aSGreg Roach * @return bool 149a25f0a04SGreg Roach */ 15024f2a3afSGreg Roach private static function isRelated(Individual $target, int $distance): bool 151c1010edaSGreg Roach { 152a25f0a04SGreg Roach static $cache = null; 153a25f0a04SGreg Roach 1541fe542e9SGreg Roach $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree); 155b6ec1ccfSGreg Roach if ($user_individual instanceof Individual) { 156a25f0a04SGreg Roach if (!$cache) { 15713abd6f3SGreg Roach $cache = [ 15813abd6f3SGreg Roach 0 => [$user_individual], 15913abd6f3SGreg Roach 1 => [], 16013abd6f3SGreg Roach ]; 1618d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 162dc124885SGreg Roach $family = $fact->target(); 163e24444eeSGreg Roach if ($family instanceof Family) { 164a25f0a04SGreg Roach $cache[1][] = $family; 165a25f0a04SGreg Roach } 166a25f0a04SGreg Roach } 167a25f0a04SGreg Roach } 168a25f0a04SGreg Roach } else { 169a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 170a25f0a04SGreg Roach return true; 171a25f0a04SGreg Roach } 172a25f0a04SGreg Roach 173a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 174a25f0a04SGreg Roach $distance *= 2; 175a25f0a04SGreg Roach 176a25f0a04SGreg Roach // Consider each path length in turn 177a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 178a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 179a25f0a04SGreg Roach // We have already calculated all records with this length 180e364afe4SGreg Roach if ($n % 2 === 0 && in_array($target, $cache[$n], true)) { 181a25f0a04SGreg Roach return true; 182a25f0a04SGreg Roach } 183a25f0a04SGreg Roach } else { 184a25f0a04SGreg Roach // Need to calculate these paths 18513abd6f3SGreg Roach $cache[$n] = []; 186e364afe4SGreg Roach if ($n % 2 === 0) { 187a25f0a04SGreg Roach // Add FAM->INDI links 188a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 1898d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 190dc124885SGreg Roach $individual = $fact->target(); 191a25f0a04SGreg Roach // Don’t backtrack 192e364afe4SGreg Roach if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) { 193a25f0a04SGreg Roach $cache[$n][] = $individual; 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach } 197a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 198a25f0a04SGreg Roach return true; 199a25f0a04SGreg Roach } 200a25f0a04SGreg Roach } else { 201a25f0a04SGreg Roach // Add INDI->FAM links 202a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 2038d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 204dc124885SGreg Roach $family = $fact->target(); 205a25f0a04SGreg Roach // Don’t backtrack 206e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 207a25f0a04SGreg Roach $cache[$n][] = $family; 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach } 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach } 214a25f0a04SGreg Roach 215a25f0a04SGreg Roach return false; 216a25f0a04SGreg Roach } 217a25f0a04SGreg Roach 21876692c8bSGreg Roach /** 21976692c8bSGreg Roach * Generate a private version of this record 22076692c8bSGreg Roach * 22176692c8bSGreg Roach * @param int $access_level 22276692c8bSGreg Roach * 22376692c8bSGreg Roach * @return string 22476692c8bSGreg Roach */ 2253c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 226c1010edaSGreg Roach { 227acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 228a25f0a04SGreg Roach 229a25f0a04SGreg Roach $rec = '0 @' . $this->xref . '@ INDI'; 230f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) { 231a25f0a04SGreg Roach // Show all the NAME tags, including subtags 2328d0ebef0SGreg Roach foreach ($this->facts(['NAME']) as $fact) { 233138ca96cSGreg Roach $rec .= "\n" . $fact->gedcom(); 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach } 236a25f0a04SGreg Roach // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data 2378d0ebef0SGreg Roach preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 238a25f0a04SGreg Roach foreach ($matches as $match) { 2396b9cb339SGreg Roach $rela = Registry::familyFactory()->make($match[1], $this->tree); 240a25f0a04SGreg Roach if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 241a25f0a04SGreg Roach $rec .= $match[0]; 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach } 244a25f0a04SGreg Roach // Don’t privatize sex. 245a25f0a04SGreg Roach if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) { 246a25f0a04SGreg Roach $rec .= $match[0]; 247a25f0a04SGreg Roach } 248a25f0a04SGreg Roach 249a25f0a04SGreg Roach return $rec; 250a25f0a04SGreg Roach } 251a25f0a04SGreg Roach 25276692c8bSGreg Roach /** 253a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 254a25f0a04SGreg Roach * If not known to be dead, then assume living. 255a25f0a04SGreg Roach * 256cbc1590aSGreg Roach * @return bool 257a25f0a04SGreg Roach */ 2588f53f488SRico Sonntag public function isDead(): bool 259c1010edaSGreg Roach { 260c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 261d97083feSGreg Roach $today_jd = Registry::timestampFactory()->now()->julianDay(); 262a25f0a04SGreg Roach 263a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 2648d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 265a25f0a04SGreg Roach return true; 266a25f0a04SGreg Roach } 267a25f0a04SGreg Roach 268e63974caSStefan Weil // If any event occurred more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 269a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 270a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 271a25f0a04SGreg Roach $date = new Date($date_match); 272269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) { 273a25f0a04SGreg Roach return true; 274a25f0a04SGreg Roach } 275a25f0a04SGreg Roach } 276a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 277a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 278a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 279a25f0a04SGreg Roach return false; 280a25f0a04SGreg Roach } 281a25f0a04SGreg Roach } 282a25f0a04SGreg Roach 283a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 284a25f0a04SGreg Roach 285a25f0a04SGreg Roach // Check parents (birth and adopted) 28639ca88baSGreg Roach foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) { 28739ca88baSGreg Roach foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) { 288a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 289a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 290a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 291a25f0a04SGreg Roach $date = new Date($date_match); 292269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) { 293a25f0a04SGreg Roach return true; 294a25f0a04SGreg Roach } 295a25f0a04SGreg Roach } 296a25f0a04SGreg Roach } 297a25f0a04SGreg Roach } 298a25f0a04SGreg Roach 299a25f0a04SGreg Roach // Check spouses 30039ca88baSGreg Roach foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) { 301a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 302a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 303a25f0a04SGreg Roach $date = new Date($date_match); 304a25f0a04SGreg Roach // Assume marriage occurs after age of 10 305269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) { 306a25f0a04SGreg Roach return true; 307a25f0a04SGreg Roach } 308a25f0a04SGreg Roach } 309a25f0a04SGreg Roach // Check spouse dates 31039ca88baSGreg Roach $spouse = $family->spouse($this, Auth::PRIV_HIDE); 311a25f0a04SGreg Roach if ($spouse) { 312a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 313a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 314a25f0a04SGreg Roach $date = new Date($date_match); 315a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 316269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) { 317a25f0a04SGreg Roach return true; 318a25f0a04SGreg Roach } 319a25f0a04SGreg Roach } 320a25f0a04SGreg Roach } 321a25f0a04SGreg Roach // Check child dates 32239ca88baSGreg Roach foreach ($family->children(Auth::PRIV_HIDE) as $child) { 323a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 324a25f0a04SGreg Roach // Assume children born after age of 15 325a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 326a25f0a04SGreg Roach $date = new Date($date_match); 327269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) { 328a25f0a04SGreg Roach return true; 329a25f0a04SGreg Roach } 330a25f0a04SGreg Roach } 331a25f0a04SGreg Roach // Check grandchildren 33239ca88baSGreg Roach foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) { 33339ca88baSGreg Roach foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) { 334a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 335a25f0a04SGreg Roach // Assume grandchildren born after age of 30 336a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 337a25f0a04SGreg Roach $date = new Date($date_match); 338269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) { 339a25f0a04SGreg Roach return true; 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach } 342a25f0a04SGreg Roach } 343a25f0a04SGreg Roach } 344a25f0a04SGreg Roach } 345a25f0a04SGreg Roach } 346a25f0a04SGreg Roach 347a25f0a04SGreg Roach return false; 348a25f0a04SGreg Roach } 349a25f0a04SGreg Roach 350a25f0a04SGreg Roach /** 351a25f0a04SGreg Roach * Find the highlighted media object for an individual 352a25f0a04SGreg Roach * 353e364afe4SGreg Roach * @return MediaFile|null 354a25f0a04SGreg Roach */ 355e364afe4SGreg Roach public function findHighlightedMediaFile(): ?MediaFile 356c1010edaSGreg Roach { 35792647683SGreg Roach $fact = $this->facts(['OBJE']) 35819d319e7SGreg Roach ->first(static function (Fact $fact): bool { 359dc124885SGreg Roach $media = $fact->target(); 36019d319e7SGreg Roach 36119d319e7SGreg Roach return $media instanceof Media && $media->firstImageFile() instanceof MediaFile; 36219d319e7SGreg Roach }); 36319d319e7SGreg Roach 36492647683SGreg Roach if ($fact instanceof Fact && $fact->target() instanceof Media) { 36592647683SGreg Roach return $fact->target()->firstImageFile(); 366a25f0a04SGreg Roach } 367a25f0a04SGreg Roach 368a25f0a04SGreg Roach return null; 369a25f0a04SGreg Roach } 370a25f0a04SGreg Roach 371a25f0a04SGreg Roach /** 3727b0d562eSGreg Roach * Display the preferred image for this individual. 373a25f0a04SGreg Roach * Use an icon if no image is available. 374a25f0a04SGreg Roach * 375dce07401SGreg Roach * @param int $width Pixels 376dce07401SGreg Roach * @param int $height Pixels 377dce07401SGreg Roach * @param string $fit "crop" or "contain" 37809482a55SGreg Roach * @param array<string> $attributes Additional HTML attributes 379dce07401SGreg Roach * 380a25f0a04SGreg Roach * @return string 381a25f0a04SGreg Roach */ 38224f2a3afSGreg Roach public function displayImage(int $width, int $height, string $fit, array $attributes): string 383c1010edaSGreg Roach { 3844a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 3854a9f750fSGreg Roach 3864a9f750fSGreg Roach if ($media_file !== null) { 3874a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 388a25f0a04SGreg Roach } 3894a9f750fSGreg Roach 390b6ec1ccfSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE') === '1') { 39168b631f1SGreg Roach return '<i class="icon-silhouette icon-silhouette-' . strtolower($this->sex()) . ' wt-icon-flip-rtl"></i>'; 3924a9f750fSGreg Roach } 3934a9f750fSGreg Roach 3944a9f750fSGreg Roach return ''; 395a25f0a04SGreg Roach } 396a25f0a04SGreg Roach 397a25f0a04SGreg Roach /** 398a25f0a04SGreg Roach * Get the date of birth 399a25f0a04SGreg Roach * 400a25f0a04SGreg Roach * @return Date 401a25f0a04SGreg Roach */ 4028f53f488SRico Sonntag public function getBirthDate(): Date 403c1010edaSGreg Roach { 404a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 405a25f0a04SGreg Roach if ($date->isOK()) { 406a25f0a04SGreg Roach return $date; 407a25f0a04SGreg Roach } 408a25f0a04SGreg Roach } 409a25f0a04SGreg Roach 410a25f0a04SGreg Roach return new Date(''); 411a25f0a04SGreg Roach } 412a25f0a04SGreg Roach 413a25f0a04SGreg Roach /** 414a25f0a04SGreg Roach * Get the place of birth 415a25f0a04SGreg Roach * 41616d0b7f7SRico Sonntag * @return Place 417a25f0a04SGreg Roach */ 4188f53f488SRico Sonntag public function getBirthPlace(): Place 419c1010edaSGreg Roach { 420a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 421a25f0a04SGreg Roach return $place; 422a25f0a04SGreg Roach } 423a25f0a04SGreg Roach 424b20ddbf9SGreg Roach return new Place('', $this->tree); 425a25f0a04SGreg Roach } 426a25f0a04SGreg Roach 427a25f0a04SGreg Roach /** 428a25f0a04SGreg Roach * Get the date of death 429a25f0a04SGreg Roach * 430a25f0a04SGreg Roach * @return Date 431a25f0a04SGreg Roach */ 4328f53f488SRico Sonntag public function getDeathDate(): Date 433c1010edaSGreg Roach { 434a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 435a25f0a04SGreg Roach if ($date->isOK()) { 436a25f0a04SGreg Roach return $date; 437a25f0a04SGreg Roach } 438a25f0a04SGreg Roach } 439a25f0a04SGreg Roach 440a25f0a04SGreg Roach return new Date(''); 441a25f0a04SGreg Roach } 442a25f0a04SGreg Roach 443a25f0a04SGreg Roach /** 444a25f0a04SGreg Roach * Get the place of death 445a25f0a04SGreg Roach * 44616d0b7f7SRico Sonntag * @return Place 447a25f0a04SGreg Roach */ 4488f53f488SRico Sonntag public function getDeathPlace(): Place 449c1010edaSGreg Roach { 450a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 451a25f0a04SGreg Roach return $place; 452a25f0a04SGreg Roach } 453a25f0a04SGreg Roach 454b20ddbf9SGreg Roach return new Place('', $this->tree); 455a25f0a04SGreg Roach } 456a25f0a04SGreg Roach 457a25f0a04SGreg Roach /** 458a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 45915d603e7SGreg Roach * Provide the place and full date using a tooltip. 460a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 461a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 462a25f0a04SGreg Roach * 463a25f0a04SGreg Roach * @return string 464a25f0a04SGreg Roach */ 4655e6816beSGreg Roach public function lifespan(): string 466c1010edaSGreg Roach { 46753f5ca04SGreg Roach // Just the first part of the place name. 468392561bbSGreg Roach $birth_place = strip_tags($this->getBirthPlace()->shortName()); 469392561bbSGreg Roach $death_place = strip_tags($this->getDeathPlace()->shortName()); 47053f5ca04SGreg Roach 47181b9dc5dSGreg Roach // Remove markup from dates. Use UTF_FSI / UTF_PDI instead of <bdi></bdi>, as 47281b9dc5dSGreg Roach // we cannot use HTML markup in title attributes. 47381b9dc5dSGreg Roach $birth_date = "\u{2068}" . strip_tags($this->getBirthDate()->display()) . "\u{2069}"; 47481b9dc5dSGreg Roach $death_date = "\u{2068}" . strip_tags($this->getDeathDate()->display()) . "\u{2069}"; 47515d603e7SGreg Roach 47653f5ca04SGreg Roach // Use minimum and maximum dates - to agree with the age calculations. 47753f5ca04SGreg Roach $birth_year = $this->getBirthDate()->minimumDate()->format('%Y'); 47853f5ca04SGreg Roach $death_year = $this->getDeathDate()->maximumDate()->format('%Y'); 47953f5ca04SGreg Roach 480f94b830fSGreg Roach if ($birth_year === '') { 481f94b830fSGreg Roach $birth_year = I18N::translate('…'); 482f94b830fSGreg Roach } 483f94b830fSGreg Roach 4847ecbeefdSGreg Roach if ($death_year === '' && $this->isDead()) { 4857ecbeefdSGreg Roach $death_year = I18N::translate('…'); 4867ecbeefdSGreg Roach } 4877ecbeefdSGreg Roach 488c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 489dacfe33cSGreg Roach return I18N::translate( 490a25f0a04SGreg Roach '%1$s–%2$s', 49153f5ca04SGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $birth_year . '</span>', 49253f5ca04SGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $death_year . '</span>' 493a25f0a04SGreg Roach ); 494a25f0a04SGreg Roach } 495a25f0a04SGreg Roach 496a25f0a04SGreg Roach /** 497a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 498a25f0a04SGreg Roach * 49909482a55SGreg Roach * @return array<Date> 500a25f0a04SGreg Roach */ 5018f53f488SRico Sonntag public function getAllBirthDates(): array 502c1010edaSGreg Roach { 5038d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 504d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 505d240bb87SGreg Roach 506d240bb87SGreg Roach if ($dates !== []) { 507d240bb87SGreg Roach return $dates; 508a25f0a04SGreg Roach } 509a25f0a04SGreg Roach } 510a25f0a04SGreg Roach 51113abd6f3SGreg Roach return []; 512a25f0a04SGreg Roach } 513a25f0a04SGreg Roach 514a25f0a04SGreg Roach /** 515a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 516a25f0a04SGreg Roach * 51709482a55SGreg Roach * @return array<Place> 518a25f0a04SGreg Roach */ 5198f53f488SRico Sonntag public function getAllBirthPlaces(): array 520c1010edaSGreg Roach { 5218d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 5228d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 523d240bb87SGreg Roach 52454c1ab5eSGreg Roach if ($places !== []) { 5254080d558SGreg Roach return $places; 526a25f0a04SGreg Roach } 527a25f0a04SGreg Roach } 528a25f0a04SGreg Roach 52913abd6f3SGreg Roach return []; 530a25f0a04SGreg Roach } 531a25f0a04SGreg Roach 532a25f0a04SGreg Roach /** 533a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 534a25f0a04SGreg Roach * 53509482a55SGreg Roach * @return array<Date> 536a25f0a04SGreg Roach */ 5378f53f488SRico Sonntag public function getAllDeathDates(): array 538c1010edaSGreg Roach { 5398d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 540d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 541d240bb87SGreg Roach 542d240bb87SGreg Roach if ($dates !== []) { 543d240bb87SGreg Roach return $dates; 544a25f0a04SGreg Roach } 545a25f0a04SGreg Roach } 546a25f0a04SGreg Roach 54713abd6f3SGreg Roach return []; 548a25f0a04SGreg Roach } 549a25f0a04SGreg Roach 550a25f0a04SGreg Roach /** 551a25f0a04SGreg Roach * Get all the death places - for the individual lists. 552a25f0a04SGreg Roach * 55309482a55SGreg Roach * @return array<Place> 554a25f0a04SGreg Roach */ 5558f53f488SRico Sonntag public function getAllDeathPlaces(): array 556c1010edaSGreg Roach { 5578d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 5588d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 559d240bb87SGreg Roach 56054c1ab5eSGreg Roach if ($places !== []) { 5614080d558SGreg Roach return $places; 562a25f0a04SGreg Roach } 563a25f0a04SGreg Roach } 564a25f0a04SGreg Roach 56513abd6f3SGreg Roach return []; 566a25f0a04SGreg Roach } 567a25f0a04SGreg Roach 568a25f0a04SGreg Roach /** 569a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 570a25f0a04SGreg Roach * 571a25f0a04SGreg Roach * @return Date 572a25f0a04SGreg Roach */ 5738f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 574c1010edaSGreg Roach { 5758f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 576a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 577a25f0a04SGreg Roach if ($date->isOK()) { 5784686330aSGreg Roach $this->estimated_birth_date = $date; 579a25f0a04SGreg Roach break; 580a25f0a04SGreg Roach } 581a25f0a04SGreg Roach } 5828f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 58313abd6f3SGreg Roach $min = []; 58413abd6f3SGreg Roach $max = []; 585a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 586f5b60decSGreg Roach if ($tmp->isOK()) { 587*6bd19c8cSGreg Roach $min[] = $tmp->minimumJulianDay() - 365 * (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 588f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 589a25f0a04SGreg Roach } 59039ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 591a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 592f5b60decSGreg Roach if ($tmp->isOK()) { 593*6bd19c8cSGreg Roach $min[] = $tmp->maximumJulianDay() - 365; 594f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 595a25f0a04SGreg Roach } 59639ca88baSGreg Roach $husband = $family->husband(); 597e364afe4SGreg Roach if ($husband instanceof self) { 5982e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 599f5b60decSGreg Roach if ($tmp->isOK()) { 600f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 601f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 602a25f0a04SGreg Roach } 603a25f0a04SGreg Roach } 60439ca88baSGreg Roach $wife = $family->wife(); 605e364afe4SGreg Roach if ($wife instanceof self) { 6062e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 607f5b60decSGreg Roach if ($tmp->isOK()) { 608f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 609f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 610a25f0a04SGreg Roach } 611a25f0a04SGreg Roach } 61239ca88baSGreg Roach foreach ($family->children() as $child) { 613a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 614f5b60decSGreg Roach if ($tmp->isOK()) { 615f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 616f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 617a25f0a04SGreg Roach } 618a25f0a04SGreg Roach } 619a25f0a04SGreg Roach } 62039ca88baSGreg Roach foreach ($this->spouseFamilies() as $family) { 621a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 622f5b60decSGreg Roach if ($tmp->isOK()) { 623f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 624f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 625a25f0a04SGreg Roach } 62639ca88baSGreg Roach $spouse = $family->spouse($this); 627a25f0a04SGreg Roach if ($spouse) { 628a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 629f5b60decSGreg Roach if ($tmp->isOK()) { 630f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 631f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 632a25f0a04SGreg Roach } 633a25f0a04SGreg Roach } 63439ca88baSGreg Roach foreach ($family->children() as $child) { 635a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 636f5b60decSGreg Roach if ($tmp->isOK()) { 637e364afe4SGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65); 638f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 639a25f0a04SGreg Roach } 640a25f0a04SGreg Roach } 641a25f0a04SGreg Roach } 642a25f0a04SGreg Roach if ($min && $max) { 64359f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 644a25f0a04SGreg Roach 64565e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 6464686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 647a25f0a04SGreg Roach } else { 6484686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 649a25f0a04SGreg Roach } 650a25f0a04SGreg Roach } 651a25f0a04SGreg Roach } 652a25f0a04SGreg Roach 6534686330aSGreg Roach return $this->estimated_birth_date; 654a25f0a04SGreg Roach } 655a25f0a04SGreg Roach 656a25f0a04SGreg Roach /** 657a25f0a04SGreg Roach * Generate an estimated date of death. 658a25f0a04SGreg Roach * 659a25f0a04SGreg Roach * @return Date 660a25f0a04SGreg Roach */ 6618f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 662c1010edaSGreg Roach { 6634686330aSGreg Roach if ($this->estimated_death_date === null) { 664a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 665a25f0a04SGreg Roach if ($date->isOK()) { 6664686330aSGreg Roach $this->estimated_death_date = $date; 667a25f0a04SGreg Roach break; 668a25f0a04SGreg Roach } 669a25f0a04SGreg Roach } 6704686330aSGreg Roach if ($this->estimated_death_date === null) { 671b6ec1ccfSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay() !== 0) { 672c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 6734686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 674a25f0a04SGreg Roach } else { 6754686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 676a25f0a04SGreg Roach } 677a25f0a04SGreg Roach } 678a25f0a04SGreg Roach } 679a25f0a04SGreg Roach 6804686330aSGreg Roach return $this->estimated_death_date; 681a25f0a04SGreg Roach } 682a25f0a04SGreg Roach 683a25f0a04SGreg Roach /** 684a25f0a04SGreg Roach * Get the sex - M F or U 685a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 686a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 687a25f0a04SGreg Roach * 688a25f0a04SGreg Roach * @return string 689a25f0a04SGreg Roach */ 690e364afe4SGreg Roach public function sex(): string 691c1010edaSGreg Roach { 6921baf69deSGreg Roach if (preg_match('/\n1 SEX ([MFX])/', $this->gedcom . $this->pending, $match)) { 693a25f0a04SGreg Roach return $match[1]; 694a25f0a04SGreg Roach } 695b2ce94c6SRico Sonntag 696b2ce94c6SRico Sonntag return 'U'; 697a25f0a04SGreg Roach } 698a25f0a04SGreg Roach 699a25f0a04SGreg Roach /** 700a25f0a04SGreg Roach * Get a list of this individual’s spouse families 701a25f0a04SGreg Roach * 702cbc1590aSGreg Roach * @param int|null $access_level 703a25f0a04SGreg Roach * 70436779af1SGreg Roach * @return Collection<int,Family> 705a25f0a04SGreg Roach */ 70673d58381SGreg Roach public function spouseFamilies(int $access_level = null): Collection 707c1010edaSGreg Roach { 7083529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 709d9e083e7SGreg Roach 710d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 711d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 7124b9ff166SGreg Roach } 7134b9ff166SGreg Roach 71439ca88baSGreg Roach $families = new Collection(); 715d9e083e7SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level) as $fact) { 716dc124885SGreg Roach $family = $fact->target(); 717d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 71839ca88baSGreg Roach $families->push($family); 719a25f0a04SGreg Roach } 720a25f0a04SGreg Roach } 721a25f0a04SGreg Roach 72239ca88baSGreg Roach return new Collection($families); 723a25f0a04SGreg Roach } 724a25f0a04SGreg Roach 725a25f0a04SGreg Roach /** 726a25f0a04SGreg Roach * Get the current spouse of this individual. 727a25f0a04SGreg Roach * 728a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 729a25f0a04SGreg Roach * in chronological order, and take the last one found. 730a25f0a04SGreg Roach * 731a25f0a04SGreg Roach * @return Individual|null 732a25f0a04SGreg Roach */ 733e364afe4SGreg Roach public function getCurrentSpouse(): ?Individual 734c1010edaSGreg Roach { 73539ca88baSGreg Roach $family = $this->spouseFamilies()->last(); 73639ca88baSGreg Roach 73739ca88baSGreg Roach if ($family instanceof Family) { 73839ca88baSGreg Roach return $family->spouse($this); 739a25f0a04SGreg Roach } 740b2ce94c6SRico Sonntag 741b2ce94c6SRico Sonntag return null; 742a25f0a04SGreg Roach } 743a25f0a04SGreg Roach 744a25f0a04SGreg Roach /** 745a25f0a04SGreg Roach * Count the children belonging to this individual. 746a25f0a04SGreg Roach * 747cbc1590aSGreg Roach * @return int 748a25f0a04SGreg Roach */ 749e364afe4SGreg Roach public function numberOfChildren(): int 750c1010edaSGreg Roach { 7517d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 7523dc7bbe9SGreg Roach return (int) $match[1]; 753b2ce94c6SRico Sonntag } 754b2ce94c6SRico Sonntag 75513abd6f3SGreg Roach $children = []; 75639ca88baSGreg Roach foreach ($this->spouseFamilies() as $fam) { 75739ca88baSGreg Roach foreach ($fam->children() as $child) { 758c0935879SGreg Roach $children[$child->xref()] = true; 759a25f0a04SGreg Roach } 760a25f0a04SGreg Roach } 761a25f0a04SGreg Roach 762a25f0a04SGreg Roach return count($children); 763a25f0a04SGreg Roach } 764a25f0a04SGreg Roach 765a25f0a04SGreg Roach /** 766a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 767a25f0a04SGreg Roach * 768cbc1590aSGreg Roach * @param int|null $access_level 769a25f0a04SGreg Roach * 77036779af1SGreg Roach * @return Collection<int,Family> 771a25f0a04SGreg Roach */ 77273d58381SGreg Roach public function childFamilies(int $access_level = null): Collection 773c1010edaSGreg Roach { 7743529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 7754b9ff166SGreg Roach 776d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 777d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 778d9e083e7SGreg Roach } 779a25f0a04SGreg Roach 78039ca88baSGreg Roach $families = new Collection(); 78139ca88baSGreg Roach 782d9e083e7SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level) as $fact) { 783dc124885SGreg Roach $family = $fact->target(); 784d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 78539ca88baSGreg Roach $families->push($family); 786a25f0a04SGreg Roach } 787a25f0a04SGreg Roach } 788a25f0a04SGreg Roach 789a25f0a04SGreg Roach return $families; 790a25f0a04SGreg Roach } 791a25f0a04SGreg Roach 792a25f0a04SGreg Roach /** 793a25f0a04SGreg Roach * Get a list of step-parent families. 794a25f0a04SGreg Roach * 79536779af1SGreg Roach * @return Collection<int,Family> 796a25f0a04SGreg Roach */ 797820b62dfSGreg Roach public function childStepFamilies(): Collection 798c1010edaSGreg Roach { 799ed5b6227SGreg Roach $step_families = new Collection(); 80039ca88baSGreg Roach $families = $this->childFamilies(); 801a25f0a04SGreg Roach foreach ($families as $family) { 802ed5b6227SGreg Roach foreach ($family->spouses() as $parent) { 803ed5b6227SGreg Roach foreach ($parent->spouseFamilies() as $step_family) { 80439ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 805ed5b6227SGreg Roach $step_families->add($step_family); 806a25f0a04SGreg Roach } 807a25f0a04SGreg Roach } 808a25f0a04SGreg Roach } 809a25f0a04SGreg Roach } 810a25f0a04SGreg Roach 8118c627a69SGreg Roach return $step_families->uniqueStrict(static function (Family $family): string { 8128c627a69SGreg Roach return $family->xref(); 8138c627a69SGreg Roach }); 814a25f0a04SGreg Roach } 815a25f0a04SGreg Roach 816a25f0a04SGreg Roach /** 817a25f0a04SGreg Roach * Get a list of step-parent families. 818a25f0a04SGreg Roach * 81936779af1SGreg Roach * @return Collection<int,Family> 820a25f0a04SGreg Roach */ 821820b62dfSGreg Roach public function spouseStepFamilies(): Collection 822c1010edaSGreg Roach { 82313abd6f3SGreg Roach $step_families = []; 82439ca88baSGreg Roach $families = $this->spouseFamilies(); 825820b62dfSGreg Roach 826a25f0a04SGreg Roach foreach ($families as $family) { 82739ca88baSGreg Roach $spouse = $family->spouse($this); 828820b62dfSGreg Roach 829d823340dSGreg Roach if ($spouse instanceof self) { 83039ca88baSGreg Roach foreach ($family->spouse($this)->spouseFamilies() as $step_family) { 83139ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 832a25f0a04SGreg Roach $step_families[] = $step_family; 833a25f0a04SGreg Roach } 834a25f0a04SGreg Roach } 835a25f0a04SGreg Roach } 836a25f0a04SGreg Roach } 837a25f0a04SGreg Roach 838820b62dfSGreg Roach return new Collection($step_families); 839a25f0a04SGreg Roach } 840a25f0a04SGreg Roach 841a25f0a04SGreg Roach /** 842a25f0a04SGreg Roach * A label for a parental family group 843a25f0a04SGreg Roach * 844a25f0a04SGreg Roach * @param Family $family 845a25f0a04SGreg Roach * 846a25f0a04SGreg Roach * @return string 847a25f0a04SGreg Roach */ 848820b62dfSGreg Roach public function getChildFamilyLabel(Family $family): string 849c1010edaSGreg Roach { 8500e7e67a6SGreg Roach $fact = $this->facts(['FAMC'])->first(static fn (Fact $fact): bool => $fact->target() === $family); 8510e7e67a6SGreg Roach 8520e7e67a6SGreg Roach if ($fact instanceof Fact) { 8530e7e67a6SGreg Roach $pedigree = $fact->attribute('PEDI'); 8540e7e67a6SGreg Roach } else { 8550e7e67a6SGreg Roach $pedigree = ''; 8560e7e67a6SGreg Roach } 857b2ce94c6SRico Sonntag 8587d70e4a7SGreg Roach $values = [ 85988a03560SGreg Roach PedigreeLinkageType::VALUE_BIRTH => I18N::translate('Family with parents'), 86088a03560SGreg Roach PedigreeLinkageType::VALUE_ADOPTED => I18N::translate('Family with adoptive parents'), 86188a03560SGreg Roach PedigreeLinkageType::VALUE_FOSTER => I18N::translate('Family with foster parents'), 862665e281aSGreg Roach /* I18N: “sealing” is a Mormon ceremony. */ 86388a03560SGreg Roach PedigreeLinkageType::VALUE_SEALING => I18N::translate('Family with sealing parents'), 864665e281aSGreg Roach /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */ 86588a03560SGreg Roach PedigreeLinkageType::VALUE_RADA => I18N::translate('Family with rada parents'), 8667d70e4a7SGreg Roach ]; 8677d70e4a7SGreg Roach 86888a03560SGreg Roach return $values[$pedigree] ?? $values[PedigreeLinkageType::VALUE_BIRTH]; 869a25f0a04SGreg Roach } 870a25f0a04SGreg Roach 871a25f0a04SGreg Roach /** 872a25f0a04SGreg Roach * Create a label for a step family 873a25f0a04SGreg Roach * 874a25f0a04SGreg Roach * @param Family $step_family 875a25f0a04SGreg Roach * 876a25f0a04SGreg Roach * @return string 877a25f0a04SGreg Roach */ 8788f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 879c1010edaSGreg Roach { 88039ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 881a25f0a04SGreg Roach if ($family !== $step_family) { 882a25f0a04SGreg Roach // Must be a step-family 88339ca88baSGreg Roach foreach ($family->spouses() as $parent) { 88439ca88baSGreg Roach foreach ($step_family->spouses() as $step_parent) { 885a25f0a04SGreg Roach if ($parent === $step_parent) { 886a25f0a04SGreg Roach // One common parent - must be a step family 887e364afe4SGreg Roach if ($parent->sex() === 'M') { 888a25f0a04SGreg Roach // Father’s family with someone else 889b6ec1ccfSGreg Roach if ($step_family->spouse($step_parent) instanceof Individual) { 890a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 89139ca88baSGreg Roach return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName()); 892b2ce94c6SRico Sonntag } 893b2ce94c6SRico Sonntag 894a25f0a04SGreg Roach /* I18N: A step-family. */ 895bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 896a25f0a04SGreg Roach } 897b2ce94c6SRico Sonntag 898a25f0a04SGreg Roach // Mother’s family with someone else 899b6ec1ccfSGreg Roach if ($step_family->spouse($step_parent) instanceof Individual) { 900a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 90139ca88baSGreg Roach return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName()); 902b2ce94c6SRico Sonntag } 903b2ce94c6SRico Sonntag 904a25f0a04SGreg Roach /* I18N: A step-family. */ 905bbb76c12SGreg Roach return I18N::translate('Mother’s family with an unknown individual'); 906a25f0a04SGreg Roach } 907a25f0a04SGreg Roach } 908a25f0a04SGreg Roach } 909a25f0a04SGreg Roach } 910a25f0a04SGreg Roach } 911a25f0a04SGreg Roach 912a25f0a04SGreg Roach // Perahps same parents - but a different family record? 913a25f0a04SGreg Roach return I18N::translate('Family with parents'); 914a25f0a04SGreg Roach } 915a25f0a04SGreg Roach 916225e381fSGreg Roach /** 917225e381fSGreg Roach * Get the description for the family. 918225e381fSGreg Roach * 919225e381fSGreg Roach * For example, "XXX's family with new wife". 920225e381fSGreg Roach * 921225e381fSGreg Roach * @param Family $family 922225e381fSGreg Roach * 923225e381fSGreg Roach * @return string 924225e381fSGreg Roach */ 925e364afe4SGreg Roach public function getSpouseFamilyLabel(Family $family): string 926c1010edaSGreg Roach { 92739ca88baSGreg Roach $spouse = $family->spouse($this); 928b6ec1ccfSGreg Roach 929b6ec1ccfSGreg Roach if ($spouse instanceof Individual) { 930225e381fSGreg Roach /* I18N: %s is the spouse name */ 93139ca88baSGreg Roach return I18N::translate('Family with %s', $spouse->fullName()); 932225e381fSGreg Roach } 933b2ce94c6SRico Sonntag 93439ca88baSGreg Roach return $family->fullName(); 935225e381fSGreg Roach } 936225e381fSGreg Roach 937a25f0a04SGreg Roach /** 938961ec755SGreg Roach * If this object has no name, what do we call it? 939961ec755SGreg Roach * 940961ec755SGreg Roach * @return string 941961ec755SGreg Roach */ 9428f53f488SRico Sonntag public function getFallBackName(): string 943c1010edaSGreg Roach { 944a25f0a04SGreg Roach return '@P.N. /@N.N./'; 945a25f0a04SGreg Roach } 946a25f0a04SGreg Roach 947a25f0a04SGreg Roach /** 948a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 949a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 950a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 951a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 952a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 953a25f0a04SGreg Roach * recorded in the NAME field. e.g. 954a25f0a04SGreg Roach * 955a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 956a25f0a04SGreg Roach * 2 GIVN Robert 957a25f0a04SGreg Roach * 2 SPFX de 958a25f0a04SGreg Roach * 2 SURN CLITHEROW 959a25f0a04SGreg Roach * 2 NICK The Bald 960a25f0a04SGreg Roach * 961a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 962a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 963a25f0a04SGreg Roach * 964a25f0a04SGreg Roach * Handle multiple surnames, either as; 965a25f0a04SGreg Roach * 966a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 967a25f0a04SGreg Roach * or 968a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 969a25f0a04SGreg Roach * 2 GIVN Carlos 970a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 971a25f0a04SGreg Roach * 972a25f0a04SGreg Roach * @param string $type 97351928f9aSGreg Roach * @param string $value 974a25f0a04SGreg Roach * @param string $gedcom 975e364afe4SGreg Roach * 976e364afe4SGreg Roach * @return void 977a25f0a04SGreg Roach */ 97851928f9aSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 979c1010edaSGreg Roach { 980a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 981a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 982a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 983a25f0a04SGreg Roach 98476f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 985ef475b14SGreg Roach $GIVN = preg_match('/\n' . $sublevel . ' GIVN (.+)/', $gedcom, $match) === 1 ? $match[1] : ''; 986ef475b14SGreg Roach $SURN = preg_match('/\n' . $sublevel . ' SURN (.+)/', $gedcom, $match) === 1 ? $match[1] : ''; 987a25f0a04SGreg Roach 988a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 98976f666f4SGreg Roach if ($SURN !== '') { 990a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 991a25f0a04SGreg Roach } else { 99213abd6f3SGreg Roach $SURNS = []; 993a25f0a04SGreg Roach } 99476f666f4SGreg Roach 995a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 996a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 997a25f0a04SGreg Roach 998a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 999a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 1000a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1001a25f0a04SGreg Roach 1002a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 100351928f9aSGreg Roach if (substr_count($value, '/') % 2 === 1) { 100451928f9aSGreg Roach $value .= '/'; 1005a25f0a04SGreg Roach } 1006a25f0a04SGreg Roach 1007a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 100851928f9aSGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $value); 1009a25f0a04SGreg Roach 1010a25f0a04SGreg Roach // Extract the surname. 1011a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 1012a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 1013a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 1014a25f0a04SGreg Roach } else { 1015a25f0a04SGreg Roach $surname = ''; 1016a25f0a04SGreg Roach } 1017a25f0a04SGreg Roach 1018a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 1019a25f0a04SGreg Roach if (!$SURNS) { 1020a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 1021a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 1022a25f0a04SGreg Roach $SURNS = $matches[1]; 1023a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 1024a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 1025a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 1026a25f0a04SGreg Roach } 1027a25f0a04SGreg Roach } else { 1028a25f0a04SGreg Roach // It is valid not to have a surname at all 102913abd6f3SGreg Roach $SURNS = ['']; 1030a25f0a04SGreg Roach } 1031a25f0a04SGreg Roach } 1032a25f0a04SGreg Roach 1033a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 1034a25f0a04SGreg Roach if (!$GIVN) { 1035c1010edaSGreg Roach // remove surname 1036c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?\/.*\/ ?/', ' ', $full); 1037c1010edaSGreg Roach // remove nickname 1038c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?".+"/', ' ', $GIVN); 1039c1010edaSGreg Roach // multiple spaces, caused by the above 1040c72b7fa4SGreg Roach $GIVN = preg_replace('/ {2,}/', ' ', $GIVN); 1041c1010edaSGreg Roach // leading/trailing spaces, caused by the above 1042c72b7fa4SGreg Roach $GIVN = preg_replace('/^ | $/', '', $GIVN); 1043a25f0a04SGreg Roach } 1044a25f0a04SGreg Roach 1045a25f0a04SGreg Roach // Add placeholder for unknown given name 1046a25f0a04SGreg Roach if (!$GIVN) { 1047d823340dSGreg Roach $GIVN = self::PRAENOMEN_NESCIO; 104873f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1049a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1050a25f0a04SGreg Roach } 1051a25f0a04SGreg Roach 1052a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1053a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1054a25f0a04SGreg Roach // $full is for display on-screen 1055a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1056a25f0a04SGreg Roach 1057a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1058d823340dSGreg Roach $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full); 1059d823340dSGreg Roach $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full); 1060c6f196c3SGreg Roach // Format for display 1061d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1062acc34ea1SGreg Roach // Localise quotation marks around the nickname 10630b5fd0a6SGreg Roach $full = preg_replace_callback('/"([^&]*)"/', static function (array $matches): string { 1064c652cdbdSGreg Roach return '<q class="wt-nickname">' . $matches[1] . '</q>'; 10658d68cabeSGreg Roach }, $full); 1066a25f0a04SGreg Roach 1067c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1068ee51991cSGreg Roach $full = preg_replace('/([^ >\x{200C}]*)\*/u', '<span class="starredname">\\1</span>', $full); 1069a25f0a04SGreg Roach 1070a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1071a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1072a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1073a25f0a04SGreg Roach 1074ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1075a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1076e364afe4SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) { 1077a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1078e364afe4SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) { 1079a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1080a25f0a04SGreg Roach } 1081a25f0a04SGreg Roach 1082bdb3725aSGreg Roach $this->getAllNames[] = [ 1083a25f0a04SGreg Roach 'type' => $type, 1084a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1085c1010edaSGreg Roach 'full' => $full, 1086c1010edaSGreg Roach // This is used for display 1087c1010edaSGreg Roach 'fullNN' => $fullNN, 1088c1010edaSGreg Roach // This goes into the database 1089c1010edaSGreg Roach 'surname' => $surname, 1090c1010edaSGreg Roach // This goes into the database 1091c1010edaSGreg Roach 'givn' => $GIVN, 1092c1010edaSGreg Roach // This goes into the database 1093c1010edaSGreg Roach 'surn' => $SURN, 1094c1010edaSGreg Roach // This goes into the database 109513abd6f3SGreg Roach ]; 1096a25f0a04SGreg Roach } 1097a25f0a04SGreg Roach } 1098a25f0a04SGreg Roach 1099a25f0a04SGreg Roach /** 110076692c8bSGreg Roach * Extract names from the GEDCOM record. 1101c7ff4153SGreg Roach * 1102c7ff4153SGreg Roach * @return void 1103a25f0a04SGreg Roach */ 1104e364afe4SGreg Roach public function extractNames(): void 1105c1010edaSGreg Roach { 1106d9e083e7SGreg Roach $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree); 1107d9e083e7SGreg Roach 11088f53f488SRico Sonntag $this->extractNamesFromFacts( 11098f53f488SRico Sonntag 1, 11108f53f488SRico Sonntag 'NAME', 1111d9e083e7SGreg Roach $this->facts(['NAME'], false, $access_level) 11128f53f488SRico Sonntag ); 1113a25f0a04SGreg Roach } 1114a25f0a04SGreg Roach 1115a25f0a04SGreg Roach /** 1116a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1117a25f0a04SGreg Roach * selection items or favorites. 1118a25f0a04SGreg Roach * 1119a25f0a04SGreg Roach * @return string 1120a25f0a04SGreg Roach */ 11218f53f488SRico Sonntag public function formatListDetails(): string 1122c1010edaSGreg Roach { 1123a25f0a04SGreg Roach return 11248d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 11258d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1126a25f0a04SGreg Roach } 11278091bfd1SGreg Roach 11288091bfd1SGreg Roach /** 11298091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11308091bfd1SGreg Roach */ 11318091bfd1SGreg Roach public function lock(): void 11328091bfd1SGreg Roach { 11338091bfd1SGreg Roach DB::table('individuals') 11348091bfd1SGreg Roach ->where('i_file', '=', $this->tree->id()) 11358091bfd1SGreg Roach ->where('i_id', '=', $this->xref()) 11368091bfd1SGreg Roach ->lockForUpdate() 11378091bfd1SGreg Roach ->get(); 11388091bfd1SGreg Roach } 1139a25f0a04SGreg Roach} 1140