1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 2228065790SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory; 23d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 242e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 259a9e551aSGreg Roachuse Illuminate\Support\Str; 269a9e551aSGreg Roach 27283375f9SGreg Roachuse function explode; 28283375f9SGreg Roachuse function htmlspecialchars_decode; 29283375f9SGreg Roachuse function preg_match; 30283375f9SGreg Roachuse function preg_replace; 31283375f9SGreg Roachuse function strip_tags; 32*63a2c22bSGreg Roach 33*63a2c22bSGreg Roachuse function var_dump; 34283375f9SGreg Roach 35283375f9SGreg Roachuse const ENT_QUOTES; 36283375f9SGreg Roach 37a25f0a04SGreg Roach/** 3876692c8bSGreg Roach * A GEDCOM note (NOTE) object. 39a25f0a04SGreg Roach */ 40c1010edaSGreg Roachclass Note extends GedcomRecord 41c1010edaSGreg Roach{ 4216d6367aSGreg Roach public const RECORD_TYPE = 'NOTE'; 4316d6367aSGreg Roach 44d7daee59SGreg Roach protected const ROUTE_NAME = NotePage::class; 45a25f0a04SGreg Roach 46a25f0a04SGreg Roach /** 47a25f0a04SGreg Roach * Get the text contents of the note 48a25f0a04SGreg Roach * 49ff166e64SGreg Roach * @return string 50a25f0a04SGreg Roach */ 51e364afe4SGreg Roach public function getNote(): string 52c1010edaSGreg Roach { 538d0ebef0SGreg Roach if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 54a25f0a04SGreg Roach return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 55a25f0a04SGreg Roach } 56b2ce94c6SRico Sonntag 57ff166e64SGreg Roach return ''; 58a25f0a04SGreg Roach } 59a25f0a04SGreg Roach 6076692c8bSGreg Roach /** 6176692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 6276692c8bSGreg Roach * 6376692c8bSGreg Roach * @param int $access_level 6476692c8bSGreg Roach * 6576692c8bSGreg Roach * @return bool 6676692c8bSGreg Roach */ 6735584196SGreg Roach protected function canShowByType(int $access_level): bool 68c1010edaSGreg Roach { 69a25f0a04SGreg Roach // Hide notes if they are attached to private records 7025366223SGreg Roach $linked_ids = DB::table('link') 7125366223SGreg Roach ->where('l_file', '=', $this->tree->id()) 7225366223SGreg Roach ->where('l_to', '=', $this->xref) 7325366223SGreg Roach ->pluck('l_from'); 7425366223SGreg Roach 75a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 766b9cb339SGreg Roach $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree); 77bb03c9f0SGreg Roach if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) { 78a25f0a04SGreg Roach return false; 79a25f0a04SGreg Roach } 80a25f0a04SGreg Roach } 81a25f0a04SGreg Roach 82b3a775f6SGreg Roach // Apply default behavior 83a25f0a04SGreg Roach return parent::canShowByType($access_level); 84a25f0a04SGreg Roach } 85a25f0a04SGreg Roach 8676692c8bSGreg Roach /** 87a25f0a04SGreg Roach * Create a name for this note - apply (and remove) markup, then take 88fbc72f88SGreg Roach * a maximum of 100 characters from the first non-empty line. 89c7ff4153SGreg Roach * 90c7ff4153SGreg Roach * @return void 91a25f0a04SGreg Roach */ 92e364afe4SGreg Roach public function extractNames(): void 93c1010edaSGreg Roach { 944d35caa7SGreg Roach if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') { 95c1d58f74SGreg Roach $html = Registry::markdownFactory()->markdown($this->getNote()); 964d35caa7SGreg Roach } else { 97c1d58f74SGreg Roach $html = Registry::markdownFactory()->autolink($this->getNote()); 984d35caa7SGreg Roach } 99a25f0a04SGreg Roach 1004d35caa7SGreg Roach // Take the first line 101*63a2c22bSGreg Roach $html = strtr($html, [ 102*63a2c22bSGreg Roach '</blockquote>' => MarkdownFactory::BREAK, 103*63a2c22bSGreg Roach '</h1>' => MarkdownFactory::BREAK, 104*63a2c22bSGreg Roach '</h2>' => MarkdownFactory::BREAK, 105*63a2c22bSGreg Roach '</h3>' => MarkdownFactory::BREAK, 106*63a2c22bSGreg Roach '</h4>' => MarkdownFactory::BREAK, 107*63a2c22bSGreg Roach '</h5>' => MarkdownFactory::BREAK, 108*63a2c22bSGreg Roach '</h6>' => MarkdownFactory::BREAK, 109*63a2c22bSGreg Roach '</li>' => MarkdownFactory::BREAK, 110*63a2c22bSGreg Roach '</p>' => MarkdownFactory::BREAK, 111*63a2c22bSGreg Roach '</pre>' => MarkdownFactory::BREAK, 112*63a2c22bSGreg Roach '</td>' => ' ', 113*63a2c22bSGreg Roach '</th>' => ' ', 114*63a2c22bSGreg Roach '<hr>' => MarkdownFactory::BREAK, 115*63a2c22bSGreg Roach ]); 116fbc72f88SGreg Roach 117*63a2c22bSGreg Roach [$first_line] = explode(MarkdownFactory::BREAK, $html, 2); 118*63a2c22bSGreg Roach 119*63a2c22bSGreg Roach $first_line = strip_tags($first_line, ['br']); 120*63a2c22bSGreg Roach $first_line = htmlspecialchars_decode($first_line, ENT_QUOTES); 121*63a2c22bSGreg Roach 122*63a2c22bSGreg Roach if ($first_line !== '') { 123*63a2c22bSGreg Roach $this->addName('NOTE', Str::limit($first_line, 100, I18N::translate('…')), $this->gedcom()); 124a25f0a04SGreg Roach } 125a25f0a04SGreg Roach } 126*63a2c22bSGreg Roach 127*63a2c22bSGreg Roach /** 128*63a2c22bSGreg Roach * Notes are converted to HTML for display. We want the first line 129*63a2c22bSGreg Roach * 130*63a2c22bSGreg Roach * @param string $html 131*63a2c22bSGreg Roach * 132*63a2c22bSGreg Roach * @return string 133*63a2c22bSGreg Roach */ 134*63a2c22bSGreg Roach public static function firstLineOfTextFromHtml(string $html): string 135*63a2c22bSGreg Roach { 136*63a2c22bSGreg Roach $html = strtr($html, [ 137*63a2c22bSGreg Roach '</blockquote>' => MarkdownFactory::BREAK, 138*63a2c22bSGreg Roach '</h1>' => MarkdownFactory::BREAK, 139*63a2c22bSGreg Roach '</h2>' => MarkdownFactory::BREAK, 140*63a2c22bSGreg Roach '</h3>' => MarkdownFactory::BREAK, 141*63a2c22bSGreg Roach '</h4>' => MarkdownFactory::BREAK, 142*63a2c22bSGreg Roach '</h5>' => MarkdownFactory::BREAK, 143*63a2c22bSGreg Roach '</h6>' => MarkdownFactory::BREAK, 144*63a2c22bSGreg Roach '</li>' => MarkdownFactory::BREAK, 145*63a2c22bSGreg Roach '</p>' => MarkdownFactory::BREAK, 146*63a2c22bSGreg Roach '</pre>' => MarkdownFactory::BREAK, 147*63a2c22bSGreg Roach '</td>' => ' ', 148*63a2c22bSGreg Roach '</th>' => ' ', 149*63a2c22bSGreg Roach '<hr>' => MarkdownFactory::BREAK, 150*63a2c22bSGreg Roach ]); 151*63a2c22bSGreg Roach 152*63a2c22bSGreg Roach $html = strip_tags($html, ['br']); 153*63a2c22bSGreg Roach 154*63a2c22bSGreg Roach [$first] = explode(MarkdownFactory::BREAK, $html, 2); 155*63a2c22bSGreg Roach 156*63a2c22bSGreg Roach return htmlspecialchars_decode($first, ENT_QUOTES); 157*63a2c22bSGreg Roach } 158a25f0a04SGreg Roach} 159