xref: /webtrees/app/Tree.php (revision 8b67c11a1199191915b4af08a3841e7ce9d528b6)
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->getUserId(), $this->user_preferences)) {
228            $this->user_preferences[$user->getUserId()] = DB::table('user_gedcom_setting')
229                ->where('user_id', '=', $user->getUserId())
230                ->where('gedcom_id', '=', $this->id)
231                ->pluck('setting_value', 'setting_name')
232                ->all();
233        }
234
235        return $this->user_preferences[$user->getUserId()][$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->getUserId(),
254                'setting_name' => $setting_name,
255            ], [
256                'setting_value' => $setting_value,
257            ]);
258
259            // Update the cache
260            $this->user_preferences[$user->getUserId()][$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        // Module privacy
451        Module::setDefaultAccess($tree_id);
452
453        // Set preferences from default tree
454        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
455            ['gedcom_id', 'setting_name', 'setting_value'],
456            function (Builder $query) use ($tree_id): void {
457                $query
458                    ->select([DB::raw($tree_id), 'setting_name', 'setting_value'])
459                    ->from('gedcom_setting')
460                    ->where('gedcom_id', '=', -1);
461            }
462        );
463
464        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
465            ['gedcom_id', 'tag_type', 'resn'],
466            function (Builder $query) use ($tree_id): void {
467                $query
468                    ->select([DB::raw($tree_id), 'tag_type', 'resn'])
469                    ->from('default_resn')
470                    ->where('gedcom_id', '=', -1);
471            }
472        );
473
474        (new Builder(DB::connection()))->from('block')->insertUsing(
475            ['gedcom_id', 'location', 'block_order', 'module_name'],
476            function (Builder $query) use ($tree_id): void {
477                $query
478                    ->select([DB::raw($tree_id), 'location', 'block_order', 'module_name'])
479                    ->from('block')
480                    ->where('gedcom_id', '=', -1);
481            }
482        );
483
484        // Gedcom and privacy settings
485        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
486        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
487        $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language
488        switch (WT_LOCALE) {
489            case 'es':
490                $tree->setPreference('SURNAME_TRADITION', 'spanish');
491                break;
492            case 'is':
493                $tree->setPreference('SURNAME_TRADITION', 'icelandic');
494                break;
495            case 'lt':
496                $tree->setPreference('SURNAME_TRADITION', 'lithuanian');
497                break;
498            case 'pl':
499                $tree->setPreference('SURNAME_TRADITION', 'polish');
500                break;
501            case 'pt':
502            case 'pt-BR':
503                $tree->setPreference('SURNAME_TRADITION', 'portuguese');
504                break;
505            default:
506                $tree->setPreference('SURNAME_TRADITION', 'paternal');
507                break;
508        }
509
510        // Genealogy data
511        // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...
512        /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */
513        $john_doe = I18N::translate('John /DOE/');
514        $note     = I18N::translate('Edit this individual and replace their details with your own.');
515        $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";
516
517        DB::table('gedcom_chunk')->insert([
518            'gedcom_id'  => $tree_id,
519            'chunk_data' => $gedcom,
520        ]);
521
522        // Update our cache
523        self::$trees[$tree->id] = $tree;
524
525        return $tree;
526    }
527
528    /**
529     * Are there any pending edits for this tree, than need reviewing by a moderator.
530     *
531     * @return bool
532     */
533    public function hasPendingEdit(): bool
534    {
535        return DB::table('change')
536            ->where('gedcom_id', '=', $this->id)
537            ->where('status', '=', 'pending')
538            ->exists();
539    }
540
541    /**
542     * Delete all the genealogy data from a tree - in preparation for importing
543     * new data. Optionally retain the media data, for when the user has been
544     * editing their data offline using an application which deletes (or does not
545     * support) media data.
546     *
547     * @param bool $keep_media
548     *
549     * @return void
550     */
551    public function deleteGenealogyData(bool $keep_media)
552    {
553        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
554        DB::table('individuals')->where('i_file', '=', $this->id)->delete();
555        DB::table('families')->where('f_file', '=', $this->id)->delete();
556        DB::table('sources')->where('s_file', '=', $this->id)->delete();
557        DB::table('other')->where('o_file', '=', $this->id)->delete();
558        DB::table('places')->where('p_file', '=', $this->id)->delete();
559        DB::table('placelinks')->where('pl_file', '=', $this->id)->delete();
560        DB::table('name')->where('n_file', '=', $this->id)->delete();
561        DB::table('dates')->where('d_file', '=', $this->id)->delete();
562        DB::table('change')->where('gedcom_id', '=', $this->id)->delete();
563
564        if ($keep_media) {
565            DB::table('link')->where('l_file', '=', $this->id)
566                ->where('l_type', '<>', 'OBJE')
567                ->delete();
568        } else {
569            DB::table('link')->where('l_file', '=', $this->id)->delete();
570            DB::table('media_file')->where('m_file', '=', $this->id)->delete();
571            DB::table('media')->where('m_file', '=', $this->id)->delete();
572        }
573    }
574
575    /**
576     * Delete everything relating to a tree
577     *
578     * @return void
579     */
580    public function delete()
581    {
582        // If this is the default tree, then unset it
583        if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) {
584            Site::setPreference('DEFAULT_GEDCOM', '');
585        }
586
587        $this->deleteGenealogyData(false);
588
589        DB::table('block_setting')
590            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
591            ->where('gedcom_id', '=', $this->id)
592            ->delete();
593        DB::table('block')->where('gedcom_id', '=', $this->id)->delete();
594        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
595        DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
596        DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete();
597        DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete();
598        DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete();
599        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
600        DB::table('log')->where('gedcom_id', '=', $this->id)->delete();
601        DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete();
602
603        // After updating the database, we need to fetch a new (sorted) copy
604        self::$trees = [];
605    }
606
607    /**
608     * Export the tree to a GEDCOM file
609     *
610     * @param resource $stream
611     *
612     * @return void
613     */
614    public function exportGedcom($stream)
615    {
616        $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8'));
617
618        $union_families = DB::table('families')
619            ->where('f_file', '=', $this->id)
620            ->select(['f_gedcom AS gedcom', 'f_id AS xref', DB::raw('LENGTH(f_id) AS len'), DB::raw('2 AS n')]);
621
622        $union_sources = DB::table('sources')
623            ->where('s_file', '=', $this->id)
624            ->select(['s_gedcom AS gedcom', 's_id AS xref', DB::raw('LENGTH(s_id) AS len'), DB::raw('3 AS n')]);
625
626        $union_other = DB::table('other')
627            ->where('o_file', '=', $this->id)
628            ->whereNotIn('o_type', ['HEAD', 'TRLR'])
629            ->select(['o_gedcom AS gedcom', 'o_id AS xref', DB::raw('LENGTH(o_id) AS len'), DB::raw('4 AS n')]);
630
631        $union_media = DB::table('media')
632            ->where('m_file', '=', $this->id)
633            ->select(['m_gedcom AS gedcom', 'm_id AS xref', DB::raw('LENGTH(m_id) AS len'), DB::raw('5 AS n')]);
634
635        $rows = DB::table('individuals')
636            ->where('i_file', '=', $this->id)
637            ->select(['i_gedcom AS gedcom', 'i_id AS xref', DB::raw('LENGTH(i_id) AS len'), DB::raw('1 AS n')])
638            ->union($union_families)
639            ->union($union_sources)
640            ->union($union_other)
641            ->union($union_media)
642            ->orderBy('n')
643            ->orderBy('len')
644            ->orderBy('xref')
645            ->chunk(100, function (Collection $rows) use ($stream, &$buffer): void {
646                foreach ($rows as $row) {
647                    $buffer .= FunctionsExport::reformatRecord($row->gedcom);
648                    if (strlen($buffer) > 65535) {
649                        fwrite($stream, $buffer);
650                        $buffer = '';
651                    }
652                }
653            });
654
655        fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL);
656    }
657
658    /**
659     * Import data from a gedcom file into this tree.
660     *
661     * @param string $path     The full path to the (possibly temporary) file.
662     * @param string $filename The preferred filename, for export/download.
663     *
664     * @return void
665     */
666    public function importGedcomFile(string $path, string $filename)
667    {
668        // Read the file in blocks of roughly 64K. Ensure that each block
669        // contains complete gedcom records. This will ensure we don’t split
670        // multi-byte characters, as well as simplifying the code to import
671        // each block.
672
673        $file_data = '';
674        $fp        = fopen($path, 'rb');
675
676        $this->deleteGenealogyData((bool) $this->getPreference('keep_media'));
677        $this->setPreference('gedcom_filename', $filename);
678        $this->setPreference('imported', '0');
679
680        while (!feof($fp)) {
681            $file_data .= fread($fp, 65536);
682            // There is no strrpos() function that searches for substrings :-(
683            for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) {
684                if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) {
685                    // We’ve found the last record boundary in this chunk of data
686                    break;
687                }
688            }
689            if ($pos) {
690                DB::table('gedcom_chunk')->insert([
691                    'gedcom_id'  => $this->id,
692                    'chunk_data' => substr($file_data, 0, $pos),
693                ]);
694
695                $file_data = substr($file_data, $pos);
696            }
697        }
698        DB::table('gedcom_chunk')->insert([
699            'gedcom_id'  => $this->id,
700            'chunk_data' => $file_data,
701        ]);
702
703        fclose($fp);
704    }
705
706    /**
707     * Generate a new XREF, unique across all family trees
708     *
709     * @return string
710     */
711    public function getNewXref(): string
712    {
713        // Lock the row, so that only one new XREF may be generated at a time.
714        DB::table('site_setting')
715            ->where('setting_name', '=', 'next_xref')
716            ->lockForUpdate()
717            ->get();
718
719        $prefix = 'X';
720
721        $increment = 1.0;
722        do {
723            $num = (int) Site::getPreference('next_xref') + (int) $increment;
724
725            // This exponential increment allows us to scan over large blocks of
726            // existing data in a reasonable time.
727            $increment *= 1.01;
728
729            $xref = $prefix . $num;
730
731            // Records may already exist with this sequence number.
732            $already_used =
733                DB::table('individuals')->where('i_id', '=', $xref)->exists() ||
734                DB::table('families')->where('f_id', '=', $xref)->exists() ||
735                DB::table('sources')->where('s_id', '=', $xref)->exists() ||
736                DB::table('media')->where('m_id', '=', $xref)->exists() ||
737                DB::table('other')->where('o_id', '=', $xref)->exists() ||
738                DB::table('change')->where('xref', '=', $xref)->exists();
739        } while ($already_used);
740
741        Site::setPreference('next_xref', (string) $num);
742
743        return $xref;
744    }
745
746    /**
747     * Create a new record from GEDCOM data.
748     *
749     * @param string $gedcom
750     *
751     * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media
752     * @throws InvalidArgumentException
753     */
754    public function createRecord(string $gedcom): GedcomRecord
755    {
756        if (!Str::startsWith($gedcom, '0 @@ ')) {
757            throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@');
758        }
759
760        $xref   = $this->getNewXref();
761        $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ ');
762
763        // Create a change record
764        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
765
766        // Create a pending change
767        DB::table('change')->insert([
768            'gedcom_id'  => $this->id,
769            'xref'       => $xref,
770            'old_gedcom' => '',
771            'new_gedcom' => $gedcom,
772            'user_id'    => Auth::id(),
773        ]);
774
775        // Accept this pending change
776        if (Auth::user()->getPreference('auto_accept')) {
777            FunctionsImport::acceptAllChanges($xref, $this);
778
779            return new GedcomRecord($xref, $gedcom, null, $this);
780        }
781
782        return GedcomRecord::getInstance($xref, $this, $gedcom);
783    }
784
785    /**
786     * Create a new family from GEDCOM data.
787     *
788     * @param string $gedcom
789     *
790     * @return Family
791     * @throws InvalidArgumentException
792     */
793    public function createFamily(string $gedcom): GedcomRecord
794    {
795        if (!Str::startsWith($gedcom, '0 @@ FAM')) {
796            throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM');
797        }
798
799        $xref   = $this->getNewXref();
800        $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM');
801
802        // Create a change record
803        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
804
805        // Create a pending change
806        DB::table('change')->insert([
807            'gedcom_id'  => $this->id,
808            'xref'       => $xref,
809            'old_gedcom' => '',
810            'new_gedcom' => $gedcom,
811            'user_id'    => Auth::id(),
812        ]);
813
814        // Accept this pending change
815        if (Auth::user()->getPreference('auto_accept')) {
816            FunctionsImport::acceptAllChanges($xref, $this);
817
818            return new Family($xref, $gedcom, null, $this);
819        }
820
821        return new Family($xref, '', $gedcom, $this);
822    }
823
824    /**
825     * Create a new individual from GEDCOM data.
826     *
827     * @param string $gedcom
828     *
829     * @return Individual
830     * @throws InvalidArgumentException
831     */
832    public function createIndividual(string $gedcom): GedcomRecord
833    {
834        if (!Str::startsWith($gedcom, '0 @@ INDI')) {
835            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI');
836        }
837
838        $xref   = $this->getNewXref();
839        $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI');
840
841        // Create a change record
842        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
843
844        // Create a pending change
845        DB::table('change')->insert([
846            'gedcom_id'  => $this->id,
847            'xref'       => $xref,
848            'old_gedcom' => '',
849            'new_gedcom' => $gedcom,
850            'user_id'    => Auth::id(),
851        ]);
852
853        // Accept this pending change
854        if (Auth::user()->getPreference('auto_accept')) {
855            FunctionsImport::acceptAllChanges($xref, $this);
856
857            return new Individual($xref, $gedcom, null, $this);
858        }
859
860        return new Individual($xref, '', $gedcom, $this);
861    }
862
863    /**
864     * Create a new media object from GEDCOM data.
865     *
866     * @param string $gedcom
867     *
868     * @return Media
869     * @throws InvalidArgumentException
870     */
871    public function createMediaObject(string $gedcom): Media
872    {
873        if (!Str::startsWith($gedcom, '0 @@ OBJE')) {
874            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE');
875        }
876
877        $xref   = $this->getNewXref();
878        $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE');
879
880        // Create a change record
881        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
882
883        // Create a pending change
884        DB::table('change')->insert([
885            'gedcom_id'  => $this->id,
886            'xref'       => $xref,
887            'old_gedcom' => '',
888            'new_gedcom' => $gedcom,
889            'user_id'    => Auth::id(),
890        ]);
891
892        // Accept this pending change
893        if (Auth::user()->getPreference('auto_accept')) {
894            FunctionsImport::acceptAllChanges($xref, $this);
895
896            return new Media($xref, $gedcom, null, $this);
897        }
898
899        return new Media($xref, '', $gedcom, $this);
900    }
901
902    /**
903     * What is the most significant individual in this tree.
904     *
905     * @param User $user
906     *
907     * @return Individual
908     */
909    public function significantIndividual(User $user): Individual
910    {
911        $individual = null;
912
913        if ($this->getUserPreference($user, 'rootid') !== '') {
914            $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this);
915        }
916
917        if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') {
918            $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this);
919        }
920
921        if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') {
922            $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this);
923        }
924        if ($individual === null) {
925            $xref = (string) DB::table('individuals')
926                ->where('i_file', '=', $this->id())
927                ->min('i_id');
928
929            $individual = Individual::getInstance($xref, $this);
930        }
931        if ($individual === null) {
932            // always return a record
933            $individual = new Individual('I', '0 @I@ INDI', null, $this);
934        }
935
936        return $individual;
937    }
938}
939