1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 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; 272e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2839ca88baSGreg Roachuse Illuminate\Support\Collection; 29a25f0a04SGreg Roach 307d70e4a7SGreg Roachuse function preg_match; 317d70e4a7SGreg Roach 32a25f0a04SGreg Roach/** 3376692c8bSGreg Roach * A GEDCOM individual (INDI) object. 34a25f0a04SGreg Roach */ 35c1010edaSGreg Roachclass Individual extends GedcomRecord 36c1010edaSGreg Roach{ 3716d6367aSGreg Roach public const RECORD_TYPE = 'INDI'; 3816d6367aSGreg Roach 398fb4e87cSGreg Roach // Placeholders to indicate unknown names 408fb4e87cSGreg Roach public const NOMEN_NESCIO = '@N.N.'; 418fb4e87cSGreg Roach public const PRAENOMEN_NESCIO = '@P.N.'; 428fb4e87cSGreg Roach 43852ede8cSGreg Roach protected const ROUTE_NAME = IndividualPage::class; 44a25f0a04SGreg Roach 457fa97a69SGreg Roach /** Used in some lists to keep track of this individual’s generation in that list */ 467fa97a69SGreg Roach public ?int $generation = null; 47a25f0a04SGreg Roach 487fa97a69SGreg Roach private ?Date $estimated_birth_date = null; 49a25f0a04SGreg Roach 507fa97a69SGreg Roach private ?Date $estimated_death_date = null; 51a25f0a04SGreg Roach 52a25f0a04SGreg Roach /** 53c156e8f5SGreg Roach * A closure which will compare individuals by birth date. 54c156e8f5SGreg Roach * 55c156e8f5SGreg Roach * @return Closure 56c156e8f5SGreg Roach */ 57c156e8f5SGreg Roach public static function birthDateComparator(): Closure 58c156e8f5SGreg Roach { 596c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 60c156e8f5SGreg Roach return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 61c156e8f5SGreg Roach }; 62c156e8f5SGreg Roach } 63c156e8f5SGreg Roach 64c156e8f5SGreg Roach /** 65c156e8f5SGreg Roach * A closure which will compare individuals by death date. 66c156e8f5SGreg Roach * 67c156e8f5SGreg Roach * @return Closure 68c156e8f5SGreg Roach */ 69c156e8f5SGreg Roach public static function deathDateComparator(): Closure 70c156e8f5SGreg Roach { 716c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 7210e21aa9SGreg Roach return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate()); 73c156e8f5SGreg Roach }; 74c156e8f5SGreg Roach } 75c156e8f5SGreg Roach 76c156e8f5SGreg Roach /** 77a25f0a04SGreg Roach * Can the name of this record be shown? 78a25f0a04SGreg Roach * 7976692c8bSGreg Roach * @param int|null $access_level 8076692c8bSGreg Roach * 8176692c8bSGreg Roach * @return bool 82a25f0a04SGreg Roach */ 8335584196SGreg Roach public function canShowName(int $access_level = null): bool 84c1010edaSGreg Roach { 85d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 864b9ff166SGreg Roach 87f56b86d2SGreg Roach return (int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 88a25f0a04SGreg Roach } 89a25f0a04SGreg Roach 90a25f0a04SGreg Roach /** 9176692c8bSGreg Roach * Can this individual be shown? 92a25f0a04SGreg Roach * 9376692c8bSGreg Roach * @param int $access_level 9476692c8bSGreg Roach * 9576692c8bSGreg Roach * @return bool 96a25f0a04SGreg Roach */ 9735584196SGreg Roach protected function canShowByType(int $access_level): bool 98c1010edaSGreg Roach { 99a25f0a04SGreg Roach // Dead people... 100f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 101a25f0a04SGreg Roach $keep_alive = false; 10254ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 103a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH) { 104dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 105a25f0a04SGreg Roach foreach ($matches as $match) { 106a25f0a04SGreg Roach $date = new Date($match[1]); 107a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 108a25f0a04SGreg Roach $keep_alive = true; 109a25f0a04SGreg Roach break; 110a25f0a04SGreg Roach } 111a25f0a04SGreg Roach } 112a25f0a04SGreg Roach } 11354ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 114a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_DEATH) { 115dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 116a25f0a04SGreg Roach foreach ($matches as $match) { 117a25f0a04SGreg Roach $date = new Date($match[1]); 118a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 119a25f0a04SGreg Roach $keep_alive = true; 120a25f0a04SGreg Roach break; 121a25f0a04SGreg Roach } 122a25f0a04SGreg Roach } 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach if (!$keep_alive) { 125a25f0a04SGreg Roach return true; 126a25f0a04SGreg Roach } 127a25f0a04SGreg Roach } 128a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 1291fe542e9SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH); 1301fe542e9SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 1317c4add84SGreg Roach 13254ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1334b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 134a25f0a04SGreg Roach } 135a25f0a04SGreg Roach 136a25f0a04SGreg Roach // No restriction found - show living people to members only: 1374b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 138a25f0a04SGreg Roach } 139a25f0a04SGreg Roach 140a25f0a04SGreg Roach /** 141a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 142a25f0a04SGreg Roach * 143a25f0a04SGreg Roach * @param Individual $target 144cbc1590aSGreg Roach * @param int $distance 145a25f0a04SGreg Roach * 146cbc1590aSGreg Roach * @return bool 147a25f0a04SGreg Roach */ 14824f2a3afSGreg Roach private static function isRelated(Individual $target, int $distance): bool 149c1010edaSGreg Roach { 150a25f0a04SGreg Roach static $cache = null; 151a25f0a04SGreg Roach 1521fe542e9SGreg Roach $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree); 153a25f0a04SGreg Roach if ($user_individual) { 154a25f0a04SGreg Roach if (!$cache) { 15513abd6f3SGreg Roach $cache = [ 15613abd6f3SGreg Roach 0 => [$user_individual], 15713abd6f3SGreg Roach 1 => [], 15813abd6f3SGreg Roach ]; 1598d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 160dc124885SGreg Roach $family = $fact->target(); 161e24444eeSGreg Roach if ($family instanceof Family) { 162a25f0a04SGreg Roach $cache[1][] = $family; 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach } 165a25f0a04SGreg Roach } 166a25f0a04SGreg Roach } else { 167a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 168a25f0a04SGreg Roach return true; 169a25f0a04SGreg Roach } 170a25f0a04SGreg Roach 171a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 172a25f0a04SGreg Roach $distance *= 2; 173a25f0a04SGreg Roach 174a25f0a04SGreg Roach // Consider each path length in turn 175a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 176a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 177a25f0a04SGreg Roach // We have already calculated all records with this length 178e364afe4SGreg Roach if ($n % 2 === 0 && in_array($target, $cache[$n], true)) { 179a25f0a04SGreg Roach return true; 180a25f0a04SGreg Roach } 181a25f0a04SGreg Roach } else { 182a25f0a04SGreg Roach // Need to calculate these paths 18313abd6f3SGreg Roach $cache[$n] = []; 184e364afe4SGreg Roach if ($n % 2 === 0) { 185a25f0a04SGreg Roach // Add FAM->INDI links 186a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 1878d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 188dc124885SGreg Roach $individual = $fact->target(); 189a25f0a04SGreg Roach // Don’t backtrack 190e364afe4SGreg Roach if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) { 191a25f0a04SGreg Roach $cache[$n][] = $individual; 192a25f0a04SGreg Roach } 193a25f0a04SGreg Roach } 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 196a25f0a04SGreg Roach return true; 197a25f0a04SGreg Roach } 198a25f0a04SGreg Roach } else { 199a25f0a04SGreg Roach // Add INDI->FAM links 200a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 2018d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 202dc124885SGreg Roach $family = $fact->target(); 203a25f0a04SGreg Roach // Don’t backtrack 204e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 205a25f0a04SGreg Roach $cache[$n][] = $family; 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach } 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach } 212a25f0a04SGreg Roach 213a25f0a04SGreg Roach return false; 214a25f0a04SGreg Roach } 215a25f0a04SGreg Roach 21676692c8bSGreg Roach /** 21776692c8bSGreg Roach * Generate a private version of this record 21876692c8bSGreg Roach * 21976692c8bSGreg Roach * @param int $access_level 22076692c8bSGreg Roach * 22176692c8bSGreg Roach * @return string 22276692c8bSGreg Roach */ 2233c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 224c1010edaSGreg Roach { 225acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 226a25f0a04SGreg Roach 227a25f0a04SGreg Roach $rec = '0 @' . $this->xref . '@ INDI'; 228f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) { 229a25f0a04SGreg Roach // Show all the NAME tags, including subtags 2308d0ebef0SGreg Roach foreach ($this->facts(['NAME']) as $fact) { 231138ca96cSGreg Roach $rec .= "\n" . $fact->gedcom(); 232a25f0a04SGreg Roach } 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data 2358d0ebef0SGreg Roach preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 236a25f0a04SGreg Roach foreach ($matches as $match) { 2376b9cb339SGreg Roach $rela = Registry::familyFactory()->make($match[1], $this->tree); 238a25f0a04SGreg Roach if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 239a25f0a04SGreg Roach $rec .= $match[0]; 240a25f0a04SGreg Roach } 241a25f0a04SGreg Roach } 242a25f0a04SGreg Roach // Don’t privatize sex. 243a25f0a04SGreg Roach if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) { 244a25f0a04SGreg Roach $rec .= $match[0]; 245a25f0a04SGreg Roach } 246a25f0a04SGreg Roach 247a25f0a04SGreg Roach return $rec; 248a25f0a04SGreg Roach } 249a25f0a04SGreg Roach 25076692c8bSGreg Roach /** 251a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 252a25f0a04SGreg Roach * If not known to be dead, then assume living. 253a25f0a04SGreg Roach * 254cbc1590aSGreg Roach * @return bool 255a25f0a04SGreg Roach */ 2568f53f488SRico Sonntag public function isDead(): bool 257c1010edaSGreg Roach { 258c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 259d97083feSGreg Roach $today_jd = Registry::timestampFactory()->now()->julianDay(); 260a25f0a04SGreg Roach 261a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 2628d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 263a25f0a04SGreg Roach return true; 264a25f0a04SGreg Roach } 265a25f0a04SGreg Roach 266a25f0a04SGreg Roach // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 267a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 268a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 269a25f0a04SGreg Roach $date = new Date($date_match); 270269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) { 271a25f0a04SGreg Roach return true; 272a25f0a04SGreg Roach } 273a25f0a04SGreg Roach } 274a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 275a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 276a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 277a25f0a04SGreg Roach return false; 278a25f0a04SGreg Roach } 279a25f0a04SGreg Roach } 280a25f0a04SGreg Roach 281a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 282a25f0a04SGreg Roach 283a25f0a04SGreg Roach // Check parents (birth and adopted) 28439ca88baSGreg Roach foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) { 28539ca88baSGreg Roach foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) { 286a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 287a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 288a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 289a25f0a04SGreg Roach $date = new Date($date_match); 290269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) { 291a25f0a04SGreg Roach return true; 292a25f0a04SGreg Roach } 293a25f0a04SGreg Roach } 294a25f0a04SGreg Roach } 295a25f0a04SGreg Roach } 296a25f0a04SGreg Roach 297a25f0a04SGreg Roach // Check spouses 29839ca88baSGreg Roach foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) { 299a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 300a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 301a25f0a04SGreg Roach $date = new Date($date_match); 302a25f0a04SGreg Roach // Assume marriage occurs after age of 10 303269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) { 304a25f0a04SGreg Roach return true; 305a25f0a04SGreg Roach } 306a25f0a04SGreg Roach } 307a25f0a04SGreg Roach // Check spouse dates 30839ca88baSGreg Roach $spouse = $family->spouse($this, Auth::PRIV_HIDE); 309a25f0a04SGreg Roach if ($spouse) { 310a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 311a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 312a25f0a04SGreg Roach $date = new Date($date_match); 313a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 314269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) { 315a25f0a04SGreg Roach return true; 316a25f0a04SGreg Roach } 317a25f0a04SGreg Roach } 318a25f0a04SGreg Roach } 319a25f0a04SGreg Roach // Check child dates 32039ca88baSGreg Roach foreach ($family->children(Auth::PRIV_HIDE) as $child) { 321a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 322a25f0a04SGreg Roach // Assume children born after age of 15 323a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 324a25f0a04SGreg Roach $date = new Date($date_match); 325269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) { 326a25f0a04SGreg Roach return true; 327a25f0a04SGreg Roach } 328a25f0a04SGreg Roach } 329a25f0a04SGreg Roach // Check grandchildren 33039ca88baSGreg Roach foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) { 33139ca88baSGreg Roach foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) { 332a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 333a25f0a04SGreg Roach // Assume grandchildren born after age of 30 334a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 335a25f0a04SGreg Roach $date = new Date($date_match); 336269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) { 337a25f0a04SGreg Roach return true; 338a25f0a04SGreg Roach } 339a25f0a04SGreg Roach } 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach } 342a25f0a04SGreg Roach } 343a25f0a04SGreg Roach } 344a25f0a04SGreg Roach 345a25f0a04SGreg Roach return false; 346a25f0a04SGreg Roach } 347a25f0a04SGreg Roach 348a25f0a04SGreg Roach /** 349a25f0a04SGreg Roach * Find the highlighted media object for an individual 350a25f0a04SGreg Roach * 351e364afe4SGreg Roach * @return MediaFile|null 352a25f0a04SGreg Roach */ 353e364afe4SGreg Roach public function findHighlightedMediaFile(): ?MediaFile 354c1010edaSGreg Roach { 35592647683SGreg Roach $fact = $this->facts(['OBJE']) 35619d319e7SGreg Roach ->first(static function (Fact $fact): bool { 357dc124885SGreg Roach $media = $fact->target(); 35819d319e7SGreg Roach 35919d319e7SGreg Roach return $media instanceof Media && $media->firstImageFile() instanceof MediaFile; 36019d319e7SGreg Roach }); 36119d319e7SGreg Roach 36292647683SGreg Roach if ($fact instanceof Fact && $fact->target() instanceof Media) { 36392647683SGreg Roach return $fact->target()->firstImageFile(); 364a25f0a04SGreg Roach } 365a25f0a04SGreg Roach 366a25f0a04SGreg Roach return null; 367a25f0a04SGreg Roach } 368a25f0a04SGreg Roach 369a25f0a04SGreg Roach /** 3707b0d562eSGreg Roach * Display the preferred image for this individual. 371a25f0a04SGreg Roach * Use an icon if no image is available. 372a25f0a04SGreg Roach * 373dce07401SGreg Roach * @param int $width Pixels 374dce07401SGreg Roach * @param int $height Pixels 375dce07401SGreg Roach * @param string $fit "crop" or "contain" 37609482a55SGreg Roach * @param array<string> $attributes Additional HTML attributes 377dce07401SGreg Roach * 378a25f0a04SGreg Roach * @return string 379a25f0a04SGreg Roach */ 38024f2a3afSGreg Roach public function displayImage(int $width, int $height, string $fit, array $attributes): string 381c1010edaSGreg Roach { 3824a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 3834a9f750fSGreg Roach 3844a9f750fSGreg Roach if ($media_file !== null) { 3854a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 386a25f0a04SGreg Roach } 3874a9f750fSGreg Roach 3884a9f750fSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE')) { 38968b631f1SGreg Roach return '<i class="icon-silhouette icon-silhouette-' . strtolower($this->sex()) . ' wt-icon-flip-rtl"></i>'; 3904a9f750fSGreg Roach } 3914a9f750fSGreg Roach 3924a9f750fSGreg Roach return ''; 393a25f0a04SGreg Roach } 394a25f0a04SGreg Roach 395a25f0a04SGreg Roach /** 396a25f0a04SGreg Roach * Get the date of birth 397a25f0a04SGreg Roach * 398a25f0a04SGreg Roach * @return Date 399a25f0a04SGreg Roach */ 4008f53f488SRico Sonntag public function getBirthDate(): Date 401c1010edaSGreg Roach { 402a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 403a25f0a04SGreg Roach if ($date->isOK()) { 404a25f0a04SGreg Roach return $date; 405a25f0a04SGreg Roach } 406a25f0a04SGreg Roach } 407a25f0a04SGreg Roach 408a25f0a04SGreg Roach return new Date(''); 409a25f0a04SGreg Roach } 410a25f0a04SGreg Roach 411a25f0a04SGreg Roach /** 412a25f0a04SGreg Roach * Get the place of birth 413a25f0a04SGreg Roach * 41416d0b7f7SRico Sonntag * @return Place 415a25f0a04SGreg Roach */ 4168f53f488SRico Sonntag public function getBirthPlace(): Place 417c1010edaSGreg Roach { 418a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 419a25f0a04SGreg Roach return $place; 420a25f0a04SGreg Roach } 421a25f0a04SGreg Roach 422b20ddbf9SGreg Roach return new Place('', $this->tree); 423a25f0a04SGreg Roach } 424a25f0a04SGreg Roach 425a25f0a04SGreg Roach /** 426a25f0a04SGreg Roach * Get the date of death 427a25f0a04SGreg Roach * 428a25f0a04SGreg Roach * @return Date 429a25f0a04SGreg Roach */ 4308f53f488SRico Sonntag public function getDeathDate(): Date 431c1010edaSGreg Roach { 432a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 433a25f0a04SGreg Roach if ($date->isOK()) { 434a25f0a04SGreg Roach return $date; 435a25f0a04SGreg Roach } 436a25f0a04SGreg Roach } 437a25f0a04SGreg Roach 438a25f0a04SGreg Roach return new Date(''); 439a25f0a04SGreg Roach } 440a25f0a04SGreg Roach 441a25f0a04SGreg Roach /** 442a25f0a04SGreg Roach * Get the place of death 443a25f0a04SGreg Roach * 44416d0b7f7SRico Sonntag * @return Place 445a25f0a04SGreg Roach */ 4468f53f488SRico Sonntag public function getDeathPlace(): Place 447c1010edaSGreg Roach { 448a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 449a25f0a04SGreg Roach return $place; 450a25f0a04SGreg Roach } 451a25f0a04SGreg Roach 452b20ddbf9SGreg Roach return new Place('', $this->tree); 453a25f0a04SGreg Roach } 454a25f0a04SGreg Roach 455a25f0a04SGreg Roach /** 456a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 45715d603e7SGreg Roach * Provide the place and full date using a tooltip. 458a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 459a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 460a25f0a04SGreg Roach * 461a25f0a04SGreg Roach * @return string 462a25f0a04SGreg Roach */ 4635e6816beSGreg Roach public function lifespan(): string 464c1010edaSGreg Roach { 46553f5ca04SGreg Roach // Just the first part of the place name. 466392561bbSGreg Roach $birth_place = strip_tags($this->getBirthPlace()->shortName()); 467392561bbSGreg Roach $death_place = strip_tags($this->getDeathPlace()->shortName()); 46853f5ca04SGreg Roach 46981b9dc5dSGreg Roach // Remove markup from dates. Use UTF_FSI / UTF_PDI instead of <bdi></bdi>, as 47081b9dc5dSGreg Roach // we cannot use HTML markup in title attributes. 47181b9dc5dSGreg Roach $birth_date = "\u{2068}" . strip_tags($this->getBirthDate()->display()) . "\u{2069}"; 47281b9dc5dSGreg Roach $death_date = "\u{2068}" . strip_tags($this->getDeathDate()->display()) . "\u{2069}"; 47315d603e7SGreg Roach 47453f5ca04SGreg Roach // Use minimum and maximum dates - to agree with the age calculations. 47553f5ca04SGreg Roach $birth_year = $this->getBirthDate()->minimumDate()->format('%Y'); 47653f5ca04SGreg Roach $death_year = $this->getDeathDate()->maximumDate()->format('%Y'); 47753f5ca04SGreg Roach 478c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 479dacfe33cSGreg Roach return I18N::translate( 480a25f0a04SGreg Roach '%1$s–%2$s', 48153f5ca04SGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $birth_year . '</span>', 48253f5ca04SGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $death_year . '</span>' 483a25f0a04SGreg Roach ); 484a25f0a04SGreg Roach } 485a25f0a04SGreg Roach 486a25f0a04SGreg Roach /** 487a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 488a25f0a04SGreg Roach * 48909482a55SGreg Roach * @return array<Date> 490a25f0a04SGreg Roach */ 4918f53f488SRico Sonntag public function getAllBirthDates(): array 492c1010edaSGreg Roach { 4938d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 494d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 495d240bb87SGreg Roach 496d240bb87SGreg Roach if ($dates !== []) { 497d240bb87SGreg Roach return $dates; 498a25f0a04SGreg Roach } 499a25f0a04SGreg Roach } 500a25f0a04SGreg Roach 50113abd6f3SGreg Roach return []; 502a25f0a04SGreg Roach } 503a25f0a04SGreg Roach 504a25f0a04SGreg Roach /** 505a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 506a25f0a04SGreg Roach * 50709482a55SGreg Roach * @return array<Place> 508a25f0a04SGreg Roach */ 5098f53f488SRico Sonntag public function getAllBirthPlaces(): array 510c1010edaSGreg Roach { 5118d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 5128d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 513d240bb87SGreg Roach 51454c1ab5eSGreg Roach if ($places !== []) { 5154080d558SGreg Roach return $places; 516a25f0a04SGreg Roach } 517a25f0a04SGreg Roach } 518a25f0a04SGreg Roach 51913abd6f3SGreg Roach return []; 520a25f0a04SGreg Roach } 521a25f0a04SGreg Roach 522a25f0a04SGreg Roach /** 523a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 524a25f0a04SGreg Roach * 52509482a55SGreg Roach * @return array<Date> 526a25f0a04SGreg Roach */ 5278f53f488SRico Sonntag public function getAllDeathDates(): array 528c1010edaSGreg Roach { 5298d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 530d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 531d240bb87SGreg Roach 532d240bb87SGreg Roach if ($dates !== []) { 533d240bb87SGreg Roach return $dates; 534a25f0a04SGreg Roach } 535a25f0a04SGreg Roach } 536a25f0a04SGreg Roach 53713abd6f3SGreg Roach return []; 538a25f0a04SGreg Roach } 539a25f0a04SGreg Roach 540a25f0a04SGreg Roach /** 541a25f0a04SGreg Roach * Get all the death places - for the individual lists. 542a25f0a04SGreg Roach * 54309482a55SGreg Roach * @return array<Place> 544a25f0a04SGreg Roach */ 5458f53f488SRico Sonntag public function getAllDeathPlaces(): array 546c1010edaSGreg Roach { 5478d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 5488d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 549d240bb87SGreg Roach 55054c1ab5eSGreg Roach if ($places !== []) { 5514080d558SGreg Roach return $places; 552a25f0a04SGreg Roach } 553a25f0a04SGreg Roach } 554a25f0a04SGreg Roach 55513abd6f3SGreg Roach return []; 556a25f0a04SGreg Roach } 557a25f0a04SGreg Roach 558a25f0a04SGreg Roach /** 559a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 560a25f0a04SGreg Roach * 561a25f0a04SGreg Roach * @return Date 562a25f0a04SGreg Roach */ 5638f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 564c1010edaSGreg Roach { 5658f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 566a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 567a25f0a04SGreg Roach if ($date->isOK()) { 5684686330aSGreg Roach $this->estimated_birth_date = $date; 569a25f0a04SGreg Roach break; 570a25f0a04SGreg Roach } 571a25f0a04SGreg Roach } 5728f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 57313abd6f3SGreg Roach $min = []; 57413abd6f3SGreg Roach $max = []; 575a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 576f5b60decSGreg Roach if ($tmp->isOK()) { 577f5b60decSGreg Roach $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365; 578f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 579a25f0a04SGreg Roach } 58039ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 581a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 582f5b60decSGreg Roach if ($tmp->isOK()) { 583f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 1; 584f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 585a25f0a04SGreg Roach } 58639ca88baSGreg Roach $husband = $family->husband(); 587e364afe4SGreg Roach if ($husband instanceof self) { 5882e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 589f5b60decSGreg Roach if ($tmp->isOK()) { 590f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 591f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 592a25f0a04SGreg Roach } 593a25f0a04SGreg Roach } 59439ca88baSGreg Roach $wife = $family->wife(); 595e364afe4SGreg Roach if ($wife instanceof self) { 5962e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 597f5b60decSGreg Roach if ($tmp->isOK()) { 598f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 599f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 600a25f0a04SGreg Roach } 601a25f0a04SGreg Roach } 60239ca88baSGreg Roach foreach ($family->children() as $child) { 603a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 604f5b60decSGreg Roach if ($tmp->isOK()) { 605f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 606f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 607a25f0a04SGreg Roach } 608a25f0a04SGreg Roach } 609a25f0a04SGreg Roach } 61039ca88baSGreg Roach foreach ($this->spouseFamilies() as $family) { 611a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 612f5b60decSGreg Roach if ($tmp->isOK()) { 613f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 614f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 615a25f0a04SGreg Roach } 61639ca88baSGreg Roach $spouse = $family->spouse($this); 617a25f0a04SGreg Roach if ($spouse) { 618a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 619f5b60decSGreg Roach if ($tmp->isOK()) { 620f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 621f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 622a25f0a04SGreg Roach } 623a25f0a04SGreg Roach } 62439ca88baSGreg Roach foreach ($family->children() as $child) { 625a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 626f5b60decSGreg Roach if ($tmp->isOK()) { 627e364afe4SGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65); 628f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 629a25f0a04SGreg Roach } 630a25f0a04SGreg Roach } 631a25f0a04SGreg Roach } 632a25f0a04SGreg Roach if ($min && $max) { 63359f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 634a25f0a04SGreg Roach 63565e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 6364686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 637a25f0a04SGreg Roach } else { 6384686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 639a25f0a04SGreg Roach } 640a25f0a04SGreg Roach } 641a25f0a04SGreg Roach } 642a25f0a04SGreg Roach 6434686330aSGreg Roach return $this->estimated_birth_date; 644a25f0a04SGreg Roach } 645a25f0a04SGreg Roach 646a25f0a04SGreg Roach /** 647a25f0a04SGreg Roach * Generate an estimated date of death. 648a25f0a04SGreg Roach * 649a25f0a04SGreg Roach * @return Date 650a25f0a04SGreg Roach */ 6518f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 652c1010edaSGreg Roach { 6534686330aSGreg Roach if ($this->estimated_death_date === null) { 654a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 655a25f0a04SGreg Roach if ($date->isOK()) { 6564686330aSGreg Roach $this->estimated_death_date = $date; 657a25f0a04SGreg Roach break; 658a25f0a04SGreg Roach } 659a25f0a04SGreg Roach } 6604686330aSGreg Roach if ($this->estimated_death_date === null) { 661f5b60decSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay()) { 662c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 6634686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 664a25f0a04SGreg Roach } else { 6654686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 666a25f0a04SGreg Roach } 667a25f0a04SGreg Roach } 668a25f0a04SGreg Roach } 669a25f0a04SGreg Roach 6704686330aSGreg Roach return $this->estimated_death_date; 671a25f0a04SGreg Roach } 672a25f0a04SGreg Roach 673a25f0a04SGreg Roach /** 674a25f0a04SGreg Roach * Get the sex - M F or U 675a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 676a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 677a25f0a04SGreg Roach * 678a25f0a04SGreg Roach * @return string 679a25f0a04SGreg Roach */ 680e364afe4SGreg Roach public function sex(): string 681c1010edaSGreg Roach { 6821baf69deSGreg Roach if (preg_match('/\n1 SEX ([MFX])/', $this->gedcom . $this->pending, $match)) { 683a25f0a04SGreg Roach return $match[1]; 684a25f0a04SGreg Roach } 685b2ce94c6SRico Sonntag 686b2ce94c6SRico Sonntag return 'U'; 687a25f0a04SGreg Roach } 688a25f0a04SGreg Roach 689a25f0a04SGreg Roach /** 690a25f0a04SGreg Roach * Get a list of this individual’s spouse families 691a25f0a04SGreg Roach * 692cbc1590aSGreg Roach * @param int|null $access_level 693a25f0a04SGreg Roach * 69436779af1SGreg Roach * @return Collection<int,Family> 695a25f0a04SGreg Roach */ 69673d58381SGreg Roach public function spouseFamilies(int $access_level = null): Collection 697c1010edaSGreg Roach { 698d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 699d9e083e7SGreg Roach 700d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 701d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 7024b9ff166SGreg Roach } 7034b9ff166SGreg Roach 70439ca88baSGreg Roach $families = new Collection(); 705d9e083e7SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level) as $fact) { 706dc124885SGreg Roach $family = $fact->target(); 707d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 70839ca88baSGreg Roach $families->push($family); 709a25f0a04SGreg Roach } 710a25f0a04SGreg Roach } 711a25f0a04SGreg Roach 71239ca88baSGreg Roach return new Collection($families); 713a25f0a04SGreg Roach } 714a25f0a04SGreg Roach 715a25f0a04SGreg Roach /** 716a25f0a04SGreg Roach * Get the current spouse of this individual. 717a25f0a04SGreg Roach * 718a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 719a25f0a04SGreg Roach * in chronological order, and take the last one found. 720a25f0a04SGreg Roach * 721a25f0a04SGreg Roach * @return Individual|null 722a25f0a04SGreg Roach */ 723e364afe4SGreg Roach public function getCurrentSpouse(): ?Individual 724c1010edaSGreg Roach { 72539ca88baSGreg Roach $family = $this->spouseFamilies()->last(); 72639ca88baSGreg Roach 72739ca88baSGreg Roach if ($family instanceof Family) { 72839ca88baSGreg Roach return $family->spouse($this); 729a25f0a04SGreg Roach } 730b2ce94c6SRico Sonntag 731b2ce94c6SRico Sonntag return null; 732a25f0a04SGreg Roach } 733a25f0a04SGreg Roach 734a25f0a04SGreg Roach /** 735a25f0a04SGreg Roach * Count the children belonging to this individual. 736a25f0a04SGreg Roach * 737cbc1590aSGreg Roach * @return int 738a25f0a04SGreg Roach */ 739e364afe4SGreg Roach public function numberOfChildren(): int 740c1010edaSGreg Roach { 7417d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 7423dc7bbe9SGreg Roach return (int) $match[1]; 743b2ce94c6SRico Sonntag } 744b2ce94c6SRico Sonntag 74513abd6f3SGreg Roach $children = []; 74639ca88baSGreg Roach foreach ($this->spouseFamilies() as $fam) { 74739ca88baSGreg Roach foreach ($fam->children() as $child) { 748c0935879SGreg Roach $children[$child->xref()] = true; 749a25f0a04SGreg Roach } 750a25f0a04SGreg Roach } 751a25f0a04SGreg Roach 752a25f0a04SGreg Roach return count($children); 753a25f0a04SGreg Roach } 754a25f0a04SGreg Roach 755a25f0a04SGreg Roach /** 756a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 757a25f0a04SGreg Roach * 758cbc1590aSGreg Roach * @param int|null $access_level 759a25f0a04SGreg Roach * 76036779af1SGreg Roach * @return Collection<int,Family> 761a25f0a04SGreg Roach */ 76273d58381SGreg Roach public function childFamilies(int $access_level = null): Collection 763c1010edaSGreg Roach { 764d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 7654b9ff166SGreg Roach 766d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 767d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 768d9e083e7SGreg Roach } 769a25f0a04SGreg Roach 77039ca88baSGreg Roach $families = new Collection(); 77139ca88baSGreg Roach 772d9e083e7SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level) as $fact) { 773dc124885SGreg Roach $family = $fact->target(); 774d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 77539ca88baSGreg Roach $families->push($family); 776a25f0a04SGreg Roach } 777a25f0a04SGreg Roach } 778a25f0a04SGreg Roach 779a25f0a04SGreg Roach return $families; 780a25f0a04SGreg Roach } 781a25f0a04SGreg Roach 782a25f0a04SGreg Roach /** 783a25f0a04SGreg Roach * Get a list of step-parent families. 784a25f0a04SGreg Roach * 78536779af1SGreg Roach * @return Collection<int,Family> 786a25f0a04SGreg Roach */ 787820b62dfSGreg Roach public function childStepFamilies(): Collection 788c1010edaSGreg Roach { 789ed5b6227SGreg Roach $step_families = new Collection(); 79039ca88baSGreg Roach $families = $this->childFamilies(); 791a25f0a04SGreg Roach foreach ($families as $family) { 792ed5b6227SGreg Roach foreach ($family->spouses() as $parent) { 793ed5b6227SGreg Roach foreach ($parent->spouseFamilies() as $step_family) { 79439ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 795ed5b6227SGreg Roach $step_families->add($step_family); 796a25f0a04SGreg Roach } 797a25f0a04SGreg Roach } 798a25f0a04SGreg Roach } 799a25f0a04SGreg Roach } 800a25f0a04SGreg Roach 8018c627a69SGreg Roach return $step_families->uniqueStrict(static function (Family $family): string { 8028c627a69SGreg Roach return $family->xref(); 8038c627a69SGreg Roach }); 804a25f0a04SGreg Roach } 805a25f0a04SGreg Roach 806a25f0a04SGreg Roach /** 807a25f0a04SGreg Roach * Get a list of step-parent families. 808a25f0a04SGreg Roach * 80936779af1SGreg Roach * @return Collection<int,Family> 810a25f0a04SGreg Roach */ 811820b62dfSGreg Roach public function spouseStepFamilies(): Collection 812c1010edaSGreg Roach { 81313abd6f3SGreg Roach $step_families = []; 81439ca88baSGreg Roach $families = $this->spouseFamilies(); 815820b62dfSGreg Roach 816a25f0a04SGreg Roach foreach ($families as $family) { 81739ca88baSGreg Roach $spouse = $family->spouse($this); 818820b62dfSGreg Roach 819d823340dSGreg Roach if ($spouse instanceof self) { 82039ca88baSGreg Roach foreach ($family->spouse($this)->spouseFamilies() as $step_family) { 82139ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 822a25f0a04SGreg Roach $step_families[] = $step_family; 823a25f0a04SGreg Roach } 824a25f0a04SGreg Roach } 825a25f0a04SGreg Roach } 826a25f0a04SGreg Roach } 827a25f0a04SGreg Roach 828820b62dfSGreg Roach return new Collection($step_families); 829a25f0a04SGreg Roach } 830a25f0a04SGreg Roach 831a25f0a04SGreg Roach /** 832a25f0a04SGreg Roach * A label for a parental family group 833a25f0a04SGreg Roach * 834a25f0a04SGreg Roach * @param Family $family 835a25f0a04SGreg Roach * 836a25f0a04SGreg Roach * @return string 837a25f0a04SGreg Roach */ 838820b62dfSGreg Roach public function getChildFamilyLabel(Family $family): string 839c1010edaSGreg Roach { 8400e7e67a6SGreg Roach $fact = $this->facts(['FAMC'])->first(static fn (Fact $fact): bool => $fact->target() === $family); 8410e7e67a6SGreg Roach 8420e7e67a6SGreg Roach if ($fact instanceof Fact) { 8430e7e67a6SGreg Roach $pedigree = $fact->attribute('PEDI'); 8440e7e67a6SGreg Roach } else { 8450e7e67a6SGreg Roach $pedigree = ''; 8460e7e67a6SGreg Roach } 847b2ce94c6SRico Sonntag 8487d70e4a7SGreg Roach $values = [ 849*88a03560SGreg Roach PedigreeLinkageType::VALUE_BIRTH => I18N::translate('Family with parents'), 850*88a03560SGreg Roach PedigreeLinkageType::VALUE_ADOPTED => I18N::translate('Family with adoptive parents'), 851*88a03560SGreg Roach PedigreeLinkageType::VALUE_FOSTER => I18N::translate('Family with foster parents'), 852665e281aSGreg Roach /* I18N: “sealing” is a Mormon ceremony. */ 853*88a03560SGreg Roach PedigreeLinkageType::VALUE_SEALING => I18N::translate('Family with sealing parents'), 854665e281aSGreg Roach /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */ 855*88a03560SGreg Roach PedigreeLinkageType::VALUE_RADA => I18N::translate('Family with rada parents'), 8567d70e4a7SGreg Roach ]; 8577d70e4a7SGreg Roach 858*88a03560SGreg Roach return $values[$pedigree] ?? $values[PedigreeLinkageType::VALUE_BIRTH]; 859a25f0a04SGreg Roach } 860a25f0a04SGreg Roach 861a25f0a04SGreg Roach /** 862a25f0a04SGreg Roach * Create a label for a step family 863a25f0a04SGreg Roach * 864a25f0a04SGreg Roach * @param Family $step_family 865a25f0a04SGreg Roach * 866a25f0a04SGreg Roach * @return string 867a25f0a04SGreg Roach */ 8688f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 869c1010edaSGreg Roach { 87039ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 871a25f0a04SGreg Roach if ($family !== $step_family) { 872a25f0a04SGreg Roach // Must be a step-family 87339ca88baSGreg Roach foreach ($family->spouses() as $parent) { 87439ca88baSGreg Roach foreach ($step_family->spouses() as $step_parent) { 875a25f0a04SGreg Roach if ($parent === $step_parent) { 876a25f0a04SGreg Roach // One common parent - must be a step family 877e364afe4SGreg Roach if ($parent->sex() === 'M') { 878a25f0a04SGreg Roach // Father’s family with someone else 87939ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 880a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 88139ca88baSGreg Roach return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName()); 882b2ce94c6SRico Sonntag } 883b2ce94c6SRico Sonntag 884a25f0a04SGreg Roach /* I18N: A step-family. */ 885bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 886a25f0a04SGreg Roach } 887b2ce94c6SRico Sonntag 888a25f0a04SGreg Roach // Mother’s family with someone else 88939ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 890a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 89139ca88baSGreg Roach return I18N::translate('Mother’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('Mother’s family with an unknown individual'); 896a25f0a04SGreg Roach } 897a25f0a04SGreg Roach } 898a25f0a04SGreg Roach } 899a25f0a04SGreg Roach } 900a25f0a04SGreg Roach } 901a25f0a04SGreg Roach 902a25f0a04SGreg Roach // Perahps same parents - but a different family record? 903a25f0a04SGreg Roach return I18N::translate('Family with parents'); 904a25f0a04SGreg Roach } 905a25f0a04SGreg Roach 906225e381fSGreg Roach /** 907225e381fSGreg Roach * Get the description for the family. 908225e381fSGreg Roach * 909225e381fSGreg Roach * For example, "XXX's family with new wife". 910225e381fSGreg Roach * 911225e381fSGreg Roach * @param Family $family 912225e381fSGreg Roach * 913225e381fSGreg Roach * @return string 914225e381fSGreg Roach */ 915e364afe4SGreg Roach public function getSpouseFamilyLabel(Family $family): string 916c1010edaSGreg Roach { 91739ca88baSGreg Roach $spouse = $family->spouse($this); 918225e381fSGreg Roach if ($spouse) { 919225e381fSGreg Roach /* I18N: %s is the spouse name */ 92039ca88baSGreg Roach return I18N::translate('Family with %s', $spouse->fullName()); 921225e381fSGreg Roach } 922b2ce94c6SRico Sonntag 92339ca88baSGreg Roach return $family->fullName(); 924225e381fSGreg Roach } 925225e381fSGreg Roach 926a25f0a04SGreg Roach /** 927961ec755SGreg Roach * If this object has no name, what do we call it? 928961ec755SGreg Roach * 929961ec755SGreg Roach * @return string 930961ec755SGreg Roach */ 9318f53f488SRico Sonntag public function getFallBackName(): string 932c1010edaSGreg Roach { 933a25f0a04SGreg Roach return '@P.N. /@N.N./'; 934a25f0a04SGreg Roach } 935a25f0a04SGreg Roach 936a25f0a04SGreg Roach /** 937a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 938a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 939a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 940a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 941a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 942a25f0a04SGreg Roach * recorded in the NAME field. e.g. 943a25f0a04SGreg Roach * 944a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 945a25f0a04SGreg Roach * 2 GIVN Robert 946a25f0a04SGreg Roach * 2 SPFX de 947a25f0a04SGreg Roach * 2 SURN CLITHEROW 948a25f0a04SGreg Roach * 2 NICK The Bald 949a25f0a04SGreg Roach * 950a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 951a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 952a25f0a04SGreg Roach * 953a25f0a04SGreg Roach * Handle multiple surnames, either as; 954a25f0a04SGreg Roach * 955a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 956a25f0a04SGreg Roach * or 957a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 958a25f0a04SGreg Roach * 2 GIVN Carlos 959a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 960a25f0a04SGreg Roach * 961a25f0a04SGreg Roach * @param string $type 96251928f9aSGreg Roach * @param string $value 963a25f0a04SGreg Roach * @param string $gedcom 964e364afe4SGreg Roach * 965e364afe4SGreg Roach * @return void 966a25f0a04SGreg Roach */ 96751928f9aSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 968c1010edaSGreg Roach { 969a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 970a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 971a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 972a25f0a04SGreg Roach 97376f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 974b6ac34efSGreg Roach $GIVN = preg_match('/\n' . $sublevel . ' GIVN (.+)/', $gedcom, $match) ? $match[1] : ''; 975b6ac34efSGreg Roach $SURN = preg_match('/\n' . $sublevel . ' SURN (.+)/', $gedcom, $match) ? $match[1] : ''; 976a25f0a04SGreg Roach 977a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 97876f666f4SGreg Roach if ($SURN !== '') { 979a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 980a25f0a04SGreg Roach } else { 98113abd6f3SGreg Roach $SURNS = []; 982a25f0a04SGreg Roach } 98376f666f4SGreg Roach 984a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 985a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 986a25f0a04SGreg Roach 987a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 988a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 989a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 990a25f0a04SGreg Roach 991a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 99251928f9aSGreg Roach if (substr_count($value, '/') % 2 === 1) { 99351928f9aSGreg Roach $value .= '/'; 994a25f0a04SGreg Roach } 995a25f0a04SGreg Roach 996a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 99751928f9aSGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $value); 998a25f0a04SGreg Roach 999a25f0a04SGreg Roach // Extract the surname. 1000a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 1001a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 1002a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 1003a25f0a04SGreg Roach } else { 1004a25f0a04SGreg Roach $surname = ''; 1005a25f0a04SGreg Roach } 1006a25f0a04SGreg Roach 1007a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 1008a25f0a04SGreg Roach if (!$SURNS) { 1009a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 1010a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 1011a25f0a04SGreg Roach $SURNS = $matches[1]; 1012a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 1013a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 1014a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 1015a25f0a04SGreg Roach } 1016a25f0a04SGreg Roach } else { 1017a25f0a04SGreg Roach // It is valid not to have a surname at all 101813abd6f3SGreg Roach $SURNS = ['']; 1019a25f0a04SGreg Roach } 1020a25f0a04SGreg Roach } 1021a25f0a04SGreg Roach 1022a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 1023a25f0a04SGreg Roach if (!$GIVN) { 1024c1010edaSGreg Roach // remove surname 1025c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?\/.*\/ ?/', ' ', $full); 1026c1010edaSGreg Roach // remove nickname 1027c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?".+"/', ' ', $GIVN); 1028c1010edaSGreg Roach // multiple spaces, caused by the above 1029c72b7fa4SGreg Roach $GIVN = preg_replace('/ {2,}/', ' ', $GIVN); 1030c1010edaSGreg Roach // leading/trailing spaces, caused by the above 1031c72b7fa4SGreg Roach $GIVN = preg_replace('/^ | $/', '', $GIVN); 1032a25f0a04SGreg Roach } 1033a25f0a04SGreg Roach 1034a25f0a04SGreg Roach // Add placeholder for unknown given name 1035a25f0a04SGreg Roach if (!$GIVN) { 1036d823340dSGreg Roach $GIVN = self::PRAENOMEN_NESCIO; 103773f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1038a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1039a25f0a04SGreg Roach } 1040a25f0a04SGreg Roach 1041a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1042a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1043a25f0a04SGreg Roach // $full is for display on-screen 1044a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1045a25f0a04SGreg Roach 1046a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1047d823340dSGreg Roach $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full); 1048d823340dSGreg Roach $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full); 1049c6f196c3SGreg Roach // Format for display 1050d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1051acc34ea1SGreg Roach // Localise quotation marks around the nickname 10520b5fd0a6SGreg Roach $full = preg_replace_callback('/"([^&]*)"/', static function (array $matches): string { 1053c652cdbdSGreg Roach return '<q class="wt-nickname">' . $matches[1] . '</q>'; 10548d68cabeSGreg Roach }, $full); 1055a25f0a04SGreg Roach 1056c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1057ee51991cSGreg Roach $full = preg_replace('/([^ >\x{200C}]*)\*/u', '<span class="starredname">\\1</span>', $full); 1058a25f0a04SGreg Roach 1059a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1060a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1061a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1062a25f0a04SGreg Roach 1063ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1064a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1065e364afe4SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) { 1066a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1067e364afe4SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) { 1068a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1069a25f0a04SGreg Roach } 1070a25f0a04SGreg Roach 1071bdb3725aSGreg Roach $this->getAllNames[] = [ 1072a25f0a04SGreg Roach 'type' => $type, 1073a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1074c1010edaSGreg Roach 'full' => $full, 1075c1010edaSGreg Roach // This is used for display 1076c1010edaSGreg Roach 'fullNN' => $fullNN, 1077c1010edaSGreg Roach // This goes into the database 1078c1010edaSGreg Roach 'surname' => $surname, 1079c1010edaSGreg Roach // This goes into the database 1080c1010edaSGreg Roach 'givn' => $GIVN, 1081c1010edaSGreg Roach // This goes into the database 1082c1010edaSGreg Roach 'surn' => $SURN, 1083c1010edaSGreg Roach // This goes into the database 108413abd6f3SGreg Roach ]; 1085a25f0a04SGreg Roach } 1086a25f0a04SGreg Roach } 1087a25f0a04SGreg Roach 1088a25f0a04SGreg Roach /** 108976692c8bSGreg Roach * Extract names from the GEDCOM record. 1090c7ff4153SGreg Roach * 1091c7ff4153SGreg Roach * @return void 1092a25f0a04SGreg Roach */ 1093e364afe4SGreg Roach public function extractNames(): void 1094c1010edaSGreg Roach { 1095d9e083e7SGreg Roach $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree); 1096d9e083e7SGreg Roach 10978f53f488SRico Sonntag $this->extractNamesFromFacts( 10988f53f488SRico Sonntag 1, 10998f53f488SRico Sonntag 'NAME', 1100d9e083e7SGreg Roach $this->facts(['NAME'], false, $access_level) 11018f53f488SRico Sonntag ); 1102a25f0a04SGreg Roach } 1103a25f0a04SGreg Roach 1104a25f0a04SGreg Roach /** 1105a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1106a25f0a04SGreg Roach * selection items or favorites. 1107a25f0a04SGreg Roach * 1108a25f0a04SGreg Roach * @return string 1109a25f0a04SGreg Roach */ 11108f53f488SRico Sonntag public function formatListDetails(): string 1111c1010edaSGreg Roach { 1112a25f0a04SGreg Roach return 11138d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 11148d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1115a25f0a04SGreg Roach } 11168091bfd1SGreg Roach 11178091bfd1SGreg Roach /** 11188091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11198091bfd1SGreg Roach */ 11208091bfd1SGreg Roach public function lock(): void 11218091bfd1SGreg Roach { 11228091bfd1SGreg Roach DB::table('individuals') 11238091bfd1SGreg Roach ->where('i_file', '=', $this->tree->id()) 11248091bfd1SGreg Roach ->where('i_id', '=', $this->xref()) 11258091bfd1SGreg Roach ->lockForUpdate() 11268091bfd1SGreg Roach ->get(); 11278091bfd1SGreg Roach } 1128a25f0a04SGreg Roach} 1129