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 20e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 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; 2694026f20SGreg Roachuse Illuminate\Support\Collection; 27bec87e94SGreg Roachuse Illuminate\Support\Str; 28afb591d7SGreg Roachuse InvalidArgumentException; 29a25f0a04SGreg Roachuse PDOException; 306ccdf4f0SGreg Roachuse Psr\Http\Message\StreamInterface; 318b67c11aSGreg Roachuse stdClass; 32a25f0a04SGreg Roach 33a25f0a04SGreg Roach/** 3476692c8bSGreg Roach * Provide an interface to the wt_gedcom table. 35a25f0a04SGreg Roach */ 36c1010edaSGreg Roachclass Tree 37c1010edaSGreg Roach{ 38061b43d7SGreg Roach private const RESN_PRIVACY = [ 39061b43d7SGreg Roach 'none' => Auth::PRIV_PRIVATE, 40061b43d7SGreg Roach 'privacy' => Auth::PRIV_USER, 41061b43d7SGreg Roach 'confidential' => Auth::PRIV_NONE, 42061b43d7SGreg Roach 'hidden' => Auth::PRIV_HIDE, 43061b43d7SGreg Roach ]; 446ccdf4f0SGreg Roach /** @var Tree[] All trees that we have permission to see, indexed by ID. */ 456ccdf4f0SGreg Roach public static $trees = []; 466ccdf4f0SGreg Roach /** @var int The tree's ID number */ 476ccdf4f0SGreg Roach private $id; 486ccdf4f0SGreg Roach /** @var string The tree's name */ 496ccdf4f0SGreg Roach private $name; 506ccdf4f0SGreg Roach /** @var string The tree's title */ 516ccdf4f0SGreg Roach private $title; 526ccdf4f0SGreg Roach /** @var int[] Default access rules for facts in this tree */ 536ccdf4f0SGreg Roach private $fact_privacy; 546ccdf4f0SGreg Roach /** @var int[] Default access rules for individuals in this tree */ 556ccdf4f0SGreg Roach private $individual_privacy; 566ccdf4f0SGreg Roach /** @var integer[][] Default access rules for individual facts in this tree */ 576ccdf4f0SGreg Roach private $individual_fact_privacy; 586ccdf4f0SGreg Roach /** @var string[] Cached copy of the wt_gedcom_setting table. */ 596ccdf4f0SGreg Roach private $preferences = []; 606ccdf4f0SGreg Roach /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ 616ccdf4f0SGreg Roach private $user_preferences = []; 62061b43d7SGreg Roach 63a25f0a04SGreg Roach /** 64a25f0a04SGreg Roach * Create a tree object. This is a private constructor - it can only 65a25f0a04SGreg Roach * be called from Tree::getAll() to ensure proper initialisation. 66a25f0a04SGreg Roach * 6772cf66d4SGreg Roach * @param int $id 68aa6f03bbSGreg Roach * @param string $name 69cc13d6d8SGreg Roach * @param string $title 70a25f0a04SGreg Roach */ 71cc13d6d8SGreg Roach private function __construct($id, $name, $title) 72c1010edaSGreg Roach { 7372cf66d4SGreg Roach $this->id = $id; 74aa6f03bbSGreg Roach $this->name = $name; 75cc13d6d8SGreg Roach $this->title = $title; 7613abd6f3SGreg Roach $this->fact_privacy = []; 7713abd6f3SGreg Roach $this->individual_privacy = []; 7813abd6f3SGreg Roach $this->individual_fact_privacy = []; 79518bbdc1SGreg Roach 80518bbdc1SGreg Roach // Load the privacy settings for this tree 81061b43d7SGreg Roach $rows = DB::table('default_resn') 82061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 83061b43d7SGreg Roach ->get(); 84518bbdc1SGreg Roach 85518bbdc1SGreg Roach foreach ($rows as $row) { 86061b43d7SGreg Roach // Convert GEDCOM privacy restriction to a webtrees access level. 87061b43d7SGreg Roach $row->resn = self::RESN_PRIVACY[$row->resn]; 88061b43d7SGreg Roach 89518bbdc1SGreg Roach if ($row->xref !== null) { 90518bbdc1SGreg Roach if ($row->tag_type !== null) { 91518bbdc1SGreg Roach $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn; 92518bbdc1SGreg Roach } else { 93518bbdc1SGreg Roach $this->individual_privacy[$row->xref] = (int) $row->resn; 94518bbdc1SGreg Roach } 95518bbdc1SGreg Roach } else { 96518bbdc1SGreg Roach $this->fact_privacy[$row->tag_type] = (int) $row->resn; 97518bbdc1SGreg Roach } 98518bbdc1SGreg Roach } 99a25f0a04SGreg Roach } 100a25f0a04SGreg Roach 101a25f0a04SGreg Roach /** 1026ccdf4f0SGreg Roach * Find the tree with a specific ID. 103a25f0a04SGreg Roach * 1046ccdf4f0SGreg Roach * @param int $tree_id 1056ccdf4f0SGreg Roach * 1066ccdf4f0SGreg Roach * @return Tree 107a25f0a04SGreg Roach */ 1086ccdf4f0SGreg Roach public static function findById(int $tree_id): Tree 109c1010edaSGreg Roach { 1106ccdf4f0SGreg Roach return self::getAll()[$tree_id]; 111a25f0a04SGreg Roach } 112a25f0a04SGreg Roach 113a25f0a04SGreg Roach /** 1146ccdf4f0SGreg Roach * Fetch all the trees that we have permission to access. 115a25f0a04SGreg Roach * 1166ccdf4f0SGreg Roach * @return Tree[] 117a25f0a04SGreg Roach */ 1186ccdf4f0SGreg Roach public static function getAll(): array 119c1010edaSGreg Roach { 1206ccdf4f0SGreg Roach if (empty(self::$trees)) { 1216ccdf4f0SGreg Roach self::$trees = self::all()->all(); 122a25f0a04SGreg Roach } 123a25f0a04SGreg Roach 1246ccdf4f0SGreg Roach return self::$trees; 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach 127a25f0a04SGreg Roach /** 1288b67c11aSGreg Roach * All the trees that we have permission to access. 129a25f0a04SGreg Roach * 13054c7f8dfSGreg Roach * @return Collection 13154c7f8dfSGreg Roach * @return Tree[] 132a25f0a04SGreg Roach */ 1338b67c11aSGreg Roach public static function all(): Collection 134c1010edaSGreg Roach { 1350b5fd0a6SGreg Roach return app('cache.array')->rememberForever(__CLASS__, static function (): Collection { 13601461f86SGreg Roach // Admins see all trees 13701461f86SGreg Roach $query = DB::table('gedcom') 1380b5fd0a6SGreg Roach ->leftJoin('gedcom_setting', static function (JoinClause $join): void { 13901461f86SGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 14001461f86SGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 14101461f86SGreg Roach }) 14201461f86SGreg Roach ->where('gedcom.gedcom_id', '>', 0) 14301461f86SGreg Roach ->select([ 14401461f86SGreg Roach 'gedcom.gedcom_id AS tree_id', 14501461f86SGreg Roach 'gedcom.gedcom_name AS tree_name', 14601461f86SGreg Roach 'gedcom_setting.setting_value AS tree_title', 14701461f86SGreg Roach ]) 14801461f86SGreg Roach ->orderBy('gedcom.sort_order') 14901461f86SGreg Roach ->orderBy('gedcom_setting.setting_value'); 15001461f86SGreg Roach 15132f20c14SGreg Roach // Non-admins may not see all trees 15232f20c14SGreg Roach if (!Auth::isAdmin()) { 15301461f86SGreg Roach $query 1540b5fd0a6SGreg Roach ->join('gedcom_setting AS gs2', static function (JoinClause $join): void { 15536357577SGreg Roach $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 15601461f86SGreg Roach ->where('gs2.setting_name', '=', 'imported'); 15736357577SGreg Roach }) 1580b5fd0a6SGreg Roach ->join('gedcom_setting AS gs3', static function (JoinClause $join): void { 15901461f86SGreg Roach $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 16001461f86SGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 16101461f86SGreg Roach }) 1620b5fd0a6SGreg Roach ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void { 16301461f86SGreg Roach $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 16401461f86SGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 16501461f86SGreg Roach ->where('user_gedcom_setting.setting_name', '=', 'canedit'); 16601461f86SGreg Roach }) 1670b5fd0a6SGreg Roach ->where(static function (Builder $query): void { 16801461f86SGreg Roach $query 16901461f86SGreg Roach // Managers 17001461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '=', 'admin') 17101461f86SGreg Roach // Members 1720b5fd0a6SGreg Roach ->orWhere(static function (Builder $query): void { 17301461f86SGreg Roach $query 17401461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 17501461f86SGreg Roach ->where('gs3.setting_value', '=', '1') 17601461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '<>', 'none'); 17701461f86SGreg Roach }) 1788b67c11aSGreg Roach // Public trees 1790b5fd0a6SGreg Roach ->orWhere(static function (Builder $query): void { 18001461f86SGreg Roach $query 18101461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 18236357577SGreg Roach ->where('gs3.setting_value', '<>', '1'); 18301461f86SGreg Roach }); 18401461f86SGreg Roach }); 18501461f86SGreg Roach } 18601461f86SGreg Roach 1878b67c11aSGreg Roach return $query 1888b67c11aSGreg Roach ->get() 1890b5fd0a6SGreg Roach ->mapWithKeys(static function (stdClass $row): array { 1908b67c11aSGreg Roach return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)]; 1918b67c11aSGreg Roach }); 1928b67c11aSGreg Roach }); 193a25f0a04SGreg Roach } 1948b67c11aSGreg Roach 1958b67c11aSGreg Roach /** 196a25f0a04SGreg Roach * Create arguments to select_edit_control() 197a25f0a04SGreg Roach * Note - these will be escaped later 198a25f0a04SGreg Roach * 199a25f0a04SGreg Roach * @return string[] 200a25f0a04SGreg Roach */ 201771ae10aSGreg Roach public static function getIdList(): array 202c1010edaSGreg Roach { 20313abd6f3SGreg Roach $list = []; 204a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 20572cf66d4SGreg Roach $list[$tree->id] = $tree->title; 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach 208a25f0a04SGreg Roach return $list; 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach /** 212a25f0a04SGreg Roach * Create arguments to select_edit_control() 213a25f0a04SGreg Roach * Note - these will be escaped later 214a25f0a04SGreg Roach * 215a25f0a04SGreg Roach * @return string[] 216a25f0a04SGreg Roach */ 217771ae10aSGreg Roach public static function getNameList(): array 218c1010edaSGreg Roach { 21913abd6f3SGreg Roach $list = []; 220a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 221a25f0a04SGreg Roach $list[$tree->name] = $tree->title; 222a25f0a04SGreg Roach } 223a25f0a04SGreg Roach 224a25f0a04SGreg Roach return $list; 225a25f0a04SGreg Roach } 226a25f0a04SGreg Roach 227a25f0a04SGreg Roach /** 228a25f0a04SGreg Roach * Create a new tree 229a25f0a04SGreg Roach * 230a25f0a04SGreg Roach * @param string $tree_name 231a25f0a04SGreg Roach * @param string $tree_title 232a25f0a04SGreg Roach * 233a25f0a04SGreg Roach * @return Tree 234a25f0a04SGreg Roach */ 235771ae10aSGreg Roach public static function create(string $tree_name, string $tree_title): Tree 236c1010edaSGreg Roach { 237a25f0a04SGreg Roach try { 238a25f0a04SGreg Roach // Create a new tree 23901461f86SGreg Roach DB::table('gedcom')->insert([ 24001461f86SGreg Roach 'gedcom_name' => $tree_name, 24101461f86SGreg Roach ]); 2424a86d714SGreg Roach 243061b43d7SGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 24432f20c14SGreg Roach 24532f20c14SGreg Roach $tree = new self($tree_id, $tree_name, $tree_title); 246a25f0a04SGreg Roach } catch (PDOException $ex) { 247a25f0a04SGreg Roach // A tree with that name already exists? 248ef2fd529SGreg Roach return self::findByName($tree_name); 249a25f0a04SGreg Roach } 250a25f0a04SGreg Roach 251a25f0a04SGreg Roach $tree->setPreference('imported', '0'); 252a25f0a04SGreg Roach $tree->setPreference('title', $tree_title); 253a25f0a04SGreg Roach 2541507cbcaSGreg Roach // Set preferences from default tree 255061b43d7SGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 256061b43d7SGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 2576ccdf4f0SGreg Roach static function (Builder $query) use ($tree_id): void { 258061b43d7SGreg Roach $query 259061b43d7SGreg Roach ->select([DB::raw($tree_id), 'setting_name', 'setting_value']) 260061b43d7SGreg Roach ->from('gedcom_setting') 261061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 262061b43d7SGreg Roach } 263061b43d7SGreg Roach ); 2641507cbcaSGreg Roach 265061b43d7SGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 266061b43d7SGreg Roach ['gedcom_id', 'tag_type', 'resn'], 267*6c2179e2SGreg Roach static function (Builder $query) use ($tree_id): void { 268061b43d7SGreg Roach $query 269061b43d7SGreg Roach ->select([DB::raw($tree_id), 'tag_type', 'resn']) 270061b43d7SGreg Roach ->from('default_resn') 271061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 272061b43d7SGreg Roach } 273061b43d7SGreg Roach ); 2741507cbcaSGreg Roach 275a25f0a04SGreg Roach // Gedcom and privacy settings 27676f666f4SGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 27776f666f4SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 278a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 279e364afe4SGreg Roach 280a25f0a04SGreg Roach switch (WT_LOCALE) { 281a25f0a04SGreg Roach case 'es': 282a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 283a25f0a04SGreg Roach break; 284a25f0a04SGreg Roach case 'is': 285a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 286a25f0a04SGreg Roach break; 287a25f0a04SGreg Roach case 'lt': 288a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 289a25f0a04SGreg Roach break; 290a25f0a04SGreg Roach case 'pl': 291a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 292a25f0a04SGreg Roach break; 293a25f0a04SGreg Roach case 'pt': 294a25f0a04SGreg Roach case 'pt-BR': 295a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 296a25f0a04SGreg Roach break; 297a25f0a04SGreg Roach default: 298a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 299a25f0a04SGreg Roach break; 300a25f0a04SGreg Roach } 301a25f0a04SGreg Roach 302a25f0a04SGreg Roach // Genealogy data 303a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 304bbb76c12SGreg Roach /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 305bbb76c12SGreg Roach $john_doe = I18N::translate('John /DOE/'); 30677e70a22SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 307061b43d7SGreg 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"; 308061b43d7SGreg Roach 309061b43d7SGreg Roach DB::table('gedcom_chunk')->insert([ 310061b43d7SGreg Roach 'gedcom_id' => $tree_id, 311061b43d7SGreg Roach 'chunk_data' => $gedcom, 31213abd6f3SGreg Roach ]); 313a25f0a04SGreg Roach 314a25f0a04SGreg Roach // Update our cache 31572cf66d4SGreg Roach self::$trees[$tree->id] = $tree; 316a25f0a04SGreg Roach 317a25f0a04SGreg Roach return $tree; 318a25f0a04SGreg Roach } 319a25f0a04SGreg Roach 320a25f0a04SGreg Roach /** 3216ccdf4f0SGreg Roach * Find the tree with a specific name. 3226ccdf4f0SGreg Roach * 3236ccdf4f0SGreg Roach * @param string $tree_name 3246ccdf4f0SGreg Roach * 3256ccdf4f0SGreg Roach * @return Tree|null 3266ccdf4f0SGreg Roach */ 3276ccdf4f0SGreg Roach public static function findByName($tree_name): ?Tree 3286ccdf4f0SGreg Roach { 3296ccdf4f0SGreg Roach foreach (self::getAll() as $tree) { 3306ccdf4f0SGreg Roach if ($tree->name === $tree_name) { 3316ccdf4f0SGreg Roach return $tree; 3326ccdf4f0SGreg Roach } 3336ccdf4f0SGreg Roach } 3346ccdf4f0SGreg Roach 3356ccdf4f0SGreg Roach return null; 3366ccdf4f0SGreg Roach } 3376ccdf4f0SGreg Roach 3386ccdf4f0SGreg Roach /** 3396ccdf4f0SGreg Roach * Set the tree’s configuration settings. 3406ccdf4f0SGreg Roach * 3416ccdf4f0SGreg Roach * @param string $setting_name 3426ccdf4f0SGreg Roach * @param string $setting_value 3436ccdf4f0SGreg Roach * 3446ccdf4f0SGreg Roach * @return $this 3456ccdf4f0SGreg Roach */ 3466ccdf4f0SGreg Roach public function setPreference(string $setting_name, string $setting_value): Tree 3476ccdf4f0SGreg Roach { 3486ccdf4f0SGreg Roach if ($setting_value !== $this->getPreference($setting_name)) { 3496ccdf4f0SGreg Roach DB::table('gedcom_setting')->updateOrInsert([ 3506ccdf4f0SGreg Roach 'gedcom_id' => $this->id, 3516ccdf4f0SGreg Roach 'setting_name' => $setting_name, 3526ccdf4f0SGreg Roach ], [ 3536ccdf4f0SGreg Roach 'setting_value' => $setting_value, 3546ccdf4f0SGreg Roach ]); 3556ccdf4f0SGreg Roach 3566ccdf4f0SGreg Roach $this->preferences[$setting_name] = $setting_value; 3576ccdf4f0SGreg Roach 3586ccdf4f0SGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); 3596ccdf4f0SGreg Roach } 3606ccdf4f0SGreg Roach 3616ccdf4f0SGreg Roach return $this; 3626ccdf4f0SGreg Roach } 3636ccdf4f0SGreg Roach 3646ccdf4f0SGreg Roach /** 3656ccdf4f0SGreg Roach * Get the tree’s configuration settings. 3666ccdf4f0SGreg Roach * 3676ccdf4f0SGreg Roach * @param string $setting_name 3686ccdf4f0SGreg Roach * @param string $default 3696ccdf4f0SGreg Roach * 3706ccdf4f0SGreg Roach * @return string 3716ccdf4f0SGreg Roach */ 3726ccdf4f0SGreg Roach public function getPreference(string $setting_name, string $default = ''): string 3736ccdf4f0SGreg Roach { 3746ccdf4f0SGreg Roach if (empty($this->preferences)) { 3756ccdf4f0SGreg Roach $this->preferences = DB::table('gedcom_setting') 3766ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 3776ccdf4f0SGreg Roach ->pluck('setting_value', 'setting_name') 3786ccdf4f0SGreg Roach ->all(); 3796ccdf4f0SGreg Roach } 3806ccdf4f0SGreg Roach 3816ccdf4f0SGreg Roach return $this->preferences[$setting_name] ?? $default; 3826ccdf4f0SGreg Roach } 3836ccdf4f0SGreg Roach 3846ccdf4f0SGreg Roach /** 3856ccdf4f0SGreg Roach * The name of this tree 3866ccdf4f0SGreg Roach * 3876ccdf4f0SGreg Roach * @return string 3886ccdf4f0SGreg Roach */ 3896ccdf4f0SGreg Roach public function name(): string 3906ccdf4f0SGreg Roach { 3916ccdf4f0SGreg Roach return $this->name; 3926ccdf4f0SGreg Roach } 3936ccdf4f0SGreg Roach 3946ccdf4f0SGreg Roach /** 3956ccdf4f0SGreg Roach * The title of this tree 3966ccdf4f0SGreg Roach * 3976ccdf4f0SGreg Roach * @return string 3986ccdf4f0SGreg Roach */ 3996ccdf4f0SGreg Roach public function title(): string 4006ccdf4f0SGreg Roach { 4016ccdf4f0SGreg Roach return $this->title; 4026ccdf4f0SGreg Roach } 4036ccdf4f0SGreg Roach 4046ccdf4f0SGreg Roach /** 4056ccdf4f0SGreg Roach * The fact-level privacy for this tree. 4066ccdf4f0SGreg Roach * 4076ccdf4f0SGreg Roach * @return int[] 4086ccdf4f0SGreg Roach */ 4096ccdf4f0SGreg Roach public function getFactPrivacy(): array 4106ccdf4f0SGreg Roach { 4116ccdf4f0SGreg Roach return $this->fact_privacy; 4126ccdf4f0SGreg Roach } 4136ccdf4f0SGreg Roach 4146ccdf4f0SGreg Roach /** 4156ccdf4f0SGreg Roach * The individual-level privacy for this tree. 4166ccdf4f0SGreg Roach * 4176ccdf4f0SGreg Roach * @return int[] 4186ccdf4f0SGreg Roach */ 4196ccdf4f0SGreg Roach public function getIndividualPrivacy(): array 4206ccdf4f0SGreg Roach { 4216ccdf4f0SGreg Roach return $this->individual_privacy; 4226ccdf4f0SGreg Roach } 4236ccdf4f0SGreg Roach 4246ccdf4f0SGreg Roach /** 4256ccdf4f0SGreg Roach * The individual-fact-level privacy for this tree. 4266ccdf4f0SGreg Roach * 4276ccdf4f0SGreg Roach * @return int[][] 4286ccdf4f0SGreg Roach */ 4296ccdf4f0SGreg Roach public function getIndividualFactPrivacy(): array 4306ccdf4f0SGreg Roach { 4316ccdf4f0SGreg Roach return $this->individual_fact_privacy; 4326ccdf4f0SGreg Roach } 4336ccdf4f0SGreg Roach 4346ccdf4f0SGreg Roach /** 4356ccdf4f0SGreg Roach * Set the tree’s user-configuration settings. 4366ccdf4f0SGreg Roach * 4376ccdf4f0SGreg Roach * @param UserInterface $user 4386ccdf4f0SGreg Roach * @param string $setting_name 4396ccdf4f0SGreg Roach * @param string $setting_value 4406ccdf4f0SGreg Roach * 4416ccdf4f0SGreg Roach * @return $this 4426ccdf4f0SGreg Roach */ 4436ccdf4f0SGreg Roach public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree 4446ccdf4f0SGreg Roach { 4456ccdf4f0SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 4466ccdf4f0SGreg Roach // Update the database 4476ccdf4f0SGreg Roach DB::table('user_gedcom_setting')->updateOrInsert([ 4486ccdf4f0SGreg Roach 'gedcom_id' => $this->id(), 4496ccdf4f0SGreg Roach 'user_id' => $user->id(), 4506ccdf4f0SGreg Roach 'setting_name' => $setting_name, 4516ccdf4f0SGreg Roach ], [ 4526ccdf4f0SGreg Roach 'setting_value' => $setting_value, 4536ccdf4f0SGreg Roach ]); 4546ccdf4f0SGreg Roach 4556ccdf4f0SGreg Roach // Update the cache 4566ccdf4f0SGreg Roach $this->user_preferences[$user->id()][$setting_name] = $setting_value; 4576ccdf4f0SGreg Roach // Audit log of changes 4586ccdf4f0SGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this); 4596ccdf4f0SGreg Roach } 4606ccdf4f0SGreg Roach 4616ccdf4f0SGreg Roach return $this; 4626ccdf4f0SGreg Roach } 4636ccdf4f0SGreg Roach 4646ccdf4f0SGreg Roach /** 4656ccdf4f0SGreg Roach * Get the tree’s user-configuration settings. 4666ccdf4f0SGreg Roach * 4676ccdf4f0SGreg Roach * @param UserInterface $user 4686ccdf4f0SGreg Roach * @param string $setting_name 4696ccdf4f0SGreg Roach * @param string $default 4706ccdf4f0SGreg Roach * 4716ccdf4f0SGreg Roach * @return string 4726ccdf4f0SGreg Roach */ 4736ccdf4f0SGreg Roach public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string 4746ccdf4f0SGreg Roach { 4756ccdf4f0SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 4766ccdf4f0SGreg Roach // so it is quicker to fetch them all in one go. 4776ccdf4f0SGreg Roach if (!array_key_exists($user->id(), $this->user_preferences)) { 4786ccdf4f0SGreg Roach $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting') 4796ccdf4f0SGreg Roach ->where('user_id', '=', $user->id()) 4806ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 4816ccdf4f0SGreg Roach ->pluck('setting_value', 'setting_name') 4826ccdf4f0SGreg Roach ->all(); 4836ccdf4f0SGreg Roach } 4846ccdf4f0SGreg Roach 4856ccdf4f0SGreg Roach return $this->user_preferences[$user->id()][$setting_name] ?? $default; 4866ccdf4f0SGreg Roach } 4876ccdf4f0SGreg Roach 4886ccdf4f0SGreg Roach /** 4896ccdf4f0SGreg Roach * The ID of this tree 4906ccdf4f0SGreg Roach * 4916ccdf4f0SGreg Roach * @return int 4926ccdf4f0SGreg Roach */ 4936ccdf4f0SGreg Roach public function id(): int 4946ccdf4f0SGreg Roach { 4956ccdf4f0SGreg Roach return $this->id; 4966ccdf4f0SGreg Roach } 4976ccdf4f0SGreg Roach 4986ccdf4f0SGreg Roach /** 4996ccdf4f0SGreg Roach * Can a user accept changes for this tree? 5006ccdf4f0SGreg Roach * 5016ccdf4f0SGreg Roach * @param UserInterface $user 5026ccdf4f0SGreg Roach * 5036ccdf4f0SGreg Roach * @return bool 5046ccdf4f0SGreg Roach */ 5056ccdf4f0SGreg Roach public function canAcceptChanges(UserInterface $user): bool 5066ccdf4f0SGreg Roach { 5076ccdf4f0SGreg Roach return Auth::isModerator($this, $user); 5086ccdf4f0SGreg Roach } 5096ccdf4f0SGreg Roach 5106ccdf4f0SGreg Roach /** 511b78374c5SGreg Roach * Are there any pending edits for this tree, than need reviewing by a moderator. 512b78374c5SGreg Roach * 513b78374c5SGreg Roach * @return bool 514b78374c5SGreg Roach */ 515771ae10aSGreg Roach public function hasPendingEdit(): bool 516c1010edaSGreg Roach { 51715a3f100SGreg Roach return DB::table('change') 51815a3f100SGreg Roach ->where('gedcom_id', '=', $this->id) 51915a3f100SGreg Roach ->where('status', '=', 'pending') 52015a3f100SGreg Roach ->exists(); 521b78374c5SGreg Roach } 522b78374c5SGreg Roach 523b78374c5SGreg Roach /** 5246ccdf4f0SGreg Roach * Delete everything relating to a tree 5256ccdf4f0SGreg Roach * 5266ccdf4f0SGreg Roach * @return void 5276ccdf4f0SGreg Roach */ 5286ccdf4f0SGreg Roach public function delete(): void 5296ccdf4f0SGreg Roach { 5306ccdf4f0SGreg Roach // If this is the default tree, then unset it 5316ccdf4f0SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 5326ccdf4f0SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 5336ccdf4f0SGreg Roach } 5346ccdf4f0SGreg Roach 5356ccdf4f0SGreg Roach $this->deleteGenealogyData(false); 5366ccdf4f0SGreg Roach 5376ccdf4f0SGreg Roach DB::table('block_setting') 5386ccdf4f0SGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 5396ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 5406ccdf4f0SGreg Roach ->delete(); 5416ccdf4f0SGreg Roach DB::table('block')->where('gedcom_id', '=', $this->id)->delete(); 5426ccdf4f0SGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 5436ccdf4f0SGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 5446ccdf4f0SGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete(); 5456ccdf4f0SGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete(); 5466ccdf4f0SGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete(); 5476ccdf4f0SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5486ccdf4f0SGreg Roach DB::table('log')->where('gedcom_id', '=', $this->id)->delete(); 5496ccdf4f0SGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete(); 5506ccdf4f0SGreg Roach 5516ccdf4f0SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 5526ccdf4f0SGreg Roach self::$trees = []; 5536ccdf4f0SGreg Roach } 5546ccdf4f0SGreg Roach 5556ccdf4f0SGreg Roach /** 556a25f0a04SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 557a25f0a04SGreg Roach * new data. Optionally retain the media data, for when the user has been 558a25f0a04SGreg Roach * editing their data offline using an application which deletes (or does not 559a25f0a04SGreg Roach * support) media data. 560a25f0a04SGreg Roach * 561a25f0a04SGreg Roach * @param bool $keep_media 562b7e60af1SGreg Roach * 563b7e60af1SGreg Roach * @return void 564a25f0a04SGreg Roach */ 565e364afe4SGreg Roach public function deleteGenealogyData(bool $keep_media): void 566c1010edaSGreg Roach { 5671ad2dde6SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5681ad2dde6SGreg Roach DB::table('individuals')->where('i_file', '=', $this->id)->delete(); 5691ad2dde6SGreg Roach DB::table('families')->where('f_file', '=', $this->id)->delete(); 5701ad2dde6SGreg Roach DB::table('sources')->where('s_file', '=', $this->id)->delete(); 5711ad2dde6SGreg Roach DB::table('other')->where('o_file', '=', $this->id)->delete(); 5721ad2dde6SGreg Roach DB::table('places')->where('p_file', '=', $this->id)->delete(); 5731ad2dde6SGreg Roach DB::table('placelinks')->where('pl_file', '=', $this->id)->delete(); 5741ad2dde6SGreg Roach DB::table('name')->where('n_file', '=', $this->id)->delete(); 5751ad2dde6SGreg Roach DB::table('dates')->where('d_file', '=', $this->id)->delete(); 5761ad2dde6SGreg Roach DB::table('change')->where('gedcom_id', '=', $this->id)->delete(); 577a25f0a04SGreg Roach 578a25f0a04SGreg Roach if ($keep_media) { 5791ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id) 5801ad2dde6SGreg Roach ->where('l_type', '<>', 'OBJE') 5811ad2dde6SGreg Roach ->delete(); 582a25f0a04SGreg Roach } else { 5831ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id)->delete(); 5841ad2dde6SGreg Roach DB::table('media_file')->where('m_file', '=', $this->id)->delete(); 5851ad2dde6SGreg Roach DB::table('media')->where('m_file', '=', $this->id)->delete(); 586a25f0a04SGreg Roach } 587a25f0a04SGreg Roach } 588a25f0a04SGreg Roach 589a25f0a04SGreg Roach /** 590a25f0a04SGreg Roach * Export the tree to a GEDCOM file 591a25f0a04SGreg Roach * 5925792757eSGreg Roach * @param resource $stream 593b7e60af1SGreg Roach * 594b7e60af1SGreg Roach * @return void 595a25f0a04SGreg Roach */ 596425af8b9SGreg Roach public function exportGedcom($stream): void 597c1010edaSGreg Roach { 598a3d8780cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8')); 59994026f20SGreg Roach 60094026f20SGreg Roach $union_families = DB::table('families') 60194026f20SGreg Roach ->where('f_file', '=', $this->id) 60294026f20SGreg Roach ->select(['f_gedcom AS gedcom', 'f_id AS xref', DB::raw('LENGTH(f_id) AS len'), DB::raw('2 AS n')]); 60394026f20SGreg Roach 60494026f20SGreg Roach $union_sources = DB::table('sources') 60594026f20SGreg Roach ->where('s_file', '=', $this->id) 60694026f20SGreg Roach ->select(['s_gedcom AS gedcom', 's_id AS xref', DB::raw('LENGTH(s_id) AS len'), DB::raw('3 AS n')]); 60794026f20SGreg Roach 60894026f20SGreg Roach $union_other = DB::table('other') 60994026f20SGreg Roach ->where('o_file', '=', $this->id) 61094026f20SGreg Roach ->whereNotIn('o_type', ['HEAD', 'TRLR']) 61194026f20SGreg Roach ->select(['o_gedcom AS gedcom', 'o_id AS xref', DB::raw('LENGTH(o_id) AS len'), DB::raw('4 AS n')]); 61294026f20SGreg Roach 61394026f20SGreg Roach $union_media = DB::table('media') 61494026f20SGreg Roach ->where('m_file', '=', $this->id) 61594026f20SGreg Roach ->select(['m_gedcom AS gedcom', 'm_id AS xref', DB::raw('LENGTH(m_id) AS len'), DB::raw('5 AS n')]); 61694026f20SGreg Roach 617e5a6b4d4SGreg Roach DB::table('individuals') 61894026f20SGreg Roach ->where('i_file', '=', $this->id) 61994026f20SGreg Roach ->select(['i_gedcom AS gedcom', 'i_id AS xref', DB::raw('LENGTH(i_id) AS len'), DB::raw('1 AS n')]) 62094026f20SGreg Roach ->union($union_families) 62194026f20SGreg Roach ->union($union_sources) 62294026f20SGreg Roach ->union($union_other) 62394026f20SGreg Roach ->union($union_media) 62494026f20SGreg Roach ->orderBy('n') 62594026f20SGreg Roach ->orderBy('len') 62694026f20SGreg Roach ->orderBy('xref') 6270b5fd0a6SGreg Roach ->chunk(100, static function (Collection $rows) use ($stream, &$buffer): void { 62894026f20SGreg Roach foreach ($rows as $row) { 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 } 63594026f20SGreg Roach }); 63694026f20SGreg Roach 6370f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 638a25f0a04SGreg Roach } 639a25f0a04SGreg Roach 640a25f0a04SGreg Roach /** 641a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 642a25f0a04SGreg Roach * 6436ccdf4f0SGreg Roach * @param StreamInterface $stream The GEDCOM file. 644a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 645a25f0a04SGreg Roach * 646b7e60af1SGreg Roach * @return void 647a25f0a04SGreg Roach */ 6486ccdf4f0SGreg Roach public function importGedcomFile(StreamInterface $stream, string $filename): void 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 657b7e60af1SGreg Roach $this->deleteGenealogyData((bool) $this->getPreference('keep_media')); 658a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 659a25f0a04SGreg Roach $this->setPreference('imported', '0'); 660a25f0a04SGreg Roach 6616ccdf4f0SGreg Roach while (!$stream->eof()) { 6626ccdf4f0SGreg Roach $file_data .= $stream->read(65536); 663a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 664a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 665a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 666a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 667a25f0a04SGreg Roach break; 668a25f0a04SGreg Roach } 669a25f0a04SGreg Roach } 670a25f0a04SGreg Roach if ($pos) { 6711ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6721ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6731ad2dde6SGreg Roach 'chunk_data' => substr($file_data, 0, $pos), 674c1010edaSGreg Roach ]); 6751ad2dde6SGreg Roach 676a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 677a25f0a04SGreg Roach } 678a25f0a04SGreg Roach } 6791ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6801ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6811ad2dde6SGreg Roach 'chunk_data' => $file_data, 682c1010edaSGreg Roach ]); 683a25f0a04SGreg Roach 6846ccdf4f0SGreg Roach $stream->close(); 6856ccdf4f0SGreg Roach } 6866ccdf4f0SGreg Roach 6876ccdf4f0SGreg Roach /** 6886ccdf4f0SGreg Roach * Create a new record from GEDCOM data. 6896ccdf4f0SGreg Roach * 6906ccdf4f0SGreg Roach * @param string $gedcom 6916ccdf4f0SGreg Roach * 6926ccdf4f0SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 6936ccdf4f0SGreg Roach * @throws InvalidArgumentException 6946ccdf4f0SGreg Roach */ 6956ccdf4f0SGreg Roach public function createRecord(string $gedcom): GedcomRecord 6966ccdf4f0SGreg Roach { 6976ccdf4f0SGreg Roach if (!Str::startsWith($gedcom, '0 @@ ')) { 6986ccdf4f0SGreg Roach throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@'); 6996ccdf4f0SGreg Roach } 7006ccdf4f0SGreg Roach 7016ccdf4f0SGreg Roach $xref = $this->getNewXref(); 7026ccdf4f0SGreg Roach $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ '); 7036ccdf4f0SGreg Roach 7046ccdf4f0SGreg Roach // Create a change record 7056ccdf4f0SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 7066ccdf4f0SGreg Roach 7076ccdf4f0SGreg Roach // Create a pending change 7086ccdf4f0SGreg Roach DB::table('change')->insert([ 7096ccdf4f0SGreg Roach 'gedcom_id' => $this->id, 7106ccdf4f0SGreg Roach 'xref' => $xref, 7116ccdf4f0SGreg Roach 'old_gedcom' => '', 7126ccdf4f0SGreg Roach 'new_gedcom' => $gedcom, 7136ccdf4f0SGreg Roach 'user_id' => Auth::id(), 7146ccdf4f0SGreg Roach ]); 7156ccdf4f0SGreg Roach 7166ccdf4f0SGreg Roach // Accept this pending change 7176ccdf4f0SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 7186ccdf4f0SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 7196ccdf4f0SGreg Roach 7206ccdf4f0SGreg Roach return new GedcomRecord($xref, $gedcom, null, $this); 7216ccdf4f0SGreg Roach } 7226ccdf4f0SGreg Roach 7236ccdf4f0SGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 724a25f0a04SGreg Roach } 725304f20d5SGreg Roach 726304f20d5SGreg Roach /** 727b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 728b90d8accSGreg Roach * 729b90d8accSGreg Roach * @return string 730b90d8accSGreg Roach */ 731771ae10aSGreg Roach public function getNewXref(): string 732c1010edaSGreg Roach { 733963fbaeeSGreg Roach // Lock the row, so that only one new XREF may be generated at a time. 734963fbaeeSGreg Roach DB::table('site_setting') 735963fbaeeSGreg Roach ->where('setting_name', '=', 'next_xref') 736963fbaeeSGreg Roach ->lockForUpdate() 737963fbaeeSGreg Roach ->get(); 738963fbaeeSGreg Roach 739a214e186SGreg Roach $prefix = 'X'; 740b90d8accSGreg Roach 741971d66c8SGreg Roach $increment = 1.0; 742b90d8accSGreg Roach do { 743963fbaeeSGreg Roach $num = (int) Site::getPreference('next_xref') + (int) $increment; 744971d66c8SGreg Roach 745971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 746971d66c8SGreg Roach // existing data in a reasonable time. 747971d66c8SGreg Roach $increment *= 1.01; 748963fbaeeSGreg Roach 749963fbaeeSGreg Roach $xref = $prefix . $num; 750963fbaeeSGreg Roach 751963fbaeeSGreg Roach // Records may already exist with this sequence number. 752963fbaeeSGreg Roach $already_used = 753963fbaeeSGreg Roach DB::table('individuals')->where('i_id', '=', $xref)->exists() || 754963fbaeeSGreg Roach DB::table('families')->where('f_id', '=', $xref)->exists() || 755963fbaeeSGreg Roach DB::table('sources')->where('s_id', '=', $xref)->exists() || 756963fbaeeSGreg Roach DB::table('media')->where('m_id', '=', $xref)->exists() || 757963fbaeeSGreg Roach DB::table('other')->where('o_id', '=', $xref)->exists() || 758963fbaeeSGreg Roach DB::table('change')->where('xref', '=', $xref)->exists(); 759963fbaeeSGreg Roach } while ($already_used); 760963fbaeeSGreg Roach 761963fbaeeSGreg Roach Site::setPreference('next_xref', (string) $num); 762b90d8accSGreg Roach 763a214e186SGreg Roach return $xref; 764b90d8accSGreg Roach } 765b90d8accSGreg Roach 766b90d8accSGreg Roach /** 767afb591d7SGreg Roach * Create a new family from GEDCOM data. 768afb591d7SGreg Roach * 769afb591d7SGreg Roach * @param string $gedcom 770afb591d7SGreg Roach * 771afb591d7SGreg Roach * @return Family 772afb591d7SGreg Roach * @throws InvalidArgumentException 773afb591d7SGreg Roach */ 774afb591d7SGreg Roach public function createFamily(string $gedcom): GedcomRecord 775afb591d7SGreg Roach { 776bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ FAM')) { 777afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM'); 778afb591d7SGreg Roach } 779afb591d7SGreg Roach 780afb591d7SGreg Roach $xref = $this->getNewXref(); 781bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM'); 782afb591d7SGreg Roach 783afb591d7SGreg Roach // Create a change record 784e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 785afb591d7SGreg Roach 786afb591d7SGreg Roach // Create a pending change 787963fbaeeSGreg Roach DB::table('change')->insert([ 788963fbaeeSGreg Roach 'gedcom_id' => $this->id, 789963fbaeeSGreg Roach 'xref' => $xref, 790963fbaeeSGreg Roach 'old_gedcom' => '', 791963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 792963fbaeeSGreg Roach 'user_id' => Auth::id(), 793afb591d7SGreg Roach ]); 794304f20d5SGreg Roach 795304f20d5SGreg Roach // Accept this pending change 796304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 797cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 798afb591d7SGreg Roach 799afb591d7SGreg Roach return new Family($xref, $gedcom, null, $this); 800304f20d5SGreg Roach } 801afb591d7SGreg Roach 802afb591d7SGreg Roach return new Family($xref, '', $gedcom, $this); 803afb591d7SGreg Roach } 804afb591d7SGreg Roach 805afb591d7SGreg Roach /** 806afb591d7SGreg Roach * Create a new individual from GEDCOM data. 807afb591d7SGreg Roach * 808afb591d7SGreg Roach * @param string $gedcom 809afb591d7SGreg Roach * 810afb591d7SGreg Roach * @return Individual 811afb591d7SGreg Roach * @throws InvalidArgumentException 812afb591d7SGreg Roach */ 813afb591d7SGreg Roach public function createIndividual(string $gedcom): GedcomRecord 814afb591d7SGreg Roach { 815bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ INDI')) { 816afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI'); 817afb591d7SGreg Roach } 818afb591d7SGreg Roach 819afb591d7SGreg Roach $xref = $this->getNewXref(); 820bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI'); 821afb591d7SGreg Roach 822afb591d7SGreg Roach // Create a change record 823e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 824afb591d7SGreg Roach 825afb591d7SGreg Roach // Create a pending change 826963fbaeeSGreg Roach DB::table('change')->insert([ 827963fbaeeSGreg Roach 'gedcom_id' => $this->id, 828963fbaeeSGreg Roach 'xref' => $xref, 829963fbaeeSGreg Roach 'old_gedcom' => '', 830963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 831963fbaeeSGreg Roach 'user_id' => Auth::id(), 832afb591d7SGreg Roach ]); 833afb591d7SGreg Roach 834afb591d7SGreg Roach // Accept this pending change 835afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 836afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 837afb591d7SGreg Roach 838afb591d7SGreg Roach return new Individual($xref, $gedcom, null, $this); 839afb591d7SGreg Roach } 840afb591d7SGreg Roach 841afb591d7SGreg Roach return new Individual($xref, '', $gedcom, $this); 842304f20d5SGreg Roach } 8438586983fSGreg Roach 8448586983fSGreg Roach /** 84520b58d20SGreg Roach * Create a new media object from GEDCOM data. 84620b58d20SGreg Roach * 84720b58d20SGreg Roach * @param string $gedcom 84820b58d20SGreg Roach * 84920b58d20SGreg Roach * @return Media 85020b58d20SGreg Roach * @throws InvalidArgumentException 85120b58d20SGreg Roach */ 85220b58d20SGreg Roach public function createMediaObject(string $gedcom): Media 85320b58d20SGreg Roach { 854bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ OBJE')) { 85520b58d20SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE'); 85620b58d20SGreg Roach } 85720b58d20SGreg Roach 85820b58d20SGreg Roach $xref = $this->getNewXref(); 859bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE'); 86020b58d20SGreg Roach 86120b58d20SGreg Roach // Create a change record 862e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 86320b58d20SGreg Roach 86420b58d20SGreg Roach // Create a pending change 865963fbaeeSGreg Roach DB::table('change')->insert([ 866963fbaeeSGreg Roach 'gedcom_id' => $this->id, 867963fbaeeSGreg Roach 'xref' => $xref, 868963fbaeeSGreg Roach 'old_gedcom' => '', 869963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 870963fbaeeSGreg Roach 'user_id' => Auth::id(), 87120b58d20SGreg Roach ]); 87220b58d20SGreg Roach 87320b58d20SGreg Roach // Accept this pending change 87420b58d20SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 87520b58d20SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 87620b58d20SGreg Roach 87720b58d20SGreg Roach return new Media($xref, $gedcom, null, $this); 87820b58d20SGreg Roach } 87920b58d20SGreg Roach 88020b58d20SGreg Roach return new Media($xref, '', $gedcom, $this); 88120b58d20SGreg Roach } 88220b58d20SGreg Roach 88320b58d20SGreg Roach /** 8848586983fSGreg Roach * What is the most significant individual in this tree. 8858586983fSGreg Roach * 886e5a6b4d4SGreg Roach * @param UserInterface $user 8878586983fSGreg Roach * 8888586983fSGreg Roach * @return Individual 8898586983fSGreg Roach */ 890e5a6b4d4SGreg Roach public function significantIndividual(UserInterface $user): Individual 891c1010edaSGreg Roach { 8928f9b0fb2SGreg Roach $individual = null; 8938586983fSGreg Roach 8948f9b0fb2SGreg Roach if ($this->getUserPreference($user, 'rootid') !== '') { 8958586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 8968586983fSGreg Roach } 8978f9b0fb2SGreg Roach 8988f9b0fb2SGreg Roach if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') { 8998586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 9008586983fSGreg Roach } 9018f9b0fb2SGreg Roach 902bec87e94SGreg Roach if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') { 9038586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 9048586983fSGreg Roach } 9058f9b0fb2SGreg Roach if ($individual === null) { 9068f9b0fb2SGreg Roach $xref = (string) DB::table('individuals') 9078f9b0fb2SGreg Roach ->where('i_file', '=', $this->id()) 9088f9b0fb2SGreg Roach ->min('i_id'); 909769d7d6eSGreg Roach 910769d7d6eSGreg Roach $individual = Individual::getInstance($xref, $this); 9115fe1add5SGreg Roach } 9128f9b0fb2SGreg Roach if ($individual === null) { 9135fe1add5SGreg Roach // always return a record 9145fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 9155fe1add5SGreg Roach } 9165fe1add5SGreg Roach 9175fe1add5SGreg Roach return $individual; 9185fe1add5SGreg Roach } 919a25f0a04SGreg Roach} 920