1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 2176692c8bSGreg Roach 22580a4d11SGreg Roachuse Closure; 2355105892SGreg Roachuse Fisharebest\Webtrees\Elements\RestrictionNotice; 248af6bbf8SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService; 251c7a6874SGreg Roachuse Illuminate\Support\Collection; 268f5f5da8SGreg Roachuse InvalidArgumentException; 273976b470SGreg Roach 28ae71f733SGreg Roachuse function array_flip; 29ae71f733SGreg Roachuse function array_key_exists; 30ae71f733SGreg Roachuse function count; 31ae71f733SGreg Roachuse function e; 32ae71f733SGreg Roachuse function implode; 33ae71f733SGreg Roachuse function in_array; 34ae71f733SGreg Roachuse function preg_match; 35ae71f733SGreg Roachuse function preg_match_all; 36ae71f733SGreg Roachuse function preg_replace; 37dec352c1SGreg Roachuse function str_contains; 3855105892SGreg Roachuse function str_ends_with; 39ae71f733SGreg Roachuse function usort; 40ae71f733SGreg Roach 41ae71f733SGreg Roachuse const PREG_SET_ORDER; 42a25f0a04SGreg Roach 43a25f0a04SGreg Roach/** 4476692c8bSGreg Roach * A GEDCOM fact or event object. 45a25f0a04SGreg Roach */ 46c1010edaSGreg Roachclass Fact 47c1010edaSGreg Roach{ 4816d6367aSGreg Roach private const FACT_ORDER = [ 49bbe4546fSGreg Roach 'BIRT', 50bbe4546fSGreg Roach '_HNM', 51bbe4546fSGreg Roach 'ALIA', 52bbe4546fSGreg Roach '_AKA', 53bbe4546fSGreg Roach '_AKAN', 54bbe4546fSGreg Roach 'ADOP', 55bbe4546fSGreg Roach '_ADPF', 56bbe4546fSGreg Roach '_ADPF', 57bbe4546fSGreg Roach '_BRTM', 58bbe4546fSGreg Roach 'CHR', 59bbe4546fSGreg Roach 'BAPM', 60bbe4546fSGreg Roach 'FCOM', 61bbe4546fSGreg Roach 'CONF', 62bbe4546fSGreg Roach 'BARM', 63bbe4546fSGreg Roach 'BASM', 64bbe4546fSGreg Roach 'EDUC', 65bbe4546fSGreg Roach 'GRAD', 66bbe4546fSGreg Roach '_DEG', 67bbe4546fSGreg Roach 'EMIG', 68bbe4546fSGreg Roach 'IMMI', 69bbe4546fSGreg Roach 'NATU', 70bbe4546fSGreg Roach '_MILI', 71bbe4546fSGreg Roach '_MILT', 72bbe4546fSGreg Roach 'ENGA', 73bbe4546fSGreg Roach 'MARB', 74bbe4546fSGreg Roach 'MARC', 75bbe4546fSGreg Roach 'MARL', 76bbe4546fSGreg Roach '_MARI', 77bbe4546fSGreg Roach '_MBON', 78bbe4546fSGreg Roach 'MARR', 79bbe4546fSGreg Roach '_COML', 80bbe4546fSGreg Roach '_STAT', 81bbe4546fSGreg Roach '_SEPR', 82bbe4546fSGreg Roach 'DIVF', 83bbe4546fSGreg Roach 'MARS', 84bbe4546fSGreg Roach 'DIV', 85bbe4546fSGreg Roach 'ANUL', 86bbe4546fSGreg Roach 'CENS', 87bbe4546fSGreg Roach 'OCCU', 88bbe4546fSGreg Roach 'RESI', 89bbe4546fSGreg Roach 'PROP', 90bbe4546fSGreg Roach 'CHRA', 91bbe4546fSGreg Roach 'RETI', 92bbe4546fSGreg Roach 'FACT', 93bbe4546fSGreg Roach 'EVEN', 94bbe4546fSGreg Roach '_NMR', 95bbe4546fSGreg Roach '_NMAR', 96bbe4546fSGreg Roach 'NMR', 97bbe4546fSGreg Roach 'NCHI', 98bbe4546fSGreg Roach 'WILL', 99bbe4546fSGreg Roach '_HOL', 100bbe4546fSGreg Roach '_????_', 101bbe4546fSGreg Roach 'DEAT', 102bbe4546fSGreg Roach '_FNRL', 103bbe4546fSGreg Roach 'CREM', 104bbe4546fSGreg Roach 'BURI', 105bbe4546fSGreg Roach '_INTE', 106bbe4546fSGreg Roach '_YART', 107bbe4546fSGreg Roach '_NLIV', 108bbe4546fSGreg Roach 'PROB', 109bbe4546fSGreg Roach 'TITL', 110bbe4546fSGreg Roach 'COMM', 111bbe4546fSGreg Roach 'NATI', 112bbe4546fSGreg Roach 'CITN', 113bbe4546fSGreg Roach 'CAST', 114bbe4546fSGreg Roach 'RELI', 115bbe4546fSGreg Roach 'SSN', 116bbe4546fSGreg Roach 'IDNO', 117bbe4546fSGreg Roach 'TEMP', 118bbe4546fSGreg Roach 'SLGC', 119bbe4546fSGreg Roach 'BAPL', 120bbe4546fSGreg Roach 'CONL', 121bbe4546fSGreg Roach 'ENDL', 122bbe4546fSGreg Roach 'SLGS', 123*701f5d18SGreg Roach 'NO', 124bbe4546fSGreg Roach 'ADDR', 125bbe4546fSGreg Roach 'PHON', 126bbe4546fSGreg Roach 'EMAIL', 127bbe4546fSGreg Roach '_EMAIL', 128bbe4546fSGreg Roach 'EMAL', 129bbe4546fSGreg Roach 'FAX', 130bbe4546fSGreg Roach 'WWW', 131bbe4546fSGreg Roach 'URL', 132bbe4546fSGreg Roach '_URL', 133bbe4546fSGreg Roach 'AFN', 134bbe4546fSGreg Roach 'REFN', 135bbe4546fSGreg Roach '_PRMN', 136bbe4546fSGreg Roach 'REF', 137bbe4546fSGreg Roach 'RIN', 138bbe4546fSGreg Roach '_UID', 139bbe4546fSGreg Roach 'OBJE', 140bbe4546fSGreg Roach 'NOTE', 141bbe4546fSGreg Roach 'SOUR', 142*701f5d18SGreg Roach 'CREA', 143bbe4546fSGreg Roach 'CHAN', 144bbe4546fSGreg Roach '_TODO', 145bbe4546fSGreg Roach ]; 146bbe4546fSGreg Roach 14733c746f1SGreg Roach // Unique identifier for this fact (currently implemented as a hash of the raw data). 14833c746f1SGreg Roach private string $id; 149a25f0a04SGreg Roach 15033c746f1SGreg Roach // The GEDCOM record from which this fact is taken 15133c746f1SGreg Roach private GedcomRecord $record; 152a25f0a04SGreg Roach 15333c746f1SGreg Roach // The raw GEDCOM data for this fact 15433c746f1SGreg Roach private string $gedcom; 155a25f0a04SGreg Roach 15633c746f1SGreg Roach // The GEDCOM tag for this record 15733c746f1SGreg Roach private string $tag; 158a25f0a04SGreg Roach 15933c746f1SGreg Roach private bool $pending_deletion = false; 160a25f0a04SGreg Roach 16133c746f1SGreg Roach private bool $pending_addition = false; 162a25f0a04SGreg Roach 16333c746f1SGreg Roach private Date $date; 164a25f0a04SGreg Roach 16533c746f1SGreg Roach private Place $place; 166a25f0a04SGreg Roach 167bfed30e4SGreg Roach // Used to sort facts 16833c746f1SGreg Roach public int $sortOrder; 169a25f0a04SGreg Roach 170bfed30e4SGreg Roach // Used by anniversary calculations 171bfed30e4SGreg Roach public int $jd; 172bfed30e4SGreg Roach public int $anniv; 173bfed30e4SGreg Roach 174a25f0a04SGreg Roach /** 175a25f0a04SGreg Roach * Create an event object from a gedcom fragment. 176a25f0a04SGreg Roach * We need the parent object (to check privacy) and a (pseudo) fact ID to 177a25f0a04SGreg Roach * identify the fact within the record. 178a25f0a04SGreg Roach * 179a25f0a04SGreg Roach * @param string $gedcom 180a25f0a04SGreg Roach * @param GedcomRecord $parent 181905ab80aSGreg Roach * @param string $id 182a25f0a04SGreg Roach * 1838f5f5da8SGreg Roach * @throws InvalidArgumentException 184a25f0a04SGreg Roach */ 18524f2a3afSGreg Roach public function __construct(string $gedcom, GedcomRecord $parent, string $id) 186c1010edaSGreg Roach { 1878d0ebef0SGreg Roach if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) { 188a25f0a04SGreg Roach $this->gedcom = $gedcom; 189e7766c08SGreg Roach $this->record = $parent; 190905ab80aSGreg Roach $this->id = $id; 191a25f0a04SGreg Roach $this->tag = $match[1]; 192a25f0a04SGreg Roach } else { 19352ac7b12SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ',' . $parent->xref() . ')'); 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach 197a25f0a04SGreg Roach /** 198a25f0a04SGreg Roach * Get the value of level 1 data in the fact 199a25f0a04SGreg Roach * Allow for multi-line values 200a25f0a04SGreg Roach * 201baacc364SGreg Roach * @return string 202a25f0a04SGreg Roach */ 203dfa848b8SGreg Roach public function value(): string 204c1010edaSGreg Roach { 205d247596dSGreg Roach if (preg_match('/^1 ' . $this->tag . ' ?(.*(?:\n2 CONT ?.*)*)/', $this->gedcom, $match)) { 206a25f0a04SGreg Roach return preg_replace("/\n2 CONT ?/", "\n", $match[1]); 207a25f0a04SGreg Roach } 208b2ce94c6SRico Sonntag 209b2ce94c6SRico Sonntag return ''; 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach 212a25f0a04SGreg Roach /** 213a25f0a04SGreg Roach * Get the record to which this fact links 214a25f0a04SGreg Roach * 2150d15532eSGreg Roach * @return Family|GedcomRecord|Individual|Location|Media|Note|Repository|Source|Submission|Submitter|null 216a25f0a04SGreg Roach */ 217dc124885SGreg Roach public function target() 218c1010edaSGreg Roach { 21962b52849SGreg Roach if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) { 22062b52849SGreg Roach return null; 22162b52849SGreg Roach } 22262b52849SGreg Roach 22362b52849SGreg Roach $xref = $match[1]; 22462b52849SGreg Roach 225a25f0a04SGreg Roach switch ($this->tag) { 226a25f0a04SGreg Roach case 'FAMC': 227a25f0a04SGreg Roach case 'FAMS': 2286b9cb339SGreg Roach return Registry::familyFactory()->make($xref, $this->record()->tree()); 229a25f0a04SGreg Roach case 'HUSB': 230a25f0a04SGreg Roach case 'WIFE': 23162b52849SGreg Roach case 'ALIA': 232a25f0a04SGreg Roach case 'CHIL': 23362b52849SGreg Roach case '_ASSO': 2346b9cb339SGreg Roach return Registry::individualFactory()->make($xref, $this->record()->tree()); 23562b52849SGreg Roach case 'ASSO': 23662b52849SGreg Roach return 2376b9cb339SGreg Roach Registry::individualFactory()->make($xref, $this->record()->tree()) ?? 2386b9cb339SGreg Roach Registry::submitterFactory()->make($xref, $this->record()->tree()); 239a25f0a04SGreg Roach case 'SOUR': 2406b9cb339SGreg Roach return Registry::sourceFactory()->make($xref, $this->record()->tree()); 241a25f0a04SGreg Roach case 'OBJE': 2426b9cb339SGreg Roach return Registry::mediaFactory()->make($xref, $this->record()->tree()); 243a25f0a04SGreg Roach case 'REPO': 2446b9cb339SGreg Roach return Registry::repositoryFactory()->make($xref, $this->record()->tree()); 245a25f0a04SGreg Roach case 'NOTE': 2466b9cb339SGreg Roach return Registry::noteFactory()->make($xref, $this->record()->tree()); 24762b52849SGreg Roach case 'ANCI': 24862b52849SGreg Roach case 'DESI': 249ffc0a61fSGreg Roach case 'SUBM': 2506b9cb339SGreg Roach return Registry::submitterFactory()->make($xref, $this->record()->tree()); 2511635452cSGreg Roach case 'SUBN': 2526b9cb339SGreg Roach return Registry::submissionFactory()->make($xref, $this->record()->tree()); 2530d15532eSGreg Roach case '_LOC': 2546b9cb339SGreg Roach return Registry::locationFactory()->make($xref, $this->record()->tree()); 255a25f0a04SGreg Roach default: 2566b9cb339SGreg Roach return Registry::gedcomRecordFactory()->make($xref, $this->record()->tree()); 257a25f0a04SGreg Roach } 258a25f0a04SGreg Roach } 259a25f0a04SGreg Roach 260a25f0a04SGreg Roach /** 261a25f0a04SGreg Roach * Get the value of level 2 data in the fact 262a25f0a04SGreg Roach * 263a25f0a04SGreg Roach * @param string $tag 264a25f0a04SGreg Roach * 265baacc364SGreg Roach * @return string 266a25f0a04SGreg Roach */ 26724f2a3afSGreg Roach public function attribute(string $tag): string 268c1010edaSGreg Roach { 26910d6d973SGreg Roach if (preg_match('/\n2 ' . $tag . '\b ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { 27049985628SGreg Roach $value = preg_replace("/\n3 CONT ?/", "\n", $match[1]); 27149985628SGreg Roach 2728939e2c2SGreg Roach return Registry::elementFactory()->make($this->tag() . ':' . $tag)->canonical($value); 273a25f0a04SGreg Roach } 274b2ce94c6SRico Sonntag 275b2ce94c6SRico Sonntag return ''; 276a25f0a04SGreg Roach } 277a25f0a04SGreg Roach 278a25f0a04SGreg Roach /** 2798af6bbf8SGreg Roach * Get the PLAC:MAP:LATI for the fact. 2808af6bbf8SGreg Roach * 28190949315SGreg Roach * @return float|null 2828af6bbf8SGreg Roach */ 28390949315SGreg Roach public function latitude(): ?float 2848af6bbf8SGreg Roach { 2858af6bbf8SGreg Roach if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) { 2868af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2878af6bbf8SGreg Roach 2888af6bbf8SGreg Roach return $gedcom_service->readLatitude($match[1]); 2898af6bbf8SGreg Roach } 2908af6bbf8SGreg Roach 2910ec592a7SDavid Drury return null; 2928af6bbf8SGreg Roach } 2938af6bbf8SGreg Roach 2948af6bbf8SGreg Roach /** 2958af6bbf8SGreg Roach * Get the PLAC:MAP:LONG for the fact. 2968af6bbf8SGreg Roach * 29790949315SGreg Roach * @return float|null 2988af6bbf8SGreg Roach */ 29990949315SGreg Roach public function longitude(): ?float 3008af6bbf8SGreg Roach { 301819ac503SRico Sonntag if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) { 3028af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 3038af6bbf8SGreg Roach 3048af6bbf8SGreg Roach return $gedcom_service->readLongitude($match[1]); 3058af6bbf8SGreg Roach } 3068af6bbf8SGreg Roach 3070ec592a7SDavid Drury return null; 3088af6bbf8SGreg Roach } 3098af6bbf8SGreg Roach 3108af6bbf8SGreg Roach /** 311a25f0a04SGreg Roach * Do the privacy rules allow us to display this fact to the current user 312a25f0a04SGreg Roach * 313cbc1590aSGreg Roach * @param int|null $access_level 314a25f0a04SGreg Roach * 315cbc1590aSGreg Roach * @return bool 316a25f0a04SGreg Roach */ 31735584196SGreg Roach public function canShow(int $access_level = null): bool 318c1010edaSGreg Roach { 319d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->record->tree()); 3204b9ff166SGreg Roach 32155105892SGreg Roach // Does this record have an explicit restriction notice? 32255105892SGreg Roach $restriction = $this->attribute('RESN'); 32355105892SGreg Roach 32455105892SGreg Roach if (str_ends_with($restriction, RestrictionNotice::VALUE_CONFIDENTIAL)) { 3254b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 326a25f0a04SGreg Roach } 32755105892SGreg Roach 32855105892SGreg Roach if (str_ends_with($restriction, RestrictionNotice::VALUE_PRIVACY)) { 3294b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 330a25f0a04SGreg Roach } 33155105892SGreg Roach if (str_ends_with($restriction, RestrictionNotice::VALUE_NONE)) { 332a25f0a04SGreg Roach return true; 333a25f0a04SGreg Roach } 334a25f0a04SGreg Roach 335b0a785fcSGreg Roach // A link to a record of the same type: NOTE=>NOTE, OBJE=>OBJE, SOUR=>SOUR, etc. 336b0a785fcSGreg Roach // Use the privacy of the target record. 337b0a785fcSGreg Roach $target = $this->target(); 338b0a785fcSGreg Roach 339b0a785fcSGreg Roach if ($target instanceof GedcomRecord && $target->tag() === $this->tag) { 340b0a785fcSGreg Roach return $target->canShow($access_level); 341b0a785fcSGreg Roach } 342b0a785fcSGreg Roach 343a25f0a04SGreg Roach // Does this record have a default RESN? 344c0935879SGreg Roach $xref = $this->record->xref(); 345f4afa648SGreg Roach $fact_privacy = $this->record->tree()->getFactPrivacy(); 346f4afa648SGreg Roach $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy(); 347518bbdc1SGreg Roach if (isset($individual_fact_privacy[$xref][$this->tag])) { 348518bbdc1SGreg Roach return $individual_fact_privacy[$xref][$this->tag] >= $access_level; 349a25f0a04SGreg Roach } 350518bbdc1SGreg Roach if (isset($fact_privacy[$this->tag])) { 351518bbdc1SGreg Roach return $fact_privacy[$this->tag] >= $access_level; 352a25f0a04SGreg Roach } 353a25f0a04SGreg Roach 354a25f0a04SGreg Roach // No restrictions - it must be public 355a25f0a04SGreg Roach return true; 356a25f0a04SGreg Roach } 357a25f0a04SGreg Roach 358a25f0a04SGreg Roach /** 359a25f0a04SGreg Roach * Check whether this fact is protected against edit 360a25f0a04SGreg Roach * 361cbc1590aSGreg Roach * @return bool 362a25f0a04SGreg Roach */ 3638f53f488SRico Sonntag public function canEdit(): bool 364c1010edaSGreg Roach { 3651450f098SGreg Roach if ($this->isPendingDeletion()) { 3661450f098SGreg Roach return false; 3671450f098SGreg Roach } 3681450f098SGreg Roach 3691450f098SGreg Roach if (Auth::isManager($this->record->tree())) { 3701450f098SGreg Roach return true; 3711450f098SGreg Roach } 3721450f098SGreg Roach 373a25f0a04SGreg Roach // Members cannot edit RESN, CHAN and locked records 37455105892SGreg Roach return Auth::isEditor($this->record->tree()) && !str_ends_with($this->attribute('RESN'), RestrictionNotice::VALUE_LOCKED) && $this->tag !== 'RESN' && $this->tag !== 'CHAN'; 375a25f0a04SGreg Roach } 376a25f0a04SGreg Roach 377a25f0a04SGreg Roach /** 378a25f0a04SGreg Roach * The place where the event occured. 379a25f0a04SGreg Roach * 380a25f0a04SGreg Roach * @return Place 381a25f0a04SGreg Roach */ 3824fb14fcbSGreg Roach public function place(): Place 383c1010edaSGreg Roach { 384448d466cSGreg Roach $this->place ??= new Place($this->attribute('PLAC'), $this->record()->tree()); 385a25f0a04SGreg Roach 386a25f0a04SGreg Roach return $this->place; 387a25f0a04SGreg Roach } 388a25f0a04SGreg Roach 389a25f0a04SGreg Roach /** 390a25f0a04SGreg Roach * Get the date for this fact. 391a25f0a04SGreg Roach * We can call this function many times, especially when sorting, 392a25f0a04SGreg Roach * so keep a copy of the date. 393a25f0a04SGreg Roach * 394a25f0a04SGreg Roach * @return Date 395a25f0a04SGreg Roach */ 3962decada7SGreg Roach public function date(): Date 397c1010edaSGreg Roach { 398448d466cSGreg Roach $this->date ??= new Date($this->attribute('DATE')); 399a25f0a04SGreg Roach 400a25f0a04SGreg Roach return $this->date; 401a25f0a04SGreg Roach } 402a25f0a04SGreg Roach 403a25f0a04SGreg Roach /** 404a25f0a04SGreg Roach * The raw GEDCOM data for this fact 405a25f0a04SGreg Roach * 406a25f0a04SGreg Roach * @return string 407a25f0a04SGreg Roach */ 408138ca96cSGreg Roach public function gedcom(): string 409c1010edaSGreg Roach { 410a25f0a04SGreg Roach return $this->gedcom; 411a25f0a04SGreg Roach } 412a25f0a04SGreg Roach 413a25f0a04SGreg Roach /** 414a25f0a04SGreg Roach * Get a (pseudo) primary key for this fact. 415a25f0a04SGreg Roach * 416a25f0a04SGreg Roach * @return string 417a25f0a04SGreg Roach */ 418905ab80aSGreg Roach public function id(): string 419c1010edaSGreg Roach { 420905ab80aSGreg Roach return $this->id; 421a25f0a04SGreg Roach } 422a25f0a04SGreg Roach 423a25f0a04SGreg Roach /** 424a25f0a04SGreg Roach * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 425a25f0a04SGreg Roach * 426a25f0a04SGreg Roach * @return string 427a25f0a04SGreg Roach */ 4289ecabc6aSGreg Roach public function tag(): string 429c1010edaSGreg Roach { 43002467d32SGreg Roach return $this->record->tag() . ':' . $this->tag; 431a25f0a04SGreg Roach } 432a25f0a04SGreg Roach 433a25f0a04SGreg Roach /** 434decb512fSGreg Roach * The GEDCOM record where this Fact came from 435a25f0a04SGreg Roach * 436decb512fSGreg Roach * @return GedcomRecord 437a25f0a04SGreg Roach */ 438decb512fSGreg Roach public function record(): GedcomRecord 439c1010edaSGreg Roach { 440e7766c08SGreg Roach return $this->record; 441a25f0a04SGreg Roach } 442a25f0a04SGreg Roach 443a25f0a04SGreg Roach /** 444a25f0a04SGreg Roach * Get the name of this fact type, for use as a label. 445a25f0a04SGreg Roach * 446a25f0a04SGreg Roach * @return string 447a25f0a04SGreg Roach */ 4487b7d8067SGreg Roach public function label(): string 449c1010edaSGreg Roach { 45070ac1a22SGreg Roach // Marriages 45170ac1a22SGreg Roach if ($this->tag() === 'FAM:MARR') { 45270ac1a22SGreg Roach $element = Registry::elementFactory()->make('FAM:MARR:TYPE'); 45370ac1a22SGreg Roach $type = $this->attribute('TYPE'); 45470ac1a22SGreg Roach 45570ac1a22SGreg Roach if ($type !== '') { 45670ac1a22SGreg Roach return $element->value($type, $this->record->tree()); 45770ac1a22SGreg Roach } 45870ac1a22SGreg Roach } 45970ac1a22SGreg Roach 460a25f0a04SGreg Roach // Custom FACT/EVEN - with a TYPE 46118475794SGreg Roach if ($this->tag === 'FACT' || $this->tag === 'EVEN') { 46218475794SGreg Roach $type = $this->attribute('TYPE'); 46318475794SGreg Roach 46418475794SGreg Roach if ($type !== '') { 4653f9fe4ddSGreg Roach if (!str_contains($type, '%')) { 46618475794SGreg Roach // Allow user-translations of custom types. 46718475794SGreg Roach $translated = I18N::translate($type); 46818475794SGreg Roach 46918475794SGreg Roach if ($translated !== $type) { 47018475794SGreg Roach return $translated; 471a25f0a04SGreg Roach } 4723f9fe4ddSGreg Roach } 4734e5adf68SGreg Roach 47418475794SGreg Roach return e($type); 47518475794SGreg Roach } 47618475794SGreg Roach } 47718475794SGreg Roach 478455a30feSGreg Roach return Registry::elementFactory()->make($this->tag())->label(); 479a25f0a04SGreg Roach } 480a25f0a04SGreg Roach 481a25f0a04SGreg Roach /** 482a25f0a04SGreg Roach * This is a newly deleted fact, pending approval. 4837e96c925SGreg Roach * 4847e96c925SGreg Roach * @return void 485a25f0a04SGreg Roach */ 486e364afe4SGreg Roach public function setPendingDeletion(): void 487c1010edaSGreg Roach { 488a25f0a04SGreg Roach $this->pending_deletion = true; 489a25f0a04SGreg Roach $this->pending_addition = false; 490a25f0a04SGreg Roach } 491a25f0a04SGreg Roach 492a25f0a04SGreg Roach /** 493a25f0a04SGreg Roach * Is this a newly deleted fact, pending approval. 494a25f0a04SGreg Roach * 495cbc1590aSGreg Roach * @return bool 496a25f0a04SGreg Roach */ 4978f53f488SRico Sonntag public function isPendingDeletion(): bool 498c1010edaSGreg Roach { 499a25f0a04SGreg Roach return $this->pending_deletion; 500a25f0a04SGreg Roach } 501a25f0a04SGreg Roach 502a25f0a04SGreg Roach /** 503a25f0a04SGreg Roach * This is a newly added fact, pending approval. 5047e96c925SGreg Roach * 5057e96c925SGreg Roach * @return void 506a25f0a04SGreg Roach */ 507e364afe4SGreg Roach public function setPendingAddition(): void 508c1010edaSGreg Roach { 509a25f0a04SGreg Roach $this->pending_addition = true; 510a25f0a04SGreg Roach $this->pending_deletion = false; 511a25f0a04SGreg Roach } 512a25f0a04SGreg Roach 513a25f0a04SGreg Roach /** 514a25f0a04SGreg Roach * Is this a newly added fact, pending approval. 515a25f0a04SGreg Roach * 516cbc1590aSGreg Roach * @return bool 517a25f0a04SGreg Roach */ 5188f53f488SRico Sonntag public function isPendingAddition(): bool 519c1010edaSGreg Roach { 520a25f0a04SGreg Roach return $this->pending_addition; 521a25f0a04SGreg Roach } 522a25f0a04SGreg Roach 523a25f0a04SGreg Roach /** 524a25f0a04SGreg Roach * Source citations linked to this fact 525a25f0a04SGreg Roach * 52624f2a3afSGreg Roach * @return array<string> 527a25f0a04SGreg Roach */ 5288f53f488SRico Sonntag public function getCitations(): array 529c1010edaSGreg Roach { 5308d0ebef0SGreg Roach preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER); 53113abd6f3SGreg Roach $citations = []; 532a25f0a04SGreg Roach foreach ($matches as $match) { 5336b9cb339SGreg Roach $source = Registry::sourceFactory()->make($match[2], $this->record()->tree()); 5342859ecedSJonathan Jaubart if ($source && $source->canShow()) { 535a25f0a04SGreg Roach $citations[] = $match[1]; 536a25f0a04SGreg Roach } 537a25f0a04SGreg Roach } 538a25f0a04SGreg Roach 539a25f0a04SGreg Roach return $citations; 540a25f0a04SGreg Roach } 541a25f0a04SGreg Roach 542a25f0a04SGreg Roach /** 543a25f0a04SGreg Roach * Notes (inline and objects) linked to this fact 544a25f0a04SGreg Roach * 54509482a55SGreg Roach * @return array<string|Note> 546a25f0a04SGreg Roach */ 5478f53f488SRico Sonntag public function getNotes(): array 548c1010edaSGreg Roach { 54913abd6f3SGreg Roach $notes = []; 550138ca96cSGreg Roach preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches); 551a25f0a04SGreg Roach foreach ($matches[1] as $match) { 552a25f0a04SGreg Roach $note = preg_replace("/\n3 CONT ?/", "\n", $match); 5538d0ebef0SGreg Roach if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) { 5546b9cb339SGreg Roach $note = Registry::noteFactory()->make($nmatch[1], $this->record()->tree()); 555a25f0a04SGreg Roach if ($note && $note->canShow()) { 556a25f0a04SGreg Roach // A note object 557a25f0a04SGreg Roach $notes[] = $note; 558a25f0a04SGreg Roach } 559a25f0a04SGreg Roach } else { 560a25f0a04SGreg Roach // An inline note 561a25f0a04SGreg Roach $notes[] = $note; 562a25f0a04SGreg Roach } 563a25f0a04SGreg Roach } 564a25f0a04SGreg Roach 565a25f0a04SGreg Roach return $notes; 566a25f0a04SGreg Roach } 567a25f0a04SGreg Roach 568a25f0a04SGreg Roach /** 569a25f0a04SGreg Roach * Media objects linked to this fact 570a25f0a04SGreg Roach * 57109482a55SGreg Roach * @return array<Media> 572a25f0a04SGreg Roach */ 5738f53f488SRico Sonntag public function getMedia(): array 574c1010edaSGreg Roach { 57513abd6f3SGreg Roach $media = []; 5768d0ebef0SGreg Roach preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches); 577a25f0a04SGreg Roach foreach ($matches[1] as $match) { 5786b9cb339SGreg Roach $obje = Registry::mediaFactory()->make($match, $this->record()->tree()); 5792859ecedSJonathan Jaubart if ($obje && $obje->canShow()) { 580a25f0a04SGreg Roach $media[] = $obje; 581a25f0a04SGreg Roach } 582a25f0a04SGreg Roach } 583a25f0a04SGreg Roach 584a25f0a04SGreg Roach return $media; 585a25f0a04SGreg Roach } 586a25f0a04SGreg Roach 587a25f0a04SGreg Roach /** 588a25f0a04SGreg Roach * A one-line summary of the fact - for charts, etc. 589a25f0a04SGreg Roach * 590a25f0a04SGreg Roach * @return string 591a25f0a04SGreg Roach */ 5928f53f488SRico Sonntag public function summary(): string 593c1010edaSGreg Roach { 59413abd6f3SGreg Roach $attributes = []; 595dc124885SGreg Roach $target = $this->target(); 596db7bb364SGreg Roach if ($target instanceof GedcomRecord) { 59739ca88baSGreg Roach $attributes[] = $target->fullName(); 598a25f0a04SGreg Roach } else { 599726d7b07SGreg Roach // Fact value 60084586c02SGreg Roach $value = $this->value(); 601726d7b07SGreg Roach if ($value !== '' && $value !== 'Y') { 602315eb316SGreg Roach $attributes[] = '<bdi>' . e($value) . '</bdi>'; 603a25f0a04SGreg Roach } 604726d7b07SGreg Roach // Fact date 6052decada7SGreg Roach $date = $this->date(); 606726d7b07SGreg Roach if ($date->isOK()) { 6077fe676e5SGreg Roach if ($this->record() instanceof Individual && in_array($this->tag, Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) { 608b315f3e1SGreg Roach $attributes[] = $date->display() . view('fact-parents-age', ['individual' => $this->record(), 'birth_date' => $date]); 609a25f0a04SGreg Roach } else { 610a25f0a04SGreg Roach $attributes[] = $date->display(); 611a25f0a04SGreg Roach } 612726d7b07SGreg Roach } 613726d7b07SGreg Roach // Fact place 614e364afe4SGreg Roach if ($this->place()->gedcomName() !== '') { 615392561bbSGreg Roach $attributes[] = $this->place()->shortName(); 616a25f0a04SGreg Roach } 617a25f0a04SGreg Roach } 6182d8f08abSGreg Roach 6197fe676e5SGreg Roach $class = 'fact_' . $this->tag; 620a25f0a04SGreg Roach if ($this->isPendingAddition()) { 62171a69701SGreg Roach $class .= ' wt-new'; 622a25f0a04SGreg Roach } elseif ($this->isPendingDeletion()) { 62371a69701SGreg Roach $class .= ' wt-old'; 624a25f0a04SGreg Roach } 6252d8f08abSGreg Roach 62606c87791SGreg Roach $label = '<span class="label">' . $this->label() . '</span>'; 62706c87791SGreg Roach $value = '<span class="field" dir="auto">' . implode(' — ', $attributes) . '</span>'; 62806c87791SGreg Roach 6292d8f08abSGreg Roach /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ 63006c87791SGreg Roach return '<div class="' . $class . '">' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>'; 631a25f0a04SGreg Roach } 632a25f0a04SGreg Roach 633a25f0a04SGreg Roach /** 6349927f70fSGreg Roach * A one-line summary of the fact - for the clipboard, etc. 6359927f70fSGreg Roach * 6369927f70fSGreg Roach * @return string 6379927f70fSGreg Roach */ 6389927f70fSGreg Roach public function name(): string 6399927f70fSGreg Roach { 6409927f70fSGreg Roach $items = [$this->label()]; 6419927f70fSGreg Roach $target = $this->target(); 6429927f70fSGreg Roach 6439927f70fSGreg Roach if ($target instanceof GedcomRecord) { 644315eb316SGreg Roach $items[] = '<bdi>' . $target->fullName() . '</bdi>'; 6459927f70fSGreg Roach } else { 6469927f70fSGreg Roach // Fact value 6479927f70fSGreg Roach $value = $this->value(); 6489927f70fSGreg Roach if ($value !== '' && $value !== 'Y') { 649315eb316SGreg Roach $items[] = '<bdi>' . e($value) . '</bdi>'; 6509927f70fSGreg Roach } 6519927f70fSGreg Roach 6529927f70fSGreg Roach // Fact date 6539927f70fSGreg Roach if ($this->date()->isOK()) { 6549927f70fSGreg Roach $items[] = $this->date()->minimumDate()->format('%Y'); 6559927f70fSGreg Roach } 6569927f70fSGreg Roach 6579927f70fSGreg Roach // Fact place 6589927f70fSGreg Roach if ($this->place()->gedcomName() !== '') { 6599927f70fSGreg Roach $items[] = $this->place()->shortName(); 6609927f70fSGreg Roach } 6619927f70fSGreg Roach } 6629927f70fSGreg Roach 6639927f70fSGreg Roach return implode(' — ', $items); 6649927f70fSGreg Roach } 6659927f70fSGreg Roach 6669927f70fSGreg Roach /** 667580a4d11SGreg Roach * Helper functions to sort facts 668a25f0a04SGreg Roach * 669580a4d11SGreg Roach * @return Closure 670a25f0a04SGreg Roach */ 671580a4d11SGreg Roach private static function dateComparator(): Closure 672c1010edaSGreg Roach { 6736c2179e2SGreg Roach return static function (Fact $a, Fact $b): int { 6742decada7SGreg Roach if ($a->date()->isOK() && $b->date()->isOK()) { 675a25f0a04SGreg Roach // If both events have dates, compare by date 6762decada7SGreg Roach $ret = Date::compare($a->date(), $b->date()); 677a25f0a04SGreg Roach 678dfa848b8SGreg Roach if ($ret === 0) { 679580a4d11SGreg Roach // If dates overlap, compare by fact type 680580a4d11SGreg Roach $ret = self::typeComparator()($a, $b); 681a25f0a04SGreg Roach 682a25f0a04SGreg Roach // If the fact type is also the same, retain the initial order 683dfa848b8SGreg Roach if ($ret === 0) { 684580a4d11SGreg Roach $ret = $a->sortOrder <=> $b->sortOrder; 685a25f0a04SGreg Roach } 686a25f0a04SGreg Roach } 687a25f0a04SGreg Roach 688a25f0a04SGreg Roach return $ret; 689b2ce94c6SRico Sonntag } 690b2ce94c6SRico Sonntag 691a25f0a04SGreg Roach // One or both events have no date - retain the initial order 692580a4d11SGreg Roach return $a->sortOrder <=> $b->sortOrder; 693580a4d11SGreg Roach }; 694a25f0a04SGreg Roach } 695a25f0a04SGreg Roach 696a25f0a04SGreg Roach /** 697580a4d11SGreg Roach * Helper functions to sort facts. 698a25f0a04SGreg Roach * 699580a4d11SGreg Roach * @return Closure 700a25f0a04SGreg Roach */ 701580a4d11SGreg Roach public static function typeComparator(): Closure 702c1010edaSGreg Roach { 703bbe4546fSGreg Roach static $factsort = []; 704a25f0a04SGreg Roach 70554c1ab5eSGreg Roach if ($factsort === []) { 706bbe4546fSGreg Roach $factsort = array_flip(self::FACT_ORDER); 707a25f0a04SGreg Roach } 708a25f0a04SGreg Roach 7096c2179e2SGreg Roach return static function (Fact $a, Fact $b) use ($factsort): int { 710a25f0a04SGreg Roach // Facts from same families stay grouped together 711a25f0a04SGreg Roach // Keep MARR and DIV from the same families from mixing with events from other FAMs 712a25f0a04SGreg Roach // Use the original order in which the facts were added 713e7766c08SGreg Roach if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) { 714a25f0a04SGreg Roach return $a->sortOrder - $b->sortOrder; 715a25f0a04SGreg Roach } 716a25f0a04SGreg Roach 7177fe676e5SGreg Roach $atag = $a->tag; 7187fe676e5SGreg Roach $btag = $b->tag; 719a25f0a04SGreg Roach 720a25f0a04SGreg Roach // Events not in the above list get mapped onto one that is. 721a25f0a04SGreg Roach if (!array_key_exists($atag, $factsort)) { 7227a6ee1acSGreg Roach $atag = '_????_'; 723a25f0a04SGreg Roach } 724a25f0a04SGreg Roach 725a25f0a04SGreg Roach if (!array_key_exists($btag, $factsort)) { 7267a6ee1acSGreg Roach $btag = '_????_'; 727a25f0a04SGreg Roach } 728a25f0a04SGreg Roach 729a25f0a04SGreg Roach // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI 730a25f0a04SGreg Roach // - Treat dated after BURI facts as BURI instead 7313425616eSGreg Roach if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { 732a25f0a04SGreg Roach $atag = 'BURI'; 733a25f0a04SGreg Roach } 734a25f0a04SGreg Roach 7353425616eSGreg Roach if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { 736a25f0a04SGreg Roach $btag = 'BURI'; 737a25f0a04SGreg Roach } 738a25f0a04SGreg Roach 739a25f0a04SGreg Roach $ret = $factsort[$atag] - $factsort[$btag]; 740a25f0a04SGreg Roach 741a25f0a04SGreg Roach // If facts are the same then put dated facts before non-dated facts 7427fa97a69SGreg Roach if ($ret === 0) { 7433425616eSGreg Roach if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') { 744a25f0a04SGreg Roach return -1; 745a25f0a04SGreg Roach } 746a25f0a04SGreg Roach 7473425616eSGreg Roach if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') { 748a25f0a04SGreg Roach return 1; 749a25f0a04SGreg Roach } 750a25f0a04SGreg Roach 751a25f0a04SGreg Roach // If no sorting preference, then keep original ordering 752a25f0a04SGreg Roach $ret = $a->sortOrder - $b->sortOrder; 753a25f0a04SGreg Roach } 754a25f0a04SGreg Roach 755a25f0a04SGreg Roach return $ret; 756580a4d11SGreg Roach }; 757580a4d11SGreg Roach } 758580a4d11SGreg Roach 759580a4d11SGreg Roach /** 760580a4d11SGreg Roach * A multi-key sort 761580a4d11SGreg Roach * 1. First divide the facts into two arrays one set with dates and one set without dates 762580a4d11SGreg Roach * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated 763580a4d11SGreg Roach * using the compare type function 764580a4d11SGreg Roach * 3. Then merge the arrays back into the original array using the compare type function 765580a4d11SGreg Roach * 76636779af1SGreg Roach * @param Collection<int,Fact> $unsorted 767580a4d11SGreg Roach * 76836779af1SGreg Roach * @return Collection<int,Fact> 769580a4d11SGreg Roach */ 7708af3e5c1SGreg Roach public static function sortFacts(Collection $unsorted): Collection 771580a4d11SGreg Roach { 772580a4d11SGreg Roach $dated = []; 773580a4d11SGreg Roach $nondated = []; 774580a4d11SGreg Roach $sorted = []; 775580a4d11SGreg Roach 776580a4d11SGreg Roach // Split the array into dated and non-dated arrays 777580a4d11SGreg Roach $order = 0; 778580a4d11SGreg Roach 779580a4d11SGreg Roach foreach ($unsorted as $fact) { 780580a4d11SGreg Roach $fact->sortOrder = $order; 781580a4d11SGreg Roach $order++; 782580a4d11SGreg Roach 783580a4d11SGreg Roach if ($fact->date()->isOK()) { 784580a4d11SGreg Roach $dated[] = $fact; 785580a4d11SGreg Roach } else { 786580a4d11SGreg Roach $nondated[] = $fact; 787580a4d11SGreg Roach } 788580a4d11SGreg Roach } 789580a4d11SGreg Roach 790580a4d11SGreg Roach usort($dated, self::dateComparator()); 791580a4d11SGreg Roach usort($nondated, self::typeComparator()); 792580a4d11SGreg Roach 793580a4d11SGreg Roach // Merge the arrays 794580a4d11SGreg Roach $dc = count($dated); 795580a4d11SGreg Roach $nc = count($nondated); 796580a4d11SGreg Roach $i = 0; 797580a4d11SGreg Roach $j = 0; 798580a4d11SGreg Roach 799580a4d11SGreg Roach // while there is anything in the dated array continue merging 800580a4d11SGreg Roach while ($i < $dc) { 801580a4d11SGreg Roach // compare each fact by type to merge them in order 802580a4d11SGreg Roach if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) { 803580a4d11SGreg Roach $sorted[] = $nondated[$j]; 804580a4d11SGreg Roach $j++; 805580a4d11SGreg Roach } else { 806580a4d11SGreg Roach $sorted[] = $dated[$i]; 807580a4d11SGreg Roach $i++; 808580a4d11SGreg Roach } 809580a4d11SGreg Roach } 810580a4d11SGreg Roach 811580a4d11SGreg Roach // get anything that might be left in the nondated array 812580a4d11SGreg Roach while ($j < $nc) { 813580a4d11SGreg Roach $sorted[] = $nondated[$j]; 814580a4d11SGreg Roach $j++; 815580a4d11SGreg Roach } 816580a4d11SGreg Roach 8171c7a6874SGreg Roach return new Collection($sorted); 818a25f0a04SGreg Roach } 819a25f0a04SGreg Roach 820a25f0a04SGreg Roach /** 821ae71f733SGreg Roach * Sort fact/event tags using the same order that we use for facts. 822ae71f733SGreg Roach * 82336779af1SGreg Roach * @param Collection<int,string> $unsorted 824ae71f733SGreg Roach * 82536779af1SGreg Roach * @return Collection<int,string> 826ae71f733SGreg Roach */ 827ae71f733SGreg Roach public static function sortFactTags(Collection $unsorted): Collection 828ae71f733SGreg Roach { 829ae71f733SGreg Roach $tag_order = array_flip(self::FACT_ORDER); 830ae71f733SGreg Roach 831ae71f733SGreg Roach return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int { 832ae71f733SGreg Roach $sort_x = $tag_order[$x] ?? $tag_order['_????_']; 833ae71f733SGreg Roach $sort_y = $tag_order[$y] ?? $tag_order['_????_']; 834ae71f733SGreg Roach 835ae71f733SGreg Roach return $sort_x - $sort_y; 836ae71f733SGreg Roach }); 837ae71f733SGreg Roach } 838ae71f733SGreg Roach 839ae71f733SGreg Roach /** 840a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 841a25f0a04SGreg Roach * 842a25f0a04SGreg Roach * @return string 843a25f0a04SGreg Roach */ 844dfa848b8SGreg Roach public function __toString(): string 845c1010edaSGreg Roach { 846c0935879SGreg Roach return $this->id . '@' . $this->record->xref(); 847a25f0a04SGreg Roach } 848a25f0a04SGreg Roach} 849