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