xref: /webtrees/app/Source.php (revision b6017f990d38d8c56e04c0096ce9a7e8745ad4ba)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
24use Illuminate\Database\Capsule\Manager as DB;
25
26/**
27 * A GEDCOM source (SOUR) object.
28 */
29class Source extends GedcomRecord
30{
31    public const RECORD_TYPE = 'SOUR';
32
33    protected const ROUTE_NAME  = SourcePage::class;
34
35    /**
36     * A closure which will create a record from a database row.
37     *
38     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::sourceFactory()
39     *
40     * @param Tree $tree
41     *
42     * @return Closure
43     */
44    public static function rowMapper(Tree $tree): Closure
45    {
46        return Registry::sourceFactory()->mapper($tree);
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     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::sourceFactory()
55     *
56     * @param string      $xref
57     * @param Tree        $tree
58     * @param string|null $gedcom
59     *
60     * @return Source|null
61     */
62    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Source
63    {
64        return Registry::sourceFactory()->make($xref, $tree, $gedcom);
65    }
66
67    /**
68     * Each object type may have its own special rules, and re-implement this function.
69     *
70     * @param int $access_level
71     *
72     * @return bool
73     */
74    protected function canShowByType(int $access_level): bool
75    {
76        // Hide sources if they are attached to private repositories ...
77        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
78        foreach ($matches[1] as $match) {
79            $repo = Registry::repositoryFactory()->make($match, $this->tree);
80            if ($repo && !$repo->canShow($access_level)) {
81                return false;
82            }
83        }
84
85        // ... otherwise apply default behavior
86        return parent::canShowByType($access_level);
87    }
88
89    /**
90     * Extract names from the GEDCOM record.
91     *
92     * @return void
93     */
94    public function extractNames(): void
95    {
96        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
97    }
98
99    /**
100     * Lock the database row, to prevent concurrent edits.
101     */
102    public function lock(): void
103    {
104        DB::table('sources')
105            ->where('s_file', '=', $this->tree->id())
106            ->where('s_id', '=', $this->xref())
107            ->lockForUpdate()
108            ->get();
109    }
110}
111