xref: /webtrees/app/Note.php (revision dd04c183d8beed05be2226b30b7dda485ea4538a)
1a25f0a04SGreg Roach<?php
2*dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
3a25f0a04SGreg Roach
4a25f0a04SGreg Roach/**
5a25f0a04SGreg Roach * webtrees: online genealogy
6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10a25f0a04SGreg Roach * (at your option) any later version.
11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a25f0a04SGreg Roach * GNU General Public License for more details.
15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17a25f0a04SGreg Roach */
18a25f0a04SGreg Roach
19a25f0a04SGreg Roach/**
20a25f0a04SGreg Roach * Class Note - Class file for a Shared Note (NOTE) object
21a25f0a04SGreg Roach */
22a25f0a04SGreg Roachclass Note extends GedcomRecord {
23a25f0a04SGreg Roach	const RECORD_TYPE = 'NOTE';
24a25f0a04SGreg Roach	const URL_PREFIX = 'note.php?nid=';
25a25f0a04SGreg Roach
26a25f0a04SGreg Roach	/**
27a25f0a04SGreg Roach	 * Get an instance of a note object.  For single records,
28a25f0a04SGreg Roach	 * we just receive the XREF.  For bulk records (such as lists
29a25f0a04SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
30a25f0a04SGreg Roach	 *
31a25f0a04SGreg Roach	 * @param string       $xref
32a25f0a04SGreg Roach	 * @param integer|null $gedcom_id
33a25f0a04SGreg Roach	 * @param string|null  $gedcom
34a25f0a04SGreg Roach	 *
35a25f0a04SGreg Roach	 * @return Note|null
36a25f0a04SGreg Roach	 */
37a25f0a04SGreg Roach	public static function getInstance($xref, $gedcom_id = WT_GED_ID, $gedcom = null) {
38a25f0a04SGreg Roach		$record = parent::getInstance($xref, $gedcom_id, $gedcom);
39a25f0a04SGreg Roach
40a25f0a04SGreg Roach		if ($record instanceof Note) {
41a25f0a04SGreg Roach			return $record;
42a25f0a04SGreg Roach		} else {
43a25f0a04SGreg Roach			return null;
44a25f0a04SGreg Roach		}
45a25f0a04SGreg Roach	}
46a25f0a04SGreg Roach
47a25f0a04SGreg Roach	/**
48a25f0a04SGreg Roach	 * Get the text contents of the note
49a25f0a04SGreg Roach	 *
50a25f0a04SGreg Roach	 * @return string|null
51a25f0a04SGreg Roach	 */
52a25f0a04SGreg Roach	public function getNote() {
53a25f0a04SGreg Roach		if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
54a25f0a04SGreg Roach			return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
55a25f0a04SGreg Roach		} else {
56a25f0a04SGreg Roach			return null;
57a25f0a04SGreg Roach		}
58a25f0a04SGreg Roach	}
59a25f0a04SGreg Roach
60a25f0a04SGreg Roach	/** {@inheritdoc} */
61a25f0a04SGreg Roach	protected function canShowByType($access_level) {
62a25f0a04SGreg Roach		// Hide notes if they are attached to private records
63a25f0a04SGreg Roach		$linked_ids = Database::prepare(
64a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
65a25f0a04SGreg Roach		)->execute(array($this->xref, $this->gedcom_id))->fetchOneColumn();
66a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
67a25f0a04SGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id);
68a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
69a25f0a04SGreg Roach				return false;
70a25f0a04SGreg Roach			}
71a25f0a04SGreg Roach		}
72a25f0a04SGreg Roach
73a25f0a04SGreg Roach		// Apply default behaviour
74a25f0a04SGreg Roach		return parent::canShowByType($access_level);
75a25f0a04SGreg Roach	}
76a25f0a04SGreg Roach
77a25f0a04SGreg Roach	/** {@inheritdoc} */
78a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
79a25f0a04SGreg Roach		return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
80a25f0a04SGreg Roach	}
81a25f0a04SGreg Roach
82a25f0a04SGreg Roach	/** {@inheritdoc} */
83a25f0a04SGreg Roach	protected static function fetchGedcomRecord($xref, $gedcom_id) {
84a25f0a04SGreg Roach		static $statement = null;
85a25f0a04SGreg Roach
86a25f0a04SGreg Roach		if ($statement === null) {
87a25f0a04SGreg Roach			$statement = Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_id=? AND o_file=? AND o_type='NOTE'");
88a25f0a04SGreg Roach		}
89a25f0a04SGreg Roach
90a25f0a04SGreg Roach		return $statement->execute(array($xref, $gedcom_id))->fetchOne();
91a25f0a04SGreg Roach	}
92a25f0a04SGreg Roach
93a25f0a04SGreg Roach	/**
94a25f0a04SGreg Roach	 * Create a name for this note - apply (and remove) markup, then take
95a25f0a04SGreg Roach	 * a maximum of 100 characters from the first line.
96a25f0a04SGreg Roach	 *
97a25f0a04SGreg Roach	 * {@inheritdoc}
98a25f0a04SGreg Roach	 */
99a25f0a04SGreg Roach	public function extractNames() {
100a25f0a04SGreg Roach		global $WT_TREE;
101a25f0a04SGreg Roach
102a25f0a04SGreg Roach		$text = $this->getNote();
103a25f0a04SGreg Roach
104a25f0a04SGreg Roach		if ($text) {
105a25f0a04SGreg Roach			switch ($WT_TREE->getPreference('FORMAT_TEXT')) {
106a25f0a04SGreg Roach			case 'markdown':
107a25f0a04SGreg Roach				$text = Filter::markdown($text);
108a25f0a04SGreg Roach				$text = Filter::unescapeHtml($text);
109a25f0a04SGreg Roach				break;
110a25f0a04SGreg Roach			}
111a25f0a04SGreg Roach
112a25f0a04SGreg Roach			list($text) = explode("\n", $text);
113a25f0a04SGreg Roach			$this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
114a25f0a04SGreg Roach		}
115a25f0a04SGreg Roach	}
116a25f0a04SGreg Roach}
117