xref: /webtrees/app/Source.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
232e5b4452SGreg Roach
24a25f0a04SGreg Roach/**
2576692c8bSGreg Roach * A GEDCOM source (SOUR) object.
26a25f0a04SGreg Roach */
27c1010edaSGreg Roachclass Source extends GedcomRecord
28c1010edaSGreg Roach{
2916d6367aSGreg Roach    public const RECORD_TYPE = 'SOUR';
3016d6367aSGreg Roach
3154186344SGreg Roach    protected const ROUTE_NAME = SourcePage::class;
32a25f0a04SGreg Roach
3376692c8bSGreg Roach    /**
3476692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
3576692c8bSGreg Roach     *
3676692c8bSGreg Roach     * @param int $access_level
3776692c8bSGreg Roach     *
3876692c8bSGreg Roach     * @return bool
3976692c8bSGreg Roach     */
4035584196SGreg Roach    protected function canShowByType(int $access_level): bool
41c1010edaSGreg Roach    {
42a25f0a04SGreg Roach        // Hide sources if they are attached to private repositories ...
43a25f0a04SGreg Roach        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
44a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
456b9cb339SGreg Roach            $repo = Registry::repositoryFactory()->make($match, $this->tree);
46ef475b14SGreg Roach            if ($repo instanceof Repository && !$repo->canShow($access_level)) {
47a25f0a04SGreg Roach                return false;
48a25f0a04SGreg Roach            }
49a25f0a04SGreg Roach        }
50a25f0a04SGreg Roach
51b3a775f6SGreg Roach        // ... otherwise apply default behavior
52a25f0a04SGreg Roach        return parent::canShowByType($access_level);
53a25f0a04SGreg Roach    }
54a25f0a04SGreg Roach
5576692c8bSGreg Roach    /**
5676692c8bSGreg Roach     * Extract names from the GEDCOM record.
57c7ff4153SGreg Roach     *
58c7ff4153SGreg Roach     * @return void
5976692c8bSGreg Roach     */
60e364afe4SGreg Roach    public function extractNames(): void
61c1010edaSGreg Roach    {
62e364afe4SGreg Roach        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
63a25f0a04SGreg Roach    }
648091bfd1SGreg Roach
658091bfd1SGreg Roach    /**
668091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
678091bfd1SGreg Roach     */
688091bfd1SGreg Roach    public function lock(): void
698091bfd1SGreg Roach    {
708091bfd1SGreg Roach        DB::table('sources')
718091bfd1SGreg Roach            ->where('s_file', '=', $this->tree->id())
728091bfd1SGreg Roach            ->where('s_id', '=', $this->xref())
738091bfd1SGreg Roach            ->lockForUpdate()
748091bfd1SGreg Roach            ->get();
758091bfd1SGreg Roach    }
76a25f0a04SGreg Roach}
77