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; 32283375f9SGreg Roachuse function trim; 33283375f9SGreg Roach 34*c1d58f74SGreg Roachuse function var_dump; 35*c1d58f74SGreg Roach 36283375f9SGreg Roachuse const ENT_QUOTES; 37283375f9SGreg Roach 38a25f0a04SGreg Roach/** 3976692c8bSGreg Roach * A GEDCOM note (NOTE) object. 40a25f0a04SGreg Roach */ 41c1010edaSGreg Roachclass Note extends GedcomRecord 42c1010edaSGreg Roach{ 4316d6367aSGreg Roach public const RECORD_TYPE = 'NOTE'; 4416d6367aSGreg Roach 45d7daee59SGreg Roach protected const ROUTE_NAME = NotePage::class; 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** 48a25f0a04SGreg Roach * Get the text contents of the note 49a25f0a04SGreg Roach * 50ff166e64SGreg Roach * @return string 51a25f0a04SGreg Roach */ 52e364afe4SGreg Roach public function getNote(): string 53c1010edaSGreg Roach { 548d0ebef0SGreg Roach if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 55a25f0a04SGreg Roach return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 56a25f0a04SGreg Roach } 57b2ce94c6SRico Sonntag 58ff166e64SGreg Roach return ''; 59a25f0a04SGreg Roach } 60a25f0a04SGreg Roach 6176692c8bSGreg Roach /** 6276692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 6376692c8bSGreg Roach * 6476692c8bSGreg Roach * @param int $access_level 6576692c8bSGreg Roach * 6676692c8bSGreg Roach * @return bool 6776692c8bSGreg Roach */ 6835584196SGreg Roach protected function canShowByType(int $access_level): bool 69c1010edaSGreg Roach { 70a25f0a04SGreg Roach // Hide notes if they are attached to private records 7125366223SGreg Roach $linked_ids = DB::table('link') 7225366223SGreg Roach ->where('l_file', '=', $this->tree->id()) 7325366223SGreg Roach ->where('l_to', '=', $this->xref) 7425366223SGreg Roach ->pluck('l_from'); 7525366223SGreg Roach 76a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 776b9cb339SGreg Roach $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree); 78bb03c9f0SGreg Roach if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) { 79a25f0a04SGreg Roach return false; 80a25f0a04SGreg Roach } 81a25f0a04SGreg Roach } 82a25f0a04SGreg Roach 83b3a775f6SGreg Roach // Apply default behavior 84a25f0a04SGreg Roach return parent::canShowByType($access_level); 85a25f0a04SGreg Roach } 86a25f0a04SGreg Roach 8776692c8bSGreg Roach /** 88a25f0a04SGreg Roach * Create a name for this note - apply (and remove) markup, then take 89fbc72f88SGreg Roach * a maximum of 100 characters from the first non-empty line. 90c7ff4153SGreg Roach * 91c7ff4153SGreg Roach * @return void 92a25f0a04SGreg Roach */ 93e364afe4SGreg Roach public function extractNames(): void 94c1010edaSGreg Roach { 954d35caa7SGreg Roach if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') { 96*c1d58f74SGreg Roach $html = Registry::markdownFactory()->markdown($this->getNote()); 974d35caa7SGreg Roach } else { 98*c1d58f74SGreg Roach $html = Registry::markdownFactory()->autolink($this->getNote()); 994d35caa7SGreg Roach } 100a25f0a04SGreg Roach 1014d35caa7SGreg Roach // Take the first line 102*c1d58f74SGreg Roach $html = strtr($html, ["</p>\n<p>" => MarkdownFactory::BREAK . MarkdownFactory::BREAK]); 103*c1d58f74SGreg Roach [$html] = explode(MarkdownFactory::BREAK, strip_tags(trim($html), ['br'])); 104fbc72f88SGreg Roach 105*c1d58f74SGreg Roach if ($html !== '') { 106*c1d58f74SGreg Roach $html = htmlspecialchars_decode($html, ENT_QUOTES); 107*c1d58f74SGreg Roach $this->addName('NOTE', Str::limit($html, 100, I18N::translate('…')), $this->gedcom()); 108a25f0a04SGreg Roach } 109a25f0a04SGreg Roach } 110a25f0a04SGreg Roach} 111