xref: /webtrees/app/Note.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4a25f0a04SGreg Roach * Copyright (C) 2015 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 */
16*76692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roach/**
19*76692c8bSGreg 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
38*76692c8bSGreg Roach	/**
39*76692c8bSGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
40*76692c8bSGreg Roach	 *
41*76692c8bSGreg Roach	 * @param int $access_level
42*76692c8bSGreg Roach	 *
43*76692c8bSGreg Roach	 * @return bool
44*76692c8bSGreg 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=?"
49000959d9SGreg Roach		)->execute(array(
50cbc1590aSGreg Roach			$this->xref, $this->tree->getTreeId(),
51000959d9SGreg 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
63*76692c8bSGreg Roach	/**
64*76692c8bSGreg Roach	 * Generate a private version of this record
65*76692c8bSGreg Roach	 *
66*76692c8bSGreg Roach	 * @param int $access_level
67*76692c8bSGreg Roach	 *
68*76692c8bSGreg Roach	 * @return string
69*76692c8bSGreg Roach	 */
70a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
71a25f0a04SGreg Roach		return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
72a25f0a04SGreg Roach	}
73a25f0a04SGreg Roach
74*76692c8bSGreg Roach	/**
75*76692c8bSGreg Roach	 * Fetch data from the database
76*76692c8bSGreg Roach	 *
77*76692c8bSGreg Roach	 * @param string $xref
78*76692c8bSGreg Roach	 * @param int    $tree_id
79*76692c8bSGreg Roach	 *
80*76692c8bSGreg Roach	 * @return null|string
81*76692c8bSGreg 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'"
8564d9078aSGreg Roach		)->execute(array(
8664d9078aSGreg Roach			'xref'    => $xref,
87cbc1590aSGreg Roach			'tree_id' => $tree_id,
8864d9078aSGreg 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);
102a25f0a04SGreg Roach				$text = Filter::unescapeHtml($text);
103a25f0a04SGreg Roach				break;
104a25f0a04SGreg Roach			}
105a25f0a04SGreg Roach
106a25f0a04SGreg Roach			list($text) = explode("\n", $text);
107a25f0a04SGreg Roach			$this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
108a25f0a04SGreg Roach		}
109a25f0a04SGreg Roach	}
110a25f0a04SGreg Roach}
111