xref: /webtrees/app/Source.php (revision 886b77daa151baff5435671d4ed5ba22ff96b9d6)
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 source (SOUR) object.
26 */
27class Source extends GedcomRecord
28{
29    public const RECORD_TYPE = 'SOUR';
30
31    protected const ROUTE_NAME  = 'source';
32
33    /**
34     * A closure which will create a record from a database row.
35     *
36     * @param Tree $tree
37     *
38     * @return Closure
39     */
40    public static function rowMapper(Tree $tree): Closure
41    {
42        return function (stdClass $row) use ($tree): Source {
43            return Source::getInstance($row->s_id, $tree, $row->s_gedcom);
44        };
45    }
46
47    /**
48     * Get an instance of a source object. For single records,
49     * we just receive the XREF. For bulk records (such as lists
50     * and search results) we can receive the GEDCOM data as well.
51     *
52     * @param string      $xref
53     * @param Tree        $tree
54     * @param string|null $gedcom
55     *
56     * @throws \Exception
57     *
58     * @return Source|null
59     */
60    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
61    {
62        $record = parent::getInstance($xref, $tree, $gedcom);
63
64        if ($record instanceof Source) {
65            return $record;
66        }
67
68        return null;
69    }
70
71    /**
72     * Each object type may have its own special rules, and re-implement this function.
73     *
74     * @param int $access_level
75     *
76     * @return bool
77     */
78    protected function canShowByType(int $access_level): bool
79    {
80        // Hide sources if they are attached to private repositories ...
81        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
82        foreach ($matches[1] as $match) {
83            $repo = Repository::getInstance($match, $this->tree);
84            if ($repo && !$repo->canShow($access_level)) {
85                return false;
86            }
87        }
88
89        // ... otherwise apply default behaviour
90        return parent::canShowByType($access_level);
91    }
92
93    /**
94     * Generate a private version of this record
95     *
96     * @param int $access_level
97     *
98     * @return string
99     */
100    protected function createPrivateGedcomRecord(int $access_level): string
101    {
102        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
103    }
104
105    /**
106     * Fetch data from the database
107     *
108     * @param string $xref
109     * @param int    $tree_id
110     *
111     * @return null|string
112     */
113    protected static function fetchGedcomRecord(string $xref, int $tree_id)
114    {
115        return DB::table('sources')
116            ->where('s_id', '=', $xref)
117            ->where('s_file', '=', $tree_id)
118            ->value('s_gedcom');
119    }
120
121    /**
122     * Extract names from the GEDCOM record.
123     *
124     * @return void
125     */
126    public function extractNames()
127    {
128        parent::extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
129    }
130}
131