1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Fisharebest\Webtrees\Factories\MarkdownFactory; 23use Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 24use Illuminate\Database\Capsule\Manager as DB; 25use Illuminate\Support\Str; 26 27use function explode; 28use function htmlspecialchars_decode; 29use function preg_match; 30use function preg_replace; 31use function strip_tags; 32use function trim; 33 34use function var_dump; 35 36use const ENT_QUOTES; 37 38/** 39 * A GEDCOM note (NOTE) object. 40 */ 41class Note extends GedcomRecord 42{ 43 public const RECORD_TYPE = 'NOTE'; 44 45 protected const ROUTE_NAME = NotePage::class; 46 47 /** 48 * Get the text contents of the note 49 * 50 * @return string 51 */ 52 public function getNote(): string 53 { 54 if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 55 return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 56 } 57 58 return ''; 59 } 60 61 /** 62 * Each object type may have its own special rules, and re-implement this function. 63 * 64 * @param int $access_level 65 * 66 * @return bool 67 */ 68 protected function canShowByType(int $access_level): bool 69 { 70 // Hide notes if they are attached to private records 71 $linked_ids = DB::table('link') 72 ->where('l_file', '=', $this->tree->id()) 73 ->where('l_to', '=', $this->xref) 74 ->pluck('l_from'); 75 76 foreach ($linked_ids as $linked_id) { 77 $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree); 78 if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) { 79 return false; 80 } 81 } 82 83 // Apply default behavior 84 return parent::canShowByType($access_level); 85 } 86 87 /** 88 * Create a name for this note - apply (and remove) markup, then take 89 * a maximum of 100 characters from the first non-empty line. 90 * 91 * @return void 92 */ 93 public function extractNames(): void 94 { 95 if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') { 96 $html = Registry::markdownFactory()->markdown($this->getNote()); 97 } else { 98 $html = Registry::markdownFactory()->autolink($this->getNote()); 99 } 100 101 // Take the first line 102 $html = strtr($html, ["</p>\n<p>" => MarkdownFactory::BREAK . MarkdownFactory::BREAK]); 103 [$html] = explode(MarkdownFactory::BREAK, strip_tags(trim($html), ['br'])); 104 105 if ($html !== '') { 106 $html = htmlspecialchars_decode($html, ENT_QUOTES); 107 $this->addName('NOTE', Str::limit($html, 100, I18N::translate('…')), $this->gedcom()); 108 } 109 } 110} 111