xref: /webtrees/app/Source.php (revision b8e5af9c5d75cb4b3ad7b48d0e8d1e7751b303da)
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 Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
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  = SourcePage::class;
33
34    /**
35     * A closure which will create a record from a database row.
36     *
37     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::source()
38     *
39     * @param Tree $tree
40     *
41     * @return Closure
42     */
43    public static function rowMapper(Tree $tree): Closure
44    {
45        return Factory::source()->mapper($tree);
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     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::source()
54     *
55     * @param string      $xref
56     * @param Tree        $tree
57     * @param string|null $gedcom
58     *
59     * @return Source|null
60     */
61    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Source
62    {
63        return Factory::source()->make($xref, $tree, $gedcom);
64    }
65
66    /**
67     * Each object type may have its own special rules, and re-implement this function.
68     *
69     * @param int $access_level
70     *
71     * @return bool
72     */
73    protected function canShowByType(int $access_level): bool
74    {
75        // Hide sources if they are attached to private repositories ...
76        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
77        foreach ($matches[1] as $match) {
78            $repo = Factory::repository()->make($match, $this->tree);
79            if ($repo && !$repo->canShow($access_level)) {
80                return false;
81            }
82        }
83
84        // ... otherwise apply default behavior
85        return parent::canShowByType($access_level);
86    }
87
88    /**
89     * Generate a private version of this record
90     *
91     * @param int $access_level
92     *
93     * @return string
94     */
95    protected function createPrivateGedcomRecord(int $access_level): string
96    {
97        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
98    }
99
100    /**
101     * Extract names from the GEDCOM record.
102     *
103     * @return void
104     */
105    public function extractNames(): void
106    {
107        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
108    }
109}
110