xref: /webtrees/app/Source.php (revision 7079cc8697c5f4c73ae0efb667272f104cc34240)
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	/**
27	 * Get an instance of a source object.  For single records,
28	 * we just receive the XREF.  For bulk records (such as lists
29	 * and search results) we can receive the GEDCOM data as well.
30	 *
31	 * @param string       $xref
32	 * @param integer|null $gedcom_id
33	 * @param string|null  $gedcom
34	 *
35	 * @return Source|null
36	 */
37	public static function getInstance($xref, $gedcom_id = WT_GED_ID, $gedcom = null) {
38		$record = parent::getInstance($xref, $gedcom_id, $gedcom);
39
40		if ($record instanceof Source) {
41			return $record;
42		} else {
43			return null;
44		}
45	}
46
47	/** {@inheritdoc} */
48	protected function canShowByType($access_level) {
49		// Hide sources if they are attached to private repositories ...
50		preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
51		foreach ($matches[1] as $match) {
52			$repo = Repository::getInstance($match);
53			if ($repo && !$repo->canShow($access_level)) {
54				return false;
55			}
56		}
57
58		// ... otherwise apply default behaviour
59		return parent::canShowByType($access_level);
60	}
61
62	/** {@inheritdoc} */
63	protected function createPrivateGedcomRecord($access_level) {
64		return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
65	}
66
67	/** {@inheritdoc} */
68	protected static function fetchGedcomRecord($xref, $gedcom_id) {
69		static $statement = null;
70
71		if ($statement === null) {
72			$statement = Database::prepare("SELECT s_gedcom FROM `##sources` WHERE s_id=? AND s_file=?");
73		}
74
75		return $statement->execute(array($xref, $gedcom_id))->fetchOne();
76	}
77
78	/** {@inheritdoc} */
79	public function extractNames() {
80		parent::extractNamesFromFacts(1, 'TITL', $this->getFacts('TITL'));
81	}
82}
83