1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 4*8fcd0d32SGreg Roach * Copyright (C) 2019 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 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 19a25f0a04SGreg Roach 20b7e60af1SGreg Roachuse Exception; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 2301461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2401461f86SGreg Roachuse Illuminate\Database\Query\Builder; 2501461f86SGreg Roachuse Illuminate\Database\Query\JoinClause; 26afb591d7SGreg Roachuse InvalidArgumentException; 27a25f0a04SGreg Roachuse PDOException; 28afb591d7SGreg Roachuse function substr_compare; 29a25f0a04SGreg Roach 30a25f0a04SGreg Roach/** 3176692c8bSGreg Roach * Provide an interface to the wt_gedcom table. 32a25f0a04SGreg Roach */ 33c1010edaSGreg Roachclass Tree 34c1010edaSGreg Roach{ 35cbc1590aSGreg Roach /** @var int The tree's ID number */ 3672cf66d4SGreg Roach private $id; 37518bbdc1SGreg Roach 38a25f0a04SGreg Roach /** @var string The tree's name */ 39a25f0a04SGreg Roach private $name; 40518bbdc1SGreg Roach 41a25f0a04SGreg Roach /** @var string The tree's title */ 42a25f0a04SGreg Roach private $title; 43a25f0a04SGreg Roach 44e2052359SGreg Roach /** @var int[] Default access rules for facts in this tree */ 45518bbdc1SGreg Roach private $fact_privacy; 46518bbdc1SGreg Roach 47e2052359SGreg Roach /** @var int[] Default access rules for individuals in this tree */ 48518bbdc1SGreg Roach private $individual_privacy; 49518bbdc1SGreg Roach 50518bbdc1SGreg Roach /** @var integer[][] Default access rules for individual facts in this tree */ 51518bbdc1SGreg Roach private $individual_fact_privacy; 52518bbdc1SGreg Roach 53a25f0a04SGreg Roach /** @var Tree[] All trees that we have permission to see. */ 5432f20c14SGreg Roach public static $trees = []; 55a25f0a04SGreg Roach 56a25f0a04SGreg Roach /** @var string[] Cached copy of the wt_gedcom_setting table. */ 5715d603e7SGreg Roach private $preferences = []; 58a25f0a04SGreg Roach 59a25f0a04SGreg Roach /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ 6013abd6f3SGreg Roach private $user_preferences = []; 61a25f0a04SGreg Roach 62061b43d7SGreg Roach private const RESN_PRIVACY = [ 63061b43d7SGreg Roach 'none' => Auth::PRIV_PRIVATE, 64061b43d7SGreg Roach 'privacy' => Auth::PRIV_USER, 65061b43d7SGreg Roach 'confidential' => Auth::PRIV_NONE, 66061b43d7SGreg Roach 'hidden' => Auth::PRIV_HIDE, 67061b43d7SGreg Roach ]; 68061b43d7SGreg Roach 69a25f0a04SGreg Roach /** 70a25f0a04SGreg Roach * Create a tree object. This is a private constructor - it can only 71a25f0a04SGreg Roach * be called from Tree::getAll() to ensure proper initialisation. 72a25f0a04SGreg Roach * 7372cf66d4SGreg Roach * @param int $id 74aa6f03bbSGreg Roach * @param string $name 75cc13d6d8SGreg Roach * @param string $title 76a25f0a04SGreg Roach */ 77cc13d6d8SGreg Roach private function __construct($id, $name, $title) 78c1010edaSGreg Roach { 7972cf66d4SGreg Roach $this->id = $id; 80aa6f03bbSGreg Roach $this->name = $name; 81cc13d6d8SGreg Roach $this->title = $title; 8213abd6f3SGreg Roach $this->fact_privacy = []; 8313abd6f3SGreg Roach $this->individual_privacy = []; 8413abd6f3SGreg Roach $this->individual_fact_privacy = []; 85518bbdc1SGreg Roach 86518bbdc1SGreg Roach // Load the privacy settings for this tree 87061b43d7SGreg Roach $rows = DB::table('default_resn') 88061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 89061b43d7SGreg Roach ->get(); 90518bbdc1SGreg Roach 91518bbdc1SGreg Roach foreach ($rows as $row) { 92061b43d7SGreg Roach // Convert GEDCOM privacy restriction to a webtrees access level. 93061b43d7SGreg Roach $row->resn = self::RESN_PRIVACY[$row->resn]; 94061b43d7SGreg Roach 95518bbdc1SGreg Roach if ($row->xref !== null) { 96518bbdc1SGreg Roach if ($row->tag_type !== null) { 97518bbdc1SGreg Roach $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn; 98518bbdc1SGreg Roach } else { 99518bbdc1SGreg Roach $this->individual_privacy[$row->xref] = (int) $row->resn; 100518bbdc1SGreg Roach } 101518bbdc1SGreg Roach } else { 102518bbdc1SGreg Roach $this->fact_privacy[$row->tag_type] = (int) $row->resn; 103518bbdc1SGreg Roach } 104518bbdc1SGreg Roach } 105a25f0a04SGreg Roach } 106a25f0a04SGreg Roach 107a25f0a04SGreg Roach /** 108a25f0a04SGreg Roach * The ID of this tree 109a25f0a04SGreg Roach * 110cbc1590aSGreg Roach * @return int 111a25f0a04SGreg Roach */ 11272cf66d4SGreg Roach public function id(): int 113c1010edaSGreg Roach { 11472cf66d4SGreg Roach return $this->id; 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach 117a25f0a04SGreg Roach /** 118a25f0a04SGreg Roach * The name of this tree 119a25f0a04SGreg Roach * 120a25f0a04SGreg Roach * @return string 121a25f0a04SGreg Roach */ 122aa6f03bbSGreg Roach public function name(): string 123c1010edaSGreg Roach { 124a25f0a04SGreg Roach return $this->name; 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach 127a25f0a04SGreg Roach /** 128a25f0a04SGreg Roach * The title of this tree 129a25f0a04SGreg Roach * 130a25f0a04SGreg Roach * @return string 131a25f0a04SGreg Roach */ 132cc13d6d8SGreg Roach public function title(): string 133c1010edaSGreg Roach { 134a25f0a04SGreg Roach return $this->title; 135a25f0a04SGreg Roach } 136a25f0a04SGreg Roach 137a25f0a04SGreg Roach /** 138518bbdc1SGreg Roach * The fact-level privacy for this tree. 139518bbdc1SGreg Roach * 140e2052359SGreg Roach * @return int[] 141518bbdc1SGreg Roach */ 1428f53f488SRico Sonntag public function getFactPrivacy(): array 143c1010edaSGreg Roach { 144518bbdc1SGreg Roach return $this->fact_privacy; 145518bbdc1SGreg Roach } 146518bbdc1SGreg Roach 147518bbdc1SGreg Roach /** 148518bbdc1SGreg Roach * The individual-level privacy for this tree. 149518bbdc1SGreg Roach * 150e2052359SGreg Roach * @return int[] 151518bbdc1SGreg Roach */ 1528f53f488SRico Sonntag public function getIndividualPrivacy(): array 153c1010edaSGreg Roach { 154518bbdc1SGreg Roach return $this->individual_privacy; 155518bbdc1SGreg Roach } 156518bbdc1SGreg Roach 157518bbdc1SGreg Roach /** 158518bbdc1SGreg Roach * The individual-fact-level privacy for this tree. 159518bbdc1SGreg Roach * 160adac8af1SGreg Roach * @return int[][] 161518bbdc1SGreg Roach */ 1628f53f488SRico Sonntag public function getIndividualFactPrivacy(): array 163c1010edaSGreg Roach { 164518bbdc1SGreg Roach return $this->individual_fact_privacy; 165518bbdc1SGreg Roach } 166518bbdc1SGreg Roach 167518bbdc1SGreg Roach /** 168a25f0a04SGreg Roach * Get the tree’s configuration settings. 169a25f0a04SGreg Roach * 170a25f0a04SGreg Roach * @param string $setting_name 17115d603e7SGreg Roach * @param string $default 172a25f0a04SGreg Roach * 17315d603e7SGreg Roach * @return string 174a25f0a04SGreg Roach */ 175771ae10aSGreg Roach public function getPreference(string $setting_name, string $default = ''): string 176c1010edaSGreg Roach { 17715d603e7SGreg Roach if (empty($this->preferences)) { 178061b43d7SGreg Roach $this->preferences = DB::table('gedcom_setting') 179061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 180061b43d7SGreg Roach ->pluck('setting_value', 'setting_name') 181061b43d7SGreg Roach ->all(); 182a25f0a04SGreg Roach } 183a25f0a04SGreg Roach 184b2ce94c6SRico Sonntag return $this->preferences[$setting_name] ?? $default; 185a25f0a04SGreg Roach } 186a25f0a04SGreg Roach 187a25f0a04SGreg Roach /** 188a25f0a04SGreg Roach * Set the tree’s configuration settings. 189a25f0a04SGreg Roach * 190a25f0a04SGreg Roach * @param string $setting_name 191a25f0a04SGreg Roach * @param string $setting_value 192a25f0a04SGreg Roach * 193a25f0a04SGreg Roach * @return $this 194a25f0a04SGreg Roach */ 195771ae10aSGreg Roach public function setPreference(string $setting_name, string $setting_value): Tree 196c1010edaSGreg Roach { 197a25f0a04SGreg Roach if ($setting_value !== $this->getPreference($setting_name)) { 198061b43d7SGreg Roach DB::table('gedcom_setting')->updateOrInsert([ 199061b43d7SGreg Roach 'gedcom_id' =>$this->id, 200a25f0a04SGreg Roach 'setting_name' => $setting_name, 201061b43d7SGreg Roach ], [ 202a25f0a04SGreg Roach 'setting_value' => $setting_value, 20313abd6f3SGreg Roach ]); 20415d603e7SGreg Roach 205a25f0a04SGreg Roach $this->preferences[$setting_name] = $setting_value; 20615d603e7SGreg Roach 20772292b7dSGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach 210a25f0a04SGreg Roach return $this; 211a25f0a04SGreg Roach } 212a25f0a04SGreg Roach 213a25f0a04SGreg Roach /** 214a25f0a04SGreg Roach * Get the tree’s user-configuration settings. 215a25f0a04SGreg Roach * 216a25f0a04SGreg Roach * @param User $user 217a25f0a04SGreg Roach * @param string $setting_name 2187015ba1fSGreg Roach * @param string $default 219a25f0a04SGreg Roach * 220a25f0a04SGreg Roach * @return string 221a25f0a04SGreg Roach */ 222771ae10aSGreg Roach public function getUserPreference(User $user, string $setting_name, string $default = ''): string 223c1010edaSGreg Roach { 224a25f0a04SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 225a25f0a04SGreg Roach // so it is quicker to fetch them all in one go. 226a25f0a04SGreg Roach if (!array_key_exists($user->getUserId(), $this->user_preferences)) { 227061b43d7SGreg Roach $this->user_preferences[$user->getUserId()] = DB::table('user_gedcom_setting') 228061b43d7SGreg Roach ->where('user_id', '=', $user->getUserId()) 229061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 230061b43d7SGreg Roach ->pluck('setting_value', 'setting_name') 231061b43d7SGreg Roach ->all(); 232a25f0a04SGreg Roach } 233a25f0a04SGreg Roach 234b2ce94c6SRico Sonntag return $this->user_preferences[$user->getUserId()][$setting_name] ?? $default; 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 */ 246771ae10aSGreg Roach public function setUserPreference(User $user, string $setting_name, string $setting_value): Tree 247c1010edaSGreg Roach { 248a25f0a04SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 249a25f0a04SGreg Roach // Update the database 250061b43d7SGreg Roach DB::table('user_gedcom_setting')->updateOrInsert([ 251061b43d7SGreg Roach 'gedcom_id' => $this->id(), 252a25f0a04SGreg Roach 'user_id' =>$user->getUserId(), 253a25f0a04SGreg Roach 'setting_name' => $setting_name, 254061b43d7SGreg Roach ], [ 255cbc1590aSGreg Roach 'setting_value' => $setting_value, 25613abd6f3SGreg Roach ]); 257061b43d7SGreg Roach 258061b43d7SGreg Roach // Update the cache 259a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()][$setting_name] = $setting_value; 260a25f0a04SGreg Roach // Audit log of changes 26172292b7dSGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this); 262a25f0a04SGreg Roach } 263a25f0a04SGreg Roach 264a25f0a04SGreg Roach return $this; 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach 267a25f0a04SGreg Roach /** 268a25f0a04SGreg Roach * Can a user accept changes for this tree? 269a25f0a04SGreg Roach * 270a25f0a04SGreg Roach * @param User $user 271a25f0a04SGreg Roach * 272cbc1590aSGreg Roach * @return bool 273a25f0a04SGreg Roach */ 274771ae10aSGreg Roach public function canAcceptChanges(User $user): bool 275c1010edaSGreg Roach { 276a25f0a04SGreg Roach return Auth::isModerator($this, $user); 277a25f0a04SGreg Roach } 278a25f0a04SGreg Roach 279a25f0a04SGreg Roach /** 280a25f0a04SGreg Roach * Fetch all the trees that we have permission to access. 281a25f0a04SGreg Roach * 282a25f0a04SGreg Roach * @return Tree[] 283a25f0a04SGreg Roach */ 284771ae10aSGreg Roach public static function getAll(): array 285c1010edaSGreg Roach { 28675a9f908SGreg Roach if (empty(self::$trees)) { 28701461f86SGreg Roach // Admins see all trees 28801461f86SGreg Roach $query = DB::table('gedcom') 28901461f86SGreg Roach ->leftJoin('gedcom_setting', function (JoinClause $join): void { 29001461f86SGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 29101461f86SGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 29201461f86SGreg Roach }) 29301461f86SGreg Roach ->where('gedcom.gedcom_id', '>', 0) 29401461f86SGreg Roach ->select([ 29501461f86SGreg Roach 'gedcom.gedcom_id AS tree_id', 29601461f86SGreg Roach 'gedcom.gedcom_name AS tree_name', 29701461f86SGreg Roach 'gedcom_setting.setting_value AS tree_title', 29801461f86SGreg Roach ]) 29901461f86SGreg Roach ->orderBy('gedcom.sort_order') 30001461f86SGreg Roach ->orderBy('gedcom_setting.setting_value'); 30101461f86SGreg Roach 30232f20c14SGreg Roach // Non-admins may not see all trees 30332f20c14SGreg Roach if (!Auth::isAdmin()) { 30401461f86SGreg Roach $query 30536357577SGreg Roach ->join('gedcom_setting AS gs2', function (JoinClause $join): void { 30636357577SGreg Roach $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 30701461f86SGreg Roach ->where('gs2.setting_name', '=', 'imported'); 30836357577SGreg Roach }) 30936357577SGreg Roach ->join('gedcom_setting AS gs3', function (JoinClause $join): void { 31001461f86SGreg Roach $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 31101461f86SGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 31201461f86SGreg Roach }) 31301461f86SGreg Roach ->leftJoin('user_gedcom_setting', function (JoinClause $join): void { 31401461f86SGreg Roach $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 31501461f86SGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 31601461f86SGreg Roach ->where('user_gedcom_setting.setting_name', '=', 'canedit'); 31701461f86SGreg Roach }) 31801461f86SGreg Roach ->where(function (Builder $query): void { 31901461f86SGreg Roach $query 32001461f86SGreg Roach // Managers 32101461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '=', 'admin') 32201461f86SGreg Roach // Members 32301461f86SGreg Roach ->orWhere(function (Builder $query): void { 32401461f86SGreg Roach $query 32501461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 32601461f86SGreg Roach ->where('gs3.setting_value', '=', '1') 32701461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '<>', 'none'); 32801461f86SGreg Roach }) 32936357577SGreg Roach // PUblic trees 33001461f86SGreg Roach ->orWhere(function (Builder $query): void { 33101461f86SGreg Roach $query 33201461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 33336357577SGreg Roach ->where('gs3.setting_value', '<>', '1'); 33401461f86SGreg Roach }); 33501461f86SGreg Roach }); 33601461f86SGreg Roach } 33701461f86SGreg Roach 33801461f86SGreg Roach $rows = $query->get(); 33901461f86SGreg Roach 340a25f0a04SGreg Roach foreach ($rows as $row) { 3413f7ece23SGreg Roach self::$trees[$row->tree_name] = new self((int) $row->tree_id, $row->tree_name, $row->tree_title); 342a25f0a04SGreg Roach } 343a25f0a04SGreg Roach } 344a25f0a04SGreg Roach 345a25f0a04SGreg Roach return self::$trees; 346a25f0a04SGreg Roach } 347a25f0a04SGreg Roach 348a25f0a04SGreg Roach /** 349d2cdeb3fSGreg Roach * Find the tree with a specific ID. 350a25f0a04SGreg Roach * 351cbc1590aSGreg Roach * @param int $tree_id 352cbc1590aSGreg Roach * 353cbc1590aSGreg Roach * @throws \DomainException 354a25f0a04SGreg Roach * @return Tree 355a25f0a04SGreg Roach */ 356771ae10aSGreg Roach public static function findById($tree_id): Tree 357c1010edaSGreg Roach { 35851d0f842SGreg Roach foreach (self::getAll() as $tree) { 35972cf66d4SGreg Roach if ($tree->id == $tree_id) { 36051d0f842SGreg Roach return $tree; 36151d0f842SGreg Roach } 36251d0f842SGreg Roach } 36359f2f229SGreg Roach throw new \DomainException(); 364a25f0a04SGreg Roach } 365a25f0a04SGreg Roach 366a25f0a04SGreg Roach /** 367d2cdeb3fSGreg Roach * Find the tree with a specific name. 368cf4bcc09SGreg Roach * 369cf4bcc09SGreg Roach * @param string $tree_name 370cf4bcc09SGreg Roach * 371cf4bcc09SGreg Roach * @return Tree|null 372cf4bcc09SGreg Roach */ 373c1010edaSGreg Roach public static function findByName($tree_name) 374c1010edaSGreg Roach { 375cf4bcc09SGreg Roach foreach (self::getAll() as $tree) { 37651d0f842SGreg Roach if ($tree->name === $tree_name) { 377cf4bcc09SGreg Roach return $tree; 378cf4bcc09SGreg Roach } 379cf4bcc09SGreg Roach } 380cf4bcc09SGreg Roach 381cf4bcc09SGreg Roach return null; 382cf4bcc09SGreg Roach } 383cf4bcc09SGreg Roach 384cf4bcc09SGreg Roach /** 385a25f0a04SGreg Roach * Create arguments to select_edit_control() 386a25f0a04SGreg Roach * Note - these will be escaped later 387a25f0a04SGreg Roach * 388a25f0a04SGreg Roach * @return string[] 389a25f0a04SGreg Roach */ 390771ae10aSGreg Roach public static function getIdList(): array 391c1010edaSGreg Roach { 39213abd6f3SGreg Roach $list = []; 393a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 39472cf66d4SGreg Roach $list[$tree->id] = $tree->title; 395a25f0a04SGreg Roach } 396a25f0a04SGreg Roach 397a25f0a04SGreg Roach return $list; 398a25f0a04SGreg Roach } 399a25f0a04SGreg Roach 400a25f0a04SGreg Roach /** 401a25f0a04SGreg Roach * Create arguments to select_edit_control() 402a25f0a04SGreg Roach * Note - these will be escaped later 403a25f0a04SGreg Roach * 404a25f0a04SGreg Roach * @return string[] 405a25f0a04SGreg Roach */ 406771ae10aSGreg Roach public static function getNameList(): array 407c1010edaSGreg Roach { 40813abd6f3SGreg Roach $list = []; 409a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 410a25f0a04SGreg Roach $list[$tree->name] = $tree->title; 411a25f0a04SGreg Roach } 412a25f0a04SGreg Roach 413a25f0a04SGreg Roach return $list; 414a25f0a04SGreg Roach } 415a25f0a04SGreg Roach 416a25f0a04SGreg Roach /** 417a25f0a04SGreg Roach * Create a new tree 418a25f0a04SGreg Roach * 419a25f0a04SGreg Roach * @param string $tree_name 420a25f0a04SGreg Roach * @param string $tree_title 421a25f0a04SGreg Roach * 422a25f0a04SGreg Roach * @return Tree 423a25f0a04SGreg Roach */ 424771ae10aSGreg Roach public static function create(string $tree_name, string $tree_title): Tree 425c1010edaSGreg Roach { 426a25f0a04SGreg Roach try { 427a25f0a04SGreg Roach // Create a new tree 42801461f86SGreg Roach DB::table('gedcom')->insert([ 42901461f86SGreg Roach 'gedcom_name' => $tree_name, 43001461f86SGreg Roach ]); 4314a86d714SGreg Roach 432061b43d7SGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 43332f20c14SGreg Roach 43432f20c14SGreg Roach $tree = new self($tree_id, $tree_name, $tree_title); 435a25f0a04SGreg Roach } catch (PDOException $ex) { 436a25f0a04SGreg Roach // A tree with that name already exists? 437ef2fd529SGreg Roach return self::findByName($tree_name); 438a25f0a04SGreg Roach } 439a25f0a04SGreg Roach 440a25f0a04SGreg Roach // Update the list of trees - to include this new one 44132f20c14SGreg Roach self::$trees[$tree_name] = $tree; 442a25f0a04SGreg Roach 443a25f0a04SGreg Roach $tree->setPreference('imported', '0'); 444a25f0a04SGreg Roach $tree->setPreference('title', $tree_title); 445a25f0a04SGreg Roach 446a25f0a04SGreg Roach // Module privacy 447a25f0a04SGreg Roach Module::setDefaultAccess($tree_id); 448a25f0a04SGreg Roach 4491507cbcaSGreg Roach // Set preferences from default tree 450061b43d7SGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 451061b43d7SGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 452061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 453061b43d7SGreg Roach $query 454061b43d7SGreg Roach ->select([DB::raw($tree_id), 'setting_name', 'setting_value']) 455061b43d7SGreg Roach ->from('gedcom_setting') 456061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 457061b43d7SGreg Roach } 458061b43d7SGreg Roach ); 4591507cbcaSGreg Roach 460061b43d7SGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 461061b43d7SGreg Roach ['gedcom_id', 'tag_type', 'resn'], 462061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 463061b43d7SGreg Roach $query 464061b43d7SGreg Roach ->select([DB::raw($tree_id), 'tag_type', 'resn']) 465061b43d7SGreg Roach ->from('default_resn') 466061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 467061b43d7SGreg Roach } 468061b43d7SGreg Roach ); 4691507cbcaSGreg Roach 470061b43d7SGreg Roach (new Builder(DB::connection()))->from('block')->insertUsing( 471061b43d7SGreg Roach ['gedcom_id', 'location', 'block_order', 'module_name'], 472061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 473061b43d7SGreg Roach $query 474061b43d7SGreg Roach ->select([DB::raw($tree_id), 'location', 'block_order', 'module_name']) 475061b43d7SGreg Roach ->from('block') 476061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 477061b43d7SGreg Roach } 478061b43d7SGreg Roach ); 4791507cbcaSGreg Roach 480a25f0a04SGreg Roach // Gedcom and privacy settings 48176f666f4SGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 48276f666f4SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 483a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 484a25f0a04SGreg Roach switch (WT_LOCALE) { 485a25f0a04SGreg Roach case 'es': 486a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 487a25f0a04SGreg Roach break; 488a25f0a04SGreg Roach case 'is': 489a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 490a25f0a04SGreg Roach break; 491a25f0a04SGreg Roach case 'lt': 492a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 493a25f0a04SGreg Roach break; 494a25f0a04SGreg Roach case 'pl': 495a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 496a25f0a04SGreg Roach break; 497a25f0a04SGreg Roach case 'pt': 498a25f0a04SGreg Roach case 'pt-BR': 499a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 500a25f0a04SGreg Roach break; 501a25f0a04SGreg Roach default: 502a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 503a25f0a04SGreg Roach break; 504a25f0a04SGreg Roach } 505a25f0a04SGreg Roach 506a25f0a04SGreg Roach // Genealogy data 507a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 508bbb76c12SGreg Roach /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 509bbb76c12SGreg Roach $john_doe = I18N::translate('John /DOE/'); 51077e70a22SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 511061b43d7SGreg Roach $gedcom = "0 HEAD\n1 CHAR UTF-8\n0 @X1@ INDI\n1 NAME {$john_doe}\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE {$note}\n0 TRLR\n"; 512061b43d7SGreg Roach 513061b43d7SGreg Roach DB::table('gedcom_chunk')->insert([ 514061b43d7SGreg Roach 'gedcom_id' => $tree_id, 515061b43d7SGreg Roach 'chunk_data' => $gedcom, 51613abd6f3SGreg Roach ]); 517a25f0a04SGreg Roach 518a25f0a04SGreg Roach // Update our cache 51972cf66d4SGreg Roach self::$trees[$tree->id] = $tree; 520a25f0a04SGreg Roach 521a25f0a04SGreg Roach return $tree; 522a25f0a04SGreg Roach } 523a25f0a04SGreg Roach 524a25f0a04SGreg Roach /** 525b78374c5SGreg Roach * Are there any pending edits for this tree, than need reviewing by a moderator. 526b78374c5SGreg Roach * 527b78374c5SGreg Roach * @return bool 528b78374c5SGreg Roach */ 529771ae10aSGreg Roach public function hasPendingEdit(): bool 530c1010edaSGreg Roach { 531b78374c5SGreg Roach return (bool) Database::prepare( 532b78374c5SGreg Roach "SELECT 1 FROM `##change` WHERE status = 'pending' AND gedcom_id = :tree_id" 53313abd6f3SGreg Roach )->execute([ 53472cf66d4SGreg Roach 'tree_id' => $this->id, 53513abd6f3SGreg Roach ])->fetchOne(); 536b78374c5SGreg Roach } 537b78374c5SGreg Roach 538b78374c5SGreg Roach /** 539a25f0a04SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 540a25f0a04SGreg Roach * new data. Optionally retain the media data, for when the user has been 541a25f0a04SGreg Roach * editing their data offline using an application which deletes (or does not 542a25f0a04SGreg Roach * support) media data. 543a25f0a04SGreg Roach * 544a25f0a04SGreg Roach * @param bool $keep_media 545b7e60af1SGreg Roach * 546b7e60af1SGreg Roach * @return void 547a25f0a04SGreg Roach */ 548b7e60af1SGreg Roach public function deleteGenealogyData(bool $keep_media) 549c1010edaSGreg Roach { 55072cf66d4SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->id]); 55172cf66d4SGreg Roach Database::prepare("DELETE FROM `##individuals` WHERE i_file = ?")->execute([$this->id]); 55272cf66d4SGreg Roach Database::prepare("DELETE FROM `##families` WHERE f_file = ?")->execute([$this->id]); 55372cf66d4SGreg Roach Database::prepare("DELETE FROM `##sources` WHERE s_file = ?")->execute([$this->id]); 55472cf66d4SGreg Roach Database::prepare("DELETE FROM `##other` WHERE o_file = ?")->execute([$this->id]); 55572cf66d4SGreg Roach Database::prepare("DELETE FROM `##places` WHERE p_file = ?")->execute([$this->id]); 55672cf66d4SGreg Roach Database::prepare("DELETE FROM `##placelinks` WHERE pl_file = ?")->execute([$this->id]); 55772cf66d4SGreg Roach Database::prepare("DELETE FROM `##name` WHERE n_file = ?")->execute([$this->id]); 55872cf66d4SGreg Roach Database::prepare("DELETE FROM `##dates` WHERE d_file = ?")->execute([$this->id]); 55972cf66d4SGreg Roach Database::prepare("DELETE FROM `##change` WHERE gedcom_id = ?")->execute([$this->id]); 560a25f0a04SGreg Roach 561a25f0a04SGreg Roach if ($keep_media) { 56272cf66d4SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute([$this->id]); 563a25f0a04SGreg Roach } else { 56472cf66d4SGreg Roach Database::prepare("DELETE FROM `##link` WHERE l_file =?")->execute([$this->id]); 56572cf66d4SGreg Roach Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute([$this->id]); 56672cf66d4SGreg Roach Database::prepare("DELETE FROM `##media_file` WHERE m_file =?")->execute([$this->id]); 567a25f0a04SGreg Roach } 568a25f0a04SGreg Roach } 569a25f0a04SGreg Roach 570a25f0a04SGreg Roach /** 571a25f0a04SGreg Roach * Delete everything relating to a tree 572b7e60af1SGreg Roach * 573b7e60af1SGreg Roach * @return void 574a25f0a04SGreg Roach */ 575c1010edaSGreg Roach public function delete() 576c1010edaSGreg Roach { 577a25f0a04SGreg Roach // If this is the default tree, then unset it 578ef2fd529SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 579a25f0a04SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 580a25f0a04SGreg Roach } 581a25f0a04SGreg Roach 582a25f0a04SGreg Roach $this->deleteGenealogyData(false); 583a25f0a04SGreg Roach 58472cf66d4SGreg Roach Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute([$this->id]); 58572cf66d4SGreg Roach Database::prepare("DELETE FROM `##block` WHERE gedcom_id = ?")->execute([$this->id]); 58672cf66d4SGreg Roach Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute([$this->id]); 58772cf66d4SGreg Roach Database::prepare("DELETE FROM `##gedcom_setting` WHERE gedcom_id = ?")->execute([$this->id]); 58872cf66d4SGreg Roach Database::prepare("DELETE FROM `##module_privacy` WHERE gedcom_id = ?")->execute([$this->id]); 58972cf66d4SGreg Roach Database::prepare("DELETE FROM `##hit_counter` WHERE gedcom_id = ?")->execute([$this->id]); 59072cf66d4SGreg Roach Database::prepare("DELETE FROM `##default_resn` WHERE gedcom_id = ?")->execute([$this->id]); 59172cf66d4SGreg Roach Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->id]); 59272cf66d4SGreg Roach Database::prepare("DELETE FROM `##log` WHERE gedcom_id = ?")->execute([$this->id]); 59372cf66d4SGreg Roach Database::prepare("DELETE FROM `##gedcom` WHERE gedcom_id = ?")->execute([$this->id]); 594a25f0a04SGreg Roach 595a25f0a04SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 59675a9f908SGreg Roach self::$trees = []; 597a25f0a04SGreg Roach } 598a25f0a04SGreg Roach 599a25f0a04SGreg Roach /** 600a25f0a04SGreg Roach * Export the tree to a GEDCOM file 601a25f0a04SGreg Roach * 6025792757eSGreg Roach * @param resource $stream 603b7e60af1SGreg Roach * 604b7e60af1SGreg Roach * @return void 605a25f0a04SGreg Roach */ 606c1010edaSGreg Roach public function exportGedcom($stream) 607c1010edaSGreg Roach { 6085792757eSGreg Roach $stmt = Database::prepare( 609e56ef01aSGreg Roach "SELECT i_gedcom AS gedcom, i_id AS xref, 1 AS n FROM `##individuals` WHERE i_file = :tree_id_1" . 6105792757eSGreg Roach " UNION ALL " . 611e56ef01aSGreg Roach "SELECT f_gedcom AS gedcom, f_id AS xref, 2 AS n FROM `##families` WHERE f_file = :tree_id_2" . 6125792757eSGreg Roach " UNION ALL " . 613e56ef01aSGreg Roach "SELECT s_gedcom AS gedcom, s_id AS xref, 3 AS n FROM `##sources` WHERE s_file = :tree_id_3" . 6145792757eSGreg Roach " UNION ALL " . 615e56ef01aSGreg 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')" . 6165792757eSGreg Roach " UNION ALL " . 617e56ef01aSGreg Roach "SELECT m_gedcom AS gedcom, m_id AS xref, 5 AS n FROM `##media` WHERE m_file = :tree_id_5" . 618e56ef01aSGreg Roach " ORDER BY n, LENGTH(xref), xref" 61913abd6f3SGreg Roach )->execute([ 62072cf66d4SGreg Roach 'tree_id_1' => $this->id, 62172cf66d4SGreg Roach 'tree_id_2' => $this->id, 62272cf66d4SGreg Roach 'tree_id_3' => $this->id, 62372cf66d4SGreg Roach 'tree_id_4' => $this->id, 62472cf66d4SGreg Roach 'tree_id_5' => $this->id, 62513abd6f3SGreg Roach ]); 626a25f0a04SGreg Roach 627a3d8780cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8')); 628a214e186SGreg Roach while (($row = $stmt->fetch()) !== false) { 6293d7a8a4cSGreg Roach $buffer .= FunctionsExport::reformatRecord($row->gedcom); 630a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 6315792757eSGreg Roach fwrite($stream, $buffer); 632a25f0a04SGreg Roach $buffer = ''; 633a25f0a04SGreg Roach } 634a25f0a04SGreg Roach } 6350f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 636195d09d8SGreg Roach $stmt->closeCursor(); 637a25f0a04SGreg Roach } 638a25f0a04SGreg Roach 639a25f0a04SGreg Roach /** 640a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 641a25f0a04SGreg Roach * 642a25f0a04SGreg Roach * @param string $path The full path to the (possibly temporary) file. 643a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 644a25f0a04SGreg Roach * 645b7e60af1SGreg Roach * @return void 646b7e60af1SGreg Roach * @throws Exception 647a25f0a04SGreg Roach */ 648771ae10aSGreg Roach public function importGedcomFile(string $path, string $filename) 649c1010edaSGreg Roach { 650a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 651a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 652a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 653a25f0a04SGreg Roach // each block. 654a25f0a04SGreg Roach 655a25f0a04SGreg Roach $file_data = ''; 656a25f0a04SGreg Roach $fp = fopen($path, 'rb'); 657a25f0a04SGreg Roach 6582e897bf2SGreg Roach if ($fp === false) { 6592e897bf2SGreg Roach throw new Exception('Cannot write file: ' . $path); 6602e897bf2SGreg Roach } 661a25f0a04SGreg Roach 662b7e60af1SGreg Roach $this->deleteGenealogyData((bool) $this->getPreference('keep_media')); 663a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 664a25f0a04SGreg Roach $this->setPreference('imported', '0'); 665a25f0a04SGreg Roach 666a25f0a04SGreg Roach while (!feof($fp)) { 667a25f0a04SGreg Roach $file_data .= fread($fp, 65536); 668a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 669a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 670a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 671a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 672a25f0a04SGreg Roach break; 673a25f0a04SGreg Roach } 674a25f0a04SGreg Roach } 675a25f0a04SGreg Roach if ($pos) { 676a25f0a04SGreg Roach Database::prepare( 677a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 678c1010edaSGreg Roach )->execute([ 67972cf66d4SGreg Roach $this->id, 680c1010edaSGreg Roach substr($file_data, 0, $pos), 681c1010edaSGreg Roach ]); 682a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 683a25f0a04SGreg Roach } 684a25f0a04SGreg Roach } 685a25f0a04SGreg Roach Database::prepare( 686a25f0a04SGreg Roach "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" 687c1010edaSGreg Roach )->execute([ 68872cf66d4SGreg Roach $this->id, 689c1010edaSGreg Roach $file_data, 690c1010edaSGreg Roach ]); 691a25f0a04SGreg Roach 692a25f0a04SGreg Roach fclose($fp); 693a25f0a04SGreg Roach } 694304f20d5SGreg Roach 695304f20d5SGreg Roach /** 696b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 697b90d8accSGreg Roach * 698b90d8accSGreg Roach * @return string 699b90d8accSGreg Roach */ 700771ae10aSGreg Roach public function getNewXref(): string 701c1010edaSGreg Roach { 702a214e186SGreg Roach $prefix = 'X'; 703b90d8accSGreg Roach 704971d66c8SGreg Roach $increment = 1.0; 705b90d8accSGreg Roach do { 706b90d8accSGreg Roach // Use LAST_INSERT_ID(expr) to provide a transaction-safe sequence. See 707b90d8accSGreg Roach // http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id 708b90d8accSGreg Roach $statement = Database::prepare( 709a214e186SGreg Roach "UPDATE `##site_setting` SET setting_value = LAST_INSERT_ID(setting_value + :increment) WHERE setting_name = 'next_xref'" 710b90d8accSGreg Roach ); 71113abd6f3SGreg Roach $statement->execute([ 712971d66c8SGreg Roach 'increment' => (int) $increment, 71313abd6f3SGreg Roach ]); 714b90d8accSGreg Roach 715b90d8accSGreg Roach if ($statement->rowCount() === 0) { 716769d7d6eSGreg Roach $num = '1'; 717bbd8bd1bSGreg Roach Site::setPreference('next_xref', $num); 718b90d8accSGreg Roach } else { 719bbd8bd1bSGreg Roach $num = (string) Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne(); 720b90d8accSGreg Roach } 721b90d8accSGreg Roach 722a214e186SGreg Roach $xref = $prefix . $num; 723a214e186SGreg Roach 724b90d8accSGreg Roach // Records may already exist with this sequence number. 725b90d8accSGreg Roach $already_used = Database::prepare( 726a214e186SGreg Roach "SELECT" . 727a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##individuals` WHERE i_id = :i_id) OR" . 728a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##families` WHERE f_id = :f_id) OR" . 729a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##sources` WHERE s_id = :s_id) OR" . 730a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##media` WHERE m_id = :m_id) OR" . 731a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##other` WHERE o_id = :o_id) OR" . 732a214e186SGreg Roach " EXISTS (SELECT 1 FROM `##change` WHERE xref = :xref)" 73313abd6f3SGreg Roach )->execute([ 734a214e186SGreg Roach 'i_id' => $xref, 735a214e186SGreg Roach 'f_id' => $xref, 736a214e186SGreg Roach 's_id' => $xref, 737a214e186SGreg Roach 'm_id' => $xref, 738a214e186SGreg Roach 'o_id' => $xref, 739a214e186SGreg Roach 'xref' => $xref, 74013abd6f3SGreg Roach ])->fetchOne(); 741971d66c8SGreg Roach 742971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 743971d66c8SGreg Roach // existing data in a reasonable time. 744971d66c8SGreg Roach $increment *= 1.01; 745a214e186SGreg Roach } while ($already_used !== '0'); 746b90d8accSGreg Roach 747a214e186SGreg Roach return $xref; 748b90d8accSGreg Roach } 749b90d8accSGreg Roach 750b90d8accSGreg Roach /** 751304f20d5SGreg Roach * Create a new record from GEDCOM data. 752304f20d5SGreg Roach * 753304f20d5SGreg Roach * @param string $gedcom 754304f20d5SGreg Roach * 75515d603e7SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 756afb591d7SGreg Roach * @throws InvalidArgumentException 757304f20d5SGreg Roach */ 758771ae10aSGreg Roach public function createRecord(string $gedcom): GedcomRecord 759c1010edaSGreg Roach { 760afb591d7SGreg Roach if (substr_compare($gedcom, '0 @@', 0, 4) !== 0) { 761afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@'); 762304f20d5SGreg Roach } 763304f20d5SGreg Roach 764a214e186SGreg Roach $xref = $this->getNewXref(); 765afb591d7SGreg Roach $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); 766304f20d5SGreg Roach 767afb591d7SGreg Roach // Create a change record 768304f20d5SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 769304f20d5SGreg Roach 770304f20d5SGreg Roach // Create a pending change 771304f20d5SGreg Roach Database::prepare( 772304f20d5SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)" 77313abd6f3SGreg Roach )->execute([ 77472cf66d4SGreg Roach $this->id, 775304f20d5SGreg Roach $xref, 776304f20d5SGreg Roach $gedcom, 777cbc1590aSGreg Roach Auth::id(), 77813abd6f3SGreg Roach ]); 779304f20d5SGreg Roach 780afb591d7SGreg Roach // Accept this pending change 781afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 782afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 783afb591d7SGreg Roach 784afb591d7SGreg Roach return new GedcomRecord($xref, $gedcom, null, $this); 785afb591d7SGreg Roach } 786afb591d7SGreg Roach 787313e72b3SGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 788afb591d7SGreg Roach } 789afb591d7SGreg Roach 790afb591d7SGreg Roach /** 791afb591d7SGreg Roach * Create a new family from GEDCOM data. 792afb591d7SGreg Roach * 793afb591d7SGreg Roach * @param string $gedcom 794afb591d7SGreg Roach * 795afb591d7SGreg Roach * @return Family 796afb591d7SGreg Roach * @throws InvalidArgumentException 797afb591d7SGreg Roach */ 798afb591d7SGreg Roach public function createFamily(string $gedcom): GedcomRecord 799afb591d7SGreg Roach { 800afb591d7SGreg Roach if (substr_compare($gedcom, '0 @@ FAM', 0, 8) !== 0) { 801afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM'); 802afb591d7SGreg Roach } 803afb591d7SGreg Roach 804afb591d7SGreg Roach $xref = $this->getNewXref(); 805afb591d7SGreg Roach $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); 806afb591d7SGreg Roach 807afb591d7SGreg Roach // Create a change record 808afb591d7SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 809afb591d7SGreg Roach 810afb591d7SGreg Roach // Create a pending change 811afb591d7SGreg Roach Database::prepare( 812afb591d7SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)" 813afb591d7SGreg Roach )->execute([ 81472cf66d4SGreg Roach $this->id, 815afb591d7SGreg Roach $xref, 816afb591d7SGreg Roach $gedcom, 817afb591d7SGreg Roach Auth::id(), 818afb591d7SGreg Roach ]); 819304f20d5SGreg Roach 820304f20d5SGreg Roach // Accept this pending change 821304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 822cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 823afb591d7SGreg Roach 824afb591d7SGreg Roach return new Family($xref, $gedcom, null, $this); 825304f20d5SGreg Roach } 826afb591d7SGreg Roach 827afb591d7SGreg Roach return new Family($xref, '', $gedcom, $this); 828afb591d7SGreg Roach } 829afb591d7SGreg Roach 830afb591d7SGreg Roach /** 831afb591d7SGreg Roach * Create a new individual from GEDCOM data. 832afb591d7SGreg Roach * 833afb591d7SGreg Roach * @param string $gedcom 834afb591d7SGreg Roach * 835afb591d7SGreg Roach * @return Individual 836afb591d7SGreg Roach * @throws InvalidArgumentException 837afb591d7SGreg Roach */ 838afb591d7SGreg Roach public function createIndividual(string $gedcom): GedcomRecord 839afb591d7SGreg Roach { 840afb591d7SGreg Roach if (substr_compare($gedcom, '0 @@ INDI', 0, 9) !== 0) { 841afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI'); 842afb591d7SGreg Roach } 843afb591d7SGreg Roach 844afb591d7SGreg Roach $xref = $this->getNewXref(); 845afb591d7SGreg Roach $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); 846afb591d7SGreg Roach 847afb591d7SGreg Roach // Create a change record 848afb591d7SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 849afb591d7SGreg Roach 850afb591d7SGreg Roach // Create a pending change 851afb591d7SGreg Roach Database::prepare( 852afb591d7SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)" 853afb591d7SGreg Roach )->execute([ 85472cf66d4SGreg Roach $this->id, 855afb591d7SGreg Roach $xref, 856afb591d7SGreg Roach $gedcom, 857afb591d7SGreg Roach Auth::id(), 858afb591d7SGreg Roach ]); 859afb591d7SGreg Roach 860afb591d7SGreg Roach // Accept this pending change 861afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 862afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 863afb591d7SGreg Roach 864afb591d7SGreg Roach return new Individual($xref, $gedcom, null, $this); 865afb591d7SGreg Roach } 866afb591d7SGreg Roach 867afb591d7SGreg Roach return new Individual($xref, '', $gedcom, $this); 868304f20d5SGreg Roach } 8698586983fSGreg Roach 8708586983fSGreg Roach /** 87120b58d20SGreg Roach * Create a new media object from GEDCOM data. 87220b58d20SGreg Roach * 87320b58d20SGreg Roach * @param string $gedcom 87420b58d20SGreg Roach * 87520b58d20SGreg Roach * @return Media 87620b58d20SGreg Roach * @throws InvalidArgumentException 87720b58d20SGreg Roach */ 87820b58d20SGreg Roach public function createMediaObject(string $gedcom): Media 87920b58d20SGreg Roach { 88020b58d20SGreg Roach if (substr_compare($gedcom, '0 @@ OBJE', 0, 9) !== 0) { 88120b58d20SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE'); 88220b58d20SGreg Roach } 88320b58d20SGreg Roach 88420b58d20SGreg Roach $xref = $this->getNewXref(); 88520b58d20SGreg Roach $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); 88620b58d20SGreg Roach 88720b58d20SGreg Roach // Create a change record 88820b58d20SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 88920b58d20SGreg Roach 89020b58d20SGreg Roach // Create a pending change 89120b58d20SGreg Roach Database::prepare( 89220b58d20SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)" 89320b58d20SGreg Roach )->execute([ 89420b58d20SGreg Roach $this->id, 89520b58d20SGreg Roach $xref, 89620b58d20SGreg Roach $gedcom, 89720b58d20SGreg Roach Auth::id(), 89820b58d20SGreg Roach ]); 89920b58d20SGreg Roach 90020b58d20SGreg Roach // Accept this pending change 90120b58d20SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 90220b58d20SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 90320b58d20SGreg Roach 90420b58d20SGreg Roach return new Media($xref, $gedcom, null, $this); 90520b58d20SGreg Roach } 90620b58d20SGreg Roach 90720b58d20SGreg Roach return new Media($xref, '', $gedcom, $this); 90820b58d20SGreg Roach } 90920b58d20SGreg Roach 91020b58d20SGreg Roach /** 9118586983fSGreg Roach * What is the most significant individual in this tree. 9128586983fSGreg Roach * 9138586983fSGreg Roach * @param User $user 9148586983fSGreg Roach * 9158586983fSGreg Roach * @return Individual 9168586983fSGreg Roach */ 917c1010edaSGreg Roach public function significantIndividual(User $user): Individual 918c1010edaSGreg Roach { 9198586983fSGreg Roach static $individual; // Only query the DB once. 9208586983fSGreg Roach 9217015ba1fSGreg Roach if (!$individual && $this->getUserPreference($user, 'rootid') !== '') { 9228586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 9238586983fSGreg Roach } 9247015ba1fSGreg Roach if (!$individual && $this->getUserPreference($user, 'gedcomid') !== '') { 9258586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 9268586983fSGreg Roach } 9278586983fSGreg Roach if (!$individual) { 9288586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 9298586983fSGreg Roach } 9308586983fSGreg Roach if (!$individual) { 931769d7d6eSGreg Roach $xref = (string) Database::prepare( 9325fe1add5SGreg Roach "SELECT MIN(i_id) FROM `##individuals` WHERE i_file = :tree_id" 9335fe1add5SGreg Roach )->execute([ 93472cf66d4SGreg Roach 'tree_id' => $this->id(), 935769d7d6eSGreg Roach ])->fetchOne(); 936769d7d6eSGreg Roach 937769d7d6eSGreg Roach $individual = Individual::getInstance($xref, $this); 9385fe1add5SGreg Roach } 9395fe1add5SGreg Roach if (!$individual) { 9405fe1add5SGreg Roach // always return a record 9415fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 9425fe1add5SGreg Roach } 9435fe1add5SGreg Roach 9445fe1add5SGreg Roach return $individual; 9455fe1add5SGreg Roach } 9465fe1add5SGreg Roach 9475fe1add5SGreg Roach /** 9485fe1add5SGreg Roach * Get significant information from this page, to allow other pages such as 9495fe1add5SGreg Roach * charts and reports to initialise with the same records 9505fe1add5SGreg Roach * 9515fe1add5SGreg Roach * @return Individual 9525fe1add5SGreg Roach */ 953771ae10aSGreg Roach public function getSignificantIndividual(): Individual 954c1010edaSGreg Roach { 9555fe1add5SGreg Roach static $individual; // Only query the DB once. 9565fe1add5SGreg Roach 9577015ba1fSGreg Roach if (!$individual && $this->getUserPreference(Auth::user(), 'rootid') !== '') { 9585fe1add5SGreg Roach $individual = Individual::getInstance($this->getUserPreference(Auth::user(), 'rootid'), $this); 9595fe1add5SGreg Roach } 9607015ba1fSGreg Roach if (!$individual && $this->getUserPreference(Auth::user(), 'gedcomid') !== '') { 9615fe1add5SGreg Roach $individual = Individual::getInstance($this->getUserPreference(Auth::user(), 'gedcomid'), $this); 9625fe1add5SGreg Roach } 9635fe1add5SGreg Roach if (!$individual) { 9645fe1add5SGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 9655fe1add5SGreg Roach } 9665fe1add5SGreg Roach if (!$individual) { 967769d7d6eSGreg Roach $xref = (string) Database::prepare( 968769d7d6eSGreg Roach "SELECT MIN(i_id) FROM `##individuals` WHERE i_file = :tree_id" 969769d7d6eSGreg Roach )->execute([ 97072cf66d4SGreg Roach 'tree_id' => $this->id(), 971769d7d6eSGreg Roach ])->fetchOne(); 972769d7d6eSGreg Roach 973769d7d6eSGreg Roach $individual = Individual::getInstance($xref, $this); 9748586983fSGreg Roach } 9758586983fSGreg Roach if (!$individual) { 9768586983fSGreg Roach // always return a record 9778586983fSGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 9788586983fSGreg Roach } 9798586983fSGreg Roach 9808586983fSGreg Roach return $individual; 9818586983fSGreg Roach } 982a25f0a04SGreg Roach} 983