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