1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Closure; 23use Exception; 24use Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 25use Illuminate\Database\Capsule\Manager as DB; 26use Illuminate\Support\Str; 27use stdClass; 28 29/** 30 * A GEDCOM note (NOTE) object. 31 */ 32class Note extends GedcomRecord 33{ 34 public const RECORD_TYPE = 'NOTE'; 35 36 protected const ROUTE_NAME = NotePage::class; 37 38 /** 39 * A closure which will create a record from a database row. 40 * 41 * @param Tree $tree 42 * 43 * @return Closure 44 */ 45 public static function rowMapper(Tree $tree): Closure 46 { 47 return static function (stdClass $row) use ($tree): Note { 48 $note = Note::getInstance($row->o_id, $tree, $row->o_gedcom); 49 assert($note instanceof Note); 50 51 return $note; 52 }; 53 } 54 55 /** 56 * Get an instance of a note object. For single records, 57 * we just receive the XREF. For bulk records (such as lists 58 * and search results) we can receive the GEDCOM data as well. 59 * 60 * @param string $xref 61 * @param Tree $tree 62 * @param string|null $gedcom 63 * 64 * @throws Exception 65 * 66 * @return Note|null 67 */ 68 public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Note 69 { 70 $record = parent::getInstance($xref, $tree, $gedcom); 71 72 if ($record instanceof self) { 73 return $record; 74 } 75 76 return null; 77 } 78 79 /** 80 * Get the text contents of the note 81 * 82 * @return string 83 */ 84 public function getNote(): string 85 { 86 if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 87 return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 88 } 89 90 return ''; 91 } 92 93 /** 94 * Each object type may have its own special rules, and re-implement this function. 95 * 96 * @param int $access_level 97 * 98 * @return bool 99 */ 100 protected function canShowByType(int $access_level): bool 101 { 102 // Hide notes if they are attached to private records 103 $linked_ids = DB::table('link') 104 ->where('l_file', '=', $this->tree->id()) 105 ->where('l_to', '=', $this->xref) 106 ->pluck('l_from'); 107 108 foreach ($linked_ids as $linked_id) { 109 $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 110 if ($linked_record && !$linked_record->canShow($access_level)) { 111 return false; 112 } 113 } 114 115 // Apply default behavior 116 return parent::canShowByType($access_level); 117 } 118 119 /** 120 * Generate a private version of this record 121 * 122 * @param int $access_level 123 * 124 * @return string 125 */ 126 protected function createPrivateGedcomRecord(int $access_level): string 127 { 128 return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private'); 129 } 130 131 /** 132 * Fetch data from the database 133 * 134 * @param string $xref 135 * @param int $tree_id 136 * 137 * @return string|null 138 */ 139 protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 140 { 141 return DB::table('other') 142 ->where('o_id', '=', $xref) 143 ->where('o_file', '=', $tree_id) 144 ->where('o_type', '=', self::RECORD_TYPE) 145 ->value('o_gedcom'); 146 } 147 148 /** 149 * Create a name for this note - apply (and remove) markup, then take 150 * a maximum of 100 characters from the first non-empty line. 151 * 152 * @return void 153 */ 154 public function extractNames(): void 155 { 156 $text = trim($this->getNote()); 157 158 [$text] = explode("\n", $text); 159 160 if ($text !== '') { 161 $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom()); 162 } 163 } 164} 165