1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 2176692c8bSGreg Roach 22886b77daSGreg Roachuse Closure; 237e96c925SGreg Roachuse Exception; 243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 255818a371SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomRecordPage; 2622e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService; 27bf4eb542SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2883242252SGreg Roachuse Illuminate\Database\Query\Builder; 2983242252SGreg Roachuse Illuminate\Database\Query\Expression; 30ba1c12e8SGreg Roachuse Illuminate\Database\Query\JoinClause; 3139ca88baSGreg Roachuse Illuminate\Support\Collection; 3279529c87SGreg Roachuse stdClass; 33a25f0a04SGreg Roach 3422e73debSGreg Roachuse function app; 3522e73debSGreg Roach 36a25f0a04SGreg Roach/** 3776692c8bSGreg Roach * A GEDCOM object. 38a25f0a04SGreg Roach */ 39c1010edaSGreg Roachclass GedcomRecord 40c1010edaSGreg Roach{ 4116d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 4216d6367aSGreg Roach 435818a371SGreg Roach protected const ROUTE_NAME = GedcomRecordPage::class; 445818a371SGreg Roach 4576692c8bSGreg Roach /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */ 46bec87e94SGreg Roach public static $gedcom_record_cache; 4779529c87SGreg Roach /** @var stdClass[][] Fetch all pending edits in one database query */ 48bec87e94SGreg Roach public static $pending_record_cache; 4922e73debSGreg Roach /** @var string The record identifier */ 5022e73debSGreg Roach protected $xref; 5122e73debSGreg Roach /** @var Tree The family tree to which this record belongs */ 5222e73debSGreg Roach protected $tree; 5322e73debSGreg Roach /** @var string GEDCOM data (before any pending edits) */ 5422e73debSGreg Roach protected $gedcom; 5522e73debSGreg Roach /** @var string|null GEDCOM data (after any pending edits) */ 5622e73debSGreg Roach protected $pending; 5722e73debSGreg Roach /** @var Fact[] facts extracted from $gedcom/$pending */ 5822e73debSGreg Roach protected $facts; 5922e73debSGreg Roach /** @var string[][] All the names of this individual */ 6022e73debSGreg Roach protected $getAllNames; 6122e73debSGreg Roach /** @var int|null Cached result */ 6222e73debSGreg Roach protected $getPrimaryName; 6322e73debSGreg Roach /** @var int|null Cached result */ 6422e73debSGreg Roach protected $getSecondaryName; 65a25f0a04SGreg Roach 66a25f0a04SGreg Roach /** 67a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 68a25f0a04SGreg Roach * 69a25f0a04SGreg Roach * @param string $xref 70a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 71a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 72a25f0a04SGreg Roach * empty string for records with pending deletions 7324ec66ceSGreg Roach * @param Tree $tree 74a25f0a04SGreg Roach */ 75e364afe4SGreg Roach public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 76c1010edaSGreg Roach { 77a25f0a04SGreg Roach $this->xref = $xref; 78a25f0a04SGreg Roach $this->gedcom = $gedcom; 79a25f0a04SGreg Roach $this->pending = $pending; 8024ec66ceSGreg Roach $this->tree = $tree; 81a25f0a04SGreg Roach 82a25f0a04SGreg Roach $this->parseFacts(); 83a25f0a04SGreg Roach } 84a25f0a04SGreg Roach 85a25f0a04SGreg Roach /** 86886b77daSGreg Roach * A closure which will create a record from a database row. 87886b77daSGreg Roach * 88d5ad3db0SGreg Roach * @param Tree $tree 89d5ad3db0SGreg Roach * 90886b77daSGreg Roach * @return Closure 91886b77daSGreg Roach */ 92d5ad3db0SGreg Roach public static function rowMapper(Tree $tree): Closure 93886b77daSGreg Roach { 94d5ad3db0SGreg Roach return static function (stdClass $row) use ($tree): GedcomRecord { 95d5ad3db0SGreg Roach return GedcomRecord::getInstance($row->o_id, $tree, $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 { 1066c2179e2SGreg Roach return static 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 { 1186c2179e2SGreg Roach return static 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 { 1446c2179e2SGreg Roach return static 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 * Get an instance of a GedcomRecord object. For single records, 151a25f0a04SGreg Roach * we just receive the XREF. For bulk records (such as lists 152a25f0a04SGreg Roach * and search results) we can receive the GEDCOM data as well. 153a25f0a04SGreg Roach * 154a25f0a04SGreg Roach * @param string $xref 15524ec66ceSGreg Roach * @param Tree $tree 156a25f0a04SGreg Roach * @param string|null $gedcom 157a25f0a04SGreg Roach * 15884658595SGreg Roach * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null 15922e73debSGreg Roach * @throws Exception 160a25f0a04SGreg Roach */ 16176f666f4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 162c1010edaSGreg Roach { 16372cf66d4SGreg Roach $tree_id = $tree->id(); 16424ec66ceSGreg Roach 165e71ef9d2SGreg Roach // Is this record already in the cache? 16624ec66ceSGreg Roach if (isset(self::$gedcom_record_cache[$xref][$tree_id])) { 167e71ef9d2SGreg Roach return self::$gedcom_record_cache[$xref][$tree_id]; 168a25f0a04SGreg Roach } 169a25f0a04SGreg Roach 170a25f0a04SGreg Roach // Do we need to fetch the record from the database? 171a25f0a04SGreg Roach if ($gedcom === null) { 17224ec66ceSGreg Roach $gedcom = static::fetchGedcomRecord($xref, $tree_id); 173a25f0a04SGreg Roach } 174a25f0a04SGreg Roach 175a25f0a04SGreg Roach // If we can edit, then we also need to be able to see pending records. 17694075df0SGreg Roach if (Auth::isEditor($tree)) { 17724ec66ceSGreg Roach if (!isset(self::$pending_record_cache[$tree_id])) { 178a25f0a04SGreg Roach // Fetch all pending records in one database query 17913abd6f3SGreg Roach self::$pending_record_cache[$tree_id] = []; 18085fc8064SGreg Roach $rows = DB::table('change') 18185fc8064SGreg Roach ->where('gedcom_id', '=', $tree_id) 18285fc8064SGreg Roach ->where('status', '=', 'pending') 18385fc8064SGreg Roach ->orderBy('change_id') 18485fc8064SGreg Roach ->select(['xref', 'new_gedcom']) 18585fc8064SGreg Roach ->get(); 186d17d7b9eSGreg Roach 187a25f0a04SGreg Roach foreach ($rows as $row) { 18824ec66ceSGreg Roach self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom; 189a25f0a04SGreg Roach } 190a25f0a04SGreg Roach } 191a25f0a04SGreg Roach 192d17d7b9eSGreg Roach $pending = self::$pending_record_cache[$tree_id][$xref] ?? null; 193a25f0a04SGreg Roach } else { 194a25f0a04SGreg Roach // There are no pending changes for this record 195a25f0a04SGreg Roach $pending = null; 196a25f0a04SGreg Roach } 197a25f0a04SGreg Roach 198a25f0a04SGreg Roach // No such record exists 199a25f0a04SGreg Roach if ($gedcom === null && $pending === null) { 200a25f0a04SGreg Roach return null; 201a25f0a04SGreg Roach } 202a25f0a04SGreg Roach 203fc3ccce4SGreg Roach // No such record, but a pending creation exists 204fc3ccce4SGreg Roach if ($gedcom === null) { 205fc3ccce4SGreg Roach $gedcom = ''; 206fc3ccce4SGreg Roach } 207fc3ccce4SGreg Roach 208a25f0a04SGreg Roach // Create the object 2098d0ebef0SGreg Roach if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ (' . Gedcom::REGEX_TAG . ')/', $gedcom . $pending, $match)) { 210a25f0a04SGreg Roach $xref = $match[1]; // Collation - we may have requested I123 and found i123 211a25f0a04SGreg Roach $type = $match[2]; 212a25f0a04SGreg Roach } elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) { 213a25f0a04SGreg Roach $xref = $match[1]; 214a25f0a04SGreg Roach $type = $match[1]; 215a25f0a04SGreg Roach } elseif ($gedcom . $pending) { 2167e96c925SGreg Roach throw new Exception('Unrecognized GEDCOM record: ' . $gedcom); 217a25f0a04SGreg Roach } else { 218a25f0a04SGreg Roach // A record with both pending creation and pending deletion 219a25f0a04SGreg Roach $type = static::RECORD_TYPE; 220a25f0a04SGreg Roach } 221a25f0a04SGreg Roach 222a25f0a04SGreg Roach switch ($type) { 223a25f0a04SGreg Roach case 'INDI': 22424ec66ceSGreg Roach $record = new Individual($xref, $gedcom, $pending, $tree); 225a25f0a04SGreg Roach break; 226a25f0a04SGreg Roach case 'FAM': 22724ec66ceSGreg Roach $record = new Family($xref, $gedcom, $pending, $tree); 228a25f0a04SGreg Roach break; 229a25f0a04SGreg Roach case 'SOUR': 23024ec66ceSGreg Roach $record = new Source($xref, $gedcom, $pending, $tree); 231a25f0a04SGreg Roach break; 232a25f0a04SGreg Roach case 'OBJE': 23324ec66ceSGreg Roach $record = new Media($xref, $gedcom, $pending, $tree); 234a25f0a04SGreg Roach break; 235a25f0a04SGreg Roach case 'REPO': 23624ec66ceSGreg Roach $record = new Repository($xref, $gedcom, $pending, $tree); 237a25f0a04SGreg Roach break; 238a25f0a04SGreg Roach case 'NOTE': 23924ec66ceSGreg Roach $record = new Note($xref, $gedcom, $pending, $tree); 240a25f0a04SGreg Roach break; 241a25f0a04SGreg Roach default: 24206ef8e02SGreg Roach $record = new self($xref, $gedcom, $pending, $tree); 243a25f0a04SGreg Roach break; 244a25f0a04SGreg Roach } 245a25f0a04SGreg Roach 246a25f0a04SGreg Roach // Store it in the cache 24724ec66ceSGreg Roach self::$gedcom_record_cache[$xref][$tree_id] = $record; 248a25f0a04SGreg Roach 249a25f0a04SGreg Roach return $record; 250a25f0a04SGreg Roach } 251a25f0a04SGreg Roach 252a25f0a04SGreg Roach /** 253a25f0a04SGreg Roach * Fetch data from the database 254a25f0a04SGreg Roach * 255a25f0a04SGreg Roach * @param string $xref 256cbc1590aSGreg Roach * @param int $tree_id 257a25f0a04SGreg Roach * 258e364afe4SGreg Roach * @return string|null 259a25f0a04SGreg Roach */ 260e364afe4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 261c1010edaSGreg Roach { 262a25f0a04SGreg Roach // We don't know what type of object this is. Try each one in turn. 26364d9078aSGreg Roach $data = Individual::fetchGedcomRecord($xref, $tree_id); 26485fc8064SGreg Roach if ($data !== null) { 265a25f0a04SGreg Roach return $data; 266a25f0a04SGreg Roach } 26764d9078aSGreg Roach $data = Family::fetchGedcomRecord($xref, $tree_id); 26885fc8064SGreg Roach if ($data !== null) { 269a25f0a04SGreg Roach return $data; 270a25f0a04SGreg Roach } 27164d9078aSGreg Roach $data = Source::fetchGedcomRecord($xref, $tree_id); 27285fc8064SGreg Roach if ($data !== null) { 273a25f0a04SGreg Roach return $data; 274a25f0a04SGreg Roach } 27564d9078aSGreg Roach $data = Repository::fetchGedcomRecord($xref, $tree_id); 27685fc8064SGreg Roach if ($data !== null) { 277a25f0a04SGreg Roach return $data; 278a25f0a04SGreg Roach } 27964d9078aSGreg Roach $data = Media::fetchGedcomRecord($xref, $tree_id); 28085fc8064SGreg Roach if ($data !== null) { 281a25f0a04SGreg Roach return $data; 282a25f0a04SGreg Roach } 28364d9078aSGreg Roach $data = Note::fetchGedcomRecord($xref, $tree_id); 28485fc8064SGreg Roach if ($data !== null) { 285a25f0a04SGreg Roach return $data; 286a25f0a04SGreg Roach } 287c1010edaSGreg Roach 288a25f0a04SGreg Roach // Some other type of record... 289d09b6323SGreg Roach return DB::table('other') 29085fc8064SGreg Roach ->where('o_file', '=', $tree_id) 29185fc8064SGreg Roach ->where('o_id', '=', $xref) 29285fc8064SGreg Roach ->value('o_gedcom'); 293a25f0a04SGreg Roach } 294a25f0a04SGreg Roach 295a25f0a04SGreg Roach /** 296a25f0a04SGreg Roach * Get the XREF for this record 297a25f0a04SGreg Roach * 298a25f0a04SGreg Roach * @return string 299a25f0a04SGreg Roach */ 300c0935879SGreg Roach public function xref(): string 301c1010edaSGreg Roach { 302a25f0a04SGreg Roach return $this->xref; 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach 305a25f0a04SGreg Roach /** 306000959d9SGreg Roach * Get the tree to which this record belongs 307000959d9SGreg Roach * 308000959d9SGreg Roach * @return Tree 309000959d9SGreg Roach */ 310f4afa648SGreg Roach public function tree(): Tree 311c1010edaSGreg Roach { 312518bbdc1SGreg Roach return $this->tree; 313000959d9SGreg Roach } 314000959d9SGreg Roach 315000959d9SGreg Roach /** 316a25f0a04SGreg Roach * Application code should access data via Fact objects. 317a25f0a04SGreg Roach * This function exists to support old code. 318a25f0a04SGreg Roach * 319a25f0a04SGreg Roach * @return string 320a25f0a04SGreg Roach */ 321e364afe4SGreg Roach public function gedcom(): string 322c1010edaSGreg Roach { 323b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 324a25f0a04SGreg Roach } 325a25f0a04SGreg Roach 326a25f0a04SGreg Roach /** 327a25f0a04SGreg Roach * Does this record have a pending change? 328a25f0a04SGreg Roach * 329cbc1590aSGreg Roach * @return bool 330a25f0a04SGreg Roach */ 3318f53f488SRico Sonntag public function isPendingAddition(): bool 332c1010edaSGreg Roach { 333a25f0a04SGreg Roach return $this->pending !== null; 334a25f0a04SGreg Roach } 335a25f0a04SGreg Roach 336a25f0a04SGreg Roach /** 337a25f0a04SGreg Roach * Does this record have a pending deletion? 338a25f0a04SGreg Roach * 339cbc1590aSGreg Roach * @return bool 340a25f0a04SGreg Roach */ 3418f53f488SRico Sonntag public function isPendingDeletion(): bool 342c1010edaSGreg Roach { 343a25f0a04SGreg Roach return $this->pending === ''; 344a25f0a04SGreg Roach } 345a25f0a04SGreg Roach 346a25f0a04SGreg Roach /** 347ee4364daSGreg Roach * Generate a "slug" to use in pretty URLs. 348ee4364daSGreg Roach * 349ee4364daSGreg Roach * @return string 350ee4364daSGreg Roach */ 351ee4364daSGreg Roach public function slug(): string 352ee4364daSGreg Roach { 353afa9f52aSGreg Roach return strip_tags($this->fullName()); 354ee4364daSGreg Roach } 355ee4364daSGreg Roach 356ee4364daSGreg Roach /** 357225e381fSGreg Roach * Generate a URL to this record. 358a25f0a04SGreg Roach * 359a25f0a04SGreg Roach * @return string 360a25f0a04SGreg Roach */ 3618f53f488SRico Sonntag public function url(): string 362c1010edaSGreg Roach { 363225e381fSGreg Roach return route(static::ROUTE_NAME, [ 364c0935879SGreg Roach 'xref' => $this->xref(), 365ee4364daSGreg Roach 'tree' => $this->tree->name(), 366ee4364daSGreg Roach 'slug' => $this->slug(), 367225e381fSGreg Roach ]); 368a25f0a04SGreg Roach } 369a25f0a04SGreg Roach 370a25f0a04SGreg Roach /** 371a25f0a04SGreg Roach * Can the details of this record be shown? 372a25f0a04SGreg Roach * 373cbc1590aSGreg Roach * @param int|null $access_level 374a25f0a04SGreg Roach * 375cbc1590aSGreg Roach * @return bool 376a25f0a04SGreg Roach */ 37735584196SGreg Roach public function canShow(int $access_level = null): bool 378c1010edaSGreg Roach { 379f0b9c048SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 3804b9ff166SGreg Roach 381a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 382a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 383f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 384a25f0a04SGreg Roach return true; 385a25f0a04SGreg Roach } 386f0b9c048SGreg Roach 3877476e8a5SGreg Roach $cache_key = 'show-' . $this->xref . '-' . $this->tree->id() . '-' . $access_level; 388f0b9c048SGreg Roach 389c692965aSGreg Roach return app('cache.array')->remember($cache_key, function () use ($access_level) { 390f0b9c048SGreg Roach return $this->canShowRecord($access_level); 391f0b9c048SGreg Roach }); 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach 394a25f0a04SGreg Roach /** 395a25f0a04SGreg Roach * Can the name of this record be shown? 396a25f0a04SGreg Roach * 397cbc1590aSGreg Roach * @param int|null $access_level 398a25f0a04SGreg Roach * 399cbc1590aSGreg Roach * @return bool 400a25f0a04SGreg Roach */ 40176f666f4SGreg Roach public function canShowName(int $access_level = null): bool 402c1010edaSGreg Roach { 403a25f0a04SGreg Roach return $this->canShow($access_level); 404a25f0a04SGreg Roach } 405a25f0a04SGreg Roach 406a25f0a04SGreg Roach /** 407a25f0a04SGreg Roach * Can we edit this record? 408a25f0a04SGreg Roach * 409cbc1590aSGreg Roach * @return bool 410a25f0a04SGreg Roach */ 4118f53f488SRico Sonntag public function canEdit(): bool 412c1010edaSGreg Roach { 4131450f098SGreg Roach if ($this->isPendingDeletion()) { 4141450f098SGreg Roach return false; 4151450f098SGreg Roach } 4161450f098SGreg Roach 4171450f098SGreg Roach if (Auth::isManager($this->tree)) { 4181450f098SGreg Roach return true; 4191450f098SGreg Roach } 4201450f098SGreg Roach 4211450f098SGreg Roach return Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false; 422a25f0a04SGreg Roach } 423a25f0a04SGreg Roach 424a25f0a04SGreg Roach /** 425a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 426a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 427a25f0a04SGreg Roach * 428cbc1590aSGreg Roach * @param int $access_level 429a25f0a04SGreg Roach * 430a25f0a04SGreg Roach * @return string 431a25f0a04SGreg Roach */ 432e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 433c1010edaSGreg Roach { 434e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 435a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 436a25f0a04SGreg Roach return $this->gedcom; 437b2ce94c6SRico Sonntag } 438b2ce94c6SRico Sonntag 439b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 440a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 441a25f0a04SGreg Roach 442a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 44365e02381SGreg Roach [$gedrec] = explode("\n", $this->gedcom, 2); 444a25f0a04SGreg Roach 445a25f0a04SGreg Roach // Check each of the facts for access 4468d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 447138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 448a25f0a04SGreg Roach } 449cbc1590aSGreg Roach 450a25f0a04SGreg Roach return $gedrec; 451b2ce94c6SRico Sonntag } 452b2ce94c6SRico Sonntag 453a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 454a25f0a04SGreg Roach // limited data, such as links to other records. 455a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 456a25f0a04SGreg Roach } 457a25f0a04SGreg Roach 458a25f0a04SGreg Roach /** 459a25f0a04SGreg Roach * Default for "other" object types 460c7ff4153SGreg Roach * 461c7ff4153SGreg Roach * @return void 462a25f0a04SGreg Roach */ 463e364afe4SGreg Roach public function extractNames(): void 464c1010edaSGreg Roach { 46576f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 466a25f0a04SGreg Roach } 467a25f0a04SGreg Roach 468a25f0a04SGreg Roach /** 469a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 470a25f0a04SGreg Roach * 471a25f0a04SGreg Roach * @return string[][] 472a25f0a04SGreg Roach */ 4738f53f488SRico Sonntag public function getAllNames(): array 474c1010edaSGreg Roach { 475bdb3725aSGreg Roach if ($this->getAllNames === null) { 476bdb3725aSGreg Roach $this->getAllNames = []; 477a25f0a04SGreg Roach if ($this->canShowName()) { 478a25f0a04SGreg Roach // Ask the record to extract its names 479a25f0a04SGreg Roach $this->extractNames(); 480a25f0a04SGreg Roach // No name found? Use a fallback. 481bdb3725aSGreg Roach if (!$this->getAllNames) { 482db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 483a25f0a04SGreg Roach } 484a25f0a04SGreg Roach } else { 485db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 486a25f0a04SGreg Roach } 487a25f0a04SGreg Roach } 488cbc1590aSGreg Roach 489bdb3725aSGreg Roach return $this->getAllNames; 490a25f0a04SGreg Roach } 491a25f0a04SGreg Roach 492a25f0a04SGreg Roach /** 493a25f0a04SGreg Roach * If this object has no name, what do we call it? 494a25f0a04SGreg Roach * 495a25f0a04SGreg Roach * @return string 496a25f0a04SGreg Roach */ 4978f53f488SRico Sonntag public function getFallBackName(): string 498c1010edaSGreg Roach { 499c0935879SGreg Roach return e($this->xref()); 500a25f0a04SGreg Roach } 501a25f0a04SGreg Roach 502a25f0a04SGreg Roach /** 503a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 504a25f0a04SGreg Roach * 505cbc1590aSGreg Roach * @return int 506a25f0a04SGreg Roach */ 5078f53f488SRico Sonntag public function getPrimaryName(): int 508c1010edaSGreg Roach { 509a25f0a04SGreg Roach static $language_script; 510a25f0a04SGreg Roach 511a25f0a04SGreg Roach if ($language_script === null) { 51265cf5706SGreg Roach $language_script = $language_script ?? I18N::locale()->script()->code(); 513a25f0a04SGreg Roach } 514a25f0a04SGreg Roach 515bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 516a25f0a04SGreg Roach // Generally, the first name is the primary one.... 517bdb3725aSGreg Roach $this->getPrimaryName = 0; 518a25f0a04SGreg Roach // ...except when the language/name use different character sets 519a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 52069546be1SGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 521bdb3725aSGreg Roach $this->getPrimaryName = $n; 522a25f0a04SGreg Roach break; 523a25f0a04SGreg Roach } 524a25f0a04SGreg Roach } 525a25f0a04SGreg Roach } 526a25f0a04SGreg Roach 527bdb3725aSGreg Roach return $this->getPrimaryName; 528a25f0a04SGreg Roach } 529a25f0a04SGreg Roach 530a25f0a04SGreg Roach /** 531a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 532a25f0a04SGreg Roach * 533cbc1590aSGreg Roach * @return int 534a25f0a04SGreg Roach */ 5358f53f488SRico Sonntag public function getSecondaryName(): int 536c1010edaSGreg Roach { 5378f038c36SRico Sonntag if ($this->getSecondaryName === null) { 538a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 539bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 540a25f0a04SGreg Roach // ....except when there are names with different character sets 541a25f0a04SGreg Roach $all_names = $this->getAllNames(); 542a25f0a04SGreg Roach if (count($all_names) > 1) { 543a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 544a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 545e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 546bdb3725aSGreg Roach $this->getSecondaryName = $n; 547a25f0a04SGreg Roach break; 548a25f0a04SGreg Roach } 549a25f0a04SGreg Roach } 550a25f0a04SGreg Roach } 551a25f0a04SGreg Roach } 552cbc1590aSGreg Roach 553bdb3725aSGreg Roach return $this->getSecondaryName; 554a25f0a04SGreg Roach } 555a25f0a04SGreg Roach 556a25f0a04SGreg Roach /** 557a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 558a25f0a04SGreg Roach * 55976f666f4SGreg Roach * @param int|null $n 5607e96c925SGreg Roach * 5617e96c925SGreg Roach * @return void 562a25f0a04SGreg Roach */ 563e364afe4SGreg Roach public function setPrimaryName(int $n = null): void 564c1010edaSGreg Roach { 565bdb3725aSGreg Roach $this->getPrimaryName = $n; 566bdb3725aSGreg Roach $this->getSecondaryName = null; 567a25f0a04SGreg Roach } 568a25f0a04SGreg Roach 569a25f0a04SGreg Roach /** 570a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 571a25f0a04SGreg Roach * 572a25f0a04SGreg Roach * @return string 573a25f0a04SGreg Roach */ 574c1010edaSGreg Roach public function __toString() 575c1010edaSGreg Roach { 57672cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 577a25f0a04SGreg Roach } 578a25f0a04SGreg Roach 579a25f0a04SGreg Roach /** 580c156e8f5SGreg Roach * /** 581a25f0a04SGreg Roach * Get variants of the name 582a25f0a04SGreg Roach * 583a25f0a04SGreg Roach * @return string 584a25f0a04SGreg Roach */ 585e364afe4SGreg Roach public function fullName(): string 586c1010edaSGreg Roach { 587a25f0a04SGreg Roach if ($this->canShowName()) { 588a25f0a04SGreg Roach $tmp = $this->getAllNames(); 589cbc1590aSGreg Roach 590a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 591a25f0a04SGreg Roach } 592b2ce94c6SRico Sonntag 593b2ce94c6SRico Sonntag return I18N::translate('Private'); 594a25f0a04SGreg Roach } 595a25f0a04SGreg Roach 596a25f0a04SGreg Roach /** 597a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 598a25f0a04SGreg Roach * 599a25f0a04SGreg Roach * @return string 600a25f0a04SGreg Roach */ 60139ca88baSGreg Roach public function sortName(): string 602c1010edaSGreg Roach { 603a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 604a25f0a04SGreg Roach $tmp = $this->getAllNames(); 605cbc1590aSGreg Roach 606a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 607a25f0a04SGreg Roach } 608a25f0a04SGreg Roach 609a25f0a04SGreg Roach /** 610a25f0a04SGreg Roach * Get the full name in an alternative character set 611a25f0a04SGreg Roach * 612e364afe4SGreg Roach * @return string|null 613a25f0a04SGreg Roach */ 614e364afe4SGreg Roach public function alternateName(): ?string 615c1010edaSGreg Roach { 616e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 617a25f0a04SGreg Roach $all_names = $this->getAllNames(); 618cbc1590aSGreg Roach 619a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 620a25f0a04SGreg Roach } 621b2ce94c6SRico Sonntag 622b2ce94c6SRico Sonntag return null; 623a25f0a04SGreg Roach } 624a25f0a04SGreg Roach 625a25f0a04SGreg Roach /** 626a25f0a04SGreg Roach * Format this object for display in a list 627a25f0a04SGreg Roach * 628a25f0a04SGreg Roach * @return string 629a25f0a04SGreg Roach */ 6308f53f488SRico Sonntag public function formatList(): string 631c1010edaSGreg Roach { 632b165e17cSGreg Roach $html = '<a href="' . e($this->url()) . '" class="list_item">'; 63339ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 634a25f0a04SGreg Roach $html .= $this->formatListDetails(); 635b165e17cSGreg Roach $html .= '</a>'; 636cbc1590aSGreg Roach 637a25f0a04SGreg Roach return $html; 638a25f0a04SGreg Roach } 639a25f0a04SGreg Roach 640a25f0a04SGreg Roach /** 641a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 642a25f0a04SGreg Roach * identifying characteristics of this record. 643a25f0a04SGreg Roach * 644a25f0a04SGreg Roach * @return string 645a25f0a04SGreg Roach */ 6468f53f488SRico Sonntag public function formatListDetails(): string 647c1010edaSGreg Roach { 648a25f0a04SGreg Roach return ''; 649a25f0a04SGreg Roach } 650a25f0a04SGreg Roach 651a25f0a04SGreg Roach /** 652a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 653a25f0a04SGreg Roach * 6548d0ebef0SGreg Roach * @param string[] $facts 655cbc1590aSGreg Roach * @param int $style 656a25f0a04SGreg Roach * 657a25f0a04SGreg Roach * @return string 658a25f0a04SGreg Roach */ 6598d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 660c1010edaSGreg Roach { 66130158ae7SGreg Roach foreach ($this->facts($facts, true) as $event) { 662a25f0a04SGreg Roach // Only display if it has a date or place (or both) 663e364afe4SGreg Roach if ($event->date()->isOK() && $event->place()->gedcomName() !== '') { 664d93f11b5SGreg Roach $joiner = ' — '; 665d93f11b5SGreg Roach } else { 666d93f11b5SGreg Roach $joiner = ''; 667d93f11b5SGreg Roach } 668e364afe4SGreg Roach if ($event->date()->isOK() || $event->place()->gedcomName() !== '') { 669a25f0a04SGreg Roach switch ($style) { 670a25f0a04SGreg Roach case 1: 6717b7d8067SGreg Roach return '<br><em>' . $event->label() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>'; 672a25f0a04SGreg Roach case 2: 6737b7d8067SGreg Roach return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>'; 674a25f0a04SGreg Roach } 675a25f0a04SGreg Roach } 676a25f0a04SGreg Roach } 677cbc1590aSGreg Roach 678a25f0a04SGreg Roach return ''; 679a25f0a04SGreg Roach } 680a25f0a04SGreg Roach 681a25f0a04SGreg Roach /** 682a25f0a04SGreg Roach * Find individuals linked to this record. 683a25f0a04SGreg Roach * 684a25f0a04SGreg Roach * @param string $link 685a25f0a04SGreg Roach * 686907c1109SGreg Roach * @return Collection 687a25f0a04SGreg Roach */ 688907c1109SGreg Roach public function linkedIndividuals(string $link): Collection 689c1010edaSGreg Roach { 690907c1109SGreg Roach return DB::table('individuals') 6910b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 692907c1109SGreg Roach $join 693907c1109SGreg Roach ->on('l_file', '=', 'i_file') 694907c1109SGreg Roach ->on('l_from', '=', 'i_id'); 695ba1c12e8SGreg Roach }) 696ba1c12e8SGreg Roach ->where('i_file', '=', $this->tree->id()) 697ba1c12e8SGreg Roach ->where('l_type', '=', $link) 698ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 699907c1109SGreg Roach ->select(['individuals.*']) 700907c1109SGreg Roach ->get() 701d5ad3db0SGreg Roach ->map(Individual::rowMapper($this->tree)) 702907c1109SGreg Roach ->filter(self::accessFilter()); 703a25f0a04SGreg Roach } 704a25f0a04SGreg Roach 705a25f0a04SGreg Roach /** 706a25f0a04SGreg Roach * Find families linked to this record. 707a25f0a04SGreg Roach * 708a25f0a04SGreg Roach * @param string $link 709a25f0a04SGreg Roach * 710907c1109SGreg Roach * @return Collection 711a25f0a04SGreg Roach */ 712907c1109SGreg Roach public function linkedFamilies(string $link): Collection 713c1010edaSGreg Roach { 714907c1109SGreg Roach return DB::table('families') 7150b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 716907c1109SGreg Roach $join 717907c1109SGreg Roach ->on('l_file', '=', 'f_file') 718907c1109SGreg Roach ->on('l_from', '=', 'f_id'); 719ba1c12e8SGreg Roach }) 720ba1c12e8SGreg Roach ->where('f_file', '=', $this->tree->id()) 721ba1c12e8SGreg Roach ->where('l_type', '=', $link) 722ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 723907c1109SGreg Roach ->select(['families.*']) 724907c1109SGreg Roach ->get() 725d5ad3db0SGreg Roach ->map(Family::rowMapper($this->tree)) 726907c1109SGreg Roach ->filter(self::accessFilter()); 727a25f0a04SGreg Roach } 728a25f0a04SGreg Roach 729a25f0a04SGreg Roach /** 730a25f0a04SGreg Roach * Find sources linked to this record. 731a25f0a04SGreg Roach * 732a25f0a04SGreg Roach * @param string $link 733a25f0a04SGreg Roach * 734907c1109SGreg Roach * @return Collection 735a25f0a04SGreg Roach */ 736907c1109SGreg Roach public function linkedSources(string $link): Collection 737c1010edaSGreg Roach { 738907c1109SGreg Roach return DB::table('sources') 7390b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 740907c1109SGreg Roach $join 741907c1109SGreg Roach ->on('l_file', '=', 's_file') 742907c1109SGreg Roach ->on('l_from', '=', 's_id'); 743ba1c12e8SGreg Roach }) 744ba1c12e8SGreg Roach ->where('s_file', '=', $this->tree->id()) 745ba1c12e8SGreg Roach ->where('l_type', '=', $link) 746ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 747907c1109SGreg Roach ->select(['sources.*']) 748907c1109SGreg Roach ->get() 749d5ad3db0SGreg Roach ->map(Source::rowMapper($this->tree)) 750907c1109SGreg Roach ->filter(self::accessFilter()); 751a25f0a04SGreg Roach } 752a25f0a04SGreg Roach 753a25f0a04SGreg Roach /** 754a25f0a04SGreg Roach * Find media objects linked to this record. 755a25f0a04SGreg Roach * 756a25f0a04SGreg Roach * @param string $link 757a25f0a04SGreg Roach * 758907c1109SGreg Roach * @return Collection 759a25f0a04SGreg Roach */ 760907c1109SGreg Roach public function linkedMedia(string $link): Collection 761c1010edaSGreg Roach { 762907c1109SGreg Roach return DB::table('media') 7630b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 764907c1109SGreg Roach $join 765907c1109SGreg Roach ->on('l_file', '=', 'm_file') 766907c1109SGreg Roach ->on('l_from', '=', 'm_id'); 767ba1c12e8SGreg Roach }) 768ba1c12e8SGreg Roach ->where('m_file', '=', $this->tree->id()) 769ba1c12e8SGreg Roach ->where('l_type', '=', $link) 770ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 771907c1109SGreg Roach ->select(['media.*']) 772907c1109SGreg Roach ->get() 773d5ad3db0SGreg Roach ->map(Media::rowMapper($this->tree)) 774907c1109SGreg Roach ->filter(self::accessFilter()); 775a25f0a04SGreg Roach } 776a25f0a04SGreg Roach 777a25f0a04SGreg Roach /** 778a25f0a04SGreg Roach * Find notes linked to this record. 779a25f0a04SGreg Roach * 780a25f0a04SGreg Roach * @param string $link 781a25f0a04SGreg Roach * 782907c1109SGreg Roach * @return Collection 783a25f0a04SGreg Roach */ 784907c1109SGreg Roach public function linkedNotes(string $link): Collection 785c1010edaSGreg Roach { 786907c1109SGreg Roach return DB::table('other') 7870b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 788907c1109SGreg Roach $join 789907c1109SGreg Roach ->on('l_file', '=', 'o_file') 790907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 791ba1c12e8SGreg Roach }) 792ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 793ba1c12e8SGreg Roach ->where('o_type', '=', 'NOTE') 794ba1c12e8SGreg Roach ->where('l_type', '=', $link) 795ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 796907c1109SGreg Roach ->select(['other.*']) 797907c1109SGreg Roach ->get() 798d5ad3db0SGreg Roach ->map(Note::rowMapper($this->tree)) 799907c1109SGreg Roach ->filter(self::accessFilter()); 800a25f0a04SGreg Roach } 801a25f0a04SGreg Roach 802a25f0a04SGreg Roach /** 803a25f0a04SGreg Roach * Find repositories linked to this record. 804a25f0a04SGreg Roach * 805a25f0a04SGreg Roach * @param string $link 806a25f0a04SGreg Roach * 807907c1109SGreg Roach * @return Collection 808a25f0a04SGreg Roach */ 809907c1109SGreg Roach public function linkedRepositories(string $link): Collection 810c1010edaSGreg Roach { 811907c1109SGreg Roach return DB::table('other') 8120b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 813907c1109SGreg Roach $join 814907c1109SGreg Roach ->on('l_file', '=', 'o_file') 815907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 816ba1c12e8SGreg Roach }) 817ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 818ba1c12e8SGreg Roach ->where('o_type', '=', 'REPO') 819ba1c12e8SGreg Roach ->where('l_type', '=', $link) 820ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 821907c1109SGreg Roach ->select(['other.*']) 822907c1109SGreg Roach ->get() 823d5ad3db0SGreg Roach ->map(Repository::rowMapper($this->tree)) 824907c1109SGreg Roach ->filter(self::accessFilter()); 825a25f0a04SGreg Roach } 826a25f0a04SGreg Roach 827a25f0a04SGreg Roach /** 828a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 829a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 830a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 831a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 832a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 833a25f0a04SGreg Roach * 8348d0ebef0SGreg Roach * @param string[] $events 835a25f0a04SGreg Roach * 836a25f0a04SGreg Roach * @return Date[] 837a25f0a04SGreg Roach */ 8388d0ebef0SGreg Roach public function getAllEventDates(array $events): array 839c1010edaSGreg Roach { 84013abd6f3SGreg Roach $dates = []; 8418d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 8422decada7SGreg Roach if ($event->date()->isOK()) { 8432decada7SGreg Roach $dates[] = $event->date(); 844a25f0a04SGreg Roach } 845a25f0a04SGreg Roach } 846a25f0a04SGreg Roach 847a25f0a04SGreg Roach return $dates; 848a25f0a04SGreg Roach } 849a25f0a04SGreg Roach 850a25f0a04SGreg Roach /** 851a25f0a04SGreg Roach * Get all the places for a particular type of event 852a25f0a04SGreg Roach * 8538d0ebef0SGreg Roach * @param string[] $events 854a25f0a04SGreg Roach * 8554080d558SGreg Roach * @return Place[] 856a25f0a04SGreg Roach */ 8578d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 858c1010edaSGreg Roach { 85913abd6f3SGreg Roach $places = []; 8608d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 861138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 862a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 86316d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 864a25f0a04SGreg Roach } 865a25f0a04SGreg Roach } 866a25f0a04SGreg Roach } 867a25f0a04SGreg Roach 868a25f0a04SGreg Roach return $places; 869a25f0a04SGreg Roach } 870a25f0a04SGreg Roach 871a25f0a04SGreg Roach /** 872a25f0a04SGreg Roach * The facts and events for this record. 873a25f0a04SGreg Roach * 8748d0ebef0SGreg Roach * @param string[] $filter 875cbc1590aSGreg Roach * @param bool $sort 876cbc1590aSGreg Roach * @param int|null $access_level 877cbc1590aSGreg Roach * @param bool $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES. 878a25f0a04SGreg Roach * 87954c7f8dfSGreg Roach * @return Collection 880a25f0a04SGreg Roach */ 88139ca88baSGreg Roach public function facts(array $filter = [], bool $sort = false, int $access_level = null, bool $override = false): Collection 882c1010edaSGreg Roach { 883*d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 8844b9ff166SGreg Roach 8858af3e5c1SGreg Roach $facts = new Collection(); 886a25f0a04SGreg Roach if ($this->canShow($access_level) || $override) { 887a25f0a04SGreg Roach foreach ($this->facts as $fact) { 88822d65e5aSGreg Roach if (($filter === [] || in_array($fact->getTag(), $filter, true)) && $fact->canShow($access_level)) { 8898af3e5c1SGreg Roach $facts->push($fact); 890a25f0a04SGreg Roach } 891a25f0a04SGreg Roach } 892a25f0a04SGreg Roach } 893d17d7b9eSGreg Roach 894a25f0a04SGreg Roach if ($sort) { 895580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 896a25f0a04SGreg Roach } 897cbc1590aSGreg Roach 89839ca88baSGreg Roach return new Collection($facts); 899a25f0a04SGreg Roach } 900a25f0a04SGreg Roach 901a25f0a04SGreg Roach /** 9024459dc9aSGreg Roach * Get the last-change timestamp for this record 903a25f0a04SGreg Roach * 9044459dc9aSGreg Roach * @return Carbon 905a25f0a04SGreg Roach */ 9064459dc9aSGreg Roach public function lastChangeTimestamp(): Carbon 907c1010edaSGreg Roach { 9084459dc9aSGreg Roach /** @var Fact|null $chan */ 909820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 910a25f0a04SGreg Roach 9114459dc9aSGreg Roach if ($chan instanceof Fact) { 912a25f0a04SGreg Roach // The record does have a CHAN event 9132decada7SGreg Roach $d = $chan->date()->minimumDate(); 9144459dc9aSGreg Roach 915138ca96cSGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->gedcom(), $match)) { 9164459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2], (int) $match[3]); 917e364afe4SGreg Roach } 918e364afe4SGreg Roach 919e364afe4SGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->gedcom(), $match)) { 9204459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2]); 921b2ce94c6SRico Sonntag } 922b2ce94c6SRico Sonntag 9234459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day()); 924a25f0a04SGreg Roach } 925b2ce94c6SRico Sonntag 926a25f0a04SGreg Roach // The record does not have a CHAN event 9274459dc9aSGreg Roach return Carbon::createFromTimestamp(0); 928a25f0a04SGreg Roach } 929a25f0a04SGreg Roach 930a25f0a04SGreg Roach /** 931a25f0a04SGreg Roach * Get the last-change user for this record 932a25f0a04SGreg Roach * 933a25f0a04SGreg Roach * @return string 934a25f0a04SGreg Roach */ 935e364afe4SGreg Roach public function lastChangeUser(): string 936c1010edaSGreg Roach { 937820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 938a25f0a04SGreg Roach 939a25f0a04SGreg Roach if ($chan === null) { 940a25f0a04SGreg Roach return I18N::translate('Unknown'); 941b2ce94c6SRico Sonntag } 942b2ce94c6SRico Sonntag 9433425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 944baacc364SGreg Roach if ($chan_user === '') { 945a25f0a04SGreg Roach return I18N::translate('Unknown'); 946b2ce94c6SRico Sonntag } 947b2ce94c6SRico Sonntag 948a25f0a04SGreg Roach return $chan_user; 949a25f0a04SGreg Roach } 950a25f0a04SGreg Roach 951a25f0a04SGreg Roach /** 952a25f0a04SGreg Roach * Add a new fact to this record 953a25f0a04SGreg Roach * 954a25f0a04SGreg Roach * @param string $gedcom 955cbc1590aSGreg Roach * @param bool $update_chan 9567e96c925SGreg Roach * 9577e96c925SGreg Roach * @return void 958a25f0a04SGreg Roach */ 959e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 960c1010edaSGreg Roach { 961fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 962a25f0a04SGreg Roach } 963a25f0a04SGreg Roach 964a25f0a04SGreg Roach /** 965a25f0a04SGreg Roach * Delete a fact from this record 966a25f0a04SGreg Roach * 967a25f0a04SGreg Roach * @param string $fact_id 968cbc1590aSGreg Roach * @param bool $update_chan 9697e96c925SGreg Roach * 9707e96c925SGreg Roach * @return void 971a25f0a04SGreg Roach */ 972e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 973c1010edaSGreg Roach { 974db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 975a25f0a04SGreg Roach } 976a25f0a04SGreg Roach 977a25f0a04SGreg Roach /** 978a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 979a25f0a04SGreg Roach * 980a25f0a04SGreg Roach * @param string $fact_id 981a25f0a04SGreg Roach * @param string $gedcom 982cbc1590aSGreg Roach * @param bool $update_chan 983a25f0a04SGreg Roach * 9847e96c925SGreg Roach * @return void 9857e96c925SGreg Roach * @throws Exception 986a25f0a04SGreg Roach */ 987e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 988c1010edaSGreg Roach { 989a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 990a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 991a25f0a04SGreg Roach $gedcom = trim($gedcom); 992a25f0a04SGreg Roach 993a25f0a04SGreg Roach if ($this->pending === '') { 9947e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 995a25f0a04SGreg Roach } 9968d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 9977e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 998a25f0a04SGreg Roach } 999a25f0a04SGreg Roach 1000a25f0a04SGreg Roach if ($this->pending) { 1001a25f0a04SGreg Roach $old_gedcom = $this->pending; 1002a25f0a04SGreg Roach } else { 1003a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 1004a25f0a04SGreg Roach } 1005a25f0a04SGreg Roach 1006a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 100765e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 1008a25f0a04SGreg Roach 1009a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 10108d0ebef0SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE) as $fact) { 1011a25f0a04SGreg Roach if (!$fact->isPendingDeletion()) { 1012905ab80aSGreg Roach if ($fact->id() === $fact_id) { 1013db7bb364SGreg Roach if ($gedcom !== '') { 1014a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1015a25f0a04SGreg Roach } 1016fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 1017e364afe4SGreg Roach } elseif ($fact->getTag() !== 'CHAN' || !$update_chan) { 1018138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 1019a25f0a04SGreg Roach } 1020a25f0a04SGreg Roach } 1021a25f0a04SGreg Roach } 1022a25f0a04SGreg Roach if ($update_chan) { 1023e5a6b4d4SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1024a25f0a04SGreg Roach } 1025a25f0a04SGreg Roach 1026a25f0a04SGreg Roach // Adding a new fact 1027fc3ccce4SGreg Roach if ($fact_id === '') { 1028a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1029a25f0a04SGreg Roach } 1030a25f0a04SGreg Roach 1031e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 1032a25f0a04SGreg Roach // Save the changes 1033d09b6323SGreg Roach DB::table('change')->insert([ 1034d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1035d09b6323SGreg Roach 'xref' => $this->xref, 1036d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 1037d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 1038d09b6323SGreg Roach 'user_id' => Auth::id(), 103913abd6f3SGreg Roach ]); 1040a25f0a04SGreg Roach 1041a25f0a04SGreg Roach $this->pending = $new_gedcom; 1042a25f0a04SGreg Roach 10437c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 104422e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1045a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 1046a25f0a04SGreg Roach $this->pending = null; 1047a25f0a04SGreg Roach } 1048a25f0a04SGreg Roach } 1049a25f0a04SGreg Roach $this->parseFacts(); 1050a25f0a04SGreg Roach } 1051a25f0a04SGreg Roach 1052a25f0a04SGreg Roach /** 1053a25f0a04SGreg Roach * Update this record 1054a25f0a04SGreg Roach * 1055a25f0a04SGreg Roach * @param string $gedcom 1056cbc1590aSGreg Roach * @param bool $update_chan 10577e96c925SGreg Roach * 10587e96c925SGreg Roach * @return void 1059a25f0a04SGreg Roach */ 1060e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 1061c1010edaSGreg Roach { 1062a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1063a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1064a25f0a04SGreg Roach $gedcom = trim($gedcom); 1065a25f0a04SGreg Roach 1066a25f0a04SGreg Roach // Update the CHAN record 1067a25f0a04SGreg Roach if ($update_chan) { 1068a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 1069e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1070a25f0a04SGreg Roach } 1071a25f0a04SGreg Roach 1072a25f0a04SGreg Roach // Create a pending change 1073d09b6323SGreg Roach DB::table('change')->insert([ 1074d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1075d09b6323SGreg Roach 'xref' => $this->xref, 1076d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1077d09b6323SGreg Roach 'new_gedcom' => $gedcom, 1078d09b6323SGreg Roach 'user_id' => Auth::id(), 107913abd6f3SGreg Roach ]); 1080a25f0a04SGreg Roach 1081a25f0a04SGreg Roach // Clear the cache 1082a25f0a04SGreg Roach $this->pending = $gedcom; 1083a25f0a04SGreg Roach 1084a25f0a04SGreg Roach // Accept this pending change 10857c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 108622e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1087a25f0a04SGreg Roach $this->gedcom = $gedcom; 1088a25f0a04SGreg Roach $this->pending = null; 1089a25f0a04SGreg Roach } 1090a25f0a04SGreg Roach 1091a25f0a04SGreg Roach $this->parseFacts(); 1092a25f0a04SGreg Roach 1093847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1094a25f0a04SGreg Roach } 1095a25f0a04SGreg Roach 1096a25f0a04SGreg Roach /** 1097a25f0a04SGreg Roach * Delete this record 1098b874da82SGreg Roach * 1099b874da82SGreg Roach * @return void 1100a25f0a04SGreg Roach */ 1101e364afe4SGreg Roach public function deleteRecord(): void 1102c1010edaSGreg Roach { 1103a25f0a04SGreg Roach // Create a pending change 11044c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 1105d09b6323SGreg Roach DB::table('change')->insert([ 1106d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1107d09b6323SGreg Roach 'xref' => $this->xref, 1108d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1109d09b6323SGreg Roach 'new_gedcom' => '', 1110d09b6323SGreg Roach 'user_id' => Auth::id(), 111113abd6f3SGreg Roach ]); 11124c0b5256SGreg Roach } 1113a25f0a04SGreg Roach 11144c0b5256SGreg Roach // Auto-accept this pending change 11157c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 111622e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1117a25f0a04SGreg Roach } 1118a25f0a04SGreg Roach 1119a25f0a04SGreg Roach // Clear the cache 1120d17d7b9eSGreg Roach self::$gedcom_record_cache = []; 1121d17d7b9eSGreg Roach self::$pending_record_cache = []; 1122a25f0a04SGreg Roach 1123847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1124a25f0a04SGreg Roach } 1125a25f0a04SGreg Roach 1126a25f0a04SGreg Roach /** 1127a25f0a04SGreg Roach * Remove all links from this record to $xref 1128a25f0a04SGreg Roach * 1129a25f0a04SGreg Roach * @param string $xref 1130cbc1590aSGreg Roach * @param bool $update_chan 11317e96c925SGreg Roach * 11327e96c925SGreg Roach * @return void 1133a25f0a04SGreg Roach */ 1134e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 1135c1010edaSGreg Roach { 1136a25f0a04SGreg Roach $value = '@' . $xref . '@'; 1137a25f0a04SGreg Roach 113830158ae7SGreg Roach foreach ($this->facts() as $fact) { 113984586c02SGreg Roach if ($fact->value() === $value) { 1140905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 11418d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 1142138ca96cSGreg Roach $gedcom = $fact->gedcom(); 1143a25f0a04SGreg Roach foreach ($matches as $match) { 1144a25f0a04SGreg Roach $next_level = $match[1] + 1; 1145a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 1146a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 1147a25f0a04SGreg Roach } 1148905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 1149a25f0a04SGreg Roach } 1150a25f0a04SGreg Roach } 1151a25f0a04SGreg Roach } 1152bf4eb542SGreg Roach 1153bf4eb542SGreg Roach /** 1154bf4eb542SGreg Roach * Fetch XREFs of all records linked to a record - when deleting an object, we must 1155bf4eb542SGreg Roach * also delete all links to it. 1156bf4eb542SGreg Roach * 1157bf4eb542SGreg Roach * @return GedcomRecord[] 1158bf4eb542SGreg Roach */ 1159bf4eb542SGreg Roach public function linkingRecords(): array 1160bf4eb542SGreg Roach { 1161bf4eb542SGreg Roach $union = DB::table('change') 1162bf4eb542SGreg Roach ->where('gedcom_id', '=', $this->tree()->id()) 1163fbbe964bSGreg Roach ->whereContains('new_gedcom', '@' . $this->xref() . '@') 1164bf4eb542SGreg Roach ->where('new_gedcom', 'NOT LIKE', '0 @' . $this->xref() . '@%') 116583242252SGreg Roach ->whereIn('change_id', function (Builder $query): void { 116683242252SGreg Roach $query->select(new Expression('MAX(change_id)')) 116783242252SGreg Roach ->from('change') 116883242252SGreg Roach ->where('gedcom_id', '=', $this->tree->id()) 116983242252SGreg Roach ->where('status', '=', 'pending') 11707f5c2944SGreg Roach ->groupBy(['xref']); 117183242252SGreg Roach }) 1172bf4eb542SGreg Roach ->select(['xref']); 1173bf4eb542SGreg Roach 1174bf4eb542SGreg Roach $xrefs = DB::table('link') 1175bf4eb542SGreg Roach ->where('l_file', '=', $this->tree()->id()) 1176bf4eb542SGreg Roach ->where('l_to', '=', $this->xref()) 117747256fc5SGreg Roach ->select(['l_from']) 1178bf4eb542SGreg Roach ->union($union) 1179bf4eb542SGreg Roach ->pluck('l_from'); 1180bf4eb542SGreg Roach 1181bf4eb542SGreg Roach return $xrefs->map(function (string $xref): GedcomRecord { 1182bf4eb542SGreg Roach return GedcomRecord::getInstance($xref, $this->tree); 1183bf4eb542SGreg Roach })->all(); 1184bf4eb542SGreg Roach } 118522e73debSGreg Roach 118622e73debSGreg Roach /** 118722e73debSGreg Roach * Each object type may have its own special rules, and re-implement this function. 118822e73debSGreg Roach * 118922e73debSGreg Roach * @param int $access_level 119022e73debSGreg Roach * 119122e73debSGreg Roach * @return bool 119222e73debSGreg Roach */ 119322e73debSGreg Roach protected function canShowByType(int $access_level): bool 119422e73debSGreg Roach { 119522e73debSGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 119622e73debSGreg Roach 119722e73debSGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 119822e73debSGreg Roach // Restriction found 119922e73debSGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 120022e73debSGreg Roach } 120122e73debSGreg Roach 120222e73debSGreg Roach // No restriction found - must be public: 120322e73debSGreg Roach return true; 120422e73debSGreg Roach } 120522e73debSGreg Roach 120622e73debSGreg Roach /** 120722e73debSGreg Roach * Generate a private version of this record 120822e73debSGreg Roach * 120922e73debSGreg Roach * @param int $access_level 121022e73debSGreg Roach * 121122e73debSGreg Roach * @return string 121222e73debSGreg Roach */ 121322e73debSGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 121422e73debSGreg Roach { 121522e73debSGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private'); 121622e73debSGreg Roach } 121722e73debSGreg Roach 121822e73debSGreg Roach /** 121922e73debSGreg Roach * Convert a name record into sortable and full/display versions. This default 122022e73debSGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 122122e73debSGreg Roach * 122222e73debSGreg Roach * @param string $type 122322e73debSGreg Roach * @param string $value 122422e73debSGreg Roach * @param string $gedcom 122522e73debSGreg Roach * 122622e73debSGreg Roach * @return void 122722e73debSGreg Roach */ 122822e73debSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 122922e73debSGreg Roach { 123022e73debSGreg Roach $this->getAllNames[] = [ 123122e73debSGreg Roach 'type' => $type, 123222e73debSGreg Roach 'sort' => preg_replace_callback('/([0-9]+)/', static function (array $matches): string { 123322e73debSGreg Roach return str_pad($matches[0], 10, '0', STR_PAD_LEFT); 123422e73debSGreg Roach }, $value), 123522e73debSGreg Roach 'full' => '<span dir="auto">' . e($value) . '</span>', 123622e73debSGreg Roach // This is used for display 123722e73debSGreg Roach 'fullNN' => $value, 123822e73debSGreg Roach // This goes into the database 123922e73debSGreg Roach ]; 124022e73debSGreg Roach } 124122e73debSGreg Roach 124222e73debSGreg Roach /** 124322e73debSGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 124422e73debSGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 124522e73debSGreg Roach * Parameters: the level 1 fact containing the name. 124622e73debSGreg Roach * Return value: an array of name structures, each containing 124722e73debSGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 124822e73debSGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 124922e73debSGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 125022e73debSGreg Roach * 125122e73debSGreg Roach * @param int $level 125222e73debSGreg Roach * @param string $fact_type 125322e73debSGreg Roach * @param Collection $facts 125422e73debSGreg Roach * 125522e73debSGreg Roach * @return void 125622e73debSGreg Roach */ 125722e73debSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 125822e73debSGreg Roach { 125922e73debSGreg Roach $sublevel = $level + 1; 126022e73debSGreg Roach $subsublevel = $sublevel + 1; 126122e73debSGreg Roach foreach ($facts as $fact) { 126222e73debSGreg Roach if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) { 126322e73debSGreg Roach foreach ($matches as $match) { 126422e73debSGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 126522e73debSGreg Roach if ($match[1] === 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) { 126622e73debSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 126722e73debSGreg Roach } else { 126822e73debSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 126922e73debSGreg Roach } 127022e73debSGreg Roach if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { 127122e73debSGreg Roach foreach ($submatches as $submatch) { 127222e73debSGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 127322e73debSGreg Roach } 127422e73debSGreg Roach } 127522e73debSGreg Roach } 127622e73debSGreg Roach } 127722e73debSGreg Roach } 127822e73debSGreg Roach } 127922e73debSGreg Roach 128022e73debSGreg Roach /** 128122e73debSGreg Roach * Split the record into facts 128222e73debSGreg Roach * 128322e73debSGreg Roach * @return void 128422e73debSGreg Roach */ 128522e73debSGreg Roach private function parseFacts(): void 128622e73debSGreg Roach { 128722e73debSGreg Roach // Split the record into facts 128822e73debSGreg Roach if ($this->gedcom) { 128922e73debSGreg Roach $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom); 129022e73debSGreg Roach array_shift($gedcom_facts); 129122e73debSGreg Roach } else { 129222e73debSGreg Roach $gedcom_facts = []; 129322e73debSGreg Roach } 129422e73debSGreg Roach if ($this->pending) { 129522e73debSGreg Roach $pending_facts = preg_split('/\n(?=1)/s', $this->pending); 129622e73debSGreg Roach array_shift($pending_facts); 129722e73debSGreg Roach } else { 129822e73debSGreg Roach $pending_facts = []; 129922e73debSGreg Roach } 130022e73debSGreg Roach 130122e73debSGreg Roach $this->facts = []; 130222e73debSGreg Roach 130322e73debSGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 130422e73debSGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 130522e73debSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 130622e73debSGreg Roach $fact->setPendingDeletion(); 130722e73debSGreg Roach } 130822e73debSGreg Roach $this->facts[] = $fact; 130922e73debSGreg Roach } 131022e73debSGreg Roach foreach ($pending_facts as $pending_fact) { 131122e73debSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 131222e73debSGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 131322e73debSGreg Roach $fact->setPendingAddition(); 131422e73debSGreg Roach $this->facts[] = $fact; 131522e73debSGreg Roach } 131622e73debSGreg Roach } 131722e73debSGreg Roach } 131822e73debSGreg Roach 131922e73debSGreg Roach /** 132022e73debSGreg Roach * Work out whether this record can be shown to a user with a given access level 132122e73debSGreg Roach * 132222e73debSGreg Roach * @param int $access_level 132322e73debSGreg Roach * 132422e73debSGreg Roach * @return bool 132522e73debSGreg Roach */ 132622e73debSGreg Roach private function canShowRecord(int $access_level): bool 132722e73debSGreg Roach { 132822e73debSGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 132922e73debSGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 133022e73debSGreg Roach return true; 133122e73debSGreg Roach } 133222e73debSGreg Roach 133322e73debSGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 13347c4add84SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) { 133522e73debSGreg Roach return true; 133622e73debSGreg Roach } 133722e73debSGreg Roach 133822e73debSGreg Roach // Does this record have a RESN? 133922e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN confidential") !== false) { 134022e73debSGreg Roach return Auth::PRIV_NONE >= $access_level; 134122e73debSGreg Roach } 134222e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN privacy") !== false) { 134322e73debSGreg Roach return Auth::PRIV_USER >= $access_level; 134422e73debSGreg Roach } 134522e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN none") !== false) { 134622e73debSGreg Roach return true; 134722e73debSGreg Roach } 134822e73debSGreg Roach 134922e73debSGreg Roach // Does this record have a default RESN? 135022e73debSGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 135122e73debSGreg Roach if (isset($individual_privacy[$this->xref()])) { 135222e73debSGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 135322e73debSGreg Roach } 135422e73debSGreg Roach 135522e73debSGreg Roach // Privacy rules do not apply to admins 135622e73debSGreg Roach if (Auth::PRIV_NONE >= $access_level) { 135722e73debSGreg Roach return true; 135822e73debSGreg Roach } 135922e73debSGreg Roach 136022e73debSGreg Roach // Different types of record have different privacy rules 136122e73debSGreg Roach return $this->canShowByType($access_level); 136222e73debSGreg Roach } 1363a25f0a04SGreg Roach} 1364