1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16namespace Fisharebest\Webtrees; 17 18use Fisharebest\Webtrees\Functions\FunctionsPrint; 19use InvalidArgumentException; 20 21/** 22 * A GEDCOM fact or event object. 23 */ 24class Fact { 25 /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */ 26 private $fact_id; 27 28 /** @var GedcomRecord The GEDCOM record from which this fact is taken */ 29 private $parent; 30 31 /** @var string The raw GEDCOM data for this fact */ 32 private $gedcom; 33 34 /** @var string The GEDCOM tag for this record */ 35 private $tag; 36 37 /** @var bool Is this a recently deleted fact, pending approval? */ 38 private $pending_deletion = false; 39 40 /** @var bool Is this a recently added fact, pending approval? */ 41 private $pending_addition = false; 42 43 /** @var Date The date of this fact, from the “2 DATE …” attribute */ 44 private $date; 45 46 /** @var Place The place of this fact, from the “2 PLAC …” attribute */ 47 private $place; 48 49 /** @var int Temporary(!) variable Used by Functions::sortFacts() */ 50 public $sortOrder; 51 52 /** 53 * Create an event object from a gedcom fragment. 54 * We need the parent object (to check privacy) and a (pseudo) fact ID to 55 * identify the fact within the record. 56 * 57 * @param string $gedcom 58 * @param GedcomRecord $parent 59 * @param string $fact_id 60 * 61 * @throws InvalidArgumentException 62 */ 63 public function __construct($gedcom, GedcomRecord $parent, $fact_id) { 64 if (preg_match('/^1 (' . WT_REGEX_TAG . ')/', $gedcom, $match)) { 65 $this->gedcom = $gedcom; 66 $this->parent = $parent; 67 $this->fact_id = $fact_id; 68 $this->tag = $match[1]; 69 } else { 70 throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')'); 71 } 72 } 73 74 /** 75 * Get the value of level 1 data in the fact 76 * Allow for multi-line values 77 * 78 * @return string 79 */ 80 public function getValue() { 81 if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) { 82 return preg_replace("/\n2 CONT ?/", "\n", $match[1]); 83 } else { 84 return ''; 85 } 86 } 87 88 /** 89 * Get the record to which this fact links 90 * 91 * @return Individual|Family|Source|Repository|Media|Note|null 92 */ 93 public function getTarget() { 94 $xref = trim($this->getValue(), '@'); 95 switch ($this->tag) { 96 case 'FAMC': 97 case 'FAMS': 98 return Family::getInstance($xref, $this->getParent()->getTree()); 99 case 'HUSB': 100 case 'WIFE': 101 case 'CHIL': 102 return Individual::getInstance($xref, $this->getParent()->getTree()); 103 case 'SOUR': 104 return Source::getInstance($xref, $this->getParent()->getTree()); 105 case 'OBJE': 106 return Media::getInstance($xref, $this->getParent()->getTree()); 107 case 'REPO': 108 return Repository::getInstance($xref, $this->getParent()->getTree()); 109 case 'NOTE': 110 return Note::getInstance($xref, $this->getParent()->getTree()); 111 default: 112 return GedcomRecord::getInstance($xref, $this->getParent()->getTree()); 113 } 114 } 115 116 /** 117 * Get the value of level 2 data in the fact 118 * 119 * @param string $tag 120 * 121 * @return string 122 */ 123 public function getAttribute($tag) { 124 if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { 125 return preg_replace("/\n3 CONT ?/", "\n", $match[1]); 126 } else { 127 return ''; 128 } 129 } 130 131 /** 132 * Do the privacy rules allow us to display this fact to the current user 133 * 134 * @param int|null $access_level 135 * 136 * @return bool 137 */ 138 public function canShow($access_level = null) { 139 if ($access_level === null) { 140 $access_level = Auth::accessLevel($this->getParent()->getTree()); 141 } 142 143 // Does this record have an explicit RESN? 144 if (strpos($this->gedcom, "\n2 RESN confidential")) { 145 return Auth::PRIV_NONE >= $access_level; 146 } 147 if (strpos($this->gedcom, "\n2 RESN privacy")) { 148 return Auth::PRIV_USER >= $access_level; 149 } 150 if (strpos($this->gedcom, "\n2 RESN none")) { 151 return true; 152 } 153 154 // Does this record have a default RESN? 155 $xref = $this->parent->getXref(); 156 $fact_privacy = $this->parent->getTree()->getFactPrivacy(); 157 $individual_fact_privacy = $this->parent->getTree()->getIndividualFactPrivacy(); 158 if (isset($individual_fact_privacy[$xref][$this->tag])) { 159 return $individual_fact_privacy[$xref][$this->tag] >= $access_level; 160 } 161 if (isset($fact_privacy[$this->tag])) { 162 return $fact_privacy[$this->tag] >= $access_level; 163 } 164 165 // No restrictions - it must be public 166 return true; 167 } 168 169 /** 170 * Check whether this fact is protected against edit 171 * 172 * @return bool 173 */ 174 public function canEdit() { 175 // Managers can edit anything 176 // Members cannot edit RESN, CHAN and locked records 177 return 178 $this->parent->canEdit() && !$this->isPendingDeletion() && ( 179 Auth::isManager($this->parent->getTree()) || 180 Auth::isEditor($this->parent->getTree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() != 'RESN' && $this->getTag() != 'CHAN' 181 ); 182 } 183 184 /** 185 * The place where the event occured. 186 * 187 * @return Place 188 */ 189 public function getPlace() { 190 if ($this->place === null) { 191 $this->place = new Place($this->getAttribute('PLAC'), $this->getParent()->getTree()); 192 } 193 194 return $this->place; 195 } 196 197 /** 198 * Get the date for this fact. 199 * We can call this function many times, especially when sorting, 200 * so keep a copy of the date. 201 * 202 * @return Date 203 */ 204 public function getDate() { 205 if ($this->date === null) { 206 $this->date = new Date($this->getAttribute('DATE')); 207 } 208 209 return $this->date; 210 } 211 212 /** 213 * The raw GEDCOM data for this fact 214 * 215 * @return string 216 */ 217 public function getGedcom() { 218 return $this->gedcom; 219 } 220 221 /** 222 * Get a (pseudo) primary key for this fact. 223 * 224 * @return string 225 */ 226 public function getFactId() { 227 return $this->fact_id; 228 } 229 230 // What sort of fact is this? 231 /** 232 * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. 233 * 234 * @return string 235 */ 236 public function getTag() { 237 return $this->tag; 238 } 239 240 /** 241 * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL) 242 * 243 * @param string $tag 244 */ 245 public function setTag($tag) { 246 $this->tag = $tag; 247 } 248 249 // 250 /** 251 * The Person/Family record where this Fact came from 252 * 253 * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord 254 */ 255 public function getParent() { 256 return $this->parent; 257 } 258 259 /** 260 * Get the name of this fact type, for use as a label. 261 * 262 * @return string 263 */ 264 public function getLabel() { 265 // Custom FACT/EVEN - with a TYPE 266 if (($this->tag ==='FACT' || $this->tag === 'EVEN') && $this->getAttribute('TYPE') !== '') { 267 return I18N::translate(e($this->getAttribute('TYPE'))); 268 } 269 270 return GedcomTag::getLabel($this->tag, $this->parent); 271 } 272 273 /** 274 * This is a newly deleted fact, pending approval. 275 */ 276 public function setPendingDeletion() { 277 $this->pending_deletion = true; 278 $this->pending_addition = false; 279 } 280 281 /** 282 * Is this a newly deleted fact, pending approval. 283 * 284 * @return bool 285 */ 286 public function isPendingDeletion() { 287 return $this->pending_deletion; 288 } 289 290 /** 291 * This is a newly added fact, pending approval. 292 */ 293 public function setPendingAddition() { 294 $this->pending_addition = true; 295 $this->pending_deletion = false; 296 } 297 298 /** 299 * Is this a newly added fact, pending approval. 300 * 301 * @return bool 302 */ 303 public function isPendingAddition() { 304 return $this->pending_addition; 305 } 306 307 /** 308 * Source citations linked to this fact 309 * 310 * @return string[] 311 */ 312 public function getCitations() { 313 preg_match_all('/\n(2 SOUR @(' . WT_REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->getGedcom(), $matches, PREG_SET_ORDER); 314 $citations = []; 315 foreach ($matches as $match) { 316 $source = Source::getInstance($match[2], $this->getParent()->getTree()); 317 if ($source->canShow()) { 318 $citations[] = $match[1]; 319 } 320 } 321 322 return $citations; 323 } 324 325 /** 326 * Notes (inline and objects) linked to this fact 327 * 328 * @return string[]|Note[] 329 */ 330 public function getNotes() { 331 $notes = []; 332 preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->getGedcom(), $matches); 333 foreach ($matches[1] as $match) { 334 $note = preg_replace("/\n3 CONT ?/", "\n", $match); 335 if (preg_match('/@(' . WT_REGEX_XREF . ')@/', $note, $nmatch)) { 336 $note = Note::getInstance($nmatch[1], $this->getParent()->getTree()); 337 if ($note && $note->canShow()) { 338 // A note object 339 $notes[] = $note; 340 } 341 } else { 342 // An inline note 343 $notes[] = $note; 344 } 345 } 346 347 return $notes; 348 } 349 350 /** 351 * Media objects linked to this fact 352 * 353 * @return Media[] 354 */ 355 public function getMedia() { 356 $media = []; 357 preg_match_all('/\n2 OBJE @(' . WT_REGEX_XREF . ')@/', $this->getGedcom(), $matches); 358 foreach ($matches[1] as $match) { 359 $obje = Media::getInstance($match, $this->getParent()->getTree()); 360 if ($obje->canShow()) { 361 $media[] = $obje; 362 } 363 } 364 365 return $media; 366 } 367 368 /** 369 * A one-line summary of the fact - for charts, etc. 370 * 371 * @return string 372 */ 373 public function summary() { 374 $attributes = []; 375 $target = $this->getTarget(); 376 if ($target) { 377 $attributes[] = $target->getFullName(); 378 } else { 379 // Fact value 380 $value = $this->getValue(); 381 if ($value !== '' && $value !== 'Y') { 382 $attributes[] = '<span dir="auto">' . e($value) . '</span>'; 383 } 384 // Fact date 385 $date = $this->getDate(); 386 if ($date->isOK()) { 387 if (in_array($this->getTag(), explode('|', WT_EVENTS_BIRT)) && $this->getParent() instanceof Individual && $this->getParent()->getTree()->getPreference('SHOW_PARENTS_AGE')) { 388 $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->getParent(), $date); 389 } else { 390 $attributes[] = $date->display(); 391 } 392 } 393 // Fact place 394 if (!$this->getPlace()->isEmpty()) { 395 $attributes[] = $this->getPlace()->getShortName(); 396 } 397 } 398 399 $class = 'fact_' . $this->getTag(); 400 if ($this->isPendingAddition()) { 401 $class .= ' new'; 402 } elseif ($this->isPendingDeletion()) { 403 $class .= ' old'; 404 } 405 406 return 407 '<div class="' . $class . '">' . 408 /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ 409 I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->getLabel(), implode(' — ', $attributes)) . 410 '</div>'; 411 } 412 413 /** 414 * Static Helper functions to sort events 415 * 416 * @param Fact $a Fact one 417 * @param Fact $b Fact two 418 * 419 * @return int 420 */ 421 public static function compareDate(Fact $a, Fact $b) { 422 if ($a->getDate()->isOK() && $b->getDate()->isOK()) { 423 // If both events have dates, compare by date 424 $ret = Date::compare($a->getDate(), $b->getDate()); 425 426 if ($ret == 0) { 427 // If dates are the same, compare by fact type 428 $ret = self::compareType($a, $b); 429 430 // If the fact type is also the same, retain the initial order 431 if ($ret == 0) { 432 $ret = $a->sortOrder - $b->sortOrder; 433 } 434 } 435 436 return $ret; 437 } else { 438 // One or both events have no date - retain the initial order 439 return $a->sortOrder - $b->sortOrder; 440 } 441 } 442 443 /** 444 * Static method to compare two events by their type. 445 * 446 * @param Fact $a Fact one 447 * @param Fact $b Fact two 448 * 449 * @return int 450 */ 451 public static function compareType(Fact $a, Fact $b) { 452 global $factsort; 453 454 if (empty($factsort)) { 455 $factsort = array_flip( 456 [ 457 'BIRT', 458 '_HNM', 459 'ALIA', '_AKA', '_AKAN', 460 'ADOP', '_ADPF', '_ADPF', 461 '_BRTM', 462 'CHR', 'BAPM', 463 'FCOM', 464 'CONF', 465 'BARM', 'BASM', 466 'EDUC', 467 'GRAD', 468 '_DEG', 469 'EMIG', 'IMMI', 470 'NATU', 471 '_MILI', '_MILT', 472 'ENGA', 473 'MARB', 'MARC', 'MARL', '_MARI', '_MBON', 474 'MARR', 'MARR_CIVIL', 'MARR_RELIGIOUS', 'MARR_PARTNERS', 'MARR_UNKNOWN', '_COML', 475 '_STAT', 476 '_SEPR', 477 'DIVF', 478 'MARS', 479 '_BIRT_CHIL', 480 'DIV', 'ANUL', 481 '_BIRT_', '_MARR_', '_DEAT_', '_BURI_', // other events of close relatives 482 'CENS', 483 'OCCU', 484 'RESI', 485 'PROP', 486 'CHRA', 487 'RETI', 488 'FACT', 'EVEN', 489 '_NMR', '_NMAR', 'NMR', 490 'NCHI', 491 'WILL', 492 '_HOL', 493 '_????_', 494 'DEAT', 495 '_FNRL', 'CREM', 'BURI', '_INTE', 496 '_YART', 497 '_NLIV', 498 'PROB', 499 'TITL', 500 'COMM', 501 'NATI', 502 'CITN', 503 'CAST', 504 'RELI', 505 'SSN', 'IDNO', 506 'TEMP', 507 'SLGC', 'BAPL', 'CONL', 'ENDL', 'SLGS', 508 'ADDR', 'PHON', 'EMAIL', '_EMAIL', 'EMAL', 'FAX', 'WWW', 'URL', '_URL', 509 'FILE', // For media objects 510 'AFN', 'REFN', '_PRMN', 'REF', 'RIN', '_UID', 511 'OBJE', 'NOTE', 'SOUR', 512 'CHAN', '_TODO', 513 ] 514 ); 515 } 516 517 // Facts from same families stay grouped together 518 // Keep MARR and DIV from the same families from mixing with events from other FAMs 519 // Use the original order in which the facts were added 520 if ($a->parent instanceof Family && $b->parent instanceof Family && $a->parent !== $b->parent) { 521 return $a->sortOrder - $b->sortOrder; 522 } 523 524 $atag = $a->getTag(); 525 $btag = $b->getTag(); 526 527 // Events not in the above list get mapped onto one that is. 528 if (!array_key_exists($atag, $factsort)) { 529 if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) { 530 $atag = $match[1]; 531 } else { 532 $atag = '_????_'; 533 } 534 } 535 536 if (!array_key_exists($btag, $factsort)) { 537 if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) { 538 $btag = $match[1]; 539 } else { 540 $btag = '_????_'; 541 } 542 } 543 544 // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI 545 // - Treat dated after BURI facts as BURI instead 546 if ($a->getAttribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { 547 $atag = 'BURI'; 548 } 549 550 if ($b->getAttribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { 551 $btag = 'BURI'; 552 } 553 554 $ret = $factsort[$atag] - $factsort[$btag]; 555 556 // If facts are the same then put dated facts before non-dated facts 557 if ($ret == 0) { 558 if ($a->getAttribute('DATE') !== '' && $b->getAttribute('DATE') === '') { 559 return -1; 560 } 561 562 if ($b->getAttribute('DATE') !== '' && $a->getAttribute('DATE') === '') { 563 return 1; 564 } 565 566 // If no sorting preference, then keep original ordering 567 $ret = $a->sortOrder - $b->sortOrder; 568 } 569 570 return $ret; 571 } 572 573 /** 574 * Allow native PHP functions such as array_unique() to work with objects 575 * 576 * @return string 577 */ 578 public function __toString() { 579 return $this->fact_id . '@' . $this->parent->getXref(); 580 } 581} 582