1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 1976692c8bSGreg Roach 20*580a4d11SGreg Roachuse Closure; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 228af6bbf8SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService; 238f5f5da8SGreg Roachuse InvalidArgumentException; 24a25f0a04SGreg Roach 25a25f0a04SGreg Roach/** 2676692c8bSGreg Roach * A GEDCOM fact or event object. 27a25f0a04SGreg Roach */ 28c1010edaSGreg Roachclass Fact 29c1010edaSGreg Roach{ 3016d6367aSGreg Roach private const FACT_ORDER = [ 31bbe4546fSGreg Roach 'BIRT', 32bbe4546fSGreg Roach '_HNM', 33bbe4546fSGreg Roach 'ALIA', 34bbe4546fSGreg Roach '_AKA', 35bbe4546fSGreg Roach '_AKAN', 36bbe4546fSGreg Roach 'ADOP', 37bbe4546fSGreg Roach '_ADPF', 38bbe4546fSGreg Roach '_ADPF', 39bbe4546fSGreg Roach '_BRTM', 40bbe4546fSGreg Roach 'CHR', 41bbe4546fSGreg Roach 'BAPM', 42bbe4546fSGreg Roach 'FCOM', 43bbe4546fSGreg Roach 'CONF', 44bbe4546fSGreg Roach 'BARM', 45bbe4546fSGreg Roach 'BASM', 46bbe4546fSGreg Roach 'EDUC', 47bbe4546fSGreg Roach 'GRAD', 48bbe4546fSGreg Roach '_DEG', 49bbe4546fSGreg Roach 'EMIG', 50bbe4546fSGreg Roach 'IMMI', 51bbe4546fSGreg Roach 'NATU', 52bbe4546fSGreg Roach '_MILI', 53bbe4546fSGreg Roach '_MILT', 54bbe4546fSGreg Roach 'ENGA', 55bbe4546fSGreg Roach 'MARB', 56bbe4546fSGreg Roach 'MARC', 57bbe4546fSGreg Roach 'MARL', 58bbe4546fSGreg Roach '_MARI', 59bbe4546fSGreg Roach '_MBON', 60bbe4546fSGreg Roach 'MARR', 61bbe4546fSGreg Roach 'MARR_CIVIL', 62bbe4546fSGreg Roach 'MARR_RELIGIOUS', 63bbe4546fSGreg Roach 'MARR_PARTNERS', 64bbe4546fSGreg Roach 'MARR_UNKNOWN', 65bbe4546fSGreg Roach '_COML', 66bbe4546fSGreg Roach '_STAT', 67bbe4546fSGreg Roach '_SEPR', 68bbe4546fSGreg Roach 'DIVF', 69bbe4546fSGreg Roach 'MARS', 70bbe4546fSGreg Roach '_BIRT_CHIL', 71bbe4546fSGreg Roach 'DIV', 72bbe4546fSGreg Roach 'ANUL', 73bbe4546fSGreg Roach '_BIRT_', 74bbe4546fSGreg Roach '_MARR_', 75bbe4546fSGreg Roach '_DEAT_', 76bbe4546fSGreg Roach '_BURI_', 77bbe4546fSGreg Roach 'CENS', 78bbe4546fSGreg Roach 'OCCU', 79bbe4546fSGreg Roach 'RESI', 80bbe4546fSGreg Roach 'PROP', 81bbe4546fSGreg Roach 'CHRA', 82bbe4546fSGreg Roach 'RETI', 83bbe4546fSGreg Roach 'FACT', 84bbe4546fSGreg Roach 'EVEN', 85bbe4546fSGreg Roach '_NMR', 86bbe4546fSGreg Roach '_NMAR', 87bbe4546fSGreg Roach 'NMR', 88bbe4546fSGreg Roach 'NCHI', 89bbe4546fSGreg Roach 'WILL', 90bbe4546fSGreg Roach '_HOL', 91bbe4546fSGreg Roach '_????_', 92bbe4546fSGreg Roach 'DEAT', 93bbe4546fSGreg Roach '_FNRL', 94bbe4546fSGreg Roach 'CREM', 95bbe4546fSGreg Roach 'BURI', 96bbe4546fSGreg Roach '_INTE', 97bbe4546fSGreg Roach '_YART', 98bbe4546fSGreg Roach '_NLIV', 99bbe4546fSGreg Roach 'PROB', 100bbe4546fSGreg Roach 'TITL', 101bbe4546fSGreg Roach 'COMM', 102bbe4546fSGreg Roach 'NATI', 103bbe4546fSGreg Roach 'CITN', 104bbe4546fSGreg Roach 'CAST', 105bbe4546fSGreg Roach 'RELI', 106bbe4546fSGreg Roach 'SSN', 107bbe4546fSGreg Roach 'IDNO', 108bbe4546fSGreg Roach 'TEMP', 109bbe4546fSGreg Roach 'SLGC', 110bbe4546fSGreg Roach 'BAPL', 111bbe4546fSGreg Roach 'CONL', 112bbe4546fSGreg Roach 'ENDL', 113bbe4546fSGreg Roach 'SLGS', 114bbe4546fSGreg Roach 'ADDR', 115bbe4546fSGreg Roach 'PHON', 116bbe4546fSGreg Roach 'EMAIL', 117bbe4546fSGreg Roach '_EMAIL', 118bbe4546fSGreg Roach 'EMAL', 119bbe4546fSGreg Roach 'FAX', 120bbe4546fSGreg Roach 'WWW', 121bbe4546fSGreg Roach 'URL', 122bbe4546fSGreg Roach '_URL', 123bbe4546fSGreg Roach 'AFN', 124bbe4546fSGreg Roach 'REFN', 125bbe4546fSGreg Roach '_PRMN', 126bbe4546fSGreg Roach 'REF', 127bbe4546fSGreg Roach 'RIN', 128bbe4546fSGreg Roach '_UID', 129bbe4546fSGreg Roach 'OBJE', 130bbe4546fSGreg Roach 'NOTE', 131bbe4546fSGreg Roach 'SOUR', 132bbe4546fSGreg Roach 'CHAN', 133bbe4546fSGreg Roach '_TODO', 134bbe4546fSGreg Roach ]; 135bbe4546fSGreg Roach 136a25f0a04SGreg Roach /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */ 137905ab80aSGreg Roach private $id; 138a25f0a04SGreg Roach 139a25f0a04SGreg Roach /** @var GedcomRecord The GEDCOM record from which this fact is taken */ 140e7766c08SGreg Roach private $record; 141a25f0a04SGreg Roach 142a25f0a04SGreg Roach /** @var string The raw GEDCOM data for this fact */ 143a25f0a04SGreg Roach private $gedcom; 144a25f0a04SGreg Roach 145a25f0a04SGreg Roach /** @var string The GEDCOM tag for this record */ 146a25f0a04SGreg Roach private $tag; 147a25f0a04SGreg Roach 148cbc1590aSGreg Roach /** @var bool Is this a recently deleted fact, pending approval? */ 149a25f0a04SGreg Roach private $pending_deletion = false; 150a25f0a04SGreg Roach 151cbc1590aSGreg Roach /** @var bool Is this a recently added fact, pending approval? */ 152a25f0a04SGreg Roach private $pending_addition = false; 153a25f0a04SGreg Roach 154a25f0a04SGreg Roach /** @var Date The date of this fact, from the “2 DATE …” attribute */ 155a25f0a04SGreg Roach private $date; 156a25f0a04SGreg Roach 157a25f0a04SGreg Roach /** @var Place The place of this fact, from the “2 PLAC …” attribute */ 158a25f0a04SGreg Roach private $place; 159a25f0a04SGreg Roach 160*580a4d11SGreg Roach /** @var int Used by Functions::sortFacts() */ 161*580a4d11SGreg Roach private $sortOrder; 162a25f0a04SGreg Roach 163a25f0a04SGreg Roach /** 164a25f0a04SGreg Roach * Create an event object from a gedcom fragment. 165a25f0a04SGreg Roach * We need the parent object (to check privacy) and a (pseudo) fact ID to 166a25f0a04SGreg Roach * identify the fact within the record. 167a25f0a04SGreg Roach * 168a25f0a04SGreg Roach * @param string $gedcom 169a25f0a04SGreg Roach * @param GedcomRecord $parent 170905ab80aSGreg Roach * @param string $id 171a25f0a04SGreg Roach * 1728f5f5da8SGreg Roach * @throws InvalidArgumentException 173a25f0a04SGreg Roach */ 174905ab80aSGreg Roach public function __construct($gedcom, GedcomRecord $parent, $id) 175c1010edaSGreg Roach { 1768d0ebef0SGreg Roach if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) { 177a25f0a04SGreg Roach $this->gedcom = $gedcom; 178e7766c08SGreg Roach $this->record = $parent; 179905ab80aSGreg Roach $this->id = $id; 180a25f0a04SGreg Roach $this->tag = $match[1]; 181a25f0a04SGreg Roach } else { 1828f5f5da8SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')'); 183a25f0a04SGreg Roach } 184a25f0a04SGreg Roach } 185a25f0a04SGreg Roach 186a25f0a04SGreg Roach /** 187a25f0a04SGreg Roach * Get the value of level 1 data in the fact 188a25f0a04SGreg Roach * Allow for multi-line values 189a25f0a04SGreg Roach * 190baacc364SGreg Roach * @return string 191a25f0a04SGreg Roach */ 192dfa848b8SGreg Roach public function value(): string 193c1010edaSGreg Roach { 194a25f0a04SGreg Roach if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) { 195a25f0a04SGreg Roach return preg_replace("/\n2 CONT ?/", "\n", $match[1]); 196a25f0a04SGreg Roach } 197b2ce94c6SRico Sonntag 198b2ce94c6SRico Sonntag return ''; 199a25f0a04SGreg Roach } 200a25f0a04SGreg Roach 201a25f0a04SGreg Roach /** 202a25f0a04SGreg Roach * Get the record to which this fact links 203a25f0a04SGreg Roach * 204188adf12SGreg Roach * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord|null 205a25f0a04SGreg Roach */ 206dc124885SGreg Roach public function target() 207c1010edaSGreg Roach { 20884586c02SGreg Roach $xref = trim($this->value(), '@'); 209a25f0a04SGreg Roach switch ($this->tag) { 210a25f0a04SGreg Roach case 'FAMC': 211a25f0a04SGreg Roach case 'FAMS': 212f4afa648SGreg Roach return Family::getInstance($xref, $this->record()->tree()); 213a25f0a04SGreg Roach case 'HUSB': 214a25f0a04SGreg Roach case 'WIFE': 215a25f0a04SGreg Roach case 'CHIL': 216f4afa648SGreg Roach return Individual::getInstance($xref, $this->record()->tree()); 217a25f0a04SGreg Roach case 'SOUR': 218f4afa648SGreg Roach return Source::getInstance($xref, $this->record()->tree()); 219a25f0a04SGreg Roach case 'OBJE': 220f4afa648SGreg Roach return Media::getInstance($xref, $this->record()->tree()); 221a25f0a04SGreg Roach case 'REPO': 222f4afa648SGreg Roach return Repository::getInstance($xref, $this->record()->tree()); 223a25f0a04SGreg Roach case 'NOTE': 224f4afa648SGreg Roach return Note::getInstance($xref, $this->record()->tree()); 225a25f0a04SGreg Roach default: 226f4afa648SGreg Roach return GedcomRecord::getInstance($xref, $this->record()->tree()); 227a25f0a04SGreg Roach } 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach 230a25f0a04SGreg Roach /** 231a25f0a04SGreg Roach * Get the value of level 2 data in the fact 232a25f0a04SGreg Roach * 233a25f0a04SGreg Roach * @param string $tag 234a25f0a04SGreg Roach * 235baacc364SGreg Roach * @return string 236a25f0a04SGreg Roach */ 237dfa848b8SGreg Roach public function attribute($tag): string 238c1010edaSGreg Roach { 239a25f0a04SGreg Roach if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { 240a25f0a04SGreg Roach return preg_replace("/\n3 CONT ?/", "\n", $match[1]); 241a25f0a04SGreg Roach } 242b2ce94c6SRico Sonntag 243b2ce94c6SRico Sonntag return ''; 244a25f0a04SGreg Roach } 245a25f0a04SGreg Roach 246a25f0a04SGreg Roach /** 2478af6bbf8SGreg Roach * Get the PLAC:MAP:LATI for the fact. 2488af6bbf8SGreg Roach * 2498af6bbf8SGreg Roach * @return float 2508af6bbf8SGreg Roach */ 2518af6bbf8SGreg Roach public function latitude(): float 2528af6bbf8SGreg Roach { 2538af6bbf8SGreg Roach if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) { 2548af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2558af6bbf8SGreg Roach 2568af6bbf8SGreg Roach return $gedcom_service->readLatitude($match[1]); 2578af6bbf8SGreg Roach } 2588af6bbf8SGreg Roach 2598af6bbf8SGreg Roach return 0.0; 2608af6bbf8SGreg Roach } 2618af6bbf8SGreg Roach 2628af6bbf8SGreg Roach /** 2638af6bbf8SGreg Roach * Get the PLAC:MAP:LONG for the fact. 2648af6bbf8SGreg Roach * 2658af6bbf8SGreg Roach * @return float 2668af6bbf8SGreg Roach */ 2678af6bbf8SGreg Roach public function longitude(): float 2688af6bbf8SGreg Roach { 269819ac503SRico Sonntag if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) { 2708af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2718af6bbf8SGreg Roach 2728af6bbf8SGreg Roach return $gedcom_service->readLongitude($match[1]); 2738af6bbf8SGreg Roach } 2748af6bbf8SGreg Roach 2758af6bbf8SGreg Roach return 0.0; 2768af6bbf8SGreg Roach } 2778af6bbf8SGreg Roach 2788af6bbf8SGreg Roach /** 279a25f0a04SGreg Roach * Do the privacy rules allow us to display this fact to the current user 280a25f0a04SGreg Roach * 281cbc1590aSGreg Roach * @param int|null $access_level 282a25f0a04SGreg Roach * 283cbc1590aSGreg Roach * @return bool 284a25f0a04SGreg Roach */ 28535584196SGreg Roach public function canShow(int $access_level = null): bool 286c1010edaSGreg Roach { 2874b9ff166SGreg Roach if ($access_level === null) { 288f4afa648SGreg Roach $access_level = Auth::accessLevel($this->record()->tree()); 2894b9ff166SGreg Roach } 2904b9ff166SGreg Roach 291a25f0a04SGreg Roach // Does this record have an explicit RESN? 29220ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN confidential") !== false) { 2934b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 294a25f0a04SGreg Roach } 29520ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN privacy") !== false) { 2964b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 297a25f0a04SGreg Roach } 29820ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN none") !== false) { 299a25f0a04SGreg Roach return true; 300a25f0a04SGreg Roach } 301a25f0a04SGreg Roach 302a25f0a04SGreg Roach // Does this record have a default RESN? 303c0935879SGreg Roach $xref = $this->record->xref(); 304f4afa648SGreg Roach $fact_privacy = $this->record->tree()->getFactPrivacy(); 305f4afa648SGreg Roach $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy(); 306518bbdc1SGreg Roach if (isset($individual_fact_privacy[$xref][$this->tag])) { 307518bbdc1SGreg Roach return $individual_fact_privacy[$xref][$this->tag] >= $access_level; 308a25f0a04SGreg Roach } 309518bbdc1SGreg Roach if (isset($fact_privacy[$this->tag])) { 310518bbdc1SGreg Roach return $fact_privacy[$this->tag] >= $access_level; 311a25f0a04SGreg Roach } 312a25f0a04SGreg Roach 313a25f0a04SGreg Roach // No restrictions - it must be public 314a25f0a04SGreg Roach return true; 315a25f0a04SGreg Roach } 316a25f0a04SGreg Roach 317a25f0a04SGreg Roach /** 318a25f0a04SGreg Roach * Check whether this fact is protected against edit 319a25f0a04SGreg Roach * 320cbc1590aSGreg Roach * @return bool 321a25f0a04SGreg Roach */ 3228f53f488SRico Sonntag public function canEdit(): bool 323c1010edaSGreg Roach { 324a25f0a04SGreg Roach // Managers can edit anything 325a25f0a04SGreg Roach // Members cannot edit RESN, CHAN and locked records 326a25f0a04SGreg Roach return 327e7766c08SGreg Roach $this->record->canEdit() && !$this->isPendingDeletion() && ( 328f4afa648SGreg Roach Auth::isManager($this->record->tree()) || 329f4afa648SGreg Roach Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() != 'RESN' && $this->getTag() != 'CHAN' 330a25f0a04SGreg Roach ); 331a25f0a04SGreg Roach } 332a25f0a04SGreg Roach 333a25f0a04SGreg Roach /** 334a25f0a04SGreg Roach * The place where the event occured. 335a25f0a04SGreg Roach * 336a25f0a04SGreg Roach * @return Place 337a25f0a04SGreg Roach */ 3384fb14fcbSGreg Roach public function place(): Place 339c1010edaSGreg Roach { 340a25f0a04SGreg Roach if ($this->place === null) { 341f4afa648SGreg Roach $this->place = new Place($this->attribute('PLAC'), $this->record()->tree()); 342a25f0a04SGreg Roach } 343a25f0a04SGreg Roach 344a25f0a04SGreg Roach return $this->place; 345a25f0a04SGreg Roach } 346a25f0a04SGreg Roach 347a25f0a04SGreg Roach /** 348a25f0a04SGreg Roach * Get the date for this fact. 349a25f0a04SGreg Roach * We can call this function many times, especially when sorting, 350a25f0a04SGreg Roach * so keep a copy of the date. 351a25f0a04SGreg Roach * 352a25f0a04SGreg Roach * @return Date 353a25f0a04SGreg Roach */ 3542decada7SGreg Roach public function date(): Date 355c1010edaSGreg Roach { 356a25f0a04SGreg Roach if ($this->date === null) { 3573425616eSGreg Roach $this->date = new Date($this->attribute('DATE')); 358a25f0a04SGreg Roach } 359a25f0a04SGreg Roach 360a25f0a04SGreg Roach return $this->date; 361a25f0a04SGreg Roach } 362a25f0a04SGreg Roach 363a25f0a04SGreg Roach /** 364a25f0a04SGreg Roach * The raw GEDCOM data for this fact 365a25f0a04SGreg Roach * 366a25f0a04SGreg Roach * @return string 367a25f0a04SGreg Roach */ 368138ca96cSGreg Roach public function gedcom(): string 369c1010edaSGreg Roach { 370a25f0a04SGreg Roach return $this->gedcom; 371a25f0a04SGreg Roach } 372a25f0a04SGreg Roach 373a25f0a04SGreg Roach /** 374a25f0a04SGreg Roach * Get a (pseudo) primary key for this fact. 375a25f0a04SGreg Roach * 376a25f0a04SGreg Roach * @return string 377a25f0a04SGreg Roach */ 378905ab80aSGreg Roach public function id(): string 379c1010edaSGreg Roach { 380905ab80aSGreg Roach return $this->id; 381a25f0a04SGreg Roach } 382a25f0a04SGreg Roach 383a25f0a04SGreg Roach /** 384a25f0a04SGreg Roach * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 385a25f0a04SGreg Roach * 386a25f0a04SGreg Roach * @return string 387a25f0a04SGreg Roach */ 3888f53f488SRico Sonntag public function getTag(): string 389c1010edaSGreg Roach { 390a25f0a04SGreg Roach return $this->tag; 391a25f0a04SGreg Roach } 392a25f0a04SGreg Roach 393a25f0a04SGreg Roach /** 394a25f0a04SGreg Roach * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL) 395a25f0a04SGreg Roach * 396a25f0a04SGreg Roach * @param string $tag 3977e96c925SGreg Roach * 3987e96c925SGreg Roach * @return void 399a25f0a04SGreg Roach */ 400c1010edaSGreg Roach public function setTag($tag) 401c1010edaSGreg Roach { 402a25f0a04SGreg Roach $this->tag = $tag; 403a25f0a04SGreg Roach } 404a25f0a04SGreg Roach 405a25f0a04SGreg Roach /** 406a25f0a04SGreg Roach * The Person/Family record where this Fact came from 407a25f0a04SGreg Roach * 4086e59b7c4SGreg Roach * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord 409a25f0a04SGreg Roach */ 410e7766c08SGreg Roach public function record() 411c1010edaSGreg Roach { 412e7766c08SGreg Roach return $this->record; 413a25f0a04SGreg Roach } 414a25f0a04SGreg Roach 415a25f0a04SGreg Roach /** 416a25f0a04SGreg Roach * Get the name of this fact type, for use as a label. 417a25f0a04SGreg Roach * 418a25f0a04SGreg Roach * @return string 419a25f0a04SGreg Roach */ 4207b7d8067SGreg Roach public function label(): string 421c1010edaSGreg Roach { 422a25f0a04SGreg Roach // Custom FACT/EVEN - with a TYPE 4233425616eSGreg Roach if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') { 4243425616eSGreg Roach return I18N::translate(e($this->attribute('TYPE'))); 425a25f0a04SGreg Roach } 4264e5adf68SGreg Roach 427e7766c08SGreg Roach return GedcomTag::getLabel($this->tag, $this->record); 428a25f0a04SGreg Roach } 429a25f0a04SGreg Roach 430a25f0a04SGreg Roach /** 431a25f0a04SGreg Roach * This is a newly deleted fact, pending approval. 4327e96c925SGreg Roach * 4337e96c925SGreg Roach * @return void 434a25f0a04SGreg Roach */ 435c1010edaSGreg Roach public function setPendingDeletion() 436c1010edaSGreg Roach { 437a25f0a04SGreg Roach $this->pending_deletion = true; 438a25f0a04SGreg Roach $this->pending_addition = false; 439a25f0a04SGreg Roach } 440a25f0a04SGreg Roach 441a25f0a04SGreg Roach /** 442a25f0a04SGreg Roach * Is this a newly deleted fact, pending approval. 443a25f0a04SGreg Roach * 444cbc1590aSGreg Roach * @return bool 445a25f0a04SGreg Roach */ 4468f53f488SRico Sonntag public function isPendingDeletion(): bool 447c1010edaSGreg Roach { 448a25f0a04SGreg Roach return $this->pending_deletion; 449a25f0a04SGreg Roach } 450a25f0a04SGreg Roach 451a25f0a04SGreg Roach /** 452a25f0a04SGreg Roach * This is a newly added fact, pending approval. 4537e96c925SGreg Roach * 4547e96c925SGreg Roach * @return void 455a25f0a04SGreg Roach */ 456c1010edaSGreg Roach public function setPendingAddition() 457c1010edaSGreg Roach { 458a25f0a04SGreg Roach $this->pending_addition = true; 459a25f0a04SGreg Roach $this->pending_deletion = false; 460a25f0a04SGreg Roach } 461a25f0a04SGreg Roach 462a25f0a04SGreg Roach /** 463a25f0a04SGreg Roach * Is this a newly added fact, pending approval. 464a25f0a04SGreg Roach * 465cbc1590aSGreg Roach * @return bool 466a25f0a04SGreg Roach */ 4678f53f488SRico Sonntag public function isPendingAddition(): bool 468c1010edaSGreg Roach { 469a25f0a04SGreg Roach return $this->pending_addition; 470a25f0a04SGreg Roach } 471a25f0a04SGreg Roach 472a25f0a04SGreg Roach /** 473a25f0a04SGreg Roach * Source citations linked to this fact 474a25f0a04SGreg Roach * 475a25f0a04SGreg Roach * @return string[] 476a25f0a04SGreg Roach */ 4778f53f488SRico Sonntag public function getCitations(): array 478c1010edaSGreg Roach { 4798d0ebef0SGreg Roach preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER); 48013abd6f3SGreg Roach $citations = []; 481a25f0a04SGreg Roach foreach ($matches as $match) { 482f4afa648SGreg Roach $source = Source::getInstance($match[2], $this->record()->tree()); 4832859ecedSJonathan Jaubart if ($source && $source->canShow()) { 484a25f0a04SGreg Roach $citations[] = $match[1]; 485a25f0a04SGreg Roach } 486a25f0a04SGreg Roach } 487a25f0a04SGreg Roach 488a25f0a04SGreg Roach return $citations; 489a25f0a04SGreg Roach } 490a25f0a04SGreg Roach 491a25f0a04SGreg Roach /** 492a25f0a04SGreg Roach * Notes (inline and objects) linked to this fact 493a25f0a04SGreg Roach * 494a25f0a04SGreg Roach * @return string[]|Note[] 495a25f0a04SGreg Roach */ 4968f53f488SRico Sonntag public function getNotes(): array 497c1010edaSGreg Roach { 49813abd6f3SGreg Roach $notes = []; 499138ca96cSGreg Roach preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches); 500a25f0a04SGreg Roach foreach ($matches[1] as $match) { 501a25f0a04SGreg Roach $note = preg_replace("/\n3 CONT ?/", "\n", $match); 5028d0ebef0SGreg Roach if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) { 503f4afa648SGreg Roach $note = Note::getInstance($nmatch[1], $this->record()->tree()); 504a25f0a04SGreg Roach if ($note && $note->canShow()) { 505a25f0a04SGreg Roach // A note object 506a25f0a04SGreg Roach $notes[] = $note; 507a25f0a04SGreg Roach } 508a25f0a04SGreg Roach } else { 509a25f0a04SGreg Roach // An inline note 510a25f0a04SGreg Roach $notes[] = $note; 511a25f0a04SGreg Roach } 512a25f0a04SGreg Roach } 513a25f0a04SGreg Roach 514a25f0a04SGreg Roach return $notes; 515a25f0a04SGreg Roach } 516a25f0a04SGreg Roach 517a25f0a04SGreg Roach /** 518a25f0a04SGreg Roach * Media objects linked to this fact 519a25f0a04SGreg Roach * 520a25f0a04SGreg Roach * @return Media[] 521a25f0a04SGreg Roach */ 5228f53f488SRico Sonntag public function getMedia(): array 523c1010edaSGreg Roach { 52413abd6f3SGreg Roach $media = []; 5258d0ebef0SGreg Roach preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches); 526a25f0a04SGreg Roach foreach ($matches[1] as $match) { 527f4afa648SGreg Roach $obje = Media::getInstance($match, $this->record()->tree()); 5282859ecedSJonathan Jaubart if ($obje && $obje->canShow()) { 529a25f0a04SGreg Roach $media[] = $obje; 530a25f0a04SGreg Roach } 531a25f0a04SGreg Roach } 532a25f0a04SGreg Roach 533a25f0a04SGreg Roach return $media; 534a25f0a04SGreg Roach } 535a25f0a04SGreg Roach 536a25f0a04SGreg Roach /** 537a25f0a04SGreg Roach * A one-line summary of the fact - for charts, etc. 538a25f0a04SGreg Roach * 539a25f0a04SGreg Roach * @return string 540a25f0a04SGreg Roach */ 5418f53f488SRico Sonntag public function summary(): string 542c1010edaSGreg Roach { 54313abd6f3SGreg Roach $attributes = []; 544dc124885SGreg Roach $target = $this->target(); 545db7bb364SGreg Roach if ($target instanceof GedcomRecord) { 54639ca88baSGreg Roach $attributes[] = $target->fullName(); 547a25f0a04SGreg Roach } else { 548726d7b07SGreg Roach // Fact value 54984586c02SGreg Roach $value = $this->value(); 550726d7b07SGreg Roach if ($value !== '' && $value !== 'Y') { 551d53324c9SGreg Roach $attributes[] = '<span dir="auto">' . e($value) . '</span>'; 552a25f0a04SGreg Roach } 553726d7b07SGreg Roach // Fact date 5542decada7SGreg Roach $date = $this->date(); 555726d7b07SGreg Roach if ($date->isOK()) { 5568d0ebef0SGreg Roach if (in_array($this->getTag(), Gedcom::BIRTH_EVENTS) && $this->record() instanceof Individual && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) { 557e7766c08SGreg Roach $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date); 558a25f0a04SGreg Roach } else { 559a25f0a04SGreg Roach $attributes[] = $date->display(); 560a25f0a04SGreg Roach } 561726d7b07SGreg Roach } 562726d7b07SGreg Roach // Fact place 563392561bbSGreg Roach if ($this->place()->gedcomName() <> '') { 564392561bbSGreg Roach $attributes[] = $this->place()->shortName(); 565a25f0a04SGreg Roach } 566a25f0a04SGreg Roach } 5672d8f08abSGreg Roach 5682d8f08abSGreg Roach $class = 'fact_' . $this->getTag(); 569a25f0a04SGreg Roach if ($this->isPendingAddition()) { 5702d8f08abSGreg Roach $class .= ' new'; 571a25f0a04SGreg Roach } elseif ($this->isPendingDeletion()) { 5722d8f08abSGreg Roach $class .= ' old'; 573a25f0a04SGreg Roach } 5742d8f08abSGreg Roach 5752d8f08abSGreg Roach return 5762d8f08abSGreg Roach '<div class="' . $class . '">' . 5772d8f08abSGreg Roach /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ 5787b7d8067SGreg Roach I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) . 5792d8f08abSGreg Roach '</div>'; 580a25f0a04SGreg Roach } 581a25f0a04SGreg Roach 582a25f0a04SGreg Roach /** 583*580a4d11SGreg Roach * Helper functions to sort facts 584a25f0a04SGreg Roach * 585*580a4d11SGreg Roach * @return Closure 586a25f0a04SGreg Roach */ 587*580a4d11SGreg Roach private static function dateComparator(): Closure 588c1010edaSGreg Roach { 589*580a4d11SGreg Roach return function (Fact $a, Fact $b): int { 5902decada7SGreg Roach if ($a->date()->isOK() && $b->date()->isOK()) { 591a25f0a04SGreg Roach // If both events have dates, compare by date 5922decada7SGreg Roach $ret = Date::compare($a->date(), $b->date()); 593a25f0a04SGreg Roach 594dfa848b8SGreg Roach if ($ret === 0) { 595*580a4d11SGreg Roach // If dates overlap, compare by fact type 596*580a4d11SGreg Roach $ret = self::typeComparator()($a, $b); 597a25f0a04SGreg Roach 598a25f0a04SGreg Roach // If the fact type is also the same, retain the initial order 599dfa848b8SGreg Roach if ($ret === 0) { 600*580a4d11SGreg Roach $ret = $a->sortOrder <=> $b->sortOrder; 601a25f0a04SGreg Roach } 602a25f0a04SGreg Roach } 603a25f0a04SGreg Roach 604a25f0a04SGreg Roach return $ret; 605b2ce94c6SRico Sonntag } 606b2ce94c6SRico Sonntag 607a25f0a04SGreg Roach // One or both events have no date - retain the initial order 608*580a4d11SGreg Roach return $a->sortOrder <=> $b->sortOrder; 609*580a4d11SGreg Roach 610*580a4d11SGreg Roach }; 611a25f0a04SGreg Roach } 612a25f0a04SGreg Roach 613a25f0a04SGreg Roach /** 614*580a4d11SGreg Roach * Helper functions to sort facts. 615a25f0a04SGreg Roach * 616*580a4d11SGreg Roach * @return Closure 617a25f0a04SGreg Roach */ 618*580a4d11SGreg Roach public static function typeComparator(): Closure 619c1010edaSGreg Roach { 620bbe4546fSGreg Roach static $factsort = []; 621a25f0a04SGreg Roach 622a25f0a04SGreg Roach if (empty($factsort)) { 623bbe4546fSGreg Roach $factsort = array_flip(self::FACT_ORDER); 624a25f0a04SGreg Roach } 625a25f0a04SGreg Roach 626*580a4d11SGreg Roach return function (Fact $a, Fact $b) use ($factsort): int { 627a25f0a04SGreg Roach // Facts from same families stay grouped together 628a25f0a04SGreg Roach // Keep MARR and DIV from the same families from mixing with events from other FAMs 629a25f0a04SGreg Roach // Use the original order in which the facts were added 630e7766c08SGreg Roach if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) { 631a25f0a04SGreg Roach return $a->sortOrder - $b->sortOrder; 632a25f0a04SGreg Roach } 633a25f0a04SGreg Roach 634a25f0a04SGreg Roach $atag = $a->getTag(); 635a25f0a04SGreg Roach $btag = $b->getTag(); 636a25f0a04SGreg Roach 637a25f0a04SGreg Roach // Events not in the above list get mapped onto one that is. 638a25f0a04SGreg Roach if (!array_key_exists($atag, $factsort)) { 639a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) { 640a25f0a04SGreg Roach $atag = $match[1]; 641a25f0a04SGreg Roach } else { 6427a6ee1acSGreg Roach $atag = '_????_'; 643a25f0a04SGreg Roach } 644a25f0a04SGreg Roach } 645a25f0a04SGreg Roach 646a25f0a04SGreg Roach if (!array_key_exists($btag, $factsort)) { 647a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) { 648a25f0a04SGreg Roach $btag = $match[1]; 649a25f0a04SGreg Roach } else { 6507a6ee1acSGreg Roach $btag = '_????_'; 651a25f0a04SGreg Roach } 652a25f0a04SGreg Roach } 653a25f0a04SGreg Roach 654a25f0a04SGreg Roach // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI 655a25f0a04SGreg Roach // - Treat dated after BURI facts as BURI instead 6563425616eSGreg Roach if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { 657a25f0a04SGreg Roach $atag = 'BURI'; 658a25f0a04SGreg Roach } 659a25f0a04SGreg Roach 6603425616eSGreg Roach if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { 661a25f0a04SGreg Roach $btag = 'BURI'; 662a25f0a04SGreg Roach } 663a25f0a04SGreg Roach 664a25f0a04SGreg Roach $ret = $factsort[$atag] - $factsort[$btag]; 665a25f0a04SGreg Roach 666a25f0a04SGreg Roach // If facts are the same then put dated facts before non-dated facts 667a25f0a04SGreg Roach if ($ret == 0) { 6683425616eSGreg Roach if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') { 669a25f0a04SGreg Roach return -1; 670a25f0a04SGreg Roach } 671a25f0a04SGreg Roach 6723425616eSGreg Roach if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') { 673a25f0a04SGreg Roach return 1; 674a25f0a04SGreg Roach } 675a25f0a04SGreg Roach 676a25f0a04SGreg Roach // If no sorting preference, then keep original ordering 677a25f0a04SGreg Roach $ret = $a->sortOrder - $b->sortOrder; 678a25f0a04SGreg Roach } 679a25f0a04SGreg Roach 680a25f0a04SGreg Roach return $ret; 681*580a4d11SGreg Roach }; 682*580a4d11SGreg Roach } 683*580a4d11SGreg Roach 684*580a4d11SGreg Roach /** 685*580a4d11SGreg Roach * A multi-key sort 686*580a4d11SGreg Roach * 1. First divide the facts into two arrays one set with dates and one set without dates 687*580a4d11SGreg Roach * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated 688*580a4d11SGreg Roach * using the compare type function 689*580a4d11SGreg Roach * 3. Then merge the arrays back into the original array using the compare type function 690*580a4d11SGreg Roach * 691*580a4d11SGreg Roach * @param Fact[] $unsorted 692*580a4d11SGreg Roach * 693*580a4d11SGreg Roach * @return Fact[] 694*580a4d11SGreg Roach */ 695*580a4d11SGreg Roach public static function sortFacts($unsorted): array 696*580a4d11SGreg Roach { 697*580a4d11SGreg Roach $dated = []; 698*580a4d11SGreg Roach $nondated = []; 699*580a4d11SGreg Roach $sorted = []; 700*580a4d11SGreg Roach 701*580a4d11SGreg Roach // Split the array into dated and non-dated arrays 702*580a4d11SGreg Roach $order = 0; 703*580a4d11SGreg Roach 704*580a4d11SGreg Roach foreach ($unsorted as $fact) { 705*580a4d11SGreg Roach $fact->sortOrder = $order; 706*580a4d11SGreg Roach $order++; 707*580a4d11SGreg Roach 708*580a4d11SGreg Roach if ($fact->date()->isOK()) { 709*580a4d11SGreg Roach $dated[] = $fact; 710*580a4d11SGreg Roach } else { 711*580a4d11SGreg Roach $nondated[] = $fact; 712*580a4d11SGreg Roach } 713*580a4d11SGreg Roach } 714*580a4d11SGreg Roach 715*580a4d11SGreg Roach usort($dated, self::dateComparator()); 716*580a4d11SGreg Roach usort($nondated, self::typeComparator()); 717*580a4d11SGreg Roach 718*580a4d11SGreg Roach // Merge the arrays 719*580a4d11SGreg Roach $dc = count($dated); 720*580a4d11SGreg Roach $nc = count($nondated); 721*580a4d11SGreg Roach $i = 0; 722*580a4d11SGreg Roach $j = 0; 723*580a4d11SGreg Roach 724*580a4d11SGreg Roach // while there is anything in the dated array continue merging 725*580a4d11SGreg Roach while ($i < $dc) { 726*580a4d11SGreg Roach // compare each fact by type to merge them in order 727*580a4d11SGreg Roach if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) { 728*580a4d11SGreg Roach $sorted[] = $nondated[$j]; 729*580a4d11SGreg Roach $j++; 730*580a4d11SGreg Roach } else { 731*580a4d11SGreg Roach $sorted[] = $dated[$i]; 732*580a4d11SGreg Roach $i++; 733*580a4d11SGreg Roach } 734*580a4d11SGreg Roach } 735*580a4d11SGreg Roach 736*580a4d11SGreg Roach // get anything that might be left in the nondated array 737*580a4d11SGreg Roach while ($j < $nc) { 738*580a4d11SGreg Roach $sorted[] = $nondated[$j]; 739*580a4d11SGreg Roach $j++; 740*580a4d11SGreg Roach } 741*580a4d11SGreg Roach 742*580a4d11SGreg Roach return $sorted; 743a25f0a04SGreg Roach } 744a25f0a04SGreg Roach 745a25f0a04SGreg Roach /** 746a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 747a25f0a04SGreg Roach * 748a25f0a04SGreg Roach * @return string 749a25f0a04SGreg Roach */ 750dfa848b8SGreg Roach public function __toString(): string 751c1010edaSGreg Roach { 752c0935879SGreg Roach return $this->id . '@' . $this->record->xref(); 753a25f0a04SGreg Roach } 754a25f0a04SGreg Roach} 755