xref: /webtrees/app/Source.php (revision 1ef93f16db4dd1521de3f5f0727918e36a2fccb4)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 ROUTE_NAME  = 'source';
24
25	/**
26	 * Get an instance of a source object. For single records,
27	 * we just receive the XREF. For bulk records (such as lists
28	 * and search results) we can receive the GEDCOM data as well.
29	 *
30	 * @param string      $xref
31	 * @param Tree        $tree
32	 * @param string|null $gedcom
33	 *
34	 * @throws \Exception
35	 *
36	 * @return Source|null
37	 */
38	public static function getInstance($xref, Tree $tree, $gedcom = null) {
39		$record = parent::getInstance($xref, $tree, $gedcom);
40
41		if ($record instanceof Source) {
42			return $record;
43		} else {
44			return null;
45		}
46	}
47
48	/**
49	 * Each object type may have its own special rules, and re-implement this function.
50	 *
51	 * @param int $access_level
52	 *
53	 * @return bool
54	 */
55	protected function canShowByType($access_level) {
56		// Hide sources if they are attached to private repositories ...
57		preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches);
58		foreach ($matches[1] as $match) {
59			$repo = Repository::getInstance($match, $this->tree);
60			if ($repo && !$repo->canShow($access_level)) {
61				return false;
62			}
63		}
64
65		// ... otherwise apply default behaviour
66		return parent::canShowByType($access_level);
67	}
68
69	/**
70	 * Generate a private version of this record
71	 *
72	 * @param int $access_level
73	 *
74	 * @return string
75	 */
76	protected function createPrivateGedcomRecord($access_level) {
77		return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private');
78	}
79
80	/**
81	 * Fetch data from the database
82	 *
83	 * @param string $xref
84	 * @param int    $tree_id
85	 *
86	 * @return null|string
87	 */
88	protected static function fetchGedcomRecord($xref, $tree_id) {
89		return Database::prepare(
90			"SELECT s_gedcom FROM `##sources` WHERE s_id = :xref AND s_file = :tree_id"
91		)->execute([
92			'xref'    => $xref,
93			'tree_id' => $tree_id,
94		])->fetchOne();
95	}
96
97	/**
98	 * Extract names from the GEDCOM record.
99	 */
100	public function extractNames() {
101		parent::extractNamesFromFacts(1, 'TITL', $this->getFacts('TITL'));
102	}
103}
104