1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 19a25f0a04SGreg Roach 202e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 219a9e551aSGreg Roachuse Illuminate\Support\Str; 229a9e551aSGreg Roach 23a25f0a04SGreg Roach/** 2476692c8bSGreg Roach * A GEDCOM note (NOTE) object. 25a25f0a04SGreg Roach */ 26c1010edaSGreg Roachclass Note extends GedcomRecord 27c1010edaSGreg Roach{ 28*16d6367aSGreg Roach public const RECORD_TYPE = 'NOTE'; 29*16d6367aSGreg Roach 30*16d6367aSGreg Roach protected const ROUTE_NAME = 'note'; 31a25f0a04SGreg Roach 32a25f0a04SGreg Roach /** 33e71ef9d2SGreg Roach * Get an instance of a note object. For single records, 34e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 35e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 36e71ef9d2SGreg Roach * 37e71ef9d2SGreg Roach * @param string $xref 38e71ef9d2SGreg Roach * @param Tree $tree 39e71ef9d2SGreg Roach * @param string|null $gedcom 40e71ef9d2SGreg Roach * 41e71ef9d2SGreg Roach * @throws \Exception 42e71ef9d2SGreg Roach * 43e71ef9d2SGreg Roach * @return Note|null 44e71ef9d2SGreg Roach */ 4576f666f4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 46c1010edaSGreg Roach { 47e71ef9d2SGreg Roach $record = parent::getInstance($xref, $tree, $gedcom); 48e71ef9d2SGreg Roach 49e71ef9d2SGreg Roach if ($record instanceof Note) { 50e71ef9d2SGreg Roach return $record; 51e71ef9d2SGreg Roach } 52b2ce94c6SRico Sonntag 53b2ce94c6SRico Sonntag return null; 54e71ef9d2SGreg Roach } 55e71ef9d2SGreg Roach 56e71ef9d2SGreg Roach /** 57a25f0a04SGreg Roach * Get the text contents of the note 58a25f0a04SGreg Roach * 59ff166e64SGreg Roach * @return string 60a25f0a04SGreg Roach */ 61c1010edaSGreg Roach public function getNote() 62c1010edaSGreg Roach { 638d0ebef0SGreg Roach if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 64a25f0a04SGreg Roach return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 65a25f0a04SGreg Roach } 66b2ce94c6SRico Sonntag 67ff166e64SGreg Roach return ''; 68a25f0a04SGreg Roach } 69a25f0a04SGreg Roach 7076692c8bSGreg Roach /** 7176692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 7276692c8bSGreg Roach * 7376692c8bSGreg Roach * @param int $access_level 7476692c8bSGreg Roach * 7576692c8bSGreg Roach * @return bool 7676692c8bSGreg Roach */ 7735584196SGreg Roach protected function canShowByType(int $access_level): bool 78c1010edaSGreg Roach { 79a25f0a04SGreg Roach // Hide notes if they are attached to private records 80a25f0a04SGreg Roach $linked_ids = Database::prepare( 81a25f0a04SGreg Roach "SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?" 8213abd6f3SGreg Roach )->execute([ 83c1010edaSGreg Roach $this->xref, 8472cf66d4SGreg Roach $this->tree->id(), 8513abd6f3SGreg Roach ])->fetchOneColumn(); 86a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 8724ec66ceSGreg Roach $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 88a25f0a04SGreg Roach if ($linked_record && !$linked_record->canShow($access_level)) { 89a25f0a04SGreg Roach return false; 90a25f0a04SGreg Roach } 91a25f0a04SGreg Roach } 92a25f0a04SGreg Roach 93a25f0a04SGreg Roach // Apply default behaviour 94a25f0a04SGreg Roach return parent::canShowByType($access_level); 95a25f0a04SGreg Roach } 96a25f0a04SGreg Roach 9776692c8bSGreg Roach /** 9876692c8bSGreg Roach * Generate a private version of this record 9976692c8bSGreg Roach * 10076692c8bSGreg Roach * @param int $access_level 10176692c8bSGreg Roach * 10276692c8bSGreg Roach * @return string 10376692c8bSGreg Roach */ 1043c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 105c1010edaSGreg Roach { 106a25f0a04SGreg Roach return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private'); 107a25f0a04SGreg Roach } 108a25f0a04SGreg Roach 10976692c8bSGreg Roach /** 11076692c8bSGreg Roach * Fetch data from the database 11176692c8bSGreg Roach * 11276692c8bSGreg Roach * @param string $xref 11376692c8bSGreg Roach * @param int $tree_id 11476692c8bSGreg Roach * 11576692c8bSGreg Roach * @return null|string 11676692c8bSGreg Roach */ 11776f666f4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id) 118c1010edaSGreg Roach { 1192e5b4452SGreg Roach return DB::table('other') 1202e5b4452SGreg Roach ->where('o_id', '=', $xref) 1212e5b4452SGreg Roach ->where('o_file', '=', $tree_id) 1222e5b4452SGreg Roach ->where('o_type', '=', self::RECORD_TYPE) 1232e5b4452SGreg Roach ->value('o_gedcom'); 124a25f0a04SGreg Roach } 125a25f0a04SGreg Roach 126a25f0a04SGreg Roach /** 127a25f0a04SGreg Roach * Create a name for this note - apply (and remove) markup, then take 128a25f0a04SGreg Roach * a maximum of 100 characters from the first line. 129c7ff4153SGreg Roach * 130c7ff4153SGreg Roach * @return void 131a25f0a04SGreg Roach */ 132c1010edaSGreg Roach public function extractNames() 133c1010edaSGreg Roach { 134a25f0a04SGreg Roach $text = $this->getNote(); 135a25f0a04SGreg Roach 136a25f0a04SGreg Roach if ($text) { 137f4afa648SGreg Roach switch ($this->tree()->getPreference('FORMAT_TEXT')) { 138a25f0a04SGreg Roach case 'markdown': 139f4afa648SGreg Roach $text = Filter::markdown($text, $this->tree()); 1409524b7b5SGreg Roach $text = strip_tags($text); 1419524b7b5SGreg Roach $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); 142a25f0a04SGreg Roach break; 143a25f0a04SGreg Roach } 144a25f0a04SGreg Roach 14565e02381SGreg Roach [$text] = explode("\n", $text); 1469a9e551aSGreg Roach $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom()); 147a25f0a04SGreg Roach } 148a25f0a04SGreg Roach } 149a25f0a04SGreg Roach} 150