xref: /webtrees/app/Repository.php (revision 71239cb694d278d044f33328daaa60c8ed7431e9)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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
20/**
21 * A GEDCOM repository (REPO) object.
22 */
23class Repository extends GedcomRecord
24{
25    const RECORD_TYPE = 'REPO';
26    const ROUTE_NAME  = 'repository';
27
28    /**
29     * Get an instance of a repository object. For single records,
30     * we just receive the XREF. For bulk records (such as lists
31     * and search results) we can receive the GEDCOM data as well.
32     *
33     * @param string      $xref
34     * @param Tree        $tree
35     * @param string|null $gedcom
36     *
37     * @throws \Exception
38     *
39     * @return Repository|null
40     */
41    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
42    {
43        $record = parent::getInstance($xref, $tree, $gedcom);
44
45        if ($record instanceof Repository) {
46            return $record;
47        }
48
49        return null;
50    }
51
52    /**
53     * Fetch data from the database
54     *
55     * @param string $xref
56     * @param int    $tree_id
57     *
58     * @return null|string
59     */
60    protected static function fetchGedcomRecord(string $xref, int $tree_id)
61    {
62        return Database::prepare(
63            "SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'REPO'"
64        )->execute([
65            'xref'    => $xref,
66            'tree_id' => $tree_id,
67        ])->fetchOne();
68    }
69
70    /**
71     * Generate a private version of this record
72     *
73     * @param int $access_level
74     *
75     * @return string
76     */
77    protected function createPrivateGedcomRecord(int $access_level): string
78    {
79        return '0 @' . $this->xref . "@ REPO\n1 NAME " . I18N::translate('Private');
80    }
81
82    /**
83     * Extract names from the GEDCOM record.
84     *
85     * @return void
86     */
87    public function extractNames()
88    {
89        parent::extractNamesFromFacts(1, 'NAME', $this->facts(['NAME']));
90    }
91}
92