xref: /webtrees/app/Source.php (revision 756a2ca01a3791b4718ecb07a1038fdc54f5cb4c)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
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 = SourcePage::class;
32
33    /**
34     * Each object type may have its own special rules, and re-implement this function.
35     *
36     * @param int $access_level
37     *
38     * @return bool
39     */
40    protected function canShowByType(int $access_level): bool
41    {
42        // Hide sources if they are attached to private repositories ...
43        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
44        foreach ($matches[1] as $match) {
45            $repo = Registry::repositoryFactory()->make($match, $this->tree);
46            if ($repo instanceof Repository && !$repo->canShow($access_level)) {
47                return false;
48            }
49        }
50
51        // ... otherwise apply default behavior
52        return parent::canShowByType($access_level);
53    }
54
55    /**
56     * Extract names from the GEDCOM record.
57     *
58     * @return void
59     */
60    public function extractNames(): void
61    {
62        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
63    }
64
65    /**
66     * Lock the database row, to prevent concurrent edits.
67     */
68    public function lock(): void
69    {
70        DB::table('sources')
71            ->where('s_file', '=', $this->tree->id())
72            ->where('s_id', '=', $this->xref())
73            ->lockForUpdate()
74            ->get();
75    }
76}
77