xref: /webtrees/app/Repository.php (revision 803193fe6835fa954147d30cdf6ce1a1da6103ff)
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 Closure;
21use Exception;
22use Illuminate\Database\Capsule\Manager as DB;
23use stdClass;
24
25/**
26 * A GEDCOM repository (REPO) object.
27 */
28class Repository extends GedcomRecord
29{
30    public const RECORD_TYPE = 'REPO';
31
32    protected const ROUTE_NAME = 'repository';
33
34    /**
35     * A closure which will create a record from a database row.
36     *
37     * @return Closure
38     */
39    public static function rowMapper(): Closure
40    {
41        return static function (stdClass $row): Repository {
42            return Repository::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom);
43        };
44    }
45
46    /**
47     * Get an instance of a repository object. For single records,
48     * we just receive the XREF. For bulk records (such as lists
49     * and search results) we can receive the GEDCOM data as well.
50     *
51     * @param string      $xref
52     * @param Tree        $tree
53     * @param string|null $gedcom
54     *
55     * @throws Exception
56     *
57     * @return Repository|null
58     */
59    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
60    {
61        $record = parent::getInstance($xref, $tree, $gedcom);
62
63        if ($record instanceof self) {
64            return $record;
65        }
66
67        return null;
68    }
69
70    /**
71     * Fetch data from the database
72     *
73     * @param string $xref
74     * @param int    $tree_id
75     *
76     * @return string|null
77     */
78    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
79    {
80        return DB::table('other')
81            ->where('o_id', '=', $xref)
82            ->where('o_file', '=', $tree_id)
83            ->where('o_type', '=', self::RECORD_TYPE)
84            ->value('o_gedcom');
85    }
86
87    /**
88     * Generate a private version of this record
89     *
90     * @param int $access_level
91     *
92     * @return string
93     */
94    protected function createPrivateGedcomRecord(int $access_level): string
95    {
96        return '0 @' . $this->xref . "@ REPO\n1 NAME " . I18N::translate('Private');
97    }
98
99    /**
100     * Extract names from the GEDCOM record.
101     *
102     * @return void
103     */
104    public function extractNames(): void
105    {
106        $this->extractNamesFromFacts(1, 'NAME', $this->facts(['NAME']));
107    }
108}
109