xref: /webtrees/app/Note.php (revision e71ef9d2ff0aa5d82e80016f655eee252a32e958)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roach/**
1976692c8bSGreg Roach * A GEDCOM note (NOTE) object.
20a25f0a04SGreg Roach */
21a25f0a04SGreg Roachclass Note extends GedcomRecord {
22a25f0a04SGreg Roach	const RECORD_TYPE = 'NOTE';
23225e381fSGreg Roach	const ROUTE_NAME  = 'note';
24a25f0a04SGreg Roach
25a25f0a04SGreg Roach	/**
26*e71ef9d2SGreg Roach	 * Get an instance of a note object. For single records,
27*e71ef9d2SGreg Roach	 * we just receive the XREF. For bulk records (such as lists
28*e71ef9d2SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
29*e71ef9d2SGreg Roach	 *
30*e71ef9d2SGreg Roach	 * @param string      $xref
31*e71ef9d2SGreg Roach	 * @param Tree        $tree
32*e71ef9d2SGreg Roach	 * @param string|null $gedcom
33*e71ef9d2SGreg Roach	 *
34*e71ef9d2SGreg Roach	 * @throws \Exception
35*e71ef9d2SGreg Roach	 *
36*e71ef9d2SGreg Roach	 * @return Note|null
37*e71ef9d2SGreg Roach	 */
38*e71ef9d2SGreg Roach	public static function getInstance($xref, Tree $tree, $gedcom = null) {
39*e71ef9d2SGreg Roach		$record = parent::getInstance($xref, $tree, $gedcom);
40*e71ef9d2SGreg Roach
41*e71ef9d2SGreg Roach		if ($record instanceof Note) {
42*e71ef9d2SGreg Roach			return $record;
43*e71ef9d2SGreg Roach		} else {
44*e71ef9d2SGreg Roach			return null;
45*e71ef9d2SGreg Roach		}
46*e71ef9d2SGreg Roach	}
47*e71ef9d2SGreg Roach
48*e71ef9d2SGreg Roach	/**
49a25f0a04SGreg Roach	 * Get the text contents of the note
50a25f0a04SGreg Roach	 *
51a25f0a04SGreg Roach	 * @return string|null
52a25f0a04SGreg Roach	 */
53a25f0a04SGreg Roach	public function getNote() {
54a25f0a04SGreg Roach		if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
55a25f0a04SGreg Roach			return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
56a25f0a04SGreg Roach		} else {
57a25f0a04SGreg Roach			return null;
58a25f0a04SGreg Roach		}
59a25f0a04SGreg Roach	}
60a25f0a04SGreg Roach
6176692c8bSGreg Roach	/**
6276692c8bSGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
6376692c8bSGreg Roach	 *
6476692c8bSGreg Roach	 * @param int $access_level
6576692c8bSGreg Roach	 *
6676692c8bSGreg Roach	 * @return bool
6776692c8bSGreg Roach	 */
68a25f0a04SGreg Roach	protected function canShowByType($access_level) {
69a25f0a04SGreg Roach		// Hide notes if they are attached to private records
70a25f0a04SGreg Roach		$linked_ids = Database::prepare(
71a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
7213abd6f3SGreg Roach		)->execute([
73cbc1590aSGreg Roach			$this->xref, $this->tree->getTreeId(),
7413abd6f3SGreg Roach		])->fetchOneColumn();
75a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
7624ec66ceSGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
77a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
78a25f0a04SGreg Roach				return false;
79a25f0a04SGreg Roach			}
80a25f0a04SGreg Roach		}
81a25f0a04SGreg Roach
82a25f0a04SGreg Roach		// Apply default behaviour
83a25f0a04SGreg Roach		return parent::canShowByType($access_level);
84a25f0a04SGreg Roach	}
85a25f0a04SGreg Roach
8676692c8bSGreg Roach	/**
8776692c8bSGreg Roach	 * Generate a private version of this record
8876692c8bSGreg Roach	 *
8976692c8bSGreg Roach	 * @param int $access_level
9076692c8bSGreg Roach	 *
9176692c8bSGreg Roach	 * @return string
9276692c8bSGreg Roach	 */
93a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
94a25f0a04SGreg Roach		return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
95a25f0a04SGreg Roach	}
96a25f0a04SGreg Roach
9776692c8bSGreg Roach	/**
9876692c8bSGreg Roach	 * Fetch data from the database
9976692c8bSGreg Roach	 *
10076692c8bSGreg Roach	 * @param string $xref
10176692c8bSGreg Roach	 * @param int    $tree_id
10276692c8bSGreg Roach	 *
10376692c8bSGreg Roach	 * @return null|string
10476692c8bSGreg Roach	 */
10564d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
10664d9078aSGreg Roach		return Database::prepare(
10764d9078aSGreg Roach			"SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'"
10813abd6f3SGreg Roach		)->execute([
10964d9078aSGreg Roach			'xref'    => $xref,
110cbc1590aSGreg Roach			'tree_id' => $tree_id,
11113abd6f3SGreg Roach		])->fetchOne();
112a25f0a04SGreg Roach	}
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach	/**
115a25f0a04SGreg Roach	 * Create a name for this note - apply (and remove) markup, then take
116a25f0a04SGreg Roach	 * a maximum of 100 characters from the first line.
117a25f0a04SGreg Roach	 */
118a25f0a04SGreg Roach	public function extractNames() {
119a25f0a04SGreg Roach		$text = $this->getNote();
120a25f0a04SGreg Roach
121a25f0a04SGreg Roach		if ($text) {
12284caa210SGreg Roach			switch ($this->getTree()->getPreference('FORMAT_TEXT')) {
123a25f0a04SGreg Roach				case 'markdown':
12422acdb08SGreg Roach					$text = Filter::markdown($text, $this->getTree());
1259524b7b5SGreg Roach					$text = strip_tags($text);
1269524b7b5SGreg Roach					$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
127a25f0a04SGreg Roach					break;
128a25f0a04SGreg Roach			}
129a25f0a04SGreg Roach
130a25f0a04SGreg Roach			list($text) = explode("\n", $text);
131a25f0a04SGreg Roach			$this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
132a25f0a04SGreg Roach		}
133a25f0a04SGreg Roach	}
134a25f0a04SGreg Roach}
135