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