xref: /webtrees/app/Source.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
2254186344SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
238091bfd1SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
242e5b4452SGreg Roach
25a25f0a04SGreg Roach/**
2676692c8bSGreg Roach * A GEDCOM source (SOUR) object.
27a25f0a04SGreg Roach */
28c1010edaSGreg Roachclass Source extends GedcomRecord
29c1010edaSGreg Roach{
3016d6367aSGreg Roach    public const RECORD_TYPE = 'SOUR';
3116d6367aSGreg Roach
3254186344SGreg Roach    protected const ROUTE_NAME = SourcePage::class;
33a25f0a04SGreg Roach
3476692c8bSGreg Roach    /**
3576692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
3676692c8bSGreg Roach     *
3776692c8bSGreg Roach     * @param int $access_level
3876692c8bSGreg Roach     *
3976692c8bSGreg Roach     * @return bool
4076692c8bSGreg Roach     */
4135584196SGreg Roach    protected function canShowByType(int $access_level): bool
42c1010edaSGreg Roach    {
43a25f0a04SGreg Roach        // Hide sources if they are attached to private repositories ...
44a25f0a04SGreg Roach        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
45a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
466b9cb339SGreg Roach            $repo = Registry::repositoryFactory()->make($match, $this->tree);
47a25f0a04SGreg Roach            if ($repo && !$repo->canShow($access_level)) {
48a25f0a04SGreg Roach                return false;
49a25f0a04SGreg Roach            }
50a25f0a04SGreg Roach        }
51a25f0a04SGreg Roach
52b3a775f6SGreg Roach        // ... otherwise apply default behavior
53a25f0a04SGreg Roach        return parent::canShowByType($access_level);
54a25f0a04SGreg Roach    }
55a25f0a04SGreg Roach
5676692c8bSGreg Roach    /**
5776692c8bSGreg Roach     * Extract names from the GEDCOM record.
58c7ff4153SGreg Roach     *
59c7ff4153SGreg Roach     * @return void
6076692c8bSGreg Roach     */
61e364afe4SGreg Roach    public function extractNames(): void
62c1010edaSGreg Roach    {
63e364afe4SGreg Roach        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
64a25f0a04SGreg Roach    }
658091bfd1SGreg Roach
668091bfd1SGreg Roach    /**
678091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
688091bfd1SGreg Roach     */
698091bfd1SGreg Roach    public function lock(): void
708091bfd1SGreg Roach    {
718091bfd1SGreg Roach        DB::table('sources')
728091bfd1SGreg Roach            ->where('s_file', '=', $this->tree->id())
738091bfd1SGreg Roach            ->where('s_id', '=', $this->xref())
748091bfd1SGreg Roach            ->lockForUpdate()
758091bfd1SGreg Roach            ->get();
768091bfd1SGreg Roach    }
77a25f0a04SGreg Roach}
78