1a25f0a04SGreg Roach<?php 2*3976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees; 20a25f0a04SGreg Roach 21456d0d35SGreg Roachuse Fisharebest\Flysystem\Adapter\ChrootAdapter; 22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 2501461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2601461f86SGreg Roachuse Illuminate\Database\Query\Builder; 27a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 2801461f86SGreg Roachuse Illuminate\Database\Query\JoinClause; 2994026f20SGreg Roachuse Illuminate\Support\Collection; 30bec87e94SGreg Roachuse Illuminate\Support\Str; 31afb591d7SGreg Roachuse InvalidArgumentException; 321df7ae39SGreg Roachuse League\Flysystem\Filesystem; 331df7ae39SGreg Roachuse League\Flysystem\FilesystemInterface; 34a25f0a04SGreg Roachuse PDOException; 356ccdf4f0SGreg Roachuse Psr\Http\Message\StreamInterface; 368b67c11aSGreg Roachuse stdClass; 37a25f0a04SGreg Roach 38a25f0a04SGreg Roach/** 3976692c8bSGreg Roach * Provide an interface to the wt_gedcom table. 40a25f0a04SGreg Roach */ 41c1010edaSGreg Roachclass Tree 42c1010edaSGreg Roach{ 43061b43d7SGreg Roach private const RESN_PRIVACY = [ 44061b43d7SGreg Roach 'none' => Auth::PRIV_PRIVATE, 45061b43d7SGreg Roach 'privacy' => Auth::PRIV_USER, 46061b43d7SGreg Roach 'confidential' => Auth::PRIV_NONE, 47061b43d7SGreg Roach 'hidden' => Auth::PRIV_HIDE, 48061b43d7SGreg Roach ]; 496ccdf4f0SGreg Roach /** @var Tree[] All trees that we have permission to see, indexed by ID. */ 506ccdf4f0SGreg Roach public static $trees = []; 516ccdf4f0SGreg Roach /** @var int The tree's ID number */ 526ccdf4f0SGreg Roach private $id; 536ccdf4f0SGreg Roach /** @var string The tree's name */ 546ccdf4f0SGreg Roach private $name; 556ccdf4f0SGreg Roach /** @var string The tree's title */ 566ccdf4f0SGreg Roach private $title; 576ccdf4f0SGreg Roach /** @var int[] Default access rules for facts in this tree */ 586ccdf4f0SGreg Roach private $fact_privacy; 596ccdf4f0SGreg Roach /** @var int[] Default access rules for individuals in this tree */ 606ccdf4f0SGreg Roach private $individual_privacy; 616ccdf4f0SGreg Roach /** @var integer[][] Default access rules for individual facts in this tree */ 626ccdf4f0SGreg Roach private $individual_fact_privacy; 636ccdf4f0SGreg Roach /** @var string[] Cached copy of the wt_gedcom_setting table. */ 646ccdf4f0SGreg Roach private $preferences = []; 656ccdf4f0SGreg Roach /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ 666ccdf4f0SGreg Roach private $user_preferences = []; 67061b43d7SGreg Roach 68a25f0a04SGreg Roach /** 69a25f0a04SGreg Roach * Create a tree object. This is a private constructor - it can only 70a25f0a04SGreg Roach * be called from Tree::getAll() to ensure proper initialisation. 71a25f0a04SGreg Roach * 7272cf66d4SGreg Roach * @param int $id 73aa6f03bbSGreg Roach * @param string $name 74cc13d6d8SGreg Roach * @param string $title 75a25f0a04SGreg Roach */ 76cc13d6d8SGreg Roach private function __construct($id, $name, $title) 77c1010edaSGreg Roach { 7872cf66d4SGreg Roach $this->id = $id; 79aa6f03bbSGreg Roach $this->name = $name; 80cc13d6d8SGreg Roach $this->title = $title; 8113abd6f3SGreg Roach $this->fact_privacy = []; 8213abd6f3SGreg Roach $this->individual_privacy = []; 8313abd6f3SGreg Roach $this->individual_fact_privacy = []; 84518bbdc1SGreg Roach 85518bbdc1SGreg Roach // Load the privacy settings for this tree 86061b43d7SGreg Roach $rows = DB::table('default_resn') 87061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 88061b43d7SGreg Roach ->get(); 89518bbdc1SGreg Roach 90518bbdc1SGreg Roach foreach ($rows as $row) { 91061b43d7SGreg Roach // Convert GEDCOM privacy restriction to a webtrees access level. 92061b43d7SGreg Roach $row->resn = self::RESN_PRIVACY[$row->resn]; 93061b43d7SGreg Roach 94518bbdc1SGreg Roach if ($row->xref !== null) { 95518bbdc1SGreg Roach if ($row->tag_type !== null) { 96518bbdc1SGreg Roach $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn; 97518bbdc1SGreg Roach } else { 98518bbdc1SGreg Roach $this->individual_privacy[$row->xref] = (int) $row->resn; 99518bbdc1SGreg Roach } 100518bbdc1SGreg Roach } else { 101518bbdc1SGreg Roach $this->fact_privacy[$row->tag_type] = (int) $row->resn; 102518bbdc1SGreg Roach } 103518bbdc1SGreg Roach } 104a25f0a04SGreg Roach } 105a25f0a04SGreg Roach 106a25f0a04SGreg Roach /** 1076ccdf4f0SGreg Roach * Find the tree with a specific ID. 108a25f0a04SGreg Roach * 1096ccdf4f0SGreg Roach * @param int $tree_id 1106ccdf4f0SGreg Roach * 1116ccdf4f0SGreg Roach * @return Tree 112a25f0a04SGreg Roach */ 1136ccdf4f0SGreg Roach public static function findById(int $tree_id): Tree 114c1010edaSGreg Roach { 1156ccdf4f0SGreg Roach return self::getAll()[$tree_id]; 116a25f0a04SGreg Roach } 117a25f0a04SGreg Roach 118a25f0a04SGreg Roach /** 1196ccdf4f0SGreg Roach * Fetch all the trees that we have permission to access. 120a25f0a04SGreg Roach * 1216ccdf4f0SGreg Roach * @return Tree[] 122a25f0a04SGreg Roach */ 1236ccdf4f0SGreg Roach public static function getAll(): array 124c1010edaSGreg Roach { 1256ccdf4f0SGreg Roach if (empty(self::$trees)) { 1266ccdf4f0SGreg Roach self::$trees = self::all()->all(); 127a25f0a04SGreg Roach } 128a25f0a04SGreg Roach 1296ccdf4f0SGreg Roach return self::$trees; 130a25f0a04SGreg Roach } 131a25f0a04SGreg Roach 132a25f0a04SGreg Roach /** 1338b67c11aSGreg Roach * All the trees that we have permission to access. 134a25f0a04SGreg Roach * 13554c7f8dfSGreg Roach * @return Collection 136a25f0a04SGreg Roach */ 1378b67c11aSGreg Roach public static function all(): Collection 138c1010edaSGreg Roach { 1390b5fd0a6SGreg Roach return app('cache.array')->rememberForever(__CLASS__, static function (): Collection { 14001461f86SGreg Roach // Admins see all trees 14101461f86SGreg Roach $query = DB::table('gedcom') 1420b5fd0a6SGreg Roach ->leftJoin('gedcom_setting', static function (JoinClause $join): void { 14301461f86SGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 14401461f86SGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 14501461f86SGreg Roach }) 14601461f86SGreg Roach ->where('gedcom.gedcom_id', '>', 0) 14701461f86SGreg Roach ->select([ 14801461f86SGreg Roach 'gedcom.gedcom_id AS tree_id', 14901461f86SGreg Roach 'gedcom.gedcom_name AS tree_name', 15001461f86SGreg Roach 'gedcom_setting.setting_value AS tree_title', 15101461f86SGreg Roach ]) 15201461f86SGreg Roach ->orderBy('gedcom.sort_order') 15301461f86SGreg Roach ->orderBy('gedcom_setting.setting_value'); 15401461f86SGreg Roach 15532f20c14SGreg Roach // Non-admins may not see all trees 15632f20c14SGreg Roach if (!Auth::isAdmin()) { 15701461f86SGreg Roach $query 1580b5fd0a6SGreg Roach ->join('gedcom_setting AS gs2', static function (JoinClause $join): void { 15936357577SGreg Roach $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 16001461f86SGreg Roach ->where('gs2.setting_name', '=', 'imported'); 16136357577SGreg Roach }) 1620b5fd0a6SGreg Roach ->join('gedcom_setting AS gs3', static function (JoinClause $join): void { 16301461f86SGreg Roach $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 16401461f86SGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 16501461f86SGreg Roach }) 1660b5fd0a6SGreg Roach ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void { 16701461f86SGreg Roach $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 16801461f86SGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 16901461f86SGreg Roach ->where('user_gedcom_setting.setting_name', '=', 'canedit'); 17001461f86SGreg Roach }) 1710b5fd0a6SGreg Roach ->where(static function (Builder $query): void { 17201461f86SGreg Roach $query 17301461f86SGreg Roach // Managers 17401461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '=', 'admin') 17501461f86SGreg Roach // Members 1760b5fd0a6SGreg Roach ->orWhere(static function (Builder $query): void { 17701461f86SGreg Roach $query 17801461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 17901461f86SGreg Roach ->where('gs3.setting_value', '=', '1') 18001461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '<>', 'none'); 18101461f86SGreg Roach }) 1828b67c11aSGreg Roach // Public trees 1830b5fd0a6SGreg Roach ->orWhere(static function (Builder $query): void { 18401461f86SGreg Roach $query 18501461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 18636357577SGreg Roach ->where('gs3.setting_value', '<>', '1'); 18701461f86SGreg Roach }); 18801461f86SGreg Roach }); 18901461f86SGreg Roach } 19001461f86SGreg Roach 1918b67c11aSGreg Roach return $query 1928b67c11aSGreg Roach ->get() 1930b5fd0a6SGreg Roach ->mapWithKeys(static function (stdClass $row): array { 1948b67c11aSGreg Roach return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)]; 1958b67c11aSGreg Roach }); 1968b67c11aSGreg Roach }); 197a25f0a04SGreg Roach } 1988b67c11aSGreg Roach 1998b67c11aSGreg Roach /** 200a25f0a04SGreg Roach * Create arguments to select_edit_control() 201a25f0a04SGreg Roach * Note - these will be escaped later 202a25f0a04SGreg Roach * 203a25f0a04SGreg Roach * @return string[] 204a25f0a04SGreg Roach */ 205771ae10aSGreg Roach public static function getIdList(): array 206c1010edaSGreg Roach { 20713abd6f3SGreg Roach $list = []; 208a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 20972cf66d4SGreg Roach $list[$tree->id] = $tree->title; 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach 212a25f0a04SGreg Roach return $list; 213a25f0a04SGreg Roach } 214a25f0a04SGreg Roach 215a25f0a04SGreg Roach /** 216a25f0a04SGreg Roach * Create arguments to select_edit_control() 217a25f0a04SGreg Roach * Note - these will be escaped later 218a25f0a04SGreg Roach * 219a25f0a04SGreg Roach * @return string[] 220a25f0a04SGreg Roach */ 221771ae10aSGreg Roach public static function getNameList(): array 222c1010edaSGreg Roach { 22313abd6f3SGreg Roach $list = []; 224a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 225a25f0a04SGreg Roach $list[$tree->name] = $tree->title; 226a25f0a04SGreg Roach } 227a25f0a04SGreg Roach 228a25f0a04SGreg Roach return $list; 229a25f0a04SGreg Roach } 230a25f0a04SGreg Roach 231a25f0a04SGreg Roach /** 232a25f0a04SGreg Roach * Create a new tree 233a25f0a04SGreg Roach * 234a25f0a04SGreg Roach * @param string $tree_name 235a25f0a04SGreg Roach * @param string $tree_title 236a25f0a04SGreg Roach * 237a25f0a04SGreg Roach * @return Tree 238a25f0a04SGreg Roach */ 239771ae10aSGreg Roach public static function create(string $tree_name, string $tree_title): Tree 240c1010edaSGreg Roach { 241a25f0a04SGreg Roach try { 242a25f0a04SGreg Roach // Create a new tree 24301461f86SGreg Roach DB::table('gedcom')->insert([ 24401461f86SGreg Roach 'gedcom_name' => $tree_name, 24501461f86SGreg Roach ]); 2464a86d714SGreg Roach 247061b43d7SGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 24832f20c14SGreg Roach 24932f20c14SGreg Roach $tree = new self($tree_id, $tree_name, $tree_title); 250a25f0a04SGreg Roach } catch (PDOException $ex) { 251a25f0a04SGreg Roach // A tree with that name already exists? 252ef2fd529SGreg Roach return self::findByName($tree_name); 253a25f0a04SGreg Roach } 254a25f0a04SGreg Roach 255a25f0a04SGreg Roach $tree->setPreference('imported', '0'); 256a25f0a04SGreg Roach $tree->setPreference('title', $tree_title); 257a25f0a04SGreg Roach 2581507cbcaSGreg Roach // Set preferences from default tree 259061b43d7SGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 260061b43d7SGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 2616ccdf4f0SGreg Roach static function (Builder $query) use ($tree_id): void { 262061b43d7SGreg Roach $query 263a69f5655SGreg Roach ->select([new Expression($tree_id), 'setting_name', 'setting_value']) 264061b43d7SGreg Roach ->from('gedcom_setting') 265061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 266061b43d7SGreg Roach } 267061b43d7SGreg Roach ); 2681507cbcaSGreg Roach 269061b43d7SGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 270061b43d7SGreg Roach ['gedcom_id', 'tag_type', 'resn'], 2716c2179e2SGreg Roach static function (Builder $query) use ($tree_id): void { 272061b43d7SGreg Roach $query 273a69f5655SGreg Roach ->select([new Expression($tree_id), 'tag_type', 'resn']) 274061b43d7SGreg Roach ->from('default_resn') 275061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 276061b43d7SGreg Roach } 277061b43d7SGreg Roach ); 2781507cbcaSGreg Roach 279a25f0a04SGreg Roach // Gedcom and privacy settings 28076f666f4SGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 28176f666f4SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 282a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 283e364afe4SGreg Roach 284a25f0a04SGreg Roach switch (WT_LOCALE) { 285a25f0a04SGreg Roach case 'es': 286a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 287a25f0a04SGreg Roach break; 288a25f0a04SGreg Roach case 'is': 289a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 290a25f0a04SGreg Roach break; 291a25f0a04SGreg Roach case 'lt': 292a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 293a25f0a04SGreg Roach break; 294a25f0a04SGreg Roach case 'pl': 295a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 296a25f0a04SGreg Roach break; 297a25f0a04SGreg Roach case 'pt': 298a25f0a04SGreg Roach case 'pt-BR': 299a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 300a25f0a04SGreg Roach break; 301a25f0a04SGreg Roach default: 302a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 303a25f0a04SGreg Roach break; 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach 306a25f0a04SGreg Roach // Genealogy data 307a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 308bbb76c12SGreg Roach /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 309bbb76c12SGreg Roach $john_doe = I18N::translate('John /DOE/'); 31077e70a22SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 311061b43d7SGreg 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"; 312061b43d7SGreg Roach 313061b43d7SGreg Roach DB::table('gedcom_chunk')->insert([ 314061b43d7SGreg Roach 'gedcom_id' => $tree_id, 315061b43d7SGreg Roach 'chunk_data' => $gedcom, 31613abd6f3SGreg Roach ]); 317a25f0a04SGreg Roach 318a25f0a04SGreg Roach // Update our cache 31972cf66d4SGreg Roach self::$trees[$tree->id] = $tree; 320a25f0a04SGreg Roach 321a25f0a04SGreg Roach return $tree; 322a25f0a04SGreg Roach } 323a25f0a04SGreg Roach 324a25f0a04SGreg Roach /** 3256ccdf4f0SGreg Roach * Find the tree with a specific name. 3266ccdf4f0SGreg Roach * 3276ccdf4f0SGreg Roach * @param string $tree_name 3286ccdf4f0SGreg Roach * 3296ccdf4f0SGreg Roach * @return Tree|null 3306ccdf4f0SGreg Roach */ 3316ccdf4f0SGreg Roach public static function findByName($tree_name): ?Tree 3326ccdf4f0SGreg Roach { 3336ccdf4f0SGreg Roach foreach (self::getAll() as $tree) { 3346ccdf4f0SGreg Roach if ($tree->name === $tree_name) { 3356ccdf4f0SGreg Roach return $tree; 3366ccdf4f0SGreg Roach } 3376ccdf4f0SGreg Roach } 3386ccdf4f0SGreg Roach 3396ccdf4f0SGreg Roach return null; 3406ccdf4f0SGreg Roach } 3416ccdf4f0SGreg Roach 3426ccdf4f0SGreg Roach /** 3436ccdf4f0SGreg Roach * Set the tree’s configuration settings. 3446ccdf4f0SGreg Roach * 3456ccdf4f0SGreg Roach * @param string $setting_name 3466ccdf4f0SGreg Roach * @param string $setting_value 3476ccdf4f0SGreg Roach * 3486ccdf4f0SGreg Roach * @return $this 3496ccdf4f0SGreg Roach */ 3506ccdf4f0SGreg Roach public function setPreference(string $setting_name, string $setting_value): Tree 3516ccdf4f0SGreg Roach { 3526ccdf4f0SGreg Roach if ($setting_value !== $this->getPreference($setting_name)) { 3536ccdf4f0SGreg Roach DB::table('gedcom_setting')->updateOrInsert([ 3546ccdf4f0SGreg Roach 'gedcom_id' => $this->id, 3556ccdf4f0SGreg Roach 'setting_name' => $setting_name, 3566ccdf4f0SGreg Roach ], [ 3576ccdf4f0SGreg Roach 'setting_value' => $setting_value, 3586ccdf4f0SGreg Roach ]); 3596ccdf4f0SGreg Roach 3606ccdf4f0SGreg Roach $this->preferences[$setting_name] = $setting_value; 3616ccdf4f0SGreg Roach 3626ccdf4f0SGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); 3636ccdf4f0SGreg Roach } 3646ccdf4f0SGreg Roach 3656ccdf4f0SGreg Roach return $this; 3666ccdf4f0SGreg Roach } 3676ccdf4f0SGreg Roach 3686ccdf4f0SGreg Roach /** 3696ccdf4f0SGreg Roach * Get the tree’s configuration settings. 3706ccdf4f0SGreg Roach * 3716ccdf4f0SGreg Roach * @param string $setting_name 3726ccdf4f0SGreg Roach * @param string $default 3736ccdf4f0SGreg Roach * 3746ccdf4f0SGreg Roach * @return string 3756ccdf4f0SGreg Roach */ 3766ccdf4f0SGreg Roach public function getPreference(string $setting_name, string $default = ''): string 3776ccdf4f0SGreg Roach { 3786ccdf4f0SGreg Roach if (empty($this->preferences)) { 3796ccdf4f0SGreg Roach $this->preferences = DB::table('gedcom_setting') 3806ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 3816ccdf4f0SGreg Roach ->pluck('setting_value', 'setting_name') 3826ccdf4f0SGreg Roach ->all(); 3836ccdf4f0SGreg Roach } 3846ccdf4f0SGreg Roach 3856ccdf4f0SGreg Roach return $this->preferences[$setting_name] ?? $default; 3866ccdf4f0SGreg Roach } 3876ccdf4f0SGreg Roach 3886ccdf4f0SGreg Roach /** 3896ccdf4f0SGreg Roach * The name of this tree 3906ccdf4f0SGreg Roach * 3916ccdf4f0SGreg Roach * @return string 3926ccdf4f0SGreg Roach */ 3936ccdf4f0SGreg Roach public function name(): string 3946ccdf4f0SGreg Roach { 3956ccdf4f0SGreg Roach return $this->name; 3966ccdf4f0SGreg Roach } 3976ccdf4f0SGreg Roach 3986ccdf4f0SGreg Roach /** 3996ccdf4f0SGreg Roach * The title of this tree 4006ccdf4f0SGreg Roach * 4016ccdf4f0SGreg Roach * @return string 4026ccdf4f0SGreg Roach */ 4036ccdf4f0SGreg Roach public function title(): string 4046ccdf4f0SGreg Roach { 4056ccdf4f0SGreg Roach return $this->title; 4066ccdf4f0SGreg Roach } 4076ccdf4f0SGreg Roach 4086ccdf4f0SGreg Roach /** 4096ccdf4f0SGreg Roach * The fact-level privacy for this tree. 4106ccdf4f0SGreg Roach * 4116ccdf4f0SGreg Roach * @return int[] 4126ccdf4f0SGreg Roach */ 4136ccdf4f0SGreg Roach public function getFactPrivacy(): array 4146ccdf4f0SGreg Roach { 4156ccdf4f0SGreg Roach return $this->fact_privacy; 4166ccdf4f0SGreg Roach } 4176ccdf4f0SGreg Roach 4186ccdf4f0SGreg Roach /** 4196ccdf4f0SGreg Roach * The individual-level privacy for this tree. 4206ccdf4f0SGreg Roach * 4216ccdf4f0SGreg Roach * @return int[] 4226ccdf4f0SGreg Roach */ 4236ccdf4f0SGreg Roach public function getIndividualPrivacy(): array 4246ccdf4f0SGreg Roach { 4256ccdf4f0SGreg Roach return $this->individual_privacy; 4266ccdf4f0SGreg Roach } 4276ccdf4f0SGreg Roach 4286ccdf4f0SGreg Roach /** 4296ccdf4f0SGreg Roach * The individual-fact-level privacy for this tree. 4306ccdf4f0SGreg Roach * 4316ccdf4f0SGreg Roach * @return int[][] 4326ccdf4f0SGreg Roach */ 4336ccdf4f0SGreg Roach public function getIndividualFactPrivacy(): array 4346ccdf4f0SGreg Roach { 4356ccdf4f0SGreg Roach return $this->individual_fact_privacy; 4366ccdf4f0SGreg Roach } 4376ccdf4f0SGreg Roach 4386ccdf4f0SGreg Roach /** 4396ccdf4f0SGreg Roach * Set the tree’s user-configuration settings. 4406ccdf4f0SGreg Roach * 4416ccdf4f0SGreg Roach * @param UserInterface $user 4426ccdf4f0SGreg Roach * @param string $setting_name 4436ccdf4f0SGreg Roach * @param string $setting_value 4446ccdf4f0SGreg Roach * 4456ccdf4f0SGreg Roach * @return $this 4466ccdf4f0SGreg Roach */ 4476ccdf4f0SGreg Roach public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree 4486ccdf4f0SGreg Roach { 4496ccdf4f0SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 4506ccdf4f0SGreg Roach // Update the database 4516ccdf4f0SGreg Roach DB::table('user_gedcom_setting')->updateOrInsert([ 4526ccdf4f0SGreg Roach 'gedcom_id' => $this->id(), 4536ccdf4f0SGreg Roach 'user_id' => $user->id(), 4546ccdf4f0SGreg Roach 'setting_name' => $setting_name, 4556ccdf4f0SGreg Roach ], [ 4566ccdf4f0SGreg Roach 'setting_value' => $setting_value, 4576ccdf4f0SGreg Roach ]); 4586ccdf4f0SGreg Roach 4596ccdf4f0SGreg Roach // Update the cache 4606ccdf4f0SGreg Roach $this->user_preferences[$user->id()][$setting_name] = $setting_value; 4616ccdf4f0SGreg Roach // Audit log of changes 4626ccdf4f0SGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this); 4636ccdf4f0SGreg Roach } 4646ccdf4f0SGreg Roach 4656ccdf4f0SGreg Roach return $this; 4666ccdf4f0SGreg Roach } 4676ccdf4f0SGreg Roach 4686ccdf4f0SGreg Roach /** 4696ccdf4f0SGreg Roach * Get the tree’s user-configuration settings. 4706ccdf4f0SGreg Roach * 4716ccdf4f0SGreg Roach * @param UserInterface $user 4726ccdf4f0SGreg Roach * @param string $setting_name 4736ccdf4f0SGreg Roach * @param string $default 4746ccdf4f0SGreg Roach * 4756ccdf4f0SGreg Roach * @return string 4766ccdf4f0SGreg Roach */ 4776ccdf4f0SGreg Roach public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string 4786ccdf4f0SGreg Roach { 4796ccdf4f0SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 4806ccdf4f0SGreg Roach // so it is quicker to fetch them all in one go. 4816ccdf4f0SGreg Roach if (!array_key_exists($user->id(), $this->user_preferences)) { 4826ccdf4f0SGreg Roach $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting') 4836ccdf4f0SGreg Roach ->where('user_id', '=', $user->id()) 4846ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 4856ccdf4f0SGreg Roach ->pluck('setting_value', 'setting_name') 4866ccdf4f0SGreg Roach ->all(); 4876ccdf4f0SGreg Roach } 4886ccdf4f0SGreg Roach 4896ccdf4f0SGreg Roach return $this->user_preferences[$user->id()][$setting_name] ?? $default; 4906ccdf4f0SGreg Roach } 4916ccdf4f0SGreg Roach 4926ccdf4f0SGreg Roach /** 4936ccdf4f0SGreg Roach * The ID of this tree 4946ccdf4f0SGreg Roach * 4956ccdf4f0SGreg Roach * @return int 4966ccdf4f0SGreg Roach */ 4976ccdf4f0SGreg Roach public function id(): int 4986ccdf4f0SGreg Roach { 4996ccdf4f0SGreg Roach return $this->id; 5006ccdf4f0SGreg Roach } 5016ccdf4f0SGreg Roach 5026ccdf4f0SGreg Roach /** 5036ccdf4f0SGreg Roach * Can a user accept changes for this tree? 5046ccdf4f0SGreg Roach * 5056ccdf4f0SGreg Roach * @param UserInterface $user 5066ccdf4f0SGreg Roach * 5076ccdf4f0SGreg Roach * @return bool 5086ccdf4f0SGreg Roach */ 5096ccdf4f0SGreg Roach public function canAcceptChanges(UserInterface $user): bool 5106ccdf4f0SGreg Roach { 5116ccdf4f0SGreg Roach return Auth::isModerator($this, $user); 5126ccdf4f0SGreg Roach } 5136ccdf4f0SGreg Roach 5146ccdf4f0SGreg Roach /** 515b78374c5SGreg Roach * Are there any pending edits for this tree, than need reviewing by a moderator. 516b78374c5SGreg Roach * 517b78374c5SGreg Roach * @return bool 518b78374c5SGreg Roach */ 519771ae10aSGreg Roach public function hasPendingEdit(): bool 520c1010edaSGreg Roach { 52115a3f100SGreg Roach return DB::table('change') 52215a3f100SGreg Roach ->where('gedcom_id', '=', $this->id) 52315a3f100SGreg Roach ->where('status', '=', 'pending') 52415a3f100SGreg Roach ->exists(); 525b78374c5SGreg Roach } 526b78374c5SGreg Roach 527b78374c5SGreg Roach /** 5286ccdf4f0SGreg Roach * Delete everything relating to a tree 5296ccdf4f0SGreg Roach * 5306ccdf4f0SGreg Roach * @return void 5316ccdf4f0SGreg Roach */ 5326ccdf4f0SGreg Roach public function delete(): void 5336ccdf4f0SGreg Roach { 5346ccdf4f0SGreg Roach // If this is the default tree, then unset it 5356ccdf4f0SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 5366ccdf4f0SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 5376ccdf4f0SGreg Roach } 5386ccdf4f0SGreg Roach 5396ccdf4f0SGreg Roach $this->deleteGenealogyData(false); 5406ccdf4f0SGreg Roach 5416ccdf4f0SGreg Roach DB::table('block_setting') 5426ccdf4f0SGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 5436ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 5446ccdf4f0SGreg Roach ->delete(); 5456ccdf4f0SGreg Roach DB::table('block')->where('gedcom_id', '=', $this->id)->delete(); 5466ccdf4f0SGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 5476ccdf4f0SGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 5486ccdf4f0SGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete(); 5496ccdf4f0SGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete(); 5506ccdf4f0SGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete(); 5516ccdf4f0SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5526ccdf4f0SGreg Roach DB::table('log')->where('gedcom_id', '=', $this->id)->delete(); 5536ccdf4f0SGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete(); 5546ccdf4f0SGreg Roach 5556ccdf4f0SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 5566ccdf4f0SGreg Roach self::$trees = []; 5576ccdf4f0SGreg Roach } 5586ccdf4f0SGreg Roach 5596ccdf4f0SGreg Roach /** 560a25f0a04SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 561a25f0a04SGreg Roach * new data. Optionally retain the media data, for when the user has been 562a25f0a04SGreg Roach * editing their data offline using an application which deletes (or does not 563a25f0a04SGreg Roach * support) media data. 564a25f0a04SGreg Roach * 565a25f0a04SGreg Roach * @param bool $keep_media 566b7e60af1SGreg Roach * 567b7e60af1SGreg Roach * @return void 568a25f0a04SGreg Roach */ 569e364afe4SGreg Roach public function deleteGenealogyData(bool $keep_media): void 570c1010edaSGreg Roach { 5711ad2dde6SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5721ad2dde6SGreg Roach DB::table('individuals')->where('i_file', '=', $this->id)->delete(); 5731ad2dde6SGreg Roach DB::table('families')->where('f_file', '=', $this->id)->delete(); 5741ad2dde6SGreg Roach DB::table('sources')->where('s_file', '=', $this->id)->delete(); 5751ad2dde6SGreg Roach DB::table('other')->where('o_file', '=', $this->id)->delete(); 5761ad2dde6SGreg Roach DB::table('places')->where('p_file', '=', $this->id)->delete(); 5771ad2dde6SGreg Roach DB::table('placelinks')->where('pl_file', '=', $this->id)->delete(); 5781ad2dde6SGreg Roach DB::table('name')->where('n_file', '=', $this->id)->delete(); 5791ad2dde6SGreg Roach DB::table('dates')->where('d_file', '=', $this->id)->delete(); 5801ad2dde6SGreg Roach DB::table('change')->where('gedcom_id', '=', $this->id)->delete(); 581a25f0a04SGreg Roach 582a25f0a04SGreg Roach if ($keep_media) { 5831ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id) 5841ad2dde6SGreg Roach ->where('l_type', '<>', 'OBJE') 5851ad2dde6SGreg Roach ->delete(); 586a25f0a04SGreg Roach } else { 5871ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id)->delete(); 5881ad2dde6SGreg Roach DB::table('media_file')->where('m_file', '=', $this->id)->delete(); 5891ad2dde6SGreg Roach DB::table('media')->where('m_file', '=', $this->id)->delete(); 590a25f0a04SGreg Roach } 591a25f0a04SGreg Roach } 592a25f0a04SGreg Roach 593a25f0a04SGreg Roach /** 594a25f0a04SGreg Roach * Export the tree to a GEDCOM file 595a25f0a04SGreg Roach * 5965792757eSGreg Roach * @param resource $stream 597b7e60af1SGreg Roach * 598b7e60af1SGreg Roach * @return void 599a25f0a04SGreg Roach */ 600425af8b9SGreg Roach public function exportGedcom($stream): void 601c1010edaSGreg Roach { 602a3d8780cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8')); 60394026f20SGreg Roach 60494026f20SGreg Roach $union_families = DB::table('families') 60594026f20SGreg Roach ->where('f_file', '=', $this->id) 606a69f5655SGreg Roach ->select(['f_gedcom AS gedcom', 'f_id AS xref', new Expression('LENGTH(f_id) AS len'), new Expression('2 AS n')]); 60794026f20SGreg Roach 60894026f20SGreg Roach $union_sources = DB::table('sources') 60994026f20SGreg Roach ->where('s_file', '=', $this->id) 610a69f5655SGreg Roach ->select(['s_gedcom AS gedcom', 's_id AS xref', new Expression('LENGTH(s_id) AS len'), new Expression('3 AS n')]); 61194026f20SGreg Roach 61294026f20SGreg Roach $union_other = DB::table('other') 61394026f20SGreg Roach ->where('o_file', '=', $this->id) 61494026f20SGreg Roach ->whereNotIn('o_type', ['HEAD', 'TRLR']) 615a69f5655SGreg Roach ->select(['o_gedcom AS gedcom', 'o_id AS xref', new Expression('LENGTH(o_id) AS len'), new Expression('4 AS n')]); 61694026f20SGreg Roach 61794026f20SGreg Roach $union_media = DB::table('media') 61894026f20SGreg Roach ->where('m_file', '=', $this->id) 619a69f5655SGreg Roach ->select(['m_gedcom AS gedcom', 'm_id AS xref', new Expression('LENGTH(m_id) AS len'), new Expression('5 AS n')]); 62094026f20SGreg Roach 621e5a6b4d4SGreg Roach DB::table('individuals') 62294026f20SGreg Roach ->where('i_file', '=', $this->id) 623a69f5655SGreg Roach ->select(['i_gedcom AS gedcom', 'i_id AS xref', new Expression('LENGTH(i_id) AS len'), new Expression('1 AS n')]) 62494026f20SGreg Roach ->union($union_families) 62594026f20SGreg Roach ->union($union_sources) 62694026f20SGreg Roach ->union($union_other) 62794026f20SGreg Roach ->union($union_media) 62894026f20SGreg Roach ->orderBy('n') 62994026f20SGreg Roach ->orderBy('len') 63094026f20SGreg Roach ->orderBy('xref') 63127825e0aSGreg Roach ->chunk(1000, static function (Collection $rows) use ($stream, &$buffer): void { 63294026f20SGreg Roach foreach ($rows as $row) { 6333d7a8a4cSGreg Roach $buffer .= FunctionsExport::reformatRecord($row->gedcom); 634a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 6355792757eSGreg Roach fwrite($stream, $buffer); 636a25f0a04SGreg Roach $buffer = ''; 637a25f0a04SGreg Roach } 638a25f0a04SGreg Roach } 63994026f20SGreg Roach }); 64094026f20SGreg Roach 6410f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 642a25f0a04SGreg Roach } 643a25f0a04SGreg Roach 644a25f0a04SGreg Roach /** 645a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 646a25f0a04SGreg Roach * 6476ccdf4f0SGreg Roach * @param StreamInterface $stream The GEDCOM file. 648a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 649a25f0a04SGreg Roach * 650b7e60af1SGreg Roach * @return void 651a25f0a04SGreg Roach */ 6526ccdf4f0SGreg Roach public function importGedcomFile(StreamInterface $stream, string $filename): void 653c1010edaSGreg Roach { 654a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 655a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 656a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 657a25f0a04SGreg Roach // each block. 658a25f0a04SGreg Roach 659a25f0a04SGreg Roach $file_data = ''; 660a25f0a04SGreg Roach 661b7e60af1SGreg Roach $this->deleteGenealogyData((bool) $this->getPreference('keep_media')); 662a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 663a25f0a04SGreg Roach $this->setPreference('imported', '0'); 664a25f0a04SGreg Roach 6656ccdf4f0SGreg Roach while (!$stream->eof()) { 6666ccdf4f0SGreg Roach $file_data .= $stream->read(65536); 667a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 668a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 669a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 670a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 671a25f0a04SGreg Roach break; 672a25f0a04SGreg Roach } 673a25f0a04SGreg Roach } 674a25f0a04SGreg Roach if ($pos) { 6751ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6761ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6771ad2dde6SGreg Roach 'chunk_data' => substr($file_data, 0, $pos), 678c1010edaSGreg Roach ]); 6791ad2dde6SGreg Roach 680a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 681a25f0a04SGreg Roach } 682a25f0a04SGreg Roach } 6831ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6841ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6851ad2dde6SGreg Roach 'chunk_data' => $file_data, 686c1010edaSGreg Roach ]); 687a25f0a04SGreg Roach 6886ccdf4f0SGreg Roach $stream->close(); 6896ccdf4f0SGreg Roach } 6906ccdf4f0SGreg Roach 6916ccdf4f0SGreg Roach /** 6926ccdf4f0SGreg Roach * Create a new record from GEDCOM data. 6936ccdf4f0SGreg Roach * 6946ccdf4f0SGreg Roach * @param string $gedcom 6956ccdf4f0SGreg Roach * 6966ccdf4f0SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 6976ccdf4f0SGreg Roach * @throws InvalidArgumentException 6986ccdf4f0SGreg Roach */ 6996ccdf4f0SGreg Roach public function createRecord(string $gedcom): GedcomRecord 7006ccdf4f0SGreg Roach { 7016ccdf4f0SGreg Roach if (!Str::startsWith($gedcom, '0 @@ ')) { 7026ccdf4f0SGreg Roach throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@'); 7036ccdf4f0SGreg Roach } 7046ccdf4f0SGreg Roach 7056ccdf4f0SGreg Roach $xref = $this->getNewXref(); 7066ccdf4f0SGreg Roach $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ '); 7076ccdf4f0SGreg Roach 7086ccdf4f0SGreg Roach // Create a change record 7096ccdf4f0SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 7106ccdf4f0SGreg Roach 7116ccdf4f0SGreg Roach // Create a pending change 7126ccdf4f0SGreg Roach DB::table('change')->insert([ 7136ccdf4f0SGreg Roach 'gedcom_id' => $this->id, 7146ccdf4f0SGreg Roach 'xref' => $xref, 7156ccdf4f0SGreg Roach 'old_gedcom' => '', 7166ccdf4f0SGreg Roach 'new_gedcom' => $gedcom, 7176ccdf4f0SGreg Roach 'user_id' => Auth::id(), 7186ccdf4f0SGreg Roach ]); 7196ccdf4f0SGreg Roach 7206ccdf4f0SGreg Roach // Accept this pending change 7216ccdf4f0SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 7226ccdf4f0SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 7236ccdf4f0SGreg Roach 7246ccdf4f0SGreg Roach return new GedcomRecord($xref, $gedcom, null, $this); 7256ccdf4f0SGreg Roach } 7266ccdf4f0SGreg Roach 7276ccdf4f0SGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 728a25f0a04SGreg Roach } 729304f20d5SGreg Roach 730304f20d5SGreg Roach /** 731b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 732b90d8accSGreg Roach * 733b90d8accSGreg Roach * @return string 734b90d8accSGreg Roach */ 735771ae10aSGreg Roach public function getNewXref(): string 736c1010edaSGreg Roach { 737963fbaeeSGreg Roach // Lock the row, so that only one new XREF may be generated at a time. 738963fbaeeSGreg Roach DB::table('site_setting') 739963fbaeeSGreg Roach ->where('setting_name', '=', 'next_xref') 740963fbaeeSGreg Roach ->lockForUpdate() 741963fbaeeSGreg Roach ->get(); 742963fbaeeSGreg Roach 743a214e186SGreg Roach $prefix = 'X'; 744b90d8accSGreg Roach 745971d66c8SGreg Roach $increment = 1.0; 746b90d8accSGreg Roach do { 747963fbaeeSGreg Roach $num = (int) Site::getPreference('next_xref') + (int) $increment; 748971d66c8SGreg Roach 749971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 750971d66c8SGreg Roach // existing data in a reasonable time. 751971d66c8SGreg Roach $increment *= 1.01; 752963fbaeeSGreg Roach 753963fbaeeSGreg Roach $xref = $prefix . $num; 754963fbaeeSGreg Roach 755963fbaeeSGreg Roach // Records may already exist with this sequence number. 756963fbaeeSGreg Roach $already_used = 757963fbaeeSGreg Roach DB::table('individuals')->where('i_id', '=', $xref)->exists() || 758963fbaeeSGreg Roach DB::table('families')->where('f_id', '=', $xref)->exists() || 759963fbaeeSGreg Roach DB::table('sources')->where('s_id', '=', $xref)->exists() || 760963fbaeeSGreg Roach DB::table('media')->where('m_id', '=', $xref)->exists() || 761963fbaeeSGreg Roach DB::table('other')->where('o_id', '=', $xref)->exists() || 762963fbaeeSGreg Roach DB::table('change')->where('xref', '=', $xref)->exists(); 763963fbaeeSGreg Roach } while ($already_used); 764963fbaeeSGreg Roach 765963fbaeeSGreg Roach Site::setPreference('next_xref', (string) $num); 766b90d8accSGreg Roach 767a214e186SGreg Roach return $xref; 768b90d8accSGreg Roach } 769b90d8accSGreg Roach 770b90d8accSGreg Roach /** 771afb591d7SGreg Roach * Create a new family from GEDCOM data. 772afb591d7SGreg Roach * 773afb591d7SGreg Roach * @param string $gedcom 774afb591d7SGreg Roach * 775afb591d7SGreg Roach * @return Family 776afb591d7SGreg Roach * @throws InvalidArgumentException 777afb591d7SGreg Roach */ 778afb591d7SGreg Roach public function createFamily(string $gedcom): GedcomRecord 779afb591d7SGreg Roach { 780bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ FAM')) { 781afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM'); 782afb591d7SGreg Roach } 783afb591d7SGreg Roach 784afb591d7SGreg Roach $xref = $this->getNewXref(); 785bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM'); 786afb591d7SGreg Roach 787afb591d7SGreg Roach // Create a change record 788e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 789afb591d7SGreg Roach 790afb591d7SGreg Roach // Create a pending change 791963fbaeeSGreg Roach DB::table('change')->insert([ 792963fbaeeSGreg Roach 'gedcom_id' => $this->id, 793963fbaeeSGreg Roach 'xref' => $xref, 794963fbaeeSGreg Roach 'old_gedcom' => '', 795963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 796963fbaeeSGreg Roach 'user_id' => Auth::id(), 797afb591d7SGreg Roach ]); 798304f20d5SGreg Roach 799304f20d5SGreg Roach // Accept this pending change 800304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 801cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 802afb591d7SGreg Roach 803afb591d7SGreg Roach return new Family($xref, $gedcom, null, $this); 804304f20d5SGreg Roach } 805afb591d7SGreg Roach 806afb591d7SGreg Roach return new Family($xref, '', $gedcom, $this); 807afb591d7SGreg Roach } 808afb591d7SGreg Roach 809afb591d7SGreg Roach /** 810afb591d7SGreg Roach * Create a new individual from GEDCOM data. 811afb591d7SGreg Roach * 812afb591d7SGreg Roach * @param string $gedcom 813afb591d7SGreg Roach * 814afb591d7SGreg Roach * @return Individual 815afb591d7SGreg Roach * @throws InvalidArgumentException 816afb591d7SGreg Roach */ 817afb591d7SGreg Roach public function createIndividual(string $gedcom): GedcomRecord 818afb591d7SGreg Roach { 819bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ INDI')) { 820afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI'); 821afb591d7SGreg Roach } 822afb591d7SGreg Roach 823afb591d7SGreg Roach $xref = $this->getNewXref(); 824bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI'); 825afb591d7SGreg Roach 826afb591d7SGreg Roach // Create a change record 827e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 828afb591d7SGreg Roach 829afb591d7SGreg Roach // Create a pending change 830963fbaeeSGreg Roach DB::table('change')->insert([ 831963fbaeeSGreg Roach 'gedcom_id' => $this->id, 832963fbaeeSGreg Roach 'xref' => $xref, 833963fbaeeSGreg Roach 'old_gedcom' => '', 834963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 835963fbaeeSGreg Roach 'user_id' => Auth::id(), 836afb591d7SGreg Roach ]); 837afb591d7SGreg Roach 838afb591d7SGreg Roach // Accept this pending change 839afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 840afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 841afb591d7SGreg Roach 842afb591d7SGreg Roach return new Individual($xref, $gedcom, null, $this); 843afb591d7SGreg Roach } 844afb591d7SGreg Roach 845afb591d7SGreg Roach return new Individual($xref, '', $gedcom, $this); 846304f20d5SGreg Roach } 8478586983fSGreg Roach 8488586983fSGreg Roach /** 84920b58d20SGreg Roach * Create a new media object from GEDCOM data. 85020b58d20SGreg Roach * 85120b58d20SGreg Roach * @param string $gedcom 85220b58d20SGreg Roach * 85320b58d20SGreg Roach * @return Media 85420b58d20SGreg Roach * @throws InvalidArgumentException 85520b58d20SGreg Roach */ 85620b58d20SGreg Roach public function createMediaObject(string $gedcom): Media 85720b58d20SGreg Roach { 858bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ OBJE')) { 85920b58d20SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE'); 86020b58d20SGreg Roach } 86120b58d20SGreg Roach 86220b58d20SGreg Roach $xref = $this->getNewXref(); 863bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE'); 86420b58d20SGreg Roach 86520b58d20SGreg Roach // Create a change record 866e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 86720b58d20SGreg Roach 86820b58d20SGreg Roach // Create a pending change 869963fbaeeSGreg Roach DB::table('change')->insert([ 870963fbaeeSGreg Roach 'gedcom_id' => $this->id, 871963fbaeeSGreg Roach 'xref' => $xref, 872963fbaeeSGreg Roach 'old_gedcom' => '', 873963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 874963fbaeeSGreg Roach 'user_id' => Auth::id(), 87520b58d20SGreg Roach ]); 87620b58d20SGreg Roach 87720b58d20SGreg Roach // Accept this pending change 87820b58d20SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 87920b58d20SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 88020b58d20SGreg Roach 88120b58d20SGreg Roach return new Media($xref, $gedcom, null, $this); 88220b58d20SGreg Roach } 88320b58d20SGreg Roach 88420b58d20SGreg Roach return new Media($xref, '', $gedcom, $this); 88520b58d20SGreg Roach } 88620b58d20SGreg Roach 88720b58d20SGreg Roach /** 8888586983fSGreg Roach * What is the most significant individual in this tree. 8898586983fSGreg Roach * 890e5a6b4d4SGreg Roach * @param UserInterface $user 8918586983fSGreg Roach * 8928586983fSGreg Roach * @return Individual 8938586983fSGreg Roach */ 894e5a6b4d4SGreg Roach public function significantIndividual(UserInterface $user): Individual 895c1010edaSGreg Roach { 8968f9b0fb2SGreg Roach $individual = null; 8978586983fSGreg Roach 8988f9b0fb2SGreg Roach if ($this->getUserPreference($user, 'rootid') !== '') { 8998586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 9008586983fSGreg Roach } 9018f9b0fb2SGreg Roach 9028f9b0fb2SGreg Roach if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') { 9038586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 9048586983fSGreg Roach } 9058f9b0fb2SGreg Roach 906bec87e94SGreg Roach if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') { 9078586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 9088586983fSGreg Roach } 9098f9b0fb2SGreg Roach if ($individual === null) { 9108f9b0fb2SGreg Roach $xref = (string) DB::table('individuals') 9118f9b0fb2SGreg Roach ->where('i_file', '=', $this->id()) 9128f9b0fb2SGreg Roach ->min('i_id'); 913769d7d6eSGreg Roach 914769d7d6eSGreg Roach $individual = Individual::getInstance($xref, $this); 9155fe1add5SGreg Roach } 9168f9b0fb2SGreg Roach if ($individual === null) { 9175fe1add5SGreg Roach // always return a record 9185fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 9195fe1add5SGreg Roach } 9205fe1add5SGreg Roach 9215fe1add5SGreg Roach return $individual; 9225fe1add5SGreg Roach } 9231df7ae39SGreg Roach 92485a166d8SGreg Roach /** 92585a166d8SGreg Roach * Where do we store our media files. 92685a166d8SGreg Roach * 92785a166d8SGreg Roach * @return FilesystemInterface 92885a166d8SGreg Roach */ 9291df7ae39SGreg Roach public function mediaFilesystem(): FilesystemInterface 9301df7ae39SGreg Roach { 931456d0d35SGreg Roach $media_dir = $this->getPreference('MEDIA_DIRECTORY', 'media/'); 932456d0d35SGreg Roach $filesystem = app(FilesystemInterface::class); 933456d0d35SGreg Roach $adapter = new ChrootAdapter($filesystem, $media_dir); 934456d0d35SGreg Roach 935456d0d35SGreg Roach return new Filesystem($adapter); 9361df7ae39SGreg Roach } 937a25f0a04SGreg Roach} 938