xref: /webtrees/app/Tree.php (revision 9458f20a3c5e8e676f65488153a8d8a76b1773fb)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
55bfc6897SGreg 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;
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
291e653452SGreg Roachuse function app;
30dec352c1SGreg Roachuse function array_key_exists;
31b55cbc6bSGreg Roachuse function assert;
3253432476SGreg Roachuse function date;
33b55cbc6bSGreg Roachuse function is_string;
34dec352c1SGreg Roachuse function str_starts_with;
3553432476SGreg Roachuse function strtoupper;
36dec352c1SGreg Roachuse function substr_replace;
371e653452SGreg Roach
38a25f0a04SGreg Roach/**
3976692c8bSGreg Roach * Provide an interface to the wt_gedcom table.
40a25f0a04SGreg Roach */
41c1010edaSGreg Roachclass Tree
42c1010edaSGreg Roach{
43061b43d7SGreg Roach    private const RESN_PRIVACY = [
44061b43d7SGreg Roach        'none'         => Auth::PRIV_PRIVATE,
45061b43d7SGreg Roach        'privacy'      => Auth::PRIV_USER,
46061b43d7SGreg Roach        'confidential' => Auth::PRIV_NONE,
47061b43d7SGreg Roach        'hidden'       => Auth::PRIV_HIDE,
48061b43d7SGreg Roach    ];
493df1e584SGreg Roach
508a07c98eSGreg Roach
518a07c98eSGreg Roach    // Default values for some tree preferences.
528a07c98eSGreg Roach    protected const DEFAULT_PREFERENCES = [
538a07c98eSGreg Roach        'CALENDAR_FORMAT'              => 'gregorian',
548a07c98eSGreg Roach        'CHART_BOX_TAGS'               => '',
558a07c98eSGreg Roach        'EXPAND_SOURCES'               => '0',
569c7bc1e3SGreg Roach        'FAM_FACTS_QUICK'              => 'ENGA,MARR,DIV',
578a07c98eSGreg Roach        'FORMAT_TEXT'                  => 'markdown',
588a07c98eSGreg Roach        'GEDCOM_MEDIA_PATH'            => '',
598a07c98eSGreg Roach        'GENERATE_UIDS'                => '0',
608a07c98eSGreg Roach        'HIDE_GEDCOM_ERRORS'           => '1',
618a07c98eSGreg Roach        'HIDE_LIVE_PEOPLE'             => '1',
628a07c98eSGreg Roach        'INDI_FACTS_QUICK'             => 'BIRT,BURI,BAPM,CENS,DEAT,OCCU,RESI',
638a07c98eSGreg Roach        'KEEP_ALIVE_YEARS_BIRTH'       => '',
648a07c98eSGreg Roach        'KEEP_ALIVE_YEARS_DEATH'       => '',
658a07c98eSGreg Roach        'LANGUAGE'                     => 'en-US',
668a07c98eSGreg Roach        'MAX_ALIVE_AGE'                => '120',
678a07c98eSGreg Roach        'MEDIA_DIRECTORY'              => 'media/',
689062008fSGreg Roach        'MEDIA_UPLOAD'                 => '1', // Auth::PRIV_USER
698a07c98eSGreg Roach        'META_DESCRIPTION'             => '',
708a07c98eSGreg Roach        'META_TITLE'                   => Webtrees::NAME,
718a07c98eSGreg Roach        'NO_UPDATE_CHAN'               => '0',
728a07c98eSGreg Roach        'PEDIGREE_ROOT_ID'             => '',
738a07c98eSGreg Roach        'QUICK_REQUIRED_FACTS'         => 'BIRT,DEAT',
748a07c98eSGreg Roach        'QUICK_REQUIRED_FAMFACTS'      => 'MARR',
758a07c98eSGreg Roach        'REQUIRE_AUTHENTICATION'       => '0',
768a07c98eSGreg Roach        'SAVE_WATERMARK_IMAGE'         => '0',
778a07c98eSGreg Roach        'SHOW_AGE_DIFF'                => '0',
788a07c98eSGreg Roach        'SHOW_COUNTER'                 => '1',
799062008fSGreg Roach        'SHOW_DEAD_PEOPLE'             => '2', // Auth::PRIV_PRIVATE
808a07c98eSGreg Roach        'SHOW_EST_LIST_DATES'          => '0',
818a07c98eSGreg Roach        'SHOW_FACT_ICONS'              => '1',
828a07c98eSGreg Roach        'SHOW_GEDCOM_RECORD'           => '0',
838a07c98eSGreg Roach        'SHOW_HIGHLIGHT_IMAGES'        => '1',
848a07c98eSGreg Roach        'SHOW_LEVEL2_NOTES'            => '1',
859062008fSGreg Roach        'SHOW_LIVING_NAMES'            => '1', // Auth::PRIV_USER
868a07c98eSGreg Roach        'SHOW_MEDIA_DOWNLOAD'          => '0',
879062008fSGreg Roach        'SHOW_NO_WATERMARK'            => '1', // Auth::PRIV_USER
888a07c98eSGreg Roach        'SHOW_PARENTS_AGE'             => '1',
898a07c98eSGreg Roach        'SHOW_PEDIGREE_PLACES'         => '9',
908a07c98eSGreg Roach        'SHOW_PEDIGREE_PLACES_SUFFIX'  => '0',
918a07c98eSGreg Roach        'SHOW_PRIVATE_RELATIONSHIPS'   => '1',
928a07c98eSGreg Roach        'SHOW_RELATIVES_EVENTS'        => '_BIRT_CHIL,_BIRT_SIBL,_MARR_CHIL,_MARR_PARE,_DEAT_CHIL,_DEAT_PARE,_DEAT_GPAR,_DEAT_SIBL,_DEAT_SPOU',
938a07c98eSGreg Roach        'SUBLIST_TRIGGER_I'            => '200',
948a07c98eSGreg Roach        'SURNAME_LIST_STYLE'           => 'style2',
958a07c98eSGreg Roach        'SURNAME_TRADITION'            => 'paternal',
968a07c98eSGreg Roach        'USE_SILHOUETTE'               => '1',
978a07c98eSGreg Roach        'WORD_WRAPPED_NOTES'           => '0',
988a07c98eSGreg Roach    ];
998a07c98eSGreg Roach
1004c78e066SGreg Roach    private int $id;
1013df1e584SGreg Roach
1024c78e066SGreg Roach    private string $name;
1033df1e584SGreg Roach
1044c78e066SGreg Roach    private string $title;
1053df1e584SGreg Roach
1064c78e066SGreg Roach    /** @var array<int> Default access rules for facts in this tree */
1074c78e066SGreg Roach    private array $fact_privacy;
1083df1e584SGreg Roach
1094c78e066SGreg Roach    /** @var array<int> Default access rules for individuals in this tree */
1104c78e066SGreg Roach    private array $individual_privacy;
1113df1e584SGreg Roach
1124c78e066SGreg Roach    /** @var array<array<int>> Default access rules for individual facts in this tree */
1134c78e066SGreg Roach    private array $individual_fact_privacy;
1143df1e584SGreg Roach
11509482a55SGreg Roach    /** @var array<string> Cached copy of the wt_gedcom_setting table. */
1166ccdf4f0SGreg Roach    private $preferences = [];
1173df1e584SGreg Roach
11809482a55SGreg Roach    /** @var array<array<string>> Cached copy of the wt_user_gedcom_setting table. */
1194c78e066SGreg Roach    private array $user_preferences = [];
120061b43d7SGreg Roach
121a25f0a04SGreg Roach    /**
1223df1e584SGreg Roach     * Create a tree object.
123a25f0a04SGreg Roach     *
12472cf66d4SGreg Roach     * @param int    $id
125aa6f03bbSGreg Roach     * @param string $name
126cc13d6d8SGreg Roach     * @param string $title
127a25f0a04SGreg Roach     */
1285afbc57aSGreg Roach    public function __construct(int $id, string $name, string $title)
129c1010edaSGreg Roach    {
13072cf66d4SGreg Roach        $this->id                      = $id;
131aa6f03bbSGreg Roach        $this->name                    = $name;
132cc13d6d8SGreg Roach        $this->title                   = $title;
13313abd6f3SGreg Roach        $this->fact_privacy            = [];
13413abd6f3SGreg Roach        $this->individual_privacy      = [];
13513abd6f3SGreg Roach        $this->individual_fact_privacy = [];
136518bbdc1SGreg Roach
137518bbdc1SGreg Roach        // Load the privacy settings for this tree
138061b43d7SGreg Roach        $rows = DB::table('default_resn')
139061b43d7SGreg Roach            ->where('gedcom_id', '=', $this->id)
140061b43d7SGreg Roach            ->get();
141518bbdc1SGreg Roach
142518bbdc1SGreg Roach        foreach ($rows as $row) {
143061b43d7SGreg Roach            // Convert GEDCOM privacy restriction to a webtrees access level.
144061b43d7SGreg Roach            $row->resn = self::RESN_PRIVACY[$row->resn];
145061b43d7SGreg Roach
146518bbdc1SGreg Roach            if ($row->xref !== null) {
147518bbdc1SGreg Roach                if ($row->tag_type !== null) {
148b262b3d3SGreg Roach                    $this->individual_fact_privacy[$row->xref][$row->tag_type] = $row->resn;
149518bbdc1SGreg Roach                } else {
150b262b3d3SGreg Roach                    $this->individual_privacy[$row->xref] = $row->resn;
151518bbdc1SGreg Roach                }
152518bbdc1SGreg Roach            } else {
153b262b3d3SGreg Roach                $this->fact_privacy[$row->tag_type] = $row->resn;
154518bbdc1SGreg Roach            }
155518bbdc1SGreg Roach        }
156a25f0a04SGreg Roach    }
157a25f0a04SGreg Roach
158a25f0a04SGreg Roach    /**
1595afbc57aSGreg Roach     * A closure which will create a record from a database row.
1605afbc57aSGreg Roach     *
1615afbc57aSGreg Roach     * @return Closure
1625afbc57aSGreg Roach     */
1635afbc57aSGreg Roach    public static function rowMapper(): Closure
1645afbc57aSGreg Roach    {
165743491b8SGreg Roach        return static fn (object $row): Tree => new Tree((int) $row->tree_id, $row->tree_name, $row->tree_title);
1665afbc57aSGreg Roach    }
1675afbc57aSGreg Roach
1685afbc57aSGreg Roach    /**
1696ccdf4f0SGreg Roach     * Set the tree’s configuration settings.
1706ccdf4f0SGreg Roach     *
1716ccdf4f0SGreg Roach     * @param string $setting_name
1726ccdf4f0SGreg Roach     * @param string $setting_value
1736ccdf4f0SGreg Roach     *
1746612c384SGreg Roach     * @return self
1756ccdf4f0SGreg Roach     */
1766ccdf4f0SGreg Roach    public function setPreference(string $setting_name, string $setting_value): Tree
1776ccdf4f0SGreg Roach    {
1786ccdf4f0SGreg Roach        if ($setting_value !== $this->getPreference($setting_name)) {
1796ccdf4f0SGreg Roach            DB::table('gedcom_setting')->updateOrInsert([
1806ccdf4f0SGreg Roach                'gedcom_id'    => $this->id,
1816ccdf4f0SGreg Roach                'setting_name' => $setting_name,
1826ccdf4f0SGreg Roach            ], [
1836ccdf4f0SGreg Roach                'setting_value' => $setting_value,
1846ccdf4f0SGreg Roach            ]);
1856ccdf4f0SGreg Roach
1866ccdf4f0SGreg Roach            $this->preferences[$setting_name] = $setting_value;
1876ccdf4f0SGreg Roach
1886ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
1896ccdf4f0SGreg Roach        }
1906ccdf4f0SGreg Roach
1916ccdf4f0SGreg Roach        return $this;
1926ccdf4f0SGreg Roach    }
1936ccdf4f0SGreg Roach
1946ccdf4f0SGreg Roach    /**
1956ccdf4f0SGreg Roach     * Get the tree’s configuration settings.
1966ccdf4f0SGreg Roach     *
1976ccdf4f0SGreg Roach     * @param string      $setting_name
1989062008fSGreg Roach     * @param string|null $default
1996ccdf4f0SGreg Roach     *
2006ccdf4f0SGreg Roach     * @return string
2016ccdf4f0SGreg Roach     */
2029062008fSGreg Roach    public function getPreference(string $setting_name, string $default = null): string
2036ccdf4f0SGreg Roach    {
20454c1ab5eSGreg Roach        if ($this->preferences === []) {
2056ccdf4f0SGreg Roach            $this->preferences = DB::table('gedcom_setting')
2066ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
2076ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
2086ccdf4f0SGreg Roach                ->all();
2096ccdf4f0SGreg Roach        }
2106ccdf4f0SGreg Roach
2119062008fSGreg Roach        return $this->preferences[$setting_name] ?? $default ?? self::DEFAULT_PREFERENCES[$setting_name] ?? '';
2126ccdf4f0SGreg Roach    }
2136ccdf4f0SGreg Roach
2146ccdf4f0SGreg Roach    /**
2156ccdf4f0SGreg Roach     * The name of this tree
2166ccdf4f0SGreg Roach     *
2176ccdf4f0SGreg Roach     * @return string
2186ccdf4f0SGreg Roach     */
2196ccdf4f0SGreg Roach    public function name(): string
2206ccdf4f0SGreg Roach    {
2216ccdf4f0SGreg Roach        return $this->name;
2226ccdf4f0SGreg Roach    }
2236ccdf4f0SGreg Roach
2246ccdf4f0SGreg Roach    /**
2256ccdf4f0SGreg Roach     * The title of this tree
2266ccdf4f0SGreg Roach     *
2276ccdf4f0SGreg Roach     * @return string
2286ccdf4f0SGreg Roach     */
2296ccdf4f0SGreg Roach    public function title(): string
2306ccdf4f0SGreg Roach    {
2316ccdf4f0SGreg Roach        return $this->title;
2326ccdf4f0SGreg Roach    }
2336ccdf4f0SGreg Roach
2346ccdf4f0SGreg Roach    /**
2356ccdf4f0SGreg Roach     * The fact-level privacy for this tree.
2366ccdf4f0SGreg Roach     *
23709482a55SGreg Roach     * @return array<int>
2386ccdf4f0SGreg Roach     */
2396ccdf4f0SGreg Roach    public function getFactPrivacy(): array
2406ccdf4f0SGreg Roach    {
2416ccdf4f0SGreg Roach        return $this->fact_privacy;
2426ccdf4f0SGreg Roach    }
2436ccdf4f0SGreg Roach
2446ccdf4f0SGreg Roach    /**
2456ccdf4f0SGreg Roach     * The individual-level privacy for this tree.
2466ccdf4f0SGreg Roach     *
24709482a55SGreg Roach     * @return array<int>
2486ccdf4f0SGreg Roach     */
2496ccdf4f0SGreg Roach    public function getIndividualPrivacy(): array
2506ccdf4f0SGreg Roach    {
2516ccdf4f0SGreg Roach        return $this->individual_privacy;
2526ccdf4f0SGreg Roach    }
2536ccdf4f0SGreg Roach
2546ccdf4f0SGreg Roach    /**
2556ccdf4f0SGreg Roach     * The individual-fact-level privacy for this tree.
2566ccdf4f0SGreg Roach     *
25709482a55SGreg Roach     * @return array<array<int>>
2586ccdf4f0SGreg Roach     */
2596ccdf4f0SGreg Roach    public function getIndividualFactPrivacy(): array
2606ccdf4f0SGreg Roach    {
2616ccdf4f0SGreg Roach        return $this->individual_fact_privacy;
2626ccdf4f0SGreg Roach    }
2636ccdf4f0SGreg Roach
2646ccdf4f0SGreg Roach    /**
2656ccdf4f0SGreg Roach     * Set the tree’s user-configuration settings.
2666ccdf4f0SGreg Roach     *
2676ccdf4f0SGreg Roach     * @param UserInterface $user
2686ccdf4f0SGreg Roach     * @param string        $setting_name
2696ccdf4f0SGreg Roach     * @param string        $setting_value
2706ccdf4f0SGreg Roach     *
2716612c384SGreg Roach     * @return self
2726ccdf4f0SGreg Roach     */
2736ccdf4f0SGreg Roach    public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree
2746ccdf4f0SGreg Roach    {
2756ccdf4f0SGreg Roach        if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
2766ccdf4f0SGreg Roach            // Update the database
2776ccdf4f0SGreg Roach            DB::table('user_gedcom_setting')->updateOrInsert([
2786ccdf4f0SGreg Roach                'gedcom_id'    => $this->id(),
2796ccdf4f0SGreg Roach                'user_id'      => $user->id(),
2806ccdf4f0SGreg Roach                'setting_name' => $setting_name,
2816ccdf4f0SGreg Roach            ], [
2826ccdf4f0SGreg Roach                'setting_value' => $setting_value,
2836ccdf4f0SGreg Roach            ]);
2846ccdf4f0SGreg Roach
2856ccdf4f0SGreg Roach            // Update the cache
2866ccdf4f0SGreg Roach            $this->user_preferences[$user->id()][$setting_name] = $setting_value;
2876ccdf4f0SGreg Roach            // Audit log of changes
2886ccdf4f0SGreg Roach            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this);
2896ccdf4f0SGreg Roach        }
2906ccdf4f0SGreg Roach
2916ccdf4f0SGreg Roach        return $this;
2926ccdf4f0SGreg Roach    }
2936ccdf4f0SGreg Roach
2946ccdf4f0SGreg Roach    /**
2956ccdf4f0SGreg Roach     * Get the tree’s user-configuration settings.
2966ccdf4f0SGreg Roach     *
2976ccdf4f0SGreg Roach     * @param UserInterface $user
2986ccdf4f0SGreg Roach     * @param string        $setting_name
2996ccdf4f0SGreg Roach     * @param string        $default
3006ccdf4f0SGreg Roach     *
3016ccdf4f0SGreg Roach     * @return string
3026ccdf4f0SGreg Roach     */
3036ccdf4f0SGreg Roach    public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string
3046ccdf4f0SGreg Roach    {
3056ccdf4f0SGreg Roach        // There are lots of settings, and we need to fetch lots of them on every page
3066ccdf4f0SGreg Roach        // so it is quicker to fetch them all in one go.
3076ccdf4f0SGreg Roach        if (!array_key_exists($user->id(), $this->user_preferences)) {
3086ccdf4f0SGreg Roach            $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting')
3096ccdf4f0SGreg Roach                ->where('user_id', '=', $user->id())
3106ccdf4f0SGreg Roach                ->where('gedcom_id', '=', $this->id)
3116ccdf4f0SGreg Roach                ->pluck('setting_value', 'setting_name')
3126ccdf4f0SGreg Roach                ->all();
3136ccdf4f0SGreg Roach        }
3146ccdf4f0SGreg Roach
3156ccdf4f0SGreg Roach        return $this->user_preferences[$user->id()][$setting_name] ?? $default;
3166ccdf4f0SGreg Roach    }
3176ccdf4f0SGreg Roach
3186ccdf4f0SGreg Roach    /**
3196ccdf4f0SGreg Roach     * The ID of this tree
3206ccdf4f0SGreg Roach     *
3216ccdf4f0SGreg Roach     * @return int
3226ccdf4f0SGreg Roach     */
3236ccdf4f0SGreg Roach    public function id(): int
3246ccdf4f0SGreg Roach    {
3256ccdf4f0SGreg Roach        return $this->id;
3266ccdf4f0SGreg Roach    }
3276ccdf4f0SGreg Roach
3286ccdf4f0SGreg Roach    /**
3296ccdf4f0SGreg Roach     * Can a user accept changes for this tree?
3306ccdf4f0SGreg Roach     *
3316ccdf4f0SGreg Roach     * @param UserInterface $user
3326ccdf4f0SGreg Roach     *
3336ccdf4f0SGreg Roach     * @return bool
3346ccdf4f0SGreg Roach     */
3356ccdf4f0SGreg Roach    public function canAcceptChanges(UserInterface $user): bool
3366ccdf4f0SGreg Roach    {
3376ccdf4f0SGreg Roach        return Auth::isModerator($this, $user);
3386ccdf4f0SGreg Roach    }
3396ccdf4f0SGreg Roach
3406ccdf4f0SGreg Roach    /**
3410474d79bSGreg Roach     * Are there any pending edits for this tree, that need reviewing by a moderator.
342b78374c5SGreg Roach     *
343b78374c5SGreg Roach     * @return bool
344b78374c5SGreg Roach     */
345771ae10aSGreg Roach    public function hasPendingEdit(): bool
346c1010edaSGreg Roach    {
34715a3f100SGreg Roach        return DB::table('change')
34815a3f100SGreg Roach            ->where('gedcom_id', '=', $this->id)
34915a3f100SGreg Roach            ->where('status', '=', 'pending')
35015a3f100SGreg Roach            ->exists();
351b78374c5SGreg Roach    }
352b78374c5SGreg Roach
353b78374c5SGreg Roach    /**
3546ccdf4f0SGreg Roach     * Create a new record from GEDCOM data.
3556ccdf4f0SGreg Roach     *
3566ccdf4f0SGreg Roach     * @param string $gedcom
3576ccdf4f0SGreg Roach     *
3580d15532eSGreg Roach     * @return GedcomRecord|Individual|Family|Location|Note|Source|Repository|Media|Submitter|Submission
3596ccdf4f0SGreg Roach     * @throws InvalidArgumentException
3606ccdf4f0SGreg Roach     */
3616ccdf4f0SGreg Roach    public function createRecord(string $gedcom): GedcomRecord
3626ccdf4f0SGreg Roach    {
363ef475b14SGreg Roach        if (preg_match('/^0 @@ ([_A-Z]+)/', $gedcom, $match) !== 1) {
3646ccdf4f0SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@');
3656ccdf4f0SGreg Roach        }
3666ccdf4f0SGreg Roach
3676b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make($match[1]);
368dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
3696ccdf4f0SGreg Roach
3706ccdf4f0SGreg Roach        // Create a change record
37153432476SGreg Roach        $today = strtoupper(date('d M Y'));
37253432476SGreg Roach        $now   = date('H:i:s');
37353432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
3746ccdf4f0SGreg Roach
3756ccdf4f0SGreg Roach        // Create a pending change
3766ccdf4f0SGreg Roach        DB::table('change')->insert([
3776ccdf4f0SGreg Roach            'gedcom_id'  => $this->id,
3786ccdf4f0SGreg Roach            'xref'       => $xref,
3796ccdf4f0SGreg Roach            'old_gedcom' => '',
3806ccdf4f0SGreg Roach            'new_gedcom' => $gedcom,
3816ccdf4f0SGreg Roach            'user_id'    => Auth::id(),
3826ccdf4f0SGreg Roach        ]);
3836ccdf4f0SGreg Roach
3846ccdf4f0SGreg Roach        // Accept this pending change
3851fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
3866b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->new($xref, $gedcom, null, $this);
3876ccdf4f0SGreg Roach
388b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
389b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
390b55cbc6bSGreg Roach
391b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
39222e73debSGreg Roach
39322e73debSGreg Roach            return $record;
3946ccdf4f0SGreg Roach        }
3956ccdf4f0SGreg Roach
3966b9cb339SGreg Roach        return Registry::gedcomRecordFactory()->new($xref, '', $gedcom, $this);
397a25f0a04SGreg Roach    }
398304f20d5SGreg Roach
399304f20d5SGreg Roach    /**
400afb591d7SGreg Roach     * Create a new family from GEDCOM data.
401afb591d7SGreg Roach     *
402afb591d7SGreg Roach     * @param string $gedcom
403afb591d7SGreg Roach     *
404afb591d7SGreg Roach     * @return Family
405afb591d7SGreg Roach     * @throws InvalidArgumentException
406afb591d7SGreg Roach     */
407afb591d7SGreg Roach    public function createFamily(string $gedcom): GedcomRecord
408afb591d7SGreg Roach    {
409dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ FAM')) {
410afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM');
411afb591d7SGreg Roach        }
412afb591d7SGreg Roach
4136b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Family::RECORD_TYPE);
414dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
415afb591d7SGreg Roach
416afb591d7SGreg Roach        // Create a change record
41753432476SGreg Roach        $today = strtoupper(date('d M Y'));
41853432476SGreg Roach        $now   = date('H:i:s');
41953432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
420afb591d7SGreg Roach
421afb591d7SGreg Roach        // Create a pending change
422963fbaeeSGreg Roach        DB::table('change')->insert([
423963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
424963fbaeeSGreg Roach            'xref'       => $xref,
425963fbaeeSGreg Roach            'old_gedcom' => '',
426963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
427963fbaeeSGreg Roach            'user_id'    => Auth::id(),
428afb591d7SGreg Roach        ]);
429304f20d5SGreg Roach
430304f20d5SGreg Roach        // Accept this pending change
4311fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
4326b9cb339SGreg Roach            $record = Registry::familyFactory()->new($xref, $gedcom, null, $this);
433afb591d7SGreg Roach
434b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
435b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
436b55cbc6bSGreg Roach
437b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
43822e73debSGreg Roach
43922e73debSGreg Roach            return $record;
440304f20d5SGreg Roach        }
441afb591d7SGreg Roach
4426b9cb339SGreg Roach        return Registry::familyFactory()->new($xref, '', $gedcom, $this);
443afb591d7SGreg Roach    }
444afb591d7SGreg Roach
445afb591d7SGreg Roach    /**
446afb591d7SGreg Roach     * Create a new individual from GEDCOM data.
447afb591d7SGreg Roach     *
448afb591d7SGreg Roach     * @param string $gedcom
449afb591d7SGreg Roach     *
450afb591d7SGreg Roach     * @return Individual
451afb591d7SGreg Roach     * @throws InvalidArgumentException
452afb591d7SGreg Roach     */
453afb591d7SGreg Roach    public function createIndividual(string $gedcom): GedcomRecord
454afb591d7SGreg Roach    {
455dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ INDI')) {
456afb591d7SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI');
457afb591d7SGreg Roach        }
458afb591d7SGreg Roach
4596b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Individual::RECORD_TYPE);
460dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
461afb591d7SGreg Roach
462afb591d7SGreg Roach        // Create a change record
46353432476SGreg Roach        $today = strtoupper(date('d M Y'));
46453432476SGreg Roach        $now   = date('H:i:s');
46553432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
466afb591d7SGreg Roach
467afb591d7SGreg Roach        // Create a pending change
468963fbaeeSGreg Roach        DB::table('change')->insert([
469963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
470963fbaeeSGreg Roach            'xref'       => $xref,
471963fbaeeSGreg Roach            'old_gedcom' => '',
472963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
473963fbaeeSGreg Roach            'user_id'    => Auth::id(),
474afb591d7SGreg Roach        ]);
475afb591d7SGreg Roach
476afb591d7SGreg Roach        // Accept this pending change
4771fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
4786b9cb339SGreg Roach            $record = Registry::individualFactory()->new($xref, $gedcom, null, $this);
479afb591d7SGreg Roach
480b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
481b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
482b55cbc6bSGreg Roach
483b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
48422e73debSGreg Roach
48522e73debSGreg Roach            return $record;
486afb591d7SGreg Roach        }
487afb591d7SGreg Roach
4886b9cb339SGreg Roach        return Registry::individualFactory()->new($xref, '', $gedcom, $this);
489304f20d5SGreg Roach    }
4908586983fSGreg Roach
4918586983fSGreg Roach    /**
49220b58d20SGreg Roach     * Create a new media object from GEDCOM data.
49320b58d20SGreg Roach     *
49420b58d20SGreg Roach     * @param string $gedcom
49520b58d20SGreg Roach     *
49620b58d20SGreg Roach     * @return Media
49720b58d20SGreg Roach     * @throws InvalidArgumentException
49820b58d20SGreg Roach     */
49920b58d20SGreg Roach    public function createMediaObject(string $gedcom): Media
50020b58d20SGreg Roach    {
501dec352c1SGreg Roach        if (!str_starts_with($gedcom, '0 @@ OBJE')) {
50220b58d20SGreg Roach            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE');
50320b58d20SGreg Roach        }
50420b58d20SGreg Roach
5056b9cb339SGreg Roach        $xref   = Registry::xrefFactory()->make(Media::RECORD_TYPE);
506dec352c1SGreg Roach        $gedcom = substr_replace($gedcom, $xref, 3, 0);
50720b58d20SGreg Roach
50820b58d20SGreg Roach        // Create a change record
50953432476SGreg Roach        $today = strtoupper(date('d M Y'));
51053432476SGreg Roach        $now   = date('H:i:s');
51153432476SGreg Roach        $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName();
51220b58d20SGreg Roach
51320b58d20SGreg Roach        // Create a pending change
514963fbaeeSGreg Roach        DB::table('change')->insert([
515963fbaeeSGreg Roach            'gedcom_id'  => $this->id,
516963fbaeeSGreg Roach            'xref'       => $xref,
517963fbaeeSGreg Roach            'old_gedcom' => '',
518963fbaeeSGreg Roach            'new_gedcom' => $gedcom,
519963fbaeeSGreg Roach            'user_id'    => Auth::id(),
52020b58d20SGreg Roach        ]);
52120b58d20SGreg Roach
52220b58d20SGreg Roach        // Accept this pending change
5231fe542e9SGreg Roach        if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') {
5246b9cb339SGreg Roach            $record = Registry::mediaFactory()->new($xref, $gedcom, null, $this);
52520b58d20SGreg Roach
526b55cbc6bSGreg Roach            $pending_changes_service = app(PendingChangesService::class);
527b55cbc6bSGreg Roach            assert($pending_changes_service instanceof PendingChangesService);
528b55cbc6bSGreg Roach
529b55cbc6bSGreg Roach            $pending_changes_service->acceptRecord($record);
53022e73debSGreg Roach
53122e73debSGreg Roach            return $record;
53220b58d20SGreg Roach        }
53320b58d20SGreg Roach
5346b9cb339SGreg Roach        return Registry::mediaFactory()->new($xref, '', $gedcom, $this);
53520b58d20SGreg Roach    }
53620b58d20SGreg Roach
53720b58d20SGreg Roach    /**
5388586983fSGreg Roach     * What is the most significant individual in this tree.
5398586983fSGreg Roach     *
540e5a6b4d4SGreg Roach     * @param UserInterface $user
5413370567dSGreg Roach     * @param string        $xref
5428586983fSGreg Roach     *
5438586983fSGreg Roach     * @return Individual
5448586983fSGreg Roach     */
54573d58381SGreg Roach    public function significantIndividual(UserInterface $user, string $xref = ''): Individual
546c1010edaSGreg Roach    {
5473370567dSGreg Roach        if ($xref === '') {
5488f9b0fb2SGreg Roach            $individual = null;
5493370567dSGreg Roach        } else {
5506b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($xref, $this);
5513370567dSGreg Roach
5523370567dSGreg Roach            if ($individual === null) {
5536b9cb339SGreg Roach                $family = Registry::familyFactory()->make($xref, $this);
5543370567dSGreg Roach
5553370567dSGreg Roach                if ($family instanceof Family) {
5563370567dSGreg Roach                    $individual = $family->spouses()->first() ?? $family->children()->first();
5573370567dSGreg Roach                }
5583370567dSGreg Roach            }
5593370567dSGreg Roach        }
5608586983fSGreg Roach
5611fe542e9SGreg Roach        if ($individual === null && $this->getUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF) !== '') {
5621fe542e9SGreg Roach            $individual = Registry::individualFactory()->make($this->getUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF), $this);
5638586983fSGreg Roach        }
5648f9b0fb2SGreg Roach
5651fe542e9SGreg Roach        if ($individual === null && $this->getUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF) !== '') {
5661fe542e9SGreg Roach            $individual = Registry::individualFactory()->make($this->getUserPreference($user, UserInterface::PREF_TREE_ACCOUNT_XREF), $this);
5678586983fSGreg Roach        }
5688f9b0fb2SGreg Roach
569bec87e94SGreg Roach        if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') {
5706b9cb339SGreg Roach            $individual = Registry::individualFactory()->make($this->getPreference('PEDIGREE_ROOT_ID'), $this);
5718586983fSGreg Roach        }
5728f9b0fb2SGreg Roach        if ($individual === null) {
573b55cbc6bSGreg Roach            $xref = DB::table('individuals')
5748f9b0fb2SGreg Roach                ->where('i_file', '=', $this->id())
5758f9b0fb2SGreg Roach                ->min('i_id');
576769d7d6eSGreg Roach
577b55cbc6bSGreg Roach            if (is_string($xref)) {
5786b9cb339SGreg Roach                $individual = Registry::individualFactory()->make($xref, $this);
5795fe1add5SGreg Roach            }
580b55cbc6bSGreg Roach        }
5818f9b0fb2SGreg Roach        if ($individual === null) {
5825fe1add5SGreg Roach            // always return a record
5836b9cb339SGreg Roach            $individual = Registry::individualFactory()->new('I', '0 @I@ INDI', null, $this);
5845fe1add5SGreg Roach        }
5855fe1add5SGreg Roach
5865fe1add5SGreg Roach        return $individual;
5875fe1add5SGreg Roach    }
5881df7ae39SGreg Roach
58985a166d8SGreg Roach    /**
59085a166d8SGreg Roach     * Where do we store our media files.
59185a166d8SGreg Roach     *
592f7cf8a15SGreg Roach     * @return FilesystemOperator
59385a166d8SGreg Roach     */
594*9458f20aSGreg Roach    public function mediaFilesystem(): FilesystemOperator
5951df7ae39SGreg Roach    {
596*9458f20aSGreg Roach        return Registry::filesystem()->data($this->getPreference('MEDIA_DIRECTORY'));
5971df7ae39SGreg Roach    }
598a25f0a04SGreg Roach}
599