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; 1976692c8bSGreg Roach 20886b77daSGreg Roachuse Closure; 217e96c925SGreg Roachuse Exception; 223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate; 243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 253d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 26bf4eb542SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 27ba1c12e8SGreg Roachuse Illuminate\Database\Query\JoinClause; 28*39ca88baSGreg Roachuse Illuminate\Support\Collection; 2979529c87SGreg Roachuse stdClass; 30a25f0a04SGreg Roach 31a25f0a04SGreg Roach/** 3276692c8bSGreg Roach * A GEDCOM object. 33a25f0a04SGreg Roach */ 34c1010edaSGreg Roachclass GedcomRecord 35c1010edaSGreg Roach{ 3616d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 3716d6367aSGreg Roach 3816d6367aSGreg Roach protected const ROUTE_NAME = 'record'; 39a25f0a04SGreg Roach 40a25f0a04SGreg Roach /** @var string The record identifier */ 41a25f0a04SGreg Roach protected $xref; 42a25f0a04SGreg Roach 43000959d9SGreg Roach /** @var Tree The family tree to which this record belongs */ 44000959d9SGreg Roach protected $tree; 45a25f0a04SGreg Roach 46a25f0a04SGreg Roach /** @var string GEDCOM data (before any pending edits) */ 47a25f0a04SGreg Roach protected $gedcom; 48a25f0a04SGreg Roach 49a25f0a04SGreg Roach /** @var string|null GEDCOM data (after any pending edits) */ 50a25f0a04SGreg Roach protected $pending; 51a25f0a04SGreg Roach 52a25f0a04SGreg Roach /** @var Fact[] facts extracted from $gedcom/$pending */ 53a25f0a04SGreg Roach protected $facts; 54a25f0a04SGreg Roach 55a25f0a04SGreg Roach /** @var string[][] All the names of this individual */ 56bdb3725aSGreg Roach protected $getAllNames; 57a25f0a04SGreg Roach 58d17d7b9eSGreg Roach /** @var int|null Cached result */ 59bdb3725aSGreg Roach protected $getPrimaryName; 60a25f0a04SGreg Roach 61d17d7b9eSGreg Roach /** @var int|null Cached result */ 62bdb3725aSGreg Roach protected $getSecondaryName; 63a25f0a04SGreg Roach 6476692c8bSGreg Roach /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */ 65bec87e94SGreg Roach public static $gedcom_record_cache; 661ae87ce5SGreg Roach 6779529c87SGreg Roach /** @var stdClass[][] Fetch all pending edits in one database query */ 68bec87e94SGreg Roach public static $pending_record_cache; 69a25f0a04SGreg Roach 70a25f0a04SGreg Roach /** 71a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 72a25f0a04SGreg Roach * 73a25f0a04SGreg Roach * @param string $xref 74a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 75a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 76a25f0a04SGreg Roach * empty string for records with pending deletions 7724ec66ceSGreg Roach * @param Tree $tree 78a25f0a04SGreg Roach */ 7976f666f4SGreg Roach public function __construct(string $xref, string $gedcom, $pending, Tree $tree) 80c1010edaSGreg Roach { 81a25f0a04SGreg Roach $this->xref = $xref; 82a25f0a04SGreg Roach $this->gedcom = $gedcom; 83a25f0a04SGreg Roach $this->pending = $pending; 8424ec66ceSGreg Roach $this->tree = $tree; 85a25f0a04SGreg Roach 86a25f0a04SGreg Roach $this->parseFacts(); 87a25f0a04SGreg Roach } 88a25f0a04SGreg Roach 89a25f0a04SGreg Roach /** 90886b77daSGreg Roach * A closure which will create a record from a database row. 91886b77daSGreg Roach * 92886b77daSGreg Roach * @return Closure 93886b77daSGreg Roach */ 94c0804649SGreg Roach public static function rowMapper(): Closure 95886b77daSGreg Roach { 96c0804649SGreg Roach return function (stdClass $row): GedcomRecord { 97e3bddf11SGreg Roach return GedcomRecord::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom); 98886b77daSGreg Roach }; 99886b77daSGreg Roach } 100886b77daSGreg Roach 101886b77daSGreg Roach /** 102886b77daSGreg Roach * A closure which will filter out private records. 103886b77daSGreg Roach * 104886b77daSGreg Roach * @return Closure 105886b77daSGreg Roach */ 1064146fabcSGreg Roach public static function accessFilter(): Closure 107886b77daSGreg Roach { 108886b77daSGreg Roach return function (GedcomRecord $record): bool { 109886b77daSGreg Roach return $record->canShow(); 110886b77daSGreg Roach }; 111886b77daSGreg Roach } 112886b77daSGreg Roach 113886b77daSGreg Roach /** 114c156e8f5SGreg Roach * A closure which will compare records by name. 115c156e8f5SGreg Roach * 116c156e8f5SGreg Roach * @return Closure 117c156e8f5SGreg Roach */ 118c156e8f5SGreg Roach public static function nameComparator(): Closure 119c156e8f5SGreg Roach { 120c156e8f5SGreg Roach return function (GedcomRecord $x, GedcomRecord $y): int { 121c156e8f5SGreg Roach if ($x->canShowName()) { 122c156e8f5SGreg Roach if ($y->canShowName()) { 123*39ca88baSGreg Roach return I18N::strcasecmp($x->sortName(), $y->sortName()); 124c156e8f5SGreg Roach } 125c156e8f5SGreg Roach 126c156e8f5SGreg Roach return -1; // only $y is private 127c156e8f5SGreg Roach } 128c156e8f5SGreg Roach 129c156e8f5SGreg Roach if ($y->canShowName()) { 130c156e8f5SGreg Roach return 1; // only $x is private 131c156e8f5SGreg Roach } 132c156e8f5SGreg Roach 133c156e8f5SGreg Roach return 0; // both $x and $y private 134c156e8f5SGreg Roach }; 135c156e8f5SGreg Roach } 136c156e8f5SGreg Roach 137c156e8f5SGreg Roach /** 138c156e8f5SGreg Roach * A closure which will compare records by change time. 139c156e8f5SGreg Roach * 140c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 141c156e8f5SGreg Roach * 142c156e8f5SGreg Roach * @return Closure 143c156e8f5SGreg Roach */ 144c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 145c156e8f5SGreg Roach { 146c156e8f5SGreg Roach return function (GedcomRecord $x, GedcomRecord $y) use ($direction): int { 147c156e8f5SGreg Roach return $direction * ($x->lastChangeTimestamp(true) <=> $y->lastChangeTimestamp(true)); 148c156e8f5SGreg Roach }; 149c156e8f5SGreg Roach } 150c156e8f5SGreg Roach 151c156e8f5SGreg Roach /** 152a25f0a04SGreg Roach * Split the record into facts 1537e96c925SGreg Roach * 1547e96c925SGreg Roach * @return void 155a25f0a04SGreg Roach */ 156c1010edaSGreg Roach private function parseFacts() 157c1010edaSGreg Roach { 158a25f0a04SGreg Roach // Split the record into facts 159a25f0a04SGreg Roach if ($this->gedcom) { 160a25f0a04SGreg Roach $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom); 161a25f0a04SGreg Roach array_shift($gedcom_facts); 162a25f0a04SGreg Roach } else { 16313abd6f3SGreg Roach $gedcom_facts = []; 164a25f0a04SGreg Roach } 165a25f0a04SGreg Roach if ($this->pending) { 166a25f0a04SGreg Roach $pending_facts = preg_split('/\n(?=1)/s', $this->pending); 167a25f0a04SGreg Roach array_shift($pending_facts); 168a25f0a04SGreg Roach } else { 16913abd6f3SGreg Roach $pending_facts = []; 170a25f0a04SGreg Roach } 171a25f0a04SGreg Roach 17213abd6f3SGreg Roach $this->facts = []; 173a25f0a04SGreg Roach 174a25f0a04SGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 175a25f0a04SGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 176a25f0a04SGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts)) { 177a25f0a04SGreg Roach $fact->setPendingDeletion(); 178a25f0a04SGreg Roach } 179a25f0a04SGreg Roach $this->facts[] = $fact; 180a25f0a04SGreg Roach } 181a25f0a04SGreg Roach foreach ($pending_facts as $pending_fact) { 182a25f0a04SGreg Roach if (!in_array($pending_fact, $gedcom_facts)) { 183a25f0a04SGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 184a25f0a04SGreg Roach $fact->setPendingAddition(); 185a25f0a04SGreg Roach $this->facts[] = $fact; 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach } 188a25f0a04SGreg Roach } 189a25f0a04SGreg Roach 190a25f0a04SGreg Roach /** 191a25f0a04SGreg Roach * Get an instance of a GedcomRecord object. For single records, 192a25f0a04SGreg Roach * we just receive the XREF. For bulk records (such as lists 193a25f0a04SGreg Roach * and search results) we can receive the GEDCOM data as well. 194a25f0a04SGreg Roach * 195a25f0a04SGreg Roach * @param string $xref 19624ec66ceSGreg Roach * @param Tree $tree 197a25f0a04SGreg Roach * @param string|null $gedcom 198a25f0a04SGreg Roach * 1997e96c925SGreg Roach * @throws Exception 20084658595SGreg Roach * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null 201a25f0a04SGreg Roach */ 20276f666f4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 203c1010edaSGreg Roach { 20472cf66d4SGreg Roach $tree_id = $tree->id(); 20524ec66ceSGreg Roach 206e71ef9d2SGreg Roach // Is this record already in the cache? 20724ec66ceSGreg Roach if (isset(self::$gedcom_record_cache[$xref][$tree_id])) { 208e71ef9d2SGreg Roach return self::$gedcom_record_cache[$xref][$tree_id]; 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach // Do we need to fetch the record from the database? 212a25f0a04SGreg Roach if ($gedcom === null) { 21324ec66ceSGreg Roach $gedcom = static::fetchGedcomRecord($xref, $tree_id); 214a25f0a04SGreg Roach } 215a25f0a04SGreg Roach 216a25f0a04SGreg Roach // If we can edit, then we also need to be able to see pending records. 21794075df0SGreg Roach if (Auth::isEditor($tree)) { 21824ec66ceSGreg Roach if (!isset(self::$pending_record_cache[$tree_id])) { 219a25f0a04SGreg Roach // Fetch all pending records in one database query 22013abd6f3SGreg Roach self::$pending_record_cache[$tree_id] = []; 22185fc8064SGreg Roach $rows = DB::table('change') 22285fc8064SGreg Roach ->where('gedcom_id', '=', $tree_id) 22385fc8064SGreg Roach ->where('status', '=', 'pending') 22485fc8064SGreg Roach ->orderBy('change_id') 22585fc8064SGreg Roach ->select(['xref', 'new_gedcom']) 22685fc8064SGreg Roach ->get(); 227d17d7b9eSGreg Roach 228a25f0a04SGreg Roach foreach ($rows as $row) { 22924ec66ceSGreg Roach self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom; 230a25f0a04SGreg Roach } 231a25f0a04SGreg Roach } 232a25f0a04SGreg Roach 233d17d7b9eSGreg Roach $pending = self::$pending_record_cache[$tree_id][$xref] ?? null; 234a25f0a04SGreg Roach } else { 235a25f0a04SGreg Roach // There are no pending changes for this record 236a25f0a04SGreg Roach $pending = null; 237a25f0a04SGreg Roach } 238a25f0a04SGreg Roach 239a25f0a04SGreg Roach // No such record exists 240a25f0a04SGreg Roach if ($gedcom === null && $pending === null) { 241a25f0a04SGreg Roach return null; 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach 244fc3ccce4SGreg Roach // No such record, but a pending creation exists 245fc3ccce4SGreg Roach if ($gedcom === null) { 246fc3ccce4SGreg Roach $gedcom = ''; 247fc3ccce4SGreg Roach } 248fc3ccce4SGreg Roach 249a25f0a04SGreg Roach // Create the object 2508d0ebef0SGreg Roach if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ (' . Gedcom::REGEX_TAG . ')/', $gedcom . $pending, $match)) { 251a25f0a04SGreg Roach $xref = $match[1]; // Collation - we may have requested I123 and found i123 252a25f0a04SGreg Roach $type = $match[2]; 253a25f0a04SGreg Roach } elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) { 254a25f0a04SGreg Roach $xref = $match[1]; 255a25f0a04SGreg Roach $type = $match[1]; 256a25f0a04SGreg Roach } elseif ($gedcom . $pending) { 2577e96c925SGreg Roach throw new Exception('Unrecognized GEDCOM record: ' . $gedcom); 258a25f0a04SGreg Roach } else { 259a25f0a04SGreg Roach // A record with both pending creation and pending deletion 260a25f0a04SGreg Roach $type = static::RECORD_TYPE; 261a25f0a04SGreg Roach } 262a25f0a04SGreg Roach 263a25f0a04SGreg Roach switch ($type) { 264a25f0a04SGreg Roach case 'INDI': 26524ec66ceSGreg Roach $record = new Individual($xref, $gedcom, $pending, $tree); 266a25f0a04SGreg Roach break; 267a25f0a04SGreg Roach case 'FAM': 26824ec66ceSGreg Roach $record = new Family($xref, $gedcom, $pending, $tree); 269a25f0a04SGreg Roach break; 270a25f0a04SGreg Roach case 'SOUR': 27124ec66ceSGreg Roach $record = new Source($xref, $gedcom, $pending, $tree); 272a25f0a04SGreg Roach break; 273a25f0a04SGreg Roach case 'OBJE': 27424ec66ceSGreg Roach $record = new Media($xref, $gedcom, $pending, $tree); 275a25f0a04SGreg Roach break; 276a25f0a04SGreg Roach case 'REPO': 27724ec66ceSGreg Roach $record = new Repository($xref, $gedcom, $pending, $tree); 278a25f0a04SGreg Roach break; 279a25f0a04SGreg Roach case 'NOTE': 28024ec66ceSGreg Roach $record = new Note($xref, $gedcom, $pending, $tree); 281a25f0a04SGreg Roach break; 282a25f0a04SGreg Roach default: 28306ef8e02SGreg Roach $record = new self($xref, $gedcom, $pending, $tree); 284a25f0a04SGreg Roach break; 285a25f0a04SGreg Roach } 286a25f0a04SGreg Roach 287a25f0a04SGreg Roach // Store it in the cache 28824ec66ceSGreg Roach self::$gedcom_record_cache[$xref][$tree_id] = $record; 289a25f0a04SGreg Roach 290a25f0a04SGreg Roach return $record; 291a25f0a04SGreg Roach } 292a25f0a04SGreg Roach 293a25f0a04SGreg Roach /** 294a25f0a04SGreg Roach * Fetch data from the database 295a25f0a04SGreg Roach * 296a25f0a04SGreg Roach * @param string $xref 297cbc1590aSGreg Roach * @param int $tree_id 298a25f0a04SGreg Roach * 299a25f0a04SGreg Roach * @return null|string 300a25f0a04SGreg Roach */ 30176f666f4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id) 302c1010edaSGreg Roach { 303a25f0a04SGreg Roach // We don't know what type of object this is. Try each one in turn. 30464d9078aSGreg Roach $data = Individual::fetchGedcomRecord($xref, $tree_id); 30585fc8064SGreg Roach if ($data !== null) { 306a25f0a04SGreg Roach return $data; 307a25f0a04SGreg Roach } 30864d9078aSGreg Roach $data = Family::fetchGedcomRecord($xref, $tree_id); 30985fc8064SGreg Roach if ($data !== null) { 310a25f0a04SGreg Roach return $data; 311a25f0a04SGreg Roach } 31264d9078aSGreg Roach $data = Source::fetchGedcomRecord($xref, $tree_id); 31385fc8064SGreg Roach if ($data !== null) { 314a25f0a04SGreg Roach return $data; 315a25f0a04SGreg Roach } 31664d9078aSGreg Roach $data = Repository::fetchGedcomRecord($xref, $tree_id); 31785fc8064SGreg Roach if ($data !== null) { 318a25f0a04SGreg Roach return $data; 319a25f0a04SGreg Roach } 32064d9078aSGreg Roach $data = Media::fetchGedcomRecord($xref, $tree_id); 32185fc8064SGreg Roach if ($data !== null) { 322a25f0a04SGreg Roach return $data; 323a25f0a04SGreg Roach } 32464d9078aSGreg Roach $data = Note::fetchGedcomRecord($xref, $tree_id); 32585fc8064SGreg Roach if ($data !== null) { 326a25f0a04SGreg Roach return $data; 327a25f0a04SGreg Roach } 328c1010edaSGreg Roach 329a25f0a04SGreg Roach // Some other type of record... 330d09b6323SGreg Roach return DB::table('other') 33185fc8064SGreg Roach ->where('o_file', '=', $tree_id) 33285fc8064SGreg Roach ->where('o_id', '=', $xref) 33385fc8064SGreg Roach ->value('o_gedcom'); 334a25f0a04SGreg Roach } 335a25f0a04SGreg Roach 336a25f0a04SGreg Roach /** 337a25f0a04SGreg Roach * Get the XREF for this record 338a25f0a04SGreg Roach * 339a25f0a04SGreg Roach * @return string 340a25f0a04SGreg Roach */ 341c0935879SGreg Roach public function xref(): string 342c1010edaSGreg Roach { 343a25f0a04SGreg Roach return $this->xref; 344a25f0a04SGreg Roach } 345a25f0a04SGreg Roach 346a25f0a04SGreg Roach /** 347000959d9SGreg Roach * Get the tree to which this record belongs 348000959d9SGreg Roach * 349000959d9SGreg Roach * @return Tree 350000959d9SGreg Roach */ 351f4afa648SGreg Roach public function tree(): Tree 352c1010edaSGreg Roach { 353518bbdc1SGreg Roach return $this->tree; 354000959d9SGreg Roach } 355000959d9SGreg Roach 356000959d9SGreg Roach /** 357a25f0a04SGreg Roach * Application code should access data via Fact objects. 358a25f0a04SGreg Roach * This function exists to support old code. 359a25f0a04SGreg Roach * 360a25f0a04SGreg Roach * @return string 361a25f0a04SGreg Roach */ 3627d0db648SGreg Roach public function gedcom() 363c1010edaSGreg Roach { 364b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach 367a25f0a04SGreg Roach /** 368a25f0a04SGreg Roach * Does this record have a pending change? 369a25f0a04SGreg Roach * 370cbc1590aSGreg Roach * @return bool 371a25f0a04SGreg Roach */ 3728f53f488SRico Sonntag public function isPendingAddition(): bool 373c1010edaSGreg Roach { 374a25f0a04SGreg Roach return $this->pending !== null; 375a25f0a04SGreg Roach } 376a25f0a04SGreg Roach 377a25f0a04SGreg Roach /** 378a25f0a04SGreg Roach * Does this record have a pending deletion? 379a25f0a04SGreg Roach * 380cbc1590aSGreg Roach * @return bool 381a25f0a04SGreg Roach */ 3828f53f488SRico Sonntag public function isPendingDeletion(): bool 383c1010edaSGreg Roach { 384a25f0a04SGreg Roach return $this->pending === ''; 385a25f0a04SGreg Roach } 386a25f0a04SGreg Roach 387a25f0a04SGreg Roach /** 388225e381fSGreg Roach * Generate a URL to this record. 389a25f0a04SGreg Roach * 390a25f0a04SGreg Roach * @return string 391a25f0a04SGreg Roach */ 3928f53f488SRico Sonntag public function url(): string 393c1010edaSGreg Roach { 394225e381fSGreg Roach return route(static::ROUTE_NAME, [ 395c0935879SGreg Roach 'xref' => $this->xref(), 396aa6f03bbSGreg Roach 'ged' => $this->tree->name(), 397225e381fSGreg Roach ]); 398a25f0a04SGreg Roach } 399a25f0a04SGreg Roach 400a25f0a04SGreg Roach /** 401a25f0a04SGreg Roach * Work out whether this record can be shown to a user with a given access level 402a25f0a04SGreg Roach * 403cbc1590aSGreg Roach * @param int $access_level 404a25f0a04SGreg Roach * 405cbc1590aSGreg Roach * @return bool 406a25f0a04SGreg Roach */ 40776f666f4SGreg Roach private function canShowRecord(int $access_level): bool 408c1010edaSGreg Roach { 409a25f0a04SGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 410518bbdc1SGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 411a25f0a04SGreg Roach return true; 412a25f0a04SGreg Roach } 413a25f0a04SGreg Roach 414a25f0a04SGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 415c0935879SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), 'gedcomid') && $access_level === Auth::accessLevel($this->tree)) { 416a25f0a04SGreg Roach return true; 417a25f0a04SGreg Roach } 418a25f0a04SGreg Roach 419a25f0a04SGreg Roach // Does this record have a RESN? 42020ff464cSGreg Roach if (strpos($this->gedcom, "\n1 RESN confidential") !== false) { 4214b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 422a25f0a04SGreg Roach } 42320ff464cSGreg Roach if (strpos($this->gedcom, "\n1 RESN privacy") !== false) { 4244b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 425a25f0a04SGreg Roach } 42620ff464cSGreg Roach if (strpos($this->gedcom, "\n1 RESN none") !== false) { 427a25f0a04SGreg Roach return true; 428a25f0a04SGreg Roach } 429a25f0a04SGreg Roach 430a25f0a04SGreg Roach // Does this record have a default RESN? 431518bbdc1SGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 432c0935879SGreg Roach if (isset($individual_privacy[$this->xref()])) { 433c0935879SGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 434a25f0a04SGreg Roach } 435a25f0a04SGreg Roach 436a25f0a04SGreg Roach // Privacy rules do not apply to admins 4374b9ff166SGreg Roach if (Auth::PRIV_NONE >= $access_level) { 438a25f0a04SGreg Roach return true; 439a25f0a04SGreg Roach } 440a25f0a04SGreg Roach 441a25f0a04SGreg Roach // Different types of record have different privacy rules 442a25f0a04SGreg Roach return $this->canShowByType($access_level); 443a25f0a04SGreg Roach } 444a25f0a04SGreg Roach 445a25f0a04SGreg Roach /** 446a25f0a04SGreg Roach * Each object type may have its own special rules, and re-implement this function. 447a25f0a04SGreg Roach * 448cbc1590aSGreg Roach * @param int $access_level 449a25f0a04SGreg Roach * 450cbc1590aSGreg Roach * @return bool 451a25f0a04SGreg Roach */ 45235584196SGreg Roach protected function canShowByType(int $access_level): bool 453c1010edaSGreg Roach { 454518bbdc1SGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 455a25f0a04SGreg Roach 456518bbdc1SGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 457a25f0a04SGreg Roach // Restriction found 458518bbdc1SGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 459b2ce94c6SRico Sonntag } 460b2ce94c6SRico Sonntag 461a25f0a04SGreg Roach // No restriction found - must be public: 462a25f0a04SGreg Roach return true; 463a25f0a04SGreg Roach } 464a25f0a04SGreg Roach 465a25f0a04SGreg Roach /** 466a25f0a04SGreg Roach * Can the details of this record be shown? 467a25f0a04SGreg Roach * 468cbc1590aSGreg Roach * @param int|null $access_level 469a25f0a04SGreg Roach * 470cbc1590aSGreg Roach * @return bool 471a25f0a04SGreg Roach */ 47235584196SGreg Roach public function canShow(int $access_level = null): bool 473c1010edaSGreg Roach { 474f0b9c048SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 4754b9ff166SGreg Roach 476a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 477a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 478f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 479a25f0a04SGreg Roach return true; 480a25f0a04SGreg Roach } 481f0b9c048SGreg Roach 482f0b9c048SGreg Roach $cache_key = 'canShow' . $this->xref . ':' . $this->tree->id() . ':' . $access_level; 483f0b9c048SGreg Roach 484f0b9c048SGreg Roach return app('cache.array')->rememberForever($cache_key, function () use ($access_level) { 485f0b9c048SGreg Roach return $this->canShowRecord($access_level); 486f0b9c048SGreg Roach }); 487a25f0a04SGreg Roach } 488a25f0a04SGreg Roach 489a25f0a04SGreg Roach /** 490a25f0a04SGreg Roach * Can the name of this record be shown? 491a25f0a04SGreg Roach * 492cbc1590aSGreg Roach * @param int|null $access_level 493a25f0a04SGreg Roach * 494cbc1590aSGreg Roach * @return bool 495a25f0a04SGreg Roach */ 49676f666f4SGreg Roach public function canShowName(int $access_level = null): bool 497c1010edaSGreg Roach { 498a25f0a04SGreg Roach return $this->canShow($access_level); 499a25f0a04SGreg Roach } 500a25f0a04SGreg Roach 501a25f0a04SGreg Roach /** 502a25f0a04SGreg Roach * Can we edit this record? 503a25f0a04SGreg Roach * 504cbc1590aSGreg Roach * @return bool 505a25f0a04SGreg Roach */ 5068f53f488SRico Sonntag public function canEdit(): bool 507c1010edaSGreg Roach { 5084b9ff166SGreg Roach return Auth::isManager($this->tree) || Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false; 509a25f0a04SGreg Roach } 510a25f0a04SGreg Roach 511a25f0a04SGreg Roach /** 512a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 513a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 514a25f0a04SGreg Roach * 515cbc1590aSGreg Roach * @param int $access_level 516a25f0a04SGreg Roach * 517a25f0a04SGreg Roach * @return string 518a25f0a04SGreg Roach */ 51976f666f4SGreg Roach public function privatizeGedcom(int $access_level) 520c1010edaSGreg Roach { 5214b9ff166SGreg Roach if ($access_level == Auth::PRIV_HIDE) { 522a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 523a25f0a04SGreg Roach return $this->gedcom; 524b2ce94c6SRico Sonntag } 525b2ce94c6SRico Sonntag 526b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 527a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 528a25f0a04SGreg Roach 529a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 53065e02381SGreg Roach [$gedrec] = explode("\n", $this->gedcom, 2); 531a25f0a04SGreg Roach 532a25f0a04SGreg Roach // Check each of the facts for access 5338d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 534138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 535a25f0a04SGreg Roach } 536cbc1590aSGreg Roach 537a25f0a04SGreg Roach return $gedrec; 538b2ce94c6SRico Sonntag } 539b2ce94c6SRico Sonntag 540a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 541a25f0a04SGreg Roach // limited data, such as links to other records. 542a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 543a25f0a04SGreg Roach } 544a25f0a04SGreg Roach 545a25f0a04SGreg Roach /** 546a25f0a04SGreg Roach * Generate a private version of this record 547a25f0a04SGreg Roach * 548cbc1590aSGreg Roach * @param int $access_level 549a25f0a04SGreg Roach * 550a25f0a04SGreg Roach * @return string 551a25f0a04SGreg Roach */ 55276f666f4SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 553c1010edaSGreg Roach { 554a25f0a04SGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private'); 555a25f0a04SGreg Roach } 556a25f0a04SGreg Roach 557a25f0a04SGreg Roach /** 558a25f0a04SGreg Roach * Convert a name record into sortable and full/display versions. This default 559a25f0a04SGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 560a25f0a04SGreg Roach * 561a25f0a04SGreg Roach * @param string $type 562a25f0a04SGreg Roach * @param string $value 563a25f0a04SGreg Roach * @param string $gedcom 5647e96c925SGreg Roach * 5657e96c925SGreg Roach * @return void 566a25f0a04SGreg Roach */ 56776f666f4SGreg Roach protected function addName(string $type, string $value, string $gedcom) 568c1010edaSGreg Roach { 569bdb3725aSGreg Roach $this->getAllNames[] = [ 570a25f0a04SGreg Roach 'type' => $type, 5717e96c925SGreg Roach 'sort' => preg_replace_callback('/([0-9]+)/', function (array $matches): string { 5728d68cabeSGreg Roach return str_pad($matches[0], 10, '0', STR_PAD_LEFT); 5738d68cabeSGreg Roach }, $value), 574c1010edaSGreg Roach 'full' => '<span dir="auto">' . e($value) . '</span>', 575c1010edaSGreg Roach // This is used for display 576c1010edaSGreg Roach 'fullNN' => $value, 577c1010edaSGreg Roach // This goes into the database 57813abd6f3SGreg Roach ]; 579a25f0a04SGreg Roach } 580a25f0a04SGreg Roach 581a25f0a04SGreg Roach /** 582a25f0a04SGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 583a25f0a04SGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 584a25f0a04SGreg Roach * Parameters: the level 1 fact containing the name. 585a25f0a04SGreg Roach * Return value: an array of name structures, each containing 586a25f0a04SGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 587a25f0a04SGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 588a25f0a04SGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 589a25f0a04SGreg Roach * 590cbc1590aSGreg Roach * @param int $level 591a25f0a04SGreg Roach * @param string $fact_type 592*39ca88baSGreg Roach * @param Collection|Fact[] $facts 5937e96c925SGreg Roach * 5947e96c925SGreg Roach * @return void 595a25f0a04SGreg Roach */ 596*39ca88baSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts) 597c1010edaSGreg Roach { 598a25f0a04SGreg Roach $sublevel = $level + 1; 599a25f0a04SGreg Roach $subsublevel = $sublevel + 1; 600a25f0a04SGreg Roach foreach ($facts as $fact) { 601138ca96cSGreg Roach if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) { 602a25f0a04SGreg Roach foreach ($matches as $match) { 603a25f0a04SGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 604a25f0a04SGreg Roach if ($match[1] == 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) { 605138ca96cSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 606a25f0a04SGreg Roach } else { 607138ca96cSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 608a25f0a04SGreg Roach } 609a25f0a04SGreg Roach if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { 610a25f0a04SGreg Roach foreach ($submatches as $submatch) { 611a25f0a04SGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 612a25f0a04SGreg Roach } 613a25f0a04SGreg Roach } 614a25f0a04SGreg Roach } 615a25f0a04SGreg Roach } 616a25f0a04SGreg Roach } 617a25f0a04SGreg Roach } 618a25f0a04SGreg Roach 619a25f0a04SGreg Roach /** 620a25f0a04SGreg Roach * Default for "other" object types 621c7ff4153SGreg Roach * 622c7ff4153SGreg Roach * @return void 623a25f0a04SGreg Roach */ 624c1010edaSGreg Roach public function extractNames() 625c1010edaSGreg Roach { 62676f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 627a25f0a04SGreg Roach } 628a25f0a04SGreg Roach 629a25f0a04SGreg Roach /** 630a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 631a25f0a04SGreg Roach * 632a25f0a04SGreg Roach * @return string[][] 633a25f0a04SGreg Roach */ 6348f53f488SRico Sonntag public function getAllNames(): array 635c1010edaSGreg Roach { 636bdb3725aSGreg Roach if ($this->getAllNames === null) { 637bdb3725aSGreg Roach $this->getAllNames = []; 638a25f0a04SGreg Roach if ($this->canShowName()) { 639a25f0a04SGreg Roach // Ask the record to extract its names 640a25f0a04SGreg Roach $this->extractNames(); 641a25f0a04SGreg Roach // No name found? Use a fallback. 642bdb3725aSGreg Roach if (!$this->getAllNames) { 643db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 644a25f0a04SGreg Roach } 645a25f0a04SGreg Roach } else { 646db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 647a25f0a04SGreg Roach } 648a25f0a04SGreg Roach } 649cbc1590aSGreg Roach 650bdb3725aSGreg Roach return $this->getAllNames; 651a25f0a04SGreg Roach } 652a25f0a04SGreg Roach 653a25f0a04SGreg Roach /** 654a25f0a04SGreg Roach * If this object has no name, what do we call it? 655a25f0a04SGreg Roach * 656a25f0a04SGreg Roach * @return string 657a25f0a04SGreg Roach */ 6588f53f488SRico Sonntag public function getFallBackName(): string 659c1010edaSGreg Roach { 660c0935879SGreg Roach return e($this->xref()); 661a25f0a04SGreg Roach } 662a25f0a04SGreg Roach 663a25f0a04SGreg Roach /** 664a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 665a25f0a04SGreg Roach * 666cbc1590aSGreg Roach * @return int 667a25f0a04SGreg Roach */ 6688f53f488SRico Sonntag public function getPrimaryName(): int 669c1010edaSGreg Roach { 670a25f0a04SGreg Roach static $language_script; 671a25f0a04SGreg Roach 672a25f0a04SGreg Roach if ($language_script === null) { 673a25f0a04SGreg Roach $language_script = I18N::languageScript(WT_LOCALE); 674a25f0a04SGreg Roach } 675a25f0a04SGreg Roach 676bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 677a25f0a04SGreg Roach // Generally, the first name is the primary one.... 678bdb3725aSGreg Roach $this->getPrimaryName = 0; 679a25f0a04SGreg Roach // ...except when the language/name use different character sets 680a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 68169546be1SGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 682bdb3725aSGreg Roach $this->getPrimaryName = $n; 683a25f0a04SGreg Roach break; 684a25f0a04SGreg Roach } 685a25f0a04SGreg Roach } 686a25f0a04SGreg Roach } 687a25f0a04SGreg Roach 688bdb3725aSGreg Roach return $this->getPrimaryName; 689a25f0a04SGreg Roach } 690a25f0a04SGreg Roach 691a25f0a04SGreg Roach /** 692a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 693a25f0a04SGreg Roach * 694cbc1590aSGreg Roach * @return int 695a25f0a04SGreg Roach */ 6968f53f488SRico Sonntag public function getSecondaryName(): int 697c1010edaSGreg Roach { 6988f038c36SRico Sonntag if ($this->getSecondaryName === null) { 699a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 700bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 701a25f0a04SGreg Roach // ....except when there are names with different character sets 702a25f0a04SGreg Roach $all_names = $this->getAllNames(); 703a25f0a04SGreg Roach if (count($all_names) > 1) { 704a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 705a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 706a25f0a04SGreg Roach if ($n != $this->getPrimaryName() && $name['type'] != '_MARNM' && I18N::textScript($name['sort']) != $primary_script) { 707bdb3725aSGreg Roach $this->getSecondaryName = $n; 708a25f0a04SGreg Roach break; 709a25f0a04SGreg Roach } 710a25f0a04SGreg Roach } 711a25f0a04SGreg Roach } 712a25f0a04SGreg Roach } 713cbc1590aSGreg Roach 714bdb3725aSGreg Roach return $this->getSecondaryName; 715a25f0a04SGreg Roach } 716a25f0a04SGreg Roach 717a25f0a04SGreg Roach /** 718a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 719a25f0a04SGreg Roach * 72076f666f4SGreg Roach * @param int|null $n 7217e96c925SGreg Roach * 7227e96c925SGreg Roach * @return void 723a25f0a04SGreg Roach */ 724251a9de7SGreg Roach public function setPrimaryName(int $n = null) 725c1010edaSGreg Roach { 726bdb3725aSGreg Roach $this->getPrimaryName = $n; 727bdb3725aSGreg Roach $this->getSecondaryName = null; 728a25f0a04SGreg Roach } 729a25f0a04SGreg Roach 730a25f0a04SGreg Roach /** 731a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 732a25f0a04SGreg Roach * 733a25f0a04SGreg Roach * @return string 734a25f0a04SGreg Roach */ 735c1010edaSGreg Roach public function __toString() 736c1010edaSGreg Roach { 73772cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 738a25f0a04SGreg Roach } 739a25f0a04SGreg Roach 740a25f0a04SGreg Roach /** 741c156e8f5SGreg Roach * /** 742a25f0a04SGreg Roach * Get variants of the name 743a25f0a04SGreg Roach * 744a25f0a04SGreg Roach * @return string 745a25f0a04SGreg Roach */ 746*39ca88baSGreg Roach public function fullName() 747c1010edaSGreg Roach { 748a25f0a04SGreg Roach if ($this->canShowName()) { 749a25f0a04SGreg Roach $tmp = $this->getAllNames(); 750cbc1590aSGreg Roach 751a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 752a25f0a04SGreg Roach } 753b2ce94c6SRico Sonntag 754b2ce94c6SRico Sonntag return I18N::translate('Private'); 755a25f0a04SGreg Roach } 756a25f0a04SGreg Roach 757a25f0a04SGreg Roach /** 758a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 759a25f0a04SGreg Roach * 760a25f0a04SGreg Roach * @return string 761a25f0a04SGreg Roach */ 762*39ca88baSGreg Roach public function sortName(): string 763c1010edaSGreg Roach { 764a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 765a25f0a04SGreg Roach $tmp = $this->getAllNames(); 766cbc1590aSGreg Roach 767a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 768a25f0a04SGreg Roach } 769a25f0a04SGreg Roach 770a25f0a04SGreg Roach /** 771a25f0a04SGreg Roach * Get the full name in an alternative character set 772a25f0a04SGreg Roach * 773a25f0a04SGreg Roach * @return null|string 774a25f0a04SGreg Roach */ 775*39ca88baSGreg Roach public function alternateName() 776c1010edaSGreg Roach { 777a25f0a04SGreg Roach if ($this->canShowName() && $this->getPrimaryName() != $this->getSecondaryName()) { 778a25f0a04SGreg Roach $all_names = $this->getAllNames(); 779cbc1590aSGreg Roach 780a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 781a25f0a04SGreg Roach } 782b2ce94c6SRico Sonntag 783b2ce94c6SRico Sonntag return null; 784a25f0a04SGreg Roach } 785a25f0a04SGreg Roach 786a25f0a04SGreg Roach /** 787a25f0a04SGreg Roach * Format this object for display in a list 788a25f0a04SGreg Roach * 789a25f0a04SGreg Roach * @return string 790a25f0a04SGreg Roach */ 7918f53f488SRico Sonntag public function formatList(): string 792c1010edaSGreg Roach { 793b165e17cSGreg Roach $html = '<a href="' . e($this->url()) . '" class="list_item">'; 794*39ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 795a25f0a04SGreg Roach $html .= $this->formatListDetails(); 796b165e17cSGreg Roach $html .= '</a>'; 797cbc1590aSGreg Roach 798a25f0a04SGreg Roach return $html; 799a25f0a04SGreg Roach } 800a25f0a04SGreg Roach 801a25f0a04SGreg Roach /** 802a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 803a25f0a04SGreg Roach * identifying characteristics of this record. 804a25f0a04SGreg Roach * 805a25f0a04SGreg Roach * @return string 806a25f0a04SGreg Roach */ 8078f53f488SRico Sonntag public function formatListDetails(): string 808c1010edaSGreg Roach { 809a25f0a04SGreg Roach return ''; 810a25f0a04SGreg Roach } 811a25f0a04SGreg Roach 812a25f0a04SGreg Roach /** 813a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 814a25f0a04SGreg Roach * 8158d0ebef0SGreg Roach * @param string[] $facts 816cbc1590aSGreg Roach * @param int $style 817a25f0a04SGreg Roach * 818a25f0a04SGreg Roach * @return string 819a25f0a04SGreg Roach */ 8208d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 821c1010edaSGreg Roach { 82230158ae7SGreg Roach foreach ($this->facts($facts, true) as $event) { 823a25f0a04SGreg Roach // Only display if it has a date or place (or both) 824392561bbSGreg Roach if ($event->date()->isOK() && $event->place()->gedcomName() <> '') { 825d93f11b5SGreg Roach $joiner = ' — '; 826d93f11b5SGreg Roach } else { 827d93f11b5SGreg Roach $joiner = ''; 828d93f11b5SGreg Roach } 829392561bbSGreg Roach if ($event->date()->isOK() || $event->place()->gedcomName() <> '') { 830a25f0a04SGreg Roach switch ($style) { 831a25f0a04SGreg Roach case 1: 8327b7d8067SGreg Roach return '<br><em>' . $event->label() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>'; 833a25f0a04SGreg Roach case 2: 8347b7d8067SGreg Roach return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>'; 835a25f0a04SGreg Roach } 836a25f0a04SGreg Roach } 837a25f0a04SGreg Roach } 838cbc1590aSGreg Roach 839a25f0a04SGreg Roach return ''; 840a25f0a04SGreg Roach } 841a25f0a04SGreg Roach 842a25f0a04SGreg Roach /** 843a25f0a04SGreg Roach * Find individuals linked to this record. 844a25f0a04SGreg Roach * 845a25f0a04SGreg Roach * @param string $link 846a25f0a04SGreg Roach * 847360dbd2cSGreg Roach * @return Individual[] 848a25f0a04SGreg Roach */ 84976f666f4SGreg Roach public function linkedIndividuals(string $link): array 850c1010edaSGreg Roach { 851ba1c12e8SGreg Roach $rows = DB::table('individuals') 852ba1c12e8SGreg Roach ->join('link', function (JoinClause $join): void { 853ba1c12e8SGreg Roach $join->on('l_file', '=', 'i_file')->on('l_from', '=', 'i_id'); 854ba1c12e8SGreg Roach }) 855ba1c12e8SGreg Roach ->where('i_file', '=', $this->tree->id()) 856ba1c12e8SGreg Roach ->where('l_type', '=', $link) 857ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 858ba1c12e8SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom']) 859ba1c12e8SGreg Roach ->get(); 860a25f0a04SGreg Roach 86113abd6f3SGreg Roach $list = []; 862a25f0a04SGreg Roach foreach ($rows as $row) { 86324ec66ceSGreg Roach $record = Individual::getInstance($row->xref, $this->tree, $row->gedcom); 864a25f0a04SGreg Roach if ($record->canShowName()) { 865a25f0a04SGreg Roach $list[] = $record; 866a25f0a04SGreg Roach } 867a25f0a04SGreg Roach } 868cbc1590aSGreg Roach 869a25f0a04SGreg Roach return $list; 870a25f0a04SGreg Roach } 871a25f0a04SGreg Roach 872a25f0a04SGreg Roach /** 873a25f0a04SGreg Roach * Find families linked to this record. 874a25f0a04SGreg Roach * 875a25f0a04SGreg Roach * @param string $link 876a25f0a04SGreg Roach * 877360dbd2cSGreg Roach * @return Family[] 878a25f0a04SGreg Roach */ 87976f666f4SGreg Roach public function linkedFamilies(string $link): array 880c1010edaSGreg Roach { 881ba1c12e8SGreg Roach $rows = DB::table('families') 882ba1c12e8SGreg Roach ->join('link', function (JoinClause $join): void { 883ba1c12e8SGreg Roach $join->on('l_file', '=', 'f_file')->on('l_from', '=', 'f_id'); 884ba1c12e8SGreg Roach }) 885ba1c12e8SGreg Roach ->where('f_file', '=', $this->tree->id()) 886ba1c12e8SGreg Roach ->where('l_type', '=', $link) 887ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 888ba1c12e8SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom']) 889ba1c12e8SGreg Roach ->get(); 890a25f0a04SGreg Roach 89113abd6f3SGreg Roach $list = []; 892a25f0a04SGreg Roach foreach ($rows as $row) { 89324ec66ceSGreg Roach $record = Family::getInstance($row->xref, $this->tree, $row->gedcom); 894a25f0a04SGreg Roach if ($record->canShowName()) { 895a25f0a04SGreg Roach $list[] = $record; 896a25f0a04SGreg Roach } 897a25f0a04SGreg Roach } 898cbc1590aSGreg Roach 899a25f0a04SGreg Roach return $list; 900a25f0a04SGreg Roach } 901a25f0a04SGreg Roach 902a25f0a04SGreg Roach /** 903a25f0a04SGreg Roach * Find sources linked to this record. 904a25f0a04SGreg Roach * 905a25f0a04SGreg Roach * @param string $link 906a25f0a04SGreg Roach * 907360dbd2cSGreg Roach * @return Source[] 908a25f0a04SGreg Roach */ 90976f666f4SGreg Roach public function linkedSources(string $link): array 910c1010edaSGreg Roach { 911ba1c12e8SGreg Roach $rows = DB::table('sources') 912ba1c12e8SGreg Roach ->join('link', function (JoinClause $join): void { 913ba1c12e8SGreg Roach $join->on('l_file', '=', 's_file')->on('l_from', '=', 's_id'); 914ba1c12e8SGreg Roach }) 915ba1c12e8SGreg Roach ->where('s_file', '=', $this->tree->id()) 916ba1c12e8SGreg Roach ->where('l_type', '=', $link) 917ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 918ba1c12e8SGreg Roach ->select(['s_id AS xref', 's_gedcom AS gedcom']) 919ba1c12e8SGreg Roach ->get(); 920a25f0a04SGreg Roach 92113abd6f3SGreg Roach $list = []; 922a25f0a04SGreg Roach foreach ($rows as $row) { 92324ec66ceSGreg Roach $record = Source::getInstance($row->xref, $this->tree, $row->gedcom); 924a25f0a04SGreg Roach if ($record->canShowName()) { 925a25f0a04SGreg Roach $list[] = $record; 926a25f0a04SGreg Roach } 927a25f0a04SGreg Roach } 928cbc1590aSGreg Roach 929a25f0a04SGreg Roach return $list; 930a25f0a04SGreg Roach } 931a25f0a04SGreg Roach 932a25f0a04SGreg Roach /** 933a25f0a04SGreg Roach * Find media objects linked to this record. 934a25f0a04SGreg Roach * 935a25f0a04SGreg Roach * @param string $link 936a25f0a04SGreg Roach * 937360dbd2cSGreg Roach * @return Media[] 938a25f0a04SGreg Roach */ 93976f666f4SGreg Roach public function linkedMedia(string $link): array 940c1010edaSGreg Roach { 941ba1c12e8SGreg Roach $rows = DB::table('media') 942ba1c12e8SGreg Roach ->join('link', function (JoinClause $join): void { 943ba1c12e8SGreg Roach $join->on('l_file', '=', 'm_file')->on('l_from', '=', 'm_id'); 944ba1c12e8SGreg Roach }) 945ba1c12e8SGreg Roach ->where('m_file', '=', $this->tree->id()) 946ba1c12e8SGreg Roach ->where('l_type', '=', $link) 947ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 948ba1c12e8SGreg Roach ->select(['m_id AS xref', 'm_gedcom AS gedcom']) 949ba1c12e8SGreg Roach ->get(); 950a25f0a04SGreg Roach 95113abd6f3SGreg Roach $list = []; 952a25f0a04SGreg Roach foreach ($rows as $row) { 95324ec66ceSGreg Roach $record = Media::getInstance($row->xref, $this->tree, $row->gedcom); 954a25f0a04SGreg Roach if ($record->canShowName()) { 955a25f0a04SGreg Roach $list[] = $record; 956a25f0a04SGreg Roach } 957a25f0a04SGreg Roach } 958cbc1590aSGreg Roach 959a25f0a04SGreg Roach return $list; 960a25f0a04SGreg Roach } 961a25f0a04SGreg Roach 962a25f0a04SGreg Roach /** 963a25f0a04SGreg Roach * Find notes linked to this record. 964a25f0a04SGreg Roach * 965a25f0a04SGreg Roach * @param string $link 966a25f0a04SGreg Roach * 967a25f0a04SGreg Roach * @return Note[] 968a25f0a04SGreg Roach */ 96976f666f4SGreg Roach public function linkedNotes(string $link): array 970c1010edaSGreg Roach { 971ba1c12e8SGreg Roach $rows = DB::table('other') 972ba1c12e8SGreg Roach ->join('link', function (JoinClause $join): void { 973ba1c12e8SGreg Roach $join->on('l_file', '=', 'o_file')->on('l_from', '=', 'o_id'); 974ba1c12e8SGreg Roach }) 975ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 976ba1c12e8SGreg Roach ->where('o_type', '=', 'NOTE') 977ba1c12e8SGreg Roach ->where('l_type', '=', $link) 978ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 979ba1c12e8SGreg Roach ->select(['o_id AS xref', 'o_gedcom AS gedcom']) 980ba1c12e8SGreg Roach ->get(); 981a25f0a04SGreg Roach 98213abd6f3SGreg Roach $list = []; 983a25f0a04SGreg Roach foreach ($rows as $row) { 98424ec66ceSGreg Roach $record = Note::getInstance($row->xref, $this->tree, $row->gedcom); 985a25f0a04SGreg Roach if ($record->canShowName()) { 986a25f0a04SGreg Roach $list[] = $record; 987a25f0a04SGreg Roach } 988a25f0a04SGreg Roach } 989cbc1590aSGreg Roach 990a25f0a04SGreg Roach return $list; 991a25f0a04SGreg Roach } 992a25f0a04SGreg Roach 993a25f0a04SGreg Roach /** 994a25f0a04SGreg Roach * Find repositories linked to this record. 995a25f0a04SGreg Roach * 996a25f0a04SGreg Roach * @param string $link 997a25f0a04SGreg Roach * 998360dbd2cSGreg Roach * @return Repository[] 999a25f0a04SGreg Roach */ 100076f666f4SGreg Roach public function linkedRepositories(string $link): array 1001c1010edaSGreg Roach { 1002ba1c12e8SGreg Roach $rows = DB::table('other') 1003ba1c12e8SGreg Roach ->join('link', function (JoinClause $join): void { 1004ba1c12e8SGreg Roach $join->on('l_file', '=', 'o_file')->on('l_from', '=', 'o_id'); 1005ba1c12e8SGreg Roach }) 1006ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 1007ba1c12e8SGreg Roach ->where('o_type', '=', 'REPO') 1008ba1c12e8SGreg Roach ->where('l_type', '=', $link) 1009ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 1010ba1c12e8SGreg Roach ->select(['o_id AS xref', 'o_gedcom AS gedcom']) 1011ba1c12e8SGreg Roach ->get(); 1012a25f0a04SGreg Roach 101313abd6f3SGreg Roach $list = []; 1014a25f0a04SGreg Roach foreach ($rows as $row) { 101524ec66ceSGreg Roach $record = Repository::getInstance($row->xref, $this->tree, $row->gedcom); 1016a25f0a04SGreg Roach if ($record->canShowName()) { 1017a25f0a04SGreg Roach $list[] = $record; 1018a25f0a04SGreg Roach } 1019a25f0a04SGreg Roach } 1020cbc1590aSGreg Roach 1021a25f0a04SGreg Roach return $list; 1022a25f0a04SGreg Roach } 1023a25f0a04SGreg Roach 1024a25f0a04SGreg Roach /** 1025a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 1026a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 1027a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 1028a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 1029a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 1030a25f0a04SGreg Roach * 10318d0ebef0SGreg Roach * @param string[] $events 1032a25f0a04SGreg Roach * 1033a25f0a04SGreg Roach * @return Date[] 1034a25f0a04SGreg Roach */ 10358d0ebef0SGreg Roach public function getAllEventDates(array $events): array 1036c1010edaSGreg Roach { 103713abd6f3SGreg Roach $dates = []; 10388d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 10392decada7SGreg Roach if ($event->date()->isOK()) { 10402decada7SGreg Roach $dates[] = $event->date(); 1041a25f0a04SGreg Roach } 1042a25f0a04SGreg Roach } 1043a25f0a04SGreg Roach 1044a25f0a04SGreg Roach return $dates; 1045a25f0a04SGreg Roach } 1046a25f0a04SGreg Roach 1047a25f0a04SGreg Roach /** 1048a25f0a04SGreg Roach * Get all the places for a particular type of event 1049a25f0a04SGreg Roach * 10508d0ebef0SGreg Roach * @param string[] $events 1051a25f0a04SGreg Roach * 10524080d558SGreg Roach * @return Place[] 1053a25f0a04SGreg Roach */ 10548d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 1055c1010edaSGreg Roach { 105613abd6f3SGreg Roach $places = []; 10578d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 1058138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 1059a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 106016d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 1061a25f0a04SGreg Roach } 1062a25f0a04SGreg Roach } 1063a25f0a04SGreg Roach } 1064a25f0a04SGreg Roach 1065a25f0a04SGreg Roach return $places; 1066a25f0a04SGreg Roach } 1067a25f0a04SGreg Roach 1068a25f0a04SGreg Roach /** 1069a25f0a04SGreg Roach * Get the first (i.e. prefered) Fact for the given fact type 1070a25f0a04SGreg Roach * 1071a25f0a04SGreg Roach * @param string $tag 1072a25f0a04SGreg Roach * 1073a25f0a04SGreg Roach * @return Fact|null 1074a25f0a04SGreg Roach */ 1075*39ca88baSGreg Roach public function firstFact(string $tag) 1076c1010edaSGreg Roach { 107730158ae7SGreg Roach foreach ($this->facts() as $fact) { 1078a25f0a04SGreg Roach if ($fact->getTag() === $tag) { 1079a25f0a04SGreg Roach return $fact; 1080a25f0a04SGreg Roach } 1081a25f0a04SGreg Roach } 1082a25f0a04SGreg Roach 1083a25f0a04SGreg Roach return null; 1084a25f0a04SGreg Roach } 1085a25f0a04SGreg Roach 1086a25f0a04SGreg Roach /** 1087a25f0a04SGreg Roach * The facts and events for this record. 1088a25f0a04SGreg Roach * 10898d0ebef0SGreg Roach * @param string[] $filter 1090cbc1590aSGreg Roach * @param bool $sort 1091cbc1590aSGreg Roach * @param int|null $access_level 1092cbc1590aSGreg Roach * @param bool $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES. 1093a25f0a04SGreg Roach * 1094*39ca88baSGreg Roach * @return Collection|Fact[] 1095a25f0a04SGreg Roach */ 1096*39ca88baSGreg Roach public function facts(array $filter = [], bool $sort = false, int $access_level = null, bool $override = false): Collection 1097c1010edaSGreg Roach { 10984b9ff166SGreg Roach if ($access_level === null) { 10994b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 11004b9ff166SGreg Roach } 11014b9ff166SGreg Roach 110213abd6f3SGreg Roach $facts = []; 1103a25f0a04SGreg Roach if ($this->canShow($access_level) || $override) { 1104a25f0a04SGreg Roach foreach ($this->facts as $fact) { 11058d0ebef0SGreg Roach if (($filter === [] || in_array($fact->getTag(), $filter)) && $fact->canShow($access_level)) { 1106a25f0a04SGreg Roach $facts[] = $fact; 1107a25f0a04SGreg Roach } 1108a25f0a04SGreg Roach } 1109a25f0a04SGreg Roach } 1110d17d7b9eSGreg Roach 1111a25f0a04SGreg Roach if ($sort) { 11123d7a8a4cSGreg Roach Functions::sortFacts($facts); 1113a25f0a04SGreg Roach } 1114cbc1590aSGreg Roach 1115*39ca88baSGreg Roach return new Collection($facts); 1116a25f0a04SGreg Roach } 1117a25f0a04SGreg Roach 1118a25f0a04SGreg Roach /** 1119a25f0a04SGreg Roach * Get the last-change timestamp for this record, either as a formatted string 1120a25f0a04SGreg Roach * (for display) or as a unix timestamp (for sorting) 1121a25f0a04SGreg Roach * 1122cbc1590aSGreg Roach * @param bool $sorting 1123a25f0a04SGreg Roach * 1124589feda3SGreg Roach * @return string|int 1125a25f0a04SGreg Roach */ 112676f666f4SGreg Roach public function lastChangeTimestamp(bool $sorting = false) 1127c1010edaSGreg Roach { 1128*39ca88baSGreg Roach $chan = $this->firstFact('CHAN'); 1129a25f0a04SGreg Roach 1130a25f0a04SGreg Roach if ($chan) { 1131a25f0a04SGreg Roach // The record does have a CHAN event 11322decada7SGreg Roach $d = $chan->date()->minimumDate(); 1133138ca96cSGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->gedcom(), $match)) { 1134a25f0a04SGreg Roach $t = mktime((int) $match[1], (int) $match[2], (int) $match[3], (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y')); 1135138ca96cSGreg Roach } elseif (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->gedcom(), $match)) { 1136a25f0a04SGreg Roach $t = mktime((int) $match[1], (int) $match[2], 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y')); 1137a25f0a04SGreg Roach } else { 1138a25f0a04SGreg Roach $t = mktime(0, 0, 0, (int) $d->format('%n'), (int) $d->format('%j'), (int) $d->format('%Y')); 1139a25f0a04SGreg Roach } 1140a25f0a04SGreg Roach if ($sorting) { 1141a25f0a04SGreg Roach return $t; 1142b2ce94c6SRico Sonntag } 1143b2ce94c6SRico Sonntag 11443d7a8a4cSGreg Roach return strip_tags(FunctionsDate::formatTimestamp($t)); 1145a25f0a04SGreg Roach } 1146b2ce94c6SRico Sonntag 1147a25f0a04SGreg Roach // The record does not have a CHAN event 1148a25f0a04SGreg Roach if ($sorting) { 1149a25f0a04SGreg Roach return '0'; 1150b2ce94c6SRico Sonntag } 1151b2ce94c6SRico Sonntag 115266b90994SGreg Roach return ''; 1153a25f0a04SGreg Roach } 1154a25f0a04SGreg Roach 1155a25f0a04SGreg Roach /** 1156a25f0a04SGreg Roach * Get the last-change user for this record 1157a25f0a04SGreg Roach * 1158a25f0a04SGreg Roach * @return string 1159a25f0a04SGreg Roach */ 1160c1010edaSGreg Roach public function lastChangeUser() 1161c1010edaSGreg Roach { 1162*39ca88baSGreg Roach $chan = $this->firstFact('CHAN'); 1163a25f0a04SGreg Roach 1164a25f0a04SGreg Roach if ($chan === null) { 1165a25f0a04SGreg Roach return I18N::translate('Unknown'); 1166b2ce94c6SRico Sonntag } 1167b2ce94c6SRico Sonntag 11683425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 1169baacc364SGreg Roach if ($chan_user === '') { 1170a25f0a04SGreg Roach return I18N::translate('Unknown'); 1171b2ce94c6SRico Sonntag } 1172b2ce94c6SRico Sonntag 1173a25f0a04SGreg Roach return $chan_user; 1174a25f0a04SGreg Roach } 1175a25f0a04SGreg Roach 1176a25f0a04SGreg Roach /** 1177a25f0a04SGreg Roach * Add a new fact to this record 1178a25f0a04SGreg Roach * 1179a25f0a04SGreg Roach * @param string $gedcom 1180cbc1590aSGreg Roach * @param bool $update_chan 11817e96c925SGreg Roach * 11827e96c925SGreg Roach * @return void 1183a25f0a04SGreg Roach */ 118476f666f4SGreg Roach public function createFact(string $gedcom, bool $update_chan) 1185c1010edaSGreg Roach { 1186fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 1187a25f0a04SGreg Roach } 1188a25f0a04SGreg Roach 1189a25f0a04SGreg Roach /** 1190a25f0a04SGreg Roach * Delete a fact from this record 1191a25f0a04SGreg Roach * 1192a25f0a04SGreg Roach * @param string $fact_id 1193cbc1590aSGreg Roach * @param bool $update_chan 11947e96c925SGreg Roach * 11957e96c925SGreg Roach * @return void 1196a25f0a04SGreg Roach */ 119776f666f4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan) 1198c1010edaSGreg Roach { 1199db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 1200a25f0a04SGreg Roach } 1201a25f0a04SGreg Roach 1202a25f0a04SGreg Roach /** 1203a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 1204a25f0a04SGreg Roach * 1205a25f0a04SGreg Roach * @param string $fact_id 1206a25f0a04SGreg Roach * @param string $gedcom 1207cbc1590aSGreg Roach * @param bool $update_chan 1208a25f0a04SGreg Roach * 12097e96c925SGreg Roach * @return void 12107e96c925SGreg Roach * @throws Exception 1211a25f0a04SGreg Roach */ 121276f666f4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan) 1213c1010edaSGreg Roach { 1214a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1215a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1216a25f0a04SGreg Roach $gedcom = trim($gedcom); 1217a25f0a04SGreg Roach 1218a25f0a04SGreg Roach if ($this->pending === '') { 12197e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 1220a25f0a04SGreg Roach } 12218d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 12227e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 1223a25f0a04SGreg Roach } 1224a25f0a04SGreg Roach 1225a25f0a04SGreg Roach if ($this->pending) { 1226a25f0a04SGreg Roach $old_gedcom = $this->pending; 1227a25f0a04SGreg Roach } else { 1228a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 1229a25f0a04SGreg Roach } 1230a25f0a04SGreg Roach 1231a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 123265e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 1233a25f0a04SGreg Roach 1234a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 12358d0ebef0SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE) as $fact) { 1236a25f0a04SGreg Roach if (!$fact->isPendingDeletion()) { 1237905ab80aSGreg Roach if ($fact->id() === $fact_id) { 1238db7bb364SGreg Roach if ($gedcom !== '') { 1239a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1240a25f0a04SGreg Roach } 1241fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 1242a25f0a04SGreg Roach } elseif ($fact->getTag() != 'CHAN' || !$update_chan) { 1243138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 1244a25f0a04SGreg Roach } 1245a25f0a04SGreg Roach } 1246a25f0a04SGreg Roach } 1247a25f0a04SGreg Roach if ($update_chan) { 1248e5a6b4d4SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1249a25f0a04SGreg Roach } 1250a25f0a04SGreg Roach 1251a25f0a04SGreg Roach // Adding a new fact 1252fc3ccce4SGreg Roach if ($fact_id === '') { 1253a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1254a25f0a04SGreg Roach } 1255a25f0a04SGreg Roach 1256a25f0a04SGreg Roach if ($new_gedcom != $old_gedcom) { 1257a25f0a04SGreg Roach // Save the changes 1258d09b6323SGreg Roach DB::table('change')->insert([ 1259d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1260d09b6323SGreg Roach 'xref' => $this->xref, 1261d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 1262d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 1263d09b6323SGreg Roach 'user_id' => Auth::id(), 126413abd6f3SGreg Roach ]); 1265a25f0a04SGreg Roach 1266a25f0a04SGreg Roach $this->pending = $new_gedcom; 1267a25f0a04SGreg Roach 1268a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1269cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($this->xref, $this->tree); 1270a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 1271a25f0a04SGreg Roach $this->pending = null; 1272a25f0a04SGreg Roach } 1273a25f0a04SGreg Roach } 1274a25f0a04SGreg Roach $this->parseFacts(); 1275a25f0a04SGreg Roach } 1276a25f0a04SGreg Roach 1277a25f0a04SGreg Roach /** 1278a25f0a04SGreg Roach * Update this record 1279a25f0a04SGreg Roach * 1280a25f0a04SGreg Roach * @param string $gedcom 1281cbc1590aSGreg Roach * @param bool $update_chan 12827e96c925SGreg Roach * 12837e96c925SGreg Roach * @return void 1284a25f0a04SGreg Roach */ 128576f666f4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan) 1286c1010edaSGreg Roach { 1287a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1288a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1289a25f0a04SGreg Roach $gedcom = trim($gedcom); 1290a25f0a04SGreg Roach 1291a25f0a04SGreg Roach // Update the CHAN record 1292a25f0a04SGreg Roach if ($update_chan) { 1293a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 1294e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1295a25f0a04SGreg Roach } 1296a25f0a04SGreg Roach 1297a25f0a04SGreg Roach // Create a pending change 1298d09b6323SGreg Roach DB::table('change')->insert([ 1299d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1300d09b6323SGreg Roach 'xref' => $this->xref, 1301d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1302d09b6323SGreg Roach 'new_gedcom' => $gedcom, 1303d09b6323SGreg Roach 'user_id' => Auth::id(), 130413abd6f3SGreg Roach ]); 1305a25f0a04SGreg Roach 1306a25f0a04SGreg Roach // Clear the cache 1307a25f0a04SGreg Roach $this->pending = $gedcom; 1308a25f0a04SGreg Roach 1309a25f0a04SGreg Roach // Accept this pending change 1310a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1311cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($this->xref, $this->tree); 1312a25f0a04SGreg Roach $this->gedcom = $gedcom; 1313a25f0a04SGreg Roach $this->pending = null; 1314a25f0a04SGreg Roach } 1315a25f0a04SGreg Roach 1316a25f0a04SGreg Roach $this->parseFacts(); 1317a25f0a04SGreg Roach 1318847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1319a25f0a04SGreg Roach } 1320a25f0a04SGreg Roach 1321a25f0a04SGreg Roach /** 1322a25f0a04SGreg Roach * Delete this record 1323b874da82SGreg Roach * 1324b874da82SGreg Roach * @return void 1325a25f0a04SGreg Roach */ 1326c1010edaSGreg Roach public function deleteRecord() 1327c1010edaSGreg Roach { 1328a25f0a04SGreg Roach // Create a pending change 13294c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 1330d09b6323SGreg Roach DB::table('change')->insert([ 1331d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1332d09b6323SGreg Roach 'xref' => $this->xref, 1333d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1334d09b6323SGreg Roach 'new_gedcom' => '', 1335d09b6323SGreg Roach 'user_id' => Auth::id(), 133613abd6f3SGreg Roach ]); 13374c0b5256SGreg Roach } 1338a25f0a04SGreg Roach 13394c0b5256SGreg Roach // Auto-accept this pending change 1340a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1341cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($this->xref, $this->tree); 1342a25f0a04SGreg Roach } 1343a25f0a04SGreg Roach 1344a25f0a04SGreg Roach // Clear the cache 1345d17d7b9eSGreg Roach self::$gedcom_record_cache = []; 1346d17d7b9eSGreg Roach self::$pending_record_cache = []; 1347a25f0a04SGreg Roach 1348847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1349a25f0a04SGreg Roach } 1350a25f0a04SGreg Roach 1351a25f0a04SGreg Roach /** 1352a25f0a04SGreg Roach * Remove all links from this record to $xref 1353a25f0a04SGreg Roach * 1354a25f0a04SGreg Roach * @param string $xref 1355cbc1590aSGreg Roach * @param bool $update_chan 13567e96c925SGreg Roach * 13577e96c925SGreg Roach * @return void 1358a25f0a04SGreg Roach */ 135976f666f4SGreg Roach public function removeLinks(string $xref, bool $update_chan) 1360c1010edaSGreg Roach { 1361a25f0a04SGreg Roach $value = '@' . $xref . '@'; 1362a25f0a04SGreg Roach 136330158ae7SGreg Roach foreach ($this->facts() as $fact) { 136484586c02SGreg Roach if ($fact->value() === $value) { 1365905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 13668d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 1367138ca96cSGreg Roach $gedcom = $fact->gedcom(); 1368a25f0a04SGreg Roach foreach ($matches as $match) { 1369a25f0a04SGreg Roach $next_level = $match[1] + 1; 1370a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 1371a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 1372a25f0a04SGreg Roach } 1373905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 1374a25f0a04SGreg Roach } 1375a25f0a04SGreg Roach } 1376a25f0a04SGreg Roach } 1377bf4eb542SGreg Roach 1378bf4eb542SGreg Roach /** 1379bf4eb542SGreg Roach * Fetch XREFs of all records linked to a record - when deleting an object, we must 1380bf4eb542SGreg Roach * also delete all links to it. 1381bf4eb542SGreg Roach * 1382bf4eb542SGreg Roach * @return GedcomRecord[] 1383bf4eb542SGreg Roach */ 1384bf4eb542SGreg Roach public function linkingRecords(): array 1385bf4eb542SGreg Roach { 1386bf4eb542SGreg Roach $union = DB::table('change') 1387bf4eb542SGreg Roach ->where('gedcom_id', '=', $this->tree()->id()) 1388fbbe964bSGreg Roach ->whereContains('new_gedcom', '@' . $this->xref() . '@') 1389bf4eb542SGreg Roach ->where('new_gedcom', 'NOT LIKE', '0 @' . $this->xref() . '@%') 1390bf4eb542SGreg Roach ->select(['xref']); 1391bf4eb542SGreg Roach 1392bf4eb542SGreg Roach $xrefs = DB::table('link') 1393bf4eb542SGreg Roach ->where('l_file', '=', $this->tree()->id()) 1394bf4eb542SGreg Roach ->where('l_to', '=', $this->xref()) 1395bf4eb542SGreg Roach ->select('l_from') 1396bf4eb542SGreg Roach ->union($union) 1397bf4eb542SGreg Roach ->pluck('l_from'); 1398bf4eb542SGreg Roach 1399bf4eb542SGreg Roach return $xrefs->map(function (string $xref): GedcomRecord { 1400bf4eb542SGreg Roach return GedcomRecord::getInstance($xref, $this->tree); 1401bf4eb542SGreg Roach })->all(); 1402bf4eb542SGreg Roach } 1403a25f0a04SGreg Roach} 1404