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; 32 33use function var_dump; 34 35use const ENT_QUOTES; 36 37/** 38 * A GEDCOM note (NOTE) object. 39 */ 40class Note extends GedcomRecord 41{ 42 public const RECORD_TYPE = 'NOTE'; 43 44 protected const ROUTE_NAME = NotePage::class; 45 46 /** 47 * Get the text contents of the note 48 * 49 * @return string 50 */ 51 public function getNote(): string 52 { 53 if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 54 return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 55 } 56 57 return ''; 58 } 59 60 /** 61 * Each object type may have its own special rules, and re-implement this function. 62 * 63 * @param int $access_level 64 * 65 * @return bool 66 */ 67 protected function canShowByType(int $access_level): bool 68 { 69 // Hide notes if they are attached to private records 70 $linked_ids = DB::table('link') 71 ->where('l_file', '=', $this->tree->id()) 72 ->where('l_to', '=', $this->xref) 73 ->pluck('l_from'); 74 75 foreach ($linked_ids as $linked_id) { 76 $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree); 77 if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) { 78 return false; 79 } 80 } 81 82 // Apply default behavior 83 return parent::canShowByType($access_level); 84 } 85 86 /** 87 * Create a name for this note - apply (and remove) markup, then take 88 * a maximum of 100 characters from the first non-empty line. 89 * 90 * @return void 91 */ 92 public function extractNames(): void 93 { 94 if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') { 95 $html = Registry::markdownFactory()->markdown($this->getNote()); 96 } else { 97 $html = Registry::markdownFactory()->autolink($this->getNote()); 98 } 99 100 $first_line = self::firstLineOfTextFromHtml($html); 101 102 if ($first_line !== '') { 103 $this->addName('NOTE', Str::limit($first_line, 100, I18N::translate('…')), $this->gedcom()); 104 } 105 } 106 107 /** 108 * Notes are converted to HTML for display. We want the first line 109 * 110 * @param string $html 111 * 112 * @return string 113 */ 114 public static function firstLineOfTextFromHtml(string $html): string 115 { 116 $html = strtr($html, [ 117 '</blockquote>' => MarkdownFactory::BREAK, 118 '</h1>' => MarkdownFactory::BREAK, 119 '</h2>' => MarkdownFactory::BREAK, 120 '</h3>' => MarkdownFactory::BREAK, 121 '</h4>' => MarkdownFactory::BREAK, 122 '</h5>' => MarkdownFactory::BREAK, 123 '</h6>' => MarkdownFactory::BREAK, 124 '</li>' => MarkdownFactory::BREAK, 125 '</p>' => MarkdownFactory::BREAK, 126 '</pre>' => MarkdownFactory::BREAK, 127 '</td>' => ' ', 128 '</th>' => ' ', 129 '<hr>' => MarkdownFactory::BREAK, 130 ]); 131 132 $html = strip_tags($html, ['br']); 133 134 [$first] = explode(MarkdownFactory::BREAK, $html, 2); 135 136 return htmlspecialchars_decode($first, ENT_QUOTES); 137 } 138} 139