1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 2176692c8bSGreg Roach 22580a4d11SGreg Roachuse Closure; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 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; 38ae71f733SGreg Roachuse function usort; 39ae71f733SGreg Roach 40ae71f733SGreg Roachuse const PREG_SET_ORDER; 41a25f0a04SGreg Roach 42a25f0a04SGreg Roach/** 4376692c8bSGreg Roach * A GEDCOM fact or event object. 44a25f0a04SGreg Roach */ 45c1010edaSGreg Roachclass Fact 46c1010edaSGreg Roach{ 4716d6367aSGreg Roach private const FACT_ORDER = [ 48bbe4546fSGreg Roach 'BIRT', 49bbe4546fSGreg Roach '_HNM', 50bbe4546fSGreg Roach 'ALIA', 51bbe4546fSGreg Roach '_AKA', 52bbe4546fSGreg Roach '_AKAN', 53bbe4546fSGreg Roach 'ADOP', 54bbe4546fSGreg Roach '_ADPF', 55bbe4546fSGreg Roach '_ADPF', 56bbe4546fSGreg Roach '_BRTM', 57bbe4546fSGreg Roach 'CHR', 58bbe4546fSGreg Roach 'BAPM', 59bbe4546fSGreg Roach 'FCOM', 60bbe4546fSGreg Roach 'CONF', 61bbe4546fSGreg Roach 'BARM', 62bbe4546fSGreg Roach 'BASM', 63bbe4546fSGreg Roach 'EDUC', 64bbe4546fSGreg Roach 'GRAD', 65bbe4546fSGreg Roach '_DEG', 66bbe4546fSGreg Roach 'EMIG', 67bbe4546fSGreg Roach 'IMMI', 68bbe4546fSGreg Roach 'NATU', 69bbe4546fSGreg Roach '_MILI', 70bbe4546fSGreg Roach '_MILT', 71bbe4546fSGreg Roach 'ENGA', 72bbe4546fSGreg Roach 'MARB', 73bbe4546fSGreg Roach 'MARC', 74bbe4546fSGreg Roach 'MARL', 75bbe4546fSGreg Roach '_MARI', 76bbe4546fSGreg Roach '_MBON', 77bbe4546fSGreg Roach 'MARR', 78bbe4546fSGreg Roach '_COML', 79bbe4546fSGreg Roach '_STAT', 80bbe4546fSGreg Roach '_SEPR', 81bbe4546fSGreg Roach 'DIVF', 82bbe4546fSGreg Roach 'MARS', 83bbe4546fSGreg Roach 'DIV', 84bbe4546fSGreg Roach 'ANUL', 85bbe4546fSGreg Roach 'CENS', 86bbe4546fSGreg Roach 'OCCU', 87bbe4546fSGreg Roach 'RESI', 88bbe4546fSGreg Roach 'PROP', 89bbe4546fSGreg Roach 'CHRA', 90bbe4546fSGreg Roach 'RETI', 91bbe4546fSGreg Roach 'FACT', 92bbe4546fSGreg Roach 'EVEN', 93bbe4546fSGreg Roach '_NMR', 94bbe4546fSGreg Roach '_NMAR', 95bbe4546fSGreg Roach 'NMR', 96bbe4546fSGreg Roach 'NCHI', 97bbe4546fSGreg Roach 'WILL', 98bbe4546fSGreg Roach '_HOL', 99bbe4546fSGreg Roach '_????_', 100bbe4546fSGreg Roach 'DEAT', 101bbe4546fSGreg Roach '_FNRL', 102bbe4546fSGreg Roach 'CREM', 103bbe4546fSGreg Roach 'BURI', 104bbe4546fSGreg Roach '_INTE', 105bbe4546fSGreg Roach '_YART', 106bbe4546fSGreg Roach '_NLIV', 107bbe4546fSGreg Roach 'PROB', 108bbe4546fSGreg Roach 'TITL', 109bbe4546fSGreg Roach 'COMM', 110bbe4546fSGreg Roach 'NATI', 111bbe4546fSGreg Roach 'CITN', 112bbe4546fSGreg Roach 'CAST', 113bbe4546fSGreg Roach 'RELI', 114bbe4546fSGreg Roach 'SSN', 115bbe4546fSGreg Roach 'IDNO', 116bbe4546fSGreg Roach 'TEMP', 117bbe4546fSGreg Roach 'SLGC', 118bbe4546fSGreg Roach 'BAPL', 119bbe4546fSGreg Roach 'CONL', 120bbe4546fSGreg Roach 'ENDL', 121bbe4546fSGreg Roach 'SLGS', 122bbe4546fSGreg Roach 'ADDR', 123bbe4546fSGreg Roach 'PHON', 124bbe4546fSGreg Roach 'EMAIL', 125bbe4546fSGreg Roach '_EMAIL', 126bbe4546fSGreg Roach 'EMAL', 127bbe4546fSGreg Roach 'FAX', 128bbe4546fSGreg Roach 'WWW', 129bbe4546fSGreg Roach 'URL', 130bbe4546fSGreg Roach '_URL', 131bbe4546fSGreg Roach 'AFN', 132bbe4546fSGreg Roach 'REFN', 133bbe4546fSGreg Roach '_PRMN', 134bbe4546fSGreg Roach 'REF', 135bbe4546fSGreg Roach 'RIN', 136bbe4546fSGreg Roach '_UID', 137bbe4546fSGreg Roach 'OBJE', 138bbe4546fSGreg Roach 'NOTE', 139bbe4546fSGreg Roach 'SOUR', 140bbe4546fSGreg Roach 'CHAN', 141bbe4546fSGreg Roach '_TODO', 142bbe4546fSGreg Roach ]; 143bbe4546fSGreg Roach 144a25f0a04SGreg Roach /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */ 145905ab80aSGreg Roach private $id; 146a25f0a04SGreg Roach 147a25f0a04SGreg Roach /** @var GedcomRecord The GEDCOM record from which this fact is taken */ 148e7766c08SGreg Roach private $record; 149a25f0a04SGreg Roach 150a25f0a04SGreg Roach /** @var string The raw GEDCOM data for this fact */ 151a25f0a04SGreg Roach private $gedcom; 152a25f0a04SGreg Roach 153a25f0a04SGreg Roach /** @var string The GEDCOM tag for this record */ 154a25f0a04SGreg Roach private $tag; 155a25f0a04SGreg Roach 156cbc1590aSGreg Roach /** @var bool Is this a recently deleted fact, pending approval? */ 157a25f0a04SGreg Roach private $pending_deletion = false; 158a25f0a04SGreg Roach 159cbc1590aSGreg Roach /** @var bool Is this a recently added fact, pending approval? */ 160a25f0a04SGreg Roach private $pending_addition = false; 161a25f0a04SGreg Roach 162a25f0a04SGreg Roach /** @var Date The date of this fact, from the “2 DATE …” attribute */ 163a25f0a04SGreg Roach private $date; 164a25f0a04SGreg Roach 165a25f0a04SGreg Roach /** @var Place The place of this fact, from the “2 PLAC …” attribute */ 166a25f0a04SGreg Roach private $place; 167a25f0a04SGreg Roach 168580a4d11SGreg Roach /** @var int Used by Functions::sortFacts() */ 169580a4d11SGreg Roach private $sortOrder; 170a25f0a04SGreg Roach 171a25f0a04SGreg Roach /** 172a25f0a04SGreg Roach * Create an event object from a gedcom fragment. 173a25f0a04SGreg Roach * We need the parent object (to check privacy) and a (pseudo) fact ID to 174a25f0a04SGreg Roach * identify the fact within the record. 175a25f0a04SGreg Roach * 176a25f0a04SGreg Roach * @param string $gedcom 177a25f0a04SGreg Roach * @param GedcomRecord $parent 178905ab80aSGreg Roach * @param string $id 179a25f0a04SGreg Roach * 1808f5f5da8SGreg Roach * @throws InvalidArgumentException 181a25f0a04SGreg Roach */ 182905ab80aSGreg Roach public function __construct($gedcom, GedcomRecord $parent, $id) 183c1010edaSGreg Roach { 1848d0ebef0SGreg Roach if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) { 185a25f0a04SGreg Roach $this->gedcom = $gedcom; 186e7766c08SGreg Roach $this->record = $parent; 187905ab80aSGreg Roach $this->id = $id; 188a25f0a04SGreg Roach $this->tag = $match[1]; 189a25f0a04SGreg Roach } else { 1908f5f5da8SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')'); 191a25f0a04SGreg Roach } 192a25f0a04SGreg Roach } 193a25f0a04SGreg Roach 194a25f0a04SGreg Roach /** 195a25f0a04SGreg Roach * Get the value of level 1 data in the fact 196a25f0a04SGreg Roach * Allow for multi-line values 197a25f0a04SGreg Roach * 198baacc364SGreg Roach * @return string 199a25f0a04SGreg Roach */ 200dfa848b8SGreg Roach public function value(): string 201c1010edaSGreg Roach { 202a25f0a04SGreg Roach if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) { 203a25f0a04SGreg Roach return preg_replace("/\n2 CONT ?/", "\n", $match[1]); 204a25f0a04SGreg Roach } 205b2ce94c6SRico Sonntag 206b2ce94c6SRico Sonntag return ''; 207a25f0a04SGreg Roach } 208a25f0a04SGreg Roach 209a25f0a04SGreg Roach /** 210a25f0a04SGreg Roach * Get the record to which this fact links 211a25f0a04SGreg Roach * 2120d15532eSGreg Roach * @return Family|GedcomRecord|Individual|Location|Media|Note|Repository|Source|Submission|Submitter|null 213a25f0a04SGreg Roach */ 214dc124885SGreg Roach public function target() 215c1010edaSGreg Roach { 21662b52849SGreg Roach if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) { 21762b52849SGreg Roach return null; 21862b52849SGreg Roach } 21962b52849SGreg Roach 22062b52849SGreg Roach $xref = $match[1]; 22162b52849SGreg Roach 222a25f0a04SGreg Roach switch ($this->tag) { 223a25f0a04SGreg Roach case 'FAMC': 224a25f0a04SGreg Roach case 'FAMS': 225a091ac74SGreg Roach return Factory::family()->make($xref, $this->record()->tree()); 226a25f0a04SGreg Roach case 'HUSB': 227a25f0a04SGreg Roach case 'WIFE': 22862b52849SGreg Roach case 'ALIA': 229a25f0a04SGreg Roach case 'CHIL': 23062b52849SGreg Roach case '_ASSO': 231a091ac74SGreg Roach return Factory::individual()->make($xref, $this->record()->tree()); 23262b52849SGreg Roach case 'ASSO': 23362b52849SGreg Roach return 234a091ac74SGreg Roach Factory::individual()->make($xref, $this->record()->tree()) ?? 235a091ac74SGreg Roach Factory::submitter()->make($xref, $this->record()->tree()); 236a25f0a04SGreg Roach case 'SOUR': 237a091ac74SGreg Roach return Factory::source()->make($xref, $this->record()->tree()); 238a25f0a04SGreg Roach case 'OBJE': 239a091ac74SGreg Roach return Factory::media()->make($xref, $this->record()->tree()); 240a25f0a04SGreg Roach case 'REPO': 241a091ac74SGreg Roach return Factory::repository()->make($xref, $this->record()->tree()); 242a25f0a04SGreg Roach case 'NOTE': 243a091ac74SGreg Roach return Factory::note()->make($xref, $this->record()->tree()); 24462b52849SGreg Roach case 'ANCI': 24562b52849SGreg Roach case 'DESI': 246ffc0a61fSGreg Roach case 'SUBM': 247a091ac74SGreg Roach return Factory::submitter()->make($xref, $this->record()->tree()); 2481635452cSGreg Roach case 'SUBN': 249a091ac74SGreg Roach return Factory::submission()->make($xref, $this->record()->tree()); 2500d15532eSGreg Roach case '_LOC': 2510d15532eSGreg Roach return Factory::location()->make($xref, $this->record()->tree()); 252a25f0a04SGreg Roach default: 253a091ac74SGreg Roach return Factory::gedcomRecord()->make($xref, $this->record()->tree()); 254a25f0a04SGreg Roach } 255a25f0a04SGreg Roach } 256a25f0a04SGreg Roach 257a25f0a04SGreg Roach /** 258a25f0a04SGreg Roach * Get the value of level 2 data in the fact 259a25f0a04SGreg Roach * 260a25f0a04SGreg Roach * @param string $tag 261a25f0a04SGreg Roach * 262baacc364SGreg Roach * @return string 263a25f0a04SGreg Roach */ 264dfa848b8SGreg Roach public function attribute($tag): string 265c1010edaSGreg Roach { 266a25f0a04SGreg Roach if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { 267a25f0a04SGreg Roach return preg_replace("/\n3 CONT ?/", "\n", $match[1]); 268a25f0a04SGreg Roach } 269b2ce94c6SRico Sonntag 270b2ce94c6SRico Sonntag return ''; 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach 273a25f0a04SGreg Roach /** 2748af6bbf8SGreg Roach * Get the PLAC:MAP:LATI for the fact. 2758af6bbf8SGreg Roach * 2768af6bbf8SGreg Roach * @return float 2778af6bbf8SGreg Roach */ 2788af6bbf8SGreg Roach public function latitude(): float 2798af6bbf8SGreg Roach { 2808af6bbf8SGreg Roach if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) { 2818af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2828af6bbf8SGreg Roach 2838af6bbf8SGreg Roach return $gedcom_service->readLatitude($match[1]); 2848af6bbf8SGreg Roach } 2858af6bbf8SGreg Roach 2868af6bbf8SGreg Roach return 0.0; 2878af6bbf8SGreg Roach } 2888af6bbf8SGreg Roach 2898af6bbf8SGreg Roach /** 2908af6bbf8SGreg Roach * Get the PLAC:MAP:LONG for the fact. 2918af6bbf8SGreg Roach * 2928af6bbf8SGreg Roach * @return float 2938af6bbf8SGreg Roach */ 2948af6bbf8SGreg Roach public function longitude(): float 2958af6bbf8SGreg Roach { 296819ac503SRico Sonntag if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) { 2978af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2988af6bbf8SGreg Roach 2998af6bbf8SGreg Roach return $gedcom_service->readLongitude($match[1]); 3008af6bbf8SGreg Roach } 3018af6bbf8SGreg Roach 3028af6bbf8SGreg Roach return 0.0; 3038af6bbf8SGreg Roach } 3048af6bbf8SGreg Roach 3058af6bbf8SGreg Roach /** 306a25f0a04SGreg Roach * Do the privacy rules allow us to display this fact to the current user 307a25f0a04SGreg Roach * 308cbc1590aSGreg Roach * @param int|null $access_level 309a25f0a04SGreg Roach * 310cbc1590aSGreg Roach * @return bool 311a25f0a04SGreg Roach */ 31235584196SGreg Roach public function canShow(int $access_level = null): bool 313c1010edaSGreg Roach { 314d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->record->tree()); 3154b9ff166SGreg Roach 316a25f0a04SGreg Roach // Does this record have an explicit RESN? 317dec352c1SGreg Roach if (str_contains($this->gedcom, "\n2 RESN confidential")) { 3184b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 319a25f0a04SGreg Roach } 320dec352c1SGreg Roach if (str_contains($this->gedcom, "\n2 RESN privacy")) { 3214b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 322a25f0a04SGreg Roach } 323dec352c1SGreg Roach if (str_contains($this->gedcom, "\n2 RESN none")) { 324a25f0a04SGreg Roach return true; 325a25f0a04SGreg Roach } 326a25f0a04SGreg Roach 327a25f0a04SGreg Roach // Does this record have a default RESN? 328c0935879SGreg Roach $xref = $this->record->xref(); 329f4afa648SGreg Roach $fact_privacy = $this->record->tree()->getFactPrivacy(); 330f4afa648SGreg Roach $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy(); 331518bbdc1SGreg Roach if (isset($individual_fact_privacy[$xref][$this->tag])) { 332518bbdc1SGreg Roach return $individual_fact_privacy[$xref][$this->tag] >= $access_level; 333a25f0a04SGreg Roach } 334518bbdc1SGreg Roach if (isset($fact_privacy[$this->tag])) { 335518bbdc1SGreg Roach return $fact_privacy[$this->tag] >= $access_level; 336a25f0a04SGreg Roach } 337a25f0a04SGreg Roach 338a25f0a04SGreg Roach // No restrictions - it must be public 339a25f0a04SGreg Roach return true; 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach 342a25f0a04SGreg Roach /** 343a25f0a04SGreg Roach * Check whether this fact is protected against edit 344a25f0a04SGreg Roach * 345cbc1590aSGreg Roach * @return bool 346a25f0a04SGreg Roach */ 3478f53f488SRico Sonntag public function canEdit(): bool 348c1010edaSGreg Roach { 3491450f098SGreg Roach if ($this->isPendingDeletion()) { 3501450f098SGreg Roach return false; 3511450f098SGreg Roach } 3521450f098SGreg Roach 3531450f098SGreg Roach if (Auth::isManager($this->record->tree())) { 3541450f098SGreg Roach return true; 3551450f098SGreg Roach } 3561450f098SGreg Roach 357a25f0a04SGreg Roach // Members cannot edit RESN, CHAN and locked records 358dec352c1SGreg Roach return Auth::isEditor($this->record->tree()) && !str_contains($this->gedcom, "\n2 RESN locked") && $this->tag !== 'RESN' && $this->tag !== 'CHAN'; 359a25f0a04SGreg Roach } 360a25f0a04SGreg Roach 361a25f0a04SGreg Roach /** 362a25f0a04SGreg Roach * The place where the event occured. 363a25f0a04SGreg Roach * 364a25f0a04SGreg Roach * @return Place 365a25f0a04SGreg Roach */ 3664fb14fcbSGreg Roach public function place(): Place 367c1010edaSGreg Roach { 368a25f0a04SGreg Roach if ($this->place === null) { 369f4afa648SGreg Roach $this->place = new Place($this->attribute('PLAC'), $this->record()->tree()); 370a25f0a04SGreg Roach } 371a25f0a04SGreg Roach 372a25f0a04SGreg Roach return $this->place; 373a25f0a04SGreg Roach } 374a25f0a04SGreg Roach 375a25f0a04SGreg Roach /** 376a25f0a04SGreg Roach * Get the date for this fact. 377a25f0a04SGreg Roach * We can call this function many times, especially when sorting, 378a25f0a04SGreg Roach * so keep a copy of the date. 379a25f0a04SGreg Roach * 380a25f0a04SGreg Roach * @return Date 381a25f0a04SGreg Roach */ 3822decada7SGreg Roach public function date(): Date 383c1010edaSGreg Roach { 384a25f0a04SGreg Roach if ($this->date === null) { 3853425616eSGreg Roach $this->date = new Date($this->attribute('DATE')); 386a25f0a04SGreg Roach } 387a25f0a04SGreg Roach 388a25f0a04SGreg Roach return $this->date; 389a25f0a04SGreg Roach } 390a25f0a04SGreg Roach 391a25f0a04SGreg Roach /** 392a25f0a04SGreg Roach * The raw GEDCOM data for this fact 393a25f0a04SGreg Roach * 394a25f0a04SGreg Roach * @return string 395a25f0a04SGreg Roach */ 396138ca96cSGreg Roach public function gedcom(): string 397c1010edaSGreg Roach { 398a25f0a04SGreg Roach return $this->gedcom; 399a25f0a04SGreg Roach } 400a25f0a04SGreg Roach 401a25f0a04SGreg Roach /** 402a25f0a04SGreg Roach * Get a (pseudo) primary key for this fact. 403a25f0a04SGreg Roach * 404a25f0a04SGreg Roach * @return string 405a25f0a04SGreg Roach */ 406905ab80aSGreg Roach public function id(): string 407c1010edaSGreg Roach { 408905ab80aSGreg Roach return $this->id; 409a25f0a04SGreg Roach } 410a25f0a04SGreg Roach 411a25f0a04SGreg Roach /** 412a25f0a04SGreg Roach * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 413a25f0a04SGreg Roach * 414a25f0a04SGreg Roach * @return string 415a25f0a04SGreg Roach */ 4169ecabc6aSGreg Roach public function tag(): string 417c1010edaSGreg Roach { 41802467d32SGreg Roach return $this->record->tag() . ':' . $this->tag; 419a25f0a04SGreg Roach } 420a25f0a04SGreg Roach 421a25f0a04SGreg Roach /** 4229ecabc6aSGreg Roach * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 4239ecabc6aSGreg Roach * 4249ecabc6aSGreg Roach * @return string 4259ecabc6aSGreg Roach * 4269ecabc6aSGreg Roach * @deprecated since 2.0.5. Will be removed in 2.1.0 4279ecabc6aSGreg Roach */ 4289ecabc6aSGreg Roach public function getTag(): string 4299ecabc6aSGreg Roach { 4307fe676e5SGreg Roach return $this->tag; 4319ecabc6aSGreg Roach } 4329ecabc6aSGreg Roach 4339ecabc6aSGreg Roach /** 434a25f0a04SGreg Roach * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL) 435a25f0a04SGreg Roach * 436a25f0a04SGreg Roach * @param string $tag 4377e96c925SGreg Roach * 4387e96c925SGreg Roach * @return void 4399ecabc6aSGreg Roach * 4409ecabc6aSGreg Roach * @deprecated since 2.0.5. Will be removed in 2.1.0 441a25f0a04SGreg Roach */ 442e364afe4SGreg Roach public function setTag($tag): void 443c1010edaSGreg Roach { 444a25f0a04SGreg Roach $this->tag = $tag; 445a25f0a04SGreg Roach } 446a25f0a04SGreg Roach 447a25f0a04SGreg Roach /** 448a25f0a04SGreg Roach * The Person/Family record where this Fact came from 449a25f0a04SGreg Roach * 4507bf6ca81SGreg Roach * @return Individual|Family|Source|Repository|Media|Note|Submitter|Submission|Location|Header|GedcomRecord 451a25f0a04SGreg Roach */ 452e7766c08SGreg Roach public function record() 453c1010edaSGreg Roach { 454e7766c08SGreg Roach return $this->record; 455a25f0a04SGreg Roach } 456a25f0a04SGreg Roach 457a25f0a04SGreg Roach /** 458a25f0a04SGreg Roach * Get the name of this fact type, for use as a label. 459a25f0a04SGreg Roach * 460a25f0a04SGreg Roach * @return string 461a25f0a04SGreg Roach */ 4627b7d8067SGreg Roach public function label(): string 463c1010edaSGreg Roach { 464a25f0a04SGreg Roach // Custom FACT/EVEN - with a TYPE 46518475794SGreg Roach if ($this->tag === 'FACT' || $this->tag === 'EVEN') { 46618475794SGreg Roach $type = $this->attribute('TYPE'); 46718475794SGreg Roach 46818475794SGreg Roach if ($type !== '') { 469*3f9fe4ddSGreg Roach if (!str_contains($type, '%')) { 47018475794SGreg Roach // Allow user-translations of custom types. 47118475794SGreg Roach $translated = I18N::translate($type); 47218475794SGreg Roach 47318475794SGreg Roach if ($translated !== $type) { 47418475794SGreg Roach return $translated; 475a25f0a04SGreg Roach } 476*3f9fe4ddSGreg Roach } 4774e5adf68SGreg Roach 47818475794SGreg Roach return e($type); 47918475794SGreg Roach } 48018475794SGreg Roach } 48118475794SGreg Roach 48202467d32SGreg Roach return GedcomTag::getLabel($this->record->tag() . ':' . $this->tag); 483a25f0a04SGreg Roach } 484a25f0a04SGreg Roach 485a25f0a04SGreg Roach /** 486a25f0a04SGreg Roach * This is a newly deleted fact, pending approval. 4877e96c925SGreg Roach * 4887e96c925SGreg Roach * @return void 489a25f0a04SGreg Roach */ 490e364afe4SGreg Roach public function setPendingDeletion(): void 491c1010edaSGreg Roach { 492a25f0a04SGreg Roach $this->pending_deletion = true; 493a25f0a04SGreg Roach $this->pending_addition = false; 494a25f0a04SGreg Roach } 495a25f0a04SGreg Roach 496a25f0a04SGreg Roach /** 497a25f0a04SGreg Roach * Is this a newly deleted fact, pending approval. 498a25f0a04SGreg Roach * 499cbc1590aSGreg Roach * @return bool 500a25f0a04SGreg Roach */ 5018f53f488SRico Sonntag public function isPendingDeletion(): bool 502c1010edaSGreg Roach { 503a25f0a04SGreg Roach return $this->pending_deletion; 504a25f0a04SGreg Roach } 505a25f0a04SGreg Roach 506a25f0a04SGreg Roach /** 507a25f0a04SGreg Roach * This is a newly added fact, pending approval. 5087e96c925SGreg Roach * 5097e96c925SGreg Roach * @return void 510a25f0a04SGreg Roach */ 511e364afe4SGreg Roach public function setPendingAddition(): void 512c1010edaSGreg Roach { 513a25f0a04SGreg Roach $this->pending_addition = true; 514a25f0a04SGreg Roach $this->pending_deletion = false; 515a25f0a04SGreg Roach } 516a25f0a04SGreg Roach 517a25f0a04SGreg Roach /** 518a25f0a04SGreg Roach * Is this a newly added fact, pending approval. 519a25f0a04SGreg Roach * 520cbc1590aSGreg Roach * @return bool 521a25f0a04SGreg Roach */ 5228f53f488SRico Sonntag public function isPendingAddition(): bool 523c1010edaSGreg Roach { 524a25f0a04SGreg Roach return $this->pending_addition; 525a25f0a04SGreg Roach } 526a25f0a04SGreg Roach 527a25f0a04SGreg Roach /** 528a25f0a04SGreg Roach * Source citations linked to this fact 529a25f0a04SGreg Roach * 530a25f0a04SGreg Roach * @return string[] 531a25f0a04SGreg Roach */ 5328f53f488SRico Sonntag public function getCitations(): array 533c1010edaSGreg Roach { 5348d0ebef0SGreg Roach preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER); 53513abd6f3SGreg Roach $citations = []; 536a25f0a04SGreg Roach foreach ($matches as $match) { 537a091ac74SGreg Roach $source = Factory::source()->make($match[2], $this->record()->tree()); 5382859ecedSJonathan Jaubart if ($source && $source->canShow()) { 539a25f0a04SGreg Roach $citations[] = $match[1]; 540a25f0a04SGreg Roach } 541a25f0a04SGreg Roach } 542a25f0a04SGreg Roach 543a25f0a04SGreg Roach return $citations; 544a25f0a04SGreg Roach } 545a25f0a04SGreg Roach 546a25f0a04SGreg Roach /** 547a25f0a04SGreg Roach * Notes (inline and objects) linked to this fact 548a25f0a04SGreg Roach * 549a25f0a04SGreg Roach * @return string[]|Note[] 550a25f0a04SGreg Roach */ 5518f53f488SRico Sonntag public function getNotes(): array 552c1010edaSGreg Roach { 55313abd6f3SGreg Roach $notes = []; 554138ca96cSGreg Roach preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches); 555a25f0a04SGreg Roach foreach ($matches[1] as $match) { 556a25f0a04SGreg Roach $note = preg_replace("/\n3 CONT ?/", "\n", $match); 5578d0ebef0SGreg Roach if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) { 558a091ac74SGreg Roach $note = Factory::note()->make($nmatch[1], $this->record()->tree()); 559a25f0a04SGreg Roach if ($note && $note->canShow()) { 560a25f0a04SGreg Roach // A note object 561a25f0a04SGreg Roach $notes[] = $note; 562a25f0a04SGreg Roach } 563a25f0a04SGreg Roach } else { 564a25f0a04SGreg Roach // An inline note 565a25f0a04SGreg Roach $notes[] = $note; 566a25f0a04SGreg Roach } 567a25f0a04SGreg Roach } 568a25f0a04SGreg Roach 569a25f0a04SGreg Roach return $notes; 570a25f0a04SGreg Roach } 571a25f0a04SGreg Roach 572a25f0a04SGreg Roach /** 573a25f0a04SGreg Roach * Media objects linked to this fact 574a25f0a04SGreg Roach * 575a25f0a04SGreg Roach * @return Media[] 576a25f0a04SGreg Roach */ 5778f53f488SRico Sonntag public function getMedia(): array 578c1010edaSGreg Roach { 57913abd6f3SGreg Roach $media = []; 5808d0ebef0SGreg Roach preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches); 581a25f0a04SGreg Roach foreach ($matches[1] as $match) { 582a091ac74SGreg Roach $obje = Factory::media()->make($match, $this->record()->tree()); 5832859ecedSJonathan Jaubart if ($obje && $obje->canShow()) { 584a25f0a04SGreg Roach $media[] = $obje; 585a25f0a04SGreg Roach } 586a25f0a04SGreg Roach } 587a25f0a04SGreg Roach 588a25f0a04SGreg Roach return $media; 589a25f0a04SGreg Roach } 590a25f0a04SGreg Roach 591a25f0a04SGreg Roach /** 592a25f0a04SGreg Roach * A one-line summary of the fact - for charts, etc. 593a25f0a04SGreg Roach * 594a25f0a04SGreg Roach * @return string 595a25f0a04SGreg Roach */ 5968f53f488SRico Sonntag public function summary(): string 597c1010edaSGreg Roach { 59813abd6f3SGreg Roach $attributes = []; 599dc124885SGreg Roach $target = $this->target(); 600db7bb364SGreg Roach if ($target instanceof GedcomRecord) { 60139ca88baSGreg Roach $attributes[] = $target->fullName(); 602a25f0a04SGreg Roach } else { 603726d7b07SGreg Roach // Fact value 60484586c02SGreg Roach $value = $this->value(); 605726d7b07SGreg Roach if ($value !== '' && $value !== 'Y') { 606d53324c9SGreg Roach $attributes[] = '<span dir="auto">' . e($value) . '</span>'; 607a25f0a04SGreg Roach } 608726d7b07SGreg Roach // Fact date 6092decada7SGreg Roach $date = $this->date(); 610726d7b07SGreg Roach if ($date->isOK()) { 6117fe676e5SGreg Roach if ($this->record() instanceof Individual && in_array($this->tag, Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) { 612e7766c08SGreg Roach $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date); 613a25f0a04SGreg Roach } else { 614a25f0a04SGreg Roach $attributes[] = $date->display(); 615a25f0a04SGreg Roach } 616726d7b07SGreg Roach } 617726d7b07SGreg Roach // Fact place 618e364afe4SGreg Roach if ($this->place()->gedcomName() !== '') { 619392561bbSGreg Roach $attributes[] = $this->place()->shortName(); 620a25f0a04SGreg Roach } 621a25f0a04SGreg Roach } 6222d8f08abSGreg Roach 6237fe676e5SGreg Roach $class = 'fact_' . $this->tag; 624a25f0a04SGreg Roach if ($this->isPendingAddition()) { 62571a69701SGreg Roach $class .= ' wt-new'; 626a25f0a04SGreg Roach } elseif ($this->isPendingDeletion()) { 62771a69701SGreg Roach $class .= ' wt-old'; 628a25f0a04SGreg Roach } 6292d8f08abSGreg Roach 6302d8f08abSGreg Roach return 6312d8f08abSGreg Roach '<div class="' . $class . '">' . 6322d8f08abSGreg Roach /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ 6337b7d8067SGreg Roach I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) . 6342d8f08abSGreg Roach '</div>'; 635a25f0a04SGreg Roach } 636a25f0a04SGreg Roach 637a25f0a04SGreg Roach /** 638580a4d11SGreg Roach * Helper functions to sort facts 639a25f0a04SGreg Roach * 640580a4d11SGreg Roach * @return Closure 641a25f0a04SGreg Roach */ 642580a4d11SGreg Roach private static function dateComparator(): Closure 643c1010edaSGreg Roach { 6446c2179e2SGreg Roach return static function (Fact $a, Fact $b): int { 6452decada7SGreg Roach if ($a->date()->isOK() && $b->date()->isOK()) { 646a25f0a04SGreg Roach // If both events have dates, compare by date 6472decada7SGreg Roach $ret = Date::compare($a->date(), $b->date()); 648a25f0a04SGreg Roach 649dfa848b8SGreg Roach if ($ret === 0) { 650580a4d11SGreg Roach // If dates overlap, compare by fact type 651580a4d11SGreg Roach $ret = self::typeComparator()($a, $b); 652a25f0a04SGreg Roach 653a25f0a04SGreg Roach // If the fact type is also the same, retain the initial order 654dfa848b8SGreg Roach if ($ret === 0) { 655580a4d11SGreg Roach $ret = $a->sortOrder <=> $b->sortOrder; 656a25f0a04SGreg Roach } 657a25f0a04SGreg Roach } 658a25f0a04SGreg Roach 659a25f0a04SGreg Roach return $ret; 660b2ce94c6SRico Sonntag } 661b2ce94c6SRico Sonntag 662a25f0a04SGreg Roach // One or both events have no date - retain the initial order 663580a4d11SGreg Roach return $a->sortOrder <=> $b->sortOrder; 664580a4d11SGreg Roach }; 665a25f0a04SGreg Roach } 666a25f0a04SGreg Roach 667a25f0a04SGreg Roach /** 668580a4d11SGreg Roach * Helper functions to sort facts. 669a25f0a04SGreg Roach * 670580a4d11SGreg Roach * @return Closure 671a25f0a04SGreg Roach */ 672580a4d11SGreg Roach public static function typeComparator(): Closure 673c1010edaSGreg Roach { 674bbe4546fSGreg Roach static $factsort = []; 675a25f0a04SGreg Roach 67654c1ab5eSGreg Roach if ($factsort === []) { 677bbe4546fSGreg Roach $factsort = array_flip(self::FACT_ORDER); 678a25f0a04SGreg Roach } 679a25f0a04SGreg Roach 6806c2179e2SGreg Roach return static function (Fact $a, Fact $b) use ($factsort): int { 681a25f0a04SGreg Roach // Facts from same families stay grouped together 682a25f0a04SGreg Roach // Keep MARR and DIV from the same families from mixing with events from other FAMs 683a25f0a04SGreg Roach // Use the original order in which the facts were added 684e7766c08SGreg Roach if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) { 685a25f0a04SGreg Roach return $a->sortOrder - $b->sortOrder; 686a25f0a04SGreg Roach } 687a25f0a04SGreg Roach 6887fe676e5SGreg Roach $atag = $a->tag; 6897fe676e5SGreg Roach $btag = $b->tag; 690a25f0a04SGreg Roach 691a25f0a04SGreg Roach // Events not in the above list get mapped onto one that is. 692a25f0a04SGreg Roach if (!array_key_exists($atag, $factsort)) { 6937a6ee1acSGreg Roach $atag = '_????_'; 694a25f0a04SGreg Roach } 695a25f0a04SGreg Roach 696a25f0a04SGreg Roach if (!array_key_exists($btag, $factsort)) { 6977a6ee1acSGreg Roach $btag = '_????_'; 698a25f0a04SGreg Roach } 699a25f0a04SGreg Roach 700a25f0a04SGreg Roach // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI 701a25f0a04SGreg Roach // - Treat dated after BURI facts as BURI instead 7023425616eSGreg Roach if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { 703a25f0a04SGreg Roach $atag = 'BURI'; 704a25f0a04SGreg Roach } 705a25f0a04SGreg Roach 7063425616eSGreg Roach if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { 707a25f0a04SGreg Roach $btag = 'BURI'; 708a25f0a04SGreg Roach } 709a25f0a04SGreg Roach 710a25f0a04SGreg Roach $ret = $factsort[$atag] - $factsort[$btag]; 711a25f0a04SGreg Roach 712a25f0a04SGreg Roach // If facts are the same then put dated facts before non-dated facts 713a25f0a04SGreg Roach if ($ret == 0) { 7143425616eSGreg Roach if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') { 715a25f0a04SGreg Roach return -1; 716a25f0a04SGreg Roach } 717a25f0a04SGreg Roach 7183425616eSGreg Roach if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') { 719a25f0a04SGreg Roach return 1; 720a25f0a04SGreg Roach } 721a25f0a04SGreg Roach 722a25f0a04SGreg Roach // If no sorting preference, then keep original ordering 723a25f0a04SGreg Roach $ret = $a->sortOrder - $b->sortOrder; 724a25f0a04SGreg Roach } 725a25f0a04SGreg Roach 726a25f0a04SGreg Roach return $ret; 727580a4d11SGreg Roach }; 728580a4d11SGreg Roach } 729580a4d11SGreg Roach 730580a4d11SGreg Roach /** 731580a4d11SGreg Roach * A multi-key sort 732580a4d11SGreg Roach * 1. First divide the facts into two arrays one set with dates and one set without dates 733580a4d11SGreg Roach * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated 734580a4d11SGreg Roach * using the compare type function 735580a4d11SGreg Roach * 3. Then merge the arrays back into the original array using the compare type function 736580a4d11SGreg Roach * 737b5c8fd7eSGreg Roach * @param Collection<Fact> $unsorted 738580a4d11SGreg Roach * 739b5c8fd7eSGreg Roach * @return Collection<Fact> 740580a4d11SGreg Roach */ 7418af3e5c1SGreg Roach public static function sortFacts(Collection $unsorted): Collection 742580a4d11SGreg Roach { 743580a4d11SGreg Roach $dated = []; 744580a4d11SGreg Roach $nondated = []; 745580a4d11SGreg Roach $sorted = []; 746580a4d11SGreg Roach 747580a4d11SGreg Roach // Split the array into dated and non-dated arrays 748580a4d11SGreg Roach $order = 0; 749580a4d11SGreg Roach 750580a4d11SGreg Roach foreach ($unsorted as $fact) { 751580a4d11SGreg Roach $fact->sortOrder = $order; 752580a4d11SGreg Roach $order++; 753580a4d11SGreg Roach 754580a4d11SGreg Roach if ($fact->date()->isOK()) { 755580a4d11SGreg Roach $dated[] = $fact; 756580a4d11SGreg Roach } else { 757580a4d11SGreg Roach $nondated[] = $fact; 758580a4d11SGreg Roach } 759580a4d11SGreg Roach } 760580a4d11SGreg Roach 761580a4d11SGreg Roach usort($dated, self::dateComparator()); 762580a4d11SGreg Roach usort($nondated, self::typeComparator()); 763580a4d11SGreg Roach 764580a4d11SGreg Roach // Merge the arrays 765580a4d11SGreg Roach $dc = count($dated); 766580a4d11SGreg Roach $nc = count($nondated); 767580a4d11SGreg Roach $i = 0; 768580a4d11SGreg Roach $j = 0; 769580a4d11SGreg Roach 770580a4d11SGreg Roach // while there is anything in the dated array continue merging 771580a4d11SGreg Roach while ($i < $dc) { 772580a4d11SGreg Roach // compare each fact by type to merge them in order 773580a4d11SGreg Roach if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) { 774580a4d11SGreg Roach $sorted[] = $nondated[$j]; 775580a4d11SGreg Roach $j++; 776580a4d11SGreg Roach } else { 777580a4d11SGreg Roach $sorted[] = $dated[$i]; 778580a4d11SGreg Roach $i++; 779580a4d11SGreg Roach } 780580a4d11SGreg Roach } 781580a4d11SGreg Roach 782580a4d11SGreg Roach // get anything that might be left in the nondated array 783580a4d11SGreg Roach while ($j < $nc) { 784580a4d11SGreg Roach $sorted[] = $nondated[$j]; 785580a4d11SGreg Roach $j++; 786580a4d11SGreg Roach } 787580a4d11SGreg Roach 7881c7a6874SGreg Roach return new Collection($sorted); 789a25f0a04SGreg Roach } 790a25f0a04SGreg Roach 791a25f0a04SGreg Roach /** 792ae71f733SGreg Roach * Sort fact/event tags using the same order that we use for facts. 793ae71f733SGreg Roach * 794ae71f733SGreg Roach * @param Collection<string> $unsorted 795ae71f733SGreg Roach * 796ae71f733SGreg Roach * @return Collection<string> 797ae71f733SGreg Roach */ 798ae71f733SGreg Roach public static function sortFactTags(Collection $unsorted): Collection 799ae71f733SGreg Roach { 800ae71f733SGreg Roach $tag_order = array_flip(self::FACT_ORDER); 801ae71f733SGreg Roach 802ae71f733SGreg Roach return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int { 803ae71f733SGreg Roach $sort_x = $tag_order[$x] ?? $tag_order['_????_']; 804ae71f733SGreg Roach $sort_y = $tag_order[$y] ?? $tag_order['_????_']; 805ae71f733SGreg Roach 806ae71f733SGreg Roach return $sort_x - $sort_y; 807ae71f733SGreg Roach }); 808ae71f733SGreg Roach } 809ae71f733SGreg Roach 810ae71f733SGreg Roach /** 811a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 812a25f0a04SGreg Roach * 813a25f0a04SGreg Roach * @return string 814a25f0a04SGreg Roach */ 815dfa848b8SGreg Roach public function __toString(): string 816c1010edaSGreg Roach { 817c0935879SGreg Roach return $this->id . '@' . $this->record->xref(); 818a25f0a04SGreg Roach } 819a25f0a04SGreg Roach} 820