xref: /webtrees/app/Source.php (revision 24ec66ce7e77188cd2495b0f8d4dd0ae6e8c9c52)
1a25f0a04SGreg Roach<?php
2dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
3a25f0a04SGreg Roach
4a25f0a04SGreg Roach/**
5a25f0a04SGreg Roach * webtrees: online genealogy
6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10a25f0a04SGreg Roach * (at your option) any later version.
11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a25f0a04SGreg Roach * GNU General Public License for more details.
15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17a25f0a04SGreg Roach */
18a25f0a04SGreg Roach
19a25f0a04SGreg Roach/**
20a25f0a04SGreg Roach * Class Source - A GEDCOM source (SOUR) object
21a25f0a04SGreg Roach */
22a25f0a04SGreg Roachclass Source extends GedcomRecord {
23a25f0a04SGreg Roach	const RECORD_TYPE = 'SOUR';
24a25f0a04SGreg Roach	const URL_PREFIX = 'source.php?sid=';
25a25f0a04SGreg Roach
26a25f0a04SGreg Roach	/**
27a25f0a04SGreg Roach	 * Get an instance of a source object.  For single records,
28a25f0a04SGreg Roach	 * we just receive the XREF.  For bulk records (such as lists
29a25f0a04SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
30a25f0a04SGreg Roach	 *
31a25f0a04SGreg Roach	 * @param string      $xref
32*24ec66ceSGreg Roach	 * @param Tree        $tree
33a25f0a04SGreg Roach	 * @param string|null $gedcom
34a25f0a04SGreg Roach	 *
35a25f0a04SGreg Roach	 * @return Source|null
36a25f0a04SGreg Roach	 */
37*24ec66ceSGreg Roach	public static function getInstance($xref, Tree $tree, $gedcom = null) {
38*24ec66ceSGreg Roach		$record = parent::getInstance($xref, $tree, $gedcom);
39a25f0a04SGreg Roach
40a25f0a04SGreg Roach		if ($record instanceof Source) {
41a25f0a04SGreg Roach			return $record;
42a25f0a04SGreg Roach		} else {
43a25f0a04SGreg Roach			return null;
44a25f0a04SGreg Roach		}
45a25f0a04SGreg Roach	}
46a25f0a04SGreg Roach
47a25f0a04SGreg Roach	/** {@inheritdoc} */
48a25f0a04SGreg Roach	protected function canShowByType($access_level) {
49a25f0a04SGreg Roach		// Hide sources if they are attached to private repositories ...
50a25f0a04SGreg Roach		preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
51a25f0a04SGreg Roach		foreach ($matches[1] as $match) {
52*24ec66ceSGreg Roach			$repo = Repository::getInstance($match, $this->tree);
53a25f0a04SGreg Roach			if ($repo && !$repo->canShow($access_level)) {
54a25f0a04SGreg Roach				return false;
55a25f0a04SGreg Roach			}
56a25f0a04SGreg Roach		}
57a25f0a04SGreg Roach
58a25f0a04SGreg Roach		// ... otherwise apply default behaviour
59a25f0a04SGreg Roach		return parent::canShowByType($access_level);
60a25f0a04SGreg Roach	}
61a25f0a04SGreg Roach
62a25f0a04SGreg Roach	/** {@inheritdoc} */
63a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
64a25f0a04SGreg Roach		return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
65a25f0a04SGreg Roach	}
66a25f0a04SGreg Roach
67a25f0a04SGreg Roach	/** {@inheritdoc} */
68a25f0a04SGreg Roach	protected static function fetchGedcomRecord($xref, $gedcom_id) {
69a25f0a04SGreg Roach		static $statement = null;
70a25f0a04SGreg Roach
71a25f0a04SGreg Roach		if ($statement === null) {
72a25f0a04SGreg Roach			$statement = Database::prepare("SELECT s_gedcom FROM `##sources` WHERE s_id=? AND s_file=?");
73a25f0a04SGreg Roach		}
74a25f0a04SGreg Roach
75a25f0a04SGreg Roach		return $statement->execute(array($xref, $gedcom_id))->fetchOne();
76a25f0a04SGreg Roach	}
77a25f0a04SGreg Roach
78a25f0a04SGreg Roach	/** {@inheritdoc} */
79a25f0a04SGreg Roach	public function extractNames() {
8036a0c51dSGreg Roach		parent::extractNamesFromFacts(1, 'TITL', $this->getFacts('TITL'));
81a25f0a04SGreg Roach	}
82a25f0a04SGreg Roach}
83