xref: /webtrees/app/Source.php (revision 9ed80f829cfb208ae92f7928345696619569bf4e)
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 Exception;
22use Illuminate\Database\Capsule\Manager as DB;
23use stdClass;
24
25/**
26 * A GEDCOM source (SOUR) object.
27 */
28class Source extends GedcomRecord
29{
30    public const RECORD_TYPE = 'SOUR';
31
32    protected const ROUTE_NAME  = 'source';
33
34    /**
35     * A closure which will create a record from a database row.
36     *
37     * @return Closure
38     */
39    public static function rowMapper(): Closure
40    {
41        return static function (stdClass $row): Source {
42            return Source::getInstance($row->s_id, Tree::findById((int) $row->s_file), $row->s_gedcom);
43        };
44    }
45
46    /**
47     * Get an instance of a source object. For single records,
48     * we just receive the XREF. For bulk records (such as lists
49     * and search results) we can receive the GEDCOM data as well.
50     *
51     * @param string      $xref
52     * @param Tree        $tree
53     * @param string|null $gedcom
54     *
55     * @throws Exception
56     *
57     * @return Source|null
58     */
59    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
60    {
61        $record = parent::getInstance($xref, $tree, $gedcom);
62
63        if ($record instanceof self) {
64            return $record;
65        }
66
67        return null;
68    }
69
70    /**
71     * Each object type may have its own special rules, and re-implement this function.
72     *
73     * @param int $access_level
74     *
75     * @return bool
76     */
77    protected function canShowByType(int $access_level): bool
78    {
79        // Hide sources if they are attached to private repositories ...
80        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
81        foreach ($matches[1] as $match) {
82            $repo = Repository::getInstance($match, $this->tree);
83            if ($repo && !$repo->canShow($access_level)) {
84                return false;
85            }
86        }
87
88        // ... otherwise apply default behaviour
89        return parent::canShowByType($access_level);
90    }
91
92    /**
93     * Generate a private version of this record
94     *
95     * @param int $access_level
96     *
97     * @return string
98     */
99    protected function createPrivateGedcomRecord(int $access_level): string
100    {
101        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
102    }
103
104    /**
105     * Fetch data from the database
106     *
107     * @param string $xref
108     * @param int    $tree_id
109     *
110     * @return string|null
111     */
112    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
113    {
114        return DB::table('sources')
115            ->where('s_id', '=', $xref)
116            ->where('s_file', '=', $tree_id)
117            ->value('s_gedcom');
118    }
119
120    /**
121     * Extract names from the GEDCOM record.
122     *
123     * @return void
124     */
125    public function extractNames(): void
126    {
127        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
128    }
129}
130