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