1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg 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 203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 2201461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2301461f86SGreg Roachuse Illuminate\Database\Query\Builder; 2401461f86SGreg Roachuse Illuminate\Database\Query\JoinClause; 2594026f20SGreg Roachuse Illuminate\Support\Collection; 26bec87e94SGreg Roachuse Illuminate\Support\Str; 27afb591d7SGreg Roachuse InvalidArgumentException; 28a25f0a04SGreg Roachuse PDOException; 298b67c11aSGreg Roachuse stdClass; 30a25f0a04SGreg Roach 31a25f0a04SGreg Roach/** 3276692c8bSGreg Roach * Provide an interface to the wt_gedcom table. 33a25f0a04SGreg Roach */ 34c1010edaSGreg Roachclass Tree 35c1010edaSGreg Roach{ 36cbc1590aSGreg Roach /** @var int The tree's ID number */ 3772cf66d4SGreg Roach private $id; 38518bbdc1SGreg Roach 39a25f0a04SGreg Roach /** @var string The tree's name */ 40a25f0a04SGreg Roach private $name; 41518bbdc1SGreg Roach 42a25f0a04SGreg Roach /** @var string The tree's title */ 43a25f0a04SGreg Roach private $title; 44a25f0a04SGreg Roach 45e2052359SGreg Roach /** @var int[] Default access rules for facts in this tree */ 46518bbdc1SGreg Roach private $fact_privacy; 47518bbdc1SGreg Roach 48e2052359SGreg Roach /** @var int[] Default access rules for individuals in this tree */ 49518bbdc1SGreg Roach private $individual_privacy; 50518bbdc1SGreg Roach 51518bbdc1SGreg Roach /** @var integer[][] Default access rules for individual facts in this tree */ 52518bbdc1SGreg Roach private $individual_fact_privacy; 53518bbdc1SGreg Roach 54c0804649SGreg Roach /** @var Tree[] All trees that we have permission to see, indexed by ID. */ 5532f20c14SGreg Roach public static $trees = []; 56a25f0a04SGreg Roach 57a25f0a04SGreg Roach /** @var string[] Cached copy of the wt_gedcom_setting table. */ 5815d603e7SGreg Roach private $preferences = []; 59a25f0a04SGreg Roach 60a25f0a04SGreg Roach /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ 6113abd6f3SGreg Roach private $user_preferences = []; 62a25f0a04SGreg Roach 63061b43d7SGreg Roach private const RESN_PRIVACY = [ 64061b43d7SGreg Roach 'none' => Auth::PRIV_PRIVATE, 65061b43d7SGreg Roach 'privacy' => Auth::PRIV_USER, 66061b43d7SGreg Roach 'confidential' => Auth::PRIV_NONE, 67061b43d7SGreg Roach 'hidden' => Auth::PRIV_HIDE, 68061b43d7SGreg Roach ]; 69061b43d7SGreg Roach 70a25f0a04SGreg Roach /** 71a25f0a04SGreg Roach * Create a tree object. This is a private constructor - it can only 72a25f0a04SGreg Roach * be called from Tree::getAll() to ensure proper initialisation. 73a25f0a04SGreg Roach * 7472cf66d4SGreg Roach * @param int $id 75aa6f03bbSGreg Roach * @param string $name 76cc13d6d8SGreg Roach * @param string $title 77a25f0a04SGreg Roach */ 78cc13d6d8SGreg Roach private function __construct($id, $name, $title) 79c1010edaSGreg Roach { 8072cf66d4SGreg Roach $this->id = $id; 81aa6f03bbSGreg Roach $this->name = $name; 82cc13d6d8SGreg Roach $this->title = $title; 8313abd6f3SGreg Roach $this->fact_privacy = []; 8413abd6f3SGreg Roach $this->individual_privacy = []; 8513abd6f3SGreg Roach $this->individual_fact_privacy = []; 86518bbdc1SGreg Roach 87518bbdc1SGreg Roach // Load the privacy settings for this tree 88061b43d7SGreg Roach $rows = DB::table('default_resn') 89061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 90061b43d7SGreg Roach ->get(); 91518bbdc1SGreg Roach 92518bbdc1SGreg Roach foreach ($rows as $row) { 93061b43d7SGreg Roach // Convert GEDCOM privacy restriction to a webtrees access level. 94061b43d7SGreg Roach $row->resn = self::RESN_PRIVACY[$row->resn]; 95061b43d7SGreg Roach 96518bbdc1SGreg Roach if ($row->xref !== null) { 97518bbdc1SGreg Roach if ($row->tag_type !== null) { 98518bbdc1SGreg Roach $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn; 99518bbdc1SGreg Roach } else { 100518bbdc1SGreg Roach $this->individual_privacy[$row->xref] = (int) $row->resn; 101518bbdc1SGreg Roach } 102518bbdc1SGreg Roach } else { 103518bbdc1SGreg Roach $this->fact_privacy[$row->tag_type] = (int) $row->resn; 104518bbdc1SGreg Roach } 105518bbdc1SGreg Roach } 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach 108a25f0a04SGreg Roach /** 109a25f0a04SGreg Roach * The ID of this tree 110a25f0a04SGreg Roach * 111cbc1590aSGreg Roach * @return int 112a25f0a04SGreg Roach */ 11372cf66d4SGreg Roach public function id(): int 114c1010edaSGreg Roach { 11572cf66d4SGreg Roach return $this->id; 116a25f0a04SGreg Roach } 117a25f0a04SGreg Roach 118a25f0a04SGreg Roach /** 119a25f0a04SGreg Roach * The name of this tree 120a25f0a04SGreg Roach * 121a25f0a04SGreg Roach * @return string 122a25f0a04SGreg Roach */ 123aa6f03bbSGreg Roach public function name(): string 124c1010edaSGreg Roach { 125a25f0a04SGreg Roach return $this->name; 126a25f0a04SGreg Roach } 127a25f0a04SGreg Roach 128a25f0a04SGreg Roach /** 129a25f0a04SGreg Roach * The title of this tree 130a25f0a04SGreg Roach * 131a25f0a04SGreg Roach * @return string 132a25f0a04SGreg Roach */ 133cc13d6d8SGreg Roach public function title(): string 134c1010edaSGreg Roach { 135a25f0a04SGreg Roach return $this->title; 136a25f0a04SGreg Roach } 137a25f0a04SGreg Roach 138a25f0a04SGreg Roach /** 139518bbdc1SGreg Roach * The fact-level privacy for this tree. 140518bbdc1SGreg Roach * 141e2052359SGreg Roach * @return int[] 142518bbdc1SGreg Roach */ 1438f53f488SRico Sonntag public function getFactPrivacy(): array 144c1010edaSGreg Roach { 145518bbdc1SGreg Roach return $this->fact_privacy; 146518bbdc1SGreg Roach } 147518bbdc1SGreg Roach 148518bbdc1SGreg Roach /** 149518bbdc1SGreg Roach * The individual-level privacy for this tree. 150518bbdc1SGreg Roach * 151e2052359SGreg Roach * @return int[] 152518bbdc1SGreg Roach */ 1538f53f488SRico Sonntag public function getIndividualPrivacy(): array 154c1010edaSGreg Roach { 155518bbdc1SGreg Roach return $this->individual_privacy; 156518bbdc1SGreg Roach } 157518bbdc1SGreg Roach 158518bbdc1SGreg Roach /** 159518bbdc1SGreg Roach * The individual-fact-level privacy for this tree. 160518bbdc1SGreg Roach * 161adac8af1SGreg Roach * @return int[][] 162518bbdc1SGreg Roach */ 1638f53f488SRico Sonntag public function getIndividualFactPrivacy(): array 164c1010edaSGreg Roach { 165518bbdc1SGreg Roach return $this->individual_fact_privacy; 166518bbdc1SGreg Roach } 167518bbdc1SGreg Roach 168518bbdc1SGreg Roach /** 169a25f0a04SGreg Roach * Get the tree’s configuration settings. 170a25f0a04SGreg Roach * 171a25f0a04SGreg Roach * @param string $setting_name 17215d603e7SGreg Roach * @param string $default 173a25f0a04SGreg Roach * 17415d603e7SGreg Roach * @return string 175a25f0a04SGreg Roach */ 176771ae10aSGreg Roach public function getPreference(string $setting_name, string $default = ''): string 177c1010edaSGreg Roach { 17815d603e7SGreg Roach if (empty($this->preferences)) { 179061b43d7SGreg Roach $this->preferences = DB::table('gedcom_setting') 180061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 181061b43d7SGreg Roach ->pluck('setting_value', 'setting_name') 182061b43d7SGreg Roach ->all(); 183a25f0a04SGreg Roach } 184a25f0a04SGreg Roach 185b2ce94c6SRico Sonntag return $this->preferences[$setting_name] ?? $default; 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach 188a25f0a04SGreg Roach /** 189a25f0a04SGreg Roach * Set the tree’s configuration settings. 190a25f0a04SGreg Roach * 191a25f0a04SGreg Roach * @param string $setting_name 192a25f0a04SGreg Roach * @param string $setting_value 193a25f0a04SGreg Roach * 194a25f0a04SGreg Roach * @return $this 195a25f0a04SGreg Roach */ 196771ae10aSGreg Roach public function setPreference(string $setting_name, string $setting_value): Tree 197c1010edaSGreg Roach { 198a25f0a04SGreg Roach if ($setting_value !== $this->getPreference($setting_name)) { 199061b43d7SGreg Roach DB::table('gedcom_setting')->updateOrInsert([ 200061b43d7SGreg Roach 'gedcom_id' => $this->id, 201a25f0a04SGreg Roach 'setting_name' => $setting_name, 202061b43d7SGreg Roach ], [ 203a25f0a04SGreg Roach 'setting_value' => $setting_value, 20413abd6f3SGreg Roach ]); 20515d603e7SGreg Roach 206a25f0a04SGreg Roach $this->preferences[$setting_name] = $setting_value; 20715d603e7SGreg Roach 20872292b7dSGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach return $this; 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 214a25f0a04SGreg Roach /** 215a25f0a04SGreg Roach * Get the tree’s user-configuration settings. 216a25f0a04SGreg Roach * 217a25f0a04SGreg Roach * @param User $user 218a25f0a04SGreg Roach * @param string $setting_name 2197015ba1fSGreg Roach * @param string $default 220a25f0a04SGreg Roach * 221a25f0a04SGreg Roach * @return string 222a25f0a04SGreg Roach */ 223771ae10aSGreg Roach public function getUserPreference(User $user, string $setting_name, string $default = ''): string 224c1010edaSGreg Roach { 225a25f0a04SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 226a25f0a04SGreg Roach // so it is quicker to fetch them all in one go. 227*895230eeSGreg Roach if (!array_key_exists($user->id(), $this->user_preferences)) { 228*895230eeSGreg Roach $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting') 229*895230eeSGreg Roach ->where('user_id', '=', $user->id()) 230061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 231061b43d7SGreg Roach ->pluck('setting_value', 'setting_name') 232061b43d7SGreg Roach ->all(); 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach 235*895230eeSGreg Roach return $this->user_preferences[$user->id()][$setting_name] ?? $default; 236a25f0a04SGreg Roach } 237a25f0a04SGreg Roach 238a25f0a04SGreg Roach /** 239a25f0a04SGreg Roach * Set the tree’s user-configuration settings. 240a25f0a04SGreg Roach * 241a25f0a04SGreg Roach * @param User $user 242a25f0a04SGreg Roach * @param string $setting_name 243a25f0a04SGreg Roach * @param string $setting_value 244a25f0a04SGreg Roach * 245a25f0a04SGreg Roach * @return $this 246a25f0a04SGreg Roach */ 247771ae10aSGreg Roach public function setUserPreference(User $user, string $setting_name, string $setting_value): Tree 248c1010edaSGreg Roach { 249a25f0a04SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 250a25f0a04SGreg Roach // Update the database 251061b43d7SGreg Roach DB::table('user_gedcom_setting')->updateOrInsert([ 252061b43d7SGreg Roach 'gedcom_id' => $this->id(), 253*895230eeSGreg Roach 'user_id' => $user->id(), 254a25f0a04SGreg Roach 'setting_name' => $setting_name, 255061b43d7SGreg Roach ], [ 256cbc1590aSGreg Roach 'setting_value' => $setting_value, 25713abd6f3SGreg Roach ]); 258061b43d7SGreg Roach 259061b43d7SGreg Roach // Update the cache 260*895230eeSGreg Roach $this->user_preferences[$user->id()][$setting_name] = $setting_value; 261a25f0a04SGreg Roach // Audit log of changes 26272292b7dSGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this); 263a25f0a04SGreg Roach } 264a25f0a04SGreg Roach 265a25f0a04SGreg Roach return $this; 266a25f0a04SGreg Roach } 267a25f0a04SGreg Roach 268a25f0a04SGreg Roach /** 269a25f0a04SGreg Roach * Can a user accept changes for this tree? 270a25f0a04SGreg Roach * 271a25f0a04SGreg Roach * @param User $user 272a25f0a04SGreg Roach * 273cbc1590aSGreg Roach * @return bool 274a25f0a04SGreg Roach */ 275771ae10aSGreg Roach public function canAcceptChanges(User $user): bool 276c1010edaSGreg Roach { 277a25f0a04SGreg Roach return Auth::isModerator($this, $user); 278a25f0a04SGreg Roach } 279a25f0a04SGreg Roach 280a25f0a04SGreg Roach /** 2818b67c11aSGreg Roach * All the trees that we have permission to access. 282a25f0a04SGreg Roach * 2838b67c11aSGreg Roach * @return Collection|Tree[] 284a25f0a04SGreg Roach */ 2858b67c11aSGreg Roach public static function all(): Collection 286c1010edaSGreg Roach { 2878b67c11aSGreg Roach return app('cache.array')->rememberForever(__CLASS__, function () { 28801461f86SGreg Roach // Admins see all trees 28901461f86SGreg Roach $query = DB::table('gedcom') 29001461f86SGreg Roach ->leftJoin('gedcom_setting', function (JoinClause $join): void { 29101461f86SGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 29201461f86SGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 29301461f86SGreg Roach }) 29401461f86SGreg Roach ->where('gedcom.gedcom_id', '>', 0) 29501461f86SGreg Roach ->select([ 29601461f86SGreg Roach 'gedcom.gedcom_id AS tree_id', 29701461f86SGreg Roach 'gedcom.gedcom_name AS tree_name', 29801461f86SGreg Roach 'gedcom_setting.setting_value AS tree_title', 29901461f86SGreg Roach ]) 30001461f86SGreg Roach ->orderBy('gedcom.sort_order') 30101461f86SGreg Roach ->orderBy('gedcom_setting.setting_value'); 30201461f86SGreg Roach 30332f20c14SGreg Roach // Non-admins may not see all trees 30432f20c14SGreg Roach if (!Auth::isAdmin()) { 30501461f86SGreg Roach $query 30636357577SGreg Roach ->join('gedcom_setting AS gs2', function (JoinClause $join): void { 30736357577SGreg Roach $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 30801461f86SGreg Roach ->where('gs2.setting_name', '=', 'imported'); 30936357577SGreg Roach }) 31036357577SGreg Roach ->join('gedcom_setting AS gs3', function (JoinClause $join): void { 31101461f86SGreg Roach $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 31201461f86SGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 31301461f86SGreg Roach }) 31401461f86SGreg Roach ->leftJoin('user_gedcom_setting', function (JoinClause $join): void { 31501461f86SGreg Roach $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 31601461f86SGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 31701461f86SGreg Roach ->where('user_gedcom_setting.setting_name', '=', 'canedit'); 31801461f86SGreg Roach }) 31901461f86SGreg Roach ->where(function (Builder $query): void { 32001461f86SGreg Roach $query 32101461f86SGreg Roach // Managers 32201461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '=', 'admin') 32301461f86SGreg Roach // Members 32401461f86SGreg Roach ->orWhere(function (Builder $query): void { 32501461f86SGreg Roach $query 32601461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 32701461f86SGreg Roach ->where('gs3.setting_value', '=', '1') 32801461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '<>', 'none'); 32901461f86SGreg Roach }) 3308b67c11aSGreg Roach // Public trees 33101461f86SGreg Roach ->orWhere(function (Builder $query): void { 33201461f86SGreg Roach $query 33301461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 33436357577SGreg Roach ->where('gs3.setting_value', '<>', '1'); 33501461f86SGreg Roach }); 33601461f86SGreg Roach }); 33701461f86SGreg Roach } 33801461f86SGreg Roach 3398b67c11aSGreg Roach return $query 3408b67c11aSGreg Roach ->get() 3418b67c11aSGreg Roach ->mapWithKeys(function (stdClass $row): array { 3428b67c11aSGreg Roach return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)]; 3438b67c11aSGreg Roach }); 3448b67c11aSGreg Roach }); 345a25f0a04SGreg Roach } 3468b67c11aSGreg Roach 3478b67c11aSGreg Roach /** 3488b67c11aSGreg Roach * Fetch all the trees that we have permission to access. 3498b67c11aSGreg Roach * 3508b67c11aSGreg Roach * @return Tree[] 3518b67c11aSGreg Roach */ 3528b67c11aSGreg Roach public static function getAll(): array 3538b67c11aSGreg Roach { 3548b67c11aSGreg Roach if (empty(self::$trees)) { 3558b67c11aSGreg Roach self::$trees = self::all()->all(); 356a25f0a04SGreg Roach } 357a25f0a04SGreg Roach 358a25f0a04SGreg Roach return self::$trees; 359a25f0a04SGreg Roach } 360a25f0a04SGreg Roach 361a25f0a04SGreg Roach /** 362d2cdeb3fSGreg Roach * Find the tree with a specific ID. 363a25f0a04SGreg Roach * 364cbc1590aSGreg Roach * @param int $tree_id 365cbc1590aSGreg Roach * 366a25f0a04SGreg Roach * @return Tree 367a25f0a04SGreg Roach */ 368c0804649SGreg Roach public static function findById(int $tree_id): Tree 369c1010edaSGreg Roach { 370c0804649SGreg Roach return self::getAll()[$tree_id]; 371a25f0a04SGreg Roach } 372a25f0a04SGreg Roach 373a25f0a04SGreg Roach /** 374d2cdeb3fSGreg Roach * Find the tree with a specific name. 375cf4bcc09SGreg Roach * 376cf4bcc09SGreg Roach * @param string $tree_name 377cf4bcc09SGreg Roach * 378cf4bcc09SGreg Roach * @return Tree|null 379cf4bcc09SGreg Roach */ 380c1010edaSGreg Roach public static function findByName($tree_name) 381c1010edaSGreg Roach { 382cf4bcc09SGreg Roach foreach (self::getAll() as $tree) { 38351d0f842SGreg Roach if ($tree->name === $tree_name) { 384cf4bcc09SGreg Roach return $tree; 385cf4bcc09SGreg Roach } 386cf4bcc09SGreg Roach } 387cf4bcc09SGreg Roach 388cf4bcc09SGreg Roach return null; 389cf4bcc09SGreg Roach } 390cf4bcc09SGreg Roach 391cf4bcc09SGreg Roach /** 392a25f0a04SGreg Roach * Create arguments to select_edit_control() 393a25f0a04SGreg Roach * Note - these will be escaped later 394a25f0a04SGreg Roach * 395a25f0a04SGreg Roach * @return string[] 396a25f0a04SGreg Roach */ 397771ae10aSGreg Roach public static function getIdList(): array 398c1010edaSGreg Roach { 39913abd6f3SGreg Roach $list = []; 400a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 40172cf66d4SGreg Roach $list[$tree->id] = $tree->title; 402a25f0a04SGreg Roach } 403a25f0a04SGreg Roach 404a25f0a04SGreg Roach return $list; 405a25f0a04SGreg Roach } 406a25f0a04SGreg Roach 407a25f0a04SGreg Roach /** 408a25f0a04SGreg Roach * Create arguments to select_edit_control() 409a25f0a04SGreg Roach * Note - these will be escaped later 410a25f0a04SGreg Roach * 411a25f0a04SGreg Roach * @return string[] 412a25f0a04SGreg Roach */ 413771ae10aSGreg Roach public static function getNameList(): array 414c1010edaSGreg Roach { 41513abd6f3SGreg Roach $list = []; 416a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 417a25f0a04SGreg Roach $list[$tree->name] = $tree->title; 418a25f0a04SGreg Roach } 419a25f0a04SGreg Roach 420a25f0a04SGreg Roach return $list; 421a25f0a04SGreg Roach } 422a25f0a04SGreg Roach 423a25f0a04SGreg Roach /** 424a25f0a04SGreg Roach * Create a new tree 425a25f0a04SGreg Roach * 426a25f0a04SGreg Roach * @param string $tree_name 427a25f0a04SGreg Roach * @param string $tree_title 428a25f0a04SGreg Roach * 429a25f0a04SGreg Roach * @return Tree 430a25f0a04SGreg Roach */ 431771ae10aSGreg Roach public static function create(string $tree_name, string $tree_title): Tree 432c1010edaSGreg Roach { 433a25f0a04SGreg Roach try { 434a25f0a04SGreg Roach // Create a new tree 43501461f86SGreg Roach DB::table('gedcom')->insert([ 43601461f86SGreg Roach 'gedcom_name' => $tree_name, 43701461f86SGreg Roach ]); 4384a86d714SGreg Roach 439061b43d7SGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 44032f20c14SGreg Roach 44132f20c14SGreg Roach $tree = new self($tree_id, $tree_name, $tree_title); 442a25f0a04SGreg Roach } catch (PDOException $ex) { 443a25f0a04SGreg Roach // A tree with that name already exists? 444ef2fd529SGreg Roach return self::findByName($tree_name); 445a25f0a04SGreg Roach } 446a25f0a04SGreg Roach 447a25f0a04SGreg Roach $tree->setPreference('imported', '0'); 448a25f0a04SGreg Roach $tree->setPreference('title', $tree_title); 449a25f0a04SGreg Roach 4501507cbcaSGreg Roach // Set preferences from default tree 451061b43d7SGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 452061b43d7SGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 453061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 454061b43d7SGreg Roach $query 455061b43d7SGreg Roach ->select([DB::raw($tree_id), 'setting_name', 'setting_value']) 456061b43d7SGreg Roach ->from('gedcom_setting') 457061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 458061b43d7SGreg Roach } 459061b43d7SGreg Roach ); 4601507cbcaSGreg Roach 461061b43d7SGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 462061b43d7SGreg Roach ['gedcom_id', 'tag_type', 'resn'], 463061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 464061b43d7SGreg Roach $query 465061b43d7SGreg Roach ->select([DB::raw($tree_id), 'tag_type', 'resn']) 466061b43d7SGreg Roach ->from('default_resn') 467061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 468061b43d7SGreg Roach } 469061b43d7SGreg Roach ); 4701507cbcaSGreg Roach 471061b43d7SGreg Roach (new Builder(DB::connection()))->from('block')->insertUsing( 472061b43d7SGreg Roach ['gedcom_id', 'location', 'block_order', 'module_name'], 473061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 474061b43d7SGreg Roach $query 475061b43d7SGreg Roach ->select([DB::raw($tree_id), 'location', 'block_order', 'module_name']) 476061b43d7SGreg Roach ->from('block') 477061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 478061b43d7SGreg Roach } 479061b43d7SGreg Roach ); 4801507cbcaSGreg Roach 481a25f0a04SGreg Roach // Gedcom and privacy settings 48276f666f4SGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 48376f666f4SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 484a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 485a25f0a04SGreg Roach switch (WT_LOCALE) { 486a25f0a04SGreg Roach case 'es': 487a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 488a25f0a04SGreg Roach break; 489a25f0a04SGreg Roach case 'is': 490a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 491a25f0a04SGreg Roach break; 492a25f0a04SGreg Roach case 'lt': 493a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 494a25f0a04SGreg Roach break; 495a25f0a04SGreg Roach case 'pl': 496a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 497a25f0a04SGreg Roach break; 498a25f0a04SGreg Roach case 'pt': 499a25f0a04SGreg Roach case 'pt-BR': 500a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 501a25f0a04SGreg Roach break; 502a25f0a04SGreg Roach default: 503a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 504a25f0a04SGreg Roach break; 505a25f0a04SGreg Roach } 506a25f0a04SGreg Roach 507a25f0a04SGreg Roach // Genealogy data 508a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 509bbb76c12SGreg Roach /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 510bbb76c12SGreg Roach $john_doe = I18N::translate('John /DOE/'); 51177e70a22SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 512061b43d7SGreg 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"; 513061b43d7SGreg Roach 514061b43d7SGreg Roach DB::table('gedcom_chunk')->insert([ 515061b43d7SGreg Roach 'gedcom_id' => $tree_id, 516061b43d7SGreg Roach 'chunk_data' => $gedcom, 51713abd6f3SGreg Roach ]); 518a25f0a04SGreg Roach 519a25f0a04SGreg Roach // Update our cache 52072cf66d4SGreg Roach self::$trees[$tree->id] = $tree; 521a25f0a04SGreg Roach 522a25f0a04SGreg Roach return $tree; 523a25f0a04SGreg Roach } 524a25f0a04SGreg Roach 525a25f0a04SGreg Roach /** 526b78374c5SGreg Roach * Are there any pending edits for this tree, than need reviewing by a moderator. 527b78374c5SGreg Roach * 528b78374c5SGreg Roach * @return bool 529b78374c5SGreg Roach */ 530771ae10aSGreg Roach public function hasPendingEdit(): bool 531c1010edaSGreg Roach { 53215a3f100SGreg Roach return DB::table('change') 53315a3f100SGreg Roach ->where('gedcom_id', '=', $this->id) 53415a3f100SGreg Roach ->where('status', '=', 'pending') 53515a3f100SGreg Roach ->exists(); 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 { 5501ad2dde6SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5511ad2dde6SGreg Roach DB::table('individuals')->where('i_file', '=', $this->id)->delete(); 5521ad2dde6SGreg Roach DB::table('families')->where('f_file', '=', $this->id)->delete(); 5531ad2dde6SGreg Roach DB::table('sources')->where('s_file', '=', $this->id)->delete(); 5541ad2dde6SGreg Roach DB::table('other')->where('o_file', '=', $this->id)->delete(); 5551ad2dde6SGreg Roach DB::table('places')->where('p_file', '=', $this->id)->delete(); 5561ad2dde6SGreg Roach DB::table('placelinks')->where('pl_file', '=', $this->id)->delete(); 5571ad2dde6SGreg Roach DB::table('name')->where('n_file', '=', $this->id)->delete(); 5581ad2dde6SGreg Roach DB::table('dates')->where('d_file', '=', $this->id)->delete(); 5591ad2dde6SGreg Roach DB::table('change')->where('gedcom_id', '=', $this->id)->delete(); 560a25f0a04SGreg Roach 561a25f0a04SGreg Roach if ($keep_media) { 5621ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id) 5631ad2dde6SGreg Roach ->where('l_type', '<>', 'OBJE') 5641ad2dde6SGreg Roach ->delete(); 565a25f0a04SGreg Roach } else { 5661ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id)->delete(); 5671ad2dde6SGreg Roach DB::table('media_file')->where('m_file', '=', $this->id)->delete(); 5681ad2dde6SGreg Roach DB::table('media')->where('m_file', '=', $this->id)->delete(); 569a25f0a04SGreg Roach } 570a25f0a04SGreg Roach } 571a25f0a04SGreg Roach 572a25f0a04SGreg Roach /** 573a25f0a04SGreg Roach * Delete everything relating to a tree 574b7e60af1SGreg Roach * 575b7e60af1SGreg Roach * @return void 576a25f0a04SGreg Roach */ 577c1010edaSGreg Roach public function delete() 578c1010edaSGreg Roach { 579a25f0a04SGreg Roach // If this is the default tree, then unset it 580ef2fd529SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 581a25f0a04SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 582a25f0a04SGreg Roach } 583a25f0a04SGreg Roach 584a25f0a04SGreg Roach $this->deleteGenealogyData(false); 585a25f0a04SGreg Roach 586a7890280SGreg Roach DB::table('block_setting') 587a7890280SGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 588a7890280SGreg Roach ->where('gedcom_id', '=', $this->id) 589a7890280SGreg Roach ->delete(); 590a7890280SGreg Roach DB::table('block')->where('gedcom_id', '=', $this->id)->delete(); 591a7890280SGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 592a7890280SGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 593a7890280SGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete(); 594a7890280SGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete(); 595a7890280SGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete(); 596a7890280SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 597a7890280SGreg Roach DB::table('log')->where('gedcom_id', '=', $this->id)->delete(); 598a7890280SGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete(); 599a25f0a04SGreg Roach 600a25f0a04SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 60175a9f908SGreg Roach self::$trees = []; 602a25f0a04SGreg Roach } 603a25f0a04SGreg Roach 604a25f0a04SGreg Roach /** 605a25f0a04SGreg Roach * Export the tree to a GEDCOM file 606a25f0a04SGreg Roach * 6075792757eSGreg Roach * @param resource $stream 608b7e60af1SGreg Roach * 609b7e60af1SGreg Roach * @return void 610a25f0a04SGreg Roach */ 611c1010edaSGreg Roach public function exportGedcom($stream) 612c1010edaSGreg Roach { 613a3d8780cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8')); 61494026f20SGreg Roach 61594026f20SGreg Roach $union_families = DB::table('families') 61694026f20SGreg Roach ->where('f_file', '=', $this->id) 61794026f20SGreg Roach ->select(['f_gedcom AS gedcom', 'f_id AS xref', DB::raw('LENGTH(f_id) AS len'), DB::raw('2 AS n')]); 61894026f20SGreg Roach 61994026f20SGreg Roach $union_sources = DB::table('sources') 62094026f20SGreg Roach ->where('s_file', '=', $this->id) 62194026f20SGreg Roach ->select(['s_gedcom AS gedcom', 's_id AS xref', DB::raw('LENGTH(s_id) AS len'), DB::raw('3 AS n')]); 62294026f20SGreg Roach 62394026f20SGreg Roach $union_other = DB::table('other') 62494026f20SGreg Roach ->where('o_file', '=', $this->id) 62594026f20SGreg Roach ->whereNotIn('o_type', ['HEAD', 'TRLR']) 62694026f20SGreg Roach ->select(['o_gedcom AS gedcom', 'o_id AS xref', DB::raw('LENGTH(o_id) AS len'), DB::raw('4 AS n')]); 62794026f20SGreg Roach 62894026f20SGreg Roach $union_media = DB::table('media') 62994026f20SGreg Roach ->where('m_file', '=', $this->id) 63094026f20SGreg Roach ->select(['m_gedcom AS gedcom', 'm_id AS xref', DB::raw('LENGTH(m_id) AS len'), DB::raw('5 AS n')]); 63194026f20SGreg Roach 63294026f20SGreg Roach $rows = DB::table('individuals') 63394026f20SGreg Roach ->where('i_file', '=', $this->id) 63494026f20SGreg Roach ->select(['i_gedcom AS gedcom', 'i_id AS xref', DB::raw('LENGTH(i_id) AS len'), DB::raw('1 AS n')]) 63594026f20SGreg Roach ->union($union_families) 63694026f20SGreg Roach ->union($union_sources) 63794026f20SGreg Roach ->union($union_other) 63894026f20SGreg Roach ->union($union_media) 63994026f20SGreg Roach ->orderBy('n') 64094026f20SGreg Roach ->orderBy('len') 64194026f20SGreg Roach ->orderBy('xref') 64294026f20SGreg Roach ->chunk(100, function (Collection $rows) use ($stream, &$buffer): void { 64394026f20SGreg Roach foreach ($rows as $row) { 6443d7a8a4cSGreg Roach $buffer .= FunctionsExport::reformatRecord($row->gedcom); 645a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 6465792757eSGreg Roach fwrite($stream, $buffer); 647a25f0a04SGreg Roach $buffer = ''; 648a25f0a04SGreg Roach } 649a25f0a04SGreg Roach } 65094026f20SGreg Roach }); 65194026f20SGreg Roach 6520f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 653a25f0a04SGreg Roach } 654a25f0a04SGreg Roach 655a25f0a04SGreg Roach /** 656a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 657a25f0a04SGreg Roach * 658a25f0a04SGreg Roach * @param string $path The full path to the (possibly temporary) file. 659a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 660a25f0a04SGreg Roach * 661b7e60af1SGreg Roach * @return void 662a25f0a04SGreg Roach */ 663771ae10aSGreg Roach public function importGedcomFile(string $path, string $filename) 664c1010edaSGreg Roach { 665a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 666a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 667a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 668a25f0a04SGreg Roach // each block. 669a25f0a04SGreg Roach 670a25f0a04SGreg Roach $file_data = ''; 671a25f0a04SGreg Roach $fp = fopen($path, 'rb'); 672a25f0a04SGreg Roach 673b7e60af1SGreg Roach $this->deleteGenealogyData((bool) $this->getPreference('keep_media')); 674a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 675a25f0a04SGreg Roach $this->setPreference('imported', '0'); 676a25f0a04SGreg Roach 677a25f0a04SGreg Roach while (!feof($fp)) { 678a25f0a04SGreg Roach $file_data .= fread($fp, 65536); 679a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 680a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 681a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 682a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 683a25f0a04SGreg Roach break; 684a25f0a04SGreg Roach } 685a25f0a04SGreg Roach } 686a25f0a04SGreg Roach if ($pos) { 6871ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6881ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6891ad2dde6SGreg Roach 'chunk_data' => substr($file_data, 0, $pos), 690c1010edaSGreg Roach ]); 6911ad2dde6SGreg Roach 692a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 693a25f0a04SGreg Roach } 694a25f0a04SGreg Roach } 6951ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6961ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6971ad2dde6SGreg Roach 'chunk_data' => $file_data, 698c1010edaSGreg Roach ]); 699a25f0a04SGreg Roach 700a25f0a04SGreg Roach fclose($fp); 701a25f0a04SGreg Roach } 702304f20d5SGreg Roach 703304f20d5SGreg Roach /** 704b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 705b90d8accSGreg Roach * 706b90d8accSGreg Roach * @return string 707b90d8accSGreg Roach */ 708771ae10aSGreg Roach public function getNewXref(): string 709c1010edaSGreg Roach { 710963fbaeeSGreg Roach // Lock the row, so that only one new XREF may be generated at a time. 711963fbaeeSGreg Roach DB::table('site_setting') 712963fbaeeSGreg Roach ->where('setting_name', '=', 'next_xref') 713963fbaeeSGreg Roach ->lockForUpdate() 714963fbaeeSGreg Roach ->get(); 715963fbaeeSGreg Roach 716a214e186SGreg Roach $prefix = 'X'; 717b90d8accSGreg Roach 718971d66c8SGreg Roach $increment = 1.0; 719b90d8accSGreg Roach do { 720963fbaeeSGreg Roach $num = (int) Site::getPreference('next_xref') + (int) $increment; 721971d66c8SGreg Roach 722971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 723971d66c8SGreg Roach // existing data in a reasonable time. 724971d66c8SGreg Roach $increment *= 1.01; 725963fbaeeSGreg Roach 726963fbaeeSGreg Roach $xref = $prefix . $num; 727963fbaeeSGreg Roach 728963fbaeeSGreg Roach // Records may already exist with this sequence number. 729963fbaeeSGreg Roach $already_used = 730963fbaeeSGreg Roach DB::table('individuals')->where('i_id', '=', $xref)->exists() || 731963fbaeeSGreg Roach DB::table('families')->where('f_id', '=', $xref)->exists() || 732963fbaeeSGreg Roach DB::table('sources')->where('s_id', '=', $xref)->exists() || 733963fbaeeSGreg Roach DB::table('media')->where('m_id', '=', $xref)->exists() || 734963fbaeeSGreg Roach DB::table('other')->where('o_id', '=', $xref)->exists() || 735963fbaeeSGreg Roach DB::table('change')->where('xref', '=', $xref)->exists(); 736963fbaeeSGreg Roach } while ($already_used); 737963fbaeeSGreg Roach 738963fbaeeSGreg Roach Site::setPreference('next_xref', (string) $num); 739b90d8accSGreg Roach 740a214e186SGreg Roach return $xref; 741b90d8accSGreg Roach } 742b90d8accSGreg Roach 743b90d8accSGreg Roach /** 744304f20d5SGreg Roach * Create a new record from GEDCOM data. 745304f20d5SGreg Roach * 746304f20d5SGreg Roach * @param string $gedcom 747304f20d5SGreg Roach * 74815d603e7SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 749afb591d7SGreg Roach * @throws InvalidArgumentException 750304f20d5SGreg Roach */ 751771ae10aSGreg Roach public function createRecord(string $gedcom): GedcomRecord 752c1010edaSGreg Roach { 753bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ ')) { 754afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@'); 755304f20d5SGreg Roach } 756304f20d5SGreg Roach 757a214e186SGreg Roach $xref = $this->getNewXref(); 758bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ '); 759304f20d5SGreg Roach 760afb591d7SGreg Roach // Create a change record 761304f20d5SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 762304f20d5SGreg Roach 763304f20d5SGreg Roach // Create a pending change 764963fbaeeSGreg Roach DB::table('change')->insert([ 765963fbaeeSGreg Roach 'gedcom_id' => $this->id, 766963fbaeeSGreg Roach 'xref' => $xref, 767963fbaeeSGreg Roach 'old_gedcom' => '', 768963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 769963fbaeeSGreg Roach 'user_id' => Auth::id(), 77013abd6f3SGreg Roach ]); 771304f20d5SGreg Roach 772afb591d7SGreg Roach // Accept this pending change 773afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 774afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 775afb591d7SGreg Roach 776afb591d7SGreg Roach return new GedcomRecord($xref, $gedcom, null, $this); 777afb591d7SGreg Roach } 778afb591d7SGreg Roach 779313e72b3SGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 780afb591d7SGreg Roach } 781afb591d7SGreg Roach 782afb591d7SGreg Roach /** 783afb591d7SGreg Roach * Create a new family from GEDCOM data. 784afb591d7SGreg Roach * 785afb591d7SGreg Roach * @param string $gedcom 786afb591d7SGreg Roach * 787afb591d7SGreg Roach * @return Family 788afb591d7SGreg Roach * @throws InvalidArgumentException 789afb591d7SGreg Roach */ 790afb591d7SGreg Roach public function createFamily(string $gedcom): GedcomRecord 791afb591d7SGreg Roach { 792bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ FAM')) { 793afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM'); 794afb591d7SGreg Roach } 795afb591d7SGreg Roach 796afb591d7SGreg Roach $xref = $this->getNewXref(); 797bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM'); 798afb591d7SGreg Roach 799afb591d7SGreg Roach // Create a change record 800afb591d7SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 801afb591d7SGreg Roach 802afb591d7SGreg Roach // Create a pending change 803963fbaeeSGreg Roach DB::table('change')->insert([ 804963fbaeeSGreg Roach 'gedcom_id' => $this->id, 805963fbaeeSGreg Roach 'xref' => $xref, 806963fbaeeSGreg Roach 'old_gedcom' => '', 807963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 808963fbaeeSGreg Roach 'user_id' => Auth::id(), 809afb591d7SGreg Roach ]); 810304f20d5SGreg Roach 811304f20d5SGreg Roach // Accept this pending change 812304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 813cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 814afb591d7SGreg Roach 815afb591d7SGreg Roach return new Family($xref, $gedcom, null, $this); 816304f20d5SGreg Roach } 817afb591d7SGreg Roach 818afb591d7SGreg Roach return new Family($xref, '', $gedcom, $this); 819afb591d7SGreg Roach } 820afb591d7SGreg Roach 821afb591d7SGreg Roach /** 822afb591d7SGreg Roach * Create a new individual from GEDCOM data. 823afb591d7SGreg Roach * 824afb591d7SGreg Roach * @param string $gedcom 825afb591d7SGreg Roach * 826afb591d7SGreg Roach * @return Individual 827afb591d7SGreg Roach * @throws InvalidArgumentException 828afb591d7SGreg Roach */ 829afb591d7SGreg Roach public function createIndividual(string $gedcom): GedcomRecord 830afb591d7SGreg Roach { 831bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ INDI')) { 832afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI'); 833afb591d7SGreg Roach } 834afb591d7SGreg Roach 835afb591d7SGreg Roach $xref = $this->getNewXref(); 836bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI'); 837afb591d7SGreg Roach 838afb591d7SGreg Roach // Create a change record 839afb591d7SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 840afb591d7SGreg Roach 841afb591d7SGreg Roach // Create a pending change 842963fbaeeSGreg Roach DB::table('change')->insert([ 843963fbaeeSGreg Roach 'gedcom_id' => $this->id, 844963fbaeeSGreg Roach 'xref' => $xref, 845963fbaeeSGreg Roach 'old_gedcom' => '', 846963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 847963fbaeeSGreg Roach 'user_id' => Auth::id(), 848afb591d7SGreg Roach ]); 849afb591d7SGreg Roach 850afb591d7SGreg Roach // Accept this pending change 851afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 852afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 853afb591d7SGreg Roach 854afb591d7SGreg Roach return new Individual($xref, $gedcom, null, $this); 855afb591d7SGreg Roach } 856afb591d7SGreg Roach 857afb591d7SGreg Roach return new Individual($xref, '', $gedcom, $this); 858304f20d5SGreg Roach } 8598586983fSGreg Roach 8608586983fSGreg Roach /** 86120b58d20SGreg Roach * Create a new media object from GEDCOM data. 86220b58d20SGreg Roach * 86320b58d20SGreg Roach * @param string $gedcom 86420b58d20SGreg Roach * 86520b58d20SGreg Roach * @return Media 86620b58d20SGreg Roach * @throws InvalidArgumentException 86720b58d20SGreg Roach */ 86820b58d20SGreg Roach public function createMediaObject(string $gedcom): Media 86920b58d20SGreg Roach { 870bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ OBJE')) { 87120b58d20SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE'); 87220b58d20SGreg Roach } 87320b58d20SGreg Roach 87420b58d20SGreg Roach $xref = $this->getNewXref(); 875bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE'); 87620b58d20SGreg Roach 87720b58d20SGreg Roach // Create a change record 87820b58d20SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 87920b58d20SGreg Roach 88020b58d20SGreg Roach // Create a pending change 881963fbaeeSGreg Roach DB::table('change')->insert([ 882963fbaeeSGreg Roach 'gedcom_id' => $this->id, 883963fbaeeSGreg Roach 'xref' => $xref, 884963fbaeeSGreg Roach 'old_gedcom' => '', 885963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 886963fbaeeSGreg Roach 'user_id' => Auth::id(), 88720b58d20SGreg Roach ]); 88820b58d20SGreg Roach 88920b58d20SGreg Roach // Accept this pending change 89020b58d20SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 89120b58d20SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 89220b58d20SGreg Roach 89320b58d20SGreg Roach return new Media($xref, $gedcom, null, $this); 89420b58d20SGreg Roach } 89520b58d20SGreg Roach 89620b58d20SGreg Roach return new Media($xref, '', $gedcom, $this); 89720b58d20SGreg Roach } 89820b58d20SGreg Roach 89920b58d20SGreg Roach /** 9008586983fSGreg Roach * What is the most significant individual in this tree. 9018586983fSGreg Roach * 9028586983fSGreg Roach * @param User $user 9038586983fSGreg Roach * 9048586983fSGreg Roach * @return Individual 9058586983fSGreg Roach */ 906c1010edaSGreg Roach public function significantIndividual(User $user): Individual 907c1010edaSGreg Roach { 9088f9b0fb2SGreg Roach $individual = null; 9098586983fSGreg Roach 9108f9b0fb2SGreg Roach if ($this->getUserPreference($user, 'rootid') !== '') { 9118586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 9128586983fSGreg Roach } 9138f9b0fb2SGreg Roach 9148f9b0fb2SGreg Roach if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') { 9158586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 9168586983fSGreg Roach } 9178f9b0fb2SGreg Roach 918bec87e94SGreg Roach if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') { 9198586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 9208586983fSGreg Roach } 9218f9b0fb2SGreg Roach if ($individual === null) { 9228f9b0fb2SGreg Roach $xref = (string) DB::table('individuals') 9238f9b0fb2SGreg Roach ->where('i_file', '=', $this->id()) 9248f9b0fb2SGreg Roach ->min('i_id'); 925769d7d6eSGreg Roach 926769d7d6eSGreg Roach $individual = Individual::getInstance($xref, $this); 9275fe1add5SGreg Roach } 9288f9b0fb2SGreg Roach if ($individual === null) { 9295fe1add5SGreg Roach // always return a record 9305fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 9315fe1add5SGreg Roach } 9325fe1add5SGreg Roach 9335fe1add5SGreg Roach return $individual; 9345fe1add5SGreg Roach } 935a25f0a04SGreg Roach} 936