xref: /webtrees/app/Tree.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2422e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService;
2501461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
26afb591d7SGreg Roachuse InvalidArgumentException;
27f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
28a25f0a04SGreg Roach
29dec352c1SGreg Roachuse function array_key_exists;
3053432476SGreg Roachuse function date;
31b55cbc6bSGreg Roachuse function is_string;
32dec352c1SGreg Roachuse function str_starts_with;
3353432476SGreg Roachuse function strtoupper;
34dec352c1SGreg Roachuse function substr_replace;
351e653452SGreg Roach
36a25f0a04SGreg Roach/**
3776692c8bSGreg Roach * Provide an interface to the wt_gedcom table.
38a25f0a04SGreg Roach */
39c1010edaSGreg Roachclass Tree
40c1010edaSGreg Roach{
41061b43d7SGreg Roach    private const RESN_PRIVACY = [
42061b43d7SGreg Roach        'none'         => Auth::PRIV_PRIVATE,
43061b43d7SGreg Roach        'privacy'      => Auth::PRIV_USER,
44061b43d7SGreg Roach        'confidential' => Auth::PRIV_NONE,
45061b43d7SGreg Roach        'hidden'       => Auth::PRIV_HIDE,
46061b43d7SGreg Roach    ];
473df1e584SGreg Roach
488a07c98eSGreg Roach
498a07c98eSGreg Roach    // Default values for some tree preferences.
508a07c98eSGreg Roach    protected const DEFAULT_PREFERENCES = [
518a07c98eSGreg Roach        'CALENDAR_FORMAT'              => 'gregorian',
528a07c98eSGreg Roach        'CHART_BOX_TAGS'               => '',
538a07c98eSGreg Roach        'EXPAND_SOURCES'               => '0',
549c7bc1e3SGreg Roach        'FAM_FACTS_QUICK'              => 'ENGA,MARR,DIV',
558a07c98eSGreg Roach        'FORMAT_TEXT'                  => 'markdown',
568a07c98eSGreg Roach        'GEDCOM_MEDIA_PATH'            => '',
578a07c98eSGreg Roach        'GENERATE_UIDS'                => '0',
588a07c98eSGreg Roach        'HIDE_GEDCOM_ERRORS'           => '1',
598a07c98eSGreg Roach        'HIDE_LIVE_PEOPLE'             => '1',
608a07c98eSGreg Roach        'INDI_FACTS_QUICK'             => 'BIRT,BURI,BAPM,CENS,DEAT,OCCU,RESI',
618a07c98eSGreg Roach        'KEEP_ALIVE_YEARS_BIRTH'       => '',
628a07c98eSGreg Roach        'KEEP_ALIVE_YEARS_DEATH'       => '',
638a07c98eSGreg Roach        'LANGUAGE'                     => 'en-US',
648a07c98eSGreg Roach        'MAX_ALIVE_AGE'                => '120',
658a07c98eSGreg Roach        'MEDIA_DIRECTORY'              => 'media/',
669062008fSGreg Roach        'MEDIA_UPLOAD'                 => '1', // Auth::PRIV_USER
678a07c98eSGreg Roach        'META_DESCRIPTION'             => '',
688a07c98eSGreg Roach        'META_TITLE'                   => Webtrees::NAME,
698a07c98eSGreg Roach        'NO_UPDATE_CHAN'               => '0',
708a07c98eSGreg Roach        'PEDIGREE_ROOT_ID'             => '',
718a07c98eSGreg Roach        'QUICK_REQUIRED_FACTS'         => 'BIRT,DEAT',
728a07c98eSGreg Roach        'QUICK_REQUIRED_FAMFACTS'      => 'MARR',
738a07c98eSGreg Roach        'REQUIRE_AUTHENTICATION'       => '0',
748a07c98eSGreg Roach        'SAVE_WATERMARK_IMAGE'         => '0',
758a07c98eSGreg Roach        'SHOW_AGE_DIFF'                => '0',
768a07c98eSGreg Roach        'SHOW_COUNTER'                 => '1',
779062008fSGreg Roach        'SHOW_DEAD_PEOPLE'             => '2', // Auth::PRIV_PRIVATE
788a07c98eSGreg Roach        'SHOW_EST_LIST_DATES'          => '0',
798a07c98eSGreg Roach        'SHOW_FACT_ICONS'              => '1',
808a07c98eSGreg Roach        'SHOW_GEDCOM_RECORD'           => '0',
818a07c98eSGreg Roach        'SHOW_HIGHLIGHT_IMAGES'        => '1',
828a07c98eSGreg Roach        'SHOW_LEVEL2_NOTES'            => '1',
839062008fSGreg Roach        'SHOW_LIVING_NAMES'            => '1', // Auth::PRIV_USER
848a07c98eSGreg Roach        'SHOW_MEDIA_DOWNLOAD'          => '0',
859062008fSGreg Roach        'SHOW_NO_WATERMARK'            => '1', // Auth::PRIV_USER
868a07c98eSGreg Roach        'SHOW_PARENTS_AGE'             => '1',
878a07c98eSGreg Roach        'SHOW_PEDIGREE_PLACES'         => '9',
888a07c98eSGreg Roach        'SHOW_PEDIGREE_PLACES_SUFFIX'  => '0',
898a07c98eSGreg Roach        'SHOW_PRIVATE_RELATIONSHIPS'   => '1',
908a07c98eSGreg Roach        'SHOW_RELATIVES_EVENTS'        => '_BIRT_CHIL,_BIRT_SIBL,_MARR_CHIL,_MARR_PARE,_DEAT_CHIL,_DEAT_PARE,_DEAT_GPAR,_DEAT_SIBL,_DEAT_SPOU',
918a07c98eSGreg Roach        'SUBLIST_TRIGGER_I'            => '200',
928a07c98eSGreg Roach        'SURNAME_LIST_STYLE'           => 'style2',
938a07c98eSGreg Roach        'SURNAME_TRADITION'            => 'paternal',
948a07c98eSGreg Roach        'USE_SILHOUETTE'               => '1',
958a07c98eSGreg Roach        'WORD_WRAPPED_NOTES'           => '0',
968a07c98eSGreg Roach    ];
978a07c98eSGreg Roach
984c78e066SGreg Roach    private int $id;
993df1e584SGreg Roach
1004c78e066SGreg Roach    private string $name;
1013df1e584SGreg Roach
1024c78e066SGreg Roach    private string $title;
1033df1e584SGreg Roach
1044c78e066SGreg Roach    /** @var array<int> Default access rules for facts in this tree */
1054c78e066SGreg Roach    private array $fact_privacy;
1063df1e584SGreg Roach
1074c78e066SGreg Roach    /** @var array<int> Default access rules for individuals in this tree */
1084c78e066SGreg Roach    private array $individual_privacy;
1093df1e584SGreg Roach
1104c78e066SGreg Roach    /** @var array<array<int>> Default access rules for individual facts in this tree */
1114c78e066SGreg Roach    private array $individual_fact_privacy;
1123df1e584SGreg Roach
11309482a55SGreg Roach    /** @var array<string> Cached copy of the wt_gedcom_setting table. */
1145f2657cbSGreg Roach    private array $preferences = [];
1153df1e584SGreg Roach
11609482a55SGreg Roach    /** @var array<array<string>> Cached copy of the wt_user_gedcom_setting table. */
1174c78e066SGreg Roach    private array $user_preferences = [];
118061b43d7SGreg Roach
119a25f0a04SGreg Roach    /**
1203df1e584SGreg Roach     * Create a tree object.
121a25f0a04SGreg Roach     *
12272cf66d4SGreg Roach     * @param int    $id
123aa6f03bbSGreg Roach     * @param string $name
124cc13d6d8SGreg Roach     * @param string $title
125a25f0a04SGreg Roach     */
1265afbc57aSGreg Roach    public function __construct(int $id, string $name, string $title)
127c1010edaSGreg Roach    {
12872cf66d4SGreg Roach        $this->id                      = $id;
129aa6f03bbSGreg Roach        $this->name                    = $name;
130cc13d6d8SGreg Roach        $this->title                   = $title;
13113abd6f3SGreg Roach        $this->fact_privacy            = [];
13213abd6f3SGreg Roach        $this->individual_privacy      = [];
13313abd6f3SGreg Roach        $this->individual_fact_privacy = [];
134518bbdc1SGreg Roach
135518bbdc1SGreg Roach        // Load the privacy settings for this tree
136061b43d7SGreg Roach        $rows = DB::table('default_resn')
137061b43d7SGreg Roach            ->where('gedcom_id', '=', $this->id)
138061b43d7SGreg Roach            ->get();
139518bbdc1SGreg Roach
140518bbdc1SGreg Roach        foreach ($rows as $row) {
141061b43d7SGreg Roach            // Convert GEDCOM privacy restriction to a webtrees access level.
142061b43d7SGreg Roach            $row->resn = self::RESN_PRIVACY[$row->resn];
143061b43d7SGreg Roach
144518bbdc1SGreg Roach            if ($row->xref !== null) {
145518bbdc1SGreg Roach                if ($row->tag_type !== null) {
146b262b3d3SGreg Roach                    $this->individual_fact_privacy[$row->xref][$row->tag_type] = $row->resn;
147518bbdc1SGreg Roach                } else {
148b262b3d3SGreg Roach                    $this->individual_privacy[$row->xref] = $row->resn;
149518bbdc1SGreg Roach                }
150518bbdc1SGreg Roach            } else {
151b262b3d3SGreg Roach                $this->fact_privacy[$row->tag_type] = $row->resn;
152518bbdc1SGreg Roach            }
153518bbdc1SGreg Roach        }
154a25f0a04SGreg Roach    }
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach    /**
1575afbc57aSGreg Roach     * A closure which will create a record from a database row.
1585afbc57aSGreg Roach     *
159c6921a17SGreg Roach     * @return Closure(object):Tree
1605afbc57aSGreg Roach     */
1615afbc57aSGreg Roach    public static function rowMapper(): Closure
1625afbc57aSGreg Roach    {
163743491b8SGreg Roach        return static fn (object $row): Tree => new Tree((int) $row->tree_id, $row->tree_name, $row->tree_title);
1645afbc57aSGreg Roach    }
1655afbc57aSGreg Roach
1665afbc57aSGreg Roach    /**
1676ccdf4f0SGreg Roach     * Set the tree’s configuration settings.
1686ccdf4f0SGreg Roach     *
1696ccdf4f0SGreg Roach     * @param string $setting_name
1706ccdf4f0SGreg Roach     * @param string $setting_value
1716ccdf4f0SGreg Roach     *
1726612c384SGreg Roach     * @return self
1736ccdf4f0SGreg Roach     */
1746ccdf4f0SGreg Roach    public function setPreference(string $setting_name, string $setting_value): Tree
1756ccdf4f0SGreg Roach    {
1766ccdf4f0SGreg Roach        if ($setting_value !== $this->getPreference($setting_name)) {
1776ccdf4f0SGreg Roach            DB::table('gedcom_setting')->updateOrInsert([
1786ccdf4f0SGreg Roach                'gedcom_id'    => $this->id,
1796ccdf4f0SGreg Roach                'setting_name' => $setting_name,
1806ccdf4f0SGreg Roach            ], [
1816ccdf4f0SGreg Roach                'setting_value' => $setting_value,
1826ccdf4f0SGreg Roach            ]);
1836ccdf4f0SGreg Roach
1846ccdf4f0SGreg Roach            $this->preferences[$setting_name] = $setting_value;
1856ccdf4f0SGreg Roach
1866ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
1876ccdf4f0SGreg Roach        }
1886ccdf4f0SGreg Roach
1896ccdf4f0SGreg Roach        return $this;
1906ccdf4f0SGreg Roach    }
1916ccdf4f0SGreg Roach
1926ccdf4f0SGreg Roach    /**
1936ccdf4f0SGreg Roach     * Get the tree’s configuration settings.
1946ccdf4f0SGreg Roach     *
1956ccdf4f0SGreg Roach     * @param string      $setting_name
1969062008fSGreg Roach     * @param string|null $default
1976ccdf4f0SGreg Roach     *
1986ccdf4f0SGreg Roach     * @return string
1996ccdf4f0SGreg Roach     */
2009062008fSGreg Roach    public function getPreference(string $setting_name, string $default = null): string
2016ccdf4f0SGreg Roach    {
20254c1ab5eSGreg Roach        if ($this->preferences === []) {
2036ccdf4f0SGreg Roach            $this->preferences = DB::table('gedcom_setting')
2046ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
2056ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
2066ccdf4f0SGreg Roach                ->all();
2076ccdf4f0SGreg Roach        }
2086ccdf4f0SGreg Roach
2099062008fSGreg Roach        return $this->preferences[$setting_name] ?? $default ?? self::DEFAULT_PREFERENCES[$setting_name] ?? '';
2106ccdf4f0SGreg Roach    }
2116ccdf4f0SGreg Roach
2126ccdf4f0SGreg Roach    /**
2136ccdf4f0SGreg Roach     * The name of this tree
2146ccdf4f0SGreg Roach     *
2156ccdf4f0SGreg Roach     * @return string
2166ccdf4f0SGreg Roach     */
2176ccdf4f0SGreg Roach    public function name(): string
2186ccdf4f0SGreg Roach    {
2196ccdf4f0SGreg Roach        return $this->name;
2206ccdf4f0SGreg Roach    }
2216ccdf4f0SGreg Roach
2226ccdf4f0SGreg Roach    /**
2236ccdf4f0SGreg Roach     * The title of this tree
2246ccdf4f0SGreg Roach     *
2256ccdf4f0SGreg Roach     * @return string
2266ccdf4f0SGreg Roach     */
2276ccdf4f0SGreg Roach    public function title(): string
2286ccdf4f0SGreg Roach    {
2296ccdf4f0SGreg Roach        return $this->title;
2306ccdf4f0SGreg Roach    }
2316ccdf4f0SGreg Roach
2326ccdf4f0SGreg Roach    /**
2336ccdf4f0SGreg Roach     * The fact-level privacy for this tree.
2346ccdf4f0SGreg Roach     *
23509482a55SGreg Roach     * @return array<int>
2366ccdf4f0SGreg Roach     */
2376ccdf4f0SGreg Roach    public function getFactPrivacy(): array
2386ccdf4f0SGreg Roach    {
2396ccdf4f0SGreg Roach        return $this->fact_privacy;
2406ccdf4f0SGreg Roach    }
2416ccdf4f0SGreg Roach
2426ccdf4f0SGreg Roach    /**
2436ccdf4f0SGreg Roach     * The individual-level privacy for this tree.
2446ccdf4f0SGreg Roach     *
24509482a55SGreg Roach     * @return array<int>
2466ccdf4f0SGreg Roach     */
2476ccdf4f0SGreg Roach    public function getIndividualPrivacy(): array
2486ccdf4f0SGreg Roach    {
2496ccdf4f0SGreg Roach        return $this->individual_privacy;
2506ccdf4f0SGreg Roach    }
2516ccdf4f0SGreg Roach
2526ccdf4f0SGreg Roach    /**
2536ccdf4f0SGreg Roach     * The individual-fact-level privacy for this tree.
2546ccdf4f0SGreg Roach     *
25509482a55SGreg Roach     * @return array<array<int>>
2566ccdf4f0SGreg Roach     */
2576ccdf4f0SGreg Roach    public function getIndividualFactPrivacy(): array
2586ccdf4f0SGreg Roach    {
2596ccdf4f0SGreg Roach        return $this->individual_fact_privacy;
2606ccdf4f0SGreg Roach    }
2616ccdf4f0SGreg Roach
2626ccdf4f0SGreg Roach    /**
2636ccdf4f0SGreg Roach     * Set the tree’s user-configuration settings.
2646ccdf4f0SGreg Roach     *
2656ccdf4f0SGreg Roach     * @param UserInterface $user
2666ccdf4f0SGreg Roach     * @param string        $setting_name
2676ccdf4f0SGreg Roach     * @param string        $setting_value
2686ccdf4f0SGreg Roach     *
2696612c384SGreg Roach     * @return self
2706ccdf4f0SGreg Roach     */
2716ccdf4f0SGreg Roach    public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree
2726ccdf4f0SGreg Roach    {
2736ccdf4f0SGreg Roach        if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
2746ccdf4f0SGreg Roach            // Update the database
2756ccdf4f0SGreg Roach            DB::table('user_gedcom_setting')->updateOrInsert([
2766ccdf4f0SGreg Roach                'gedcom_id'    => $this->id(),
2776ccdf4f0SGreg Roach                'user_id'      => $user->id(),
2786ccdf4f0SGreg Roach                'setting_name' => $setting_name,
2796ccdf4f0SGreg Roach            ], [
2806ccdf4f0SGreg Roach                'setting_value' => $setting_value,
2816ccdf4f0SGreg Roach            ]);
2826ccdf4f0SGreg Roach
2836ccdf4f0SGreg Roach            // Update the cache
2846ccdf4f0SGreg Roach            $this->user_preferences[$user->id()][$setting_name] = $setting_value;
2856ccdf4f0SGreg Roach            // Audit log of changes
2866ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this);
2876ccdf4f0SGreg Roach        }
2886ccdf4f0SGreg Roach
2896ccdf4f0SGreg Roach        return $this;
2906ccdf4f0SGreg Roach    }
2916ccdf4f0SGreg Roach
2926ccdf4f0SGreg Roach    /**
2936ccdf4f0SGreg Roach     * Get the tree’s user-configuration settings.
2946ccdf4f0SGreg Roach     *
2956ccdf4f0SGreg Roach     * @param UserInterface $user
2966ccdf4f0SGreg Roach     * @param string        $setting_name
2976ccdf4f0SGreg Roach     * @param string        $default
2986ccdf4f0SGreg Roach     *
2996ccdf4f0SGreg Roach     * @return string
3006ccdf4f0SGreg Roach     */
3016ccdf4f0SGreg Roach    public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string
3026ccdf4f0SGreg Roach    {
3036ccdf4f0SGreg Roach        // There are lots of settings, and we need to fetch lots of them on every page
3046ccdf4f0SGreg Roach        // so it is quicker to fetch them all in one go.
3056ccdf4f0SGreg Roach        if (!array_key_exists($user->id(), $this->user_preferences)) {
3066ccdf4f0SGreg Roach            $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting')
3076ccdf4f0SGreg Roach                ->where('user_id', '=', $user->id())
3086ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
3096ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
3106ccdf4f0SGreg Roach                ->all();
3116ccdf4f0SGreg Roach        }
3126ccdf4f0SGreg Roach
3136ccdf4f0SGreg Roach        return $this->user_preferences[$user->id()][$setting_name] ?? $default;
3146ccdf4f0SGreg Roach    }
3156ccdf4f0SGreg Roach
3166ccdf4f0SGreg Roach    /**
3176ccdf4f0SGreg Roach     * The ID of this tree
3186ccdf4f0SGreg Roach     *
3196ccdf4f0SGreg Roach     * @return int
3206ccdf4f0SGreg Roach     */
3216ccdf4f0SGreg Roach    public function id(): int
3226ccdf4f0SGreg Roach    {
3236ccdf4f0SGreg Roach        return $this->id;
3246ccdf4f0SGreg Roach    }
3256ccdf4f0SGreg Roach
3266ccdf4f0SGreg Roach    /**
3276ccdf4f0SGreg Roach     * Can a user accept changes for this tree?
3286ccdf4f0SGreg Roach     *
3296ccdf4f0SGreg Roach     * @param UserInterface $user
3306ccdf4f0SGreg Roach     *
3316ccdf4f0SGreg Roach     * @return bool
3326ccdf4f0SGreg Roach     */
3336ccdf4f0SGreg Roach    public function canAcceptChanges(UserInterface $user): bool
3346ccdf4f0SGreg Roach    {
3356ccdf4f0SGreg Roach        return Auth::isModerator($this, $user);
3366ccdf4f0SGreg Roach    }
3376ccdf4f0SGreg Roach
3386ccdf4f0SGreg Roach    /**
3390474d79bSGreg Roach     * Are there any pending edits for this tree, that need reviewing by a moderator.
340b78374c5SGreg Roach     *
341b78374c5SGreg Roach     * @return bool
342b78374c5SGreg Roach     */
343771ae10aSGreg Roach    public function hasPendingEdit(): bool
344c1010edaSGreg Roach    {
34515a3f100SGreg Roach        return DB::table('change')
34615a3f100SGreg Roach            ->where('gedcom_id', '=', $this->id)
34715a3f100SGreg Roach            ->where('status', '=', 'pending')
34815a3f100SGreg Roach            ->exists();
349b78374c5SGreg Roach    }
350b78374c5SGreg Roach
351b78374c5SGreg Roach    /**
3526ccdf4f0SGreg Roach     * Create a new record from GEDCOM data.
3536ccdf4f0SGreg Roach     *
3546ccdf4f0SGreg Roach     * @param string $gedcom
3556ccdf4f0SGreg Roach     *
3560d15532eSGreg Roach     * @return GedcomRecord|Individual|Family|Location|Note|Source|Repository|Media|Submitter|Submission
3576ccdf4f0SGreg Roach     * @throws InvalidArgumentException
3586ccdf4f0SGreg Roach     */
3596ccdf4f0SGreg Roach    public function createRecord(string $gedcom): GedcomRecord
3606ccdf4f0SGreg Roach    {
361ef475b14SGreg Roach        if (preg_match('/^0 @@ ([_A-Z]+)/', $gedcom, $match) !== 1) {
3626ccdf4f0SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@');
3636ccdf4f0SGreg Roach        }
3646ccdf4f0SGreg Roach
3656b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make($match[1]);
366dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
3676ccdf4f0SGreg Roach
3686ccdf4f0SGreg Roach        // Create a change record
36953432476SGreg Roach        $today = strtoupper(date('d M Y'));
37053432476SGreg Roach        $now   = date('H:i:s');
37153432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
3726ccdf4f0SGreg Roach
3736ccdf4f0SGreg Roach        // Create a pending change
3746ccdf4f0SGreg Roach        DB::table('change')->insert([
3756ccdf4f0SGreg Roach            'gedcom_id'  => $this->id,
3766ccdf4f0SGreg Roach            'xref'       => $xref,
3776ccdf4f0SGreg Roach            'old_gedcom' => '',
3786ccdf4f0SGreg Roach            'new_gedcom' => $gedcom,
3796ccdf4f0SGreg Roach            'user_id'    => Auth::id(),
3806ccdf4f0SGreg Roach        ]);
3816ccdf4f0SGreg Roach
3826ccdf4f0SGreg Roach        // Accept this pending change
3831fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
3846b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->new($xref, $gedcom, null, $this);
3856ccdf4f0SGreg Roach
386*d35568b4SGreg Roach            $pending_changes_service = Registry::container()->get(PendingChangesService::class);
387b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
38822e73debSGreg Roach
38922e73debSGreg Roach            return $record;
3906ccdf4f0SGreg Roach        }
3916ccdf4f0SGreg Roach
3926b9cb339SGreg Roach        return Registry::gedcomRecordFactory()->new($xref, '', $gedcom, $this);
393a25f0a04SGreg Roach    }
394304f20d5SGreg Roach
395304f20d5SGreg Roach    /**
396afb591d7SGreg Roach     * Create a new family from GEDCOM data.
397afb591d7SGreg Roach     *
398afb591d7SGreg Roach     * @param string $gedcom
399afb591d7SGreg Roach     *
400afb591d7SGreg Roach     * @return Family
401afb591d7SGreg Roach     * @throws InvalidArgumentException
402afb591d7SGreg Roach     */
403afb591d7SGreg Roach    public function createFamily(string $gedcom): GedcomRecord
404afb591d7SGreg Roach    {
405dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ FAM')) {
406afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM');
407afb591d7SGreg Roach        }
408afb591d7SGreg Roach
4096b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Family::RECORD_TYPE);
410dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
411afb591d7SGreg Roach
412afb591d7SGreg Roach        // Create a change record
41353432476SGreg Roach        $today = strtoupper(date('d M Y'));
41453432476SGreg Roach        $now   = date('H:i:s');
41553432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
416afb591d7SGreg Roach
417afb591d7SGreg Roach        // Create a pending change
418963fbaeeSGreg Roach        DB::table('change')->insert([
419963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
420963fbaeeSGreg Roach            'xref'       => $xref,
421963fbaeeSGreg Roach            'old_gedcom' => '',
422963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
423963fbaeeSGreg Roach            'user_id'    => Auth::id(),
424afb591d7SGreg Roach        ]);
425304f20d5SGreg Roach
426304f20d5SGreg Roach        // Accept this pending change
4271fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
4286b9cb339SGreg Roach            $record = Registry::familyFactory()->new($xref, $gedcom, null, $this);
429afb591d7SGreg Roach
430*d35568b4SGreg Roach            $pending_changes_service = Registry::container()->get(PendingChangesService::class);
431b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
43222e73debSGreg Roach
43322e73debSGreg Roach            return $record;
434304f20d5SGreg Roach        }
435afb591d7SGreg Roach
4366b9cb339SGreg Roach        return Registry::familyFactory()->new($xref, '', $gedcom, $this);
437afb591d7SGreg Roach    }
438afb591d7SGreg Roach
439afb591d7SGreg Roach    /**
440afb591d7SGreg Roach     * Create a new individual from GEDCOM data.
441afb591d7SGreg Roach     *
442afb591d7SGreg Roach     * @param string $gedcom
443afb591d7SGreg Roach     *
444afb591d7SGreg Roach     * @return Individual
445afb591d7SGreg Roach     * @throws InvalidArgumentException
446afb591d7SGreg Roach     */
447afb591d7SGreg Roach    public function createIndividual(string $gedcom): GedcomRecord
448afb591d7SGreg Roach    {
449dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ INDI')) {
450afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI');
451afb591d7SGreg Roach        }
452afb591d7SGreg Roach
4536b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Individual::RECORD_TYPE);
454dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
455afb591d7SGreg Roach
456afb591d7SGreg Roach        // Create a change record
45753432476SGreg Roach        $today = strtoupper(date('d M Y'));
45853432476SGreg Roach        $now   = date('H:i:s');
45953432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
460afb591d7SGreg Roach
461afb591d7SGreg Roach        // Create a pending change
462963fbaeeSGreg Roach        DB::table('change')->insert([
463963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
464963fbaeeSGreg Roach            'xref'       => $xref,
465963fbaeeSGreg Roach            'old_gedcom' => '',
466963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
467963fbaeeSGreg Roach            'user_id'    => Auth::id(),
468afb591d7SGreg Roach        ]);
469afb591d7SGreg Roach
470afb591d7SGreg Roach        // Accept this pending change
4711fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
4726b9cb339SGreg Roach            $record = Registry::individualFactory()->new($xref, $gedcom, null, $this);
473afb591d7SGreg Roach
474*d35568b4SGreg Roach            $pending_changes_service = Registry::container()->get(PendingChangesService::class);
475b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
47622e73debSGreg Roach
47722e73debSGreg Roach            return $record;
478afb591d7SGreg Roach        }
479afb591d7SGreg Roach
4806b9cb339SGreg Roach        return Registry::individualFactory()->new($xref, '', $gedcom, $this);
481304f20d5SGreg Roach    }
4828586983fSGreg Roach
4838586983fSGreg Roach    /**
48420b58d20SGreg Roach     * Create a new media object from GEDCOM data.
48520b58d20SGreg Roach     *
48620b58d20SGreg Roach     * @param string $gedcom
48720b58d20SGreg Roach     *
48820b58d20SGreg Roach     * @return Media
48920b58d20SGreg Roach     * @throws InvalidArgumentException
49020b58d20SGreg Roach     */
49120b58d20SGreg Roach    public function createMediaObject(string $gedcom): Media
49220b58d20SGreg Roach    {
493dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ OBJE')) {
49420b58d20SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE');
49520b58d20SGreg Roach        }
49620b58d20SGreg Roach
4976b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Media::RECORD_TYPE);
498dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
49920b58d20SGreg Roach
50020b58d20SGreg Roach        // Create a change record
50153432476SGreg Roach        $today = strtoupper(date('d M Y'));
50253432476SGreg Roach        $now   = date('H:i:s');
50353432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
50420b58d20SGreg Roach
50520b58d20SGreg Roach        // Create a pending change
506963fbaeeSGreg Roach        DB::table('change')->insert([
507963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
508963fbaeeSGreg Roach            'xref'       => $xref,
509963fbaeeSGreg Roach            'old_gedcom' => '',
510963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
511963fbaeeSGreg Roach            'user_id'    => Auth::id(),
51220b58d20SGreg Roach        ]);
51320b58d20SGreg Roach
51420b58d20SGreg Roach        // Accept this pending change
5151fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
5166b9cb339SGreg Roach            $record = Registry::mediaFactory()->new($xref, $gedcom, null, $this);
51720b58d20SGreg Roach
518*d35568b4SGreg Roach            $pending_changes_service = Registry::container()->get(PendingChangesService::class);
519b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
52022e73debSGreg Roach
52122e73debSGreg Roach            return $record;
52220b58d20SGreg Roach        }
52320b58d20SGreg Roach
5246b9cb339SGreg Roach        return Registry::mediaFactory()->new($xref, '', $gedcom, $this);
52520b58d20SGreg Roach    }
52620b58d20SGreg Roach
52720b58d20SGreg Roach    /**
5288586983fSGreg Roach     * What is the most significant individual in this tree.
5298586983fSGreg Roach     *
530e5a6b4d4SGreg Roach     * @param UserInterface $user
5313370567dSGreg Roach     * @param string        $xref
5328586983fSGreg Roach     *
5338586983fSGreg Roach     * @return Individual
5348586983fSGreg Roach     */
53573d58381SGreg Roach    public function significantIndividual(UserInterface $user, string $xref = ''): Individual
536c1010edaSGreg Roach    {
5373370567dSGreg Roach        if ($xref === '') {
5388f9b0fb2SGreg Roach            $individual = null;
5393370567dSGreg Roach        } else {
5406b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($xref, $this);
5413370567dSGreg Roach
5423370567dSGreg Roach            if ($individual === null) {
5436b9cb339SGreg Roach                $family = Registry::familyFactory()->make($xref, $this);
5443370567dSGreg Roach
5453370567dSGreg Roach                if ($family instanceof Family) {
5463370567dSGreg Roach                    $individual = $family->spouses()->first() ?? $family->children()->first();
5473370567dSGreg Roach                }
5483370567dSGreg Roach            }
5493370567dSGreg Roach        }
5508586983fSGreg Roach
5511fe542e9SGreg Roach        if ($individual === null && $this->getUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF) !== '') {
5521fe542e9SGreg Roach            $individual = Registry::individualFactory()->make($this->getUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF), $this);
5538586983fSGreg Roach        }
5548f9b0fb2SGreg Roach
5551fe542e9SGreg Roach        if ($individual === null && $this->getUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF) !== '') {
5561fe542e9SGreg Roach            $individual = Registry::individualFactory()->make($this->getUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF), $this);
5578586983fSGreg Roach        }
5588f9b0fb2SGreg Roach
559bec87e94SGreg Roach        if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') {
5606b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($this->getPreference('PEDIGREE_ROOT_ID'), $this);
5618586983fSGreg Roach        }
5628f9b0fb2SGreg Roach        if ($individual === null) {
563b55cbc6bSGreg Roach            $xref = DB::table('individuals')
5648f9b0fb2SGreg Roach                ->where('i_file', '=', $this->id())
5658f9b0fb2SGreg Roach                ->min('i_id');
566769d7d6eSGreg Roach
567b55cbc6bSGreg Roach            if (is_string($xref)) {
5686b9cb339SGreg Roach                $individual = Registry::individualFactory()->make($xref, $this);
5695fe1add5SGreg Roach            }
570b55cbc6bSGreg Roach        }
5718f9b0fb2SGreg Roach        if ($individual === null) {
5725fe1add5SGreg Roach            // always return a record
5736b9cb339SGreg Roach            $individual = Registry::individualFactory()->new('I', '0 @I@ INDI', null, $this);
5745fe1add5SGreg Roach        }
5755fe1add5SGreg Roach
5765fe1add5SGreg Roach        return $individual;
5775fe1add5SGreg Roach    }
5781df7ae39SGreg Roach
57985a166d8SGreg Roach    /**
58085a166d8SGreg Roach     * Where do we store our media files.
58185a166d8SGreg Roach     *
582f7cf8a15SGreg Roach     * @return FilesystemOperator
58385a166d8SGreg Roach     */
5849458f20aSGreg Roach    public function mediaFilesystem(): FilesystemOperator
5851df7ae39SGreg Roach    {
5869458f20aSGreg Roach        return Registry::filesystem()->data($this->getPreference('MEDIA_DIRECTORY'));
5871df7ae39SGreg Roach    }
588a25f0a04SGreg Roach}
589