1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 22886b77daSGreg Roachuse Closure; 23a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar; 241fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 25665e281aSGreg Roachuse Fisharebest\Webtrees\Elements\PedigreeLinkageType; 26852ede8cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 2739ca88baSGreg Roachuse Illuminate\Support\Collection; 28a25f0a04SGreg Roach 2910e06497SGreg Roachuse function array_key_exists; 3010e06497SGreg Roachuse function count; 3110e06497SGreg Roachuse function in_array; 327d70e4a7SGreg Roachuse function preg_match; 337d70e4a7SGreg Roach 34a25f0a04SGreg Roach/** 3576692c8bSGreg Roach * A GEDCOM individual (INDI) object. 36a25f0a04SGreg Roach */ 37c1010edaSGreg Roachclass Individual extends GedcomRecord 38c1010edaSGreg Roach{ 39*e873f434SGreg Roach public const string RECORD_TYPE = 'INDI'; 4016d6367aSGreg Roach 418fb4e87cSGreg Roach // Placeholders to indicate unknown names 42*e873f434SGreg Roach public const string NOMEN_NESCIO = '@N.N.'; 43*e873f434SGreg Roach public const string PRAENOMEN_NESCIO = '@P.N.'; 448fb4e87cSGreg Roach 45*e873f434SGreg Roach protected const string ROUTE_NAME = IndividualPage::class; 46a25f0a04SGreg Roach 477fa97a69SGreg Roach /** Used in some lists to keep track of this individual’s generation in that list */ 481ff45046SGreg Roach public int|null $generation = null; 49a25f0a04SGreg Roach 501ff45046SGreg Roach private Date|null $estimated_birth_date = null; 51a25f0a04SGreg Roach 521ff45046SGreg Roach private Date|null $estimated_death_date = null; 53a25f0a04SGreg Roach 54a25f0a04SGreg Roach /** 55c156e8f5SGreg Roach * A closure which will compare individuals by birth date. 56c156e8f5SGreg Roach * 57c6921a17SGreg Roach * @return Closure(Individual,Individual):int 58c156e8f5SGreg Roach */ 59c156e8f5SGreg Roach public static function birthDateComparator(): Closure 60c156e8f5SGreg Roach { 61f25fc0f9SGreg Roach return static fn (Individual $x, Individual $y): int => Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 62c156e8f5SGreg Roach } 63c156e8f5SGreg Roach 64c156e8f5SGreg Roach /** 65c156e8f5SGreg Roach * A closure which will compare individuals by death date. 66c156e8f5SGreg Roach * 67c6921a17SGreg Roach * @return Closure(Individual,Individual):int 68c156e8f5SGreg Roach */ 69c156e8f5SGreg Roach public static function deathDateComparator(): Closure 70c156e8f5SGreg Roach { 71f25fc0f9SGreg Roach return static fn (Individual $x, Individual $y): int => Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate()); 72c156e8f5SGreg Roach } 73c156e8f5SGreg Roach 74c156e8f5SGreg Roach /** 75a25f0a04SGreg Roach * Can the name of this record be shown? 76a25f0a04SGreg Roach * 7776692c8bSGreg Roach * @param int|null $access_level 7876692c8bSGreg Roach * 7976692c8bSGreg Roach * @return bool 80a25f0a04SGreg Roach */ 812c6f1bd5SGreg Roach public function canShowName(int|null $access_level = null): bool 82c1010edaSGreg Roach { 833529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 844b9ff166SGreg Roach 85f56b86d2SGreg Roach return (int) $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 86a25f0a04SGreg Roach } 87a25f0a04SGreg Roach 88a25f0a04SGreg Roach /** 8976692c8bSGreg Roach * Can this individual be shown? 90a25f0a04SGreg Roach * 9176692c8bSGreg Roach * @param int $access_level 9276692c8bSGreg Roach * 9376692c8bSGreg Roach * @return bool 94a25f0a04SGreg Roach */ 9535584196SGreg Roach protected function canShowByType(int $access_level): bool 96c1010edaSGreg Roach { 97a25f0a04SGreg Roach // Dead people... 98f56b86d2SGreg Roach if ((int) $this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 99a25f0a04SGreg Roach $keep_alive = false; 10054ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 101b6ec1ccfSGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH !== 0) { 102dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 103a25f0a04SGreg Roach foreach ($matches as $match) { 104a25f0a04SGreg Roach $date = new Date($match[1]); 105a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 106a25f0a04SGreg Roach $keep_alive = true; 107a25f0a04SGreg Roach break; 108a25f0a04SGreg Roach } 109a25f0a04SGreg Roach } 110a25f0a04SGreg Roach } 11154ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 112b6ec1ccfSGreg Roach if ($KEEP_ALIVE_YEARS_DEATH !== 0) { 113dce4f3a4SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*\n2 DATE (.+)/', $this->gedcom, $matches, PREG_SET_ORDER); 114a25f0a04SGreg Roach foreach ($matches as $match) { 115a25f0a04SGreg Roach $date = new Date($match[1]); 116a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 117a25f0a04SGreg Roach $keep_alive = true; 118a25f0a04SGreg Roach break; 119a25f0a04SGreg Roach } 120a25f0a04SGreg Roach } 121a25f0a04SGreg Roach } 122a25f0a04SGreg Roach if (!$keep_alive) { 123a25f0a04SGreg Roach return true; 124a25f0a04SGreg Roach } 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 1271fe542e9SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_PATH_LENGTH); 1281fe542e9SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 1297c4add84SGreg Roach 13054ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1314b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 132a25f0a04SGreg Roach } 133a25f0a04SGreg Roach 134a25f0a04SGreg Roach // No restriction found - show living people to members only: 1354b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 136a25f0a04SGreg Roach } 137a25f0a04SGreg Roach 138a25f0a04SGreg Roach /** 139a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 140a25f0a04SGreg Roach * 141a25f0a04SGreg Roach * @param Individual $target 142cbc1590aSGreg Roach * @param int $distance 143a25f0a04SGreg Roach * 144cbc1590aSGreg Roach * @return bool 145a25f0a04SGreg Roach */ 14624f2a3afSGreg Roach private static function isRelated(Individual $target, int $distance): bool 147c1010edaSGreg Roach { 148a25f0a04SGreg Roach static $cache = null; 149a25f0a04SGreg Roach 1501fe542e9SGreg Roach $user_individual = Registry::individualFactory()->make($target->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $target->tree); 151b6ec1ccfSGreg Roach if ($user_individual instanceof Individual) { 152a25f0a04SGreg Roach if (!$cache) { 15313abd6f3SGreg Roach $cache = [ 15413abd6f3SGreg Roach 0 => [$user_individual], 15513abd6f3SGreg Roach 1 => [], 15613abd6f3SGreg Roach ]; 1578d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 158dc124885SGreg Roach $family = $fact->target(); 159e24444eeSGreg Roach if ($family instanceof Family) { 160a25f0a04SGreg Roach $cache[1][] = $family; 161a25f0a04SGreg Roach } 162a25f0a04SGreg Roach } 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach } else { 165a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 166a25f0a04SGreg Roach return true; 167a25f0a04SGreg Roach } 168a25f0a04SGreg Roach 169a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 170a25f0a04SGreg Roach $distance *= 2; 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach // Consider each path length in turn 173a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 174a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 175a25f0a04SGreg Roach // We have already calculated all records with this length 176e364afe4SGreg Roach if ($n % 2 === 0 && in_array($target, $cache[$n], true)) { 177a25f0a04SGreg Roach return true; 178a25f0a04SGreg Roach } 179a25f0a04SGreg Roach } else { 180a25f0a04SGreg Roach // Need to calculate these paths 18113abd6f3SGreg Roach $cache[$n] = []; 182e364afe4SGreg Roach if ($n % 2 === 0) { 183a25f0a04SGreg Roach // Add FAM->INDI links 184a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 1858d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 186dc124885SGreg Roach $individual = $fact->target(); 187a25f0a04SGreg Roach // Don’t backtrack 188e364afe4SGreg Roach if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) { 189a25f0a04SGreg Roach $cache[$n][] = $individual; 190a25f0a04SGreg Roach } 191a25f0a04SGreg Roach } 192a25f0a04SGreg Roach } 193a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 194a25f0a04SGreg Roach return true; 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach } else { 197a25f0a04SGreg Roach // Add INDI->FAM links 198a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 1998d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 200dc124885SGreg Roach $family = $fact->target(); 201a25f0a04SGreg Roach // Don’t backtrack 202e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 203a25f0a04SGreg Roach $cache[$n][] = $family; 204a25f0a04SGreg Roach } 205a25f0a04SGreg Roach } 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach } 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach return false; 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 21476692c8bSGreg Roach /** 215a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 216a25f0a04SGreg Roach * If not known to be dead, then assume living. 217a25f0a04SGreg Roach * 218cbc1590aSGreg Roach * @return bool 219a25f0a04SGreg Roach */ 2208f53f488SRico Sonntag public function isDead(): bool 221c1010edaSGreg Roach { 222c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 223d97083feSGreg Roach $today_jd = Registry::timestampFactory()->now()->julianDay(); 224a25f0a04SGreg Roach 225a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 2268d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 227a25f0a04SGreg Roach return true; 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach 230e63974caSStefan Weil // If any event occurred more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 231a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 232a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 233a25f0a04SGreg Roach $date = new Date($date_match); 234269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) { 235a25f0a04SGreg Roach return true; 236a25f0a04SGreg Roach } 237a25f0a04SGreg Roach } 238a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 239a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 240a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 241a25f0a04SGreg Roach return false; 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach } 244a25f0a04SGreg Roach 245a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 246a25f0a04SGreg Roach 247a25f0a04SGreg Roach // Check parents (birth and adopted) 24839ca88baSGreg Roach foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) { 24939ca88baSGreg Roach foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) { 250a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 251a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 252a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 253a25f0a04SGreg Roach $date = new Date($date_match); 254269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) { 255a25f0a04SGreg Roach return true; 256a25f0a04SGreg Roach } 257a25f0a04SGreg Roach } 258a25f0a04SGreg Roach } 259a25f0a04SGreg Roach } 260a25f0a04SGreg Roach 261a25f0a04SGreg Roach // Check spouses 26239ca88baSGreg Roach foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) { 263a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 264a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 265a25f0a04SGreg Roach $date = new Date($date_match); 266a25f0a04SGreg Roach // Assume marriage occurs after age of 10 267269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) { 268a25f0a04SGreg Roach return true; 269a25f0a04SGreg Roach } 270a25f0a04SGreg Roach } 271a25f0a04SGreg Roach // Check spouse dates 27239ca88baSGreg Roach $spouse = $family->spouse($this, Auth::PRIV_HIDE); 273a25f0a04SGreg Roach if ($spouse) { 274a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 275a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 276a25f0a04SGreg Roach $date = new Date($date_match); 277a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 278269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) { 279a25f0a04SGreg Roach return true; 280a25f0a04SGreg Roach } 281a25f0a04SGreg Roach } 282a25f0a04SGreg Roach } 283a25f0a04SGreg Roach // Check child dates 28439ca88baSGreg Roach foreach ($family->children(Auth::PRIV_HIDE) as $child) { 285a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 286a25f0a04SGreg Roach // Assume children born after age of 15 287a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 288a25f0a04SGreg Roach $date = new Date($date_match); 289269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) { 290a25f0a04SGreg Roach return true; 291a25f0a04SGreg Roach } 292a25f0a04SGreg Roach } 293a25f0a04SGreg Roach // Check grandchildren 29439ca88baSGreg Roach foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) { 29539ca88baSGreg Roach foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) { 296a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 297a25f0a04SGreg Roach // Assume grandchildren born after age of 30 298a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 299a25f0a04SGreg Roach $date = new Date($date_match); 300269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) { 301a25f0a04SGreg Roach return true; 302a25f0a04SGreg Roach } 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach } 306a25f0a04SGreg Roach } 307a25f0a04SGreg Roach } 308a25f0a04SGreg Roach 309a25f0a04SGreg Roach return false; 310a25f0a04SGreg Roach } 311a25f0a04SGreg Roach 312a25f0a04SGreg Roach /** 313a25f0a04SGreg Roach * Find the highlighted media object for an individual 314a25f0a04SGreg Roach */ 3151ff45046SGreg Roach public function findHighlightedMediaFile(): MediaFile|null 316c1010edaSGreg Roach { 31792647683SGreg Roach $fact = $this->facts(['OBJE']) 31819d319e7SGreg Roach ->first(static function (Fact $fact): bool { 319dc124885SGreg Roach $media = $fact->target(); 32019d319e7SGreg Roach 32119d319e7SGreg Roach return $media instanceof Media && $media->firstImageFile() instanceof MediaFile; 32219d319e7SGreg Roach }); 32319d319e7SGreg Roach 32492647683SGreg Roach if ($fact instanceof Fact && $fact->target() instanceof Media) { 32592647683SGreg Roach return $fact->target()->firstImageFile(); 326a25f0a04SGreg Roach } 327a25f0a04SGreg Roach 328a25f0a04SGreg Roach return null; 329a25f0a04SGreg Roach } 330a25f0a04SGreg Roach 331a25f0a04SGreg Roach /** 3327b0d562eSGreg Roach * Display the preferred image for this individual. 333a25f0a04SGreg Roach * Use an icon if no image is available. 334a25f0a04SGreg Roach * 335dce07401SGreg Roach * @param int $width Pixels 336dce07401SGreg Roach * @param int $height Pixels 337dce07401SGreg Roach * @param string $fit "crop" or "contain" 33809482a55SGreg Roach * @param array<string> $attributes Additional HTML attributes 339dce07401SGreg Roach * 340a25f0a04SGreg Roach * @return string 341a25f0a04SGreg Roach */ 34224f2a3afSGreg Roach public function displayImage(int $width, int $height, string $fit, array $attributes): string 343c1010edaSGreg Roach { 3444a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 3454a9f750fSGreg Roach 3464a9f750fSGreg Roach if ($media_file !== null) { 3474a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 348a25f0a04SGreg Roach } 3494a9f750fSGreg Roach 350b6ec1ccfSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE') === '1') { 35168b631f1SGreg Roach return '<i class="icon-silhouette icon-silhouette-' . strtolower($this->sex()) . ' wt-icon-flip-rtl"></i>'; 3524a9f750fSGreg Roach } 3534a9f750fSGreg Roach 3544a9f750fSGreg Roach return ''; 355a25f0a04SGreg Roach } 356a25f0a04SGreg Roach 357a25f0a04SGreg Roach /** 358a25f0a04SGreg Roach * Get the date of birth 359a25f0a04SGreg Roach * 360a25f0a04SGreg Roach * @return Date 361a25f0a04SGreg Roach */ 3628f53f488SRico Sonntag public function getBirthDate(): Date 363c1010edaSGreg Roach { 364a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 365a25f0a04SGreg Roach if ($date->isOK()) { 366a25f0a04SGreg Roach return $date; 367a25f0a04SGreg Roach } 368a25f0a04SGreg Roach } 369a25f0a04SGreg Roach 370a25f0a04SGreg Roach return new Date(''); 371a25f0a04SGreg Roach } 372a25f0a04SGreg Roach 373a25f0a04SGreg Roach /** 374a25f0a04SGreg Roach * Get the place of birth 375a25f0a04SGreg Roach * 37616d0b7f7SRico Sonntag * @return Place 377a25f0a04SGreg Roach */ 3788f53f488SRico Sonntag public function getBirthPlace(): Place 379c1010edaSGreg Roach { 380a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 381a25f0a04SGreg Roach return $place; 382a25f0a04SGreg Roach } 383a25f0a04SGreg Roach 384b20ddbf9SGreg Roach return new Place('', $this->tree); 385a25f0a04SGreg Roach } 386a25f0a04SGreg Roach 387a25f0a04SGreg Roach /** 388a25f0a04SGreg Roach * Get the date of death 389a25f0a04SGreg Roach * 390a25f0a04SGreg Roach * @return Date 391a25f0a04SGreg Roach */ 3928f53f488SRico Sonntag public function getDeathDate(): Date 393c1010edaSGreg Roach { 394a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 395a25f0a04SGreg Roach if ($date->isOK()) { 396a25f0a04SGreg Roach return $date; 397a25f0a04SGreg Roach } 398a25f0a04SGreg Roach } 399a25f0a04SGreg Roach 400a25f0a04SGreg Roach return new Date(''); 401a25f0a04SGreg Roach } 402a25f0a04SGreg Roach 403a25f0a04SGreg Roach /** 404a25f0a04SGreg Roach * Get the place of death 405a25f0a04SGreg Roach * 40616d0b7f7SRico Sonntag * @return Place 407a25f0a04SGreg Roach */ 4088f53f488SRico Sonntag public function getDeathPlace(): Place 409c1010edaSGreg Roach { 410a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 411a25f0a04SGreg Roach return $place; 412a25f0a04SGreg Roach } 413a25f0a04SGreg Roach 414b20ddbf9SGreg Roach return new Place('', $this->tree); 415a25f0a04SGreg Roach } 416a25f0a04SGreg Roach 417a25f0a04SGreg Roach /** 418a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 41915d603e7SGreg Roach * Provide the place and full date using a tooltip. 420a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 421a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 422a25f0a04SGreg Roach * 423a25f0a04SGreg Roach * @return string 424a25f0a04SGreg Roach */ 4255e6816beSGreg Roach public function lifespan(): string 426c1010edaSGreg Roach { 42753f5ca04SGreg Roach // Just the first part of the place name. 428392561bbSGreg Roach $birth_place = strip_tags($this->getBirthPlace()->shortName()); 429392561bbSGreg Roach $death_place = strip_tags($this->getDeathPlace()->shortName()); 43053f5ca04SGreg Roach 43181b9dc5dSGreg Roach // Remove markup from dates. Use UTF_FSI / UTF_PDI instead of <bdi></bdi>, as 43281b9dc5dSGreg Roach // we cannot use HTML markup in title attributes. 43381b9dc5dSGreg Roach $birth_date = "\u{2068}" . strip_tags($this->getBirthDate()->display()) . "\u{2069}"; 43481b9dc5dSGreg Roach $death_date = "\u{2068}" . strip_tags($this->getDeathDate()->display()) . "\u{2069}"; 43515d603e7SGreg Roach 43653f5ca04SGreg Roach // Use minimum and maximum dates - to agree with the age calculations. 43753f5ca04SGreg Roach $birth_year = $this->getBirthDate()->minimumDate()->format('%Y'); 43853f5ca04SGreg Roach $death_year = $this->getDeathDate()->maximumDate()->format('%Y'); 43953f5ca04SGreg Roach 440f94b830fSGreg Roach if ($birth_year === '') { 441f94b830fSGreg Roach $birth_year = I18N::translate('…'); 442f94b830fSGreg Roach } 443f94b830fSGreg Roach 4447ecbeefdSGreg Roach if ($death_year === '' && $this->isDead()) { 4457ecbeefdSGreg Roach $death_year = I18N::translate('…'); 4467ecbeefdSGreg Roach } 4477ecbeefdSGreg Roach 448c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 449dacfe33cSGreg Roach return I18N::translate( 450a25f0a04SGreg Roach '%1$s–%2$s', 45153f5ca04SGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $birth_year . '</span>', 45253f5ca04SGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $death_year . '</span>' 453a25f0a04SGreg Roach ); 454a25f0a04SGreg Roach } 455a25f0a04SGreg Roach 456a25f0a04SGreg Roach /** 457a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 458a25f0a04SGreg Roach * 45909482a55SGreg Roach * @return array<Date> 460a25f0a04SGreg Roach */ 4618f53f488SRico Sonntag public function getAllBirthDates(): array 462c1010edaSGreg Roach { 4638d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 464d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 465d240bb87SGreg Roach 466d240bb87SGreg Roach if ($dates !== []) { 467d240bb87SGreg Roach return $dates; 468a25f0a04SGreg Roach } 469a25f0a04SGreg Roach } 470a25f0a04SGreg Roach 47113abd6f3SGreg Roach return []; 472a25f0a04SGreg Roach } 473a25f0a04SGreg Roach 474a25f0a04SGreg Roach /** 475a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 476a25f0a04SGreg Roach * 47709482a55SGreg Roach * @return array<Place> 478a25f0a04SGreg Roach */ 4798f53f488SRico Sonntag public function getAllBirthPlaces(): array 480c1010edaSGreg Roach { 4818d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 4828d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 483d240bb87SGreg Roach 48454c1ab5eSGreg Roach if ($places !== []) { 4854080d558SGreg Roach return $places; 486a25f0a04SGreg Roach } 487a25f0a04SGreg Roach } 488a25f0a04SGreg Roach 48913abd6f3SGreg Roach return []; 490a25f0a04SGreg Roach } 491a25f0a04SGreg Roach 492a25f0a04SGreg Roach /** 493a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 494a25f0a04SGreg Roach * 49509482a55SGreg Roach * @return array<Date> 496a25f0a04SGreg Roach */ 4978f53f488SRico Sonntag public function getAllDeathDates(): array 498c1010edaSGreg Roach { 4998d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 500d240bb87SGreg Roach $dates = $this->getAllEventDates([$event]); 501d240bb87SGreg Roach 502d240bb87SGreg Roach if ($dates !== []) { 503d240bb87SGreg Roach return $dates; 504a25f0a04SGreg Roach } 505a25f0a04SGreg Roach } 506a25f0a04SGreg Roach 50713abd6f3SGreg Roach return []; 508a25f0a04SGreg Roach } 509a25f0a04SGreg Roach 510a25f0a04SGreg Roach /** 511a25f0a04SGreg Roach * Get all the death places - for the individual lists. 512a25f0a04SGreg Roach * 51309482a55SGreg Roach * @return array<Place> 514a25f0a04SGreg Roach */ 5158f53f488SRico Sonntag public function getAllDeathPlaces(): array 516c1010edaSGreg Roach { 5178d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 5188d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 519d240bb87SGreg Roach 52054c1ab5eSGreg Roach if ($places !== []) { 5214080d558SGreg Roach return $places; 522a25f0a04SGreg Roach } 523a25f0a04SGreg Roach } 524a25f0a04SGreg Roach 52513abd6f3SGreg Roach return []; 526a25f0a04SGreg Roach } 527a25f0a04SGreg Roach 528a25f0a04SGreg Roach /** 529a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 530a25f0a04SGreg Roach * 531a25f0a04SGreg Roach * @return Date 532a25f0a04SGreg Roach */ 5338f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 534c1010edaSGreg Roach { 5358f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 536a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 537a25f0a04SGreg Roach if ($date->isOK()) { 5384686330aSGreg Roach $this->estimated_birth_date = $date; 539a25f0a04SGreg Roach break; 540a25f0a04SGreg Roach } 541a25f0a04SGreg Roach } 5428f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 54313abd6f3SGreg Roach $min = []; 54413abd6f3SGreg Roach $max = []; 545a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 546f5b60decSGreg Roach if ($tmp->isOK()) { 5476bd19c8cSGreg Roach $min[] = $tmp->minimumJulianDay() - 365 * (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 548f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 549a25f0a04SGreg Roach } 55039ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 551a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 552f5b60decSGreg Roach if ($tmp->isOK()) { 5536bd19c8cSGreg Roach $min[] = $tmp->maximumJulianDay() - 365; 554f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 555a25f0a04SGreg Roach } 55639ca88baSGreg Roach $husband = $family->husband(); 557e364afe4SGreg Roach if ($husband instanceof self) { 5582e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 559f5b60decSGreg Roach if ($tmp->isOK()) { 560f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 561f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 562a25f0a04SGreg Roach } 563a25f0a04SGreg Roach } 56439ca88baSGreg Roach $wife = $family->wife(); 565e364afe4SGreg Roach if ($wife instanceof self) { 5662e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 567f5b60decSGreg Roach if ($tmp->isOK()) { 568f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 569f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 570a25f0a04SGreg Roach } 571a25f0a04SGreg Roach } 57239ca88baSGreg Roach foreach ($family->children() as $child) { 573a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 574f5b60decSGreg Roach if ($tmp->isOK()) { 575f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 576f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 577a25f0a04SGreg Roach } 578a25f0a04SGreg Roach } 579a25f0a04SGreg Roach } 58039ca88baSGreg Roach foreach ($this->spouseFamilies() as $family) { 581a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 582f5b60decSGreg Roach if ($tmp->isOK()) { 583f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 584f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 585a25f0a04SGreg Roach } 58639ca88baSGreg Roach $spouse = $family->spouse($this); 587a25f0a04SGreg Roach if ($spouse) { 588a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 589f5b60decSGreg Roach if ($tmp->isOK()) { 590f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 591f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 592a25f0a04SGreg Roach } 593a25f0a04SGreg Roach } 59439ca88baSGreg Roach foreach ($family->children() as $child) { 595a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 596f5b60decSGreg Roach if ($tmp->isOK()) { 597e364afe4SGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65); 598f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 599a25f0a04SGreg Roach } 600a25f0a04SGreg Roach } 601a25f0a04SGreg Roach } 602a25f0a04SGreg Roach if ($min && $max) { 60359f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 604a25f0a04SGreg Roach 60565e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 6064686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 607a25f0a04SGreg Roach } else { 6084686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 609a25f0a04SGreg Roach } 610a25f0a04SGreg Roach } 611a25f0a04SGreg Roach } 612a25f0a04SGreg Roach 6134686330aSGreg Roach return $this->estimated_birth_date; 614a25f0a04SGreg Roach } 615a25f0a04SGreg Roach 616a25f0a04SGreg Roach /** 617a25f0a04SGreg Roach * Generate an estimated date of death. 618a25f0a04SGreg Roach * 619a25f0a04SGreg Roach * @return Date 620a25f0a04SGreg Roach */ 6218f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 622c1010edaSGreg Roach { 6234686330aSGreg Roach if ($this->estimated_death_date === null) { 624a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 625a25f0a04SGreg Roach if ($date->isOK()) { 6264686330aSGreg Roach $this->estimated_death_date = $date; 627a25f0a04SGreg Roach break; 628a25f0a04SGreg Roach } 629a25f0a04SGreg Roach } 6304686330aSGreg Roach if ($this->estimated_death_date === null) { 631b6ec1ccfSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay() !== 0) { 632c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 6334686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 634a25f0a04SGreg Roach } else { 6354686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 636a25f0a04SGreg Roach } 637a25f0a04SGreg Roach } 638a25f0a04SGreg Roach } 639a25f0a04SGreg Roach 6404686330aSGreg Roach return $this->estimated_death_date; 641a25f0a04SGreg Roach } 642a25f0a04SGreg Roach 643a25f0a04SGreg Roach /** 644a25f0a04SGreg Roach * Get the sex - M F or U 645a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 646a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 647a25f0a04SGreg Roach * 648a25f0a04SGreg Roach * @return string 649a25f0a04SGreg Roach */ 650e364afe4SGreg Roach public function sex(): string 651c1010edaSGreg Roach { 6521baf69deSGreg Roach if (preg_match('/\n1 SEX ([MFX])/', $this->gedcom . $this->pending, $match)) { 653a25f0a04SGreg Roach return $match[1]; 654a25f0a04SGreg Roach } 655b2ce94c6SRico Sonntag 656b2ce94c6SRico Sonntag return 'U'; 657a25f0a04SGreg Roach } 658a25f0a04SGreg Roach 659a25f0a04SGreg Roach /** 660a25f0a04SGreg Roach * Get a list of this individual’s spouse families 661a25f0a04SGreg Roach * 662cbc1590aSGreg Roach * @param int|null $access_level 663a25f0a04SGreg Roach * 66436779af1SGreg Roach * @return Collection<int,Family> 665a25f0a04SGreg Roach */ 6662c6f1bd5SGreg Roach public function spouseFamilies(int|null $access_level = null): Collection 667c1010edaSGreg Roach { 6683529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 669d9e083e7SGreg Roach 670d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 671d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 6724b9ff166SGreg Roach } 6734b9ff166SGreg Roach 67439ca88baSGreg Roach $families = new Collection(); 675d9e083e7SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level) as $fact) { 676dc124885SGreg Roach $family = $fact->target(); 677d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 67839ca88baSGreg Roach $families->push($family); 679a25f0a04SGreg Roach } 680a25f0a04SGreg Roach } 681a25f0a04SGreg Roach 68239ca88baSGreg Roach return new Collection($families); 683a25f0a04SGreg Roach } 684a25f0a04SGreg Roach 685a25f0a04SGreg Roach /** 686a25f0a04SGreg Roach * Get the current spouse of this individual. 687a25f0a04SGreg Roach * 688a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 689a25f0a04SGreg Roach * in chronological order, and take the last one found. 690a25f0a04SGreg Roach * 691a25f0a04SGreg Roach * @return Individual|null 692a25f0a04SGreg Roach */ 6931ff45046SGreg Roach public function getCurrentSpouse(): Individual|null 694c1010edaSGreg Roach { 69539ca88baSGreg Roach $family = $this->spouseFamilies()->last(); 69639ca88baSGreg Roach 69739ca88baSGreg Roach if ($family instanceof Family) { 69839ca88baSGreg Roach return $family->spouse($this); 699a25f0a04SGreg Roach } 700b2ce94c6SRico Sonntag 701b2ce94c6SRico Sonntag return null; 702a25f0a04SGreg Roach } 703a25f0a04SGreg Roach 704a25f0a04SGreg Roach /** 705a25f0a04SGreg Roach * Count the children belonging to this individual. 706a25f0a04SGreg Roach * 707cbc1590aSGreg Roach * @return int 708a25f0a04SGreg Roach */ 709e364afe4SGreg Roach public function numberOfChildren(): int 710c1010edaSGreg Roach { 7117d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 7123dc7bbe9SGreg Roach return (int) $match[1]; 713b2ce94c6SRico Sonntag } 714b2ce94c6SRico Sonntag 71513abd6f3SGreg Roach $children = []; 71639ca88baSGreg Roach foreach ($this->spouseFamilies() as $fam) { 71739ca88baSGreg Roach foreach ($fam->children() as $child) { 718c0935879SGreg Roach $children[$child->xref()] = true; 719a25f0a04SGreg Roach } 720a25f0a04SGreg Roach } 721a25f0a04SGreg Roach 722a25f0a04SGreg Roach return count($children); 723a25f0a04SGreg Roach } 724a25f0a04SGreg Roach 725a25f0a04SGreg Roach /** 726a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 727a25f0a04SGreg Roach * 728cbc1590aSGreg Roach * @param int|null $access_level 729a25f0a04SGreg Roach * 73036779af1SGreg Roach * @return Collection<int,Family> 731a25f0a04SGreg Roach */ 7322c6f1bd5SGreg Roach public function childFamilies(int|null $access_level = null): Collection 733c1010edaSGreg Roach { 7343529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 7354b9ff166SGreg Roach 736d9e083e7SGreg Roach if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 737d9e083e7SGreg Roach $access_level = Auth::PRIV_HIDE; 738d9e083e7SGreg Roach } 739a25f0a04SGreg Roach 74039ca88baSGreg Roach $families = new Collection(); 74139ca88baSGreg Roach 742d9e083e7SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level) as $fact) { 743dc124885SGreg Roach $family = $fact->target(); 744d9e083e7SGreg Roach if ($family instanceof Family && $family->canShow($access_level)) { 74539ca88baSGreg Roach $families->push($family); 746a25f0a04SGreg Roach } 747a25f0a04SGreg Roach } 748a25f0a04SGreg Roach 749a25f0a04SGreg Roach return $families; 750a25f0a04SGreg Roach } 751a25f0a04SGreg Roach 752a25f0a04SGreg Roach /** 753a25f0a04SGreg Roach * Get a list of step-parent families. 754a25f0a04SGreg Roach * 75536779af1SGreg Roach * @return Collection<int,Family> 756a25f0a04SGreg Roach */ 757820b62dfSGreg Roach public function childStepFamilies(): Collection 758c1010edaSGreg Roach { 759ed5b6227SGreg Roach $step_families = new Collection(); 76039ca88baSGreg Roach $families = $this->childFamilies(); 761a25f0a04SGreg Roach foreach ($families as $family) { 762ed5b6227SGreg Roach foreach ($family->spouses() as $parent) { 763ed5b6227SGreg Roach foreach ($parent->spouseFamilies() as $step_family) { 76439ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 765ed5b6227SGreg Roach $step_families->add($step_family); 766a25f0a04SGreg Roach } 767a25f0a04SGreg Roach } 768a25f0a04SGreg Roach } 769a25f0a04SGreg Roach } 770a25f0a04SGreg Roach 771f25fc0f9SGreg Roach return $step_families->uniqueStrict(static fn (Family $family): string => $family->xref()); 772a25f0a04SGreg Roach } 773a25f0a04SGreg Roach 774a25f0a04SGreg Roach /** 775a25f0a04SGreg Roach * Get a list of step-parent families. 776a25f0a04SGreg Roach * 77736779af1SGreg Roach * @return Collection<int,Family> 778a25f0a04SGreg Roach */ 779820b62dfSGreg Roach public function spouseStepFamilies(): Collection 780c1010edaSGreg Roach { 78113abd6f3SGreg Roach $step_families = []; 78239ca88baSGreg Roach $families = $this->spouseFamilies(); 783820b62dfSGreg Roach 784a25f0a04SGreg Roach foreach ($families as $family) { 78539ca88baSGreg Roach $spouse = $family->spouse($this); 786820b62dfSGreg Roach 787d823340dSGreg Roach if ($spouse instanceof self) { 78839ca88baSGreg Roach foreach ($family->spouse($this)->spouseFamilies() as $step_family) { 78939ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 790a25f0a04SGreg Roach $step_families[] = $step_family; 791a25f0a04SGreg Roach } 792a25f0a04SGreg Roach } 793a25f0a04SGreg Roach } 794a25f0a04SGreg Roach } 795a25f0a04SGreg Roach 796820b62dfSGreg Roach return new Collection($step_families); 797a25f0a04SGreg Roach } 798a25f0a04SGreg Roach 799a25f0a04SGreg Roach /** 800a25f0a04SGreg Roach * A label for a parental family group 801a25f0a04SGreg Roach * 802a25f0a04SGreg Roach * @param Family $family 803a25f0a04SGreg Roach * 804a25f0a04SGreg Roach * @return string 805a25f0a04SGreg Roach */ 806820b62dfSGreg Roach public function getChildFamilyLabel(Family $family): string 807c1010edaSGreg Roach { 8080e7e67a6SGreg Roach $fact = $this->facts(['FAMC'])->first(static fn (Fact $fact): bool => $fact->target() === $family); 8090e7e67a6SGreg Roach 8100e7e67a6SGreg Roach if ($fact instanceof Fact) { 8110e7e67a6SGreg Roach $pedigree = $fact->attribute('PEDI'); 8120e7e67a6SGreg Roach } else { 8130e7e67a6SGreg Roach $pedigree = ''; 8140e7e67a6SGreg Roach } 815b2ce94c6SRico Sonntag 8167d70e4a7SGreg Roach $values = [ 81788a03560SGreg Roach PedigreeLinkageType::VALUE_BIRTH => I18N::translate('Family with parents'), 81888a03560SGreg Roach PedigreeLinkageType::VALUE_ADOPTED => I18N::translate('Family with adoptive parents'), 81988a03560SGreg Roach PedigreeLinkageType::VALUE_FOSTER => I18N::translate('Family with foster parents'), 820665e281aSGreg Roach /* I18N: “sealing” is a Mormon ceremony. */ 82188a03560SGreg Roach PedigreeLinkageType::VALUE_SEALING => I18N::translate('Family with sealing parents'), 822665e281aSGreg Roach /* I18N: “rada” is an Arabic word, pronounced “ra DAH”. It is child-to-parent pedigree, established by wet-nursing. */ 82388a03560SGreg Roach PedigreeLinkageType::VALUE_RADA => I18N::translate('Family with rada parents'), 8247d70e4a7SGreg Roach ]; 8257d70e4a7SGreg Roach 82688a03560SGreg Roach return $values[$pedigree] ?? $values[PedigreeLinkageType::VALUE_BIRTH]; 827a25f0a04SGreg Roach } 828a25f0a04SGreg Roach 829a25f0a04SGreg Roach /** 830a25f0a04SGreg Roach * Create a label for a step family 831a25f0a04SGreg Roach * 832a25f0a04SGreg Roach * @param Family $step_family 833a25f0a04SGreg Roach * 834a25f0a04SGreg Roach * @return string 835a25f0a04SGreg Roach */ 8368f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 837c1010edaSGreg Roach { 83839ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 839a25f0a04SGreg Roach if ($family !== $step_family) { 840a25f0a04SGreg Roach // Must be a step-family 84139ca88baSGreg Roach foreach ($family->spouses() as $parent) { 84239ca88baSGreg Roach foreach ($step_family->spouses() as $step_parent) { 843a25f0a04SGreg Roach if ($parent === $step_parent) { 844a25f0a04SGreg Roach // One common parent - must be a step family 845e364afe4SGreg Roach if ($parent->sex() === 'M') { 846a25f0a04SGreg Roach // Father’s family with someone else 847b6ec1ccfSGreg Roach if ($step_family->spouse($step_parent) instanceof Individual) { 848a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 84939ca88baSGreg Roach return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName()); 850b2ce94c6SRico Sonntag } 851b2ce94c6SRico Sonntag 852a25f0a04SGreg Roach /* I18N: A step-family. */ 853bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 854a25f0a04SGreg Roach } 855b2ce94c6SRico Sonntag 856a25f0a04SGreg Roach // Mother’s family with someone else 857b6ec1ccfSGreg Roach if ($step_family->spouse($step_parent) instanceof Individual) { 858a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 85939ca88baSGreg Roach return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName()); 860b2ce94c6SRico Sonntag } 861b2ce94c6SRico Sonntag 862a25f0a04SGreg Roach /* I18N: A step-family. */ 863bbb76c12SGreg Roach return I18N::translate('Mother’s family with an unknown individual'); 864a25f0a04SGreg Roach } 865a25f0a04SGreg Roach } 866a25f0a04SGreg Roach } 867a25f0a04SGreg Roach } 868a25f0a04SGreg Roach } 869a25f0a04SGreg Roach 870a25f0a04SGreg Roach // Perahps same parents - but a different family record? 871a25f0a04SGreg Roach return I18N::translate('Family with parents'); 872a25f0a04SGreg Roach } 873a25f0a04SGreg Roach 874225e381fSGreg Roach /** 875225e381fSGreg Roach * Get the description for the family. 876225e381fSGreg Roach * 877225e381fSGreg Roach * For example, "XXX's family with new wife". 878225e381fSGreg Roach * 879225e381fSGreg Roach * @param Family $family 880225e381fSGreg Roach * 881225e381fSGreg Roach * @return string 882225e381fSGreg Roach */ 883e364afe4SGreg Roach public function getSpouseFamilyLabel(Family $family): string 884c1010edaSGreg Roach { 88539ca88baSGreg Roach $spouse = $family->spouse($this); 886b6ec1ccfSGreg Roach 887b6ec1ccfSGreg Roach if ($spouse instanceof Individual) { 888225e381fSGreg Roach /* I18N: %s is the spouse name */ 88939ca88baSGreg Roach return I18N::translate('Family with %s', $spouse->fullName()); 890225e381fSGreg Roach } 891b2ce94c6SRico Sonntag 89239ca88baSGreg Roach return $family->fullName(); 893225e381fSGreg Roach } 894225e381fSGreg Roach 895a25f0a04SGreg Roach /** 896961ec755SGreg Roach * If this object has no name, what do we call it? 897961ec755SGreg Roach * 898961ec755SGreg Roach * @return string 899961ec755SGreg Roach */ 9008f53f488SRico Sonntag public function getFallBackName(): string 901c1010edaSGreg Roach { 902a25f0a04SGreg Roach return '@P.N. /@N.N./'; 903a25f0a04SGreg Roach } 904a25f0a04SGreg Roach 905a25f0a04SGreg Roach /** 906a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 907a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 908a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 909a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 910a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 911a25f0a04SGreg Roach * recorded in the NAME field. e.g. 912a25f0a04SGreg Roach * 913a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 914a25f0a04SGreg Roach * 2 GIVN Robert 915a25f0a04SGreg Roach * 2 SPFX de 916a25f0a04SGreg Roach * 2 SURN CLITHEROW 917a25f0a04SGreg Roach * 2 NICK The Bald 918a25f0a04SGreg Roach * 919a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 920a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 921a25f0a04SGreg Roach * 922a25f0a04SGreg Roach * Handle multiple surnames, either as; 923a25f0a04SGreg Roach * 924a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 925a25f0a04SGreg Roach * or 926a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 927a25f0a04SGreg Roach * 2 GIVN Carlos 928a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 929a25f0a04SGreg Roach * 930a25f0a04SGreg Roach * @param string $type 93151928f9aSGreg Roach * @param string $value 932a25f0a04SGreg Roach * @param string $gedcom 933e364afe4SGreg Roach * 934e364afe4SGreg Roach * @return void 935a25f0a04SGreg Roach */ 93651928f9aSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 937c1010edaSGreg Roach { 938a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 939a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 940a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 941a25f0a04SGreg Roach 94276f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 943ef475b14SGreg Roach $GIVN = preg_match('/\n' . $sublevel . ' GIVN (.+)/', $gedcom, $match) === 1 ? $match[1] : ''; 944ef475b14SGreg Roach $SURN = preg_match('/\n' . $sublevel . ' SURN (.+)/', $gedcom, $match) === 1 ? $match[1] : ''; 945a25f0a04SGreg Roach 946a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 94776f666f4SGreg Roach if ($SURN !== '') { 948a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 949a25f0a04SGreg Roach } else { 95013abd6f3SGreg Roach $SURNS = []; 951a25f0a04SGreg Roach } 95276f666f4SGreg Roach 953a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 954a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 955a25f0a04SGreg Roach 956a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 957a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 958a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 959a25f0a04SGreg Roach 960a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 96151928f9aSGreg Roach if (substr_count($value, '/') % 2 === 1) { 96251928f9aSGreg Roach $value .= '/'; 963a25f0a04SGreg Roach } 964a25f0a04SGreg Roach 965a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 96651928f9aSGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $value); 967a25f0a04SGreg Roach 968a25f0a04SGreg Roach // Extract the surname. 969a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 970a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 971a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 972a25f0a04SGreg Roach } else { 973a25f0a04SGreg Roach $surname = ''; 974a25f0a04SGreg Roach } 975a25f0a04SGreg Roach 976a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 977a25f0a04SGreg Roach if (!$SURNS) { 978a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 979a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 980a25f0a04SGreg Roach $SURNS = $matches[1]; 981a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 982a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 983a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 984a25f0a04SGreg Roach } 985a25f0a04SGreg Roach } else { 986a25f0a04SGreg Roach // It is valid not to have a surname at all 98713abd6f3SGreg Roach $SURNS = ['']; 988a25f0a04SGreg Roach } 989a25f0a04SGreg Roach } 990a25f0a04SGreg Roach 991a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 992a25f0a04SGreg Roach if (!$GIVN) { 993c1010edaSGreg Roach // remove surname 994c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?\/.*\/ ?/', ' ', $full); 995c1010edaSGreg Roach // remove nickname 996c72b7fa4SGreg Roach $GIVN = preg_replace('/ ?".+"/', ' ', $GIVN); 997c1010edaSGreg Roach // multiple spaces, caused by the above 998c72b7fa4SGreg Roach $GIVN = preg_replace('/ {2,}/', ' ', $GIVN); 999c1010edaSGreg Roach // leading/trailing spaces, caused by the above 1000c72b7fa4SGreg Roach $GIVN = preg_replace('/^ | $/', '', $GIVN); 1001a25f0a04SGreg Roach } 1002a25f0a04SGreg Roach 1003a25f0a04SGreg Roach // Add placeholder for unknown given name 1004a25f0a04SGreg Roach if (!$GIVN) { 1005d823340dSGreg Roach $GIVN = self::PRAENOMEN_NESCIO; 100673f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1007a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1008a25f0a04SGreg Roach } 1009a25f0a04SGreg Roach 1010a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1011a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1012a25f0a04SGreg Roach // $full is for display on-screen 1013a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1014a25f0a04SGreg Roach 1015a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1016d823340dSGreg Roach $full = str_replace(self::NOMEN_NESCIO, I18N::translateContext('Unknown surname', '…'), $full); 1017d823340dSGreg Roach $full = str_replace(self::PRAENOMEN_NESCIO, I18N::translateContext('Unknown given name', '…'), $full); 1018c6f196c3SGreg Roach // Format for display 1019d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1020acc34ea1SGreg Roach // Localise quotation marks around the nickname 1021f25fc0f9SGreg Roach $full = preg_replace_callback('/"([^&]*)"/', static fn (array $matches): string => '<q class="wt-nickname">' . $matches[1] . '</q>', $full); 1022a25f0a04SGreg Roach 1023c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1024ee51991cSGreg Roach $full = preg_replace('/([^ >\x{200C}]*)\*/u', '<span class="starredname">\\1</span>', $full); 1025a25f0a04SGreg Roach 1026a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1027a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1028a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1029a25f0a04SGreg Roach 1030ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1031a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1032e364afe4SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) { 1033a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1034e364afe4SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) { 1035a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1036a25f0a04SGreg Roach } 1037a25f0a04SGreg Roach 1038bdb3725aSGreg Roach $this->getAllNames[] = [ 1039a25f0a04SGreg Roach 'type' => $type, 1040a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1041c1010edaSGreg Roach 'full' => $full, 1042c1010edaSGreg Roach // This is used for display 1043c1010edaSGreg Roach 'fullNN' => $fullNN, 1044c1010edaSGreg Roach // This goes into the database 1045c1010edaSGreg Roach 'surname' => $surname, 1046c1010edaSGreg Roach // This goes into the database 1047c1010edaSGreg Roach 'givn' => $GIVN, 1048c1010edaSGreg Roach // This goes into the database 1049c1010edaSGreg Roach 'surn' => $SURN, 1050c1010edaSGreg Roach // This goes into the database 105113abd6f3SGreg Roach ]; 1052a25f0a04SGreg Roach } 1053a25f0a04SGreg Roach } 1054a25f0a04SGreg Roach 1055a25f0a04SGreg Roach /** 105676692c8bSGreg Roach * Extract names from the GEDCOM record. 1057c7ff4153SGreg Roach * 1058c7ff4153SGreg Roach * @return void 1059a25f0a04SGreg Roach */ 1060e364afe4SGreg Roach public function extractNames(): void 1061c1010edaSGreg Roach { 1062d9e083e7SGreg Roach $access_level = $this->canShowName() ? Auth::PRIV_HIDE : Auth::accessLevel($this->tree); 1063d9e083e7SGreg Roach 10648f53f488SRico Sonntag $this->extractNamesFromFacts( 10658f53f488SRico Sonntag 1, 10668f53f488SRico Sonntag 'NAME', 1067d9e083e7SGreg Roach $this->facts(['NAME'], false, $access_level) 10688f53f488SRico Sonntag ); 1069a25f0a04SGreg Roach } 1070a25f0a04SGreg Roach 1071a25f0a04SGreg Roach /** 1072a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1073a25f0a04SGreg Roach * selection items or favorites. 1074a25f0a04SGreg Roach * 1075a25f0a04SGreg Roach * @return string 1076a25f0a04SGreg Roach */ 10778f53f488SRico Sonntag public function formatListDetails(): string 1078c1010edaSGreg Roach { 1079a25f0a04SGreg Roach return 10808d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 10818d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1082a25f0a04SGreg Roach } 10838091bfd1SGreg Roach 10848091bfd1SGreg Roach /** 10858091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 10868091bfd1SGreg Roach */ 10878091bfd1SGreg Roach public function lock(): void 10888091bfd1SGreg Roach { 10898091bfd1SGreg Roach DB::table('individuals') 10908091bfd1SGreg Roach ->where('i_file', '=', $this->tree->id()) 10918091bfd1SGreg Roach ->where('i_id', '=', $this->xref()) 10928091bfd1SGreg Roach ->lockForUpdate() 10938091bfd1SGreg Roach ->get(); 10948091bfd1SGreg Roach } 1095a25f0a04SGreg Roach} 1096