1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17*fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 22886b77daSGreg Roachuse Closure; 236ccdf4f0SGreg Roachuse Exception; 24a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi; 262e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2739ca88baSGreg Roachuse Illuminate\Support\Collection; 28886b77daSGreg Roachuse stdClass; 29a25f0a04SGreg Roach 30a25f0a04SGreg Roach/** 3176692c8bSGreg Roach * A GEDCOM individual (INDI) object. 32a25f0a04SGreg Roach */ 33c1010edaSGreg Roachclass Individual extends GedcomRecord 34c1010edaSGreg Roach{ 3516d6367aSGreg Roach public const RECORD_TYPE = 'INDI'; 3616d6367aSGreg Roach 3716d6367aSGreg Roach protected const ROUTE_NAME = 'individual'; 38a25f0a04SGreg Roach 3976692c8bSGreg Roach /** @var int used in some lists to keep track of this individual’s generation in that list */ 4076692c8bSGreg Roach public $generation; 41a25f0a04SGreg Roach 42a25f0a04SGreg Roach /** @var Date The estimated date of birth */ 434686330aSGreg Roach private $estimated_birth_date; 44a25f0a04SGreg Roach 45a25f0a04SGreg Roach /** @var Date The estimated date of death */ 464686330aSGreg Roach private $estimated_death_date; 47a25f0a04SGreg Roach 48a25f0a04SGreg Roach /** 49886b77daSGreg Roach * A closure which will create a record from a database row. 50886b77daSGreg Roach * 51886b77daSGreg Roach * @return Closure 52886b77daSGreg Roach */ 53c0804649SGreg Roach public static function rowMapper(): Closure 54886b77daSGreg Roach { 556c2179e2SGreg Roach return static function (stdClass $row): Individual { 56a7a24840SGreg Roach $individual = Individual::getInstance($row->i_id, Tree::findById((int) $row->i_file), $row->i_gedcom); 57e84cf2deSGreg Roach 58e84cf2deSGreg Roach if ($row->n_num ?? null) { 59e0458bdcSGreg Roach $individual = clone $individual; 60e84cf2deSGreg Roach $individual->setPrimaryName($row->n_num); 61e0458bdcSGreg Roach 62e0458bdcSGreg Roach return $individual; 63e84cf2deSGreg Roach } 64e84cf2deSGreg Roach 65e84cf2deSGreg Roach return $individual; 66886b77daSGreg Roach }; 67886b77daSGreg Roach } 68886b77daSGreg Roach 69886b77daSGreg Roach /** 70c156e8f5SGreg Roach * A closure which will compare individuals by birth date. 71c156e8f5SGreg Roach * 72c156e8f5SGreg Roach * @return Closure 73c156e8f5SGreg Roach */ 74c156e8f5SGreg Roach public static function birthDateComparator(): Closure 75c156e8f5SGreg Roach { 766c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 77c156e8f5SGreg Roach return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 78c156e8f5SGreg Roach }; 79c156e8f5SGreg Roach } 80c156e8f5SGreg Roach 81c156e8f5SGreg Roach /** 82c156e8f5SGreg Roach * A closure which will compare individuals by death date. 83c156e8f5SGreg Roach * 84c156e8f5SGreg Roach * @return Closure 85c156e8f5SGreg Roach */ 86c156e8f5SGreg Roach public static function deathDateComparator(): Closure 87c156e8f5SGreg Roach { 886c2179e2SGreg Roach return static function (Individual $x, Individual $y): int { 89c156e8f5SGreg Roach return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 90c156e8f5SGreg Roach }; 91c156e8f5SGreg Roach } 92c156e8f5SGreg Roach 93c156e8f5SGreg Roach /** 94e71ef9d2SGreg Roach * Get an instance of an individual object. For single records, 95e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 96e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 97e71ef9d2SGreg Roach * 98e71ef9d2SGreg Roach * @param string $xref 99e71ef9d2SGreg Roach * @param Tree $tree 100e71ef9d2SGreg Roach * @param string|null $gedcom 101e71ef9d2SGreg Roach * 1026ccdf4f0SGreg Roach * @throws Exception 103e71ef9d2SGreg Roach * @return Individual|null 104e71ef9d2SGreg Roach */ 105e364afe4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self 106c1010edaSGreg Roach { 107e71ef9d2SGreg Roach $record = parent::getInstance($xref, $tree, $gedcom); 108e71ef9d2SGreg Roach 109e364afe4SGreg Roach if ($record instanceof self) { 110e71ef9d2SGreg Roach return $record; 111e71ef9d2SGreg Roach } 112b2ce94c6SRico Sonntag 113b2ce94c6SRico Sonntag return null; 114e71ef9d2SGreg Roach } 115e71ef9d2SGreg Roach 116e71ef9d2SGreg Roach /** 117395f0fe0SGreg Roach * Sometimes, we'll know in advance that we need to load a set of records. 118395f0fe0SGreg Roach * Typically when we load families and their members. 119395f0fe0SGreg Roach * 120395f0fe0SGreg Roach * @param Tree $tree 1214a8aaa00SScrutinizer Auto-Fixer * @param string[] $xrefs 12218d7a90dSGreg Roach * 12318d7a90dSGreg Roach * @return void 124395f0fe0SGreg Roach */ 1252e5b4452SGreg Roach public static function load(Tree $tree, array $xrefs): void 126c1010edaSGreg Roach { 1272e5b4452SGreg Roach $rows = DB::table('individuals') 1282e5b4452SGreg Roach ->where('i_file', '=', $tree->id()) 1292e5b4452SGreg Roach ->whereIn('i_id', array_unique($xrefs)) 1302e5b4452SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom']) 1312e5b4452SGreg Roach ->get(); 132395f0fe0SGreg Roach 133395f0fe0SGreg Roach foreach ($rows as $row) { 134395f0fe0SGreg Roach self::getInstance($row->xref, $tree, $row->gedcom); 135395f0fe0SGreg Roach } 136395f0fe0SGreg Roach } 137395f0fe0SGreg Roach 138395f0fe0SGreg Roach /** 139a25f0a04SGreg Roach * Can the name of this record be shown? 140a25f0a04SGreg Roach * 14176692c8bSGreg Roach * @param int|null $access_level 14276692c8bSGreg Roach * 14376692c8bSGreg Roach * @return bool 144a25f0a04SGreg Roach */ 14535584196SGreg Roach public function canShowName(int $access_level = null): bool 146c1010edaSGreg Roach { 1474b9ff166SGreg Roach if ($access_level === null) { 1484b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 1494b9ff166SGreg Roach } 1504b9ff166SGreg Roach 151518bbdc1SGreg Roach return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 152a25f0a04SGreg Roach } 153a25f0a04SGreg Roach 154a25f0a04SGreg Roach /** 15576692c8bSGreg Roach * Can this individual be shown? 156a25f0a04SGreg Roach * 15776692c8bSGreg Roach * @param int $access_level 15876692c8bSGreg Roach * 15976692c8bSGreg Roach * @return bool 160a25f0a04SGreg Roach */ 16135584196SGreg Roach protected function canShowByType(int $access_level): bool 162c1010edaSGreg Roach { 163a25f0a04SGreg Roach // Dead people... 164518bbdc1SGreg Roach if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 165a25f0a04SGreg Roach $keep_alive = false; 16654ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 167a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH) { 1688d0ebef0SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER); 169a25f0a04SGreg Roach foreach ($matches as $match) { 170a25f0a04SGreg Roach $date = new Date($match[1]); 171a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 172a25f0a04SGreg Roach $keep_alive = true; 173a25f0a04SGreg Roach break; 174a25f0a04SGreg Roach } 175a25f0a04SGreg Roach } 176a25f0a04SGreg Roach } 17754ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 178a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_DEATH) { 1798d0ebef0SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER); 180a25f0a04SGreg Roach foreach ($matches as $match) { 181a25f0a04SGreg Roach $date = new Date($match[1]); 182a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 183a25f0a04SGreg Roach $keep_alive = true; 184a25f0a04SGreg Roach break; 185a25f0a04SGreg Roach } 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach } 188a25f0a04SGreg Roach if (!$keep_alive) { 189a25f0a04SGreg Roach return true; 190a25f0a04SGreg Roach } 191a25f0a04SGreg Roach } 192a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 19354ba7dc5SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH'); 1944b9ff166SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), 'gedcomid'); 19554ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1964b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 197a25f0a04SGreg Roach } 198a25f0a04SGreg Roach 199a25f0a04SGreg Roach // No restriction found - show living people to members only: 2004b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 201a25f0a04SGreg Roach } 202a25f0a04SGreg Roach 203a25f0a04SGreg Roach /** 204a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 205a25f0a04SGreg Roach * 206a25f0a04SGreg Roach * @param Individual $target 207cbc1590aSGreg Roach * @param int $distance 208a25f0a04SGreg Roach * 209cbc1590aSGreg Roach * @return bool 210a25f0a04SGreg Roach */ 2118f53f488SRico Sonntag private static function isRelated(Individual $target, $distance): bool 212c1010edaSGreg Roach { 213a25f0a04SGreg Roach static $cache = null; 214a25f0a04SGreg Roach 21506ef8e02SGreg Roach $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree); 216a25f0a04SGreg Roach if ($user_individual) { 217a25f0a04SGreg Roach if (!$cache) { 21813abd6f3SGreg Roach $cache = [ 21913abd6f3SGreg Roach 0 => [$user_individual], 22013abd6f3SGreg Roach 1 => [], 22113abd6f3SGreg Roach ]; 2228d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 223dc124885SGreg Roach $family = $fact->target(); 224e24444eeSGreg Roach if ($family instanceof Family) { 225a25f0a04SGreg Roach $cache[1][] = $family; 226a25f0a04SGreg Roach } 227a25f0a04SGreg Roach } 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach } else { 230a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 231a25f0a04SGreg Roach return true; 232a25f0a04SGreg Roach } 233a25f0a04SGreg Roach 234a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 235a25f0a04SGreg Roach $distance *= 2; 236a25f0a04SGreg Roach 237a25f0a04SGreg Roach // Consider each path length in turn 238a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 239a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 240a25f0a04SGreg Roach // We have already calculated all records with this length 241e364afe4SGreg Roach if ($n % 2 === 0 && in_array($target, $cache[$n], true)) { 242a25f0a04SGreg Roach return true; 243a25f0a04SGreg Roach } 244a25f0a04SGreg Roach } else { 245a25f0a04SGreg Roach // Need to calculate these paths 24613abd6f3SGreg Roach $cache[$n] = []; 247e364afe4SGreg Roach if ($n % 2 === 0) { 248a25f0a04SGreg Roach // Add FAM->INDI links 249a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 2508d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 251dc124885SGreg Roach $individual = $fact->target(); 252a25f0a04SGreg Roach // Don’t backtrack 253e364afe4SGreg Roach if ($individual instanceof self && !in_array($individual, $cache[$n - 2], true)) { 254a25f0a04SGreg Roach $cache[$n][] = $individual; 255a25f0a04SGreg Roach } 256a25f0a04SGreg Roach } 257a25f0a04SGreg Roach } 258a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 259a25f0a04SGreg Roach return true; 260a25f0a04SGreg Roach } 261a25f0a04SGreg Roach } else { 262a25f0a04SGreg Roach // Add INDI->FAM links 263a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 2648d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 265dc124885SGreg Roach $family = $fact->target(); 266a25f0a04SGreg Roach // Don’t backtrack 267e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 268a25f0a04SGreg Roach $cache[$n][] = $family; 269a25f0a04SGreg Roach } 270a25f0a04SGreg Roach } 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach } 273a25f0a04SGreg Roach } 274a25f0a04SGreg Roach } 275a25f0a04SGreg Roach 276a25f0a04SGreg Roach return false; 277a25f0a04SGreg Roach } 278a25f0a04SGreg Roach 27976692c8bSGreg Roach /** 28076692c8bSGreg Roach * Generate a private version of this record 28176692c8bSGreg Roach * 28276692c8bSGreg Roach * @param int $access_level 28376692c8bSGreg Roach * 28476692c8bSGreg Roach * @return string 28576692c8bSGreg Roach */ 2863c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 287c1010edaSGreg Roach { 288acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 289a25f0a04SGreg Roach 290a25f0a04SGreg Roach $rec = '0 @' . $this->xref . '@ INDI'; 291518bbdc1SGreg Roach if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) { 292a25f0a04SGreg Roach // Show all the NAME tags, including subtags 2938d0ebef0SGreg Roach foreach ($this->facts(['NAME']) as $fact) { 294138ca96cSGreg Roach $rec .= "\n" . $fact->gedcom(); 295a25f0a04SGreg Roach } 296a25f0a04SGreg Roach } 297a25f0a04SGreg Roach // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data 2988d0ebef0SGreg Roach preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 299a25f0a04SGreg Roach foreach ($matches as $match) { 30024ec66ceSGreg Roach $rela = Family::getInstance($match[1], $this->tree); 301a25f0a04SGreg Roach if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 302a25f0a04SGreg Roach $rec .= $match[0]; 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach // Don’t privatize sex. 306a25f0a04SGreg Roach if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) { 307a25f0a04SGreg Roach $rec .= $match[0]; 308a25f0a04SGreg Roach } 309a25f0a04SGreg Roach 310a25f0a04SGreg Roach return $rec; 311a25f0a04SGreg Roach } 312a25f0a04SGreg Roach 31376692c8bSGreg Roach /** 31476692c8bSGreg Roach * Fetch data from the database 31576692c8bSGreg Roach * 31676692c8bSGreg Roach * @param string $xref 31776692c8bSGreg Roach * @param int $tree_id 31876692c8bSGreg Roach * 319e364afe4SGreg Roach * @return string|null 32076692c8bSGreg Roach */ 321e364afe4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 322c1010edaSGreg Roach { 3232e5b4452SGreg Roach return DB::table('individuals') 3242e5b4452SGreg Roach ->where('i_id', '=', $xref) 3252e5b4452SGreg Roach ->where('i_file', '=', $tree_id) 3262e5b4452SGreg Roach ->value('i_gedcom'); 327a25f0a04SGreg Roach } 328a25f0a04SGreg Roach 329a25f0a04SGreg Roach /** 330a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 331a25f0a04SGreg Roach * If not known to be dead, then assume living. 332a25f0a04SGreg Roach * 333cbc1590aSGreg Roach * @return bool 334a25f0a04SGreg Roach */ 3358f53f488SRico Sonntag public function isDead(): bool 336c1010edaSGreg Roach { 337c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 3384459dc9aSGreg Roach $today_jd = Carbon::now()->julianDay(); 339a25f0a04SGreg Roach 340a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 3418d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 342a25f0a04SGreg Roach return true; 343a25f0a04SGreg Roach } 344a25f0a04SGreg Roach 345a25f0a04SGreg Roach // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 346a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 347a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 348a25f0a04SGreg Roach $date = new Date($date_match); 349269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * $MAX_ALIVE_AGE) { 350a25f0a04SGreg Roach return true; 351a25f0a04SGreg Roach } 352a25f0a04SGreg Roach } 353a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 354a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 355a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 356a25f0a04SGreg Roach return false; 357a25f0a04SGreg Roach } 358a25f0a04SGreg Roach } 359a25f0a04SGreg Roach 360a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 361a25f0a04SGreg Roach 362a25f0a04SGreg Roach // Check parents (birth and adopted) 36339ca88baSGreg Roach foreach ($this->childFamilies(Auth::PRIV_HIDE) as $family) { 36439ca88baSGreg Roach foreach ($family->spouses(Auth::PRIV_HIDE) as $parent) { 365a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 366a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 367a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 368a25f0a04SGreg Roach $date = new Date($date_match); 369269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 45)) { 370a25f0a04SGreg Roach return true; 371a25f0a04SGreg Roach } 372a25f0a04SGreg Roach } 373a25f0a04SGreg Roach } 374a25f0a04SGreg Roach } 375a25f0a04SGreg Roach 376a25f0a04SGreg Roach // Check spouses 37739ca88baSGreg Roach foreach ($this->spouseFamilies(Auth::PRIV_HIDE) as $family) { 378a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 379a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 380a25f0a04SGreg Roach $date = new Date($date_match); 381a25f0a04SGreg Roach // Assume marriage occurs after age of 10 382269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 10)) { 383a25f0a04SGreg Roach return true; 384a25f0a04SGreg Roach } 385a25f0a04SGreg Roach } 386a25f0a04SGreg Roach // Check spouse dates 38739ca88baSGreg Roach $spouse = $family->spouse($this, Auth::PRIV_HIDE); 388a25f0a04SGreg Roach if ($spouse) { 389a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 390a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 391a25f0a04SGreg Roach $date = new Date($date_match); 392a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 393269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE + 40)) { 394a25f0a04SGreg Roach return true; 395a25f0a04SGreg Roach } 396a25f0a04SGreg Roach } 397a25f0a04SGreg Roach } 398a25f0a04SGreg Roach // Check child dates 39939ca88baSGreg Roach foreach ($family->children(Auth::PRIV_HIDE) as $child) { 400a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 401a25f0a04SGreg Roach // Assume children born after age of 15 402a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 403a25f0a04SGreg Roach $date = new Date($date_match); 404269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 15)) { 405a25f0a04SGreg Roach return true; 406a25f0a04SGreg Roach } 407a25f0a04SGreg Roach } 408a25f0a04SGreg Roach // Check grandchildren 40939ca88baSGreg Roach foreach ($child->spouseFamilies(Auth::PRIV_HIDE) as $child_family) { 41039ca88baSGreg Roach foreach ($child_family->children(Auth::PRIV_HIDE) as $grandchild) { 411a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 412a25f0a04SGreg Roach // Assume grandchildren born after age of 30 413a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 414a25f0a04SGreg Roach $date = new Date($date_match); 415269fd10dSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= $today_jd - 365 * ($MAX_ALIVE_AGE - 30)) { 416a25f0a04SGreg Roach return true; 417a25f0a04SGreg Roach } 418a25f0a04SGreg Roach } 419a25f0a04SGreg Roach } 420a25f0a04SGreg Roach } 421a25f0a04SGreg Roach } 422a25f0a04SGreg Roach } 423a25f0a04SGreg Roach 424a25f0a04SGreg Roach return false; 425a25f0a04SGreg Roach } 426a25f0a04SGreg Roach 427a25f0a04SGreg Roach /** 428a25f0a04SGreg Roach * Find the highlighted media object for an individual 429a25f0a04SGreg Roach * 430e364afe4SGreg Roach * @return MediaFile|null 431a25f0a04SGreg Roach */ 432e364afe4SGreg Roach public function findHighlightedMediaFile(): ?MediaFile 433c1010edaSGreg Roach { 4348d0ebef0SGreg Roach foreach ($this->facts(['OBJE']) as $fact) { 435dc124885SGreg Roach $media = $fact->target(); 436a213a63cSGreg Roach if ($media instanceof Media) { 4374a9f750fSGreg Roach foreach ($media->mediaFiles() as $media_file) { 438a213a63cSGreg Roach if ($media_file->isImage() && !$media_file->isExternal()) { 4394a9f750fSGreg Roach return $media_file; 4404a9f750fSGreg Roach } 4414a9f750fSGreg Roach } 442a25f0a04SGreg Roach } 443a25f0a04SGreg Roach } 444a25f0a04SGreg Roach 445a25f0a04SGreg Roach return null; 446a25f0a04SGreg Roach } 447a25f0a04SGreg Roach 448a25f0a04SGreg Roach /** 449a25f0a04SGreg Roach * Display the prefered image for this individual. 450a25f0a04SGreg Roach * Use an icon if no image is available. 451a25f0a04SGreg Roach * 452dce07401SGreg Roach * @param int $width Pixels 453dce07401SGreg Roach * @param int $height Pixels 454dce07401SGreg Roach * @param string $fit "crop" or "contain" 455dce07401SGreg Roach * @param string[] $attributes Additional HTML attributes 456dce07401SGreg Roach * 457a25f0a04SGreg Roach * @return string 458a25f0a04SGreg Roach */ 4598f53f488SRico Sonntag public function displayImage($width, $height, $fit, $attributes): string 460c1010edaSGreg Roach { 4614a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 4624a9f750fSGreg Roach 4634a9f750fSGreg Roach if ($media_file !== null) { 4644a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 465a25f0a04SGreg Roach } 4664a9f750fSGreg Roach 4674a9f750fSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE')) { 46839ca88baSGreg Roach return '<i class="icon-silhouette-' . $this->sex() . '"></i>'; 4694a9f750fSGreg Roach } 4704a9f750fSGreg Roach 4714a9f750fSGreg Roach return ''; 472a25f0a04SGreg Roach } 473a25f0a04SGreg Roach 474a25f0a04SGreg Roach /** 475a25f0a04SGreg Roach * Get the date of birth 476a25f0a04SGreg Roach * 477a25f0a04SGreg Roach * @return Date 478a25f0a04SGreg Roach */ 4798f53f488SRico Sonntag public function getBirthDate(): Date 480c1010edaSGreg Roach { 481a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 482a25f0a04SGreg Roach if ($date->isOK()) { 483a25f0a04SGreg Roach return $date; 484a25f0a04SGreg Roach } 485a25f0a04SGreg Roach } 486a25f0a04SGreg Roach 487a25f0a04SGreg Roach return new Date(''); 488a25f0a04SGreg Roach } 489a25f0a04SGreg Roach 490a25f0a04SGreg Roach /** 491a25f0a04SGreg Roach * Get the place of birth 492a25f0a04SGreg Roach * 49316d0b7f7SRico Sonntag * @return Place 494a25f0a04SGreg Roach */ 4958f53f488SRico Sonntag public function getBirthPlace(): Place 496c1010edaSGreg Roach { 497a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 498a25f0a04SGreg Roach return $place; 499a25f0a04SGreg Roach } 500a25f0a04SGreg Roach 501b20ddbf9SGreg Roach return new Place('', $this->tree); 502a25f0a04SGreg Roach } 503a25f0a04SGreg Roach 504a25f0a04SGreg Roach /** 505a25f0a04SGreg Roach * Get the year of birth 506a25f0a04SGreg Roach * 507a25f0a04SGreg Roach * @return string the year of birth 508a25f0a04SGreg Roach */ 5098f53f488SRico Sonntag public function getBirthYear(): string 510c1010edaSGreg Roach { 511f5b60decSGreg Roach return $this->getBirthDate()->minimumDate()->format('%Y'); 512a25f0a04SGreg Roach } 513a25f0a04SGreg Roach 514a25f0a04SGreg Roach /** 515a25f0a04SGreg Roach * Get the date of death 516a25f0a04SGreg Roach * 517a25f0a04SGreg Roach * @return Date 518a25f0a04SGreg Roach */ 5198f53f488SRico Sonntag public function getDeathDate(): Date 520c1010edaSGreg Roach { 521a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 522a25f0a04SGreg Roach if ($date->isOK()) { 523a25f0a04SGreg Roach return $date; 524a25f0a04SGreg Roach } 525a25f0a04SGreg Roach } 526a25f0a04SGreg Roach 527a25f0a04SGreg Roach return new Date(''); 528a25f0a04SGreg Roach } 529a25f0a04SGreg Roach 530a25f0a04SGreg Roach /** 531a25f0a04SGreg Roach * Get the place of death 532a25f0a04SGreg Roach * 53316d0b7f7SRico Sonntag * @return Place 534a25f0a04SGreg Roach */ 5358f53f488SRico Sonntag public function getDeathPlace(): Place 536c1010edaSGreg Roach { 537a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 538a25f0a04SGreg Roach return $place; 539a25f0a04SGreg Roach } 540a25f0a04SGreg Roach 541b20ddbf9SGreg Roach return new Place('', $this->tree); 542a25f0a04SGreg Roach } 543a25f0a04SGreg Roach 544a25f0a04SGreg Roach /** 545a25f0a04SGreg Roach * get the death year 546a25f0a04SGreg Roach * 547a25f0a04SGreg Roach * @return string the year of death 548a25f0a04SGreg Roach */ 5498f53f488SRico Sonntag public function getDeathYear(): string 550c1010edaSGreg Roach { 551f5b60decSGreg Roach return $this->getDeathDate()->minimumDate()->format('%Y'); 552a25f0a04SGreg Roach } 553a25f0a04SGreg Roach 554a25f0a04SGreg Roach /** 555a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 55615d603e7SGreg Roach * Provide the place and full date using a tooltip. 557a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 558a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 559a25f0a04SGreg Roach * 560a25f0a04SGreg Roach * @return string 561a25f0a04SGreg Roach */ 5628f53f488SRico Sonntag public function getLifeSpan(): string 563c1010edaSGreg Roach { 56415d603e7SGreg Roach // Just the first part of the place name 565392561bbSGreg Roach $birth_place = strip_tags($this->getBirthPlace()->shortName()); 566392561bbSGreg Roach $death_place = strip_tags($this->getDeathPlace()->shortName()); 56715d603e7SGreg Roach // Remove markup from dates 56815d603e7SGreg Roach $birth_date = strip_tags($this->getBirthDate()->display()); 56915d603e7SGreg Roach $death_date = strip_tags($this->getDeathDate()->display()); 57015d603e7SGreg Roach 571c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 572bbb76c12SGreg Roach return 573c1010edaSGreg Roach I18N::translate( 574a25f0a04SGreg Roach '%1$s–%2$s', 5752d4c1bedSGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>', 5762d4c1bedSGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>' 577a25f0a04SGreg Roach ); 578a25f0a04SGreg Roach } 579a25f0a04SGreg Roach 580a25f0a04SGreg Roach /** 581a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 582a25f0a04SGreg Roach * 583a25f0a04SGreg Roach * @return Date[] 584a25f0a04SGreg Roach */ 5858f53f488SRico Sonntag public function getAllBirthDates(): array 586c1010edaSGreg Roach { 5878d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 5888d0ebef0SGreg Roach $tmp = $this->getAllEventDates([$event]); 589a25f0a04SGreg Roach if ($tmp) { 590a25f0a04SGreg Roach return $tmp; 591a25f0a04SGreg Roach } 592a25f0a04SGreg Roach } 593a25f0a04SGreg Roach 59413abd6f3SGreg Roach return []; 595a25f0a04SGreg Roach } 596a25f0a04SGreg Roach 597a25f0a04SGreg Roach /** 598a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 599a25f0a04SGreg Roach * 6004080d558SGreg Roach * @return Place[] 601a25f0a04SGreg Roach */ 6028f53f488SRico Sonntag public function getAllBirthPlaces(): array 603c1010edaSGreg Roach { 6048d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 6058d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 6064080d558SGreg Roach if (!empty($places)) { 6074080d558SGreg Roach return $places; 608a25f0a04SGreg Roach } 609a25f0a04SGreg Roach } 610a25f0a04SGreg Roach 61113abd6f3SGreg Roach return []; 612a25f0a04SGreg Roach } 613a25f0a04SGreg Roach 614a25f0a04SGreg Roach /** 615a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 616a25f0a04SGreg Roach * 617a25f0a04SGreg Roach * @return Date[] 618a25f0a04SGreg Roach */ 6198f53f488SRico Sonntag public function getAllDeathDates(): array 620c1010edaSGreg Roach { 6218d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 6228d0ebef0SGreg Roach $tmp = $this->getAllEventDates([$event]); 623a25f0a04SGreg Roach if ($tmp) { 624a25f0a04SGreg Roach return $tmp; 625a25f0a04SGreg Roach } 626a25f0a04SGreg Roach } 627a25f0a04SGreg Roach 62813abd6f3SGreg Roach return []; 629a25f0a04SGreg Roach } 630a25f0a04SGreg Roach 631a25f0a04SGreg Roach /** 632a25f0a04SGreg Roach * Get all the death places - for the individual lists. 633a25f0a04SGreg Roach * 6344080d558SGreg Roach * @return Place[] 635a25f0a04SGreg Roach */ 6368f53f488SRico Sonntag public function getAllDeathPlaces(): array 637c1010edaSGreg Roach { 6388d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 6398d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 6404080d558SGreg Roach if (!empty($places)) { 6414080d558SGreg Roach return $places; 642a25f0a04SGreg Roach } 643a25f0a04SGreg Roach } 644a25f0a04SGreg Roach 64513abd6f3SGreg Roach return []; 646a25f0a04SGreg Roach } 647a25f0a04SGreg Roach 648a25f0a04SGreg Roach /** 649a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 650a25f0a04SGreg Roach * 651a25f0a04SGreg Roach * @return Date 652a25f0a04SGreg Roach */ 6538f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 654c1010edaSGreg Roach { 6558f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 656a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 657a25f0a04SGreg Roach if ($date->isOK()) { 6584686330aSGreg Roach $this->estimated_birth_date = $date; 659a25f0a04SGreg Roach break; 660a25f0a04SGreg Roach } 661a25f0a04SGreg Roach } 6628f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 66313abd6f3SGreg Roach $min = []; 66413abd6f3SGreg Roach $max = []; 665a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 666f5b60decSGreg Roach if ($tmp->isOK()) { 667f5b60decSGreg Roach $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365; 668f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 669a25f0a04SGreg Roach } 67039ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 671a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 672f5b60decSGreg Roach if ($tmp->isOK()) { 673f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 1; 674f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 675a25f0a04SGreg Roach } 67639ca88baSGreg Roach $husband = $family->husband(); 677e364afe4SGreg Roach if ($husband instanceof self) { 6782e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 679f5b60decSGreg Roach if ($tmp->isOK()) { 680f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 681f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 682a25f0a04SGreg Roach } 683a25f0a04SGreg Roach } 68439ca88baSGreg Roach $wife = $family->wife(); 685e364afe4SGreg Roach if ($wife instanceof self) { 6862e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 687f5b60decSGreg Roach if ($tmp->isOK()) { 688f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 689f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 690a25f0a04SGreg Roach } 691a25f0a04SGreg Roach } 69239ca88baSGreg Roach foreach ($family->children() as $child) { 693a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 694f5b60decSGreg Roach if ($tmp->isOK()) { 695f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 696f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 697a25f0a04SGreg Roach } 698a25f0a04SGreg Roach } 699a25f0a04SGreg Roach } 70039ca88baSGreg Roach foreach ($this->spouseFamilies() as $family) { 701a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 702f5b60decSGreg Roach if ($tmp->isOK()) { 703f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 704f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 705a25f0a04SGreg Roach } 70639ca88baSGreg Roach $spouse = $family->spouse($this); 707a25f0a04SGreg Roach if ($spouse) { 708a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 709f5b60decSGreg Roach if ($tmp->isOK()) { 710f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 711f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 712a25f0a04SGreg Roach } 713a25f0a04SGreg Roach } 71439ca88baSGreg Roach foreach ($family->children() as $child) { 715a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 716f5b60decSGreg Roach if ($tmp->isOK()) { 717e364afe4SGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->sex() === 'F' ? 45 : 65); 718f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 719a25f0a04SGreg Roach } 720a25f0a04SGreg Roach } 721a25f0a04SGreg Roach } 722a25f0a04SGreg Roach if ($min && $max) { 72359f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 724a25f0a04SGreg Roach 72565e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 7264686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 727a25f0a04SGreg Roach } else { 7284686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 729a25f0a04SGreg Roach } 730a25f0a04SGreg Roach } 731a25f0a04SGreg Roach } 732a25f0a04SGreg Roach 7334686330aSGreg Roach return $this->estimated_birth_date; 734a25f0a04SGreg Roach } 735a25f0a04SGreg Roach 736a25f0a04SGreg Roach /** 737a25f0a04SGreg Roach * Generate an estimated date of death. 738a25f0a04SGreg Roach * 739a25f0a04SGreg Roach * @return Date 740a25f0a04SGreg Roach */ 7418f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 742c1010edaSGreg Roach { 7434686330aSGreg Roach if ($this->estimated_death_date === null) { 744a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 745a25f0a04SGreg Roach if ($date->isOK()) { 7464686330aSGreg Roach $this->estimated_death_date = $date; 747a25f0a04SGreg Roach break; 748a25f0a04SGreg Roach } 749a25f0a04SGreg Roach } 7504686330aSGreg Roach if ($this->estimated_death_date === null) { 751f5b60decSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay()) { 752c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 7534686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 754a25f0a04SGreg Roach } else { 7554686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 756a25f0a04SGreg Roach } 757a25f0a04SGreg Roach } 758a25f0a04SGreg Roach } 759a25f0a04SGreg Roach 7604686330aSGreg Roach return $this->estimated_death_date; 761a25f0a04SGreg Roach } 762a25f0a04SGreg Roach 763a25f0a04SGreg Roach /** 764a25f0a04SGreg Roach * Get the sex - M F or U 765a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 766a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 767a25f0a04SGreg Roach * 768a25f0a04SGreg Roach * @return string 769a25f0a04SGreg Roach */ 770e364afe4SGreg Roach public function sex(): string 771c1010edaSGreg Roach { 772a25f0a04SGreg Roach if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) { 773a25f0a04SGreg Roach return $match[1]; 774a25f0a04SGreg Roach } 775b2ce94c6SRico Sonntag 776b2ce94c6SRico Sonntag return 'U'; 777a25f0a04SGreg Roach } 778a25f0a04SGreg Roach 779a25f0a04SGreg Roach /** 780a25f0a04SGreg Roach * Generate the CSS class to be used for drawing this individual 781a25f0a04SGreg Roach * 782a25f0a04SGreg Roach * @return string 783a25f0a04SGreg Roach */ 7848f53f488SRico Sonntag public function getBoxStyle(): string 785c1010edaSGreg Roach { 786c1010edaSGreg Roach $tmp = [ 787c1010edaSGreg Roach 'M' => '', 788c1010edaSGreg Roach 'F' => 'F', 789c1010edaSGreg Roach 'U' => 'NN', 790c1010edaSGreg Roach ]; 791a25f0a04SGreg Roach 79239ca88baSGreg Roach return 'person_box' . $tmp[$this->sex()]; 793a25f0a04SGreg Roach } 794a25f0a04SGreg Roach 795a25f0a04SGreg Roach /** 796a25f0a04SGreg Roach * Get a list of this individual’s spouse families 797a25f0a04SGreg Roach * 798cbc1590aSGreg Roach * @param int|null $access_level 799a25f0a04SGreg Roach * 80054c7f8dfSGreg Roach * @return Collection 801a25f0a04SGreg Roach */ 80239ca88baSGreg Roach public function spouseFamilies($access_level = null): Collection 803c1010edaSGreg Roach { 8044b9ff166SGreg Roach if ($access_level === null) { 8054b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 8064b9ff166SGreg Roach } 8074b9ff166SGreg Roach 808acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 809a25f0a04SGreg Roach 81039ca88baSGreg Roach $families = new Collection(); 8118d0ebef0SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) { 812dc124885SGreg Roach $family = $fact->target(); 813e24444eeSGreg Roach if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) { 81439ca88baSGreg Roach $families->push($family); 815a25f0a04SGreg Roach } 816a25f0a04SGreg Roach } 817a25f0a04SGreg Roach 81839ca88baSGreg Roach return new Collection($families); 819a25f0a04SGreg Roach } 820a25f0a04SGreg Roach 821a25f0a04SGreg Roach /** 822a25f0a04SGreg Roach * Get the current spouse of this individual. 823a25f0a04SGreg Roach * 824a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 825a25f0a04SGreg Roach * in chronological order, and take the last one found. 826a25f0a04SGreg Roach * 827a25f0a04SGreg Roach * @return Individual|null 828a25f0a04SGreg Roach */ 829e364afe4SGreg Roach public function getCurrentSpouse(): ?Individual 830c1010edaSGreg Roach { 83139ca88baSGreg Roach $family = $this->spouseFamilies()->last(); 83239ca88baSGreg Roach 83339ca88baSGreg Roach if ($family instanceof Family) { 83439ca88baSGreg Roach return $family->spouse($this); 835a25f0a04SGreg Roach } 836b2ce94c6SRico Sonntag 837b2ce94c6SRico Sonntag return null; 838a25f0a04SGreg Roach } 839a25f0a04SGreg Roach 840a25f0a04SGreg Roach /** 841a25f0a04SGreg Roach * Count the children belonging to this individual. 842a25f0a04SGreg Roach * 843cbc1590aSGreg Roach * @return int 844a25f0a04SGreg Roach */ 845e364afe4SGreg Roach public function numberOfChildren(): int 846c1010edaSGreg Roach { 8477d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 8483dc7bbe9SGreg Roach return (int) $match[1]; 849b2ce94c6SRico Sonntag } 850b2ce94c6SRico Sonntag 85113abd6f3SGreg Roach $children = []; 85239ca88baSGreg Roach foreach ($this->spouseFamilies() as $fam) { 85339ca88baSGreg Roach foreach ($fam->children() as $child) { 854c0935879SGreg Roach $children[$child->xref()] = true; 855a25f0a04SGreg Roach } 856a25f0a04SGreg Roach } 857a25f0a04SGreg Roach 858a25f0a04SGreg Roach return count($children); 859a25f0a04SGreg Roach } 860a25f0a04SGreg Roach 861a25f0a04SGreg Roach /** 862a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 863a25f0a04SGreg Roach * 864cbc1590aSGreg Roach * @param int|null $access_level 865a25f0a04SGreg Roach * 86654c7f8dfSGreg Roach * @return Collection 867a25f0a04SGreg Roach */ 86839ca88baSGreg Roach public function childFamilies($access_level = null): Collection 869c1010edaSGreg Roach { 8704b9ff166SGreg Roach if ($access_level === null) { 8714b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 8724b9ff166SGreg Roach } 8734b9ff166SGreg Roach 874acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 875a25f0a04SGreg Roach 87639ca88baSGreg Roach $families = new Collection(); 87739ca88baSGreg Roach 8788d0ebef0SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) { 879dc124885SGreg Roach $family = $fact->target(); 880e24444eeSGreg Roach if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) { 88139ca88baSGreg Roach $families->push($family); 882a25f0a04SGreg Roach } 883a25f0a04SGreg Roach } 884a25f0a04SGreg Roach 885a25f0a04SGreg Roach return $families; 886a25f0a04SGreg Roach } 887a25f0a04SGreg Roach 888a25f0a04SGreg Roach /** 889a25f0a04SGreg Roach * Get the preferred parents for this individual. 890a25f0a04SGreg Roach * 891a25f0a04SGreg Roach * An individual may multiple parents (e.g. birth, adopted, disputed). 892a25f0a04SGreg Roach * The preferred family record is: 893a25f0a04SGreg Roach * (a) the first one with an explicit tag "_PRIMARY Y" 894a25f0a04SGreg Roach * (b) the first one with a pedigree of "birth" 895a25f0a04SGreg Roach * (c) the first one with no pedigree (default is "birth") 896a25f0a04SGreg Roach * (d) the first one found 897a25f0a04SGreg Roach * 898a25f0a04SGreg Roach * @return Family|null 899a25f0a04SGreg Roach */ 900e364afe4SGreg Roach public function primaryChildFamily(): ?Family 901c1010edaSGreg Roach { 90239ca88baSGreg Roach $families = $this->childFamilies(); 9033b092cb5SGreg Roach switch ($families->count()) { 904a25f0a04SGreg Roach case 0: 905a25f0a04SGreg Roach return null; 906a25f0a04SGreg Roach case 1: 90715d603e7SGreg Roach return $families[0]; 908a25f0a04SGreg Roach default: 909a25f0a04SGreg Roach // If there is more than one FAMC record, choose the preferred parents: 910a25f0a04SGreg Roach // a) records with '2 _PRIMARY' 91174e78bfaSJonathan Jaubart foreach ($families as $fam) { 912c0935879SGreg Roach $famid = $fam->xref(); 9137d0db648SGreg Roach if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) { 914a25f0a04SGreg Roach return $fam; 915a25f0a04SGreg Roach } 916a25f0a04SGreg Roach } 917a25f0a04SGreg Roach // b) records with '2 PEDI birt' 91874e78bfaSJonathan Jaubart foreach ($families as $fam) { 919c0935879SGreg Roach $famid = $fam->xref(); 9207d0db648SGreg Roach if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) { 921a25f0a04SGreg Roach return $fam; 922a25f0a04SGreg Roach } 923a25f0a04SGreg Roach } 924a25f0a04SGreg Roach // c) records with no '2 PEDI' 92574e78bfaSJonathan Jaubart foreach ($families as $fam) { 926c0935879SGreg Roach $famid = $fam->xref(); 9277d0db648SGreg Roach if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) { 928a25f0a04SGreg Roach return $fam; 929a25f0a04SGreg Roach } 930a25f0a04SGreg Roach } 931a25f0a04SGreg Roach 932a25f0a04SGreg Roach // d) any record 93315d603e7SGreg Roach return $families[0]; 934a25f0a04SGreg Roach } 935a25f0a04SGreg Roach } 936a25f0a04SGreg Roach 937a25f0a04SGreg Roach /** 938a25f0a04SGreg Roach * Get a list of step-parent families. 939a25f0a04SGreg Roach * 94054c7f8dfSGreg Roach * @return Collection 941a25f0a04SGreg Roach */ 942820b62dfSGreg Roach public function childStepFamilies(): Collection 943c1010edaSGreg Roach { 94413abd6f3SGreg Roach $step_families = []; 94539ca88baSGreg Roach $families = $this->childFamilies(); 946a25f0a04SGreg Roach foreach ($families as $family) { 94739ca88baSGreg Roach $father = $family->husband(); 948a25f0a04SGreg Roach if ($father) { 94939ca88baSGreg Roach foreach ($father->spouseFamilies() as $step_family) { 95039ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 951a25f0a04SGreg Roach $step_families[] = $step_family; 952a25f0a04SGreg Roach } 953a25f0a04SGreg Roach } 954a25f0a04SGreg Roach } 95539ca88baSGreg Roach $mother = $family->wife(); 956a25f0a04SGreg Roach if ($mother) { 95739ca88baSGreg Roach foreach ($mother->spouseFamilies() as $step_family) { 95839ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 959a25f0a04SGreg Roach $step_families[] = $step_family; 960a25f0a04SGreg Roach } 961a25f0a04SGreg Roach } 962a25f0a04SGreg Roach } 963a25f0a04SGreg Roach } 964a25f0a04SGreg Roach 965820b62dfSGreg Roach return new Collection($step_families); 966a25f0a04SGreg Roach } 967a25f0a04SGreg Roach 968a25f0a04SGreg Roach /** 969a25f0a04SGreg Roach * Get a list of step-parent families. 970a25f0a04SGreg Roach * 97154c7f8dfSGreg Roach * @return Collection 972a25f0a04SGreg Roach */ 973820b62dfSGreg Roach public function spouseStepFamilies(): Collection 974c1010edaSGreg Roach { 97513abd6f3SGreg Roach $step_families = []; 97639ca88baSGreg Roach $families = $this->spouseFamilies(); 977820b62dfSGreg Roach 978a25f0a04SGreg Roach foreach ($families as $family) { 97939ca88baSGreg Roach $spouse = $family->spouse($this); 980820b62dfSGreg Roach 981a25f0a04SGreg Roach if ($spouse) { 98239ca88baSGreg Roach foreach ($family->spouse($this)->spouseFamilies() as $step_family) { 98339ca88baSGreg Roach if (!$families->containsStrict($step_family)) { 984a25f0a04SGreg Roach $step_families[] = $step_family; 985a25f0a04SGreg Roach } 986a25f0a04SGreg Roach } 987a25f0a04SGreg Roach } 988a25f0a04SGreg Roach } 989a25f0a04SGreg Roach 990820b62dfSGreg Roach return new Collection($step_families); 991a25f0a04SGreg Roach } 992a25f0a04SGreg Roach 993a25f0a04SGreg Roach /** 994a25f0a04SGreg Roach * A label for a parental family group 995a25f0a04SGreg Roach * 996a25f0a04SGreg Roach * @param Family $family 997a25f0a04SGreg Roach * 998a25f0a04SGreg Roach * @return string 999a25f0a04SGreg Roach */ 1000820b62dfSGreg Roach public function getChildFamilyLabel(Family $family): string 1001c1010edaSGreg Roach { 10027d0db648SGreg Roach if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) { 1003a25f0a04SGreg Roach // A specified pedigree 1004764a01d9SGreg Roach return GedcomCodePedi::getChildFamilyLabel($match[1]); 1005b2ce94c6SRico Sonntag } 1006b2ce94c6SRico Sonntag 1007a25f0a04SGreg Roach // Default (birth) pedigree 1008764a01d9SGreg Roach return GedcomCodePedi::getChildFamilyLabel(''); 1009a25f0a04SGreg Roach } 1010a25f0a04SGreg Roach 1011a25f0a04SGreg Roach /** 1012a25f0a04SGreg Roach * Create a label for a step family 1013a25f0a04SGreg Roach * 1014a25f0a04SGreg Roach * @param Family $step_family 1015a25f0a04SGreg Roach * 1016a25f0a04SGreg Roach * @return string 1017a25f0a04SGreg Roach */ 10188f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 1019c1010edaSGreg Roach { 102039ca88baSGreg Roach foreach ($this->childFamilies() as $family) { 1021a25f0a04SGreg Roach if ($family !== $step_family) { 1022a25f0a04SGreg Roach // Must be a step-family 102339ca88baSGreg Roach foreach ($family->spouses() as $parent) { 102439ca88baSGreg Roach foreach ($step_family->spouses() as $step_parent) { 1025a25f0a04SGreg Roach if ($parent === $step_parent) { 1026a25f0a04SGreg Roach // One common parent - must be a step family 1027e364afe4SGreg Roach if ($parent->sex() === 'M') { 1028a25f0a04SGreg Roach // Father’s family with someone else 102939ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 1030a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 103139ca88baSGreg Roach return I18N::translate('Father’s family with %s', $step_family->spouse($step_parent)->fullName()); 1032b2ce94c6SRico Sonntag } 1033b2ce94c6SRico Sonntag 1034a25f0a04SGreg Roach /* I18N: A step-family. */ 1035bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 1036a25f0a04SGreg Roach } 1037b2ce94c6SRico Sonntag 1038a25f0a04SGreg Roach // Mother’s family with someone else 103939ca88baSGreg Roach if ($step_family->spouse($step_parent)) { 1040a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 104139ca88baSGreg Roach return I18N::translate('Mother’s family with %s', $step_family->spouse($step_parent)->fullName()); 1042b2ce94c6SRico Sonntag } 1043b2ce94c6SRico Sonntag 1044a25f0a04SGreg Roach /* I18N: A step-family. */ 1045bbb76c12SGreg Roach return I18N::translate('Mother’s family with an unknown individual'); 1046a25f0a04SGreg Roach } 1047a25f0a04SGreg Roach } 1048a25f0a04SGreg Roach } 1049a25f0a04SGreg Roach } 1050a25f0a04SGreg Roach } 1051a25f0a04SGreg Roach 1052a25f0a04SGreg Roach // Perahps same parents - but a different family record? 1053a25f0a04SGreg Roach return I18N::translate('Family with parents'); 1054a25f0a04SGreg Roach } 1055a25f0a04SGreg Roach 1056225e381fSGreg Roach /** 1057225e381fSGreg Roach * Get the description for the family. 1058225e381fSGreg Roach * 1059225e381fSGreg Roach * For example, "XXX's family with new wife". 1060225e381fSGreg Roach * 1061225e381fSGreg Roach * @param Family $family 1062225e381fSGreg Roach * 1063225e381fSGreg Roach * @return string 1064225e381fSGreg Roach */ 1065e364afe4SGreg Roach public function getSpouseFamilyLabel(Family $family): string 1066c1010edaSGreg Roach { 106739ca88baSGreg Roach $spouse = $family->spouse($this); 1068225e381fSGreg Roach if ($spouse) { 1069225e381fSGreg Roach /* I18N: %s is the spouse name */ 107039ca88baSGreg Roach return I18N::translate('Family with %s', $spouse->fullName()); 1071225e381fSGreg Roach } 1072b2ce94c6SRico Sonntag 107339ca88baSGreg Roach return $family->fullName(); 1074225e381fSGreg Roach } 1075225e381fSGreg Roach 1076a25f0a04SGreg Roach /** 1077a25f0a04SGreg Roach * get primary parents names for this individual 1078a25f0a04SGreg Roach * 1079a25f0a04SGreg Roach * @param string $classname optional css class 1080a25f0a04SGreg Roach * @param string $display optional css style display 1081a25f0a04SGreg Roach * 1082a25f0a04SGreg Roach * @return string a div block with father & mother names 1083a25f0a04SGreg Roach */ 10848f53f488SRico Sonntag public function getPrimaryParentsNames($classname = '', $display = ''): string 1085c1010edaSGreg Roach { 108639ca88baSGreg Roach $fam = $this->primaryChildFamily(); 1087a25f0a04SGreg Roach if (!$fam) { 1088a25f0a04SGreg Roach return ''; 1089a25f0a04SGreg Roach } 1090a25f0a04SGreg Roach $txt = '<div'; 1091a25f0a04SGreg Roach if ($classname) { 10924c621133SGreg Roach $txt .= ' class="' . $classname . '"'; 1093a25f0a04SGreg Roach } 1094a25f0a04SGreg Roach if ($display) { 10954c621133SGreg Roach $txt .= ' style="display:' . $display . '"'; 1096a25f0a04SGreg Roach } 1097a25f0a04SGreg Roach $txt .= '>'; 109839ca88baSGreg Roach $husb = $fam->husband(); 1099a25f0a04SGreg Roach if ($husb) { 1100a25f0a04SGreg Roach // Temporarily reset the 'prefered' display name, as we always 1101a25f0a04SGreg Roach // want the default name, not the one selected for display on the indilist. 1102a25f0a04SGreg Roach $primary = $husb->getPrimaryName(); 1103a25f0a04SGreg Roach $husb->setPrimaryName(null); 1104a25f0a04SGreg Roach /* I18N: %s is the name of an individual’s father */ 110539ca88baSGreg Roach $txt .= I18N::translate('Father: %s', $husb->fullName()) . '<br>'; 1106a25f0a04SGreg Roach $husb->setPrimaryName($primary); 1107a25f0a04SGreg Roach } 110839ca88baSGreg Roach $wife = $fam->wife(); 1109a25f0a04SGreg Roach if ($wife) { 1110a25f0a04SGreg Roach // Temporarily reset the 'prefered' display name, as we always 1111a25f0a04SGreg Roach // want the default name, not the one selected for display on the indilist. 1112a25f0a04SGreg Roach $primary = $wife->getPrimaryName(); 1113a25f0a04SGreg Roach $wife->setPrimaryName(null); 1114a25f0a04SGreg Roach /* I18N: %s is the name of an individual’s mother */ 111539ca88baSGreg Roach $txt .= I18N::translate('Mother: %s', $wife->fullName()); 1116a25f0a04SGreg Roach $wife->setPrimaryName($primary); 1117a25f0a04SGreg Roach } 1118a25f0a04SGreg Roach $txt .= '</div>'; 1119a25f0a04SGreg Roach 1120a25f0a04SGreg Roach return $txt; 1121a25f0a04SGreg Roach } 1122a25f0a04SGreg Roach 1123961ec755SGreg Roach /** 1124961ec755SGreg Roach * If this object has no name, what do we call it? 1125961ec755SGreg Roach * 1126961ec755SGreg Roach * @return string 1127961ec755SGreg Roach */ 11288f53f488SRico Sonntag public function getFallBackName(): string 1129c1010edaSGreg Roach { 1130a25f0a04SGreg Roach return '@P.N. /@N.N./'; 1131a25f0a04SGreg Roach } 1132a25f0a04SGreg Roach 1133a25f0a04SGreg Roach /** 1134a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 1135a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 1136a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 1137a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 1138a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 1139a25f0a04SGreg Roach * recorded in the NAME field. e.g. 1140a25f0a04SGreg Roach * 1141a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 1142a25f0a04SGreg Roach * 2 GIVN Robert 1143a25f0a04SGreg Roach * 2 SPFX de 1144a25f0a04SGreg Roach * 2 SURN CLITHEROW 1145a25f0a04SGreg Roach * 2 NICK The Bald 1146a25f0a04SGreg Roach * 1147a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 1148a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 1149a25f0a04SGreg Roach * 1150a25f0a04SGreg Roach * Handle multiple surnames, either as; 1151a25f0a04SGreg Roach * 1152a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 1153a25f0a04SGreg Roach * or 1154a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 1155a25f0a04SGreg Roach * 2 GIVN Carlos 1156a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 1157a25f0a04SGreg Roach * 1158a25f0a04SGreg Roach * @param string $type 1159a25f0a04SGreg Roach * @param string $full 1160a25f0a04SGreg Roach * @param string $gedcom 1161e364afe4SGreg Roach * 1162e364afe4SGreg Roach * @return void 1163a25f0a04SGreg Roach */ 1164e364afe4SGreg Roach protected function addName(string $type, string $full, string $gedcom): void 1165c1010edaSGreg Roach { 1166a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1167a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 1168a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1169a25f0a04SGreg Roach 117076f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 1171a25f0a04SGreg Roach $GIVN = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : ''; 1172a25f0a04SGreg Roach $SURN = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : ''; 1173a25f0a04SGreg Roach $NICK = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : ''; 1174a25f0a04SGreg Roach 1175a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 117676f666f4SGreg Roach if ($SURN !== '') { 1177a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 1178a25f0a04SGreg Roach } else { 117913abd6f3SGreg Roach $SURNS = []; 1180a25f0a04SGreg Roach } 118176f666f4SGreg Roach 1182a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 1183a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 1184a25f0a04SGreg Roach 1185a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1186a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 1187a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1188a25f0a04SGreg Roach 1189a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 119076f666f4SGreg Roach if (substr_count($full, '/') % 2 === 1) { 1191e364afe4SGreg Roach $full .= '/'; 1192a25f0a04SGreg Roach } 1193a25f0a04SGreg Roach 1194a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 1195a25f0a04SGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $full); 1196a25f0a04SGreg Roach 1197a25f0a04SGreg Roach // Extract the surname. 1198a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 1199a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 1200a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 1201a25f0a04SGreg Roach } else { 1202a25f0a04SGreg Roach $surname = ''; 1203a25f0a04SGreg Roach } 1204a25f0a04SGreg Roach 1205a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 1206a25f0a04SGreg Roach if (!$SURNS) { 1207a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 1208a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 1209a25f0a04SGreg Roach $SURNS = $matches[1]; 1210a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 1211a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 1212a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 1213a25f0a04SGreg Roach } 1214a25f0a04SGreg Roach } else { 1215a25f0a04SGreg Roach // It is valid not to have a surname at all 121613abd6f3SGreg Roach $SURNS = ['']; 1217a25f0a04SGreg Roach } 1218a25f0a04SGreg Roach } 1219a25f0a04SGreg Roach 1220a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 1221a25f0a04SGreg Roach if (!$GIVN) { 1222a25f0a04SGreg Roach $GIVN = preg_replace( 122313abd6f3SGreg Roach [ 1224c1010edaSGreg Roach '/ ?\/.*\/ ?/', 1225c1010edaSGreg Roach // remove surname 1226c1010edaSGreg Roach '/ ?".+"/', 1227c1010edaSGreg Roach // remove nickname 1228c1010edaSGreg Roach '/ {2,}/', 1229c1010edaSGreg Roach // multiple spaces, caused by the above 1230c1010edaSGreg Roach '/^ | $/', 1231c1010edaSGreg Roach // leading/trailing spaces, caused by the above 123213abd6f3SGreg Roach ], 123313abd6f3SGreg Roach [ 1234a25f0a04SGreg Roach ' ', 1235a25f0a04SGreg Roach ' ', 1236a25f0a04SGreg Roach ' ', 1237a25f0a04SGreg Roach '', 123813abd6f3SGreg Roach ], 1239a25f0a04SGreg Roach $full 1240a25f0a04SGreg Roach ); 1241a25f0a04SGreg Roach } 1242a25f0a04SGreg Roach 1243a25f0a04SGreg Roach // Add placeholder for unknown given name 1244a25f0a04SGreg Roach if (!$GIVN) { 1245a25f0a04SGreg Roach $GIVN = '@P.N.'; 124673f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1247a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1248a25f0a04SGreg Roach } 1249a25f0a04SGreg Roach 12507b0e71c3SGreg Roach // GEDCOM 5.5.1 nicknames should be specificied in a NICK field 12517b0e71c3SGreg Roach // GEDCOM 5.5 nicknames should be specified in the NAME field, surrounded by quotes 1252c6f196c3SGreg Roach if ($NICK && strpos($full, '"' . $NICK . '"') === false) { 12537b0e71c3SGreg Roach // A NICK field is present, but not included in the NAME. Show it at the end. 1254c6f196c3SGreg Roach $full .= ' "' . $NICK . '"'; 1255a25f0a04SGreg Roach } 1256a25f0a04SGreg Roach 1257a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1258a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1259a25f0a04SGreg Roach // $full is for display on-screen 1260a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1261a25f0a04SGreg Roach 1262a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1263ad1a1cd2SGreg Roach $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full); 1264ad1a1cd2SGreg Roach $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full); 1265c6f196c3SGreg Roach // Format for display 1266d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1267acc34ea1SGreg Roach // Localise quotation marks around the nickname 12680b5fd0a6SGreg Roach $full = preg_replace_callback('/"([^&]*)"/', static function (array $matches): string { 12698d68cabeSGreg Roach return I18N::translate('“%s”', $matches[1]); 12708d68cabeSGreg Roach }, $full); 1271a25f0a04SGreg Roach 1272c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1273a25f0a04SGreg Roach $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full); 1274a25f0a04SGreg Roach 1275a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1276a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1277a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1278a25f0a04SGreg Roach 1279ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1280a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1281e364afe4SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') === 0) { 1282a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1283e364afe4SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') === 0) { 1284a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1285a25f0a04SGreg Roach } 1286a25f0a04SGreg Roach 1287bdb3725aSGreg Roach $this->getAllNames[] = [ 1288a25f0a04SGreg Roach 'type' => $type, 1289a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1290c1010edaSGreg Roach 'full' => $full, 1291c1010edaSGreg Roach // This is used for display 1292c1010edaSGreg Roach 'fullNN' => $fullNN, 1293c1010edaSGreg Roach // This goes into the database 1294c1010edaSGreg Roach 'surname' => $surname, 1295c1010edaSGreg Roach // This goes into the database 1296c1010edaSGreg Roach 'givn' => $GIVN, 1297c1010edaSGreg Roach // This goes into the database 1298c1010edaSGreg Roach 'surn' => $SURN, 1299c1010edaSGreg Roach // This goes into the database 130013abd6f3SGreg Roach ]; 1301a25f0a04SGreg Roach } 1302a25f0a04SGreg Roach } 1303a25f0a04SGreg Roach 1304a25f0a04SGreg Roach /** 130576692c8bSGreg Roach * Extract names from the GEDCOM record. 1306c7ff4153SGreg Roach * 1307c7ff4153SGreg Roach * @return void 1308a25f0a04SGreg Roach */ 1309e364afe4SGreg Roach public function extractNames(): void 1310c1010edaSGreg Roach { 13118f53f488SRico Sonntag $this->extractNamesFromFacts( 13128f53f488SRico Sonntag 1, 13138f53f488SRico Sonntag 'NAME', 131430158ae7SGreg Roach $this->facts( 13158d0ebef0SGreg Roach ['NAME'], 13168f53f488SRico Sonntag false, 13178f53f488SRico Sonntag Auth::accessLevel($this->tree), 13188f53f488SRico Sonntag $this->canShowName() 13198f53f488SRico Sonntag ) 13208f53f488SRico Sonntag ); 1321a25f0a04SGreg Roach } 1322a25f0a04SGreg Roach 1323a25f0a04SGreg Roach /** 1324a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1325a25f0a04SGreg Roach * selection items or favorites. 1326a25f0a04SGreg Roach * 1327a25f0a04SGreg Roach * @return string 1328a25f0a04SGreg Roach */ 13298f53f488SRico Sonntag public function formatListDetails(): string 1330c1010edaSGreg Roach { 1331a25f0a04SGreg Roach return 13328d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 13338d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1334a25f0a04SGreg Roach } 1335a25f0a04SGreg Roach} 1336