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 // Take the first line 101 $html = strtr($html, [ 102 '</blockquote>' => MarkdownFactory::BREAK, 103 '</h1>' => MarkdownFactory::BREAK, 104 '</h2>' => MarkdownFactory::BREAK, 105 '</h3>' => MarkdownFactory::BREAK, 106 '</h4>' => MarkdownFactory::BREAK, 107 '</h5>' => MarkdownFactory::BREAK, 108 '</h6>' => MarkdownFactory::BREAK, 109 '</li>' => MarkdownFactory::BREAK, 110 '</p>' => MarkdownFactory::BREAK, 111 '</pre>' => MarkdownFactory::BREAK, 112 '</td>' => ' ', 113 '</th>' => ' ', 114 '<hr>' => MarkdownFactory::BREAK, 115 ]); 116 117 [$first_line] = explode(MarkdownFactory::BREAK, $html, 2); 118 119 $first_line = strip_tags($first_line, ['br']); 120 $first_line = htmlspecialchars_decode($first_line, ENT_QUOTES); 121 122 if ($first_line !== '') { 123 $this->addName('NOTE', Str::limit($first_line, 100, I18N::translate('…')), $this->gedcom()); 124 } 125 } 126 127 /** 128 * Notes are converted to HTML for display. We want the first line 129 * 130 * @param string $html 131 * 132 * @return string 133 */ 134 public static function firstLineOfTextFromHtml(string $html): string 135 { 136 $html = strtr($html, [ 137 '</blockquote>' => MarkdownFactory::BREAK, 138 '</h1>' => MarkdownFactory::BREAK, 139 '</h2>' => MarkdownFactory::BREAK, 140 '</h3>' => MarkdownFactory::BREAK, 141 '</h4>' => MarkdownFactory::BREAK, 142 '</h5>' => MarkdownFactory::BREAK, 143 '</h6>' => MarkdownFactory::BREAK, 144 '</li>' => MarkdownFactory::BREAK, 145 '</p>' => MarkdownFactory::BREAK, 146 '</pre>' => MarkdownFactory::BREAK, 147 '</td>' => ' ', 148 '</th>' => ' ', 149 '<hr>' => MarkdownFactory::BREAK, 150 ]); 151 152 $html = strip_tags($html, ['br']); 153 154 [$first] = explode(MarkdownFactory::BREAK, $html, 2); 155 156 return htmlspecialchars_decode($first, ENT_QUOTES); 157 } 158} 159