xref: /webtrees/app/Tree.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
225afbc57aSGreg Roachuse Closure;
23456d0d35SGreg Roachuse Fisharebest\Flysystem\Adapter\ChrootAdapter;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2522e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService;
2601461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27afb591d7SGreg Roachuse InvalidArgumentException;
281df7ae39SGreg Roachuse League\Flysystem\Filesystem;
29f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
30a25f0a04SGreg Roach
311e653452SGreg Roachuse function app;
32dec352c1SGreg Roachuse function array_key_exists;
33b55cbc6bSGreg Roachuse function assert;
3453432476SGreg Roachuse function date;
35b55cbc6bSGreg Roachuse function is_string;
36dec352c1SGreg Roachuse function str_starts_with;
3753432476SGreg Roachuse function strtoupper;
38dec352c1SGreg Roachuse function substr_replace;
391e653452SGreg Roach
40a25f0a04SGreg Roach/**
4176692c8bSGreg Roach * Provide an interface to the wt_gedcom table.
42a25f0a04SGreg Roach */
43c1010edaSGreg Roachclass Tree
44c1010edaSGreg Roach{
45061b43d7SGreg Roach    private const RESN_PRIVACY = [
46061b43d7SGreg Roach        'none'         => Auth::PRIV_PRIVATE,
47061b43d7SGreg Roach        'privacy'      => Auth::PRIV_USER,
48061b43d7SGreg Roach        'confidential' => Auth::PRIV_NONE,
49061b43d7SGreg Roach        'hidden'       => Auth::PRIV_HIDE,
50061b43d7SGreg Roach    ];
513df1e584SGreg Roach
528a07c98eSGreg Roach
538a07c98eSGreg Roach    // Default values for some tree preferences.
548a07c98eSGreg Roach    protected const DEFAULT_PREFERENCES = [
558a07c98eSGreg Roach        'CALENDAR_FORMAT'              => 'gregorian',
568a07c98eSGreg Roach        'CHART_BOX_TAGS'               => '',
578a07c98eSGreg Roach        'EXPAND_SOURCES'               => '0',
589c7bc1e3SGreg Roach        'FAM_FACTS_QUICK'              => 'ENGA,MARR,DIV',
598a07c98eSGreg Roach        'FORMAT_TEXT'                  => 'markdown',
608a07c98eSGreg Roach        'FULL_SOURCES'                 => '0',
618a07c98eSGreg Roach        'GEDCOM_MEDIA_PATH'            => '',
628a07c98eSGreg Roach        'GENERATE_UIDS'                => '0',
638a07c98eSGreg Roach        'HIDE_GEDCOM_ERRORS'           => '1',
648a07c98eSGreg Roach        'HIDE_LIVE_PEOPLE'             => '1',
658a07c98eSGreg Roach        'INDI_FACTS_QUICK'             => 'BIRT,BURI,BAPM,CENS,DEAT,OCCU,RESI',
668a07c98eSGreg Roach        'KEEP_ALIVE_YEARS_BIRTH'       => '',
678a07c98eSGreg Roach        'KEEP_ALIVE_YEARS_DEATH'       => '',
688a07c98eSGreg Roach        'LANGUAGE'                     => 'en-US',
698a07c98eSGreg Roach        'MAX_ALIVE_AGE'                => '120',
708a07c98eSGreg Roach        'MEDIA_DIRECTORY'              => 'media/',
719062008fSGreg Roach        'MEDIA_UPLOAD'                 => '1', // Auth::PRIV_USER
728a07c98eSGreg Roach        'META_DESCRIPTION'             => '',
738a07c98eSGreg Roach        'META_TITLE'                   => Webtrees::NAME,
748a07c98eSGreg Roach        'NO_UPDATE_CHAN'               => '0',
758a07c98eSGreg Roach        'PEDIGREE_ROOT_ID'             => '',
768a07c98eSGreg Roach        'PREFER_LEVEL2_SOURCES'        => '1',
778a07c98eSGreg Roach        'QUICK_REQUIRED_FACTS'         => 'BIRT,DEAT',
788a07c98eSGreg Roach        'QUICK_REQUIRED_FAMFACTS'      => 'MARR',
798a07c98eSGreg Roach        'REQUIRE_AUTHENTICATION'       => '0',
808a07c98eSGreg Roach        'SAVE_WATERMARK_IMAGE'         => '0',
818a07c98eSGreg Roach        'SHOW_AGE_DIFF'                => '0',
828a07c98eSGreg Roach        'SHOW_COUNTER'                 => '1',
839062008fSGreg Roach        'SHOW_DEAD_PEOPLE'             => '2', // Auth::PRIV_PRIVATE
848a07c98eSGreg Roach        'SHOW_EST_LIST_DATES'          => '0',
858a07c98eSGreg Roach        'SHOW_FACT_ICONS'              => '1',
868a07c98eSGreg Roach        'SHOW_GEDCOM_RECORD'           => '0',
878a07c98eSGreg Roach        'SHOW_HIGHLIGHT_IMAGES'        => '1',
888a07c98eSGreg Roach        'SHOW_LEVEL2_NOTES'            => '1',
899062008fSGreg Roach        'SHOW_LIVING_NAMES'            => '1', // Auth::PRIV_USER
908a07c98eSGreg Roach        'SHOW_MEDIA_DOWNLOAD'          => '0',
919062008fSGreg Roach        'SHOW_NO_WATERMARK'            => '1', // Auth::PRIV_USER
928a07c98eSGreg Roach        'SHOW_PARENTS_AGE'             => '1',
938a07c98eSGreg Roach        'SHOW_PEDIGREE_PLACES'         => '9',
948a07c98eSGreg Roach        'SHOW_PEDIGREE_PLACES_SUFFIX'  => '0',
958a07c98eSGreg Roach        'SHOW_PRIVATE_RELATIONSHIPS'   => '1',
968a07c98eSGreg Roach        'SHOW_RELATIVES_EVENTS'        => '_BIRT_CHIL,_BIRT_SIBL,_MARR_CHIL,_MARR_PARE,_DEAT_CHIL,_DEAT_PARE,_DEAT_GPAR,_DEAT_SIBL,_DEAT_SPOU',
978a07c98eSGreg Roach        'SUBLIST_TRIGGER_I'            => '200',
988a07c98eSGreg Roach        'SURNAME_LIST_STYLE'           => 'style2',
998a07c98eSGreg Roach        'SURNAME_TRADITION'            => 'paternal',
1008a07c98eSGreg Roach        'USE_SILHOUETTE'               => '1',
1018a07c98eSGreg Roach        'WORD_WRAPPED_NOTES'           => '0',
1028a07c98eSGreg Roach    ];
1038a07c98eSGreg Roach
1044c78e066SGreg Roach    private int $id;
1053df1e584SGreg Roach
1064c78e066SGreg Roach    private string $name;
1073df1e584SGreg Roach
1084c78e066SGreg Roach    private string $title;
1093df1e584SGreg Roach
1104c78e066SGreg Roach    /** @var array<int> Default access rules for facts in this tree */
1114c78e066SGreg Roach    private array $fact_privacy;
1123df1e584SGreg Roach
1134c78e066SGreg Roach    /** @var array<int> Default access rules for individuals in this tree */
1144c78e066SGreg Roach    private array $individual_privacy;
1153df1e584SGreg Roach
1164c78e066SGreg Roach    /** @var array<array<int>> Default access rules for individual facts in this tree */
1174c78e066SGreg Roach    private array $individual_fact_privacy;
1183df1e584SGreg Roach
11909482a55SGreg Roach    /** @var array<string> Cached copy of the wt_gedcom_setting table. */
1206ccdf4f0SGreg Roach    private $preferences = [];
1213df1e584SGreg Roach
12209482a55SGreg Roach    /** @var array<array<string>> Cached copy of the wt_user_gedcom_setting table. */
1234c78e066SGreg Roach    private array $user_preferences = [];
124061b43d7SGreg Roach
125a25f0a04SGreg Roach    /**
1263df1e584SGreg Roach     * Create a tree object.
127a25f0a04SGreg Roach     *
12872cf66d4SGreg Roach     * @param int    $id
129aa6f03bbSGreg Roach     * @param string $name
130cc13d6d8SGreg Roach     * @param string $title
131a25f0a04SGreg Roach     */
1325afbc57aSGreg Roach    public function __construct(int $id, string $name, string $title)
133c1010edaSGreg Roach    {
13472cf66d4SGreg Roach        $this->id                      = $id;
135aa6f03bbSGreg Roach        $this->name                    = $name;
136cc13d6d8SGreg Roach        $this->title                   = $title;
13713abd6f3SGreg Roach        $this->fact_privacy            = [];
13813abd6f3SGreg Roach        $this->individual_privacy      = [];
13913abd6f3SGreg Roach        $this->individual_fact_privacy = [];
140518bbdc1SGreg Roach
141518bbdc1SGreg Roach        // Load the privacy settings for this tree
142061b43d7SGreg Roach        $rows = DB::table('default_resn')
143061b43d7SGreg Roach            ->where('gedcom_id', '=', $this->id)
144061b43d7SGreg Roach            ->get();
145518bbdc1SGreg Roach
146518bbdc1SGreg Roach        foreach ($rows as $row) {
147061b43d7SGreg Roach            // Convert GEDCOM privacy restriction to a webtrees access level.
148061b43d7SGreg Roach            $row->resn = self::RESN_PRIVACY[$row->resn];
149061b43d7SGreg Roach
150518bbdc1SGreg Roach            if ($row->xref !== null) {
151518bbdc1SGreg Roach                if ($row->tag_type !== null) {
152b262b3d3SGreg Roach                    $this->individual_fact_privacy[$row->xref][$row->tag_type] = $row->resn;
153518bbdc1SGreg Roach                } else {
154b262b3d3SGreg Roach                    $this->individual_privacy[$row->xref] = $row->resn;
155518bbdc1SGreg Roach                }
156518bbdc1SGreg Roach            } else {
157b262b3d3SGreg Roach                $this->fact_privacy[$row->tag_type] = $row->resn;
158518bbdc1SGreg Roach            }
159518bbdc1SGreg Roach        }
160a25f0a04SGreg Roach    }
161a25f0a04SGreg Roach
162a25f0a04SGreg Roach    /**
1635afbc57aSGreg Roach     * A closure which will create a record from a database row.
1645afbc57aSGreg Roach     *
1655afbc57aSGreg Roach     * @return Closure
1665afbc57aSGreg Roach     */
1675afbc57aSGreg Roach    public static function rowMapper(): Closure
1685afbc57aSGreg Roach    {
169f70bcff5SGreg Roach        return static function (object $row): Tree {
1705afbc57aSGreg Roach            return new Tree((int) $row->tree_id, $row->tree_name, $row->tree_title);
1715afbc57aSGreg Roach        };
1725afbc57aSGreg Roach    }
1735afbc57aSGreg Roach
1745afbc57aSGreg Roach    /**
1756ccdf4f0SGreg Roach     * Set the tree’s configuration settings.
1766ccdf4f0SGreg Roach     *
1776ccdf4f0SGreg Roach     * @param string $setting_name
1786ccdf4f0SGreg Roach     * @param string $setting_value
1796ccdf4f0SGreg Roach     *
1806612c384SGreg Roach     * @return self
1816ccdf4f0SGreg Roach     */
1826ccdf4f0SGreg Roach    public function setPreference(string $setting_name, string $setting_value): Tree
1836ccdf4f0SGreg Roach    {
1846ccdf4f0SGreg Roach        if ($setting_value !== $this->getPreference($setting_name)) {
1856ccdf4f0SGreg Roach            DB::table('gedcom_setting')->updateOrInsert([
1866ccdf4f0SGreg Roach                'gedcom_id'    => $this->id,
1876ccdf4f0SGreg Roach                'setting_name' => $setting_name,
1886ccdf4f0SGreg Roach            ], [
1896ccdf4f0SGreg Roach                'setting_value' => $setting_value,
1906ccdf4f0SGreg Roach            ]);
1916ccdf4f0SGreg Roach
1926ccdf4f0SGreg Roach            $this->preferences[$setting_name] = $setting_value;
1936ccdf4f0SGreg Roach
1946ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
1956ccdf4f0SGreg Roach        }
1966ccdf4f0SGreg Roach
1976ccdf4f0SGreg Roach        return $this;
1986ccdf4f0SGreg Roach    }
1996ccdf4f0SGreg Roach
2006ccdf4f0SGreg Roach    /**
2016ccdf4f0SGreg Roach     * Get the tree’s configuration settings.
2026ccdf4f0SGreg Roach     *
2036ccdf4f0SGreg Roach     * @param string      $setting_name
2049062008fSGreg Roach     * @param string|null $default
2056ccdf4f0SGreg Roach     *
2066ccdf4f0SGreg Roach     * @return string
2076ccdf4f0SGreg Roach     */
2089062008fSGreg Roach    public function getPreference(string $setting_name, string $default = null): string
2096ccdf4f0SGreg Roach    {
21054c1ab5eSGreg Roach        if ($this->preferences === []) {
2116ccdf4f0SGreg Roach            $this->preferences = DB::table('gedcom_setting')
2126ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
2136ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
2146ccdf4f0SGreg Roach                ->all();
2156ccdf4f0SGreg Roach        }
2166ccdf4f0SGreg Roach
2179062008fSGreg Roach        return $this->preferences[$setting_name] ?? $default ?? self::DEFAULT_PREFERENCES[$setting_name] ?? '';
2186ccdf4f0SGreg Roach    }
2196ccdf4f0SGreg Roach
2206ccdf4f0SGreg Roach    /**
2216ccdf4f0SGreg Roach     * The name of this tree
2226ccdf4f0SGreg Roach     *
2236ccdf4f0SGreg Roach     * @return string
2246ccdf4f0SGreg Roach     */
2256ccdf4f0SGreg Roach    public function name(): string
2266ccdf4f0SGreg Roach    {
2276ccdf4f0SGreg Roach        return $this->name;
2286ccdf4f0SGreg Roach    }
2296ccdf4f0SGreg Roach
2306ccdf4f0SGreg Roach    /**
2316ccdf4f0SGreg Roach     * The title of this tree
2326ccdf4f0SGreg Roach     *
2336ccdf4f0SGreg Roach     * @return string
2346ccdf4f0SGreg Roach     */
2356ccdf4f0SGreg Roach    public function title(): string
2366ccdf4f0SGreg Roach    {
2376ccdf4f0SGreg Roach        return $this->title;
2386ccdf4f0SGreg Roach    }
2396ccdf4f0SGreg Roach
2406ccdf4f0SGreg Roach    /**
2416ccdf4f0SGreg Roach     * The fact-level privacy for this tree.
2426ccdf4f0SGreg Roach     *
24309482a55SGreg Roach     * @return array<int>
2446ccdf4f0SGreg Roach     */
2456ccdf4f0SGreg Roach    public function getFactPrivacy(): array
2466ccdf4f0SGreg Roach    {
2476ccdf4f0SGreg Roach        return $this->fact_privacy;
2486ccdf4f0SGreg Roach    }
2496ccdf4f0SGreg Roach
2506ccdf4f0SGreg Roach    /**
2516ccdf4f0SGreg Roach     * The individual-level privacy for this tree.
2526ccdf4f0SGreg Roach     *
25309482a55SGreg Roach     * @return array<int>
2546ccdf4f0SGreg Roach     */
2556ccdf4f0SGreg Roach    public function getIndividualPrivacy(): array
2566ccdf4f0SGreg Roach    {
2576ccdf4f0SGreg Roach        return $this->individual_privacy;
2586ccdf4f0SGreg Roach    }
2596ccdf4f0SGreg Roach
2606ccdf4f0SGreg Roach    /**
2616ccdf4f0SGreg Roach     * The individual-fact-level privacy for this tree.
2626ccdf4f0SGreg Roach     *
26309482a55SGreg Roach     * @return array<array<int>>
2646ccdf4f0SGreg Roach     */
2656ccdf4f0SGreg Roach    public function getIndividualFactPrivacy(): array
2666ccdf4f0SGreg Roach    {
2676ccdf4f0SGreg Roach        return $this->individual_fact_privacy;
2686ccdf4f0SGreg Roach    }
2696ccdf4f0SGreg Roach
2706ccdf4f0SGreg Roach    /**
2716ccdf4f0SGreg Roach     * Set the tree’s user-configuration settings.
2726ccdf4f0SGreg Roach     *
2736ccdf4f0SGreg Roach     * @param UserInterface $user
2746ccdf4f0SGreg Roach     * @param string        $setting_name
2756ccdf4f0SGreg Roach     * @param string        $setting_value
2766ccdf4f0SGreg Roach     *
2776612c384SGreg Roach     * @return self
2786ccdf4f0SGreg Roach     */
2796ccdf4f0SGreg Roach    public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree
2806ccdf4f0SGreg Roach    {
2816ccdf4f0SGreg Roach        if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
2826ccdf4f0SGreg Roach            // Update the database
2836ccdf4f0SGreg Roach            DB::table('user_gedcom_setting')->updateOrInsert([
2846ccdf4f0SGreg Roach                'gedcom_id'    => $this->id(),
2856ccdf4f0SGreg Roach                'user_id'      => $user->id(),
2866ccdf4f0SGreg Roach                'setting_name' => $setting_name,
2876ccdf4f0SGreg Roach            ], [
2886ccdf4f0SGreg Roach                'setting_value' => $setting_value,
2896ccdf4f0SGreg Roach            ]);
2906ccdf4f0SGreg Roach
2916ccdf4f0SGreg Roach            // Update the cache
2926ccdf4f0SGreg Roach            $this->user_preferences[$user->id()][$setting_name] = $setting_value;
2936ccdf4f0SGreg Roach            // Audit log of changes
2946ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this);
2956ccdf4f0SGreg Roach        }
2966ccdf4f0SGreg Roach
2976ccdf4f0SGreg Roach        return $this;
2986ccdf4f0SGreg Roach    }
2996ccdf4f0SGreg Roach
3006ccdf4f0SGreg Roach    /**
3016ccdf4f0SGreg Roach     * Get the tree’s user-configuration settings.
3026ccdf4f0SGreg Roach     *
3036ccdf4f0SGreg Roach     * @param UserInterface $user
3046ccdf4f0SGreg Roach     * @param string        $setting_name
3056ccdf4f0SGreg Roach     * @param string        $default
3066ccdf4f0SGreg Roach     *
3076ccdf4f0SGreg Roach     * @return string
3086ccdf4f0SGreg Roach     */
3096ccdf4f0SGreg Roach    public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string
3106ccdf4f0SGreg Roach    {
3116ccdf4f0SGreg Roach        // There are lots of settings, and we need to fetch lots of them on every page
3126ccdf4f0SGreg Roach        // so it is quicker to fetch them all in one go.
3136ccdf4f0SGreg Roach        if (!array_key_exists($user->id(), $this->user_preferences)) {
3146ccdf4f0SGreg Roach            $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting')
3156ccdf4f0SGreg Roach                ->where('user_id', '=', $user->id())
3166ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
3176ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
3186ccdf4f0SGreg Roach                ->all();
3196ccdf4f0SGreg Roach        }
3206ccdf4f0SGreg Roach
3216ccdf4f0SGreg Roach        return $this->user_preferences[$user->id()][$setting_name] ?? $default;
3226ccdf4f0SGreg Roach    }
3236ccdf4f0SGreg Roach
3246ccdf4f0SGreg Roach    /**
3256ccdf4f0SGreg Roach     * The ID of this tree
3266ccdf4f0SGreg Roach     *
3276ccdf4f0SGreg Roach     * @return int
3286ccdf4f0SGreg Roach     */
3296ccdf4f0SGreg Roach    public function id(): int
3306ccdf4f0SGreg Roach    {
3316ccdf4f0SGreg Roach        return $this->id;
3326ccdf4f0SGreg Roach    }
3336ccdf4f0SGreg Roach
3346ccdf4f0SGreg Roach    /**
3356ccdf4f0SGreg Roach     * Can a user accept changes for this tree?
3366ccdf4f0SGreg Roach     *
3376ccdf4f0SGreg Roach     * @param UserInterface $user
3386ccdf4f0SGreg Roach     *
3396ccdf4f0SGreg Roach     * @return bool
3406ccdf4f0SGreg Roach     */
3416ccdf4f0SGreg Roach    public function canAcceptChanges(UserInterface $user): bool
3426ccdf4f0SGreg Roach    {
3436ccdf4f0SGreg Roach        return Auth::isModerator($this, $user);
3446ccdf4f0SGreg Roach    }
3456ccdf4f0SGreg Roach
3466ccdf4f0SGreg Roach    /**
347b78374c5SGreg Roach     * Are there any pending edits for this tree, than need reviewing by a moderator.
348b78374c5SGreg Roach     *
349b78374c5SGreg Roach     * @return bool
350b78374c5SGreg Roach     */
351771ae10aSGreg Roach    public function hasPendingEdit(): bool
352c1010edaSGreg Roach    {
35315a3f100SGreg Roach        return DB::table('change')
35415a3f100SGreg Roach            ->where('gedcom_id', '=', $this->id)
35515a3f100SGreg Roach            ->where('status', '=', 'pending')
35615a3f100SGreg Roach            ->exists();
357b78374c5SGreg Roach    }
358b78374c5SGreg Roach
359b78374c5SGreg Roach    /**
3606ccdf4f0SGreg Roach     * Create a new record from GEDCOM data.
3616ccdf4f0SGreg Roach     *
3626ccdf4f0SGreg Roach     * @param string $gedcom
3636ccdf4f0SGreg Roach     *
3640d15532eSGreg Roach     * @return GedcomRecord|Individual|Family|Location|Note|Source|Repository|Media|Submitter|Submission
3656ccdf4f0SGreg Roach     * @throws InvalidArgumentException
3666ccdf4f0SGreg Roach     */
3676ccdf4f0SGreg Roach    public function createRecord(string $gedcom): GedcomRecord
3686ccdf4f0SGreg Roach    {
369b4a2f885SGreg Roach        if (!preg_match('/^0 @@ ([_A-Z]+)/', $gedcom, $match)) {
3706ccdf4f0SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@');
3716ccdf4f0SGreg Roach        }
3726ccdf4f0SGreg Roach
3736b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make($match[1]);
374dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
3756ccdf4f0SGreg Roach
3766ccdf4f0SGreg Roach        // Create a change record
37753432476SGreg Roach        $today = strtoupper(date('d M Y'));
37853432476SGreg Roach        $now   = date('H:i:s');
37953432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
3806ccdf4f0SGreg Roach
3816ccdf4f0SGreg Roach        // Create a pending change
3826ccdf4f0SGreg Roach        DB::table('change')->insert([
3836ccdf4f0SGreg Roach            'gedcom_id'  => $this->id,
3846ccdf4f0SGreg Roach            'xref'       => $xref,
3856ccdf4f0SGreg Roach            'old_gedcom' => '',
3866ccdf4f0SGreg Roach            'new_gedcom' => $gedcom,
3876ccdf4f0SGreg Roach            'user_id'    => Auth::id(),
3886ccdf4f0SGreg Roach        ]);
3896ccdf4f0SGreg Roach
3906ccdf4f0SGreg Roach        // Accept this pending change
3911fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
3926b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->new($xref, $gedcom, null, $this);
3936ccdf4f0SGreg Roach
394b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
395b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
396b55cbc6bSGreg Roach
397b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
39822e73debSGreg Roach
39922e73debSGreg Roach            return $record;
4006ccdf4f0SGreg Roach        }
4016ccdf4f0SGreg Roach
4026b9cb339SGreg Roach        return Registry::gedcomRecordFactory()->new($xref, '', $gedcom, $this);
403a25f0a04SGreg Roach    }
404304f20d5SGreg Roach
405304f20d5SGreg Roach    /**
406afb591d7SGreg Roach     * Create a new family from GEDCOM data.
407afb591d7SGreg Roach     *
408afb591d7SGreg Roach     * @param string $gedcom
409afb591d7SGreg Roach     *
410afb591d7SGreg Roach     * @return Family
411afb591d7SGreg Roach     * @throws InvalidArgumentException
412afb591d7SGreg Roach     */
413afb591d7SGreg Roach    public function createFamily(string $gedcom): GedcomRecord
414afb591d7SGreg Roach    {
415dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ FAM')) {
416afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM');
417afb591d7SGreg Roach        }
418afb591d7SGreg Roach
4196b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Family::RECORD_TYPE);
420dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
421afb591d7SGreg Roach
422afb591d7SGreg Roach        // Create a change record
42353432476SGreg Roach        $today = strtoupper(date('d M Y'));
42453432476SGreg Roach        $now   = date('H:i:s');
42553432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
426afb591d7SGreg Roach
427afb591d7SGreg Roach        // Create a pending change
428963fbaeeSGreg Roach        DB::table('change')->insert([
429963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
430963fbaeeSGreg Roach            'xref'       => $xref,
431963fbaeeSGreg Roach            'old_gedcom' => '',
432963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
433963fbaeeSGreg Roach            'user_id'    => Auth::id(),
434afb591d7SGreg Roach        ]);
435304f20d5SGreg Roach
436304f20d5SGreg Roach        // Accept this pending change
4371fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
4386b9cb339SGreg Roach            $record = Registry::familyFactory()->new($xref, $gedcom, null, $this);
439afb591d7SGreg Roach
440b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
441b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
442b55cbc6bSGreg Roach
443b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
44422e73debSGreg Roach
44522e73debSGreg Roach            return $record;
446304f20d5SGreg Roach        }
447afb591d7SGreg Roach
4486b9cb339SGreg Roach        return Registry::familyFactory()->new($xref, '', $gedcom, $this);
449afb591d7SGreg Roach    }
450afb591d7SGreg Roach
451afb591d7SGreg Roach    /**
452afb591d7SGreg Roach     * Create a new individual from GEDCOM data.
453afb591d7SGreg Roach     *
454afb591d7SGreg Roach     * @param string $gedcom
455afb591d7SGreg Roach     *
456afb591d7SGreg Roach     * @return Individual
457afb591d7SGreg Roach     * @throws InvalidArgumentException
458afb591d7SGreg Roach     */
459afb591d7SGreg Roach    public function createIndividual(string $gedcom): GedcomRecord
460afb591d7SGreg Roach    {
461dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ INDI')) {
462afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI');
463afb591d7SGreg Roach        }
464afb591d7SGreg Roach
4656b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Individual::RECORD_TYPE);
466dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
467afb591d7SGreg Roach
468afb591d7SGreg Roach        // Create a change record
46953432476SGreg Roach        $today = strtoupper(date('d M Y'));
47053432476SGreg Roach        $now   = date('H:i:s');
47153432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
472afb591d7SGreg Roach
473afb591d7SGreg Roach        // Create a pending change
474963fbaeeSGreg Roach        DB::table('change')->insert([
475963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
476963fbaeeSGreg Roach            'xref'       => $xref,
477963fbaeeSGreg Roach            'old_gedcom' => '',
478963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
479963fbaeeSGreg Roach            'user_id'    => Auth::id(),
480afb591d7SGreg Roach        ]);
481afb591d7SGreg Roach
482afb591d7SGreg Roach        // Accept this pending change
4831fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
4846b9cb339SGreg Roach            $record = Registry::individualFactory()->new($xref, $gedcom, null, $this);
485afb591d7SGreg Roach
486b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
487b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
488b55cbc6bSGreg Roach
489b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
49022e73debSGreg Roach
49122e73debSGreg Roach            return $record;
492afb591d7SGreg Roach        }
493afb591d7SGreg Roach
4946b9cb339SGreg Roach        return Registry::individualFactory()->new($xref, '', $gedcom, $this);
495304f20d5SGreg Roach    }
4968586983fSGreg Roach
4978586983fSGreg Roach    /**
49820b58d20SGreg Roach     * Create a new media object from GEDCOM data.
49920b58d20SGreg Roach     *
50020b58d20SGreg Roach     * @param string $gedcom
50120b58d20SGreg Roach     *
50220b58d20SGreg Roach     * @return Media
50320b58d20SGreg Roach     * @throws InvalidArgumentException
50420b58d20SGreg Roach     */
50520b58d20SGreg Roach    public function createMediaObject(string $gedcom): Media
50620b58d20SGreg Roach    {
507dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ OBJE')) {
50820b58d20SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE');
50920b58d20SGreg Roach        }
51020b58d20SGreg Roach
5116b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Media::RECORD_TYPE);
512dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
51320b58d20SGreg Roach
51420b58d20SGreg Roach        // Create a change record
51553432476SGreg Roach        $today = strtoupper(date('d M Y'));
51653432476SGreg Roach        $now   = date('H:i:s');
51753432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
51820b58d20SGreg Roach
51920b58d20SGreg Roach        // Create a pending change
520963fbaeeSGreg Roach        DB::table('change')->insert([
521963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
522963fbaeeSGreg Roach            'xref'       => $xref,
523963fbaeeSGreg Roach            'old_gedcom' => '',
524963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
525963fbaeeSGreg Roach            'user_id'    => Auth::id(),
52620b58d20SGreg Roach        ]);
52720b58d20SGreg Roach
52820b58d20SGreg Roach        // Accept this pending change
5291fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
5306b9cb339SGreg Roach            $record = Registry::mediaFactory()->new($xref, $gedcom, null, $this);
53120b58d20SGreg Roach
532b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
533b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
534b55cbc6bSGreg Roach
535b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
53622e73debSGreg Roach
53722e73debSGreg Roach            return $record;
53820b58d20SGreg Roach        }
53920b58d20SGreg Roach
5406b9cb339SGreg Roach        return Registry::mediaFactory()->new($xref, '', $gedcom, $this);
54120b58d20SGreg Roach    }
54220b58d20SGreg Roach
54320b58d20SGreg Roach    /**
5448586983fSGreg Roach     * What is the most significant individual in this tree.
5458586983fSGreg Roach     *
546e5a6b4d4SGreg Roach     * @param UserInterface $user
5473370567dSGreg Roach     * @param string        $xref
5488586983fSGreg Roach     *
5498586983fSGreg Roach     * @return Individual
5508586983fSGreg Roach     */
55173d58381SGreg Roach    public function significantIndividual(UserInterface $user, string $xref = ''): Individual
552c1010edaSGreg Roach    {
5533370567dSGreg Roach        if ($xref === '') {
5548f9b0fb2SGreg Roach            $individual = null;
5553370567dSGreg Roach        } else {
5566b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($xref, $this);
5573370567dSGreg Roach
5583370567dSGreg Roach            if ($individual === null) {
5596b9cb339SGreg Roach                $family = Registry::familyFactory()->make($xref, $this);
5603370567dSGreg Roach
5613370567dSGreg Roach                if ($family instanceof Family) {
5623370567dSGreg Roach                    $individual = $family->spouses()->first() ?? $family->children()->first();
5633370567dSGreg Roach                }
5643370567dSGreg Roach            }
5653370567dSGreg Roach        }
5668586983fSGreg Roach
5671fe542e9SGreg Roach        if ($individual === null && $this->getUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF) !== '') {
5681fe542e9SGreg Roach            $individual = Registry::individualFactory()->make($this->getUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF), $this);
5698586983fSGreg Roach        }
5708f9b0fb2SGreg Roach
5711fe542e9SGreg Roach        if ($individual === null && $this->getUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF) !== '') {
5721fe542e9SGreg Roach            $individual = Registry::individualFactory()->make($this->getUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF), $this);
5738586983fSGreg Roach        }
5748f9b0fb2SGreg Roach
575bec87e94SGreg Roach        if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') {
5766b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($this->getPreference('PEDIGREE_ROOT_ID'), $this);
5778586983fSGreg Roach        }
5788f9b0fb2SGreg Roach        if ($individual === null) {
579b55cbc6bSGreg Roach            $xref = DB::table('individuals')
5808f9b0fb2SGreg Roach                ->where('i_file', '=', $this->id())
5818f9b0fb2SGreg Roach                ->min('i_id');
582769d7d6eSGreg Roach
583b55cbc6bSGreg Roach            if (is_string($xref)) {
5846b9cb339SGreg Roach                $individual = Registry::individualFactory()->make($xref, $this);
5855fe1add5SGreg Roach            }
586b55cbc6bSGreg Roach        }
5878f9b0fb2SGreg Roach        if ($individual === null) {
5885fe1add5SGreg Roach            // always return a record
5896b9cb339SGreg Roach            $individual = Registry::individualFactory()->new('I', '0 @I@ INDI', null, $this);
5905fe1add5SGreg Roach        }
5915fe1add5SGreg Roach
5925fe1add5SGreg Roach        return $individual;
5935fe1add5SGreg Roach    }
5941df7ae39SGreg Roach
59585a166d8SGreg Roach    /**
59685a166d8SGreg Roach     * Where do we store our media files.
59785a166d8SGreg Roach     *
598f7cf8a15SGreg Roach     * @param FilesystemOperator $data_filesystem
599a04bb9a2SGreg Roach     *
600f7cf8a15SGreg Roach     * @return FilesystemOperator
60185a166d8SGreg Roach     */
602f7cf8a15SGreg Roach    public function mediaFilesystem(FilesystemOperator $data_filesystem): FilesystemOperator
6031df7ae39SGreg Roach    {
6048a07c98eSGreg Roach        $media_dir = $this->getPreference('MEDIA_DIRECTORY');
605a04bb9a2SGreg Roach        $adapter   = new ChrootAdapter($data_filesystem, $media_dir);
606456d0d35SGreg Roach
607456d0d35SGreg Roach        return new Filesystem($adapter);
6081df7ae39SGreg Roach    }
609a25f0a04SGreg Roach}
610