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