xref: /webtrees/app/Source.php (revision 16d6367af0fc87302889bd9d5831cab67c1a54f0)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
202e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
212e5b4452SGreg Roach
22a25f0a04SGreg Roach/**
2376692c8bSGreg Roach * A GEDCOM source (SOUR) object.
24a25f0a04SGreg Roach */
25c1010edaSGreg Roachclass Source extends GedcomRecord
26c1010edaSGreg Roach{
27*16d6367aSGreg Roach    public const RECORD_TYPE = 'SOUR';
28*16d6367aSGreg Roach
29*16d6367aSGreg Roach    protected const ROUTE_NAME  = 'source';
30a25f0a04SGreg Roach
3176692c8bSGreg Roach    /**
32e71ef9d2SGreg Roach     * Get an instance of a source object. For single records,
33e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
34e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
35e71ef9d2SGreg Roach     *
36e71ef9d2SGreg Roach     * @param string      $xref
37e71ef9d2SGreg Roach     * @param Tree        $tree
38e71ef9d2SGreg Roach     * @param string|null $gedcom
39e71ef9d2SGreg Roach     *
40e71ef9d2SGreg Roach     * @throws \Exception
41e71ef9d2SGreg Roach     *
42e71ef9d2SGreg Roach     * @return Source|null
43e71ef9d2SGreg Roach     */
4476f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
45c1010edaSGreg Roach    {
46e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
47e71ef9d2SGreg Roach
48e71ef9d2SGreg Roach        if ($record instanceof Source) {
49e71ef9d2SGreg Roach            return $record;
50e71ef9d2SGreg Roach        }
51b2ce94c6SRico Sonntag
52b2ce94c6SRico Sonntag        return null;
53e71ef9d2SGreg Roach    }
54e71ef9d2SGreg Roach
55e71ef9d2SGreg Roach    /**
5676692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
5776692c8bSGreg Roach     *
5876692c8bSGreg Roach     * @param int $access_level
5976692c8bSGreg Roach     *
6076692c8bSGreg Roach     * @return bool
6176692c8bSGreg Roach     */
6235584196SGreg Roach    protected function canShowByType(int $access_level): bool
63c1010edaSGreg Roach    {
64a25f0a04SGreg Roach        // Hide sources if they are attached to private repositories ...
65a25f0a04SGreg Roach        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
66a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
6724ec66ceSGreg Roach            $repo = Repository::getInstance($match, $this->tree);
68a25f0a04SGreg Roach            if ($repo && !$repo->canShow($access_level)) {
69a25f0a04SGreg Roach                return false;
70a25f0a04SGreg Roach            }
71a25f0a04SGreg Roach        }
72a25f0a04SGreg Roach
73a25f0a04SGreg Roach        // ... otherwise apply default behaviour
74a25f0a04SGreg Roach        return parent::canShowByType($access_level);
75a25f0a04SGreg Roach    }
76a25f0a04SGreg Roach
7776692c8bSGreg Roach    /**
7876692c8bSGreg Roach     * Generate a private version of this record
7976692c8bSGreg Roach     *
8076692c8bSGreg Roach     * @param int $access_level
8176692c8bSGreg Roach     *
8276692c8bSGreg Roach     * @return string
8376692c8bSGreg Roach     */
843c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
85c1010edaSGreg Roach    {
86a25f0a04SGreg Roach        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
87a25f0a04SGreg Roach    }
88a25f0a04SGreg Roach
8976692c8bSGreg Roach    /**
9076692c8bSGreg Roach     * Fetch data from the database
9176692c8bSGreg Roach     *
9276692c8bSGreg Roach     * @param string $xref
9376692c8bSGreg Roach     * @param int    $tree_id
9476692c8bSGreg Roach     *
9576692c8bSGreg Roach     * @return null|string
9676692c8bSGreg Roach     */
9776f666f4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
98c1010edaSGreg Roach    {
992e5b4452SGreg Roach        return DB::table('sources')
1002e5b4452SGreg Roach            ->where('s_id', '=', $xref)
1012e5b4452SGreg Roach            ->where('s_file', '=', $tree_id)
1022e5b4452SGreg Roach            ->value('s_gedcom');
103a25f0a04SGreg Roach    }
104a25f0a04SGreg Roach
10576692c8bSGreg Roach    /**
10676692c8bSGreg Roach     * Extract names from the GEDCOM record.
107c7ff4153SGreg Roach     *
108c7ff4153SGreg Roach     * @return void
10976692c8bSGreg Roach     */
110c1010edaSGreg Roach    public function extractNames()
111c1010edaSGreg Roach    {
1128d0ebef0SGreg Roach        parent::extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
113a25f0a04SGreg Roach    }
114a25f0a04SGreg Roach}
115