1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 51fe542e9SGreg Roach * Copyright (C) 2021 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 22886b77daSGreg Roachuse Closure; 23a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar; 241fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 25852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 262e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2739ca88baSGreg Roachuse Illuminate\Support\Collection; 28a25f0a04SGreg Roach 297d70e4a7SGreg Roachuse function preg_match; 307d70e4a7SGreg Roach 31a25f0a04SGreg Roach/** 3276692c8bSGreg Roach * A GEDCOM individual (INDI) object. 33a25f0a04SGreg Roach */ 34c1010edaSGreg Roachclass Individual extends GedcomRecord 35c1010edaSGreg Roach{ 3616d6367aSGreg Roach public const RECORD_TYPE = 'INDI'; 3716d6367aSGreg Roach 388fb4e87cSGreg Roach // Placeholders to indicate unknown names 398fb4e87cSGreg Roach public const NOMEN_NESCIO = '@N.N.'; 408fb4e87cSGreg Roach public const PRAENOMEN_NESCIO = '@P.N.'; 418fb4e87cSGreg Roach 42852ede8cSGreg Roach protected const ROUTE_NAME = IndividualPage::class; 43a25f0a04SGreg Roach 4476692c8bSGreg Roach /** @var int used in some lists to keep track of this individual’s generation in that list */ 4576692c8bSGreg Roach public $generation; 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** @var Date The estimated date of birth */ 484686330aSGreg Roach private $estimated_birth_date; 49a25f0a04SGreg Roach 50a25f0a04SGreg Roach /** @var Date The estimated date of death */ 514686330aSGreg Roach private $estimated_death_date; 52a25f0a04SGreg Roach 53a25f0a04SGreg Roach /** 54c156e8f5SGreg Roach * A closure which will compare individuals by birth date. 55c156e8f5SGreg Roach * 56c156e8f5SGreg Roach * @return Closure 57c156e8f5SGreg Roach */ 58c156e8f5SGreg Roach public static function birthDateComparator(): Closure 59c156e8f5SGreg Roach { 606c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 61c156e8f5SGreg Roach return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 62c156e8f5SGreg Roach }; 63c156e8f5SGreg Roach } 64c156e8f5SGreg Roach 65c156e8f5SGreg Roach /** 66c156e8f5SGreg Roach * A closure which will compare individuals by death date. 67c156e8f5SGreg Roach * 68c156e8f5SGreg Roach * @return Closure 69c156e8f5SGreg Roach */ 70c156e8f5SGreg Roach public static function deathDateComparator(): Closure 71c156e8f5SGreg Roach { 726c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 7310e21aa9SGreg Roach return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate()); 74c156e8f5SGreg Roach }; 75c156e8f5SGreg Roach } 76c156e8f5SGreg Roach 77c156e8f5SGreg Roach /** 78a25f0a04SGreg Roach * Can the name of this record be shown? 79a25f0a04SGreg Roach * 8076692c8bSGreg Roach * @param int|null $access_level 8176692c8bSGreg Roach * 8276692c8bSGreg Roach * @return bool 83a25f0a04SGreg Roach */ 8435584196SGreg Roach public function canShowName(int $access_level = null): bool 85c1010edaSGreg Roach { 86d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 874b9ff166SGreg Roach 88f56b86d2SGreg Roach return (int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 89a25f0a04SGreg Roach } 90a25f0a04SGreg Roach 91a25f0a04SGreg Roach /** 9276692c8bSGreg Roach * Can this individual be shown? 93a25f0a04SGreg Roach * 9476692c8bSGreg Roach * @param int $access_level 9576692c8bSGreg Roach * 9676692c8bSGreg Roach * @return bool 97a25f0a04SGreg Roach */ 9835584196SGreg Roach protected function canShowByType(int $access_level): bool 99c1010edaSGreg Roach { 100a25f0a04SGreg Roach // Dead people... 101f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 102a25f0a04SGreg Roach $keep_alive = false; 10354ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 104a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH) { 105*dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 106a25f0a04SGreg Roach foreach ($matches as $match) { 107a25f0a04SGreg Roach $date = new Date($match[1]); 108a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 109a25f0a04SGreg Roach $keep_alive = true; 110a25f0a04SGreg Roach break; 111a25f0a04SGreg Roach } 112a25f0a04SGreg Roach } 113a25f0a04SGreg Roach } 11454ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 115a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_DEATH) { 116*dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 117a25f0a04SGreg Roach foreach ($matches as $match) { 118a25f0a04SGreg Roach $date = new Date($match[1]); 119a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 120a25f0a04SGreg Roach $keep_alive = true; 121a25f0a04SGreg Roach break; 122a25f0a04SGreg Roach } 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach } 125a25f0a04SGreg Roach if (!$keep_alive) { 126a25f0a04SGreg Roach return true; 127a25f0a04SGreg Roach } 128a25f0a04SGreg Roach } 129a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 1301fe542e9SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH); 1311fe542e9SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 1327c4add84SGreg Roach 13354ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1344b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 135a25f0a04SGreg Roach } 136a25f0a04SGreg Roach 137a25f0a04SGreg Roach // No restriction found - show living people to members only: 1384b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 139a25f0a04SGreg Roach } 140a25f0a04SGreg Roach 141a25f0a04SGreg Roach /** 142a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 143a25f0a04SGreg Roach * 144a25f0a04SGreg Roach * @param Individual $target 145cbc1590aSGreg Roach * @param int $distance 146a25f0a04SGreg Roach * 147cbc1590aSGreg Roach * @return bool 148a25f0a04SGreg Roach */ 14924f2a3afSGreg Roach private static function isRelated(Individual $target, int $distance): bool 150c1010edaSGreg Roach { 151a25f0a04SGreg Roach static $cache = null; 152a25f0a04SGreg Roach 1531fe542e9SGreg Roach $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree); 154a25f0a04SGreg Roach if ($user_individual) { 155a25f0a04SGreg Roach if (!$cache) { 15613abd6f3SGreg Roach $cache = [ 15713abd6f3SGreg Roach 0 => [$user_individual], 15813abd6f3SGreg Roach 1 => [], 15913abd6f3SGreg Roach ]; 1608d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 161dc124885SGreg Roach $family = $fact->target(); 162e24444eeSGreg Roach if ($family instanceof Family) { 163a25f0a04SGreg Roach $cache[1][] = $family; 164a25f0a04SGreg Roach } 165a25f0a04SGreg Roach } 166a25f0a04SGreg Roach } 167a25f0a04SGreg Roach } else { 168a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 169a25f0a04SGreg Roach return true; 170a25f0a04SGreg Roach } 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 173a25f0a04SGreg Roach $distance *= 2; 174a25f0a04SGreg Roach 175a25f0a04SGreg Roach // Consider each path length in turn 176a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 177a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 178a25f0a04SGreg Roach // We have already calculated all records with this length 179e364afe4SGreg Roach if ($n % 2 === 0 && in_array($target, $cache[$n], true)) { 180a25f0a04SGreg Roach return true; 181a25f0a04SGreg Roach } 182a25f0a04SGreg Roach } else { 183a25f0a04SGreg Roach // Need to calculate these paths 18413abd6f3SGreg Roach $cache[$n] = []; 185e364afe4SGreg Roach if ($n % 2 === 0) { 186a25f0a04SGreg Roach // Add FAM->INDI links 187a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 1888d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 189dc124885SGreg Roach $individual = $fact->target(); 190a25f0a04SGreg Roach // Don’t backtrack 191e364afe4SGreg Roach if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) { 192a25f0a04SGreg Roach $cache[$n][] = $individual; 193a25f0a04SGreg Roach } 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 197a25f0a04SGreg Roach return true; 198a25f0a04SGreg Roach } 199a25f0a04SGreg Roach } else { 200a25f0a04SGreg Roach // Add INDI->FAM links 201a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 2028d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 203dc124885SGreg Roach $family = $fact->target(); 204a25f0a04SGreg Roach // Don’t backtrack 205e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 206a25f0a04SGreg Roach $cache[$n][] = $family; 207a25f0a04SGreg Roach } 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach } 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 214a25f0a04SGreg Roach return false; 215a25f0a04SGreg Roach } 216a25f0a04SGreg Roach 21776692c8bSGreg Roach /** 21876692c8bSGreg Roach * Generate a private version of this record 21976692c8bSGreg Roach * 22076692c8bSGreg Roach * @param int $access_level 22176692c8bSGreg Roach * 22276692c8bSGreg Roach * @return string 22376692c8bSGreg Roach */ 2243c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 225c1010edaSGreg Roach { 226acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 227a25f0a04SGreg Roach 228a25f0a04SGreg Roach $rec = '0 @' . $this->xref . '@ INDI'; 229f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) { 230a25f0a04SGreg Roach // Show all the NAME tags, including subtags 2318d0ebef0SGreg Roach foreach ($this->facts(['NAME']) as $fact) { 232138ca96cSGreg Roach $rec .= "\n" . $fact->gedcom(); 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data 2368d0ebef0SGreg Roach preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 237a25f0a04SGreg Roach foreach ($matches as $match) { 2386b9cb339SGreg Roach $rela = Registry::familyFactory()->make($match[1], $this->tree); 239a25f0a04SGreg Roach if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 240a25f0a04SGreg Roach $rec .= $match[0]; 241a25f0a04SGreg Roach } 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach // Don’t privatize sex. 244a25f0a04SGreg Roach if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) { 245a25f0a04SGreg Roach $rec .= $match[0]; 246a25f0a04SGreg Roach } 247a25f0a04SGreg Roach 248a25f0a04SGreg Roach return $rec; 249a25f0a04SGreg Roach } 250a25f0a04SGreg Roach 25176692c8bSGreg Roach /** 252a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 253a25f0a04SGreg Roach * If not known to be dead, then assume living. 254a25f0a04SGreg Roach * 255cbc1590aSGreg Roach * @return bool 256a25f0a04SGreg Roach */ 2578f53f488SRico Sonntag public function isDead(): bool 258c1010edaSGreg Roach { 259c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 2604459dc9aSGreg Roach $today_jd = Carbon::now()->julianDay(); 261a25f0a04SGreg Roach 262a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 2638d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 264a25f0a04SGreg Roach return true; 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach 267a25f0a04SGreg Roach // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 268a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 269a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 270a25f0a04SGreg Roach $date = new Date($date_match); 271269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) { 272a25f0a04SGreg Roach return true; 273a25f0a04SGreg Roach } 274a25f0a04SGreg Roach } 275a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 276a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 277a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 278a25f0a04SGreg Roach return false; 279a25f0a04SGreg Roach } 280a25f0a04SGreg Roach } 281a25f0a04SGreg Roach 282a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 283a25f0a04SGreg Roach 284a25f0a04SGreg Roach // Check parents (birth and adopted) 28539ca88baSGreg Roach foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) { 28639ca88baSGreg Roach foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) { 287a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 288a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 289a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 290a25f0a04SGreg Roach $date = new Date($date_match); 291269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) { 292a25f0a04SGreg Roach return true; 293a25f0a04SGreg Roach } 294a25f0a04SGreg Roach } 295a25f0a04SGreg Roach } 296a25f0a04SGreg Roach } 297a25f0a04SGreg Roach 298a25f0a04SGreg Roach // Check spouses 29939ca88baSGreg Roach foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) { 300a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 301a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 302a25f0a04SGreg Roach $date = new Date($date_match); 303a25f0a04SGreg Roach // Assume marriage occurs after age of 10 304269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) { 305a25f0a04SGreg Roach return true; 306a25f0a04SGreg Roach } 307a25f0a04SGreg Roach } 308a25f0a04SGreg Roach // Check spouse dates 30939ca88baSGreg Roach $spouse = $family->spouse($this, Auth::PRIV_HIDE); 310a25f0a04SGreg Roach if ($spouse) { 311a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 312a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 313a25f0a04SGreg Roach $date = new Date($date_match); 314a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 315269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) { 316a25f0a04SGreg Roach return true; 317a25f0a04SGreg Roach } 318a25f0a04SGreg Roach } 319a25f0a04SGreg Roach } 320a25f0a04SGreg Roach // Check child dates 32139ca88baSGreg Roach foreach ($family->children(Auth::PRIV_HIDE) as $child) { 322a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 323a25f0a04SGreg Roach // Assume children born after age of 15 324a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 325a25f0a04SGreg Roach $date = new Date($date_match); 326269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) { 327a25f0a04SGreg Roach return true; 328a25f0a04SGreg Roach } 329a25f0a04SGreg Roach } 330a25f0a04SGreg Roach // Check grandchildren 33139ca88baSGreg Roach foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) { 33239ca88baSGreg Roach foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) { 333a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 334a25f0a04SGreg Roach // Assume grandchildren born after age of 30 335a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 336a25f0a04SGreg Roach $date = new Date($date_match); 337269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) { 338a25f0a04SGreg Roach return true; 339a25f0a04SGreg Roach } 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach } 342a25f0a04SGreg Roach } 343a25f0a04SGreg Roach } 344a25f0a04SGreg Roach } 345a25f0a04SGreg Roach 346a25f0a04SGreg Roach return false; 347a25f0a04SGreg Roach } 348a25f0a04SGreg Roach 349a25f0a04SGreg Roach /** 350a25f0a04SGreg Roach * Find the highlighted media object for an individual 351a25f0a04SGreg Roach * 352e364afe4SGreg Roach * @return MediaFile|null 353a25f0a04SGreg Roach */ 354e364afe4SGreg Roach public function findHighlightedMediaFile(): ?MediaFile 355c1010edaSGreg Roach { 35692647683SGreg Roach $fact = $this->facts(['OBJE']) 35719d319e7SGreg Roach ->first(static function (Fact $fact): bool { 358dc124885SGreg Roach $media = $fact->target(); 35919d319e7SGreg Roach 36019d319e7SGreg Roach return $media instanceof Media && $media->firstImageFile() instanceof MediaFile; 36119d319e7SGreg Roach }); 36219d319e7SGreg Roach 36392647683SGreg Roach if ($fact instanceof Fact && $fact->target() instanceof Media) { 36492647683SGreg Roach return $fact->target()->firstImageFile(); 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach 367a25f0a04SGreg Roach return null; 368a25f0a04SGreg Roach } 369a25f0a04SGreg Roach 370a25f0a04SGreg Roach /** 371a25f0a04SGreg Roach * Display the prefered image for this individual. 372a25f0a04SGreg Roach * Use an icon if no image is available. 373a25f0a04SGreg Roach * 374dce07401SGreg Roach * @param int $width Pixels 375dce07401SGreg Roach * @param int $height Pixels 376dce07401SGreg Roach * @param string $fit "crop" or "contain" 37709482a55SGreg Roach * @param array<string> $attributes Additional HTML attributes 378dce07401SGreg Roach * 379a25f0a04SGreg Roach * @return string 380a25f0a04SGreg Roach */ 38124f2a3afSGreg Roach public function displayImage(int $width, int $height, string $fit, array $attributes): string 382c1010edaSGreg Roach { 3834a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 3844a9f750fSGreg Roach 3854a9f750fSGreg Roach if ($media_file !== null) { 3864a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 387a25f0a04SGreg Roach } 3884a9f750fSGreg Roach 3894a9f750fSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE')) { 39039ca88baSGreg Roach return '<i class="icon-silhouette-' . $this->sex() . '"></i>'; 3914a9f750fSGreg Roach } 3924a9f750fSGreg Roach 3934a9f750fSGreg Roach return ''; 394a25f0a04SGreg Roach } 395a25f0a04SGreg Roach 396a25f0a04SGreg Roach /** 397a25f0a04SGreg Roach * Get the date of birth 398a25f0a04SGreg Roach * 399a25f0a04SGreg Roach * @return Date 400a25f0a04SGreg Roach */ 4018f53f488SRico Sonntag public function getBirthDate(): Date 402c1010edaSGreg Roach { 403a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 404a25f0a04SGreg Roach if ($date->isOK()) { 405a25f0a04SGreg Roach return $date; 406a25f0a04SGreg Roach } 407a25f0a04SGreg Roach } 408a25f0a04SGreg Roach 409a25f0a04SGreg Roach return new Date(''); 410a25f0a04SGreg Roach } 411a25f0a04SGreg Roach 412a25f0a04SGreg Roach /** 413a25f0a04SGreg Roach * Get the place of birth 414a25f0a04SGreg Roach * 41516d0b7f7SRico Sonntag * @return Place 416a25f0a04SGreg Roach */ 4178f53f488SRico Sonntag public function getBirthPlace(): Place 418c1010edaSGreg Roach { 419a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 420a25f0a04SGreg Roach return $place; 421a25f0a04SGreg Roach } 422a25f0a04SGreg Roach 423b20ddbf9SGreg Roach return new Place('', $this->tree); 424a25f0a04SGreg Roach } 425a25f0a04SGreg Roach 426a25f0a04SGreg Roach /** 427a25f0a04SGreg Roach * Get the date of death 428a25f0a04SGreg Roach * 429a25f0a04SGreg Roach * @return Date 430a25f0a04SGreg Roach */ 4318f53f488SRico Sonntag public function getDeathDate(): Date 432c1010edaSGreg Roach { 433a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 434a25f0a04SGreg Roach if ($date->isOK()) { 435a25f0a04SGreg Roach return $date; 436a25f0a04SGreg Roach } 437a25f0a04SGreg Roach } 438a25f0a04SGreg Roach 439a25f0a04SGreg Roach return new Date(''); 440a25f0a04SGreg Roach } 441a25f0a04SGreg Roach 442a25f0a04SGreg Roach /** 443a25f0a04SGreg Roach * Get the place of death 444a25f0a04SGreg Roach * 44516d0b7f7SRico Sonntag * @return Place 446a25f0a04SGreg Roach */ 4478f53f488SRico Sonntag public function getDeathPlace(): Place 448c1010edaSGreg Roach { 449a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 450a25f0a04SGreg Roach return $place; 451a25f0a04SGreg Roach } 452a25f0a04SGreg Roach 453b20ddbf9SGreg Roach return new Place('', $this->tree); 454a25f0a04SGreg Roach } 455a25f0a04SGreg Roach 456a25f0a04SGreg Roach /** 457a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 45815d603e7SGreg Roach * Provide the place and full date using a tooltip. 459a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 460a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 461a25f0a04SGreg Roach * 462a25f0a04SGreg Roach * @return string 463a25f0a04SGreg Roach */ 4645e6816beSGreg Roach public function lifespan(): string 465c1010edaSGreg Roach { 46653f5ca04SGreg Roach // Just the first part of the place name. 467392561bbSGreg Roach $birth_place = strip_tags($this->getBirthPlace()->shortName()); 468392561bbSGreg Roach $death_place = strip_tags($this->getDeathPlace()->shortName()); 46953f5ca04SGreg Roach 47081b9dc5dSGreg Roach // Remove markup from dates. Use UTF_FSI / UTF_PDI instead of <bdi></bdi>, as 47181b9dc5dSGreg Roach // we cannot use HTML markup in title attributes. 47281b9dc5dSGreg Roach $birth_date = "\u{2068}" . strip_tags($this->getBirthDate()->display()) . "\u{2069}"; 47381b9dc5dSGreg Roach $death_date = "\u{2068}" . strip_tags($this->getDeathDate()->display()) . "\u{2069}"; 47415d603e7SGreg Roach 47553f5ca04SGreg Roach // Use minimum and maximum dates - to agree with the age calculations. 47653f5ca04SGreg Roach $birth_year = $this->getBirthDate()->minimumDate()->format('%Y'); 47753f5ca04SGreg Roach $death_year = $this->getDeathDate()->maximumDate()->format('%Y'); 47853f5ca04SGreg Roach 479c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 480dacfe33cSGreg Roach return I18N::translate( 481a25f0a04SGreg Roach '%1$s–%2$s', 48253f5ca04SGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $birth_year . '</span>', 48353f5ca04SGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $death_year . '</span>' 484a25f0a04SGreg Roach ); 485a25f0a04SGreg Roach } 486a25f0a04SGreg Roach 487a25f0a04SGreg Roach /** 488a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 489a25f0a04SGreg Roach * 49009482a55SGreg Roach * @return array<Date> 491a25f0a04SGreg Roach */ 4928f53f488SRico Sonntag public function getAllBirthDates(): array 493c1010edaSGreg Roach { 4948d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 495d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 496d240bb87SGreg Roach 497d240bb87SGreg Roach if ($dates !== []) { 498d240bb87SGreg Roach return $dates; 499a25f0a04SGreg Roach } 500a25f0a04SGreg Roach } 501a25f0a04SGreg Roach 50213abd6f3SGreg Roach return []; 503a25f0a04SGreg Roach } 504a25f0a04SGreg Roach 505a25f0a04SGreg Roach /** 506a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 507a25f0a04SGreg Roach * 50809482a55SGreg Roach * @return array<Place> 509a25f0a04SGreg Roach */ 5108f53f488SRico Sonntag public function getAllBirthPlaces(): array 511c1010edaSGreg Roach { 5128d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 5138d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 514d240bb87SGreg Roach 51554c1ab5eSGreg Roach if ($places !== []) { 5164080d558SGreg Roach return $places; 517a25f0a04SGreg Roach } 518a25f0a04SGreg Roach } 519a25f0a04SGreg Roach 52013abd6f3SGreg Roach return []; 521a25f0a04SGreg Roach } 522a25f0a04SGreg Roach 523a25f0a04SGreg Roach /** 524a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 525a25f0a04SGreg Roach * 52609482a55SGreg Roach * @return array<Date> 527a25f0a04SGreg Roach */ 5288f53f488SRico Sonntag public function getAllDeathDates(): array 529c1010edaSGreg Roach { 5308d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 531d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 532d240bb87SGreg Roach 533d240bb87SGreg Roach if ($dates !== []) { 534d240bb87SGreg Roach return $dates; 535a25f0a04SGreg Roach } 536a25f0a04SGreg Roach } 537a25f0a04SGreg Roach 53813abd6f3SGreg Roach return []; 539a25f0a04SGreg Roach } 540a25f0a04SGreg Roach 541a25f0a04SGreg Roach /** 542a25f0a04SGreg Roach * Get all the death places - for the individual lists. 543a25f0a04SGreg Roach * 54409482a55SGreg Roach * @return array<Place> 545a25f0a04SGreg Roach */ 5468f53f488SRico Sonntag public function getAllDeathPlaces(): array 547c1010edaSGreg Roach { 5488d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 5498d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 550d240bb87SGreg Roach 55154c1ab5eSGreg Roach if ($places !== []) { 5524080d558SGreg Roach return $places; 553a25f0a04SGreg Roach } 554a25f0a04SGreg Roach } 555a25f0a04SGreg Roach 55613abd6f3SGreg Roach return []; 557a25f0a04SGreg Roach } 558a25f0a04SGreg Roach 559a25f0a04SGreg Roach /** 560a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 561a25f0a04SGreg Roach * 562a25f0a04SGreg Roach * @return Date 563a25f0a04SGreg Roach */ 5648f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 565c1010edaSGreg Roach { 5668f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 567a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 568a25f0a04SGreg Roach if ($date->isOK()) { 5694686330aSGreg Roach $this->estimated_birth_date = $date; 570a25f0a04SGreg Roach break; 571a25f0a04SGreg Roach } 572a25f0a04SGreg Roach } 5738f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 57413abd6f3SGreg Roach $min = []; 57513abd6f3SGreg Roach $max = []; 576a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 577f5b60decSGreg Roach if ($tmp->isOK()) { 578f5b60decSGreg Roach $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365; 579f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 580a25f0a04SGreg Roach } 58139ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 582a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 583f5b60decSGreg Roach if ($tmp->isOK()) { 584f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 1; 585f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 586a25f0a04SGreg Roach } 58739ca88baSGreg Roach $husband = $family->husband(); 588e364afe4SGreg Roach if ($husband instanceof self) { 5892e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 590f5b60decSGreg Roach if ($tmp->isOK()) { 591f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 592f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 593a25f0a04SGreg Roach } 594a25f0a04SGreg Roach } 59539ca88baSGreg Roach $wife = $family->wife(); 596e364afe4SGreg Roach if ($wife instanceof self) { 5972e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 598f5b60decSGreg Roach if ($tmp->isOK()) { 599f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 600f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 601a25f0a04SGreg Roach } 602a25f0a04SGreg Roach } 60339ca88baSGreg Roach foreach ($family->children() as $child) { 604a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 605f5b60decSGreg Roach if ($tmp->isOK()) { 606f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 607f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 608a25f0a04SGreg Roach } 609a25f0a04SGreg Roach } 610a25f0a04SGreg Roach } 61139ca88baSGreg Roach foreach ($this->spouseFamilies() as $family) { 612a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 613f5b60decSGreg Roach if ($tmp->isOK()) { 614f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 615f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 616a25f0a04SGreg Roach } 61739ca88baSGreg Roach $spouse = $family->spouse($this); 618a25f0a04SGreg Roach if ($spouse) { 619a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 620f5b60decSGreg Roach if ($tmp->isOK()) { 621f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 622f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 623a25f0a04SGreg Roach } 624a25f0a04SGreg Roach } 62539ca88baSGreg Roach foreach ($family->children() as $child) { 626a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 627f5b60decSGreg Roach if ($tmp->isOK()) { 628e364afe4SGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65); 629f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 630a25f0a04SGreg Roach } 631a25f0a04SGreg Roach } 632a25f0a04SGreg Roach } 633a25f0a04SGreg Roach if ($min && $max) { 63459f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 635a25f0a04SGreg Roach 63665e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 6374686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 638a25f0a04SGreg Roach } else { 6394686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 640a25f0a04SGreg Roach } 641a25f0a04SGreg Roach } 642a25f0a04SGreg Roach } 643a25f0a04SGreg Roach 6444686330aSGreg Roach return $this->estimated_birth_date; 645a25f0a04SGreg Roach } 646a25f0a04SGreg Roach 647a25f0a04SGreg Roach /** 648a25f0a04SGreg Roach * Generate an estimated date of death. 649a25f0a04SGreg Roach * 650a25f0a04SGreg Roach * @return Date 651a25f0a04SGreg Roach */ 6528f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 653c1010edaSGreg Roach { 6544686330aSGreg Roach if ($this->estimated_death_date === null) { 655a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 656a25f0a04SGreg Roach if ($date->isOK()) { 6574686330aSGreg Roach $this->estimated_death_date = $date; 658a25f0a04SGreg Roach break; 659a25f0a04SGreg Roach } 660a25f0a04SGreg Roach } 6614686330aSGreg Roach if ($this->estimated_death_date === null) { 662f5b60decSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay()) { 663c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 6644686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 665a25f0a04SGreg Roach } else { 6664686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 667a25f0a04SGreg Roach } 668a25f0a04SGreg Roach } 669a25f0a04SGreg Roach } 670a25f0a04SGreg Roach 6714686330aSGreg Roach return $this->estimated_death_date; 672a25f0a04SGreg Roach } 673a25f0a04SGreg Roach 674a25f0a04SGreg Roach /** 675a25f0a04SGreg Roach * Get the sex - M F or U 676a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 677a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 678a25f0a04SGreg Roach * 679a25f0a04SGreg Roach * @return string 680a25f0a04SGreg Roach */ 681e364afe4SGreg Roach public function sex(): string 682c1010edaSGreg Roach { 683a25f0a04SGreg Roach if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) { 684a25f0a04SGreg Roach return $match[1]; 685a25f0a04SGreg Roach } 686b2ce94c6SRico Sonntag 687b2ce94c6SRico Sonntag return 'U'; 688a25f0a04SGreg Roach } 689a25f0a04SGreg Roach 690a25f0a04SGreg Roach /** 691a25f0a04SGreg Roach * Get a list of this individual’s spouse families 692a25f0a04SGreg Roach * 693cbc1590aSGreg Roach * @param int|null $access_level 694a25f0a04SGreg Roach * 695b5c8fd7eSGreg Roach * @return Collection<Family> 696a25f0a04SGreg Roach */ 69773d58381SGreg Roach public function spouseFamilies(int $access_level = null): Collection 698c1010edaSGreg Roach { 699d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 700d9e083e7SGreg Roach 701d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 702d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 7034b9ff166SGreg Roach } 7044b9ff166SGreg Roach 70539ca88baSGreg Roach $families = new Collection(); 706d9e083e7SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level) as $fact) { 707dc124885SGreg Roach $family = $fact->target(); 708d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 70939ca88baSGreg Roach $families->push($family); 710a25f0a04SGreg Roach } 711a25f0a04SGreg Roach } 712a25f0a04SGreg Roach 71339ca88baSGreg Roach return new Collection($families); 714a25f0a04SGreg Roach } 715a25f0a04SGreg Roach 716a25f0a04SGreg Roach /** 717a25f0a04SGreg Roach * Get the current spouse of this individual. 718a25f0a04SGreg Roach * 719a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 720a25f0a04SGreg Roach * in chronological order, and take the last one found. 721a25f0a04SGreg Roach * 722a25f0a04SGreg Roach * @return Individual|null 723a25f0a04SGreg Roach */ 724e364afe4SGreg Roach public function getCurrentSpouse(): ?Individual 725c1010edaSGreg Roach { 72639ca88baSGreg Roach $family = $this->spouseFamilies()->last(); 72739ca88baSGreg Roach 72839ca88baSGreg Roach if ($family instanceof Family) { 72939ca88baSGreg Roach return $family->spouse($this); 730a25f0a04SGreg Roach } 731b2ce94c6SRico Sonntag 732b2ce94c6SRico Sonntag return null; 733a25f0a04SGreg Roach } 734a25f0a04SGreg Roach 735a25f0a04SGreg Roach /** 736a25f0a04SGreg Roach * Count the children belonging to this individual. 737a25f0a04SGreg Roach * 738cbc1590aSGreg Roach * @return int 739a25f0a04SGreg Roach */ 740e364afe4SGreg Roach public function numberOfChildren(): int 741c1010edaSGreg Roach { 7427d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 7433dc7bbe9SGreg Roach return (int) $match[1]; 744b2ce94c6SRico Sonntag } 745b2ce94c6SRico Sonntag 74613abd6f3SGreg Roach $children = []; 74739ca88baSGreg Roach foreach ($this->spouseFamilies() as $fam) { 74839ca88baSGreg Roach foreach ($fam->children() as $child) { 749c0935879SGreg Roach $children[$child->xref()] = true; 750a25f0a04SGreg Roach } 751a25f0a04SGreg Roach } 752a25f0a04SGreg Roach 753a25f0a04SGreg Roach return count($children); 754a25f0a04SGreg Roach } 755a25f0a04SGreg Roach 756a25f0a04SGreg Roach /** 757a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 758a25f0a04SGreg Roach * 759cbc1590aSGreg Roach * @param int|null $access_level 760a25f0a04SGreg Roach * 761b5c8fd7eSGreg Roach * @return Collection<Family> 762a25f0a04SGreg Roach */ 76373d58381SGreg Roach public function childFamilies(int $access_level = null): Collection 764c1010edaSGreg Roach { 765d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 7664b9ff166SGreg Roach 767d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 768d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 769d9e083e7SGreg Roach } 770a25f0a04SGreg Roach 77139ca88baSGreg Roach $families = new Collection(); 77239ca88baSGreg Roach 773d9e083e7SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level) as $fact) { 774dc124885SGreg Roach $family = $fact->target(); 775d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 77639ca88baSGreg Roach $families->push($family); 777a25f0a04SGreg Roach } 778a25f0a04SGreg Roach } 779a25f0a04SGreg Roach 780a25f0a04SGreg Roach return $families; 781a25f0a04SGreg Roach } 782a25f0a04SGreg Roach 783a25f0a04SGreg Roach /** 784a25f0a04SGreg Roach * Get a list of step-parent families. 785a25f0a04SGreg Roach * 786b5c8fd7eSGreg Roach * @return Collection<Family> 787a25f0a04SGreg Roach */ 788820b62dfSGreg Roach public function childStepFamilies(): Collection 789c1010edaSGreg Roach { 790ed5b6227SGreg Roach $step_families = new Collection(); 79139ca88baSGreg Roach $families = $this->childFamilies(); 792a25f0a04SGreg Roach foreach ($families as $family) { 793ed5b6227SGreg Roach foreach ($family->spouses() as $parent) { 794ed5b6227SGreg Roach foreach ($parent->spouseFamilies() as $step_family) { 79539ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 796ed5b6227SGreg Roach $step_families->add($step_family); 797a25f0a04SGreg Roach } 798a25f0a04SGreg Roach } 799a25f0a04SGreg Roach } 800a25f0a04SGreg Roach } 801a25f0a04SGreg Roach 8028c627a69SGreg Roach return $step_families->uniqueStrict(static function (Family $family): string { 8038c627a69SGreg Roach return $family->xref(); 8048c627a69SGreg Roach }); 805a25f0a04SGreg Roach } 806a25f0a04SGreg Roach 807a25f0a04SGreg Roach /** 808a25f0a04SGreg Roach * Get a list of step-parent families. 809a25f0a04SGreg Roach * 810b5c8fd7eSGreg Roach * @return Collection<Family> 811a25f0a04SGreg Roach */ 812820b62dfSGreg Roach public function spouseStepFamilies(): Collection 813c1010edaSGreg Roach { 81413abd6f3SGreg Roach $step_families = []; 81539ca88baSGreg Roach $families = $this->spouseFamilies(); 816820b62dfSGreg Roach 817a25f0a04SGreg Roach foreach ($families as $family) { 81839ca88baSGreg Roach $spouse = $family->spouse($this); 819820b62dfSGreg Roach 820d823340dSGreg Roach if ($spouse instanceof self) { 82139ca88baSGreg Roach foreach ($family->spouse($this)->spouseFamilies() as $step_family) { 82239ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 823a25f0a04SGreg Roach $step_families[] = $step_family; 824a25f0a04SGreg Roach } 825a25f0a04SGreg Roach } 826a25f0a04SGreg Roach } 827a25f0a04SGreg Roach } 828a25f0a04SGreg Roach 829820b62dfSGreg Roach return new Collection($step_families); 830a25f0a04SGreg Roach } 831a25f0a04SGreg Roach 832a25f0a04SGreg Roach /** 833a25f0a04SGreg Roach * A label for a parental family group 834a25f0a04SGreg Roach * 835a25f0a04SGreg Roach * @param Family $family 836a25f0a04SGreg Roach * 837a25f0a04SGreg Roach * @return string 838a25f0a04SGreg Roach */ 839820b62dfSGreg Roach public function getChildFamilyLabel(Family $family): string 840c1010edaSGreg Roach { 8417d70e4a7SGreg Roach preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match); 842b2ce94c6SRico Sonntag 8437d70e4a7SGreg Roach $values = [ 8447d70e4a7SGreg Roach 'birth' => I18N::translate('Family with parents'), 8457d70e4a7SGreg Roach 'adopted' => I18N::translate('Family with adoptive parents'), 8467d70e4a7SGreg Roach 'foster' => I18N::translate('Family with foster parents'), 8477d70e4a7SGreg Roach 'sealing' => /* I18N: “sealing” is a Mormon ceremony. */ 8487d70e4a7SGreg Roach I18N::translate('Family with sealing parents'), 8497d70e4a7SGreg Roach 'rada' => /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */ 8507d70e4a7SGreg Roach I18N::translate('Family with rada parents'), 8517d70e4a7SGreg Roach ]; 8527d70e4a7SGreg Roach 8537d70e4a7SGreg Roach return $values[$match[1] ?? 'birth'] ?? $values['birth']; 854a25f0a04SGreg Roach } 855a25f0a04SGreg Roach 856a25f0a04SGreg Roach /** 857a25f0a04SGreg Roach * Create a label for a step family 858a25f0a04SGreg Roach * 859a25f0a04SGreg Roach * @param Family $step_family 860a25f0a04SGreg Roach * 861a25f0a04SGreg Roach * @return string 862a25f0a04SGreg Roach */ 8638f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 864c1010edaSGreg Roach { 86539ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 866a25f0a04SGreg Roach if ($family !== $step_family) { 867a25f0a04SGreg Roach // Must be a step-family 86839ca88baSGreg Roach foreach ($family->spouses() as $parent) { 86939ca88baSGreg Roach foreach ($step_family->spouses() as $step_parent) { 870a25f0a04SGreg Roach if ($parent === $step_parent) { 871a25f0a04SGreg Roach // One common parent - must be a step family 872e364afe4SGreg Roach if ($parent->sex() === 'M') { 873a25f0a04SGreg Roach // Father’s family with someone else 87439ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 875a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 87639ca88baSGreg Roach return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName()); 877b2ce94c6SRico Sonntag } 878b2ce94c6SRico Sonntag 879a25f0a04SGreg Roach /* I18N: A step-family. */ 880bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 881a25f0a04SGreg Roach } 882b2ce94c6SRico Sonntag 883a25f0a04SGreg Roach // Mother’s family with someone else 88439ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 885a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 88639ca88baSGreg Roach return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName()); 887b2ce94c6SRico Sonntag } 888b2ce94c6SRico Sonntag 889a25f0a04SGreg Roach /* I18N: A step-family. */ 890bbb76c12SGreg Roach return I18N::translate('Mother’s family with an unknown individual'); 891a25f0a04SGreg Roach } 892a25f0a04SGreg Roach } 893a25f0a04SGreg Roach } 894a25f0a04SGreg Roach } 895a25f0a04SGreg Roach } 896a25f0a04SGreg Roach 897a25f0a04SGreg Roach // Perahps same parents - but a different family record? 898a25f0a04SGreg Roach return I18N::translate('Family with parents'); 899a25f0a04SGreg Roach } 900a25f0a04SGreg Roach 901225e381fSGreg Roach /** 902225e381fSGreg Roach * Get the description for the family. 903225e381fSGreg Roach * 904225e381fSGreg Roach * For example, "XXX's family with new wife". 905225e381fSGreg Roach * 906225e381fSGreg Roach * @param Family $family 907225e381fSGreg Roach * 908225e381fSGreg Roach * @return string 909225e381fSGreg Roach */ 910e364afe4SGreg Roach public function getSpouseFamilyLabel(Family $family): string 911c1010edaSGreg Roach { 91239ca88baSGreg Roach $spouse = $family->spouse($this); 913225e381fSGreg Roach if ($spouse) { 914225e381fSGreg Roach /* I18N: %s is the spouse name */ 91539ca88baSGreg Roach return I18N::translate('Family with %s', $spouse->fullName()); 916225e381fSGreg Roach } 917b2ce94c6SRico Sonntag 91839ca88baSGreg Roach return $family->fullName(); 919225e381fSGreg Roach } 920225e381fSGreg Roach 921a25f0a04SGreg Roach /** 922961ec755SGreg Roach * If this object has no name, what do we call it? 923961ec755SGreg Roach * 924961ec755SGreg Roach * @return string 925961ec755SGreg Roach */ 9268f53f488SRico Sonntag public function getFallBackName(): string 927c1010edaSGreg Roach { 928a25f0a04SGreg Roach return '@P.N. /@N.N./'; 929a25f0a04SGreg Roach } 930a25f0a04SGreg Roach 931a25f0a04SGreg Roach /** 932a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 933a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 934a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 935a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 936a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 937a25f0a04SGreg Roach * recorded in the NAME field. e.g. 938a25f0a04SGreg Roach * 939a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 940a25f0a04SGreg Roach * 2 GIVN Robert 941a25f0a04SGreg Roach * 2 SPFX de 942a25f0a04SGreg Roach * 2 SURN CLITHEROW 943a25f0a04SGreg Roach * 2 NICK The Bald 944a25f0a04SGreg Roach * 945a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 946a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 947a25f0a04SGreg Roach * 948a25f0a04SGreg Roach * Handle multiple surnames, either as; 949a25f0a04SGreg Roach * 950a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 951a25f0a04SGreg Roach * or 952a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 953a25f0a04SGreg Roach * 2 GIVN Carlos 954a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 955a25f0a04SGreg Roach * 956a25f0a04SGreg Roach * @param string $type 95751928f9aSGreg Roach * @param string $value 958a25f0a04SGreg Roach * @param string $gedcom 959e364afe4SGreg Roach * 960e364afe4SGreg Roach * @return void 961a25f0a04SGreg Roach */ 96251928f9aSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 963c1010edaSGreg Roach { 964a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 965a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 966a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 967a25f0a04SGreg Roach 96876f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 969b6ac34efSGreg Roach $GIVN = preg_match('/\n' . $sublevel . ' GIVN (.+)/', $gedcom, $match) ? $match[1] : ''; 970b6ac34efSGreg Roach $SURN = preg_match('/\n' . $sublevel . ' SURN (.+)/', $gedcom, $match) ? $match[1] : ''; 971a25f0a04SGreg Roach 972a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 97376f666f4SGreg Roach if ($SURN !== '') { 974a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 975a25f0a04SGreg Roach } else { 97613abd6f3SGreg Roach $SURNS = []; 977a25f0a04SGreg Roach } 97876f666f4SGreg Roach 979a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 980a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 981a25f0a04SGreg Roach 982a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 983a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 984a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 985a25f0a04SGreg Roach 986a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 98751928f9aSGreg Roach if (substr_count($value, '/') % 2 === 1) { 98851928f9aSGreg Roach $value .= '/'; 989a25f0a04SGreg Roach } 990a25f0a04SGreg Roach 991a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 99251928f9aSGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $value); 993a25f0a04SGreg Roach 994a25f0a04SGreg Roach // Extract the surname. 995a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 996a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 997a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 998a25f0a04SGreg Roach } else { 999a25f0a04SGreg Roach $surname = ''; 1000a25f0a04SGreg Roach } 1001a25f0a04SGreg Roach 1002a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 1003a25f0a04SGreg Roach if (!$SURNS) { 1004a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 1005a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 1006a25f0a04SGreg Roach $SURNS = $matches[1]; 1007a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 1008a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 1009a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 1010a25f0a04SGreg Roach } 1011a25f0a04SGreg Roach } else { 1012a25f0a04SGreg Roach // It is valid not to have a surname at all 101313abd6f3SGreg Roach $SURNS = ['']; 1014a25f0a04SGreg Roach } 1015a25f0a04SGreg Roach } 1016a25f0a04SGreg Roach 1017a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 1018a25f0a04SGreg Roach if (!$GIVN) { 1019c1010edaSGreg Roach // remove surname 1020c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?\/.*\/ ?/', ' ', $full); 1021c1010edaSGreg Roach // remove nickname 1022c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?".+"/', ' ', $GIVN); 1023c1010edaSGreg Roach // multiple spaces, caused by the above 1024c72b7fa4SGreg Roach $GIVN = preg_replace('/ {2,}/', ' ', $GIVN); 1025c1010edaSGreg Roach // leading/trailing spaces, caused by the above 1026c72b7fa4SGreg Roach $GIVN = preg_replace('/^ | $/', '', $GIVN); 1027a25f0a04SGreg Roach } 1028a25f0a04SGreg Roach 1029a25f0a04SGreg Roach // Add placeholder for unknown given name 1030a25f0a04SGreg Roach if (!$GIVN) { 1031d823340dSGreg Roach $GIVN = self::PRAENOMEN_NESCIO; 103273f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1033a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1034a25f0a04SGreg Roach } 1035a25f0a04SGreg Roach 1036a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1037a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1038a25f0a04SGreg Roach // $full is for display on-screen 1039a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1040a25f0a04SGreg Roach 1041a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1042d823340dSGreg Roach $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full); 1043d823340dSGreg Roach $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full); 1044c6f196c3SGreg Roach // Format for display 1045d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1046acc34ea1SGreg Roach // Localise quotation marks around the nickname 10470b5fd0a6SGreg Roach $full = preg_replace_callback('/"([^&]*)"/', static function (array $matches): string { 1048c652cdbdSGreg Roach return '<q class="wt-nickname">' . $matches[1] . '</q>'; 10498d68cabeSGreg Roach }, $full); 1050a25f0a04SGreg Roach 1051c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1052ee51991cSGreg Roach $full = preg_replace('/([^ >\x{200C}]*)\*/u', '<span class="starredname">\\1</span>', $full); 1053a25f0a04SGreg Roach 1054a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1055a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1056a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1057a25f0a04SGreg Roach 1058ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1059a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1060e364afe4SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) { 1061a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1062e364afe4SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) { 1063a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1064a25f0a04SGreg Roach } 1065a25f0a04SGreg Roach 1066bdb3725aSGreg Roach $this->getAllNames[] = [ 1067a25f0a04SGreg Roach 'type' => $type, 1068a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1069c1010edaSGreg Roach 'full' => $full, 1070c1010edaSGreg Roach // This is used for display 1071c1010edaSGreg Roach 'fullNN' => $fullNN, 1072c1010edaSGreg Roach // This goes into the database 1073c1010edaSGreg Roach 'surname' => $surname, 1074c1010edaSGreg Roach // This goes into the database 1075c1010edaSGreg Roach 'givn' => $GIVN, 1076c1010edaSGreg Roach // This goes into the database 1077c1010edaSGreg Roach 'surn' => $SURN, 1078c1010edaSGreg Roach // This goes into the database 107913abd6f3SGreg Roach ]; 1080a25f0a04SGreg Roach } 1081a25f0a04SGreg Roach } 1082a25f0a04SGreg Roach 1083a25f0a04SGreg Roach /** 108476692c8bSGreg Roach * Extract names from the GEDCOM record. 1085c7ff4153SGreg Roach * 1086c7ff4153SGreg Roach * @return void 1087a25f0a04SGreg Roach */ 1088e364afe4SGreg Roach public function extractNames(): void 1089c1010edaSGreg Roach { 1090d9e083e7SGreg Roach $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree); 1091d9e083e7SGreg Roach 10928f53f488SRico Sonntag $this->extractNamesFromFacts( 10938f53f488SRico Sonntag 1, 10948f53f488SRico Sonntag 'NAME', 1095d9e083e7SGreg Roach $this->facts(['NAME'], false, $access_level) 10968f53f488SRico Sonntag ); 1097a25f0a04SGreg Roach } 1098a25f0a04SGreg Roach 1099a25f0a04SGreg Roach /** 1100a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1101a25f0a04SGreg Roach * selection items or favorites. 1102a25f0a04SGreg Roach * 1103a25f0a04SGreg Roach * @return string 1104a25f0a04SGreg Roach */ 11058f53f488SRico Sonntag public function formatListDetails(): string 1106c1010edaSGreg Roach { 1107a25f0a04SGreg Roach return 11088d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 11098d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1110a25f0a04SGreg Roach } 11118091bfd1SGreg Roach 11128091bfd1SGreg Roach /** 11138091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11148091bfd1SGreg Roach */ 11158091bfd1SGreg Roach public function lock(): void 11168091bfd1SGreg Roach { 11178091bfd1SGreg Roach DB::table('individuals') 11188091bfd1SGreg Roach ->where('i_file', '=', $this->tree->id()) 11198091bfd1SGreg Roach ->where('i_id', '=', $this->xref()) 11208091bfd1SGreg Roach ->lockForUpdate() 11218091bfd1SGreg Roach ->get(); 11228091bfd1SGreg Roach } 1123a25f0a04SGreg Roach} 1124