xref: /webtrees/app/Note.php (revision 9524b7b5895902d235d01b514319510ed7c9f934)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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';
23a25f0a04SGreg Roach	const URL_PREFIX  = 'note.php?nid=';
24a25f0a04SGreg Roach
25a25f0a04SGreg Roach	/**
26a25f0a04SGreg Roach	 * Get the text contents of the note
27a25f0a04SGreg Roach	 *
28a25f0a04SGreg Roach	 * @return string|null
29a25f0a04SGreg Roach	 */
30a25f0a04SGreg Roach	public function getNote() {
31a25f0a04SGreg Roach		if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
32a25f0a04SGreg Roach			return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
33a25f0a04SGreg Roach		} else {
34a25f0a04SGreg Roach			return null;
35a25f0a04SGreg Roach		}
36a25f0a04SGreg Roach	}
37a25f0a04SGreg Roach
3876692c8bSGreg Roach	/**
3976692c8bSGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
4076692c8bSGreg Roach	 *
4176692c8bSGreg Roach	 * @param int $access_level
4276692c8bSGreg Roach	 *
4376692c8bSGreg Roach	 * @return bool
4476692c8bSGreg Roach	 */
45a25f0a04SGreg Roach	protected function canShowByType($access_level) {
46a25f0a04SGreg Roach		// Hide notes if they are attached to private records
47a25f0a04SGreg Roach		$linked_ids = Database::prepare(
48a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
4913abd6f3SGreg Roach		)->execute([
50cbc1590aSGreg Roach			$this->xref, $this->tree->getTreeId(),
5113abd6f3SGreg Roach		])->fetchOneColumn();
52a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
5324ec66ceSGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
54a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
55a25f0a04SGreg Roach				return false;
56a25f0a04SGreg Roach			}
57a25f0a04SGreg Roach		}
58a25f0a04SGreg Roach
59a25f0a04SGreg Roach		// Apply default behaviour
60a25f0a04SGreg Roach		return parent::canShowByType($access_level);
61a25f0a04SGreg Roach	}
62a25f0a04SGreg Roach
6376692c8bSGreg Roach	/**
6476692c8bSGreg Roach	 * Generate a private version of this record
6576692c8bSGreg Roach	 *
6676692c8bSGreg Roach	 * @param int $access_level
6776692c8bSGreg Roach	 *
6876692c8bSGreg Roach	 * @return string
6976692c8bSGreg Roach	 */
70a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
71a25f0a04SGreg Roach		return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
72a25f0a04SGreg Roach	}
73a25f0a04SGreg Roach
7476692c8bSGreg Roach	/**
7576692c8bSGreg Roach	 * Fetch data from the database
7676692c8bSGreg Roach	 *
7776692c8bSGreg Roach	 * @param string $xref
7876692c8bSGreg Roach	 * @param int    $tree_id
7976692c8bSGreg Roach	 *
8076692c8bSGreg Roach	 * @return null|string
8176692c8bSGreg Roach	 */
8264d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
8364d9078aSGreg Roach		return Database::prepare(
8464d9078aSGreg Roach			"SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'"
8513abd6f3SGreg Roach		)->execute([
8664d9078aSGreg Roach			'xref'    => $xref,
87cbc1590aSGreg Roach			'tree_id' => $tree_id,
8813abd6f3SGreg Roach		])->fetchOne();
89a25f0a04SGreg Roach	}
90a25f0a04SGreg Roach
91a25f0a04SGreg Roach	/**
92a25f0a04SGreg Roach	 * Create a name for this note - apply (and remove) markup, then take
93a25f0a04SGreg Roach	 * a maximum of 100 characters from the first line.
94a25f0a04SGreg Roach	 */
95a25f0a04SGreg Roach	public function extractNames() {
96a25f0a04SGreg Roach		$text = $this->getNote();
97a25f0a04SGreg Roach
98a25f0a04SGreg Roach		if ($text) {
9984caa210SGreg Roach			switch ($this->getTree()->getPreference('FORMAT_TEXT')) {
100a25f0a04SGreg Roach			case 'markdown':
101a25f0a04SGreg Roach				$text = Filter::markdown($text);
102*9524b7b5SGreg Roach				$text = strip_tags($text);
103*9524b7b5SGreg Roach				$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
104a25f0a04SGreg Roach				break;
105a25f0a04SGreg Roach			}
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach			list($text) = explode("\n", $text);
108a25f0a04SGreg Roach			$this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
109a25f0a04SGreg Roach		}
110a25f0a04SGreg Roach	}
111a25f0a04SGreg Roach}
112