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