xref: /webtrees/app/Tree.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport;
2301461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2401461f86SGreg Roachuse Illuminate\Database\Query\Builder;
2501461f86SGreg Roachuse Illuminate\Database\Query\JoinClause;
2694026f20SGreg Roachuse Illuminate\Support\Collection;
27bec87e94SGreg Roachuse Illuminate\Support\Str;
28afb591d7SGreg Roachuse InvalidArgumentException;
29a25f0a04SGreg Roachuse PDOException;
30*6ccdf4f0SGreg Roachuse Psr\Http\Message\StreamInterface;
318b67c11aSGreg Roachuse stdClass;
32a25f0a04SGreg Roach
33a25f0a04SGreg Roach/**
3476692c8bSGreg Roach * Provide an interface to the wt_gedcom table.
35a25f0a04SGreg Roach */
36c1010edaSGreg Roachclass Tree
37c1010edaSGreg Roach{
38061b43d7SGreg Roach    private const RESN_PRIVACY = [
39061b43d7SGreg Roach        'none'         => Auth::PRIV_PRIVATE,
40061b43d7SGreg Roach        'privacy'      => Auth::PRIV_USER,
41061b43d7SGreg Roach        'confidential' => Auth::PRIV_NONE,
42061b43d7SGreg Roach        'hidden'       => Auth::PRIV_HIDE,
43061b43d7SGreg Roach    ];
44*6ccdf4f0SGreg Roach    /** @var Tree[] All trees that we have permission to see, indexed by ID. */
45*6ccdf4f0SGreg Roach    public static $trees = [];
46*6ccdf4f0SGreg Roach    /** @var int The tree's ID number */
47*6ccdf4f0SGreg Roach    private $id;
48*6ccdf4f0SGreg Roach    /** @var string The tree's name */
49*6ccdf4f0SGreg Roach    private $name;
50*6ccdf4f0SGreg Roach    /** @var string The tree's title */
51*6ccdf4f0SGreg Roach    private $title;
52*6ccdf4f0SGreg Roach    /** @var int[] Default access rules for facts in this tree */
53*6ccdf4f0SGreg Roach    private $fact_privacy;
54*6ccdf4f0SGreg Roach    /** @var int[] Default access rules for individuals in this tree */
55*6ccdf4f0SGreg Roach    private $individual_privacy;
56*6ccdf4f0SGreg Roach    /** @var integer[][] Default access rules for individual facts in this tree */
57*6ccdf4f0SGreg Roach    private $individual_fact_privacy;
58*6ccdf4f0SGreg Roach    /** @var string[] Cached copy of the wt_gedcom_setting table. */
59*6ccdf4f0SGreg Roach    private $preferences = [];
60*6ccdf4f0SGreg Roach    /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */
61*6ccdf4f0SGreg Roach    private $user_preferences = [];
62061b43d7SGreg Roach
63a25f0a04SGreg Roach    /**
64a25f0a04SGreg Roach     * Create a tree object. This is a private constructor - it can only
65a25f0a04SGreg Roach     * be called from Tree::getAll() to ensure proper initialisation.
66a25f0a04SGreg Roach     *
6772cf66d4SGreg Roach     * @param int    $id
68aa6f03bbSGreg Roach     * @param string $name
69cc13d6d8SGreg Roach     * @param string $title
70a25f0a04SGreg Roach     */
71cc13d6d8SGreg Roach    private function __construct($id, $name, $title)
72c1010edaSGreg Roach    {
7372cf66d4SGreg Roach        $this->id                      = $id;
74aa6f03bbSGreg Roach        $this->name                    = $name;
75cc13d6d8SGreg Roach        $this->title                   = $title;
7613abd6f3SGreg Roach        $this->fact_privacy            = [];
7713abd6f3SGreg Roach        $this->individual_privacy      = [];
7813abd6f3SGreg Roach        $this->individual_fact_privacy = [];
79518bbdc1SGreg Roach
80518bbdc1SGreg Roach        // Load the privacy settings for this tree
81061b43d7SGreg Roach        $rows = DB::table('default_resn')
82061b43d7SGreg Roach            ->where('gedcom_id', '=', $this->id)
83061b43d7SGreg Roach            ->get();
84518bbdc1SGreg Roach
85518bbdc1SGreg Roach        foreach ($rows as $row) {
86061b43d7SGreg Roach            // Convert GEDCOM privacy restriction to a webtrees access level.
87061b43d7SGreg Roach            $row->resn = self::RESN_PRIVACY[$row->resn];
88061b43d7SGreg Roach
89518bbdc1SGreg Roach            if ($row->xref !== null) {
90518bbdc1SGreg Roach                if ($row->tag_type !== null) {
91518bbdc1SGreg Roach                    $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn;
92518bbdc1SGreg Roach                } else {
93518bbdc1SGreg Roach                    $this->individual_privacy[$row->xref] = (int) $row->resn;
94518bbdc1SGreg Roach                }
95518bbdc1SGreg Roach            } else {
96518bbdc1SGreg Roach                $this->fact_privacy[$row->tag_type] = (int) $row->resn;
97518bbdc1SGreg Roach            }
98518bbdc1SGreg Roach        }
99a25f0a04SGreg Roach    }
100a25f0a04SGreg Roach
101a25f0a04SGreg Roach    /**
102*6ccdf4f0SGreg Roach     * Find the tree with a specific ID.
103a25f0a04SGreg Roach     *
104*6ccdf4f0SGreg Roach     * @param int $tree_id
105*6ccdf4f0SGreg Roach     *
106*6ccdf4f0SGreg Roach     * @return Tree
107a25f0a04SGreg Roach     */
108*6ccdf4f0SGreg Roach    public static function findById(int $tree_id): Tree
109c1010edaSGreg Roach    {
110*6ccdf4f0SGreg Roach        return self::getAll()[$tree_id];
111a25f0a04SGreg Roach    }
112a25f0a04SGreg Roach
113a25f0a04SGreg Roach    /**
114*6ccdf4f0SGreg Roach     * Fetch all the trees that we have permission to access.
115a25f0a04SGreg Roach     *
116*6ccdf4f0SGreg Roach     * @return Tree[]
117a25f0a04SGreg Roach     */
118*6ccdf4f0SGreg Roach    public static function getAll(): array
119c1010edaSGreg Roach    {
120*6ccdf4f0SGreg Roach        if (empty(self::$trees)) {
121*6ccdf4f0SGreg Roach            self::$trees = self::all()->all();
122a25f0a04SGreg Roach        }
123a25f0a04SGreg Roach
124*6ccdf4f0SGreg Roach        return self::$trees;
125a25f0a04SGreg Roach    }
126a25f0a04SGreg Roach
127a25f0a04SGreg Roach    /**
1288b67c11aSGreg Roach     * All the trees that we have permission to access.
129a25f0a04SGreg Roach     *
13054c7f8dfSGreg Roach     * @return Collection
13154c7f8dfSGreg Roach     * @return Tree[]
132a25f0a04SGreg Roach     */
1338b67c11aSGreg Roach    public static function all(): Collection
134c1010edaSGreg Roach    {
1350b5fd0a6SGreg Roach        return app('cache.array')->rememberForever(__CLASS__, static function (): Collection {
13601461f86SGreg Roach            // Admins see all trees
13701461f86SGreg Roach            $query = DB::table('gedcom')
1380b5fd0a6SGreg Roach                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
13901461f86SGreg Roach                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
14001461f86SGreg Roach                        ->where('gedcom_setting.setting_name', '=', 'title');
14101461f86SGreg Roach                })
14201461f86SGreg Roach                ->where('gedcom.gedcom_id', '>', 0)
14301461f86SGreg Roach                ->select([
14401461f86SGreg Roach                    'gedcom.gedcom_id AS tree_id',
14501461f86SGreg Roach                    'gedcom.gedcom_name AS tree_name',
14601461f86SGreg Roach                    'gedcom_setting.setting_value AS tree_title',
14701461f86SGreg Roach                ])
14801461f86SGreg Roach                ->orderBy('gedcom.sort_order')
14901461f86SGreg Roach                ->orderBy('gedcom_setting.setting_value');
15001461f86SGreg Roach
15132f20c14SGreg Roach            // Non-admins may not see all trees
15232f20c14SGreg Roach            if (!Auth::isAdmin()) {
15301461f86SGreg Roach                $query
1540b5fd0a6SGreg Roach                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
15536357577SGreg Roach                        $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
15601461f86SGreg Roach                            ->where('gs2.setting_name', '=', 'imported');
15736357577SGreg Roach                    })
1580b5fd0a6SGreg Roach                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
15901461f86SGreg Roach                        $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
16001461f86SGreg Roach                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
16101461f86SGreg Roach                    })
1620b5fd0a6SGreg Roach                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
16301461f86SGreg Roach                        $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
16401461f86SGreg Roach                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
16501461f86SGreg Roach                            ->where('user_gedcom_setting.setting_name', '=', 'canedit');
16601461f86SGreg Roach                    })
1670b5fd0a6SGreg Roach                    ->where(static function (Builder $query): void {
16801461f86SGreg Roach                        $query
16901461f86SGreg Roach                            // Managers
17001461f86SGreg Roach                            ->where('user_gedcom_setting.setting_value', '=', 'admin')
17101461f86SGreg Roach                            // Members
1720b5fd0a6SGreg Roach                            ->orWhere(static function (Builder $query): void {
17301461f86SGreg Roach                                $query
17401461f86SGreg Roach                                    ->where('gs2.setting_value', '=', '1')
17501461f86SGreg Roach                                    ->where('gs3.setting_value', '=', '1')
17601461f86SGreg Roach                                    ->where('user_gedcom_setting.setting_value', '<>', 'none');
17701461f86SGreg Roach                            })
1788b67c11aSGreg Roach                            // Public trees
1790b5fd0a6SGreg Roach                            ->orWhere(static function (Builder $query): void {
18001461f86SGreg Roach                                $query
18101461f86SGreg Roach                                    ->where('gs2.setting_value', '=', '1')
18236357577SGreg Roach                                    ->where('gs3.setting_value', '<>', '1');
18301461f86SGreg Roach                            });
18401461f86SGreg Roach                    });
18501461f86SGreg Roach            }
18601461f86SGreg Roach
1878b67c11aSGreg Roach            return $query
1888b67c11aSGreg Roach                ->get()
1890b5fd0a6SGreg Roach                ->mapWithKeys(static function (stdClass $row): array {
1908b67c11aSGreg Roach                    return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)];
1918b67c11aSGreg Roach                });
1928b67c11aSGreg Roach        });
193a25f0a04SGreg Roach    }
1948b67c11aSGreg Roach
1958b67c11aSGreg Roach    /**
196a25f0a04SGreg Roach     * Create arguments to select_edit_control()
197a25f0a04SGreg Roach     * Note - these will be escaped later
198a25f0a04SGreg Roach     *
199a25f0a04SGreg Roach     * @return string[]
200a25f0a04SGreg Roach     */
201771ae10aSGreg Roach    public static function getIdList(): array
202c1010edaSGreg Roach    {
20313abd6f3SGreg Roach        $list = [];
204a25f0a04SGreg Roach        foreach (self::getAll() as $tree) {
20572cf66d4SGreg Roach            $list[$tree->id] = $tree->title;
206a25f0a04SGreg Roach        }
207a25f0a04SGreg Roach
208a25f0a04SGreg Roach        return $list;
209a25f0a04SGreg Roach    }
210a25f0a04SGreg Roach
211a25f0a04SGreg Roach    /**
212a25f0a04SGreg Roach     * Create arguments to select_edit_control()
213a25f0a04SGreg Roach     * Note - these will be escaped later
214a25f0a04SGreg Roach     *
215a25f0a04SGreg Roach     * @return string[]
216a25f0a04SGreg Roach     */
217771ae10aSGreg Roach    public static function getNameList(): array
218c1010edaSGreg Roach    {
21913abd6f3SGreg Roach        $list = [];
220a25f0a04SGreg Roach        foreach (self::getAll() as $tree) {
221a25f0a04SGreg Roach            $list[$tree->name] = $tree->title;
222a25f0a04SGreg Roach        }
223a25f0a04SGreg Roach
224a25f0a04SGreg Roach        return $list;
225a25f0a04SGreg Roach    }
226a25f0a04SGreg Roach
227a25f0a04SGreg Roach    /**
228a25f0a04SGreg Roach     * Create a new tree
229a25f0a04SGreg Roach     *
230a25f0a04SGreg Roach     * @param string $tree_name
231a25f0a04SGreg Roach     * @param string $tree_title
232a25f0a04SGreg Roach     *
233a25f0a04SGreg Roach     * @return Tree
234a25f0a04SGreg Roach     */
235771ae10aSGreg Roach    public static function create(string $tree_name, string $tree_title): Tree
236c1010edaSGreg Roach    {
237a25f0a04SGreg Roach        try {
238a25f0a04SGreg Roach            // Create a new tree
23901461f86SGreg Roach            DB::table('gedcom')->insert([
24001461f86SGreg Roach                'gedcom_name' => $tree_name,
24101461f86SGreg Roach            ]);
2424a86d714SGreg Roach
243061b43d7SGreg Roach            $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
24432f20c14SGreg Roach
24532f20c14SGreg Roach            $tree = new self($tree_id, $tree_name, $tree_title);
246a25f0a04SGreg Roach        } catch (PDOException $ex) {
247a25f0a04SGreg Roach            // A tree with that name already exists?
248ef2fd529SGreg Roach            return self::findByName($tree_name);
249a25f0a04SGreg Roach        }
250a25f0a04SGreg Roach
251a25f0a04SGreg Roach        $tree->setPreference('imported', '0');
252a25f0a04SGreg Roach        $tree->setPreference('title', $tree_title);
253a25f0a04SGreg Roach
2541507cbcaSGreg Roach        // Set preferences from default tree
255061b43d7SGreg Roach        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
256061b43d7SGreg Roach            ['gedcom_id', 'setting_name', 'setting_value'],
257*6ccdf4f0SGreg Roach            static function (Builder $query) use ($tree_id): void {
258061b43d7SGreg Roach                $query
259061b43d7SGreg Roach                    ->select([DB::raw($tree_id), 'setting_name', 'setting_value'])
260061b43d7SGreg Roach                    ->from('gedcom_setting')
261061b43d7SGreg Roach                    ->where('gedcom_id', '=', -1);
262061b43d7SGreg Roach            }
263061b43d7SGreg Roach        );
2641507cbcaSGreg Roach
265061b43d7SGreg Roach        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
266061b43d7SGreg Roach            ['gedcom_id', 'tag_type', 'resn'],
267061b43d7SGreg Roach            function (Builder $query) use ($tree_id): void {
268061b43d7SGreg Roach                $query
269061b43d7SGreg Roach                    ->select([DB::raw($tree_id), 'tag_type', 'resn'])
270061b43d7SGreg Roach                    ->from('default_resn')
271061b43d7SGreg Roach                    ->where('gedcom_id', '=', -1);
272061b43d7SGreg Roach            }
273061b43d7SGreg Roach        );
2741507cbcaSGreg Roach
275a25f0a04SGreg Roach        // Gedcom and privacy settings
27676f666f4SGreg Roach        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
27776f666f4SGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
278a25f0a04SGreg Roach        $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language
279e364afe4SGreg Roach
280a25f0a04SGreg Roach        switch (WT_LOCALE) {
281a25f0a04SGreg Roach            case 'es':
282a25f0a04SGreg Roach                $tree->setPreference('SURNAME_TRADITION', 'spanish');
283a25f0a04SGreg Roach                break;
284a25f0a04SGreg Roach            case 'is':
285a25f0a04SGreg Roach                $tree->setPreference('SURNAME_TRADITION', 'icelandic');
286a25f0a04SGreg Roach                break;
287a25f0a04SGreg Roach            case 'lt':
288a25f0a04SGreg Roach                $tree->setPreference('SURNAME_TRADITION', 'lithuanian');
289a25f0a04SGreg Roach                break;
290a25f0a04SGreg Roach            case 'pl':
291a25f0a04SGreg Roach                $tree->setPreference('SURNAME_TRADITION', 'polish');
292a25f0a04SGreg Roach                break;
293a25f0a04SGreg Roach            case 'pt':
294a25f0a04SGreg Roach            case 'pt-BR':
295a25f0a04SGreg Roach                $tree->setPreference('SURNAME_TRADITION', 'portuguese');
296a25f0a04SGreg Roach                break;
297a25f0a04SGreg Roach            default:
298a25f0a04SGreg Roach                $tree->setPreference('SURNAME_TRADITION', 'paternal');
299a25f0a04SGreg Roach                break;
300a25f0a04SGreg Roach        }
301a25f0a04SGreg Roach
302a25f0a04SGreg Roach        // Genealogy data
303a25f0a04SGreg Roach        // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...
304bbb76c12SGreg Roach        /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */
305bbb76c12SGreg Roach        $john_doe = I18N::translate('John /DOE/');
30677e70a22SGreg Roach        $note     = I18N::translate('Edit this individual and replace their details with your own.');
307061b43d7SGreg Roach        $gedcom   = "0 HEAD\n1 CHAR UTF-8\n0 @X1@ INDI\n1 NAME {$john_doe}\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE {$note}\n0 TRLR\n";
308061b43d7SGreg Roach
309061b43d7SGreg Roach        DB::table('gedcom_chunk')->insert([
310061b43d7SGreg Roach            'gedcom_id'  => $tree_id,
311061b43d7SGreg Roach            'chunk_data' => $gedcom,
31213abd6f3SGreg Roach        ]);
313a25f0a04SGreg Roach
314a25f0a04SGreg Roach        // Update our cache
31572cf66d4SGreg Roach        self::$trees[$tree->id] = $tree;
316a25f0a04SGreg Roach
317a25f0a04SGreg Roach        return $tree;
318a25f0a04SGreg Roach    }
319a25f0a04SGreg Roach
320a25f0a04SGreg Roach    /**
321*6ccdf4f0SGreg Roach     * Find the tree with a specific name.
322*6ccdf4f0SGreg Roach     *
323*6ccdf4f0SGreg Roach     * @param string $tree_name
324*6ccdf4f0SGreg Roach     *
325*6ccdf4f0SGreg Roach     * @return Tree|null
326*6ccdf4f0SGreg Roach     */
327*6ccdf4f0SGreg Roach    public static function findByName($tree_name): ?Tree
328*6ccdf4f0SGreg Roach    {
329*6ccdf4f0SGreg Roach        foreach (self::getAll() as $tree) {
330*6ccdf4f0SGreg Roach            if ($tree->name === $tree_name) {
331*6ccdf4f0SGreg Roach                return $tree;
332*6ccdf4f0SGreg Roach            }
333*6ccdf4f0SGreg Roach        }
334*6ccdf4f0SGreg Roach
335*6ccdf4f0SGreg Roach        return null;
336*6ccdf4f0SGreg Roach    }
337*6ccdf4f0SGreg Roach
338*6ccdf4f0SGreg Roach    /**
339*6ccdf4f0SGreg Roach     * Set the tree’s configuration settings.
340*6ccdf4f0SGreg Roach     *
341*6ccdf4f0SGreg Roach     * @param string $setting_name
342*6ccdf4f0SGreg Roach     * @param string $setting_value
343*6ccdf4f0SGreg Roach     *
344*6ccdf4f0SGreg Roach     * @return $this
345*6ccdf4f0SGreg Roach     */
346*6ccdf4f0SGreg Roach    public function setPreference(string $setting_name, string $setting_value): Tree
347*6ccdf4f0SGreg Roach    {
348*6ccdf4f0SGreg Roach        if ($setting_value !== $this->getPreference($setting_name)) {
349*6ccdf4f0SGreg Roach            DB::table('gedcom_setting')->updateOrInsert([
350*6ccdf4f0SGreg Roach                'gedcom_id'    => $this->id,
351*6ccdf4f0SGreg Roach                'setting_name' => $setting_name,
352*6ccdf4f0SGreg Roach            ], [
353*6ccdf4f0SGreg Roach                'setting_value' => $setting_value,
354*6ccdf4f0SGreg Roach            ]);
355*6ccdf4f0SGreg Roach
356*6ccdf4f0SGreg Roach            $this->preferences[$setting_name] = $setting_value;
357*6ccdf4f0SGreg Roach
358*6ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
359*6ccdf4f0SGreg Roach        }
360*6ccdf4f0SGreg Roach
361*6ccdf4f0SGreg Roach        return $this;
362*6ccdf4f0SGreg Roach    }
363*6ccdf4f0SGreg Roach
364*6ccdf4f0SGreg Roach    /**
365*6ccdf4f0SGreg Roach     * Get the tree’s configuration settings.
366*6ccdf4f0SGreg Roach     *
367*6ccdf4f0SGreg Roach     * @param string $setting_name
368*6ccdf4f0SGreg Roach     * @param string $default
369*6ccdf4f0SGreg Roach     *
370*6ccdf4f0SGreg Roach     * @return string
371*6ccdf4f0SGreg Roach     */
372*6ccdf4f0SGreg Roach    public function getPreference(string $setting_name, string $default = ''): string
373*6ccdf4f0SGreg Roach    {
374*6ccdf4f0SGreg Roach        if (empty($this->preferences)) {
375*6ccdf4f0SGreg Roach            $this->preferences = DB::table('gedcom_setting')
376*6ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
377*6ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
378*6ccdf4f0SGreg Roach                ->all();
379*6ccdf4f0SGreg Roach        }
380*6ccdf4f0SGreg Roach
381*6ccdf4f0SGreg Roach        return $this->preferences[$setting_name] ?? $default;
382*6ccdf4f0SGreg Roach    }
383*6ccdf4f0SGreg Roach
384*6ccdf4f0SGreg Roach    /**
385*6ccdf4f0SGreg Roach     * The name of this tree
386*6ccdf4f0SGreg Roach     *
387*6ccdf4f0SGreg Roach     * @return string
388*6ccdf4f0SGreg Roach     */
389*6ccdf4f0SGreg Roach    public function name(): string
390*6ccdf4f0SGreg Roach    {
391*6ccdf4f0SGreg Roach        return $this->name;
392*6ccdf4f0SGreg Roach    }
393*6ccdf4f0SGreg Roach
394*6ccdf4f0SGreg Roach    /**
395*6ccdf4f0SGreg Roach     * The title of this tree
396*6ccdf4f0SGreg Roach     *
397*6ccdf4f0SGreg Roach     * @return string
398*6ccdf4f0SGreg Roach     */
399*6ccdf4f0SGreg Roach    public function title(): string
400*6ccdf4f0SGreg Roach    {
401*6ccdf4f0SGreg Roach        return $this->title;
402*6ccdf4f0SGreg Roach    }
403*6ccdf4f0SGreg Roach
404*6ccdf4f0SGreg Roach    /**
405*6ccdf4f0SGreg Roach     * The fact-level privacy for this tree.
406*6ccdf4f0SGreg Roach     *
407*6ccdf4f0SGreg Roach     * @return int[]
408*6ccdf4f0SGreg Roach     */
409*6ccdf4f0SGreg Roach    public function getFactPrivacy(): array
410*6ccdf4f0SGreg Roach    {
411*6ccdf4f0SGreg Roach        return $this->fact_privacy;
412*6ccdf4f0SGreg Roach    }
413*6ccdf4f0SGreg Roach
414*6ccdf4f0SGreg Roach    /**
415*6ccdf4f0SGreg Roach     * The individual-level privacy for this tree.
416*6ccdf4f0SGreg Roach     *
417*6ccdf4f0SGreg Roach     * @return int[]
418*6ccdf4f0SGreg Roach     */
419*6ccdf4f0SGreg Roach    public function getIndividualPrivacy(): array
420*6ccdf4f0SGreg Roach    {
421*6ccdf4f0SGreg Roach        return $this->individual_privacy;
422*6ccdf4f0SGreg Roach    }
423*6ccdf4f0SGreg Roach
424*6ccdf4f0SGreg Roach    /**
425*6ccdf4f0SGreg Roach     * The individual-fact-level privacy for this tree.
426*6ccdf4f0SGreg Roach     *
427*6ccdf4f0SGreg Roach     * @return int[][]
428*6ccdf4f0SGreg Roach     */
429*6ccdf4f0SGreg Roach    public function getIndividualFactPrivacy(): array
430*6ccdf4f0SGreg Roach    {
431*6ccdf4f0SGreg Roach        return $this->individual_fact_privacy;
432*6ccdf4f0SGreg Roach    }
433*6ccdf4f0SGreg Roach
434*6ccdf4f0SGreg Roach    /**
435*6ccdf4f0SGreg Roach     * Set the tree’s user-configuration settings.
436*6ccdf4f0SGreg Roach     *
437*6ccdf4f0SGreg Roach     * @param UserInterface $user
438*6ccdf4f0SGreg Roach     * @param string        $setting_name
439*6ccdf4f0SGreg Roach     * @param string        $setting_value
440*6ccdf4f0SGreg Roach     *
441*6ccdf4f0SGreg Roach     * @return $this
442*6ccdf4f0SGreg Roach     */
443*6ccdf4f0SGreg Roach    public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree
444*6ccdf4f0SGreg Roach    {
445*6ccdf4f0SGreg Roach        if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
446*6ccdf4f0SGreg Roach            // Update the database
447*6ccdf4f0SGreg Roach            DB::table('user_gedcom_setting')->updateOrInsert([
448*6ccdf4f0SGreg Roach                'gedcom_id'    => $this->id(),
449*6ccdf4f0SGreg Roach                'user_id'      => $user->id(),
450*6ccdf4f0SGreg Roach                'setting_name' => $setting_name,
451*6ccdf4f0SGreg Roach            ], [
452*6ccdf4f0SGreg Roach                'setting_value' => $setting_value,
453*6ccdf4f0SGreg Roach            ]);
454*6ccdf4f0SGreg Roach
455*6ccdf4f0SGreg Roach            // Update the cache
456*6ccdf4f0SGreg Roach            $this->user_preferences[$user->id()][$setting_name] = $setting_value;
457*6ccdf4f0SGreg Roach            // Audit log of changes
458*6ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this);
459*6ccdf4f0SGreg Roach        }
460*6ccdf4f0SGreg Roach
461*6ccdf4f0SGreg Roach        return $this;
462*6ccdf4f0SGreg Roach    }
463*6ccdf4f0SGreg Roach
464*6ccdf4f0SGreg Roach    /**
465*6ccdf4f0SGreg Roach     * Get the tree’s user-configuration settings.
466*6ccdf4f0SGreg Roach     *
467*6ccdf4f0SGreg Roach     * @param UserInterface $user
468*6ccdf4f0SGreg Roach     * @param string        $setting_name
469*6ccdf4f0SGreg Roach     * @param string        $default
470*6ccdf4f0SGreg Roach     *
471*6ccdf4f0SGreg Roach     * @return string
472*6ccdf4f0SGreg Roach     */
473*6ccdf4f0SGreg Roach    public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string
474*6ccdf4f0SGreg Roach    {
475*6ccdf4f0SGreg Roach        // There are lots of settings, and we need to fetch lots of them on every page
476*6ccdf4f0SGreg Roach        // so it is quicker to fetch them all in one go.
477*6ccdf4f0SGreg Roach        if (!array_key_exists($user->id(), $this->user_preferences)) {
478*6ccdf4f0SGreg Roach            $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting')
479*6ccdf4f0SGreg Roach                ->where('user_id', '=', $user->id())
480*6ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
481*6ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
482*6ccdf4f0SGreg Roach                ->all();
483*6ccdf4f0SGreg Roach        }
484*6ccdf4f0SGreg Roach
485*6ccdf4f0SGreg Roach        return $this->user_preferences[$user->id()][$setting_name] ?? $default;
486*6ccdf4f0SGreg Roach    }
487*6ccdf4f0SGreg Roach
488*6ccdf4f0SGreg Roach    /**
489*6ccdf4f0SGreg Roach     * The ID of this tree
490*6ccdf4f0SGreg Roach     *
491*6ccdf4f0SGreg Roach     * @return int
492*6ccdf4f0SGreg Roach     */
493*6ccdf4f0SGreg Roach    public function id(): int
494*6ccdf4f0SGreg Roach    {
495*6ccdf4f0SGreg Roach        return $this->id;
496*6ccdf4f0SGreg Roach    }
497*6ccdf4f0SGreg Roach
498*6ccdf4f0SGreg Roach    /**
499*6ccdf4f0SGreg Roach     * Can a user accept changes for this tree?
500*6ccdf4f0SGreg Roach     *
501*6ccdf4f0SGreg Roach     * @param UserInterface $user
502*6ccdf4f0SGreg Roach     *
503*6ccdf4f0SGreg Roach     * @return bool
504*6ccdf4f0SGreg Roach     */
505*6ccdf4f0SGreg Roach    public function canAcceptChanges(UserInterface $user): bool
506*6ccdf4f0SGreg Roach    {
507*6ccdf4f0SGreg Roach        return Auth::isModerator($this, $user);
508*6ccdf4f0SGreg Roach    }
509*6ccdf4f0SGreg Roach
510*6ccdf4f0SGreg Roach    /**
511b78374c5SGreg Roach     * Are there any pending edits for this tree, than need reviewing by a moderator.
512b78374c5SGreg Roach     *
513b78374c5SGreg Roach     * @return bool
514b78374c5SGreg Roach     */
515771ae10aSGreg Roach    public function hasPendingEdit(): bool
516c1010edaSGreg Roach    {
51715a3f100SGreg Roach        return DB::table('change')
51815a3f100SGreg Roach            ->where('gedcom_id', '=', $this->id)
51915a3f100SGreg Roach            ->where('status', '=', 'pending')
52015a3f100SGreg Roach            ->exists();
521b78374c5SGreg Roach    }
522b78374c5SGreg Roach
523b78374c5SGreg Roach    /**
524*6ccdf4f0SGreg Roach     * Delete everything relating to a tree
525*6ccdf4f0SGreg Roach     *
526*6ccdf4f0SGreg Roach     * @return void
527*6ccdf4f0SGreg Roach     */
528*6ccdf4f0SGreg Roach    public function delete(): void
529*6ccdf4f0SGreg Roach    {
530*6ccdf4f0SGreg Roach        // If this is the default tree, then unset it
531*6ccdf4f0SGreg Roach        if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) {
532*6ccdf4f0SGreg Roach            Site::setPreference('DEFAULT_GEDCOM', '');
533*6ccdf4f0SGreg Roach        }
534*6ccdf4f0SGreg Roach
535*6ccdf4f0SGreg Roach        $this->deleteGenealogyData(false);
536*6ccdf4f0SGreg Roach
537*6ccdf4f0SGreg Roach        DB::table('block_setting')
538*6ccdf4f0SGreg Roach            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
539*6ccdf4f0SGreg Roach            ->where('gedcom_id', '=', $this->id)
540*6ccdf4f0SGreg Roach            ->delete();
541*6ccdf4f0SGreg Roach        DB::table('block')->where('gedcom_id', '=', $this->id)->delete();
542*6ccdf4f0SGreg Roach        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
543*6ccdf4f0SGreg Roach        DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
544*6ccdf4f0SGreg Roach        DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete();
545*6ccdf4f0SGreg Roach        DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete();
546*6ccdf4f0SGreg Roach        DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete();
547*6ccdf4f0SGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
548*6ccdf4f0SGreg Roach        DB::table('log')->where('gedcom_id', '=', $this->id)->delete();
549*6ccdf4f0SGreg Roach        DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete();
550*6ccdf4f0SGreg Roach
551*6ccdf4f0SGreg Roach        // After updating the database, we need to fetch a new (sorted) copy
552*6ccdf4f0SGreg Roach        self::$trees = [];
553*6ccdf4f0SGreg Roach    }
554*6ccdf4f0SGreg Roach
555*6ccdf4f0SGreg Roach    /**
556a25f0a04SGreg Roach     * Delete all the genealogy data from a tree - in preparation for importing
557a25f0a04SGreg Roach     * new data. Optionally retain the media data, for when the user has been
558a25f0a04SGreg Roach     * editing their data offline using an application which deletes (or does not
559a25f0a04SGreg Roach     * support) media data.
560a25f0a04SGreg Roach     *
561a25f0a04SGreg Roach     * @param bool $keep_media
562b7e60af1SGreg Roach     *
563b7e60af1SGreg Roach     * @return void
564a25f0a04SGreg Roach     */
565e364afe4SGreg Roach    public function deleteGenealogyData(bool $keep_media): void
566c1010edaSGreg Roach    {
5671ad2dde6SGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
5681ad2dde6SGreg Roach        DB::table('individuals')->where('i_file', '=', $this->id)->delete();
5691ad2dde6SGreg Roach        DB::table('families')->where('f_file', '=', $this->id)->delete();
5701ad2dde6SGreg Roach        DB::table('sources')->where('s_file', '=', $this->id)->delete();
5711ad2dde6SGreg Roach        DB::table('other')->where('o_file', '=', $this->id)->delete();
5721ad2dde6SGreg Roach        DB::table('places')->where('p_file', '=', $this->id)->delete();
5731ad2dde6SGreg Roach        DB::table('placelinks')->where('pl_file', '=', $this->id)->delete();
5741ad2dde6SGreg Roach        DB::table('name')->where('n_file', '=', $this->id)->delete();
5751ad2dde6SGreg Roach        DB::table('dates')->where('d_file', '=', $this->id)->delete();
5761ad2dde6SGreg Roach        DB::table('change')->where('gedcom_id', '=', $this->id)->delete();
577a25f0a04SGreg Roach
578a25f0a04SGreg Roach        if ($keep_media) {
5791ad2dde6SGreg Roach            DB::table('link')->where('l_file', '=', $this->id)
5801ad2dde6SGreg Roach                ->where('l_type', '<>', 'OBJE')
5811ad2dde6SGreg Roach                ->delete();
582a25f0a04SGreg Roach        } else {
5831ad2dde6SGreg Roach            DB::table('link')->where('l_file', '=', $this->id)->delete();
5841ad2dde6SGreg Roach            DB::table('media_file')->where('m_file', '=', $this->id)->delete();
5851ad2dde6SGreg Roach            DB::table('media')->where('m_file', '=', $this->id)->delete();
586a25f0a04SGreg Roach        }
587a25f0a04SGreg Roach    }
588a25f0a04SGreg Roach
589a25f0a04SGreg Roach    /**
590a25f0a04SGreg Roach     * Export the tree to a GEDCOM file
591a25f0a04SGreg Roach     *
5925792757eSGreg Roach     * @param resource $stream
593b7e60af1SGreg Roach     *
594b7e60af1SGreg Roach     * @return void
595a25f0a04SGreg Roach     */
596425af8b9SGreg Roach    public function exportGedcom($stream): void
597c1010edaSGreg Roach    {
598a3d8780cSGreg Roach        $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8'));
59994026f20SGreg Roach
60094026f20SGreg Roach        $union_families = DB::table('families')
60194026f20SGreg Roach            ->where('f_file', '=', $this->id)
60294026f20SGreg Roach            ->select(['f_gedcom AS gedcom', 'f_id AS xref', DB::raw('LENGTH(f_id) AS len'), DB::raw('2 AS n')]);
60394026f20SGreg Roach
60494026f20SGreg Roach        $union_sources = DB::table('sources')
60594026f20SGreg Roach            ->where('s_file', '=', $this->id)
60694026f20SGreg Roach            ->select(['s_gedcom AS gedcom', 's_id AS xref', DB::raw('LENGTH(s_id) AS len'), DB::raw('3 AS n')]);
60794026f20SGreg Roach
60894026f20SGreg Roach        $union_other = DB::table('other')
60994026f20SGreg Roach            ->where('o_file', '=', $this->id)
61094026f20SGreg Roach            ->whereNotIn('o_type', ['HEAD', 'TRLR'])
61194026f20SGreg Roach            ->select(['o_gedcom AS gedcom', 'o_id AS xref', DB::raw('LENGTH(o_id) AS len'), DB::raw('4 AS n')]);
61294026f20SGreg Roach
61394026f20SGreg Roach        $union_media = DB::table('media')
61494026f20SGreg Roach            ->where('m_file', '=', $this->id)
61594026f20SGreg Roach            ->select(['m_gedcom AS gedcom', 'm_id AS xref', DB::raw('LENGTH(m_id) AS len'), DB::raw('5 AS n')]);
61694026f20SGreg Roach
617e5a6b4d4SGreg Roach        DB::table('individuals')
61894026f20SGreg Roach            ->where('i_file', '=', $this->id)
61994026f20SGreg Roach            ->select(['i_gedcom AS gedcom', 'i_id AS xref', DB::raw('LENGTH(i_id) AS len'), DB::raw('1 AS n')])
62094026f20SGreg Roach            ->union($union_families)
62194026f20SGreg Roach            ->union($union_sources)
62294026f20SGreg Roach            ->union($union_other)
62394026f20SGreg Roach            ->union($union_media)
62494026f20SGreg Roach            ->orderBy('n')
62594026f20SGreg Roach            ->orderBy('len')
62694026f20SGreg Roach            ->orderBy('xref')
6270b5fd0a6SGreg Roach            ->chunk(100, static function (Collection $rows) use ($stream, &$buffer): void {
62894026f20SGreg Roach                foreach ($rows as $row) {
6293d7a8a4cSGreg Roach                    $buffer .= FunctionsExport::reformatRecord($row->gedcom);
630a25f0a04SGreg Roach                    if (strlen($buffer) > 65535) {
6315792757eSGreg Roach                        fwrite($stream, $buffer);
632a25f0a04SGreg Roach                        $buffer = '';
633a25f0a04SGreg Roach                    }
634a25f0a04SGreg Roach                }
63594026f20SGreg Roach            });
63694026f20SGreg Roach
6370f471f91SGreg Roach        fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL);
638a25f0a04SGreg Roach    }
639a25f0a04SGreg Roach
640a25f0a04SGreg Roach    /**
641a25f0a04SGreg Roach     * Import data from a gedcom file into this tree.
642a25f0a04SGreg Roach     *
643*6ccdf4f0SGreg Roach     * @param StreamInterface $stream   The GEDCOM file.
644a25f0a04SGreg Roach     * @param string          $filename The preferred filename, for export/download.
645a25f0a04SGreg Roach     *
646b7e60af1SGreg Roach     * @return void
647a25f0a04SGreg Roach     */
648*6ccdf4f0SGreg Roach    public function importGedcomFile(StreamInterface $stream, string $filename): void
649c1010edaSGreg Roach    {
650a25f0a04SGreg Roach        // Read the file in blocks of roughly 64K. Ensure that each block
651a25f0a04SGreg Roach        // contains complete gedcom records. This will ensure we don’t split
652a25f0a04SGreg Roach        // multi-byte characters, as well as simplifying the code to import
653a25f0a04SGreg Roach        // each block.
654a25f0a04SGreg Roach
655a25f0a04SGreg Roach        $file_data = '';
656a25f0a04SGreg Roach
657b7e60af1SGreg Roach        $this->deleteGenealogyData((bool) $this->getPreference('keep_media'));
658a25f0a04SGreg Roach        $this->setPreference('gedcom_filename', $filename);
659a25f0a04SGreg Roach        $this->setPreference('imported', '0');
660a25f0a04SGreg Roach
661*6ccdf4f0SGreg Roach        while (!$stream->eof()) {
662*6ccdf4f0SGreg Roach            $file_data .= $stream->read(65536);
663a25f0a04SGreg Roach            // There is no strrpos() function that searches for substrings :-(
664a25f0a04SGreg Roach            for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) {
665a25f0a04SGreg Roach                if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) {
666a25f0a04SGreg Roach                    // We’ve found the last record boundary in this chunk of data
667a25f0a04SGreg Roach                    break;
668a25f0a04SGreg Roach                }
669a25f0a04SGreg Roach            }
670a25f0a04SGreg Roach            if ($pos) {
6711ad2dde6SGreg Roach                DB::table('gedcom_chunk')->insert([
6721ad2dde6SGreg Roach                    'gedcom_id'  => $this->id,
6731ad2dde6SGreg Roach                    'chunk_data' => substr($file_data, 0, $pos),
674c1010edaSGreg Roach                ]);
6751ad2dde6SGreg Roach
676a25f0a04SGreg Roach                $file_data = substr($file_data, $pos);
677a25f0a04SGreg Roach            }
678a25f0a04SGreg Roach        }
6791ad2dde6SGreg Roach        DB::table('gedcom_chunk')->insert([
6801ad2dde6SGreg Roach            'gedcom_id'  => $this->id,
6811ad2dde6SGreg Roach            'chunk_data' => $file_data,
682c1010edaSGreg Roach        ]);
683a25f0a04SGreg Roach
684*6ccdf4f0SGreg Roach        $stream->close();
685*6ccdf4f0SGreg Roach    }
686*6ccdf4f0SGreg Roach
687*6ccdf4f0SGreg Roach    /**
688*6ccdf4f0SGreg Roach     * Create a new record from GEDCOM data.
689*6ccdf4f0SGreg Roach     *
690*6ccdf4f0SGreg Roach     * @param string $gedcom
691*6ccdf4f0SGreg Roach     *
692*6ccdf4f0SGreg Roach     * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media
693*6ccdf4f0SGreg Roach     * @throws InvalidArgumentException
694*6ccdf4f0SGreg Roach     */
695*6ccdf4f0SGreg Roach    public function createRecord(string $gedcom): GedcomRecord
696*6ccdf4f0SGreg Roach    {
697*6ccdf4f0SGreg Roach        if (!Str::startsWith($gedcom, '0 @@ ')) {
698*6ccdf4f0SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@');
699*6ccdf4f0SGreg Roach        }
700*6ccdf4f0SGreg Roach
701*6ccdf4f0SGreg Roach        $xref   = $this->getNewXref();
702*6ccdf4f0SGreg Roach        $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ ');
703*6ccdf4f0SGreg Roach
704*6ccdf4f0SGreg Roach        // Create a change record
705*6ccdf4f0SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
706*6ccdf4f0SGreg Roach
707*6ccdf4f0SGreg Roach        // Create a pending change
708*6ccdf4f0SGreg Roach        DB::table('change')->insert([
709*6ccdf4f0SGreg Roach            'gedcom_id'  => $this->id,
710*6ccdf4f0SGreg Roach            'xref'       => $xref,
711*6ccdf4f0SGreg Roach            'old_gedcom' => '',
712*6ccdf4f0SGreg Roach            'new_gedcom' => $gedcom,
713*6ccdf4f0SGreg Roach            'user_id'    => Auth::id(),
714*6ccdf4f0SGreg Roach        ]);
715*6ccdf4f0SGreg Roach
716*6ccdf4f0SGreg Roach        // Accept this pending change
717*6ccdf4f0SGreg Roach        if (Auth::user()->getPreference('auto_accept')) {
718*6ccdf4f0SGreg Roach            FunctionsImport::acceptAllChanges($xref, $this);
719*6ccdf4f0SGreg Roach
720*6ccdf4f0SGreg Roach            return new GedcomRecord($xref, $gedcom, null, $this);
721*6ccdf4f0SGreg Roach        }
722*6ccdf4f0SGreg Roach
723*6ccdf4f0SGreg Roach        return GedcomRecord::getInstance($xref, $this, $gedcom);
724a25f0a04SGreg Roach    }
725304f20d5SGreg Roach
726304f20d5SGreg Roach    /**
727b90d8accSGreg Roach     * Generate a new XREF, unique across all family trees
728b90d8accSGreg Roach     *
729b90d8accSGreg Roach     * @return string
730b90d8accSGreg Roach     */
731771ae10aSGreg Roach    public function getNewXref(): string
732c1010edaSGreg Roach    {
733963fbaeeSGreg Roach        // Lock the row, so that only one new XREF may be generated at a time.
734963fbaeeSGreg Roach        DB::table('site_setting')
735963fbaeeSGreg Roach            ->where('setting_name', '=', 'next_xref')
736963fbaeeSGreg Roach            ->lockForUpdate()
737963fbaeeSGreg Roach            ->get();
738963fbaeeSGreg Roach
739a214e186SGreg Roach        $prefix = 'X';
740b90d8accSGreg Roach
741971d66c8SGreg Roach        $increment = 1.0;
742b90d8accSGreg Roach        do {
743963fbaeeSGreg Roach            $num = (int) Site::getPreference('next_xref') + (int) $increment;
744971d66c8SGreg Roach
745971d66c8SGreg Roach            // This exponential increment allows us to scan over large blocks of
746971d66c8SGreg Roach            // existing data in a reasonable time.
747971d66c8SGreg Roach            $increment *= 1.01;
748963fbaeeSGreg Roach
749963fbaeeSGreg Roach            $xref = $prefix . $num;
750963fbaeeSGreg Roach
751963fbaeeSGreg Roach            // Records may already exist with this sequence number.
752963fbaeeSGreg Roach            $already_used =
753963fbaeeSGreg Roach                DB::table('individuals')->where('i_id', '=', $xref)->exists() ||
754963fbaeeSGreg Roach                DB::table('families')->where('f_id', '=', $xref)->exists() ||
755963fbaeeSGreg Roach                DB::table('sources')->where('s_id', '=', $xref)->exists() ||
756963fbaeeSGreg Roach                DB::table('media')->where('m_id', '=', $xref)->exists() ||
757963fbaeeSGreg Roach                DB::table('other')->where('o_id', '=', $xref)->exists() ||
758963fbaeeSGreg Roach                DB::table('change')->where('xref', '=', $xref)->exists();
759963fbaeeSGreg Roach        } while ($already_used);
760963fbaeeSGreg Roach
761963fbaeeSGreg Roach        Site::setPreference('next_xref', (string) $num);
762b90d8accSGreg Roach
763a214e186SGreg Roach        return $xref;
764b90d8accSGreg Roach    }
765b90d8accSGreg Roach
766b90d8accSGreg Roach    /**
767afb591d7SGreg Roach     * Create a new family from GEDCOM data.
768afb591d7SGreg Roach     *
769afb591d7SGreg Roach     * @param string $gedcom
770afb591d7SGreg Roach     *
771afb591d7SGreg Roach     * @return Family
772afb591d7SGreg Roach     * @throws InvalidArgumentException
773afb591d7SGreg Roach     */
774afb591d7SGreg Roach    public function createFamily(string $gedcom): GedcomRecord
775afb591d7SGreg Roach    {
776bec87e94SGreg Roach        if (!Str::startsWith($gedcom, '0 @@ FAM')) {
777afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM');
778afb591d7SGreg Roach        }
779afb591d7SGreg Roach
780afb591d7SGreg Roach        $xref   = $this->getNewXref();
781bec87e94SGreg Roach        $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM');
782afb591d7SGreg Roach
783afb591d7SGreg Roach        // Create a change record
784e5a6b4d4SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
785afb591d7SGreg Roach
786afb591d7SGreg Roach        // Create a pending change
787963fbaeeSGreg Roach        DB::table('change')->insert([
788963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
789963fbaeeSGreg Roach            'xref'       => $xref,
790963fbaeeSGreg Roach            'old_gedcom' => '',
791963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
792963fbaeeSGreg Roach            'user_id'    => Auth::id(),
793afb591d7SGreg Roach        ]);
794304f20d5SGreg Roach
795304f20d5SGreg Roach        // Accept this pending change
796304f20d5SGreg Roach        if (Auth::user()->getPreference('auto_accept')) {
797cc5684fdSGreg Roach            FunctionsImport::acceptAllChanges($xref, $this);
798afb591d7SGreg Roach
799afb591d7SGreg Roach            return new Family($xref, $gedcom, null, $this);
800304f20d5SGreg Roach        }
801afb591d7SGreg Roach
802afb591d7SGreg Roach        return new Family($xref, '', $gedcom, $this);
803afb591d7SGreg Roach    }
804afb591d7SGreg Roach
805afb591d7SGreg Roach    /**
806afb591d7SGreg Roach     * Create a new individual from GEDCOM data.
807afb591d7SGreg Roach     *
808afb591d7SGreg Roach     * @param string $gedcom
809afb591d7SGreg Roach     *
810afb591d7SGreg Roach     * @return Individual
811afb591d7SGreg Roach     * @throws InvalidArgumentException
812afb591d7SGreg Roach     */
813afb591d7SGreg Roach    public function createIndividual(string $gedcom): GedcomRecord
814afb591d7SGreg Roach    {
815bec87e94SGreg Roach        if (!Str::startsWith($gedcom, '0 @@ INDI')) {
816afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI');
817afb591d7SGreg Roach        }
818afb591d7SGreg Roach
819afb591d7SGreg Roach        $xref   = $this->getNewXref();
820bec87e94SGreg Roach        $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI');
821afb591d7SGreg Roach
822afb591d7SGreg Roach        // Create a change record
823e5a6b4d4SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
824afb591d7SGreg Roach
825afb591d7SGreg Roach        // Create a pending change
826963fbaeeSGreg Roach        DB::table('change')->insert([
827963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
828963fbaeeSGreg Roach            'xref'       => $xref,
829963fbaeeSGreg Roach            'old_gedcom' => '',
830963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
831963fbaeeSGreg Roach            'user_id'    => Auth::id(),
832afb591d7SGreg Roach        ]);
833afb591d7SGreg Roach
834afb591d7SGreg Roach        // Accept this pending change
835afb591d7SGreg Roach        if (Auth::user()->getPreference('auto_accept')) {
836afb591d7SGreg Roach            FunctionsImport::acceptAllChanges($xref, $this);
837afb591d7SGreg Roach
838afb591d7SGreg Roach            return new Individual($xref, $gedcom, null, $this);
839afb591d7SGreg Roach        }
840afb591d7SGreg Roach
841afb591d7SGreg Roach        return new Individual($xref, '', $gedcom, $this);
842304f20d5SGreg Roach    }
8438586983fSGreg Roach
8448586983fSGreg Roach    /**
84520b58d20SGreg Roach     * Create a new media object from GEDCOM data.
84620b58d20SGreg Roach     *
84720b58d20SGreg Roach     * @param string $gedcom
84820b58d20SGreg Roach     *
84920b58d20SGreg Roach     * @return Media
85020b58d20SGreg Roach     * @throws InvalidArgumentException
85120b58d20SGreg Roach     */
85220b58d20SGreg Roach    public function createMediaObject(string $gedcom): Media
85320b58d20SGreg Roach    {
854bec87e94SGreg Roach        if (!Str::startsWith($gedcom, '0 @@ OBJE')) {
85520b58d20SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE');
85620b58d20SGreg Roach        }
85720b58d20SGreg Roach
85820b58d20SGreg Roach        $xref   = $this->getNewXref();
859bec87e94SGreg Roach        $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE');
86020b58d20SGreg Roach
86120b58d20SGreg Roach        // Create a change record
862e5a6b4d4SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
86320b58d20SGreg Roach
86420b58d20SGreg Roach        // Create a pending change
865963fbaeeSGreg Roach        DB::table('change')->insert([
866963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
867963fbaeeSGreg Roach            'xref'       => $xref,
868963fbaeeSGreg Roach            'old_gedcom' => '',
869963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
870963fbaeeSGreg Roach            'user_id'    => Auth::id(),
87120b58d20SGreg Roach        ]);
87220b58d20SGreg Roach
87320b58d20SGreg Roach        // Accept this pending change
87420b58d20SGreg Roach        if (Auth::user()->getPreference('auto_accept')) {
87520b58d20SGreg Roach            FunctionsImport::acceptAllChanges($xref, $this);
87620b58d20SGreg Roach
87720b58d20SGreg Roach            return new Media($xref, $gedcom, null, $this);
87820b58d20SGreg Roach        }
87920b58d20SGreg Roach
88020b58d20SGreg Roach        return new Media($xref, '', $gedcom, $this);
88120b58d20SGreg Roach    }
88220b58d20SGreg Roach
88320b58d20SGreg Roach    /**
8848586983fSGreg Roach     * What is the most significant individual in this tree.
8858586983fSGreg Roach     *
886e5a6b4d4SGreg Roach     * @param UserInterface $user
8878586983fSGreg Roach     *
8888586983fSGreg Roach     * @return Individual
8898586983fSGreg Roach     */
890e5a6b4d4SGreg Roach    public function significantIndividual(UserInterface $user): Individual
891c1010edaSGreg Roach    {
8928f9b0fb2SGreg Roach        $individual = null;
8938586983fSGreg Roach
8948f9b0fb2SGreg Roach        if ($this->getUserPreference($user, 'rootid') !== '') {
8958586983fSGreg Roach            $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this);
8968586983fSGreg Roach        }
8978f9b0fb2SGreg Roach
8988f9b0fb2SGreg Roach        if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') {
8998586983fSGreg Roach            $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this);
9008586983fSGreg Roach        }
9018f9b0fb2SGreg Roach
902bec87e94SGreg Roach        if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') {
9038586983fSGreg Roach            $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this);
9048586983fSGreg Roach        }
9058f9b0fb2SGreg Roach        if ($individual === null) {
9068f9b0fb2SGreg Roach            $xref = (string) DB::table('individuals')
9078f9b0fb2SGreg Roach                ->where('i_file', '=', $this->id())
9088f9b0fb2SGreg Roach                ->min('i_id');
909769d7d6eSGreg Roach
910769d7d6eSGreg Roach            $individual = Individual::getInstance($xref, $this);
9115fe1add5SGreg Roach        }
9128f9b0fb2SGreg Roach        if ($individual === null) {
9135fe1add5SGreg Roach            // always return a record
9145fe1add5SGreg Roach            $individual = new Individual('I', '0 @I@ INDI', null, $this);
9155fe1add5SGreg Roach        }
9165fe1add5SGreg Roach
9175fe1add5SGreg Roach        return $individual;
9185fe1add5SGreg Roach    }
919a25f0a04SGreg Roach}
920