xref: /webtrees/app/Note.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
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 the text contents of the note
28	 *
29	 * @return string|null
30	 */
31	public function getNote() {
32		if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
33			return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
34		} else {
35			return null;
36		}
37	}
38
39	/** {@inheritdoc} */
40	protected function canShowByType($access_level) {
41		// Hide notes if they are attached to private records
42		$linked_ids = Database::prepare(
43			"SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
44		)->execute(array(
45			$this->xref, $this->tree->getTreeId(),
46		))->fetchOneColumn();
47		foreach ($linked_ids as $linked_id) {
48			$linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
49			if ($linked_record && !$linked_record->canShow($access_level)) {
50				return false;
51			}
52		}
53
54		// Apply default behaviour
55		return parent::canShowByType($access_level);
56	}
57
58	/** {@inheritdoc} */
59	protected function createPrivateGedcomRecord($access_level) {
60		return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
61	}
62
63	/** {@inheritdoc} */
64	protected static function fetchGedcomRecord($xref, $tree_id) {
65		return Database::prepare(
66			"SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'"
67		)->execute(array(
68			'xref'    => $xref,
69			'tree_id' => $tree_id,
70		))->fetchOne();
71	}
72
73	/**
74	 * Create a name for this note - apply (and remove) markup, then take
75	 * a maximum of 100 characters from the first line.
76	 *
77	 * {@inheritdoc}
78	 */
79	public function extractNames() {
80		$text = $this->getNote();
81
82		if ($text) {
83			switch ($this->getTree()->getPreference('FORMAT_TEXT')) {
84			case 'markdown':
85				$text = Filter::markdown($text);
86				$text = Filter::unescapeHtml($text);
87				break;
88			}
89
90			list($text) = explode("\n", $text);
91			$this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
92		}
93	}
94}
95