xref: /webtrees/app/GedcomRecord.php (revision d53324c994737e50e57b9e0bc691b4f742b400b2)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
1776692c8bSGreg Roach
183d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
193d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
22a25f0a04SGreg Roach
23a25f0a04SGreg Roach/**
2476692c8bSGreg Roach * A GEDCOM object.
25a25f0a04SGreg Roach */
26a25f0a04SGreg Roachclass GedcomRecord {
27a25f0a04SGreg Roach	const RECORD_TYPE = 'UNKNOWN';
28a25f0a04SGreg Roach	const URL_PREFIX  = 'gedrecord.php?pid=';
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach	/** @var string The record identifier */
31a25f0a04SGreg Roach	protected $xref;
32a25f0a04SGreg Roach
33000959d9SGreg Roach	/** @var Tree  The family tree to which this record belongs */
34000959d9SGreg Roach	protected $tree;
35a25f0a04SGreg Roach
36a25f0a04SGreg Roach	/** @var string  GEDCOM data (before any pending edits) */
37a25f0a04SGreg Roach	protected $gedcom;
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach	/** @var string|null  GEDCOM data (after any pending edits) */
40a25f0a04SGreg Roach	protected $pending;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach	/** @var Fact[] facts extracted from $gedcom/$pending */
43a25f0a04SGreg Roach	protected $facts;
44a25f0a04SGreg Roach
45cbc1590aSGreg Roach	/** @var bool Can we display details of this record to Auth::PRIV_PRIVATE */
46a25f0a04SGreg Roach	private $disp_public;
47a25f0a04SGreg Roach
48cbc1590aSGreg Roach	/** @var bool Can we display details of this record to Auth::PRIV_USER */
49a25f0a04SGreg Roach	private $disp_user;
50a25f0a04SGreg Roach
51cbc1590aSGreg Roach	/** @var bool Can we display details of this record to Auth::PRIV_NONE */
52a25f0a04SGreg Roach	private $disp_none;
53a25f0a04SGreg Roach
54a25f0a04SGreg Roach	/** @var string[][] All the names of this individual */
55a25f0a04SGreg Roach	protected $_getAllNames;
56a25f0a04SGreg Roach
57cbc1590aSGreg Roach	/** @var int Cached result */
58a25f0a04SGreg Roach	protected $_getPrimaryName;
59a25f0a04SGreg Roach
60cbc1590aSGreg Roach	/** @var int Cached result */
61a25f0a04SGreg Roach	protected $_getSecondaryName;
62a25f0a04SGreg Roach
6376692c8bSGreg Roach	/** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */
64395f0fe0SGreg Roach	protected static $gedcom_record_cache;
651ae87ce5SGreg Roach
6676692c8bSGreg Roach	/** @var \stdClass[][] Fetch all pending edits in one database query */
67a25f0a04SGreg Roach	private static $pending_record_cache;
68a25f0a04SGreg Roach
69a25f0a04SGreg Roach	/**
70a25f0a04SGreg Roach	 * Create a GedcomRecord object from raw GEDCOM data.
71a25f0a04SGreg Roach	 *
72a25f0a04SGreg Roach	 * @param string      $xref
73a25f0a04SGreg Roach	 * @param string      $gedcom  an empty string for new/pending records
74a25f0a04SGreg Roach	 * @param string|null $pending null for a record with no pending edits,
75a25f0a04SGreg Roach	 *                             empty string for records with pending deletions
7624ec66ceSGreg Roach	 * @param Tree        $tree
77a25f0a04SGreg Roach	 */
7824ec66ceSGreg Roach	public function __construct($xref, $gedcom, $pending, $tree) {
79a25f0a04SGreg Roach		$this->xref    = $xref;
80a25f0a04SGreg Roach		$this->gedcom  = $gedcom;
81a25f0a04SGreg Roach		$this->pending = $pending;
8224ec66ceSGreg Roach		$this->tree    = $tree;
83a25f0a04SGreg Roach
84a25f0a04SGreg Roach		$this->parseFacts();
85a25f0a04SGreg Roach	}
86a25f0a04SGreg Roach
87a25f0a04SGreg Roach	/**
88a25f0a04SGreg Roach	 * Split the record into facts
89a25f0a04SGreg Roach	 */
90a25f0a04SGreg Roach	private function parseFacts() {
91a25f0a04SGreg Roach		// Split the record into facts
92a25f0a04SGreg Roach		if ($this->gedcom) {
93a25f0a04SGreg Roach			$gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom);
94a25f0a04SGreg Roach			array_shift($gedcom_facts);
95a25f0a04SGreg Roach		} else {
9613abd6f3SGreg Roach			$gedcom_facts = [];
97a25f0a04SGreg Roach		}
98a25f0a04SGreg Roach		if ($this->pending) {
99a25f0a04SGreg Roach			$pending_facts = preg_split('/\n(?=1)/s', $this->pending);
100a25f0a04SGreg Roach			array_shift($pending_facts);
101a25f0a04SGreg Roach		} else {
10213abd6f3SGreg Roach			$pending_facts = [];
103a25f0a04SGreg Roach		}
104a25f0a04SGreg Roach
10513abd6f3SGreg Roach		$this->facts = [];
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach		foreach ($gedcom_facts as $gedcom_fact) {
108a25f0a04SGreg Roach			$fact = new Fact($gedcom_fact, $this, md5($gedcom_fact));
109a25f0a04SGreg Roach			if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts)) {
110a25f0a04SGreg Roach				$fact->setPendingDeletion();
111a25f0a04SGreg Roach			}
112a25f0a04SGreg Roach			$this->facts[] = $fact;
113a25f0a04SGreg Roach		}
114a25f0a04SGreg Roach		foreach ($pending_facts as $pending_fact) {
115a25f0a04SGreg Roach			if (!in_array($pending_fact, $gedcom_facts)) {
116a25f0a04SGreg Roach				$fact = new Fact($pending_fact, $this, md5($pending_fact));
117a25f0a04SGreg Roach				$fact->setPendingAddition();
118a25f0a04SGreg Roach				$this->facts[] = $fact;
119a25f0a04SGreg Roach			}
120a25f0a04SGreg Roach		}
121a25f0a04SGreg Roach	}
122a25f0a04SGreg Roach
123a25f0a04SGreg Roach	/**
124a25f0a04SGreg Roach	 * Get an instance of a GedcomRecord object. For single records,
125a25f0a04SGreg Roach	 * we just receive the XREF. For bulk records (such as lists
126a25f0a04SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
127a25f0a04SGreg Roach	 *
128a25f0a04SGreg Roach	 * @param string      $xref
12924ec66ceSGreg Roach	 * @param Tree        $tree
130a25f0a04SGreg Roach	 * @param string|null $gedcom
131a25f0a04SGreg Roach	 *
132a25f0a04SGreg Roach	 * @throws \Exception
133cbc1590aSGreg Roach	 *
13484658595SGreg Roach	 * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null
135a25f0a04SGreg Roach	 */
13624ec66ceSGreg Roach	public static function getInstance($xref, Tree $tree, $gedcom = null) {
13724ec66ceSGreg Roach		$tree_id = $tree->getTreeId();
13824ec66ceSGreg Roach
13906ef8e02SGreg Roach		// Is this record already in the cache, and of the correct type?
14024ec66ceSGreg Roach		if (isset(self::$gedcom_record_cache[$xref][$tree_id])) {
14106ef8e02SGreg Roach			$record = self::$gedcom_record_cache[$xref][$tree_id];
14206ef8e02SGreg Roach
14306ef8e02SGreg Roach			if ($record instanceof static) {
14406ef8e02SGreg Roach				return $record;
14506ef8e02SGreg Roach			} else {
14606ef8e02SGreg Roach				null;
14706ef8e02SGreg Roach			}
148a25f0a04SGreg Roach		}
149a25f0a04SGreg Roach
150a25f0a04SGreg Roach		// Do we need to fetch the record from the database?
151a25f0a04SGreg Roach		if ($gedcom === null) {
15224ec66ceSGreg Roach			$gedcom = static::fetchGedcomRecord($xref, $tree_id);
153a25f0a04SGreg Roach		}
154a25f0a04SGreg Roach
155a25f0a04SGreg Roach		// If we can edit, then we also need to be able to see pending records.
15694075df0SGreg Roach		if (Auth::isEditor($tree)) {
15724ec66ceSGreg Roach			if (!isset(self::$pending_record_cache[$tree_id])) {
158a25f0a04SGreg Roach				// Fetch all pending records in one database query
15913abd6f3SGreg Roach				self::$pending_record_cache[$tree_id] = [];
160a25f0a04SGreg Roach				$rows                                 = Database::prepare(
16194075df0SGreg Roach					"SELECT xref, new_gedcom FROM `##change` WHERE status = 'pending' AND gedcom_id = :tree_id ORDER BY change_id"
16213abd6f3SGreg Roach				)->execute([
163cbc1590aSGreg Roach					'tree_id' => $tree_id,
16413abd6f3SGreg Roach				])->fetchAll();
165a25f0a04SGreg Roach				foreach ($rows as $row) {
16624ec66ceSGreg Roach					self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom;
167a25f0a04SGreg Roach				}
168a25f0a04SGreg Roach			}
169a25f0a04SGreg Roach
17024ec66ceSGreg Roach			if (isset(self::$pending_record_cache[$tree_id][$xref])) {
171a25f0a04SGreg Roach				// A pending edit exists for this record
17224ec66ceSGreg Roach				$pending = self::$pending_record_cache[$tree_id][$xref];
173a25f0a04SGreg Roach			} else {
174a25f0a04SGreg Roach				$pending = null;
175a25f0a04SGreg Roach			}
176a25f0a04SGreg Roach		} else {
177a25f0a04SGreg Roach			// There are no pending changes for this record
178a25f0a04SGreg Roach			$pending = null;
179a25f0a04SGreg Roach		}
180a25f0a04SGreg Roach
181a25f0a04SGreg Roach		// No such record exists
182a25f0a04SGreg Roach		if ($gedcom === null && $pending === null) {
183a25f0a04SGreg Roach			return null;
184a25f0a04SGreg Roach		}
185a25f0a04SGreg Roach
186a25f0a04SGreg Roach		// Create the object
187a25f0a04SGreg Roach		if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom . $pending, $match)) {
188a25f0a04SGreg Roach			$xref = $match[1]; // Collation - we may have requested I123 and found i123
189a25f0a04SGreg Roach			$type = $match[2];
190a25f0a04SGreg Roach		} elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) {
191a25f0a04SGreg Roach			$xref = $match[1];
192a25f0a04SGreg Roach			$type = $match[1];
193a25f0a04SGreg Roach		} elseif ($gedcom . $pending) {
194a25f0a04SGreg Roach			throw new \Exception('Unrecognized GEDCOM record: ' . $gedcom);
195a25f0a04SGreg Roach		} else {
196a25f0a04SGreg Roach			// A record with both pending creation and pending deletion
197a25f0a04SGreg Roach			$type = static::RECORD_TYPE;
198a25f0a04SGreg Roach		}
199a25f0a04SGreg Roach
200a25f0a04SGreg Roach		switch ($type) {
201a25f0a04SGreg Roach			case 'INDI':
20224ec66ceSGreg Roach				$record = new Individual($xref, $gedcom, $pending, $tree);
203a25f0a04SGreg Roach				break;
204a25f0a04SGreg Roach			case 'FAM':
20524ec66ceSGreg Roach				$record = new Family($xref, $gedcom, $pending, $tree);
206a25f0a04SGreg Roach				break;
207a25f0a04SGreg Roach			case 'SOUR':
20824ec66ceSGreg Roach				$record = new Source($xref, $gedcom, $pending, $tree);
209a25f0a04SGreg Roach				break;
210a25f0a04SGreg Roach			case 'OBJE':
21124ec66ceSGreg Roach				$record = new Media($xref, $gedcom, $pending, $tree);
212a25f0a04SGreg Roach				break;
213a25f0a04SGreg Roach			case 'REPO':
21424ec66ceSGreg Roach				$record = new Repository($xref, $gedcom, $pending, $tree);
215a25f0a04SGreg Roach				break;
216a25f0a04SGreg Roach			case 'NOTE':
21724ec66ceSGreg Roach				$record = new Note($xref, $gedcom, $pending, $tree);
218a25f0a04SGreg Roach				break;
219a25f0a04SGreg Roach			default:
22006ef8e02SGreg Roach				$record = new self($xref, $gedcom, $pending, $tree);
221a25f0a04SGreg Roach				break;
222a25f0a04SGreg Roach		}
223a25f0a04SGreg Roach
224a25f0a04SGreg Roach		// Store it in the cache
22524ec66ceSGreg Roach		self::$gedcom_record_cache[$xref][$tree_id] = $record;
226a25f0a04SGreg Roach
227a25f0a04SGreg Roach		return $record;
228a25f0a04SGreg Roach	}
229a25f0a04SGreg Roach
230a25f0a04SGreg Roach	/**
231a25f0a04SGreg Roach	 * Fetch data from the database
232a25f0a04SGreg Roach	 *
233a25f0a04SGreg Roach	 * @param string $xref
234cbc1590aSGreg Roach	 * @param int    $tree_id
235a25f0a04SGreg Roach	 *
236a25f0a04SGreg Roach	 * @return null|string
237a25f0a04SGreg Roach	 */
23864d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
239a25f0a04SGreg Roach		// We don't know what type of object this is. Try each one in turn.
24064d9078aSGreg Roach		$data = Individual::fetchGedcomRecord($xref, $tree_id);
241a25f0a04SGreg Roach		if ($data) {
242a25f0a04SGreg Roach			return $data;
243a25f0a04SGreg Roach		}
24464d9078aSGreg Roach		$data = Family::fetchGedcomRecord($xref, $tree_id);
245a25f0a04SGreg Roach		if ($data) {
246a25f0a04SGreg Roach			return $data;
247a25f0a04SGreg Roach		}
24864d9078aSGreg Roach		$data = Source::fetchGedcomRecord($xref, $tree_id);
249a25f0a04SGreg Roach		if ($data) {
250a25f0a04SGreg Roach			return $data;
251a25f0a04SGreg Roach		}
25264d9078aSGreg Roach		$data = Repository::fetchGedcomRecord($xref, $tree_id);
253a25f0a04SGreg Roach		if ($data) {
254a25f0a04SGreg Roach			return $data;
255a25f0a04SGreg Roach		}
25664d9078aSGreg Roach		$data = Media::fetchGedcomRecord($xref, $tree_id);
257a25f0a04SGreg Roach		if ($data) {
258a25f0a04SGreg Roach			return $data;
259a25f0a04SGreg Roach		}
26064d9078aSGreg Roach		$data = Note::fetchGedcomRecord($xref, $tree_id);
261a25f0a04SGreg Roach		if ($data) {
262a25f0a04SGreg Roach			return $data;
263a25f0a04SGreg Roach		}
264a25f0a04SGreg Roach		// Some other type of record...
265a25f0a04SGreg Roach
26664d9078aSGreg Roach		return Database::prepare(
26764d9078aSGreg Roach			"SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id"
26813abd6f3SGreg Roach		)->execute([
26964d9078aSGreg Roach			'xref'    => $xref,
27064d9078aSGreg Roach			'tree_id' => $tree_id,
27113abd6f3SGreg Roach		])->fetchOne();
272a25f0a04SGreg Roach	}
273a25f0a04SGreg Roach
274a25f0a04SGreg Roach	/**
275a25f0a04SGreg Roach	 * Get the XREF for this record
276a25f0a04SGreg Roach	 *
277a25f0a04SGreg Roach	 * @return string
278a25f0a04SGreg Roach	 */
279a25f0a04SGreg Roach	public function getXref() {
280a25f0a04SGreg Roach		return $this->xref;
281a25f0a04SGreg Roach	}
282a25f0a04SGreg Roach
283a25f0a04SGreg Roach	/**
284000959d9SGreg Roach	 * Get the tree to which this record belongs
285000959d9SGreg Roach	 *
286000959d9SGreg Roach	 * @return Tree
287000959d9SGreg Roach	 */
288000959d9SGreg Roach	public function getTree() {
289518bbdc1SGreg Roach		return $this->tree;
290000959d9SGreg Roach	}
291000959d9SGreg Roach
292000959d9SGreg Roach	/**
293a25f0a04SGreg Roach	 * Application code should access data via Fact objects.
294a25f0a04SGreg Roach	 * This function exists to support old code.
295a25f0a04SGreg Roach	 *
296a25f0a04SGreg Roach	 * @return string
297a25f0a04SGreg Roach	 */
298a25f0a04SGreg Roach	public function getGedcom() {
299a25f0a04SGreg Roach		if ($this->pending === null) {
300a25f0a04SGreg Roach			return $this->gedcom;
301a25f0a04SGreg Roach		} else {
302a25f0a04SGreg Roach			return $this->pending;
303a25f0a04SGreg Roach		}
304a25f0a04SGreg Roach	}
305a25f0a04SGreg Roach
306a25f0a04SGreg Roach	/**
307a25f0a04SGreg Roach	 * Does this record have a pending change?
308a25f0a04SGreg Roach	 *
309cbc1590aSGreg Roach	 * @return bool
310a25f0a04SGreg Roach	 */
311a25f0a04SGreg Roach	public function isPendingAddtion() {
312a25f0a04SGreg Roach		return $this->pending !== null;
313a25f0a04SGreg Roach	}
314a25f0a04SGreg Roach
315a25f0a04SGreg Roach	/**
316a25f0a04SGreg Roach	 * Does this record have a pending deletion?
317a25f0a04SGreg Roach	 *
318cbc1590aSGreg Roach	 * @return bool
319a25f0a04SGreg Roach	 */
320a25f0a04SGreg Roach	public function isPendingDeletion() {
321a25f0a04SGreg Roach		return $this->pending === '';
322a25f0a04SGreg Roach	}
323a25f0a04SGreg Roach
324a25f0a04SGreg Roach	/**
325a25f0a04SGreg Roach	 * Generate a URL to this record, suitable for use in HTML, etc.
326a25f0a04SGreg Roach	 *
327b1f1e4efSGreg Roach	 * @deprecated
328b1f1e4efSGreg Roach	 *
329a25f0a04SGreg Roach	 * @return string
330a25f0a04SGreg Roach	 */
331a25f0a04SGreg Roach	public function getHtmlUrl() {
332b1f1e4efSGreg Roach		return e($this->url());
333b1f1e4efSGreg Roach	}
334b1f1e4efSGreg Roach
335b1f1e4efSGreg Roach	/**
336b1f1e4efSGreg Roach	 * Generate a URL to this record, suitable for use in javascript, HTTP headers, etc.
337b1f1e4efSGreg Roach	 *
338b1f1e4efSGreg Roach	 * @deprecated
339b1f1e4efSGreg Roach	 *
340b1f1e4efSGreg Roach	 * @return string
341b1f1e4efSGreg Roach	 */
342b1f1e4efSGreg Roach	public function getRawUrl() {
343b1f1e4efSGreg Roach		return $this->url();
344a25f0a04SGreg Roach	}
345a25f0a04SGreg Roach
346a25f0a04SGreg Roach	/**
347a25f0a04SGreg Roach	 * Generate a URL to this record, suitable for use in javascript, HTTP headers, etc.
348a25f0a04SGreg Roach	 *
349a25f0a04SGreg Roach	 * @return string
350a25f0a04SGreg Roach	 */
351b1f1e4efSGreg Roach	public function url() {
352b1f1e4efSGreg Roach		return static::URL_PREFIX . rawurlencode($this->getXref()) . '&ged=' . rawurlencode($this->tree->getName());
353a25f0a04SGreg Roach	}
354a25f0a04SGreg Roach
355a25f0a04SGreg Roach	/**
356a25f0a04SGreg Roach	 * Work out whether this record can be shown to a user with a given access level
357a25f0a04SGreg Roach	 *
358cbc1590aSGreg Roach	 * @param int $access_level
359a25f0a04SGreg Roach	 *
360cbc1590aSGreg Roach	 * @return bool
361a25f0a04SGreg Roach	 */
362b13407b4SGreg Roach	private function canShowRecord($access_level) {
363a25f0a04SGreg Roach		// This setting would better be called "$ENABLE_PRIVACY"
364518bbdc1SGreg Roach		if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) {
365a25f0a04SGreg Roach			return true;
366a25f0a04SGreg Roach		}
367a25f0a04SGreg Roach
368a25f0a04SGreg Roach		// We should always be able to see our own record (unless an admin is applying download restrictions)
3694b9ff166SGreg Roach		if ($this->getXref() === $this->tree->getUserPreference(Auth::user(), 'gedcomid') && $access_level === Auth::accessLevel($this->tree)) {
370a25f0a04SGreg Roach			return true;
371a25f0a04SGreg Roach		}
372a25f0a04SGreg Roach
373a25f0a04SGreg Roach		// Does this record have a RESN?
374a25f0a04SGreg Roach		if (strpos($this->gedcom, "\n1 RESN confidential")) {
3754b9ff166SGreg Roach			return Auth::PRIV_NONE >= $access_level;
376a25f0a04SGreg Roach		}
377a25f0a04SGreg Roach		if (strpos($this->gedcom, "\n1 RESN privacy")) {
3784b9ff166SGreg Roach			return Auth::PRIV_USER >= $access_level;
379a25f0a04SGreg Roach		}
380a25f0a04SGreg Roach		if (strpos($this->gedcom, "\n1 RESN none")) {
381a25f0a04SGreg Roach			return true;
382a25f0a04SGreg Roach		}
383a25f0a04SGreg Roach
384a25f0a04SGreg Roach		// Does this record have a default RESN?
385518bbdc1SGreg Roach		$individual_privacy = $this->tree->getIndividualPrivacy();
386518bbdc1SGreg Roach		if (isset($individual_privacy[$this->getXref()])) {
387518bbdc1SGreg Roach			return $individual_privacy[$this->getXref()] >= $access_level;
388a25f0a04SGreg Roach		}
389a25f0a04SGreg Roach
390a25f0a04SGreg Roach		// Privacy rules do not apply to admins
3914b9ff166SGreg Roach		if (Auth::PRIV_NONE >= $access_level) {
392a25f0a04SGreg Roach			return true;
393a25f0a04SGreg Roach		}
394a25f0a04SGreg Roach
395a25f0a04SGreg Roach		// Different types of record have different privacy rules
396a25f0a04SGreg Roach		return $this->canShowByType($access_level);
397a25f0a04SGreg Roach	}
398a25f0a04SGreg Roach
399a25f0a04SGreg Roach	/**
400a25f0a04SGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
401a25f0a04SGreg Roach	 *
402cbc1590aSGreg Roach	 * @param int $access_level
403a25f0a04SGreg Roach	 *
404cbc1590aSGreg Roach	 * @return bool
405a25f0a04SGreg Roach	 */
406a25f0a04SGreg Roach	protected function canShowByType($access_level) {
407518bbdc1SGreg Roach		$fact_privacy = $this->tree->getFactPrivacy();
408a25f0a04SGreg Roach
409518bbdc1SGreg Roach		if (isset($fact_privacy[static::RECORD_TYPE])) {
410a25f0a04SGreg Roach			// Restriction found
411518bbdc1SGreg Roach			return $fact_privacy[static::RECORD_TYPE] >= $access_level;
412a25f0a04SGreg Roach		} else {
413a25f0a04SGreg Roach			// No restriction found - must be public:
414a25f0a04SGreg Roach			return true;
415a25f0a04SGreg Roach		}
416a25f0a04SGreg Roach	}
417a25f0a04SGreg Roach
418a25f0a04SGreg Roach	/**
419a25f0a04SGreg Roach	 * Can the details of this record be shown?
420a25f0a04SGreg Roach	 *
421cbc1590aSGreg Roach	 * @param int|null $access_level
422a25f0a04SGreg Roach	 *
423cbc1590aSGreg Roach	 * @return bool
424a25f0a04SGreg Roach	 */
4254b9ff166SGreg Roach	public function canShow($access_level = null) {
4264b9ff166SGreg Roach		if ($access_level === null) {
4274b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
4284b9ff166SGreg Roach		}
4294b9ff166SGreg Roach
430a25f0a04SGreg Roach		// CACHING: this function can take three different parameters,
431a25f0a04SGreg Roach		// and therefore needs three different caches for the result.
432a25f0a04SGreg Roach		switch ($access_level) {
4334b9ff166SGreg Roach			case Auth::PRIV_PRIVATE: // visitor
434a25f0a04SGreg Roach				if ($this->disp_public === null) {
4354b9ff166SGreg Roach					$this->disp_public = $this->canShowRecord(Auth::PRIV_PRIVATE);
436a25f0a04SGreg Roach				}
437cbc1590aSGreg Roach
438a25f0a04SGreg Roach				return $this->disp_public;
4394b9ff166SGreg Roach			case Auth::PRIV_USER: // member
440a25f0a04SGreg Roach				if ($this->disp_user === null) {
4414b9ff166SGreg Roach					$this->disp_user = $this->canShowRecord(Auth::PRIV_USER);
442a25f0a04SGreg Roach				}
443cbc1590aSGreg Roach
444a25f0a04SGreg Roach				return $this->disp_user;
4454b9ff166SGreg Roach			case Auth::PRIV_NONE: // admin
446a25f0a04SGreg Roach				if ($this->disp_none === null) {
4474b9ff166SGreg Roach					$this->disp_none = $this->canShowRecord(Auth::PRIV_NONE);
448a25f0a04SGreg Roach				}
449cbc1590aSGreg Roach
450a25f0a04SGreg Roach				return $this->disp_none;
4514b9ff166SGreg Roach			case Auth::PRIV_HIDE: // hidden from admins
452a25f0a04SGreg Roach				// We use this value to bypass privacy checks. For example,
453a25f0a04SGreg Roach				// when downloading data or when calculating privacy itself.
454a25f0a04SGreg Roach				return true;
455a25f0a04SGreg Roach			default:
456a25f0a04SGreg Roach				// Should never get here.
457a25f0a04SGreg Roach				return false;
458a25f0a04SGreg Roach		}
459a25f0a04SGreg Roach	}
460a25f0a04SGreg Roach
461a25f0a04SGreg Roach	/**
462a25f0a04SGreg Roach	 * Can the name of this record be shown?
463a25f0a04SGreg Roach	 *
464cbc1590aSGreg Roach	 * @param int|null $access_level
465a25f0a04SGreg Roach	 *
466cbc1590aSGreg Roach	 * @return bool
467a25f0a04SGreg Roach	 */
4684b9ff166SGreg Roach	public function canShowName($access_level = null) {
4694b9ff166SGreg Roach		if ($access_level === null) {
4704b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
4714b9ff166SGreg Roach		}
4724b9ff166SGreg Roach
473a25f0a04SGreg Roach		return $this->canShow($access_level);
474a25f0a04SGreg Roach	}
475a25f0a04SGreg Roach
476a25f0a04SGreg Roach	/**
477a25f0a04SGreg Roach	 * Can we edit this record?
478a25f0a04SGreg Roach	 *
479cbc1590aSGreg Roach	 * @return bool
480a25f0a04SGreg Roach	 */
481a25f0a04SGreg Roach	public function canEdit() {
4824b9ff166SGreg Roach		return Auth::isManager($this->tree) || Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false;
483a25f0a04SGreg Roach	}
484a25f0a04SGreg Roach
485a25f0a04SGreg Roach	/**
486a25f0a04SGreg Roach	 * Remove private data from the raw gedcom record.
487a25f0a04SGreg Roach	 * Return both the visible and invisible data. We need the invisible data when editing.
488a25f0a04SGreg Roach	 *
489cbc1590aSGreg Roach	 * @param int $access_level
490a25f0a04SGreg Roach	 *
491a25f0a04SGreg Roach	 * @return string
492a25f0a04SGreg Roach	 */
493a25f0a04SGreg Roach	public function privatizeGedcom($access_level) {
4944b9ff166SGreg Roach		if ($access_level == Auth::PRIV_HIDE) {
495a25f0a04SGreg Roach			// We may need the original record, for example when downloading a GEDCOM or clippings cart
496a25f0a04SGreg Roach			return $this->gedcom;
497a25f0a04SGreg Roach		} elseif ($this->canShow($access_level)) {
498a25f0a04SGreg Roach			// The record is not private, but the individual facts may be.
499a25f0a04SGreg Roach
500a25f0a04SGreg Roach			// Include the entire first line (for NOTE records)
501a25f0a04SGreg Roach			list($gedrec) = explode("\n", $this->gedcom, 2);
502a25f0a04SGreg Roach
503a25f0a04SGreg Roach			// Check each of the facts for access
504a25f0a04SGreg Roach			foreach ($this->getFacts(null, false, $access_level) as $fact) {
505a25f0a04SGreg Roach				$gedrec .= "\n" . $fact->getGedcom();
506a25f0a04SGreg Roach			}
507cbc1590aSGreg Roach
508a25f0a04SGreg Roach			return $gedrec;
509a25f0a04SGreg Roach		} else {
510a25f0a04SGreg Roach			// We cannot display the details, but we may be able to display
511a25f0a04SGreg Roach			// limited data, such as links to other records.
512a25f0a04SGreg Roach			return $this->createPrivateGedcomRecord($access_level);
513a25f0a04SGreg Roach		}
514a25f0a04SGreg Roach	}
515a25f0a04SGreg Roach
516a25f0a04SGreg Roach	/**
517a25f0a04SGreg Roach	 * Generate a private version of this record
518a25f0a04SGreg Roach	 *
519cbc1590aSGreg Roach	 * @param int $access_level
520a25f0a04SGreg Roach	 *
521a25f0a04SGreg Roach	 * @return string
522a25f0a04SGreg Roach	 */
523a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
524a25f0a04SGreg Roach		return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private');
525a25f0a04SGreg Roach	}
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach	/**
528a25f0a04SGreg Roach	 * Convert a name record into sortable and full/display versions. This default
529a25f0a04SGreg Roach	 * should be OK for simple record types. INDI/FAM records will need to redefine it.
530a25f0a04SGreg Roach	 *
531a25f0a04SGreg Roach	 * @param string $type
532a25f0a04SGreg Roach	 * @param string $value
533a25f0a04SGreg Roach	 * @param string $gedcom
534a25f0a04SGreg Roach	 */
535a25f0a04SGreg Roach	protected function addName($type, $value, $gedcom) {
53613abd6f3SGreg Roach		$this->_getAllNames[] = [
537a25f0a04SGreg Roach			'type'   => $type,
5388d68cabeSGreg Roach			'sort'   => preg_replace_callback('/([0-9]+)/', function ($matches) {
5398d68cabeSGreg Roach				return str_pad($matches[0], 10, '0', STR_PAD_LEFT);
5408d68cabeSGreg Roach			}, $value),
541*d53324c9SGreg Roach			'full'   => '<span dir="auto">' . e($value) . '</span>', // This is used for display
542a25f0a04SGreg Roach			'fullNN' => $value, // This goes into the database
54313abd6f3SGreg Roach		];
544a25f0a04SGreg Roach	}
545a25f0a04SGreg Roach
546a25f0a04SGreg Roach	/**
547a25f0a04SGreg Roach	 * Get all the names of a record, including ROMN, FONE and _HEB alternatives.
548a25f0a04SGreg Roach	 * Records without a name (e.g. FAM) will need to redefine this function.
549a25f0a04SGreg Roach	 * Parameters: the level 1 fact containing the name.
550a25f0a04SGreg Roach	 * Return value: an array of name structures, each containing
551a25f0a04SGreg Roach	 * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc.
552a25f0a04SGreg Roach	 * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown'
553a25f0a04SGreg Roach	 * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John'
554a25f0a04SGreg Roach	 *
555cbc1590aSGreg Roach	 * @param int    $level
556a25f0a04SGreg Roach	 * @param string $fact_type
557a25f0a04SGreg Roach	 * @param Fact[] $facts
558a25f0a04SGreg Roach	 */
55936a0c51dSGreg Roach	protected function extractNamesFromFacts($level, $fact_type, $facts) {
560a25f0a04SGreg Roach		$sublevel    = $level + 1;
561a25f0a04SGreg Roach		$subsublevel = $sublevel + 1;
562a25f0a04SGreg Roach		foreach ($facts as $fact) {
563a25f0a04SGreg Roach			if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->getGedcom(), $matches, PREG_SET_ORDER)) {
564a25f0a04SGreg Roach				foreach ($matches as $match) {
565a25f0a04SGreg Roach					// Treat 1 NAME / 2 TYPE married the same as _MARNM
566a25f0a04SGreg Roach					if ($match[1] == 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) {
567a25f0a04SGreg Roach						$this->addName('_MARNM', $match[2], $fact->getGedcom());
568a25f0a04SGreg Roach					} else {
569a25f0a04SGreg Roach						$this->addName($match[1], $match[2], $fact->getGedcom());
570a25f0a04SGreg Roach					}
571a25f0a04SGreg Roach					if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) {
572a25f0a04SGreg Roach						foreach ($submatches as $submatch) {
573a25f0a04SGreg Roach							$this->addName($submatch[1], $submatch[2], $match[3]);
574a25f0a04SGreg Roach						}
575a25f0a04SGreg Roach					}
576a25f0a04SGreg Roach				}
577a25f0a04SGreg Roach			}
578a25f0a04SGreg Roach		}
579a25f0a04SGreg Roach	}
580a25f0a04SGreg Roach
581a25f0a04SGreg Roach	/**
582a25f0a04SGreg Roach	 * Default for "other" object types
583a25f0a04SGreg Roach	 */
584a25f0a04SGreg Roach	public function extractNames() {
585a25f0a04SGreg Roach		$this->addName(static::RECORD_TYPE, $this->getFallBackName(), null);
586a25f0a04SGreg Roach	}
587a25f0a04SGreg Roach
588a25f0a04SGreg Roach	/**
589a25f0a04SGreg Roach	 * Derived classes should redefine this function, otherwise the object will have no name
590a25f0a04SGreg Roach	 *
591a25f0a04SGreg Roach	 * @return string[][]
592a25f0a04SGreg Roach	 */
593a25f0a04SGreg Roach	public function getAllNames() {
594a25f0a04SGreg Roach		if ($this->_getAllNames === null) {
59513abd6f3SGreg Roach			$this->_getAllNames = [];
596a25f0a04SGreg Roach			if ($this->canShowName()) {
597a25f0a04SGreg Roach				// Ask the record to extract its names
598a25f0a04SGreg Roach				$this->extractNames();
599a25f0a04SGreg Roach				// No name found? Use a fallback.
600a25f0a04SGreg Roach				if (!$this->_getAllNames) {
601a25f0a04SGreg Roach					$this->addName(static::RECORD_TYPE, $this->getFallBackName(), null);
602a25f0a04SGreg Roach				}
603a25f0a04SGreg Roach			} else {
604a25f0a04SGreg Roach				$this->addName(static::RECORD_TYPE, I18N::translate('Private'), null);
605a25f0a04SGreg Roach			}
606a25f0a04SGreg Roach		}
607cbc1590aSGreg Roach
608a25f0a04SGreg Roach		return $this->_getAllNames;
609a25f0a04SGreg Roach	}
610a25f0a04SGreg Roach
611a25f0a04SGreg Roach	/**
612a25f0a04SGreg Roach	 * If this object has no name, what do we call it?
613a25f0a04SGreg Roach	 *
614a25f0a04SGreg Roach	 * @return string
615a25f0a04SGreg Roach	 */
616a25f0a04SGreg Roach	public function getFallBackName() {
617*d53324c9SGreg Roach		return e($this->getXref());
618a25f0a04SGreg Roach	}
619a25f0a04SGreg Roach
620a25f0a04SGreg Roach	/**
621a25f0a04SGreg Roach	 * Which of the (possibly several) names of this record is the primary one.
622a25f0a04SGreg Roach	 *
623cbc1590aSGreg Roach	 * @return int
624a25f0a04SGreg Roach	 */
625a25f0a04SGreg Roach	public function getPrimaryName() {
626a25f0a04SGreg Roach		static $language_script;
627a25f0a04SGreg Roach
628a25f0a04SGreg Roach		if ($language_script === null) {
629a25f0a04SGreg Roach			$language_script = I18N::languageScript(WT_LOCALE);
630a25f0a04SGreg Roach		}
631a25f0a04SGreg Roach
632a25f0a04SGreg Roach		if ($this->_getPrimaryName === null) {
633a25f0a04SGreg Roach			// Generally, the first name is the primary one....
634a25f0a04SGreg Roach			$this->_getPrimaryName = 0;
635a25f0a04SGreg Roach			// ...except when the language/name use different character sets
636a25f0a04SGreg Roach			if (count($this->getAllNames()) > 1) {
637a25f0a04SGreg Roach				foreach ($this->getAllNames() as $n => $name) {
63869546be1SGreg Roach					if (I18N::textScript($name['sort']) === $language_script) {
639a25f0a04SGreg Roach						$this->_getPrimaryName = $n;
640a25f0a04SGreg Roach						break;
641a25f0a04SGreg Roach					}
642a25f0a04SGreg Roach				}
643a25f0a04SGreg Roach			}
644a25f0a04SGreg Roach		}
645a25f0a04SGreg Roach
646a25f0a04SGreg Roach		return $this->_getPrimaryName;
647a25f0a04SGreg Roach	}
648a25f0a04SGreg Roach
649a25f0a04SGreg Roach	/**
650a25f0a04SGreg Roach	 * Which of the (possibly several) names of this record is the secondary one.
651a25f0a04SGreg Roach	 *
652cbc1590aSGreg Roach	 * @return int
653a25f0a04SGreg Roach	 */
654a25f0a04SGreg Roach	public function getSecondaryName() {
655a25f0a04SGreg Roach		if (is_null($this->_getSecondaryName)) {
656a25f0a04SGreg Roach			// Generally, the primary and secondary names are the same
657a25f0a04SGreg Roach			$this->_getSecondaryName = $this->getPrimaryName();
658a25f0a04SGreg Roach			// ....except when there are names with different character sets
659a25f0a04SGreg Roach			$all_names = $this->getAllNames();
660a25f0a04SGreg Roach			if (count($all_names) > 1) {
661a25f0a04SGreg Roach				$primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']);
662a25f0a04SGreg Roach				foreach ($all_names as $n => $name) {
663a25f0a04SGreg Roach					if ($n != $this->getPrimaryName() && $name['type'] != '_MARNM' && I18N::textScript($name['sort']) != $primary_script) {
664a25f0a04SGreg Roach						$this->_getSecondaryName = $n;
665a25f0a04SGreg Roach						break;
666a25f0a04SGreg Roach					}
667a25f0a04SGreg Roach				}
668a25f0a04SGreg Roach			}
669a25f0a04SGreg Roach		}
670cbc1590aSGreg Roach
671a25f0a04SGreg Roach		return $this->_getSecondaryName;
672a25f0a04SGreg Roach	}
673a25f0a04SGreg Roach
674a25f0a04SGreg Roach	/**
675a25f0a04SGreg Roach	 * Allow the choice of primary name to be overidden, e.g. in a search result
676a25f0a04SGreg Roach	 *
677cbc1590aSGreg Roach	 * @param int $n
678a25f0a04SGreg Roach	 */
679a25f0a04SGreg Roach	public function setPrimaryName($n) {
680a25f0a04SGreg Roach		$this->_getPrimaryName   = $n;
681a25f0a04SGreg Roach		$this->_getSecondaryName = null;
682a25f0a04SGreg Roach	}
683a25f0a04SGreg Roach
684a25f0a04SGreg Roach	/**
685a25f0a04SGreg Roach	 * Allow native PHP functions such as array_unique() to work with objects
686a25f0a04SGreg Roach	 *
687a25f0a04SGreg Roach	 * @return string
688a25f0a04SGreg Roach	 */
689a25f0a04SGreg Roach	public function __toString() {
690518bbdc1SGreg Roach		return $this->xref . '@' . $this->tree->getTreeId();
691a25f0a04SGreg Roach	}
692a25f0a04SGreg Roach
693a25f0a04SGreg Roach	/**
694a25f0a04SGreg Roach	 * Static helper function to sort an array of objects by name
695a25f0a04SGreg Roach	 * Records whose names cannot be displayed are sorted at the end.
696a25f0a04SGreg Roach	 *
697a25f0a04SGreg Roach	 * @param GedcomRecord $x
698a25f0a04SGreg Roach	 * @param GedcomRecord $y
699a25f0a04SGreg Roach	 *
700cbc1590aSGreg Roach	 * @return int
701a25f0a04SGreg Roach	 */
702a25f0a04SGreg Roach	public static function compare(GedcomRecord $x, GedcomRecord $y) {
703a25f0a04SGreg Roach		if ($x->canShowName()) {
704a25f0a04SGreg Roach			if ($y->canShowName()) {
705a25f0a04SGreg Roach				return I18N::strcasecmp($x->getSortName(), $y->getSortName());
706a25f0a04SGreg Roach			} else {
707a25f0a04SGreg Roach				return -1; // only $y is private
708a25f0a04SGreg Roach			}
709a25f0a04SGreg Roach		} else {
710a25f0a04SGreg Roach			if ($y->canShowName()) {
711a25f0a04SGreg Roach				return 1; // only $x is private
712a25f0a04SGreg Roach			} else {
713a25f0a04SGreg Roach				return 0; // both $x and $y private
714a25f0a04SGreg Roach			}
715a25f0a04SGreg Roach		}
716a25f0a04SGreg Roach	}
717a25f0a04SGreg Roach
718a25f0a04SGreg Roach	/**
719a25f0a04SGreg Roach	 * Get variants of the name
720a25f0a04SGreg Roach	 *
721a25f0a04SGreg Roach	 * @return string
722a25f0a04SGreg Roach	 */
723a25f0a04SGreg Roach	public function getFullName() {
724a25f0a04SGreg Roach		if ($this->canShowName()) {
725a25f0a04SGreg Roach			$tmp = $this->getAllNames();
726cbc1590aSGreg Roach
727a25f0a04SGreg Roach			return $tmp[$this->getPrimaryName()]['full'];
728a25f0a04SGreg Roach		} else {
729a25f0a04SGreg Roach			return I18N::translate('Private');
730a25f0a04SGreg Roach		}
731a25f0a04SGreg Roach	}
732a25f0a04SGreg Roach
733a25f0a04SGreg Roach	/**
734a25f0a04SGreg Roach	 * Get a sortable version of the name. Do not display this!
735a25f0a04SGreg Roach	 *
736a25f0a04SGreg Roach	 * @return string
737a25f0a04SGreg Roach	 */
738a25f0a04SGreg Roach	public function getSortName() {
739a25f0a04SGreg Roach		// The sortable name is never displayed, no need to call canShowName()
740a25f0a04SGreg Roach		$tmp = $this->getAllNames();
741cbc1590aSGreg Roach
742a25f0a04SGreg Roach		return $tmp[$this->getPrimaryName()]['sort'];
743a25f0a04SGreg Roach	}
744a25f0a04SGreg Roach
745a25f0a04SGreg Roach	/**
746a25f0a04SGreg Roach	 * Get the full name in an alternative character set
747a25f0a04SGreg Roach	 *
748a25f0a04SGreg Roach	 * @return null|string
749a25f0a04SGreg Roach	 */
750a25f0a04SGreg Roach	public function getAddName() {
751a25f0a04SGreg Roach		if ($this->canShowName() && $this->getPrimaryName() != $this->getSecondaryName()) {
752a25f0a04SGreg Roach			$all_names = $this->getAllNames();
753cbc1590aSGreg Roach
754a25f0a04SGreg Roach			return $all_names[$this->getSecondaryName()]['full'];
755a25f0a04SGreg Roach		} else {
756a25f0a04SGreg Roach			return null;
757a25f0a04SGreg Roach		}
758a25f0a04SGreg Roach	}
759a25f0a04SGreg Roach
760a25f0a04SGreg Roach	/**
761a25f0a04SGreg Roach	 * Format this object for display in a list
762a25f0a04SGreg Roach	 * If $find is set, then we are displaying items from a selection list.
763a25f0a04SGreg Roach	 * $name allows us to use something other than the record name.
764a25f0a04SGreg Roach	 *
765a25f0a04SGreg Roach	 * @param string $tag
766cbc1590aSGreg Roach	 * @param bool   $find
767a25f0a04SGreg Roach	 * @param null   $name
768a25f0a04SGreg Roach	 *
769a25f0a04SGreg Roach	 * @return string
770a25f0a04SGreg Roach	 */
771841014f1SGreg Roach	public function formatList($tag = 'li', $find = false, $name = null) {
772a25f0a04SGreg Roach		if (is_null($name)) {
773a25f0a04SGreg Roach			$name = $this->getFullName();
774a25f0a04SGreg Roach		}
775b1f1e4efSGreg Roach		$html = '<a href="' . e($this->url()) . '"';
776a25f0a04SGreg Roach		if ($find) {
777a25f0a04SGreg Roach			$html .= ' onclick="pasteid(\'' . $this->getXref() . '\', \'' . htmlentities($name) . '\');"';
778a25f0a04SGreg Roach		}
779a25f0a04SGreg Roach		$html .= ' class="list_item"><b>' . $name . '</b>';
780a25f0a04SGreg Roach		$html .= $this->formatListDetails();
781a25f0a04SGreg Roach		$html = '<' . $tag . '>' . $html . '</a></' . $tag . '>';
782cbc1590aSGreg Roach
783a25f0a04SGreg Roach		return $html;
784a25f0a04SGreg Roach	}
785a25f0a04SGreg Roach
786a25f0a04SGreg Roach	/**
787a25f0a04SGreg Roach	 * This function should be redefined in derived classes to show any major
788a25f0a04SGreg Roach	 * identifying characteristics of this record.
789a25f0a04SGreg Roach	 *
790a25f0a04SGreg Roach	 * @return string
791a25f0a04SGreg Roach	 */
792a25f0a04SGreg Roach	public function formatListDetails() {
793a25f0a04SGreg Roach		return '';
794a25f0a04SGreg Roach	}
795a25f0a04SGreg Roach
796a25f0a04SGreg Roach	/**
797a25f0a04SGreg Roach	 * Extract/format the first fact from a list of facts.
798a25f0a04SGreg Roach	 *
799a25f0a04SGreg Roach	 * @param string $facts
800cbc1590aSGreg Roach	 * @param int    $style
801a25f0a04SGreg Roach	 *
802a25f0a04SGreg Roach	 * @return string
803a25f0a04SGreg Roach	 */
804841014f1SGreg Roach	public function formatFirstMajorFact($facts, $style) {
805a25f0a04SGreg Roach		foreach ($this->getFacts($facts, true) as $event) {
806a25f0a04SGreg Roach			// Only display if it has a date or place (or both)
807d93f11b5SGreg Roach			if ($event->getDate()->isOK() && !$event->getPlace()->isEmpty()) {
808d93f11b5SGreg Roach				$joiner = ' — ';
809d93f11b5SGreg Roach			} else {
810d93f11b5SGreg Roach				$joiner = '';
811d93f11b5SGreg Roach			}
812a25f0a04SGreg Roach			if ($event->getDate()->isOK() || !$event->getPlace()->isEmpty()) {
813a25f0a04SGreg Roach				switch ($style) {
814a25f0a04SGreg Roach					case 1:
815d93f11b5SGreg Roach						return '<br><em>' . $event->getLabel() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>';
816a25f0a04SGreg Roach					case 2:
817d93f11b5SGreg Roach						return '<dl><dt class="label">' . $event->getLabel() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>';
818a25f0a04SGreg Roach				}
819a25f0a04SGreg Roach			}
820a25f0a04SGreg Roach		}
821cbc1590aSGreg Roach
822a25f0a04SGreg Roach		return '';
823a25f0a04SGreg Roach	}
824a25f0a04SGreg Roach
825a25f0a04SGreg Roach	/**
826a25f0a04SGreg Roach	 * Find individuals linked to this record.
827a25f0a04SGreg Roach	 *
828a25f0a04SGreg Roach	 * @param string $link
829a25f0a04SGreg Roach	 *
830360dbd2cSGreg Roach	 * @return Individual[]
831a25f0a04SGreg Roach	 */
832a25f0a04SGreg Roach	public function linkedIndividuals($link) {
833a25f0a04SGreg Roach		$rows = Database::prepare(
83424ec66ceSGreg Roach			"SELECT i_id AS xref, i_gedcom AS gedcom" .
835a25f0a04SGreg Roach			" FROM `##individuals`" .
83681088023SGreg Roach			" JOIN `##link` ON i_file = l_file AND i_id = l_from" .
83781088023SGreg Roach			" LEFT JOIN `##name` ON i_file = n_file AND i_id = n_id AND n_num = 0" .
83881088023SGreg Roach			" WHERE i_file = :tree_id AND l_type = :link AND l_to = :xref" .
83981088023SGreg Roach			" ORDER BY n_sort COLLATE :collation"
84013abd6f3SGreg Roach		)->execute([
84181088023SGreg Roach			'tree_id'   => $this->tree->getTreeId(),
84281088023SGreg Roach			'link'      => $link,
84381088023SGreg Roach			'xref'      => $this->xref,
84481088023SGreg Roach			'collation' => I18N::collation(),
84513abd6f3SGreg Roach		])->fetchAll();
846a25f0a04SGreg Roach
84713abd6f3SGreg Roach		$list = [];
848a25f0a04SGreg Roach		foreach ($rows as $row) {
84924ec66ceSGreg Roach			$record = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
850a25f0a04SGreg Roach			if ($record->canShowName()) {
851a25f0a04SGreg Roach				$list[] = $record;
852a25f0a04SGreg Roach			}
853a25f0a04SGreg Roach		}
854cbc1590aSGreg Roach
855a25f0a04SGreg Roach		return $list;
856a25f0a04SGreg Roach	}
857a25f0a04SGreg Roach
858a25f0a04SGreg Roach	/**
859a25f0a04SGreg Roach	 * Find families linked to this record.
860a25f0a04SGreg Roach	 *
861a25f0a04SGreg Roach	 * @param string $link
862a25f0a04SGreg Roach	 *
863360dbd2cSGreg Roach	 * @return Family[]
864a25f0a04SGreg Roach	 */
865a25f0a04SGreg Roach	public function linkedFamilies($link) {
866a25f0a04SGreg Roach		$rows = Database::prepare(
86724ec66ceSGreg Roach			"SELECT f_id AS xref, f_gedcom AS gedcom" .
868a25f0a04SGreg Roach			" FROM `##families`" .
86981088023SGreg Roach			" JOIN `##link` ON f_file = l_file AND f_id = l_from" .
87081088023SGreg Roach			" LEFT JOIN `##name` ON f_file = n_file AND f_id = n_id AND n_num = 0" .
87181088023SGreg Roach			" WHERE f_file = :tree_id AND l_type = :link AND l_to = :xref"
87213abd6f3SGreg Roach		)->execute([
87381088023SGreg Roach			'tree_id' => $this->tree->getTreeId(),
87481088023SGreg Roach			'link'    => $link,
87581088023SGreg Roach			'xref'    => $this->xref,
87613abd6f3SGreg Roach		])->fetchAll();
877a25f0a04SGreg Roach
87813abd6f3SGreg Roach		$list = [];
879a25f0a04SGreg Roach		foreach ($rows as $row) {
88024ec66ceSGreg Roach			$record = Family::getInstance($row->xref, $this->tree, $row->gedcom);
881a25f0a04SGreg Roach			if ($record->canShowName()) {
882a25f0a04SGreg Roach				$list[] = $record;
883a25f0a04SGreg Roach			}
884a25f0a04SGreg Roach		}
885cbc1590aSGreg Roach
886a25f0a04SGreg Roach		return $list;
887a25f0a04SGreg Roach	}
888a25f0a04SGreg Roach
889a25f0a04SGreg Roach	/**
890a25f0a04SGreg Roach	 * Find sources linked to this record.
891a25f0a04SGreg Roach	 *
892a25f0a04SGreg Roach	 * @param string $link
893a25f0a04SGreg Roach	 *
894360dbd2cSGreg Roach	 * @return Source[]
895a25f0a04SGreg Roach	 */
896a25f0a04SGreg Roach	public function linkedSources($link) {
897a25f0a04SGreg Roach		$rows = Database::prepare(
89824ec66ceSGreg Roach			"SELECT s_id AS xref, s_gedcom AS gedcom" .
899a25f0a04SGreg Roach			" FROM `##sources`" .
90081088023SGreg Roach			" JOIN `##link` ON s_file = l_file AND s_id = l_from" .
90181088023SGreg Roach			" WHERE s_file = :tree_id AND l_type = :link AND l_to = :xref" .
90281088023SGreg Roach			" ORDER BY s_name COLLATE :collation"
90313abd6f3SGreg Roach		)->execute([
90481088023SGreg Roach			'tree_id'   => $this->tree->getTreeId(),
90581088023SGreg Roach			'link'      => $link,
90681088023SGreg Roach			'xref'      => $this->xref,
90781088023SGreg Roach			'collation' => I18N::collation(),
90813abd6f3SGreg Roach		])->fetchAll();
909a25f0a04SGreg Roach
91013abd6f3SGreg Roach		$list = [];
911a25f0a04SGreg Roach		foreach ($rows as $row) {
91224ec66ceSGreg Roach			$record = Source::getInstance($row->xref, $this->tree, $row->gedcom);
913a25f0a04SGreg Roach			if ($record->canShowName()) {
914a25f0a04SGreg Roach				$list[] = $record;
915a25f0a04SGreg Roach			}
916a25f0a04SGreg Roach		}
917cbc1590aSGreg Roach
918a25f0a04SGreg Roach		return $list;
919a25f0a04SGreg Roach	}
920a25f0a04SGreg Roach
921a25f0a04SGreg Roach	/**
922a25f0a04SGreg Roach	 * Find media objects linked to this record.
923a25f0a04SGreg Roach	 *
924a25f0a04SGreg Roach	 * @param string $link
925a25f0a04SGreg Roach	 *
926360dbd2cSGreg Roach	 * @return Media[]
927a25f0a04SGreg Roach	 */
928a25f0a04SGreg Roach	public function linkedMedia($link) {
929a25f0a04SGreg Roach		$rows = Database::prepare(
93024ec66ceSGreg Roach			"SELECT m_id AS xref, m_gedcom AS gedcom" .
931a25f0a04SGreg Roach			" FROM `##media`" .
93281088023SGreg Roach			" JOIN `##link` ON m_file = l_file AND m_id = l_from" .
93364b90bf1SGreg Roach			" WHERE m_file = :tree_id AND l_type = :link AND l_to = :xref"
93413abd6f3SGreg Roach		)->execute([
93581088023SGreg Roach			'tree_id'   => $this->tree->getTreeId(),
93681088023SGreg Roach			'link'      => $link,
93781088023SGreg Roach			'xref'      => $this->xref,
93813abd6f3SGreg Roach		])->fetchAll();
939a25f0a04SGreg Roach
94013abd6f3SGreg Roach		$list = [];
941a25f0a04SGreg Roach		foreach ($rows as $row) {
94224ec66ceSGreg Roach			$record = Media::getInstance($row->xref, $this->tree, $row->gedcom);
943a25f0a04SGreg Roach			if ($record->canShowName()) {
944a25f0a04SGreg Roach				$list[] = $record;
945a25f0a04SGreg Roach			}
946a25f0a04SGreg Roach		}
947cbc1590aSGreg Roach
948a25f0a04SGreg Roach		return $list;
949a25f0a04SGreg Roach	}
950a25f0a04SGreg Roach
951a25f0a04SGreg Roach	/**
952a25f0a04SGreg Roach	 * Find notes linked to this record.
953a25f0a04SGreg Roach	 *
954a25f0a04SGreg Roach	 * @param string $link
955a25f0a04SGreg Roach	 *
956a25f0a04SGreg Roach	 * @return Note[]
957a25f0a04SGreg Roach	 */
958a25f0a04SGreg Roach	public function linkedNotes($link) {
959a25f0a04SGreg Roach		$rows = Database::prepare(
96024ec66ceSGreg Roach			"SELECT o_id AS xref, o_gedcom AS gedcom" .
961a25f0a04SGreg Roach			" FROM `##other`" .
96281088023SGreg Roach			" JOIN `##link` ON o_file = l_file AND o_id = l_from" .
96381088023SGreg Roach			" LEFT JOIN `##name` ON o_file = n_file AND o_id = n_id AND n_num = 0" .
96481088023SGreg Roach			" WHERE o_file = :tree_id AND o_type = 'NOTE' AND l_type = :link AND l_to = :xref" .
96581088023SGreg Roach			" ORDER BY n_sort COLLATE :collation"
96613abd6f3SGreg Roach		)->execute([
96781088023SGreg Roach			'tree_id'   => $this->tree->getTreeId(),
96881088023SGreg Roach			'link'      => $link,
96981088023SGreg Roach			'xref'      => $this->xref,
97081088023SGreg Roach			'collation' => I18N::collation(),
97113abd6f3SGreg Roach		])->fetchAll();
972a25f0a04SGreg Roach
97313abd6f3SGreg Roach		$list = [];
974a25f0a04SGreg Roach		foreach ($rows as $row) {
97524ec66ceSGreg Roach			$record = Note::getInstance($row->xref, $this->tree, $row->gedcom);
976a25f0a04SGreg Roach			if ($record->canShowName()) {
977a25f0a04SGreg Roach				$list[] = $record;
978a25f0a04SGreg Roach			}
979a25f0a04SGreg Roach		}
980cbc1590aSGreg Roach
981a25f0a04SGreg Roach		return $list;
982a25f0a04SGreg Roach	}
983a25f0a04SGreg Roach
984a25f0a04SGreg Roach	/**
985a25f0a04SGreg Roach	 * Find repositories linked to this record.
986a25f0a04SGreg Roach	 *
987a25f0a04SGreg Roach	 * @param string $link
988a25f0a04SGreg Roach	 *
989360dbd2cSGreg Roach	 * @return Repository[]
990a25f0a04SGreg Roach	 */
991a25f0a04SGreg Roach	public function linkedRepositories($link) {
992a25f0a04SGreg Roach		$rows = Database::prepare(
99324ec66ceSGreg Roach			"SELECT o_id AS xref, o_gedcom AS gedcom" .
994a25f0a04SGreg Roach			" FROM `##other`" .
99581088023SGreg Roach			" JOIN `##link` ON o_file = l_file AND o_id = l_from" .
99681088023SGreg Roach			" LEFT JOIN `##name` ON o_file = n_file AND o_id = n_id AND n_num = 0" .
99781088023SGreg Roach			" WHERE o_file = :tree_id AND o_type = 'REPO' AND l_type = :link AND l_to = :xref" .
99851a671c6SGreg Roach			" ORDER BY n_sort COLLATE :collation"
99913abd6f3SGreg Roach		)->execute([
100081088023SGreg Roach			'tree_id'   => $this->tree->getTreeId(),
100181088023SGreg Roach			'link'      => $link,
100281088023SGreg Roach			'xref'      => $this->xref,
100381088023SGreg Roach			'collation' => I18N::collation(),
100413abd6f3SGreg Roach		])->fetchAll();
1005a25f0a04SGreg Roach
100613abd6f3SGreg Roach		$list = [];
1007a25f0a04SGreg Roach		foreach ($rows as $row) {
100824ec66ceSGreg Roach			$record = Repository::getInstance($row->xref, $this->tree, $row->gedcom);
1009a25f0a04SGreg Roach			if ($record->canShowName()) {
1010a25f0a04SGreg Roach				$list[] = $record;
1011a25f0a04SGreg Roach			}
1012a25f0a04SGreg Roach		}
1013cbc1590aSGreg Roach
1014a25f0a04SGreg Roach		return $list;
1015a25f0a04SGreg Roach	}
1016a25f0a04SGreg Roach
1017a25f0a04SGreg Roach	/**
1018a25f0a04SGreg Roach	 * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR).
1019a25f0a04SGreg Roach	 * This is used to display multiple events on the individual/family lists.
1020a25f0a04SGreg Roach	 * Multiple events can exist because of uncertainty in dates, dates in different
1021a25f0a04SGreg Roach	 * calendars, place-names in both latin and hebrew character sets, etc.
1022a25f0a04SGreg Roach	 * It also allows us to combine dates/places from different events in the summaries.
1023a25f0a04SGreg Roach	 *
1024a25f0a04SGreg Roach	 * @param string $event_type
1025a25f0a04SGreg Roach	 *
1026a25f0a04SGreg Roach	 * @return Date[]
1027a25f0a04SGreg Roach	 */
1028a25f0a04SGreg Roach	public function getAllEventDates($event_type) {
102913abd6f3SGreg Roach		$dates = [];
1030a25f0a04SGreg Roach		foreach ($this->getFacts($event_type) as $event) {
1031a25f0a04SGreg Roach			if ($event->getDate()->isOK()) {
1032a25f0a04SGreg Roach				$dates[] = $event->getDate();
1033a25f0a04SGreg Roach			}
1034a25f0a04SGreg Roach		}
1035a25f0a04SGreg Roach
1036a25f0a04SGreg Roach		return $dates;
1037a25f0a04SGreg Roach	}
1038a25f0a04SGreg Roach
1039a25f0a04SGreg Roach	/**
1040a25f0a04SGreg Roach	 * Get all the places for a particular type of event
1041a25f0a04SGreg Roach	 *
1042a25f0a04SGreg Roach	 * @param string $event_type
1043a25f0a04SGreg Roach	 *
10444080d558SGreg Roach	 * @return Place[]
1045a25f0a04SGreg Roach	 */
1046a25f0a04SGreg Roach	public function getAllEventPlaces($event_type) {
104713abd6f3SGreg Roach		$places = [];
1048a25f0a04SGreg Roach		foreach ($this->getFacts($event_type) as $event) {
1049a25f0a04SGreg Roach			if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->getGedcom(), $ged_places)) {
1050a25f0a04SGreg Roach				foreach ($ged_places[1] as $ged_place) {
105116d0b7f7SRico Sonntag					$places[] = new Place($ged_place, $this->tree);
1052a25f0a04SGreg Roach				}
1053a25f0a04SGreg Roach			}
1054a25f0a04SGreg Roach		}
1055a25f0a04SGreg Roach
1056a25f0a04SGreg Roach		return $places;
1057a25f0a04SGreg Roach	}
1058a25f0a04SGreg Roach
1059a25f0a04SGreg Roach	/**
1060a25f0a04SGreg Roach	 * Get the first (i.e. prefered) Fact for the given fact type
1061a25f0a04SGreg Roach	 *
1062a25f0a04SGreg Roach	 * @param string $tag
1063a25f0a04SGreg Roach	 *
1064a25f0a04SGreg Roach	 * @return Fact|null
1065a25f0a04SGreg Roach	 */
1066a25f0a04SGreg Roach	public function getFirstFact($tag) {
1067a25f0a04SGreg Roach		foreach ($this->getFacts() as $fact) {
1068a25f0a04SGreg Roach			if ($fact->getTag() === $tag) {
1069a25f0a04SGreg Roach				return $fact;
1070a25f0a04SGreg Roach			}
1071a25f0a04SGreg Roach		}
1072a25f0a04SGreg Roach
1073a25f0a04SGreg Roach		return null;
1074a25f0a04SGreg Roach	}
1075a25f0a04SGreg Roach
1076a25f0a04SGreg Roach	/**
1077a25f0a04SGreg Roach	 * The facts and events for this record.
1078a25f0a04SGreg Roach	 *
1079a25f0a04SGreg Roach	 * @param string    $filter
1080cbc1590aSGreg Roach	 * @param bool      $sort
1081cbc1590aSGreg Roach	 * @param int|null  $access_level
1082cbc1590aSGreg Roach	 * @param bool      $override     Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES.
1083a25f0a04SGreg Roach	 *
1084a25f0a04SGreg Roach	 * @return Fact[]
1085a25f0a04SGreg Roach	 */
10864b9ff166SGreg Roach	public function getFacts($filter = null, $sort = false, $access_level = null, $override = false) {
10874b9ff166SGreg Roach		if ($access_level === null) {
10884b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
10894b9ff166SGreg Roach		}
10904b9ff166SGreg Roach
109113abd6f3SGreg Roach		$facts = [];
1092a25f0a04SGreg Roach		if ($this->canShow($access_level) || $override) {
1093a25f0a04SGreg Roach			foreach ($this->facts as $fact) {
10943a7d762dSGreg Roach				if (($filter === null || preg_match('/^' . $filter . '$/', $fact->getTag())) && $fact->canShow($access_level)) {
1095a25f0a04SGreg Roach					$facts[] = $fact;
1096a25f0a04SGreg Roach				}
1097a25f0a04SGreg Roach			}
1098a25f0a04SGreg Roach		}
1099a25f0a04SGreg Roach		if ($sort) {
11003d7a8a4cSGreg Roach			Functions::sortFacts($facts);
1101a25f0a04SGreg Roach		}
1102cbc1590aSGreg Roach
1103a25f0a04SGreg Roach		return $facts;
1104a25f0a04SGreg Roach	}
1105a25f0a04SGreg Roach
1106a25f0a04SGreg Roach	/**
1107a25f0a04SGreg Roach	 * Get the last-change timestamp for this record, either as a formatted string
1108a25f0a04SGreg Roach	 * (for display) or as a unix timestamp (for sorting)
1109a25f0a04SGreg Roach	 *
1110cbc1590aSGreg Roach	 * @param bool $sorting
1111a25f0a04SGreg Roach	 *
1112a25f0a04SGreg Roach	 * @return string
1113a25f0a04SGreg Roach	 */
1114a25f0a04SGreg Roach	public function lastChangeTimestamp($sorting = false) {
1115a25f0a04SGreg Roach		$chan = $this->getFirstFact('CHAN');
1116a25f0a04SGreg Roach
1117a25f0a04SGreg Roach		if ($chan) {
1118a25f0a04SGreg Roach			// The record does have a CHAN event
1119f5b60decSGreg Roach			$d = $chan->getDate()->minimumDate();
1120a25f0a04SGreg Roach			if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->getGedcom(), $match)) {
1121a25f0a04SGreg Roach				$t = mktime((int) $match[1], (int) $match[2], (int) $match[3], (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y'));
1122a25f0a04SGreg Roach			} elseif (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->getGedcom(), $match)) {
1123a25f0a04SGreg Roach				$t = mktime((int) $match[1], (int) $match[2], 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y'));
1124a25f0a04SGreg Roach			} else {
1125a25f0a04SGreg Roach				$t = mktime(0, 0, 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y'));
1126a25f0a04SGreg Roach			}
1127a25f0a04SGreg Roach			if ($sorting) {
1128a25f0a04SGreg Roach				return $t;
1129a25f0a04SGreg Roach			} else {
11303d7a8a4cSGreg Roach				return strip_tags(FunctionsDate::formatTimestamp($t));
1131a25f0a04SGreg Roach			}
1132a25f0a04SGreg Roach		} else {
1133a25f0a04SGreg Roach			// The record does not have a CHAN event
1134a25f0a04SGreg Roach			if ($sorting) {
1135a25f0a04SGreg Roach				return '0';
1136a25f0a04SGreg Roach			} else {
113766b90994SGreg Roach				return '';
1138a25f0a04SGreg Roach			}
1139a25f0a04SGreg Roach		}
1140a25f0a04SGreg Roach	}
1141a25f0a04SGreg Roach
1142a25f0a04SGreg Roach	/**
1143a25f0a04SGreg Roach	 * Get the last-change user for this record
1144a25f0a04SGreg Roach	 *
1145a25f0a04SGreg Roach	 * @return string
1146a25f0a04SGreg Roach	 */
1147a25f0a04SGreg Roach	public function lastChangeUser() {
1148a25f0a04SGreg Roach		$chan = $this->getFirstFact('CHAN');
1149a25f0a04SGreg Roach
1150a25f0a04SGreg Roach		if ($chan === null) {
1151a25f0a04SGreg Roach			return I18N::translate('Unknown');
1152a25f0a04SGreg Roach		} else {
1153a25f0a04SGreg Roach			$chan_user = $chan->getAttribute('_WT_USER');
1154baacc364SGreg Roach			if ($chan_user === '') {
1155a25f0a04SGreg Roach				return I18N::translate('Unknown');
1156a25f0a04SGreg Roach			} else {
1157a25f0a04SGreg Roach				return $chan_user;
1158a25f0a04SGreg Roach			}
1159a25f0a04SGreg Roach		}
1160a25f0a04SGreg Roach	}
1161a25f0a04SGreg Roach
1162a25f0a04SGreg Roach	/**
1163a25f0a04SGreg Roach	 * Add a new fact to this record
1164a25f0a04SGreg Roach	 *
1165a25f0a04SGreg Roach	 * @param string $gedcom
1166cbc1590aSGreg Roach	 * @param bool   $update_chan
1167a25f0a04SGreg Roach	 */
1168a25f0a04SGreg Roach	public function createFact($gedcom, $update_chan) {
1169a25f0a04SGreg Roach		$this->updateFact(null, $gedcom, $update_chan);
1170a25f0a04SGreg Roach	}
1171a25f0a04SGreg Roach
1172a25f0a04SGreg Roach	/**
1173a25f0a04SGreg Roach	 * Delete a fact from this record
1174a25f0a04SGreg Roach	 *
1175a25f0a04SGreg Roach	 * @param string $fact_id
1176cbc1590aSGreg Roach	 * @param bool   $update_chan
1177a25f0a04SGreg Roach	 */
1178a25f0a04SGreg Roach	public function deleteFact($fact_id, $update_chan) {
1179a25f0a04SGreg Roach		$this->updateFact($fact_id, null, $update_chan);
1180a25f0a04SGreg Roach	}
1181a25f0a04SGreg Roach
1182a25f0a04SGreg Roach	/**
1183a25f0a04SGreg Roach	 * Replace a fact with a new gedcom data.
1184a25f0a04SGreg Roach	 *
1185a25f0a04SGreg Roach	 * @param string $fact_id
1186a25f0a04SGreg Roach	 * @param string $gedcom
1187cbc1590aSGreg Roach	 * @param bool   $update_chan
1188a25f0a04SGreg Roach	 *
1189a25f0a04SGreg Roach	 * @throws \Exception
1190a25f0a04SGreg Roach	 */
1191a25f0a04SGreg Roach	public function updateFact($fact_id, $gedcom, $update_chan) {
1192a25f0a04SGreg Roach		// MSDOS line endings will break things in horrible ways
1193a25f0a04SGreg Roach		$gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
1194a25f0a04SGreg Roach		$gedcom = trim($gedcom);
1195a25f0a04SGreg Roach
1196a25f0a04SGreg Roach		if ($this->pending === '') {
1197a25f0a04SGreg Roach			throw new \Exception('Cannot edit a deleted record');
1198a25f0a04SGreg Roach		}
1199a25f0a04SGreg Roach		if ($gedcom && !preg_match('/^1 ' . WT_REGEX_TAG . '/', $gedcom)) {
1200a25f0a04SGreg Roach			throw new \Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')');
1201a25f0a04SGreg Roach		}
1202a25f0a04SGreg Roach
1203a25f0a04SGreg Roach		if ($this->pending) {
1204a25f0a04SGreg Roach			$old_gedcom = $this->pending;
1205a25f0a04SGreg Roach		} else {
1206a25f0a04SGreg Roach			$old_gedcom = $this->gedcom;
1207a25f0a04SGreg Roach		}
1208a25f0a04SGreg Roach
1209a25f0a04SGreg Roach		// First line of record may contain data - e.g. NOTE records.
1210a25f0a04SGreg Roach		list($new_gedcom) = explode("\n", $old_gedcom, 2);
1211a25f0a04SGreg Roach
1212a25f0a04SGreg Roach		// Replacing (or deleting) an existing fact
12134b9ff166SGreg Roach		foreach ($this->getFacts(null, false, Auth::PRIV_HIDE) as $fact) {
1214a25f0a04SGreg Roach			if (!$fact->isPendingDeletion()) {
1215a25f0a04SGreg Roach				if ($fact->getFactId() === $fact_id) {
1216a25f0a04SGreg Roach					if ($gedcom) {
1217a25f0a04SGreg Roach						$new_gedcom .= "\n" . $gedcom;
1218a25f0a04SGreg Roach					}
1219a25f0a04SGreg Roach					$fact_id = true; // Only replace/delete one copy of a duplicate fact
1220a25f0a04SGreg Roach				} elseif ($fact->getTag() != 'CHAN' || !$update_chan) {
1221a25f0a04SGreg Roach					$new_gedcom .= "\n" . $fact->getGedcom();
1222a25f0a04SGreg Roach				}
1223a25f0a04SGreg Roach			}
1224a25f0a04SGreg Roach		}
1225a25f0a04SGreg Roach		if ($update_chan) {
12264e9cf5abSGreg Roach			$new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
1227a25f0a04SGreg Roach		}
1228a25f0a04SGreg Roach
1229a25f0a04SGreg Roach		// Adding a new fact
1230a25f0a04SGreg Roach		if (!$fact_id) {
1231a25f0a04SGreg Roach			$new_gedcom .= "\n" . $gedcom;
1232a25f0a04SGreg Roach		}
1233a25f0a04SGreg Roach
1234a25f0a04SGreg Roach		if ($new_gedcom != $old_gedcom) {
1235a25f0a04SGreg Roach			// Save the changes
1236a25f0a04SGreg Roach			Database::prepare(
1237a25f0a04SGreg Roach				"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, ?, ?)"
123813abd6f3SGreg Roach			)->execute([
1239518bbdc1SGreg Roach				$this->tree->getTreeId(),
1240a25f0a04SGreg Roach				$this->xref,
1241a25f0a04SGreg Roach				$old_gedcom,
1242a25f0a04SGreg Roach				$new_gedcom,
1243cbc1590aSGreg Roach				Auth::id(),
124413abd6f3SGreg Roach			]);
1245a25f0a04SGreg Roach
1246a25f0a04SGreg Roach			$this->pending = $new_gedcom;
1247a25f0a04SGreg Roach
1248a25f0a04SGreg Roach			if (Auth::user()->getPreference('auto_accept')) {
12493d7a8a4cSGreg Roach				FunctionsImport::acceptAllChanges($this->xref, $this->tree->getTreeId());
1250a25f0a04SGreg Roach				$this->gedcom  = $new_gedcom;
1251a25f0a04SGreg Roach				$this->pending = null;
1252a25f0a04SGreg Roach			}
1253a25f0a04SGreg Roach		}
1254a25f0a04SGreg Roach		$this->parseFacts();
1255a25f0a04SGreg Roach	}
1256a25f0a04SGreg Roach
1257a25f0a04SGreg Roach	/**
1258a25f0a04SGreg Roach	 * Update this record
1259a25f0a04SGreg Roach	 *
1260a25f0a04SGreg Roach	 * @param string $gedcom
1261cbc1590aSGreg Roach	 * @param bool   $update_chan
1262a25f0a04SGreg Roach	 */
1263a25f0a04SGreg Roach	public function updateRecord($gedcom, $update_chan) {
1264a25f0a04SGreg Roach		// MSDOS line endings will break things in horrible ways
1265a25f0a04SGreg Roach		$gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
1266a25f0a04SGreg Roach		$gedcom = trim($gedcom);
1267a25f0a04SGreg Roach
1268a25f0a04SGreg Roach		// Update the CHAN record
1269a25f0a04SGreg Roach		if ($update_chan) {
1270a25f0a04SGreg Roach			$gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom);
1271a25f0a04SGreg Roach			$gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
1272a25f0a04SGreg Roach		}
1273a25f0a04SGreg Roach
1274a25f0a04SGreg Roach		// Create a pending change
1275a25f0a04SGreg Roach		Database::prepare(
1276a25f0a04SGreg Roach			"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, ?, ?)"
127713abd6f3SGreg Roach		)->execute([
1278518bbdc1SGreg Roach			$this->tree->getTreeId(),
1279a25f0a04SGreg Roach			$this->xref,
1280a25f0a04SGreg Roach			$this->getGedcom(),
1281a25f0a04SGreg Roach			$gedcom,
1282cbc1590aSGreg Roach			Auth::id(),
128313abd6f3SGreg Roach		]);
1284a25f0a04SGreg Roach
1285a25f0a04SGreg Roach		// Clear the cache
1286a25f0a04SGreg Roach		$this->pending = $gedcom;
1287a25f0a04SGreg Roach
1288a25f0a04SGreg Roach		// Accept this pending change
1289a25f0a04SGreg Roach		if (Auth::user()->getPreference('auto_accept')) {
12903d7a8a4cSGreg Roach			FunctionsImport::acceptAllChanges($this->xref, $this->tree->getTreeId());
1291a25f0a04SGreg Roach			$this->gedcom  = $gedcom;
1292a25f0a04SGreg Roach			$this->pending = null;
1293a25f0a04SGreg Roach		}
1294a25f0a04SGreg Roach
1295a25f0a04SGreg Roach		$this->parseFacts();
1296a25f0a04SGreg Roach
1297a25f0a04SGreg Roach		Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref);
1298a25f0a04SGreg Roach	}
1299a25f0a04SGreg Roach
1300a25f0a04SGreg Roach	/**
1301a25f0a04SGreg Roach	 * Delete this record
1302a25f0a04SGreg Roach	 */
1303a25f0a04SGreg Roach	public function deleteRecord() {
1304a25f0a04SGreg Roach		// Create a pending change
13054c0b5256SGreg Roach		if (!$this->isPendingDeletion()) {
1306a25f0a04SGreg Roach			Database::prepare(
1307a25f0a04SGreg Roach				"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, '', ?)"
130813abd6f3SGreg Roach			)->execute([
1309518bbdc1SGreg Roach				$this->tree->getTreeId(),
1310a25f0a04SGreg Roach				$this->xref,
1311a25f0a04SGreg Roach				$this->getGedcom(),
1312a25f0a04SGreg Roach				Auth::id(),
131313abd6f3SGreg Roach			]);
13144c0b5256SGreg Roach		}
1315a25f0a04SGreg Roach
13164c0b5256SGreg Roach		// Auto-accept this pending change
1317a25f0a04SGreg Roach		if (Auth::user()->getPreference('auto_accept')) {
13183d7a8a4cSGreg Roach			FunctionsImport::acceptAllChanges($this->xref, $this->tree->getTreeId());
1319a25f0a04SGreg Roach		}
1320a25f0a04SGreg Roach
1321a25f0a04SGreg Roach		// Clear the cache
1322a25f0a04SGreg Roach		self::$gedcom_record_cache  = null;
1323a25f0a04SGreg Roach		self::$pending_record_cache = null;
1324a25f0a04SGreg Roach
1325a25f0a04SGreg Roach		Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref);
1326a25f0a04SGreg Roach	}
1327a25f0a04SGreg Roach
1328a25f0a04SGreg Roach	/**
1329a25f0a04SGreg Roach	 * Remove all links from this record to $xref
1330a25f0a04SGreg Roach	 *
1331a25f0a04SGreg Roach	 * @param string $xref
1332cbc1590aSGreg Roach	 * @param bool   $update_chan
1333a25f0a04SGreg Roach	 */
1334a25f0a04SGreg Roach	public function removeLinks($xref, $update_chan) {
1335a25f0a04SGreg Roach		$value = '@' . $xref . '@';
1336a25f0a04SGreg Roach
1337a25f0a04SGreg Roach		foreach ($this->getFacts() as $fact) {
1338baacc364SGreg Roach			if ($fact->getValue() === $value) {
1339a25f0a04SGreg Roach				$this->deleteFact($fact->getFactId(), $update_chan);
1340a25f0a04SGreg Roach			} elseif (preg_match_all('/\n(\d) ' . WT_REGEX_TAG . ' ' . $value . '/', $fact->getGedcom(), $matches, PREG_SET_ORDER)) {
1341a25f0a04SGreg Roach				$gedcom = $fact->getGedcom();
1342a25f0a04SGreg Roach				foreach ($matches as $match) {
1343a25f0a04SGreg Roach					$next_level  = $match[1] + 1;
1344a25f0a04SGreg Roach					$next_levels = '[' . $next_level . '-9]';
1345a25f0a04SGreg Roach					$gedcom      = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom);
1346a25f0a04SGreg Roach				}
1347a25f0a04SGreg Roach				$this->updateFact($fact->getFactId(), $gedcom, $update_chan);
1348a25f0a04SGreg Roach			}
1349a25f0a04SGreg Roach		}
1350a25f0a04SGreg Roach	}
1351a25f0a04SGreg Roach}
1352