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