xref: /webtrees/app/Source.php (revision 541863442b12cca992acb13068986125a0560d78)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
236ccdf4f0SGreg Roachuse Exception;
24*54186344SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
252e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
26886b77daSGreg Roachuse stdClass;
272e5b4452SGreg Roach
28a25f0a04SGreg Roach/**
2976692c8bSGreg Roach * A GEDCOM source (SOUR) object.
30a25f0a04SGreg Roach */
31c1010edaSGreg Roachclass Source extends GedcomRecord
32c1010edaSGreg Roach{
3316d6367aSGreg Roach    public const RECORD_TYPE = 'SOUR';
3416d6367aSGreg Roach
35*54186344SGreg Roach    protected const ROUTE_NAME  = SourcePage::class;
36a25f0a04SGreg Roach
3776692c8bSGreg Roach    /**
38886b77daSGreg Roach     * A closure which will create a record from a database row.
39886b77daSGreg Roach     *
40886b77daSGreg Roach     * @return Closure
41886b77daSGreg Roach     */
42c0804649SGreg Roach    public static function rowMapper(): Closure
43886b77daSGreg Roach    {
446c2179e2SGreg Roach        return static function (stdClass $row): Source {
45a7a24840SGreg Roach            return Source::getInstance($row->s_id, Tree::findById((int) $row->s_file), $row->s_gedcom);
46886b77daSGreg Roach        };
47886b77daSGreg Roach    }
48886b77daSGreg Roach
49886b77daSGreg Roach    /**
50e71ef9d2SGreg Roach     * Get an instance of a source object. For single records,
51e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
52e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
53e71ef9d2SGreg Roach     *
54e71ef9d2SGreg Roach     * @param string      $xref
55e71ef9d2SGreg Roach     * @param Tree        $tree
56e71ef9d2SGreg Roach     * @param string|null $gedcom
57e71ef9d2SGreg Roach     *
586ccdf4f0SGreg Roach     * @throws Exception
59e71ef9d2SGreg Roach     *
60e71ef9d2SGreg Roach     * @return Source|null
61e71ef9d2SGreg Roach     */
62e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
63c1010edaSGreg Roach    {
64e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
65e71ef9d2SGreg Roach
66e364afe4SGreg Roach        if ($record instanceof self) {
67e71ef9d2SGreg Roach            return $record;
68e71ef9d2SGreg Roach        }
69b2ce94c6SRico Sonntag
70b2ce94c6SRico Sonntag        return null;
71e71ef9d2SGreg Roach    }
72e71ef9d2SGreg Roach
73e71ef9d2SGreg Roach    /**
7476692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
7576692c8bSGreg Roach     *
7676692c8bSGreg Roach     * @param int $access_level
7776692c8bSGreg Roach     *
7876692c8bSGreg Roach     * @return bool
7976692c8bSGreg Roach     */
8035584196SGreg Roach    protected function canShowByType(int $access_level): bool
81c1010edaSGreg Roach    {
82a25f0a04SGreg Roach        // Hide sources if they are attached to private repositories ...
83a25f0a04SGreg Roach        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
84a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
8524ec66ceSGreg Roach            $repo = Repository::getInstance($match, $this->tree);
86a25f0a04SGreg Roach            if ($repo && !$repo->canShow($access_level)) {
87a25f0a04SGreg Roach                return false;
88a25f0a04SGreg Roach            }
89a25f0a04SGreg Roach        }
90a25f0a04SGreg Roach
91a25f0a04SGreg Roach        // ... otherwise apply default behaviour
92a25f0a04SGreg Roach        return parent::canShowByType($access_level);
93a25f0a04SGreg Roach    }
94a25f0a04SGreg Roach
9576692c8bSGreg Roach    /**
9676692c8bSGreg Roach     * Generate a private version of this record
9776692c8bSGreg Roach     *
9876692c8bSGreg Roach     * @param int $access_level
9976692c8bSGreg Roach     *
10076692c8bSGreg Roach     * @return string
10176692c8bSGreg Roach     */
1023c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
103c1010edaSGreg Roach    {
104a25f0a04SGreg Roach        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
105a25f0a04SGreg Roach    }
106a25f0a04SGreg Roach
10776692c8bSGreg Roach    /**
10876692c8bSGreg Roach     * Fetch data from the database
10976692c8bSGreg Roach     *
11076692c8bSGreg Roach     * @param string $xref
11176692c8bSGreg Roach     * @param int    $tree_id
11276692c8bSGreg Roach     *
113e364afe4SGreg Roach     * @return string|null
11476692c8bSGreg Roach     */
115e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
116c1010edaSGreg Roach    {
1172e5b4452SGreg Roach        return DB::table('sources')
1182e5b4452SGreg Roach            ->where('s_id', '=', $xref)
1192e5b4452SGreg Roach            ->where('s_file', '=', $tree_id)
1202e5b4452SGreg Roach            ->value('s_gedcom');
121a25f0a04SGreg Roach    }
122a25f0a04SGreg Roach
12376692c8bSGreg Roach    /**
12476692c8bSGreg Roach     * Extract names from the GEDCOM record.
125c7ff4153SGreg Roach     *
126c7ff4153SGreg Roach     * @return void
12776692c8bSGreg Roach     */
128e364afe4SGreg Roach    public function extractNames(): void
129c1010edaSGreg Roach    {
130e364afe4SGreg Roach        $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL']));
131a25f0a04SGreg Roach    }
132a25f0a04SGreg Roach}
133