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 20456d0d35SGreg Roachuse Fisharebest\Flysystem\Adapter\ChrootAdapter; 21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 2401461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2501461f86SGreg Roachuse Illuminate\Database\Query\Builder; 26a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 2701461f86SGreg Roachuse Illuminate\Database\Query\JoinClause; 2894026f20SGreg Roachuse Illuminate\Support\Collection; 29bec87e94SGreg Roachuse Illuminate\Support\Str; 30afb591d7SGreg Roachuse InvalidArgumentException; 311df7ae39SGreg Roachuse League\Flysystem\Filesystem; 321df7ae39SGreg Roachuse League\Flysystem\FilesystemInterface; 33a25f0a04SGreg Roachuse PDOException; 346ccdf4f0SGreg Roachuse Psr\Http\Message\StreamInterface; 358b67c11aSGreg Roachuse stdClass; 36a25f0a04SGreg Roach 37a25f0a04SGreg Roach/** 3876692c8bSGreg Roach * Provide an interface to the wt_gedcom table. 39a25f0a04SGreg Roach */ 40c1010edaSGreg Roachclass Tree 41c1010edaSGreg Roach{ 42061b43d7SGreg Roach private const RESN_PRIVACY = [ 43061b43d7SGreg Roach 'none' => Auth::PRIV_PRIVATE, 44061b43d7SGreg Roach 'privacy' => Auth::PRIV_USER, 45061b43d7SGreg Roach 'confidential' => Auth::PRIV_NONE, 46061b43d7SGreg Roach 'hidden' => Auth::PRIV_HIDE, 47061b43d7SGreg Roach ]; 486ccdf4f0SGreg Roach /** @var Tree[] All trees that we have permission to see, indexed by ID. */ 496ccdf4f0SGreg Roach public static $trees = []; 506ccdf4f0SGreg Roach /** @var int The tree's ID number */ 516ccdf4f0SGreg Roach private $id; 526ccdf4f0SGreg Roach /** @var string The tree's name */ 536ccdf4f0SGreg Roach private $name; 546ccdf4f0SGreg Roach /** @var string The tree's title */ 556ccdf4f0SGreg Roach private $title; 566ccdf4f0SGreg Roach /** @var int[] Default access rules for facts in this tree */ 576ccdf4f0SGreg Roach private $fact_privacy; 586ccdf4f0SGreg Roach /** @var int[] Default access rules for individuals in this tree */ 596ccdf4f0SGreg Roach private $individual_privacy; 606ccdf4f0SGreg Roach /** @var integer[][] Default access rules for individual facts in this tree */ 616ccdf4f0SGreg Roach private $individual_fact_privacy; 626ccdf4f0SGreg Roach /** @var string[] Cached copy of the wt_gedcom_setting table. */ 636ccdf4f0SGreg Roach private $preferences = []; 646ccdf4f0SGreg Roach /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ 656ccdf4f0SGreg Roach private $user_preferences = []; 66061b43d7SGreg Roach 67a25f0a04SGreg Roach /** 68a25f0a04SGreg Roach * Create a tree object. This is a private constructor - it can only 69a25f0a04SGreg Roach * be called from Tree::getAll() to ensure proper initialisation. 70a25f0a04SGreg Roach * 7172cf66d4SGreg Roach * @param int $id 72aa6f03bbSGreg Roach * @param string $name 73cc13d6d8SGreg Roach * @param string $title 74a25f0a04SGreg Roach */ 75cc13d6d8SGreg Roach private function __construct($id, $name, $title) 76c1010edaSGreg Roach { 7772cf66d4SGreg Roach $this->id = $id; 78aa6f03bbSGreg Roach $this->name = $name; 79cc13d6d8SGreg Roach $this->title = $title; 8013abd6f3SGreg Roach $this->fact_privacy = []; 8113abd6f3SGreg Roach $this->individual_privacy = []; 8213abd6f3SGreg Roach $this->individual_fact_privacy = []; 83518bbdc1SGreg Roach 84518bbdc1SGreg Roach // Load the privacy settings for this tree 85061b43d7SGreg Roach $rows = DB::table('default_resn') 86061b43d7SGreg Roach ->where('gedcom_id', '=', $this->id) 87061b43d7SGreg Roach ->get(); 88518bbdc1SGreg Roach 89518bbdc1SGreg Roach foreach ($rows as $row) { 90061b43d7SGreg Roach // Convert GEDCOM privacy restriction to a webtrees access level. 91061b43d7SGreg Roach $row->resn = self::RESN_PRIVACY[$row->resn]; 92061b43d7SGreg Roach 93518bbdc1SGreg Roach if ($row->xref !== null) { 94518bbdc1SGreg Roach if ($row->tag_type !== null) { 95518bbdc1SGreg Roach $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn; 96518bbdc1SGreg Roach } else { 97518bbdc1SGreg Roach $this->individual_privacy[$row->xref] = (int) $row->resn; 98518bbdc1SGreg Roach } 99518bbdc1SGreg Roach } else { 100518bbdc1SGreg Roach $this->fact_privacy[$row->tag_type] = (int) $row->resn; 101518bbdc1SGreg Roach } 102518bbdc1SGreg Roach } 103a25f0a04SGreg Roach } 104a25f0a04SGreg Roach 105a25f0a04SGreg Roach /** 1066ccdf4f0SGreg Roach * Find the tree with a specific ID. 107a25f0a04SGreg Roach * 1086ccdf4f0SGreg Roach * @param int $tree_id 1096ccdf4f0SGreg Roach * 1106ccdf4f0SGreg Roach * @return Tree 111a25f0a04SGreg Roach */ 1126ccdf4f0SGreg Roach public static function findById(int $tree_id): Tree 113c1010edaSGreg Roach { 1146ccdf4f0SGreg Roach return self::getAll()[$tree_id]; 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach 117a25f0a04SGreg Roach /** 1186ccdf4f0SGreg Roach * Fetch all the trees that we have permission to access. 119a25f0a04SGreg Roach * 1206ccdf4f0SGreg Roach * @return Tree[] 121a25f0a04SGreg Roach */ 1226ccdf4f0SGreg Roach public static function getAll(): array 123c1010edaSGreg Roach { 1246ccdf4f0SGreg Roach if (empty(self::$trees)) { 1256ccdf4f0SGreg Roach self::$trees = self::all()->all(); 126a25f0a04SGreg Roach } 127a25f0a04SGreg Roach 1286ccdf4f0SGreg Roach return self::$trees; 129a25f0a04SGreg Roach } 130a25f0a04SGreg Roach 131a25f0a04SGreg Roach /** 1328b67c11aSGreg Roach * All the trees that we have permission to access. 133a25f0a04SGreg Roach * 13454c7f8dfSGreg Roach * @return Collection 135a25f0a04SGreg Roach */ 1368b67c11aSGreg Roach public static function all(): Collection 137c1010edaSGreg Roach { 1380b5fd0a6SGreg Roach return app('cache.array')->rememberForever(__CLASS__, static function (): Collection { 13901461f86SGreg Roach // Admins see all trees 14001461f86SGreg Roach $query = DB::table('gedcom') 1410b5fd0a6SGreg Roach ->leftJoin('gedcom_setting', static function (JoinClause $join): void { 14201461f86SGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 14301461f86SGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 14401461f86SGreg Roach }) 14501461f86SGreg Roach ->where('gedcom.gedcom_id', '>', 0) 14601461f86SGreg Roach ->select([ 14701461f86SGreg Roach 'gedcom.gedcom_id AS tree_id', 14801461f86SGreg Roach 'gedcom.gedcom_name AS tree_name', 14901461f86SGreg Roach 'gedcom_setting.setting_value AS tree_title', 15001461f86SGreg Roach ]) 15101461f86SGreg Roach ->orderBy('gedcom.sort_order') 15201461f86SGreg Roach ->orderBy('gedcom_setting.setting_value'); 15301461f86SGreg Roach 15432f20c14SGreg Roach // Non-admins may not see all trees 15532f20c14SGreg Roach if (!Auth::isAdmin()) { 15601461f86SGreg Roach $query 1570b5fd0a6SGreg Roach ->join('gedcom_setting AS gs2', static function (JoinClause $join): void { 15836357577SGreg Roach $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 15901461f86SGreg Roach ->where('gs2.setting_name', '=', 'imported'); 16036357577SGreg Roach }) 1610b5fd0a6SGreg Roach ->join('gedcom_setting AS gs3', static function (JoinClause $join): void { 16201461f86SGreg Roach $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 16301461f86SGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 16401461f86SGreg Roach }) 1650b5fd0a6SGreg Roach ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void { 16601461f86SGreg Roach $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 16701461f86SGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 16801461f86SGreg Roach ->where('user_gedcom_setting.setting_name', '=', 'canedit'); 16901461f86SGreg Roach }) 1700b5fd0a6SGreg Roach ->where(static function (Builder $query): void { 17101461f86SGreg Roach $query 17201461f86SGreg Roach // Managers 17301461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '=', 'admin') 17401461f86SGreg Roach // Members 1750b5fd0a6SGreg Roach ->orWhere(static function (Builder $query): void { 17601461f86SGreg Roach $query 17701461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 17801461f86SGreg Roach ->where('gs3.setting_value', '=', '1') 17901461f86SGreg Roach ->where('user_gedcom_setting.setting_value', '<>', 'none'); 18001461f86SGreg Roach }) 1818b67c11aSGreg Roach // Public trees 1820b5fd0a6SGreg Roach ->orWhere(static function (Builder $query): void { 18301461f86SGreg Roach $query 18401461f86SGreg Roach ->where('gs2.setting_value', '=', '1') 18536357577SGreg Roach ->where('gs3.setting_value', '<>', '1'); 18601461f86SGreg Roach }); 18701461f86SGreg Roach }); 18801461f86SGreg Roach } 18901461f86SGreg Roach 1908b67c11aSGreg Roach return $query 1918b67c11aSGreg Roach ->get() 1920b5fd0a6SGreg Roach ->mapWithKeys(static function (stdClass $row): array { 1938b67c11aSGreg Roach return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)]; 1948b67c11aSGreg Roach }); 1958b67c11aSGreg Roach }); 196a25f0a04SGreg Roach } 1978b67c11aSGreg Roach 1988b67c11aSGreg Roach /** 199a25f0a04SGreg Roach * Create arguments to select_edit_control() 200a25f0a04SGreg Roach * Note - these will be escaped later 201a25f0a04SGreg Roach * 202a25f0a04SGreg Roach * @return string[] 203a25f0a04SGreg Roach */ 204771ae10aSGreg Roach public static function getIdList(): array 205c1010edaSGreg Roach { 20613abd6f3SGreg Roach $list = []; 207a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 20872cf66d4SGreg Roach $list[$tree->id] = $tree->title; 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach return $list; 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 214a25f0a04SGreg Roach /** 215a25f0a04SGreg Roach * Create arguments to select_edit_control() 216a25f0a04SGreg Roach * Note - these will be escaped later 217a25f0a04SGreg Roach * 218a25f0a04SGreg Roach * @return string[] 219a25f0a04SGreg Roach */ 220771ae10aSGreg Roach public static function getNameList(): array 221c1010edaSGreg Roach { 22213abd6f3SGreg Roach $list = []; 223a25f0a04SGreg Roach foreach (self::getAll() as $tree) { 224a25f0a04SGreg Roach $list[$tree->name] = $tree->title; 225a25f0a04SGreg Roach } 226a25f0a04SGreg Roach 227a25f0a04SGreg Roach return $list; 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach 230a25f0a04SGreg Roach /** 231a25f0a04SGreg Roach * Create a new tree 232a25f0a04SGreg Roach * 233a25f0a04SGreg Roach * @param string $tree_name 234a25f0a04SGreg Roach * @param string $tree_title 235a25f0a04SGreg Roach * 236a25f0a04SGreg Roach * @return Tree 237a25f0a04SGreg Roach */ 238771ae10aSGreg Roach public static function create(string $tree_name, string $tree_title): Tree 239c1010edaSGreg Roach { 240a25f0a04SGreg Roach try { 241a25f0a04SGreg Roach // Create a new tree 24201461f86SGreg Roach DB::table('gedcom')->insert([ 24301461f86SGreg Roach 'gedcom_name' => $tree_name, 24401461f86SGreg Roach ]); 2454a86d714SGreg Roach 246061b43d7SGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 24732f20c14SGreg Roach 24832f20c14SGreg Roach $tree = new self($tree_id, $tree_name, $tree_title); 249a25f0a04SGreg Roach } catch (PDOException $ex) { 250a25f0a04SGreg Roach // A tree with that name already exists? 251ef2fd529SGreg Roach return self::findByName($tree_name); 252a25f0a04SGreg Roach } 253a25f0a04SGreg Roach 254a25f0a04SGreg Roach $tree->setPreference('imported', '0'); 255a25f0a04SGreg Roach $tree->setPreference('title', $tree_title); 256a25f0a04SGreg Roach 2571507cbcaSGreg Roach // Set preferences from default tree 258061b43d7SGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 259061b43d7SGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 2606ccdf4f0SGreg Roach static function (Builder $query) use ($tree_id): void { 261061b43d7SGreg Roach $query 262a69f5655SGreg Roach ->select([new Expression($tree_id), 'setting_name', 'setting_value']) 263061b43d7SGreg Roach ->from('gedcom_setting') 264061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 265061b43d7SGreg Roach } 266061b43d7SGreg Roach ); 2671507cbcaSGreg Roach 268061b43d7SGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 269061b43d7SGreg Roach ['gedcom_id', 'tag_type', 'resn'], 2706c2179e2SGreg Roach static function (Builder $query) use ($tree_id): void { 271061b43d7SGreg Roach $query 272a69f5655SGreg Roach ->select([new Expression($tree_id), 'tag_type', 'resn']) 273061b43d7SGreg Roach ->from('default_resn') 274061b43d7SGreg Roach ->where('gedcom_id', '=', -1); 275061b43d7SGreg Roach } 276061b43d7SGreg Roach ); 2771507cbcaSGreg Roach 278a25f0a04SGreg Roach // Gedcom and privacy settings 27976f666f4SGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 28076f666f4SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 281a25f0a04SGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 282e364afe4SGreg Roach 283a25f0a04SGreg Roach switch (WT_LOCALE) { 284a25f0a04SGreg Roach case 'es': 285a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'spanish'); 286a25f0a04SGreg Roach break; 287a25f0a04SGreg Roach case 'is': 288a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'icelandic'); 289a25f0a04SGreg Roach break; 290a25f0a04SGreg Roach case 'lt': 291a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); 292a25f0a04SGreg Roach break; 293a25f0a04SGreg Roach case 'pl': 294a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'polish'); 295a25f0a04SGreg Roach break; 296a25f0a04SGreg Roach case 'pt': 297a25f0a04SGreg Roach case 'pt-BR': 298a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'portuguese'); 299a25f0a04SGreg Roach break; 300a25f0a04SGreg Roach default: 301a25f0a04SGreg Roach $tree->setPreference('SURNAME_TRADITION', 'paternal'); 302a25f0a04SGreg Roach break; 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach 305a25f0a04SGreg Roach // Genealogy data 306a25f0a04SGreg Roach // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... 307bbb76c12SGreg Roach /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ 308bbb76c12SGreg Roach $john_doe = I18N::translate('John /DOE/'); 30977e70a22SGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 310061b43d7SGreg 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"; 311061b43d7SGreg Roach 312061b43d7SGreg Roach DB::table('gedcom_chunk')->insert([ 313061b43d7SGreg Roach 'gedcom_id' => $tree_id, 314061b43d7SGreg Roach 'chunk_data' => $gedcom, 31513abd6f3SGreg Roach ]); 316a25f0a04SGreg Roach 317a25f0a04SGreg Roach // Update our cache 31872cf66d4SGreg Roach self::$trees[$tree->id] = $tree; 319a25f0a04SGreg Roach 320a25f0a04SGreg Roach return $tree; 321a25f0a04SGreg Roach } 322a25f0a04SGreg Roach 323a25f0a04SGreg Roach /** 3246ccdf4f0SGreg Roach * Find the tree with a specific name. 3256ccdf4f0SGreg Roach * 3266ccdf4f0SGreg Roach * @param string $tree_name 3276ccdf4f0SGreg Roach * 3286ccdf4f0SGreg Roach * @return Tree|null 3296ccdf4f0SGreg Roach */ 3306ccdf4f0SGreg Roach public static function findByName($tree_name): ?Tree 3316ccdf4f0SGreg Roach { 3326ccdf4f0SGreg Roach foreach (self::getAll() as $tree) { 3336ccdf4f0SGreg Roach if ($tree->name === $tree_name) { 3346ccdf4f0SGreg Roach return $tree; 3356ccdf4f0SGreg Roach } 3366ccdf4f0SGreg Roach } 3376ccdf4f0SGreg Roach 3386ccdf4f0SGreg Roach return null; 3396ccdf4f0SGreg Roach } 3406ccdf4f0SGreg Roach 3416ccdf4f0SGreg Roach /** 3426ccdf4f0SGreg Roach * Set the tree’s configuration settings. 3436ccdf4f0SGreg Roach * 3446ccdf4f0SGreg Roach * @param string $setting_name 3456ccdf4f0SGreg Roach * @param string $setting_value 3466ccdf4f0SGreg Roach * 3476ccdf4f0SGreg Roach * @return $this 3486ccdf4f0SGreg Roach */ 3496ccdf4f0SGreg Roach public function setPreference(string $setting_name, string $setting_value): Tree 3506ccdf4f0SGreg Roach { 3516ccdf4f0SGreg Roach if ($setting_value !== $this->getPreference($setting_name)) { 3526ccdf4f0SGreg Roach DB::table('gedcom_setting')->updateOrInsert([ 3536ccdf4f0SGreg Roach 'gedcom_id' => $this->id, 3546ccdf4f0SGreg Roach 'setting_name' => $setting_name, 3556ccdf4f0SGreg Roach ], [ 3566ccdf4f0SGreg Roach 'setting_value' => $setting_value, 3576ccdf4f0SGreg Roach ]); 3586ccdf4f0SGreg Roach 3596ccdf4f0SGreg Roach $this->preferences[$setting_name] = $setting_value; 3606ccdf4f0SGreg Roach 3616ccdf4f0SGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); 3626ccdf4f0SGreg Roach } 3636ccdf4f0SGreg Roach 3646ccdf4f0SGreg Roach return $this; 3656ccdf4f0SGreg Roach } 3666ccdf4f0SGreg Roach 3676ccdf4f0SGreg Roach /** 3686ccdf4f0SGreg Roach * Get the tree’s configuration settings. 3696ccdf4f0SGreg Roach * 3706ccdf4f0SGreg Roach * @param string $setting_name 3716ccdf4f0SGreg Roach * @param string $default 3726ccdf4f0SGreg Roach * 3736ccdf4f0SGreg Roach * @return string 3746ccdf4f0SGreg Roach */ 3756ccdf4f0SGreg Roach public function getPreference(string $setting_name, string $default = ''): string 3766ccdf4f0SGreg Roach { 3776ccdf4f0SGreg Roach if (empty($this->preferences)) { 3786ccdf4f0SGreg Roach $this->preferences = DB::table('gedcom_setting') 3796ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 3806ccdf4f0SGreg Roach ->pluck('setting_value', 'setting_name') 3816ccdf4f0SGreg Roach ->all(); 3826ccdf4f0SGreg Roach } 3836ccdf4f0SGreg Roach 3846ccdf4f0SGreg Roach return $this->preferences[$setting_name] ?? $default; 3856ccdf4f0SGreg Roach } 3866ccdf4f0SGreg Roach 3876ccdf4f0SGreg Roach /** 3886ccdf4f0SGreg Roach * The name of this tree 3896ccdf4f0SGreg Roach * 3906ccdf4f0SGreg Roach * @return string 3916ccdf4f0SGreg Roach */ 3926ccdf4f0SGreg Roach public function name(): string 3936ccdf4f0SGreg Roach { 3946ccdf4f0SGreg Roach return $this->name; 3956ccdf4f0SGreg Roach } 3966ccdf4f0SGreg Roach 3976ccdf4f0SGreg Roach /** 3986ccdf4f0SGreg Roach * The title of this tree 3996ccdf4f0SGreg Roach * 4006ccdf4f0SGreg Roach * @return string 4016ccdf4f0SGreg Roach */ 4026ccdf4f0SGreg Roach public function title(): string 4036ccdf4f0SGreg Roach { 4046ccdf4f0SGreg Roach return $this->title; 4056ccdf4f0SGreg Roach } 4066ccdf4f0SGreg Roach 4076ccdf4f0SGreg Roach /** 4086ccdf4f0SGreg Roach * The fact-level privacy for this tree. 4096ccdf4f0SGreg Roach * 4106ccdf4f0SGreg Roach * @return int[] 4116ccdf4f0SGreg Roach */ 4126ccdf4f0SGreg Roach public function getFactPrivacy(): array 4136ccdf4f0SGreg Roach { 4146ccdf4f0SGreg Roach return $this->fact_privacy; 4156ccdf4f0SGreg Roach } 4166ccdf4f0SGreg Roach 4176ccdf4f0SGreg Roach /** 4186ccdf4f0SGreg Roach * The individual-level privacy for this tree. 4196ccdf4f0SGreg Roach * 4206ccdf4f0SGreg Roach * @return int[] 4216ccdf4f0SGreg Roach */ 4226ccdf4f0SGreg Roach public function getIndividualPrivacy(): array 4236ccdf4f0SGreg Roach { 4246ccdf4f0SGreg Roach return $this->individual_privacy; 4256ccdf4f0SGreg Roach } 4266ccdf4f0SGreg Roach 4276ccdf4f0SGreg Roach /** 4286ccdf4f0SGreg Roach * The individual-fact-level privacy for this tree. 4296ccdf4f0SGreg Roach * 4306ccdf4f0SGreg Roach * @return int[][] 4316ccdf4f0SGreg Roach */ 4326ccdf4f0SGreg Roach public function getIndividualFactPrivacy(): array 4336ccdf4f0SGreg Roach { 4346ccdf4f0SGreg Roach return $this->individual_fact_privacy; 4356ccdf4f0SGreg Roach } 4366ccdf4f0SGreg Roach 4376ccdf4f0SGreg Roach /** 4386ccdf4f0SGreg Roach * Set the tree’s user-configuration settings. 4396ccdf4f0SGreg Roach * 4406ccdf4f0SGreg Roach * @param UserInterface $user 4416ccdf4f0SGreg Roach * @param string $setting_name 4426ccdf4f0SGreg Roach * @param string $setting_value 4436ccdf4f0SGreg Roach * 4446ccdf4f0SGreg Roach * @return $this 4456ccdf4f0SGreg Roach */ 4466ccdf4f0SGreg Roach public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree 4476ccdf4f0SGreg Roach { 4486ccdf4f0SGreg Roach if ($this->getUserPreference($user, $setting_name) !== $setting_value) { 4496ccdf4f0SGreg Roach // Update the database 4506ccdf4f0SGreg Roach DB::table('user_gedcom_setting')->updateOrInsert([ 4516ccdf4f0SGreg Roach 'gedcom_id' => $this->id(), 4526ccdf4f0SGreg Roach 'user_id' => $user->id(), 4536ccdf4f0SGreg Roach 'setting_name' => $setting_name, 4546ccdf4f0SGreg Roach ], [ 4556ccdf4f0SGreg Roach 'setting_value' => $setting_value, 4566ccdf4f0SGreg Roach ]); 4576ccdf4f0SGreg Roach 4586ccdf4f0SGreg Roach // Update the cache 4596ccdf4f0SGreg Roach $this->user_preferences[$user->id()][$setting_name] = $setting_value; 4606ccdf4f0SGreg Roach // Audit log of changes 4616ccdf4f0SGreg Roach Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this); 4626ccdf4f0SGreg Roach } 4636ccdf4f0SGreg Roach 4646ccdf4f0SGreg Roach return $this; 4656ccdf4f0SGreg Roach } 4666ccdf4f0SGreg Roach 4676ccdf4f0SGreg Roach /** 4686ccdf4f0SGreg Roach * Get the tree’s user-configuration settings. 4696ccdf4f0SGreg Roach * 4706ccdf4f0SGreg Roach * @param UserInterface $user 4716ccdf4f0SGreg Roach * @param string $setting_name 4726ccdf4f0SGreg Roach * @param string $default 4736ccdf4f0SGreg Roach * 4746ccdf4f0SGreg Roach * @return string 4756ccdf4f0SGreg Roach */ 4766ccdf4f0SGreg Roach public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string 4776ccdf4f0SGreg Roach { 4786ccdf4f0SGreg Roach // There are lots of settings, and we need to fetch lots of them on every page 4796ccdf4f0SGreg Roach // so it is quicker to fetch them all in one go. 4806ccdf4f0SGreg Roach if (!array_key_exists($user->id(), $this->user_preferences)) { 4816ccdf4f0SGreg Roach $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting') 4826ccdf4f0SGreg Roach ->where('user_id', '=', $user->id()) 4836ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 4846ccdf4f0SGreg Roach ->pluck('setting_value', 'setting_name') 4856ccdf4f0SGreg Roach ->all(); 4866ccdf4f0SGreg Roach } 4876ccdf4f0SGreg Roach 4886ccdf4f0SGreg Roach return $this->user_preferences[$user->id()][$setting_name] ?? $default; 4896ccdf4f0SGreg Roach } 4906ccdf4f0SGreg Roach 4916ccdf4f0SGreg Roach /** 4926ccdf4f0SGreg Roach * The ID of this tree 4936ccdf4f0SGreg Roach * 4946ccdf4f0SGreg Roach * @return int 4956ccdf4f0SGreg Roach */ 4966ccdf4f0SGreg Roach public function id(): int 4976ccdf4f0SGreg Roach { 4986ccdf4f0SGreg Roach return $this->id; 4996ccdf4f0SGreg Roach } 5006ccdf4f0SGreg Roach 5016ccdf4f0SGreg Roach /** 5026ccdf4f0SGreg Roach * Can a user accept changes for this tree? 5036ccdf4f0SGreg Roach * 5046ccdf4f0SGreg Roach * @param UserInterface $user 5056ccdf4f0SGreg Roach * 5066ccdf4f0SGreg Roach * @return bool 5076ccdf4f0SGreg Roach */ 5086ccdf4f0SGreg Roach public function canAcceptChanges(UserInterface $user): bool 5096ccdf4f0SGreg Roach { 5106ccdf4f0SGreg Roach return Auth::isModerator($this, $user); 5116ccdf4f0SGreg Roach } 5126ccdf4f0SGreg Roach 5136ccdf4f0SGreg Roach /** 514b78374c5SGreg Roach * Are there any pending edits for this tree, than need reviewing by a moderator. 515b78374c5SGreg Roach * 516b78374c5SGreg Roach * @return bool 517b78374c5SGreg Roach */ 518771ae10aSGreg Roach public function hasPendingEdit(): bool 519c1010edaSGreg Roach { 52015a3f100SGreg Roach return DB::table('change') 52115a3f100SGreg Roach ->where('gedcom_id', '=', $this->id) 52215a3f100SGreg Roach ->where('status', '=', 'pending') 52315a3f100SGreg Roach ->exists(); 524b78374c5SGreg Roach } 525b78374c5SGreg Roach 526b78374c5SGreg Roach /** 5276ccdf4f0SGreg Roach * Delete everything relating to a tree 5286ccdf4f0SGreg Roach * 5296ccdf4f0SGreg Roach * @return void 5306ccdf4f0SGreg Roach */ 5316ccdf4f0SGreg Roach public function delete(): void 5326ccdf4f0SGreg Roach { 5336ccdf4f0SGreg Roach // If this is the default tree, then unset it 5346ccdf4f0SGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { 5356ccdf4f0SGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 5366ccdf4f0SGreg Roach } 5376ccdf4f0SGreg Roach 5386ccdf4f0SGreg Roach $this->deleteGenealogyData(false); 5396ccdf4f0SGreg Roach 5406ccdf4f0SGreg Roach DB::table('block_setting') 5416ccdf4f0SGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 5426ccdf4f0SGreg Roach ->where('gedcom_id', '=', $this->id) 5436ccdf4f0SGreg Roach ->delete(); 5446ccdf4f0SGreg Roach DB::table('block')->where('gedcom_id', '=', $this->id)->delete(); 5456ccdf4f0SGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 5466ccdf4f0SGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete(); 5476ccdf4f0SGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete(); 5486ccdf4f0SGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete(); 5496ccdf4f0SGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete(); 5506ccdf4f0SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5516ccdf4f0SGreg Roach DB::table('log')->where('gedcom_id', '=', $this->id)->delete(); 5526ccdf4f0SGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete(); 5536ccdf4f0SGreg Roach 5546ccdf4f0SGreg Roach // After updating the database, we need to fetch a new (sorted) copy 5556ccdf4f0SGreg Roach self::$trees = []; 5566ccdf4f0SGreg Roach } 5576ccdf4f0SGreg Roach 5586ccdf4f0SGreg Roach /** 559a25f0a04SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 560a25f0a04SGreg Roach * new data. Optionally retain the media data, for when the user has been 561a25f0a04SGreg Roach * editing their data offline using an application which deletes (or does not 562a25f0a04SGreg Roach * support) media data. 563a25f0a04SGreg Roach * 564a25f0a04SGreg Roach * @param bool $keep_media 565b7e60af1SGreg Roach * 566b7e60af1SGreg Roach * @return void 567a25f0a04SGreg Roach */ 568e364afe4SGreg Roach public function deleteGenealogyData(bool $keep_media): void 569c1010edaSGreg Roach { 5701ad2dde6SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete(); 5711ad2dde6SGreg Roach DB::table('individuals')->where('i_file', '=', $this->id)->delete(); 5721ad2dde6SGreg Roach DB::table('families')->where('f_file', '=', $this->id)->delete(); 5731ad2dde6SGreg Roach DB::table('sources')->where('s_file', '=', $this->id)->delete(); 5741ad2dde6SGreg Roach DB::table('other')->where('o_file', '=', $this->id)->delete(); 5751ad2dde6SGreg Roach DB::table('places')->where('p_file', '=', $this->id)->delete(); 5761ad2dde6SGreg Roach DB::table('placelinks')->where('pl_file', '=', $this->id)->delete(); 5771ad2dde6SGreg Roach DB::table('name')->where('n_file', '=', $this->id)->delete(); 5781ad2dde6SGreg Roach DB::table('dates')->where('d_file', '=', $this->id)->delete(); 5791ad2dde6SGreg Roach DB::table('change')->where('gedcom_id', '=', $this->id)->delete(); 580a25f0a04SGreg Roach 581a25f0a04SGreg Roach if ($keep_media) { 5821ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id) 5831ad2dde6SGreg Roach ->where('l_type', '<>', 'OBJE') 5841ad2dde6SGreg Roach ->delete(); 585a25f0a04SGreg Roach } else { 5861ad2dde6SGreg Roach DB::table('link')->where('l_file', '=', $this->id)->delete(); 5871ad2dde6SGreg Roach DB::table('media_file')->where('m_file', '=', $this->id)->delete(); 5881ad2dde6SGreg Roach DB::table('media')->where('m_file', '=', $this->id)->delete(); 589a25f0a04SGreg Roach } 590a25f0a04SGreg Roach } 591a25f0a04SGreg Roach 592a25f0a04SGreg Roach /** 593a25f0a04SGreg Roach * Export the tree to a GEDCOM file 594a25f0a04SGreg Roach * 5955792757eSGreg Roach * @param resource $stream 596b7e60af1SGreg Roach * 597b7e60af1SGreg Roach * @return void 598a25f0a04SGreg Roach */ 599425af8b9SGreg Roach public function exportGedcom($stream): void 600c1010edaSGreg Roach { 601a3d8780cSGreg Roach $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8')); 60294026f20SGreg Roach 60394026f20SGreg Roach $union_families = DB::table('families') 60494026f20SGreg Roach ->where('f_file', '=', $this->id) 605a69f5655SGreg Roach ->select(['f_gedcom AS gedcom', 'f_id AS xref', new Expression('LENGTH(f_id) AS len'), new Expression('2 AS n')]); 60694026f20SGreg Roach 60794026f20SGreg Roach $union_sources = DB::table('sources') 60894026f20SGreg Roach ->where('s_file', '=', $this->id) 609a69f5655SGreg Roach ->select(['s_gedcom AS gedcom', 's_id AS xref', new Expression('LENGTH(s_id) AS len'), new Expression('3 AS n')]); 61094026f20SGreg Roach 61194026f20SGreg Roach $union_other = DB::table('other') 61294026f20SGreg Roach ->where('o_file', '=', $this->id) 61394026f20SGreg Roach ->whereNotIn('o_type', ['HEAD', 'TRLR']) 614a69f5655SGreg Roach ->select(['o_gedcom AS gedcom', 'o_id AS xref', new Expression('LENGTH(o_id) AS len'), new Expression('4 AS n')]); 61594026f20SGreg Roach 61694026f20SGreg Roach $union_media = DB::table('media') 61794026f20SGreg Roach ->where('m_file', '=', $this->id) 618a69f5655SGreg Roach ->select(['m_gedcom AS gedcom', 'm_id AS xref', new Expression('LENGTH(m_id) AS len'), new Expression('5 AS n')]); 61994026f20SGreg Roach 620e5a6b4d4SGreg Roach DB::table('individuals') 62194026f20SGreg Roach ->where('i_file', '=', $this->id) 622a69f5655SGreg Roach ->select(['i_gedcom AS gedcom', 'i_id AS xref', new Expression('LENGTH(i_id) AS len'), new Expression('1 AS n')]) 62394026f20SGreg Roach ->union($union_families) 62494026f20SGreg Roach ->union($union_sources) 62594026f20SGreg Roach ->union($union_other) 62694026f20SGreg Roach ->union($union_media) 62794026f20SGreg Roach ->orderBy('n') 62894026f20SGreg Roach ->orderBy('len') 62994026f20SGreg Roach ->orderBy('xref') 63027825e0aSGreg Roach ->chunk(1000, static function (Collection $rows) use ($stream, &$buffer): void { 63194026f20SGreg Roach foreach ($rows as $row) { 6323d7a8a4cSGreg Roach $buffer .= FunctionsExport::reformatRecord($row->gedcom); 633a25f0a04SGreg Roach if (strlen($buffer) > 65535) { 6345792757eSGreg Roach fwrite($stream, $buffer); 635a25f0a04SGreg Roach $buffer = ''; 636a25f0a04SGreg Roach } 637a25f0a04SGreg Roach } 63894026f20SGreg Roach }); 63994026f20SGreg Roach 6400f471f91SGreg Roach fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL); 641a25f0a04SGreg Roach } 642a25f0a04SGreg Roach 643a25f0a04SGreg Roach /** 644a25f0a04SGreg Roach * Import data from a gedcom file into this tree. 645a25f0a04SGreg Roach * 6466ccdf4f0SGreg Roach * @param StreamInterface $stream The GEDCOM file. 647a25f0a04SGreg Roach * @param string $filename The preferred filename, for export/download. 648a25f0a04SGreg Roach * 649b7e60af1SGreg Roach * @return void 650a25f0a04SGreg Roach */ 6516ccdf4f0SGreg Roach public function importGedcomFile(StreamInterface $stream, string $filename): void 652c1010edaSGreg Roach { 653a25f0a04SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 654a25f0a04SGreg Roach // contains complete gedcom records. This will ensure we don’t split 655a25f0a04SGreg Roach // multi-byte characters, as well as simplifying the code to import 656a25f0a04SGreg Roach // each block. 657a25f0a04SGreg Roach 658a25f0a04SGreg Roach $file_data = ''; 659a25f0a04SGreg Roach 660b7e60af1SGreg Roach $this->deleteGenealogyData((bool) $this->getPreference('keep_media')); 661a25f0a04SGreg Roach $this->setPreference('gedcom_filename', $filename); 662a25f0a04SGreg Roach $this->setPreference('imported', '0'); 663a25f0a04SGreg Roach 6646ccdf4f0SGreg Roach while (!$stream->eof()) { 6656ccdf4f0SGreg Roach $file_data .= $stream->read(65536); 666a25f0a04SGreg Roach // There is no strrpos() function that searches for substrings :-( 667a25f0a04SGreg Roach for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { 668a25f0a04SGreg Roach if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { 669a25f0a04SGreg Roach // We’ve found the last record boundary in this chunk of data 670a25f0a04SGreg Roach break; 671a25f0a04SGreg Roach } 672a25f0a04SGreg Roach } 673a25f0a04SGreg Roach if ($pos) { 6741ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6751ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6761ad2dde6SGreg Roach 'chunk_data' => substr($file_data, 0, $pos), 677c1010edaSGreg Roach ]); 6781ad2dde6SGreg Roach 679a25f0a04SGreg Roach $file_data = substr($file_data, $pos); 680a25f0a04SGreg Roach } 681a25f0a04SGreg Roach } 6821ad2dde6SGreg Roach DB::table('gedcom_chunk')->insert([ 6831ad2dde6SGreg Roach 'gedcom_id' => $this->id, 6841ad2dde6SGreg Roach 'chunk_data' => $file_data, 685c1010edaSGreg Roach ]); 686a25f0a04SGreg Roach 6876ccdf4f0SGreg Roach $stream->close(); 6886ccdf4f0SGreg Roach } 6896ccdf4f0SGreg Roach 6906ccdf4f0SGreg Roach /** 6916ccdf4f0SGreg Roach * Create a new record from GEDCOM data. 6926ccdf4f0SGreg Roach * 6936ccdf4f0SGreg Roach * @param string $gedcom 6946ccdf4f0SGreg Roach * 6956ccdf4f0SGreg Roach * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media 6966ccdf4f0SGreg Roach * @throws InvalidArgumentException 6976ccdf4f0SGreg Roach */ 6986ccdf4f0SGreg Roach public function createRecord(string $gedcom): GedcomRecord 6996ccdf4f0SGreg Roach { 7006ccdf4f0SGreg Roach if (!Str::startsWith($gedcom, '0 @@ ')) { 7016ccdf4f0SGreg Roach throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@'); 7026ccdf4f0SGreg Roach } 7036ccdf4f0SGreg Roach 7046ccdf4f0SGreg Roach $xref = $this->getNewXref(); 7056ccdf4f0SGreg Roach $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ '); 7066ccdf4f0SGreg Roach 7076ccdf4f0SGreg Roach // Create a change record 7086ccdf4f0SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 7096ccdf4f0SGreg Roach 7106ccdf4f0SGreg Roach // Create a pending change 7116ccdf4f0SGreg Roach DB::table('change')->insert([ 7126ccdf4f0SGreg Roach 'gedcom_id' => $this->id, 7136ccdf4f0SGreg Roach 'xref' => $xref, 7146ccdf4f0SGreg Roach 'old_gedcom' => '', 7156ccdf4f0SGreg Roach 'new_gedcom' => $gedcom, 7166ccdf4f0SGreg Roach 'user_id' => Auth::id(), 7176ccdf4f0SGreg Roach ]); 7186ccdf4f0SGreg Roach 7196ccdf4f0SGreg Roach // Accept this pending change 7206ccdf4f0SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 7216ccdf4f0SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 7226ccdf4f0SGreg Roach 7236ccdf4f0SGreg Roach return new GedcomRecord($xref, $gedcom, null, $this); 7246ccdf4f0SGreg Roach } 7256ccdf4f0SGreg Roach 7266ccdf4f0SGreg Roach return GedcomRecord::getInstance($xref, $this, $gedcom); 727a25f0a04SGreg Roach } 728304f20d5SGreg Roach 729304f20d5SGreg Roach /** 730b90d8accSGreg Roach * Generate a new XREF, unique across all family trees 731b90d8accSGreg Roach * 732b90d8accSGreg Roach * @return string 733b90d8accSGreg Roach */ 734771ae10aSGreg Roach public function getNewXref(): string 735c1010edaSGreg Roach { 736963fbaeeSGreg Roach // Lock the row, so that only one new XREF may be generated at a time. 737963fbaeeSGreg Roach DB::table('site_setting') 738963fbaeeSGreg Roach ->where('setting_name', '=', 'next_xref') 739963fbaeeSGreg Roach ->lockForUpdate() 740963fbaeeSGreg Roach ->get(); 741963fbaeeSGreg Roach 742a214e186SGreg Roach $prefix = 'X'; 743b90d8accSGreg Roach 744971d66c8SGreg Roach $increment = 1.0; 745b90d8accSGreg Roach do { 746963fbaeeSGreg Roach $num = (int) Site::getPreference('next_xref') + (int) $increment; 747971d66c8SGreg Roach 748971d66c8SGreg Roach // This exponential increment allows us to scan over large blocks of 749971d66c8SGreg Roach // existing data in a reasonable time. 750971d66c8SGreg Roach $increment *= 1.01; 751963fbaeeSGreg Roach 752963fbaeeSGreg Roach $xref = $prefix . $num; 753963fbaeeSGreg Roach 754963fbaeeSGreg Roach // Records may already exist with this sequence number. 755963fbaeeSGreg Roach $already_used = 756963fbaeeSGreg Roach DB::table('individuals')->where('i_id', '=', $xref)->exists() || 757963fbaeeSGreg Roach DB::table('families')->where('f_id', '=', $xref)->exists() || 758963fbaeeSGreg Roach DB::table('sources')->where('s_id', '=', $xref)->exists() || 759963fbaeeSGreg Roach DB::table('media')->where('m_id', '=', $xref)->exists() || 760963fbaeeSGreg Roach DB::table('other')->where('o_id', '=', $xref)->exists() || 761963fbaeeSGreg Roach DB::table('change')->where('xref', '=', $xref)->exists(); 762963fbaeeSGreg Roach } while ($already_used); 763963fbaeeSGreg Roach 764963fbaeeSGreg Roach Site::setPreference('next_xref', (string) $num); 765b90d8accSGreg Roach 766a214e186SGreg Roach return $xref; 767b90d8accSGreg Roach } 768b90d8accSGreg Roach 769b90d8accSGreg Roach /** 770afb591d7SGreg Roach * Create a new family from GEDCOM data. 771afb591d7SGreg Roach * 772afb591d7SGreg Roach * @param string $gedcom 773afb591d7SGreg Roach * 774afb591d7SGreg Roach * @return Family 775afb591d7SGreg Roach * @throws InvalidArgumentException 776afb591d7SGreg Roach */ 777afb591d7SGreg Roach public function createFamily(string $gedcom): GedcomRecord 778afb591d7SGreg Roach { 779bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ FAM')) { 780afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM'); 781afb591d7SGreg Roach } 782afb591d7SGreg Roach 783afb591d7SGreg Roach $xref = $this->getNewXref(); 784bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM'); 785afb591d7SGreg Roach 786afb591d7SGreg Roach // Create a change record 787e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 788afb591d7SGreg Roach 789afb591d7SGreg Roach // Create a pending change 790963fbaeeSGreg Roach DB::table('change')->insert([ 791963fbaeeSGreg Roach 'gedcom_id' => $this->id, 792963fbaeeSGreg Roach 'xref' => $xref, 793963fbaeeSGreg Roach 'old_gedcom' => '', 794963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 795963fbaeeSGreg Roach 'user_id' => Auth::id(), 796afb591d7SGreg Roach ]); 797304f20d5SGreg Roach 798304f20d5SGreg Roach // Accept this pending change 799304f20d5SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 800cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 801afb591d7SGreg Roach 802afb591d7SGreg Roach return new Family($xref, $gedcom, null, $this); 803304f20d5SGreg Roach } 804afb591d7SGreg Roach 805afb591d7SGreg Roach return new Family($xref, '', $gedcom, $this); 806afb591d7SGreg Roach } 807afb591d7SGreg Roach 808afb591d7SGreg Roach /** 809afb591d7SGreg Roach * Create a new individual from GEDCOM data. 810afb591d7SGreg Roach * 811afb591d7SGreg Roach * @param string $gedcom 812afb591d7SGreg Roach * 813afb591d7SGreg Roach * @return Individual 814afb591d7SGreg Roach * @throws InvalidArgumentException 815afb591d7SGreg Roach */ 816afb591d7SGreg Roach public function createIndividual(string $gedcom): GedcomRecord 817afb591d7SGreg Roach { 818bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ INDI')) { 819afb591d7SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI'); 820afb591d7SGreg Roach } 821afb591d7SGreg Roach 822afb591d7SGreg Roach $xref = $this->getNewXref(); 823bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI'); 824afb591d7SGreg Roach 825afb591d7SGreg Roach // Create a change record 826e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 827afb591d7SGreg Roach 828afb591d7SGreg Roach // Create a pending change 829963fbaeeSGreg Roach DB::table('change')->insert([ 830963fbaeeSGreg Roach 'gedcom_id' => $this->id, 831963fbaeeSGreg Roach 'xref' => $xref, 832963fbaeeSGreg Roach 'old_gedcom' => '', 833963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 834963fbaeeSGreg Roach 'user_id' => Auth::id(), 835afb591d7SGreg Roach ]); 836afb591d7SGreg Roach 837afb591d7SGreg Roach // Accept this pending change 838afb591d7SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 839afb591d7SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 840afb591d7SGreg Roach 841afb591d7SGreg Roach return new Individual($xref, $gedcom, null, $this); 842afb591d7SGreg Roach } 843afb591d7SGreg Roach 844afb591d7SGreg Roach return new Individual($xref, '', $gedcom, $this); 845304f20d5SGreg Roach } 8468586983fSGreg Roach 8478586983fSGreg Roach /** 84820b58d20SGreg Roach * Create a new media object from GEDCOM data. 84920b58d20SGreg Roach * 85020b58d20SGreg Roach * @param string $gedcom 85120b58d20SGreg Roach * 85220b58d20SGreg Roach * @return Media 85320b58d20SGreg Roach * @throws InvalidArgumentException 85420b58d20SGreg Roach */ 85520b58d20SGreg Roach public function createMediaObject(string $gedcom): Media 85620b58d20SGreg Roach { 857bec87e94SGreg Roach if (!Str::startsWith($gedcom, '0 @@ OBJE')) { 85820b58d20SGreg Roach throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE'); 85920b58d20SGreg Roach } 86020b58d20SGreg Roach 86120b58d20SGreg Roach $xref = $this->getNewXref(); 862bec87e94SGreg Roach $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE'); 86320b58d20SGreg Roach 86420b58d20SGreg Roach // Create a change record 865e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 86620b58d20SGreg Roach 86720b58d20SGreg Roach // Create a pending change 868963fbaeeSGreg Roach DB::table('change')->insert([ 869963fbaeeSGreg Roach 'gedcom_id' => $this->id, 870963fbaeeSGreg Roach 'xref' => $xref, 871963fbaeeSGreg Roach 'old_gedcom' => '', 872963fbaeeSGreg Roach 'new_gedcom' => $gedcom, 873963fbaeeSGreg Roach 'user_id' => Auth::id(), 87420b58d20SGreg Roach ]); 87520b58d20SGreg Roach 87620b58d20SGreg Roach // Accept this pending change 87720b58d20SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 87820b58d20SGreg Roach FunctionsImport::acceptAllChanges($xref, $this); 87920b58d20SGreg Roach 88020b58d20SGreg Roach return new Media($xref, $gedcom, null, $this); 88120b58d20SGreg Roach } 88220b58d20SGreg Roach 88320b58d20SGreg Roach return new Media($xref, '', $gedcom, $this); 88420b58d20SGreg Roach } 88520b58d20SGreg Roach 88620b58d20SGreg Roach /** 8878586983fSGreg Roach * What is the most significant individual in this tree. 8888586983fSGreg Roach * 889e5a6b4d4SGreg Roach * @param UserInterface $user 8908586983fSGreg Roach * 8918586983fSGreg Roach * @return Individual 8928586983fSGreg Roach */ 893e5a6b4d4SGreg Roach public function significantIndividual(UserInterface $user): Individual 894c1010edaSGreg Roach { 8958f9b0fb2SGreg Roach $individual = null; 8968586983fSGreg Roach 8978f9b0fb2SGreg Roach if ($this->getUserPreference($user, 'rootid') !== '') { 8988586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this); 8998586983fSGreg Roach } 9008f9b0fb2SGreg Roach 9018f9b0fb2SGreg Roach if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') { 9028586983fSGreg Roach $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); 9038586983fSGreg Roach } 9048f9b0fb2SGreg Roach 905bec87e94SGreg Roach if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') { 9068586983fSGreg Roach $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); 9078586983fSGreg Roach } 9088f9b0fb2SGreg Roach if ($individual === null) { 9098f9b0fb2SGreg Roach $xref = (string) DB::table('individuals') 9108f9b0fb2SGreg Roach ->where('i_file', '=', $this->id()) 9118f9b0fb2SGreg Roach ->min('i_id'); 912769d7d6eSGreg Roach 913769d7d6eSGreg Roach $individual = Individual::getInstance($xref, $this); 9145fe1add5SGreg Roach } 9158f9b0fb2SGreg Roach if ($individual === null) { 9165fe1add5SGreg Roach // always return a record 9175fe1add5SGreg Roach $individual = new Individual('I', '0 @I@ INDI', null, $this); 9185fe1add5SGreg Roach } 9195fe1add5SGreg Roach 9205fe1add5SGreg Roach return $individual; 9215fe1add5SGreg Roach } 9221df7ae39SGreg Roach 923*85a166d8SGreg Roach /** 924*85a166d8SGreg Roach * Where do we store our media files. 925*85a166d8SGreg Roach * 926*85a166d8SGreg Roach * @return FilesystemInterface 927*85a166d8SGreg Roach */ 9281df7ae39SGreg Roach public function mediaFilesystem(): FilesystemInterface 9291df7ae39SGreg Roach { 930456d0d35SGreg Roach $media_dir = $this->getPreference('MEDIA_DIRECTORY', 'media/'); 931456d0d35SGreg Roach $filesystem = app(FilesystemInterface::class); 932456d0d35SGreg Roach $adapter = new ChrootAdapter($filesystem, $media_dir); 933456d0d35SGreg Roach 934456d0d35SGreg Roach return new Filesystem($adapter); 9351df7ae39SGreg Roach } 936a25f0a04SGreg Roach} 937