1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 17a25f0a04SGreg Roach 18a25f0a04SGreg Roach/** 1976692c8bSGreg Roach * A GEDCOM note (NOTE) object. 20a25f0a04SGreg Roach */ 21*c1010edaSGreg Roachclass Note extends GedcomRecord 22*c1010edaSGreg Roach{ 23a25f0a04SGreg Roach const RECORD_TYPE = 'NOTE'; 24225e381fSGreg Roach const ROUTE_NAME = 'note'; 25a25f0a04SGreg Roach 26a25f0a04SGreg Roach /** 27e71ef9d2SGreg Roach * Get an instance of a note object. For single records, 28e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 29e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 30e71ef9d2SGreg Roach * 31e71ef9d2SGreg Roach * @param string $xref 32e71ef9d2SGreg Roach * @param Tree $tree 33e71ef9d2SGreg Roach * @param string|null $gedcom 34e71ef9d2SGreg Roach * 35e71ef9d2SGreg Roach * @throws \Exception 36e71ef9d2SGreg Roach * 37e71ef9d2SGreg Roach * @return Note|null 38e71ef9d2SGreg Roach */ 39*c1010edaSGreg Roach public static function getInstance($xref, Tree $tree, $gedcom = null) 40*c1010edaSGreg Roach { 41e71ef9d2SGreg Roach $record = parent::getInstance($xref, $tree, $gedcom); 42e71ef9d2SGreg Roach 43e71ef9d2SGreg Roach if ($record instanceof Note) { 44e71ef9d2SGreg Roach return $record; 45e71ef9d2SGreg Roach } else { 46e71ef9d2SGreg Roach return null; 47e71ef9d2SGreg Roach } 48e71ef9d2SGreg Roach } 49e71ef9d2SGreg Roach 50e71ef9d2SGreg Roach /** 51a25f0a04SGreg Roach * Get the text contents of the note 52a25f0a04SGreg Roach * 53a25f0a04SGreg Roach * @return string|null 54a25f0a04SGreg Roach */ 55*c1010edaSGreg Roach public function getNote() 56*c1010edaSGreg Roach { 57a25f0a04SGreg Roach if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) { 58a25f0a04SGreg Roach return preg_replace("/\n1 CONT ?/", "\n", $match[1]); 59a25f0a04SGreg Roach } else { 60a25f0a04SGreg Roach return null; 61a25f0a04SGreg Roach } 62a25f0a04SGreg Roach } 63a25f0a04SGreg Roach 6476692c8bSGreg Roach /** 6576692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 6676692c8bSGreg Roach * 6776692c8bSGreg Roach * @param int $access_level 6876692c8bSGreg Roach * 6976692c8bSGreg Roach * @return bool 7076692c8bSGreg Roach */ 71*c1010edaSGreg Roach protected function canShowByType($access_level) 72*c1010edaSGreg Roach { 73a25f0a04SGreg Roach // Hide notes if they are attached to private records 74a25f0a04SGreg Roach $linked_ids = Database::prepare( 75a25f0a04SGreg Roach "SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?" 7613abd6f3SGreg Roach )->execute([ 77*c1010edaSGreg Roach $this->xref, 78*c1010edaSGreg Roach $this->tree->getTreeId(), 7913abd6f3SGreg Roach ])->fetchOneColumn(); 80a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 8124ec66ceSGreg Roach $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 82a25f0a04SGreg Roach if ($linked_record && !$linked_record->canShow($access_level)) { 83a25f0a04SGreg Roach return false; 84a25f0a04SGreg Roach } 85a25f0a04SGreg Roach } 86a25f0a04SGreg Roach 87a25f0a04SGreg Roach // Apply default behaviour 88a25f0a04SGreg Roach return parent::canShowByType($access_level); 89a25f0a04SGreg Roach } 90a25f0a04SGreg Roach 9176692c8bSGreg Roach /** 9276692c8bSGreg Roach * Generate a private version of this record 9376692c8bSGreg Roach * 9476692c8bSGreg Roach * @param int $access_level 9576692c8bSGreg Roach * 9676692c8bSGreg Roach * @return string 9776692c8bSGreg Roach */ 98*c1010edaSGreg Roach protected function createPrivateGedcomRecord($access_level) 99*c1010edaSGreg Roach { 100a25f0a04SGreg Roach return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private'); 101a25f0a04SGreg Roach } 102a25f0a04SGreg Roach 10376692c8bSGreg Roach /** 10476692c8bSGreg Roach * Fetch data from the database 10576692c8bSGreg Roach * 10676692c8bSGreg Roach * @param string $xref 10776692c8bSGreg Roach * @param int $tree_id 10876692c8bSGreg Roach * 10976692c8bSGreg Roach * @return null|string 11076692c8bSGreg Roach */ 111*c1010edaSGreg Roach protected static function fetchGedcomRecord($xref, $tree_id) 112*c1010edaSGreg Roach { 11364d9078aSGreg Roach return Database::prepare( 11464d9078aSGreg Roach "SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'" 11513abd6f3SGreg Roach )->execute([ 11664d9078aSGreg Roach 'xref' => $xref, 117cbc1590aSGreg Roach 'tree_id' => $tree_id, 11813abd6f3SGreg Roach ])->fetchOne(); 119a25f0a04SGreg Roach } 120a25f0a04SGreg Roach 121a25f0a04SGreg Roach /** 122a25f0a04SGreg Roach * Create a name for this note - apply (and remove) markup, then take 123a25f0a04SGreg Roach * a maximum of 100 characters from the first line. 124a25f0a04SGreg Roach */ 125*c1010edaSGreg Roach public function extractNames() 126*c1010edaSGreg Roach { 127a25f0a04SGreg Roach $text = $this->getNote(); 128a25f0a04SGreg Roach 129a25f0a04SGreg Roach if ($text) { 13084caa210SGreg Roach switch ($this->getTree()->getPreference('FORMAT_TEXT')) { 131a25f0a04SGreg Roach case 'markdown': 13222acdb08SGreg Roach $text = Filter::markdown($text, $this->getTree()); 1339524b7b5SGreg Roach $text = strip_tags($text); 1349524b7b5SGreg Roach $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); 135a25f0a04SGreg Roach break; 136a25f0a04SGreg Roach } 137a25f0a04SGreg Roach 138a25f0a04SGreg Roach list($text) = explode("\n", $text); 139a25f0a04SGreg Roach $this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom()); 140a25f0a04SGreg Roach } 141a25f0a04SGreg Roach } 142a25f0a04SGreg Roach} 143