1a25f0a04SGreg Roach<?php 2*dd04c183SGreg Roachnamespace Fisharebest\Webtrees; 3a25f0a04SGreg Roach 4a25f0a04SGreg Roach/** 5a25f0a04SGreg Roach * webtrees: online genealogy 6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team 7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 10a25f0a04SGreg Roach * (at your option) any later version. 11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14a25f0a04SGreg Roach * GNU General Public License for more details. 15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 17a25f0a04SGreg Roach */ 18a25f0a04SGreg Roach 19a25f0a04SGreg Roachuse PDOException; 20a25f0a04SGreg Roach 21a25f0a04SGreg Roach/** 22a25f0a04SGreg Roach * Class Tree - Provide an interface to the wt_gedcom table 23a25f0a04SGreg Roach */ 24a25f0a04SGreg Roachclass Tree { 25a25f0a04SGreg Roach /** @var integer The tree's ID number */ 26a25f0a04SGreg Roach private $id; 27a25f0a04SGreg Roach /** @var string The tree's name */ 28a25f0a04SGreg Roach private $name; 29a25f0a04SGreg Roach /** @var string The tree's title */ 30a25f0a04SGreg Roach private $title; 31a25f0a04SGreg Roach 32a25f0a04SGreg Roach /** @var Tree[] All trees that we have permission to see. */ 33a25f0a04SGreg Roach private static $trees; 34a25f0a04SGreg Roach 35a25f0a04SGreg Roach /** @var string[] Cached copy of the wt_gedcom_setting table. */ 36a25f0a04SGreg Roach private $preferences; 37a25f0a04SGreg Roach 38a25f0a04SGreg Roach /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ 39a25f0a04SGreg Roach private $user_preferences = array(); 40a25f0a04SGreg Roach 41a25f0a04SGreg Roach /** 42a25f0a04SGreg Roach * Create a tree object. This is a private constructor - it can only 43a25f0a04SGreg Roach * be called from Tree::getAll() to ensure proper initialisation. 44a25f0a04SGreg Roach * 45a25f0a04SGreg Roach * @param integer $id 46a25f0a04SGreg Roach * @param string $tree_name 47a25f0a04SGreg Roach * @param string $tree_title 48a25f0a04SGreg Roach */ 49a25f0a04SGreg Roach private function __construct($id, $tree_name, $tree_title) { 50a25f0a04SGreg Roach $this->id = $id; 51a25f0a04SGreg Roach $this->name = $tree_name; 52a25f0a04SGreg Roach $this->title = $tree_title; 53a25f0a04SGreg Roach } 54a25f0a04SGreg Roach 55a25f0a04SGreg Roach /** 56a25f0a04SGreg Roach * The ID of this tree 57a25f0a04SGreg Roach * 58a25f0a04SGreg Roach * @return integer 59a25f0a04SGreg Roach */ 60a25f0a04SGreg Roach public function id() { 61a25f0a04SGreg Roach return $this->id; 62a25f0a04SGreg Roach } 63a25f0a04SGreg Roach 64a25f0a04SGreg Roach /** 65a25f0a04SGreg Roach * The name of this tree 66a25f0a04SGreg Roach * 67a25f0a04SGreg Roach * @return string 68a25f0a04SGreg Roach */ 69a25f0a04SGreg Roach public function name() { 70a25f0a04SGreg Roach return $this->name; 71a25f0a04SGreg Roach } 72a25f0a04SGreg Roach 73a25f0a04SGreg Roach /** 74a25f0a04SGreg Roach * The name of this tree 75a25f0a04SGreg Roach * 76a25f0a04SGreg Roach * @return string 77a25f0a04SGreg Roach */ 78a25f0a04SGreg Roach public function nameHtml() { 79a25f0a04SGreg Roach return Filter::escapeHtml($this->name); 80a25f0a04SGreg Roach } 81a25f0a04SGreg Roach 82a25f0a04SGreg Roach /** 83a25f0a04SGreg Roach * The name of this tree 84a25f0a04SGreg Roach * 85a25f0a04SGreg Roach * @return string 86a25f0a04SGreg Roach */ 87a25f0a04SGreg Roach public function nameUrl() { 88a25f0a04SGreg Roach return Filter::escapeUrl($this->name); 89a25f0a04SGreg Roach } 90a25f0a04SGreg Roach 91a25f0a04SGreg Roach /** 92a25f0a04SGreg Roach * The title of this tree 93a25f0a04SGreg Roach * 94a25f0a04SGreg Roach * @return string 95a25f0a04SGreg Roach */ 96a25f0a04SGreg Roach public function title() { 97a25f0a04SGreg Roach return $this->title; 98a25f0a04SGreg Roach } 99a25f0a04SGreg Roach 100a25f0a04SGreg Roach /** 101a25f0a04SGreg Roach * The title of this tree, with HTML markup 102a25f0a04SGreg Roach * 103a25f0a04SGreg Roach * @return string 104a25f0a04SGreg Roach */ 105a25f0a04SGreg Roach public function titleHtml() { 106a25f0a04SGreg Roach return Filter::escapeHtml($this->title); 107a25f0a04SGreg Roach } 108a25f0a04SGreg Roach 109a25f0a04SGreg Roach /** 110a25f0a04SGreg Roach * Get the tree’s configuration settings. 111a25f0a04SGreg Roach * 112a25f0a04SGreg Roach * @param string $setting_name 113a25f0a04SGreg Roach * @param string|null $default 114a25f0a04SGreg Roach * 115a25f0a04SGreg Roach * @return string|null 116a25f0a04SGreg Roach */ 117a25f0a04SGreg Roach public function getPreference($setting_name, $default = null) { 118a25f0a04SGreg Roach if ($this->preferences === null) { 119a25f0a04SGreg Roach $this->preferences = Database::prepare( 120a25f0a04SGreg Roach "SELECT SQL_CACHE setting_name, setting_value FROM `##gedcom_setting` WHERE gedcom_id = ?" 121a25f0a04SGreg Roach )->execute(array($this->id))->fetchAssoc(); 122a25f0a04SGreg Roach } 123a25f0a04SGreg Roach 124a25f0a04SGreg Roach if (array_key_exists($setting_name, $this->preferences)) { 125a25f0a04SGreg Roach return $this->preferences[$setting_name]; 126a25f0a04SGreg Roach } else { 127a25f0a04SGreg Roach return $default; 128a25f0a04SGreg Roach } 129a25f0a04SGreg Roach } 130a25f0a04SGreg Roach 131a25f0a04SGreg Roach /** 132a25f0a04SGreg Roach * Set the tree’s configuration settings. 133a25f0a04SGreg Roach * 134a25f0a04SGreg Roach * @param string $setting_name 135a25f0a04SGreg Roach * @param string $setting_value 136a25f0a04SGreg Roach * 137a25f0a04SGreg Roach * @return $this 138a25f0a04SGreg Roach */ 139a25f0a04SGreg Roach public function setPreference($setting_name, $setting_value) { 140a25f0a04SGreg Roach if ($setting_value !== $this->getPreference($setting_name)) { 141a25f0a04SGreg Roach // Update the database 142a25f0a04SGreg Roach if ($setting_value === null) { 143a25f0a04SGreg Roach Database::prepare( 144a25f0a04SGreg Roach "DELETE FROM `##gedcom_setting` WHERE gedcom_id = :tree_id AND setting_name = :setting_name" 145a25f0a04SGreg Roach )->execute(array( 146a25f0a04SGreg Roach 'tree_id' => $this->id, 147a25f0a04SGreg Roach 'setting_name' => $setting_name, 148a25f0a04SGreg Roach )); 149a25f0a04SGreg Roach } else { 150a25f0a04SGreg Roach Database::prepare( 151a25f0a04SGreg Roach "REPLACE INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" . 152a25f0a04SGreg Roach " VALUES (:tree_id, :setting_name, LEFT(:setting_value, 255))" 153a25f0a04SGreg Roach )->execute(array( 154a25f0a04SGreg Roach 'tree_id' => $this->id, 155a25f0a04SGreg Roach 'setting_name' => $setting_name, 156a25f0a04SGreg Roach 'setting_value' => $setting_value, 157a25f0a04SGreg Roach )); 158a25f0a04SGreg Roach } 159a25f0a04SGreg Roach // Update our cache 160a25f0a04SGreg Roach $this->preferences[$setting_name] = $setting_value; 161a25f0a04SGreg Roach // Audit log of changes 162a25f0a04SGreg Roach Log::addConfigurationLog('Tree setting "' . $setting_name . '" set to "' . $setting_value . '"', $this); 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach 165a25f0a04SGreg Roach return $this; 166a25f0a04SGreg Roach } 167a25f0a04SGreg Roach 168a25f0a04SGreg Roach /** 169a25f0a04SGreg Roach * Get the tree’s user-configuration settings. 170a25f0a04SGreg Roach * 171a25f0a04SGreg Roach * @param User $user 172a25f0a04SGreg Roach * @param string $setting_name 173a25f0a04SGreg Roach * @param string|null $default 174a25f0a04SGreg Roach * 175a25f0a04SGreg Roach * @return string 176a25f0a04SGreg Roach */ 177a25f0a04SGreg Roach public function getUserPreference(User $user, $setting_name, $default = null) { 178a25f0a04SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 179a25f0a04SGreg Roach // so it is quicker to fetch them all in one go. 180a25f0a04SGreg Roach if (!array_key_exists($user->getUserId(), $this->user_preferences)) { 181a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()] = Database::prepare( 182a25f0a04SGreg Roach "SELECT SQL_CACHE setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?" 183a25f0a04SGreg Roach )->execute(array($user->getUserId(), $this->id))->fetchAssoc(); 184a25f0a04SGreg Roach } 185a25f0a04SGreg Roach 186a25f0a04SGreg Roach if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) { 187a25f0a04SGreg Roach return $this->user_preferences[$user->getUserId()][$setting_name]; 188a25f0a04SGreg Roach } else { 189a25f0a04SGreg Roach return $default; 190a25f0a04SGreg Roach } 191a25f0a04SGreg Roach } 192a25f0a04SGreg Roach 193a25f0a04SGreg Roach /** 194a25f0a04SGreg Roach * Set the tree’s user-configuration settings. 195a25f0a04SGreg Roach * 196a25f0a04SGreg Roach * @param User $user 197a25f0a04SGreg Roach * @param string $setting_name 198a25f0a04SGreg Roach * @param string $setting_value 199a25f0a04SGreg Roach * 200a25f0a04SGreg Roach * @return $this 201a25f0a04SGreg Roach */ 202a25f0a04SGreg Roach public function setUserPreference(User $user, $setting_name, $setting_value) { 203a25f0a04SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 204a25f0a04SGreg Roach // Update the database 205a25f0a04SGreg Roach if ($setting_value === null) { 206a25f0a04SGreg Roach Database::prepare( 207a25f0a04SGreg Roach "DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name" 208a25f0a04SGreg Roach )->execute(array( 209a25f0a04SGreg Roach 'tree_id' => $this->id, 210a25f0a04SGreg Roach 'user_id' => $user->getUserId(), 211a25f0a04SGreg Roach 'setting_name' => $setting_name, 212a25f0a04SGreg Roach )); 213a25f0a04SGreg Roach } else { 214a25f0a04SGreg Roach Database::prepare( 215a25f0a04SGreg 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))" 216a25f0a04SGreg Roach )->execute(array( 217a25f0a04SGreg Roach 'user_id' => $user->getUserId(), 218a25f0a04SGreg Roach 'tree_id' => $this->id, 219a25f0a04SGreg Roach 'setting_name' => $setting_name, 220a25f0a04SGreg Roach 'setting_value' => $setting_value 221a25f0a04SGreg Roach )); 222a25f0a04SGreg Roach } 223a25f0a04SGreg Roach // Update our cache 224a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()][$setting_name] = $setting_value; 225a25f0a04SGreg Roach // Audit log of changes 226a25f0a04SGreg Roach Log::addConfigurationLog('Tree setting "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this); 227a25f0a04SGreg Roach } 228a25f0a04SGreg Roach 229a25f0a04SGreg Roach return $this; 230a25f0a04SGreg Roach } 231a25f0a04SGreg Roach 232a25f0a04SGreg Roach /** 233a25f0a04SGreg Roach * Can a user accept changes for this tree? 234a25f0a04SGreg Roach * 235a25f0a04SGreg Roach * @param User $user 236a25f0a04SGreg Roach * 237a25f0a04SGreg Roach * @return boolean 238a25f0a04SGreg Roach */ 239a25f0a04SGreg Roach public function canAcceptChanges(User $user) { 240a25f0a04SGreg Roach return Auth::isModerator($this, $user); 241a25f0a04SGreg Roach } 242a25f0a04SGreg Roach 243a25f0a04SGreg Roach /** 244a25f0a04SGreg Roach * Fetch all the trees that we have permission to access. 245a25f0a04SGreg Roach * 246a25f0a04SGreg Roach * @return Tree[] 247a25f0a04SGreg Roach */ 248a25f0a04SGreg Roach public static function getAll() { 249a25f0a04SGreg Roach if (self::$trees === null) { 250a25f0a04SGreg Roach self::$trees = array(); 251a25f0a04SGreg Roach $rows = Database::prepare( 252a25f0a04SGreg Roach "SELECT SQL_CACHE g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" . 253a25f0a04SGreg Roach " FROM `##gedcom` g" . 254a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" . 255a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" . 256a25f0a04SGreg Roach " LEFT JOIN `##gedcom_setting` gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" . 257a25f0a04SGreg Roach " LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" . 258a25f0a04SGreg Roach " WHERE " . 259a25f0a04SGreg Roach " g.gedcom_id>0 AND (" . // exclude the "template" tree 260a25f0a04SGreg Roach " EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all 261a25f0a04SGreg Roach " ) OR (" . 262a25f0a04SGreg Roach " gs2.setting_value = 1 AND (" . // Allow imported trees, with either: 263a25f0a04SGreg Roach " gs3.setting_value <> 1 OR" . // visitor access 264a25f0a04SGreg Roach " IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access 265a25f0a04SGreg Roach " )" . 266a25f0a04SGreg Roach " )" . 267a25f0a04SGreg Roach " ORDER BY g.sort_order, 3" 268a25f0a04SGreg Roach )->execute(array(Auth::id(), Auth::id()))->fetchAll(); 269a25f0a04SGreg Roach foreach ($rows as $row) { 270a25f0a04SGreg Roach self::$trees[$row->tree_id] = new self($row->tree_id, $row->tree_name, $row->tree_title); 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach } 273a25f0a04SGreg Roach 274a25f0a04SGreg Roach return self::$trees; 275a25f0a04SGreg Roach } 276a25f0a04SGreg Roach 277a25f0a04SGreg Roach /** 278a25f0a04SGreg Roach * Get the tree with a specific ID. 279a25f0a04SGreg Roach * 280a25f0a04SGreg Roach * @param integer $tree_id 281a25f0a04SGreg Roach * 282a25f0a04SGreg Roach * @return Tree 283a25f0a04SGreg Roach */ 284a25f0a04SGreg Roach public static function get($tree_id) { 285a25f0a04SGreg Roach $trees = self::getAll(); 286a25f0a04SGreg Roach 287a25f0a04SGreg Roach return $trees[$tree_id]; 288a25f0a04SGreg Roach } 289a25f0a04SGreg Roach 290a25f0a04SGreg Roach /** 291a25f0a04SGreg Roach * Create arguments to select_edit_control() 292a25f0a04SGreg Roach * Note - these will be escaped later 293a25f0a04SGreg Roach * 294a25f0a04SGreg Roach * @return string[] 295a25f0a04SGreg Roach */ 296a25f0a04SGreg Roach public static function getIdList() { 297a25f0a04SGreg Roach $list = array(); 298a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 299a25f0a04SGreg Roach $list[$tree->id] = $tree->title; 300a25f0a04SGreg Roach } 301a25f0a04SGreg Roach 302a25f0a04SGreg Roach return $list; 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach 305a25f0a04SGreg Roach /** 306a25f0a04SGreg Roach * Create arguments to select_edit_control() 307a25f0a04SGreg Roach * Note - these will be escaped later 308a25f0a04SGreg Roach * 309a25f0a04SGreg Roach * @return string[] 310a25f0a04SGreg Roach */ 311a25f0a04SGreg Roach public static function getNameList() { 312a25f0a04SGreg Roach $list = array(); 313a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 314a25f0a04SGreg Roach $list[$tree->name] = $tree->title; 315a25f0a04SGreg Roach } 316a25f0a04SGreg Roach 317a25f0a04SGreg Roach return $list; 318a25f0a04SGreg Roach } 319a25f0a04SGreg Roach 320a25f0a04SGreg Roach /** 321a25f0a04SGreg Roach * Find the ID number for a tree name 322a25f0a04SGreg Roach * 323a25f0a04SGreg Roach * @param integer $tree_name 324a25f0a04SGreg Roach * 325a25f0a04SGreg Roach * @return integer|null 326a25f0a04SGreg Roach */ 327a25f0a04SGreg Roach public static function getIdFromName($tree_name) { 328a25f0a04SGreg Roach foreach (self::getAll() as $tree_id => $tree) { 329a25f0a04SGreg Roach if ($tree->name == $tree_name) { 330a25f0a04SGreg Roach return $tree_id; 331a25f0a04SGreg Roach } 332a25f0a04SGreg Roach } 333a25f0a04SGreg Roach 334a25f0a04SGreg Roach return null; 335a25f0a04SGreg Roach } 336a25f0a04SGreg Roach 337a25f0a04SGreg Roach /** 338a25f0a04SGreg Roach * Find the tree name from a numeric ID. 339a25f0a04SGreg Roach * @param integer $tree_id 340a25f0a04SGreg Roach * 341a25f0a04SGreg Roach * @return string 342a25f0a04SGreg Roach */ 343a25f0a04SGreg Roach public static function getNameFromId($tree_id) { 344a25f0a04SGreg Roach return self::get($tree_id)->name; 345a25f0a04SGreg Roach } 346a25f0a04SGreg Roach 347a25f0a04SGreg Roach /** 348a25f0a04SGreg Roach * Create a new tree 349a25f0a04SGreg Roach * 350a25f0a04SGreg Roach * @param string $tree_name 351a25f0a04SGreg Roach * @param string $tree_title 352a25f0a04SGreg Roach * 353a25f0a04SGreg Roach * @return Tree 354a25f0a04SGreg Roach */ 355a25f0a04SGreg Roach public static function create($tree_name, $tree_title) { 356a25f0a04SGreg Roach try { 357a25f0a04SGreg Roach // Create a new tree 358a25f0a04SGreg Roach Database::prepare( 359a25f0a04SGreg Roach "INSERT INTO `##gedcom` (gedcom_name) VALUES (?)" 360a25f0a04SGreg Roach )->execute(array($tree_name)); 361a25f0a04SGreg Roach $tree_id = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne(); 362a25f0a04SGreg Roach } catch (PDOException $ex) { 363a25f0a04SGreg Roach // A tree with that name already exists? 364a25f0a04SGreg Roach return self::get(self::getIdFromName($tree_name)); 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach 367a25f0a04SGreg Roach // Update the list of trees - to include this new one 368a25f0a04SGreg Roach self::$trees = null; 369a25f0a04SGreg Roach $tree = self::get($tree_id); 370a25f0a04SGreg Roach 371a25f0a04SGreg Roach $tree->setPreference('imported', '0'); 372a25f0a04SGreg Roach $tree->setPreference('title', $tree_title); 373a25f0a04SGreg Roach 374a25f0a04SGreg Roach // Module privacy 375a25f0a04SGreg Roach Module::setDefaultAccess($tree_id); 376a25f0a04SGreg Roach 377a25f0a04SGreg Roach // Gedcom and privacy settings 378a25f0a04SGreg Roach $tree->setPreference('ADVANCED_NAME_FACTS', 'NICK,_AKA'); 379a25f0a04SGreg Roach $tree->setPreference('ADVANCED_PLAC_FACTS', ''); 380a25f0a04SGreg Roach $tree->setPreference('ALLOW_THEME_DROPDOWN', '1'); 381a25f0a04SGreg Roach $tree->setPreference('CALENDAR_FORMAT', 'gregorian'); 382a25f0a04SGreg Roach $tree->setPreference('CHART_BOX_TAGS', ''); 383a25f0a04SGreg Roach $tree->setPreference('COMMON_NAMES_ADD', ''); 384a25f0a04SGreg Roach $tree->setPreference('COMMON_NAMES_REMOVE', ''); 385a25f0a04SGreg Roach $tree->setPreference('COMMON_NAMES_THRESHOLD', '40'); 386a25f0a04SGreg Roach $tree->setPreference('CONTACT_USER_ID', Auth::id()); 387a25f0a04SGreg Roach $tree->setPreference('DEFAULT_PEDIGREE_GENERATIONS', '4'); 388a25f0a04SGreg Roach $tree->setPreference('EXPAND_RELATIVES_EVENTS', '0'); 389a25f0a04SGreg Roach $tree->setPreference('EXPAND_SOURCES', '0'); 390a25f0a04SGreg Roach $tree->setPreference('FAM_FACTS_ADD', 'CENS,MARR,RESI,SLGS,MARR_CIVIL,MARR_RELIGIOUS,MARR_PARTNERS,RESN'); 391a25f0a04SGreg Roach $tree->setPreference('FAM_FACTS_QUICK', 'MARR,DIV,_NMR'); 392a25f0a04SGreg Roach $tree->setPreference('FAM_FACTS_UNIQUE', 'NCHI,MARL,DIV,ANUL,DIVF,ENGA,MARB,MARC,MARS'); 393a25f0a04SGreg Roach $tree->setPreference('FAM_ID_PREFIX', 'F'); 394a25f0a04SGreg Roach $tree->setPreference('FORMAT_TEXT', 'markdown'); 395a25f0a04SGreg Roach $tree->setPreference('FULL_SOURCES', '0'); 396a25f0a04SGreg Roach $tree->setPreference('GEDCOM_ID_PREFIX', 'I'); 397a25f0a04SGreg Roach $tree->setPreference('GEDCOM_MEDIA_PATH', ''); 398a25f0a04SGreg Roach $tree->setPreference('GENERATE_UIDS', '0'); 399a25f0a04SGreg Roach $tree->setPreference('HIDE_GEDCOM_ERRORS', '1'); 400a25f0a04SGreg Roach $tree->setPreference('HIDE_LIVE_PEOPLE', '1'); 401a25f0a04SGreg Roach $tree->setPreference('INDI_FACTS_ADD', 'AFN,BIRT,DEAT,BURI,CREM,ADOP,BAPM,BARM,BASM,BLES,CHRA,CONF,FCOM,ORDN,NATU,EMIG,IMMI,CENS,PROB,WILL,GRAD,RETI,DSCR,EDUC,IDNO,NATI,NCHI,NMR,OCCU,PROP,RELI,RESI,SSN,TITL,BAPL,CONL,ENDL,SLGC,_MILI,ASSO,RESN'); 402a25f0a04SGreg Roach $tree->setPreference('INDI_FACTS_QUICK', 'BIRT,BURI,BAPM,CENS,DEAT,OCCU,RESI'); 403a25f0a04SGreg Roach $tree->setPreference('INDI_FACTS_UNIQUE', ''); 404a25f0a04SGreg Roach $tree->setPreference('KEEP_ALIVE_YEARS_BIRTH', ''); 405a25f0a04SGreg Roach $tree->setPreference('KEEP_ALIVE_YEARS_DEATH', ''); 406a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 407a25f0a04SGreg Roach $tree->setPreference('MAX_ALIVE_AGE', 120); 408a25f0a04SGreg Roach $tree->setPreference('MAX_DESCENDANCY_GENERATIONS', '15'); 409a25f0a04SGreg Roach $tree->setPreference('MAX_PEDIGREE_GENERATIONS', '10'); 410a25f0a04SGreg Roach $tree->setPreference('MEDIA_DIRECTORY', 'media/'); 411a25f0a04SGreg Roach $tree->setPreference('MEDIA_ID_PREFIX', 'M'); 412a25f0a04SGreg Roach $tree->setPreference('MEDIA_UPLOAD', WT_PRIV_USER); 413a25f0a04SGreg Roach $tree->setPreference('META_DESCRIPTION', ''); 414a25f0a04SGreg Roach $tree->setPreference('META_TITLE', WT_WEBTREES); 415a25f0a04SGreg Roach $tree->setPreference('NOTE_FACTS_ADD', 'SOUR,RESN'); 416a25f0a04SGreg Roach $tree->setPreference('NOTE_FACTS_QUICK', ''); 417a25f0a04SGreg Roach $tree->setPreference('NOTE_FACTS_UNIQUE', ''); 418a25f0a04SGreg Roach $tree->setPreference('NOTE_ID_PREFIX', 'N'); 419a25f0a04SGreg Roach $tree->setPreference('NO_UPDATE_CHAN', '0'); 420a25f0a04SGreg Roach $tree->setPreference('PEDIGREE_FULL_DETAILS', '1'); 421a25f0a04SGreg Roach $tree->setPreference('PEDIGREE_LAYOUT', '1'); 422a25f0a04SGreg Roach $tree->setPreference('PEDIGREE_ROOT_ID', ''); 423a25f0a04SGreg Roach $tree->setPreference('PEDIGREE_SHOW_GENDER', '0'); 424a25f0a04SGreg Roach $tree->setPreference('PREFER_LEVEL2_SOURCES', '1'); 425a25f0a04SGreg Roach $tree->setPreference('QUICK_REQUIRED_FACTS', 'BIRT,DEAT'); 426a25f0a04SGreg Roach $tree->setPreference('QUICK_REQUIRED_FAMFACTS', 'MARR'); 427a25f0a04SGreg Roach $tree->setPreference('REPO_FACTS_ADD', 'PHON,EMAIL,FAX,WWW,RESN'); 428a25f0a04SGreg Roach $tree->setPreference('REPO_FACTS_QUICK', ''); 429a25f0a04SGreg Roach $tree->setPreference('REPO_FACTS_UNIQUE', 'NAME,ADDR'); 430a25f0a04SGreg Roach $tree->setPreference('REPO_ID_PREFIX', 'R'); 431a25f0a04SGreg Roach $tree->setPreference('REQUIRE_AUTHENTICATION', '0'); 432a25f0a04SGreg Roach $tree->setPreference('SAVE_WATERMARK_IMAGE', '0'); 433a25f0a04SGreg Roach $tree->setPreference('SAVE_WATERMARK_THUMB', '0'); 434a25f0a04SGreg Roach $tree->setPreference('SHOW_AGE_DIFF', '0'); 435a25f0a04SGreg Roach $tree->setPreference('SHOW_COUNTER', '1'); 436a25f0a04SGreg Roach $tree->setPreference('SHOW_DEAD_PEOPLE', WT_PRIV_PUBLIC); 437a25f0a04SGreg Roach $tree->setPreference('SHOW_EST_LIST_DATES', '0'); 438a25f0a04SGreg Roach $tree->setPreference('SHOW_FACT_ICONS', '1'); 439a25f0a04SGreg Roach $tree->setPreference('SHOW_GEDCOM_RECORD', '0'); 440a25f0a04SGreg Roach $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', '1'); 441a25f0a04SGreg Roach $tree->setPreference('SHOW_LDS_AT_GLANCE', '0'); 442a25f0a04SGreg Roach $tree->setPreference('SHOW_LEVEL2_NOTES', '1'); 443a25f0a04SGreg Roach $tree->setPreference('SHOW_LIVING_NAMES', WT_PRIV_USER); 444a25f0a04SGreg Roach $tree->setPreference('SHOW_MEDIA_DOWNLOAD', '0'); 445a25f0a04SGreg Roach $tree->setPreference('SHOW_NO_WATERMARK', WT_PRIV_USER); 446a25f0a04SGreg Roach $tree->setPreference('SHOW_PARENTS_AGE', '1'); 447a25f0a04SGreg Roach $tree->setPreference('SHOW_PEDIGREE_PLACES', '9'); 448a25f0a04SGreg Roach $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', '0'); 449a25f0a04SGreg Roach $tree->setPreference('SHOW_PRIVATE_RELATIONSHIPS', '1'); 450a25f0a04SGreg Roach $tree->setPreference('SHOW_RELATIVES_EVENTS', '_BIRT_CHIL,_BIRT_SIBL,_MARR_CHIL,_MARR_PARE,_DEAT_CHIL,_DEAT_PARE,_DEAT_GPAR,_DEAT_SIBL,_DEAT_SPOU'); 451a25f0a04SGreg Roach $tree->setPreference('SHOW_STATS', '0'); 452a25f0a04SGreg Roach $tree->setPreference('SOURCE_ID_PREFIX', 'S'); 453a25f0a04SGreg Roach $tree->setPreference('SOUR_FACTS_ADD', 'NOTE,REPO,SHARED_NOTE,RESN'); 454a25f0a04SGreg Roach $tree->setPreference('SOUR_FACTS_QUICK', 'TEXT,NOTE,REPO'); 455a25f0a04SGreg Roach $tree->setPreference('SOUR_FACTS_UNIQUE', 'AUTH,ABBR,TITL,PUBL,TEXT'); 456a25f0a04SGreg Roach $tree->setPreference('SUBLIST_TRIGGER_I', '200'); 457a25f0a04SGreg Roach $tree->setPreference('SURNAME_LIST_STYLE', 'style2'); 458a25f0a04SGreg Roach switch (WT_LOCALE) { 459a25f0a04SGreg Roach case 'es': 460a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 461a25f0a04SGreg Roach break; 462a25f0a04SGreg Roach case 'is': 463a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 464a25f0a04SGreg Roach break; 465a25f0a04SGreg Roach case 'lt': 466a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 467a25f0a04SGreg Roach break; 468a25f0a04SGreg Roach case 'pl': 469a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 470a25f0a04SGreg Roach break; 471a25f0a04SGreg Roach case 'pt': 472a25f0a04SGreg Roach case 'pt-BR': 473a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 474a25f0a04SGreg Roach break; 475a25f0a04SGreg Roach default: 476a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 477a25f0a04SGreg Roach break; 478a25f0a04SGreg Roach } 479a25f0a04SGreg Roach $tree->setPreference('THEME_DIR', 'webtrees'); 480a25f0a04SGreg Roach $tree->setPreference('THUMBNAIL_WIDTH', '100'); 481a25f0a04SGreg Roach $tree->setPreference('USE_RIN', '0'); 482a25f0a04SGreg Roach $tree->setPreference('USE_SILHOUETTE', '1'); 483a25f0a04SGreg Roach $tree->setPreference('WATERMARK_THUMB', '0'); 484a25f0a04SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', Auth::id()); 485a25f0a04SGreg Roach $tree->setPreference('WEBTREES_EMAIL', ''); 486a25f0a04SGreg Roach $tree->setPreference('WORD_WRAPPED_NOTES', '0'); 487a25f0a04SGreg Roach 488a25f0a04SGreg Roach // Default restriction settings 489a25f0a04SGreg Roach $statement = Database::prepare( 490a25f0a04SGreg Roach "INSERT INTO `##default_resn` (gedcom_id, xref, tag_type, resn) VALUES (?, NULL, ?, ?)" 491a25f0a04SGreg Roach ); 492a25f0a04SGreg Roach $statement->execute(array($tree_id, 'SSN', 'confidential')); 493a25f0a04SGreg Roach $statement->execute(array($tree_id, 'SOUR', 'privacy')); 494a25f0a04SGreg Roach $statement->execute(array($tree_id, 'REPO', 'privacy')); 495a25f0a04SGreg Roach $statement->execute(array($tree_id, 'SUBM', 'confidential')); 496a25f0a04SGreg Roach $statement->execute(array($tree_id, 'SUBN', 'confidential')); 497a25f0a04SGreg Roach 498a25f0a04SGreg Roach // Genealogy data 499a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 500a25f0a04SGreg Roach $john_doe = /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 501a25f0a04SGreg Roach I18N::translate('John /DOE/'); 502a25f0a04SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own'); 503a25f0a04SGreg Roach Database::prepare("INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)")->execute(array( 504a25f0a04SGreg Roach $tree_id, 505a25f0a04SGreg 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" 506a25f0a04SGreg Roach )); 507a25f0a04SGreg Roach 508a25f0a04SGreg Roach // Set the initial blocks 509a25f0a04SGreg Roach Database::prepare( 510a25f0a04SGreg Roach "INSERT INTO `##block` (gedcom_id, location, block_order, module_name)" . 511a25f0a04SGreg Roach " SELECT ?, location, block_order, module_name" . 512a25f0a04SGreg Roach " FROM `##block`" . 513a25f0a04SGreg Roach " WHERE gedcom_id = -1" 514a25f0a04SGreg Roach )->execute(array($tree_id)); 515a25f0a04SGreg Roach 516a25f0a04SGreg Roach // Update our cache 517a25f0a04SGreg Roach self::$trees[$tree->id] = $tree; 518a25f0a04SGreg Roach 519a25f0a04SGreg Roach return $tree; 520a25f0a04SGreg Roach } 521a25f0a04SGreg Roach 522a25f0a04SGreg Roach /** 523a25f0a04SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 524a25f0a04SGreg Roach * new data. Optionally retain the media data, for when the user has been 525a25f0a04SGreg Roach * editing their data offline using an application which deletes (or does not 526a25f0a04SGreg Roach * support) media data. 527a25f0a04SGreg Roach * 528a25f0a04SGreg Roach * @param bool $keep_media 529a25f0a04SGreg Roach */ 530a25f0a04SGreg Roach public function deleteGenealogyData($keep_media) { 531a25f0a04SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute(array($this->id)); 532a25f0a04SGreg Roach Database::prepare("DELETE FROM `##individuals` WHERE i_file = ?")->execute(array($this->id)); 533a25f0a04SGreg Roach Database::prepare("DELETE FROM `##families` WHERE f_file = ?")->execute(array($this->id)); 534a25f0a04SGreg Roach Database::prepare("DELETE FROM `##sources` WHERE s_file = ?")->execute(array($this->id)); 535a25f0a04SGreg Roach Database::prepare("DELETE FROM `##other` WHERE o_file = ?")->execute(array($this->id)); 536a25f0a04SGreg Roach Database::prepare("DELETE FROM `##places` WHERE p_file = ?")->execute(array($this->id)); 537a25f0a04SGreg Roach Database::prepare("DELETE FROM `##placelinks` WHERE pl_file = ?")->execute(array($this->id)); 538a25f0a04SGreg Roach Database::prepare("DELETE FROM `##name` WHERE n_file = ?")->execute(array($this->id)); 539a25f0a04SGreg Roach Database::prepare("DELETE FROM `##dates` WHERE d_file = ?")->execute(array($this->id)); 540a25f0a04SGreg Roach Database::prepare("DELETE FROM `##change` WHERE gedcom_id = ?")->execute(array($this->id)); 541a25f0a04SGreg Roach 542a25f0a04SGreg Roach if ($keep_media) { 543a25f0a04SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute(array($this->id)); 544a25f0a04SGreg Roach } else { 545a25f0a04SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =?")->execute(array($this->id)); 546a25f0a04SGreg Roach Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute(array($this->id)); 547a25f0a04SGreg Roach } 548a25f0a04SGreg Roach } 549a25f0a04SGreg Roach 550a25f0a04SGreg Roach /** 551a25f0a04SGreg Roach * Delete everything relating to a tree 552a25f0a04SGreg Roach */ 553a25f0a04SGreg Roach public function delete() { 554a25f0a04SGreg Roach // If this is the default tree, then unset it 555a25f0a04SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === self::getNameFromId($this->id)) { 556a25f0a04SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 557a25f0a04SGreg Roach } 558a25f0a04SGreg Roach 559a25f0a04SGreg Roach $this->deleteGenealogyData(false); 560a25f0a04SGreg Roach 561a25f0a04SGreg Roach Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute(array($this->id)); 562a25f0a04SGreg Roach Database::prepare("DELETE FROM `##block` WHERE gedcom_id = ?")->execute(array($this->id)); 563a25f0a04SGreg Roach Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute(array($this->id)); 564a25f0a04SGreg Roach Database::prepare("DELETE FROM `##gedcom_setting` WHERE gedcom_id = ?")->execute(array($this->id)); 565a25f0a04SGreg Roach Database::prepare("DELETE FROM `##module_privacy` WHERE gedcom_id = ?")->execute(array($this->id)); 566a25f0a04SGreg Roach Database::prepare("DELETE FROM `##next_id` WHERE gedcom_id = ?")->execute(array($this->id)); 567a25f0a04SGreg Roach Database::prepare("DELETE FROM `##hit_counter` WHERE gedcom_id = ?")->execute(array($this->id)); 568a25f0a04SGreg Roach Database::prepare("DELETE FROM `##default_resn` WHERE gedcom_id = ?")->execute(array($this->id)); 569a25f0a04SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute(array($this->id)); 570a25f0a04SGreg Roach Database::prepare("DELETE FROM `##log` WHERE gedcom_id = ?")->execute(array($this->id)); 571a25f0a04SGreg Roach Database::prepare("DELETE FROM `##gedcom` WHERE gedcom_id = ?")->execute(array($this->id)); 572a25f0a04SGreg Roach 573a25f0a04SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 574a25f0a04SGreg Roach self::$trees = null; 575a25f0a04SGreg Roach } 576a25f0a04SGreg Roach 577a25f0a04SGreg Roach /** 578a25f0a04SGreg Roach * Export the tree to a GEDCOM file 579a25f0a04SGreg Roach * 580a25f0a04SGreg Roach * @param $gedcom_file 581a25f0a04SGreg Roach * 582a25f0a04SGreg Roach * @return bool 583a25f0a04SGreg Roach */ 584a25f0a04SGreg Roach public function exportGedcom($gedcom_file) { 585a25f0a04SGreg Roach // To avoid partial trees on timeout/diskspace/etc, write to a temporary file first 586a25f0a04SGreg Roach $tmp_file = $gedcom_file . '.tmp'; 587a25f0a04SGreg Roach 588a25f0a04SGreg Roach $file_pointer = @fopen($tmp_file, 'w'); 589a25f0a04SGreg Roach if ($file_pointer === false) { 590a25f0a04SGreg Roach return false; 591a25f0a04SGreg Roach } 592a25f0a04SGreg Roach 593a25f0a04SGreg Roach $buffer = reformat_record_export(gedcom_header($this->name)); 594a25f0a04SGreg Roach 595a25f0a04SGreg Roach $stmt = Database::prepare( 596a25f0a04SGreg Roach "SELECT i_gedcom AS gedcom FROM `##individuals` WHERE i_file = ?" . 597a25f0a04SGreg Roach " UNION ALL " . 598a25f0a04SGreg Roach "SELECT f_gedcom AS gedcom FROM `##families` WHERE f_file = ?" . 599a25f0a04SGreg Roach " UNION ALL " . 600a25f0a04SGreg Roach "SELECT s_gedcom AS gedcom FROM `##sources` WHERE s_file = ?" . 601a25f0a04SGreg Roach " UNION ALL " . 602a25f0a04SGreg Roach "SELECT o_gedcom AS gedcom FROM `##other` WHERE o_file = ? AND o_type NOT IN ('HEAD', 'TRLR')" . 603a25f0a04SGreg Roach " UNION ALL " . 604a25f0a04SGreg Roach "SELECT m_gedcom AS gedcom FROM `##media` WHERE m_file = ?" 605a25f0a04SGreg Roach )->execute(array($this->id, $this->id, $this->id, $this->id, $this->id)); 606a25f0a04SGreg Roach 607a25f0a04SGreg Roach while ($row = $stmt->fetch()) { 608a25f0a04SGreg Roach $buffer .= reformat_record_export($row->gedcom); 609a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 610a25f0a04SGreg Roach fwrite($file_pointer, $buffer); 611a25f0a04SGreg Roach $buffer = ''; 612a25f0a04SGreg Roach } 613a25f0a04SGreg Roach } 614a25f0a04SGreg Roach 615a25f0a04SGreg Roach fwrite($file_pointer, $buffer . '0 TRLR' . WT_EOL); 616a25f0a04SGreg Roach fclose($file_pointer); 617a25f0a04SGreg Roach 618a25f0a04SGreg Roach return @rename($tmp_file, $gedcom_file); 619a25f0a04SGreg Roach } 620a25f0a04SGreg Roach 621a25f0a04SGreg Roach /** 622a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 623a25f0a04SGreg Roach * 624a25f0a04SGreg Roach * @param string $path The full path to the (possibly temporary) file. 625a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 626a25f0a04SGreg Roach * @param boolean $keep_media Whether to retain any existing media records 627a25f0a04SGreg Roach * 628a25f0a04SGreg Roach * @throws \Exception 629a25f0a04SGreg Roach */ 630a25f0a04SGreg Roach public function importGedcomFile($path, $filename, $keep_media) { 631a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 632a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 633a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 634a25f0a04SGreg Roach // each block. 635a25f0a04SGreg Roach 636a25f0a04SGreg Roach $file_data = ''; 637a25f0a04SGreg Roach $fp = fopen($path, 'rb'); 638a25f0a04SGreg Roach 639a25f0a04SGreg Roach // Don’t allow the user to cancel the request. We do not want to be left with an incomplete transaction. 640a25f0a04SGreg Roach ignore_user_abort(true); 641a25f0a04SGreg Roach 642a25f0a04SGreg Roach Database::beginTransaction(); 643a25f0a04SGreg Roach $this->deleteGenealogyData($keep_media); 644a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 645a25f0a04SGreg Roach $this->setPreference('imported', '0'); 646a25f0a04SGreg Roach 647a25f0a04SGreg Roach while (!feof($fp)) { 648a25f0a04SGreg Roach $file_data .= fread($fp, 65536); 649a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 650a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 651a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 652a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 653a25f0a04SGreg Roach break; 654a25f0a04SGreg Roach } 655a25f0a04SGreg Roach } 656a25f0a04SGreg Roach if ($pos) { 657a25f0a04SGreg Roach Database::prepare( 658a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 659a25f0a04SGreg Roach )->execute(array($this->id, substr($file_data, 0, $pos))); 660a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 661a25f0a04SGreg Roach } 662a25f0a04SGreg Roach } 663a25f0a04SGreg Roach Database::prepare( 664a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 665a25f0a04SGreg Roach )->execute(array($this->id, $file_data)); 666a25f0a04SGreg Roach 667a25f0a04SGreg Roach Database::commit(); 668a25f0a04SGreg Roach fclose($fp); 669a25f0a04SGreg Roach } 670a25f0a04SGreg Roach} 671