xref: /webtrees/app/GedcomRecord.php (revision 69546be111c306e6607e599b4290c298a62cff79)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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\Functions;
19use Fisharebest\Webtrees\Functions\FunctionsDate;
20use Fisharebest\Webtrees\Functions\FunctionsImport;
21use Fisharebest\Webtrees\Functions\FunctionsPrint;
22
23/**
24 * A GEDCOM object.
25 */
26class GedcomRecord {
27	const RECORD_TYPE = 'UNKNOWN';
28	const URL_PREFIX  = 'gedrecord.php?pid=';
29
30	/** @var string The record identifier */
31	protected $xref;
32
33	/** @var Tree  The family tree to which this record belongs */
34	protected $tree;
35
36	/** @var string  GEDCOM data (before any pending edits) */
37	protected $gedcom;
38
39	/** @var string|null  GEDCOM data (after any pending edits) */
40	protected $pending;
41
42	/** @var Fact[] facts extracted from $gedcom/$pending */
43	protected $facts;
44
45	/** @var bool Can we display details of this record to Auth::PRIV_PRIVATE */
46	private $disp_public;
47
48	/** @var bool Can we display details of this record to Auth::PRIV_USER */
49	private $disp_user;
50
51	/** @var bool Can we display details of this record to Auth::PRIV_NONE */
52	private $disp_none;
53
54	/** @var string[][] All the names of this individual */
55	protected $_getAllNames;
56
57	/** @var int Cached result */
58	protected $_getPrimaryName;
59
60	/** @var int Cached result */
61	protected $_getSecondaryName;
62
63	/** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */
64	protected static $gedcom_record_cache;
65
66	/** @var \stdClass[][] Fetch all pending edits in one database query */
67	private static $pending_record_cache;
68
69	/**
70	 * Create a GedcomRecord object from raw GEDCOM data.
71	 *
72	 * @param string      $xref
73	 * @param string      $gedcom  an empty string for new/pending records
74	 * @param string|null $pending null for a record with no pending edits,
75	 *                             empty string for records with pending deletions
76	 * @param Tree        $tree
77	 */
78	public function __construct($xref, $gedcom, $pending, $tree) {
79		$this->xref    = $xref;
80		$this->gedcom  = $gedcom;
81		$this->pending = $pending;
82		$this->tree    = $tree;
83
84		$this->parseFacts();
85	}
86
87	/**
88	 * Split the record into facts
89	 */
90	private function parseFacts() {
91		// Split the record into facts
92		if ($this->gedcom) {
93			$gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom);
94			array_shift($gedcom_facts);
95		} else {
96			$gedcom_facts = [];
97		}
98		if ($this->pending) {
99			$pending_facts = preg_split('/\n(?=1)/s', $this->pending);
100			array_shift($pending_facts);
101		} else {
102			$pending_facts = [];
103		}
104
105		$this->facts = [];
106
107		foreach ($gedcom_facts as $gedcom_fact) {
108			$fact = new Fact($gedcom_fact, $this, md5($gedcom_fact));
109			if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts)) {
110				$fact->setPendingDeletion();
111			}
112			$this->facts[] = $fact;
113		}
114		foreach ($pending_facts as $pending_fact) {
115			if (!in_array($pending_fact, $gedcom_facts)) {
116				$fact = new Fact($pending_fact, $this, md5($pending_fact));
117				$fact->setPendingAddition();
118				$this->facts[] = $fact;
119			}
120		}
121	}
122
123	/**
124	 * Get an instance of a GedcomRecord object. For single records,
125	 * we just receive the XREF. For bulk records (such as lists
126	 * and search results) we can receive the GEDCOM data as well.
127	 *
128	 * @param string      $xref
129	 * @param Tree        $tree
130	 * @param string|null $gedcom
131	 *
132	 * @throws \Exception
133	 *
134	 * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null
135	 */
136	public static function getInstance($xref, Tree $tree, $gedcom = null) {
137		$tree_id = $tree->getTreeId();
138
139		// Is this record already in the cache, and of the correct type?
140		if (isset(self::$gedcom_record_cache[$xref][$tree_id])) {
141			$record = self::$gedcom_record_cache[$xref][$tree_id];
142
143			if ($record instanceof static) {
144				return $record;
145			} else {
146				null;
147			}
148		}
149
150		// Do we need to fetch the record from the database?
151		if ($gedcom === null) {
152			$gedcom = static::fetchGedcomRecord($xref, $tree_id);
153		}
154
155		// If we can edit, then we also need to be able to see pending records.
156		if (Auth::isEditor($tree)) {
157			if (!isset(self::$pending_record_cache[$tree_id])) {
158				// Fetch all pending records in one database query
159				self::$pending_record_cache[$tree_id] = [];
160				$rows                                 = Database::prepare(
161					"SELECT xref, new_gedcom FROM `##change` WHERE status = 'pending' AND gedcom_id = :tree_id ORDER BY change_id"
162				)->execute([
163					'tree_id' => $tree_id,
164				])->fetchAll();
165				foreach ($rows as $row) {
166					self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom;
167				}
168			}
169
170			if (isset(self::$pending_record_cache[$tree_id][$xref])) {
171				// A pending edit exists for this record
172				$pending = self::$pending_record_cache[$tree_id][$xref];
173			} else {
174				$pending = null;
175			}
176		} else {
177			// There are no pending changes for this record
178			$pending = null;
179		}
180
181		// No such record exists
182		if ($gedcom === null && $pending === null) {
183			return null;
184		}
185
186		// Create the object
187		if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom . $pending, $match)) {
188			$xref = $match[1]; // Collation - we may have requested I123 and found i123
189			$type = $match[2];
190		} elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) {
191			$xref = $match[1];
192			$type = $match[1];
193		} elseif ($gedcom . $pending) {
194			throw new \Exception('Unrecognized GEDCOM record: ' . $gedcom);
195		} else {
196			// A record with both pending creation and pending deletion
197			$type = static::RECORD_TYPE;
198		}
199
200		switch ($type) {
201		case 'INDI':
202			$record = new Individual($xref, $gedcom, $pending, $tree);
203			break;
204		case 'FAM':
205			$record = new Family($xref, $gedcom, $pending, $tree);
206			break;
207		case 'SOUR':
208			$record = new Source($xref, $gedcom, $pending, $tree);
209			break;
210		case 'OBJE':
211			$record = new Media($xref, $gedcom, $pending, $tree);
212			break;
213		case 'REPO':
214			$record = new Repository($xref, $gedcom, $pending, $tree);
215			break;
216		case 'NOTE':
217			$record = new Note($xref, $gedcom, $pending, $tree);
218			break;
219		default:
220			$record = new self($xref, $gedcom, $pending, $tree);
221			break;
222		}
223
224		// Store it in the cache
225		self::$gedcom_record_cache[$xref][$tree_id] = $record;
226
227		return $record;
228	}
229
230	/**
231	 * Fetch data from the database
232	 *
233	 * @param string $xref
234	 * @param int    $tree_id
235	 *
236	 * @return null|string
237	 */
238	protected static function fetchGedcomRecord($xref, $tree_id) {
239		// We don't know what type of object this is. Try each one in turn.
240		$data = Individual::fetchGedcomRecord($xref, $tree_id);
241		if ($data) {
242			return $data;
243		}
244		$data = Family::fetchGedcomRecord($xref, $tree_id);
245		if ($data) {
246			return $data;
247		}
248		$data = Source::fetchGedcomRecord($xref, $tree_id);
249		if ($data) {
250			return $data;
251		}
252		$data = Repository::fetchGedcomRecord($xref, $tree_id);
253		if ($data) {
254			return $data;
255		}
256		$data = Media::fetchGedcomRecord($xref, $tree_id);
257		if ($data) {
258			return $data;
259		}
260		$data = Note::fetchGedcomRecord($xref, $tree_id);
261		if ($data) {
262			return $data;
263		}
264		// Some other type of record...
265
266		return Database::prepare(
267			"SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id"
268		)->execute([
269			'xref'    => $xref,
270			'tree_id' => $tree_id,
271		])->fetchOne();
272	}
273
274	/**
275	 * Get the XREF for this record
276	 *
277	 * @return string
278	 */
279	public function getXref() {
280		return $this->xref;
281	}
282
283	/**
284	 * Get the tree to which this record belongs
285	 *
286	 * @return Tree
287	 */
288	public function getTree() {
289		return $this->tree;
290	}
291
292	/**
293	 * Application code should access data via Fact objects.
294	 * This function exists to support old code.
295	 *
296	 * @return string
297	 */
298	public function getGedcom() {
299		if ($this->pending === null) {
300			return $this->gedcom;
301		} else {
302			return $this->pending;
303		}
304	}
305
306	/**
307	 * Does this record have a pending change?
308	 *
309	 * @return bool
310	 */
311	public function isPendingAddtion() {
312		return $this->pending !== null;
313	}
314
315	/**
316	 * Does this record have a pending deletion?
317	 *
318	 * @return bool
319	 */
320	public function isPendingDeletion() {
321		return $this->pending === '';
322	}
323
324	/**
325	 * Generate a URL to this record, suitable for use in HTML, etc.
326	 *
327	 * @return string
328	 */
329	public function getHtmlUrl() {
330		return $this->getLinkUrl('&amp;');
331	}
332
333	/**
334	 * Generate a URL to this record, suitable for use in javascript, HTTP headers, etc.
335	 *
336	 * @return string
337	 */
338	public function getRawUrl() {
339		return $this->getLinkUrl('&');
340	}
341
342	/**
343	 * Generate a URL to this record.
344	 *
345	 * @param string $separator
346	 *
347	 * @return string
348	 */
349	private function getLinkUrl($separator) {
350		return static::URL_PREFIX . rawurlencode($this->getXref()) . $separator . 'ged=' . rawurlencode($this->tree->getName());
351	}
352
353	/**
354	 * Work out whether this record can be shown to a user with a given access level
355	 *
356	 * @param int $access_level
357	 *
358	 * @return bool
359	 */
360	private function canShowRecord($access_level) {
361		// This setting would better be called "$ENABLE_PRIVACY"
362		if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) {
363			return true;
364		}
365
366		// We should always be able to see our own record (unless an admin is applying download restrictions)
367		if ($this->getXref() === $this->tree->getUserPreference(Auth::user(), 'gedcomid') && $access_level === Auth::accessLevel($this->tree)) {
368			return true;
369		}
370
371		// Does this record have a RESN?
372		if (strpos($this->gedcom, "\n1 RESN confidential")) {
373			return Auth::PRIV_NONE >= $access_level;
374		}
375		if (strpos($this->gedcom, "\n1 RESN privacy")) {
376			return Auth::PRIV_USER >= $access_level;
377		}
378		if (strpos($this->gedcom, "\n1 RESN none")) {
379			return true;
380		}
381
382		// Does this record have a default RESN?
383		$individual_privacy = $this->tree->getIndividualPrivacy();
384		if (isset($individual_privacy[$this->getXref()])) {
385			return $individual_privacy[$this->getXref()] >= $access_level;
386		}
387
388		// Privacy rules do not apply to admins
389		if (Auth::PRIV_NONE >= $access_level) {
390			return true;
391		}
392
393		// Different types of record have different privacy rules
394		return $this->canShowByType($access_level);
395	}
396
397	/**
398	 * Each object type may have its own special rules, and re-implement this function.
399	 *
400	 * @param int $access_level
401	 *
402	 * @return bool
403	 */
404	protected function canShowByType($access_level) {
405		$fact_privacy = $this->tree->getFactPrivacy();
406
407		if (isset($fact_privacy[static::RECORD_TYPE])) {
408			// Restriction found
409			return $fact_privacy[static::RECORD_TYPE] >= $access_level;
410		} else {
411			// No restriction found - must be public:
412			return true;
413		}
414	}
415
416	/**
417	 * Can the details of this record be shown?
418	 *
419	 * @param int|null $access_level
420	 *
421	 * @return bool
422	 */
423	public function canShow($access_level = null) {
424		if ($access_level === null) {
425			$access_level = Auth::accessLevel($this->tree);
426		}
427
428// CACHING: this function can take three different parameters,
429		// and therefore needs three different caches for the result.
430		switch ($access_level) {
431		case Auth::PRIV_PRIVATE: // visitor
432			if ($this->disp_public === null) {
433				$this->disp_public = $this->canShowRecord(Auth::PRIV_PRIVATE);
434			}
435
436			return $this->disp_public;
437		case Auth::PRIV_USER: // member
438			if ($this->disp_user === null) {
439				$this->disp_user = $this->canShowRecord(Auth::PRIV_USER);
440			}
441
442			return $this->disp_user;
443		case Auth::PRIV_NONE: // admin
444			if ($this->disp_none === null) {
445				$this->disp_none = $this->canShowRecord(Auth::PRIV_NONE);
446			}
447
448			return $this->disp_none;
449		case Auth::PRIV_HIDE: // hidden from admins
450			// We use this value to bypass privacy checks. For example,
451			// when downloading data or when calculating privacy itself.
452			return true;
453		default:
454			// Should never get here.
455			return false;
456		}
457	}
458
459	/**
460	 * Can the name of this record be shown?
461	 *
462	 * @param int|null $access_level
463	 *
464	 * @return bool
465	 */
466	public function canShowName($access_level = null) {
467		if ($access_level === null) {
468			$access_level = Auth::accessLevel($this->tree);
469		}
470
471		return $this->canShow($access_level);
472	}
473
474	/**
475	 * Can we edit this record?
476	 *
477	 * @return bool
478	 */
479	public function canEdit() {
480		return Auth::isManager($this->tree) || Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false;
481	}
482
483	/**
484	 * Remove private data from the raw gedcom record.
485	 * Return both the visible and invisible data. We need the invisible data when editing.
486	 *
487	 * @param int $access_level
488	 *
489	 * @return string
490	 */
491	public function privatizeGedcom($access_level) {
492		if ($access_level == Auth::PRIV_HIDE) {
493			// We may need the original record, for example when downloading a GEDCOM or clippings cart
494			return $this->gedcom;
495		} elseif ($this->canShow($access_level)) {
496			// The record is not private, but the individual facts may be.
497
498			// Include the entire first line (for NOTE records)
499			list($gedrec) = explode("\n", $this->gedcom, 2);
500
501			// Check each of the facts for access
502			foreach ($this->getFacts(null, false, $access_level) as $fact) {
503				$gedrec .= "\n" . $fact->getGedcom();
504			}
505
506			return $gedrec;
507		} else {
508			// We cannot display the details, but we may be able to display
509			// limited data, such as links to other records.
510			return $this->createPrivateGedcomRecord($access_level);
511		}
512	}
513
514	/**
515	 * Generate a private version of this record
516	 *
517	 * @param int $access_level
518	 *
519	 * @return string
520	 */
521	protected function createPrivateGedcomRecord($access_level) {
522		return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private');
523	}
524
525	/**
526	 * Convert a name record into sortable and full/display versions. This default
527	 * should be OK for simple record types. INDI/FAM records will need to redefine it.
528	 *
529	 * @param string $type
530	 * @param string $value
531	 * @param string $gedcom
532	 */
533	protected function addName($type, $value, $gedcom) {
534		$this->_getAllNames[] = [
535			'type'   => $type,
536			'sort'   => preg_replace_callback('/([0-9]+)/', function ($matches) { return str_pad($matches[0], 10, '0', STR_PAD_LEFT); }, $value),
537			'full'   => '<span dir="auto">' . Html::escape($value) . '</span>', // This is used for display
538			'fullNN' => $value, // This goes into the database
539		];
540	}
541
542	/**
543	 * Get all the names of a record, including ROMN, FONE and _HEB alternatives.
544	 * Records without a name (e.g. FAM) will need to redefine this function.
545	 * Parameters: the level 1 fact containing the name.
546	 * Return value: an array of name structures, each containing
547	 * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc.
548	 * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown'
549	 * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John'
550	 *
551	 * @param int    $level
552	 * @param string $fact_type
553	 * @param Fact[] $facts
554	 */
555	protected function extractNamesFromFacts($level, $fact_type, $facts) {
556		$sublevel    = $level + 1;
557		$subsublevel = $sublevel + 1;
558		foreach ($facts as $fact) {
559			if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->getGedcom(), $matches, PREG_SET_ORDER)) {
560				foreach ($matches as $match) {
561					// Treat 1 NAME / 2 TYPE married the same as _MARNM
562					if ($match[1] == 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) {
563						$this->addName('_MARNM', $match[2], $fact->getGedcom());
564					} else {
565						$this->addName($match[1], $match[2], $fact->getGedcom());
566					}
567					if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) {
568						foreach ($submatches as $submatch) {
569							$this->addName($submatch[1], $submatch[2], $match[3]);
570						}
571					}
572				}
573			}
574		}
575	}
576
577	/**
578	 * Default for "other" object types
579	 */
580	public function extractNames() {
581		$this->addName(static::RECORD_TYPE, $this->getFallBackName(), null);
582	}
583
584	/**
585	 * Derived classes should redefine this function, otherwise the object will have no name
586	 *
587	 * @return string[][]
588	 */
589	public function getAllNames() {
590		if ($this->_getAllNames === null) {
591			$this->_getAllNames = [];
592			if ($this->canShowName()) {
593				// Ask the record to extract its names
594				$this->extractNames();
595				// No name found? Use a fallback.
596				if (!$this->_getAllNames) {
597					$this->addName(static::RECORD_TYPE, $this->getFallBackName(), null);
598				}
599			} else {
600				$this->addName(static::RECORD_TYPE, I18N::translate('Private'), null);
601			}
602		}
603
604		return $this->_getAllNames;
605	}
606
607	/**
608	 * If this object has no name, what do we call it?
609	 *
610	 * @return string
611	 */
612	public function getFallBackName() {
613		return Html::escape($this->getXref());
614	}
615
616	/**
617	 * Which of the (possibly several) names of this record is the primary one.
618	 *
619	 * @return int
620	 */
621	public function getPrimaryName() {
622		static $language_script;
623
624		if ($language_script === null) {
625			$language_script = I18N::languageScript(WT_LOCALE);
626		}
627
628		if ($this->_getPrimaryName === null) {
629			// Generally, the first name is the primary one....
630			$this->_getPrimaryName = 0;
631			// ...except when the language/name use different character sets
632			if (count($this->getAllNames()) > 1) {
633				foreach ($this->getAllNames() as $n => $name) {
634					if (I18N::textScript($name['sort']) === $language_script) {
635						$this->_getPrimaryName = $n;
636						break;
637					}
638				}
639			}
640		}
641
642		return $this->_getPrimaryName;
643	}
644
645	/**
646	 * Which of the (possibly several) names of this record is the secondary one.
647	 *
648	 * @return int
649	 */
650	public function getSecondaryName() {
651		if (is_null($this->_getSecondaryName)) {
652			// Generally, the primary and secondary names are the same
653			$this->_getSecondaryName = $this->getPrimaryName();
654			// ....except when there are names with different character sets
655			$all_names = $this->getAllNames();
656			if (count($all_names) > 1) {
657				$primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']);
658				foreach ($all_names as $n => $name) {
659					if ($n != $this->getPrimaryName() && $name['type'] != '_MARNM' && I18N::textScript($name['sort']) != $primary_script) {
660						$this->_getSecondaryName = $n;
661						break;
662					}
663				}
664			}
665		}
666
667		return $this->_getSecondaryName;
668	}
669
670	/**
671	 * Allow the choice of primary name to be overidden, e.g. in a search result
672	 *
673	 * @param int $n
674	 */
675	public function setPrimaryName($n) {
676		$this->_getPrimaryName   = $n;
677		$this->_getSecondaryName = null;
678	}
679
680	/**
681	 * Allow native PHP functions such as array_unique() to work with objects
682	 *
683	 * @return string
684	 */
685	public function __toString() {
686		return $this->xref . '@' . $this->tree->getTreeId();
687	}
688
689	/**
690	 * Static helper function to sort an array of objects by name
691	 * Records whose names cannot be displayed are sorted at the end.
692	 *
693	 * @param GedcomRecord $x
694	 * @param GedcomRecord $y
695	 *
696	 * @return int
697	 */
698	public static function compare(GedcomRecord $x, GedcomRecord $y) {
699		if ($x->canShowName()) {
700			if ($y->canShowName()) {
701				return I18N::strcasecmp($x->getSortName(), $y->getSortName());
702			} else {
703				return -1; // only $y is private
704			}
705		} else {
706			if ($y->canShowName()) {
707				return 1; // only $x is private
708			} else {
709				return 0; // both $x and $y private
710			}
711		}
712	}
713
714	/**
715	 * Get variants of the name
716	 *
717	 * @return string
718	 */
719	public function getFullName() {
720		if ($this->canShowName()) {
721			$tmp = $this->getAllNames();
722
723			return $tmp[$this->getPrimaryName()]['full'];
724		} else {
725			return I18N::translate('Private');
726		}
727	}
728
729	/**
730	 * Get a sortable version of the name. Do not display this!
731	 *
732	 * @return string
733	 */
734	public function getSortName() {
735		// The sortable name is never displayed, no need to call canShowName()
736		$tmp = $this->getAllNames();
737
738		return $tmp[$this->getPrimaryName()]['sort'];
739	}
740
741	/**
742	 * Get the full name in an alternative character set
743	 *
744	 * @return null|string
745	 */
746	public function getAddName() {
747		if ($this->canShowName() && $this->getPrimaryName() != $this->getSecondaryName()) {
748			$all_names = $this->getAllNames();
749
750			return $all_names[$this->getSecondaryName()]['full'];
751		} else {
752			return null;
753		}
754	}
755
756	/**
757	 * Format this object for display in a list
758	 * If $find is set, then we are displaying items from a selection list.
759	 * $name allows us to use something other than the record name.
760	 *
761	 * @param string $tag
762	 * @param bool   $find
763	 * @param null   $name
764	 *
765	 * @return string
766	 */
767	public function formatList($tag = 'li', $find = false, $name = null) {
768		if (is_null($name)) {
769			$name = $this->getFullName();
770		}
771		$html = '<a href="' . $this->getHtmlUrl() . '"';
772		if ($find) {
773			$html .= ' onclick="pasteid(\'' . $this->getXref() . '\', \'' . htmlentities($name) . '\');"';
774		}
775		$html .= ' class="list_item"><b>' . $name . '</b>';
776		$html .= $this->formatListDetails();
777		$html = '<' . $tag . '>' . $html . '</a></' . $tag . '>';
778
779		return $html;
780	}
781
782	/**
783	 * This function should be redefined in derived classes to show any major
784	 * identifying characteristics of this record.
785	 *
786	 * @return string
787	 */
788	public function formatListDetails() {
789		return '';
790	}
791
792	/**
793	 * Extract/format the first fact from a list of facts.
794	 *
795	 * @param string $facts
796	 * @param int    $style
797	 *
798	 * @return string
799	 */
800	public function formatFirstMajorFact($facts, $style) {
801		foreach ($this->getFacts($facts, true) as $event) {
802			// Only display if it has a date or place (or both)
803			if ($event->getDate()->isOK() && !$event->getPlace()->isEmpty()) {
804				$joiner = ' — ';
805			} else {
806				$joiner = '';
807			}
808			if ($event->getDate()->isOK() || !$event->getPlace()->isEmpty()) {
809				switch ($style) {
810				case 1:
811					return '<br><em>' . $event->getLabel() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>';
812				case 2:
813					return '<dl><dt class="label">' . $event->getLabel() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>';
814				}
815			}
816		}
817
818		return '';
819	}
820
821	/**
822	 * Find individuals linked to this record.
823	 *
824	 * @param string $link
825	 *
826	 * @return Individual[]
827	 */
828	public function linkedIndividuals($link) {
829		$rows = Database::prepare(
830			"SELECT i_id AS xref, i_gedcom AS gedcom" .
831			" FROM `##individuals`" .
832			" JOIN `##link` ON i_file = l_file AND i_id = l_from" .
833			" LEFT JOIN `##name` ON i_file = n_file AND i_id = n_id AND n_num = 0" .
834			" WHERE i_file = :tree_id AND l_type = :link AND l_to = :xref" .
835			" ORDER BY n_sort COLLATE :collation"
836		)->execute([
837			'tree_id'   => $this->tree->getTreeId(),
838			'link'      => $link,
839			'xref'      => $this->xref,
840			'collation' => I18N::collation(),
841		])->fetchAll();
842
843		$list = [];
844		foreach ($rows as $row) {
845			$record = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
846			if ($record->canShowName()) {
847				$list[] = $record;
848			}
849		}
850
851		return $list;
852	}
853
854	/**
855	 * Find families linked to this record.
856	 *
857	 * @param string $link
858	 *
859	 * @return Family[]
860	 */
861	public function linkedFamilies($link) {
862		$rows = Database::prepare(
863			"SELECT f_id AS xref, f_gedcom AS gedcom" .
864			" FROM `##families`" .
865			" JOIN `##link` ON f_file = l_file AND f_id = l_from" .
866			" LEFT JOIN `##name` ON f_file = n_file AND f_id = n_id AND n_num = 0" .
867			" WHERE f_file = :tree_id AND l_type = :link AND l_to = :xref"
868		)->execute([
869			'tree_id' => $this->tree->getTreeId(),
870			'link'    => $link,
871			'xref'    => $this->xref,
872		])->fetchAll();
873
874		$list = [];
875		foreach ($rows as $row) {
876			$record = Family::getInstance($row->xref, $this->tree, $row->gedcom);
877			if ($record->canShowName()) {
878				$list[] = $record;
879			}
880		}
881
882		return $list;
883	}
884
885	/**
886	 * Find sources linked to this record.
887	 *
888	 * @param string $link
889	 *
890	 * @return Source[]
891	 */
892	public function linkedSources($link) {
893		$rows = Database::prepare(
894			"SELECT s_id AS xref, s_gedcom AS gedcom" .
895			" FROM `##sources`" .
896			" JOIN `##link` ON s_file = l_file AND s_id = l_from" .
897			" WHERE s_file = :tree_id AND l_type = :link AND l_to = :xref" .
898			" ORDER BY s_name COLLATE :collation"
899		)->execute([
900			'tree_id'   => $this->tree->getTreeId(),
901			'link'      => $link,
902			'xref'      => $this->xref,
903			'collation' => I18N::collation(),
904		])->fetchAll();
905
906		$list = [];
907		foreach ($rows as $row) {
908			$record = Source::getInstance($row->xref, $this->tree, $row->gedcom);
909			if ($record->canShowName()) {
910				$list[] = $record;
911			}
912		}
913
914		return $list;
915	}
916
917	/**
918	 * Find media objects linked to this record.
919	 *
920	 * @param string $link
921	 *
922	 * @return Media[]
923	 */
924	public function linkedMedia($link) {
925		$rows = Database::prepare(
926			"SELECT m_id AS xref, m_gedcom AS gedcom" .
927			" FROM `##media`" .
928			" JOIN `##link` ON m_file = l_file AND m_id = l_from" .
929			" WHERE m_file = :tree_id AND l_type = :link AND l_to = :xref" .
930			" ORDER BY m_titl COLLATE :collation"
931		)->execute([
932			'tree_id'   => $this->tree->getTreeId(),
933			'link'      => $link,
934			'xref'      => $this->xref,
935			'collation' => I18N::collation(),
936		])->fetchAll();
937
938		$list = [];
939		foreach ($rows as $row) {
940			$record = Media::getInstance($row->xref, $this->tree, $row->gedcom);
941			if ($record->canShowName()) {
942				$list[] = $record;
943			}
944		}
945
946		return $list;
947	}
948
949	/**
950	 * Find notes linked to this record.
951	 *
952	 * @param string $link
953	 *
954	 * @return Note[]
955	 */
956	public function linkedNotes($link) {
957		$rows = Database::prepare(
958			"SELECT o_id AS xref, o_gedcom AS gedcom" .
959			" FROM `##other`" .
960			" JOIN `##link` ON o_file = l_file AND o_id = l_from" .
961			" LEFT JOIN `##name` ON o_file = n_file AND o_id = n_id AND n_num = 0" .
962			" WHERE o_file = :tree_id AND o_type = 'NOTE' AND l_type = :link AND l_to = :xref" .
963			" ORDER BY n_sort COLLATE :collation"
964		)->execute([
965			'tree_id'   => $this->tree->getTreeId(),
966			'link'      => $link,
967			'xref'      => $this->xref,
968			'collation' => I18N::collation(),
969		])->fetchAll();
970
971		$list = [];
972		foreach ($rows as $row) {
973			$record = Note::getInstance($row->xref, $this->tree, $row->gedcom);
974			if ($record->canShowName()) {
975				$list[] = $record;
976			}
977		}
978
979		return $list;
980	}
981
982	/**
983	 * Find repositories linked to this record.
984	 *
985	 * @param string $link
986	 *
987	 * @return Repository[]
988	 */
989	public function linkedRepositories($link) {
990		$rows = Database::prepare(
991			"SELECT o_id AS xref, o_gedcom AS gedcom" .
992			" FROM `##other`" .
993			" JOIN `##link` ON o_file = l_file AND o_id = l_from" .
994			" LEFT JOIN `##name` ON o_file = n_file AND o_id = n_id AND n_num = 0" .
995			" WHERE o_file = :tree_id AND o_type = 'REPO' AND l_type = :link AND l_to = :xref" .
996			" ORDER BY n_sort COLLATE :collation"
997		)->execute([
998			'tree_id'   => $this->tree->getTreeId(),
999			'link'      => $link,
1000			'xref'      => $this->xref,
1001			'collation' => I18N::collation(),
1002		])->fetchAll();
1003
1004		$list = [];
1005		foreach ($rows as $row) {
1006			$record = Repository::getInstance($row->xref, $this->tree, $row->gedcom);
1007			if ($record->canShowName()) {
1008				$list[] = $record;
1009			}
1010		}
1011
1012		return $list;
1013	}
1014
1015	/**
1016	 * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR).
1017	 * This is used to display multiple events on the individual/family lists.
1018	 * Multiple events can exist because of uncertainty in dates, dates in different
1019	 * calendars, place-names in both latin and hebrew character sets, etc.
1020	 * It also allows us to combine dates/places from different events in the summaries.
1021	 *
1022	 * @param string $event_type
1023	 *
1024	 * @return Date[]
1025	 */
1026	public function getAllEventDates($event_type) {
1027		$dates = [];
1028		foreach ($this->getFacts($event_type) as $event) {
1029			if ($event->getDate()->isOK()) {
1030				$dates[] = $event->getDate();
1031			}
1032		}
1033
1034		return $dates;
1035	}
1036
1037	/**
1038	 * Get all the places for a particular type of event
1039	 *
1040	 * @param string $event_type
1041	 *
1042	 * @return array
1043	 */
1044	public function getAllEventPlaces($event_type) {
1045		$places = [];
1046		foreach ($this->getFacts($event_type) as $event) {
1047			if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->getGedcom(), $ged_places)) {
1048				foreach ($ged_places[1] as $ged_place) {
1049					$places[] = $ged_place;
1050				}
1051			}
1052		}
1053
1054		return $places;
1055	}
1056
1057	/**
1058	 * Get the first (i.e. prefered) Fact for the given fact type
1059	 *
1060	 * @param string $tag
1061	 *
1062	 * @return Fact|null
1063	 */
1064	public function getFirstFact($tag) {
1065		foreach ($this->getFacts() as $fact) {
1066			if ($fact->getTag() === $tag) {
1067				return $fact;
1068			}
1069		}
1070
1071		return null;
1072	}
1073
1074	/**
1075	 * The facts and events for this record.
1076	 *
1077	 * @param string    $filter
1078	 * @param bool      $sort
1079	 * @param int|null  $access_level
1080	 * @param bool      $override     Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES.
1081	 *
1082	 * @return Fact[]
1083	 */
1084	public function getFacts($filter = null, $sort = false, $access_level = null, $override = false) {
1085		if ($access_level === null) {
1086			$access_level = Auth::accessLevel($this->tree);
1087		}
1088
1089			$facts = [];
1090		if ($this->canShow($access_level) || $override) {
1091			foreach ($this->facts as $fact) {
1092				if (($filter === null || preg_match('/^' . $filter . '$/', $fact->getTag())) && $fact->canShow($access_level)) {
1093					$facts[] = $fact;
1094				}
1095			}
1096		}
1097		if ($sort) {
1098			Functions::sortFacts($facts);
1099		}
1100
1101		return $facts;
1102	}
1103
1104	/**
1105	 * Get the last-change timestamp for this record, either as a formatted string
1106	 * (for display) or as a unix timestamp (for sorting)
1107	 *
1108	 * @param bool $sorting
1109	 *
1110	 * @return string
1111	 */
1112	public function lastChangeTimestamp($sorting = false) {
1113		$chan = $this->getFirstFact('CHAN');
1114
1115		if ($chan) {
1116			// The record does have a CHAN event
1117			$d = $chan->getDate()->minimumDate();
1118			if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->getGedcom(), $match)) {
1119				$t = mktime((int) $match[1], (int) $match[2], (int) $match[3], (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y'));
1120			} elseif (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->getGedcom(), $match)) {
1121				$t = mktime((int) $match[1], (int) $match[2], 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y'));
1122			} else {
1123				$t = mktime(0, 0, 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y'));
1124			}
1125			if ($sorting) {
1126				return $t;
1127			} else {
1128				return strip_tags(FunctionsDate::formatTimestamp($t));
1129			}
1130		} else {
1131			// The record does not have a CHAN event
1132			if ($sorting) {
1133				return '0';
1134			} else {
1135				return '';
1136			}
1137		}
1138	}
1139
1140	/**
1141	 * Get the last-change user for this record
1142	 *
1143	 * @return string
1144	 */
1145	public function lastChangeUser() {
1146		$chan = $this->getFirstFact('CHAN');
1147
1148		if ($chan === null) {
1149			return I18N::translate('Unknown');
1150		} else {
1151			$chan_user = $chan->getAttribute('_WT_USER');
1152			if ($chan_user === '') {
1153				return I18N::translate('Unknown');
1154			} else {
1155				return $chan_user;
1156			}
1157		}
1158	}
1159
1160	/**
1161	 * Add a new fact to this record
1162	 *
1163	 * @param string $gedcom
1164	 * @param bool   $update_chan
1165	 */
1166	public function createFact($gedcom, $update_chan) {
1167		$this->updateFact(null, $gedcom, $update_chan);
1168	}
1169
1170	/**
1171	 * Delete a fact from this record
1172	 *
1173	 * @param string $fact_id
1174	 * @param bool   $update_chan
1175	 */
1176	public function deleteFact($fact_id, $update_chan) {
1177		$this->updateFact($fact_id, null, $update_chan);
1178	}
1179
1180	/**
1181	 * Replace a fact with a new gedcom data.
1182	 *
1183	 * @param string $fact_id
1184	 * @param string $gedcom
1185	 * @param bool   $update_chan
1186	 *
1187	 * @throws \Exception
1188	 */
1189	public function updateFact($fact_id, $gedcom, $update_chan) {
1190		// MSDOS line endings will break things in horrible ways
1191		$gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
1192		$gedcom = trim($gedcom);
1193
1194		if ($this->pending === '') {
1195			throw new \Exception('Cannot edit a deleted record');
1196		}
1197		if ($gedcom && !preg_match('/^1 ' . WT_REGEX_TAG . '/', $gedcom)) {
1198			throw new \Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')');
1199		}
1200
1201		if ($this->pending) {
1202			$old_gedcom = $this->pending;
1203		} else {
1204			$old_gedcom = $this->gedcom;
1205		}
1206
1207		// First line of record may contain data - e.g. NOTE records.
1208		list($new_gedcom) = explode("\n", $old_gedcom, 2);
1209
1210		// Replacing (or deleting) an existing fact
1211		foreach ($this->getFacts(null, false, Auth::PRIV_HIDE) as $fact) {
1212			if (!$fact->isPendingDeletion()) {
1213				if ($fact->getFactId() === $fact_id) {
1214					if ($gedcom) {
1215						$new_gedcom .= "\n" . $gedcom;
1216					}
1217					$fact_id = true; // Only replace/delete one copy of a duplicate fact
1218				} elseif ($fact->getTag() != 'CHAN' || !$update_chan) {
1219					$new_gedcom .= "\n" . $fact->getGedcom();
1220				}
1221			}
1222		}
1223		if ($update_chan) {
1224			$new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
1225		}
1226
1227		// Adding a new fact
1228		if (!$fact_id) {
1229			$new_gedcom .= "\n" . $gedcom;
1230		}
1231
1232		if ($new_gedcom != $old_gedcom) {
1233			// Save the changes
1234			Database::prepare(
1235				"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, ?, ?)"
1236			)->execute([
1237				$this->tree->getTreeId(),
1238				$this->xref,
1239				$old_gedcom,
1240				$new_gedcom,
1241				Auth::id(),
1242			]);
1243
1244			$this->pending = $new_gedcom;
1245
1246			if (Auth::user()->getPreference('auto_accept')) {
1247				FunctionsImport::acceptAllChanges($this->xref, $this->tree->getTreeId());
1248				$this->gedcom  = $new_gedcom;
1249				$this->pending = null;
1250			}
1251		}
1252		$this->parseFacts();
1253	}
1254
1255	/**
1256	 * Update this record
1257	 *
1258	 * @param string $gedcom
1259	 * @param bool   $update_chan
1260	 */
1261	public function updateRecord($gedcom, $update_chan) {
1262		// MSDOS line endings will break things in horrible ways
1263		$gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
1264		$gedcom = trim($gedcom);
1265
1266		// Update the CHAN record
1267		if ($update_chan) {
1268			$gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom);
1269			$gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
1270		}
1271
1272		// Create a pending change
1273		Database::prepare(
1274			"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, ?, ?)"
1275		)->execute([
1276			$this->tree->getTreeId(),
1277			$this->xref,
1278			$this->getGedcom(),
1279			$gedcom,
1280			Auth::id(),
1281		]);
1282
1283		// Clear the cache
1284		$this->pending = $gedcom;
1285
1286		// Accept this pending change
1287		if (Auth::user()->getPreference('auto_accept')) {
1288			FunctionsImport::acceptAllChanges($this->xref, $this->tree->getTreeId());
1289			$this->gedcom  = $gedcom;
1290			$this->pending = null;
1291		}
1292
1293		$this->parseFacts();
1294
1295		Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref);
1296	}
1297
1298	/**
1299	 * Delete this record
1300	 */
1301	public function deleteRecord() {
1302		// Create a pending change
1303		if (!$this->isPendingDeletion()) {
1304			Database::prepare(
1305				"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, '', ?)"
1306			)->execute([
1307				$this->tree->getTreeId(),
1308				$this->xref,
1309				$this->getGedcom(),
1310				Auth::id(),
1311			]);
1312		}
1313
1314		// Auto-accept this pending change
1315		if (Auth::user()->getPreference('auto_accept')) {
1316			FunctionsImport::acceptAllChanges($this->xref, $this->tree->getTreeId());
1317		}
1318
1319		// Clear the cache
1320		self::$gedcom_record_cache  = null;
1321		self::$pending_record_cache = null;
1322
1323		Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref);
1324	}
1325
1326	/**
1327	 * Remove all links from this record to $xref
1328	 *
1329	 * @param string $xref
1330	 * @param bool   $update_chan
1331	 */
1332	public function removeLinks($xref, $update_chan) {
1333		$value = '@' . $xref . '@';
1334
1335		foreach ($this->getFacts() as $fact) {
1336			if ($fact->getValue() === $value) {
1337				$this->deleteFact($fact->getFactId(), $update_chan);
1338			} elseif (preg_match_all('/\n(\d) ' . WT_REGEX_TAG . ' ' . $value . '/', $fact->getGedcom(), $matches, PREG_SET_ORDER)) {
1339				$gedcom = $fact->getGedcom();
1340				foreach ($matches as $match) {
1341					$next_level  = $match[1] + 1;
1342					$next_levels = '[' . $next_level . '-9]';
1343					$gedcom      = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom);
1344				}
1345				$this->updateFact($fact->getFactId(), $gedcom, $update_chan);
1346			}
1347		}
1348	}
1349}
1350