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