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