xref: /webtrees/app/Tree.php (revision 895230eed7521b5cd885b90d4f5310405ff0b69a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Fisharebest\Webtrees\Functions\FunctionsExport;
21use Fisharebest\Webtrees\Functions\FunctionsImport;
22use Illuminate\Database\Capsule\Manager as DB;
23use Illuminate\Database\Query\Builder;
24use Illuminate\Database\Query\JoinClause;
25use Illuminate\Support\Collection;
26use Illuminate\Support\Str;
27use InvalidArgumentException;
28use PDOException;
29use stdClass;
30
31/**
32 * Provide an interface to the wt_gedcom table.
33 */
34class Tree
35{
36    /** @var int The tree's ID number */
37    private $id;
38
39    /** @var string The tree's name */
40    private $name;
41
42    /** @var string The tree's title */
43    private $title;
44
45    /** @var int[] Default access rules for facts in this tree */
46    private $fact_privacy;
47
48    /** @var int[] Default access rules for individuals in this tree */
49    private $individual_privacy;
50
51    /** @var integer[][] Default access rules for individual facts in this tree */
52    private $individual_fact_privacy;
53
54    /** @var Tree[] All trees that we have permission to see, indexed by ID. */
55    public static $trees = [];
56
57    /** @var string[] Cached copy of the wt_gedcom_setting table. */
58    private $preferences = [];
59
60    /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */
61    private $user_preferences = [];
62
63    private const RESN_PRIVACY = [
64        'none'         => Auth::PRIV_PRIVATE,
65        'privacy'      => Auth::PRIV_USER,
66        'confidential' => Auth::PRIV_NONE,
67        'hidden'       => Auth::PRIV_HIDE,
68    ];
69
70    /**
71     * Create a tree object. This is a private constructor - it can only
72     * be called from Tree::getAll() to ensure proper initialisation.
73     *
74     * @param int    $id
75     * @param string $name
76     * @param string $title
77     */
78    private function __construct($id, $name, $title)
79    {
80        $this->id                      = $id;
81        $this->name                    = $name;
82        $this->title                   = $title;
83        $this->fact_privacy            = [];
84        $this->individual_privacy      = [];
85        $this->individual_fact_privacy = [];
86
87        // Load the privacy settings for this tree
88        $rows = DB::table('default_resn')
89            ->where('gedcom_id', '=', $this->id)
90            ->get();
91
92        foreach ($rows as $row) {
93            // Convert GEDCOM privacy restriction to a webtrees access level.
94            $row->resn = self::RESN_PRIVACY[$row->resn];
95
96            if ($row->xref !== null) {
97                if ($row->tag_type !== null) {
98                    $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn;
99                } else {
100                    $this->individual_privacy[$row->xref] = (int) $row->resn;
101                }
102            } else {
103                $this->fact_privacy[$row->tag_type] = (int) $row->resn;
104            }
105        }
106    }
107
108    /**
109     * The ID of this tree
110     *
111     * @return int
112     */
113    public function id(): int
114    {
115        return $this->id;
116    }
117
118    /**
119     * The name of this tree
120     *
121     * @return string
122     */
123    public function name(): string
124    {
125        return $this->name;
126    }
127
128    /**
129     * The title of this tree
130     *
131     * @return string
132     */
133    public function title(): string
134    {
135        return $this->title;
136    }
137
138    /**
139     * The fact-level privacy for this tree.
140     *
141     * @return int[]
142     */
143    public function getFactPrivacy(): array
144    {
145        return $this->fact_privacy;
146    }
147
148    /**
149     * The individual-level privacy for this tree.
150     *
151     * @return int[]
152     */
153    public function getIndividualPrivacy(): array
154    {
155        return $this->individual_privacy;
156    }
157
158    /**
159     * The individual-fact-level privacy for this tree.
160     *
161     * @return int[][]
162     */
163    public function getIndividualFactPrivacy(): array
164    {
165        return $this->individual_fact_privacy;
166    }
167
168    /**
169     * Get the tree’s configuration settings.
170     *
171     * @param string $setting_name
172     * @param string $default
173     *
174     * @return string
175     */
176    public function getPreference(string $setting_name, string $default = ''): string
177    {
178        if (empty($this->preferences)) {
179            $this->preferences = DB::table('gedcom_setting')
180                ->where('gedcom_id', '=', $this->id)
181                ->pluck('setting_value', 'setting_name')
182                ->all();
183        }
184
185        return $this->preferences[$setting_name] ?? $default;
186    }
187
188    /**
189     * Set the tree’s configuration settings.
190     *
191     * @param string $setting_name
192     * @param string $setting_value
193     *
194     * @return $this
195     */
196    public function setPreference(string $setting_name, string $setting_value): Tree
197    {
198        if ($setting_value !== $this->getPreference($setting_name)) {
199            DB::table('gedcom_setting')->updateOrInsert([
200                'gedcom_id'    => $this->id,
201                'setting_name' => $setting_name,
202            ], [
203                'setting_value' => $setting_value,
204            ]);
205
206            $this->preferences[$setting_name] = $setting_value;
207
208            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
209        }
210
211        return $this;
212    }
213
214    /**
215     * Get the tree’s user-configuration settings.
216     *
217     * @param User   $user
218     * @param string $setting_name
219     * @param string $default
220     *
221     * @return string
222     */
223    public function getUserPreference(User $user, string $setting_name, string $default = ''): string
224    {
225        // There are lots of settings, and we need to fetch lots of them on every page
226        // so it is quicker to fetch them all in one go.
227        if (!array_key_exists($user->id(), $this->user_preferences)) {
228            $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting')
229                ->where('user_id', '=', $user->id())
230                ->where('gedcom_id', '=', $this->id)
231                ->pluck('setting_value', 'setting_name')
232                ->all();
233        }
234
235        return $this->user_preferences[$user->id()][$setting_name] ?? $default;
236    }
237
238    /**
239     * Set the tree’s user-configuration settings.
240     *
241     * @param User   $user
242     * @param string $setting_name
243     * @param string $setting_value
244     *
245     * @return $this
246     */
247    public function setUserPreference(User $user, string $setting_name, string $setting_value): Tree
248    {
249        if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
250            // Update the database
251            DB::table('user_gedcom_setting')->updateOrInsert([
252                'gedcom_id'    => $this->id(),
253                'user_id'      => $user->id(),
254                'setting_name' => $setting_name,
255            ], [
256                'setting_value' => $setting_value,
257            ]);
258
259            // Update the cache
260            $this->user_preferences[$user->id()][$setting_name] = $setting_value;
261            // Audit log of changes
262            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this);
263        }
264
265        return $this;
266    }
267
268    /**
269     * Can a user accept changes for this tree?
270     *
271     * @param User $user
272     *
273     * @return bool
274     */
275    public function canAcceptChanges(User $user): bool
276    {
277        return Auth::isModerator($this, $user);
278    }
279
280    /**
281     * All the trees that we have permission to access.
282     *
283     * @return Collection|Tree[]
284     */
285    public static function all(): Collection
286    {
287        return app('cache.array')->rememberForever(__CLASS__, function () {
288            // Admins see all trees
289            $query = DB::table('gedcom')
290                ->leftJoin('gedcom_setting', function (JoinClause $join): void {
291                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
292                        ->where('gedcom_setting.setting_name', '=', 'title');
293                })
294                ->where('gedcom.gedcom_id', '>', 0)
295                ->select([
296                    'gedcom.gedcom_id AS tree_id',
297                    'gedcom.gedcom_name AS tree_name',
298                    'gedcom_setting.setting_value AS tree_title',
299                ])
300                ->orderBy('gedcom.sort_order')
301                ->orderBy('gedcom_setting.setting_value');
302
303            // Non-admins may not see all trees
304            if (!Auth::isAdmin()) {
305                $query
306                    ->join('gedcom_setting AS gs2', function (JoinClause $join): void {
307                        $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
308                            ->where('gs2.setting_name', '=', 'imported');
309                    })
310                    ->join('gedcom_setting AS gs3', function (JoinClause $join): void {
311                        $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
312                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
313                    })
314                    ->leftJoin('user_gedcom_setting', function (JoinClause $join): void {
315                        $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
316                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
317                            ->where('user_gedcom_setting.setting_name', '=', 'canedit');
318                    })
319                    ->where(function (Builder $query): void {
320                        $query
321                            // Managers
322                            ->where('user_gedcom_setting.setting_value', '=', 'admin')
323                            // Members
324                            ->orWhere(function (Builder $query): void {
325                                $query
326                                    ->where('gs2.setting_value', '=', '1')
327                                    ->where('gs3.setting_value', '=', '1')
328                                    ->where('user_gedcom_setting.setting_value', '<>', 'none');
329                            })
330                            // Public trees
331                            ->orWhere(function (Builder $query): void {
332                                $query
333                                    ->where('gs2.setting_value', '=', '1')
334                                    ->where('gs3.setting_value', '<>', '1');
335                            });
336                    });
337            }
338
339            return $query
340                ->get()
341                ->mapWithKeys(function (stdClass $row): array {
342                    return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)];
343                });
344        });
345    }
346
347    /**
348     * Fetch all the trees that we have permission to access.
349     *
350     * @return Tree[]
351     */
352    public static function getAll(): array
353    {
354        if (empty(self::$trees)) {
355            self::$trees = self::all()->all();
356        }
357
358        return self::$trees;
359    }
360
361    /**
362     * Find the tree with a specific ID.
363     *
364     * @param int $tree_id
365     *
366     * @return Tree
367     */
368    public static function findById(int $tree_id): Tree
369    {
370        return self::getAll()[$tree_id];
371    }
372
373    /**
374     * Find the tree with a specific name.
375     *
376     * @param string $tree_name
377     *
378     * @return Tree|null
379     */
380    public static function findByName($tree_name)
381    {
382        foreach (self::getAll() as $tree) {
383            if ($tree->name === $tree_name) {
384                return $tree;
385            }
386        }
387
388        return null;
389    }
390
391    /**
392     * Create arguments to select_edit_control()
393     * Note - these will be escaped later
394     *
395     * @return string[]
396     */
397    public static function getIdList(): array
398    {
399        $list = [];
400        foreach (self::getAll() as $tree) {
401            $list[$tree->id] = $tree->title;
402        }
403
404        return $list;
405    }
406
407    /**
408     * Create arguments to select_edit_control()
409     * Note - these will be escaped later
410     *
411     * @return string[]
412     */
413    public static function getNameList(): array
414    {
415        $list = [];
416        foreach (self::getAll() as $tree) {
417            $list[$tree->name] = $tree->title;
418        }
419
420        return $list;
421    }
422
423    /**
424     * Create a new tree
425     *
426     * @param string $tree_name
427     * @param string $tree_title
428     *
429     * @return Tree
430     */
431    public static function create(string $tree_name, string $tree_title): Tree
432    {
433        try {
434            // Create a new tree
435            DB::table('gedcom')->insert([
436                'gedcom_name' => $tree_name,
437            ]);
438
439            $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
440
441            $tree = new self($tree_id, $tree_name, $tree_title);
442        } catch (PDOException $ex) {
443            // A tree with that name already exists?
444            return self::findByName($tree_name);
445        }
446
447        $tree->setPreference('imported', '0');
448        $tree->setPreference('title', $tree_title);
449
450        // Set preferences from default tree
451        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
452            ['gedcom_id', 'setting_name', 'setting_value'],
453            function (Builder $query) use ($tree_id): void {
454                $query
455                    ->select([DB::raw($tree_id), 'setting_name', 'setting_value'])
456                    ->from('gedcom_setting')
457                    ->where('gedcom_id', '=', -1);
458            }
459        );
460
461        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
462            ['gedcom_id', 'tag_type', 'resn'],
463            function (Builder $query) use ($tree_id): void {
464                $query
465                    ->select([DB::raw($tree_id), 'tag_type', 'resn'])
466                    ->from('default_resn')
467                    ->where('gedcom_id', '=', -1);
468            }
469        );
470
471        (new Builder(DB::connection()))->from('block')->insertUsing(
472            ['gedcom_id', 'location', 'block_order', 'module_name'],
473            function (Builder $query) use ($tree_id): void {
474                $query
475                    ->select([DB::raw($tree_id), 'location', 'block_order', 'module_name'])
476                    ->from('block')
477                    ->where('gedcom_id', '=', -1);
478            }
479        );
480
481        // Gedcom and privacy settings
482        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
483        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
484        $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language
485        switch (WT_LOCALE) {
486            case 'es':
487                $tree->setPreference('SURNAME_TRADITION', 'spanish');
488                break;
489            case 'is':
490                $tree->setPreference('SURNAME_TRADITION', 'icelandic');
491                break;
492            case 'lt':
493                $tree->setPreference('SURNAME_TRADITION', 'lithuanian');
494                break;
495            case 'pl':
496                $tree->setPreference('SURNAME_TRADITION', 'polish');
497                break;
498            case 'pt':
499            case 'pt-BR':
500                $tree->setPreference('SURNAME_TRADITION', 'portuguese');
501                break;
502            default:
503                $tree->setPreference('SURNAME_TRADITION', 'paternal');
504                break;
505        }
506
507        // Genealogy data
508        // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...
509        /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */
510        $john_doe = I18N::translate('John /DOE/');
511        $note     = I18N::translate('Edit this individual and replace their details with your own.');
512        $gedcom   = "0 HEAD\n1 CHAR UTF-8\n0 @X1@ INDI\n1 NAME {$john_doe}\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE {$note}\n0 TRLR\n";
513
514        DB::table('gedcom_chunk')->insert([
515            'gedcom_id'  => $tree_id,
516            'chunk_data' => $gedcom,
517        ]);
518
519        // Update our cache
520        self::$trees[$tree->id] = $tree;
521
522        return $tree;
523    }
524
525    /**
526     * Are there any pending edits for this tree, than need reviewing by a moderator.
527     *
528     * @return bool
529     */
530    public function hasPendingEdit(): bool
531    {
532        return DB::table('change')
533            ->where('gedcom_id', '=', $this->id)
534            ->where('status', '=', 'pending')
535            ->exists();
536    }
537
538    /**
539     * Delete all the genealogy data from a tree - in preparation for importing
540     * new data. Optionally retain the media data, for when the user has been
541     * editing their data offline using an application which deletes (or does not
542     * support) media data.
543     *
544     * @param bool $keep_media
545     *
546     * @return void
547     */
548    public function deleteGenealogyData(bool $keep_media)
549    {
550        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
551        DB::table('individuals')->where('i_file', '=', $this->id)->delete();
552        DB::table('families')->where('f_file', '=', $this->id)->delete();
553        DB::table('sources')->where('s_file', '=', $this->id)->delete();
554        DB::table('other')->where('o_file', '=', $this->id)->delete();
555        DB::table('places')->where('p_file', '=', $this->id)->delete();
556        DB::table('placelinks')->where('pl_file', '=', $this->id)->delete();
557        DB::table('name')->where('n_file', '=', $this->id)->delete();
558        DB::table('dates')->where('d_file', '=', $this->id)->delete();
559        DB::table('change')->where('gedcom_id', '=', $this->id)->delete();
560
561        if ($keep_media) {
562            DB::table('link')->where('l_file', '=', $this->id)
563                ->where('l_type', '<>', 'OBJE')
564                ->delete();
565        } else {
566            DB::table('link')->where('l_file', '=', $this->id)->delete();
567            DB::table('media_file')->where('m_file', '=', $this->id)->delete();
568            DB::table('media')->where('m_file', '=', $this->id)->delete();
569        }
570    }
571
572    /**
573     * Delete everything relating to a tree
574     *
575     * @return void
576     */
577    public function delete()
578    {
579        // If this is the default tree, then unset it
580        if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) {
581            Site::setPreference('DEFAULT_GEDCOM', '');
582        }
583
584        $this->deleteGenealogyData(false);
585
586        DB::table('block_setting')
587            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
588            ->where('gedcom_id', '=', $this->id)
589            ->delete();
590        DB::table('block')->where('gedcom_id', '=', $this->id)->delete();
591        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
592        DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
593        DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete();
594        DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete();
595        DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete();
596        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
597        DB::table('log')->where('gedcom_id', '=', $this->id)->delete();
598        DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete();
599
600        // After updating the database, we need to fetch a new (sorted) copy
601        self::$trees = [];
602    }
603
604    /**
605     * Export the tree to a GEDCOM file
606     *
607     * @param resource $stream
608     *
609     * @return void
610     */
611    public function exportGedcom($stream)
612    {
613        $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8'));
614
615        $union_families = DB::table('families')
616            ->where('f_file', '=', $this->id)
617            ->select(['f_gedcom AS gedcom', 'f_id AS xref', DB::raw('LENGTH(f_id) AS len'), DB::raw('2 AS n')]);
618
619        $union_sources = DB::table('sources')
620            ->where('s_file', '=', $this->id)
621            ->select(['s_gedcom AS gedcom', 's_id AS xref', DB::raw('LENGTH(s_id) AS len'), DB::raw('3 AS n')]);
622
623        $union_other = DB::table('other')
624            ->where('o_file', '=', $this->id)
625            ->whereNotIn('o_type', ['HEAD', 'TRLR'])
626            ->select(['o_gedcom AS gedcom', 'o_id AS xref', DB::raw('LENGTH(o_id) AS len'), DB::raw('4 AS n')]);
627
628        $union_media = DB::table('media')
629            ->where('m_file', '=', $this->id)
630            ->select(['m_gedcom AS gedcom', 'm_id AS xref', DB::raw('LENGTH(m_id) AS len'), DB::raw('5 AS n')]);
631
632        $rows = DB::table('individuals')
633            ->where('i_file', '=', $this->id)
634            ->select(['i_gedcom AS gedcom', 'i_id AS xref', DB::raw('LENGTH(i_id) AS len'), DB::raw('1 AS n')])
635            ->union($union_families)
636            ->union($union_sources)
637            ->union($union_other)
638            ->union($union_media)
639            ->orderBy('n')
640            ->orderBy('len')
641            ->orderBy('xref')
642            ->chunk(100, function (Collection $rows) use ($stream, &$buffer): void {
643                foreach ($rows as $row) {
644                    $buffer .= FunctionsExport::reformatRecord($row->gedcom);
645                    if (strlen($buffer) > 65535) {
646                        fwrite($stream, $buffer);
647                        $buffer = '';
648                    }
649                }
650            });
651
652        fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL);
653    }
654
655    /**
656     * Import data from a gedcom file into this tree.
657     *
658     * @param string $path     The full path to the (possibly temporary) file.
659     * @param string $filename The preferred filename, for export/download.
660     *
661     * @return void
662     */
663    public function importGedcomFile(string $path, string $filename)
664    {
665        // Read the file in blocks of roughly 64K. Ensure that each block
666        // contains complete gedcom records. This will ensure we don’t split
667        // multi-byte characters, as well as simplifying the code to import
668        // each block.
669
670        $file_data = '';
671        $fp        = fopen($path, 'rb');
672
673        $this->deleteGenealogyData((bool) $this->getPreference('keep_media'));
674        $this->setPreference('gedcom_filename', $filename);
675        $this->setPreference('imported', '0');
676
677        while (!feof($fp)) {
678            $file_data .= fread($fp, 65536);
679            // There is no strrpos() function that searches for substrings :-(
680            for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) {
681                if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) {
682                    // We’ve found the last record boundary in this chunk of data
683                    break;
684                }
685            }
686            if ($pos) {
687                DB::table('gedcom_chunk')->insert([
688                    'gedcom_id'  => $this->id,
689                    'chunk_data' => substr($file_data, 0, $pos),
690                ]);
691
692                $file_data = substr($file_data, $pos);
693            }
694        }
695        DB::table('gedcom_chunk')->insert([
696            'gedcom_id'  => $this->id,
697            'chunk_data' => $file_data,
698        ]);
699
700        fclose($fp);
701    }
702
703    /**
704     * Generate a new XREF, unique across all family trees
705     *
706     * @return string
707     */
708    public function getNewXref(): string
709    {
710        // Lock the row, so that only one new XREF may be generated at a time.
711        DB::table('site_setting')
712            ->where('setting_name', '=', 'next_xref')
713            ->lockForUpdate()
714            ->get();
715
716        $prefix = 'X';
717
718        $increment = 1.0;
719        do {
720            $num = (int) Site::getPreference('next_xref') + (int) $increment;
721
722            // This exponential increment allows us to scan over large blocks of
723            // existing data in a reasonable time.
724            $increment *= 1.01;
725
726            $xref = $prefix . $num;
727
728            // Records may already exist with this sequence number.
729            $already_used =
730                DB::table('individuals')->where('i_id', '=', $xref)->exists() ||
731                DB::table('families')->where('f_id', '=', $xref)->exists() ||
732                DB::table('sources')->where('s_id', '=', $xref)->exists() ||
733                DB::table('media')->where('m_id', '=', $xref)->exists() ||
734                DB::table('other')->where('o_id', '=', $xref)->exists() ||
735                DB::table('change')->where('xref', '=', $xref)->exists();
736        } while ($already_used);
737
738        Site::setPreference('next_xref', (string) $num);
739
740        return $xref;
741    }
742
743    /**
744     * Create a new record from GEDCOM data.
745     *
746     * @param string $gedcom
747     *
748     * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media
749     * @throws InvalidArgumentException
750     */
751    public function createRecord(string $gedcom): GedcomRecord
752    {
753        if (!Str::startsWith($gedcom, '0 @@ ')) {
754            throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@');
755        }
756
757        $xref   = $this->getNewXref();
758        $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ ');
759
760        // Create a change record
761        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
762
763        // Create a pending change
764        DB::table('change')->insert([
765            'gedcom_id'  => $this->id,
766            'xref'       => $xref,
767            'old_gedcom' => '',
768            'new_gedcom' => $gedcom,
769            'user_id'    => Auth::id(),
770        ]);
771
772        // Accept this pending change
773        if (Auth::user()->getPreference('auto_accept')) {
774            FunctionsImport::acceptAllChanges($xref, $this);
775
776            return new GedcomRecord($xref, $gedcom, null, $this);
777        }
778
779        return GedcomRecord::getInstance($xref, $this, $gedcom);
780    }
781
782    /**
783     * Create a new family from GEDCOM data.
784     *
785     * @param string $gedcom
786     *
787     * @return Family
788     * @throws InvalidArgumentException
789     */
790    public function createFamily(string $gedcom): GedcomRecord
791    {
792        if (!Str::startsWith($gedcom, '0 @@ FAM')) {
793            throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM');
794        }
795
796        $xref   = $this->getNewXref();
797        $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM');
798
799        // Create a change record
800        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
801
802        // Create a pending change
803        DB::table('change')->insert([
804            'gedcom_id'  => $this->id,
805            'xref'       => $xref,
806            'old_gedcom' => '',
807            'new_gedcom' => $gedcom,
808            'user_id'    => Auth::id(),
809        ]);
810
811        // Accept this pending change
812        if (Auth::user()->getPreference('auto_accept')) {
813            FunctionsImport::acceptAllChanges($xref, $this);
814
815            return new Family($xref, $gedcom, null, $this);
816        }
817
818        return new Family($xref, '', $gedcom, $this);
819    }
820
821    /**
822     * Create a new individual from GEDCOM data.
823     *
824     * @param string $gedcom
825     *
826     * @return Individual
827     * @throws InvalidArgumentException
828     */
829    public function createIndividual(string $gedcom): GedcomRecord
830    {
831        if (!Str::startsWith($gedcom, '0 @@ INDI')) {
832            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI');
833        }
834
835        $xref   = $this->getNewXref();
836        $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI');
837
838        // Create a change record
839        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
840
841        // Create a pending change
842        DB::table('change')->insert([
843            'gedcom_id'  => $this->id,
844            'xref'       => $xref,
845            'old_gedcom' => '',
846            'new_gedcom' => $gedcom,
847            'user_id'    => Auth::id(),
848        ]);
849
850        // Accept this pending change
851        if (Auth::user()->getPreference('auto_accept')) {
852            FunctionsImport::acceptAllChanges($xref, $this);
853
854            return new Individual($xref, $gedcom, null, $this);
855        }
856
857        return new Individual($xref, '', $gedcom, $this);
858    }
859
860    /**
861     * Create a new media object from GEDCOM data.
862     *
863     * @param string $gedcom
864     *
865     * @return Media
866     * @throws InvalidArgumentException
867     */
868    public function createMediaObject(string $gedcom): Media
869    {
870        if (!Str::startsWith($gedcom, '0 @@ OBJE')) {
871            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE');
872        }
873
874        $xref   = $this->getNewXref();
875        $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE');
876
877        // Create a change record
878        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
879
880        // Create a pending change
881        DB::table('change')->insert([
882            'gedcom_id'  => $this->id,
883            'xref'       => $xref,
884            'old_gedcom' => '',
885            'new_gedcom' => $gedcom,
886            'user_id'    => Auth::id(),
887        ]);
888
889        // Accept this pending change
890        if (Auth::user()->getPreference('auto_accept')) {
891            FunctionsImport::acceptAllChanges($xref, $this);
892
893            return new Media($xref, $gedcom, null, $this);
894        }
895
896        return new Media($xref, '', $gedcom, $this);
897    }
898
899    /**
900     * What is the most significant individual in this tree.
901     *
902     * @param User $user
903     *
904     * @return Individual
905     */
906    public function significantIndividual(User $user): Individual
907    {
908        $individual = null;
909
910        if ($this->getUserPreference($user, 'rootid') !== '') {
911            $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this);
912        }
913
914        if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') {
915            $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this);
916        }
917
918        if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') {
919            $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this);
920        }
921        if ($individual === null) {
922            $xref = (string) DB::table('individuals')
923                ->where('i_file', '=', $this->id())
924                ->min('i_id');
925
926            $individual = Individual::getInstance($xref, $this);
927        }
928        if ($individual === null) {
929            // always return a record
930            $individual = new Individual('I', '0 @I@ INDI', null, $this);
931        }
932
933        return $individual;
934    }
935}
936