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