1a25f0a04SGreg Roach<?php 2*3976b470SGreg 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 */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees; 2076692c8bSGreg Roach 21580a4d11SGreg Roachuse Closure; 223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 238af6bbf8SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService; 241c7a6874SGreg Roachuse Illuminate\Support\Collection; 258f5f5da8SGreg Roachuse InvalidArgumentException; 26*3976b470SGreg Roach 271450f098SGreg Roachuse function strpos; 28a25f0a04SGreg Roach 29a25f0a04SGreg Roach/** 3076692c8bSGreg Roach * A GEDCOM fact or event object. 31a25f0a04SGreg Roach */ 32c1010edaSGreg Roachclass Fact 33c1010edaSGreg Roach{ 3416d6367aSGreg Roach private const FACT_ORDER = [ 35bbe4546fSGreg Roach 'BIRT', 36bbe4546fSGreg Roach '_HNM', 37bbe4546fSGreg Roach 'ALIA', 38bbe4546fSGreg Roach '_AKA', 39bbe4546fSGreg Roach '_AKAN', 40bbe4546fSGreg Roach 'ADOP', 41bbe4546fSGreg Roach '_ADPF', 42bbe4546fSGreg Roach '_ADPF', 43bbe4546fSGreg Roach '_BRTM', 44bbe4546fSGreg Roach 'CHR', 45bbe4546fSGreg Roach 'BAPM', 46bbe4546fSGreg Roach 'FCOM', 47bbe4546fSGreg Roach 'CONF', 48bbe4546fSGreg Roach 'BARM', 49bbe4546fSGreg Roach 'BASM', 50bbe4546fSGreg Roach 'EDUC', 51bbe4546fSGreg Roach 'GRAD', 52bbe4546fSGreg Roach '_DEG', 53bbe4546fSGreg Roach 'EMIG', 54bbe4546fSGreg Roach 'IMMI', 55bbe4546fSGreg Roach 'NATU', 56bbe4546fSGreg Roach '_MILI', 57bbe4546fSGreg Roach '_MILT', 58bbe4546fSGreg Roach 'ENGA', 59bbe4546fSGreg Roach 'MARB', 60bbe4546fSGreg Roach 'MARC', 61bbe4546fSGreg Roach 'MARL', 62bbe4546fSGreg Roach '_MARI', 63bbe4546fSGreg Roach '_MBON', 64bbe4546fSGreg Roach 'MARR', 65bbe4546fSGreg Roach 'MARR_CIVIL', 66bbe4546fSGreg Roach 'MARR_RELIGIOUS', 67bbe4546fSGreg Roach 'MARR_PARTNERS', 68bbe4546fSGreg Roach 'MARR_UNKNOWN', 69bbe4546fSGreg Roach '_COML', 70bbe4546fSGreg Roach '_STAT', 71bbe4546fSGreg Roach '_SEPR', 72bbe4546fSGreg Roach 'DIVF', 73bbe4546fSGreg Roach 'MARS', 74bbe4546fSGreg Roach '_BIRT_CHIL', 75bbe4546fSGreg Roach 'DIV', 76bbe4546fSGreg Roach 'ANUL', 77bbe4546fSGreg Roach '_BIRT_', 78bbe4546fSGreg Roach '_MARR_', 79bbe4546fSGreg Roach '_DEAT_', 80bbe4546fSGreg Roach '_BURI_', 81bbe4546fSGreg Roach 'CENS', 82bbe4546fSGreg Roach 'OCCU', 83bbe4546fSGreg Roach 'RESI', 84bbe4546fSGreg Roach 'PROP', 85bbe4546fSGreg Roach 'CHRA', 86bbe4546fSGreg Roach 'RETI', 87bbe4546fSGreg Roach 'FACT', 88bbe4546fSGreg Roach 'EVEN', 89bbe4546fSGreg Roach '_NMR', 90bbe4546fSGreg Roach '_NMAR', 91bbe4546fSGreg Roach 'NMR', 92bbe4546fSGreg Roach 'NCHI', 93bbe4546fSGreg Roach 'WILL', 94bbe4546fSGreg Roach '_HOL', 95bbe4546fSGreg Roach '_????_', 96bbe4546fSGreg Roach 'DEAT', 97bbe4546fSGreg Roach '_FNRL', 98bbe4546fSGreg Roach 'CREM', 99bbe4546fSGreg Roach 'BURI', 100bbe4546fSGreg Roach '_INTE', 101bbe4546fSGreg Roach '_YART', 102bbe4546fSGreg Roach '_NLIV', 103bbe4546fSGreg Roach 'PROB', 104bbe4546fSGreg Roach 'TITL', 105bbe4546fSGreg Roach 'COMM', 106bbe4546fSGreg Roach 'NATI', 107bbe4546fSGreg Roach 'CITN', 108bbe4546fSGreg Roach 'CAST', 109bbe4546fSGreg Roach 'RELI', 110bbe4546fSGreg Roach 'SSN', 111bbe4546fSGreg Roach 'IDNO', 112bbe4546fSGreg Roach 'TEMP', 113bbe4546fSGreg Roach 'SLGC', 114bbe4546fSGreg Roach 'BAPL', 115bbe4546fSGreg Roach 'CONL', 116bbe4546fSGreg Roach 'ENDL', 117bbe4546fSGreg Roach 'SLGS', 118bbe4546fSGreg Roach 'ADDR', 119bbe4546fSGreg Roach 'PHON', 120bbe4546fSGreg Roach 'EMAIL', 121bbe4546fSGreg Roach '_EMAIL', 122bbe4546fSGreg Roach 'EMAL', 123bbe4546fSGreg Roach 'FAX', 124bbe4546fSGreg Roach 'WWW', 125bbe4546fSGreg Roach 'URL', 126bbe4546fSGreg Roach '_URL', 127bbe4546fSGreg Roach 'AFN', 128bbe4546fSGreg Roach 'REFN', 129bbe4546fSGreg Roach '_PRMN', 130bbe4546fSGreg Roach 'REF', 131bbe4546fSGreg Roach 'RIN', 132bbe4546fSGreg Roach '_UID', 133bbe4546fSGreg Roach 'OBJE', 134bbe4546fSGreg Roach 'NOTE', 135bbe4546fSGreg Roach 'SOUR', 136bbe4546fSGreg Roach 'CHAN', 137bbe4546fSGreg Roach '_TODO', 138bbe4546fSGreg Roach ]; 139bbe4546fSGreg Roach 140a25f0a04SGreg Roach /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */ 141905ab80aSGreg Roach private $id; 142a25f0a04SGreg Roach 143a25f0a04SGreg Roach /** @var GedcomRecord The GEDCOM record from which this fact is taken */ 144e7766c08SGreg Roach private $record; 145a25f0a04SGreg Roach 146a25f0a04SGreg Roach /** @var string The raw GEDCOM data for this fact */ 147a25f0a04SGreg Roach private $gedcom; 148a25f0a04SGreg Roach 149a25f0a04SGreg Roach /** @var string The GEDCOM tag for this record */ 150a25f0a04SGreg Roach private $tag; 151a25f0a04SGreg Roach 152cbc1590aSGreg Roach /** @var bool Is this a recently deleted fact, pending approval? */ 153a25f0a04SGreg Roach private $pending_deletion = false; 154a25f0a04SGreg Roach 155cbc1590aSGreg Roach /** @var bool Is this a recently added fact, pending approval? */ 156a25f0a04SGreg Roach private $pending_addition = false; 157a25f0a04SGreg Roach 158a25f0a04SGreg Roach /** @var Date The date of this fact, from the “2 DATE …” attribute */ 159a25f0a04SGreg Roach private $date; 160a25f0a04SGreg Roach 161a25f0a04SGreg Roach /** @var Place The place of this fact, from the “2 PLAC …” attribute */ 162a25f0a04SGreg Roach private $place; 163a25f0a04SGreg Roach 164580a4d11SGreg Roach /** @var int Used by Functions::sortFacts() */ 165580a4d11SGreg Roach private $sortOrder; 166a25f0a04SGreg Roach 167a25f0a04SGreg Roach /** 168a25f0a04SGreg Roach * Create an event object from a gedcom fragment. 169a25f0a04SGreg Roach * We need the parent object (to check privacy) and a (pseudo) fact ID to 170a25f0a04SGreg Roach * identify the fact within the record. 171a25f0a04SGreg Roach * 172a25f0a04SGreg Roach * @param string $gedcom 173a25f0a04SGreg Roach * @param GedcomRecord $parent 174905ab80aSGreg Roach * @param string $id 175a25f0a04SGreg Roach * 1768f5f5da8SGreg Roach * @throws InvalidArgumentException 177a25f0a04SGreg Roach */ 178905ab80aSGreg Roach public function __construct($gedcom, GedcomRecord $parent, $id) 179c1010edaSGreg Roach { 1808d0ebef0SGreg Roach if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) { 181a25f0a04SGreg Roach $this->gedcom = $gedcom; 182e7766c08SGreg Roach $this->record = $parent; 183905ab80aSGreg Roach $this->id = $id; 184a25f0a04SGreg Roach $this->tag = $match[1]; 185a25f0a04SGreg Roach } else { 1868f5f5da8SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')'); 187a25f0a04SGreg Roach } 188a25f0a04SGreg Roach } 189a25f0a04SGreg Roach 190a25f0a04SGreg Roach /** 191a25f0a04SGreg Roach * Get the value of level 1 data in the fact 192a25f0a04SGreg Roach * Allow for multi-line values 193a25f0a04SGreg Roach * 194baacc364SGreg Roach * @return string 195a25f0a04SGreg Roach */ 196dfa848b8SGreg Roach public function value(): string 197c1010edaSGreg Roach { 198a25f0a04SGreg Roach if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) { 199a25f0a04SGreg Roach return preg_replace("/\n2 CONT ?/", "\n", $match[1]); 200a25f0a04SGreg Roach } 201b2ce94c6SRico Sonntag 202b2ce94c6SRico Sonntag return ''; 203a25f0a04SGreg Roach } 204a25f0a04SGreg Roach 205a25f0a04SGreg Roach /** 206a25f0a04SGreg Roach * Get the record to which this fact links 207a25f0a04SGreg Roach * 208188adf12SGreg Roach * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord|null 209a25f0a04SGreg Roach */ 210dc124885SGreg Roach public function target() 211c1010edaSGreg Roach { 21284586c02SGreg Roach $xref = trim($this->value(), '@'); 213a25f0a04SGreg Roach switch ($this->tag) { 214a25f0a04SGreg Roach case 'FAMC': 215a25f0a04SGreg Roach case 'FAMS': 216f4afa648SGreg Roach return Family::getInstance($xref, $this->record()->tree()); 217a25f0a04SGreg Roach case 'HUSB': 218a25f0a04SGreg Roach case 'WIFE': 219a25f0a04SGreg Roach case 'CHIL': 220f4afa648SGreg Roach return Individual::getInstance($xref, $this->record()->tree()); 221a25f0a04SGreg Roach case 'SOUR': 222f4afa648SGreg Roach return Source::getInstance($xref, $this->record()->tree()); 223a25f0a04SGreg Roach case 'OBJE': 224f4afa648SGreg Roach return Media::getInstance($xref, $this->record()->tree()); 225a25f0a04SGreg Roach case 'REPO': 226f4afa648SGreg Roach return Repository::getInstance($xref, $this->record()->tree()); 227a25f0a04SGreg Roach case 'NOTE': 228f4afa648SGreg Roach return Note::getInstance($xref, $this->record()->tree()); 229a25f0a04SGreg Roach default: 230f4afa648SGreg Roach return GedcomRecord::getInstance($xref, $this->record()->tree()); 231a25f0a04SGreg Roach } 232a25f0a04SGreg Roach } 233a25f0a04SGreg Roach 234a25f0a04SGreg Roach /** 235a25f0a04SGreg Roach * Get the value of level 2 data in the fact 236a25f0a04SGreg Roach * 237a25f0a04SGreg Roach * @param string $tag 238a25f0a04SGreg Roach * 239baacc364SGreg Roach * @return string 240a25f0a04SGreg Roach */ 241dfa848b8SGreg Roach public function attribute($tag): string 242c1010edaSGreg Roach { 243a25f0a04SGreg Roach if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { 244a25f0a04SGreg Roach return preg_replace("/\n3 CONT ?/", "\n", $match[1]); 245a25f0a04SGreg Roach } 246b2ce94c6SRico Sonntag 247b2ce94c6SRico Sonntag return ''; 248a25f0a04SGreg Roach } 249a25f0a04SGreg Roach 250a25f0a04SGreg Roach /** 2518af6bbf8SGreg Roach * Get the PLAC:MAP:LATI for the fact. 2528af6bbf8SGreg Roach * 2538af6bbf8SGreg Roach * @return float 2548af6bbf8SGreg Roach */ 2558af6bbf8SGreg Roach public function latitude(): float 2568af6bbf8SGreg Roach { 2578af6bbf8SGreg Roach if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) { 2588af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2598af6bbf8SGreg Roach 2608af6bbf8SGreg Roach return $gedcom_service->readLatitude($match[1]); 2618af6bbf8SGreg Roach } 2628af6bbf8SGreg Roach 2638af6bbf8SGreg Roach return 0.0; 2648af6bbf8SGreg Roach } 2658af6bbf8SGreg Roach 2668af6bbf8SGreg Roach /** 2678af6bbf8SGreg Roach * Get the PLAC:MAP:LONG for the fact. 2688af6bbf8SGreg Roach * 2698af6bbf8SGreg Roach * @return float 2708af6bbf8SGreg Roach */ 2718af6bbf8SGreg Roach public function longitude(): float 2728af6bbf8SGreg Roach { 273819ac503SRico Sonntag if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) { 2748af6bbf8SGreg Roach $gedcom_service = new GedcomService(); 2758af6bbf8SGreg Roach 2768af6bbf8SGreg Roach return $gedcom_service->readLongitude($match[1]); 2778af6bbf8SGreg Roach } 2788af6bbf8SGreg Roach 2798af6bbf8SGreg Roach return 0.0; 2808af6bbf8SGreg Roach } 2818af6bbf8SGreg Roach 2828af6bbf8SGreg Roach /** 283a25f0a04SGreg Roach * Do the privacy rules allow us to display this fact to the current user 284a25f0a04SGreg Roach * 285cbc1590aSGreg Roach * @param int|null $access_level 286a25f0a04SGreg Roach * 287cbc1590aSGreg Roach * @return bool 288a25f0a04SGreg Roach */ 28935584196SGreg Roach public function canShow(int $access_level = null): bool 290c1010edaSGreg Roach { 2914b9ff166SGreg Roach if ($access_level === null) { 292f4afa648SGreg Roach $access_level = Auth::accessLevel($this->record()->tree()); 2934b9ff166SGreg Roach } 2944b9ff166SGreg Roach 295a25f0a04SGreg Roach // Does this record have an explicit RESN? 29620ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN confidential") !== false) { 2974b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 298a25f0a04SGreg Roach } 29920ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN privacy") !== false) { 3004b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 301a25f0a04SGreg Roach } 30220ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN none") !== false) { 303a25f0a04SGreg Roach return true; 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach 306a25f0a04SGreg Roach // Does this record have a default RESN? 307c0935879SGreg Roach $xref = $this->record->xref(); 308f4afa648SGreg Roach $fact_privacy = $this->record->tree()->getFactPrivacy(); 309f4afa648SGreg Roach $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy(); 310518bbdc1SGreg Roach if (isset($individual_fact_privacy[$xref][$this->tag])) { 311518bbdc1SGreg Roach return $individual_fact_privacy[$xref][$this->tag] >= $access_level; 312a25f0a04SGreg Roach } 313518bbdc1SGreg Roach if (isset($fact_privacy[$this->tag])) { 314518bbdc1SGreg Roach return $fact_privacy[$this->tag] >= $access_level; 315a25f0a04SGreg Roach } 316a25f0a04SGreg Roach 317a25f0a04SGreg Roach // No restrictions - it must be public 318a25f0a04SGreg Roach return true; 319a25f0a04SGreg Roach } 320a25f0a04SGreg Roach 321a25f0a04SGreg Roach /** 322a25f0a04SGreg Roach * Check whether this fact is protected against edit 323a25f0a04SGreg Roach * 324cbc1590aSGreg Roach * @return bool 325a25f0a04SGreg Roach */ 3268f53f488SRico Sonntag public function canEdit(): bool 327c1010edaSGreg Roach { 3281450f098SGreg Roach if ($this->isPendingDeletion()) { 3291450f098SGreg Roach return false; 3301450f098SGreg Roach } 3311450f098SGreg Roach 3321450f098SGreg Roach if (Auth::isManager($this->record->tree())) { 3331450f098SGreg Roach return true; 3341450f098SGreg Roach } 3351450f098SGreg Roach 336a25f0a04SGreg Roach // Members cannot edit RESN, CHAN and locked records 3371450f098SGreg Roach return Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() !== 'RESN' && $this->getTag() !== 'CHAN'; 338a25f0a04SGreg Roach } 339a25f0a04SGreg Roach 340a25f0a04SGreg Roach /** 341a25f0a04SGreg Roach * The place where the event occured. 342a25f0a04SGreg Roach * 343a25f0a04SGreg Roach * @return Place 344a25f0a04SGreg Roach */ 3454fb14fcbSGreg Roach public function place(): Place 346c1010edaSGreg Roach { 347a25f0a04SGreg Roach if ($this->place === null) { 348f4afa648SGreg Roach $this->place = new Place($this->attribute('PLAC'), $this->record()->tree()); 349a25f0a04SGreg Roach } 350a25f0a04SGreg Roach 351a25f0a04SGreg Roach return $this->place; 352a25f0a04SGreg Roach } 353a25f0a04SGreg Roach 354a25f0a04SGreg Roach /** 355a25f0a04SGreg Roach * Get the date for this fact. 356a25f0a04SGreg Roach * We can call this function many times, especially when sorting, 357a25f0a04SGreg Roach * so keep a copy of the date. 358a25f0a04SGreg Roach * 359a25f0a04SGreg Roach * @return Date 360a25f0a04SGreg Roach */ 3612decada7SGreg Roach public function date(): Date 362c1010edaSGreg Roach { 363a25f0a04SGreg Roach if ($this->date === null) { 3643425616eSGreg Roach $this->date = new Date($this->attribute('DATE')); 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach 367a25f0a04SGreg Roach return $this->date; 368a25f0a04SGreg Roach } 369a25f0a04SGreg Roach 370a25f0a04SGreg Roach /** 371a25f0a04SGreg Roach * The raw GEDCOM data for this fact 372a25f0a04SGreg Roach * 373a25f0a04SGreg Roach * @return string 374a25f0a04SGreg Roach */ 375138ca96cSGreg Roach public function gedcom(): string 376c1010edaSGreg Roach { 377a25f0a04SGreg Roach return $this->gedcom; 378a25f0a04SGreg Roach } 379a25f0a04SGreg Roach 380a25f0a04SGreg Roach /** 381a25f0a04SGreg Roach * Get a (pseudo) primary key for this fact. 382a25f0a04SGreg Roach * 383a25f0a04SGreg Roach * @return string 384a25f0a04SGreg Roach */ 385905ab80aSGreg Roach public function id(): string 386c1010edaSGreg Roach { 387905ab80aSGreg Roach return $this->id; 388a25f0a04SGreg Roach } 389a25f0a04SGreg Roach 390a25f0a04SGreg Roach /** 391a25f0a04SGreg Roach * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 392a25f0a04SGreg Roach * 393a25f0a04SGreg Roach * @return string 394a25f0a04SGreg Roach */ 3958f53f488SRico Sonntag public function getTag(): string 396c1010edaSGreg Roach { 397a25f0a04SGreg Roach return $this->tag; 398a25f0a04SGreg Roach } 399a25f0a04SGreg Roach 400a25f0a04SGreg Roach /** 401a25f0a04SGreg Roach * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL) 402a25f0a04SGreg Roach * 403a25f0a04SGreg Roach * @param string $tag 4047e96c925SGreg Roach * 4057e96c925SGreg Roach * @return void 406a25f0a04SGreg Roach */ 407e364afe4SGreg Roach public function setTag($tag): void 408c1010edaSGreg Roach { 409a25f0a04SGreg Roach $this->tag = $tag; 410a25f0a04SGreg Roach } 411a25f0a04SGreg Roach 412a25f0a04SGreg Roach /** 413a25f0a04SGreg Roach * The Person/Family record where this Fact came from 414a25f0a04SGreg Roach * 4156e59b7c4SGreg Roach * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord 416a25f0a04SGreg Roach */ 417e7766c08SGreg Roach public function record() 418c1010edaSGreg Roach { 419e7766c08SGreg Roach return $this->record; 420a25f0a04SGreg Roach } 421a25f0a04SGreg Roach 422a25f0a04SGreg Roach /** 423a25f0a04SGreg Roach * Get the name of this fact type, for use as a label. 424a25f0a04SGreg Roach * 425a25f0a04SGreg Roach * @return string 426a25f0a04SGreg Roach */ 4277b7d8067SGreg Roach public function label(): string 428c1010edaSGreg Roach { 429a25f0a04SGreg Roach // Custom FACT/EVEN - with a TYPE 4303425616eSGreg Roach if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') { 4313425616eSGreg Roach return I18N::translate(e($this->attribute('TYPE'))); 432a25f0a04SGreg Roach } 4334e5adf68SGreg Roach 434e7766c08SGreg Roach return GedcomTag::getLabel($this->tag, $this->record); 435a25f0a04SGreg Roach } 436a25f0a04SGreg Roach 437a25f0a04SGreg Roach /** 438a25f0a04SGreg Roach * This is a newly deleted fact, pending approval. 4397e96c925SGreg Roach * 4407e96c925SGreg Roach * @return void 441a25f0a04SGreg Roach */ 442e364afe4SGreg Roach public function setPendingDeletion(): void 443c1010edaSGreg Roach { 444a25f0a04SGreg Roach $this->pending_deletion = true; 445a25f0a04SGreg Roach $this->pending_addition = false; 446a25f0a04SGreg Roach } 447a25f0a04SGreg Roach 448a25f0a04SGreg Roach /** 449a25f0a04SGreg Roach * Is this a newly deleted fact, pending approval. 450a25f0a04SGreg Roach * 451cbc1590aSGreg Roach * @return bool 452a25f0a04SGreg Roach */ 4538f53f488SRico Sonntag public function isPendingDeletion(): bool 454c1010edaSGreg Roach { 455a25f0a04SGreg Roach return $this->pending_deletion; 456a25f0a04SGreg Roach } 457a25f0a04SGreg Roach 458a25f0a04SGreg Roach /** 459a25f0a04SGreg Roach * This is a newly added fact, pending approval. 4607e96c925SGreg Roach * 4617e96c925SGreg Roach * @return void 462a25f0a04SGreg Roach */ 463e364afe4SGreg Roach public function setPendingAddition(): void 464c1010edaSGreg Roach { 465a25f0a04SGreg Roach $this->pending_addition = true; 466a25f0a04SGreg Roach $this->pending_deletion = false; 467a25f0a04SGreg Roach } 468a25f0a04SGreg Roach 469a25f0a04SGreg Roach /** 470a25f0a04SGreg Roach * Is this a newly added fact, pending approval. 471a25f0a04SGreg Roach * 472cbc1590aSGreg Roach * @return bool 473a25f0a04SGreg Roach */ 4748f53f488SRico Sonntag public function isPendingAddition(): bool 475c1010edaSGreg Roach { 476a25f0a04SGreg Roach return $this->pending_addition; 477a25f0a04SGreg Roach } 478a25f0a04SGreg Roach 479a25f0a04SGreg Roach /** 480a25f0a04SGreg Roach * Source citations linked to this fact 481a25f0a04SGreg Roach * 482a25f0a04SGreg Roach * @return string[] 483a25f0a04SGreg Roach */ 4848f53f488SRico Sonntag public function getCitations(): array 485c1010edaSGreg Roach { 4868d0ebef0SGreg Roach preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER); 48713abd6f3SGreg Roach $citations = []; 488a25f0a04SGreg Roach foreach ($matches as $match) { 489f4afa648SGreg Roach $source = Source::getInstance($match[2], $this->record()->tree()); 4902859ecedSJonathan Jaubart if ($source && $source->canShow()) { 491a25f0a04SGreg Roach $citations[] = $match[1]; 492a25f0a04SGreg Roach } 493a25f0a04SGreg Roach } 494a25f0a04SGreg Roach 495a25f0a04SGreg Roach return $citations; 496a25f0a04SGreg Roach } 497a25f0a04SGreg Roach 498a25f0a04SGreg Roach /** 499a25f0a04SGreg Roach * Notes (inline and objects) linked to this fact 500a25f0a04SGreg Roach * 501a25f0a04SGreg Roach * @return string[]|Note[] 502a25f0a04SGreg Roach */ 5038f53f488SRico Sonntag public function getNotes(): array 504c1010edaSGreg Roach { 50513abd6f3SGreg Roach $notes = []; 506138ca96cSGreg Roach preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches); 507a25f0a04SGreg Roach foreach ($matches[1] as $match) { 508a25f0a04SGreg Roach $note = preg_replace("/\n3 CONT ?/", "\n", $match); 5098d0ebef0SGreg Roach if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) { 510f4afa648SGreg Roach $note = Note::getInstance($nmatch[1], $this->record()->tree()); 511a25f0a04SGreg Roach if ($note && $note->canShow()) { 512a25f0a04SGreg Roach // A note object 513a25f0a04SGreg Roach $notes[] = $note; 514a25f0a04SGreg Roach } 515a25f0a04SGreg Roach } else { 516a25f0a04SGreg Roach // An inline note 517a25f0a04SGreg Roach $notes[] = $note; 518a25f0a04SGreg Roach } 519a25f0a04SGreg Roach } 520a25f0a04SGreg Roach 521a25f0a04SGreg Roach return $notes; 522a25f0a04SGreg Roach } 523a25f0a04SGreg Roach 524a25f0a04SGreg Roach /** 525a25f0a04SGreg Roach * Media objects linked to this fact 526a25f0a04SGreg Roach * 527a25f0a04SGreg Roach * @return Media[] 528a25f0a04SGreg Roach */ 5298f53f488SRico Sonntag public function getMedia(): array 530c1010edaSGreg Roach { 53113abd6f3SGreg Roach $media = []; 5328d0ebef0SGreg Roach preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches); 533a25f0a04SGreg Roach foreach ($matches[1] as $match) { 534f4afa648SGreg Roach $obje = Media::getInstance($match, $this->record()->tree()); 5352859ecedSJonathan Jaubart if ($obje && $obje->canShow()) { 536a25f0a04SGreg Roach $media[] = $obje; 537a25f0a04SGreg Roach } 538a25f0a04SGreg Roach } 539a25f0a04SGreg Roach 540a25f0a04SGreg Roach return $media; 541a25f0a04SGreg Roach } 542a25f0a04SGreg Roach 543a25f0a04SGreg Roach /** 544a25f0a04SGreg Roach * A one-line summary of the fact - for charts, etc. 545a25f0a04SGreg Roach * 546a25f0a04SGreg Roach * @return string 547a25f0a04SGreg Roach */ 5488f53f488SRico Sonntag public function summary(): string 549c1010edaSGreg Roach { 55013abd6f3SGreg Roach $attributes = []; 551dc124885SGreg Roach $target = $this->target(); 552db7bb364SGreg Roach if ($target instanceof GedcomRecord) { 55339ca88baSGreg Roach $attributes[] = $target->fullName(); 554a25f0a04SGreg Roach } else { 555726d7b07SGreg Roach // Fact value 55684586c02SGreg Roach $value = $this->value(); 557726d7b07SGreg Roach if ($value !== '' && $value !== 'Y') { 558d53324c9SGreg Roach $attributes[] = '<span dir="auto">' . e($value) . '</span>'; 559a25f0a04SGreg Roach } 560726d7b07SGreg Roach // Fact date 5612decada7SGreg Roach $date = $this->date(); 562726d7b07SGreg Roach if ($date->isOK()) { 563e364afe4SGreg Roach if ($this->record() instanceof Individual && in_array($this->getTag(), Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) { 564e7766c08SGreg Roach $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date); 565a25f0a04SGreg Roach } else { 566a25f0a04SGreg Roach $attributes[] = $date->display(); 567a25f0a04SGreg Roach } 568726d7b07SGreg Roach } 569726d7b07SGreg Roach // Fact place 570e364afe4SGreg Roach if ($this->place()->gedcomName() !== '') { 571392561bbSGreg Roach $attributes[] = $this->place()->shortName(); 572a25f0a04SGreg Roach } 573a25f0a04SGreg Roach } 5742d8f08abSGreg Roach 5752d8f08abSGreg Roach $class = 'fact_' . $this->getTag(); 576a25f0a04SGreg Roach if ($this->isPendingAddition()) { 5772d8f08abSGreg Roach $class .= ' new'; 578a25f0a04SGreg Roach } elseif ($this->isPendingDeletion()) { 5792d8f08abSGreg Roach $class .= ' old'; 580a25f0a04SGreg Roach } 5812d8f08abSGreg Roach 5822d8f08abSGreg Roach return 5832d8f08abSGreg Roach '<div class="' . $class . '">' . 5842d8f08abSGreg Roach /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ 5857b7d8067SGreg Roach I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) . 5862d8f08abSGreg Roach '</div>'; 587a25f0a04SGreg Roach } 588a25f0a04SGreg Roach 589a25f0a04SGreg Roach /** 590580a4d11SGreg Roach * Helper functions to sort facts 591a25f0a04SGreg Roach * 592580a4d11SGreg Roach * @return Closure 593a25f0a04SGreg Roach */ 594580a4d11SGreg Roach private static function dateComparator(): Closure 595c1010edaSGreg Roach { 5966c2179e2SGreg Roach return static function (Fact $a, Fact $b): int { 5972decada7SGreg Roach if ($a->date()->isOK() && $b->date()->isOK()) { 598a25f0a04SGreg Roach // If both events have dates, compare by date 5992decada7SGreg Roach $ret = Date::compare($a->date(), $b->date()); 600a25f0a04SGreg Roach 601dfa848b8SGreg Roach if ($ret === 0) { 602580a4d11SGreg Roach // If dates overlap, compare by fact type 603580a4d11SGreg Roach $ret = self::typeComparator()($a, $b); 604a25f0a04SGreg Roach 605a25f0a04SGreg Roach // If the fact type is also the same, retain the initial order 606dfa848b8SGreg Roach if ($ret === 0) { 607580a4d11SGreg Roach $ret = $a->sortOrder <=> $b->sortOrder; 608a25f0a04SGreg Roach } 609a25f0a04SGreg Roach } 610a25f0a04SGreg Roach 611a25f0a04SGreg Roach return $ret; 612b2ce94c6SRico Sonntag } 613b2ce94c6SRico Sonntag 614a25f0a04SGreg Roach // One or both events have no date - retain the initial order 615580a4d11SGreg Roach return $a->sortOrder <=> $b->sortOrder; 616580a4d11SGreg Roach }; 617a25f0a04SGreg Roach } 618a25f0a04SGreg Roach 619a25f0a04SGreg Roach /** 620580a4d11SGreg Roach * Helper functions to sort facts. 621a25f0a04SGreg Roach * 622580a4d11SGreg Roach * @return Closure 623a25f0a04SGreg Roach */ 624580a4d11SGreg Roach public static function typeComparator(): Closure 625c1010edaSGreg Roach { 626bbe4546fSGreg Roach static $factsort = []; 627a25f0a04SGreg Roach 628a25f0a04SGreg Roach if (empty($factsort)) { 629bbe4546fSGreg Roach $factsort = array_flip(self::FACT_ORDER); 630a25f0a04SGreg Roach } 631a25f0a04SGreg Roach 6326c2179e2SGreg Roach return static function (Fact $a, Fact $b) use ($factsort): int { 633a25f0a04SGreg Roach // Facts from same families stay grouped together 634a25f0a04SGreg Roach // Keep MARR and DIV from the same families from mixing with events from other FAMs 635a25f0a04SGreg Roach // Use the original order in which the facts were added 636e7766c08SGreg Roach if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) { 637a25f0a04SGreg Roach return $a->sortOrder - $b->sortOrder; 638a25f0a04SGreg Roach } 639a25f0a04SGreg Roach 640a25f0a04SGreg Roach $atag = $a->getTag(); 641a25f0a04SGreg Roach $btag = $b->getTag(); 642a25f0a04SGreg Roach 643a25f0a04SGreg Roach // Events not in the above list get mapped onto one that is. 644a25f0a04SGreg Roach if (!array_key_exists($atag, $factsort)) { 645a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) { 646a25f0a04SGreg Roach $atag = $match[1]; 647a25f0a04SGreg Roach } else { 6487a6ee1acSGreg Roach $atag = '_????_'; 649a25f0a04SGreg Roach } 650a25f0a04SGreg Roach } 651a25f0a04SGreg Roach 652a25f0a04SGreg Roach if (!array_key_exists($btag, $factsort)) { 653a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) { 654a25f0a04SGreg Roach $btag = $match[1]; 655a25f0a04SGreg Roach } else { 6567a6ee1acSGreg Roach $btag = '_????_'; 657a25f0a04SGreg Roach } 658a25f0a04SGreg Roach } 659a25f0a04SGreg Roach 660a25f0a04SGreg Roach // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI 661a25f0a04SGreg Roach // - Treat dated after BURI facts as BURI instead 6623425616eSGreg Roach if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { 663a25f0a04SGreg Roach $atag = 'BURI'; 664a25f0a04SGreg Roach } 665a25f0a04SGreg Roach 6663425616eSGreg Roach if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { 667a25f0a04SGreg Roach $btag = 'BURI'; 668a25f0a04SGreg Roach } 669a25f0a04SGreg Roach 670a25f0a04SGreg Roach $ret = $factsort[$atag] - $factsort[$btag]; 671a25f0a04SGreg Roach 672a25f0a04SGreg Roach // If facts are the same then put dated facts before non-dated facts 673a25f0a04SGreg Roach if ($ret == 0) { 6743425616eSGreg Roach if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') { 675a25f0a04SGreg Roach return -1; 676a25f0a04SGreg Roach } 677a25f0a04SGreg Roach 6783425616eSGreg Roach if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') { 679a25f0a04SGreg Roach return 1; 680a25f0a04SGreg Roach } 681a25f0a04SGreg Roach 682a25f0a04SGreg Roach // If no sorting preference, then keep original ordering 683a25f0a04SGreg Roach $ret = $a->sortOrder - $b->sortOrder; 684a25f0a04SGreg Roach } 685a25f0a04SGreg Roach 686a25f0a04SGreg Roach return $ret; 687580a4d11SGreg Roach }; 688580a4d11SGreg Roach } 689580a4d11SGreg Roach 690580a4d11SGreg Roach /** 691580a4d11SGreg Roach * A multi-key sort 692580a4d11SGreg Roach * 1. First divide the facts into two arrays one set with dates and one set without dates 693580a4d11SGreg Roach * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated 694580a4d11SGreg Roach * using the compare type function 695580a4d11SGreg Roach * 3. Then merge the arrays back into the original array using the compare type function 696580a4d11SGreg Roach * 6978af3e5c1SGreg Roach * @param Collection $unsorted 698580a4d11SGreg Roach * 69954c7f8dfSGreg Roach * @return Collection 700580a4d11SGreg Roach */ 7018af3e5c1SGreg Roach public static function sortFacts(Collection $unsorted): Collection 702580a4d11SGreg Roach { 703580a4d11SGreg Roach $dated = []; 704580a4d11SGreg Roach $nondated = []; 705580a4d11SGreg Roach $sorted = []; 706580a4d11SGreg Roach 707580a4d11SGreg Roach // Split the array into dated and non-dated arrays 708580a4d11SGreg Roach $order = 0; 709580a4d11SGreg Roach 710580a4d11SGreg Roach foreach ($unsorted as $fact) { 711580a4d11SGreg Roach $fact->sortOrder = $order; 712580a4d11SGreg Roach $order++; 713580a4d11SGreg Roach 714580a4d11SGreg Roach if ($fact->date()->isOK()) { 715580a4d11SGreg Roach $dated[] = $fact; 716580a4d11SGreg Roach } else { 717580a4d11SGreg Roach $nondated[] = $fact; 718580a4d11SGreg Roach } 719580a4d11SGreg Roach } 720580a4d11SGreg Roach 721580a4d11SGreg Roach usort($dated, self::dateComparator()); 722580a4d11SGreg Roach usort($nondated, self::typeComparator()); 723580a4d11SGreg Roach 724580a4d11SGreg Roach // Merge the arrays 725580a4d11SGreg Roach $dc = count($dated); 726580a4d11SGreg Roach $nc = count($nondated); 727580a4d11SGreg Roach $i = 0; 728580a4d11SGreg Roach $j = 0; 729580a4d11SGreg Roach 730580a4d11SGreg Roach // while there is anything in the dated array continue merging 731580a4d11SGreg Roach while ($i < $dc) { 732580a4d11SGreg Roach // compare each fact by type to merge them in order 733580a4d11SGreg Roach if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) { 734580a4d11SGreg Roach $sorted[] = $nondated[$j]; 735580a4d11SGreg Roach $j++; 736580a4d11SGreg Roach } else { 737580a4d11SGreg Roach $sorted[] = $dated[$i]; 738580a4d11SGreg Roach $i++; 739580a4d11SGreg Roach } 740580a4d11SGreg Roach } 741580a4d11SGreg Roach 742580a4d11SGreg Roach // get anything that might be left in the nondated array 743580a4d11SGreg Roach while ($j < $nc) { 744580a4d11SGreg Roach $sorted[] = $nondated[$j]; 745580a4d11SGreg Roach $j++; 746580a4d11SGreg Roach } 747580a4d11SGreg Roach 7481c7a6874SGreg Roach return new Collection($sorted); 749a25f0a04SGreg Roach } 750a25f0a04SGreg Roach 751a25f0a04SGreg Roach /** 752a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 753a25f0a04SGreg Roach * 754a25f0a04SGreg Roach * @return string 755a25f0a04SGreg Roach */ 756dfa848b8SGreg Roach public function __toString(): string 757c1010edaSGreg Roach { 758c0935879SGreg Roach return $this->id . '@' . $this->record->xref(); 759a25f0a04SGreg Roach } 760a25f0a04SGreg Roach} 761