xref: /webtrees/app/Source.php (revision 2adcbd9abb8c46fec6ee23c7f719f34dd6b5451d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Closure;
21use Illuminate\Database\Capsule\Manager as DB;
22use stdClass;
23
24/**
25 * A GEDCOM source (SOUR) object.
26 */
27class Source extends GedcomRecord
28{
29    public const RECORD_TYPE = 'SOUR';
30
31    protected const ROUTE_NAME  = 'source';
32
33    /**
34     * A closure which will create a record from a database row.
35     *
36     * @return Closure
37     */
38    public static function rowMapper(): Closure
39    {
40        return function (stdClass $row): Source {
41            return Source::getInstance($row->s_id, Tree::findById((int) $row->s_file), $row->s_gedcom);
42        };
43    }
44
45    /**
46     * Get an instance of a source object. For single records,
47     * we just receive the XREF. For bulk records (such as lists
48     * and search results) we can receive the GEDCOM data as well.
49     *
50     * @param string      $xref
51     * @param Tree        $tree
52     * @param string|null $gedcom
53     *
54     * @throws \Exception
55     *
56     * @return Source|null
57     */
58    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
59    {
60        $record = parent::getInstance($xref, $tree, $gedcom);
61
62        if ($record instanceof Source) {
63            return $record;
64        }
65
66        return null;
67    }
68
69    /**
70     * Each object type may have its own special rules, and re-implement this function.
71     *
72     * @param int $access_level
73     *
74     * @return bool
75     */
76    protected function canShowByType(int $access_level): bool
77    {
78        // Hide sources if they are attached to private repositories ...
79        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
80        foreach ($matches[1] as $match) {
81            $repo = Repository::getInstance($match, $this->tree);
82            if ($repo && !$repo->canShow($access_level)) {
83                return false;
84            }
85        }
86
87        // ... otherwise apply default behaviour
88        return parent::canShowByType($access_level);
89    }
90
91    /**
92     * Generate a private version of this record
93     *
94     * @param int $access_level
95     *
96     * @return string
97     */
98    protected function createPrivateGedcomRecord(int $access_level): string
99    {
100        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
101    }
102
103    /**
104     * Fetch data from the database
105     *
106     * @param string $xref
107     * @param int    $tree_id
108     *
109     * @return null|string
110     */
111    protected static function fetchGedcomRecord(string $xref, int $tree_id)
112    {
113        return DB::table('sources')
114            ->where('s_id', '=', $xref)
115            ->where('s_file', '=', $tree_id)
116            ->value('s_gedcom');
117    }
118
119    /**
120     * Extract names from the GEDCOM record.
121     *
122     * @return void
123     */
124    public function extractNames()
125    {
126        parent::extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
127    }
128}
129