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; 29*8b67c11aSGreg 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. 227a25f0a04SGreg Roach if (!array_key_exists($user->getUserId(), $this->user_preferences)) { 228061b43d7SGreg Roach $this->user_preferences[$user->getUserId()] = DB::table('user_gedcom_setting') 229061b43d7SGreg Roach ->where('user_id', '=', $user->getUserId()) 230061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 231061b43d7SGreg Roach ->pluck('setting_value', 'setting_name') 232061b43d7SGreg Roach ->all(); 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach 235b2ce94c6SRico Sonntag return $this->user_preferences[$user->getUserId()][$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(), 253a25f0a04SGreg Roach 'user_id' => $user->getUserId(), 254a25f0a04SGreg Roach 'setting_name' => $setting_name, 255061b43d7SGreg Roach ], [ 256cbc1590aSGreg Roach 'setting_value' => $setting_value, 25713abd6f3SGreg Roach ]); 258061b43d7SGreg Roach 259061b43d7SGreg Roach // Update the cache 260a25f0a04SGreg Roach $this->user_preferences[$user->getUserId()][$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 /** 281*8b67c11aSGreg Roach * All the trees that we have permission to access. 282a25f0a04SGreg Roach * 283*8b67c11aSGreg Roach * @return Collection|Tree[] 284a25f0a04SGreg Roach */ 285*8b67c11aSGreg Roach public static function all(): Collection 286c1010edaSGreg Roach { 287*8b67c11aSGreg 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 }) 330*8b67c11aSGreg 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 339*8b67c11aSGreg Roach return $query 340*8b67c11aSGreg Roach ->get() 341*8b67c11aSGreg Roach ->mapWithKeys(function (stdClass $row): array { 342*8b67c11aSGreg Roach return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)]; 343*8b67c11aSGreg Roach }); 344*8b67c11aSGreg Roach }); 345a25f0a04SGreg Roach } 346*8b67c11aSGreg Roach 347*8b67c11aSGreg Roach /** 348*8b67c11aSGreg Roach * Fetch all the trees that we have permission to access. 349*8b67c11aSGreg Roach * 350*8b67c11aSGreg Roach * @return Tree[] 351*8b67c11aSGreg Roach */ 352*8b67c11aSGreg Roach public static function getAll(): array 353*8b67c11aSGreg Roach { 354*8b67c11aSGreg Roach if (empty(self::$trees)) { 355*8b67c11aSGreg 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 450a25f0a04SGreg Roach // Module privacy 451a25f0a04SGreg Roach Module::setDefaultAccess($tree_id); 452a25f0a04SGreg Roach 4531507cbcaSGreg Roach // Set preferences from default tree 454061b43d7SGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 455061b43d7SGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 456061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 457061b43d7SGreg Roach $query 458061b43d7SGreg Roach ->select([DB::raw($tree_id), 'setting_name', 'setting_value']) 459061b43d7SGreg Roach ->from('gedcom_setting') 460061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 461061b43d7SGreg Roach } 462061b43d7SGreg Roach ); 4631507cbcaSGreg Roach 464061b43d7SGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 465061b43d7SGreg Roach ['gedcom_id', 'tag_type', 'resn'], 466061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 467061b43d7SGreg Roach $query 468061b43d7SGreg Roach ->select([DB::raw($tree_id), 'tag_type', 'resn']) 469061b43d7SGreg Roach ->from('default_resn') 470061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 471061b43d7SGreg Roach } 472061b43d7SGreg Roach ); 4731507cbcaSGreg Roach 474061b43d7SGreg Roach (new Builder(DB::connection()))->from('block')->insertUsing( 475061b43d7SGreg Roach ['gedcom_id', 'location', 'block_order', 'module_name'], 476061b43d7SGreg Roach function (Builder $query) use ($tree_id): void { 477061b43d7SGreg Roach $query 478061b43d7SGreg Roach ->select([DB::raw($tree_id), 'location', 'block_order', 'module_name']) 479061b43d7SGreg Roach ->from('block') 480061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 481061b43d7SGreg Roach } 482061b43d7SGreg Roach ); 4831507cbcaSGreg Roach 484a25f0a04SGreg Roach // Gedcom and privacy settings 48576f666f4SGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 48676f666f4SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 487a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 488a25f0a04SGreg Roach switch (WT_LOCALE) { 489a25f0a04SGreg Roach case 'es': 490a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 491a25f0a04SGreg Roach break; 492a25f0a04SGreg Roach case 'is': 493a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 494a25f0a04SGreg Roach break; 495a25f0a04SGreg Roach case 'lt': 496a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 497a25f0a04SGreg Roach break; 498a25f0a04SGreg Roach case 'pl': 499a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 500a25f0a04SGreg Roach break; 501a25f0a04SGreg Roach case 'pt': 502a25f0a04SGreg Roach case 'pt-BR': 503a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 504a25f0a04SGreg Roach break; 505a25f0a04SGreg Roach default: 506a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 507a25f0a04SGreg Roach break; 508a25f0a04SGreg Roach } 509a25f0a04SGreg Roach 510a25f0a04SGreg Roach // Genealogy data 511a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 512bbb76c12SGreg Roach /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 513bbb76c12SGreg Roach $john_doe = I18N::translate('John /DOE/'); 51477e70a22SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 515061b43d7SGreg 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"; 516061b43d7SGreg Roach 517061b43d7SGreg Roach DB::table('gedcom_chunk')->insert([ 518061b43d7SGreg Roach 'gedcom_id' => $tree_id, 519061b43d7SGreg Roach 'chunk_data' => $gedcom, 52013abd6f3SGreg Roach ]); 521a25f0a04SGreg Roach 522a25f0a04SGreg Roach // Update our cache 52372cf66d4SGreg Roach self::$trees[$tree->id] = $tree; 524a25f0a04SGreg Roach 525a25f0a04SGreg Roach return $tree; 526a25f0a04SGreg Roach } 527a25f0a04SGreg Roach 528a25f0a04SGreg Roach /** 529b78374c5SGreg Roach * Are there any pending edits for this tree, than need reviewing by a moderator. 530b78374c5SGreg Roach * 531b78374c5SGreg Roach * @return bool 532b78374c5SGreg Roach */ 533771ae10aSGreg Roach public function hasPendingEdit(): bool 534c1010edaSGreg Roach { 53515a3f100SGreg Roach return DB::table('change') 53615a3f100SGreg Roach ->where('gedcom_id', '=', $this->id) 53715a3f100SGreg Roach ->where('status', '=', 'pending') 53815a3f100SGreg Roach ->exists(); 539b78374c5SGreg Roach } 540b78374c5SGreg Roach 541b78374c5SGreg Roach /** 542a25f0a04SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 543a25f0a04SGreg Roach * new data. Optionally retain the media data, for when the user has been 544a25f0a04SGreg Roach * editing their data offline using an application which deletes (or does not 545a25f0a04SGreg Roach * support) media data. 546a25f0a04SGreg Roach * 547a25f0a04SGreg Roach * @param bool $keep_media 548b7e60af1SGreg Roach * 549b7e60af1SGreg Roach * @return void 550a25f0a04SGreg Roach */ 551b7e60af1SGreg Roach public function deleteGenealogyData(bool $keep_media) 552c1010edaSGreg Roach { 5531ad2dde6SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5541ad2dde6SGreg Roach DB::table('individuals')->where('i_file', '=', $this->id)->delete(); 5551ad2dde6SGreg Roach DB::table('families')->where('f_file', '=', $this->id)->delete(); 5561ad2dde6SGreg Roach DB::table('sources')->where('s_file', '=', $this->id)->delete(); 5571ad2dde6SGreg Roach DB::table('other')->where('o_file', '=', $this->id)->delete(); 5581ad2dde6SGreg Roach DB::table('places')->where('p_file', '=', $this->id)->delete(); 5591ad2dde6SGreg Roach DB::table('placelinks')->where('pl_file', '=', $this->id)->delete(); 5601ad2dde6SGreg Roach DB::table('name')->where('n_file', '=', $this->id)->delete(); 5611ad2dde6SGreg Roach DB::table('dates')->where('d_file', '=', $this->id)->delete(); 5621ad2dde6SGreg Roach DB::table('change')->where('gedcom_id', '=', $this->id)->delete(); 563a25f0a04SGreg Roach 564a25f0a04SGreg Roach if ($keep_media) { 5651ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id) 5661ad2dde6SGreg Roach ->where('l_type', '<>', 'OBJE') 5671ad2dde6SGreg Roach ->delete(); 568a25f0a04SGreg Roach } else { 5691ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id)->delete(); 5701ad2dde6SGreg Roach DB::table('media_file')->where('m_file', '=', $this->id)->delete(); 5711ad2dde6SGreg Roach DB::table('media')->where('m_file', '=', $this->id)->delete(); 572a25f0a04SGreg Roach } 573a25f0a04SGreg Roach } 574a25f0a04SGreg Roach 575a25f0a04SGreg Roach /** 576a25f0a04SGreg Roach * Delete everything relating to a tree 577b7e60af1SGreg Roach * 578b7e60af1SGreg Roach * @return void 579a25f0a04SGreg Roach */ 580c1010edaSGreg Roach public function delete() 581c1010edaSGreg Roach { 582a25f0a04SGreg Roach // If this is the default tree, then unset it 583ef2fd529SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 584a25f0a04SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 585a25f0a04SGreg Roach } 586a25f0a04SGreg Roach 587a25f0a04SGreg Roach $this->deleteGenealogyData(false); 588a25f0a04SGreg Roach 589a7890280SGreg Roach DB::table('block_setting') 590a7890280SGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 591a7890280SGreg Roach ->where('gedcom_id', '=', $this->id) 592a7890280SGreg Roach ->delete(); 593a7890280SGreg Roach DB::table('block')->where('gedcom_id', '=', $this->id)->delete(); 594a7890280SGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 595a7890280SGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 596a7890280SGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete(); 597a7890280SGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete(); 598a7890280SGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete(); 599a7890280SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 600a7890280SGreg Roach DB::table('log')->where('gedcom_id', '=', $this->id)->delete(); 601a7890280SGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete(); 602a25f0a04SGreg Roach 603a25f0a04SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 60475a9f908SGreg Roach self::$trees = []; 605a25f0a04SGreg Roach } 606a25f0a04SGreg Roach 607a25f0a04SGreg Roach /** 608a25f0a04SGreg Roach * Export the tree to a GEDCOM file 609a25f0a04SGreg Roach * 6105792757eSGreg Roach * @param resource $stream 611b7e60af1SGreg Roach * 612b7e60af1SGreg Roach * @return void 613a25f0a04SGreg Roach */ 614c1010edaSGreg Roach public function exportGedcom($stream) 615c1010edaSGreg Roach { 616a3d8780cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8')); 61794026f20SGreg Roach 61894026f20SGreg Roach $union_families = DB::table('families') 61994026f20SGreg Roach ->where('f_file', '=', $this->id) 62094026f20SGreg Roach ->select(['f_gedcom AS gedcom', 'f_id AS xref', DB::raw('LENGTH(f_id) AS len'), DB::raw('2 AS n')]); 62194026f20SGreg Roach 62294026f20SGreg Roach $union_sources = DB::table('sources') 62394026f20SGreg Roach ->where('s_file', '=', $this->id) 62494026f20SGreg Roach ->select(['s_gedcom AS gedcom', 's_id AS xref', DB::raw('LENGTH(s_id) AS len'), DB::raw('3 AS n')]); 62594026f20SGreg Roach 62694026f20SGreg Roach $union_other = DB::table('other') 62794026f20SGreg Roach ->where('o_file', '=', $this->id) 62894026f20SGreg Roach ->whereNotIn('o_type', ['HEAD', 'TRLR']) 62994026f20SGreg Roach ->select(['o_gedcom AS gedcom', 'o_id AS xref', DB::raw('LENGTH(o_id) AS len'), DB::raw('4 AS n')]); 63094026f20SGreg Roach 63194026f20SGreg Roach $union_media = DB::table('media') 63294026f20SGreg Roach ->where('m_file', '=', $this->id) 63394026f20SGreg Roach ->select(['m_gedcom AS gedcom', 'm_id AS xref', DB::raw('LENGTH(m_id) AS len'), DB::raw('5 AS n')]); 63494026f20SGreg Roach 63594026f20SGreg Roach $rows = DB::table('individuals') 63694026f20SGreg Roach ->where('i_file', '=', $this->id) 63794026f20SGreg Roach ->select(['i_gedcom AS gedcom', 'i_id AS xref', DB::raw('LENGTH(i_id) AS len'), DB::raw('1 AS n')]) 63894026f20SGreg Roach ->union($union_families) 63994026f20SGreg Roach ->union($union_sources) 64094026f20SGreg Roach ->union($union_other) 64194026f20SGreg Roach ->union($union_media) 64294026f20SGreg Roach ->orderBy('n') 64394026f20SGreg Roach ->orderBy('len') 64494026f20SGreg Roach ->orderBy('xref') 64594026f20SGreg Roach ->chunk(100, function (Collection $rows) use ($stream, &$buffer): void { 64694026f20SGreg Roach foreach ($rows as $row) { 6473d7a8a4cSGreg Roach $buffer .= FunctionsExport::reformatRecord($row->gedcom); 648a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 6495792757eSGreg Roach fwrite($stream, $buffer); 650a25f0a04SGreg Roach $buffer = ''; 651a25f0a04SGreg Roach } 652a25f0a04SGreg Roach } 65394026f20SGreg Roach }); 65494026f20SGreg Roach 6550f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 656a25f0a04SGreg Roach } 657a25f0a04SGreg Roach 658a25f0a04SGreg Roach /** 659a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 660a25f0a04SGreg Roach * 661a25f0a04SGreg Roach * @param string $path The full path to the (possibly temporary) file. 662a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 663a25f0a04SGreg Roach * 664b7e60af1SGreg Roach * @return void 665a25f0a04SGreg Roach */ 666771ae10aSGreg Roach public function importGedcomFile(string $path, string $filename) 667c1010edaSGreg Roach { 668a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 669a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 670a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 671a25f0a04SGreg Roach // each block. 672a25f0a04SGreg Roach 673a25f0a04SGreg Roach $file_data = ''; 674a25f0a04SGreg Roach $fp = fopen($path, 'rb'); 675a25f0a04SGreg Roach 676b7e60af1SGreg Roach $this->deleteGenealogyData((bool) $this->getPreference('keep_media')); 677a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 678a25f0a04SGreg Roach $this->setPreference('imported', '0'); 679a25f0a04SGreg Roach 680a25f0a04SGreg Roach while (!feof($fp)) { 681a25f0a04SGreg Roach $file_data .= fread($fp, 65536); 682a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 683a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 684a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 685a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 686a25f0a04SGreg Roach break; 687a25f0a04SGreg Roach } 688a25f0a04SGreg Roach } 689a25f0a04SGreg Roach if ($pos) { 6901ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6911ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6921ad2dde6SGreg Roach 'chunk_data' => substr($file_data, 0, $pos), 693c1010edaSGreg Roach ]); 6941ad2dde6SGreg Roach 695a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 696a25f0a04SGreg Roach } 697a25f0a04SGreg Roach } 6981ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6991ad2dde6SGreg Roach 'gedcom_id' => $this->id, 7001ad2dde6SGreg Roach 'chunk_data' => $file_data, 701c1010edaSGreg Roach ]); 702a25f0a04SGreg Roach 703a25f0a04SGreg Roach fclose($fp); 704a25f0a04SGreg Roach } 705304f20d5SGreg Roach 706304f20d5SGreg Roach /** 707b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 708b90d8accSGreg Roach * 709b90d8accSGreg Roach * @return string 710b90d8accSGreg Roach */ 711771ae10aSGreg Roach public function getNewXref(): string 712c1010edaSGreg Roach { 713963fbaeeSGreg Roach // Lock the row, so that only one new XREF may be generated at a time. 714963fbaeeSGreg Roach DB::table('site_setting') 715963fbaeeSGreg Roach ->where('setting_name', '=', 'next_xref') 716963fbaeeSGreg Roach ->lockForUpdate() 717963fbaeeSGreg Roach ->get(); 718963fbaeeSGreg Roach 719a214e186SGreg Roach $prefix = 'X'; 720b90d8accSGreg Roach 721971d66c8SGreg Roach $increment = 1.0; 722b90d8accSGreg Roach do { 723963fbaeeSGreg Roach $num = (int) Site::getPreference('next_xref') + (int) $increment; 724971d66c8SGreg Roach 725971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 726971d66c8SGreg Roach // existing data in a reasonable time. 727971d66c8SGreg Roach $increment *= 1.01; 728963fbaeeSGreg Roach 729963fbaeeSGreg Roach $xref = $prefix . $num; 730963fbaeeSGreg Roach 731963fbaeeSGreg Roach // Records may already exist with this sequence number. 732963fbaeeSGreg Roach $already_used = 733963fbaeeSGreg Roach DB::table('individuals')->where('i_id', '=', $xref)->exists() || 734963fbaeeSGreg Roach DB::table('families')->where('f_id', '=', $xref)->exists() || 735963fbaeeSGreg Roach DB::table('sources')->where('s_id', '=', $xref)->exists() || 736963fbaeeSGreg Roach DB::table('media')->where('m_id', '=', $xref)->exists() || 737963fbaeeSGreg Roach DB::table('other')->where('o_id', '=', $xref)->exists() || 738963fbaeeSGreg Roach DB::table('change')->where('xref', '=', $xref)->exists(); 739963fbaeeSGreg Roach } while ($already_used); 740963fbaeeSGreg Roach 741963fbaeeSGreg Roach Site::setPreference('next_xref', (string) $num); 742b90d8accSGreg Roach 743a214e186SGreg Roach return $xref; 744b90d8accSGreg Roach } 745b90d8accSGreg Roach 746b90d8accSGreg Roach /** 747304f20d5SGreg Roach * Create a new record from GEDCOM data. 748304f20d5SGreg Roach * 749304f20d5SGreg Roach * @param string $gedcom 750304f20d5SGreg Roach * 75115d603e7SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 752afb591d7SGreg Roach * @throws InvalidArgumentException 753304f20d5SGreg Roach */ 754771ae10aSGreg Roach public function createRecord(string $gedcom): GedcomRecord 755c1010edaSGreg Roach { 756bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ ')) { 757afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@'); 758304f20d5SGreg Roach } 759304f20d5SGreg Roach 760a214e186SGreg Roach $xref = $this->getNewXref(); 761bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ '); 762304f20d5SGreg Roach 763afb591d7SGreg Roach // Create a change record 764304f20d5SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 765304f20d5SGreg Roach 766304f20d5SGreg Roach // Create a pending change 767963fbaeeSGreg Roach DB::table('change')->insert([ 768963fbaeeSGreg Roach 'gedcom_id' => $this->id, 769963fbaeeSGreg Roach 'xref' => $xref, 770963fbaeeSGreg Roach 'old_gedcom' => '', 771963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 772963fbaeeSGreg Roach 'user_id' => Auth::id(), 77313abd6f3SGreg Roach ]); 774304f20d5SGreg Roach 775afb591d7SGreg Roach // Accept this pending change 776afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 777afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 778afb591d7SGreg Roach 779afb591d7SGreg Roach return new GedcomRecord($xref, $gedcom, null, $this); 780afb591d7SGreg Roach } 781afb591d7SGreg Roach 782313e72b3SGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 783afb591d7SGreg Roach } 784afb591d7SGreg Roach 785afb591d7SGreg Roach /** 786afb591d7SGreg Roach * Create a new family from GEDCOM data. 787afb591d7SGreg Roach * 788afb591d7SGreg Roach * @param string $gedcom 789afb591d7SGreg Roach * 790afb591d7SGreg Roach * @return Family 791afb591d7SGreg Roach * @throws InvalidArgumentException 792afb591d7SGreg Roach */ 793afb591d7SGreg Roach public function createFamily(string $gedcom): GedcomRecord 794afb591d7SGreg Roach { 795bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ FAM')) { 796afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM'); 797afb591d7SGreg Roach } 798afb591d7SGreg Roach 799afb591d7SGreg Roach $xref = $this->getNewXref(); 800bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM'); 801afb591d7SGreg Roach 802afb591d7SGreg Roach // Create a change record 803afb591d7SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 804afb591d7SGreg Roach 805afb591d7SGreg Roach // Create a pending change 806963fbaeeSGreg Roach DB::table('change')->insert([ 807963fbaeeSGreg Roach 'gedcom_id' => $this->id, 808963fbaeeSGreg Roach 'xref' => $xref, 809963fbaeeSGreg Roach 'old_gedcom' => '', 810963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 811963fbaeeSGreg Roach 'user_id' => Auth::id(), 812afb591d7SGreg Roach ]); 813304f20d5SGreg Roach 814304f20d5SGreg Roach // Accept this pending change 815304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 816cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 817afb591d7SGreg Roach 818afb591d7SGreg Roach return new Family($xref, $gedcom, null, $this); 819304f20d5SGreg Roach } 820afb591d7SGreg Roach 821afb591d7SGreg Roach return new Family($xref, '', $gedcom, $this); 822afb591d7SGreg Roach } 823afb591d7SGreg Roach 824afb591d7SGreg Roach /** 825afb591d7SGreg Roach * Create a new individual from GEDCOM data. 826afb591d7SGreg Roach * 827afb591d7SGreg Roach * @param string $gedcom 828afb591d7SGreg Roach * 829afb591d7SGreg Roach * @return Individual 830afb591d7SGreg Roach * @throws InvalidArgumentException 831afb591d7SGreg Roach */ 832afb591d7SGreg Roach public function createIndividual(string $gedcom): GedcomRecord 833afb591d7SGreg Roach { 834bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ INDI')) { 835afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI'); 836afb591d7SGreg Roach } 837afb591d7SGreg Roach 838afb591d7SGreg Roach $xref = $this->getNewXref(); 839bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI'); 840afb591d7SGreg Roach 841afb591d7SGreg Roach // Create a change record 842afb591d7SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 843afb591d7SGreg Roach 844afb591d7SGreg Roach // Create a pending change 845963fbaeeSGreg Roach DB::table('change')->insert([ 846963fbaeeSGreg Roach 'gedcom_id' => $this->id, 847963fbaeeSGreg Roach 'xref' => $xref, 848963fbaeeSGreg Roach 'old_gedcom' => '', 849963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 850963fbaeeSGreg Roach 'user_id' => Auth::id(), 851afb591d7SGreg Roach ]); 852afb591d7SGreg Roach 853afb591d7SGreg Roach // Accept this pending change 854afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 855afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 856afb591d7SGreg Roach 857afb591d7SGreg Roach return new Individual($xref, $gedcom, null, $this); 858afb591d7SGreg Roach } 859afb591d7SGreg Roach 860afb591d7SGreg Roach return new Individual($xref, '', $gedcom, $this); 861304f20d5SGreg Roach } 8628586983fSGreg Roach 8638586983fSGreg Roach /** 86420b58d20SGreg Roach * Create a new media object from GEDCOM data. 86520b58d20SGreg Roach * 86620b58d20SGreg Roach * @param string $gedcom 86720b58d20SGreg Roach * 86820b58d20SGreg Roach * @return Media 86920b58d20SGreg Roach * @throws InvalidArgumentException 87020b58d20SGreg Roach */ 87120b58d20SGreg Roach public function createMediaObject(string $gedcom): Media 87220b58d20SGreg Roach { 873bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ OBJE')) { 87420b58d20SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE'); 87520b58d20SGreg Roach } 87620b58d20SGreg Roach 87720b58d20SGreg Roach $xref = $this->getNewXref(); 878bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE'); 87920b58d20SGreg Roach 88020b58d20SGreg Roach // Create a change record 88120b58d20SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 88220b58d20SGreg Roach 88320b58d20SGreg Roach // Create a pending change 884963fbaeeSGreg Roach DB::table('change')->insert([ 885963fbaeeSGreg Roach 'gedcom_id' => $this->id, 886963fbaeeSGreg Roach 'xref' => $xref, 887963fbaeeSGreg Roach 'old_gedcom' => '', 888963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 889963fbaeeSGreg Roach 'user_id' => Auth::id(), 89020b58d20SGreg Roach ]); 89120b58d20SGreg Roach 89220b58d20SGreg Roach // Accept this pending change 89320b58d20SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 89420b58d20SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 89520b58d20SGreg Roach 89620b58d20SGreg Roach return new Media($xref, $gedcom, null, $this); 89720b58d20SGreg Roach } 89820b58d20SGreg Roach 89920b58d20SGreg Roach return new Media($xref, '', $gedcom, $this); 90020b58d20SGreg Roach } 90120b58d20SGreg Roach 90220b58d20SGreg Roach /** 9038586983fSGreg Roach * What is the most significant individual in this tree. 9048586983fSGreg Roach * 9058586983fSGreg Roach * @param User $user 9068586983fSGreg Roach * 9078586983fSGreg Roach * @return Individual 9088586983fSGreg Roach */ 909c1010edaSGreg Roach public function significantIndividual(User $user): Individual 910c1010edaSGreg Roach { 9118f9b0fb2SGreg Roach $individual = null; 9128586983fSGreg Roach 9138f9b0fb2SGreg Roach if ($this->getUserPreference($user, 'rootid') !== '') { 9148586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 9158586983fSGreg Roach } 9168f9b0fb2SGreg Roach 9178f9b0fb2SGreg Roach if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') { 9188586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 9198586983fSGreg Roach } 9208f9b0fb2SGreg Roach 921bec87e94SGreg Roach if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') { 9228586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 9238586983fSGreg Roach } 9248f9b0fb2SGreg Roach if ($individual === null) { 9258f9b0fb2SGreg Roach $xref = (string) DB::table('individuals') 9268f9b0fb2SGreg Roach ->where('i_file', '=', $this->id()) 9278f9b0fb2SGreg Roach ->min('i_id'); 928769d7d6eSGreg Roach 929769d7d6eSGreg Roach $individual = Individual::getInstance($xref, $this); 9305fe1add5SGreg Roach } 9318f9b0fb2SGreg Roach if ($individual === null) { 9325fe1add5SGreg Roach // always return a record 9335fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 9345fe1add5SGreg Roach } 9355fe1add5SGreg Roach 9365fe1add5SGreg Roach return $individual; 9375fe1add5SGreg Roach } 938a25f0a04SGreg Roach} 939