xref: /webtrees/app/Source.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16*e7f56f2aSGreg Roachdeclare(strict_types=1);
17*e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roach/**
2176692c8bSGreg Roach * A GEDCOM source (SOUR) object.
22a25f0a04SGreg Roach */
23c1010edaSGreg Roachclass Source extends GedcomRecord
24c1010edaSGreg Roach{
25a25f0a04SGreg Roach    const RECORD_TYPE = 'SOUR';
26225e381fSGreg Roach    const ROUTE_NAME  = 'source';
27a25f0a04SGreg Roach
2876692c8bSGreg Roach    /**
29e71ef9d2SGreg Roach     * Get an instance of a source object. For single records,
30e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
31e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
32e71ef9d2SGreg Roach     *
33e71ef9d2SGreg Roach     * @param string      $xref
34e71ef9d2SGreg Roach     * @param Tree        $tree
35e71ef9d2SGreg Roach     * @param string|null $gedcom
36e71ef9d2SGreg Roach     *
37e71ef9d2SGreg Roach     * @throws \Exception
38e71ef9d2SGreg Roach     *
39e71ef9d2SGreg Roach     * @return Source|null
40e71ef9d2SGreg Roach     */
4176f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
42c1010edaSGreg Roach    {
43e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
44e71ef9d2SGreg Roach
45e71ef9d2SGreg Roach        if ($record instanceof Source) {
46e71ef9d2SGreg Roach            return $record;
47e71ef9d2SGreg Roach        }
48b2ce94c6SRico Sonntag
49b2ce94c6SRico Sonntag        return null;
50e71ef9d2SGreg Roach    }
51e71ef9d2SGreg Roach
52e71ef9d2SGreg Roach    /**
5376692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
5476692c8bSGreg Roach     *
5576692c8bSGreg Roach     * @param int $access_level
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @return bool
5876692c8bSGreg Roach     */
5935584196SGreg Roach    protected function canShowByType(int $access_level): bool
60c1010edaSGreg Roach    {
61a25f0a04SGreg Roach        // Hide sources if they are attached to private repositories ...
62a25f0a04SGreg Roach        preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
63a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
6424ec66ceSGreg Roach            $repo = Repository::getInstance($match, $this->tree);
65a25f0a04SGreg Roach            if ($repo && !$repo->canShow($access_level)) {
66a25f0a04SGreg Roach                return false;
67a25f0a04SGreg Roach            }
68a25f0a04SGreg Roach        }
69a25f0a04SGreg Roach
70a25f0a04SGreg Roach        // ... otherwise apply default behaviour
71a25f0a04SGreg Roach        return parent::canShowByType($access_level);
72a25f0a04SGreg Roach    }
73a25f0a04SGreg Roach
7476692c8bSGreg Roach    /**
7576692c8bSGreg Roach     * Generate a private version of this record
7676692c8bSGreg Roach     *
7776692c8bSGreg Roach     * @param int $access_level
7876692c8bSGreg Roach     *
7976692c8bSGreg Roach     * @return string
8076692c8bSGreg Roach     */
813c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
82c1010edaSGreg Roach    {
83a25f0a04SGreg Roach        return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
84a25f0a04SGreg Roach    }
85a25f0a04SGreg Roach
8676692c8bSGreg Roach    /**
8776692c8bSGreg Roach     * Fetch data from the database
8876692c8bSGreg Roach     *
8976692c8bSGreg Roach     * @param string $xref
9076692c8bSGreg Roach     * @param int    $tree_id
9176692c8bSGreg Roach     *
9276692c8bSGreg Roach     * @return null|string
9376692c8bSGreg Roach     */
9476f666f4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
95c1010edaSGreg Roach    {
9664d9078aSGreg Roach        return Database::prepare(
9764d9078aSGreg Roach            "SELECT s_gedcom FROM `##sources` WHERE s_id = :xref AND s_file = :tree_id"
9813abd6f3SGreg Roach        )->execute([
9964d9078aSGreg Roach            'xref'    => $xref,
10064d9078aSGreg Roach            'tree_id' => $tree_id,
10113abd6f3SGreg Roach        ])->fetchOne();
102a25f0a04SGreg Roach    }
103a25f0a04SGreg Roach
10476692c8bSGreg Roach    /**
10576692c8bSGreg Roach     * Extract names from the GEDCOM record.
106c7ff4153SGreg Roach     *
107c7ff4153SGreg Roach     * @return void
10876692c8bSGreg Roach     */
109c1010edaSGreg Roach    public function extractNames()
110c1010edaSGreg Roach    {
11136a0c51dSGreg Roach        parent::extractNamesFromFacts(1, 'TITL', $this->getFacts('TITL'));
112a25f0a04SGreg Roach    }
113a25f0a04SGreg Roach}
114