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