1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 1776692c8bSGreg Roach 183d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 198f5f5da8SGreg Roachuse InvalidArgumentException; 20a25f0a04SGreg Roach 21a25f0a04SGreg Roach/** 2276692c8bSGreg Roach * A GEDCOM fact or event object. 23a25f0a04SGreg Roach */ 24c1010edaSGreg Roachclass Fact 25c1010edaSGreg Roach{ 26bbe4546fSGreg Roach const FACT_ORDER = [ 27bbe4546fSGreg Roach 'BIRT', 28bbe4546fSGreg Roach '_HNM', 29bbe4546fSGreg Roach 'ALIA', 30bbe4546fSGreg Roach '_AKA', 31bbe4546fSGreg Roach '_AKAN', 32bbe4546fSGreg Roach 'ADOP', 33bbe4546fSGreg Roach '_ADPF', 34bbe4546fSGreg Roach '_ADPF', 35bbe4546fSGreg Roach '_BRTM', 36bbe4546fSGreg Roach 'CHR', 37bbe4546fSGreg Roach 'BAPM', 38bbe4546fSGreg Roach 'FCOM', 39bbe4546fSGreg Roach 'CONF', 40bbe4546fSGreg Roach 'BARM', 41bbe4546fSGreg Roach 'BASM', 42bbe4546fSGreg Roach 'EDUC', 43bbe4546fSGreg Roach 'GRAD', 44bbe4546fSGreg Roach '_DEG', 45bbe4546fSGreg Roach 'EMIG', 46bbe4546fSGreg Roach 'IMMI', 47bbe4546fSGreg Roach 'NATU', 48bbe4546fSGreg Roach '_MILI', 49bbe4546fSGreg Roach '_MILT', 50bbe4546fSGreg Roach 'ENGA', 51bbe4546fSGreg Roach 'MARB', 52bbe4546fSGreg Roach 'MARC', 53bbe4546fSGreg Roach 'MARL', 54bbe4546fSGreg Roach '_MARI', 55bbe4546fSGreg Roach '_MBON', 56bbe4546fSGreg Roach 'MARR', 57bbe4546fSGreg Roach 'MARR_CIVIL', 58bbe4546fSGreg Roach 'MARR_RELIGIOUS', 59bbe4546fSGreg Roach 'MARR_PARTNERS', 60bbe4546fSGreg Roach 'MARR_UNKNOWN', 61bbe4546fSGreg Roach '_COML', 62bbe4546fSGreg Roach '_STAT', 63bbe4546fSGreg Roach '_SEPR', 64bbe4546fSGreg Roach 'DIVF', 65bbe4546fSGreg Roach 'MARS', 66bbe4546fSGreg Roach '_BIRT_CHIL', 67bbe4546fSGreg Roach 'DIV', 68bbe4546fSGreg Roach 'ANUL', 69bbe4546fSGreg Roach '_BIRT_', 70bbe4546fSGreg Roach '_MARR_', 71bbe4546fSGreg Roach '_DEAT_', 72bbe4546fSGreg Roach '_BURI_', 73bbe4546fSGreg Roach 'CENS', 74bbe4546fSGreg Roach 'OCCU', 75bbe4546fSGreg Roach 'RESI', 76bbe4546fSGreg Roach 'PROP', 77bbe4546fSGreg Roach 'CHRA', 78bbe4546fSGreg Roach 'RETI', 79bbe4546fSGreg Roach 'FACT', 80bbe4546fSGreg Roach 'EVEN', 81bbe4546fSGreg Roach '_NMR', 82bbe4546fSGreg Roach '_NMAR', 83bbe4546fSGreg Roach 'NMR', 84bbe4546fSGreg Roach 'NCHI', 85bbe4546fSGreg Roach 'WILL', 86bbe4546fSGreg Roach '_HOL', 87bbe4546fSGreg Roach '_????_', 88bbe4546fSGreg Roach 'DEAT', 89bbe4546fSGreg Roach '_FNRL', 90bbe4546fSGreg Roach 'CREM', 91bbe4546fSGreg Roach 'BURI', 92bbe4546fSGreg Roach '_INTE', 93bbe4546fSGreg Roach '_YART', 94bbe4546fSGreg Roach '_NLIV', 95bbe4546fSGreg Roach 'PROB', 96bbe4546fSGreg Roach 'TITL', 97bbe4546fSGreg Roach 'COMM', 98bbe4546fSGreg Roach 'NATI', 99bbe4546fSGreg Roach 'CITN', 100bbe4546fSGreg Roach 'CAST', 101bbe4546fSGreg Roach 'RELI', 102bbe4546fSGreg Roach 'SSN', 103bbe4546fSGreg Roach 'IDNO', 104bbe4546fSGreg Roach 'TEMP', 105bbe4546fSGreg Roach 'SLGC', 106bbe4546fSGreg Roach 'BAPL', 107bbe4546fSGreg Roach 'CONL', 108bbe4546fSGreg Roach 'ENDL', 109bbe4546fSGreg Roach 'SLGS', 110bbe4546fSGreg Roach 'ADDR', 111bbe4546fSGreg Roach 'PHON', 112bbe4546fSGreg Roach 'EMAIL', 113bbe4546fSGreg Roach '_EMAIL', 114bbe4546fSGreg Roach 'EMAL', 115bbe4546fSGreg Roach 'FAX', 116bbe4546fSGreg Roach 'WWW', 117bbe4546fSGreg Roach 'URL', 118bbe4546fSGreg Roach '_URL', 119bbe4546fSGreg Roach 'AFN', 120bbe4546fSGreg Roach 'REFN', 121bbe4546fSGreg Roach '_PRMN', 122bbe4546fSGreg Roach 'REF', 123bbe4546fSGreg Roach 'RIN', 124bbe4546fSGreg Roach '_UID', 125bbe4546fSGreg Roach 'OBJE', 126bbe4546fSGreg Roach 'NOTE', 127bbe4546fSGreg Roach 'SOUR', 128bbe4546fSGreg Roach 'CHAN', 129bbe4546fSGreg Roach '_TODO', 130bbe4546fSGreg Roach ]; 131bbe4546fSGreg Roach 132a25f0a04SGreg Roach /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */ 133a25f0a04SGreg Roach private $fact_id; 134a25f0a04SGreg Roach 135a25f0a04SGreg Roach /** @var GedcomRecord The GEDCOM record from which this fact is taken */ 136a25f0a04SGreg Roach private $parent; 137a25f0a04SGreg Roach 138a25f0a04SGreg Roach /** @var string The raw GEDCOM data for this fact */ 139a25f0a04SGreg Roach private $gedcom; 140a25f0a04SGreg Roach 141a25f0a04SGreg Roach /** @var string The GEDCOM tag for this record */ 142a25f0a04SGreg Roach private $tag; 143a25f0a04SGreg Roach 144cbc1590aSGreg Roach /** @var bool Is this a recently deleted fact, pending approval? */ 145a25f0a04SGreg Roach private $pending_deletion = false; 146a25f0a04SGreg Roach 147cbc1590aSGreg Roach /** @var bool Is this a recently added fact, pending approval? */ 148a25f0a04SGreg Roach private $pending_addition = false; 149a25f0a04SGreg Roach 150a25f0a04SGreg Roach /** @var Date The date of this fact, from the “2 DATE …” attribute */ 151a25f0a04SGreg Roach private $date; 152a25f0a04SGreg Roach 153a25f0a04SGreg Roach /** @var Place The place of this fact, from the “2 PLAC …” attribute */ 154a25f0a04SGreg Roach private $place; 155a25f0a04SGreg Roach 1563d7a8a4cSGreg Roach /** @var int Temporary(!) variable Used by Functions::sortFacts() */ 157a25f0a04SGreg Roach public $sortOrder; 158a25f0a04SGreg Roach 159a25f0a04SGreg Roach /** 160a25f0a04SGreg Roach * Create an event object from a gedcom fragment. 161a25f0a04SGreg Roach * We need the parent object (to check privacy) and a (pseudo) fact ID to 162a25f0a04SGreg Roach * identify the fact within the record. 163a25f0a04SGreg Roach * 164a25f0a04SGreg Roach * @param string $gedcom 165a25f0a04SGreg Roach * @param GedcomRecord $parent 166a25f0a04SGreg Roach * @param string $fact_id 167a25f0a04SGreg Roach * 1688f5f5da8SGreg Roach * @throws InvalidArgumentException 169a25f0a04SGreg Roach */ 170c1010edaSGreg Roach public function __construct($gedcom, GedcomRecord $parent, $fact_id) 171c1010edaSGreg Roach { 172a25f0a04SGreg Roach if (preg_match('/^1 (' . WT_REGEX_TAG . ')/', $gedcom, $match)) { 173a25f0a04SGreg Roach $this->gedcom = $gedcom; 174a25f0a04SGreg Roach $this->parent = $parent; 175a25f0a04SGreg Roach $this->fact_id = $fact_id; 176a25f0a04SGreg Roach $this->tag = $match[1]; 177a25f0a04SGreg Roach } else { 1788f5f5da8SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')'); 179a25f0a04SGreg Roach } 180a25f0a04SGreg Roach } 181a25f0a04SGreg Roach 182a25f0a04SGreg Roach /** 183a25f0a04SGreg Roach * Get the value of level 1 data in the fact 184a25f0a04SGreg Roach * Allow for multi-line values 185a25f0a04SGreg Roach * 186baacc364SGreg Roach * @return string 187a25f0a04SGreg Roach */ 188c1010edaSGreg Roach public function getValue() 189c1010edaSGreg Roach { 190a25f0a04SGreg Roach if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) { 191a25f0a04SGreg Roach return preg_replace("/\n2 CONT ?/", "\n", $match[1]); 192a25f0a04SGreg Roach } else { 193baacc364SGreg Roach return ''; 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach 197a25f0a04SGreg Roach /** 198a25f0a04SGreg Roach * Get the record to which this fact links 199a25f0a04SGreg Roach * 200805a90eaSGreg Roach * @return Individual|Family|Source|Repository|Media|Note|null 201a25f0a04SGreg Roach */ 202c1010edaSGreg Roach public function getTarget() 203c1010edaSGreg Roach { 204a25f0a04SGreg Roach $xref = trim($this->getValue(), '@'); 205a25f0a04SGreg Roach switch ($this->tag) { 206a25f0a04SGreg Roach case 'FAMC': 207a25f0a04SGreg Roach case 'FAMS': 20824ec66ceSGreg Roach return Family::getInstance($xref, $this->getParent()->getTree()); 209a25f0a04SGreg Roach case 'HUSB': 210a25f0a04SGreg Roach case 'WIFE': 211a25f0a04SGreg Roach case 'CHIL': 21224ec66ceSGreg Roach return Individual::getInstance($xref, $this->getParent()->getTree()); 213a25f0a04SGreg Roach case 'SOUR': 21424ec66ceSGreg Roach return Source::getInstance($xref, $this->getParent()->getTree()); 215a25f0a04SGreg Roach case 'OBJE': 21624ec66ceSGreg Roach return Media::getInstance($xref, $this->getParent()->getTree()); 217a25f0a04SGreg Roach case 'REPO': 21824ec66ceSGreg Roach return Repository::getInstance($xref, $this->getParent()->getTree()); 219a25f0a04SGreg Roach case 'NOTE': 22024ec66ceSGreg Roach return Note::getInstance($xref, $this->getParent()->getTree()); 221a25f0a04SGreg Roach default: 22224ec66ceSGreg Roach return GedcomRecord::getInstance($xref, $this->getParent()->getTree()); 223a25f0a04SGreg Roach } 224a25f0a04SGreg Roach } 225a25f0a04SGreg Roach 226a25f0a04SGreg Roach /** 227a25f0a04SGreg Roach * Get the value of level 2 data in the fact 228a25f0a04SGreg Roach * 229a25f0a04SGreg Roach * @param string $tag 230a25f0a04SGreg Roach * 231baacc364SGreg Roach * @return string 232a25f0a04SGreg Roach */ 233c1010edaSGreg Roach public function getAttribute($tag) 234c1010edaSGreg Roach { 235a25f0a04SGreg Roach if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { 236a25f0a04SGreg Roach return preg_replace("/\n3 CONT ?/", "\n", $match[1]); 237a25f0a04SGreg Roach } else { 238baacc364SGreg Roach return ''; 239a25f0a04SGreg Roach } 240a25f0a04SGreg Roach } 241a25f0a04SGreg Roach 242a25f0a04SGreg Roach /** 243a25f0a04SGreg Roach * Do the privacy rules allow us to display this fact to the current user 244a25f0a04SGreg Roach * 245cbc1590aSGreg Roach * @param int|null $access_level 246a25f0a04SGreg Roach * 247cbc1590aSGreg Roach * @return bool 248a25f0a04SGreg Roach */ 2498f53f488SRico Sonntag public function canShow($access_level = null): bool 250c1010edaSGreg Roach { 2514b9ff166SGreg Roach if ($access_level === null) { 2524b9ff166SGreg Roach $access_level = Auth::accessLevel($this->getParent()->getTree()); 2534b9ff166SGreg Roach } 2544b9ff166SGreg Roach 255a25f0a04SGreg Roach // Does this record have an explicit RESN? 25620ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN confidential") !== false) { 2574b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 258a25f0a04SGreg Roach } 25920ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN privacy") !== false) { 2604b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 261a25f0a04SGreg Roach } 26220ff464cSGreg Roach if (strpos($this->gedcom, "\n2 RESN none") !== false) { 263a25f0a04SGreg Roach return true; 264a25f0a04SGreg Roach } 265a25f0a04SGreg Roach 266a25f0a04SGreg Roach // Does this record have a default RESN? 267a25f0a04SGreg Roach $xref = $this->parent->getXref(); 268518bbdc1SGreg Roach $fact_privacy = $this->parent->getTree()->getFactPrivacy(); 269518bbdc1SGreg Roach $individual_fact_privacy = $this->parent->getTree()->getIndividualFactPrivacy(); 270518bbdc1SGreg Roach if (isset($individual_fact_privacy[$xref][$this->tag])) { 271518bbdc1SGreg Roach return $individual_fact_privacy[$xref][$this->tag] >= $access_level; 272a25f0a04SGreg Roach } 273518bbdc1SGreg Roach if (isset($fact_privacy[$this->tag])) { 274518bbdc1SGreg Roach return $fact_privacy[$this->tag] >= $access_level; 275a25f0a04SGreg Roach } 276a25f0a04SGreg Roach 277a25f0a04SGreg Roach // No restrictions - it must be public 278a25f0a04SGreg Roach return true; 279a25f0a04SGreg Roach } 280a25f0a04SGreg Roach 281a25f0a04SGreg Roach /** 282a25f0a04SGreg Roach * Check whether this fact is protected against edit 283a25f0a04SGreg Roach * 284cbc1590aSGreg Roach * @return bool 285a25f0a04SGreg Roach */ 2868f53f488SRico Sonntag public function canEdit(): bool 287c1010edaSGreg Roach { 288a25f0a04SGreg Roach // Managers can edit anything 289a25f0a04SGreg Roach // Members cannot edit RESN, CHAN and locked records 290a25f0a04SGreg Roach return 291a25f0a04SGreg Roach $this->parent->canEdit() && !$this->isPendingDeletion() && ( 2924b9ff166SGreg Roach Auth::isManager($this->parent->getTree()) || 2934b9ff166SGreg Roach Auth::isEditor($this->parent->getTree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() != 'RESN' && $this->getTag() != 'CHAN' 294a25f0a04SGreg Roach ); 295a25f0a04SGreg Roach } 296a25f0a04SGreg Roach 297a25f0a04SGreg Roach /** 298a25f0a04SGreg Roach * The place where the event occured. 299a25f0a04SGreg Roach * 300a25f0a04SGreg Roach * @return Place 301a25f0a04SGreg Roach */ 3028f53f488SRico Sonntag public function getPlace(): Place 303c1010edaSGreg Roach { 304a25f0a04SGreg Roach if ($this->place === null) { 30584caa210SGreg Roach $this->place = new Place($this->getAttribute('PLAC'), $this->getParent()->getTree()); 306a25f0a04SGreg Roach } 307a25f0a04SGreg Roach 308a25f0a04SGreg Roach return $this->place; 309a25f0a04SGreg Roach } 310a25f0a04SGreg Roach 311a25f0a04SGreg Roach /** 312a25f0a04SGreg Roach * Get the date for this fact. 313a25f0a04SGreg Roach * We can call this function many times, especially when sorting, 314a25f0a04SGreg Roach * so keep a copy of the date. 315a25f0a04SGreg Roach * 316a25f0a04SGreg Roach * @return Date 317a25f0a04SGreg Roach */ 3188f53f488SRico Sonntag public function getDate(): Date 319c1010edaSGreg Roach { 320a25f0a04SGreg Roach if ($this->date === null) { 321a25f0a04SGreg Roach $this->date = new Date($this->getAttribute('DATE')); 322a25f0a04SGreg Roach } 323a25f0a04SGreg Roach 324a25f0a04SGreg Roach return $this->date; 325a25f0a04SGreg Roach } 326a25f0a04SGreg Roach 327a25f0a04SGreg Roach /** 328a25f0a04SGreg Roach * The raw GEDCOM data for this fact 329a25f0a04SGreg Roach * 330a25f0a04SGreg Roach * @return string 331a25f0a04SGreg Roach */ 3328f53f488SRico Sonntag public function getGedcom(): string 333c1010edaSGreg Roach { 334a25f0a04SGreg Roach return $this->gedcom; 335a25f0a04SGreg Roach } 336a25f0a04SGreg Roach 337a25f0a04SGreg Roach /** 338a25f0a04SGreg Roach * Get a (pseudo) primary key for this fact. 339a25f0a04SGreg Roach * 340a25f0a04SGreg Roach * @return string 341a25f0a04SGreg Roach */ 3428f53f488SRico Sonntag public function getFactId(): string 343c1010edaSGreg Roach { 344a25f0a04SGreg Roach return $this->fact_id; 345a25f0a04SGreg Roach } 346a25f0a04SGreg Roach 347a25f0a04SGreg Roach /** 348a25f0a04SGreg Roach * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 349a25f0a04SGreg Roach * 350a25f0a04SGreg Roach * @return string 351a25f0a04SGreg Roach */ 3528f53f488SRico Sonntag public function getTag(): string 353c1010edaSGreg Roach { 354a25f0a04SGreg Roach return $this->tag; 355a25f0a04SGreg Roach } 356a25f0a04SGreg Roach 357a25f0a04SGreg Roach /** 358a25f0a04SGreg Roach * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL) 359a25f0a04SGreg Roach * 360a25f0a04SGreg Roach * @param string $tag 3617e96c925SGreg Roach * 3627e96c925SGreg Roach * @return void 363a25f0a04SGreg Roach */ 364c1010edaSGreg Roach public function setTag($tag) 365c1010edaSGreg Roach { 366a25f0a04SGreg Roach $this->tag = $tag; 367a25f0a04SGreg Roach } 368a25f0a04SGreg Roach 369a25f0a04SGreg Roach /** 370a25f0a04SGreg Roach * The Person/Family record where this Fact came from 371a25f0a04SGreg Roach * 3726e59b7c4SGreg Roach * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord 373a25f0a04SGreg Roach */ 374c1010edaSGreg Roach public function getParent() 375c1010edaSGreg Roach { 376a25f0a04SGreg Roach return $this->parent; 377a25f0a04SGreg Roach } 378a25f0a04SGreg Roach 379a25f0a04SGreg Roach /** 380a25f0a04SGreg Roach * Get the name of this fact type, for use as a label. 381a25f0a04SGreg Roach * 382a25f0a04SGreg Roach * @return string 383a25f0a04SGreg Roach */ 3848f53f488SRico Sonntag public function getLabel(): string 385c1010edaSGreg Roach { 386a25f0a04SGreg Roach // Custom FACT/EVEN - with a TYPE 3874e5adf68SGreg Roach if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->getAttribute('TYPE') !== '') { 388d53324c9SGreg Roach return I18N::translate(e($this->getAttribute('TYPE'))); 389a25f0a04SGreg Roach } 3904e5adf68SGreg Roach 391764a01d9SGreg Roach return GedcomTag::getLabel($this->tag, $this->parent); 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach 394a25f0a04SGreg Roach /** 395a25f0a04SGreg Roach * This is a newly deleted fact, pending approval. 3967e96c925SGreg Roach * 3977e96c925SGreg Roach * @return void 398a25f0a04SGreg Roach */ 399c1010edaSGreg Roach public function setPendingDeletion() 400c1010edaSGreg Roach { 401a25f0a04SGreg Roach $this->pending_deletion = true; 402a25f0a04SGreg Roach $this->pending_addition = false; 403a25f0a04SGreg Roach } 404a25f0a04SGreg Roach 405a25f0a04SGreg Roach /** 406a25f0a04SGreg Roach * Is this a newly deleted fact, pending approval. 407a25f0a04SGreg Roach * 408cbc1590aSGreg Roach * @return bool 409a25f0a04SGreg Roach */ 4108f53f488SRico Sonntag public function isPendingDeletion(): bool 411c1010edaSGreg Roach { 412a25f0a04SGreg Roach return $this->pending_deletion; 413a25f0a04SGreg Roach } 414a25f0a04SGreg Roach 415a25f0a04SGreg Roach /** 416a25f0a04SGreg Roach * This is a newly added fact, pending approval. 4177e96c925SGreg Roach * 4187e96c925SGreg Roach * @return void 419a25f0a04SGreg Roach */ 420c1010edaSGreg Roach public function setPendingAddition() 421c1010edaSGreg Roach { 422a25f0a04SGreg Roach $this->pending_addition = true; 423a25f0a04SGreg Roach $this->pending_deletion = false; 424a25f0a04SGreg Roach } 425a25f0a04SGreg Roach 426a25f0a04SGreg Roach /** 427a25f0a04SGreg Roach * Is this a newly added fact, pending approval. 428a25f0a04SGreg Roach * 429cbc1590aSGreg Roach * @return bool 430a25f0a04SGreg Roach */ 4318f53f488SRico Sonntag public function isPendingAddition(): bool 432c1010edaSGreg Roach { 433a25f0a04SGreg Roach return $this->pending_addition; 434a25f0a04SGreg Roach } 435a25f0a04SGreg Roach 436a25f0a04SGreg Roach /** 437a25f0a04SGreg Roach * Source citations linked to this fact 438a25f0a04SGreg Roach * 439a25f0a04SGreg Roach * @return string[] 440a25f0a04SGreg Roach */ 4418f53f488SRico Sonntag public function getCitations(): array 442c1010edaSGreg Roach { 443a25f0a04SGreg Roach preg_match_all('/\n(2 SOUR @(' . WT_REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->getGedcom(), $matches, PREG_SET_ORDER); 44413abd6f3SGreg Roach $citations = []; 445a25f0a04SGreg Roach foreach ($matches as $match) { 44624ec66ceSGreg Roach $source = Source::getInstance($match[2], $this->getParent()->getTree()); 4472859ecedSJonathan Jaubart if ($source && $source->canShow()) { 448a25f0a04SGreg Roach $citations[] = $match[1]; 449a25f0a04SGreg Roach } 450a25f0a04SGreg Roach } 451a25f0a04SGreg Roach 452a25f0a04SGreg Roach return $citations; 453a25f0a04SGreg Roach } 454a25f0a04SGreg Roach 455a25f0a04SGreg Roach /** 456a25f0a04SGreg Roach * Notes (inline and objects) linked to this fact 457a25f0a04SGreg Roach * 458a25f0a04SGreg Roach * @return string[]|Note[] 459a25f0a04SGreg Roach */ 4608f53f488SRico Sonntag public function getNotes(): array 461c1010edaSGreg Roach { 46213abd6f3SGreg Roach $notes = []; 463a25f0a04SGreg Roach preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->getGedcom(), $matches); 464a25f0a04SGreg Roach foreach ($matches[1] as $match) { 465a25f0a04SGreg Roach $note = preg_replace("/\n3 CONT ?/", "\n", $match); 466a25f0a04SGreg Roach if (preg_match('/@(' . WT_REGEX_XREF . ')@/', $note, $nmatch)) { 46724ec66ceSGreg Roach $note = Note::getInstance($nmatch[1], $this->getParent()->getTree()); 468a25f0a04SGreg Roach if ($note && $note->canShow()) { 469a25f0a04SGreg Roach // A note object 470a25f0a04SGreg Roach $notes[] = $note; 471a25f0a04SGreg Roach } 472a25f0a04SGreg Roach } else { 473a25f0a04SGreg Roach // An inline note 474a25f0a04SGreg Roach $notes[] = $note; 475a25f0a04SGreg Roach } 476a25f0a04SGreg Roach } 477a25f0a04SGreg Roach 478a25f0a04SGreg Roach return $notes; 479a25f0a04SGreg Roach } 480a25f0a04SGreg Roach 481a25f0a04SGreg Roach /** 482a25f0a04SGreg Roach * Media objects linked to this fact 483a25f0a04SGreg Roach * 484a25f0a04SGreg Roach * @return Media[] 485a25f0a04SGreg Roach */ 4868f53f488SRico Sonntag public function getMedia(): array 487c1010edaSGreg Roach { 48813abd6f3SGreg Roach $media = []; 489a25f0a04SGreg Roach preg_match_all('/\n2 OBJE @(' . WT_REGEX_XREF . ')@/', $this->getGedcom(), $matches); 490a25f0a04SGreg Roach foreach ($matches[1] as $match) { 49124ec66ceSGreg Roach $obje = Media::getInstance($match, $this->getParent()->getTree()); 4922859ecedSJonathan Jaubart if ($obje && $obje->canShow()) { 493a25f0a04SGreg Roach $media[] = $obje; 494a25f0a04SGreg Roach } 495a25f0a04SGreg Roach } 496a25f0a04SGreg Roach 497a25f0a04SGreg Roach return $media; 498a25f0a04SGreg Roach } 499a25f0a04SGreg Roach 500a25f0a04SGreg Roach /** 501a25f0a04SGreg Roach * A one-line summary of the fact - for charts, etc. 502a25f0a04SGreg Roach * 503a25f0a04SGreg Roach * @return string 504a25f0a04SGreg Roach */ 5058f53f488SRico Sonntag public function summary(): string 506c1010edaSGreg Roach { 50713abd6f3SGreg Roach $attributes = []; 508a25f0a04SGreg Roach $target = $this->getTarget(); 509*db7bb364SGreg Roach if ($target instanceof GedcomRecord) { 510a25f0a04SGreg Roach $attributes[] = $target->getFullName(); 511a25f0a04SGreg Roach } else { 512726d7b07SGreg Roach // Fact value 513a25f0a04SGreg Roach $value = $this->getValue(); 514726d7b07SGreg Roach if ($value !== '' && $value !== 'Y') { 515d53324c9SGreg Roach $attributes[] = '<span dir="auto">' . e($value) . '</span>'; 516a25f0a04SGreg Roach } 517726d7b07SGreg Roach // Fact date 518a25f0a04SGreg Roach $date = $this->getDate(); 519726d7b07SGreg Roach if ($date->isOK()) { 520af459ec4SGreg Roach if (in_array($this->getTag(), explode('|', WT_EVENTS_BIRT)) && $this->getParent() instanceof Individual && $this->getParent()->getTree()->getPreference('SHOW_PARENTS_AGE')) { 5213d7a8a4cSGreg Roach $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->getParent(), $date); 522a25f0a04SGreg Roach } else { 523a25f0a04SGreg Roach $attributes[] = $date->display(); 524a25f0a04SGreg Roach } 525726d7b07SGreg Roach } 526726d7b07SGreg Roach // Fact place 527726d7b07SGreg Roach if (!$this->getPlace()->isEmpty()) { 528726d7b07SGreg Roach $attributes[] = $this->getPlace()->getShortName(); 529a25f0a04SGreg Roach } 530a25f0a04SGreg Roach } 5312d8f08abSGreg Roach 5322d8f08abSGreg Roach $class = 'fact_' . $this->getTag(); 533a25f0a04SGreg Roach if ($this->isPendingAddition()) { 5342d8f08abSGreg Roach $class .= ' new'; 535a25f0a04SGreg Roach } elseif ($this->isPendingDeletion()) { 5362d8f08abSGreg Roach $class .= ' old'; 537a25f0a04SGreg Roach } 5382d8f08abSGreg Roach 5392d8f08abSGreg Roach return 5402d8f08abSGreg Roach '<div class="' . $class . '">' . 5412d8f08abSGreg Roach /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ 5422d8f08abSGreg Roach I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->getLabel(), implode(' — ', $attributes)) . 5432d8f08abSGreg Roach '</div>'; 544a25f0a04SGreg Roach } 545a25f0a04SGreg Roach 546a25f0a04SGreg Roach /** 547a25f0a04SGreg Roach * Static Helper functions to sort events 548a25f0a04SGreg Roach * 549a25f0a04SGreg Roach * @param Fact $a Fact one 550a25f0a04SGreg Roach * @param Fact $b Fact two 551a25f0a04SGreg Roach * 552cbc1590aSGreg Roach * @return int 553a25f0a04SGreg Roach */ 554c1010edaSGreg Roach public static function compareDate(Fact $a, Fact $b) 555c1010edaSGreg Roach { 556a25f0a04SGreg Roach if ($a->getDate()->isOK() && $b->getDate()->isOK()) { 557a25f0a04SGreg Roach // If both events have dates, compare by date 558f5b60decSGreg Roach $ret = Date::compare($a->getDate(), $b->getDate()); 559a25f0a04SGreg Roach 560a25f0a04SGreg Roach if ($ret == 0) { 561a25f0a04SGreg Roach // If dates are the same, compare by fact type 562a25f0a04SGreg Roach $ret = self::compareType($a, $b); 563a25f0a04SGreg Roach 564a25f0a04SGreg Roach // If the fact type is also the same, retain the initial order 565a25f0a04SGreg Roach if ($ret == 0) { 566a25f0a04SGreg Roach $ret = $a->sortOrder - $b->sortOrder; 567a25f0a04SGreg Roach } 568a25f0a04SGreg Roach } 569a25f0a04SGreg Roach 570a25f0a04SGreg Roach return $ret; 571a25f0a04SGreg Roach } else { 572a25f0a04SGreg Roach // One or both events have no date - retain the initial order 573a25f0a04SGreg Roach return $a->sortOrder - $b->sortOrder; 574a25f0a04SGreg Roach } 575a25f0a04SGreg Roach } 576a25f0a04SGreg Roach 577a25f0a04SGreg Roach /** 578a25f0a04SGreg Roach * Static method to compare two events by their type. 579a25f0a04SGreg Roach * 580a25f0a04SGreg Roach * @param Fact $a Fact one 581a25f0a04SGreg Roach * @param Fact $b Fact two 582a25f0a04SGreg Roach * 583cbc1590aSGreg Roach * @return int 584a25f0a04SGreg Roach */ 5858f53f488SRico Sonntag public static function compareType(Fact $a, Fact $b): int 586c1010edaSGreg Roach { 587bbe4546fSGreg Roach static $factsort = []; 588a25f0a04SGreg Roach 589a25f0a04SGreg Roach if (empty($factsort)) { 590bbe4546fSGreg Roach $factsort = array_flip(self::FACT_ORDER); 591a25f0a04SGreg Roach } 592a25f0a04SGreg Roach 593a25f0a04SGreg Roach // Facts from same families stay grouped together 594a25f0a04SGreg Roach // Keep MARR and DIV from the same families from mixing with events from other FAMs 595a25f0a04SGreg Roach // Use the original order in which the facts were added 596a25f0a04SGreg Roach if ($a->parent instanceof Family && $b->parent instanceof Family && $a->parent !== $b->parent) { 597a25f0a04SGreg Roach return $a->sortOrder - $b->sortOrder; 598a25f0a04SGreg Roach } 599a25f0a04SGreg Roach 600a25f0a04SGreg Roach $atag = $a->getTag(); 601a25f0a04SGreg Roach $btag = $b->getTag(); 602a25f0a04SGreg Roach 603a25f0a04SGreg Roach // Events not in the above list get mapped onto one that is. 604a25f0a04SGreg Roach if (!array_key_exists($atag, $factsort)) { 605a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) { 606a25f0a04SGreg Roach $atag = $match[1]; 607a25f0a04SGreg Roach } else { 6087a6ee1acSGreg Roach $atag = '_????_'; 609a25f0a04SGreg Roach } 610a25f0a04SGreg Roach } 611a25f0a04SGreg Roach 612a25f0a04SGreg Roach if (!array_key_exists($btag, $factsort)) { 613a25f0a04SGreg Roach if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) { 614a25f0a04SGreg Roach $btag = $match[1]; 615a25f0a04SGreg Roach } else { 6167a6ee1acSGreg Roach $btag = '_????_'; 617a25f0a04SGreg Roach } 618a25f0a04SGreg Roach } 619a25f0a04SGreg Roach 620a25f0a04SGreg Roach // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI 621a25f0a04SGreg Roach // - Treat dated after BURI facts as BURI instead 622baacc364SGreg Roach if ($a->getAttribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { 623a25f0a04SGreg Roach $atag = 'BURI'; 624a25f0a04SGreg Roach } 625a25f0a04SGreg Roach 626baacc364SGreg Roach if ($b->getAttribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { 627a25f0a04SGreg Roach $btag = 'BURI'; 628a25f0a04SGreg Roach } 629a25f0a04SGreg Roach 630a25f0a04SGreg Roach $ret = $factsort[$atag] - $factsort[$btag]; 631a25f0a04SGreg Roach 632a25f0a04SGreg Roach // If facts are the same then put dated facts before non-dated facts 633a25f0a04SGreg Roach if ($ret == 0) { 634baacc364SGreg Roach if ($a->getAttribute('DATE') !== '' && $b->getAttribute('DATE') === '') { 635a25f0a04SGreg Roach return -1; 636a25f0a04SGreg Roach } 637a25f0a04SGreg Roach 638baacc364SGreg Roach if ($b->getAttribute('DATE') !== '' && $a->getAttribute('DATE') === '') { 639a25f0a04SGreg Roach return 1; 640a25f0a04SGreg Roach } 641a25f0a04SGreg Roach 642a25f0a04SGreg Roach // If no sorting preference, then keep original ordering 643a25f0a04SGreg Roach $ret = $a->sortOrder - $b->sortOrder; 644a25f0a04SGreg Roach } 645a25f0a04SGreg Roach 646a25f0a04SGreg Roach return $ret; 647a25f0a04SGreg Roach } 648a25f0a04SGreg Roach 649a25f0a04SGreg Roach /** 650a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 651a25f0a04SGreg Roach * 652a25f0a04SGreg Roach * @return string 653a25f0a04SGreg Roach */ 654c1010edaSGreg Roach public function __toString() 655c1010edaSGreg Roach { 656a25f0a04SGreg Roach return $this->fact_id . '@' . $this->parent->getXref(); 657a25f0a04SGreg Roach } 658a25f0a04SGreg Roach} 659