xref: /webtrees/app/Source.php (revision 873953697c930fadbf3243d2b8c0029fd684da0e)
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 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * A GEDCOM source (SOUR) object.
20 */
21class Source extends GedcomRecord
22{
23    const RECORD_TYPE = 'SOUR';
24    const ROUTE_NAME  = 'source';
25
26    /**
27     * Get an instance of a source object. For single records,
28     * we just receive the XREF. For bulk records (such as lists
29     * and search results) we can receive the GEDCOM data as well.
30     *
31     * @param string      $xref
32     * @param Tree        $tree
33     * @param string|null $gedcom
34     *
35     * @throws \Exception
36     *
37     * @return Source|null
38     */
39    public static function getInstance($xref, Tree $tree, $gedcom = null)
40    {
41        $record = parent::getInstance($xref, $tree, $gedcom);
42
43        if ($record instanceof Source) {
44            return $record;
45        } else {
46            return null;
47        }
48    }
49
50    /**
51     * Each object type may have its own special rules, and re-implement this function.
52     *
53     * @param int $access_level
54     *
55     * @return bool
56     */
57    protected function canShowByType($access_level): bool
58    {
59        // Hide sources if they are attached to private repositories ...
60        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
61        foreach ($matches[1] as $match) {
62            $repo = Repository::getInstance($match, $this->tree);
63            if ($repo && !$repo->canShow($access_level)) {
64                return false;
65            }
66        }
67
68        // ... otherwise apply default behaviour
69        return parent::canShowByType($access_level);
70    }
71
72    /**
73     * Generate a private version of this record
74     *
75     * @param int $access_level
76     *
77     * @return string
78     */
79    protected function createPrivateGedcomRecord($access_level): string
80    {
81        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
82    }
83
84    /**
85     * Fetch data from the database
86     *
87     * @param string $xref
88     * @param int    $tree_id
89     *
90     * @return null|string
91     */
92    protected static function fetchGedcomRecord($xref, $tree_id)
93    {
94        return Database::prepare(
95            "SELECT s_gedcom FROM `##sources` WHERE s_id = :xref AND s_file = :tree_id"
96        )->execute([
97            'xref'    => $xref,
98            'tree_id' => $tree_id,
99        ])->fetchOne();
100    }
101
102    /**
103     * Extract names from the GEDCOM record.
104     *
105     * @return void
106     */
107    public function extractNames()
108    {
109        parent::extractNamesFromFacts(1, 'TITL', $this->getFacts('TITL'));
110    }
111}
112