1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 19a25f0a04SGreg Roach 20886b77daSGreg Roachuse Closure; 21a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi; 232e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 24886b77daSGreg Roachuse stdClass; 25a25f0a04SGreg Roach 26a25f0a04SGreg Roach/** 2776692c8bSGreg Roach * A GEDCOM individual (INDI) object. 28a25f0a04SGreg Roach */ 29c1010edaSGreg Roachclass Individual extends GedcomRecord 30c1010edaSGreg Roach{ 3116d6367aSGreg Roach public const RECORD_TYPE = 'INDI'; 3216d6367aSGreg Roach 3316d6367aSGreg Roach protected const ROUTE_NAME = 'individual'; 34a25f0a04SGreg Roach 3576692c8bSGreg Roach /** @var int used in some lists to keep track of this individual’s generation in that list */ 3676692c8bSGreg Roach public $generation; 37a25f0a04SGreg Roach 38a25f0a04SGreg Roach /** @var Date The estimated date of birth */ 394686330aSGreg Roach private $estimated_birth_date; 40a25f0a04SGreg Roach 41a25f0a04SGreg Roach /** @var Date The estimated date of death */ 424686330aSGreg Roach private $estimated_death_date; 43a25f0a04SGreg Roach 44a25f0a04SGreg Roach /** 45886b77daSGreg Roach * A closure which will create a record from a database row. 46886b77daSGreg Roach * 47886b77daSGreg Roach * @param Tree $tree 48886b77daSGreg Roach * 49886b77daSGreg Roach * @return Closure 50886b77daSGreg Roach */ 51886b77daSGreg Roach public static function rowMapper(Tree $tree): Closure 52886b77daSGreg Roach { 53886b77daSGreg Roach return function (stdClass $row) use ($tree): Individual { 54*e84cf2deSGreg Roach $individual = Individual::getInstance($row->i_id, $tree, $row->i_gedcom); 55*e84cf2deSGreg Roach 56*e84cf2deSGreg Roach if ($row->n_num ?? null) { 57*e84cf2deSGreg Roach $individual->setPrimaryName($row->n_num); 58*e84cf2deSGreg Roach } 59*e84cf2deSGreg Roach 60*e84cf2deSGreg Roach return $individual; 61886b77daSGreg Roach }; 62886b77daSGreg Roach } 63886b77daSGreg Roach 64886b77daSGreg Roach /** 65e71ef9d2SGreg Roach * Get an instance of an individual object. For single records, 66e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 67e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 68e71ef9d2SGreg Roach * 69e71ef9d2SGreg Roach * @param string $xref 70e71ef9d2SGreg Roach * @param Tree $tree 71e71ef9d2SGreg Roach * @param string|null $gedcom 72e71ef9d2SGreg Roach * 73e71ef9d2SGreg Roach * @throws \Exception 74e71ef9d2SGreg Roach * @return Individual|null 75e71ef9d2SGreg Roach */ 7676f666f4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 77c1010edaSGreg Roach { 78e71ef9d2SGreg Roach $record = parent::getInstance($xref, $tree, $gedcom); 79e71ef9d2SGreg Roach 80e71ef9d2SGreg Roach if ($record instanceof Individual) { 81e71ef9d2SGreg Roach return $record; 82e71ef9d2SGreg Roach } 83b2ce94c6SRico Sonntag 84b2ce94c6SRico Sonntag return null; 85e71ef9d2SGreg Roach } 86e71ef9d2SGreg Roach 87e71ef9d2SGreg Roach /** 88395f0fe0SGreg Roach * Sometimes, we'll know in advance that we need to load a set of records. 89395f0fe0SGreg Roach * Typically when we load families and their members. 90395f0fe0SGreg Roach * 91395f0fe0SGreg Roach * @param Tree $tree 924a8aaa00SScrutinizer Auto-Fixer * @param string[] $xrefs 9318d7a90dSGreg Roach * 9418d7a90dSGreg Roach * @return void 95395f0fe0SGreg Roach */ 962e5b4452SGreg Roach public static function load(Tree $tree, array $xrefs): void 97c1010edaSGreg Roach { 982e5b4452SGreg Roach $rows = DB::table('individuals') 992e5b4452SGreg Roach ->where('i_file', '=', $tree->id()) 1002e5b4452SGreg Roach ->whereIn('i_id', array_unique($xrefs)) 1012e5b4452SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom']) 1022e5b4452SGreg Roach ->get(); 103395f0fe0SGreg Roach 104395f0fe0SGreg Roach foreach ($rows as $row) { 105395f0fe0SGreg Roach self::getInstance($row->xref, $tree, $row->gedcom); 106395f0fe0SGreg Roach } 107395f0fe0SGreg Roach } 108395f0fe0SGreg Roach 109395f0fe0SGreg Roach /** 110a25f0a04SGreg Roach * Can the name of this record be shown? 111a25f0a04SGreg Roach * 11276692c8bSGreg Roach * @param int|null $access_level 11376692c8bSGreg Roach * 11476692c8bSGreg Roach * @return bool 115a25f0a04SGreg Roach */ 11635584196SGreg Roach public function canShowName(int $access_level = null): bool 117c1010edaSGreg Roach { 1184b9ff166SGreg Roach if ($access_level === null) { 1194b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 1204b9ff166SGreg Roach } 1214b9ff166SGreg Roach 122518bbdc1SGreg Roach return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level); 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach 125a25f0a04SGreg Roach /** 12676692c8bSGreg Roach * Can this individual be shown? 127a25f0a04SGreg Roach * 12876692c8bSGreg Roach * @param int $access_level 12976692c8bSGreg Roach * 13076692c8bSGreg Roach * @return bool 131a25f0a04SGreg Roach */ 13235584196SGreg Roach protected function canShowByType(int $access_level): bool 133c1010edaSGreg Roach { 134a25f0a04SGreg Roach // Dead people... 135518bbdc1SGreg Roach if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) { 136a25f0a04SGreg Roach $keep_alive = false; 13754ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH'); 138a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_BIRTH) { 1398d0ebef0SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::BIRTH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER); 140a25f0a04SGreg Roach foreach ($matches as $match) { 141a25f0a04SGreg Roach $date = new Date($match[1]); 142a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) { 143a25f0a04SGreg Roach $keep_alive = true; 144a25f0a04SGreg Roach break; 145a25f0a04SGreg Roach } 146a25f0a04SGreg Roach } 147a25f0a04SGreg Roach } 14854ba7dc5SGreg Roach $KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH'); 149a25f0a04SGreg Roach if ($KEEP_ALIVE_YEARS_DEATH) { 1508d0ebef0SGreg Roach preg_match_all('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER); 151a25f0a04SGreg Roach foreach ($matches as $match) { 152a25f0a04SGreg Roach $date = new Date($match[1]); 153a25f0a04SGreg Roach if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) { 154a25f0a04SGreg Roach $keep_alive = true; 155a25f0a04SGreg Roach break; 156a25f0a04SGreg Roach } 157a25f0a04SGreg Roach } 158a25f0a04SGreg Roach } 159a25f0a04SGreg Roach if (!$keep_alive) { 160a25f0a04SGreg Roach return true; 161a25f0a04SGreg Roach } 162a25f0a04SGreg Roach } 163a25f0a04SGreg Roach // Consider relationship privacy (unless an admin is applying download restrictions) 16454ba7dc5SGreg Roach $user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH'); 1654b9ff166SGreg Roach $gedcomid = $this->tree->getUserPreference(Auth::user(), 'gedcomid'); 16654ba7dc5SGreg Roach if ($gedcomid !== '' && $user_path_length > 0) { 1674b9ff166SGreg Roach return self::isRelated($this, $user_path_length); 168a25f0a04SGreg Roach } 169a25f0a04SGreg Roach 170a25f0a04SGreg Roach // No restriction found - show living people to members only: 1714b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 172a25f0a04SGreg Roach } 173a25f0a04SGreg Roach 174a25f0a04SGreg Roach /** 175a25f0a04SGreg Roach * For relationship privacy calculations - is this individual a close relative? 176a25f0a04SGreg Roach * 177a25f0a04SGreg Roach * @param Individual $target 178cbc1590aSGreg Roach * @param int $distance 179a25f0a04SGreg Roach * 180cbc1590aSGreg Roach * @return bool 181a25f0a04SGreg Roach */ 1828f53f488SRico Sonntag private static function isRelated(Individual $target, $distance): bool 183c1010edaSGreg Roach { 184a25f0a04SGreg Roach static $cache = null; 185a25f0a04SGreg Roach 18606ef8e02SGreg Roach $user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree); 187a25f0a04SGreg Roach if ($user_individual) { 188a25f0a04SGreg Roach if (!$cache) { 18913abd6f3SGreg Roach $cache = [ 19013abd6f3SGreg Roach 0 => [$user_individual], 19113abd6f3SGreg Roach 1 => [], 19213abd6f3SGreg Roach ]; 1938d0ebef0SGreg Roach foreach ($user_individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 194dc124885SGreg Roach $family = $fact->target(); 195e24444eeSGreg Roach if ($family instanceof Family) { 196a25f0a04SGreg Roach $cache[1][] = $family; 197a25f0a04SGreg Roach } 198a25f0a04SGreg Roach } 199a25f0a04SGreg Roach } 200a25f0a04SGreg Roach } else { 201a25f0a04SGreg Roach // No individual linked to this account? Cannot use relationship privacy. 202a25f0a04SGreg Roach return true; 203a25f0a04SGreg Roach } 204a25f0a04SGreg Roach 205a25f0a04SGreg Roach // Double the distance, as we count the INDI-FAM and FAM-INDI links separately 206a25f0a04SGreg Roach $distance *= 2; 207a25f0a04SGreg Roach 208a25f0a04SGreg Roach // Consider each path length in turn 209a25f0a04SGreg Roach for ($n = 0; $n <= $distance; ++$n) { 210a25f0a04SGreg Roach if (array_key_exists($n, $cache)) { 211a25f0a04SGreg Roach // We have already calculated all records with this length 212a25f0a04SGreg Roach if ($n % 2 == 0 && in_array($target, $cache[$n], true)) { 213a25f0a04SGreg Roach return true; 214a25f0a04SGreg Roach } 215a25f0a04SGreg Roach } else { 216a25f0a04SGreg Roach // Need to calculate these paths 21713abd6f3SGreg Roach $cache[$n] = []; 218a25f0a04SGreg Roach if ($n % 2 == 0) { 219a25f0a04SGreg Roach // Add FAM->INDI links 220a25f0a04SGreg Roach foreach ($cache[$n - 1] as $family) { 2218d0ebef0SGreg Roach foreach ($family->facts(['HUSB', 'WIFE', 'CHIL'], false, Auth::PRIV_HIDE) as $fact) { 222dc124885SGreg Roach $individual = $fact->target(); 223a25f0a04SGreg Roach // Don’t backtrack 224e24444eeSGreg Roach if ($individual instanceof Individual && !in_array($individual, $cache[$n - 2], true)) { 225a25f0a04SGreg Roach $cache[$n][] = $individual; 226a25f0a04SGreg Roach } 227a25f0a04SGreg Roach } 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach if (in_array($target, $cache[$n], true)) { 230a25f0a04SGreg Roach return true; 231a25f0a04SGreg Roach } 232a25f0a04SGreg Roach } else { 233a25f0a04SGreg Roach // Add INDI->FAM links 234a25f0a04SGreg Roach foreach ($cache[$n - 1] as $individual) { 2358d0ebef0SGreg Roach foreach ($individual->facts(['FAMC', 'FAMS'], false, Auth::PRIV_HIDE) as $fact) { 236dc124885SGreg Roach $family = $fact->target(); 237a25f0a04SGreg Roach // Don’t backtrack 238e24444eeSGreg Roach if ($family instanceof Family && !in_array($family, $cache[$n - 2], true)) { 239a25f0a04SGreg Roach $cache[$n][] = $family; 240a25f0a04SGreg Roach } 241a25f0a04SGreg Roach } 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach } 244a25f0a04SGreg Roach } 245a25f0a04SGreg Roach } 246a25f0a04SGreg Roach 247a25f0a04SGreg Roach return false; 248a25f0a04SGreg Roach } 249a25f0a04SGreg Roach 25076692c8bSGreg Roach /** 25176692c8bSGreg Roach * Generate a private version of this record 25276692c8bSGreg Roach * 25376692c8bSGreg Roach * @param int $access_level 25476692c8bSGreg Roach * 25576692c8bSGreg Roach * @return string 25676692c8bSGreg Roach */ 2573c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 258c1010edaSGreg Roach { 259acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 260a25f0a04SGreg Roach 261a25f0a04SGreg Roach $rec = '0 @' . $this->xref . '@ INDI'; 262518bbdc1SGreg Roach if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) { 263a25f0a04SGreg Roach // Show all the NAME tags, including subtags 2648d0ebef0SGreg Roach foreach ($this->facts(['NAME']) as $fact) { 265138ca96cSGreg Roach $rec .= "\n" . $fact->gedcom(); 266a25f0a04SGreg Roach } 267a25f0a04SGreg Roach } 268a25f0a04SGreg Roach // Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data 2698d0ebef0SGreg Roach preg_match_all('/\n1 (?:FAMC|FAMS) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 270a25f0a04SGreg Roach foreach ($matches as $match) { 27124ec66ceSGreg Roach $rela = Family::getInstance($match[1], $this->tree); 272a25f0a04SGreg Roach if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 273a25f0a04SGreg Roach $rec .= $match[0]; 274a25f0a04SGreg Roach } 275a25f0a04SGreg Roach } 276a25f0a04SGreg Roach // Don’t privatize sex. 277a25f0a04SGreg Roach if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) { 278a25f0a04SGreg Roach $rec .= $match[0]; 279a25f0a04SGreg Roach } 280a25f0a04SGreg Roach 281a25f0a04SGreg Roach return $rec; 282a25f0a04SGreg Roach } 283a25f0a04SGreg Roach 28476692c8bSGreg Roach /** 28576692c8bSGreg Roach * Fetch data from the database 28676692c8bSGreg Roach * 28776692c8bSGreg Roach * @param string $xref 28876692c8bSGreg Roach * @param int $tree_id 28976692c8bSGreg Roach * 29076692c8bSGreg Roach * @return null|string 29176692c8bSGreg Roach */ 29225e95e6eSGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id) 293c1010edaSGreg Roach { 2942e5b4452SGreg Roach return DB::table('individuals') 2952e5b4452SGreg Roach ->where('i_id', '=', $xref) 2962e5b4452SGreg Roach ->where('i_file', '=', $tree_id) 2972e5b4452SGreg Roach ->value('i_gedcom'); 298a25f0a04SGreg Roach } 299a25f0a04SGreg Roach 300a25f0a04SGreg Roach /** 301a25f0a04SGreg Roach * Static helper function to sort an array of people by birth date 302a25f0a04SGreg Roach * 303a25f0a04SGreg Roach * @param Individual $x 304a25f0a04SGreg Roach * @param Individual $y 305a25f0a04SGreg Roach * 306cbc1590aSGreg Roach * @return int 307a25f0a04SGreg Roach */ 3088f53f488SRico Sonntag public static function compareBirthDate(Individual $x, Individual $y): int 309c1010edaSGreg Roach { 310f5b60decSGreg Roach return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate()); 311a25f0a04SGreg Roach } 312a25f0a04SGreg Roach 313a25f0a04SGreg Roach /** 314a25f0a04SGreg Roach * Static helper function to sort an array of people by death date 315a25f0a04SGreg Roach * 316a25f0a04SGreg Roach * @param Individual $x 317a25f0a04SGreg Roach * @param Individual $y 318a25f0a04SGreg Roach * 319cbc1590aSGreg Roach * @return int 320a25f0a04SGreg Roach */ 3218f53f488SRico Sonntag public static function compareDeathDate(Individual $x, Individual $y): int 322c1010edaSGreg Roach { 323f5b60decSGreg Roach return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate()); 324a25f0a04SGreg Roach } 325a25f0a04SGreg Roach 326a25f0a04SGreg Roach /** 327a25f0a04SGreg Roach * Calculate whether this individual is living or dead. 328a25f0a04SGreg Roach * If not known to be dead, then assume living. 329a25f0a04SGreg Roach * 330cbc1590aSGreg Roach * @return bool 331a25f0a04SGreg Roach */ 3328f53f488SRico Sonntag public function isDead(): bool 333c1010edaSGreg Roach { 334c4b3e5a2SGreg Roach $MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 335a25f0a04SGreg Roach 336a25f0a04SGreg Roach // "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC" 3378d0ebef0SGreg Roach if (preg_match('/\n1 (?:' . implode('|', Gedcom::DEATH_EVENTS) . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) { 338a25f0a04SGreg Roach return true; 339a25f0a04SGreg Roach } 340a25f0a04SGreg Roach 341a25f0a04SGreg Roach // If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead 342a25f0a04SGreg Roach if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) { 343a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 344a25f0a04SGreg Roach $date = new Date($date_match); 345f5b60decSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) { 346a25f0a04SGreg Roach return true; 347a25f0a04SGreg Roach } 348a25f0a04SGreg Roach } 349a25f0a04SGreg Roach // The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago. 350a25f0a04SGreg Roach // If one of these is a birth, the individual must be alive. 351a25f0a04SGreg Roach if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) { 352a25f0a04SGreg Roach return false; 353a25f0a04SGreg Roach } 354a25f0a04SGreg Roach } 355a25f0a04SGreg Roach 356a25f0a04SGreg Roach // If we found no conclusive dates then check the dates of close relatives. 357a25f0a04SGreg Roach 358a25f0a04SGreg Roach // Check parents (birth and adopted) 3594b9ff166SGreg Roach foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) { 3604b9ff166SGreg Roach foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) { 361a25f0a04SGreg Roach // Assume parents are no more than 45 years older than their children 362a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches); 363a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 364a25f0a04SGreg Roach $date = new Date($date_match); 365f5b60decSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) { 366a25f0a04SGreg Roach return true; 367a25f0a04SGreg Roach } 368a25f0a04SGreg Roach } 369a25f0a04SGreg Roach } 370a25f0a04SGreg Roach } 371a25f0a04SGreg Roach 372a25f0a04SGreg Roach // Check spouses 3734b9ff166SGreg Roach foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) { 374a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches); 375a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 376a25f0a04SGreg Roach $date = new Date($date_match); 377a25f0a04SGreg Roach // Assume marriage occurs after age of 10 378f5b60decSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) { 379a25f0a04SGreg Roach return true; 380a25f0a04SGreg Roach } 381a25f0a04SGreg Roach } 382a25f0a04SGreg Roach // Check spouse dates 38313e3123bSGreg Roach $spouse = $family->getSpouse($this, Auth::PRIV_HIDE); 384a25f0a04SGreg Roach if ($spouse) { 385a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches); 386a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 387a25f0a04SGreg Roach $date = new Date($date_match); 388a25f0a04SGreg Roach // Assume max age difference between spouses of 40 years 389f5b60decSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) { 390a25f0a04SGreg Roach return true; 391a25f0a04SGreg Roach } 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach } 394a25f0a04SGreg Roach // Check child dates 3954b9ff166SGreg Roach foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) { 396a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches); 397a25f0a04SGreg Roach // Assume children born after age of 15 398a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 399a25f0a04SGreg Roach $date = new Date($date_match); 400f5b60decSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) { 401a25f0a04SGreg Roach return true; 402a25f0a04SGreg Roach } 403a25f0a04SGreg Roach } 404a25f0a04SGreg Roach // Check grandchildren 4054b9ff166SGreg Roach foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) { 4064b9ff166SGreg Roach foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) { 407a25f0a04SGreg Roach preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches); 408a25f0a04SGreg Roach // Assume grandchildren born after age of 30 409a25f0a04SGreg Roach foreach ($date_matches[1] as $date_match) { 410a25f0a04SGreg Roach $date = new Date($date_match); 411f5b60decSGreg Roach if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) { 412a25f0a04SGreg Roach return true; 413a25f0a04SGreg Roach } 414a25f0a04SGreg Roach } 415a25f0a04SGreg Roach } 416a25f0a04SGreg Roach } 417a25f0a04SGreg Roach } 418a25f0a04SGreg Roach } 419a25f0a04SGreg Roach 420a25f0a04SGreg Roach return false; 421a25f0a04SGreg Roach } 422a25f0a04SGreg Roach 423a25f0a04SGreg Roach /** 424a25f0a04SGreg Roach * Find the highlighted media object for an individual 425a25f0a04SGreg Roach * 4264a9f750fSGreg Roach * @return null|MediaFile 427a25f0a04SGreg Roach */ 428c1010edaSGreg Roach public function findHighlightedMediaFile() 429c1010edaSGreg Roach { 4308d0ebef0SGreg Roach foreach ($this->facts(['OBJE']) as $fact) { 431dc124885SGreg Roach $media = $fact->target(); 432a213a63cSGreg Roach if ($media instanceof Media) { 4334a9f750fSGreg Roach foreach ($media->mediaFiles() as $media_file) { 434a213a63cSGreg Roach if ($media_file->isImage() && !$media_file->isExternal()) { 4354a9f750fSGreg Roach return $media_file; 4364a9f750fSGreg Roach } 4374a9f750fSGreg Roach } 438a25f0a04SGreg Roach } 439a25f0a04SGreg Roach } 440a25f0a04SGreg Roach 441a25f0a04SGreg Roach return null; 442a25f0a04SGreg Roach } 443a25f0a04SGreg Roach 444a25f0a04SGreg Roach /** 445a25f0a04SGreg Roach * Display the prefered image for this individual. 446a25f0a04SGreg Roach * Use an icon if no image is available. 447a25f0a04SGreg Roach * 448dce07401SGreg Roach * @param int $width Pixels 449dce07401SGreg Roach * @param int $height Pixels 450dce07401SGreg Roach * @param string $fit "crop" or "contain" 451dce07401SGreg Roach * @param string[] $attributes Additional HTML attributes 452dce07401SGreg Roach * 453a25f0a04SGreg Roach * @return string 454a25f0a04SGreg Roach */ 4558f53f488SRico Sonntag public function displayImage($width, $height, $fit, $attributes): string 456c1010edaSGreg Roach { 4574a9f750fSGreg Roach $media_file = $this->findHighlightedMediaFile(); 4584a9f750fSGreg Roach 4594a9f750fSGreg Roach if ($media_file !== null) { 4604a9f750fSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 461a25f0a04SGreg Roach } 4624a9f750fSGreg Roach 4634a9f750fSGreg Roach if ($this->tree->getPreference('USE_SILHOUETTE')) { 4644a9f750fSGreg Roach return '<i class="icon-silhouette-' . $this->getSex() . '"></i>'; 4654a9f750fSGreg Roach } 4664a9f750fSGreg Roach 4674a9f750fSGreg Roach return ''; 468a25f0a04SGreg Roach } 469a25f0a04SGreg Roach 470a25f0a04SGreg Roach /** 471a25f0a04SGreg Roach * Get the date of birth 472a25f0a04SGreg Roach * 473a25f0a04SGreg Roach * @return Date 474a25f0a04SGreg Roach */ 4758f53f488SRico Sonntag public function getBirthDate(): Date 476c1010edaSGreg Roach { 477a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 478a25f0a04SGreg Roach if ($date->isOK()) { 479a25f0a04SGreg Roach return $date; 480a25f0a04SGreg Roach } 481a25f0a04SGreg Roach } 482a25f0a04SGreg Roach 483a25f0a04SGreg Roach return new Date(''); 484a25f0a04SGreg Roach } 485a25f0a04SGreg Roach 486a25f0a04SGreg Roach /** 487a25f0a04SGreg Roach * Get the place of birth 488a25f0a04SGreg Roach * 48916d0b7f7SRico Sonntag * @return Place 490a25f0a04SGreg Roach */ 4918f53f488SRico Sonntag public function getBirthPlace(): Place 492c1010edaSGreg Roach { 493a25f0a04SGreg Roach foreach ($this->getAllBirthPlaces() as $place) { 494a25f0a04SGreg Roach return $place; 495a25f0a04SGreg Roach } 496a25f0a04SGreg Roach 497b20ddbf9SGreg Roach return new Place('', $this->tree); 498a25f0a04SGreg Roach } 499a25f0a04SGreg Roach 500a25f0a04SGreg Roach /** 501a25f0a04SGreg Roach * Get the year of birth 502a25f0a04SGreg Roach * 503a25f0a04SGreg Roach * @return string the year of birth 504a25f0a04SGreg Roach */ 5058f53f488SRico Sonntag public function getBirthYear(): string 506c1010edaSGreg Roach { 507f5b60decSGreg Roach return $this->getBirthDate()->minimumDate()->format('%Y'); 508a25f0a04SGreg Roach } 509a25f0a04SGreg Roach 510a25f0a04SGreg Roach /** 511a25f0a04SGreg Roach * Get the date of death 512a25f0a04SGreg Roach * 513a25f0a04SGreg Roach * @return Date 514a25f0a04SGreg Roach */ 5158f53f488SRico Sonntag public function getDeathDate(): Date 516c1010edaSGreg Roach { 517a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 518a25f0a04SGreg Roach if ($date->isOK()) { 519a25f0a04SGreg Roach return $date; 520a25f0a04SGreg Roach } 521a25f0a04SGreg Roach } 522a25f0a04SGreg Roach 523a25f0a04SGreg Roach return new Date(''); 524a25f0a04SGreg Roach } 525a25f0a04SGreg Roach 526a25f0a04SGreg Roach /** 527a25f0a04SGreg Roach * Get the place of death 528a25f0a04SGreg Roach * 52916d0b7f7SRico Sonntag * @return Place 530a25f0a04SGreg Roach */ 5318f53f488SRico Sonntag public function getDeathPlace(): Place 532c1010edaSGreg Roach { 533a25f0a04SGreg Roach foreach ($this->getAllDeathPlaces() as $place) { 534a25f0a04SGreg Roach return $place; 535a25f0a04SGreg Roach } 536a25f0a04SGreg Roach 537b20ddbf9SGreg Roach return new Place('', $this->tree); 538a25f0a04SGreg Roach } 539a25f0a04SGreg Roach 540a25f0a04SGreg Roach /** 541a25f0a04SGreg Roach * get the death year 542a25f0a04SGreg Roach * 543a25f0a04SGreg Roach * @return string the year of death 544a25f0a04SGreg Roach */ 5458f53f488SRico Sonntag public function getDeathYear(): string 546c1010edaSGreg Roach { 547f5b60decSGreg Roach return $this->getDeathDate()->minimumDate()->format('%Y'); 548a25f0a04SGreg Roach } 549a25f0a04SGreg Roach 550a25f0a04SGreg Roach /** 551a25f0a04SGreg Roach * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”. 55215d603e7SGreg Roach * Provide the place and full date using a tooltip. 553a25f0a04SGreg Roach * For consistent layout in charts, etc., show just a “–” when no dates are known. 554a25f0a04SGreg Roach * Note that this is a (non-breaking) en-dash, and not a hyphen. 555a25f0a04SGreg Roach * 556a25f0a04SGreg Roach * @return string 557a25f0a04SGreg Roach */ 5588f53f488SRico Sonntag public function getLifeSpan(): string 559c1010edaSGreg Roach { 56015d603e7SGreg Roach // Just the first part of the place name 5611e9c2c49SGreg Roach $birth_place = strip_tags($this->getBirthPlace()->getShortName()); 5621e9c2c49SGreg Roach $death_place = strip_tags($this->getDeathPlace()->getShortName()); 56315d603e7SGreg Roach // Remove markup from dates 56415d603e7SGreg Roach $birth_date = strip_tags($this->getBirthDate()->display()); 56515d603e7SGreg Roach $death_date = strip_tags($this->getDeathDate()->display()); 56615d603e7SGreg Roach 567c1010edaSGreg Roach /* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ 568bbb76c12SGreg Roach return 569c1010edaSGreg Roach I18N::translate( 570a25f0a04SGreg Roach '%1$s–%2$s', 5712d4c1bedSGreg Roach '<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>', 5722d4c1bedSGreg Roach '<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>' 573a25f0a04SGreg Roach ); 574a25f0a04SGreg Roach } 575a25f0a04SGreg Roach 576a25f0a04SGreg Roach /** 577a25f0a04SGreg Roach * Get all the birth dates - for the individual lists. 578a25f0a04SGreg Roach * 579a25f0a04SGreg Roach * @return Date[] 580a25f0a04SGreg Roach */ 5818f53f488SRico Sonntag public function getAllBirthDates(): array 582c1010edaSGreg Roach { 5838d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 5848d0ebef0SGreg Roach $tmp = $this->getAllEventDates([$event]); 585a25f0a04SGreg Roach if ($tmp) { 586a25f0a04SGreg Roach return $tmp; 587a25f0a04SGreg Roach } 588a25f0a04SGreg Roach } 589a25f0a04SGreg Roach 59013abd6f3SGreg Roach return []; 591a25f0a04SGreg Roach } 592a25f0a04SGreg Roach 593a25f0a04SGreg Roach /** 594a25f0a04SGreg Roach * Gat all the birth places - for the individual lists. 595a25f0a04SGreg Roach * 5964080d558SGreg Roach * @return Place[] 597a25f0a04SGreg Roach */ 5988f53f488SRico Sonntag public function getAllBirthPlaces(): array 599c1010edaSGreg Roach { 6008d0ebef0SGreg Roach foreach (Gedcom::BIRTH_EVENTS as $event) { 6018d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 6024080d558SGreg Roach if (!empty($places)) { 6034080d558SGreg Roach return $places; 604a25f0a04SGreg Roach } 605a25f0a04SGreg Roach } 606a25f0a04SGreg Roach 60713abd6f3SGreg Roach return []; 608a25f0a04SGreg Roach } 609a25f0a04SGreg Roach 610a25f0a04SGreg Roach /** 611a25f0a04SGreg Roach * Get all the death dates - for the individual lists. 612a25f0a04SGreg Roach * 613a25f0a04SGreg Roach * @return Date[] 614a25f0a04SGreg Roach */ 6158f53f488SRico Sonntag public function getAllDeathDates(): array 616c1010edaSGreg Roach { 6178d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 6188d0ebef0SGreg Roach $tmp = $this->getAllEventDates([$event]); 619a25f0a04SGreg Roach if ($tmp) { 620a25f0a04SGreg Roach return $tmp; 621a25f0a04SGreg Roach } 622a25f0a04SGreg Roach } 623a25f0a04SGreg Roach 62413abd6f3SGreg Roach return []; 625a25f0a04SGreg Roach } 626a25f0a04SGreg Roach 627a25f0a04SGreg Roach /** 628a25f0a04SGreg Roach * Get all the death places - for the individual lists. 629a25f0a04SGreg Roach * 6304080d558SGreg Roach * @return Place[] 631a25f0a04SGreg Roach */ 6328f53f488SRico Sonntag public function getAllDeathPlaces(): array 633c1010edaSGreg Roach { 6348d0ebef0SGreg Roach foreach (Gedcom::DEATH_EVENTS as $event) { 6358d0ebef0SGreg Roach $places = $this->getAllEventPlaces([$event]); 6364080d558SGreg Roach if (!empty($places)) { 6374080d558SGreg Roach return $places; 638a25f0a04SGreg Roach } 639a25f0a04SGreg Roach } 640a25f0a04SGreg Roach 64113abd6f3SGreg Roach return []; 642a25f0a04SGreg Roach } 643a25f0a04SGreg Roach 644a25f0a04SGreg Roach /** 645a25f0a04SGreg Roach * Generate an estimate for the date of birth, based on dates of parents/children/spouses 646a25f0a04SGreg Roach * 647a25f0a04SGreg Roach * @return Date 648a25f0a04SGreg Roach */ 6498f53f488SRico Sonntag public function getEstimatedBirthDate(): Date 650c1010edaSGreg Roach { 6518f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 652a25f0a04SGreg Roach foreach ($this->getAllBirthDates() as $date) { 653a25f0a04SGreg Roach if ($date->isOK()) { 6544686330aSGreg Roach $this->estimated_birth_date = $date; 655a25f0a04SGreg Roach break; 656a25f0a04SGreg Roach } 657a25f0a04SGreg Roach } 6588f038c36SRico Sonntag if ($this->estimated_birth_date === null) { 65913abd6f3SGreg Roach $min = []; 66013abd6f3SGreg Roach $max = []; 661a25f0a04SGreg Roach $tmp = $this->getDeathDate(); 662f5b60decSGreg Roach if ($tmp->isOK()) { 663f5b60decSGreg Roach $min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365; 664f5b60decSGreg Roach $max[] = $tmp->maximumJulianDay(); 665a25f0a04SGreg Roach } 666a25f0a04SGreg Roach foreach ($this->getChildFamilies() as $family) { 667a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 668f5b60decSGreg Roach if ($tmp->isOK()) { 669f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 1; 670f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 671a25f0a04SGreg Roach } 6722e5b4452SGreg Roach $husband = $family->getHusband(); 6732e5b4452SGreg Roach if ($husband instanceof Individual) { 6742e5b4452SGreg Roach $tmp = $husband->getBirthDate(); 675f5b60decSGreg Roach if ($tmp->isOK()) { 676f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 677f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 65; 678a25f0a04SGreg Roach } 679a25f0a04SGreg Roach } 6802e5b4452SGreg Roach $wife = $family->getWife(); 6812e5b4452SGreg Roach if ($wife instanceof Individual) { 6822e5b4452SGreg Roach $tmp = $wife->getBirthDate(); 683f5b60decSGreg Roach if ($tmp->isOK()) { 684f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() + 365 * 15; 685f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 45; 686a25f0a04SGreg Roach } 687a25f0a04SGreg Roach } 688a25f0a04SGreg Roach foreach ($family->getChildren() as $child) { 689a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 690f5b60decSGreg Roach if ($tmp->isOK()) { 691f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 30; 692f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 30; 693a25f0a04SGreg Roach } 694a25f0a04SGreg Roach } 695a25f0a04SGreg Roach } 696a25f0a04SGreg Roach foreach ($this->getSpouseFamilies() as $family) { 697a25f0a04SGreg Roach $tmp = $family->getMarriageDate(); 698f5b60decSGreg Roach if ($tmp->isOK()) { 699f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 45; 700f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 701a25f0a04SGreg Roach } 702a25f0a04SGreg Roach $spouse = $family->getSpouse($this); 703a25f0a04SGreg Roach if ($spouse) { 704a25f0a04SGreg Roach $tmp = $spouse->getBirthDate(); 705f5b60decSGreg Roach if ($tmp->isOK()) { 706f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * 25; 707f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() + 365 * 25; 708a25f0a04SGreg Roach } 709a25f0a04SGreg Roach } 710a25f0a04SGreg Roach foreach ($family->getChildren() as $child) { 711a25f0a04SGreg Roach $tmp = $child->getBirthDate(); 712f5b60decSGreg Roach if ($tmp->isOK()) { 713f5b60decSGreg Roach $min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65); 714f5b60decSGreg Roach $max[] = $tmp->minimumJulianDay() - 365 * 15; 715a25f0a04SGreg Roach } 716a25f0a04SGreg Roach } 717a25f0a04SGreg Roach } 718a25f0a04SGreg Roach if ($min && $max) { 71959f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 720a25f0a04SGreg Roach 72165e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd(intdiv(max($min) + min($max), 2)); 7224686330aSGreg Roach $this->estimated_birth_date = new Date('EST ' . $year); 723a25f0a04SGreg Roach } else { 7244686330aSGreg Roach $this->estimated_birth_date = new Date(''); // always return a date object 725a25f0a04SGreg Roach } 726a25f0a04SGreg Roach } 727a25f0a04SGreg Roach } 728a25f0a04SGreg Roach 7294686330aSGreg Roach return $this->estimated_birth_date; 730a25f0a04SGreg Roach } 731a25f0a04SGreg Roach 732a25f0a04SGreg Roach /** 733a25f0a04SGreg Roach * Generate an estimated date of death. 734a25f0a04SGreg Roach * 735a25f0a04SGreg Roach * @return Date 736a25f0a04SGreg Roach */ 7378f53f488SRico Sonntag public function getEstimatedDeathDate(): Date 738c1010edaSGreg Roach { 7394686330aSGreg Roach if ($this->estimated_death_date === null) { 740a25f0a04SGreg Roach foreach ($this->getAllDeathDates() as $date) { 741a25f0a04SGreg Roach if ($date->isOK()) { 7424686330aSGreg Roach $this->estimated_death_date = $date; 743a25f0a04SGreg Roach break; 744a25f0a04SGreg Roach } 745a25f0a04SGreg Roach } 7464686330aSGreg Roach if ($this->estimated_death_date === null) { 747f5b60decSGreg Roach if ($this->getEstimatedBirthDate()->minimumJulianDay()) { 748c4b3e5a2SGreg Roach $max_alive_age = (int) $this->tree->getPreference('MAX_ALIVE_AGE'); 7494686330aSGreg Roach $this->estimated_death_date = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF'); 750a25f0a04SGreg Roach } else { 7514686330aSGreg Roach $this->estimated_death_date = new Date(''); // always return a date object 752a25f0a04SGreg Roach } 753a25f0a04SGreg Roach } 754a25f0a04SGreg Roach } 755a25f0a04SGreg Roach 7564686330aSGreg Roach return $this->estimated_death_date; 757a25f0a04SGreg Roach } 758a25f0a04SGreg Roach 759a25f0a04SGreg Roach /** 760a25f0a04SGreg Roach * Get the sex - M F or U 761a25f0a04SGreg Roach * Use the un-privatised gedcom record. We call this function during 762a25f0a04SGreg Roach * the privatize-gedcom function, and we are allowed to know this. 763a25f0a04SGreg Roach * 764a25f0a04SGreg Roach * @return string 765a25f0a04SGreg Roach */ 766c1010edaSGreg Roach public function getSex() 767c1010edaSGreg Roach { 768a25f0a04SGreg Roach if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) { 769a25f0a04SGreg Roach return $match[1]; 770a25f0a04SGreg Roach } 771b2ce94c6SRico Sonntag 772b2ce94c6SRico Sonntag return 'U'; 773a25f0a04SGreg Roach } 774a25f0a04SGreg Roach 775a25f0a04SGreg Roach /** 776a25f0a04SGreg Roach * Get the individual’s sex image 777a25f0a04SGreg Roach * 778a25f0a04SGreg Roach * @param string $size 779a25f0a04SGreg Roach * 780a25f0a04SGreg Roach * @return string 781a25f0a04SGreg Roach */ 7828f53f488SRico Sonntag public function getSexImage($size = 'small'): string 783c1010edaSGreg Roach { 784a25f0a04SGreg Roach return self::sexImage($this->getSex(), $size); 785a25f0a04SGreg Roach } 786a25f0a04SGreg Roach 787a25f0a04SGreg Roach /** 788a25f0a04SGreg Roach * Generate a sex icon/image 789a25f0a04SGreg Roach * 790a25f0a04SGreg Roach * @param string $sex 791a25f0a04SGreg Roach * @param string $size 792a25f0a04SGreg Roach * 793a25f0a04SGreg Roach * @return string 794a25f0a04SGreg Roach */ 7958f53f488SRico Sonntag public static function sexImage($sex, $size = 'small'): string 796c1010edaSGreg Roach { 797a25f0a04SGreg Roach return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>'; 798a25f0a04SGreg Roach } 799a25f0a04SGreg Roach 800a25f0a04SGreg Roach /** 801a25f0a04SGreg Roach * Generate the CSS class to be used for drawing this individual 802a25f0a04SGreg Roach * 803a25f0a04SGreg Roach * @return string 804a25f0a04SGreg Roach */ 8058f53f488SRico Sonntag public function getBoxStyle(): string 806c1010edaSGreg Roach { 807c1010edaSGreg Roach $tmp = [ 808c1010edaSGreg Roach 'M' => '', 809c1010edaSGreg Roach 'F' => 'F', 810c1010edaSGreg Roach 'U' => 'NN', 811c1010edaSGreg Roach ]; 812a25f0a04SGreg Roach 813a25f0a04SGreg Roach return 'person_box' . $tmp[$this->getSex()]; 814a25f0a04SGreg Roach } 815a25f0a04SGreg Roach 816a25f0a04SGreg Roach /** 817a25f0a04SGreg Roach * Get a list of this individual’s spouse families 818a25f0a04SGreg Roach * 819cbc1590aSGreg Roach * @param int|null $access_level 820a25f0a04SGreg Roach * 821a25f0a04SGreg Roach * @return Family[] 822a25f0a04SGreg Roach */ 8238f53f488SRico Sonntag public function getSpouseFamilies($access_level = null): array 824c1010edaSGreg Roach { 8254b9ff166SGreg Roach if ($access_level === null) { 8264b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 8274b9ff166SGreg Roach } 8284b9ff166SGreg Roach 829acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 830a25f0a04SGreg Roach 83113abd6f3SGreg Roach $families = []; 8328d0ebef0SGreg Roach foreach ($this->facts(['FAMS'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) { 833dc124885SGreg Roach $family = $fact->target(); 834e24444eeSGreg Roach if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) { 835a25f0a04SGreg Roach $families[] = $family; 836a25f0a04SGreg Roach } 837a25f0a04SGreg Roach } 838a25f0a04SGreg Roach 839a25f0a04SGreg Roach return $families; 840a25f0a04SGreg Roach } 841a25f0a04SGreg Roach 842a25f0a04SGreg Roach /** 843a25f0a04SGreg Roach * Get the current spouse of this individual. 844a25f0a04SGreg Roach * 845a25f0a04SGreg Roach * Where an individual has multiple spouses, assume they are stored 846a25f0a04SGreg Roach * in chronological order, and take the last one found. 847a25f0a04SGreg Roach * 848a25f0a04SGreg Roach * @return Individual|null 849a25f0a04SGreg Roach */ 850c1010edaSGreg Roach public function getCurrentSpouse() 851c1010edaSGreg Roach { 852a25f0a04SGreg Roach $tmp = $this->getSpouseFamilies(); 853a25f0a04SGreg Roach $family = end($tmp); 854a25f0a04SGreg Roach if ($family) { 855a25f0a04SGreg Roach return $family->getSpouse($this); 856a25f0a04SGreg Roach } 857b2ce94c6SRico Sonntag 858b2ce94c6SRico Sonntag return null; 859a25f0a04SGreg Roach } 860a25f0a04SGreg Roach 861a25f0a04SGreg Roach /** 862a25f0a04SGreg Roach * Count the children belonging to this individual. 863a25f0a04SGreg Roach * 864cbc1590aSGreg Roach * @return int 865a25f0a04SGreg Roach */ 866c1010edaSGreg Roach public function getNumberOfChildren() 867c1010edaSGreg Roach { 8687d0db648SGreg Roach if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->gedcom(), $match)) { 8693dc7bbe9SGreg Roach return (int) $match[1]; 870b2ce94c6SRico Sonntag } 871b2ce94c6SRico Sonntag 87213abd6f3SGreg Roach $children = []; 873a25f0a04SGreg Roach foreach ($this->getSpouseFamilies() as $fam) { 874a25f0a04SGreg Roach foreach ($fam->getChildren() as $child) { 875c0935879SGreg Roach $children[$child->xref()] = true; 876a25f0a04SGreg Roach } 877a25f0a04SGreg Roach } 878a25f0a04SGreg Roach 879a25f0a04SGreg Roach return count($children); 880a25f0a04SGreg Roach } 881a25f0a04SGreg Roach 882a25f0a04SGreg Roach /** 883a25f0a04SGreg Roach * Get a list of this individual’s child families (i.e. their parents). 884a25f0a04SGreg Roach * 885cbc1590aSGreg Roach * @param int|null $access_level 886a25f0a04SGreg Roach * 887a25f0a04SGreg Roach * @return Family[] 888a25f0a04SGreg Roach */ 8898f53f488SRico Sonntag public function getChildFamilies($access_level = null): array 890c1010edaSGreg Roach { 8914b9ff166SGreg Roach if ($access_level === null) { 8924b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 8934b9ff166SGreg Roach } 8944b9ff166SGreg Roach 895acf76a54SGreg Roach $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 896a25f0a04SGreg Roach 89713abd6f3SGreg Roach $families = []; 8988d0ebef0SGreg Roach foreach ($this->facts(['FAMC'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) { 899dc124885SGreg Roach $family = $fact->target(); 900e24444eeSGreg Roach if ($family instanceof Family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) { 901a25f0a04SGreg Roach $families[] = $family; 902a25f0a04SGreg Roach } 903a25f0a04SGreg Roach } 904a25f0a04SGreg Roach 905a25f0a04SGreg Roach return $families; 906a25f0a04SGreg Roach } 907a25f0a04SGreg Roach 908a25f0a04SGreg Roach /** 909a25f0a04SGreg Roach * Get the preferred parents for this individual. 910a25f0a04SGreg Roach * 911a25f0a04SGreg Roach * An individual may multiple parents (e.g. birth, adopted, disputed). 912a25f0a04SGreg Roach * The preferred family record is: 913a25f0a04SGreg Roach * (a) the first one with an explicit tag "_PRIMARY Y" 914a25f0a04SGreg Roach * (b) the first one with a pedigree of "birth" 915a25f0a04SGreg Roach * (c) the first one with no pedigree (default is "birth") 916a25f0a04SGreg Roach * (d) the first one found 917a25f0a04SGreg Roach * 918a25f0a04SGreg Roach * @return Family|null 919a25f0a04SGreg Roach */ 920c1010edaSGreg Roach public function getPrimaryChildFamily() 921c1010edaSGreg Roach { 922a25f0a04SGreg Roach $families = $this->getChildFamilies(); 923a25f0a04SGreg Roach switch (count($families)) { 924a25f0a04SGreg Roach case 0: 925a25f0a04SGreg Roach return null; 926a25f0a04SGreg Roach case 1: 92715d603e7SGreg Roach return $families[0]; 928a25f0a04SGreg Roach default: 929a25f0a04SGreg Roach // If there is more than one FAMC record, choose the preferred parents: 930a25f0a04SGreg Roach // a) records with '2 _PRIMARY' 93174e78bfaSJonathan Jaubart foreach ($families as $fam) { 932c0935879SGreg Roach $famid = $fam->xref(); 9337d0db648SGreg Roach if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) { 934a25f0a04SGreg Roach return $fam; 935a25f0a04SGreg Roach } 936a25f0a04SGreg Roach } 937a25f0a04SGreg Roach // b) records with '2 PEDI birt' 93874e78bfaSJonathan Jaubart foreach ($families as $fam) { 939c0935879SGreg Roach $famid = $fam->xref(); 9407d0db648SGreg Roach if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) { 941a25f0a04SGreg Roach return $fam; 942a25f0a04SGreg Roach } 943a25f0a04SGreg Roach } 944a25f0a04SGreg Roach // c) records with no '2 PEDI' 94574e78bfaSJonathan Jaubart foreach ($families as $fam) { 946c0935879SGreg Roach $famid = $fam->xref(); 9477d0db648SGreg Roach if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) { 948a25f0a04SGreg Roach return $fam; 949a25f0a04SGreg Roach } 950a25f0a04SGreg Roach } 951a25f0a04SGreg Roach 952a25f0a04SGreg Roach // d) any record 95315d603e7SGreg Roach return $families[0]; 954a25f0a04SGreg Roach } 955a25f0a04SGreg Roach } 956a25f0a04SGreg Roach 957a25f0a04SGreg Roach /** 958a25f0a04SGreg Roach * Get a list of step-parent families. 959a25f0a04SGreg Roach * 960a25f0a04SGreg Roach * @return Family[] 961a25f0a04SGreg Roach */ 9628f53f488SRico Sonntag public function getChildStepFamilies(): array 963c1010edaSGreg Roach { 96413abd6f3SGreg Roach $step_families = []; 965a25f0a04SGreg Roach $families = $this->getChildFamilies(); 966a25f0a04SGreg Roach foreach ($families as $family) { 967a25f0a04SGreg Roach $father = $family->getHusband(); 968a25f0a04SGreg Roach if ($father) { 969a25f0a04SGreg Roach foreach ($father->getSpouseFamilies() as $step_family) { 970a25f0a04SGreg Roach if (!in_array($step_family, $families, true)) { 971a25f0a04SGreg Roach $step_families[] = $step_family; 972a25f0a04SGreg Roach } 973a25f0a04SGreg Roach } 974a25f0a04SGreg Roach } 975a25f0a04SGreg Roach $mother = $family->getWife(); 976a25f0a04SGreg Roach if ($mother) { 977a25f0a04SGreg Roach foreach ($mother->getSpouseFamilies() as $step_family) { 978a25f0a04SGreg Roach if (!in_array($step_family, $families, true)) { 979a25f0a04SGreg Roach $step_families[] = $step_family; 980a25f0a04SGreg Roach } 981a25f0a04SGreg Roach } 982a25f0a04SGreg Roach } 983a25f0a04SGreg Roach } 984a25f0a04SGreg Roach 985a25f0a04SGreg Roach return $step_families; 986a25f0a04SGreg Roach } 987a25f0a04SGreg Roach 988a25f0a04SGreg Roach /** 989a25f0a04SGreg Roach * Get a list of step-parent families. 990a25f0a04SGreg Roach * 991a25f0a04SGreg Roach * @return Family[] 992a25f0a04SGreg Roach */ 9938f53f488SRico Sonntag public function getSpouseStepFamilies(): array 994c1010edaSGreg Roach { 99513abd6f3SGreg Roach $step_families = []; 996a25f0a04SGreg Roach $families = $this->getSpouseFamilies(); 997a25f0a04SGreg Roach foreach ($families as $family) { 998a25f0a04SGreg Roach $spouse = $family->getSpouse($this); 999a25f0a04SGreg Roach if ($spouse) { 1000a25f0a04SGreg Roach foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) { 1001a25f0a04SGreg Roach if (!in_array($step_family, $families, true)) { 1002a25f0a04SGreg Roach $step_families[] = $step_family; 1003a25f0a04SGreg Roach } 1004a25f0a04SGreg Roach } 1005a25f0a04SGreg Roach } 1006a25f0a04SGreg Roach } 1007a25f0a04SGreg Roach 1008a25f0a04SGreg Roach return $step_families; 1009a25f0a04SGreg Roach } 1010a25f0a04SGreg Roach 1011a25f0a04SGreg Roach /** 1012a25f0a04SGreg Roach * A label for a parental family group 1013a25f0a04SGreg Roach * 1014a25f0a04SGreg Roach * @param Family $family 1015a25f0a04SGreg Roach * 1016a25f0a04SGreg Roach * @return string 1017a25f0a04SGreg Roach */ 1018c1010edaSGreg Roach public function getChildFamilyLabel(Family $family) 1019c1010edaSGreg Roach { 10207d0db648SGreg Roach if (preg_match('/\n1 FAMC @' . $family->xref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->gedcom(), $match)) { 1021a25f0a04SGreg Roach // A specified pedigree 1022764a01d9SGreg Roach return GedcomCodePedi::getChildFamilyLabel($match[1]); 1023b2ce94c6SRico Sonntag } 1024b2ce94c6SRico Sonntag 1025a25f0a04SGreg Roach // Default (birth) pedigree 1026764a01d9SGreg Roach return GedcomCodePedi::getChildFamilyLabel(''); 1027a25f0a04SGreg Roach } 1028a25f0a04SGreg Roach 1029a25f0a04SGreg Roach /** 1030a25f0a04SGreg Roach * Create a label for a step family 1031a25f0a04SGreg Roach * 1032a25f0a04SGreg Roach * @param Family $step_family 1033a25f0a04SGreg Roach * 1034a25f0a04SGreg Roach * @return string 1035a25f0a04SGreg Roach */ 10368f53f488SRico Sonntag public function getStepFamilyLabel(Family $step_family): string 1037c1010edaSGreg Roach { 1038a25f0a04SGreg Roach foreach ($this->getChildFamilies() as $family) { 1039a25f0a04SGreg Roach if ($family !== $step_family) { 1040a25f0a04SGreg Roach // Must be a step-family 1041a25f0a04SGreg Roach foreach ($family->getSpouses() as $parent) { 1042a25f0a04SGreg Roach foreach ($step_family->getSpouses() as $step_parent) { 1043a25f0a04SGreg Roach if ($parent === $step_parent) { 1044a25f0a04SGreg Roach // One common parent - must be a step family 1045a25f0a04SGreg Roach if ($parent->getSex() == 'M') { 1046a25f0a04SGreg Roach // Father’s family with someone else 1047a25f0a04SGreg Roach if ($step_family->getSpouse($step_parent)) { 1048a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 1049bbb76c12SGreg Roach return I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName()); 1050b2ce94c6SRico Sonntag } 1051b2ce94c6SRico Sonntag 1052a25f0a04SGreg Roach /* I18N: A step-family. */ 1053bbb76c12SGreg Roach return I18N::translate('Father’s family with an unknown individual'); 1054a25f0a04SGreg Roach } 1055b2ce94c6SRico Sonntag 1056a25f0a04SGreg Roach // Mother’s family with someone else 1057a25f0a04SGreg Roach if ($step_family->getSpouse($step_parent)) { 1058a25f0a04SGreg Roach /* I18N: A step-family. %s is an individual’s name */ 1059bbb76c12SGreg Roach return I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName()); 1060b2ce94c6SRico Sonntag } 1061b2ce94c6SRico Sonntag 1062a25f0a04SGreg Roach /* I18N: A step-family. */ 1063bbb76c12SGreg Roach return I18N::translate('Mother’s family with an unknown individual'); 1064a25f0a04SGreg Roach } 1065a25f0a04SGreg Roach } 1066a25f0a04SGreg Roach } 1067a25f0a04SGreg Roach } 1068a25f0a04SGreg Roach } 1069a25f0a04SGreg Roach 1070a25f0a04SGreg Roach // Perahps same parents - but a different family record? 1071a25f0a04SGreg Roach return I18N::translate('Family with parents'); 1072a25f0a04SGreg Roach } 1073a25f0a04SGreg Roach 1074225e381fSGreg Roach /** 1075225e381fSGreg Roach * Get the description for the family. 1076225e381fSGreg Roach * 1077225e381fSGreg Roach * For example, "XXX's family with new wife". 1078225e381fSGreg Roach * 1079225e381fSGreg Roach * @param Family $family 1080225e381fSGreg Roach * 1081225e381fSGreg Roach * @return string 1082225e381fSGreg Roach */ 1083c1010edaSGreg Roach public function getSpouseFamilyLabel(Family $family) 1084c1010edaSGreg Roach { 1085225e381fSGreg Roach $spouse = $family->getSpouse($this); 1086225e381fSGreg Roach if ($spouse) { 1087225e381fSGreg Roach /* I18N: %s is the spouse name */ 1088bbb76c12SGreg Roach return I18N::translate('Family with %s', $spouse->getFullName()); 1089225e381fSGreg Roach } 1090b2ce94c6SRico Sonntag 1091b2ce94c6SRico Sonntag return $family->getFullName(); 1092225e381fSGreg Roach } 1093225e381fSGreg Roach 1094a25f0a04SGreg Roach /** 1095a25f0a04SGreg Roach * get primary parents names for this individual 1096a25f0a04SGreg Roach * 1097a25f0a04SGreg Roach * @param string $classname optional css class 1098a25f0a04SGreg Roach * @param string $display optional css style display 1099a25f0a04SGreg Roach * 1100a25f0a04SGreg Roach * @return string a div block with father & mother names 1101a25f0a04SGreg Roach */ 11028f53f488SRico Sonntag public function getPrimaryParentsNames($classname = '', $display = ''): string 1103c1010edaSGreg Roach { 1104a25f0a04SGreg Roach $fam = $this->getPrimaryChildFamily(); 1105a25f0a04SGreg Roach if (!$fam) { 1106a25f0a04SGreg Roach return ''; 1107a25f0a04SGreg Roach } 1108a25f0a04SGreg Roach $txt = '<div'; 1109a25f0a04SGreg Roach if ($classname) { 11104c621133SGreg Roach $txt .= ' class="' . $classname . '"'; 1111a25f0a04SGreg Roach } 1112a25f0a04SGreg Roach if ($display) { 11134c621133SGreg Roach $txt .= ' style="display:' . $display . '"'; 1114a25f0a04SGreg Roach } 1115a25f0a04SGreg Roach $txt .= '>'; 1116a25f0a04SGreg Roach $husb = $fam->getHusband(); 1117a25f0a04SGreg Roach if ($husb) { 1118a25f0a04SGreg Roach // Temporarily reset the 'prefered' display name, as we always 1119a25f0a04SGreg Roach // want the default name, not the one selected for display on the indilist. 1120a25f0a04SGreg Roach $primary = $husb->getPrimaryName(); 1121a25f0a04SGreg Roach $husb->setPrimaryName(null); 1122a25f0a04SGreg Roach /* I18N: %s is the name of an individual’s father */ 1123bbb76c12SGreg Roach $txt .= I18N::translate('Father: %s', $husb->getFullName()) . '<br>'; 1124a25f0a04SGreg Roach $husb->setPrimaryName($primary); 1125a25f0a04SGreg Roach } 1126a25f0a04SGreg Roach $wife = $fam->getWife(); 1127a25f0a04SGreg Roach if ($wife) { 1128a25f0a04SGreg Roach // Temporarily reset the 'prefered' display name, as we always 1129a25f0a04SGreg Roach // want the default name, not the one selected for display on the indilist. 1130a25f0a04SGreg Roach $primary = $wife->getPrimaryName(); 1131a25f0a04SGreg Roach $wife->setPrimaryName(null); 1132a25f0a04SGreg Roach /* I18N: %s is the name of an individual’s mother */ 1133bbb76c12SGreg Roach $txt .= I18N::translate('Mother: %s', $wife->getFullName()); 1134a25f0a04SGreg Roach $wife->setPrimaryName($primary); 1135a25f0a04SGreg Roach } 1136a25f0a04SGreg Roach $txt .= '</div>'; 1137a25f0a04SGreg Roach 1138a25f0a04SGreg Roach return $txt; 1139a25f0a04SGreg Roach } 1140a25f0a04SGreg Roach 1141a25f0a04SGreg Roach /** {@inheritdoc} */ 11428f53f488SRico Sonntag public function getFallBackName(): string 1143c1010edaSGreg Roach { 1144a25f0a04SGreg Roach return '@P.N. /@N.N./'; 1145a25f0a04SGreg Roach } 1146a25f0a04SGreg Roach 1147a25f0a04SGreg Roach /** 1148a25f0a04SGreg Roach * Convert a name record into ‘full’ and ‘sort’ versions. 1149a25f0a04SGreg Roach * Use the NAME field to generate the ‘full’ version, as the 1150a25f0a04SGreg Roach * gedcom spec says that this is the individual’s name, as they would write it. 1151a25f0a04SGreg Roach * Use the SURN field to generate the sortable names. Note that this field 1152a25f0a04SGreg Roach * may also be used for the ‘true’ surname, perhaps spelt differently to that 1153a25f0a04SGreg Roach * recorded in the NAME field. e.g. 1154a25f0a04SGreg Roach * 1155a25f0a04SGreg Roach * 1 NAME Robert /de Gliderow/ 1156a25f0a04SGreg Roach * 2 GIVN Robert 1157a25f0a04SGreg Roach * 2 SPFX de 1158a25f0a04SGreg Roach * 2 SURN CLITHEROW 1159a25f0a04SGreg Roach * 2 NICK The Bald 1160a25f0a04SGreg Roach * 1161a25f0a04SGreg Roach * full=>'Robert de Gliderow 'The Bald'' 1162a25f0a04SGreg Roach * sort=>'CLITHEROW, ROBERT' 1163a25f0a04SGreg Roach * 1164a25f0a04SGreg Roach * Handle multiple surnames, either as; 1165a25f0a04SGreg Roach * 1166a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez/ y /Sante/ 1167a25f0a04SGreg Roach * or 1168a25f0a04SGreg Roach * 1 NAME Carlos /Vasquez y Sante/ 1169a25f0a04SGreg Roach * 2 GIVN Carlos 1170a25f0a04SGreg Roach * 2 SURN Vasquez,Sante 1171a25f0a04SGreg Roach * 1172a25f0a04SGreg Roach * @param string $type 1173a25f0a04SGreg Roach * @param string $full 1174a25f0a04SGreg Roach * @param string $gedcom 1175a25f0a04SGreg Roach */ 117676f666f4SGreg Roach protected function addName(string $type, string $full, string $gedcom) 1177c1010edaSGreg Roach { 1178a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1179a25f0a04SGreg Roach // Extract the structured name parts - use for "sortable" names and indexes 1180a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1181a25f0a04SGreg Roach 118276f666f4SGreg Roach $sublevel = 1 + (int) substr($gedcom, 0, 1); 1183a25f0a04SGreg Roach $GIVN = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : ''; 1184a25f0a04SGreg Roach $SURN = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : ''; 1185a25f0a04SGreg Roach $NICK = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : ''; 1186a25f0a04SGreg Roach 1187a25f0a04SGreg Roach // SURN is an comma-separated list of surnames... 118876f666f4SGreg Roach if ($SURN !== '') { 1189a25f0a04SGreg Roach $SURNS = preg_split('/ *, */', $SURN); 1190a25f0a04SGreg Roach } else { 119113abd6f3SGreg Roach $SURNS = []; 1192a25f0a04SGreg Roach } 119376f666f4SGreg Roach 1194a25f0a04SGreg Roach // ...so is GIVN - but nobody uses it like that 1195a25f0a04SGreg Roach $GIVN = str_replace('/ *, */', ' ', $GIVN); 1196a25f0a04SGreg Roach 1197a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1198a25f0a04SGreg Roach // Extract the components from NAME - use for the "full" names 1199a25f0a04SGreg Roach //////////////////////////////////////////////////////////////////////////// 1200a25f0a04SGreg Roach 1201a25f0a04SGreg Roach // Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/' 120276f666f4SGreg Roach if (substr_count($full, '/') % 2 === 1) { 1203a25f0a04SGreg Roach $full = $full . '/'; 1204a25f0a04SGreg Roach } 1205a25f0a04SGreg Roach 1206a25f0a04SGreg Roach // GEDCOM uses "//" to indicate an unknown surname 1207a25f0a04SGreg Roach $full = preg_replace('/\/\//', '/@N.N./', $full); 1208a25f0a04SGreg Roach 1209a25f0a04SGreg Roach // Extract the surname. 1210a25f0a04SGreg Roach // Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/ 1211a25f0a04SGreg Roach if (preg_match('/\/.*\//', $full, $match)) { 1212a25f0a04SGreg Roach $surname = str_replace('/', '', $match[0]); 1213a25f0a04SGreg Roach } else { 1214a25f0a04SGreg Roach $surname = ''; 1215a25f0a04SGreg Roach } 1216a25f0a04SGreg Roach 1217a25f0a04SGreg Roach // If we don’t have a SURN record, extract it from the NAME 1218a25f0a04SGreg Roach if (!$SURNS) { 1219a25f0a04SGreg Roach if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) { 1220a25f0a04SGreg Roach // There can be many surnames, each wrapped with '/' 1221a25f0a04SGreg Roach $SURNS = $matches[1]; 1222a25f0a04SGreg Roach foreach ($SURNS as $n => $SURN) { 1223a25f0a04SGreg Roach // Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only) 1224a25f0a04SGreg Roach $SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN); 1225a25f0a04SGreg Roach } 1226a25f0a04SGreg Roach } else { 1227a25f0a04SGreg Roach // It is valid not to have a surname at all 122813abd6f3SGreg Roach $SURNS = ['']; 1229a25f0a04SGreg Roach } 1230a25f0a04SGreg Roach } 1231a25f0a04SGreg Roach 1232a25f0a04SGreg Roach // If we don’t have a GIVN record, extract it from the NAME 1233a25f0a04SGreg Roach if (!$GIVN) { 1234a25f0a04SGreg Roach $GIVN = preg_replace( 123513abd6f3SGreg Roach [ 1236c1010edaSGreg Roach '/ ?\/.*\/ ?/', 1237c1010edaSGreg Roach // remove surname 1238c1010edaSGreg Roach '/ ?".+"/', 1239c1010edaSGreg Roach // remove nickname 1240c1010edaSGreg Roach '/ {2,}/', 1241c1010edaSGreg Roach // multiple spaces, caused by the above 1242c1010edaSGreg Roach '/^ | $/', 1243c1010edaSGreg Roach // leading/trailing spaces, caused by the above 124413abd6f3SGreg Roach ], 124513abd6f3SGreg Roach [ 1246a25f0a04SGreg Roach ' ', 1247a25f0a04SGreg Roach ' ', 1248a25f0a04SGreg Roach ' ', 1249a25f0a04SGreg Roach '', 125013abd6f3SGreg Roach ], 1251a25f0a04SGreg Roach $full 1252a25f0a04SGreg Roach ); 1253a25f0a04SGreg Roach } 1254a25f0a04SGreg Roach 1255a25f0a04SGreg Roach // Add placeholder for unknown given name 1256a25f0a04SGreg Roach if (!$GIVN) { 1257a25f0a04SGreg Roach $GIVN = '@P.N.'; 125873f4f553SGreg Roach $pos = (int) strpos($full, '/'); 1259a25f0a04SGreg Roach $full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos); 1260a25f0a04SGreg Roach } 1261a25f0a04SGreg Roach 12627b0e71c3SGreg Roach // GEDCOM 5.5.1 nicknames should be specificied in a NICK field 12637b0e71c3SGreg Roach // GEDCOM 5.5 nicknames should be specified in the NAME field, surrounded by quotes 1264c6f196c3SGreg Roach if ($NICK && strpos($full, '"' . $NICK . '"') === false) { 12657b0e71c3SGreg Roach // A NICK field is present, but not included in the NAME. Show it at the end. 1266c6f196c3SGreg Roach $full .= ' "' . $NICK . '"'; 1267a25f0a04SGreg Roach } 1268a25f0a04SGreg Roach 1269a25f0a04SGreg Roach // Remove slashes - they don’t get displayed 1270a25f0a04SGreg Roach // $fullNN keeps the @N.N. placeholders, for the database 1271a25f0a04SGreg Roach // $full is for display on-screen 1272a25f0a04SGreg Roach $fullNN = str_replace('/', '', $full); 1273a25f0a04SGreg Roach 1274a25f0a04SGreg Roach // Insert placeholders for any missing/unknown names 1275ad1a1cd2SGreg Roach $full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full); 1276ad1a1cd2SGreg Roach $full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full); 1277c6f196c3SGreg Roach // Format for display 1278d53324c9SGreg Roach $full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>'; 1279acc34ea1SGreg Roach // Localise quotation marks around the nickname 128018d7a90dSGreg Roach $full = preg_replace_callback('/"([^&]*)"/', function (array $matches): string { 12818d68cabeSGreg Roach return I18N::translate('“%s”', $matches[1]); 12828d68cabeSGreg Roach }, $full); 1283a25f0a04SGreg Roach 1284c6f196c3SGreg Roach // A suffix of “*” indicates a preferred name 1285a25f0a04SGreg Roach $full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full); 1286a25f0a04SGreg Roach 1287a25f0a04SGreg Roach // Remove prefered-name indicater - they don’t go in the database 1288a25f0a04SGreg Roach $GIVN = str_replace('*', '', $GIVN); 1289a25f0a04SGreg Roach $fullNN = str_replace('*', '', $fullNN); 1290a25f0a04SGreg Roach 1291ffd703eaSGreg Roach foreach ($SURNS as $SURN) { 1292a25f0a04SGreg Roach // Scottish 'Mc and Mac ' prefixes both sort under 'Mac' 1293a25f0a04SGreg Roach if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) { 1294a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 2); 1295a25f0a04SGreg Roach } elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) { 1296a25f0a04SGreg Roach $SURN = substr_replace($SURN, 'Mac', 0, 4); 1297a25f0a04SGreg Roach } 1298a25f0a04SGreg Roach 1299bdb3725aSGreg Roach $this->getAllNames[] = [ 1300a25f0a04SGreg Roach 'type' => $type, 1301a25f0a04SGreg Roach 'sort' => $SURN . ',' . $GIVN, 1302c1010edaSGreg Roach 'full' => $full, 1303c1010edaSGreg Roach // This is used for display 1304c1010edaSGreg Roach 'fullNN' => $fullNN, 1305c1010edaSGreg Roach // This goes into the database 1306c1010edaSGreg Roach 'surname' => $surname, 1307c1010edaSGreg Roach // This goes into the database 1308c1010edaSGreg Roach 'givn' => $GIVN, 1309c1010edaSGreg Roach // This goes into the database 1310c1010edaSGreg Roach 'surn' => $SURN, 1311c1010edaSGreg Roach // This goes into the database 131213abd6f3SGreg Roach ]; 1313a25f0a04SGreg Roach } 1314a25f0a04SGreg Roach } 1315a25f0a04SGreg Roach 1316a25f0a04SGreg Roach /** 131776692c8bSGreg Roach * Extract names from the GEDCOM record. 1318c7ff4153SGreg Roach * 1319c7ff4153SGreg Roach * @return void 1320a25f0a04SGreg Roach */ 1321c1010edaSGreg Roach public function extractNames() 1322c1010edaSGreg Roach { 13238f53f488SRico Sonntag $this->extractNamesFromFacts( 13248f53f488SRico Sonntag 1, 13258f53f488SRico Sonntag 'NAME', 132630158ae7SGreg Roach $this->facts( 13278d0ebef0SGreg Roach ['NAME'], 13288f53f488SRico Sonntag false, 13298f53f488SRico Sonntag Auth::accessLevel($this->tree), 13308f53f488SRico Sonntag $this->canShowName() 13318f53f488SRico Sonntag ) 13328f53f488SRico Sonntag ); 1333a25f0a04SGreg Roach } 1334a25f0a04SGreg Roach 1335a25f0a04SGreg Roach /** 1336a25f0a04SGreg Roach * Extra info to display when displaying this record in a list of 1337a25f0a04SGreg Roach * selection items or favorites. 1338a25f0a04SGreg Roach * 1339a25f0a04SGreg Roach * @return string 1340a25f0a04SGreg Roach */ 13418f53f488SRico Sonntag public function formatListDetails(): string 1342c1010edaSGreg Roach { 1343a25f0a04SGreg Roach return 13448d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::BIRTH_EVENTS, 1) . 13458d0ebef0SGreg Roach $this->formatFirstMajorFact(Gedcom::DEATH_EVENTS, 1); 1346a25f0a04SGreg Roach } 1347a25f0a04SGreg Roach} 1348