xref: /webtrees/app/Source.php (revision 886b77daa151baff5435671d4ed5ba22ff96b9d6)
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
20*886b77daSGreg Roachuse Closure;
212e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
22*886b77daSGreg Roachuse stdClass;
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
3116d6367aSGreg Roach    protected const ROUTE_NAME  = 'source';
32a25f0a04SGreg Roach
3376692c8bSGreg Roach    /**
34*886b77daSGreg Roach     * A closure which will create a record from a database row.
35*886b77daSGreg Roach     *
36*886b77daSGreg Roach     * @param Tree $tree
37*886b77daSGreg Roach     *
38*886b77daSGreg Roach     * @return Closure
39*886b77daSGreg Roach     */
40*886b77daSGreg Roach    public static function rowMapper(Tree $tree): Closure
41*886b77daSGreg Roach    {
42*886b77daSGreg Roach        return function (stdClass $row) use ($tree): Source {
43*886b77daSGreg Roach            return Source::getInstance($row->s_id, $tree, $row->s_gedcom);
44*886b77daSGreg Roach        };
45*886b77daSGreg Roach    }
46*886b77daSGreg Roach
47*886b77daSGreg Roach    /**
48e71ef9d2SGreg Roach     * Get an instance of a source object. For single records,
49e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
50e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
51e71ef9d2SGreg Roach     *
52e71ef9d2SGreg Roach     * @param string      $xref
53e71ef9d2SGreg Roach     * @param Tree        $tree
54e71ef9d2SGreg Roach     * @param string|null $gedcom
55e71ef9d2SGreg Roach     *
56e71ef9d2SGreg Roach     * @throws \Exception
57e71ef9d2SGreg Roach     *
58e71ef9d2SGreg Roach     * @return Source|null
59e71ef9d2SGreg Roach     */
6076f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
61c1010edaSGreg Roach    {
62e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
63e71ef9d2SGreg Roach
64e71ef9d2SGreg Roach        if ($record instanceof Source) {
65e71ef9d2SGreg Roach            return $record;
66e71ef9d2SGreg Roach        }
67b2ce94c6SRico Sonntag
68b2ce94c6SRico Sonntag        return null;
69e71ef9d2SGreg Roach    }
70e71ef9d2SGreg Roach
71e71ef9d2SGreg Roach    /**
7276692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
7376692c8bSGreg Roach     *
7476692c8bSGreg Roach     * @param int $access_level
7576692c8bSGreg Roach     *
7676692c8bSGreg Roach     * @return bool
7776692c8bSGreg Roach     */
7835584196SGreg Roach    protected function canShowByType(int $access_level): bool
79c1010edaSGreg Roach    {
80a25f0a04SGreg Roach        // Hide sources if they are attached to private repositories ...
81a25f0a04SGreg Roach        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
82a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
8324ec66ceSGreg Roach            $repo = Repository::getInstance($match, $this->tree);
84a25f0a04SGreg Roach            if ($repo && !$repo->canShow($access_level)) {
85a25f0a04SGreg Roach                return false;
86a25f0a04SGreg Roach            }
87a25f0a04SGreg Roach        }
88a25f0a04SGreg Roach
89a25f0a04SGreg Roach        // ... otherwise apply default behaviour
90a25f0a04SGreg Roach        return parent::canShowByType($access_level);
91a25f0a04SGreg Roach    }
92a25f0a04SGreg Roach
9376692c8bSGreg Roach    /**
9476692c8bSGreg Roach     * Generate a private version of this record
9576692c8bSGreg Roach     *
9676692c8bSGreg Roach     * @param int $access_level
9776692c8bSGreg Roach     *
9876692c8bSGreg Roach     * @return string
9976692c8bSGreg Roach     */
1003c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
101c1010edaSGreg Roach    {
102a25f0a04SGreg Roach        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
103a25f0a04SGreg Roach    }
104a25f0a04SGreg Roach
10576692c8bSGreg Roach    /**
10676692c8bSGreg Roach     * Fetch data from the database
10776692c8bSGreg Roach     *
10876692c8bSGreg Roach     * @param string $xref
10976692c8bSGreg Roach     * @param int    $tree_id
11076692c8bSGreg Roach     *
11176692c8bSGreg Roach     * @return null|string
11276692c8bSGreg Roach     */
11376f666f4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
114c1010edaSGreg Roach    {
1152e5b4452SGreg Roach        return DB::table('sources')
1162e5b4452SGreg Roach            ->where('s_id', '=', $xref)
1172e5b4452SGreg Roach            ->where('s_file', '=', $tree_id)
1182e5b4452SGreg Roach            ->value('s_gedcom');
119a25f0a04SGreg Roach    }
120a25f0a04SGreg Roach
12176692c8bSGreg Roach    /**
12276692c8bSGreg Roach     * Extract names from the GEDCOM record.
123c7ff4153SGreg Roach     *
124c7ff4153SGreg Roach     * @return void
12576692c8bSGreg Roach     */
126c1010edaSGreg Roach    public function extractNames()
127c1010edaSGreg Roach    {
1288d0ebef0SGreg Roach        parent::extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
129a25f0a04SGreg Roach    }
130a25f0a04SGreg Roach}
131