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; 23d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 242e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 259a9e551aSGreg Roachuse Illuminate\Support\Str; 269a9e551aSGreg Roach 27a25f0a04SGreg Roach/** 2876692c8bSGreg Roach * A GEDCOM note (NOTE) object. 29a25f0a04SGreg Roach */ 30c1010edaSGreg Roachclass Note extends GedcomRecord 31c1010edaSGreg Roach{ 3216d6367aSGreg Roach public const RECORD_TYPE = 'NOTE'; 3316d6367aSGreg Roach 34d7daee59SGreg Roach protected const ROUTE_NAME = NotePage::class; 35a25f0a04SGreg Roach 36a25f0a04SGreg Roach /** 37886b77daSGreg Roach * A closure which will create a record from a database row. 38886b77daSGreg Roach * 39*bb03c9f0SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 - Use Factory::note() 40*bb03c9f0SGreg 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 { 47*bb03c9f0SGreg Roach return Factory::note()->mapper($tree); 48886b77daSGreg Roach } 49886b77daSGreg Roach 50886b77daSGreg Roach /** 51e71ef9d2SGreg Roach * Get an instance of a note object. For single records, 52e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 53e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 54e71ef9d2SGreg Roach * 55*bb03c9f0SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 - Use Factory::note() 56*bb03c9f0SGreg Roach * 57e71ef9d2SGreg Roach * @param string $xref 58e71ef9d2SGreg Roach * @param Tree $tree 59e71ef9d2SGreg Roach * @param string|null $gedcom 60e71ef9d2SGreg Roach * 61e71ef9d2SGreg Roach * @return Note|null 62e71ef9d2SGreg Roach */ 63ffc0a61fSGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Note 64c1010edaSGreg Roach { 65*bb03c9f0SGreg Roach return Factory::note()->make($xref, $tree, $gedcom); 66e71ef9d2SGreg Roach } 67e71ef9d2SGreg Roach 68e71ef9d2SGreg Roach /** 69a25f0a04SGreg Roach * Get the text contents of the note 70a25f0a04SGreg Roach * 71ff166e64SGreg Roach * @return string 72a25f0a04SGreg Roach */ 73e364afe4SGreg Roach public function getNote(): string 74c1010edaSGreg Roach { 758d0ebef0SGreg Roach if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 76a25f0a04SGreg Roach return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 77a25f0a04SGreg Roach } 78b2ce94c6SRico Sonntag 79ff166e64SGreg Roach return ''; 80a25f0a04SGreg Roach } 81a25f0a04SGreg Roach 8276692c8bSGreg Roach /** 8376692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 8476692c8bSGreg Roach * 8576692c8bSGreg Roach * @param int $access_level 8676692c8bSGreg Roach * 8776692c8bSGreg Roach * @return bool 8876692c8bSGreg Roach */ 8935584196SGreg Roach protected function canShowByType(int $access_level): bool 90c1010edaSGreg Roach { 91a25f0a04SGreg Roach // Hide notes if they are attached to private records 9225366223SGreg Roach $linked_ids = DB::table('link') 9325366223SGreg Roach ->where('l_file', '=', $this->tree->id()) 9425366223SGreg Roach ->where('l_to', '=', $this->xref) 9525366223SGreg Roach ->pluck('l_from'); 9625366223SGreg Roach 97a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 98*bb03c9f0SGreg Roach $linked_record = Factory::gedcomRecord()->make($linked_id, $this->tree); 99*bb03c9f0SGreg Roach if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) { 100a25f0a04SGreg Roach return false; 101a25f0a04SGreg Roach } 102a25f0a04SGreg Roach } 103a25f0a04SGreg Roach 104b3a775f6SGreg Roach // Apply default behavior 105a25f0a04SGreg Roach return parent::canShowByType($access_level); 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach 10876692c8bSGreg Roach /** 10976692c8bSGreg Roach * Generate a private version of this record 11076692c8bSGreg Roach * 11176692c8bSGreg Roach * @param int $access_level 11276692c8bSGreg Roach * 11376692c8bSGreg Roach * @return string 11476692c8bSGreg Roach */ 1153c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 116c1010edaSGreg Roach { 117a25f0a04SGreg Roach return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private'); 118a25f0a04SGreg Roach } 119a25f0a04SGreg Roach 12076692c8bSGreg Roach /** 121a25f0a04SGreg Roach * Create a name for this note - apply (and remove) markup, then take 122fbc72f88SGreg Roach * a maximum of 100 characters from the first non-empty line. 123c7ff4153SGreg Roach * 124c7ff4153SGreg Roach * @return void 125a25f0a04SGreg Roach */ 126e364afe4SGreg Roach public function extractNames(): void 127c1010edaSGreg Roach { 128fbc72f88SGreg Roach $text = trim($this->getNote()); 129a25f0a04SGreg Roach 13065e02381SGreg Roach [$text] = explode("\n", $text); 131fbc72f88SGreg Roach 132fbc72f88SGreg Roach if ($text !== '') { 1339a9e551aSGreg Roach $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom()); 134a25f0a04SGreg Roach } 135a25f0a04SGreg Roach } 136a25f0a04SGreg Roach} 137