xref: /webtrees/app/Tree.php (revision e5588fb099ce26dcdcb7faeb13861818cd9a4306)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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;
17a25f0a04SGreg Roach
183d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
193d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport;
200f471f91SGreg Roachuse Fisharebest\Webtrees\Gedcom;
21a25f0a04SGreg Roachuse PDOException;
22a25f0a04SGreg Roach
23a25f0a04SGreg Roach/**
2476692c8bSGreg Roach * Provide an interface to the wt_gedcom table.
25a25f0a04SGreg Roach */
26a25f0a04SGreg Roachclass Tree {
27cbc1590aSGreg Roach	/** @var int The tree's ID number */
28518bbdc1SGreg Roach	private $tree_id;
29518bbdc1SGreg Roach
30a25f0a04SGreg Roach	/** @var string The tree's name */
31a25f0a04SGreg Roach	private $name;
32518bbdc1SGreg Roach
33a25f0a04SGreg Roach	/** @var string The tree's title */
34a25f0a04SGreg Roach	private $title;
35a25f0a04SGreg Roach
36e2052359SGreg Roach	/** @var int[] Default access rules for facts in this tree */
37518bbdc1SGreg Roach	private $fact_privacy;
38518bbdc1SGreg Roach
39e2052359SGreg Roach	/** @var int[] Default access rules for individuals in this tree */
40518bbdc1SGreg Roach	private $individual_privacy;
41518bbdc1SGreg Roach
42518bbdc1SGreg Roach	/** @var integer[][] Default access rules for individual facts in this tree */
43518bbdc1SGreg Roach	private $individual_fact_privacy;
44518bbdc1SGreg Roach
45a25f0a04SGreg Roach	/** @var Tree[] All trees that we have permission to see. */
46a25f0a04SGreg Roach	private static $trees;
47a25f0a04SGreg Roach
48a25f0a04SGreg Roach	/** @var string[] Cached copy of the wt_gedcom_setting table. */
4915d603e7SGreg Roach	private $preferences = [];
50a25f0a04SGreg Roach
51a25f0a04SGreg Roach	/** @var string[][] Cached copy of the wt_user_gedcom_setting table. */
5213abd6f3SGreg Roach	private $user_preferences = [];
53a25f0a04SGreg Roach
54a25f0a04SGreg Roach	/**
55a25f0a04SGreg Roach	 * Create a tree object. This is a private constructor - it can only
56a25f0a04SGreg Roach	 * be called from Tree::getAll() to ensure proper initialisation.
57a25f0a04SGreg Roach	 *
58cbc1590aSGreg Roach	 * @param int    $tree_id
59a25f0a04SGreg Roach	 * @param string $tree_name
60a25f0a04SGreg Roach	 * @param string $tree_title
61a25f0a04SGreg Roach	 */
62518bbdc1SGreg Roach	private function __construct($tree_id, $tree_name, $tree_title) {
63518bbdc1SGreg Roach		$this->tree_id                 = $tree_id;
64a25f0a04SGreg Roach		$this->name                    = $tree_name;
65a25f0a04SGreg Roach		$this->title                   = $tree_title;
6613abd6f3SGreg Roach		$this->fact_privacy            = [];
6713abd6f3SGreg Roach		$this->individual_privacy      = [];
6813abd6f3SGreg Roach		$this->individual_fact_privacy = [];
69518bbdc1SGreg Roach
70518bbdc1SGreg Roach		// Load the privacy settings for this tree
71518bbdc1SGreg Roach		$rows = Database::prepare(
72*e5588fb0SGreg Roach			"SELECT xref, tag_type, CASE resn WHEN 'none' THEN :priv_public WHEN 'privacy' THEN :priv_user WHEN 'confidential' THEN :priv_none WHEN 'hidden' THEN :priv_hide END AS resn" .
73518bbdc1SGreg Roach			" FROM `##default_resn` WHERE gedcom_id = :tree_id"
7413abd6f3SGreg Roach		)->execute([
754b9ff166SGreg Roach			'priv_public' => Auth::PRIV_PRIVATE,
764b9ff166SGreg Roach			'priv_user'   => Auth::PRIV_USER,
774b9ff166SGreg Roach			'priv_none'   => Auth::PRIV_NONE,
784b9ff166SGreg Roach			'priv_hide'   => Auth::PRIV_HIDE,
79cbc1590aSGreg Roach			'tree_id'     => $this->tree_id,
8013abd6f3SGreg Roach		])->fetchAll();
81518bbdc1SGreg Roach
82518bbdc1SGreg Roach		foreach ($rows as $row) {
83518bbdc1SGreg Roach			if ($row->xref !== null) {
84518bbdc1SGreg Roach				if ($row->tag_type !== null) {
85518bbdc1SGreg Roach					$this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn;
86518bbdc1SGreg Roach				} else {
87518bbdc1SGreg Roach					$this->individual_privacy[$row->xref] = (int) $row->resn;
88518bbdc1SGreg Roach				}
89518bbdc1SGreg Roach			} else {
90518bbdc1SGreg Roach				$this->fact_privacy[$row->tag_type] = (int) $row->resn;
91518bbdc1SGreg Roach			}
92518bbdc1SGreg Roach		}
93a25f0a04SGreg Roach	}
94a25f0a04SGreg Roach
95a25f0a04SGreg Roach	/**
96a25f0a04SGreg Roach	 * The ID of this tree
97a25f0a04SGreg Roach	 *
98cbc1590aSGreg Roach	 * @return int
99a25f0a04SGreg Roach	 */
100518bbdc1SGreg Roach	public function getTreeId() {
101518bbdc1SGreg Roach		return $this->tree_id;
102a25f0a04SGreg Roach	}
103a25f0a04SGreg Roach
104a25f0a04SGreg Roach	/**
105a25f0a04SGreg Roach	 * The name of this tree
106a25f0a04SGreg Roach	 *
107a25f0a04SGreg Roach	 * @return string
108a25f0a04SGreg Roach	 */
109000959d9SGreg Roach	public function getName() {
110a25f0a04SGreg Roach		return $this->name;
111a25f0a04SGreg Roach	}
112a25f0a04SGreg Roach
113a25f0a04SGreg Roach	/**
114a25f0a04SGreg Roach	 * The name of this tree
115a25f0a04SGreg Roach	 *
116a25f0a04SGreg Roach	 * @return string
117a25f0a04SGreg Roach	 */
118000959d9SGreg Roach	public function getNameUrl() {
11985ccaa18SGreg Roach		return rawurlencode($this->name);
120a25f0a04SGreg Roach	}
121a25f0a04SGreg Roach
122a25f0a04SGreg Roach	/**
123a25f0a04SGreg Roach	 * The title of this tree
124a25f0a04SGreg Roach	 *
125a25f0a04SGreg Roach	 * @return string
126a25f0a04SGreg Roach	 */
127000959d9SGreg Roach	public function getTitle() {
128a25f0a04SGreg Roach		return $this->title;
129a25f0a04SGreg Roach	}
130a25f0a04SGreg Roach
131a25f0a04SGreg Roach	/**
132518bbdc1SGreg Roach	 * The fact-level privacy for this tree.
133518bbdc1SGreg Roach	 *
134e2052359SGreg Roach	 * @return int[]
135518bbdc1SGreg Roach	 */
136518bbdc1SGreg Roach	public function getFactPrivacy() {
137518bbdc1SGreg Roach		return $this->fact_privacy;
138518bbdc1SGreg Roach	}
139518bbdc1SGreg Roach
140518bbdc1SGreg Roach	/**
141518bbdc1SGreg Roach	 * The individual-level privacy for this tree.
142518bbdc1SGreg Roach	 *
143e2052359SGreg Roach	 * @return int[]
144518bbdc1SGreg Roach	 */
145518bbdc1SGreg Roach	public function getIndividualPrivacy() {
146518bbdc1SGreg Roach		return $this->individual_privacy;
147518bbdc1SGreg Roach	}
148518bbdc1SGreg Roach
149518bbdc1SGreg Roach	/**
150518bbdc1SGreg Roach	 * The individual-fact-level privacy for this tree.
151518bbdc1SGreg Roach	 *
152adac8af1SGreg Roach	 * @return int[][]
153518bbdc1SGreg Roach	 */
154518bbdc1SGreg Roach	public function getIndividualFactPrivacy() {
155518bbdc1SGreg Roach		return $this->individual_fact_privacy;
156518bbdc1SGreg Roach	}
157518bbdc1SGreg Roach
158518bbdc1SGreg Roach	/**
159a25f0a04SGreg Roach	 * Get the tree’s configuration settings.
160a25f0a04SGreg Roach	 *
161a25f0a04SGreg Roach	 * @param string $setting_name
16215d603e7SGreg Roach	 * @param string $default
163a25f0a04SGreg Roach	 *
16415d603e7SGreg Roach	 * @return string
165a25f0a04SGreg Roach	 */
16615d603e7SGreg Roach	public function getPreference($setting_name, $default = '') {
16715d603e7SGreg Roach		if (empty($this->preferences)) {
168a25f0a04SGreg Roach			$this->preferences = Database::prepare(
169*e5588fb0SGreg Roach				"SELECT setting_name, setting_value FROM `##gedcom_setting` WHERE gedcom_id = ?"
17013abd6f3SGreg Roach			)->execute([$this->tree_id])->fetchAssoc();
171a25f0a04SGreg Roach		}
172a25f0a04SGreg Roach
173a25f0a04SGreg Roach		if (array_key_exists($setting_name, $this->preferences)) {
174a25f0a04SGreg Roach			return $this->preferences[$setting_name];
175a25f0a04SGreg Roach		} else {
176a25f0a04SGreg Roach			return $default;
177a25f0a04SGreg Roach		}
178a25f0a04SGreg Roach	}
179a25f0a04SGreg Roach
180a25f0a04SGreg Roach	/**
181a25f0a04SGreg Roach	 * Set the tree’s configuration settings.
182a25f0a04SGreg Roach	 *
183a25f0a04SGreg Roach	 * @param string $setting_name
184a25f0a04SGreg Roach	 * @param string $setting_value
185a25f0a04SGreg Roach	 *
186a25f0a04SGreg Roach	 * @return $this
187a25f0a04SGreg Roach	 */
188a25f0a04SGreg Roach	public function setPreference($setting_name, $setting_value) {
189a25f0a04SGreg Roach		if ($setting_value !== $this->getPreference($setting_name)) {
190a25f0a04SGreg Roach			Database::prepare(
191a25f0a04SGreg Roach				"REPLACE INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" .
192a25f0a04SGreg Roach				" VALUES (:tree_id, :setting_name, LEFT(:setting_value, 255))"
19313abd6f3SGreg Roach			)->execute([
194518bbdc1SGreg Roach				'tree_id'       => $this->tree_id,
195a25f0a04SGreg Roach				'setting_name'  => $setting_name,
196a25f0a04SGreg Roach				'setting_value' => $setting_value,
19713abd6f3SGreg Roach			]);
19815d603e7SGreg Roach
199a25f0a04SGreg Roach			$this->preferences[$setting_name] = $setting_value;
20015d603e7SGreg Roach
20172292b7dSGreg Roach			Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
202a25f0a04SGreg Roach		}
203a25f0a04SGreg Roach
204a25f0a04SGreg Roach		return $this;
205a25f0a04SGreg Roach	}
206a25f0a04SGreg Roach
207a25f0a04SGreg Roach	/**
208a25f0a04SGreg Roach	 * Get the tree’s user-configuration settings.
209a25f0a04SGreg Roach	 *
210a25f0a04SGreg Roach	 * @param User        $user
211a25f0a04SGreg Roach	 * @param string      $setting_name
212a25f0a04SGreg Roach	 * @param string|null $default
213a25f0a04SGreg Roach	 *
214a25f0a04SGreg Roach	 * @return string
215a25f0a04SGreg Roach	 */
216a25f0a04SGreg Roach	public function getUserPreference(User $user, $setting_name, $default = null) {
217a25f0a04SGreg Roach		// There are lots of settings, and we need to fetch lots of them on every page
218a25f0a04SGreg Roach		// so it is quicker to fetch them all in one go.
219a25f0a04SGreg Roach		if (!array_key_exists($user->getUserId(), $this->user_preferences)) {
220a25f0a04SGreg Roach			$this->user_preferences[$user->getUserId()] = Database::prepare(
221*e5588fb0SGreg Roach				"SELECT setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?"
22213abd6f3SGreg Roach			)->execute([$user->getUserId(), $this->tree_id])->fetchAssoc();
223a25f0a04SGreg Roach		}
224a25f0a04SGreg Roach
225a25f0a04SGreg Roach		if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) {
226a25f0a04SGreg Roach			return $this->user_preferences[$user->getUserId()][$setting_name];
227a25f0a04SGreg Roach		} else {
228a25f0a04SGreg Roach			return $default;
229a25f0a04SGreg Roach		}
230a25f0a04SGreg Roach	}
231a25f0a04SGreg Roach
232a25f0a04SGreg Roach	/**
233a25f0a04SGreg Roach	 * Set the tree’s user-configuration settings.
234a25f0a04SGreg Roach	 *
235a25f0a04SGreg Roach	 * @param User    $user
236a25f0a04SGreg Roach	 * @param string  $setting_name
237a25f0a04SGreg Roach	 * @param string  $setting_value
238a25f0a04SGreg Roach	 *
239a25f0a04SGreg Roach	 * @return $this
240a25f0a04SGreg Roach	 */
241a25f0a04SGreg Roach	public function setUserPreference(User $user, $setting_name, $setting_value) {
242a25f0a04SGreg Roach		if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
243a25f0a04SGreg Roach			// Update the database
244a25f0a04SGreg Roach			if ($setting_value === null) {
245a25f0a04SGreg Roach				Database::prepare(
246a25f0a04SGreg Roach					"DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name"
24713abd6f3SGreg Roach				)->execute([
248518bbdc1SGreg Roach					'tree_id'      => $this->tree_id,
249a25f0a04SGreg Roach					'user_id'      => $user->getUserId(),
250a25f0a04SGreg Roach					'setting_name' => $setting_name,
25113abd6f3SGreg Roach				]);
252a25f0a04SGreg Roach			} else {
253a25f0a04SGreg Roach				Database::prepare(
254a25f0a04SGreg Roach					"REPLACE INTO `##user_gedcom_setting` (user_id, gedcom_id, setting_name, setting_value) VALUES (:user_id, :tree_id, :setting_name, LEFT(:setting_value, 255))"
25513abd6f3SGreg Roach				)->execute([
256a25f0a04SGreg Roach					'user_id'       => $user->getUserId(),
257518bbdc1SGreg Roach					'tree_id'       => $this->tree_id,
258a25f0a04SGreg Roach					'setting_name'  => $setting_name,
259cbc1590aSGreg Roach					'setting_value' => $setting_value,
26013abd6f3SGreg Roach				]);
261a25f0a04SGreg Roach			}
262a25f0a04SGreg Roach			// Update our cache
263a25f0a04SGreg Roach			$this->user_preferences[$user->getUserId()][$setting_name] = $setting_value;
264a25f0a04SGreg Roach			// Audit log of changes
26572292b7dSGreg Roach			Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this);
266a25f0a04SGreg Roach		}
267a25f0a04SGreg Roach
268a25f0a04SGreg Roach		return $this;
269a25f0a04SGreg Roach	}
270a25f0a04SGreg Roach
271a25f0a04SGreg Roach	/**
272a25f0a04SGreg Roach	 * Can a user accept changes for this tree?
273a25f0a04SGreg Roach	 *
274a25f0a04SGreg Roach	 * @param User $user
275a25f0a04SGreg Roach	 *
276cbc1590aSGreg Roach	 * @return bool
277a25f0a04SGreg Roach	 */
278a25f0a04SGreg Roach	public function canAcceptChanges(User $user) {
279a25f0a04SGreg Roach		return Auth::isModerator($this, $user);
280a25f0a04SGreg Roach	}
281a25f0a04SGreg Roach
282a25f0a04SGreg Roach	/**
283a25f0a04SGreg Roach	 * Fetch all the trees that we have permission to access.
284a25f0a04SGreg Roach	 *
285a25f0a04SGreg Roach	 * @return Tree[]
286a25f0a04SGreg Roach	 */
287a25f0a04SGreg Roach	public static function getAll() {
288a25f0a04SGreg Roach		if (self::$trees === null) {
28913abd6f3SGreg Roach			self::$trees = [];
290a25f0a04SGreg Roach			$rows        = Database::prepare(
291*e5588fb0SGreg Roach				"SELECT g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" .
292a25f0a04SGreg Roach				" FROM `##gedcom` g" .
293a25f0a04SGreg Roach				" LEFT JOIN `##gedcom_setting`      gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" .
294a25f0a04SGreg Roach				" LEFT JOIN `##gedcom_setting`      gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" .
295a25f0a04SGreg Roach				" LEFT JOIN `##gedcom_setting`      gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" .
296a25f0a04SGreg Roach				" LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" .
297a25f0a04SGreg Roach				" WHERE " .
298a25f0a04SGreg Roach				"  g.gedcom_id>0 AND (" . // exclude the "template" tree
299a25f0a04SGreg Roach				"    EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all
300a25f0a04SGreg Roach				"   ) OR (" .
3018cca4ddeSGreg Roach				"    (gs2.setting_value = 1 OR ugs.setting_value = 'admin') AND (" . // Allow imported trees, with either:
302a25f0a04SGreg Roach				"     gs3.setting_value <> 1 OR" . // visitor access
303a25f0a04SGreg Roach				"     IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access
304a25f0a04SGreg Roach				"   )" .
305a25f0a04SGreg Roach				"  )" .
306a25f0a04SGreg Roach				" ORDER BY g.sort_order, 3"
30713abd6f3SGreg Roach			)->execute([Auth::id(), Auth::id()])->fetchAll();
308a25f0a04SGreg Roach			foreach ($rows as $row) {
3093f7ece23SGreg Roach				self::$trees[$row->tree_name] = new self((int) $row->tree_id, $row->tree_name, $row->tree_title);
310a25f0a04SGreg Roach			}
311a25f0a04SGreg Roach		}
312a25f0a04SGreg Roach
313a25f0a04SGreg Roach		return self::$trees;
314a25f0a04SGreg Roach	}
315a25f0a04SGreg Roach
316a25f0a04SGreg Roach	/**
317d2cdeb3fSGreg Roach	 * Find the tree with a specific ID.
318a25f0a04SGreg Roach	 *
319cbc1590aSGreg Roach	 * @param int $tree_id
320cbc1590aSGreg Roach	 *
321cbc1590aSGreg Roach	 * @throws \DomainException
322a25f0a04SGreg Roach	 *
323a25f0a04SGreg Roach	 * @return Tree
324a25f0a04SGreg Roach	 */
325d2cdeb3fSGreg Roach	public static function findById($tree_id) {
32651d0f842SGreg Roach		foreach (self::getAll() as $tree) {
327e568acceSGreg Roach			if ($tree->tree_id == $tree_id) {
32851d0f842SGreg Roach				return $tree;
32951d0f842SGreg Roach			}
33051d0f842SGreg Roach		}
33151d0f842SGreg Roach		throw new \DomainException;
332a25f0a04SGreg Roach	}
333a25f0a04SGreg Roach
334a25f0a04SGreg Roach	/**
335d2cdeb3fSGreg Roach	 * Find the tree with a specific name.
336cf4bcc09SGreg Roach	 *
337cf4bcc09SGreg Roach	 * @param string $tree_name
338cf4bcc09SGreg Roach	 *
339cf4bcc09SGreg Roach	 * @return Tree|null
340cf4bcc09SGreg Roach	 */
341cf4bcc09SGreg Roach	public static function findByName($tree_name) {
342cf4bcc09SGreg Roach		foreach (self::getAll() as $tree) {
34351d0f842SGreg Roach			if ($tree->name === $tree_name) {
344cf4bcc09SGreg Roach				return $tree;
345cf4bcc09SGreg Roach			}
346cf4bcc09SGreg Roach		}
347cf4bcc09SGreg Roach
348cf4bcc09SGreg Roach		return null;
349cf4bcc09SGreg Roach	}
350cf4bcc09SGreg Roach
351cf4bcc09SGreg Roach	/**
352a25f0a04SGreg Roach	 * Create arguments to select_edit_control()
353a25f0a04SGreg Roach	 * Note - these will be escaped later
354a25f0a04SGreg Roach	 *
355a25f0a04SGreg Roach	 * @return string[]
356a25f0a04SGreg Roach	 */
357a25f0a04SGreg Roach	public static function getIdList() {
35813abd6f3SGreg Roach		$list = [];
359a25f0a04SGreg Roach		foreach (self::getAll() as $tree) {
360518bbdc1SGreg Roach			$list[$tree->tree_id] = $tree->title;
361a25f0a04SGreg Roach		}
362a25f0a04SGreg Roach
363a25f0a04SGreg Roach		return $list;
364a25f0a04SGreg Roach	}
365a25f0a04SGreg Roach
366a25f0a04SGreg Roach	/**
367a25f0a04SGreg Roach	 * Create arguments to select_edit_control()
368a25f0a04SGreg Roach	 * Note - these will be escaped later
369a25f0a04SGreg Roach	 *
370a25f0a04SGreg Roach	 * @return string[]
371a25f0a04SGreg Roach	 */
372a25f0a04SGreg Roach	public static function getNameList() {
37313abd6f3SGreg Roach		$list = [];
374a25f0a04SGreg Roach		foreach (self::getAll() as $tree) {
375a25f0a04SGreg Roach			$list[$tree->name] = $tree->title;
376a25f0a04SGreg Roach		}
377a25f0a04SGreg Roach
378a25f0a04SGreg Roach		return $list;
379a25f0a04SGreg Roach	}
380a25f0a04SGreg Roach
381a25f0a04SGreg Roach	/**
382a25f0a04SGreg Roach	 * Create a new tree
383a25f0a04SGreg Roach	 *
384a25f0a04SGreg Roach	 * @param string $tree_name
385a25f0a04SGreg Roach	 * @param string $tree_title
386a25f0a04SGreg Roach	 *
387a25f0a04SGreg Roach	 * @return Tree
388a25f0a04SGreg Roach	 */
389a25f0a04SGreg Roach	public static function create($tree_name, $tree_title) {
390a25f0a04SGreg Roach		try {
391a25f0a04SGreg Roach			// Create a new tree
392a25f0a04SGreg Roach			Database::prepare(
393a25f0a04SGreg Roach				"INSERT INTO `##gedcom` (gedcom_name) VALUES (?)"
39413abd6f3SGreg Roach			)->execute([$tree_name]);
395a25f0a04SGreg Roach			$tree_id = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne();
396a25f0a04SGreg Roach		} catch (PDOException $ex) {
397bd52fa32SGreg Roach			DebugBar::addThrowable($ex);
398bd52fa32SGreg Roach
399a25f0a04SGreg Roach			// A tree with that name already exists?
400ef2fd529SGreg Roach			return self::findByName($tree_name);
401a25f0a04SGreg Roach		}
402a25f0a04SGreg Roach
403a25f0a04SGreg Roach		// Update the list of trees - to include this new one
404a25f0a04SGreg Roach		self::$trees = null;
405d2cdeb3fSGreg Roach		$tree        = self::findById($tree_id);
406a25f0a04SGreg Roach
407a25f0a04SGreg Roach		$tree->setPreference('imported', '0');
408a25f0a04SGreg Roach		$tree->setPreference('title', $tree_title);
409a25f0a04SGreg Roach
410a25f0a04SGreg Roach		// Module privacy
411a25f0a04SGreg Roach		Module::setDefaultAccess($tree_id);
412a25f0a04SGreg Roach
4131507cbcaSGreg Roach		// Set preferences from default tree
4141507cbcaSGreg Roach		Database::prepare(
4151507cbcaSGreg Roach			"INSERT INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" .
4161507cbcaSGreg Roach			" SELECT :tree_id, setting_name, setting_value" .
4171507cbcaSGreg Roach			" FROM `##gedcom_setting` WHERE gedcom_id = -1"
41813abd6f3SGreg Roach		)->execute([
4191507cbcaSGreg Roach			'tree_id' => $tree_id,
42013abd6f3SGreg Roach		]);
4211507cbcaSGreg Roach
4221507cbcaSGreg Roach		Database::prepare(
4231507cbcaSGreg Roach			"INSERT INTO `##default_resn` (gedcom_id, tag_type, resn)" .
4241507cbcaSGreg Roach			" SELECT :tree_id, tag_type, resn" .
4251507cbcaSGreg Roach			" FROM `##default_resn` WHERE gedcom_id = -1"
42613abd6f3SGreg Roach		)->execute([
4271507cbcaSGreg Roach			'tree_id' => $tree_id,
42813abd6f3SGreg Roach		]);
4291507cbcaSGreg Roach
4301507cbcaSGreg Roach		Database::prepare(
4311507cbcaSGreg Roach			"INSERT INTO `##block` (gedcom_id, location, block_order, module_name)" .
4321507cbcaSGreg Roach			" SELECT :tree_id, location, block_order, module_name" .
4331507cbcaSGreg Roach			" FROM `##block` WHERE gedcom_id = -1"
43413abd6f3SGreg Roach		)->execute([
4351507cbcaSGreg Roach			'tree_id' => $tree_id,
43613abd6f3SGreg Roach		]);
4371507cbcaSGreg Roach
438a25f0a04SGreg Roach		// Gedcom and privacy settings
439a25f0a04SGreg Roach		$tree->setPreference('CONTACT_USER_ID', Auth::id());
4401507cbcaSGreg Roach		$tree->setPreference('WEBMASTER_USER_ID', Auth::id());
441a25f0a04SGreg Roach		$tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language
442a25f0a04SGreg Roach		switch (WT_LOCALE) {
443a25f0a04SGreg Roach			case 'es':
444a25f0a04SGreg Roach				$tree->setPreference('SURNAME_TRADITION', 'spanish');
445a25f0a04SGreg Roach				break;
446a25f0a04SGreg Roach			case 'is':
447a25f0a04SGreg Roach				$tree->setPreference('SURNAME_TRADITION', 'icelandic');
448a25f0a04SGreg Roach				break;
449a25f0a04SGreg Roach			case 'lt':
450a25f0a04SGreg Roach				$tree->setPreference('SURNAME_TRADITION', 'lithuanian');
451a25f0a04SGreg Roach				break;
452a25f0a04SGreg Roach			case 'pl':
453a25f0a04SGreg Roach				$tree->setPreference('SURNAME_TRADITION', 'polish');
454a25f0a04SGreg Roach				break;
455a25f0a04SGreg Roach			case 'pt':
456a25f0a04SGreg Roach			case 'pt-BR':
457a25f0a04SGreg Roach				$tree->setPreference('SURNAME_TRADITION', 'portuguese');
458a25f0a04SGreg Roach				break;
459a25f0a04SGreg Roach			default:
460a25f0a04SGreg Roach				$tree->setPreference('SURNAME_TRADITION', 'paternal');
461a25f0a04SGreg Roach				break;
462a25f0a04SGreg Roach		}
463a25f0a04SGreg Roach
464a25f0a04SGreg Roach		// Genealogy data
465a25f0a04SGreg Roach		// It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...
4660fd39724SGreg Roach		$john_doe = /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ I18N::translate('John /DOE/');
46777e70a22SGreg Roach		$note     = I18N::translate('Edit this individual and replace their details with your own.');
46813abd6f3SGreg Roach		Database::prepare("INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)")->execute([
469a25f0a04SGreg Roach			$tree_id,
470cbc1590aSGreg Roach			"0 HEAD\n1 CHAR UTF-8\n0 @I1@ INDI\n1 NAME {$john_doe}\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE {$note}\n0 TRLR\n",
47113abd6f3SGreg Roach		]);
472a25f0a04SGreg Roach
473a25f0a04SGreg Roach		// Update our cache
474518bbdc1SGreg Roach		self::$trees[$tree->tree_id] = $tree;
475a25f0a04SGreg Roach
476a25f0a04SGreg Roach		return $tree;
477a25f0a04SGreg Roach	}
478a25f0a04SGreg Roach
479a25f0a04SGreg Roach	/**
480b78374c5SGreg Roach	 * Are there any pending edits for this tree, than need reviewing by a moderator.
481b78374c5SGreg Roach	 *
482b78374c5SGreg Roach	 * @return bool
483b78374c5SGreg Roach	 */
484b78374c5SGreg Roach	public function hasPendingEdit() {
485b78374c5SGreg Roach		return (bool) Database::prepare(
486b78374c5SGreg Roach			"SELECT 1 FROM `##change` WHERE status = 'pending' AND gedcom_id = :tree_id"
48713abd6f3SGreg Roach		)->execute([
488cbc1590aSGreg Roach			'tree_id' => $this->tree_id,
48913abd6f3SGreg Roach		])->fetchOne();
490b78374c5SGreg Roach	}
491b78374c5SGreg Roach
492b78374c5SGreg Roach	/**
493a25f0a04SGreg Roach	 * Delete all the genealogy data from a tree - in preparation for importing
494a25f0a04SGreg Roach	 * new data. Optionally retain the media data, for when the user has been
495a25f0a04SGreg Roach	 * editing their data offline using an application which deletes (or does not
496a25f0a04SGreg Roach	 * support) media data.
497a25f0a04SGreg Roach	 *
498a25f0a04SGreg Roach	 * @param bool $keep_media
499a25f0a04SGreg Roach	 */
500a25f0a04SGreg Roach	public function deleteGenealogyData($keep_media) {
50113abd6f3SGreg Roach		Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]);
50213abd6f3SGreg Roach		Database::prepare("DELETE FROM `##individuals`  WHERE i_file    = ?")->execute([$this->tree_id]);
50313abd6f3SGreg Roach		Database::prepare("DELETE FROM `##families`     WHERE f_file    = ?")->execute([$this->tree_id]);
50413abd6f3SGreg Roach		Database::prepare("DELETE FROM `##sources`      WHERE s_file    = ?")->execute([$this->tree_id]);
50513abd6f3SGreg Roach		Database::prepare("DELETE FROM `##other`        WHERE o_file    = ?")->execute([$this->tree_id]);
50613abd6f3SGreg Roach		Database::prepare("DELETE FROM `##places`       WHERE p_file    = ?")->execute([$this->tree_id]);
50713abd6f3SGreg Roach		Database::prepare("DELETE FROM `##placelinks`   WHERE pl_file   = ?")->execute([$this->tree_id]);
50813abd6f3SGreg Roach		Database::prepare("DELETE FROM `##name`         WHERE n_file    = ?")->execute([$this->tree_id]);
50913abd6f3SGreg Roach		Database::prepare("DELETE FROM `##dates`        WHERE d_file    = ?")->execute([$this->tree_id]);
51013abd6f3SGreg Roach		Database::prepare("DELETE FROM `##change`       WHERE gedcom_id = ?")->execute([$this->tree_id]);
511a25f0a04SGreg Roach
512a25f0a04SGreg Roach		if ($keep_media) {
51313abd6f3SGreg Roach			Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute([$this->tree_id]);
514a25f0a04SGreg Roach		} else {
51513abd6f3SGreg Roach			Database::prepare("DELETE FROM `##link`  WHERE l_file =?")->execute([$this->tree_id]);
51613abd6f3SGreg Roach			Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute([$this->tree_id]);
51723ed9d24SGreg Roach			Database::prepare("DELETE FROM `##media_file` WHERE m_file =?")->execute([$this->tree_id]);
518a25f0a04SGreg Roach		}
519a25f0a04SGreg Roach	}
520a25f0a04SGreg Roach
521a25f0a04SGreg Roach	/**
522a25f0a04SGreg Roach	 * Delete everything relating to a tree
523a25f0a04SGreg Roach	 */
524a25f0a04SGreg Roach	public function delete() {
525a25f0a04SGreg Roach		// If this is the default tree, then unset it
526ef2fd529SGreg Roach		if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) {
527a25f0a04SGreg Roach			Site::setPreference('DEFAULT_GEDCOM', '');
528a25f0a04SGreg Roach		}
529a25f0a04SGreg Roach
530a25f0a04SGreg Roach		$this->deleteGenealogyData(false);
531a25f0a04SGreg Roach
53213abd6f3SGreg Roach		Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute([$this->tree_id]);
53313abd6f3SGreg Roach		Database::prepare("DELETE FROM `##block`               WHERE gedcom_id = ?")->execute([$this->tree_id]);
53413abd6f3SGreg Roach		Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]);
53513abd6f3SGreg Roach		Database::prepare("DELETE FROM `##gedcom_setting`      WHERE gedcom_id = ?")->execute([$this->tree_id]);
53613abd6f3SGreg Roach		Database::prepare("DELETE FROM `##module_privacy`      WHERE gedcom_id = ?")->execute([$this->tree_id]);
53713abd6f3SGreg Roach		Database::prepare("DELETE FROM `##hit_counter`         WHERE gedcom_id = ?")->execute([$this->tree_id]);
53813abd6f3SGreg Roach		Database::prepare("DELETE FROM `##default_resn`        WHERE gedcom_id = ?")->execute([$this->tree_id]);
53913abd6f3SGreg Roach		Database::prepare("DELETE FROM `##gedcom_chunk`        WHERE gedcom_id = ?")->execute([$this->tree_id]);
54013abd6f3SGreg Roach		Database::prepare("DELETE FROM `##log`                 WHERE gedcom_id = ?")->execute([$this->tree_id]);
54113abd6f3SGreg Roach		Database::prepare("DELETE FROM `##gedcom`              WHERE gedcom_id = ?")->execute([$this->tree_id]);
542a25f0a04SGreg Roach
543a25f0a04SGreg Roach		// After updating the database, we need to fetch a new (sorted) copy
544a25f0a04SGreg Roach		self::$trees = null;
545a25f0a04SGreg Roach	}
546a25f0a04SGreg Roach
547a25f0a04SGreg Roach	/**
548a25f0a04SGreg Roach	 * Export the tree to a GEDCOM file
549a25f0a04SGreg Roach	 *
5505792757eSGreg Roach	 * @param resource $stream
551a25f0a04SGreg Roach	 */
5525792757eSGreg Roach	public function exportGedcom($stream) {
5535792757eSGreg Roach		$stmt = Database::prepare(
554e56ef01aSGreg Roach			"SELECT i_gedcom AS gedcom, i_id AS xref, 1 AS n FROM `##individuals` WHERE i_file = :tree_id_1" .
5555792757eSGreg Roach			" UNION ALL " .
556e56ef01aSGreg Roach			"SELECT f_gedcom AS gedcom, f_id AS xref, 2 AS n FROM `##families`    WHERE f_file = :tree_id_2" .
5575792757eSGreg Roach			" UNION ALL " .
558e56ef01aSGreg Roach			"SELECT s_gedcom AS gedcom, s_id AS xref, 3 AS n FROM `##sources`     WHERE s_file = :tree_id_3" .
5595792757eSGreg Roach			" UNION ALL " .
560e56ef01aSGreg Roach			"SELECT o_gedcom AS gedcom, o_id AS xref, 4 AS n FROM `##other`       WHERE o_file = :tree_id_4 AND o_type NOT IN ('HEAD', 'TRLR')" .
5615792757eSGreg Roach			" UNION ALL " .
562e56ef01aSGreg Roach			"SELECT m_gedcom AS gedcom, m_id AS xref, 5 AS n FROM `##media`       WHERE m_file = :tree_id_5" .
563e56ef01aSGreg Roach			" ORDER BY n, LENGTH(xref), xref"
56413abd6f3SGreg Roach		)->execute([
5655792757eSGreg Roach			'tree_id_1' => $this->tree_id,
5665792757eSGreg Roach			'tree_id_2' => $this->tree_id,
5675792757eSGreg Roach			'tree_id_3' => $this->tree_id,
5685792757eSGreg Roach			'tree_id_4' => $this->tree_id,
569cbc1590aSGreg Roach			'tree_id_5' => $this->tree_id,
57013abd6f3SGreg Roach		]);
571a25f0a04SGreg Roach
5723d7a8a4cSGreg Roach		$buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this));
573a214e186SGreg Roach		while (($row = $stmt->fetch()) !== false) {
5743d7a8a4cSGreg Roach			$buffer .= FunctionsExport::reformatRecord($row->gedcom);
575a25f0a04SGreg Roach			if (strlen($buffer) > 65535) {
5765792757eSGreg Roach				fwrite($stream, $buffer);
577a25f0a04SGreg Roach				$buffer = '';
578a25f0a04SGreg Roach			}
579a25f0a04SGreg Roach		}
5800f471f91SGreg Roach		fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL);
581195d09d8SGreg Roach		$stmt->closeCursor();
582a25f0a04SGreg Roach	}
583a25f0a04SGreg Roach
584a25f0a04SGreg Roach	/**
585a25f0a04SGreg Roach	 * Import data from a gedcom file into this tree.
586a25f0a04SGreg Roach	 *
587a25f0a04SGreg Roach	 * @param string  $path       The full path to the (possibly temporary) file.
588a25f0a04SGreg Roach	 * @param string  $filename   The preferred filename, for export/download.
589a25f0a04SGreg Roach	 *
590a25f0a04SGreg Roach	 * @throws \Exception
591a25f0a04SGreg Roach	 */
592ed1bbedbSGreg Roach	public function importGedcomFile($path, $filename) {
593a25f0a04SGreg Roach		// Read the file in blocks of roughly 64K. Ensure that each block
594a25f0a04SGreg Roach		// contains complete gedcom records. This will ensure we don’t split
595a25f0a04SGreg Roach		// multi-byte characters, as well as simplifying the code to import
596a25f0a04SGreg Roach		// each block.
597a25f0a04SGreg Roach
598a25f0a04SGreg Roach		$file_data = '';
599a25f0a04SGreg Roach		$fp        = fopen($path, 'rb');
600a25f0a04SGreg Roach
601a25f0a04SGreg Roach		// Don’t allow the user to cancel the request. We do not want to be left with an incomplete transaction.
602a25f0a04SGreg Roach		ignore_user_abort(true);
603a25f0a04SGreg Roach
604ed1bbedbSGreg Roach		$this->deleteGenealogyData($this->getPreference('keep_media'));
605a25f0a04SGreg Roach		$this->setPreference('gedcom_filename', $filename);
606a25f0a04SGreg Roach		$this->setPreference('imported', '0');
607a25f0a04SGreg Roach
608a25f0a04SGreg Roach		while (!feof($fp)) {
609a25f0a04SGreg Roach			$file_data .= fread($fp, 65536);
610a25f0a04SGreg Roach			// There is no strrpos() function that searches for substrings :-(
611a25f0a04SGreg Roach			for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) {
612a25f0a04SGreg Roach				if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) {
613a25f0a04SGreg Roach					// We’ve found the last record boundary in this chunk of data
614a25f0a04SGreg Roach					break;
615a25f0a04SGreg Roach				}
616a25f0a04SGreg Roach			}
617a25f0a04SGreg Roach			if ($pos) {
618a25f0a04SGreg Roach				Database::prepare(
619a25f0a04SGreg Roach					"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
62013abd6f3SGreg Roach				)->execute([$this->tree_id, substr($file_data, 0, $pos)]);
621a25f0a04SGreg Roach				$file_data = substr($file_data, $pos);
622a25f0a04SGreg Roach			}
623a25f0a04SGreg Roach		}
624a25f0a04SGreg Roach		Database::prepare(
625a25f0a04SGreg Roach			"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
62613abd6f3SGreg Roach		)->execute([$this->tree_id, $file_data]);
627a25f0a04SGreg Roach
628a25f0a04SGreg Roach		fclose($fp);
629a25f0a04SGreg Roach	}
630304f20d5SGreg Roach
631304f20d5SGreg Roach	/**
632b90d8accSGreg Roach	 * Generate a new XREF, unique across all family trees
633b90d8accSGreg Roach	 *
634b90d8accSGreg Roach	 * @return string
635b90d8accSGreg Roach	 */
636a214e186SGreg Roach	public function getNewXref() {
637a214e186SGreg Roach		$prefix = 'X';
638b90d8accSGreg Roach
639971d66c8SGreg Roach		$increment = 1.0;
640b90d8accSGreg Roach		do {
641b90d8accSGreg Roach			// Use LAST_INSERT_ID(expr) to provide a transaction-safe sequence. See
642b90d8accSGreg Roach			// http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
643b90d8accSGreg Roach			$statement = Database::prepare(
644a214e186SGreg Roach				"UPDATE `##site_setting` SET setting_value = LAST_INSERT_ID(setting_value + :increment) WHERE setting_name = 'next_xref'"
645b90d8accSGreg Roach			);
64613abd6f3SGreg Roach			$statement->execute([
647971d66c8SGreg Roach				'increment'   => (int) $increment,
64813abd6f3SGreg Roach			]);
649b90d8accSGreg Roach
650b90d8accSGreg Roach			if ($statement->rowCount() === 0) {
651b90d8accSGreg Roach				// First time we've used this record type.
652a214e186SGreg Roach				Site::setPreference('next_xref', '1');
653b90d8accSGreg Roach				$num = 1;
654b90d8accSGreg Roach			} else {
655b90d8accSGreg Roach				$num = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne();
656b90d8accSGreg Roach			}
657b90d8accSGreg Roach
658a214e186SGreg Roach			$xref = $prefix . $num;
659a214e186SGreg Roach
660b90d8accSGreg Roach			// Records may already exist with this sequence number.
661b90d8accSGreg Roach			$already_used = Database::prepare(
662a214e186SGreg Roach				"SELECT" .
663a214e186SGreg Roach				" EXISTS (SELECT 1 FROM `##individuals` WHERE i_id = :i_id) OR" .
664a214e186SGreg Roach				" EXISTS (SELECT 1 FROM `##families` WHERE f_id = :f_id) OR" .
665a214e186SGreg Roach				" EXISTS (SELECT 1 FROM `##sources` WHERE s_id = :s_id) OR" .
666a214e186SGreg Roach				" EXISTS (SELECT 1 FROM `##media` WHERE m_id = :m_id) OR" .
667a214e186SGreg Roach				" EXISTS (SELECT 1 FROM `##other` WHERE o_id = :o_id) OR" .
668a214e186SGreg Roach				" EXISTS (SELECT 1 FROM `##change` WHERE xref = :xref)"
66913abd6f3SGreg Roach			)->execute([
670a214e186SGreg Roach				'i_id' => $xref,
671a214e186SGreg Roach				'f_id' => $xref,
672a214e186SGreg Roach				's_id' => $xref,
673a214e186SGreg Roach				'm_id' => $xref,
674a214e186SGreg Roach				'o_id' => $xref,
675a214e186SGreg Roach				'xref' => $xref,
67613abd6f3SGreg Roach			])->fetchOne();
677971d66c8SGreg Roach
678971d66c8SGreg Roach			// This exponential increment allows us to scan over large blocks of
679971d66c8SGreg Roach			// existing data in a reasonable time.
680971d66c8SGreg Roach			$increment *= 1.01;
681a214e186SGreg Roach		} while ($already_used !== '0');
682b90d8accSGreg Roach
683a214e186SGreg Roach		return $xref;
684b90d8accSGreg Roach	}
685b90d8accSGreg Roach
686b90d8accSGreg Roach	/**
687304f20d5SGreg Roach	 * Create a new record from GEDCOM data.
688304f20d5SGreg Roach	 *
689304f20d5SGreg Roach	 * @param string $gedcom
690304f20d5SGreg Roach	 *
691304f20d5SGreg Roach	 * @throws \Exception
692cbc1590aSGreg Roach	 *
69315d603e7SGreg Roach	 * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media
694304f20d5SGreg Roach	 */
695304f20d5SGreg Roach	public function createRecord($gedcom) {
696304f20d5SGreg Roach		if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom, $match)) {
697304f20d5SGreg Roach			$xref = $match[1];
698304f20d5SGreg Roach			$type = $match[2];
699304f20d5SGreg Roach		} else {
700304f20d5SGreg Roach			throw new \Exception('Invalid argument to GedcomRecord::createRecord(' . $gedcom . ')');
701304f20d5SGreg Roach		}
702304f20d5SGreg Roach		if (strpos("\r", $gedcom) !== false) {
703304f20d5SGreg Roach			// MSDOS line endings will break things in horrible ways
704304f20d5SGreg Roach			throw new \Exception('Evil line endings found in GedcomRecord::createRecord(' . $gedcom . ')');
705304f20d5SGreg Roach		}
706304f20d5SGreg Roach
707304f20d5SGreg Roach		// webtrees creates XREFs containing digits. Anything else (e.g. “new”) is just a placeholder.
708304f20d5SGreg Roach		if (!preg_match('/\d/', $xref)) {
709a214e186SGreg Roach			$xref   = $this->getNewXref();
710304f20d5SGreg Roach			$gedcom = preg_replace('/^0 @(' . WT_REGEX_XREF . ')@/', '0 @' . $xref . '@', $gedcom);
711304f20d5SGreg Roach		}
712304f20d5SGreg Roach
713304f20d5SGreg Roach		// Create a change record, if not already present
714304f20d5SGreg Roach		if (!preg_match('/\n1 CHAN/', $gedcom)) {
715304f20d5SGreg Roach			$gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
716304f20d5SGreg Roach		}
717304f20d5SGreg Roach
718304f20d5SGreg Roach		// Create a pending change
719304f20d5SGreg Roach		Database::prepare(
720304f20d5SGreg Roach			"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)"
72113abd6f3SGreg Roach		)->execute([
722bb7c2cdcSGreg Roach			$this->tree_id,
723304f20d5SGreg Roach			$xref,
724304f20d5SGreg Roach			$gedcom,
725cbc1590aSGreg Roach			Auth::id(),
72613abd6f3SGreg Roach		]);
727304f20d5SGreg Roach
728304f20d5SGreg Roach		Log::addEditLog('Create: ' . $type . ' ' . $xref);
729304f20d5SGreg Roach
730304f20d5SGreg Roach		// Accept this pending change
731304f20d5SGreg Roach		if (Auth::user()->getPreference('auto_accept')) {
7323d7a8a4cSGreg Roach			FunctionsImport::acceptAllChanges($xref, $this->tree_id);
733304f20d5SGreg Roach		}
734bb7c2cdcSGreg Roach		// Return the newly created record. Note that since GedcomRecord
735bb7c2cdcSGreg Roach		// has a cache of pending changes, we cannot use it to create a
736bb7c2cdcSGreg Roach		// record with a newly created pending change.
73724ec66ceSGreg Roach		return GedcomRecord::getInstance($xref, $this, $gedcom);
738304f20d5SGreg Roach	}
7398586983fSGreg Roach
7408586983fSGreg Roach	/**
7418586983fSGreg Roach	 * What is the most significant individual in this tree.
7428586983fSGreg Roach	 *
7438586983fSGreg Roach	 * @param User $user
7448586983fSGreg Roach	 *
7458586983fSGreg Roach	 * @return Individual
7468586983fSGreg Roach	 */
7478586983fSGreg Roach	public function significantIndividual(User $user): Individual {
7488586983fSGreg Roach		static $individual; // Only query the DB once.
7498586983fSGreg Roach
7508586983fSGreg Roach		if (!$individual && $this->getUserPreference($user, 'rootid')) {
7518586983fSGreg Roach			$individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this);
7528586983fSGreg Roach		}
7538586983fSGreg Roach		if (!$individual && $this->getUserPreference($user, 'gedcomid')) {
7548586983fSGreg Roach			$individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this);
7558586983fSGreg Roach		}
7568586983fSGreg Roach		if (!$individual) {
7578586983fSGreg Roach			$individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this);
7588586983fSGreg Roach		}
7598586983fSGreg Roach		if (!$individual) {
7608586983fSGreg Roach			$individual = Individual::getInstance(
7618586983fSGreg Roach				Database::prepare(
7628586983fSGreg Roach					"SELECT MIN(i_id) FROM `##individuals` WHERE i_file=?"
7638586983fSGreg Roach				)->execute([$this->getTreeId()])->fetchOne(),
7648586983fSGreg Roach				$this
7658586983fSGreg Roach			);
7668586983fSGreg Roach		}
7678586983fSGreg Roach		if (!$individual) {
7688586983fSGreg Roach			// always return a record
7698586983fSGreg Roach			$individual = new Individual('I', '0 @I@ INDI', null, $this);
7708586983fSGreg Roach		}
7718586983fSGreg Roach
7728586983fSGreg Roach		return $individual;
7738586983fSGreg Roach	}
774a25f0a04SGreg Roach}
775