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