xref: /webtrees/app/Source.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 source (SOUR) object.
22 */
23class Source extends GedcomRecord
24{
25    const RECORD_TYPE = 'SOUR';
26    const ROUTE_NAME  = 'source';
27
28    /**
29     * Get an instance of a source 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 Source|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 Source) {
46            return $record;
47        }
48
49        return null;
50    }
51
52    /**
53     * Each object type may have its own special rules, and re-implement this function.
54     *
55     * @param int $access_level
56     *
57     * @return bool
58     */
59    protected function canShowByType(int $access_level): bool
60    {
61        // Hide sources if they are attached to private repositories ...
62        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
63        foreach ($matches[1] as $match) {
64            $repo = Repository::getInstance($match, $this->tree);
65            if ($repo && !$repo->canShow($access_level)) {
66                return false;
67            }
68        }
69
70        // ... otherwise apply default behaviour
71        return parent::canShowByType($access_level);
72    }
73
74    /**
75     * Generate a private version of this record
76     *
77     * @param int $access_level
78     *
79     * @return string
80     */
81    protected function createPrivateGedcomRecord(int $access_level): string
82    {
83        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
84    }
85
86    /**
87     * Fetch data from the database
88     *
89     * @param string $xref
90     * @param int    $tree_id
91     *
92     * @return null|string
93     */
94    protected static function fetchGedcomRecord(string $xref, int $tree_id)
95    {
96        return Database::prepare(
97            "SELECT s_gedcom FROM `##sources` WHERE s_id = :xref AND s_file = :tree_id"
98        )->execute([
99            'xref'    => $xref,
100            'tree_id' => $tree_id,
101        ])->fetchOne();
102    }
103
104    /**
105     * Extract names from the GEDCOM record.
106     *
107     * @return void
108     */
109    public function extractNames()
110    {
111        parent::extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
112    }
113}
114