1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 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; 371450f098SGreg Roachuse function strpos; 38ae71f733SGreg Roachuse function trim; 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 'MARR_CIVIL', 80bbe4546fSGreg Roach 'MARR_RELIGIOUS', 81bbe4546fSGreg Roach 'MARR_PARTNERS', 82bbe4546fSGreg Roach 'MARR_UNKNOWN', 83bbe4546fSGreg Roach '_COML', 84bbe4546fSGreg Roach '_STAT', 85bbe4546fSGreg Roach '_SEPR', 86bbe4546fSGreg Roach 'DIVF', 87bbe4546fSGreg Roach 'MARS', 88bbe4546fSGreg Roach '_BIRT_CHIL', 89bbe4546fSGreg Roach 'DIV', 90bbe4546fSGreg Roach 'ANUL', 91bbe4546fSGreg Roach '_BIRT_', 92bbe4546fSGreg Roach '_MARR_', 93bbe4546fSGreg Roach '_DEAT_', 94bbe4546fSGreg Roach '_BURI_', 95bbe4546fSGreg Roach 'CENS', 96bbe4546fSGreg Roach 'OCCU', 97bbe4546fSGreg Roach 'RESI', 98bbe4546fSGreg Roach 'PROP', 99bbe4546fSGreg Roach 'CHRA', 100bbe4546fSGreg Roach 'RETI', 101bbe4546fSGreg Roach 'FACT', 102bbe4546fSGreg Roach 'EVEN', 103bbe4546fSGreg Roach '_NMR', 104bbe4546fSGreg Roach '_NMAR', 105bbe4546fSGreg Roach 'NMR', 106bbe4546fSGreg Roach 'NCHI', 107bbe4546fSGreg Roach 'WILL', 108bbe4546fSGreg Roach '_HOL', 109bbe4546fSGreg Roach '_????_', 110bbe4546fSGreg Roach 'DEAT', 111bbe4546fSGreg Roach '_FNRL', 112bbe4546fSGreg Roach 'CREM', 113bbe4546fSGreg Roach 'BURI', 114bbe4546fSGreg Roach '_INTE', 115bbe4546fSGreg Roach '_YART', 116bbe4546fSGreg Roach '_NLIV', 117bbe4546fSGreg Roach 'PROB', 118bbe4546fSGreg Roach 'TITL', 119bbe4546fSGreg Roach 'COMM', 120bbe4546fSGreg Roach 'NATI', 121bbe4546fSGreg Roach 'CITN', 122bbe4546fSGreg Roach 'CAST', 123bbe4546fSGreg Roach 'RELI', 124bbe4546fSGreg Roach 'SSN', 125bbe4546fSGreg Roach 'IDNO', 126bbe4546fSGreg Roach 'TEMP', 127bbe4546fSGreg Roach 'SLGC', 128bbe4546fSGreg Roach 'BAPL', 129bbe4546fSGreg Roach 'CONL', 130bbe4546fSGreg Roach 'ENDL', 131bbe4546fSGreg Roach 'SLGS', 132bbe4546fSGreg Roach 'ADDR', 133bbe4546fSGreg Roach 'PHON', 134bbe4546fSGreg Roach 'EMAIL', 135bbe4546fSGreg Roach '_EMAIL', 136bbe4546fSGreg Roach 'EMAL', 137bbe4546fSGreg Roach 'FAX', 138bbe4546fSGreg Roach 'WWW', 139bbe4546fSGreg Roach 'URL', 140bbe4546fSGreg Roach '_URL', 141bbe4546fSGreg Roach 'AFN', 142bbe4546fSGreg Roach 'REFN', 143bbe4546fSGreg Roach '_PRMN', 144bbe4546fSGreg Roach 'REF', 145bbe4546fSGreg Roach 'RIN', 146bbe4546fSGreg Roach '_UID', 147bbe4546fSGreg Roach 'OBJE', 148bbe4546fSGreg Roach 'NOTE', 149bbe4546fSGreg Roach 'SOUR', 150bbe4546fSGreg Roach 'CHAN', 151bbe4546fSGreg Roach '_TODO', 152bbe4546fSGreg Roach ]; 153bbe4546fSGreg Roach 154a25f0a04SGreg Roach /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */ 155905ab80aSGreg Roach private $id; 156a25f0a04SGreg Roach 157a25f0a04SGreg Roach /** @var GedcomRecord The GEDCOM record from which this fact is taken */ 158e7766c08SGreg Roach private $record; 159a25f0a04SGreg Roach 160a25f0a04SGreg Roach /** @var string The raw GEDCOM data for this fact */ 161a25f0a04SGreg Roach private $gedcom; 162a25f0a04SGreg Roach 163a25f0a04SGreg Roach /** @var string The GEDCOM tag for this record */ 164a25f0a04SGreg Roach private $tag; 165a25f0a04SGreg Roach 166cbc1590aSGreg Roach /** @var bool Is this a recently deleted fact, pending approval? */ 167a25f0a04SGreg Roach private $pending_deletion = false; 168a25f0a04SGreg Roach 169cbc1590aSGreg Roach /** @var bool Is this a recently added fact, pending approval? */ 170a25f0a04SGreg Roach private $pending_addition = false; 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach /** @var Date The date of this fact, from the “2 DATE …” attribute */ 173a25f0a04SGreg Roach private $date; 174a25f0a04SGreg Roach 175a25f0a04SGreg Roach /** @var Place The place of this fact, from the “2 PLAC …” attribute */ 176a25f0a04SGreg Roach private $place; 177a25f0a04SGreg Roach 178580a4d11SGreg Roach /** @var int Used by Functions::sortFacts() */ 179580a4d11SGreg Roach private $sortOrder; 180a25f0a04SGreg Roach 181a25f0a04SGreg Roach /** 182a25f0a04SGreg Roach * Create an event object from a gedcom fragment. 183a25f0a04SGreg Roach * We need the parent object (to check privacy) and a (pseudo) fact ID to 184a25f0a04SGreg Roach * identify the fact within the record. 185a25f0a04SGreg Roach * 186a25f0a04SGreg Roach * @param string $gedcom 187a25f0a04SGreg Roach * @param GedcomRecord $parent 188905ab80aSGreg Roach * @param string $id 189a25f0a04SGreg Roach * 1908f5f5da8SGreg Roach * @throws InvalidArgumentException 191a25f0a04SGreg Roach */ 192905ab80aSGreg Roach public function __construct($gedcom, GedcomRecord $parent, $id) 193c1010edaSGreg Roach { 1948d0ebef0SGreg Roach if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) { 195a25f0a04SGreg Roach $this->gedcom = $gedcom; 196e7766c08SGreg Roach $this->record = $parent; 197905ab80aSGreg Roach $this->id = $id; 198a25f0a04SGreg Roach $this->tag = $match[1]; 199a25f0a04SGreg Roach } else { 2008f5f5da8SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')'); 201a25f0a04SGreg Roach } 202a25f0a04SGreg Roach } 203a25f0a04SGreg Roach 204a25f0a04SGreg Roach /** 205a25f0a04SGreg Roach * Get the value of level 1 data in the fact 206a25f0a04SGreg Roach * Allow for multi-line values 207a25f0a04SGreg Roach * 208baacc364SGreg Roach * @return string 209a25f0a04SGreg Roach */ 210dfa848b8SGreg Roach public function value(): string 211c1010edaSGreg Roach { 212a25f0a04SGreg Roach if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) { 213a25f0a04SGreg Roach return preg_replace("/\n2 CONT ?/", "\n", $match[1]); 214a25f0a04SGreg Roach } 215b2ce94c6SRico Sonntag 216b2ce94c6SRico Sonntag return ''; 217a25f0a04SGreg Roach } 218a25f0a04SGreg Roach 219a25f0a04SGreg Roach /** 220a25f0a04SGreg Roach * Get the record to which this fact links 221a25f0a04SGreg Roach * 222*62b52849SGreg Roach * @return Individual|Family|Source|Submitter|Repository|Media|Note|GedcomRecord|null 223a25f0a04SGreg Roach */ 224dc124885SGreg Roach public function target() 225c1010edaSGreg Roach { 226*62b52849SGreg Roach if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) { 227*62b52849SGreg Roach return null; 228*62b52849SGreg Roach } 229*62b52849SGreg Roach 230*62b52849SGreg Roach $xref = $match[1]; 231*62b52849SGreg Roach 232a25f0a04SGreg Roach switch ($this->tag) { 233a25f0a04SGreg Roach case 'FAMC': 234a25f0a04SGreg Roach case 'FAMS': 235f4afa648SGreg Roach return Family::getInstance($xref, $this->record()->tree()); 236a25f0a04SGreg Roach case 'HUSB': 237a25f0a04SGreg Roach case 'WIFE': 238*62b52849SGreg Roach case 'ALIA': 239a25f0a04SGreg Roach case 'CHIL': 240*62b52849SGreg Roach case '_ASSO': 241f4afa648SGreg Roach return Individual::getInstance($xref, $this->record()->tree()); 242*62b52849SGreg Roach case 'ASSO': 243*62b52849SGreg Roach return 244*62b52849SGreg Roach Individual::getInstance($xref, $this->record()->tree()) ?? 245*62b52849SGreg Roach Submitter::getInstance($xref, $this->record()->tree()); 246a25f0a04SGreg Roach case 'SOUR': 247f4afa648SGreg Roach return Source::getInstance($xref, $this->record()->tree()); 248a25f0a04SGreg Roach case 'OBJE': 249f4afa648SGreg Roach return Media::getInstance($xref, $this->record()->tree()); 250a25f0a04SGreg Roach case 'REPO': 251f4afa648SGreg Roach return Repository::getInstance($xref, $this->record()->tree()); 252a25f0a04SGreg Roach case 'NOTE': 253f4afa648SGreg Roach return Note::getInstance($xref, $this->record()->tree()); 254*62b52849SGreg Roach case 'ANCI': 255*62b52849SGreg Roach case 'DESI': 256ffc0a61fSGreg Roach case 'SUBM': 257ffc0a61fSGreg Roach return Submitter::getInstance($xref, $this->record()->tree()); 258a25f0a04SGreg Roach default: 259f4afa648SGreg Roach return GedcomRecord::getInstance($xref, $this->record()->tree()); 260a25f0a04SGreg Roach } 261a25f0a04SGreg Roach } 262a25f0a04SGreg Roach 263a25f0a04SGreg Roach /** 264a25f0a04SGreg Roach * Get the value of level 2 data in the fact 265a25f0a04SGreg Roach * 266a25f0a04SGreg Roach * @param string $tag 267a25f0a04SGreg Roach * 268baacc364SGreg Roach * @return string 269a25f0a04SGreg Roach */ 270dfa848b8SGreg Roach public function attribute($tag): string 271c1010edaSGreg Roach { 272a25f0a04SGreg Roach if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { 273a25f0a04SGreg Roach return preg_replace("/\n3 CONT ?/", "\n", $match[1]); 274a25f0a04SGreg Roach } 275b2ce94c6SRico Sonntag 276b2ce94c6SRico Sonntag return ''; 277a25f0a04SGreg Roach } 278a25f0a04SGreg Roach 279a25f0a04SGreg Roach /** 2808af6bbf8SGreg Roach * Get the PLAC:MAP:LATI for the fact. 2818af6bbf8SGreg Roach * 2828af6bbf8SGreg Roach * @return float 2838af6bbf8SGreg Roach */ 2848af6bbf8SGreg Roach public function latitude(): float 2858af6bbf8SGreg Roach { 2868af6bbf8SGreg Roach if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) { 2878af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2888af6bbf8SGreg Roach 2898af6bbf8SGreg Roach return $gedcom_service->readLatitude($match[1]); 2908af6bbf8SGreg Roach } 2918af6bbf8SGreg Roach 2928af6bbf8SGreg Roach return 0.0; 2938af6bbf8SGreg Roach } 2948af6bbf8SGreg Roach 2958af6bbf8SGreg Roach /** 2968af6bbf8SGreg Roach * Get the PLAC:MAP:LONG for the fact. 2978af6bbf8SGreg Roach * 2988af6bbf8SGreg Roach * @return float 2998af6bbf8SGreg Roach */ 3008af6bbf8SGreg Roach public function longitude(): float 3018af6bbf8SGreg Roach { 302819ac503SRico Sonntag if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) { 3038af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 3048af6bbf8SGreg Roach 3058af6bbf8SGreg Roach return $gedcom_service->readLongitude($match[1]); 3068af6bbf8SGreg Roach } 3078af6bbf8SGreg Roach 3088af6bbf8SGreg Roach return 0.0; 3098af6bbf8SGreg Roach } 3108af6bbf8SGreg Roach 3118af6bbf8SGreg Roach /** 312a25f0a04SGreg Roach * Do the privacy rules allow us to display this fact to the current user 313a25f0a04SGreg Roach * 314cbc1590aSGreg Roach * @param int|null $access_level 315a25f0a04SGreg Roach * 316cbc1590aSGreg Roach * @return bool 317a25f0a04SGreg Roach */ 31835584196SGreg Roach public function canShow(int $access_level = null): bool 319c1010edaSGreg Roach { 320d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->record->tree()); 3214b9ff166SGreg Roach 322a25f0a04SGreg Roach // Does this record have an explicit RESN? 32320ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN confidential") !== false) { 3244b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 325a25f0a04SGreg Roach } 32620ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN privacy") !== false) { 3274b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 328a25f0a04SGreg Roach } 32920ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN none") !== false) { 330a25f0a04SGreg Roach return true; 331a25f0a04SGreg Roach } 332a25f0a04SGreg Roach 333a25f0a04SGreg Roach // Does this record have a default RESN? 334c0935879SGreg Roach $xref = $this->record->xref(); 335f4afa648SGreg Roach $fact_privacy = $this->record->tree()->getFactPrivacy(); 336f4afa648SGreg Roach $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy(); 337518bbdc1SGreg Roach if (isset($individual_fact_privacy[$xref][$this->tag])) { 338518bbdc1SGreg Roach return $individual_fact_privacy[$xref][$this->tag] >= $access_level; 339a25f0a04SGreg Roach } 340518bbdc1SGreg Roach if (isset($fact_privacy[$this->tag])) { 341518bbdc1SGreg Roach return $fact_privacy[$this->tag] >= $access_level; 342a25f0a04SGreg Roach } 343a25f0a04SGreg Roach 344a25f0a04SGreg Roach // No restrictions - it must be public 345a25f0a04SGreg Roach return true; 346a25f0a04SGreg Roach } 347a25f0a04SGreg Roach 348a25f0a04SGreg Roach /** 349a25f0a04SGreg Roach * Check whether this fact is protected against edit 350a25f0a04SGreg Roach * 351cbc1590aSGreg Roach * @return bool 352a25f0a04SGreg Roach */ 3538f53f488SRico Sonntag public function canEdit(): bool 354c1010edaSGreg Roach { 3551450f098SGreg Roach if ($this->isPendingDeletion()) { 3561450f098SGreg Roach return false; 3571450f098SGreg Roach } 3581450f098SGreg Roach 3591450f098SGreg Roach if (Auth::isManager($this->record->tree())) { 3601450f098SGreg Roach return true; 3611450f098SGreg Roach } 3621450f098SGreg Roach 363a25f0a04SGreg Roach // Members cannot edit RESN, CHAN and locked records 3641450f098SGreg Roach return Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() !== 'RESN' && $this->getTag() !== 'CHAN'; 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach 367a25f0a04SGreg Roach /** 368a25f0a04SGreg Roach * The place where the event occured. 369a25f0a04SGreg Roach * 370a25f0a04SGreg Roach * @return Place 371a25f0a04SGreg Roach */ 3724fb14fcbSGreg Roach public function place(): Place 373c1010edaSGreg Roach { 374a25f0a04SGreg Roach if ($this->place === null) { 375f4afa648SGreg Roach $this->place = new Place($this->attribute('PLAC'), $this->record()->tree()); 376a25f0a04SGreg Roach } 377a25f0a04SGreg Roach 378a25f0a04SGreg Roach return $this->place; 379a25f0a04SGreg Roach } 380a25f0a04SGreg Roach 381a25f0a04SGreg Roach /** 382a25f0a04SGreg Roach * Get the date for this fact. 383a25f0a04SGreg Roach * We can call this function many times, especially when sorting, 384a25f0a04SGreg Roach * so keep a copy of the date. 385a25f0a04SGreg Roach * 386a25f0a04SGreg Roach * @return Date 387a25f0a04SGreg Roach */ 3882decada7SGreg Roach public function date(): Date 389c1010edaSGreg Roach { 390a25f0a04SGreg Roach if ($this->date === null) { 3913425616eSGreg Roach $this->date = new Date($this->attribute('DATE')); 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach 394a25f0a04SGreg Roach return $this->date; 395a25f0a04SGreg Roach } 396a25f0a04SGreg Roach 397a25f0a04SGreg Roach /** 398a25f0a04SGreg Roach * The raw GEDCOM data for this fact 399a25f0a04SGreg Roach * 400a25f0a04SGreg Roach * @return string 401a25f0a04SGreg Roach */ 402138ca96cSGreg Roach public function gedcom(): string 403c1010edaSGreg Roach { 404a25f0a04SGreg Roach return $this->gedcom; 405a25f0a04SGreg Roach } 406a25f0a04SGreg Roach 407a25f0a04SGreg Roach /** 408a25f0a04SGreg Roach * Get a (pseudo) primary key for this fact. 409a25f0a04SGreg Roach * 410a25f0a04SGreg Roach * @return string 411a25f0a04SGreg Roach */ 412905ab80aSGreg Roach public function id(): string 413c1010edaSGreg Roach { 414905ab80aSGreg Roach return $this->id; 415a25f0a04SGreg Roach } 416a25f0a04SGreg Roach 417a25f0a04SGreg Roach /** 418a25f0a04SGreg Roach * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 419a25f0a04SGreg Roach * 420a25f0a04SGreg Roach * @return string 421a25f0a04SGreg Roach */ 4228f53f488SRico Sonntag public function getTag(): string 423c1010edaSGreg Roach { 424a25f0a04SGreg Roach return $this->tag; 425a25f0a04SGreg Roach } 426a25f0a04SGreg Roach 427a25f0a04SGreg Roach /** 428a25f0a04SGreg Roach * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL) 429a25f0a04SGreg Roach * 430a25f0a04SGreg Roach * @param string $tag 4317e96c925SGreg Roach * 4327e96c925SGreg Roach * @return void 433a25f0a04SGreg Roach */ 434e364afe4SGreg Roach public function setTag($tag): void 435c1010edaSGreg Roach { 436a25f0a04SGreg Roach $this->tag = $tag; 437a25f0a04SGreg Roach } 438a25f0a04SGreg Roach 439a25f0a04SGreg Roach /** 440a25f0a04SGreg Roach * The Person/Family record where this Fact came from 441a25f0a04SGreg Roach * 4426e59b7c4SGreg Roach * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord 443a25f0a04SGreg Roach */ 444e7766c08SGreg Roach public function record() 445c1010edaSGreg Roach { 446e7766c08SGreg Roach return $this->record; 447a25f0a04SGreg Roach } 448a25f0a04SGreg Roach 449a25f0a04SGreg Roach /** 450a25f0a04SGreg Roach * Get the name of this fact type, for use as a label. 451a25f0a04SGreg Roach * 452a25f0a04SGreg Roach * @return string 453a25f0a04SGreg Roach */ 4547b7d8067SGreg Roach public function label(): string 455c1010edaSGreg Roach { 456a25f0a04SGreg Roach // Custom FACT/EVEN - with a TYPE 4573425616eSGreg Roach if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') { 4583425616eSGreg Roach return I18N::translate(e($this->attribute('TYPE'))); 459a25f0a04SGreg Roach } 4604e5adf68SGreg Roach 461e7766c08SGreg Roach return GedcomTag::getLabel($this->tag, $this->record); 462a25f0a04SGreg Roach } 463a25f0a04SGreg Roach 464a25f0a04SGreg Roach /** 465a25f0a04SGreg Roach * This is a newly deleted fact, pending approval. 4667e96c925SGreg Roach * 4677e96c925SGreg Roach * @return void 468a25f0a04SGreg Roach */ 469e364afe4SGreg Roach public function setPendingDeletion(): void 470c1010edaSGreg Roach { 471a25f0a04SGreg Roach $this->pending_deletion = true; 472a25f0a04SGreg Roach $this->pending_addition = false; 473a25f0a04SGreg Roach } 474a25f0a04SGreg Roach 475a25f0a04SGreg Roach /** 476a25f0a04SGreg Roach * Is this a newly deleted fact, pending approval. 477a25f0a04SGreg Roach * 478cbc1590aSGreg Roach * @return bool 479a25f0a04SGreg Roach */ 4808f53f488SRico Sonntag public function isPendingDeletion(): bool 481c1010edaSGreg Roach { 482a25f0a04SGreg Roach return $this->pending_deletion; 483a25f0a04SGreg Roach } 484a25f0a04SGreg Roach 485a25f0a04SGreg Roach /** 486a25f0a04SGreg Roach * This is a newly added fact, pending approval. 4877e96c925SGreg Roach * 4887e96c925SGreg Roach * @return void 489a25f0a04SGreg Roach */ 490e364afe4SGreg Roach public function setPendingAddition(): void 491c1010edaSGreg Roach { 492a25f0a04SGreg Roach $this->pending_addition = true; 493a25f0a04SGreg Roach $this->pending_deletion = false; 494a25f0a04SGreg Roach } 495a25f0a04SGreg Roach 496a25f0a04SGreg Roach /** 497a25f0a04SGreg Roach * Is this a newly added fact, pending approval. 498a25f0a04SGreg Roach * 499cbc1590aSGreg Roach * @return bool 500a25f0a04SGreg Roach */ 5018f53f488SRico Sonntag public function isPendingAddition(): bool 502c1010edaSGreg Roach { 503a25f0a04SGreg Roach return $this->pending_addition; 504a25f0a04SGreg Roach } 505a25f0a04SGreg Roach 506a25f0a04SGreg Roach /** 507a25f0a04SGreg Roach * Source citations linked to this fact 508a25f0a04SGreg Roach * 509a25f0a04SGreg Roach * @return string[] 510a25f0a04SGreg Roach */ 5118f53f488SRico Sonntag public function getCitations(): array 512c1010edaSGreg Roach { 5138d0ebef0SGreg Roach preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER); 51413abd6f3SGreg Roach $citations = []; 515a25f0a04SGreg Roach foreach ($matches as $match) { 516f4afa648SGreg Roach $source = Source::getInstance($match[2], $this->record()->tree()); 5172859ecedSJonathan Jaubart if ($source && $source->canShow()) { 518a25f0a04SGreg Roach $citations[] = $match[1]; 519a25f0a04SGreg Roach } 520a25f0a04SGreg Roach } 521a25f0a04SGreg Roach 522a25f0a04SGreg Roach return $citations; 523a25f0a04SGreg Roach } 524a25f0a04SGreg Roach 525a25f0a04SGreg Roach /** 526a25f0a04SGreg Roach * Notes (inline and objects) linked to this fact 527a25f0a04SGreg Roach * 528a25f0a04SGreg Roach * @return string[]|Note[] 529a25f0a04SGreg Roach */ 5308f53f488SRico Sonntag public function getNotes(): array 531c1010edaSGreg Roach { 53213abd6f3SGreg Roach $notes = []; 533138ca96cSGreg Roach preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches); 534a25f0a04SGreg Roach foreach ($matches[1] as $match) { 535a25f0a04SGreg Roach $note = preg_replace("/\n3 CONT ?/", "\n", $match); 5368d0ebef0SGreg Roach if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) { 537f4afa648SGreg Roach $note = Note::getInstance($nmatch[1], $this->record()->tree()); 538a25f0a04SGreg Roach if ($note && $note->canShow()) { 539a25f0a04SGreg Roach // A note object 540a25f0a04SGreg Roach $notes[] = $note; 541a25f0a04SGreg Roach } 542a25f0a04SGreg Roach } else { 543a25f0a04SGreg Roach // An inline note 544a25f0a04SGreg Roach $notes[] = $note; 545a25f0a04SGreg Roach } 546a25f0a04SGreg Roach } 547a25f0a04SGreg Roach 548a25f0a04SGreg Roach return $notes; 549a25f0a04SGreg Roach } 550a25f0a04SGreg Roach 551a25f0a04SGreg Roach /** 552a25f0a04SGreg Roach * Media objects linked to this fact 553a25f0a04SGreg Roach * 554a25f0a04SGreg Roach * @return Media[] 555a25f0a04SGreg Roach */ 5568f53f488SRico Sonntag public function getMedia(): array 557c1010edaSGreg Roach { 55813abd6f3SGreg Roach $media = []; 5598d0ebef0SGreg Roach preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches); 560a25f0a04SGreg Roach foreach ($matches[1] as $match) { 561f4afa648SGreg Roach $obje = Media::getInstance($match, $this->record()->tree()); 5622859ecedSJonathan Jaubart if ($obje && $obje->canShow()) { 563a25f0a04SGreg Roach $media[] = $obje; 564a25f0a04SGreg Roach } 565a25f0a04SGreg Roach } 566a25f0a04SGreg Roach 567a25f0a04SGreg Roach return $media; 568a25f0a04SGreg Roach } 569a25f0a04SGreg Roach 570a25f0a04SGreg Roach /** 571a25f0a04SGreg Roach * A one-line summary of the fact - for charts, etc. 572a25f0a04SGreg Roach * 573a25f0a04SGreg Roach * @return string 574a25f0a04SGreg Roach */ 5758f53f488SRico Sonntag public function summary(): string 576c1010edaSGreg Roach { 57713abd6f3SGreg Roach $attributes = []; 578dc124885SGreg Roach $target = $this->target(); 579db7bb364SGreg Roach if ($target instanceof GedcomRecord) { 58039ca88baSGreg Roach $attributes[] = $target->fullName(); 581a25f0a04SGreg Roach } else { 582726d7b07SGreg Roach // Fact value 58384586c02SGreg Roach $value = $this->value(); 584726d7b07SGreg Roach if ($value !== '' && $value !== 'Y') { 585d53324c9SGreg Roach $attributes[] = '<span dir="auto">' . e($value) . '</span>'; 586a25f0a04SGreg Roach } 587726d7b07SGreg Roach // Fact date 5882decada7SGreg Roach $date = $this->date(); 589726d7b07SGreg Roach if ($date->isOK()) { 590e364afe4SGreg Roach if ($this->record() instanceof Individual && in_array($this->getTag(), Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) { 591e7766c08SGreg Roach $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date); 592a25f0a04SGreg Roach } else { 593a25f0a04SGreg Roach $attributes[] = $date->display(); 594a25f0a04SGreg Roach } 595726d7b07SGreg Roach } 596726d7b07SGreg Roach // Fact place 597e364afe4SGreg Roach if ($this->place()->gedcomName() !== '') { 598392561bbSGreg Roach $attributes[] = $this->place()->shortName(); 599a25f0a04SGreg Roach } 600a25f0a04SGreg Roach } 6012d8f08abSGreg Roach 6022d8f08abSGreg Roach $class = 'fact_' . $this->getTag(); 603a25f0a04SGreg Roach if ($this->isPendingAddition()) { 60471a69701SGreg Roach $class .= ' wt-new'; 605a25f0a04SGreg Roach } elseif ($this->isPendingDeletion()) { 60671a69701SGreg Roach $class .= ' wt-old'; 607a25f0a04SGreg Roach } 6082d8f08abSGreg Roach 6092d8f08abSGreg Roach return 6102d8f08abSGreg Roach '<div class="' . $class . '">' . 6112d8f08abSGreg Roach /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ 6127b7d8067SGreg Roach I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) . 6132d8f08abSGreg Roach '</div>'; 614a25f0a04SGreg Roach } 615a25f0a04SGreg Roach 616a25f0a04SGreg Roach /** 617580a4d11SGreg Roach * Helper functions to sort facts 618a25f0a04SGreg Roach * 619580a4d11SGreg Roach * @return Closure 620a25f0a04SGreg Roach */ 621580a4d11SGreg Roach private static function dateComparator(): Closure 622c1010edaSGreg Roach { 6236c2179e2SGreg Roach return static function (Fact $a, Fact $b): int { 6242decada7SGreg Roach if ($a->date()->isOK() && $b->date()->isOK()) { 625a25f0a04SGreg Roach // If both events have dates, compare by date 6262decada7SGreg Roach $ret = Date::compare($a->date(), $b->date()); 627a25f0a04SGreg Roach 628dfa848b8SGreg Roach if ($ret === 0) { 629580a4d11SGreg Roach // If dates overlap, compare by fact type 630580a4d11SGreg Roach $ret = self::typeComparator()($a, $b); 631a25f0a04SGreg Roach 632a25f0a04SGreg Roach // If the fact type is also the same, retain the initial order 633dfa848b8SGreg Roach if ($ret === 0) { 634580a4d11SGreg Roach $ret = $a->sortOrder <=> $b->sortOrder; 635a25f0a04SGreg Roach } 636a25f0a04SGreg Roach } 637a25f0a04SGreg Roach 638a25f0a04SGreg Roach return $ret; 639b2ce94c6SRico Sonntag } 640b2ce94c6SRico Sonntag 641a25f0a04SGreg Roach // One or both events have no date - retain the initial order 642580a4d11SGreg Roach return $a->sortOrder <=> $b->sortOrder; 643580a4d11SGreg Roach }; 644a25f0a04SGreg Roach } 645a25f0a04SGreg Roach 646a25f0a04SGreg Roach /** 647580a4d11SGreg Roach * Helper functions to sort facts. 648a25f0a04SGreg Roach * 649580a4d11SGreg Roach * @return Closure 650a25f0a04SGreg Roach */ 651580a4d11SGreg Roach public static function typeComparator(): Closure 652c1010edaSGreg Roach { 653bbe4546fSGreg Roach static $factsort = []; 654a25f0a04SGreg Roach 65554c1ab5eSGreg Roach if ($factsort === []) { 656bbe4546fSGreg Roach $factsort = array_flip(self::FACT_ORDER); 657a25f0a04SGreg Roach } 658a25f0a04SGreg Roach 6596c2179e2SGreg Roach return static function (Fact $a, Fact $b) use ($factsort): int { 660a25f0a04SGreg Roach // Facts from same families stay grouped together 661a25f0a04SGreg Roach // Keep MARR and DIV from the same families from mixing with events from other FAMs 662a25f0a04SGreg Roach // Use the original order in which the facts were added 663e7766c08SGreg Roach if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) { 664a25f0a04SGreg Roach return $a->sortOrder - $b->sortOrder; 665a25f0a04SGreg Roach } 666a25f0a04SGreg Roach 667a25f0a04SGreg Roach $atag = $a->getTag(); 668a25f0a04SGreg Roach $btag = $b->getTag(); 669a25f0a04SGreg Roach 670a25f0a04SGreg Roach // Events not in the above list get mapped onto one that is. 671a25f0a04SGreg Roach if (!array_key_exists($atag, $factsort)) { 672a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) { 673a25f0a04SGreg Roach $atag = $match[1]; 674a25f0a04SGreg Roach } else { 6757a6ee1acSGreg Roach $atag = '_????_'; 676a25f0a04SGreg Roach } 677a25f0a04SGreg Roach } 678a25f0a04SGreg Roach 679a25f0a04SGreg Roach if (!array_key_exists($btag, $factsort)) { 680a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) { 681a25f0a04SGreg Roach $btag = $match[1]; 682a25f0a04SGreg Roach } else { 6837a6ee1acSGreg Roach $btag = '_????_'; 684a25f0a04SGreg Roach } 685a25f0a04SGreg Roach } 686a25f0a04SGreg Roach 687a25f0a04SGreg Roach // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI 688a25f0a04SGreg Roach // - Treat dated after BURI facts as BURI instead 6893425616eSGreg Roach if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { 690a25f0a04SGreg Roach $atag = 'BURI'; 691a25f0a04SGreg Roach } 692a25f0a04SGreg Roach 6933425616eSGreg Roach if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { 694a25f0a04SGreg Roach $btag = 'BURI'; 695a25f0a04SGreg Roach } 696a25f0a04SGreg Roach 697a25f0a04SGreg Roach $ret = $factsort[$atag] - $factsort[$btag]; 698a25f0a04SGreg Roach 699a25f0a04SGreg Roach // If facts are the same then put dated facts before non-dated facts 700a25f0a04SGreg Roach if ($ret == 0) { 7013425616eSGreg Roach if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') { 702a25f0a04SGreg Roach return -1; 703a25f0a04SGreg Roach } 704a25f0a04SGreg Roach 7053425616eSGreg Roach if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') { 706a25f0a04SGreg Roach return 1; 707a25f0a04SGreg Roach } 708a25f0a04SGreg Roach 709a25f0a04SGreg Roach // If no sorting preference, then keep original ordering 710a25f0a04SGreg Roach $ret = $a->sortOrder - $b->sortOrder; 711a25f0a04SGreg Roach } 712a25f0a04SGreg Roach 713a25f0a04SGreg Roach return $ret; 714580a4d11SGreg Roach }; 715580a4d11SGreg Roach } 716580a4d11SGreg Roach 717580a4d11SGreg Roach /** 718580a4d11SGreg Roach * A multi-key sort 719580a4d11SGreg Roach * 1. First divide the facts into two arrays one set with dates and one set without dates 720580a4d11SGreg Roach * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated 721580a4d11SGreg Roach * using the compare type function 722580a4d11SGreg Roach * 3. Then merge the arrays back into the original array using the compare type function 723580a4d11SGreg Roach * 724b5c8fd7eSGreg Roach * @param Collection<Fact> $unsorted 725580a4d11SGreg Roach * 726b5c8fd7eSGreg Roach * @return Collection<Fact> 727580a4d11SGreg Roach */ 7288af3e5c1SGreg Roach public static function sortFacts(Collection $unsorted): Collection 729580a4d11SGreg Roach { 730580a4d11SGreg Roach $dated = []; 731580a4d11SGreg Roach $nondated = []; 732580a4d11SGreg Roach $sorted = []; 733580a4d11SGreg Roach 734580a4d11SGreg Roach // Split the array into dated and non-dated arrays 735580a4d11SGreg Roach $order = 0; 736580a4d11SGreg Roach 737580a4d11SGreg Roach foreach ($unsorted as $fact) { 738580a4d11SGreg Roach $fact->sortOrder = $order; 739580a4d11SGreg Roach $order++; 740580a4d11SGreg Roach 741580a4d11SGreg Roach if ($fact->date()->isOK()) { 742580a4d11SGreg Roach $dated[] = $fact; 743580a4d11SGreg Roach } else { 744580a4d11SGreg Roach $nondated[] = $fact; 745580a4d11SGreg Roach } 746580a4d11SGreg Roach } 747580a4d11SGreg Roach 748580a4d11SGreg Roach usort($dated, self::dateComparator()); 749580a4d11SGreg Roach usort($nondated, self::typeComparator()); 750580a4d11SGreg Roach 751580a4d11SGreg Roach // Merge the arrays 752580a4d11SGreg Roach $dc = count($dated); 753580a4d11SGreg Roach $nc = count($nondated); 754580a4d11SGreg Roach $i = 0; 755580a4d11SGreg Roach $j = 0; 756580a4d11SGreg Roach 757580a4d11SGreg Roach // while there is anything in the dated array continue merging 758580a4d11SGreg Roach while ($i < $dc) { 759580a4d11SGreg Roach // compare each fact by type to merge them in order 760580a4d11SGreg Roach if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) { 761580a4d11SGreg Roach $sorted[] = $nondated[$j]; 762580a4d11SGreg Roach $j++; 763580a4d11SGreg Roach } else { 764580a4d11SGreg Roach $sorted[] = $dated[$i]; 765580a4d11SGreg Roach $i++; 766580a4d11SGreg Roach } 767580a4d11SGreg Roach } 768580a4d11SGreg Roach 769580a4d11SGreg Roach // get anything that might be left in the nondated array 770580a4d11SGreg Roach while ($j < $nc) { 771580a4d11SGreg Roach $sorted[] = $nondated[$j]; 772580a4d11SGreg Roach $j++; 773580a4d11SGreg Roach } 774580a4d11SGreg Roach 7751c7a6874SGreg Roach return new Collection($sorted); 776a25f0a04SGreg Roach } 777a25f0a04SGreg Roach 778a25f0a04SGreg Roach /** 779ae71f733SGreg Roach * Sort fact/event tags using the same order that we use for facts. 780ae71f733SGreg Roach * 781ae71f733SGreg Roach * @param Collection<string> $unsorted 782ae71f733SGreg Roach * 783ae71f733SGreg Roach * @return Collection<string> 784ae71f733SGreg Roach */ 785ae71f733SGreg Roach public static function sortFactTags(Collection $unsorted): Collection 786ae71f733SGreg Roach { 787ae71f733SGreg Roach $tag_order = array_flip(self::FACT_ORDER); 788ae71f733SGreg Roach 789ae71f733SGreg Roach return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int { 790ae71f733SGreg Roach $sort_x = $tag_order[$x] ?? $tag_order['_????_']; 791ae71f733SGreg Roach $sort_y = $tag_order[$y] ?? $tag_order['_????_']; 792ae71f733SGreg Roach 793ae71f733SGreg Roach return $sort_x - $sort_y; 794ae71f733SGreg Roach }); 795ae71f733SGreg Roach } 796ae71f733SGreg Roach 797ae71f733SGreg Roach /** 798a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 799a25f0a04SGreg Roach * 800a25f0a04SGreg Roach * @return string 801a25f0a04SGreg Roach */ 802dfa848b8SGreg Roach public function __toString(): string 803c1010edaSGreg Roach { 804c0935879SGreg Roach return $this->id . '@' . $this->record->xref(); 805a25f0a04SGreg Roach } 806a25f0a04SGreg Roach} 807