xref: /webtrees/app/Source.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4a25f0a04SGreg Roach * Copyright (C) 2015 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*76692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roach/**
19*76692c8bSGreg Roach * A GEDCOM source (SOUR) object.
20a25f0a04SGreg Roach */
21a25f0a04SGreg Roachclass Source extends GedcomRecord {
22a25f0a04SGreg Roach	const RECORD_TYPE = 'SOUR';
23a25f0a04SGreg Roach	const URL_PREFIX  = 'source.php?sid=';
24a25f0a04SGreg Roach
25*76692c8bSGreg Roach	/**
26*76692c8bSGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
27*76692c8bSGreg Roach	 *
28*76692c8bSGreg Roach	 * @param int $access_level
29*76692c8bSGreg Roach	 *
30*76692c8bSGreg Roach	 * @return bool
31*76692c8bSGreg Roach	 */
32a25f0a04SGreg Roach	protected function canShowByType($access_level) {
33a25f0a04SGreg Roach		// Hide sources if they are attached to private repositories ...
34a25f0a04SGreg Roach		preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
35a25f0a04SGreg Roach		foreach ($matches[1] as $match) {
3624ec66ceSGreg Roach			$repo = Repository::getInstance($match, $this->tree);
37a25f0a04SGreg Roach			if ($repo && !$repo->canShow($access_level)) {
38a25f0a04SGreg Roach				return false;
39a25f0a04SGreg Roach			}
40a25f0a04SGreg Roach		}
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach		// ... otherwise apply default behaviour
43a25f0a04SGreg Roach		return parent::canShowByType($access_level);
44a25f0a04SGreg Roach	}
45a25f0a04SGreg Roach
46*76692c8bSGreg Roach	/**
47*76692c8bSGreg Roach	 * Generate a private version of this record
48*76692c8bSGreg Roach	 *
49*76692c8bSGreg Roach	 * @param int $access_level
50*76692c8bSGreg Roach	 *
51*76692c8bSGreg Roach	 * @return string
52*76692c8bSGreg Roach	 */
53a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
54a25f0a04SGreg Roach		return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
55a25f0a04SGreg Roach	}
56a25f0a04SGreg Roach
57*76692c8bSGreg Roach	/**
58*76692c8bSGreg Roach	 * Fetch data from the database
59*76692c8bSGreg Roach	 *
60*76692c8bSGreg Roach	 * @param string $xref
61*76692c8bSGreg Roach	 * @param int    $tree_id
62*76692c8bSGreg Roach	 *
63*76692c8bSGreg Roach	 * @return null|string
64*76692c8bSGreg Roach	 */
6564d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
6664d9078aSGreg Roach		return Database::prepare(
6764d9078aSGreg Roach			"SELECT s_gedcom FROM `##sources` WHERE s_id = :xref AND s_file = :tree_id"
6864d9078aSGreg Roach		)->execute(array(
6964d9078aSGreg Roach			'xref'    => $xref,
7064d9078aSGreg Roach			'tree_id' => $tree_id,
7164d9078aSGreg Roach		))->fetchOne();
72a25f0a04SGreg Roach	}
73a25f0a04SGreg Roach
74*76692c8bSGreg Roach	/**
75*76692c8bSGreg Roach	 * Extract names from the GEDCOM record.
76*76692c8bSGreg Roach	 */
77a25f0a04SGreg Roach	public function extractNames() {
7836a0c51dSGreg Roach		parent::extractNamesFromFacts(1, 'TITL', $this->getFacts('TITL'));
79a25f0a04SGreg Roach	}
80a25f0a04SGreg Roach}
81