xref: /webtrees/app/Note.php (revision 1f3fb95c905e623518962b025974a0e6c19d068b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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 note (NOTE) object.
20 */
21class Note extends GedcomRecord {
22	const RECORD_TYPE = 'NOTE';
23	const URL_PREFIX  = 'note.php?nid=';
24
25	/**
26	 * Get the text contents of the note
27	 *
28	 * @return string|null
29	 */
30	public function getNote() {
31		if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
32			return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
33		} else {
34			return null;
35		}
36	}
37
38	/**
39	 * Each object type may have its own special rules, and re-implement this function.
40	 *
41	 * @param int $access_level
42	 *
43	 * @return bool
44	 */
45	protected function canShowByType($access_level) {
46		// Hide notes if they are attached to private records
47		$linked_ids = Database::prepare(
48			"SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
49		)->execute([
50			$this->xref, $this->tree->getTreeId(),
51		])->fetchOneColumn();
52		foreach ($linked_ids as $linked_id) {
53			$linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
54			if ($linked_record && !$linked_record->canShow($access_level)) {
55				return false;
56			}
57		}
58
59		// Apply default behaviour
60		return parent::canShowByType($access_level);
61	}
62
63	/**
64	 * Generate a private version of this record
65	 *
66	 * @param int $access_level
67	 *
68	 * @return string
69	 */
70	protected function createPrivateGedcomRecord($access_level) {
71		return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
72	}
73
74	/**
75	 * Fetch data from the database
76	 *
77	 * @param string $xref
78	 * @param int    $tree_id
79	 *
80	 * @return null|string
81	 */
82	protected static function fetchGedcomRecord($xref, $tree_id) {
83		return Database::prepare(
84			"SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'"
85		)->execute([
86			'xref'    => $xref,
87			'tree_id' => $tree_id,
88		])->fetchOne();
89	}
90
91	/**
92	 * Create a name for this note - apply (and remove) markup, then take
93	 * a maximum of 100 characters from the first line.
94	 */
95	public function extractNames() {
96		$text = $this->getNote();
97
98		if ($text) {
99			switch ($this->getTree()->getPreference('FORMAT_TEXT')) {
100			case 'markdown':
101				$text = Filter::markdown($text, $this->getTree());
102				$text = strip_tags($text);
103				$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
104				break;
105			}
106
107			list($text) = explode("\n", $text);
108			$this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
109		}
110	}
111}
112