xref: /webtrees/app/Repository.php (revision 557edc6474b92c23923f1472519b2caf1e5c5d91)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Exception;
24use Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage;
25use Illuminate\Database\Capsule\Manager as DB;
26use stdClass;
27
28/**
29 * A GEDCOM repository (REPO) object.
30 */
31class Repository extends GedcomRecord
32{
33    public const RECORD_TYPE = 'REPO';
34
35    protected const ROUTE_NAME = RepositoryPage::class;
36
37    /**
38     * A closure which will create a record from a database row.
39     *
40     * @param Tree $tree
41     *
42     * @return Closure
43     */
44    public static function rowMapper(Tree $tree): Closure
45    {
46        return static function (stdClass $row) use ($tree): Repository {
47            return Repository::getInstance($row->o_id, $tree, $row->o_gedcom);
48        };
49    }
50
51    /**
52     * Get an instance of a repository object. For single records,
53     * we just receive the XREF. For bulk records (such as lists
54     * and search results) we can receive the GEDCOM data as well.
55     *
56     * @param string      $xref
57     * @param Tree        $tree
58     * @param string|null $gedcom
59     *
60     * @throws Exception
61     *
62     * @return Repository|null
63     */
64    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
65    {
66        $record = parent::getInstance($xref, $tree, $gedcom);
67
68        if ($record instanceof self) {
69            return $record;
70        }
71
72        return null;
73    }
74
75    /**
76     * Fetch data from the database
77     *
78     * @param string $xref
79     * @param int    $tree_id
80     *
81     * @return string|null
82     */
83    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
84    {
85        return DB::table('other')
86            ->where('o_id', '=', $xref)
87            ->where('o_file', '=', $tree_id)
88            ->where('o_type', '=', self::RECORD_TYPE)
89            ->value('o_gedcom');
90    }
91
92    /**
93     * Generate a private version of this record
94     *
95     * @param int $access_level
96     *
97     * @return string
98     */
99    protected function createPrivateGedcomRecord(int $access_level): string
100    {
101        return '0 @' . $this->xref . "@ REPO\n1 NAME " . I18N::translate('Private');
102    }
103
104    /**
105     * Extract names from the GEDCOM record.
106     *
107     * @return void
108     */
109    public function extractNames(): void
110    {
111        $this->extractNamesFromFacts(1, 'NAME', $this->facts(['NAME']));
112    }
113}
114