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; 20a25f0a04SGreg Roachuse PDOException; 21a25f0a04SGreg Roach 22a25f0a04SGreg Roach/** 2376692c8bSGreg Roach * Provide an interface to the wt_gedcom table. 24a25f0a04SGreg Roach */ 25*c1010edaSGreg Roachclass Tree 26*c1010edaSGreg Roach{ 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 */ 62*c1010edaSGreg Roach private function __construct($tree_id, $tree_name, $tree_title) 63*c1010edaSGreg Roach { 64518bbdc1SGreg Roach $this->tree_id = $tree_id; 65a25f0a04SGreg Roach $this->name = $tree_name; 66a25f0a04SGreg Roach $this->title = $tree_title; 6713abd6f3SGreg Roach $this->fact_privacy = []; 6813abd6f3SGreg Roach $this->individual_privacy = []; 6913abd6f3SGreg Roach $this->individual_fact_privacy = []; 70518bbdc1SGreg Roach 71518bbdc1SGreg Roach // Load the privacy settings for this tree 72518bbdc1SGreg Roach $rows = Database::prepare( 73e5588fb0SGreg 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" . 74518bbdc1SGreg Roach " FROM `##default_resn` WHERE gedcom_id = :tree_id" 7513abd6f3SGreg Roach )->execute([ 764b9ff166SGreg Roach 'priv_public' => Auth::PRIV_PRIVATE, 774b9ff166SGreg Roach 'priv_user' => Auth::PRIV_USER, 784b9ff166SGreg Roach 'priv_none' => Auth::PRIV_NONE, 794b9ff166SGreg Roach 'priv_hide' => Auth::PRIV_HIDE, 80cbc1590aSGreg Roach 'tree_id' => $this->tree_id, 8113abd6f3SGreg Roach ])->fetchAll(); 82518bbdc1SGreg Roach 83518bbdc1SGreg Roach foreach ($rows as $row) { 84518bbdc1SGreg Roach if ($row->xref !== null) { 85518bbdc1SGreg Roach if ($row->tag_type !== null) { 86518bbdc1SGreg Roach $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int)$row->resn; 87518bbdc1SGreg Roach } else { 88518bbdc1SGreg Roach $this->individual_privacy[$row->xref] = (int)$row->resn; 89518bbdc1SGreg Roach } 90518bbdc1SGreg Roach } else { 91518bbdc1SGreg Roach $this->fact_privacy[$row->tag_type] = (int)$row->resn; 92518bbdc1SGreg Roach } 93518bbdc1SGreg Roach } 94a25f0a04SGreg Roach } 95a25f0a04SGreg Roach 96a25f0a04SGreg Roach /** 97a25f0a04SGreg Roach * The ID of this tree 98a25f0a04SGreg Roach * 99cbc1590aSGreg Roach * @return int 100a25f0a04SGreg Roach */ 101*c1010edaSGreg Roach public function getTreeId() 102*c1010edaSGreg Roach { 103518bbdc1SGreg Roach return $this->tree_id; 104a25f0a04SGreg Roach } 105a25f0a04SGreg Roach 106a25f0a04SGreg Roach /** 107a25f0a04SGreg Roach * The name of this tree 108a25f0a04SGreg Roach * 109a25f0a04SGreg Roach * @return string 110a25f0a04SGreg Roach */ 111*c1010edaSGreg Roach public function getName() 112*c1010edaSGreg Roach { 113a25f0a04SGreg Roach return $this->name; 114a25f0a04SGreg Roach } 115a25f0a04SGreg Roach 116a25f0a04SGreg Roach /** 117a25f0a04SGreg Roach * The title of this tree 118a25f0a04SGreg Roach * 119a25f0a04SGreg Roach * @return string 120a25f0a04SGreg Roach */ 121*c1010edaSGreg Roach public function getTitle() 122*c1010edaSGreg Roach { 123a25f0a04SGreg Roach return $this->title; 124a25f0a04SGreg Roach } 125a25f0a04SGreg Roach 126a25f0a04SGreg Roach /** 127518bbdc1SGreg Roach * The fact-level privacy for this tree. 128518bbdc1SGreg Roach * 129e2052359SGreg Roach * @return int[] 130518bbdc1SGreg Roach */ 131*c1010edaSGreg Roach public function getFactPrivacy() 132*c1010edaSGreg Roach { 133518bbdc1SGreg Roach return $this->fact_privacy; 134518bbdc1SGreg Roach } 135518bbdc1SGreg Roach 136518bbdc1SGreg Roach /** 137518bbdc1SGreg Roach * The individual-level privacy for this tree. 138518bbdc1SGreg Roach * 139e2052359SGreg Roach * @return int[] 140518bbdc1SGreg Roach */ 141*c1010edaSGreg Roach public function getIndividualPrivacy() 142*c1010edaSGreg Roach { 143518bbdc1SGreg Roach return $this->individual_privacy; 144518bbdc1SGreg Roach } 145518bbdc1SGreg Roach 146518bbdc1SGreg Roach /** 147518bbdc1SGreg Roach * The individual-fact-level privacy for this tree. 148518bbdc1SGreg Roach * 149adac8af1SGreg Roach * @return int[][] 150518bbdc1SGreg Roach */ 151*c1010edaSGreg Roach public function getIndividualFactPrivacy() 152*c1010edaSGreg Roach { 153518bbdc1SGreg Roach return $this->individual_fact_privacy; 154518bbdc1SGreg Roach } 155518bbdc1SGreg Roach 156518bbdc1SGreg Roach /** 157a25f0a04SGreg Roach * Get the tree’s configuration settings. 158a25f0a04SGreg Roach * 159a25f0a04SGreg Roach * @param string $setting_name 16015d603e7SGreg Roach * @param string $default 161a25f0a04SGreg Roach * 16215d603e7SGreg Roach * @return string 163a25f0a04SGreg Roach */ 164*c1010edaSGreg Roach public function getPreference($setting_name, $default = '') 165*c1010edaSGreg Roach { 16615d603e7SGreg Roach if (empty($this->preferences)) { 167a25f0a04SGreg Roach $this->preferences = Database::prepare( 168e5588fb0SGreg Roach "SELECT setting_name, setting_value FROM `##gedcom_setting` WHERE gedcom_id = ?" 16913abd6f3SGreg Roach )->execute([$this->tree_id])->fetchAssoc(); 170a25f0a04SGreg Roach } 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach if (array_key_exists($setting_name, $this->preferences)) { 173a25f0a04SGreg Roach return $this->preferences[$setting_name]; 174a25f0a04SGreg Roach } else { 175a25f0a04SGreg Roach return $default; 176a25f0a04SGreg Roach } 177a25f0a04SGreg Roach } 178a25f0a04SGreg Roach 179a25f0a04SGreg Roach /** 180a25f0a04SGreg Roach * Set the tree’s configuration settings. 181a25f0a04SGreg Roach * 182a25f0a04SGreg Roach * @param string $setting_name 183a25f0a04SGreg Roach * @param string $setting_value 184a25f0a04SGreg Roach * 185a25f0a04SGreg Roach * @return $this 186a25f0a04SGreg Roach */ 187*c1010edaSGreg Roach public function setPreference($setting_name, $setting_value) 188*c1010edaSGreg Roach { 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 */ 216*c1010edaSGreg Roach public function getUserPreference(User $user, $setting_name, $default = null) 217*c1010edaSGreg Roach { 218a25f0a04SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 219a25f0a04SGreg Roach // so it is quicker to fetch them all in one go. 220a25f0a04SGreg Roach if (!array_key_exists($user->getUserId(), $this->user_preferences)) { 221a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()] = Database::prepare( 222e5588fb0SGreg Roach "SELECT setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?" 223*c1010edaSGreg Roach )->execute([ 224*c1010edaSGreg Roach $user->getUserId(), 225*c1010edaSGreg Roach $this->tree_id, 226*c1010edaSGreg Roach ])->fetchAssoc(); 227a25f0a04SGreg Roach } 228a25f0a04SGreg Roach 229a25f0a04SGreg Roach if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) { 230a25f0a04SGreg Roach return $this->user_preferences[$user->getUserId()][$setting_name]; 231a25f0a04SGreg Roach } else { 232a25f0a04SGreg Roach return $default; 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach 236a25f0a04SGreg Roach /** 237a25f0a04SGreg Roach * Set the tree’s user-configuration settings. 238a25f0a04SGreg Roach * 239a25f0a04SGreg Roach * @param User $user 240a25f0a04SGreg Roach * @param string $setting_name 241a25f0a04SGreg Roach * @param string $setting_value 242a25f0a04SGreg Roach * 243a25f0a04SGreg Roach * @return $this 244a25f0a04SGreg Roach */ 245*c1010edaSGreg Roach public function setUserPreference(User $user, $setting_name, $setting_value) 246*c1010edaSGreg Roach { 247a25f0a04SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 248a25f0a04SGreg Roach // Update the database 249a25f0a04SGreg Roach if ($setting_value === null) { 250a25f0a04SGreg Roach Database::prepare( 251a25f0a04SGreg Roach "DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name" 25213abd6f3SGreg Roach )->execute([ 253518bbdc1SGreg Roach 'tree_id' => $this->tree_id, 254a25f0a04SGreg Roach 'user_id' => $user->getUserId(), 255a25f0a04SGreg Roach 'setting_name' => $setting_name, 25613abd6f3SGreg Roach ]); 257a25f0a04SGreg Roach } else { 258a25f0a04SGreg Roach Database::prepare( 259a25f0a04SGreg 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))" 26013abd6f3SGreg Roach )->execute([ 261a25f0a04SGreg Roach 'user_id' => $user->getUserId(), 262518bbdc1SGreg Roach 'tree_id' => $this->tree_id, 263a25f0a04SGreg Roach 'setting_name' => $setting_name, 264cbc1590aSGreg Roach 'setting_value' => $setting_value, 26513abd6f3SGreg Roach ]); 266a25f0a04SGreg Roach } 267a25f0a04SGreg Roach // Update our cache 268a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()][$setting_name] = $setting_value; 269a25f0a04SGreg Roach // Audit log of changes 27072292b7dSGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this); 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach 273a25f0a04SGreg Roach return $this; 274a25f0a04SGreg Roach } 275a25f0a04SGreg Roach 276a25f0a04SGreg Roach /** 277a25f0a04SGreg Roach * Can a user accept changes for this tree? 278a25f0a04SGreg Roach * 279a25f0a04SGreg Roach * @param User $user 280a25f0a04SGreg Roach * 281cbc1590aSGreg Roach * @return bool 282a25f0a04SGreg Roach */ 283*c1010edaSGreg Roach public function canAcceptChanges(User $user) 284*c1010edaSGreg Roach { 285a25f0a04SGreg Roach return Auth::isModerator($this, $user); 286a25f0a04SGreg Roach } 287a25f0a04SGreg Roach 288a25f0a04SGreg Roach /** 289a25f0a04SGreg Roach * Fetch all the trees that we have permission to access. 290a25f0a04SGreg Roach * 291a25f0a04SGreg Roach * @return Tree[] 292a25f0a04SGreg Roach */ 293*c1010edaSGreg Roach public static function getAll() 294*c1010edaSGreg Roach { 295a25f0a04SGreg Roach if (self::$trees === null) { 29613abd6f3SGreg Roach self::$trees = []; 297a25f0a04SGreg Roach $rows = Database::prepare( 298e5588fb0SGreg Roach "SELECT g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" . 299a25f0a04SGreg Roach " FROM `##gedcom` g" . 300a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" . 301a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" . 302a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" . 303a25f0a04SGreg Roach " LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" . 304a25f0a04SGreg Roach " WHERE " . 305a25f0a04SGreg Roach " g.gedcom_id>0 AND (" . // exclude the "template" tree 306a25f0a04SGreg Roach " EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all 307a25f0a04SGreg Roach " ) OR (" . 3088cca4ddeSGreg Roach " (gs2.setting_value = 1 OR ugs.setting_value = 'admin') AND (" . // Allow imported trees, with either: 309a25f0a04SGreg Roach " gs3.setting_value <> 1 OR" . // visitor access 310a25f0a04SGreg Roach " IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access 311a25f0a04SGreg Roach " )" . 312a25f0a04SGreg Roach " )" . 313a25f0a04SGreg Roach " ORDER BY g.sort_order, 3" 314*c1010edaSGreg Roach )->execute([ 315*c1010edaSGreg Roach Auth::id(), 316*c1010edaSGreg Roach Auth::id(), 317*c1010edaSGreg Roach ])->fetchAll(); 318a25f0a04SGreg Roach foreach ($rows as $row) { 3193f7ece23SGreg Roach self::$trees[$row->tree_name] = new self((int)$row->tree_id, $row->tree_name, $row->tree_title); 320a25f0a04SGreg Roach } 321a25f0a04SGreg Roach } 322a25f0a04SGreg Roach 323a25f0a04SGreg Roach return self::$trees; 324a25f0a04SGreg Roach } 325a25f0a04SGreg Roach 326a25f0a04SGreg Roach /** 327d2cdeb3fSGreg Roach * Find the tree with a specific ID. 328a25f0a04SGreg Roach * 329cbc1590aSGreg Roach * @param int $tree_id 330cbc1590aSGreg Roach * 331cbc1590aSGreg Roach * @throws \DomainException 332a25f0a04SGreg Roach * 333a25f0a04SGreg Roach * @return Tree 334a25f0a04SGreg Roach */ 335*c1010edaSGreg Roach public static function findById($tree_id) 336*c1010edaSGreg 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 */ 352*c1010edaSGreg Roach public static function findByName($tree_name) 353*c1010edaSGreg 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 */ 369*c1010edaSGreg Roach public static function getIdList() 370*c1010edaSGreg 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 */ 385*c1010edaSGreg Roach public static function getNameList() 386*c1010edaSGreg 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 */ 403*c1010edaSGreg Roach public static function create($tree_name, $tree_title) 404*c1010edaSGreg 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... 481*c1010edaSGreg Roach $john_doe = /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 482*c1010edaSGreg 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 */ 500*c1010edaSGreg Roach public function hasPendingEdit() 501*c1010edaSGreg 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 516a25f0a04SGreg Roach */ 517*c1010edaSGreg Roach public function deleteGenealogyData($keep_media) 518*c1010edaSGreg Roach { 51913abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]); 52013abd6f3SGreg Roach Database::prepare("DELETE FROM `##individuals` WHERE i_file = ?")->execute([$this->tree_id]); 52113abd6f3SGreg Roach Database::prepare("DELETE FROM `##families` WHERE f_file = ?")->execute([$this->tree_id]); 52213abd6f3SGreg Roach Database::prepare("DELETE FROM `##sources` WHERE s_file = ?")->execute([$this->tree_id]); 52313abd6f3SGreg Roach Database::prepare("DELETE FROM `##other` WHERE o_file = ?")->execute([$this->tree_id]); 52413abd6f3SGreg Roach Database::prepare("DELETE FROM `##places` WHERE p_file = ?")->execute([$this->tree_id]); 52513abd6f3SGreg Roach Database::prepare("DELETE FROM `##placelinks` WHERE pl_file = ?")->execute([$this->tree_id]); 52613abd6f3SGreg Roach Database::prepare("DELETE FROM `##name` WHERE n_file = ?")->execute([$this->tree_id]); 52713abd6f3SGreg Roach Database::prepare("DELETE FROM `##dates` WHERE d_file = ?")->execute([$this->tree_id]); 52813abd6f3SGreg Roach Database::prepare("DELETE FROM `##change` WHERE gedcom_id = ?")->execute([$this->tree_id]); 529a25f0a04SGreg Roach 530a25f0a04SGreg Roach if ($keep_media) { 53113abd6f3SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute([$this->tree_id]); 532a25f0a04SGreg Roach } else { 53313abd6f3SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =?")->execute([$this->tree_id]); 53413abd6f3SGreg Roach Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute([$this->tree_id]); 53523ed9d24SGreg Roach Database::prepare("DELETE FROM `##media_file` WHERE m_file =?")->execute([$this->tree_id]); 536a25f0a04SGreg Roach } 537a25f0a04SGreg Roach } 538a25f0a04SGreg Roach 539a25f0a04SGreg Roach /** 540a25f0a04SGreg Roach * Delete everything relating to a tree 541a25f0a04SGreg Roach */ 542*c1010edaSGreg Roach public function delete() 543*c1010edaSGreg Roach { 544a25f0a04SGreg Roach // If this is the default tree, then unset it 545ef2fd529SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 546a25f0a04SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 547a25f0a04SGreg Roach } 548a25f0a04SGreg Roach 549a25f0a04SGreg Roach $this->deleteGenealogyData(false); 550a25f0a04SGreg Roach 55113abd6f3SGreg Roach Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute([$this->tree_id]); 55213abd6f3SGreg Roach Database::prepare("DELETE FROM `##block` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55313abd6f3SGreg Roach Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55413abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55513abd6f3SGreg Roach Database::prepare("DELETE FROM `##module_privacy` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55613abd6f3SGreg Roach Database::prepare("DELETE FROM `##hit_counter` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55713abd6f3SGreg Roach Database::prepare("DELETE FROM `##default_resn` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55813abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]); 55913abd6f3SGreg Roach Database::prepare("DELETE FROM `##log` WHERE gedcom_id = ?")->execute([$this->tree_id]); 56013abd6f3SGreg Roach Database::prepare("DELETE FROM `##gedcom` WHERE gedcom_id = ?")->execute([$this->tree_id]); 561a25f0a04SGreg Roach 562a25f0a04SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 563a25f0a04SGreg Roach self::$trees = null; 564a25f0a04SGreg Roach } 565a25f0a04SGreg Roach 566a25f0a04SGreg Roach /** 567a25f0a04SGreg Roach * Export the tree to a GEDCOM file 568a25f0a04SGreg Roach * 5695792757eSGreg Roach * @param resource $stream 570a25f0a04SGreg Roach */ 571*c1010edaSGreg Roach public function exportGedcom($stream) 572*c1010edaSGreg Roach { 5735792757eSGreg Roach $stmt = Database::prepare( 574e56ef01aSGreg Roach "SELECT i_gedcom AS gedcom, i_id AS xref, 1 AS n FROM `##individuals` WHERE i_file = :tree_id_1" . 5755792757eSGreg Roach " UNION ALL " . 576e56ef01aSGreg Roach "SELECT f_gedcom AS gedcom, f_id AS xref, 2 AS n FROM `##families` WHERE f_file = :tree_id_2" . 5775792757eSGreg Roach " UNION ALL " . 578e56ef01aSGreg Roach "SELECT s_gedcom AS gedcom, s_id AS xref, 3 AS n FROM `##sources` WHERE s_file = :tree_id_3" . 5795792757eSGreg Roach " UNION ALL " . 580e56ef01aSGreg 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')" . 5815792757eSGreg Roach " UNION ALL " . 582e56ef01aSGreg Roach "SELECT m_gedcom AS gedcom, m_id AS xref, 5 AS n FROM `##media` WHERE m_file = :tree_id_5" . 583e56ef01aSGreg Roach " ORDER BY n, LENGTH(xref), xref" 58413abd6f3SGreg Roach )->execute([ 5855792757eSGreg Roach 'tree_id_1' => $this->tree_id, 5865792757eSGreg Roach 'tree_id_2' => $this->tree_id, 5875792757eSGreg Roach 'tree_id_3' => $this->tree_id, 5885792757eSGreg Roach 'tree_id_4' => $this->tree_id, 589cbc1590aSGreg Roach 'tree_id_5' => $this->tree_id, 59013abd6f3SGreg Roach ]); 591a25f0a04SGreg Roach 5923d7a8a4cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this)); 593a214e186SGreg Roach while (($row = $stmt->fetch()) !== false) { 5943d7a8a4cSGreg Roach $buffer .= FunctionsExport::reformatRecord($row->gedcom); 595a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 5965792757eSGreg Roach fwrite($stream, $buffer); 597a25f0a04SGreg Roach $buffer = ''; 598a25f0a04SGreg Roach } 599a25f0a04SGreg Roach } 6000f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 601195d09d8SGreg Roach $stmt->closeCursor(); 602a25f0a04SGreg Roach } 603a25f0a04SGreg Roach 604a25f0a04SGreg Roach /** 605a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 606a25f0a04SGreg Roach * 607a25f0a04SGreg Roach * @param string $path The full path to the (possibly temporary) file. 608a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 609a25f0a04SGreg Roach * 610a25f0a04SGreg Roach * @throws \Exception 611a25f0a04SGreg Roach */ 612*c1010edaSGreg Roach public function importGedcomFile($path, $filename) 613*c1010edaSGreg Roach { 614a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 615a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 616a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 617a25f0a04SGreg Roach // each block. 618a25f0a04SGreg Roach 619a25f0a04SGreg Roach $file_data = ''; 620a25f0a04SGreg Roach $fp = fopen($path, 'rb'); 621a25f0a04SGreg Roach 622a25f0a04SGreg Roach // Don’t allow the user to cancel the request. We do not want to be left with an incomplete transaction. 623a25f0a04SGreg Roach ignore_user_abort(true); 624a25f0a04SGreg Roach 625ed1bbedbSGreg Roach $this->deleteGenealogyData($this->getPreference('keep_media')); 626a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 627a25f0a04SGreg Roach $this->setPreference('imported', '0'); 628a25f0a04SGreg Roach 629a25f0a04SGreg Roach while (!feof($fp)) { 630a25f0a04SGreg Roach $file_data .= fread($fp, 65536); 631a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 632a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 633a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 634a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 635a25f0a04SGreg Roach break; 636a25f0a04SGreg Roach } 637a25f0a04SGreg Roach } 638a25f0a04SGreg Roach if ($pos) { 639a25f0a04SGreg Roach Database::prepare( 640a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 641*c1010edaSGreg Roach )->execute([ 642*c1010edaSGreg Roach $this->tree_id, 643*c1010edaSGreg Roach substr($file_data, 0, $pos), 644*c1010edaSGreg Roach ]); 645a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 646a25f0a04SGreg Roach } 647a25f0a04SGreg Roach } 648a25f0a04SGreg Roach Database::prepare( 649a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 650*c1010edaSGreg Roach )->execute([ 651*c1010edaSGreg Roach $this->tree_id, 652*c1010edaSGreg Roach $file_data, 653*c1010edaSGreg Roach ]); 654a25f0a04SGreg Roach 655a25f0a04SGreg Roach fclose($fp); 656a25f0a04SGreg Roach } 657304f20d5SGreg Roach 658304f20d5SGreg Roach /** 659b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 660b90d8accSGreg Roach * 661b90d8accSGreg Roach * @return string 662b90d8accSGreg Roach */ 663*c1010edaSGreg Roach public function getNewXref() 664*c1010edaSGreg Roach { 665a214e186SGreg Roach $prefix = 'X'; 666b90d8accSGreg Roach 667971d66c8SGreg Roach $increment = 1.0; 668b90d8accSGreg Roach do { 669b90d8accSGreg Roach // Use LAST_INSERT_ID(expr) to provide a transaction-safe sequence. See 670b90d8accSGreg Roach // http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id 671b90d8accSGreg Roach $statement = Database::prepare( 672a214e186SGreg Roach "UPDATE `##site_setting` SET setting_value = LAST_INSERT_ID(setting_value + :increment) WHERE setting_name = 'next_xref'" 673b90d8accSGreg Roach ); 67413abd6f3SGreg Roach $statement->execute([ 675971d66c8SGreg Roach 'increment' => (int)$increment, 67613abd6f3SGreg Roach ]); 677b90d8accSGreg Roach 678b90d8accSGreg Roach if ($statement->rowCount() === 0) { 679b90d8accSGreg Roach // First time we've used this record type. 680a214e186SGreg Roach Site::setPreference('next_xref', '1'); 681b90d8accSGreg Roach $num = 1; 682b90d8accSGreg Roach } else { 683b90d8accSGreg Roach $num = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne(); 684b90d8accSGreg Roach } 685b90d8accSGreg Roach 686a214e186SGreg Roach $xref = $prefix . $num; 687a214e186SGreg Roach 688b90d8accSGreg Roach // Records may already exist with this sequence number. 689b90d8accSGreg Roach $already_used = Database::prepare( 690a214e186SGreg Roach "SELECT" . 691a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##individuals` WHERE i_id = :i_id) OR" . 692a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##families` WHERE f_id = :f_id) OR" . 693a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##sources` WHERE s_id = :s_id) OR" . 694a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##media` WHERE m_id = :m_id) OR" . 695a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##other` WHERE o_id = :o_id) OR" . 696a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##change` WHERE xref = :xref)" 69713abd6f3SGreg Roach )->execute([ 698a214e186SGreg Roach 'i_id' => $xref, 699a214e186SGreg Roach 'f_id' => $xref, 700a214e186SGreg Roach 's_id' => $xref, 701a214e186SGreg Roach 'm_id' => $xref, 702a214e186SGreg Roach 'o_id' => $xref, 703a214e186SGreg Roach 'xref' => $xref, 70413abd6f3SGreg Roach ])->fetchOne(); 705971d66c8SGreg Roach 706971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 707971d66c8SGreg Roach // existing data in a reasonable time. 708971d66c8SGreg Roach $increment *= 1.01; 709a214e186SGreg Roach } while ($already_used !== '0'); 710b90d8accSGreg Roach 711a214e186SGreg Roach return $xref; 712b90d8accSGreg Roach } 713b90d8accSGreg Roach 714b90d8accSGreg Roach /** 715304f20d5SGreg Roach * Create a new record from GEDCOM data. 716304f20d5SGreg Roach * 717304f20d5SGreg Roach * @param string $gedcom 718304f20d5SGreg Roach * 719304f20d5SGreg Roach * @throws \Exception 720cbc1590aSGreg Roach * 72115d603e7SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 722304f20d5SGreg Roach */ 723*c1010edaSGreg Roach public function createRecord($gedcom) 724*c1010edaSGreg Roach { 725304f20d5SGreg Roach if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom, $match)) { 726304f20d5SGreg Roach $xref = $match[1]; 727304f20d5SGreg Roach $type = $match[2]; 728304f20d5SGreg Roach } else { 729304f20d5SGreg Roach throw new \Exception('Invalid argument to GedcomRecord::createRecord(' . $gedcom . ')'); 730304f20d5SGreg Roach } 731304f20d5SGreg Roach if (strpos("\r", $gedcom) !== false) { 732304f20d5SGreg Roach // MSDOS line endings will break things in horrible ways 733304f20d5SGreg Roach throw new \Exception('Evil line endings found in GedcomRecord::createRecord(' . $gedcom . ')'); 734304f20d5SGreg Roach } 735304f20d5SGreg Roach 736304f20d5SGreg Roach // webtrees creates XREFs containing digits. Anything else (e.g. “new”) is just a placeholder. 737304f20d5SGreg Roach if (!preg_match('/\d/', $xref)) { 738a214e186SGreg Roach $xref = $this->getNewXref(); 739304f20d5SGreg Roach $gedcom = preg_replace('/^0 @(' . WT_REGEX_XREF . ')@/', '0 @' . $xref . '@', $gedcom); 740304f20d5SGreg Roach } 741304f20d5SGreg Roach 742304f20d5SGreg Roach // Create a change record, if not already present 743304f20d5SGreg Roach if (!preg_match('/\n1 CHAN/', $gedcom)) { 744304f20d5SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 745304f20d5SGreg Roach } 746304f20d5SGreg Roach 747304f20d5SGreg Roach // Create a pending change 748304f20d5SGreg Roach Database::prepare( 749304f20d5SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)" 75013abd6f3SGreg Roach )->execute([ 751bb7c2cdcSGreg Roach $this->tree_id, 752304f20d5SGreg Roach $xref, 753304f20d5SGreg Roach $gedcom, 754cbc1590aSGreg Roach Auth::id(), 75513abd6f3SGreg Roach ]); 756304f20d5SGreg Roach 757847d5489SGreg Roach Log::addEditLog('Create: ' . $type . ' ' . $xref, $this); 758304f20d5SGreg Roach 759304f20d5SGreg Roach // Accept this pending change 760304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 761cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 762304f20d5SGreg Roach } 763bb7c2cdcSGreg Roach // Return the newly created record. Note that since GedcomRecord 764bb7c2cdcSGreg Roach // has a cache of pending changes, we cannot use it to create a 765bb7c2cdcSGreg Roach // record with a newly created pending change. 76624ec66ceSGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 767304f20d5SGreg Roach } 7688586983fSGreg Roach 7698586983fSGreg Roach /** 7708586983fSGreg Roach * What is the most significant individual in this tree. 7718586983fSGreg Roach * 7728586983fSGreg Roach * @param User $user 7738586983fSGreg Roach * 7748586983fSGreg Roach * @return Individual 7758586983fSGreg Roach */ 776*c1010edaSGreg Roach public function significantIndividual(User $user): Individual 777*c1010edaSGreg Roach { 7788586983fSGreg Roach static $individual; // Only query the DB once. 7798586983fSGreg Roach 7808586983fSGreg Roach if (!$individual && $this->getUserPreference($user, 'rootid')) { 7818586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 7828586983fSGreg Roach } 7838586983fSGreg Roach if (!$individual && $this->getUserPreference($user, 'gedcomid')) { 7848586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 7858586983fSGreg Roach } 7868586983fSGreg Roach if (!$individual) { 7878586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 7888586983fSGreg Roach } 7898586983fSGreg Roach if (!$individual) { 7908586983fSGreg Roach $individual = Individual::getInstance( 7918586983fSGreg Roach Database::prepare( 7925fe1add5SGreg Roach "SELECT MIN(i_id) FROM `##individuals` WHERE i_file = :tree_id" 7935fe1add5SGreg Roach )->execute([ 7945fe1add5SGreg Roach 'tree_id' => $this->getTreeId(), 7955fe1add5SGreg Roach ])->fetchOne(), 7965fe1add5SGreg Roach $this 7975fe1add5SGreg Roach ); 7985fe1add5SGreg Roach } 7995fe1add5SGreg Roach if (!$individual) { 8005fe1add5SGreg Roach // always return a record 8015fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 8025fe1add5SGreg Roach } 8035fe1add5SGreg Roach 8045fe1add5SGreg Roach return $individual; 8055fe1add5SGreg Roach } 8065fe1add5SGreg Roach 8075fe1add5SGreg Roach /** 8085fe1add5SGreg Roach * Get significant information from this page, to allow other pages such as 8095fe1add5SGreg Roach * charts and reports to initialise with the same records 8105fe1add5SGreg Roach * 8115fe1add5SGreg Roach * @return Individual 8125fe1add5SGreg Roach */ 813*c1010edaSGreg Roach public function getSignificantIndividual() 814*c1010edaSGreg Roach { 8155fe1add5SGreg Roach static $individual; // Only query the DB once. 8165fe1add5SGreg Roach 8175fe1add5SGreg Roach if (!$individual && $this->getUserPreference(Auth::user(), 'rootid')) { 8185fe1add5SGreg Roach $individual = Individual::getInstance($this->getUserPreference(Auth::user(), 'rootid'), $this); 8195fe1add5SGreg Roach } 8205fe1add5SGreg Roach if (!$individual && $this->getUserPreference(Auth::user(), 'gedcomid')) { 8215fe1add5SGreg Roach $individual = Individual::getInstance($this->getUserPreference(Auth::user(), 'gedcomid'), $this); 8225fe1add5SGreg Roach } 8235fe1add5SGreg Roach if (!$individual) { 8245fe1add5SGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 8255fe1add5SGreg Roach } 8265fe1add5SGreg Roach if (!$individual) { 8275fe1add5SGreg Roach $individual = Individual::getInstance( 8285fe1add5SGreg Roach Database::prepare( 8298586983fSGreg Roach "SELECT MIN(i_id) FROM `##individuals` WHERE i_file=?" 8308586983fSGreg Roach )->execute([$this->getTreeId()])->fetchOne(), 8318586983fSGreg Roach $this 8328586983fSGreg Roach ); 8338586983fSGreg Roach } 8348586983fSGreg Roach if (!$individual) { 8358586983fSGreg Roach // always return a record 8368586983fSGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 8378586983fSGreg Roach } 8388586983fSGreg Roach 8398586983fSGreg Roach return $individual; 8408586983fSGreg Roach } 841a25f0a04SGreg Roach} 842