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