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\FunctionsImport; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 24bf4eb542SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 25ba1c12e8SGreg Roachuse Illuminate\Database\Query\JoinClause; 2639ca88baSGreg Roachuse Illuminate\Support\Collection; 2779529c87SGreg Roachuse stdClass; 28a25f0a04SGreg Roach 29a25f0a04SGreg Roach/** 3076692c8bSGreg Roach * A GEDCOM object. 31a25f0a04SGreg Roach */ 32c1010edaSGreg Roachclass GedcomRecord 33c1010edaSGreg Roach{ 3416d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 3516d6367aSGreg Roach 3616d6367aSGreg Roach protected const ROUTE_NAME = 'record'; 37a25f0a04SGreg Roach 38a25f0a04SGreg Roach /** @var string The record identifier */ 39a25f0a04SGreg Roach protected $xref; 40a25f0a04SGreg Roach 41000959d9SGreg Roach /** @var Tree The family tree to which this record belongs */ 42000959d9SGreg Roach protected $tree; 43a25f0a04SGreg Roach 44a25f0a04SGreg Roach /** @var string GEDCOM data (before any pending edits) */ 45a25f0a04SGreg Roach protected $gedcom; 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** @var string|null GEDCOM data (after any pending edits) */ 48a25f0a04SGreg Roach protected $pending; 49a25f0a04SGreg Roach 50a25f0a04SGreg Roach /** @var Fact[] facts extracted from $gedcom/$pending */ 51a25f0a04SGreg Roach protected $facts; 52a25f0a04SGreg Roach 53a25f0a04SGreg Roach /** @var string[][] All the names of this individual */ 54bdb3725aSGreg Roach protected $getAllNames; 55a25f0a04SGreg Roach 56d17d7b9eSGreg Roach /** @var int|null Cached result */ 57bdb3725aSGreg Roach protected $getPrimaryName; 58a25f0a04SGreg Roach 59d17d7b9eSGreg Roach /** @var int|null Cached result */ 60bdb3725aSGreg Roach protected $getSecondaryName; 61a25f0a04SGreg Roach 6276692c8bSGreg Roach /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */ 63bec87e94SGreg Roach public static $gedcom_record_cache; 641ae87ce5SGreg Roach 6579529c87SGreg Roach /** @var stdClass[][] Fetch all pending edits in one database query */ 66bec87e94SGreg Roach public static $pending_record_cache; 67a25f0a04SGreg Roach 68a25f0a04SGreg Roach /** 69a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 70a25f0a04SGreg Roach * 71a25f0a04SGreg Roach * @param string $xref 72a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 73a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 74a25f0a04SGreg Roach * empty string for records with pending deletions 7524ec66ceSGreg Roach * @param Tree $tree 76a25f0a04SGreg Roach */ 77e364afe4SGreg Roach public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 78c1010edaSGreg Roach { 79a25f0a04SGreg Roach $this->xref = $xref; 80a25f0a04SGreg Roach $this->gedcom = $gedcom; 81a25f0a04SGreg Roach $this->pending = $pending; 8224ec66ceSGreg Roach $this->tree = $tree; 83a25f0a04SGreg Roach 84a25f0a04SGreg Roach $this->parseFacts(); 85a25f0a04SGreg Roach } 86a25f0a04SGreg Roach 87a25f0a04SGreg Roach /** 88886b77daSGreg Roach * A closure which will create a record from a database row. 89886b77daSGreg Roach * 90886b77daSGreg Roach * @return Closure 91886b77daSGreg Roach */ 92c0804649SGreg Roach public static function rowMapper(): Closure 93886b77daSGreg Roach { 94c0804649SGreg Roach return function (stdClass $row): GedcomRecord { 95e3bddf11SGreg Roach return GedcomRecord::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom); 96886b77daSGreg Roach }; 97886b77daSGreg Roach } 98886b77daSGreg Roach 99886b77daSGreg Roach /** 100886b77daSGreg Roach * A closure which will filter out private records. 101886b77daSGreg Roach * 102886b77daSGreg Roach * @return Closure 103886b77daSGreg Roach */ 1044146fabcSGreg Roach public static function accessFilter(): Closure 105886b77daSGreg Roach { 106886b77daSGreg Roach return function (GedcomRecord $record): bool { 107886b77daSGreg Roach return $record->canShow(); 108886b77daSGreg Roach }; 109886b77daSGreg Roach } 110886b77daSGreg Roach 111886b77daSGreg Roach /** 112c156e8f5SGreg Roach * A closure which will compare records by name. 113c156e8f5SGreg Roach * 114c156e8f5SGreg Roach * @return Closure 115c156e8f5SGreg Roach */ 116c156e8f5SGreg Roach public static function nameComparator(): Closure 117c156e8f5SGreg Roach { 118c156e8f5SGreg Roach return function (GedcomRecord $x, GedcomRecord $y): int { 119c156e8f5SGreg Roach if ($x->canShowName()) { 120c156e8f5SGreg Roach if ($y->canShowName()) { 12139ca88baSGreg Roach return I18N::strcasecmp($x->sortName(), $y->sortName()); 122c156e8f5SGreg Roach } 123c156e8f5SGreg Roach 124c156e8f5SGreg Roach return -1; // only $y is private 125c156e8f5SGreg Roach } 126c156e8f5SGreg Roach 127c156e8f5SGreg Roach if ($y->canShowName()) { 128c156e8f5SGreg Roach return 1; // only $x is private 129c156e8f5SGreg Roach } 130c156e8f5SGreg Roach 131c156e8f5SGreg Roach return 0; // both $x and $y private 132c156e8f5SGreg Roach }; 133c156e8f5SGreg Roach } 134c156e8f5SGreg Roach 135c156e8f5SGreg Roach /** 136c156e8f5SGreg Roach * A closure which will compare records by change time. 137c156e8f5SGreg Roach * 138c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 139c156e8f5SGreg Roach * 140c156e8f5SGreg Roach * @return Closure 141c156e8f5SGreg Roach */ 142c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 143c156e8f5SGreg Roach { 144c156e8f5SGreg Roach return function (GedcomRecord $x, GedcomRecord $y) use ($direction): int { 1454459dc9aSGreg Roach return $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp()); 146c156e8f5SGreg Roach }; 147c156e8f5SGreg Roach } 148c156e8f5SGreg Roach 149c156e8f5SGreg Roach /** 150a25f0a04SGreg Roach * Split the record into facts 1517e96c925SGreg Roach * 1527e96c925SGreg Roach * @return void 153a25f0a04SGreg Roach */ 154e364afe4SGreg Roach private function parseFacts(): void 155c1010edaSGreg Roach { 156a25f0a04SGreg Roach // Split the record into facts 157a25f0a04SGreg Roach if ($this->gedcom) { 158a25f0a04SGreg Roach $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom); 159a25f0a04SGreg Roach array_shift($gedcom_facts); 160a25f0a04SGreg Roach } else { 16113abd6f3SGreg Roach $gedcom_facts = []; 162a25f0a04SGreg Roach } 163a25f0a04SGreg Roach if ($this->pending) { 164a25f0a04SGreg Roach $pending_facts = preg_split('/\n(?=1)/s', $this->pending); 165a25f0a04SGreg Roach array_shift($pending_facts); 166a25f0a04SGreg Roach } else { 16713abd6f3SGreg Roach $pending_facts = []; 168a25f0a04SGreg Roach } 169a25f0a04SGreg Roach 17013abd6f3SGreg Roach $this->facts = []; 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 173a25f0a04SGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 174*22d65e5aSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 175a25f0a04SGreg Roach $fact->setPendingDeletion(); 176a25f0a04SGreg Roach } 177a25f0a04SGreg Roach $this->facts[] = $fact; 178a25f0a04SGreg Roach } 179a25f0a04SGreg Roach foreach ($pending_facts as $pending_fact) { 180*22d65e5aSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 181a25f0a04SGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 182a25f0a04SGreg Roach $fact->setPendingAddition(); 183a25f0a04SGreg Roach $this->facts[] = $fact; 184a25f0a04SGreg Roach } 185a25f0a04SGreg Roach } 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach 188a25f0a04SGreg Roach /** 189a25f0a04SGreg Roach * Get an instance of a GedcomRecord object. For single records, 190a25f0a04SGreg Roach * we just receive the XREF. For bulk records (such as lists 191a25f0a04SGreg Roach * and search results) we can receive the GEDCOM data as well. 192a25f0a04SGreg Roach * 193a25f0a04SGreg Roach * @param string $xref 19424ec66ceSGreg Roach * @param Tree $tree 195a25f0a04SGreg Roach * @param string|null $gedcom 196a25f0a04SGreg Roach * 1977e96c925SGreg Roach * @throws Exception 19884658595SGreg Roach * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null 199a25f0a04SGreg Roach */ 20076f666f4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 201c1010edaSGreg Roach { 20272cf66d4SGreg Roach $tree_id = $tree->id(); 20324ec66ceSGreg Roach 204e71ef9d2SGreg Roach // Is this record already in the cache? 20524ec66ceSGreg Roach if (isset(self::$gedcom_record_cache[$xref][$tree_id])) { 206e71ef9d2SGreg Roach return self::$gedcom_record_cache[$xref][$tree_id]; 207a25f0a04SGreg Roach } 208a25f0a04SGreg Roach 209a25f0a04SGreg Roach // Do we need to fetch the record from the database? 210a25f0a04SGreg Roach if ($gedcom === null) { 21124ec66ceSGreg Roach $gedcom = static::fetchGedcomRecord($xref, $tree_id); 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 214a25f0a04SGreg Roach // If we can edit, then we also need to be able to see pending records. 21594075df0SGreg Roach if (Auth::isEditor($tree)) { 21624ec66ceSGreg Roach if (!isset(self::$pending_record_cache[$tree_id])) { 217a25f0a04SGreg Roach // Fetch all pending records in one database query 21813abd6f3SGreg Roach self::$pending_record_cache[$tree_id] = []; 21985fc8064SGreg Roach $rows = DB::table('change') 22085fc8064SGreg Roach ->where('gedcom_id', '=', $tree_id) 22185fc8064SGreg Roach ->where('status', '=', 'pending') 22285fc8064SGreg Roach ->orderBy('change_id') 22385fc8064SGreg Roach ->select(['xref', 'new_gedcom']) 22485fc8064SGreg Roach ->get(); 225d17d7b9eSGreg Roach 226a25f0a04SGreg Roach foreach ($rows as $row) { 22724ec66ceSGreg Roach self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom; 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach } 230a25f0a04SGreg Roach 231d17d7b9eSGreg Roach $pending = self::$pending_record_cache[$tree_id][$xref] ?? null; 232a25f0a04SGreg Roach } else { 233a25f0a04SGreg Roach // There are no pending changes for this record 234a25f0a04SGreg Roach $pending = null; 235a25f0a04SGreg Roach } 236a25f0a04SGreg Roach 237a25f0a04SGreg Roach // No such record exists 238a25f0a04SGreg Roach if ($gedcom === null && $pending === null) { 239a25f0a04SGreg Roach return null; 240a25f0a04SGreg Roach } 241a25f0a04SGreg Roach 242fc3ccce4SGreg Roach // No such record, but a pending creation exists 243fc3ccce4SGreg Roach if ($gedcom === null) { 244fc3ccce4SGreg Roach $gedcom = ''; 245fc3ccce4SGreg Roach } 246fc3ccce4SGreg Roach 247a25f0a04SGreg Roach // Create the object 2488d0ebef0SGreg Roach if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ (' . Gedcom::REGEX_TAG . ')/', $gedcom . $pending, $match)) { 249a25f0a04SGreg Roach $xref = $match[1]; // Collation - we may have requested I123 and found i123 250a25f0a04SGreg Roach $type = $match[2]; 251a25f0a04SGreg Roach } elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) { 252a25f0a04SGreg Roach $xref = $match[1]; 253a25f0a04SGreg Roach $type = $match[1]; 254a25f0a04SGreg Roach } elseif ($gedcom . $pending) { 2557e96c925SGreg Roach throw new Exception('Unrecognized GEDCOM record: ' . $gedcom); 256a25f0a04SGreg Roach } else { 257a25f0a04SGreg Roach // A record with both pending creation and pending deletion 258a25f0a04SGreg Roach $type = static::RECORD_TYPE; 259a25f0a04SGreg Roach } 260a25f0a04SGreg Roach 261a25f0a04SGreg Roach switch ($type) { 262a25f0a04SGreg Roach case 'INDI': 26324ec66ceSGreg Roach $record = new Individual($xref, $gedcom, $pending, $tree); 264a25f0a04SGreg Roach break; 265a25f0a04SGreg Roach case 'FAM': 26624ec66ceSGreg Roach $record = new Family($xref, $gedcom, $pending, $tree); 267a25f0a04SGreg Roach break; 268a25f0a04SGreg Roach case 'SOUR': 26924ec66ceSGreg Roach $record = new Source($xref, $gedcom, $pending, $tree); 270a25f0a04SGreg Roach break; 271a25f0a04SGreg Roach case 'OBJE': 27224ec66ceSGreg Roach $record = new Media($xref, $gedcom, $pending, $tree); 273a25f0a04SGreg Roach break; 274a25f0a04SGreg Roach case 'REPO': 27524ec66ceSGreg Roach $record = new Repository($xref, $gedcom, $pending, $tree); 276a25f0a04SGreg Roach break; 277a25f0a04SGreg Roach case 'NOTE': 27824ec66ceSGreg Roach $record = new Note($xref, $gedcom, $pending, $tree); 279a25f0a04SGreg Roach break; 280a25f0a04SGreg Roach default: 28106ef8e02SGreg Roach $record = new self($xref, $gedcom, $pending, $tree); 282a25f0a04SGreg Roach break; 283a25f0a04SGreg Roach } 284a25f0a04SGreg Roach 285a25f0a04SGreg Roach // Store it in the cache 28624ec66ceSGreg Roach self::$gedcom_record_cache[$xref][$tree_id] = $record; 287a25f0a04SGreg Roach 288a25f0a04SGreg Roach return $record; 289a25f0a04SGreg Roach } 290a25f0a04SGreg Roach 291a25f0a04SGreg Roach /** 292a25f0a04SGreg Roach * Fetch data from the database 293a25f0a04SGreg Roach * 294a25f0a04SGreg Roach * @param string $xref 295cbc1590aSGreg Roach * @param int $tree_id 296a25f0a04SGreg Roach * 297e364afe4SGreg Roach * @return string|null 298a25f0a04SGreg Roach */ 299e364afe4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 300c1010edaSGreg Roach { 301a25f0a04SGreg Roach // We don't know what type of object this is. Try each one in turn. 30264d9078aSGreg Roach $data = Individual::fetchGedcomRecord($xref, $tree_id); 30385fc8064SGreg Roach if ($data !== null) { 304a25f0a04SGreg Roach return $data; 305a25f0a04SGreg Roach } 30664d9078aSGreg Roach $data = Family::fetchGedcomRecord($xref, $tree_id); 30785fc8064SGreg Roach if ($data !== null) { 308a25f0a04SGreg Roach return $data; 309a25f0a04SGreg Roach } 31064d9078aSGreg Roach $data = Source::fetchGedcomRecord($xref, $tree_id); 31185fc8064SGreg Roach if ($data !== null) { 312a25f0a04SGreg Roach return $data; 313a25f0a04SGreg Roach } 31464d9078aSGreg Roach $data = Repository::fetchGedcomRecord($xref, $tree_id); 31585fc8064SGreg Roach if ($data !== null) { 316a25f0a04SGreg Roach return $data; 317a25f0a04SGreg Roach } 31864d9078aSGreg Roach $data = Media::fetchGedcomRecord($xref, $tree_id); 31985fc8064SGreg Roach if ($data !== null) { 320a25f0a04SGreg Roach return $data; 321a25f0a04SGreg Roach } 32264d9078aSGreg Roach $data = Note::fetchGedcomRecord($xref, $tree_id); 32385fc8064SGreg Roach if ($data !== null) { 324a25f0a04SGreg Roach return $data; 325a25f0a04SGreg Roach } 326c1010edaSGreg Roach 327a25f0a04SGreg Roach // Some other type of record... 328d09b6323SGreg Roach return DB::table('other') 32985fc8064SGreg Roach ->where('o_file', '=', $tree_id) 33085fc8064SGreg Roach ->where('o_id', '=', $xref) 33185fc8064SGreg Roach ->value('o_gedcom'); 332a25f0a04SGreg Roach } 333a25f0a04SGreg Roach 334a25f0a04SGreg Roach /** 335a25f0a04SGreg Roach * Get the XREF for this record 336a25f0a04SGreg Roach * 337a25f0a04SGreg Roach * @return string 338a25f0a04SGreg Roach */ 339c0935879SGreg Roach public function xref(): string 340c1010edaSGreg Roach { 341a25f0a04SGreg Roach return $this->xref; 342a25f0a04SGreg Roach } 343a25f0a04SGreg Roach 344a25f0a04SGreg Roach /** 345000959d9SGreg Roach * Get the tree to which this record belongs 346000959d9SGreg Roach * 347000959d9SGreg Roach * @return Tree 348000959d9SGreg Roach */ 349f4afa648SGreg Roach public function tree(): Tree 350c1010edaSGreg Roach { 351518bbdc1SGreg Roach return $this->tree; 352000959d9SGreg Roach } 353000959d9SGreg Roach 354000959d9SGreg Roach /** 355a25f0a04SGreg Roach * Application code should access data via Fact objects. 356a25f0a04SGreg Roach * This function exists to support old code. 357a25f0a04SGreg Roach * 358a25f0a04SGreg Roach * @return string 359a25f0a04SGreg Roach */ 360e364afe4SGreg Roach public function gedcom(): string 361c1010edaSGreg Roach { 362b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 363a25f0a04SGreg Roach } 364a25f0a04SGreg Roach 365a25f0a04SGreg Roach /** 366a25f0a04SGreg Roach * Does this record have a pending change? 367a25f0a04SGreg Roach * 368cbc1590aSGreg Roach * @return bool 369a25f0a04SGreg Roach */ 3708f53f488SRico Sonntag public function isPendingAddition(): bool 371c1010edaSGreg Roach { 372a25f0a04SGreg Roach return $this->pending !== null; 373a25f0a04SGreg Roach } 374a25f0a04SGreg Roach 375a25f0a04SGreg Roach /** 376a25f0a04SGreg Roach * Does this record have a pending deletion? 377a25f0a04SGreg Roach * 378cbc1590aSGreg Roach * @return bool 379a25f0a04SGreg Roach */ 3808f53f488SRico Sonntag public function isPendingDeletion(): bool 381c1010edaSGreg Roach { 382a25f0a04SGreg Roach return $this->pending === ''; 383a25f0a04SGreg Roach } 384a25f0a04SGreg Roach 385a25f0a04SGreg Roach /** 386225e381fSGreg Roach * Generate a URL to this record. 387a25f0a04SGreg Roach * 388a25f0a04SGreg Roach * @return string 389a25f0a04SGreg Roach */ 3908f53f488SRico Sonntag public function url(): string 391c1010edaSGreg Roach { 392225e381fSGreg Roach return route(static::ROUTE_NAME, [ 393c0935879SGreg Roach 'xref' => $this->xref(), 394aa6f03bbSGreg Roach 'ged' => $this->tree->name(), 395225e381fSGreg Roach ]); 396a25f0a04SGreg Roach } 397a25f0a04SGreg Roach 398a25f0a04SGreg Roach /** 399a25f0a04SGreg Roach * Work out whether this record can be shown to a user with a given access level 400a25f0a04SGreg Roach * 401cbc1590aSGreg Roach * @param int $access_level 402a25f0a04SGreg Roach * 403cbc1590aSGreg Roach * @return bool 404a25f0a04SGreg Roach */ 40576f666f4SGreg Roach private function canShowRecord(int $access_level): bool 406c1010edaSGreg Roach { 407a25f0a04SGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 408518bbdc1SGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 409a25f0a04SGreg Roach return true; 410a25f0a04SGreg Roach } 411a25f0a04SGreg Roach 412a25f0a04SGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 413c0935879SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), 'gedcomid') && $access_level === Auth::accessLevel($this->tree)) { 414a25f0a04SGreg Roach return true; 415a25f0a04SGreg Roach } 416a25f0a04SGreg Roach 417a25f0a04SGreg Roach // Does this record have a RESN? 41820ff464cSGreg Roach if (strpos($this->gedcom, "\n1 RESN confidential") !== false) { 4194b9ff166SGreg Roach return Auth::PRIV_NONE >= $access_level; 420a25f0a04SGreg Roach } 42120ff464cSGreg Roach if (strpos($this->gedcom, "\n1 RESN privacy") !== false) { 4224b9ff166SGreg Roach return Auth::PRIV_USER >= $access_level; 423a25f0a04SGreg Roach } 42420ff464cSGreg Roach if (strpos($this->gedcom, "\n1 RESN none") !== false) { 425a25f0a04SGreg Roach return true; 426a25f0a04SGreg Roach } 427a25f0a04SGreg Roach 428a25f0a04SGreg Roach // Does this record have a default RESN? 429518bbdc1SGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 430c0935879SGreg Roach if (isset($individual_privacy[$this->xref()])) { 431c0935879SGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 432a25f0a04SGreg Roach } 433a25f0a04SGreg Roach 434a25f0a04SGreg Roach // Privacy rules do not apply to admins 4354b9ff166SGreg Roach if (Auth::PRIV_NONE >= $access_level) { 436a25f0a04SGreg Roach return true; 437a25f0a04SGreg Roach } 438a25f0a04SGreg Roach 439a25f0a04SGreg Roach // Different types of record have different privacy rules 440a25f0a04SGreg Roach return $this->canShowByType($access_level); 441a25f0a04SGreg Roach } 442a25f0a04SGreg Roach 443a25f0a04SGreg Roach /** 444a25f0a04SGreg Roach * Each object type may have its own special rules, and re-implement this function. 445a25f0a04SGreg Roach * 446cbc1590aSGreg Roach * @param int $access_level 447a25f0a04SGreg Roach * 448cbc1590aSGreg Roach * @return bool 449a25f0a04SGreg Roach */ 45035584196SGreg Roach protected function canShowByType(int $access_level): bool 451c1010edaSGreg Roach { 452518bbdc1SGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 453a25f0a04SGreg Roach 454518bbdc1SGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 455a25f0a04SGreg Roach // Restriction found 456518bbdc1SGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 457b2ce94c6SRico Sonntag } 458b2ce94c6SRico Sonntag 459a25f0a04SGreg Roach // No restriction found - must be public: 460a25f0a04SGreg Roach return true; 461a25f0a04SGreg Roach } 462a25f0a04SGreg Roach 463a25f0a04SGreg Roach /** 464a25f0a04SGreg Roach * Can the details of this record be shown? 465a25f0a04SGreg Roach * 466cbc1590aSGreg Roach * @param int|null $access_level 467a25f0a04SGreg Roach * 468cbc1590aSGreg Roach * @return bool 469a25f0a04SGreg Roach */ 47035584196SGreg Roach public function canShow(int $access_level = null): bool 471c1010edaSGreg Roach { 472f0b9c048SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 4734b9ff166SGreg Roach 474a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 475a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 476f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 477a25f0a04SGreg Roach return true; 478a25f0a04SGreg Roach } 479f0b9c048SGreg Roach 480f0b9c048SGreg Roach $cache_key = 'canShow' . $this->xref . ':' . $this->tree->id() . ':' . $access_level; 481f0b9c048SGreg Roach 482f0b9c048SGreg Roach return app('cache.array')->rememberForever($cache_key, function () use ($access_level) { 483f0b9c048SGreg Roach return $this->canShowRecord($access_level); 484f0b9c048SGreg Roach }); 485a25f0a04SGreg Roach } 486a25f0a04SGreg Roach 487a25f0a04SGreg Roach /** 488a25f0a04SGreg Roach * Can the name of this record be shown? 489a25f0a04SGreg Roach * 490cbc1590aSGreg Roach * @param int|null $access_level 491a25f0a04SGreg Roach * 492cbc1590aSGreg Roach * @return bool 493a25f0a04SGreg Roach */ 49476f666f4SGreg Roach public function canShowName(int $access_level = null): bool 495c1010edaSGreg Roach { 496a25f0a04SGreg Roach return $this->canShow($access_level); 497a25f0a04SGreg Roach } 498a25f0a04SGreg Roach 499a25f0a04SGreg Roach /** 500a25f0a04SGreg Roach * Can we edit this record? 501a25f0a04SGreg Roach * 502cbc1590aSGreg Roach * @return bool 503a25f0a04SGreg Roach */ 5048f53f488SRico Sonntag public function canEdit(): bool 505c1010edaSGreg Roach { 5061450f098SGreg Roach if ($this->isPendingDeletion()) { 5071450f098SGreg Roach return false; 5081450f098SGreg Roach } 5091450f098SGreg Roach 5101450f098SGreg Roach if (Auth::isManager($this->tree)) { 5111450f098SGreg Roach return true; 5121450f098SGreg Roach } 5131450f098SGreg Roach 5141450f098SGreg Roach return Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false; 515a25f0a04SGreg Roach } 516a25f0a04SGreg Roach 517a25f0a04SGreg Roach /** 518a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 519a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 520a25f0a04SGreg Roach * 521cbc1590aSGreg Roach * @param int $access_level 522a25f0a04SGreg Roach * 523a25f0a04SGreg Roach * @return string 524a25f0a04SGreg Roach */ 525e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 526c1010edaSGreg Roach { 527e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 528a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 529a25f0a04SGreg Roach return $this->gedcom; 530b2ce94c6SRico Sonntag } 531b2ce94c6SRico Sonntag 532b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 533a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 534a25f0a04SGreg Roach 535a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 53665e02381SGreg Roach [$gedrec] = explode("\n", $this->gedcom, 2); 537a25f0a04SGreg Roach 538a25f0a04SGreg Roach // Check each of the facts for access 5398d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 540138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 541a25f0a04SGreg Roach } 542cbc1590aSGreg Roach 543a25f0a04SGreg Roach return $gedrec; 544b2ce94c6SRico Sonntag } 545b2ce94c6SRico Sonntag 546a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 547a25f0a04SGreg Roach // limited data, such as links to other records. 548a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 549a25f0a04SGreg Roach } 550a25f0a04SGreg Roach 551a25f0a04SGreg Roach /** 552a25f0a04SGreg Roach * Generate a private version of this record 553a25f0a04SGreg Roach * 554cbc1590aSGreg Roach * @param int $access_level 555a25f0a04SGreg Roach * 556a25f0a04SGreg Roach * @return string 557a25f0a04SGreg Roach */ 55876f666f4SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 559c1010edaSGreg Roach { 560a25f0a04SGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private'); 561a25f0a04SGreg Roach } 562a25f0a04SGreg Roach 563a25f0a04SGreg Roach /** 564a25f0a04SGreg Roach * Convert a name record into sortable and full/display versions. This default 565a25f0a04SGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 566a25f0a04SGreg Roach * 567a25f0a04SGreg Roach * @param string $type 568a25f0a04SGreg Roach * @param string $value 569a25f0a04SGreg Roach * @param string $gedcom 5707e96c925SGreg Roach * 5717e96c925SGreg Roach * @return void 572a25f0a04SGreg Roach */ 573e364afe4SGreg Roach protected function addName(string $type, string $value, string $gedcom): void 574c1010edaSGreg Roach { 575bdb3725aSGreg Roach $this->getAllNames[] = [ 576a25f0a04SGreg Roach 'type' => $type, 5770b5fd0a6SGreg Roach 'sort' => preg_replace_callback('/([0-9]+)/', static function (array $matches): string { 5788d68cabeSGreg Roach return str_pad($matches[0], 10, '0', STR_PAD_LEFT); 5798d68cabeSGreg Roach }, $value), 580c1010edaSGreg Roach 'full' => '<span dir="auto">' . e($value) . '</span>', 581c1010edaSGreg Roach // This is used for display 582c1010edaSGreg Roach 'fullNN' => $value, 583c1010edaSGreg Roach // This goes into the database 58413abd6f3SGreg Roach ]; 585a25f0a04SGreg Roach } 586a25f0a04SGreg Roach 587a25f0a04SGreg Roach /** 588a25f0a04SGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 589a25f0a04SGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 590a25f0a04SGreg Roach * Parameters: the level 1 fact containing the name. 591a25f0a04SGreg Roach * Return value: an array of name structures, each containing 592a25f0a04SGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 593a25f0a04SGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 594a25f0a04SGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 595a25f0a04SGreg Roach * 596cbc1590aSGreg Roach * @param int $level 597a25f0a04SGreg Roach * @param string $fact_type 59854c7f8dfSGreg Roach * @param Collection $facts 5997e96c925SGreg Roach * 6007e96c925SGreg Roach * @return void 601a25f0a04SGreg Roach */ 602e364afe4SGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 603c1010edaSGreg Roach { 604a25f0a04SGreg Roach $sublevel = $level + 1; 605a25f0a04SGreg Roach $subsublevel = $sublevel + 1; 606a25f0a04SGreg Roach foreach ($facts as $fact) { 607138ca96cSGreg Roach if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) { 608a25f0a04SGreg Roach foreach ($matches as $match) { 609a25f0a04SGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 610e364afe4SGreg Roach if ($match[1] === 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) { 611138ca96cSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 612a25f0a04SGreg Roach } else { 613138ca96cSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 614a25f0a04SGreg Roach } 615a25f0a04SGreg Roach if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { 616a25f0a04SGreg Roach foreach ($submatches as $submatch) { 617a25f0a04SGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 618a25f0a04SGreg Roach } 619a25f0a04SGreg Roach } 620a25f0a04SGreg Roach } 621a25f0a04SGreg Roach } 622a25f0a04SGreg Roach } 623a25f0a04SGreg Roach } 624a25f0a04SGreg Roach 625a25f0a04SGreg Roach /** 626a25f0a04SGreg Roach * Default for "other" object types 627c7ff4153SGreg Roach * 628c7ff4153SGreg Roach * @return void 629a25f0a04SGreg Roach */ 630e364afe4SGreg Roach public function extractNames(): void 631c1010edaSGreg Roach { 63276f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 633a25f0a04SGreg Roach } 634a25f0a04SGreg Roach 635a25f0a04SGreg Roach /** 636a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 637a25f0a04SGreg Roach * 638a25f0a04SGreg Roach * @return string[][] 639a25f0a04SGreg Roach */ 6408f53f488SRico Sonntag public function getAllNames(): array 641c1010edaSGreg Roach { 642bdb3725aSGreg Roach if ($this->getAllNames === null) { 643bdb3725aSGreg Roach $this->getAllNames = []; 644a25f0a04SGreg Roach if ($this->canShowName()) { 645a25f0a04SGreg Roach // Ask the record to extract its names 646a25f0a04SGreg Roach $this->extractNames(); 647a25f0a04SGreg Roach // No name found? Use a fallback. 648bdb3725aSGreg Roach if (!$this->getAllNames) { 649db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 650a25f0a04SGreg Roach } 651a25f0a04SGreg Roach } else { 652db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 653a25f0a04SGreg Roach } 654a25f0a04SGreg Roach } 655cbc1590aSGreg Roach 656bdb3725aSGreg Roach return $this->getAllNames; 657a25f0a04SGreg Roach } 658a25f0a04SGreg Roach 659a25f0a04SGreg Roach /** 660a25f0a04SGreg Roach * If this object has no name, what do we call it? 661a25f0a04SGreg Roach * 662a25f0a04SGreg Roach * @return string 663a25f0a04SGreg Roach */ 6648f53f488SRico Sonntag public function getFallBackName(): string 665c1010edaSGreg Roach { 666c0935879SGreg Roach return e($this->xref()); 667a25f0a04SGreg Roach } 668a25f0a04SGreg Roach 669a25f0a04SGreg Roach /** 670a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 671a25f0a04SGreg Roach * 672cbc1590aSGreg Roach * @return int 673a25f0a04SGreg Roach */ 6748f53f488SRico Sonntag public function getPrimaryName(): int 675c1010edaSGreg Roach { 676a25f0a04SGreg Roach static $language_script; 677a25f0a04SGreg Roach 678a25f0a04SGreg Roach if ($language_script === null) { 679a25f0a04SGreg Roach $language_script = I18N::languageScript(WT_LOCALE); 680a25f0a04SGreg Roach } 681a25f0a04SGreg Roach 682bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 683a25f0a04SGreg Roach // Generally, the first name is the primary one.... 684bdb3725aSGreg Roach $this->getPrimaryName = 0; 685a25f0a04SGreg Roach // ...except when the language/name use different character sets 686a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 68769546be1SGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 688bdb3725aSGreg Roach $this->getPrimaryName = $n; 689a25f0a04SGreg Roach break; 690a25f0a04SGreg Roach } 691a25f0a04SGreg Roach } 692a25f0a04SGreg Roach } 693a25f0a04SGreg Roach 694bdb3725aSGreg Roach return $this->getPrimaryName; 695a25f0a04SGreg Roach } 696a25f0a04SGreg Roach 697a25f0a04SGreg Roach /** 698a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 699a25f0a04SGreg Roach * 700cbc1590aSGreg Roach * @return int 701a25f0a04SGreg Roach */ 7028f53f488SRico Sonntag public function getSecondaryName(): int 703c1010edaSGreg Roach { 7048f038c36SRico Sonntag if ($this->getSecondaryName === null) { 705a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 706bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 707a25f0a04SGreg Roach // ....except when there are names with different character sets 708a25f0a04SGreg Roach $all_names = $this->getAllNames(); 709a25f0a04SGreg Roach if (count($all_names) > 1) { 710a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 711a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 712e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 713bdb3725aSGreg Roach $this->getSecondaryName = $n; 714a25f0a04SGreg Roach break; 715a25f0a04SGreg Roach } 716a25f0a04SGreg Roach } 717a25f0a04SGreg Roach } 718a25f0a04SGreg Roach } 719cbc1590aSGreg Roach 720bdb3725aSGreg Roach return $this->getSecondaryName; 721a25f0a04SGreg Roach } 722a25f0a04SGreg Roach 723a25f0a04SGreg Roach /** 724a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 725a25f0a04SGreg Roach * 72676f666f4SGreg Roach * @param int|null $n 7277e96c925SGreg Roach * 7287e96c925SGreg Roach * @return void 729a25f0a04SGreg Roach */ 730e364afe4SGreg Roach public function setPrimaryName(int $n = null): void 731c1010edaSGreg Roach { 732bdb3725aSGreg Roach $this->getPrimaryName = $n; 733bdb3725aSGreg Roach $this->getSecondaryName = null; 734a25f0a04SGreg Roach } 735a25f0a04SGreg Roach 736a25f0a04SGreg Roach /** 737a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 738a25f0a04SGreg Roach * 739a25f0a04SGreg Roach * @return string 740a25f0a04SGreg Roach */ 741c1010edaSGreg Roach public function __toString() 742c1010edaSGreg Roach { 74372cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 744a25f0a04SGreg Roach } 745a25f0a04SGreg Roach 746a25f0a04SGreg Roach /** 747c156e8f5SGreg Roach * /** 748a25f0a04SGreg Roach * Get variants of the name 749a25f0a04SGreg Roach * 750a25f0a04SGreg Roach * @return string 751a25f0a04SGreg Roach */ 752e364afe4SGreg Roach public function fullName(): string 753c1010edaSGreg Roach { 754a25f0a04SGreg Roach if ($this->canShowName()) { 755a25f0a04SGreg Roach $tmp = $this->getAllNames(); 756cbc1590aSGreg Roach 757a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 758a25f0a04SGreg Roach } 759b2ce94c6SRico Sonntag 760b2ce94c6SRico Sonntag return I18N::translate('Private'); 761a25f0a04SGreg Roach } 762a25f0a04SGreg Roach 763a25f0a04SGreg Roach /** 764a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 765a25f0a04SGreg Roach * 766a25f0a04SGreg Roach * @return string 767a25f0a04SGreg Roach */ 76839ca88baSGreg Roach public function sortName(): string 769c1010edaSGreg Roach { 770a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 771a25f0a04SGreg Roach $tmp = $this->getAllNames(); 772cbc1590aSGreg Roach 773a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 774a25f0a04SGreg Roach } 775a25f0a04SGreg Roach 776a25f0a04SGreg Roach /** 777a25f0a04SGreg Roach * Get the full name in an alternative character set 778a25f0a04SGreg Roach * 779e364afe4SGreg Roach * @return string|null 780a25f0a04SGreg Roach */ 781e364afe4SGreg Roach public function alternateName(): ?string 782c1010edaSGreg Roach { 783e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 784a25f0a04SGreg Roach $all_names = $this->getAllNames(); 785cbc1590aSGreg Roach 786a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 787a25f0a04SGreg Roach } 788b2ce94c6SRico Sonntag 789b2ce94c6SRico Sonntag return null; 790a25f0a04SGreg Roach } 791a25f0a04SGreg Roach 792a25f0a04SGreg Roach /** 793a25f0a04SGreg Roach * Format this object for display in a list 794a25f0a04SGreg Roach * 795a25f0a04SGreg Roach * @return string 796a25f0a04SGreg Roach */ 7978f53f488SRico Sonntag public function formatList(): string 798c1010edaSGreg Roach { 799b165e17cSGreg Roach $html = '<a href="' . e($this->url()) . '" class="list_item">'; 80039ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 801a25f0a04SGreg Roach $html .= $this->formatListDetails(); 802b165e17cSGreg Roach $html .= '</a>'; 803cbc1590aSGreg Roach 804a25f0a04SGreg Roach return $html; 805a25f0a04SGreg Roach } 806a25f0a04SGreg Roach 807a25f0a04SGreg Roach /** 808a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 809a25f0a04SGreg Roach * identifying characteristics of this record. 810a25f0a04SGreg Roach * 811a25f0a04SGreg Roach * @return string 812a25f0a04SGreg Roach */ 8138f53f488SRico Sonntag public function formatListDetails(): string 814c1010edaSGreg Roach { 815a25f0a04SGreg Roach return ''; 816a25f0a04SGreg Roach } 817a25f0a04SGreg Roach 818a25f0a04SGreg Roach /** 819a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 820a25f0a04SGreg Roach * 8218d0ebef0SGreg Roach * @param string[] $facts 822cbc1590aSGreg Roach * @param int $style 823a25f0a04SGreg Roach * 824a25f0a04SGreg Roach * @return string 825a25f0a04SGreg Roach */ 8268d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 827c1010edaSGreg Roach { 82830158ae7SGreg Roach foreach ($this->facts($facts, true) as $event) { 829a25f0a04SGreg Roach // Only display if it has a date or place (or both) 830e364afe4SGreg Roach if ($event->date()->isOK() && $event->place()->gedcomName() !== '') { 831d93f11b5SGreg Roach $joiner = ' — '; 832d93f11b5SGreg Roach } else { 833d93f11b5SGreg Roach $joiner = ''; 834d93f11b5SGreg Roach } 835e364afe4SGreg Roach if ($event->date()->isOK() || $event->place()->gedcomName() !== '') { 836a25f0a04SGreg Roach switch ($style) { 837a25f0a04SGreg Roach case 1: 8387b7d8067SGreg Roach return '<br><em>' . $event->label() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>'; 839a25f0a04SGreg Roach case 2: 8407b7d8067SGreg Roach return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>'; 841a25f0a04SGreg Roach } 842a25f0a04SGreg Roach } 843a25f0a04SGreg Roach } 844cbc1590aSGreg Roach 845a25f0a04SGreg Roach return ''; 846a25f0a04SGreg Roach } 847a25f0a04SGreg Roach 848a25f0a04SGreg Roach /** 849a25f0a04SGreg Roach * Find individuals linked to this record. 850a25f0a04SGreg Roach * 851a25f0a04SGreg Roach * @param string $link 852a25f0a04SGreg Roach * 853360dbd2cSGreg Roach * @return Individual[] 854a25f0a04SGreg Roach */ 85576f666f4SGreg Roach public function linkedIndividuals(string $link): array 856c1010edaSGreg Roach { 857ba1c12e8SGreg Roach $rows = DB::table('individuals') 8580b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 859ba1c12e8SGreg Roach $join->on('l_file', '=', 'i_file')->on('l_from', '=', 'i_id'); 860ba1c12e8SGreg Roach }) 861ba1c12e8SGreg Roach ->where('i_file', '=', $this->tree->id()) 862ba1c12e8SGreg Roach ->where('l_type', '=', $link) 863ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 864ba1c12e8SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom']) 865ba1c12e8SGreg Roach ->get(); 866a25f0a04SGreg Roach 86713abd6f3SGreg Roach $list = []; 868a25f0a04SGreg Roach foreach ($rows as $row) { 86924ec66ceSGreg Roach $record = Individual::getInstance($row->xref, $this->tree, $row->gedcom); 870a25f0a04SGreg Roach if ($record->canShowName()) { 871a25f0a04SGreg Roach $list[] = $record; 872a25f0a04SGreg Roach } 873a25f0a04SGreg Roach } 874cbc1590aSGreg Roach 875a25f0a04SGreg Roach return $list; 876a25f0a04SGreg Roach } 877a25f0a04SGreg Roach 878a25f0a04SGreg Roach /** 879a25f0a04SGreg Roach * Find families linked to this record. 880a25f0a04SGreg Roach * 881a25f0a04SGreg Roach * @param string $link 882a25f0a04SGreg Roach * 883360dbd2cSGreg Roach * @return Family[] 884a25f0a04SGreg Roach */ 88576f666f4SGreg Roach public function linkedFamilies(string $link): array 886c1010edaSGreg Roach { 887ba1c12e8SGreg Roach $rows = DB::table('families') 8880b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 889ba1c12e8SGreg Roach $join->on('l_file', '=', 'f_file')->on('l_from', '=', 'f_id'); 890ba1c12e8SGreg Roach }) 891ba1c12e8SGreg Roach ->where('f_file', '=', $this->tree->id()) 892ba1c12e8SGreg Roach ->where('l_type', '=', $link) 893ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 894ba1c12e8SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom']) 895ba1c12e8SGreg Roach ->get(); 896a25f0a04SGreg Roach 89713abd6f3SGreg Roach $list = []; 898a25f0a04SGreg Roach foreach ($rows as $row) { 89924ec66ceSGreg Roach $record = Family::getInstance($row->xref, $this->tree, $row->gedcom); 900a25f0a04SGreg Roach if ($record->canShowName()) { 901a25f0a04SGreg Roach $list[] = $record; 902a25f0a04SGreg Roach } 903a25f0a04SGreg Roach } 904cbc1590aSGreg Roach 905a25f0a04SGreg Roach return $list; 906a25f0a04SGreg Roach } 907a25f0a04SGreg Roach 908a25f0a04SGreg Roach /** 909a25f0a04SGreg Roach * Find sources linked to this record. 910a25f0a04SGreg Roach * 911a25f0a04SGreg Roach * @param string $link 912a25f0a04SGreg Roach * 913360dbd2cSGreg Roach * @return Source[] 914a25f0a04SGreg Roach */ 91576f666f4SGreg Roach public function linkedSources(string $link): array 916c1010edaSGreg Roach { 917ba1c12e8SGreg Roach $rows = DB::table('sources') 9180b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 919ba1c12e8SGreg Roach $join->on('l_file', '=', 's_file')->on('l_from', '=', 's_id'); 920ba1c12e8SGreg Roach }) 921ba1c12e8SGreg Roach ->where('s_file', '=', $this->tree->id()) 922ba1c12e8SGreg Roach ->where('l_type', '=', $link) 923ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 924ba1c12e8SGreg Roach ->select(['s_id AS xref', 's_gedcom AS gedcom']) 925ba1c12e8SGreg Roach ->get(); 926a25f0a04SGreg Roach 92713abd6f3SGreg Roach $list = []; 928a25f0a04SGreg Roach foreach ($rows as $row) { 92924ec66ceSGreg Roach $record = Source::getInstance($row->xref, $this->tree, $row->gedcom); 930a25f0a04SGreg Roach if ($record->canShowName()) { 931a25f0a04SGreg Roach $list[] = $record; 932a25f0a04SGreg Roach } 933a25f0a04SGreg Roach } 934cbc1590aSGreg Roach 935a25f0a04SGreg Roach return $list; 936a25f0a04SGreg Roach } 937a25f0a04SGreg Roach 938a25f0a04SGreg Roach /** 939a25f0a04SGreg Roach * Find media objects linked to this record. 940a25f0a04SGreg Roach * 941a25f0a04SGreg Roach * @param string $link 942a25f0a04SGreg Roach * 943360dbd2cSGreg Roach * @return Media[] 944a25f0a04SGreg Roach */ 94576f666f4SGreg Roach public function linkedMedia(string $link): array 946c1010edaSGreg Roach { 947ba1c12e8SGreg Roach $rows = DB::table('media') 9480b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 949ba1c12e8SGreg Roach $join->on('l_file', '=', 'm_file')->on('l_from', '=', 'm_id'); 950ba1c12e8SGreg Roach }) 951ba1c12e8SGreg Roach ->where('m_file', '=', $this->tree->id()) 952ba1c12e8SGreg Roach ->where('l_type', '=', $link) 953ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 954ba1c12e8SGreg Roach ->select(['m_id AS xref', 'm_gedcom AS gedcom']) 955ba1c12e8SGreg Roach ->get(); 956a25f0a04SGreg Roach 95713abd6f3SGreg Roach $list = []; 958a25f0a04SGreg Roach foreach ($rows as $row) { 95924ec66ceSGreg Roach $record = Media::getInstance($row->xref, $this->tree, $row->gedcom); 960a25f0a04SGreg Roach if ($record->canShowName()) { 961a25f0a04SGreg Roach $list[] = $record; 962a25f0a04SGreg Roach } 963a25f0a04SGreg Roach } 964cbc1590aSGreg Roach 965a25f0a04SGreg Roach return $list; 966a25f0a04SGreg Roach } 967a25f0a04SGreg Roach 968a25f0a04SGreg Roach /** 969a25f0a04SGreg Roach * Find notes linked to this record. 970a25f0a04SGreg Roach * 971a25f0a04SGreg Roach * @param string $link 972a25f0a04SGreg Roach * 973a25f0a04SGreg Roach * @return Note[] 974a25f0a04SGreg Roach */ 97576f666f4SGreg Roach public function linkedNotes(string $link): array 976c1010edaSGreg Roach { 977ba1c12e8SGreg Roach $rows = DB::table('other') 9780b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 979ba1c12e8SGreg Roach $join->on('l_file', '=', 'o_file')->on('l_from', '=', 'o_id'); 980ba1c12e8SGreg Roach }) 981ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 982ba1c12e8SGreg Roach ->where('o_type', '=', 'NOTE') 983ba1c12e8SGreg Roach ->where('l_type', '=', $link) 984ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 985ba1c12e8SGreg Roach ->select(['o_id AS xref', 'o_gedcom AS gedcom']) 986ba1c12e8SGreg Roach ->get(); 987a25f0a04SGreg Roach 98813abd6f3SGreg Roach $list = []; 989a25f0a04SGreg Roach foreach ($rows as $row) { 99024ec66ceSGreg Roach $record = Note::getInstance($row->xref, $this->tree, $row->gedcom); 991a25f0a04SGreg Roach if ($record->canShowName()) { 992a25f0a04SGreg Roach $list[] = $record; 993a25f0a04SGreg Roach } 994a25f0a04SGreg Roach } 995cbc1590aSGreg Roach 996a25f0a04SGreg Roach return $list; 997a25f0a04SGreg Roach } 998a25f0a04SGreg Roach 999a25f0a04SGreg Roach /** 1000a25f0a04SGreg Roach * Find repositories linked to this record. 1001a25f0a04SGreg Roach * 1002a25f0a04SGreg Roach * @param string $link 1003a25f0a04SGreg Roach * 1004360dbd2cSGreg Roach * @return Repository[] 1005a25f0a04SGreg Roach */ 100676f666f4SGreg Roach public function linkedRepositories(string $link): array 1007c1010edaSGreg Roach { 1008ba1c12e8SGreg Roach $rows = DB::table('other') 10090b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 1010ba1c12e8SGreg Roach $join->on('l_file', '=', 'o_file')->on('l_from', '=', 'o_id'); 1011ba1c12e8SGreg Roach }) 1012ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 1013ba1c12e8SGreg Roach ->where('o_type', '=', 'REPO') 1014ba1c12e8SGreg Roach ->where('l_type', '=', $link) 1015ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 1016ba1c12e8SGreg Roach ->select(['o_id AS xref', 'o_gedcom AS gedcom']) 1017ba1c12e8SGreg Roach ->get(); 1018a25f0a04SGreg Roach 101913abd6f3SGreg Roach $list = []; 1020a25f0a04SGreg Roach foreach ($rows as $row) { 102124ec66ceSGreg Roach $record = Repository::getInstance($row->xref, $this->tree, $row->gedcom); 1022a25f0a04SGreg Roach if ($record->canShowName()) { 1023a25f0a04SGreg Roach $list[] = $record; 1024a25f0a04SGreg Roach } 1025a25f0a04SGreg Roach } 1026cbc1590aSGreg Roach 1027a25f0a04SGreg Roach return $list; 1028a25f0a04SGreg Roach } 1029a25f0a04SGreg Roach 1030a25f0a04SGreg Roach /** 1031a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 1032a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 1033a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 1034a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 1035a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 1036a25f0a04SGreg Roach * 10378d0ebef0SGreg Roach * @param string[] $events 1038a25f0a04SGreg Roach * 1039a25f0a04SGreg Roach * @return Date[] 1040a25f0a04SGreg Roach */ 10418d0ebef0SGreg Roach public function getAllEventDates(array $events): array 1042c1010edaSGreg Roach { 104313abd6f3SGreg Roach $dates = []; 10448d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 10452decada7SGreg Roach if ($event->date()->isOK()) { 10462decada7SGreg Roach $dates[] = $event->date(); 1047a25f0a04SGreg Roach } 1048a25f0a04SGreg Roach } 1049a25f0a04SGreg Roach 1050a25f0a04SGreg Roach return $dates; 1051a25f0a04SGreg Roach } 1052a25f0a04SGreg Roach 1053a25f0a04SGreg Roach /** 1054a25f0a04SGreg Roach * Get all the places for a particular type of event 1055a25f0a04SGreg Roach * 10568d0ebef0SGreg Roach * @param string[] $events 1057a25f0a04SGreg Roach * 10584080d558SGreg Roach * @return Place[] 1059a25f0a04SGreg Roach */ 10608d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 1061c1010edaSGreg Roach { 106213abd6f3SGreg Roach $places = []; 10638d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 1064138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 1065a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 106616d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 1067a25f0a04SGreg Roach } 1068a25f0a04SGreg Roach } 1069a25f0a04SGreg Roach } 1070a25f0a04SGreg Roach 1071a25f0a04SGreg Roach return $places; 1072a25f0a04SGreg Roach } 1073a25f0a04SGreg Roach 1074a25f0a04SGreg Roach /** 1075a25f0a04SGreg Roach * The facts and events for this record. 1076a25f0a04SGreg Roach * 10778d0ebef0SGreg Roach * @param string[] $filter 1078cbc1590aSGreg Roach * @param bool $sort 1079cbc1590aSGreg Roach * @param int|null $access_level 1080cbc1590aSGreg Roach * @param bool $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES. 1081a25f0a04SGreg Roach * 108254c7f8dfSGreg Roach * @return Collection 108354c7f8dfSGreg Roach * @return Fact[] 1084a25f0a04SGreg Roach */ 108539ca88baSGreg Roach public function facts(array $filter = [], bool $sort = false, int $access_level = null, bool $override = false): Collection 1086c1010edaSGreg Roach { 10874b9ff166SGreg Roach if ($access_level === null) { 10884b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 10894b9ff166SGreg Roach } 10904b9ff166SGreg Roach 10918af3e5c1SGreg Roach $facts = new Collection(); 1092a25f0a04SGreg Roach if ($this->canShow($access_level) || $override) { 1093a25f0a04SGreg Roach foreach ($this->facts as $fact) { 1094*22d65e5aSGreg Roach if (($filter === [] || in_array($fact->getTag(), $filter, true)) && $fact->canShow($access_level)) { 10958af3e5c1SGreg Roach $facts->push($fact); 1096a25f0a04SGreg Roach } 1097a25f0a04SGreg Roach } 1098a25f0a04SGreg Roach } 1099d17d7b9eSGreg Roach 1100a25f0a04SGreg Roach if ($sort) { 1101580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 1102a25f0a04SGreg Roach } 1103cbc1590aSGreg Roach 110439ca88baSGreg Roach return new Collection($facts); 1105a25f0a04SGreg Roach } 1106a25f0a04SGreg Roach 1107a25f0a04SGreg Roach /** 11084459dc9aSGreg Roach * Get the last-change timestamp for this record 1109a25f0a04SGreg Roach * 11104459dc9aSGreg Roach * @return Carbon 1111a25f0a04SGreg Roach */ 11124459dc9aSGreg Roach public function lastChangeTimestamp(): Carbon 1113c1010edaSGreg Roach { 11144459dc9aSGreg Roach /** @var Fact|null $chan */ 1115820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 1116a25f0a04SGreg Roach 11174459dc9aSGreg Roach if ($chan instanceof Fact) { 1118a25f0a04SGreg Roach // The record does have a CHAN event 11192decada7SGreg Roach $d = $chan->date()->minimumDate(); 11204459dc9aSGreg Roach 1121138ca96cSGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->gedcom(), $match)) { 11224459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2], (int) $match[3]); 1123e364afe4SGreg Roach } 1124e364afe4SGreg Roach 1125e364afe4SGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->gedcom(), $match)) { 11264459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2]); 1127b2ce94c6SRico Sonntag } 1128b2ce94c6SRico Sonntag 11294459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day()); 1130a25f0a04SGreg Roach } 1131b2ce94c6SRico Sonntag 1132a25f0a04SGreg Roach // The record does not have a CHAN event 11334459dc9aSGreg Roach return Carbon::createFromTimestamp(0); 1134a25f0a04SGreg Roach } 1135a25f0a04SGreg Roach 1136a25f0a04SGreg Roach /** 1137a25f0a04SGreg Roach * Get the last-change user for this record 1138a25f0a04SGreg Roach * 1139a25f0a04SGreg Roach * @return string 1140a25f0a04SGreg Roach */ 1141e364afe4SGreg Roach public function lastChangeUser(): string 1142c1010edaSGreg Roach { 1143820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 1144a25f0a04SGreg Roach 1145a25f0a04SGreg Roach if ($chan === null) { 1146a25f0a04SGreg Roach return I18N::translate('Unknown'); 1147b2ce94c6SRico Sonntag } 1148b2ce94c6SRico Sonntag 11493425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 1150baacc364SGreg Roach if ($chan_user === '') { 1151a25f0a04SGreg Roach return I18N::translate('Unknown'); 1152b2ce94c6SRico Sonntag } 1153b2ce94c6SRico Sonntag 1154a25f0a04SGreg Roach return $chan_user; 1155a25f0a04SGreg Roach } 1156a25f0a04SGreg Roach 1157a25f0a04SGreg Roach /** 1158a25f0a04SGreg Roach * Add a new fact to this record 1159a25f0a04SGreg Roach * 1160a25f0a04SGreg Roach * @param string $gedcom 1161cbc1590aSGreg Roach * @param bool $update_chan 11627e96c925SGreg Roach * 11637e96c925SGreg Roach * @return void 1164a25f0a04SGreg Roach */ 1165e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 1166c1010edaSGreg Roach { 1167fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 1168a25f0a04SGreg Roach } 1169a25f0a04SGreg Roach 1170a25f0a04SGreg Roach /** 1171a25f0a04SGreg Roach * Delete a fact from this record 1172a25f0a04SGreg Roach * 1173a25f0a04SGreg Roach * @param string $fact_id 1174cbc1590aSGreg Roach * @param bool $update_chan 11757e96c925SGreg Roach * 11767e96c925SGreg Roach * @return void 1177a25f0a04SGreg Roach */ 1178e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 1179c1010edaSGreg Roach { 1180db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 1181a25f0a04SGreg Roach } 1182a25f0a04SGreg Roach 1183a25f0a04SGreg Roach /** 1184a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 1185a25f0a04SGreg Roach * 1186a25f0a04SGreg Roach * @param string $fact_id 1187a25f0a04SGreg Roach * @param string $gedcom 1188cbc1590aSGreg Roach * @param bool $update_chan 1189a25f0a04SGreg Roach * 11907e96c925SGreg Roach * @return void 11917e96c925SGreg Roach * @throws Exception 1192a25f0a04SGreg Roach */ 1193e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 1194c1010edaSGreg Roach { 1195a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1196a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1197a25f0a04SGreg Roach $gedcom = trim($gedcom); 1198a25f0a04SGreg Roach 1199a25f0a04SGreg Roach if ($this->pending === '') { 12007e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 1201a25f0a04SGreg Roach } 12028d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 12037e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 1204a25f0a04SGreg Roach } 1205a25f0a04SGreg Roach 1206a25f0a04SGreg Roach if ($this->pending) { 1207a25f0a04SGreg Roach $old_gedcom = $this->pending; 1208a25f0a04SGreg Roach } else { 1209a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 1210a25f0a04SGreg Roach } 1211a25f0a04SGreg Roach 1212a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 121365e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 1214a25f0a04SGreg Roach 1215a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 12168d0ebef0SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE) as $fact) { 1217a25f0a04SGreg Roach if (!$fact->isPendingDeletion()) { 1218905ab80aSGreg Roach if ($fact->id() === $fact_id) { 1219db7bb364SGreg Roach if ($gedcom !== '') { 1220a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1221a25f0a04SGreg Roach } 1222fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 1223e364afe4SGreg Roach } elseif ($fact->getTag() !== 'CHAN' || !$update_chan) { 1224138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 1225a25f0a04SGreg Roach } 1226a25f0a04SGreg Roach } 1227a25f0a04SGreg Roach } 1228a25f0a04SGreg Roach if ($update_chan) { 1229e5a6b4d4SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1230a25f0a04SGreg Roach } 1231a25f0a04SGreg Roach 1232a25f0a04SGreg Roach // Adding a new fact 1233fc3ccce4SGreg Roach if ($fact_id === '') { 1234a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1235a25f0a04SGreg Roach } 1236a25f0a04SGreg Roach 1237e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 1238a25f0a04SGreg Roach // Save the changes 1239d09b6323SGreg Roach DB::table('change')->insert([ 1240d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1241d09b6323SGreg Roach 'xref' => $this->xref, 1242d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 1243d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 1244d09b6323SGreg Roach 'user_id' => Auth::id(), 124513abd6f3SGreg Roach ]); 1246a25f0a04SGreg Roach 1247a25f0a04SGreg Roach $this->pending = $new_gedcom; 1248a25f0a04SGreg Roach 1249a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1250cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($this->xref, $this->tree); 1251a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 1252a25f0a04SGreg Roach $this->pending = null; 1253a25f0a04SGreg Roach } 1254a25f0a04SGreg Roach } 1255a25f0a04SGreg Roach $this->parseFacts(); 1256a25f0a04SGreg Roach } 1257a25f0a04SGreg Roach 1258a25f0a04SGreg Roach /** 1259a25f0a04SGreg Roach * Update this record 1260a25f0a04SGreg Roach * 1261a25f0a04SGreg Roach * @param string $gedcom 1262cbc1590aSGreg Roach * @param bool $update_chan 12637e96c925SGreg Roach * 12647e96c925SGreg Roach * @return void 1265a25f0a04SGreg Roach */ 1266e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 1267c1010edaSGreg Roach { 1268a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1269a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1270a25f0a04SGreg Roach $gedcom = trim($gedcom); 1271a25f0a04SGreg Roach 1272a25f0a04SGreg Roach // Update the CHAN record 1273a25f0a04SGreg Roach if ($update_chan) { 1274a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 1275e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1276a25f0a04SGreg Roach } 1277a25f0a04SGreg Roach 1278a25f0a04SGreg Roach // Create a pending change 1279d09b6323SGreg Roach DB::table('change')->insert([ 1280d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1281d09b6323SGreg Roach 'xref' => $this->xref, 1282d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1283d09b6323SGreg Roach 'new_gedcom' => $gedcom, 1284d09b6323SGreg Roach 'user_id' => Auth::id(), 128513abd6f3SGreg Roach ]); 1286a25f0a04SGreg Roach 1287a25f0a04SGreg Roach // Clear the cache 1288a25f0a04SGreg Roach $this->pending = $gedcom; 1289a25f0a04SGreg Roach 1290a25f0a04SGreg Roach // Accept this pending change 1291a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1292cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($this->xref, $this->tree); 1293a25f0a04SGreg Roach $this->gedcom = $gedcom; 1294a25f0a04SGreg Roach $this->pending = null; 1295a25f0a04SGreg Roach } 1296a25f0a04SGreg Roach 1297a25f0a04SGreg Roach $this->parseFacts(); 1298a25f0a04SGreg Roach 1299847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1300a25f0a04SGreg Roach } 1301a25f0a04SGreg Roach 1302a25f0a04SGreg Roach /** 1303a25f0a04SGreg Roach * Delete this record 1304b874da82SGreg Roach * 1305b874da82SGreg Roach * @return void 1306a25f0a04SGreg Roach */ 1307e364afe4SGreg Roach public function deleteRecord(): void 1308c1010edaSGreg Roach { 1309a25f0a04SGreg Roach // Create a pending change 13104c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 1311d09b6323SGreg Roach DB::table('change')->insert([ 1312d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1313d09b6323SGreg Roach 'xref' => $this->xref, 1314d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1315d09b6323SGreg Roach 'new_gedcom' => '', 1316d09b6323SGreg Roach 'user_id' => Auth::id(), 131713abd6f3SGreg Roach ]); 13184c0b5256SGreg Roach } 1319a25f0a04SGreg Roach 13204c0b5256SGreg Roach // Auto-accept this pending change 1321a25f0a04SGreg Roach if (Auth::user()->getPreference('auto_accept')) { 1322cc5684fdSGreg Roach FunctionsImport::acceptAllChanges($this->xref, $this->tree); 1323a25f0a04SGreg Roach } 1324a25f0a04SGreg Roach 1325a25f0a04SGreg Roach // Clear the cache 1326d17d7b9eSGreg Roach self::$gedcom_record_cache = []; 1327d17d7b9eSGreg Roach self::$pending_record_cache = []; 1328a25f0a04SGreg Roach 1329847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1330a25f0a04SGreg Roach } 1331a25f0a04SGreg Roach 1332a25f0a04SGreg Roach /** 1333a25f0a04SGreg Roach * Remove all links from this record to $xref 1334a25f0a04SGreg Roach * 1335a25f0a04SGreg Roach * @param string $xref 1336cbc1590aSGreg Roach * @param bool $update_chan 13377e96c925SGreg Roach * 13387e96c925SGreg Roach * @return void 1339a25f0a04SGreg Roach */ 1340e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 1341c1010edaSGreg Roach { 1342a25f0a04SGreg Roach $value = '@' . $xref . '@'; 1343a25f0a04SGreg Roach 134430158ae7SGreg Roach foreach ($this->facts() as $fact) { 134584586c02SGreg Roach if ($fact->value() === $value) { 1346905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 13478d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 1348138ca96cSGreg Roach $gedcom = $fact->gedcom(); 1349a25f0a04SGreg Roach foreach ($matches as $match) { 1350a25f0a04SGreg Roach $next_level = $match[1] + 1; 1351a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 1352a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 1353a25f0a04SGreg Roach } 1354905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 1355a25f0a04SGreg Roach } 1356a25f0a04SGreg Roach } 1357a25f0a04SGreg Roach } 1358bf4eb542SGreg Roach 1359bf4eb542SGreg Roach /** 1360bf4eb542SGreg Roach * Fetch XREFs of all records linked to a record - when deleting an object, we must 1361bf4eb542SGreg Roach * also delete all links to it. 1362bf4eb542SGreg Roach * 1363bf4eb542SGreg Roach * @return GedcomRecord[] 1364bf4eb542SGreg Roach */ 1365bf4eb542SGreg Roach public function linkingRecords(): array 1366bf4eb542SGreg Roach { 1367bf4eb542SGreg Roach $union = DB::table('change') 1368bf4eb542SGreg Roach ->where('gedcom_id', '=', $this->tree()->id()) 1369fbbe964bSGreg Roach ->whereContains('new_gedcom', '@' . $this->xref() . '@') 1370bf4eb542SGreg Roach ->where('new_gedcom', 'NOT LIKE', '0 @' . $this->xref() . '@%') 1371bf4eb542SGreg Roach ->select(['xref']); 1372bf4eb542SGreg Roach 1373bf4eb542SGreg Roach $xrefs = DB::table('link') 1374bf4eb542SGreg Roach ->where('l_file', '=', $this->tree()->id()) 1375bf4eb542SGreg Roach ->where('l_to', '=', $this->xref()) 1376bf4eb542SGreg Roach ->select('l_from') 1377bf4eb542SGreg Roach ->union($union) 1378bf4eb542SGreg Roach ->pluck('l_from'); 1379bf4eb542SGreg Roach 1380bf4eb542SGreg Roach return $xrefs->map(function (string $xref): GedcomRecord { 1381bf4eb542SGreg Roach return GedcomRecord::getInstance($xref, $this->tree); 1382bf4eb542SGreg Roach })->all(); 1383bf4eb542SGreg Roach } 1384a25f0a04SGreg Roach} 1385