1a25f0a04SGreg Roach<?php 2dd04c183SGreg Roachnamespace Fisharebest\Webtrees; 3a25f0a04SGreg Roach 4a25f0a04SGreg Roach/** 5a25f0a04SGreg Roach * webtrees: online genealogy 6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team 7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 10a25f0a04SGreg Roach * (at your option) any later version. 11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14a25f0a04SGreg Roach * GNU General Public License for more details. 15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 17a25f0a04SGreg Roach */ 18a25f0a04SGreg Roach 19a25f0a04SGreg Roach/** 20a25f0a04SGreg Roach * Class GedcomRecord - Base class for all gedcom records 21a25f0a04SGreg Roach */ 22a25f0a04SGreg Roachclass GedcomRecord { 23a25f0a04SGreg Roach const RECORD_TYPE = 'UNKNOWN'; 24a25f0a04SGreg Roach const URL_PREFIX = 'gedrecord.php?pid='; 25a25f0a04SGreg Roach 26a25f0a04SGreg Roach /** @var string The record identifier */ 27a25f0a04SGreg Roach protected $xref; 28a25f0a04SGreg Roach 29000959d9SGreg Roach /** @var Tree The family tree to which this record belongs */ 30000959d9SGreg Roach protected $tree; 31a25f0a04SGreg Roach 32a25f0a04SGreg Roach /** @var string GEDCOM data (before any pending edits) */ 33a25f0a04SGreg Roach protected $gedcom; 34a25f0a04SGreg Roach 35a25f0a04SGreg Roach /** @var string|null GEDCOM data (after any pending edits) */ 36a25f0a04SGreg Roach protected $pending; 37a25f0a04SGreg Roach 38a25f0a04SGreg Roach /** @var Fact[] facts extracted from $gedcom/$pending */ 39a25f0a04SGreg Roach protected $facts; 40a25f0a04SGreg Roach 41a25f0a04SGreg Roach /** @var bool Can we display details of this record to WT_PRIV_PUBLIC */ 42a25f0a04SGreg Roach private $disp_public; 43a25f0a04SGreg Roach 44a25f0a04SGreg Roach /** @var bool Can we display details of this record to WT_PRIV_USER */ 45a25f0a04SGreg Roach private $disp_user; 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** @var bool Can we display details of this record to WT_PRIV_NONE */ 48a25f0a04SGreg Roach private $disp_none; 49a25f0a04SGreg Roach 50a25f0a04SGreg Roach /** @var string[][] All the names of this individual */ 51a25f0a04SGreg Roach protected $_getAllNames; 52a25f0a04SGreg Roach 53a25f0a04SGreg Roach /** @var int Cached result */ 54a25f0a04SGreg Roach protected $_getPrimaryName; 55a25f0a04SGreg Roach 56a25f0a04SGreg Roach /** @var int Cached result */ 57a25f0a04SGreg Roach protected $_getSecondaryName; 58a25f0a04SGreg Roach 59a25f0a04SGreg Roach // Allow getInstance() to return references to existing objects 60a25f0a04SGreg Roach private static $gedcom_record_cache; 61a25f0a04SGreg Roach // Fetch all pending edits in one database query 62a25f0a04SGreg Roach private static $pending_record_cache; 63a25f0a04SGreg Roach 64a25f0a04SGreg Roach /** 65a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 66a25f0a04SGreg Roach * 67a25f0a04SGreg Roach * @param string $xref 68a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 69a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 70a25f0a04SGreg Roach * empty string for records with pending deletions 71000959d9SGreg Roach * @param integer $tree_id 72a25f0a04SGreg Roach */ 73000959d9SGreg Roach public function __construct($xref, $gedcom, $pending, $tree_id) { 74a25f0a04SGreg Roach $this->xref = $xref; 75a25f0a04SGreg Roach $this->gedcom = $gedcom; 76a25f0a04SGreg Roach $this->pending = $pending; 77*d2cdeb3fSGreg Roach $this->tree = Tree::findById($tree_id); 78a25f0a04SGreg Roach 79a25f0a04SGreg Roach $this->parseFacts(); 80a25f0a04SGreg Roach } 81a25f0a04SGreg Roach 82a25f0a04SGreg Roach /** 83a25f0a04SGreg Roach * Split the record into facts 84a25f0a04SGreg Roach */ 85a25f0a04SGreg Roach private function parseFacts() { 86a25f0a04SGreg Roach // Split the record into facts 87a25f0a04SGreg Roach if ($this->gedcom) { 88a25f0a04SGreg Roach $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom); 89a25f0a04SGreg Roach array_shift($gedcom_facts); 90a25f0a04SGreg Roach } else { 91a25f0a04SGreg Roach $gedcom_facts = array(); 92a25f0a04SGreg Roach } 93a25f0a04SGreg Roach if ($this->pending) { 94a25f0a04SGreg Roach $pending_facts = preg_split('/\n(?=1)/s', $this->pending); 95a25f0a04SGreg Roach array_shift($pending_facts); 96a25f0a04SGreg Roach } else { 97a25f0a04SGreg Roach $pending_facts = array(); 98a25f0a04SGreg Roach } 99a25f0a04SGreg Roach 100a25f0a04SGreg Roach $this->facts = array(); 101a25f0a04SGreg Roach 102a25f0a04SGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 103a25f0a04SGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 104a25f0a04SGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts)) { 105a25f0a04SGreg Roach $fact->setPendingDeletion(); 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach $this->facts[] = $fact; 108a25f0a04SGreg Roach } 109a25f0a04SGreg Roach foreach ($pending_facts as $pending_fact) { 110a25f0a04SGreg Roach if (!in_array($pending_fact, $gedcom_facts)) { 111a25f0a04SGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 112a25f0a04SGreg Roach $fact->setPendingAddition(); 113a25f0a04SGreg Roach $this->facts[] = $fact; 114a25f0a04SGreg Roach } 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach } 117a25f0a04SGreg Roach 118a25f0a04SGreg Roach /** 119a25f0a04SGreg Roach * Get an instance of a GedcomRecord object. For single records, 120a25f0a04SGreg Roach * we just receive the XREF. For bulk records (such as lists 121a25f0a04SGreg Roach * and search results) we can receive the GEDCOM data as well. 122a25f0a04SGreg Roach * 123a25f0a04SGreg Roach * @param string $xref 124a25f0a04SGreg Roach * @param integer|null $gedcom_id 125a25f0a04SGreg Roach * @param string|null $gedcom 126a25f0a04SGreg Roach * 127a25f0a04SGreg Roach * @return GedcomRecord|null 128a25f0a04SGreg Roach * @throws \Exception 129a25f0a04SGreg Roach */ 130a25f0a04SGreg Roach public static function getInstance($xref, $gedcom_id = WT_GED_ID, $gedcom = null) { 131a25f0a04SGreg Roach // Is this record already in the cache? 132a25f0a04SGreg Roach if (isset(self::$gedcom_record_cache[$xref][$gedcom_id])) { 133a25f0a04SGreg Roach return self::$gedcom_record_cache[$xref][$gedcom_id]; 134a25f0a04SGreg Roach } 135a25f0a04SGreg Roach 136a25f0a04SGreg Roach // Do we need to fetch the record from the database? 137a25f0a04SGreg Roach if ($gedcom === null) { 138a25f0a04SGreg Roach $gedcom = static::fetchGedcomRecord($xref, $gedcom_id); 139a25f0a04SGreg Roach } 140a25f0a04SGreg Roach 141a25f0a04SGreg Roach // If we can edit, then we also need to be able to see pending records. 142a25f0a04SGreg Roach if (WT_USER_CAN_EDIT) { 143a25f0a04SGreg Roach if (!isset(self::$pending_record_cache[$gedcom_id])) { 144a25f0a04SGreg Roach // Fetch all pending records in one database query 145a25f0a04SGreg Roach self::$pending_record_cache[$gedcom_id] = array(); 146a25f0a04SGreg Roach $rows = Database::prepare( 147a25f0a04SGreg Roach "SELECT xref, new_gedcom FROM `##change` WHERE status='pending' AND gedcom_id=?" 148a25f0a04SGreg Roach )->execute(array($gedcom_id))->fetchAll(); 149a25f0a04SGreg Roach foreach ($rows as $row) { 150a25f0a04SGreg Roach self::$pending_record_cache[$gedcom_id][$row->xref] = $row->new_gedcom; 151a25f0a04SGreg Roach } 152a25f0a04SGreg Roach } 153a25f0a04SGreg Roach 154a25f0a04SGreg Roach if (isset(self::$pending_record_cache[$gedcom_id][$xref])) { 155a25f0a04SGreg Roach // A pending edit exists for this record 156a25f0a04SGreg Roach $pending = self::$pending_record_cache[$gedcom_id][$xref]; 157a25f0a04SGreg Roach } else { 158a25f0a04SGreg Roach $pending = null; 159a25f0a04SGreg Roach } 160a25f0a04SGreg Roach } else { 161a25f0a04SGreg Roach // There are no pending changes for this record 162a25f0a04SGreg Roach $pending = null; 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach 165a25f0a04SGreg Roach // No such record exists 166a25f0a04SGreg Roach if ($gedcom === null && $pending === null) { 167a25f0a04SGreg Roach return null; 168a25f0a04SGreg Roach } 169a25f0a04SGreg Roach 170a25f0a04SGreg Roach // Create the object 171a25f0a04SGreg Roach if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom . $pending, $match)) { 172a25f0a04SGreg Roach $xref = $match[1]; // Collation - we may have requested I123 and found i123 173a25f0a04SGreg Roach $type = $match[2]; 174a25f0a04SGreg Roach } elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) { 175a25f0a04SGreg Roach $xref = $match[1]; 176a25f0a04SGreg Roach $type = $match[1]; 177a25f0a04SGreg Roach } elseif ($gedcom . $pending) { 178a25f0a04SGreg Roach throw new \Exception('Unrecognized GEDCOM record: ' . $gedcom); 179a25f0a04SGreg Roach } else { 180a25f0a04SGreg Roach // A record with both pending creation and pending deletion 181a25f0a04SGreg Roach $type = static::RECORD_TYPE; 182a25f0a04SGreg Roach } 183a25f0a04SGreg Roach 184a25f0a04SGreg Roach switch ($type) { 185a25f0a04SGreg Roach case 'INDI': 186a25f0a04SGreg Roach $record = new Individual($xref, $gedcom, $pending, $gedcom_id); 187a25f0a04SGreg Roach break; 188a25f0a04SGreg Roach case 'FAM': 189a25f0a04SGreg Roach $record = new Family($xref, $gedcom, $pending, $gedcom_id); 190a25f0a04SGreg Roach break; 191a25f0a04SGreg Roach case 'SOUR': 192a25f0a04SGreg Roach $record = new Source($xref, $gedcom, $pending, $gedcom_id); 193a25f0a04SGreg Roach break; 194a25f0a04SGreg Roach case 'OBJE': 195a25f0a04SGreg Roach $record = new Media($xref, $gedcom, $pending, $gedcom_id); 196a25f0a04SGreg Roach break; 197a25f0a04SGreg Roach case 'REPO': 198a25f0a04SGreg Roach $record = new Repository($xref, $gedcom, $pending, $gedcom_id); 199a25f0a04SGreg Roach break; 200a25f0a04SGreg Roach case 'NOTE': 201a25f0a04SGreg Roach $record = new Note($xref, $gedcom, $pending, $gedcom_id); 202a25f0a04SGreg Roach break; 203a25f0a04SGreg Roach default: 204a25f0a04SGreg Roach $record = new GedcomRecord($xref, $gedcom, $pending, $gedcom_id); 205a25f0a04SGreg Roach break; 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach 208a25f0a04SGreg Roach // Store it in the cache 209a25f0a04SGreg Roach self::$gedcom_record_cache[$xref][$gedcom_id] = $record; 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach return $record; 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 214a25f0a04SGreg Roach /** 215a25f0a04SGreg Roach * Fetch data from the database 216a25f0a04SGreg Roach * 217a25f0a04SGreg Roach * @param string $xref 218a25f0a04SGreg Roach * @param integer $gedcom_id 219a25f0a04SGreg Roach * 220a25f0a04SGreg Roach * @return null|string 221a25f0a04SGreg Roach */ 222a25f0a04SGreg Roach protected static function fetchGedcomRecord($xref, $gedcom_id) { 223a25f0a04SGreg Roach static $statement = null; 224a25f0a04SGreg Roach 225a25f0a04SGreg Roach // We don't know what type of object this is. Try each one in turn. 226a25f0a04SGreg Roach $data = Individual::fetchGedcomRecord($xref, $gedcom_id); 227a25f0a04SGreg Roach if ($data) { 228a25f0a04SGreg Roach return $data; 229a25f0a04SGreg Roach } 230a25f0a04SGreg Roach $data = Family::fetchGedcomRecord($xref, $gedcom_id); 231a25f0a04SGreg Roach if ($data) { 232a25f0a04SGreg Roach return $data; 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach $data = Source::fetchGedcomRecord($xref, $gedcom_id); 235a25f0a04SGreg Roach if ($data) { 236a25f0a04SGreg Roach return $data; 237a25f0a04SGreg Roach } 238a25f0a04SGreg Roach $data = Repository::fetchGedcomRecord($xref, $gedcom_id); 239a25f0a04SGreg Roach if ($data) { 240a25f0a04SGreg Roach return $data; 241a25f0a04SGreg Roach } 242a25f0a04SGreg Roach $data = Media::fetchGedcomRecord($xref, $gedcom_id); 243a25f0a04SGreg Roach if ($data) { 244a25f0a04SGreg Roach return $data; 245a25f0a04SGreg Roach } 246a25f0a04SGreg Roach $data = Note::fetchGedcomRecord($xref, $gedcom_id); 247a25f0a04SGreg Roach if ($data) { 248a25f0a04SGreg Roach return $data; 249a25f0a04SGreg Roach } 250a25f0a04SGreg Roach // Some other type of record... 251a25f0a04SGreg Roach if (is_null($statement)) { 252a25f0a04SGreg Roach $statement = Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_id=? AND o_file=?"); 253a25f0a04SGreg Roach } 254a25f0a04SGreg Roach return $statement->execute(array($xref, $gedcom_id))->fetchOne(); 255a25f0a04SGreg Roach 256a25f0a04SGreg Roach } 257a25f0a04SGreg Roach 258a25f0a04SGreg Roach /** 259a25f0a04SGreg Roach * Get the XREF for this record 260a25f0a04SGreg Roach * 261a25f0a04SGreg Roach * @return string 262a25f0a04SGreg Roach */ 263a25f0a04SGreg Roach public function getXref() { 264a25f0a04SGreg Roach return $this->xref; 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach 267a25f0a04SGreg Roach /** 268000959d9SGreg Roach * Get the tree to which this record belongs 269000959d9SGreg Roach * 270000959d9SGreg Roach * @return Tree 271000959d9SGreg Roach */ 272000959d9SGreg Roach public function getTree() { 273518bbdc1SGreg Roach return $this->tree; 274000959d9SGreg Roach } 275000959d9SGreg Roach 276000959d9SGreg Roach /** 277a25f0a04SGreg Roach * Application code should access data via Fact objects. 278a25f0a04SGreg Roach * This function exists to support old code. 279a25f0a04SGreg Roach * 280a25f0a04SGreg Roach * @return string 281a25f0a04SGreg Roach */ 282a25f0a04SGreg Roach public function getGedcom() { 283a25f0a04SGreg Roach if ($this->pending === null) { 284a25f0a04SGreg Roach return $this->gedcom; 285a25f0a04SGreg Roach } else { 286a25f0a04SGreg Roach return $this->pending; 287a25f0a04SGreg Roach } 288a25f0a04SGreg Roach } 289a25f0a04SGreg Roach 290a25f0a04SGreg Roach /** 291a25f0a04SGreg Roach * Does this record have a pending change? 292a25f0a04SGreg Roach * 293a25f0a04SGreg Roach * @return boolean 294a25f0a04SGreg Roach */ 295a25f0a04SGreg Roach public function isPendingAddtion() { 296a25f0a04SGreg Roach return $this->pending !== null; 297a25f0a04SGreg Roach } 298a25f0a04SGreg Roach 299a25f0a04SGreg Roach /** 300a25f0a04SGreg Roach * Does this record have a pending deletion? 301a25f0a04SGreg Roach * 302a25f0a04SGreg Roach * @return boolean 303a25f0a04SGreg Roach */ 304a25f0a04SGreg Roach public function isPendingDeletion() { 305a25f0a04SGreg Roach return $this->pending === ''; 306a25f0a04SGreg Roach } 307a25f0a04SGreg Roach 308a25f0a04SGreg Roach /** 309a25f0a04SGreg Roach * Generate a URL to this record, suitable for use in HTML, etc. 310a25f0a04SGreg Roach * 311a25f0a04SGreg Roach * @return string 312a25f0a04SGreg Roach */ 313a25f0a04SGreg Roach public function getHtmlUrl() { 314a25f0a04SGreg Roach return $this->getLinkUrl(static::URL_PREFIX, '&'); 315a25f0a04SGreg Roach } 316a25f0a04SGreg Roach 317a25f0a04SGreg Roach /** 318a25f0a04SGreg Roach * Generate a URL to this record, suitable for use in javascript, HTTP headers, etc. 319a25f0a04SGreg Roach * 320a25f0a04SGreg Roach * @return string 321a25f0a04SGreg Roach */ 322a25f0a04SGreg Roach public function getRawUrl() { 323a25f0a04SGreg Roach return $this->getLinkUrl(static::URL_PREFIX, '&'); 324a25f0a04SGreg Roach } 325a25f0a04SGreg Roach 326a25f0a04SGreg Roach /** 327a25f0a04SGreg Roach * Generate an absolute URL for this record, suitable for sitemap.xml, RSS feeds, etc. 328a25f0a04SGreg Roach * 329a25f0a04SGreg Roach * @return string 330a25f0a04SGreg Roach */ 331a25f0a04SGreg Roach public function getAbsoluteLinkUrl() { 332a25f0a04SGreg Roach return WT_BASE_URL . $this->getHtmlUrl(); 333a25f0a04SGreg Roach } 334a25f0a04SGreg Roach 335a25f0a04SGreg Roach /** 336a25f0a04SGreg Roach * Generate a URL to this record. 337a25f0a04SGreg Roach * 338a25f0a04SGreg Roach * @param string $link 339a25f0a04SGreg Roach * @param string $separator 340a25f0a04SGreg Roach * 341a25f0a04SGreg Roach * @return string 342a25f0a04SGreg Roach */ 343a25f0a04SGreg Roach private function getLinkUrl($link, $separator) { 344518bbdc1SGreg Roach if ($this->tree->getTreeId() == WT_GED_ID) { 345a25f0a04SGreg Roach return $link . $this->getXref() . $separator . 'ged=' . WT_GEDURL; 346518bbdc1SGreg Roach } elseif ($this->tree->getTreeId() == 0) { 347a25f0a04SGreg Roach return '#'; 348a25f0a04SGreg Roach } else { 349518bbdc1SGreg Roach return $link . $this->getXref() . $separator . 'ged=' . rawurlencode(get_gedcom_from_id($this->tree->getTreeId())); 350a25f0a04SGreg Roach } 351a25f0a04SGreg Roach } 352a25f0a04SGreg Roach 353a25f0a04SGreg Roach /** 354a25f0a04SGreg Roach * Work out whether this record can be shown to a user with a given access level 355a25f0a04SGreg Roach * 356a25f0a04SGreg Roach * @param integer $access_level 357a25f0a04SGreg Roach * 358a25f0a04SGreg Roach * @return boolean 359a25f0a04SGreg Roach */ 360a25f0a04SGreg Roach private function _canShow($access_level) { 361a25f0a04SGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 362518bbdc1SGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 363a25f0a04SGreg Roach return true; 364a25f0a04SGreg Roach } 365a25f0a04SGreg Roach 366a25f0a04SGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 367518bbdc1SGreg Roach if ($this->getXref() === WT_USER_GEDCOM_ID && $this->tree->getTreeId() === WT_GED_ID && $access_level === WT_USER_ACCESS_LEVEL) { 368a25f0a04SGreg Roach return true; 369a25f0a04SGreg Roach } 370a25f0a04SGreg Roach 371a25f0a04SGreg Roach // Does this record have a RESN? 372a25f0a04SGreg Roach if (strpos($this->gedcom, "\n1 RESN confidential")) { 373a25f0a04SGreg Roach return WT_PRIV_NONE >= $access_level; 374a25f0a04SGreg Roach } 375a25f0a04SGreg Roach if (strpos($this->gedcom, "\n1 RESN privacy")) { 376a25f0a04SGreg Roach return WT_PRIV_USER >= $access_level; 377a25f0a04SGreg Roach } 378a25f0a04SGreg Roach if (strpos($this->gedcom, "\n1 RESN none")) { 379a25f0a04SGreg Roach return true; 380a25f0a04SGreg Roach } 381a25f0a04SGreg Roach 382a25f0a04SGreg Roach // Does this record have a default RESN? 383518bbdc1SGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 384518bbdc1SGreg Roach if (isset($individual_privacy[$this->getXref()])) { 385518bbdc1SGreg Roach return $individual_privacy[$this->getXref()] >= $access_level; 386a25f0a04SGreg Roach } 387a25f0a04SGreg Roach 388a25f0a04SGreg Roach // Privacy rules do not apply to admins 389a25f0a04SGreg Roach if (WT_PRIV_NONE >= $access_level) { 390a25f0a04SGreg Roach return true; 391a25f0a04SGreg Roach } 392a25f0a04SGreg Roach 393a25f0a04SGreg Roach // Different types of record have different privacy rules 394a25f0a04SGreg Roach return $this->canShowByType($access_level); 395a25f0a04SGreg Roach } 396a25f0a04SGreg Roach 397a25f0a04SGreg Roach /** 398a25f0a04SGreg Roach * Each object type may have its own special rules, and re-implement this function. 399a25f0a04SGreg Roach * 400a25f0a04SGreg Roach * @param integer $access_level 401a25f0a04SGreg Roach * 402a25f0a04SGreg Roach * @return boolean 403a25f0a04SGreg Roach */ 404a25f0a04SGreg Roach protected function canShowByType($access_level) { 405518bbdc1SGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 406a25f0a04SGreg Roach 407518bbdc1SGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 408a25f0a04SGreg Roach // Restriction found 409518bbdc1SGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 410a25f0a04SGreg Roach } else { 411a25f0a04SGreg Roach // No restriction found - must be public: 412a25f0a04SGreg Roach return true; 413a25f0a04SGreg Roach } 414a25f0a04SGreg Roach } 415a25f0a04SGreg Roach 416a25f0a04SGreg Roach /** 417a25f0a04SGreg Roach * Can the details of this record be shown? 418a25f0a04SGreg Roach * 419a25f0a04SGreg Roach * @param integer $access_level 420a25f0a04SGreg Roach * 421a25f0a04SGreg Roach * @return boolean 422a25f0a04SGreg Roach */ 423a25f0a04SGreg Roach public function canShow($access_level = WT_USER_ACCESS_LEVEL) { 424a25f0a04SGreg Roach // CACHING: this function can take three different parameters, 425a25f0a04SGreg Roach // and therefore needs three different caches for the result. 426a25f0a04SGreg Roach switch ($access_level) { 427a25f0a04SGreg Roach case WT_PRIV_PUBLIC: // visitor 428a25f0a04SGreg Roach if ($this->disp_public === null) { 429a25f0a04SGreg Roach $this->disp_public = $this->_canShow(WT_PRIV_PUBLIC); 430a25f0a04SGreg Roach } 431a25f0a04SGreg Roach return $this->disp_public; 432a25f0a04SGreg Roach case WT_PRIV_USER: // member 433a25f0a04SGreg Roach if ($this->disp_user === null) { 434a25f0a04SGreg Roach $this->disp_user = $this->_canShow(WT_PRIV_USER); 435a25f0a04SGreg Roach } 436a25f0a04SGreg Roach return $this->disp_user; 437a25f0a04SGreg Roach case WT_PRIV_NONE: // admin 438a25f0a04SGreg Roach if ($this->disp_none === null) { 439a25f0a04SGreg Roach $this->disp_none = $this->_canShow(WT_PRIV_NONE); 440a25f0a04SGreg Roach } 441a25f0a04SGreg Roach return $this->disp_none; 442a25f0a04SGreg Roach case WT_PRIV_HIDE: // hidden from admins 443a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 444a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 445a25f0a04SGreg Roach return true; 446a25f0a04SGreg Roach default: 447a25f0a04SGreg Roach // Should never get here. 448a25f0a04SGreg Roach return false; 449a25f0a04SGreg Roach } 450a25f0a04SGreg Roach } 451a25f0a04SGreg Roach 452a25f0a04SGreg Roach /** 453a25f0a04SGreg Roach * Can the name of this record be shown? 454a25f0a04SGreg Roach * 455a25f0a04SGreg Roach * @param integer $access_level 456a25f0a04SGreg Roach * 457a25f0a04SGreg Roach * @return boolean 458a25f0a04SGreg Roach */ 459a25f0a04SGreg Roach public function canShowName($access_level = WT_USER_ACCESS_LEVEL) { 460a25f0a04SGreg Roach return $this->canShow($access_level); 461a25f0a04SGreg Roach } 462a25f0a04SGreg Roach 463a25f0a04SGreg Roach /** 464a25f0a04SGreg Roach * Can we edit this record? 465a25f0a04SGreg Roach * 466a25f0a04SGreg Roach * @return boolean 467a25f0a04SGreg Roach */ 468a25f0a04SGreg Roach public function canEdit() { 469a25f0a04SGreg Roach return WT_USER_GEDCOM_ADMIN || WT_USER_CAN_EDIT && strpos($this->gedcom, "\n1 RESN locked") === false; 470a25f0a04SGreg Roach } 471a25f0a04SGreg Roach 472a25f0a04SGreg Roach /** 473a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 474a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 475a25f0a04SGreg Roach * 476a25f0a04SGreg Roach * @param integer $access_level 477a25f0a04SGreg Roach * 478a25f0a04SGreg Roach * @return string 479a25f0a04SGreg Roach */ 480a25f0a04SGreg Roach public function privatizeGedcom($access_level) { 481a25f0a04SGreg Roach if ($access_level == WT_PRIV_HIDE) { 482a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 483a25f0a04SGreg Roach return $this->gedcom; 484a25f0a04SGreg Roach } elseif ($this->canShow($access_level)) { 485a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 486a25f0a04SGreg Roach 487a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 488a25f0a04SGreg Roach list($gedrec) = explode("\n", $this->gedcom, 2); 489a25f0a04SGreg Roach 490a25f0a04SGreg Roach // Check each of the facts for access 491a25f0a04SGreg Roach foreach ($this->getFacts(null, false, $access_level) as $fact) { 492a25f0a04SGreg Roach $gedrec .= "\n" . $fact->getGedcom(); 493a25f0a04SGreg Roach } 494a25f0a04SGreg Roach return $gedrec; 495a25f0a04SGreg Roach } else { 496a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 497a25f0a04SGreg Roach // limited data, such as links to other records. 498a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 499a25f0a04SGreg Roach } 500a25f0a04SGreg Roach } 501a25f0a04SGreg Roach 502a25f0a04SGreg Roach /** 503a25f0a04SGreg Roach * Generate a private version of this record 504a25f0a04SGreg Roach * 505a25f0a04SGreg Roach * @param integer $access_level 506a25f0a04SGreg Roach * 507a25f0a04SGreg Roach * @return string 508a25f0a04SGreg Roach */ 509a25f0a04SGreg Roach protected function createPrivateGedcomRecord($access_level) { 510a25f0a04SGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private'); 511a25f0a04SGreg Roach } 512a25f0a04SGreg Roach 513a25f0a04SGreg Roach /** 514a25f0a04SGreg Roach * Convert a name record into sortable and full/display versions. This default 515a25f0a04SGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 516a25f0a04SGreg Roach * 517a25f0a04SGreg Roach * @param string $type 518a25f0a04SGreg Roach * @param string $value 519a25f0a04SGreg Roach * @param string $gedcom 520a25f0a04SGreg Roach */ 521a25f0a04SGreg Roach protected function addName($type, $value, $gedcom) { 522a25f0a04SGreg Roach $this->_getAllNames[] = array( 523a25f0a04SGreg Roach 'type' => $type, 524a25f0a04SGreg Roach 'sort' => preg_replace_callback('/([0-9]+)/', function($matches) { return str_pad($matches[0], 10, '0', STR_PAD_LEFT); }, $value), 525a25f0a04SGreg Roach 'full' => '<span dir="auto">' . Filter::escapeHtml($value) . '</span>', // This is used for display 526a25f0a04SGreg Roach 'fullNN' => $value, // This goes into the database 527a25f0a04SGreg Roach ); 528a25f0a04SGreg Roach } 529a25f0a04SGreg Roach 530a25f0a04SGreg Roach /** 531a25f0a04SGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 532a25f0a04SGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 533a25f0a04SGreg Roach * Parameters: the level 1 fact containing the name. 534a25f0a04SGreg Roach * Return value: an array of name structures, each containing 535a25f0a04SGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 536a25f0a04SGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 537a25f0a04SGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 538a25f0a04SGreg Roach * 539a25f0a04SGreg Roach * @param integer $level 540a25f0a04SGreg Roach * @param string $fact_type 541a25f0a04SGreg Roach * @param Fact[] $facts 542a25f0a04SGreg Roach */ 543a25f0a04SGreg Roach protected function _extractNames($level, $fact_type, $facts) { 544a25f0a04SGreg Roach $sublevel = $level + 1; 545a25f0a04SGreg Roach $subsublevel = $sublevel + 1; 546a25f0a04SGreg Roach foreach ($facts as $fact) { 547a25f0a04SGreg Roach if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->getGedcom(), $matches, PREG_SET_ORDER)) { 548a25f0a04SGreg Roach foreach ($matches as $match) { 549a25f0a04SGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 550a25f0a04SGreg Roach if ($match[1] == 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) { 551a25f0a04SGreg Roach $this->addName('_MARNM', $match[2], $fact->getGedcom()); 552a25f0a04SGreg Roach } else { 553a25f0a04SGreg Roach $this->addName($match[1], $match[2], $fact->getGedcom()); 554a25f0a04SGreg Roach } 555a25f0a04SGreg Roach if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { 556a25f0a04SGreg Roach foreach ($submatches as $submatch) { 557a25f0a04SGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 558a25f0a04SGreg Roach } 559a25f0a04SGreg Roach } 560a25f0a04SGreg Roach } 561a25f0a04SGreg Roach } 562a25f0a04SGreg Roach } 563a25f0a04SGreg Roach } 564a25f0a04SGreg Roach 565a25f0a04SGreg Roach /** 566a25f0a04SGreg Roach * Default for "other" object types 567a25f0a04SGreg Roach */ 568a25f0a04SGreg Roach public function extractNames() { 569a25f0a04SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), null); 570a25f0a04SGreg Roach } 571a25f0a04SGreg Roach 572a25f0a04SGreg Roach /** 573a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 574a25f0a04SGreg Roach * 575a25f0a04SGreg Roach * @return string[][] 576a25f0a04SGreg Roach */ 577a25f0a04SGreg Roach public function getAllNames() { 578a25f0a04SGreg Roach if ($this->_getAllNames === null) { 579a25f0a04SGreg Roach $this->_getAllNames = array(); 580a25f0a04SGreg Roach if ($this->canShowName()) { 581a25f0a04SGreg Roach // Ask the record to extract its names 582a25f0a04SGreg Roach $this->extractNames(); 583a25f0a04SGreg Roach // No name found? Use a fallback. 584a25f0a04SGreg Roach if (!$this->_getAllNames) { 585a25f0a04SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), null); 586a25f0a04SGreg Roach } 587a25f0a04SGreg Roach } else { 588a25f0a04SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), null); 589a25f0a04SGreg Roach } 590a25f0a04SGreg Roach } 591a25f0a04SGreg Roach return $this->_getAllNames; 592a25f0a04SGreg Roach } 593a25f0a04SGreg Roach 594a25f0a04SGreg Roach /** 595a25f0a04SGreg Roach * If this object has no name, what do we call it? 596a25f0a04SGreg Roach * 597a25f0a04SGreg Roach * @return string 598a25f0a04SGreg Roach */ 599a25f0a04SGreg Roach public function getFallBackName() { 600a25f0a04SGreg Roach return $this->getXref(); 601a25f0a04SGreg Roach } 602a25f0a04SGreg Roach 603a25f0a04SGreg Roach /** 604a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 605a25f0a04SGreg Roach * 606a25f0a04SGreg Roach * @return integer 607a25f0a04SGreg Roach */ 608a25f0a04SGreg Roach public function getPrimaryName() { 609a25f0a04SGreg Roach static $language_script; 610a25f0a04SGreg Roach 611a25f0a04SGreg Roach if ($language_script === null) { 612a25f0a04SGreg Roach $language_script = I18N::languageScript(WT_LOCALE); 613a25f0a04SGreg Roach } 614a25f0a04SGreg Roach 615a25f0a04SGreg Roach if ($this->_getPrimaryName === null) { 616a25f0a04SGreg Roach // Generally, the first name is the primary one.... 617a25f0a04SGreg Roach $this->_getPrimaryName = 0; 618a25f0a04SGreg Roach // ...except when the language/name use different character sets 619a25f0a04SGreg Roach if (count($this->getAllNames()) > 1) { 620a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 621a25f0a04SGreg Roach if ($name['type'] !== '_MARNM' && I18N::textScript($name['sort']) === $language_script) { 622a25f0a04SGreg Roach $this->_getPrimaryName = $n; 623a25f0a04SGreg Roach break; 624a25f0a04SGreg Roach } 625a25f0a04SGreg Roach } 626a25f0a04SGreg Roach } 627a25f0a04SGreg Roach } 628a25f0a04SGreg Roach 629a25f0a04SGreg Roach return $this->_getPrimaryName; 630a25f0a04SGreg Roach } 631a25f0a04SGreg Roach 632a25f0a04SGreg Roach /** 633a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 634a25f0a04SGreg Roach * 635a25f0a04SGreg Roach * @return integer 636a25f0a04SGreg Roach */ 637a25f0a04SGreg Roach public function getSecondaryName() { 638a25f0a04SGreg Roach if (is_null($this->_getSecondaryName)) { 639a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 640a25f0a04SGreg Roach $this->_getSecondaryName = $this->getPrimaryName(); 641a25f0a04SGreg Roach // ....except when there are names with different character sets 642a25f0a04SGreg Roach $all_names = $this->getAllNames(); 643a25f0a04SGreg Roach if (count($all_names) > 1) { 644a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 645a25f0a04SGreg Roach foreach ($all_names as $n=>$name) { 646a25f0a04SGreg Roach if ($n != $this->getPrimaryName() && $name['type'] != '_MARNM' && I18N::textScript($name['sort']) != $primary_script) { 647a25f0a04SGreg Roach $this->_getSecondaryName = $n; 648a25f0a04SGreg Roach break; 649a25f0a04SGreg Roach } 650a25f0a04SGreg Roach } 651a25f0a04SGreg Roach } 652a25f0a04SGreg Roach } 653a25f0a04SGreg Roach return $this->_getSecondaryName; 654a25f0a04SGreg Roach } 655a25f0a04SGreg Roach 656a25f0a04SGreg Roach /** 657a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 658a25f0a04SGreg Roach * 659a25f0a04SGreg Roach * @param integer $n 660a25f0a04SGreg Roach */ 661a25f0a04SGreg Roach public function setPrimaryName($n) { 662a25f0a04SGreg Roach $this->_getPrimaryName = $n; 663a25f0a04SGreg Roach $this->_getSecondaryName = null; 664a25f0a04SGreg Roach } 665a25f0a04SGreg Roach 666a25f0a04SGreg Roach /** 667a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 668a25f0a04SGreg Roach * 669a25f0a04SGreg Roach * @return string 670a25f0a04SGreg Roach */ 671a25f0a04SGreg Roach public function __toString() { 672518bbdc1SGreg Roach return $this->xref . '@' . $this->tree->getTreeId(); 673a25f0a04SGreg Roach } 674a25f0a04SGreg Roach 675a25f0a04SGreg Roach /** 676a25f0a04SGreg Roach * Static helper function to sort an array of objects by name 677a25f0a04SGreg Roach * Records whose names cannot be displayed are sorted at the end. 678a25f0a04SGreg Roach * 679a25f0a04SGreg Roach * @param GedcomRecord $x 680a25f0a04SGreg Roach * @param GedcomRecord $y 681a25f0a04SGreg Roach * 682a25f0a04SGreg Roach * @return integer 683a25f0a04SGreg Roach */ 684a25f0a04SGreg Roach public static function compare(GedcomRecord $x, GedcomRecord $y) { 685a25f0a04SGreg Roach if ($x->canShowName()) { 686a25f0a04SGreg Roach if ($y->canShowName()) { 687a25f0a04SGreg Roach return I18N::strcasecmp($x->getSortName(), $y->getSortName()); 688a25f0a04SGreg Roach } else { 689a25f0a04SGreg Roach return -1; // only $y is private 690a25f0a04SGreg Roach } 691a25f0a04SGreg Roach } else { 692a25f0a04SGreg Roach if ($y->canShowName()) { 693a25f0a04SGreg Roach return 1; // only $x is private 694a25f0a04SGreg Roach } else { 695a25f0a04SGreg Roach return 0; // both $x and $y private 696a25f0a04SGreg Roach } 697a25f0a04SGreg Roach } 698a25f0a04SGreg Roach } 699a25f0a04SGreg Roach 700a25f0a04SGreg Roach /** 701a25f0a04SGreg Roach * Get variants of the name 702a25f0a04SGreg Roach * 703a25f0a04SGreg Roach * @return string 704a25f0a04SGreg Roach */ 705a25f0a04SGreg Roach public function getFullName() { 706a25f0a04SGreg Roach if ($this->canShowName()) { 707a25f0a04SGreg Roach $tmp = $this->getAllNames(); 708a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 709a25f0a04SGreg Roach } else { 710a25f0a04SGreg Roach return I18N::translate('Private'); 711a25f0a04SGreg Roach } 712a25f0a04SGreg Roach } 713a25f0a04SGreg Roach 714a25f0a04SGreg Roach /** 715a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 716a25f0a04SGreg Roach * 717a25f0a04SGreg Roach * @return string 718a25f0a04SGreg Roach */ 719a25f0a04SGreg Roach public function getSortName() { 720a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 721a25f0a04SGreg Roach $tmp = $this->getAllNames(); 722a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 723a25f0a04SGreg Roach } 724a25f0a04SGreg Roach 725a25f0a04SGreg Roach /** 726a25f0a04SGreg Roach * Get the full name in an alternative character set 727a25f0a04SGreg Roach * 728a25f0a04SGreg Roach * @return null|string 729a25f0a04SGreg Roach */ 730a25f0a04SGreg Roach public function getAddName() { 731a25f0a04SGreg Roach if ($this->canShowName() && $this->getPrimaryName() != $this->getSecondaryName()) { 732a25f0a04SGreg Roach $all_names = $this->getAllNames(); 733a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 734a25f0a04SGreg Roach } else { 735a25f0a04SGreg Roach return null; 736a25f0a04SGreg Roach } 737a25f0a04SGreg Roach } 738a25f0a04SGreg Roach 739a25f0a04SGreg Roach /** 740a25f0a04SGreg Roach * Format this object for display in a list 741a25f0a04SGreg Roach * If $find is set, then we are displaying items from a selection list. 742a25f0a04SGreg Roach * $name allows us to use something other than the record name. 743a25f0a04SGreg Roach * 744a25f0a04SGreg Roach * @param string $tag 745a25f0a04SGreg Roach * @param boolean $find 746a25f0a04SGreg Roach * @param null $name 747a25f0a04SGreg Roach * 748a25f0a04SGreg Roach * @return string 749a25f0a04SGreg Roach */ 750a25f0a04SGreg Roach public function format_list($tag = 'li', $find = false, $name = null) { 751a25f0a04SGreg Roach if (is_null($name)) { 752a25f0a04SGreg Roach $name = $this->getFullName(); 753a25f0a04SGreg Roach } 754a25f0a04SGreg Roach $html = '<a href="' . $this->getHtmlUrl() . '"'; 755a25f0a04SGreg Roach if ($find) { 756a25f0a04SGreg Roach $html .= ' onclick="pasteid(\'' . $this->getXref() . '\', \'' . htmlentities($name) . '\');"'; 757a25f0a04SGreg Roach } 758a25f0a04SGreg Roach $html .= ' class="list_item"><b>' . $name . '</b>'; 759a25f0a04SGreg Roach $html .= $this->formatListDetails(); 760a25f0a04SGreg Roach $html = '<' . $tag . '>' . $html . '</a></' . $tag . '>'; 761a25f0a04SGreg Roach return $html; 762a25f0a04SGreg Roach } 763a25f0a04SGreg Roach 764a25f0a04SGreg Roach /** 765a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 766a25f0a04SGreg Roach * identifying characteristics of this record. 767a25f0a04SGreg Roach * 768a25f0a04SGreg Roach * @return string 769a25f0a04SGreg Roach */ 770a25f0a04SGreg Roach public function formatListDetails() { 771a25f0a04SGreg Roach return ''; 772a25f0a04SGreg Roach } 773a25f0a04SGreg Roach 774a25f0a04SGreg Roach /** 775a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 776a25f0a04SGreg Roach * 777a25f0a04SGreg Roach * @param string $facts 778a25f0a04SGreg Roach * @param integer $style 779a25f0a04SGreg Roach * 780a25f0a04SGreg Roach * @return string 781a25f0a04SGreg Roach */ 782a25f0a04SGreg Roach public function format_first_major_fact($facts, $style) { 783a25f0a04SGreg Roach foreach ($this->getFacts($facts, true) as $event) { 784a25f0a04SGreg Roach // Only display if it has a date or place (or both) 785a25f0a04SGreg Roach if ($event->getDate()->isOK() || !$event->getPlace()->isEmpty()) { 786a25f0a04SGreg Roach switch ($style) { 787a25f0a04SGreg Roach case 1: 788a25f0a04SGreg Roach return '<br><em>' . $event->getLabel() . ' ' . format_fact_date($event, $this, false, false) . ' ' . format_fact_place($event) . '</em>'; 789a25f0a04SGreg Roach case 2: 790a25f0a04SGreg Roach return '<dl><dt class="label">' . $event->getLabel() . '</dt><dd class="field">' . format_fact_date($event, $this, false, false) . ' ' . format_fact_place($event) . '</dd></dl>'; 791a25f0a04SGreg Roach } 792a25f0a04SGreg Roach } 793a25f0a04SGreg Roach } 794a25f0a04SGreg Roach return ''; 795a25f0a04SGreg Roach } 796a25f0a04SGreg Roach 797a25f0a04SGreg Roach /** 798a25f0a04SGreg Roach * Find individuals linked to this record. 799a25f0a04SGreg Roach * 800a25f0a04SGreg Roach * @param string $link 801a25f0a04SGreg Roach * 802360dbd2cSGreg Roach * @return Individual[] 803a25f0a04SGreg Roach */ 804a25f0a04SGreg Roach public function linkedIndividuals($link) { 805a25f0a04SGreg Roach $rows = Database::prepare( 806a25f0a04SGreg Roach "SELECT i_id AS xref, i_file AS gedcom_id, i_gedcom AS gedcom" . 807a25f0a04SGreg Roach " FROM `##individuals`" . 808a25f0a04SGreg Roach " JOIN `##link` ON (i_file=l_file AND i_id=l_from)" . 809a25f0a04SGreg Roach " LEFT JOIN `##name` ON (i_file=n_file AND i_id=n_id AND n_num=0)" . 810a25f0a04SGreg Roach " WHERE i_file=? AND l_type=? AND l_to=?" . 811a25f0a04SGreg Roach " ORDER BY n_sort COLLATE '" . I18N::$collation . "'" 812518bbdc1SGreg Roach )->execute(array($this->tree->getTreeId(), $link, $this->xref))->fetchAll(); 813a25f0a04SGreg Roach 814a25f0a04SGreg Roach $list = array(); 815a25f0a04SGreg Roach foreach ($rows as $row) { 816a25f0a04SGreg Roach $record = Individual::getInstance($row->xref, $row->gedcom_id, $row->gedcom); 817a25f0a04SGreg Roach if ($record->canShowName()) { 818a25f0a04SGreg Roach $list[] = $record; 819a25f0a04SGreg Roach } 820a25f0a04SGreg Roach } 821a25f0a04SGreg Roach return $list; 822a25f0a04SGreg Roach } 823a25f0a04SGreg Roach 824a25f0a04SGreg Roach /** 825a25f0a04SGreg Roach * Find families linked to this record. 826a25f0a04SGreg Roach * 827a25f0a04SGreg Roach * @param string $link 828a25f0a04SGreg Roach * 829360dbd2cSGreg Roach * @return Family[] 830a25f0a04SGreg Roach */ 831a25f0a04SGreg Roach public function linkedFamilies($link) { 832a25f0a04SGreg Roach $rows = Database::prepare( 833a25f0a04SGreg Roach "SELECT f_id AS xref, f_file AS gedcom_id, f_gedcom AS gedcom" . 834a25f0a04SGreg Roach " FROM `##families`" . 835a25f0a04SGreg Roach " JOIN `##link` ON (f_file=l_file AND f_id=l_from)" . 836a25f0a04SGreg Roach " LEFT JOIN `##name` ON (f_file=n_file AND f_id=n_id AND n_num=0)" . 837a25f0a04SGreg Roach " WHERE f_file=? AND l_type=? AND l_to=?" 838518bbdc1SGreg Roach )->execute(array($this->tree->getTreeId(), $link, $this->xref))->fetchAll(); 839a25f0a04SGreg Roach 840a25f0a04SGreg Roach $list = array(); 841a25f0a04SGreg Roach foreach ($rows as $row) { 842a25f0a04SGreg Roach $record = Family::getInstance($row->xref, $row->gedcom_id, $row->gedcom); 843a25f0a04SGreg Roach if ($record->canShowName()) { 844a25f0a04SGreg Roach $list[] = $record; 845a25f0a04SGreg Roach } 846a25f0a04SGreg Roach } 847a25f0a04SGreg Roach return $list; 848a25f0a04SGreg Roach } 849a25f0a04SGreg Roach 850a25f0a04SGreg Roach /** 851a25f0a04SGreg Roach * Find sources linked to this record. 852a25f0a04SGreg Roach * 853a25f0a04SGreg Roach * @param string $link 854a25f0a04SGreg Roach * 855360dbd2cSGreg Roach * @return Source[] 856a25f0a04SGreg Roach */ 857a25f0a04SGreg Roach public function linkedSources($link) { 858a25f0a04SGreg Roach $rows = Database::prepare( 859a25f0a04SGreg Roach "SELECT s_id AS xref, s_file AS gedcom_id, s_gedcom AS gedcom" . 860a25f0a04SGreg Roach " FROM `##sources`" . 861a25f0a04SGreg Roach " JOIN `##link` ON (s_file=l_file AND s_id=l_from)" . 862a25f0a04SGreg Roach " WHERE s_file=? AND l_type=? AND l_to=?" . 863a25f0a04SGreg Roach " ORDER BY s_name COLLATE '" . I18N::$collation . "'" 864518bbdc1SGreg Roach )->execute(array($this->tree->getTreeId(), $link, $this->xref))->fetchAll(); 865a25f0a04SGreg Roach 866a25f0a04SGreg Roach $list = array(); 867a25f0a04SGreg Roach foreach ($rows as $row) { 868a25f0a04SGreg Roach $record = Source::getInstance($row->xref, $row->gedcom_id, $row->gedcom); 869a25f0a04SGreg Roach if ($record->canShowName()) { 870a25f0a04SGreg Roach $list[] = $record; 871a25f0a04SGreg Roach } 872a25f0a04SGreg Roach } 873a25f0a04SGreg Roach return $list; 874a25f0a04SGreg Roach } 875a25f0a04SGreg Roach 876a25f0a04SGreg Roach /** 877a25f0a04SGreg Roach * Find media objects linked to this record. 878a25f0a04SGreg Roach * 879a25f0a04SGreg Roach * @param string $link 880a25f0a04SGreg Roach * 881360dbd2cSGreg Roach * @return Media[] 882a25f0a04SGreg Roach */ 883a25f0a04SGreg Roach public function linkedMedia($link) { 884a25f0a04SGreg Roach $rows = Database::prepare( 885a25f0a04SGreg Roach "SELECT m_id AS xref, m_file AS gedcom_id, m_gedcom AS gedcom" . 886a25f0a04SGreg Roach " FROM `##media`" . 887a25f0a04SGreg Roach " JOIN `##link` ON (m_file=l_file AND m_id=l_from)" . 888a25f0a04SGreg Roach " WHERE m_file=? AND l_type=? AND l_to=?" . 889a25f0a04SGreg Roach " ORDER BY m_titl COLLATE '" . I18N::$collation . "'" 890518bbdc1SGreg Roach )->execute(array($this->tree->getTreeId(), $link, $this->xref))->fetchAll(); 891a25f0a04SGreg Roach 892a25f0a04SGreg Roach $list = array(); 893a25f0a04SGreg Roach foreach ($rows as $row) { 894a25f0a04SGreg Roach $record = Media::getInstance($row->xref, $row->gedcom_id, $row->gedcom); 895a25f0a04SGreg Roach if ($record->canShowName()) { 896a25f0a04SGreg Roach $list[] = $record; 897a25f0a04SGreg Roach } 898a25f0a04SGreg Roach } 899a25f0a04SGreg Roach return $list; 900a25f0a04SGreg Roach } 901a25f0a04SGreg Roach 902a25f0a04SGreg Roach /** 903a25f0a04SGreg Roach * Find notes linked to this record. 904a25f0a04SGreg Roach * 905a25f0a04SGreg Roach * @param string $link 906a25f0a04SGreg Roach * 907a25f0a04SGreg Roach * @return Note[] 908a25f0a04SGreg Roach */ 909a25f0a04SGreg Roach public function linkedNotes($link) { 910a25f0a04SGreg Roach $rows = Database::prepare( 911a25f0a04SGreg Roach "SELECT o_id AS xref, o_file AS gedcom_id, o_gedcom AS gedcom" . 912a25f0a04SGreg Roach " FROM `##other`" . 913a25f0a04SGreg Roach " JOIN `##link` ON (o_file=l_file AND o_id=l_from)" . 914a25f0a04SGreg Roach " LEFT JOIN `##name` ON (o_file=n_file AND o_id=n_id AND n_num=0)" . 915a25f0a04SGreg Roach " WHERE o_file=? AND o_type='NOTE' AND l_type=? AND l_to=?" . 916a25f0a04SGreg Roach " ORDER BY n_sort COLLATE '" . I18N::$collation . "'" 917518bbdc1SGreg Roach )->execute(array($this->tree->getTreeId(), $link, $this->xref))->fetchAll(); 918a25f0a04SGreg Roach 919a25f0a04SGreg Roach $list = array(); 920a25f0a04SGreg Roach foreach ($rows as $row) { 921a25f0a04SGreg Roach $record = Note::getInstance($row->xref, $row->gedcom_id, $row->gedcom); 922a25f0a04SGreg Roach if ($record->canShowName()) { 923a25f0a04SGreg Roach $list[] = $record; 924a25f0a04SGreg Roach } 925a25f0a04SGreg Roach } 926a25f0a04SGreg Roach return $list; 927a25f0a04SGreg Roach } 928a25f0a04SGreg Roach 929a25f0a04SGreg Roach /** 930a25f0a04SGreg Roach * Find repositories linked to this record. 931a25f0a04SGreg Roach * 932a25f0a04SGreg Roach * @param string $link 933a25f0a04SGreg Roach * 934360dbd2cSGreg Roach * @return Repository[] 935a25f0a04SGreg Roach */ 936a25f0a04SGreg Roach public function linkedRepositories($link) { 937a25f0a04SGreg Roach $rows = Database::prepare( 938a25f0a04SGreg Roach "SELECT o_id AS xref, o_file AS gedcom_id, o_gedcom AS gedcom" . 939a25f0a04SGreg Roach " FROM `##other`" . 940a25f0a04SGreg Roach " JOIN `##link` ON (o_file=l_file AND o_id=l_from)" . 941a25f0a04SGreg Roach " LEFT JOIN `##name` ON (o_file=n_file AND o_id=n_id AND n_num=0)" . 942a25f0a04SGreg Roach " WHERE o_file=? AND o_type='REPO' AND l_type=? AND l_to=?" . 943a25f0a04SGreg Roach " ORDER BY n_sort COLLATE '" . I18N::$collation . "'" 944518bbdc1SGreg Roach )->execute(array($this->tree->getTreeId(), $link, $this->xref))->fetchAll(); 945a25f0a04SGreg Roach 946a25f0a04SGreg Roach $list = array(); 947a25f0a04SGreg Roach foreach ($rows as $row) { 948a25f0a04SGreg Roach $record = Repository::getInstance($row->xref, $row->gedcom_id, $row->gedcom); 949a25f0a04SGreg Roach if ($record->canShowName()) { 950a25f0a04SGreg Roach $list[] = $record; 951a25f0a04SGreg Roach } 952a25f0a04SGreg Roach } 953a25f0a04SGreg Roach return $list; 954a25f0a04SGreg Roach } 955a25f0a04SGreg Roach 956a25f0a04SGreg Roach /** 957a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 958a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 959a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 960a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 961a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 962a25f0a04SGreg Roach * 963a25f0a04SGreg Roach * @param string $event_type 964a25f0a04SGreg Roach * 965a25f0a04SGreg Roach * @return Date[] 966a25f0a04SGreg Roach */ 967a25f0a04SGreg Roach public function getAllEventDates($event_type) { 968a25f0a04SGreg Roach $dates = array(); 969a25f0a04SGreg Roach foreach ($this->getFacts($event_type) as $event) { 970a25f0a04SGreg Roach if ($event->getDate()->isOK()) { 971a25f0a04SGreg Roach $dates[] = $event->getDate(); 972a25f0a04SGreg Roach } 973a25f0a04SGreg Roach } 974a25f0a04SGreg Roach 975a25f0a04SGreg Roach return $dates; 976a25f0a04SGreg Roach } 977a25f0a04SGreg Roach 978a25f0a04SGreg Roach /** 979a25f0a04SGreg Roach * Get all the places for a particular type of event 980a25f0a04SGreg Roach * 981a25f0a04SGreg Roach * @param string $event_type 982a25f0a04SGreg Roach * 983a25f0a04SGreg Roach * @return array 984a25f0a04SGreg Roach */ 985a25f0a04SGreg Roach public function getAllEventPlaces($event_type) { 986a25f0a04SGreg Roach $places = array(); 987a25f0a04SGreg Roach foreach ($this->getFacts($event_type) as $event) { 988a25f0a04SGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->getGedcom(), $ged_places)) { 989a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 990a25f0a04SGreg Roach $places[] = $ged_place; 991a25f0a04SGreg Roach } 992a25f0a04SGreg Roach } 993a25f0a04SGreg Roach } 994a25f0a04SGreg Roach 995a25f0a04SGreg Roach return $places; 996a25f0a04SGreg Roach } 997a25f0a04SGreg Roach 998a25f0a04SGreg Roach /** 999a25f0a04SGreg Roach * Get the first (i.e. prefered) Fact for the given fact type 1000a25f0a04SGreg Roach * 1001a25f0a04SGreg Roach * @param string $tag 1002a25f0a04SGreg Roach * 1003a25f0a04SGreg Roach * @return Fact|null 1004a25f0a04SGreg Roach */ 1005a25f0a04SGreg Roach public function getFirstFact($tag) { 1006a25f0a04SGreg Roach foreach ($this->getFacts() as $fact) { 1007a25f0a04SGreg Roach if ($fact->getTag() === $tag) { 1008a25f0a04SGreg Roach return $fact; 1009a25f0a04SGreg Roach } 1010a25f0a04SGreg Roach } 1011a25f0a04SGreg Roach 1012a25f0a04SGreg Roach return null; 1013a25f0a04SGreg Roach } 1014a25f0a04SGreg Roach 1015a25f0a04SGreg Roach /** 1016a25f0a04SGreg Roach * The facts and events for this record. 1017a25f0a04SGreg Roach * 1018a25f0a04SGreg Roach * @param string $filter 1019a25f0a04SGreg Roach * @param boolean $sort 1020a25f0a04SGreg Roach * @param integer $access_level 1021a25f0a04SGreg Roach * @param boolean $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES. 1022a25f0a04SGreg Roach * 1023a25f0a04SGreg Roach * @return Fact[] 1024a25f0a04SGreg Roach */ 1025a25f0a04SGreg Roach public function getFacts($filter = null, $sort = false, $access_level = WT_USER_ACCESS_LEVEL, $override = false) { 1026a25f0a04SGreg Roach $facts = array(); 1027a25f0a04SGreg Roach if ($this->canShow($access_level) || $override) { 1028a25f0a04SGreg Roach foreach ($this->facts as $fact) { 1029a25f0a04SGreg Roach if (($filter == null || preg_match('/^' . $filter . '$/', $fact->getTag())) && $fact->canShow($access_level)) { 1030a25f0a04SGreg Roach $facts[] = $fact; 1031a25f0a04SGreg Roach } 1032a25f0a04SGreg Roach } 1033a25f0a04SGreg Roach } 1034a25f0a04SGreg Roach if ($sort) { 1035a25f0a04SGreg Roach sort_facts($facts); 1036a25f0a04SGreg Roach } 1037a25f0a04SGreg Roach return $facts; 1038a25f0a04SGreg Roach } 1039a25f0a04SGreg Roach 1040a25f0a04SGreg Roach /** 1041a25f0a04SGreg Roach * Get the last-change timestamp for this record, either as a formatted string 1042a25f0a04SGreg Roach * (for display) or as a unix timestamp (for sorting) 1043a25f0a04SGreg Roach * 1044a25f0a04SGreg Roach * @param boolean $sorting 1045a25f0a04SGreg Roach * 1046a25f0a04SGreg Roach * @return string 1047a25f0a04SGreg Roach */ 1048a25f0a04SGreg Roach public function lastChangeTimestamp($sorting = false) { 1049a25f0a04SGreg Roach $chan = $this->getFirstFact('CHAN'); 1050a25f0a04SGreg Roach 1051a25f0a04SGreg Roach if ($chan) { 1052a25f0a04SGreg Roach // The record does have a CHAN event 1053a25f0a04SGreg Roach $d = $chan->getDate()->MinDate(); 1054a25f0a04SGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->getGedcom(), $match)) { 1055a25f0a04SGreg Roach $t = mktime((int) $match[1], (int) $match[2], (int) $match[3], (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y')); 1056a25f0a04SGreg Roach } elseif (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->getGedcom(), $match)) { 1057a25f0a04SGreg Roach $t = mktime((int) $match[1], (int) $match[2], 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y')); 1058a25f0a04SGreg Roach } else { 1059a25f0a04SGreg Roach $t = mktime(0, 0, 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y')); 1060a25f0a04SGreg Roach } 1061a25f0a04SGreg Roach if ($sorting) { 1062a25f0a04SGreg Roach return $t; 1063a25f0a04SGreg Roach } else { 1064a25f0a04SGreg Roach return strip_tags(format_timestamp($t)); 1065a25f0a04SGreg Roach } 1066a25f0a04SGreg Roach } else { 1067a25f0a04SGreg Roach // The record does not have a CHAN event 1068a25f0a04SGreg Roach if ($sorting) { 1069a25f0a04SGreg Roach return '0'; 1070a25f0a04SGreg Roach } else { 1071a25f0a04SGreg Roach return ' '; 1072a25f0a04SGreg Roach } 1073a25f0a04SGreg Roach } 1074a25f0a04SGreg Roach } 1075a25f0a04SGreg Roach 1076a25f0a04SGreg Roach /** 1077a25f0a04SGreg Roach * Get the last-change user for this record 1078a25f0a04SGreg Roach * 1079a25f0a04SGreg Roach * @return string 1080a25f0a04SGreg Roach */ 1081a25f0a04SGreg Roach public function lastChangeUser() { 1082a25f0a04SGreg Roach $chan = $this->getFirstFact('CHAN'); 1083a25f0a04SGreg Roach 1084a25f0a04SGreg Roach if ($chan === null) { 1085a25f0a04SGreg Roach return I18N::translate('Unknown'); 1086a25f0a04SGreg Roach } else { 1087a25f0a04SGreg Roach $chan_user = $chan->getAttribute('_WT_USER'); 1088a25f0a04SGreg Roach if ($chan_user === null) { 1089a25f0a04SGreg Roach return I18N::translate('Unknown'); 1090a25f0a04SGreg Roach } else { 1091a25f0a04SGreg Roach return $chan_user; 1092a25f0a04SGreg Roach } 1093a25f0a04SGreg Roach } 1094a25f0a04SGreg Roach } 1095a25f0a04SGreg Roach 1096a25f0a04SGreg Roach /** 1097a25f0a04SGreg Roach * Add a new fact to this record 1098a25f0a04SGreg Roach * 1099a25f0a04SGreg Roach * @param string $gedcom 1100a25f0a04SGreg Roach * @param boolean $update_chan 1101a25f0a04SGreg Roach */ 1102a25f0a04SGreg Roach public function createFact($gedcom, $update_chan) { 1103a25f0a04SGreg Roach $this->updateFact(null, $gedcom, $update_chan); 1104a25f0a04SGreg Roach } 1105a25f0a04SGreg Roach 1106a25f0a04SGreg Roach /** 1107a25f0a04SGreg Roach * Delete a fact from this record 1108a25f0a04SGreg Roach * 1109a25f0a04SGreg Roach * @param string $fact_id 1110a25f0a04SGreg Roach * @param boolean $update_chan 1111a25f0a04SGreg Roach */ 1112a25f0a04SGreg Roach public function deleteFact($fact_id, $update_chan) { 1113a25f0a04SGreg Roach $this->updateFact($fact_id, null, $update_chan); 1114a25f0a04SGreg Roach } 1115a25f0a04SGreg Roach 1116a25f0a04SGreg Roach /** 1117a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 1118a25f0a04SGreg Roach * 1119a25f0a04SGreg Roach * @param string $fact_id 1120a25f0a04SGreg Roach * @param string $gedcom 1121a25f0a04SGreg Roach * @param boolean $update_chan 1122a25f0a04SGreg Roach * 1123a25f0a04SGreg Roach * @throws \Exception 1124a25f0a04SGreg Roach */ 1125a25f0a04SGreg Roach public function updateFact($fact_id, $gedcom, $update_chan) { 1126a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1127a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1128a25f0a04SGreg Roach $gedcom = trim($gedcom); 1129a25f0a04SGreg Roach 1130a25f0a04SGreg Roach if ($this->pending === '') { 1131a25f0a04SGreg Roach throw new \Exception('Cannot edit a deleted record'); 1132a25f0a04SGreg Roach } 1133a25f0a04SGreg Roach if ($gedcom && !preg_match('/^1 ' . WT_REGEX_TAG . '/', $gedcom)) { 1134a25f0a04SGreg Roach throw new \Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 1135a25f0a04SGreg Roach } 1136a25f0a04SGreg Roach 1137a25f0a04SGreg Roach if ($this->pending) { 1138a25f0a04SGreg Roach $old_gedcom = $this->pending; 1139a25f0a04SGreg Roach } else { 1140a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 1141a25f0a04SGreg Roach } 1142a25f0a04SGreg Roach 1143a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 1144a25f0a04SGreg Roach list($new_gedcom) = explode("\n", $old_gedcom, 2); 1145a25f0a04SGreg Roach 1146a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 1147a25f0a04SGreg Roach foreach ($this->getFacts(null, false, WT_PRIV_HIDE) as $fact) { 1148a25f0a04SGreg Roach if (!$fact->isPendingDeletion()) { 1149a25f0a04SGreg Roach if ($fact->getFactId() === $fact_id) { 1150a25f0a04SGreg Roach if ($gedcom) { 1151a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1152a25f0a04SGreg Roach } 1153a25f0a04SGreg Roach $fact_id = true; // Only replace/delete one copy of a duplicate fact 1154a25f0a04SGreg Roach } elseif ($fact->getTag() != 'CHAN' || !$update_chan) { 1155a25f0a04SGreg Roach $new_gedcom .= "\n" . $fact->getGedcom(); 1156a25f0a04SGreg Roach } 1157a25f0a04SGreg Roach } 1158a25f0a04SGreg Roach } 1159a25f0a04SGreg Roach if ($update_chan) { 1160a25f0a04SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 1161a25f0a04SGreg Roach } 1162a25f0a04SGreg Roach 1163a25f0a04SGreg Roach // Adding a new fact 1164a25f0a04SGreg Roach if (!$fact_id) { 1165a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1166a25f0a04SGreg Roach } 1167a25f0a04SGreg Roach 1168a25f0a04SGreg Roach if ($new_gedcom != $old_gedcom) { 1169a25f0a04SGreg Roach // Save the changes 1170a25f0a04SGreg Roach Database::prepare( 1171a25f0a04SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, ?, ?)" 1172a25f0a04SGreg Roach )->execute(array( 1173518bbdc1SGreg Roach $this->tree->getTreeId(), 1174a25f0a04SGreg Roach $this->xref, 1175a25f0a04SGreg Roach $old_gedcom, 1176a25f0a04SGreg Roach $new_gedcom, 1177a25f0a04SGreg Roach Auth::id() 1178a25f0a04SGreg Roach )); 1179a25f0a04SGreg Roach 1180a25f0a04SGreg Roach $this->pending = $new_gedcom; 1181a25f0a04SGreg Roach 1182a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1183518bbdc1SGreg Roach accept_all_changes($this->xref, $this->tree->getTreeId()); 1184a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 1185a25f0a04SGreg Roach $this->pending = null; 1186a25f0a04SGreg Roach } 1187a25f0a04SGreg Roach } 1188a25f0a04SGreg Roach $this->parseFacts(); 1189a25f0a04SGreg Roach } 1190a25f0a04SGreg Roach 1191a25f0a04SGreg Roach /** 1192a25f0a04SGreg Roach * Create a new record from GEDCOM data. 1193a25f0a04SGreg Roach * 1194a25f0a04SGreg Roach * @param string $gedcom 1195a25f0a04SGreg Roach * @param integer $gedcom_id 1196a25f0a04SGreg Roach * 1197a25f0a04SGreg Roach * @return GedcomRecord 1198a25f0a04SGreg Roach * @throws \Exception 1199a25f0a04SGreg Roach */ 1200a25f0a04SGreg Roach static public function createRecord($gedcom, $gedcom_id) { 1201a25f0a04SGreg Roach if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom, $match)) { 1202a25f0a04SGreg Roach $xref = $match[1]; 1203a25f0a04SGreg Roach $type = $match[2]; 1204a25f0a04SGreg Roach } else { 1205a25f0a04SGreg Roach throw new \Exception('Invalid argument to GedcomRecord::createRecord(' . $gedcom . ')'); 1206a25f0a04SGreg Roach } 1207a25f0a04SGreg Roach if (strpos("\r", $gedcom) !== false) { 1208a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1209a25f0a04SGreg Roach throw new \Exception('Evil line endings found in GedcomRecord::createRecord(' . $gedcom . ')'); 1210a25f0a04SGreg Roach } 1211a25f0a04SGreg Roach 1212a25f0a04SGreg Roach // webtrees creates XREFs containing digits. Anything else (e.g. “new”) is just a placeholder. 1213a25f0a04SGreg Roach if (!preg_match('/\d/', $xref)) { 1214a25f0a04SGreg Roach $xref = get_new_xref($type); 1215a25f0a04SGreg Roach $gedcom = preg_replace('/^0 @(' . WT_REGEX_XREF . ')@/', '0 @' . $xref . '@', $gedcom); 1216a25f0a04SGreg Roach } 1217a25f0a04SGreg Roach 1218a25f0a04SGreg Roach // Create a change record, if not already present 1219a25f0a04SGreg Roach if (!preg_match('/\n1 CHAN/', $gedcom)) { 1220a25f0a04SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 1221a25f0a04SGreg Roach } 1222a25f0a04SGreg Roach 1223a25f0a04SGreg Roach // Create a pending change 1224a25f0a04SGreg Roach Database::prepare( 1225a25f0a04SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)" 1226a25f0a04SGreg Roach )->execute(array( 1227a25f0a04SGreg Roach $gedcom_id, 1228a25f0a04SGreg Roach $xref, 1229a25f0a04SGreg Roach $gedcom, 1230a25f0a04SGreg Roach Auth::id() 1231a25f0a04SGreg Roach )); 1232a25f0a04SGreg Roach 1233a25f0a04SGreg Roach // Accept this pending change 1234a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1235a25f0a04SGreg Roach accept_all_changes($xref, $gedcom_id); 1236a25f0a04SGreg Roach } 1237a25f0a04SGreg Roach 1238a25f0a04SGreg Roach // Clear this record from the cache 1239a25f0a04SGreg Roach self::$pending_record_cache = null; 1240a25f0a04SGreg Roach 1241a25f0a04SGreg Roach Log::addEditLog('Create: ' . $type . ' ' . $xref); 1242a25f0a04SGreg Roach 1243a25f0a04SGreg Roach // Return the newly created record 1244a25f0a04SGreg Roach return GedcomRecord::getInstance($xref); 1245a25f0a04SGreg Roach } 1246a25f0a04SGreg Roach 1247a25f0a04SGreg Roach /** 1248a25f0a04SGreg Roach * Update this record 1249a25f0a04SGreg Roach * 1250a25f0a04SGreg Roach * @param string $gedcom 1251a25f0a04SGreg Roach * @param boolean $update_chan 1252a25f0a04SGreg Roach */ 1253a25f0a04SGreg Roach public function updateRecord($gedcom, $update_chan) { 1254a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1255a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1256a25f0a04SGreg Roach $gedcom = trim($gedcom); 1257a25f0a04SGreg Roach 1258a25f0a04SGreg Roach // Update the CHAN record 1259a25f0a04SGreg Roach if ($update_chan) { 1260a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 1261a25f0a04SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); 1262a25f0a04SGreg Roach } 1263a25f0a04SGreg Roach 1264a25f0a04SGreg Roach // Create a pending change 1265a25f0a04SGreg Roach Database::prepare( 1266a25f0a04SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, ?, ?)" 1267a25f0a04SGreg Roach )->execute(array( 1268518bbdc1SGreg Roach $this->tree->getTreeId(), 1269a25f0a04SGreg Roach $this->xref, 1270a25f0a04SGreg Roach $this->getGedcom(), 1271a25f0a04SGreg Roach $gedcom, 1272a25f0a04SGreg Roach Auth::id() 1273a25f0a04SGreg Roach )); 1274a25f0a04SGreg Roach 1275a25f0a04SGreg Roach // Clear the cache 1276a25f0a04SGreg Roach $this->pending = $gedcom; 1277a25f0a04SGreg Roach 1278a25f0a04SGreg Roach // Accept this pending change 1279a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1280518bbdc1SGreg Roach accept_all_changes($this->xref, $this->tree->getTreeId()); 1281a25f0a04SGreg Roach $this->gedcom = $gedcom; 1282a25f0a04SGreg Roach $this->pending = null; 1283a25f0a04SGreg Roach } 1284a25f0a04SGreg Roach 1285a25f0a04SGreg Roach $this->parseFacts(); 1286a25f0a04SGreg Roach 1287a25f0a04SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref); 1288a25f0a04SGreg Roach } 1289a25f0a04SGreg Roach 1290a25f0a04SGreg Roach /** 1291a25f0a04SGreg Roach * Delete this record 1292a25f0a04SGreg Roach */ 1293a25f0a04SGreg Roach public function deleteRecord() { 1294a25f0a04SGreg Roach // Create a pending change 1295a25f0a04SGreg Roach Database::prepare( 1296a25f0a04SGreg Roach "INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, ?, '', ?)" 1297a25f0a04SGreg Roach )->execute(array( 1298518bbdc1SGreg Roach $this->tree->getTreeId(), 1299a25f0a04SGreg Roach $this->xref, 1300a25f0a04SGreg Roach $this->getGedcom(), 1301a25f0a04SGreg Roach Auth::id(), 1302a25f0a04SGreg Roach )); 1303a25f0a04SGreg Roach 1304a25f0a04SGreg Roach // Accept this pending change 1305a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1306518bbdc1SGreg Roach accept_all_changes($this->xref, $this->tree->getTreeId()); 1307a25f0a04SGreg Roach } 1308a25f0a04SGreg Roach 1309a25f0a04SGreg Roach // Clear the cache 1310a25f0a04SGreg Roach self::$gedcom_record_cache = null; 1311a25f0a04SGreg Roach self::$pending_record_cache = null; 1312a25f0a04SGreg Roach 1313a25f0a04SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref); 1314a25f0a04SGreg Roach } 1315a25f0a04SGreg Roach 1316a25f0a04SGreg Roach /** 1317a25f0a04SGreg Roach * Remove all links from this record to $xref 1318a25f0a04SGreg Roach * 1319a25f0a04SGreg Roach * @param string $xref 1320a25f0a04SGreg Roach * @param boolean $update_chan 1321a25f0a04SGreg Roach */ 1322a25f0a04SGreg Roach public function removeLinks($xref, $update_chan) { 1323a25f0a04SGreg Roach $value = '@' . $xref . '@'; 1324a25f0a04SGreg Roach 1325a25f0a04SGreg Roach foreach ($this->getFacts() as $fact) { 1326a25f0a04SGreg Roach if ($fact->getValue() == $value) { 1327a25f0a04SGreg Roach $this->deleteFact($fact->getFactId(), $update_chan); 1328a25f0a04SGreg Roach } elseif (preg_match_all('/\n(\d) ' . WT_REGEX_TAG . ' ' . $value . '/', $fact->getGedcom(), $matches, PREG_SET_ORDER)) { 1329a25f0a04SGreg Roach $gedcom = $fact->getGedcom(); 1330a25f0a04SGreg Roach foreach ($matches as $match) { 1331a25f0a04SGreg Roach $next_level = $match[1] + 1; 1332a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 1333a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 1334a25f0a04SGreg Roach } 1335a25f0a04SGreg Roach $this->updateFact($fact->getFactId(), $gedcom, $update_chan); 1336a25f0a04SGreg Roach } 1337a25f0a04SGreg Roach } 1338a25f0a04SGreg Roach } 1339a25f0a04SGreg Roach} 1340