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 */ 17*fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 22886b77daSGreg Roachuse Closure; 236ccdf4f0SGreg Roachuse Exception; 242e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 259a9e551aSGreg Roachuse Illuminate\Support\Str; 26886b77daSGreg Roachuse stdClass; 279a9e551aSGreg Roach 28a25f0a04SGreg Roach/** 2976692c8bSGreg Roach * A GEDCOM note (NOTE) object. 30a25f0a04SGreg Roach */ 31c1010edaSGreg Roachclass Note extends GedcomRecord 32c1010edaSGreg Roach{ 3316d6367aSGreg Roach public const RECORD_TYPE = 'NOTE'; 3416d6367aSGreg Roach 3516d6367aSGreg Roach protected const ROUTE_NAME = 'note'; 36a25f0a04SGreg Roach 37a25f0a04SGreg Roach /** 38886b77daSGreg Roach * A closure which will create a record from a database row. 39886b77daSGreg Roach * 40886b77daSGreg Roach * @return Closure 41886b77daSGreg Roach */ 42c0804649SGreg Roach public static function rowMapper(): Closure 43886b77daSGreg Roach { 446c2179e2SGreg Roach return static function (stdClass $row): Note { 45a7a24840SGreg Roach return Note::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom); 46886b77daSGreg Roach }; 47886b77daSGreg Roach } 48886b77daSGreg Roach 49886b77daSGreg Roach /** 50e71ef9d2SGreg Roach * Get an instance of a note object. For single records, 51e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 52e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 53e71ef9d2SGreg Roach * 54e71ef9d2SGreg Roach * @param string $xref 55e71ef9d2SGreg Roach * @param Tree $tree 56e71ef9d2SGreg Roach * @param string|null $gedcom 57e71ef9d2SGreg Roach * 586ccdf4f0SGreg Roach * @throws Exception 59e71ef9d2SGreg Roach * 60e71ef9d2SGreg Roach * @return Note|null 61e71ef9d2SGreg Roach */ 62e364afe4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self 63c1010edaSGreg Roach { 64e71ef9d2SGreg Roach $record = parent::getInstance($xref, $tree, $gedcom); 65e71ef9d2SGreg Roach 66e364afe4SGreg Roach if ($record instanceof self) { 67e71ef9d2SGreg Roach return $record; 68e71ef9d2SGreg Roach } 69b2ce94c6SRico Sonntag 70b2ce94c6SRico Sonntag return null; 71e71ef9d2SGreg Roach } 72e71ef9d2SGreg Roach 73e71ef9d2SGreg Roach /** 74a25f0a04SGreg Roach * Get the text contents of the note 75a25f0a04SGreg Roach * 76ff166e64SGreg Roach * @return string 77a25f0a04SGreg Roach */ 78e364afe4SGreg Roach public function getNote(): string 79c1010edaSGreg Roach { 808d0ebef0SGreg Roach if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 81a25f0a04SGreg Roach return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 82a25f0a04SGreg Roach } 83b2ce94c6SRico Sonntag 84ff166e64SGreg Roach return ''; 85a25f0a04SGreg Roach } 86a25f0a04SGreg Roach 8776692c8bSGreg Roach /** 8876692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 8976692c8bSGreg Roach * 9076692c8bSGreg Roach * @param int $access_level 9176692c8bSGreg Roach * 9276692c8bSGreg Roach * @return bool 9376692c8bSGreg Roach */ 9435584196SGreg Roach protected function canShowByType(int $access_level): bool 95c1010edaSGreg Roach { 96a25f0a04SGreg Roach // Hide notes if they are attached to private records 9725366223SGreg Roach $linked_ids = DB::table('link') 9825366223SGreg Roach ->where('l_file', '=', $this->tree->id()) 9925366223SGreg Roach ->where('l_to', '=', $this->xref) 10025366223SGreg Roach ->pluck('l_from'); 10125366223SGreg Roach 102a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 10324ec66ceSGreg Roach $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 104a25f0a04SGreg Roach if ($linked_record && !$linked_record->canShow($access_level)) { 105a25f0a04SGreg Roach return false; 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach } 108a25f0a04SGreg Roach 109a25f0a04SGreg Roach // Apply default behaviour 110a25f0a04SGreg Roach return parent::canShowByType($access_level); 111a25f0a04SGreg Roach } 112a25f0a04SGreg Roach 11376692c8bSGreg Roach /** 11476692c8bSGreg Roach * Generate a private version of this record 11576692c8bSGreg Roach * 11676692c8bSGreg Roach * @param int $access_level 11776692c8bSGreg Roach * 11876692c8bSGreg Roach * @return string 11976692c8bSGreg Roach */ 1203c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 121c1010edaSGreg Roach { 122a25f0a04SGreg Roach return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private'); 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach 12576692c8bSGreg Roach /** 12676692c8bSGreg Roach * Fetch data from the database 12776692c8bSGreg Roach * 12876692c8bSGreg Roach * @param string $xref 12976692c8bSGreg Roach * @param int $tree_id 13076692c8bSGreg Roach * 131e364afe4SGreg Roach * @return string|null 13276692c8bSGreg Roach */ 133e364afe4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 134c1010edaSGreg Roach { 1352e5b4452SGreg Roach return DB::table('other') 1362e5b4452SGreg Roach ->where('o_id', '=', $xref) 1372e5b4452SGreg Roach ->where('o_file', '=', $tree_id) 1382e5b4452SGreg Roach ->where('o_type', '=', self::RECORD_TYPE) 1392e5b4452SGreg Roach ->value('o_gedcom'); 140a25f0a04SGreg Roach } 141a25f0a04SGreg Roach 142a25f0a04SGreg Roach /** 143a25f0a04SGreg Roach * Create a name for this note - apply (and remove) markup, then take 144a25f0a04SGreg Roach * a maximum of 100 characters from the first line. 145c7ff4153SGreg Roach * 146c7ff4153SGreg Roach * @return void 147a25f0a04SGreg Roach */ 148e364afe4SGreg Roach public function extractNames(): void 149c1010edaSGreg Roach { 150a25f0a04SGreg Roach $text = $this->getNote(); 151a25f0a04SGreg Roach 152a25f0a04SGreg Roach if ($text) { 15365e02381SGreg Roach [$text] = explode("\n", $text); 1549a9e551aSGreg Roach $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom()); 155a25f0a04SGreg Roach } 156a25f0a04SGreg Roach } 157a25f0a04SGreg Roach} 158