1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 22886b77daSGreg Roachuse Closure; 236ccdf4f0SGreg Roachuse Exception; 24d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 252e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 269a9e551aSGreg Roachuse Illuminate\Support\Str; 27886b77daSGreg Roachuse stdClass; 289a9e551aSGreg Roach 29a25f0a04SGreg Roach/** 3076692c8bSGreg Roach * A GEDCOM note (NOTE) object. 31a25f0a04SGreg Roach */ 32c1010edaSGreg Roachclass Note extends GedcomRecord 33c1010edaSGreg Roach{ 3416d6367aSGreg Roach public const RECORD_TYPE = 'NOTE'; 3516d6367aSGreg Roach 36d7daee59SGreg Roach protected const ROUTE_NAME = NotePage::class; 37a25f0a04SGreg Roach 38a25f0a04SGreg Roach /** 39886b77daSGreg Roach * A closure which will create a record from a database row. 40886b77daSGreg Roach * 41d5ad3db0SGreg Roach * @param Tree $tree 42d5ad3db0SGreg Roach * 43886b77daSGreg Roach * @return Closure 44886b77daSGreg Roach */ 45d5ad3db0SGreg Roach public static function rowMapper(Tree $tree): Closure 46886b77daSGreg Roach { 47d5ad3db0SGreg Roach return static function (stdClass $row) use ($tree): Note { 48ffc0a61fSGreg Roach $note = Note::getInstance($row->o_id, $tree, $row->o_gedcom); 49ffc0a61fSGreg Roach assert($note instanceof Note); 50ffc0a61fSGreg Roach 51ffc0a61fSGreg Roach return $note; 52886b77daSGreg Roach }; 53886b77daSGreg Roach } 54886b77daSGreg Roach 55886b77daSGreg Roach /** 56e71ef9d2SGreg Roach * Get an instance of a note object. For single records, 57e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 58e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 59e71ef9d2SGreg Roach * 60e71ef9d2SGreg Roach * @param string $xref 61e71ef9d2SGreg Roach * @param Tree $tree 62e71ef9d2SGreg Roach * @param string|null $gedcom 63e71ef9d2SGreg Roach * 646ccdf4f0SGreg Roach * @throws Exception 65e71ef9d2SGreg Roach * 66e71ef9d2SGreg Roach * @return Note|null 67e71ef9d2SGreg Roach */ 68ffc0a61fSGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Note 69c1010edaSGreg Roach { 70e71ef9d2SGreg Roach $record = parent::getInstance($xref, $tree, $gedcom); 71e71ef9d2SGreg Roach 72e364afe4SGreg Roach if ($record instanceof self) { 73e71ef9d2SGreg Roach return $record; 74e71ef9d2SGreg Roach } 75b2ce94c6SRico Sonntag 76b2ce94c6SRico Sonntag return null; 77e71ef9d2SGreg Roach } 78e71ef9d2SGreg Roach 79e71ef9d2SGreg Roach /** 80a25f0a04SGreg Roach * Get the text contents of the note 81a25f0a04SGreg Roach * 82ff166e64SGreg Roach * @return string 83a25f0a04SGreg Roach */ 84e364afe4SGreg Roach public function getNote(): string 85c1010edaSGreg Roach { 868d0ebef0SGreg Roach if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 87a25f0a04SGreg Roach return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 88a25f0a04SGreg Roach } 89b2ce94c6SRico Sonntag 90ff166e64SGreg Roach return ''; 91a25f0a04SGreg Roach } 92a25f0a04SGreg Roach 9376692c8bSGreg Roach /** 9476692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 9576692c8bSGreg Roach * 9676692c8bSGreg Roach * @param int $access_level 9776692c8bSGreg Roach * 9876692c8bSGreg Roach * @return bool 9976692c8bSGreg Roach */ 10035584196SGreg Roach protected function canShowByType(int $access_level): bool 101c1010edaSGreg Roach { 102a25f0a04SGreg Roach // Hide notes if they are attached to private records 10325366223SGreg Roach $linked_ids = DB::table('link') 10425366223SGreg Roach ->where('l_file', '=', $this->tree->id()) 10525366223SGreg Roach ->where('l_to', '=', $this->xref) 10625366223SGreg Roach ->pluck('l_from'); 10725366223SGreg Roach 108a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 10924ec66ceSGreg Roach $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 110a25f0a04SGreg Roach if ($linked_record && !$linked_record->canShow($access_level)) { 111a25f0a04SGreg Roach return false; 112a25f0a04SGreg Roach } 113a25f0a04SGreg Roach } 114a25f0a04SGreg Roach 115b3a775f6SGreg Roach // Apply default behavior 116a25f0a04SGreg Roach return parent::canShowByType($access_level); 117a25f0a04SGreg Roach } 118a25f0a04SGreg Roach 11976692c8bSGreg Roach /** 12076692c8bSGreg Roach * Generate a private version of this record 12176692c8bSGreg Roach * 12276692c8bSGreg Roach * @param int $access_level 12376692c8bSGreg Roach * 12476692c8bSGreg Roach * @return string 12576692c8bSGreg Roach */ 1263c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 127c1010edaSGreg Roach { 128a25f0a04SGreg Roach return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private'); 129a25f0a04SGreg Roach } 130a25f0a04SGreg Roach 13176692c8bSGreg Roach /** 13276692c8bSGreg Roach * Fetch data from the database 13376692c8bSGreg Roach * 13476692c8bSGreg Roach * @param string $xref 13576692c8bSGreg Roach * @param int $tree_id 13676692c8bSGreg Roach * 137e364afe4SGreg Roach * @return string|null 13876692c8bSGreg Roach */ 139e364afe4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 140c1010edaSGreg Roach { 1412e5b4452SGreg Roach return DB::table('other') 1422e5b4452SGreg Roach ->where('o_id', '=', $xref) 1432e5b4452SGreg Roach ->where('o_file', '=', $tree_id) 1442e5b4452SGreg Roach ->where('o_type', '=', self::RECORD_TYPE) 1452e5b4452SGreg Roach ->value('o_gedcom'); 146a25f0a04SGreg Roach } 147a25f0a04SGreg Roach 148a25f0a04SGreg Roach /** 149a25f0a04SGreg Roach * Create a name for this note - apply (and remove) markup, then take 150*fbc72f88SGreg Roach * a maximum of 100 characters from the first non-empty line. 151c7ff4153SGreg Roach * 152c7ff4153SGreg Roach * @return void 153a25f0a04SGreg Roach */ 154e364afe4SGreg Roach public function extractNames(): void 155c1010edaSGreg Roach { 156*fbc72f88SGreg Roach $text = trim($this->getNote()); 157a25f0a04SGreg Roach 15865e02381SGreg Roach [$text] = explode("\n", $text); 159*fbc72f88SGreg Roach 160*fbc72f88SGreg Roach if ($text !== '') { 1619a9e551aSGreg Roach $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom()); 162a25f0a04SGreg Roach } 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach} 165