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