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 18*b7e60af1SGreg Roachuse Exception; 193d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 21a25f0a04SGreg Roachuse PDOException; 22a25f0a04SGreg Roach 23a25f0a04SGreg Roach/** 2476692c8bSGreg Roach * Provide an interface to the wt_gedcom table. 25a25f0a04SGreg Roach */ 26c1010edaSGreg Roachclass Tree 27c1010edaSGreg Roach{ 28cbc1590aSGreg Roach /** @var int The tree's ID number */ 29518bbdc1SGreg Roach private $tree_id; 30518bbdc1SGreg Roach 31a25f0a04SGreg Roach /** @var string The tree's name */ 32a25f0a04SGreg Roach private $name; 33518bbdc1SGreg Roach 34a25f0a04SGreg Roach /** @var string The tree's title */ 35a25f0a04SGreg Roach private $title; 36a25f0a04SGreg Roach 37e2052359SGreg Roach /** @var int[] Default access rules for facts in this tree */ 38518bbdc1SGreg Roach private $fact_privacy; 39518bbdc1SGreg Roach 40e2052359SGreg Roach /** @var int[] Default access rules for individuals in this tree */ 41518bbdc1SGreg Roach private $individual_privacy; 42518bbdc1SGreg Roach 43518bbdc1SGreg Roach /** @var integer[][] Default access rules for individual facts in this tree */ 44518bbdc1SGreg Roach private $individual_fact_privacy; 45518bbdc1SGreg Roach 46a25f0a04SGreg Roach /** @var Tree[] All trees that we have permission to see. */ 47a25f0a04SGreg Roach private static $trees; 48a25f0a04SGreg Roach 49a25f0a04SGreg Roach /** @var string[] Cached copy of the wt_gedcom_setting table. */ 5015d603e7SGreg Roach private $preferences = []; 51a25f0a04SGreg Roach 52a25f0a04SGreg Roach /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ 5313abd6f3SGreg Roach private $user_preferences = []; 54a25f0a04SGreg Roach 55a25f0a04SGreg Roach /** 56a25f0a04SGreg Roach * Create a tree object. This is a private constructor - it can only 57a25f0a04SGreg Roach * be called from Tree::getAll() to ensure proper initialisation. 58a25f0a04SGreg Roach * 59cbc1590aSGreg Roach * @param int $tree_id 60a25f0a04SGreg Roach * @param string $tree_name 61a25f0a04SGreg Roach * @param string $tree_title 62a25f0a04SGreg Roach */ 63c1010edaSGreg Roach private function __construct($tree_id, $tree_name, $tree_title) 64c1010edaSGreg Roach { 65518bbdc1SGreg Roach $this->tree_id = $tree_id; 66a25f0a04SGreg Roach $this->name = $tree_name; 67a25f0a04SGreg Roach $this->title = $tree_title; 6813abd6f3SGreg Roach $this->fact_privacy = []; 6913abd6f3SGreg Roach $this->individual_privacy = []; 7013abd6f3SGreg Roach $this->individual_fact_privacy = []; 71518bbdc1SGreg Roach 72518bbdc1SGreg Roach // Load the privacy settings for this tree 73518bbdc1SGreg Roach $rows = Database::prepare( 74e5588fb0SGreg 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" . 75518bbdc1SGreg Roach " FROM `##default_resn` WHERE gedcom_id = :tree_id" 7613abd6f3SGreg Roach )->execute([ 774b9ff166SGreg Roach 'priv_public' => Auth::PRIV_PRIVATE, 784b9ff166SGreg Roach 'priv_user' => Auth::PRIV_USER, 794b9ff166SGreg Roach 'priv_none' => Auth::PRIV_NONE, 804b9ff166SGreg Roach 'priv_hide' => Auth::PRIV_HIDE, 81cbc1590aSGreg Roach 'tree_id' => $this->tree_id, 8213abd6f3SGreg Roach ])->fetchAll(); 83518bbdc1SGreg Roach 84518bbdc1SGreg Roach foreach ($rows as $row) { 85518bbdc1SGreg Roach if ($row->xref !== null) { 86518bbdc1SGreg Roach if ($row->tag_type !== null) { 87518bbdc1SGreg Roach $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn; 88518bbdc1SGreg Roach } else { 89518bbdc1SGreg Roach $this->individual_privacy[$row->xref] = (int) $row->resn; 90518bbdc1SGreg Roach } 91518bbdc1SGreg Roach } else { 92518bbdc1SGreg Roach $this->fact_privacy[$row->tag_type] = (int) $row->resn; 93518bbdc1SGreg Roach } 94518bbdc1SGreg Roach } 95a25f0a04SGreg Roach } 96a25f0a04SGreg Roach 97a25f0a04SGreg Roach /** 98a25f0a04SGreg Roach * The ID of this tree 99a25f0a04SGreg Roach * 100cbc1590aSGreg Roach * @return int 101a25f0a04SGreg Roach */ 102c1010edaSGreg Roach public function getTreeId() 103c1010edaSGreg Roach { 104518bbdc1SGreg Roach return $this->tree_id; 105a25f0a04SGreg Roach } 106a25f0a04SGreg Roach 107a25f0a04SGreg Roach /** 108a25f0a04SGreg Roach * The name of this tree 109a25f0a04SGreg Roach * 110a25f0a04SGreg Roach * @return string 111a25f0a04SGreg Roach */ 112c1010edaSGreg Roach public function getName() 113c1010edaSGreg Roach { 114a25f0a04SGreg Roach return $this->name; 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach 117a25f0a04SGreg Roach /** 118a25f0a04SGreg Roach * The title of this tree 119a25f0a04SGreg Roach * 120a25f0a04SGreg Roach * @return string 121a25f0a04SGreg Roach */ 122c1010edaSGreg Roach public function getTitle() 123c1010edaSGreg Roach { 124a25f0a04SGreg Roach return $this->title; 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach 127a25f0a04SGreg Roach /** 128518bbdc1SGreg Roach * The fact-level privacy for this tree. 129518bbdc1SGreg Roach * 130e2052359SGreg Roach * @return int[] 131518bbdc1SGreg Roach */ 132c1010edaSGreg Roach public function getFactPrivacy() 133c1010edaSGreg Roach { 134518bbdc1SGreg Roach return $this->fact_privacy; 135518bbdc1SGreg Roach } 136518bbdc1SGreg Roach 137518bbdc1SGreg Roach /** 138518bbdc1SGreg Roach * The individual-level privacy for this tree. 139518bbdc1SGreg Roach * 140e2052359SGreg Roach * @return int[] 141518bbdc1SGreg Roach */ 142c1010edaSGreg Roach public function getIndividualPrivacy() 143c1010edaSGreg Roach { 144518bbdc1SGreg Roach return $this->individual_privacy; 145518bbdc1SGreg Roach } 146518bbdc1SGreg Roach 147518bbdc1SGreg Roach /** 148518bbdc1SGreg Roach * The individual-fact-level privacy for this tree. 149518bbdc1SGreg Roach * 150adac8af1SGreg Roach * @return int[][] 151518bbdc1SGreg Roach */ 152c1010edaSGreg Roach public function getIndividualFactPrivacy() 153c1010edaSGreg Roach { 154518bbdc1SGreg Roach return $this->individual_fact_privacy; 155518bbdc1SGreg Roach } 156518bbdc1SGreg Roach 157518bbdc1SGreg Roach /** 158a25f0a04SGreg Roach * Get the tree’s configuration settings. 159a25f0a04SGreg Roach * 160a25f0a04SGreg Roach * @param string $setting_name 16115d603e7SGreg Roach * @param string $default 162a25f0a04SGreg Roach * 16315d603e7SGreg Roach * @return string 164a25f0a04SGreg Roach */ 165c1010edaSGreg Roach public function getPreference($setting_name, $default = '') 166c1010edaSGreg Roach { 16715d603e7SGreg Roach if (empty($this->preferences)) { 168a25f0a04SGreg Roach $this->preferences = Database::prepare( 169e5588fb0SGreg 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 */ 188c1010edaSGreg Roach public function setPreference($setting_name, $setting_value) 189c1010edaSGreg Roach { 190a25f0a04SGreg Roach if ($setting_value !== $this->getPreference($setting_name)) { 191a25f0a04SGreg Roach Database::prepare( 192a25f0a04SGreg Roach "REPLACE INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" . 193a25f0a04SGreg Roach " VALUES (:tree_id, :setting_name, LEFT(:setting_value, 255))" 19413abd6f3SGreg Roach )->execute([ 195518bbdc1SGreg Roach 'tree_id' => $this->tree_id, 196a25f0a04SGreg Roach 'setting_name' => $setting_name, 197a25f0a04SGreg Roach 'setting_value' => $setting_value, 19813abd6f3SGreg Roach ]); 19915d603e7SGreg Roach 200a25f0a04SGreg Roach $this->preferences[$setting_name] = $setting_value; 20115d603e7SGreg Roach 20272292b7dSGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); 203a25f0a04SGreg Roach } 204a25f0a04SGreg Roach 205a25f0a04SGreg Roach return $this; 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach 208a25f0a04SGreg Roach /** 209a25f0a04SGreg Roach * Get the tree’s user-configuration settings. 210a25f0a04SGreg Roach * 211a25f0a04SGreg Roach * @param User $user 212a25f0a04SGreg Roach * @param string $setting_name 213a25f0a04SGreg Roach * @param string|null $default 214a25f0a04SGreg Roach * 215a25f0a04SGreg Roach * @return string 216a25f0a04SGreg Roach */ 217c1010edaSGreg Roach public function getUserPreference(User $user, $setting_name, $default = null) 218c1010edaSGreg Roach { 219a25f0a04SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 220a25f0a04SGreg Roach // so it is quicker to fetch them all in one go. 221a25f0a04SGreg Roach if (!array_key_exists($user->getUserId(), $this->user_preferences)) { 222a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()] = Database::prepare( 223e5588fb0SGreg Roach "SELECT setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?" 224c1010edaSGreg Roach )->execute([ 225c1010edaSGreg Roach $user->getUserId(), 226c1010edaSGreg Roach $this->tree_id, 227c1010edaSGreg Roach ])->fetchAssoc(); 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach 230a25f0a04SGreg Roach if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) { 231a25f0a04SGreg Roach return $this->user_preferences[$user->getUserId()][$setting_name]; 232a25f0a04SGreg Roach } else { 233a25f0a04SGreg Roach return $default; 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach } 236a25f0a04SGreg Roach 237a25f0a04SGreg Roach /** 238a25f0a04SGreg Roach * Set the tree’s user-configuration settings. 239a25f0a04SGreg Roach * 240a25f0a04SGreg Roach * @param User $user 241a25f0a04SGreg Roach * @param string $setting_name 242a25f0a04SGreg Roach * @param string $setting_value 243a25f0a04SGreg Roach * 244a25f0a04SGreg Roach * @return $this 245a25f0a04SGreg Roach */ 246c1010edaSGreg Roach public function setUserPreference(User $user, $setting_name, $setting_value) 247c1010edaSGreg Roach { 248a25f0a04SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 249a25f0a04SGreg Roach // Update the database 250a25f0a04SGreg Roach if ($setting_value === null) { 251a25f0a04SGreg Roach Database::prepare( 252a25f0a04SGreg Roach "DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name" 25313abd6f3SGreg Roach )->execute([ 254518bbdc1SGreg Roach 'tree_id' => $this->tree_id, 255a25f0a04SGreg Roach 'user_id' => $user->getUserId(), 256a25f0a04SGreg Roach 'setting_name' => $setting_name, 25713abd6f3SGreg Roach ]); 258a25f0a04SGreg Roach } else { 259a25f0a04SGreg Roach Database::prepare( 260a25f0a04SGreg 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))" 26113abd6f3SGreg Roach )->execute([ 262a25f0a04SGreg Roach 'user_id' => $user->getUserId(), 263518bbdc1SGreg Roach 'tree_id' => $this->tree_id, 264a25f0a04SGreg Roach 'setting_name' => $setting_name, 265cbc1590aSGreg Roach 'setting_value' => $setting_value, 26613abd6f3SGreg Roach ]); 267a25f0a04SGreg Roach } 268a25f0a04SGreg Roach // Update our cache 269a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()][$setting_name] = $setting_value; 270a25f0a04SGreg Roach // Audit log of changes 27172292b7dSGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this); 272a25f0a04SGreg Roach } 273a25f0a04SGreg Roach 274a25f0a04SGreg Roach return $this; 275a25f0a04SGreg Roach } 276a25f0a04SGreg Roach 277a25f0a04SGreg Roach /** 278a25f0a04SGreg Roach * Can a user accept changes for this tree? 279a25f0a04SGreg Roach * 280a25f0a04SGreg Roach * @param User $user 281a25f0a04SGreg Roach * 282cbc1590aSGreg Roach * @return bool 283a25f0a04SGreg Roach */ 284c1010edaSGreg Roach public function canAcceptChanges(User $user) 285c1010edaSGreg Roach { 286a25f0a04SGreg Roach return Auth::isModerator($this, $user); 287a25f0a04SGreg Roach } 288a25f0a04SGreg Roach 289a25f0a04SGreg Roach /** 290a25f0a04SGreg Roach * Fetch all the trees that we have permission to access. 291a25f0a04SGreg Roach * 292a25f0a04SGreg Roach * @return Tree[] 293a25f0a04SGreg Roach */ 294c1010edaSGreg Roach public static function getAll() 295c1010edaSGreg Roach { 296a25f0a04SGreg Roach if (self::$trees === null) { 29713abd6f3SGreg Roach self::$trees = []; 298a25f0a04SGreg Roach $rows = Database::prepare( 299e5588fb0SGreg Roach "SELECT g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" . 300a25f0a04SGreg Roach " FROM `##gedcom` g" . 301a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" . 302a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" . 303a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" . 304a25f0a04SGreg Roach " LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" . 305a25f0a04SGreg Roach " WHERE " . 306a25f0a04SGreg Roach " g.gedcom_id>0 AND (" . // exclude the "template" tree 307a25f0a04SGreg Roach " EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all 308a25f0a04SGreg Roach " ) OR (" . 3098cca4ddeSGreg Roach " (gs2.setting_value = 1 OR ugs.setting_value = 'admin') AND (" . // Allow imported trees, with either: 310a25f0a04SGreg Roach " gs3.setting_value <> 1 OR" . // visitor access 311a25f0a04SGreg Roach " IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access 312a25f0a04SGreg Roach " )" . 313a25f0a04SGreg Roach " )" . 314a25f0a04SGreg Roach " ORDER BY g.sort_order, 3" 315c1010edaSGreg Roach )->execute([ 316c1010edaSGreg Roach Auth::id(), 317c1010edaSGreg Roach Auth::id(), 318c1010edaSGreg Roach ])->fetchAll(); 319a25f0a04SGreg Roach foreach ($rows as $row) { 3203f7ece23SGreg Roach self::$trees[$row->tree_name] = new self((int) $row->tree_id, $row->tree_name, $row->tree_title); 321a25f0a04SGreg Roach } 322a25f0a04SGreg Roach } 323a25f0a04SGreg Roach 324a25f0a04SGreg Roach return self::$trees; 325a25f0a04SGreg Roach } 326a25f0a04SGreg Roach 327a25f0a04SGreg Roach /** 328d2cdeb3fSGreg Roach * Find the tree with a specific ID. 329a25f0a04SGreg Roach * 330cbc1590aSGreg Roach * @param int $tree_id 331cbc1590aSGreg Roach * 332cbc1590aSGreg Roach * @throws \DomainException 333a25f0a04SGreg Roach * @return Tree 334a25f0a04SGreg Roach */ 335c1010edaSGreg Roach public static function findById($tree_id) 336c1010edaSGreg Roach { 33751d0f842SGreg Roach foreach (self::getAll() as $tree) { 338e568acceSGreg Roach if ($tree->tree_id == $tree_id) { 33951d0f842SGreg Roach return $tree; 34051d0f842SGreg Roach } 34151d0f842SGreg Roach } 34251d0f842SGreg Roach throw new \DomainException; 343a25f0a04SGreg Roach } 344a25f0a04SGreg Roach 345a25f0a04SGreg Roach /** 346d2cdeb3fSGreg Roach * Find the tree with a specific name. 347cf4bcc09SGreg Roach * 348cf4bcc09SGreg Roach * @param string $tree_name 349cf4bcc09SGreg Roach * 350cf4bcc09SGreg Roach * @return Tree|null 351cf4bcc09SGreg Roach */ 352c1010edaSGreg Roach public static function findByName($tree_name) 353c1010edaSGreg Roach { 354cf4bcc09SGreg Roach foreach (self::getAll() as $tree) { 35551d0f842SGreg Roach if ($tree->name === $tree_name) { 356cf4bcc09SGreg Roach return $tree; 357cf4bcc09SGreg Roach } 358cf4bcc09SGreg Roach } 359cf4bcc09SGreg Roach 360cf4bcc09SGreg Roach return null; 361cf4bcc09SGreg Roach } 362cf4bcc09SGreg Roach 363cf4bcc09SGreg Roach /** 364a25f0a04SGreg Roach * Create arguments to select_edit_control() 365a25f0a04SGreg Roach * Note - these will be escaped later 366a25f0a04SGreg Roach * 367a25f0a04SGreg Roach * @return string[] 368a25f0a04SGreg Roach */ 369c1010edaSGreg Roach public static function getIdList() 370c1010edaSGreg Roach { 37113abd6f3SGreg Roach $list = []; 372a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 373518bbdc1SGreg Roach $list[$tree->tree_id] = $tree->title; 374a25f0a04SGreg Roach } 375a25f0a04SGreg Roach 376a25f0a04SGreg Roach return $list; 377a25f0a04SGreg Roach } 378a25f0a04SGreg Roach 379a25f0a04SGreg Roach /** 380a25f0a04SGreg Roach * Create arguments to select_edit_control() 381a25f0a04SGreg Roach * Note - these will be escaped later 382a25f0a04SGreg Roach * 383a25f0a04SGreg Roach * @return string[] 384a25f0a04SGreg Roach */ 385c1010edaSGreg Roach public static function getNameList() 386c1010edaSGreg Roach { 38713abd6f3SGreg Roach $list = []; 388a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 389a25f0a04SGreg Roach $list[$tree->name] = $tree->title; 390a25f0a04SGreg Roach } 391a25f0a04SGreg Roach 392a25f0a04SGreg Roach return $list; 393a25f0a04SGreg Roach } 394a25f0a04SGreg Roach 395a25f0a04SGreg Roach /** 396a25f0a04SGreg Roach * Create a new tree 397a25f0a04SGreg Roach * 398a25f0a04SGreg Roach * @param string $tree_name 399a25f0a04SGreg Roach * @param string $tree_title 400a25f0a04SGreg Roach * 401a25f0a04SGreg Roach * @return Tree 402a25f0a04SGreg Roach */ 403c1010edaSGreg Roach public static function create($tree_name, $tree_title) 404c1010edaSGreg Roach { 405a25f0a04SGreg Roach try { 406a25f0a04SGreg Roach // Create a new tree 407a25f0a04SGreg Roach Database::prepare( 408a25f0a04SGreg Roach "INSERT INTO `##gedcom` (gedcom_name) VALUES (?)" 40913abd6f3SGreg Roach )->execute([$tree_name]); 410a25f0a04SGreg Roach $tree_id = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne(); 411a25f0a04SGreg Roach } catch (PDOException $ex) { 412bd52fa32SGreg Roach DebugBar::addThrowable($ex); 413bd52fa32SGreg Roach 414a25f0a04SGreg Roach // A tree with that name already exists? 415ef2fd529SGreg Roach return self::findByName($tree_name); 416a25f0a04SGreg Roach } 417a25f0a04SGreg Roach 418a25f0a04SGreg Roach // Update the list of trees - to include this new one 419a25f0a04SGreg Roach self::$trees = null; 420d2cdeb3fSGreg Roach $tree = self::findById($tree_id); 421a25f0a04SGreg Roach 422a25f0a04SGreg Roach $tree->setPreference('imported', '0'); 423a25f0a04SGreg Roach $tree->setPreference('title', $tree_title); 424a25f0a04SGreg Roach 425a25f0a04SGreg Roach // Module privacy 426a25f0a04SGreg Roach Module::setDefaultAccess($tree_id); 427a25f0a04SGreg Roach 4281507cbcaSGreg Roach // Set preferences from default tree 4291507cbcaSGreg Roach Database::prepare( 4301507cbcaSGreg Roach "INSERT INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" . 4311507cbcaSGreg Roach " SELECT :tree_id, setting_name, setting_value" . 4321507cbcaSGreg Roach " FROM `##gedcom_setting` WHERE gedcom_id = -1" 43313abd6f3SGreg Roach )->execute([ 4341507cbcaSGreg Roach 'tree_id' => $tree_id, 43513abd6f3SGreg Roach ]); 4361507cbcaSGreg Roach 4371507cbcaSGreg Roach Database::prepare( 4381507cbcaSGreg Roach "INSERT INTO `##default_resn` (gedcom_id, tag_type, resn)" . 4391507cbcaSGreg Roach " SELECT :tree_id, tag_type, resn" . 4401507cbcaSGreg Roach " FROM `##default_resn` WHERE gedcom_id = -1" 44113abd6f3SGreg Roach )->execute([ 4421507cbcaSGreg Roach 'tree_id' => $tree_id, 44313abd6f3SGreg Roach ]); 4441507cbcaSGreg Roach 4451507cbcaSGreg Roach Database::prepare( 4461507cbcaSGreg Roach "INSERT INTO `##block` (gedcom_id, location, block_order, module_name)" . 4471507cbcaSGreg Roach " SELECT :tree_id, location, block_order, module_name" . 4481507cbcaSGreg Roach " FROM `##block` WHERE gedcom_id = -1" 44913abd6f3SGreg Roach )->execute([ 4501507cbcaSGreg Roach 'tree_id' => $tree_id, 45113abd6f3SGreg Roach ]); 4521507cbcaSGreg Roach 453a25f0a04SGreg Roach // Gedcom and privacy settings 454a25f0a04SGreg Roach $tree->setPreference('CONTACT_USER_ID', Auth::id()); 4551507cbcaSGreg Roach $tree->setPreference('WEBMASTER_USER_ID', Auth::id()); 456a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 457a25f0a04SGreg Roach switch (WT_LOCALE) { 458a25f0a04SGreg Roach case 'es': 459a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 460a25f0a04SGreg Roach break; 461a25f0a04SGreg Roach case 'is': 462a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 463a25f0a04SGreg Roach break; 464a25f0a04SGreg Roach case 'lt': 465a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 466a25f0a04SGreg Roach break; 467a25f0a04SGreg Roach case 'pl': 468a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 469a25f0a04SGreg Roach break; 470a25f0a04SGreg Roach case 'pt': 471a25f0a04SGreg Roach case 'pt-BR': 472a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 473a25f0a04SGreg Roach break; 474a25f0a04SGreg Roach default: 475a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 476a25f0a04SGreg Roach break; 477a25f0a04SGreg Roach } 478a25f0a04SGreg Roach 479a25f0a04SGreg Roach // Genealogy data 480a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 481c1010edaSGreg Roach $john_doe = /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 482c1010edaSGreg Roach I18N::translate('John /DOE/'); 48377e70a22SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 48413abd6f3SGreg Roach Database::prepare("INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)")->execute([ 485a25f0a04SGreg Roach $tree_id, 486cbc1590aSGreg 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", 48713abd6f3SGreg Roach ]); 488a25f0a04SGreg Roach 489a25f0a04SGreg Roach // Update our cache 490518bbdc1SGreg Roach self::$trees[$tree->tree_id] = $tree; 491a25f0a04SGreg Roach 492a25f0a04SGreg Roach return $tree; 493a25f0a04SGreg Roach } 494a25f0a04SGreg Roach 495a25f0a04SGreg Roach /** 496b78374c5SGreg Roach * Are there any pending edits for this tree, than need reviewing by a moderator. 497b78374c5SGreg Roach * 498b78374c5SGreg Roach * @return bool 499b78374c5SGreg Roach */ 500c1010edaSGreg Roach public function hasPendingEdit() 501c1010edaSGreg Roach { 502b78374c5SGreg Roach return (bool) Database::prepare( 503b78374c5SGreg Roach "SELECT 1 FROM `##change` WHERE status = 'pending' AND gedcom_id = :tree_id" 50413abd6f3SGreg Roach )->execute([ 505cbc1590aSGreg Roach 'tree_id' => $this->tree_id, 50613abd6f3SGreg Roach ])->fetchOne(); 507b78374c5SGreg Roach } 508b78374c5SGreg Roach 509b78374c5SGreg Roach /** 510a25f0a04SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 511a25f0a04SGreg Roach * new data. Optionally retain the media data, for when the user has been 512a25f0a04SGreg Roach * editing their data offline using an application which deletes (or does not 513a25f0a04SGreg Roach * support) media data. 514a25f0a04SGreg Roach * 515a25f0a04SGreg Roach * @param bool $keep_media 516*b7e60af1SGreg Roach * 517*b7e60af1SGreg Roach * @return void 518a25f0a04SGreg Roach */ 519*b7e60af1SGreg Roach public function deleteGenealogyData(bool $keep_media) 520c1010edaSGreg Roach { 52113abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]); 52213abd6f3SGreg Roach Database::prepare("DELETE FROM `##individuals` WHERE i_file = ?")->execute([$this->tree_id]); 52313abd6f3SGreg Roach Database::prepare("DELETE FROM `##families` WHERE f_file = ?")->execute([$this->tree_id]); 52413abd6f3SGreg Roach Database::prepare("DELETE FROM `##sources` WHERE s_file = ?")->execute([$this->tree_id]); 52513abd6f3SGreg Roach Database::prepare("DELETE FROM `##other` WHERE o_file = ?")->execute([$this->tree_id]); 52613abd6f3SGreg Roach Database::prepare("DELETE FROM `##places` WHERE p_file = ?")->execute([$this->tree_id]); 52713abd6f3SGreg Roach Database::prepare("DELETE FROM `##placelinks` WHERE pl_file = ?")->execute([$this->tree_id]); 52813abd6f3SGreg Roach Database::prepare("DELETE FROM `##name` WHERE n_file = ?")->execute([$this->tree_id]); 52913abd6f3SGreg Roach Database::prepare("DELETE FROM `##dates` WHERE d_file = ?")->execute([$this->tree_id]); 53013abd6f3SGreg Roach Database::prepare("DELETE FROM `##change` WHERE gedcom_id = ?")->execute([$this->tree_id]); 531a25f0a04SGreg Roach 532a25f0a04SGreg Roach if ($keep_media) { 53313abd6f3SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute([$this->tree_id]); 534a25f0a04SGreg Roach } else { 53513abd6f3SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =?")->execute([$this->tree_id]); 53613abd6f3SGreg Roach Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute([$this->tree_id]); 53723ed9d24SGreg Roach Database::prepare("DELETE FROM `##media_file` WHERE m_file =?")->execute([$this->tree_id]); 538a25f0a04SGreg Roach } 539a25f0a04SGreg Roach } 540a25f0a04SGreg Roach 541a25f0a04SGreg Roach /** 542a25f0a04SGreg Roach * Delete everything relating to a tree 543*b7e60af1SGreg Roach * 544*b7e60af1SGreg Roach * @return void 545a25f0a04SGreg Roach */ 546c1010edaSGreg Roach public function delete() 547c1010edaSGreg Roach { 548a25f0a04SGreg Roach // If this is the default tree, then unset it 549ef2fd529SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 550a25f0a04SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 551a25f0a04SGreg Roach } 552a25f0a04SGreg Roach 553a25f0a04SGreg Roach $this->deleteGenealogyData(false); 554a25f0a04SGreg Roach 55513abd6f3SGreg Roach Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute([$this->tree_id]); 55613abd6f3SGreg Roach Database::prepare("DELETE FROM `##block` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55713abd6f3SGreg Roach Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55813abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55913abd6f3SGreg Roach Database::prepare("DELETE FROM `##module_privacy` WHERE gedcom_id = ?")->execute([$this->tree_id]); 56013abd6f3SGreg Roach Database::prepare("DELETE FROM `##hit_counter` WHERE gedcom_id = ?")->execute([$this->tree_id]); 56113abd6f3SGreg Roach Database::prepare("DELETE FROM `##default_resn` WHERE gedcom_id = ?")->execute([$this->tree_id]); 56213abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]); 56313abd6f3SGreg Roach Database::prepare("DELETE FROM `##log` WHERE gedcom_id = ?")->execute([$this->tree_id]); 56413abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom` WHERE gedcom_id = ?")->execute([$this->tree_id]); 565a25f0a04SGreg Roach 566a25f0a04SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 567a25f0a04SGreg Roach self::$trees = null; 568a25f0a04SGreg Roach } 569a25f0a04SGreg Roach 570a25f0a04SGreg Roach /** 571a25f0a04SGreg Roach * Export the tree to a GEDCOM file 572a25f0a04SGreg Roach * 5735792757eSGreg Roach * @param resource $stream 574*b7e60af1SGreg Roach * 575*b7e60af1SGreg Roach * @return void 576a25f0a04SGreg Roach */ 577c1010edaSGreg Roach public function exportGedcom($stream) 578c1010edaSGreg Roach { 5795792757eSGreg Roach $stmt = Database::prepare( 580e56ef01aSGreg Roach "SELECT i_gedcom AS gedcom, i_id AS xref, 1 AS n FROM `##individuals` WHERE i_file = :tree_id_1" . 5815792757eSGreg Roach " UNION ALL " . 582e56ef01aSGreg Roach "SELECT f_gedcom AS gedcom, f_id AS xref, 2 AS n FROM `##families` WHERE f_file = :tree_id_2" . 5835792757eSGreg Roach " UNION ALL " . 584e56ef01aSGreg Roach "SELECT s_gedcom AS gedcom, s_id AS xref, 3 AS n FROM `##sources` WHERE s_file = :tree_id_3" . 5855792757eSGreg Roach " UNION ALL " . 586e56ef01aSGreg 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')" . 5875792757eSGreg Roach " UNION ALL " . 588e56ef01aSGreg Roach "SELECT m_gedcom AS gedcom, m_id AS xref, 5 AS n FROM `##media` WHERE m_file = :tree_id_5" . 589e56ef01aSGreg Roach " ORDER BY n, LENGTH(xref), xref" 59013abd6f3SGreg Roach )->execute([ 5915792757eSGreg Roach 'tree_id_1' => $this->tree_id, 5925792757eSGreg Roach 'tree_id_2' => $this->tree_id, 5935792757eSGreg Roach 'tree_id_3' => $this->tree_id, 5945792757eSGreg Roach 'tree_id_4' => $this->tree_id, 595cbc1590aSGreg Roach 'tree_id_5' => $this->tree_id, 59613abd6f3SGreg Roach ]); 597a25f0a04SGreg Roach 5983d7a8a4cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this)); 599a214e186SGreg Roach while (($row = $stmt->fetch()) !== false) { 6003d7a8a4cSGreg Roach $buffer .= FunctionsExport::reformatRecord($row->gedcom); 601a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 6025792757eSGreg Roach fwrite($stream, $buffer); 603a25f0a04SGreg Roach $buffer = ''; 604a25f0a04SGreg Roach } 605a25f0a04SGreg Roach } 6060f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 607195d09d8SGreg Roach $stmt->closeCursor(); 608a25f0a04SGreg Roach } 609a25f0a04SGreg Roach 610a25f0a04SGreg Roach /** 611a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 612a25f0a04SGreg Roach * 613a25f0a04SGreg Roach * @param string $path The full path to the (possibly temporary) file. 614a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 615a25f0a04SGreg Roach * 616*b7e60af1SGreg Roach * @return void 617*b7e60af1SGreg Roach * @throws Exception 618a25f0a04SGreg Roach */ 619c1010edaSGreg Roach public function importGedcomFile($path, $filename) 620c1010edaSGreg Roach { 621a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 622a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 623a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 624a25f0a04SGreg Roach // each block. 625a25f0a04SGreg Roach 626a25f0a04SGreg Roach $file_data = ''; 627a25f0a04SGreg Roach $fp = fopen($path, 'rb'); 628a25f0a04SGreg Roach 629a25f0a04SGreg Roach // Don’t allow the user to cancel the request. We do not want to be left with an incomplete transaction. 630a25f0a04SGreg Roach ignore_user_abort(true); 631a25f0a04SGreg Roach 632*b7e60af1SGreg Roach $this->deleteGenealogyData((bool) $this->getPreference('keep_media')); 633a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 634a25f0a04SGreg Roach $this->setPreference('imported', '0'); 635a25f0a04SGreg Roach 636a25f0a04SGreg Roach while (!feof($fp)) { 637a25f0a04SGreg Roach $file_data .= fread($fp, 65536); 638a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 639a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 640a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 641a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 642a25f0a04SGreg Roach break; 643a25f0a04SGreg Roach } 644a25f0a04SGreg Roach } 645a25f0a04SGreg Roach if ($pos) { 646a25f0a04SGreg Roach Database::prepare( 647a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 648c1010edaSGreg Roach )->execute([ 649c1010edaSGreg Roach $this->tree_id, 650c1010edaSGreg Roach substr($file_data, 0, $pos), 651c1010edaSGreg Roach ]); 652a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 653a25f0a04SGreg Roach } 654a25f0a04SGreg Roach } 655a25f0a04SGreg Roach Database::prepare( 656a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 657c1010edaSGreg Roach )->execute([ 658c1010edaSGreg Roach $this->tree_id, 659c1010edaSGreg Roach $file_data, 660c1010edaSGreg Roach ]); 661a25f0a04SGreg Roach 662a25f0a04SGreg Roach fclose($fp); 663a25f0a04SGreg Roach } 664304f20d5SGreg Roach 665304f20d5SGreg Roach /** 666b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 667b90d8accSGreg Roach * 668b90d8accSGreg Roach * @return string 669b90d8accSGreg Roach */ 670c1010edaSGreg Roach public function getNewXref() 671c1010edaSGreg Roach { 672a214e186SGreg Roach $prefix = 'X'; 673b90d8accSGreg Roach 674971d66c8SGreg Roach $increment = 1.0; 675b90d8accSGreg Roach do { 676b90d8accSGreg Roach // Use LAST_INSERT_ID(expr) to provide a transaction-safe sequence. See 677b90d8accSGreg Roach // http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id 678b90d8accSGreg Roach $statement = Database::prepare( 679a214e186SGreg Roach "UPDATE `##site_setting` SET setting_value = LAST_INSERT_ID(setting_value + :increment) WHERE setting_name = 'next_xref'" 680b90d8accSGreg Roach ); 68113abd6f3SGreg Roach $statement->execute([ 682971d66c8SGreg Roach 'increment' => (int) $increment, 68313abd6f3SGreg Roach ]); 684b90d8accSGreg Roach 685b90d8accSGreg Roach if ($statement->rowCount() === 0) { 686b90d8accSGreg Roach // First time we've used this record type. 687a214e186SGreg Roach Site::setPreference('next_xref', '1'); 688b90d8accSGreg Roach $num = 1; 689b90d8accSGreg Roach } else { 690b90d8accSGreg Roach $num = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne(); 691b90d8accSGreg Roach } 692b90d8accSGreg Roach 693a214e186SGreg Roach $xref = $prefix . $num; 694a214e186SGreg Roach 695b90d8accSGreg Roach // Records may already exist with this sequence number. 696b90d8accSGreg Roach $already_used = Database::prepare( 697a214e186SGreg Roach "SELECT" . 698a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##individuals` WHERE i_id = :i_id) OR" . 699a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##families` WHERE f_id = :f_id) OR" . 700a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##sources` WHERE s_id = :s_id) OR" . 701a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##media` WHERE m_id = :m_id) OR" . 702a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##other` WHERE o_id = :o_id) OR" . 703a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##change` WHERE xref = :xref)" 70413abd6f3SGreg Roach )->execute([ 705a214e186SGreg Roach 'i_id' => $xref, 706a214e186SGreg Roach 'f_id' => $xref, 707a214e186SGreg Roach 's_id' => $xref, 708a214e186SGreg Roach 'm_id' => $xref, 709a214e186SGreg Roach 'o_id' => $xref, 710a214e186SGreg Roach 'xref' => $xref, 71113abd6f3SGreg Roach ])->fetchOne(); 712971d66c8SGreg Roach 713971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 714971d66c8SGreg Roach // existing data in a reasonable time. 715971d66c8SGreg Roach $increment *= 1.01; 716a214e186SGreg Roach } while ($already_used !== '0'); 717b90d8accSGreg Roach 718a214e186SGreg Roach return $xref; 719b90d8accSGreg Roach } 720b90d8accSGreg Roach 721b90d8accSGreg Roach /** 722304f20d5SGreg Roach * Create a new record from GEDCOM data. 723304f20d5SGreg Roach * 724304f20d5SGreg Roach * @param string $gedcom 725304f20d5SGreg Roach * 72615d603e7SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 727*b7e60af1SGreg Roach * @throws Exception 728304f20d5SGreg Roach */ 729c1010edaSGreg Roach public function createRecord($gedcom) 730c1010edaSGreg Roach { 731304f20d5SGreg Roach if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom, $match)) { 732304f20d5SGreg Roach $xref = $match[1]; 733304f20d5SGreg Roach $type = $match[2]; 734304f20d5SGreg Roach } else { 735*b7e60af1SGreg Roach throw new Exception('Invalid argument to GedcomRecord::createRecord(' . $gedcom . ')'); 736304f20d5SGreg Roach } 737304f20d5SGreg Roach if (strpos("\r", $gedcom) !== false) { 738304f20d5SGreg Roach // MSDOS line endings will break things in horrible ways 739*b7e60af1SGreg Roach throw new Exception('Evil line endings found in GedcomRecord::createRecord(' . $gedcom . ')'); 740304f20d5SGreg Roach } 741304f20d5SGreg Roach 742304f20d5SGreg Roach // webtrees creates XREFs containing digits. Anything else (e.g. “new”) is just a placeholder. 743304f20d5SGreg Roach if (!preg_match('/\d/', $xref)) { 744a214e186SGreg Roach $xref = $this->getNewXref(); 745304f20d5SGreg Roach $gedcom = preg_replace('/^0 @(' . WT_REGEX_XREF . ')@/', '0 @' . $xref . '@', $gedcom); 746304f20d5SGreg Roach } 747304f20d5SGreg Roach 748304f20d5SGreg Roach // Create a change record, if not already present 749304f20d5SGreg Roach if (!preg_match('/\n1 CHAN/', $gedcom)) { 750304f20d5SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 751304f20d5SGreg Roach } 752304f20d5SGreg Roach 753304f20d5SGreg Roach // Create a pending change 754304f20d5SGreg Roach Database::prepare( 755304f20d5SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)" 75613abd6f3SGreg Roach )->execute([ 757bb7c2cdcSGreg Roach $this->tree_id, 758304f20d5SGreg Roach $xref, 759304f20d5SGreg Roach $gedcom, 760cbc1590aSGreg Roach Auth::id(), 76113abd6f3SGreg Roach ]); 762304f20d5SGreg Roach 763847d5489SGreg Roach Log::addEditLog('Create: ' . $type . ' ' . $xref, $this); 764304f20d5SGreg Roach 765304f20d5SGreg Roach // Accept this pending change 766304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 767cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 768304f20d5SGreg Roach } 769bb7c2cdcSGreg Roach // Return the newly created record. Note that since GedcomRecord 770bb7c2cdcSGreg Roach // has a cache of pending changes, we cannot use it to create a 771bb7c2cdcSGreg Roach // record with a newly created pending change. 77224ec66ceSGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 773304f20d5SGreg Roach } 7748586983fSGreg Roach 7758586983fSGreg Roach /** 7768586983fSGreg Roach * What is the most significant individual in this tree. 7778586983fSGreg Roach * 7788586983fSGreg Roach * @param User $user 7798586983fSGreg Roach * 7808586983fSGreg Roach * @return Individual 7818586983fSGreg Roach */ 782c1010edaSGreg Roach public function significantIndividual(User $user): Individual 783c1010edaSGreg Roach { 7848586983fSGreg Roach static $individual; // Only query the DB once. 7858586983fSGreg Roach 7868586983fSGreg Roach if (!$individual && $this->getUserPreference($user, 'rootid')) { 7878586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 7888586983fSGreg Roach } 7898586983fSGreg Roach if (!$individual && $this->getUserPreference($user, 'gedcomid')) { 7908586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 7918586983fSGreg Roach } 7928586983fSGreg Roach if (!$individual) { 7938586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 7948586983fSGreg Roach } 7958586983fSGreg Roach if (!$individual) { 7968586983fSGreg Roach $individual = Individual::getInstance( 7978586983fSGreg Roach Database::prepare( 7985fe1add5SGreg Roach "SELECT MIN(i_id) FROM `##individuals` WHERE i_file = :tree_id" 7995fe1add5SGreg Roach )->execute([ 8005fe1add5SGreg Roach 'tree_id' => $this->getTreeId(), 8015fe1add5SGreg Roach ])->fetchOne(), 8025fe1add5SGreg Roach $this 8035fe1add5SGreg Roach ); 8045fe1add5SGreg Roach } 8055fe1add5SGreg Roach if (!$individual) { 8065fe1add5SGreg Roach // always return a record 8075fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 8085fe1add5SGreg Roach } 8095fe1add5SGreg Roach 8105fe1add5SGreg Roach return $individual; 8115fe1add5SGreg Roach } 8125fe1add5SGreg Roach 8135fe1add5SGreg Roach /** 8145fe1add5SGreg Roach * Get significant information from this page, to allow other pages such as 8155fe1add5SGreg Roach * charts and reports to initialise with the same records 8165fe1add5SGreg Roach * 8175fe1add5SGreg Roach * @return Individual 8185fe1add5SGreg Roach */ 819c1010edaSGreg Roach public function getSignificantIndividual() 820c1010edaSGreg Roach { 8215fe1add5SGreg Roach static $individual; // Only query the DB once. 8225fe1add5SGreg Roach 8235fe1add5SGreg Roach if (!$individual && $this->getUserPreference(Auth::user(), 'rootid')) { 8245fe1add5SGreg Roach $individual = Individual::getInstance($this->getUserPreference(Auth::user(), 'rootid'), $this); 8255fe1add5SGreg Roach } 8265fe1add5SGreg Roach if (!$individual && $this->getUserPreference(Auth::user(), 'gedcomid')) { 8275fe1add5SGreg Roach $individual = Individual::getInstance($this->getUserPreference(Auth::user(), 'gedcomid'), $this); 8285fe1add5SGreg Roach } 8295fe1add5SGreg Roach if (!$individual) { 8305fe1add5SGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 8315fe1add5SGreg Roach } 8325fe1add5SGreg Roach if (!$individual) { 8335fe1add5SGreg Roach $individual = Individual::getInstance( 8345fe1add5SGreg Roach Database::prepare( 8358586983fSGreg Roach "SELECT MIN(i_id) FROM `##individuals` WHERE i_file=?" 8368586983fSGreg Roach )->execute([$this->getTreeId()])->fetchOne(), 8378586983fSGreg Roach $this 8388586983fSGreg Roach ); 8398586983fSGreg Roach } 8408586983fSGreg Roach if (!$individual) { 8418586983fSGreg Roach // always return a record 8428586983fSGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 8438586983fSGreg Roach } 8448586983fSGreg Roach 8458586983fSGreg Roach return $individual; 8468586983fSGreg Roach } 847a25f0a04SGreg Roach} 848