xref: /webtrees/app/Note.php (revision a25f0a04682c4c39c1947220c90af4118c713952)
1*a25f0a04SGreg Roach<?php
2*a25f0a04SGreg Roachnamespace Webtrees;
3*a25f0a04SGreg Roach
4*a25f0a04SGreg Roach/**
5*a25f0a04SGreg Roach * webtrees: online genealogy
6*a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7*a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8*a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9*a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10*a25f0a04SGreg Roach * (at your option) any later version.
11*a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12*a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*a25f0a04SGreg Roach * GNU General Public License for more details.
15*a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16*a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17*a25f0a04SGreg Roach */
18*a25f0a04SGreg Roach
19*a25f0a04SGreg Roach/**
20*a25f0a04SGreg Roach * Class Note - Class file for a Shared Note (NOTE) object
21*a25f0a04SGreg Roach */
22*a25f0a04SGreg Roachclass Note extends GedcomRecord {
23*a25f0a04SGreg Roach	const RECORD_TYPE = 'NOTE';
24*a25f0a04SGreg Roach	const URL_PREFIX = 'note.php?nid=';
25*a25f0a04SGreg Roach
26*a25f0a04SGreg Roach	/**
27*a25f0a04SGreg Roach	 * Get an instance of a note object.  For single records,
28*a25f0a04SGreg Roach	 * we just receive the XREF.  For bulk records (such as lists
29*a25f0a04SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
30*a25f0a04SGreg Roach	 *
31*a25f0a04SGreg Roach	 * @param string       $xref
32*a25f0a04SGreg Roach	 * @param integer|null $gedcom_id
33*a25f0a04SGreg Roach	 * @param string|null  $gedcom
34*a25f0a04SGreg Roach	 *
35*a25f0a04SGreg Roach	 * @return Note|null
36*a25f0a04SGreg Roach	 */
37*a25f0a04SGreg Roach	public static function getInstance($xref, $gedcom_id = WT_GED_ID, $gedcom = null) {
38*a25f0a04SGreg Roach		$record = parent::getInstance($xref, $gedcom_id, $gedcom);
39*a25f0a04SGreg Roach
40*a25f0a04SGreg Roach		if ($record instanceof Note) {
41*a25f0a04SGreg Roach			return $record;
42*a25f0a04SGreg Roach		} else {
43*a25f0a04SGreg Roach			return null;
44*a25f0a04SGreg Roach		}
45*a25f0a04SGreg Roach	}
46*a25f0a04SGreg Roach
47*a25f0a04SGreg Roach	/**
48*a25f0a04SGreg Roach	 * Get the text contents of the note
49*a25f0a04SGreg Roach	 *
50*a25f0a04SGreg Roach	 * @return string|null
51*a25f0a04SGreg Roach	 */
52*a25f0a04SGreg Roach	public function getNote() {
53*a25f0a04SGreg Roach		if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
54*a25f0a04SGreg Roach			return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
55*a25f0a04SGreg Roach		} else {
56*a25f0a04SGreg Roach			return null;
57*a25f0a04SGreg Roach		}
58*a25f0a04SGreg Roach	}
59*a25f0a04SGreg Roach
60*a25f0a04SGreg Roach	/** {@inheritdoc} */
61*a25f0a04SGreg Roach	protected function canShowByType($access_level) {
62*a25f0a04SGreg Roach		// Hide notes if they are attached to private records
63*a25f0a04SGreg Roach		$linked_ids = Database::prepare(
64*a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
65*a25f0a04SGreg Roach		)->execute(array($this->xref, $this->gedcom_id))->fetchOneColumn();
66*a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
67*a25f0a04SGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id);
68*a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
69*a25f0a04SGreg Roach				return false;
70*a25f0a04SGreg Roach			}
71*a25f0a04SGreg Roach		}
72*a25f0a04SGreg Roach
73*a25f0a04SGreg Roach		// Apply default behaviour
74*a25f0a04SGreg Roach		return parent::canShowByType($access_level);
75*a25f0a04SGreg Roach	}
76*a25f0a04SGreg Roach
77*a25f0a04SGreg Roach	/** {@inheritdoc} */
78*a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
79*a25f0a04SGreg Roach		return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
80*a25f0a04SGreg Roach	}
81*a25f0a04SGreg Roach
82*a25f0a04SGreg Roach	/** {@inheritdoc} */
83*a25f0a04SGreg Roach	protected static function fetchGedcomRecord($xref, $gedcom_id) {
84*a25f0a04SGreg Roach		static $statement = null;
85*a25f0a04SGreg Roach
86*a25f0a04SGreg Roach		if ($statement === null) {
87*a25f0a04SGreg Roach			$statement = Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_id=? AND o_file=? AND o_type='NOTE'");
88*a25f0a04SGreg Roach		}
89*a25f0a04SGreg Roach
90*a25f0a04SGreg Roach		return $statement->execute(array($xref, $gedcom_id))->fetchOne();
91*a25f0a04SGreg Roach	}
92*a25f0a04SGreg Roach
93*a25f0a04SGreg Roach	/**
94*a25f0a04SGreg Roach	 * Create a name for this note - apply (and remove) markup, then take
95*a25f0a04SGreg Roach	 * a maximum of 100 characters from the first line.
96*a25f0a04SGreg Roach	 *
97*a25f0a04SGreg Roach	 * {@inheritdoc}
98*a25f0a04SGreg Roach	 */
99*a25f0a04SGreg Roach	public function extractNames() {
100*a25f0a04SGreg Roach		global $WT_TREE;
101*a25f0a04SGreg Roach
102*a25f0a04SGreg Roach		$text = $this->getNote();
103*a25f0a04SGreg Roach
104*a25f0a04SGreg Roach		if ($text) {
105*a25f0a04SGreg Roach			switch ($WT_TREE->getPreference('FORMAT_TEXT')) {
106*a25f0a04SGreg Roach			case 'markdown':
107*a25f0a04SGreg Roach				$text = Filter::markdown($text);
108*a25f0a04SGreg Roach				$text = Filter::unescapeHtml($text);
109*a25f0a04SGreg Roach				break;
110*a25f0a04SGreg Roach			}
111*a25f0a04SGreg Roach
112*a25f0a04SGreg Roach			list($text) = explode("\n", $text);
113*a25f0a04SGreg Roach			$this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
114*a25f0a04SGreg Roach		}
115*a25f0a04SGreg Roach	}
116*a25f0a04SGreg Roach}
117