xref: /webtrees/app/Source.php (revision 1792ff1cf1956b41f3e3c853cfb279a803a71ed2)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Exception;
24use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
25use Illuminate\Database\Capsule\Manager as DB;
26use stdClass;
27
28/**
29 * A GEDCOM source (SOUR) object.
30 */
31class Source extends GedcomRecord
32{
33    public const RECORD_TYPE = 'SOUR';
34
35    protected const ROUTE_NAME  = SourcePage::class;
36
37    /**
38     * A closure which will create a record from a database row.
39     *
40     * @param Tree $tree
41     *
42     * @return Closure
43     */
44    public static function rowMapper(Tree $tree): Closure
45    {
46        return static function (stdClass $row) use ($tree): Source {
47            $source = Source::getInstance($row->s_id, $tree, $row->s_gedcom);
48            assert($source instanceof Source);
49
50            return $source;
51        };
52    }
53
54    /**
55     * Get an instance of a source object. For single records,
56     * we just receive the XREF. For bulk records (such as lists
57     * and search results) we can receive the GEDCOM data as well.
58     *
59     * @param string      $xref
60     * @param Tree        $tree
61     * @param string|null $gedcom
62     *
63     * @throws Exception
64     *
65     * @return Source|null
66     */
67    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Source
68    {
69        $record = parent::getInstance($xref, $tree, $gedcom);
70
71        if ($record instanceof self) {
72            return $record;
73        }
74
75        return null;
76    }
77
78    /**
79     * Each object type may have its own special rules, and re-implement this function.
80     *
81     * @param int $access_level
82     *
83     * @return bool
84     */
85    protected function canShowByType(int $access_level): bool
86    {
87        // Hide sources if they are attached to private repositories ...
88        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
89        foreach ($matches[1] as $match) {
90            $repo = Repository::getInstance($match, $this->tree);
91            if ($repo && !$repo->canShow($access_level)) {
92                return false;
93            }
94        }
95
96        // ... otherwise apply default behavior
97        return parent::canShowByType($access_level);
98    }
99
100    /**
101     * Generate a private version of this record
102     *
103     * @param int $access_level
104     *
105     * @return string
106     */
107    protected function createPrivateGedcomRecord(int $access_level): string
108    {
109        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
110    }
111
112    /**
113     * Fetch data from the database
114     *
115     * @param string $xref
116     * @param int    $tree_id
117     *
118     * @return string|null
119     */
120    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
121    {
122        return DB::table('sources')
123            ->where('s_id', '=', $xref)
124            ->where('s_file', '=', $tree_id)
125            ->value('s_gedcom');
126    }
127
128    /**
129     * Extract names from the GEDCOM record.
130     *
131     * @return void
132     */
133    public function extractNames(): void
134    {
135        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
136    }
137}
138