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