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; 2490a2f718SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 253d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 265818a371SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomRecordPage; 2722e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService; 28bf4eb542SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2983242252SGreg Roachuse Illuminate\Database\Query\Builder; 3083242252SGreg Roachuse Illuminate\Database\Query\Expression; 31ba1c12e8SGreg Roachuse Illuminate\Database\Query\JoinClause; 3239ca88baSGreg Roachuse Illuminate\Support\Collection; 3390a2f718SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3479529c87SGreg Roachuse stdClass; 35a25f0a04SGreg Roach 3622e73debSGreg Roachuse function app; 3790a2f718SGreg Roachuse function assert; 3822e73debSGreg Roach 39a25f0a04SGreg Roach/** 4076692c8bSGreg Roach * A GEDCOM object. 41a25f0a04SGreg Roach */ 42c1010edaSGreg Roachclass GedcomRecord 43c1010edaSGreg Roach{ 4416d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 4516d6367aSGreg Roach 465818a371SGreg Roach protected const ROUTE_NAME = GedcomRecordPage::class; 475818a371SGreg Roach 4876692c8bSGreg Roach /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */ 49bec87e94SGreg Roach public static $gedcom_record_cache; 5079529c87SGreg Roach /** @var stdClass[][] Fetch all pending edits in one database query */ 51bec87e94SGreg Roach public static $pending_record_cache; 5222e73debSGreg Roach /** @var string The record identifier */ 5322e73debSGreg Roach protected $xref; 5422e73debSGreg Roach /** @var Tree The family tree to which this record belongs */ 5522e73debSGreg Roach protected $tree; 5622e73debSGreg Roach /** @var string GEDCOM data (before any pending edits) */ 5722e73debSGreg Roach protected $gedcom; 5822e73debSGreg Roach /** @var string|null GEDCOM data (after any pending edits) */ 5922e73debSGreg Roach protected $pending; 6022e73debSGreg Roach /** @var Fact[] facts extracted from $gedcom/$pending */ 6122e73debSGreg Roach protected $facts; 6222e73debSGreg Roach /** @var string[][] All the names of this individual */ 6322e73debSGreg Roach protected $getAllNames; 6422e73debSGreg Roach /** @var int|null Cached result */ 6522e73debSGreg Roach protected $getPrimaryName; 6622e73debSGreg Roach /** @var int|null Cached result */ 6722e73debSGreg Roach protected $getSecondaryName; 68a25f0a04SGreg Roach 69a25f0a04SGreg Roach /** 70a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 71a25f0a04SGreg Roach * 72a25f0a04SGreg Roach * @param string $xref 73a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 74a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 75a25f0a04SGreg Roach * empty string for records with pending deletions 7624ec66ceSGreg Roach * @param Tree $tree 77a25f0a04SGreg Roach */ 78e364afe4SGreg Roach public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 79c1010edaSGreg Roach { 80a25f0a04SGreg Roach $this->xref = $xref; 81a25f0a04SGreg Roach $this->gedcom = $gedcom; 82a25f0a04SGreg Roach $this->pending = $pending; 8324ec66ceSGreg Roach $this->tree = $tree; 84a25f0a04SGreg Roach 85a25f0a04SGreg Roach $this->parseFacts(); 86a25f0a04SGreg Roach } 87a25f0a04SGreg Roach 88a25f0a04SGreg Roach /** 89886b77daSGreg Roach * A closure which will create a record from a database row. 90886b77daSGreg Roach * 91886b77daSGreg Roach * @return Closure 92886b77daSGreg Roach */ 93c0804649SGreg Roach public static function rowMapper(): Closure 94886b77daSGreg Roach { 956c2179e2SGreg Roach return static function (stdClass $row): GedcomRecord { 96e3bddf11SGreg Roach return GedcomRecord::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom); 97886b77daSGreg Roach }; 98886b77daSGreg Roach } 99886b77daSGreg Roach 100886b77daSGreg Roach /** 101886b77daSGreg Roach * A closure which will filter out private records. 102886b77daSGreg Roach * 103886b77daSGreg Roach * @return Closure 104886b77daSGreg Roach */ 1054146fabcSGreg Roach public static function accessFilter(): Closure 106886b77daSGreg Roach { 1076c2179e2SGreg Roach return static function (GedcomRecord $record): bool { 108886b77daSGreg Roach return $record->canShow(); 109886b77daSGreg Roach }; 110886b77daSGreg Roach } 111886b77daSGreg Roach 112886b77daSGreg Roach /** 113c156e8f5SGreg Roach * A closure which will compare records by name. 114c156e8f5SGreg Roach * 115c156e8f5SGreg Roach * @return Closure 116c156e8f5SGreg Roach */ 117c156e8f5SGreg Roach public static function nameComparator(): Closure 118c156e8f5SGreg Roach { 1196c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y): int { 120c156e8f5SGreg Roach if ($x->canShowName()) { 121c156e8f5SGreg Roach if ($y->canShowName()) { 12239ca88baSGreg Roach return I18N::strcasecmp($x->sortName(), $y->sortName()); 123c156e8f5SGreg Roach } 124c156e8f5SGreg Roach 125c156e8f5SGreg Roach return -1; // only $y is private 126c156e8f5SGreg Roach } 127c156e8f5SGreg Roach 128c156e8f5SGreg Roach if ($y->canShowName()) { 129c156e8f5SGreg Roach return 1; // only $x is private 130c156e8f5SGreg Roach } 131c156e8f5SGreg Roach 132c156e8f5SGreg Roach return 0; // both $x and $y private 133c156e8f5SGreg Roach }; 134c156e8f5SGreg Roach } 135c156e8f5SGreg Roach 136c156e8f5SGreg Roach /** 137c156e8f5SGreg Roach * A closure which will compare records by change time. 138c156e8f5SGreg Roach * 139c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 140c156e8f5SGreg Roach * 141c156e8f5SGreg Roach * @return Closure 142c156e8f5SGreg Roach */ 143c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 144c156e8f5SGreg Roach { 1456c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y) use ($direction): int { 1464459dc9aSGreg Roach return $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp()); 147c156e8f5SGreg Roach }; 148c156e8f5SGreg Roach } 149c156e8f5SGreg Roach 150c156e8f5SGreg Roach /** 151a25f0a04SGreg Roach * Get an instance of a GedcomRecord object. For single records, 152a25f0a04SGreg Roach * we just receive the XREF. For bulk records (such as lists 153a25f0a04SGreg Roach * and search results) we can receive the GEDCOM data as well. 154a25f0a04SGreg Roach * 155a25f0a04SGreg Roach * @param string $xref 15624ec66ceSGreg Roach * @param Tree $tree 157a25f0a04SGreg Roach * @param string|null $gedcom 158a25f0a04SGreg Roach * 15984658595SGreg Roach * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null 16022e73debSGreg Roach * @throws Exception 161a25f0a04SGreg Roach */ 16276f666f4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 163c1010edaSGreg Roach { 16472cf66d4SGreg Roach $tree_id = $tree->id(); 16524ec66ceSGreg Roach 166e71ef9d2SGreg Roach // Is this record already in the cache? 16724ec66ceSGreg Roach if (isset(self::$gedcom_record_cache[$xref][$tree_id])) { 168e71ef9d2SGreg Roach return self::$gedcom_record_cache[$xref][$tree_id]; 169a25f0a04SGreg Roach } 170a25f0a04SGreg Roach 171a25f0a04SGreg Roach // Do we need to fetch the record from the database? 172a25f0a04SGreg Roach if ($gedcom === null) { 17324ec66ceSGreg Roach $gedcom = static::fetchGedcomRecord($xref, $tree_id); 174a25f0a04SGreg Roach } 175a25f0a04SGreg Roach 176a25f0a04SGreg Roach // If we can edit, then we also need to be able to see pending records. 17794075df0SGreg Roach if (Auth::isEditor($tree)) { 17824ec66ceSGreg Roach if (!isset(self::$pending_record_cache[$tree_id])) { 179a25f0a04SGreg Roach // Fetch all pending records in one database query 18013abd6f3SGreg Roach self::$pending_record_cache[$tree_id] = []; 18185fc8064SGreg Roach $rows = DB::table('change') 18285fc8064SGreg Roach ->where('gedcom_id', '=', $tree_id) 18385fc8064SGreg Roach ->where('status', '=', 'pending') 18485fc8064SGreg Roach ->orderBy('change_id') 18585fc8064SGreg Roach ->select(['xref', 'new_gedcom']) 18685fc8064SGreg Roach ->get(); 187d17d7b9eSGreg Roach 188a25f0a04SGreg Roach foreach ($rows as $row) { 18924ec66ceSGreg Roach self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom; 190a25f0a04SGreg Roach } 191a25f0a04SGreg Roach } 192a25f0a04SGreg Roach 193d17d7b9eSGreg Roach $pending = self::$pending_record_cache[$tree_id][$xref] ?? null; 194a25f0a04SGreg Roach } else { 195a25f0a04SGreg Roach // There are no pending changes for this record 196a25f0a04SGreg Roach $pending = null; 197a25f0a04SGreg Roach } 198a25f0a04SGreg Roach 199a25f0a04SGreg Roach // No such record exists 200a25f0a04SGreg Roach if ($gedcom === null && $pending === null) { 201a25f0a04SGreg Roach return null; 202a25f0a04SGreg Roach } 203a25f0a04SGreg Roach 204fc3ccce4SGreg Roach // No such record, but a pending creation exists 205fc3ccce4SGreg Roach if ($gedcom === null) { 206fc3ccce4SGreg Roach $gedcom = ''; 207fc3ccce4SGreg Roach } 208fc3ccce4SGreg Roach 209a25f0a04SGreg Roach // Create the object 2108d0ebef0SGreg Roach if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ (' . Gedcom::REGEX_TAG . ')/', $gedcom . $pending, $match)) { 211a25f0a04SGreg Roach $xref = $match[1]; // Collation - we may have requested I123 and found i123 212a25f0a04SGreg Roach $type = $match[2]; 213a25f0a04SGreg Roach } elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) { 214a25f0a04SGreg Roach $xref = $match[1]; 215a25f0a04SGreg Roach $type = $match[1]; 216a25f0a04SGreg Roach } elseif ($gedcom . $pending) { 2177e96c925SGreg Roach throw new Exception('Unrecognized GEDCOM record: ' . $gedcom); 218a25f0a04SGreg Roach } else { 219a25f0a04SGreg Roach // A record with both pending creation and pending deletion 220a25f0a04SGreg Roach $type = static::RECORD_TYPE; 221a25f0a04SGreg Roach } 222a25f0a04SGreg Roach 223a25f0a04SGreg Roach switch ($type) { 224a25f0a04SGreg Roach case 'INDI': 22524ec66ceSGreg Roach $record = new Individual($xref, $gedcom, $pending, $tree); 226a25f0a04SGreg Roach break; 227a25f0a04SGreg Roach case 'FAM': 22824ec66ceSGreg Roach $record = new Family($xref, $gedcom, $pending, $tree); 229a25f0a04SGreg Roach break; 230a25f0a04SGreg Roach case 'SOUR': 23124ec66ceSGreg Roach $record = new Source($xref, $gedcom, $pending, $tree); 232a25f0a04SGreg Roach break; 233a25f0a04SGreg Roach case 'OBJE': 23424ec66ceSGreg Roach $record = new Media($xref, $gedcom, $pending, $tree); 235a25f0a04SGreg Roach break; 236a25f0a04SGreg Roach case 'REPO': 23724ec66ceSGreg Roach $record = new Repository($xref, $gedcom, $pending, $tree); 238a25f0a04SGreg Roach break; 239a25f0a04SGreg Roach case 'NOTE': 24024ec66ceSGreg Roach $record = new Note($xref, $gedcom, $pending, $tree); 241a25f0a04SGreg Roach break; 242a25f0a04SGreg Roach default: 24306ef8e02SGreg Roach $record = new self($xref, $gedcom, $pending, $tree); 244a25f0a04SGreg Roach break; 245a25f0a04SGreg Roach } 246a25f0a04SGreg Roach 247a25f0a04SGreg Roach // Store it in the cache 24824ec66ceSGreg Roach self::$gedcom_record_cache[$xref][$tree_id] = $record; 249a25f0a04SGreg Roach 250a25f0a04SGreg Roach return $record; 251a25f0a04SGreg Roach } 252a25f0a04SGreg Roach 253a25f0a04SGreg Roach /** 254a25f0a04SGreg Roach * Fetch data from the database 255a25f0a04SGreg Roach * 256a25f0a04SGreg Roach * @param string $xref 257cbc1590aSGreg Roach * @param int $tree_id 258a25f0a04SGreg Roach * 259e364afe4SGreg Roach * @return string|null 260a25f0a04SGreg Roach */ 261e364afe4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 262c1010edaSGreg Roach { 263a25f0a04SGreg Roach // We don't know what type of object this is. Try each one in turn. 26464d9078aSGreg Roach $data = Individual::fetchGedcomRecord($xref, $tree_id); 26585fc8064SGreg Roach if ($data !== null) { 266a25f0a04SGreg Roach return $data; 267a25f0a04SGreg Roach } 26864d9078aSGreg Roach $data = Family::fetchGedcomRecord($xref, $tree_id); 26985fc8064SGreg Roach if ($data !== null) { 270a25f0a04SGreg Roach return $data; 271a25f0a04SGreg Roach } 27264d9078aSGreg Roach $data = Source::fetchGedcomRecord($xref, $tree_id); 27385fc8064SGreg Roach if ($data !== null) { 274a25f0a04SGreg Roach return $data; 275a25f0a04SGreg Roach } 27664d9078aSGreg Roach $data = Repository::fetchGedcomRecord($xref, $tree_id); 27785fc8064SGreg Roach if ($data !== null) { 278a25f0a04SGreg Roach return $data; 279a25f0a04SGreg Roach } 28064d9078aSGreg Roach $data = Media::fetchGedcomRecord($xref, $tree_id); 28185fc8064SGreg Roach if ($data !== null) { 282a25f0a04SGreg Roach return $data; 283a25f0a04SGreg Roach } 28464d9078aSGreg Roach $data = Note::fetchGedcomRecord($xref, $tree_id); 28585fc8064SGreg Roach if ($data !== null) { 286a25f0a04SGreg Roach return $data; 287a25f0a04SGreg Roach } 288c1010edaSGreg Roach 289a25f0a04SGreg Roach // Some other type of record... 290d09b6323SGreg Roach return DB::table('other') 29185fc8064SGreg Roach ->where('o_file', '=', $tree_id) 29285fc8064SGreg Roach ->where('o_id', '=', $xref) 29385fc8064SGreg Roach ->value('o_gedcom'); 294a25f0a04SGreg Roach } 295a25f0a04SGreg Roach 296a25f0a04SGreg Roach /** 297a25f0a04SGreg Roach * Get the XREF for this record 298a25f0a04SGreg Roach * 299a25f0a04SGreg Roach * @return string 300a25f0a04SGreg Roach */ 301c0935879SGreg Roach public function xref(): string 302c1010edaSGreg Roach { 303a25f0a04SGreg Roach return $this->xref; 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach 306a25f0a04SGreg Roach /** 307000959d9SGreg Roach * Get the tree to which this record belongs 308000959d9SGreg Roach * 309000959d9SGreg Roach * @return Tree 310000959d9SGreg Roach */ 311f4afa648SGreg Roach public function tree(): Tree 312c1010edaSGreg Roach { 313518bbdc1SGreg Roach return $this->tree; 314000959d9SGreg Roach } 315000959d9SGreg Roach 316000959d9SGreg Roach /** 317a25f0a04SGreg Roach * Application code should access data via Fact objects. 318a25f0a04SGreg Roach * This function exists to support old code. 319a25f0a04SGreg Roach * 320a25f0a04SGreg Roach * @return string 321a25f0a04SGreg Roach */ 322e364afe4SGreg Roach public function gedcom(): string 323c1010edaSGreg Roach { 324b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 325a25f0a04SGreg Roach } 326a25f0a04SGreg Roach 327a25f0a04SGreg Roach /** 328a25f0a04SGreg Roach * Does this record have a pending change? 329a25f0a04SGreg Roach * 330cbc1590aSGreg Roach * @return bool 331a25f0a04SGreg Roach */ 3328f53f488SRico Sonntag public function isPendingAddition(): bool 333c1010edaSGreg Roach { 334a25f0a04SGreg Roach return $this->pending !== null; 335a25f0a04SGreg Roach } 336a25f0a04SGreg Roach 337a25f0a04SGreg Roach /** 338a25f0a04SGreg Roach * Does this record have a pending deletion? 339a25f0a04SGreg Roach * 340cbc1590aSGreg Roach * @return bool 341a25f0a04SGreg Roach */ 3428f53f488SRico Sonntag public function isPendingDeletion(): bool 343c1010edaSGreg Roach { 344a25f0a04SGreg Roach return $this->pending === ''; 345a25f0a04SGreg Roach } 346a25f0a04SGreg Roach 347a25f0a04SGreg Roach /** 348ee4364daSGreg Roach * Generate a "slug" to use in pretty URLs. 349ee4364daSGreg Roach * 350ee4364daSGreg Roach * @return string 351ee4364daSGreg Roach */ 352ee4364daSGreg Roach public function slug(): string 353ee4364daSGreg Roach { 354afa9f52aSGreg Roach return strip_tags($this->fullName()); 355ee4364daSGreg Roach } 356ee4364daSGreg Roach 357ee4364daSGreg Roach /** 358225e381fSGreg Roach * Generate a URL to this record. 359a25f0a04SGreg Roach * 360a25f0a04SGreg Roach * @return string 361a25f0a04SGreg Roach */ 3628f53f488SRico Sonntag public function url(): string 363c1010edaSGreg Roach { 364225e381fSGreg Roach return route(static::ROUTE_NAME, [ 365c0935879SGreg Roach 'xref' => $this->xref(), 366ee4364daSGreg Roach 'tree' => $this->tree->name(), 367ee4364daSGreg Roach 'slug' => $this->slug(), 368225e381fSGreg Roach ]); 369a25f0a04SGreg Roach } 370a25f0a04SGreg Roach 371a25f0a04SGreg Roach /** 372a25f0a04SGreg Roach * Can the details of this record be shown? 373a25f0a04SGreg Roach * 374cbc1590aSGreg Roach * @param int|null $access_level 375a25f0a04SGreg Roach * 376cbc1590aSGreg Roach * @return bool 377a25f0a04SGreg Roach */ 37835584196SGreg Roach public function canShow(int $access_level = null): bool 379c1010edaSGreg Roach { 380f0b9c048SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 3814b9ff166SGreg Roach 382a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 383a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 384f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 385a25f0a04SGreg Roach return true; 386a25f0a04SGreg Roach } 387f0b9c048SGreg Roach 388f0b9c048SGreg Roach $cache_key = 'canShow' . $this->xref . ':' . $this->tree->id() . ':' . $access_level; 389f0b9c048SGreg Roach 390f0b9c048SGreg Roach return app('cache.array')->rememberForever($cache_key, function () use ($access_level) { 391f0b9c048SGreg Roach return $this->canShowRecord($access_level); 392f0b9c048SGreg Roach }); 393a25f0a04SGreg Roach } 394a25f0a04SGreg Roach 395a25f0a04SGreg Roach /** 396a25f0a04SGreg Roach * Can the name of this record be shown? 397a25f0a04SGreg Roach * 398cbc1590aSGreg Roach * @param int|null $access_level 399a25f0a04SGreg Roach * 400cbc1590aSGreg Roach * @return bool 401a25f0a04SGreg Roach */ 40276f666f4SGreg Roach public function canShowName(int $access_level = null): bool 403c1010edaSGreg Roach { 404a25f0a04SGreg Roach return $this->canShow($access_level); 405a25f0a04SGreg Roach } 406a25f0a04SGreg Roach 407a25f0a04SGreg Roach /** 408a25f0a04SGreg Roach * Can we edit this record? 409a25f0a04SGreg Roach * 410cbc1590aSGreg Roach * @return bool 411a25f0a04SGreg Roach */ 4128f53f488SRico Sonntag public function canEdit(): bool 413c1010edaSGreg Roach { 4141450f098SGreg Roach if ($this->isPendingDeletion()) { 4151450f098SGreg Roach return false; 4161450f098SGreg Roach } 4171450f098SGreg Roach 4181450f098SGreg Roach if (Auth::isManager($this->tree)) { 4191450f098SGreg Roach return true; 4201450f098SGreg Roach } 4211450f098SGreg Roach 4221450f098SGreg Roach return Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false; 423a25f0a04SGreg Roach } 424a25f0a04SGreg Roach 425a25f0a04SGreg Roach /** 426a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 427a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 428a25f0a04SGreg Roach * 429cbc1590aSGreg Roach * @param int $access_level 430a25f0a04SGreg Roach * 431a25f0a04SGreg Roach * @return string 432a25f0a04SGreg Roach */ 433e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 434c1010edaSGreg Roach { 435e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 436a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 437a25f0a04SGreg Roach return $this->gedcom; 438b2ce94c6SRico Sonntag } 439b2ce94c6SRico Sonntag 440b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 441a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 442a25f0a04SGreg Roach 443a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 44465e02381SGreg Roach [$gedrec] = explode("\n", $this->gedcom, 2); 445a25f0a04SGreg Roach 446a25f0a04SGreg Roach // Check each of the facts for access 4478d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 448138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 449a25f0a04SGreg Roach } 450cbc1590aSGreg Roach 451a25f0a04SGreg Roach return $gedrec; 452b2ce94c6SRico Sonntag } 453b2ce94c6SRico Sonntag 454a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 455a25f0a04SGreg Roach // limited data, such as links to other records. 456a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 457a25f0a04SGreg Roach } 458a25f0a04SGreg Roach 459a25f0a04SGreg Roach /** 460a25f0a04SGreg Roach * Default for "other" object types 461c7ff4153SGreg Roach * 462c7ff4153SGreg Roach * @return void 463a25f0a04SGreg Roach */ 464e364afe4SGreg Roach public function extractNames(): void 465c1010edaSGreg Roach { 46676f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 467a25f0a04SGreg Roach } 468a25f0a04SGreg Roach 469a25f0a04SGreg Roach /** 470a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 471a25f0a04SGreg Roach * 472a25f0a04SGreg Roach * @return string[][] 473a25f0a04SGreg Roach */ 4748f53f488SRico Sonntag public function getAllNames(): array 475c1010edaSGreg Roach { 476bdb3725aSGreg Roach if ($this->getAllNames === null) { 477bdb3725aSGreg Roach $this->getAllNames = []; 478a25f0a04SGreg Roach if ($this->canShowName()) { 479a25f0a04SGreg Roach // Ask the record to extract its names 480a25f0a04SGreg Roach $this->extractNames(); 481a25f0a04SGreg Roach // No name found? Use a fallback. 482bdb3725aSGreg Roach if (!$this->getAllNames) { 483db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 484a25f0a04SGreg Roach } 485a25f0a04SGreg Roach } else { 486db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 487a25f0a04SGreg Roach } 488a25f0a04SGreg Roach } 489cbc1590aSGreg Roach 490bdb3725aSGreg Roach return $this->getAllNames; 491a25f0a04SGreg Roach } 492a25f0a04SGreg Roach 493a25f0a04SGreg Roach /** 494a25f0a04SGreg Roach * If this object has no name, what do we call it? 495a25f0a04SGreg Roach * 496a25f0a04SGreg Roach * @return string 497a25f0a04SGreg Roach */ 4988f53f488SRico Sonntag public function getFallBackName(): string 499c1010edaSGreg Roach { 500c0935879SGreg Roach return e($this->xref()); 501a25f0a04SGreg Roach } 502a25f0a04SGreg Roach 503a25f0a04SGreg Roach /** 504a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 505a25f0a04SGreg Roach * 506cbc1590aSGreg Roach * @return int 507a25f0a04SGreg Roach */ 5088f53f488SRico Sonntag public function getPrimaryName(): int 509c1010edaSGreg Roach { 510a25f0a04SGreg Roach static $language_script; 511a25f0a04SGreg Roach 512a25f0a04SGreg Roach if ($language_script === null) { 51390a2f718SGreg Roach $locale = app(ServerRequestInterface::class)->getAttribute('locale'); 51490a2f718SGreg Roach assert($locale instanceof LocaleInterface); 51590a2f718SGreg Roach 51690a2f718SGreg Roach $language_script = $locale->script()->code(); 517a25f0a04SGreg Roach } 518a25f0a04SGreg Roach 519bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 520a25f0a04SGreg Roach // Generally, the first name is the primary one.... 521bdb3725aSGreg Roach $this->getPrimaryName = 0; 522a25f0a04SGreg Roach // ...except when the language/name use different character sets 523a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 52469546be1SGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 525bdb3725aSGreg Roach $this->getPrimaryName = $n; 526a25f0a04SGreg Roach break; 527a25f0a04SGreg Roach } 528a25f0a04SGreg Roach } 529a25f0a04SGreg Roach } 530a25f0a04SGreg Roach 531bdb3725aSGreg Roach return $this->getPrimaryName; 532a25f0a04SGreg Roach } 533a25f0a04SGreg Roach 534a25f0a04SGreg Roach /** 535a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 536a25f0a04SGreg Roach * 537cbc1590aSGreg Roach * @return int 538a25f0a04SGreg Roach */ 5398f53f488SRico Sonntag public function getSecondaryName(): int 540c1010edaSGreg Roach { 5418f038c36SRico Sonntag if ($this->getSecondaryName === null) { 542a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 543bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 544a25f0a04SGreg Roach // ....except when there are names with different character sets 545a25f0a04SGreg Roach $all_names = $this->getAllNames(); 546a25f0a04SGreg Roach if (count($all_names) > 1) { 547a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 548a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 549e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 550bdb3725aSGreg Roach $this->getSecondaryName = $n; 551a25f0a04SGreg Roach break; 552a25f0a04SGreg Roach } 553a25f0a04SGreg Roach } 554a25f0a04SGreg Roach } 555a25f0a04SGreg Roach } 556cbc1590aSGreg Roach 557bdb3725aSGreg Roach return $this->getSecondaryName; 558a25f0a04SGreg Roach } 559a25f0a04SGreg Roach 560a25f0a04SGreg Roach /** 561a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 562a25f0a04SGreg Roach * 56376f666f4SGreg Roach * @param int|null $n 5647e96c925SGreg Roach * 5657e96c925SGreg Roach * @return void 566a25f0a04SGreg Roach */ 567e364afe4SGreg Roach public function setPrimaryName(int $n = null): void 568c1010edaSGreg Roach { 569bdb3725aSGreg Roach $this->getPrimaryName = $n; 570bdb3725aSGreg Roach $this->getSecondaryName = null; 571a25f0a04SGreg Roach } 572a25f0a04SGreg Roach 573a25f0a04SGreg Roach /** 574a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 575a25f0a04SGreg Roach * 576a25f0a04SGreg Roach * @return string 577a25f0a04SGreg Roach */ 578c1010edaSGreg Roach public function __toString() 579c1010edaSGreg Roach { 58072cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 581a25f0a04SGreg Roach } 582a25f0a04SGreg Roach 583a25f0a04SGreg Roach /** 584c156e8f5SGreg Roach * /** 585a25f0a04SGreg Roach * Get variants of the name 586a25f0a04SGreg Roach * 587a25f0a04SGreg Roach * @return string 588a25f0a04SGreg Roach */ 589e364afe4SGreg Roach public function fullName(): string 590c1010edaSGreg Roach { 591a25f0a04SGreg Roach if ($this->canShowName()) { 592a25f0a04SGreg Roach $tmp = $this->getAllNames(); 593cbc1590aSGreg Roach 594a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 595a25f0a04SGreg Roach } 596b2ce94c6SRico Sonntag 597b2ce94c6SRico Sonntag return I18N::translate('Private'); 598a25f0a04SGreg Roach } 599a25f0a04SGreg Roach 600a25f0a04SGreg Roach /** 601a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 602a25f0a04SGreg Roach * 603a25f0a04SGreg Roach * @return string 604a25f0a04SGreg Roach */ 60539ca88baSGreg Roach public function sortName(): string 606c1010edaSGreg Roach { 607a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 608a25f0a04SGreg Roach $tmp = $this->getAllNames(); 609cbc1590aSGreg Roach 610a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 611a25f0a04SGreg Roach } 612a25f0a04SGreg Roach 613a25f0a04SGreg Roach /** 614a25f0a04SGreg Roach * Get the full name in an alternative character set 615a25f0a04SGreg Roach * 616e364afe4SGreg Roach * @return string|null 617a25f0a04SGreg Roach */ 618e364afe4SGreg Roach public function alternateName(): ?string 619c1010edaSGreg Roach { 620e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 621a25f0a04SGreg Roach $all_names = $this->getAllNames(); 622cbc1590aSGreg Roach 623a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 624a25f0a04SGreg Roach } 625b2ce94c6SRico Sonntag 626b2ce94c6SRico Sonntag return null; 627a25f0a04SGreg Roach } 628a25f0a04SGreg Roach 629a25f0a04SGreg Roach /** 630a25f0a04SGreg Roach * Format this object for display in a list 631a25f0a04SGreg Roach * 632a25f0a04SGreg Roach * @return string 633a25f0a04SGreg Roach */ 6348f53f488SRico Sonntag public function formatList(): string 635c1010edaSGreg Roach { 636b165e17cSGreg Roach $html = '<a href="' . e($this->url()) . '" class="list_item">'; 63739ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 638a25f0a04SGreg Roach $html .= $this->formatListDetails(); 639b165e17cSGreg Roach $html .= '</a>'; 640cbc1590aSGreg Roach 641a25f0a04SGreg Roach return $html; 642a25f0a04SGreg Roach } 643a25f0a04SGreg Roach 644a25f0a04SGreg Roach /** 645a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 646a25f0a04SGreg Roach * identifying characteristics of this record. 647a25f0a04SGreg Roach * 648a25f0a04SGreg Roach * @return string 649a25f0a04SGreg Roach */ 6508f53f488SRico Sonntag public function formatListDetails(): string 651c1010edaSGreg Roach { 652a25f0a04SGreg Roach return ''; 653a25f0a04SGreg Roach } 654a25f0a04SGreg Roach 655a25f0a04SGreg Roach /** 656a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 657a25f0a04SGreg Roach * 6588d0ebef0SGreg Roach * @param string[] $facts 659cbc1590aSGreg Roach * @param int $style 660a25f0a04SGreg Roach * 661a25f0a04SGreg Roach * @return string 662a25f0a04SGreg Roach */ 6638d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 664c1010edaSGreg Roach { 66530158ae7SGreg Roach foreach ($this->facts($facts, true) as $event) { 666a25f0a04SGreg Roach // Only display if it has a date or place (or both) 667e364afe4SGreg Roach if ($event->date()->isOK() && $event->place()->gedcomName() !== '') { 668d93f11b5SGreg Roach $joiner = ' — '; 669d93f11b5SGreg Roach } else { 670d93f11b5SGreg Roach $joiner = ''; 671d93f11b5SGreg Roach } 672e364afe4SGreg Roach if ($event->date()->isOK() || $event->place()->gedcomName() !== '') { 673a25f0a04SGreg Roach switch ($style) { 674a25f0a04SGreg Roach case 1: 6757b7d8067SGreg Roach return '<br><em>' . $event->label() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>'; 676a25f0a04SGreg Roach case 2: 6777b7d8067SGreg Roach return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>'; 678a25f0a04SGreg Roach } 679a25f0a04SGreg Roach } 680a25f0a04SGreg Roach } 681cbc1590aSGreg Roach 682a25f0a04SGreg Roach return ''; 683a25f0a04SGreg Roach } 684a25f0a04SGreg Roach 685a25f0a04SGreg Roach /** 686a25f0a04SGreg Roach * Find individuals linked to this record. 687a25f0a04SGreg Roach * 688a25f0a04SGreg Roach * @param string $link 689a25f0a04SGreg Roach * 690907c1109SGreg Roach * @return Collection 691a25f0a04SGreg Roach */ 692907c1109SGreg Roach public function linkedIndividuals(string $link): Collection 693c1010edaSGreg Roach { 694907c1109SGreg Roach return DB::table('individuals') 6950b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 696907c1109SGreg Roach $join 697907c1109SGreg Roach ->on('l_file', '=', 'i_file') 698907c1109SGreg Roach ->on('l_from', '=', 'i_id'); 699ba1c12e8SGreg Roach }) 700ba1c12e8SGreg Roach ->where('i_file', '=', $this->tree->id()) 701ba1c12e8SGreg Roach ->where('l_type', '=', $link) 702ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 703907c1109SGreg Roach ->select(['individuals.*']) 704907c1109SGreg Roach ->get() 705907c1109SGreg Roach ->map(Individual::rowMapper()) 706907c1109SGreg Roach ->filter(self::accessFilter()); 707a25f0a04SGreg Roach } 708a25f0a04SGreg Roach 709a25f0a04SGreg Roach /** 710a25f0a04SGreg Roach * Find families linked to this record. 711a25f0a04SGreg Roach * 712a25f0a04SGreg Roach * @param string $link 713a25f0a04SGreg Roach * 714907c1109SGreg Roach * @return Collection 715a25f0a04SGreg Roach */ 716907c1109SGreg Roach public function linkedFamilies(string $link): Collection 717c1010edaSGreg Roach { 718907c1109SGreg Roach return DB::table('families') 7190b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 720907c1109SGreg Roach $join 721907c1109SGreg Roach ->on('l_file', '=', 'f_file') 722907c1109SGreg Roach ->on('l_from', '=', 'f_id'); 723ba1c12e8SGreg Roach }) 724ba1c12e8SGreg Roach ->where('f_file', '=', $this->tree->id()) 725ba1c12e8SGreg Roach ->where('l_type', '=', $link) 726ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 727907c1109SGreg Roach ->select(['families.*']) 728907c1109SGreg Roach ->get() 729907c1109SGreg Roach ->map(Family::rowMapper()) 730907c1109SGreg Roach ->filter(self::accessFilter()); 731a25f0a04SGreg Roach } 732a25f0a04SGreg Roach 733a25f0a04SGreg Roach /** 734a25f0a04SGreg Roach * Find sources linked to this record. 735a25f0a04SGreg Roach * 736a25f0a04SGreg Roach * @param string $link 737a25f0a04SGreg Roach * 738907c1109SGreg Roach * @return Collection 739a25f0a04SGreg Roach */ 740907c1109SGreg Roach public function linkedSources(string $link): Collection 741c1010edaSGreg Roach { 742907c1109SGreg Roach return DB::table('sources') 7430b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 744907c1109SGreg Roach $join 745907c1109SGreg Roach ->on('l_file', '=', 's_file') 746907c1109SGreg Roach ->on('l_from', '=', 's_id'); 747ba1c12e8SGreg Roach }) 748ba1c12e8SGreg Roach ->where('s_file', '=', $this->tree->id()) 749ba1c12e8SGreg Roach ->where('l_type', '=', $link) 750ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 751907c1109SGreg Roach ->select(['sources.*']) 752907c1109SGreg Roach ->get() 753907c1109SGreg Roach ->map(Source::rowMapper()) 754907c1109SGreg Roach ->filter(self::accessFilter()); 755a25f0a04SGreg Roach } 756a25f0a04SGreg Roach 757a25f0a04SGreg Roach /** 758a25f0a04SGreg Roach * Find media objects linked to this record. 759a25f0a04SGreg Roach * 760a25f0a04SGreg Roach * @param string $link 761a25f0a04SGreg Roach * 762907c1109SGreg Roach * @return Collection 763a25f0a04SGreg Roach */ 764907c1109SGreg Roach public function linkedMedia(string $link): Collection 765c1010edaSGreg Roach { 766907c1109SGreg Roach return DB::table('media') 7670b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 768907c1109SGreg Roach $join 769907c1109SGreg Roach ->on('l_file', '=', 'm_file') 770907c1109SGreg Roach ->on('l_from', '=', 'm_id'); 771ba1c12e8SGreg Roach }) 772ba1c12e8SGreg Roach ->where('m_file', '=', $this->tree->id()) 773ba1c12e8SGreg Roach ->where('l_type', '=', $link) 774ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 775907c1109SGreg Roach ->select(['media.*']) 776907c1109SGreg Roach ->get() 777907c1109SGreg Roach ->map(Media::rowMapper()) 778907c1109SGreg Roach ->filter(self::accessFilter()); 779a25f0a04SGreg Roach } 780a25f0a04SGreg Roach 781a25f0a04SGreg Roach /** 782a25f0a04SGreg Roach * Find notes linked to this record. 783a25f0a04SGreg Roach * 784a25f0a04SGreg Roach * @param string $link 785a25f0a04SGreg Roach * 786907c1109SGreg Roach * @return Collection 787a25f0a04SGreg Roach */ 788907c1109SGreg Roach public function linkedNotes(string $link): Collection 789c1010edaSGreg Roach { 790907c1109SGreg Roach return DB::table('other') 7910b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 792907c1109SGreg Roach $join 793907c1109SGreg Roach ->on('l_file', '=', 'o_file') 794907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 795ba1c12e8SGreg Roach }) 796ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 797ba1c12e8SGreg Roach ->where('o_type', '=', 'NOTE') 798ba1c12e8SGreg Roach ->where('l_type', '=', $link) 799ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 800907c1109SGreg Roach ->select(['other.*']) 801907c1109SGreg Roach ->get() 802907c1109SGreg Roach ->map(Note::rowMapper()) 803907c1109SGreg Roach ->filter(self::accessFilter()); 804a25f0a04SGreg Roach } 805a25f0a04SGreg Roach 806a25f0a04SGreg Roach /** 807a25f0a04SGreg Roach * Find repositories linked to this record. 808a25f0a04SGreg Roach * 809a25f0a04SGreg Roach * @param string $link 810a25f0a04SGreg Roach * 811907c1109SGreg Roach * @return Collection 812a25f0a04SGreg Roach */ 813907c1109SGreg Roach public function linkedRepositories(string $link): Collection 814c1010edaSGreg Roach { 815907c1109SGreg Roach return DB::table('other') 8160b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 817907c1109SGreg Roach $join 818907c1109SGreg Roach ->on('l_file', '=', 'o_file') 819907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 820ba1c12e8SGreg Roach }) 821ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 822ba1c12e8SGreg Roach ->where('o_type', '=', 'REPO') 823ba1c12e8SGreg Roach ->where('l_type', '=', $link) 824ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 825907c1109SGreg Roach ->select(['other.*']) 826907c1109SGreg Roach ->get() 827907c1109SGreg Roach ->map(Individual::rowMapper()) 828907c1109SGreg Roach ->filter(self::accessFilter()); 829a25f0a04SGreg Roach } 830a25f0a04SGreg Roach 831a25f0a04SGreg Roach /** 832a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 833a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 834a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 835a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 836a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 837a25f0a04SGreg Roach * 8388d0ebef0SGreg Roach * @param string[] $events 839a25f0a04SGreg Roach * 840a25f0a04SGreg Roach * @return Date[] 841a25f0a04SGreg Roach */ 8428d0ebef0SGreg Roach public function getAllEventDates(array $events): array 843c1010edaSGreg Roach { 84413abd6f3SGreg Roach $dates = []; 8458d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 8462decada7SGreg Roach if ($event->date()->isOK()) { 8472decada7SGreg Roach $dates[] = $event->date(); 848a25f0a04SGreg Roach } 849a25f0a04SGreg Roach } 850a25f0a04SGreg Roach 851a25f0a04SGreg Roach return $dates; 852a25f0a04SGreg Roach } 853a25f0a04SGreg Roach 854a25f0a04SGreg Roach /** 855a25f0a04SGreg Roach * Get all the places for a particular type of event 856a25f0a04SGreg Roach * 8578d0ebef0SGreg Roach * @param string[] $events 858a25f0a04SGreg Roach * 8594080d558SGreg Roach * @return Place[] 860a25f0a04SGreg Roach */ 8618d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 862c1010edaSGreg Roach { 86313abd6f3SGreg Roach $places = []; 8648d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 865138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 866a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 86716d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 868a25f0a04SGreg Roach } 869a25f0a04SGreg Roach } 870a25f0a04SGreg Roach } 871a25f0a04SGreg Roach 872a25f0a04SGreg Roach return $places; 873a25f0a04SGreg Roach } 874a25f0a04SGreg Roach 875a25f0a04SGreg Roach /** 876a25f0a04SGreg Roach * The facts and events for this record. 877a25f0a04SGreg Roach * 8788d0ebef0SGreg Roach * @param string[] $filter 879cbc1590aSGreg Roach * @param bool $sort 880cbc1590aSGreg Roach * @param int|null $access_level 881cbc1590aSGreg Roach * @param bool $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES. 882a25f0a04SGreg Roach * 88354c7f8dfSGreg Roach * @return Collection 884a25f0a04SGreg Roach */ 88539ca88baSGreg Roach public function facts(array $filter = [], bool $sort = false, int $access_level = null, bool $override = false): Collection 886c1010edaSGreg Roach { 8874b9ff166SGreg Roach if ($access_level === null) { 8884b9ff166SGreg Roach $access_level = Auth::accessLevel($this->tree); 8894b9ff166SGreg Roach } 8904b9ff166SGreg Roach 8918af3e5c1SGreg Roach $facts = new Collection(); 892a25f0a04SGreg Roach if ($this->canShow($access_level) || $override) { 893a25f0a04SGreg Roach foreach ($this->facts as $fact) { 89422d65e5aSGreg Roach if (($filter === [] || in_array($fact->getTag(), $filter, true)) && $fact->canShow($access_level)) { 8958af3e5c1SGreg Roach $facts->push($fact); 896a25f0a04SGreg Roach } 897a25f0a04SGreg Roach } 898a25f0a04SGreg Roach } 899d17d7b9eSGreg Roach 900a25f0a04SGreg Roach if ($sort) { 901580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 902a25f0a04SGreg Roach } 903cbc1590aSGreg Roach 90439ca88baSGreg Roach return new Collection($facts); 905a25f0a04SGreg Roach } 906a25f0a04SGreg Roach 907a25f0a04SGreg Roach /** 9084459dc9aSGreg Roach * Get the last-change timestamp for this record 909a25f0a04SGreg Roach * 9104459dc9aSGreg Roach * @return Carbon 911a25f0a04SGreg Roach */ 9124459dc9aSGreg Roach public function lastChangeTimestamp(): Carbon 913c1010edaSGreg Roach { 9144459dc9aSGreg Roach /** @var Fact|null $chan */ 915820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 916a25f0a04SGreg Roach 9174459dc9aSGreg Roach if ($chan instanceof Fact) { 918a25f0a04SGreg Roach // The record does have a CHAN event 9192decada7SGreg Roach $d = $chan->date()->minimumDate(); 9204459dc9aSGreg Roach 921138ca96cSGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->gedcom(), $match)) { 9224459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2], (int) $match[3]); 923e364afe4SGreg Roach } 924e364afe4SGreg Roach 925e364afe4SGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->gedcom(), $match)) { 9264459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2]); 927b2ce94c6SRico Sonntag } 928b2ce94c6SRico Sonntag 9294459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day()); 930a25f0a04SGreg Roach } 931b2ce94c6SRico Sonntag 932a25f0a04SGreg Roach // The record does not have a CHAN event 9334459dc9aSGreg Roach return Carbon::createFromTimestamp(0); 934a25f0a04SGreg Roach } 935a25f0a04SGreg Roach 936a25f0a04SGreg Roach /** 937a25f0a04SGreg Roach * Get the last-change user for this record 938a25f0a04SGreg Roach * 939a25f0a04SGreg Roach * @return string 940a25f0a04SGreg Roach */ 941e364afe4SGreg Roach public function lastChangeUser(): string 942c1010edaSGreg Roach { 943820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 944a25f0a04SGreg Roach 945a25f0a04SGreg Roach if ($chan === null) { 946a25f0a04SGreg Roach return I18N::translate('Unknown'); 947b2ce94c6SRico Sonntag } 948b2ce94c6SRico Sonntag 9493425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 950baacc364SGreg Roach if ($chan_user === '') { 951a25f0a04SGreg Roach return I18N::translate('Unknown'); 952b2ce94c6SRico Sonntag } 953b2ce94c6SRico Sonntag 954a25f0a04SGreg Roach return $chan_user; 955a25f0a04SGreg Roach } 956a25f0a04SGreg Roach 957a25f0a04SGreg Roach /** 958a25f0a04SGreg Roach * Add a new fact to this record 959a25f0a04SGreg Roach * 960a25f0a04SGreg Roach * @param string $gedcom 961cbc1590aSGreg Roach * @param bool $update_chan 9627e96c925SGreg Roach * 9637e96c925SGreg Roach * @return void 964a25f0a04SGreg Roach */ 965e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 966c1010edaSGreg Roach { 967fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 968a25f0a04SGreg Roach } 969a25f0a04SGreg Roach 970a25f0a04SGreg Roach /** 971a25f0a04SGreg Roach * Delete a fact from this record 972a25f0a04SGreg Roach * 973a25f0a04SGreg Roach * @param string $fact_id 974cbc1590aSGreg Roach * @param bool $update_chan 9757e96c925SGreg Roach * 9767e96c925SGreg Roach * @return void 977a25f0a04SGreg Roach */ 978e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 979c1010edaSGreg Roach { 980db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 981a25f0a04SGreg Roach } 982a25f0a04SGreg Roach 983a25f0a04SGreg Roach /** 984a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 985a25f0a04SGreg Roach * 986a25f0a04SGreg Roach * @param string $fact_id 987a25f0a04SGreg Roach * @param string $gedcom 988cbc1590aSGreg Roach * @param bool $update_chan 989a25f0a04SGreg Roach * 9907e96c925SGreg Roach * @return void 9917e96c925SGreg Roach * @throws Exception 992a25f0a04SGreg Roach */ 993e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 994c1010edaSGreg Roach { 995a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 996a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 997a25f0a04SGreg Roach $gedcom = trim($gedcom); 998a25f0a04SGreg Roach 999a25f0a04SGreg Roach if ($this->pending === '') { 10007e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 1001a25f0a04SGreg Roach } 10028d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 10037e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 1004a25f0a04SGreg Roach } 1005a25f0a04SGreg Roach 1006a25f0a04SGreg Roach if ($this->pending) { 1007a25f0a04SGreg Roach $old_gedcom = $this->pending; 1008a25f0a04SGreg Roach } else { 1009a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 1010a25f0a04SGreg Roach } 1011a25f0a04SGreg Roach 1012a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 101365e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 1014a25f0a04SGreg Roach 1015a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 10168d0ebef0SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE) as $fact) { 1017a25f0a04SGreg Roach if (!$fact->isPendingDeletion()) { 1018905ab80aSGreg Roach if ($fact->id() === $fact_id) { 1019db7bb364SGreg Roach if ($gedcom !== '') { 1020a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1021a25f0a04SGreg Roach } 1022fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 1023e364afe4SGreg Roach } elseif ($fact->getTag() !== 'CHAN' || !$update_chan) { 1024138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 1025a25f0a04SGreg Roach } 1026a25f0a04SGreg Roach } 1027a25f0a04SGreg Roach } 1028a25f0a04SGreg Roach if ($update_chan) { 1029e5a6b4d4SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1030a25f0a04SGreg Roach } 1031a25f0a04SGreg Roach 1032a25f0a04SGreg Roach // Adding a new fact 1033fc3ccce4SGreg Roach if ($fact_id === '') { 1034a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1035a25f0a04SGreg Roach } 1036a25f0a04SGreg Roach 1037e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 1038a25f0a04SGreg Roach // Save the changes 1039d09b6323SGreg Roach DB::table('change')->insert([ 1040d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1041d09b6323SGreg Roach 'xref' => $this->xref, 1042d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 1043d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 1044d09b6323SGreg Roach 'user_id' => Auth::id(), 104513abd6f3SGreg Roach ]); 1046a25f0a04SGreg Roach 1047a25f0a04SGreg Roach $this->pending = $new_gedcom; 1048a25f0a04SGreg Roach 1049*7c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 105022e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1051a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 1052a25f0a04SGreg Roach $this->pending = null; 1053a25f0a04SGreg Roach } 1054a25f0a04SGreg Roach } 1055a25f0a04SGreg Roach $this->parseFacts(); 1056a25f0a04SGreg Roach } 1057a25f0a04SGreg Roach 1058a25f0a04SGreg Roach /** 1059a25f0a04SGreg Roach * Update this record 1060a25f0a04SGreg Roach * 1061a25f0a04SGreg Roach * @param string $gedcom 1062cbc1590aSGreg Roach * @param bool $update_chan 10637e96c925SGreg Roach * 10647e96c925SGreg Roach * @return void 1065a25f0a04SGreg Roach */ 1066e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 1067c1010edaSGreg Roach { 1068a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1069a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1070a25f0a04SGreg Roach $gedcom = trim($gedcom); 1071a25f0a04SGreg Roach 1072a25f0a04SGreg Roach // Update the CHAN record 1073a25f0a04SGreg Roach if ($update_chan) { 1074a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 1075e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1076a25f0a04SGreg Roach } 1077a25f0a04SGreg Roach 1078a25f0a04SGreg Roach // Create a pending change 1079d09b6323SGreg Roach DB::table('change')->insert([ 1080d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1081d09b6323SGreg Roach 'xref' => $this->xref, 1082d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1083d09b6323SGreg Roach 'new_gedcom' => $gedcom, 1084d09b6323SGreg Roach 'user_id' => Auth::id(), 108513abd6f3SGreg Roach ]); 1086a25f0a04SGreg Roach 1087a25f0a04SGreg Roach // Clear the cache 1088a25f0a04SGreg Roach $this->pending = $gedcom; 1089a25f0a04SGreg Roach 1090a25f0a04SGreg Roach // Accept this pending change 1091*7c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 109222e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1093a25f0a04SGreg Roach $this->gedcom = $gedcom; 1094a25f0a04SGreg Roach $this->pending = null; 1095a25f0a04SGreg Roach } 1096a25f0a04SGreg Roach 1097a25f0a04SGreg Roach $this->parseFacts(); 1098a25f0a04SGreg Roach 1099847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1100a25f0a04SGreg Roach } 1101a25f0a04SGreg Roach 1102a25f0a04SGreg Roach /** 1103a25f0a04SGreg Roach * Delete this record 1104b874da82SGreg Roach * 1105b874da82SGreg Roach * @return void 1106a25f0a04SGreg Roach */ 1107e364afe4SGreg Roach public function deleteRecord(): void 1108c1010edaSGreg Roach { 1109a25f0a04SGreg Roach // Create a pending change 11104c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 1111d09b6323SGreg Roach DB::table('change')->insert([ 1112d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1113d09b6323SGreg Roach 'xref' => $this->xref, 1114d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1115d09b6323SGreg Roach 'new_gedcom' => '', 1116d09b6323SGreg Roach 'user_id' => Auth::id(), 111713abd6f3SGreg Roach ]); 11184c0b5256SGreg Roach } 1119a25f0a04SGreg Roach 11204c0b5256SGreg Roach // Auto-accept this pending change 1121*7c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 112222e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1123a25f0a04SGreg Roach } 1124a25f0a04SGreg Roach 1125a25f0a04SGreg Roach // Clear the cache 1126d17d7b9eSGreg Roach self::$gedcom_record_cache = []; 1127d17d7b9eSGreg Roach self::$pending_record_cache = []; 1128a25f0a04SGreg Roach 1129847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1130a25f0a04SGreg Roach } 1131a25f0a04SGreg Roach 1132a25f0a04SGreg Roach /** 1133a25f0a04SGreg Roach * Remove all links from this record to $xref 1134a25f0a04SGreg Roach * 1135a25f0a04SGreg Roach * @param string $xref 1136cbc1590aSGreg Roach * @param bool $update_chan 11377e96c925SGreg Roach * 11387e96c925SGreg Roach * @return void 1139a25f0a04SGreg Roach */ 1140e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 1141c1010edaSGreg Roach { 1142a25f0a04SGreg Roach $value = '@' . $xref . '@'; 1143a25f0a04SGreg Roach 114430158ae7SGreg Roach foreach ($this->facts() as $fact) { 114584586c02SGreg Roach if ($fact->value() === $value) { 1146905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 11478d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 1148138ca96cSGreg Roach $gedcom = $fact->gedcom(); 1149a25f0a04SGreg Roach foreach ($matches as $match) { 1150a25f0a04SGreg Roach $next_level = $match[1] + 1; 1151a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 1152a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 1153a25f0a04SGreg Roach } 1154905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 1155a25f0a04SGreg Roach } 1156a25f0a04SGreg Roach } 1157a25f0a04SGreg Roach } 1158bf4eb542SGreg Roach 1159bf4eb542SGreg Roach /** 1160bf4eb542SGreg Roach * Fetch XREFs of all records linked to a record - when deleting an object, we must 1161bf4eb542SGreg Roach * also delete all links to it. 1162bf4eb542SGreg Roach * 1163bf4eb542SGreg Roach * @return GedcomRecord[] 1164bf4eb542SGreg Roach */ 1165bf4eb542SGreg Roach public function linkingRecords(): array 1166bf4eb542SGreg Roach { 1167bf4eb542SGreg Roach $union = DB::table('change') 1168bf4eb542SGreg Roach ->where('gedcom_id', '=', $this->tree()->id()) 1169fbbe964bSGreg Roach ->whereContains('new_gedcom', '@' . $this->xref() . '@') 1170bf4eb542SGreg Roach ->where('new_gedcom', 'NOT LIKE', '0 @' . $this->xref() . '@%') 117183242252SGreg Roach ->whereIn('change_id', function (Builder $query): void { 117283242252SGreg Roach $query->select(new Expression('MAX(change_id)')) 117383242252SGreg Roach ->from('change') 117483242252SGreg Roach ->where('gedcom_id', '=', $this->tree->id()) 117583242252SGreg Roach ->where('status', '=', 'pending') 11767f5c2944SGreg Roach ->groupBy(['xref']); 117783242252SGreg Roach }) 1178bf4eb542SGreg Roach ->select(['xref']); 1179bf4eb542SGreg Roach 1180bf4eb542SGreg Roach $xrefs = DB::table('link') 1181bf4eb542SGreg Roach ->where('l_file', '=', $this->tree()->id()) 1182bf4eb542SGreg Roach ->where('l_to', '=', $this->xref()) 118347256fc5SGreg Roach ->select(['l_from']) 1184bf4eb542SGreg Roach ->union($union) 1185bf4eb542SGreg Roach ->pluck('l_from'); 1186bf4eb542SGreg Roach 1187bf4eb542SGreg Roach return $xrefs->map(function (string $xref): GedcomRecord { 1188bf4eb542SGreg Roach return GedcomRecord::getInstance($xref, $this->tree); 1189bf4eb542SGreg Roach })->all(); 1190bf4eb542SGreg Roach } 119122e73debSGreg Roach 119222e73debSGreg Roach /** 119322e73debSGreg Roach * Each object type may have its own special rules, and re-implement this function. 119422e73debSGreg Roach * 119522e73debSGreg Roach * @param int $access_level 119622e73debSGreg Roach * 119722e73debSGreg Roach * @return bool 119822e73debSGreg Roach */ 119922e73debSGreg Roach protected function canShowByType(int $access_level): bool 120022e73debSGreg Roach { 120122e73debSGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 120222e73debSGreg Roach 120322e73debSGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 120422e73debSGreg Roach // Restriction found 120522e73debSGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 120622e73debSGreg Roach } 120722e73debSGreg Roach 120822e73debSGreg Roach // No restriction found - must be public: 120922e73debSGreg Roach return true; 121022e73debSGreg Roach } 121122e73debSGreg Roach 121222e73debSGreg Roach /** 121322e73debSGreg Roach * Generate a private version of this record 121422e73debSGreg Roach * 121522e73debSGreg Roach * @param int $access_level 121622e73debSGreg Roach * 121722e73debSGreg Roach * @return string 121822e73debSGreg Roach */ 121922e73debSGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 122022e73debSGreg Roach { 122122e73debSGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private'); 122222e73debSGreg Roach } 122322e73debSGreg Roach 122422e73debSGreg Roach /** 122522e73debSGreg Roach * Convert a name record into sortable and full/display versions. This default 122622e73debSGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 122722e73debSGreg Roach * 122822e73debSGreg Roach * @param string $type 122922e73debSGreg Roach * @param string $value 123022e73debSGreg Roach * @param string $gedcom 123122e73debSGreg Roach * 123222e73debSGreg Roach * @return void 123322e73debSGreg Roach */ 123422e73debSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 123522e73debSGreg Roach { 123622e73debSGreg Roach $this->getAllNames[] = [ 123722e73debSGreg Roach 'type' => $type, 123822e73debSGreg Roach 'sort' => preg_replace_callback('/([0-9]+)/', static function (array $matches): string { 123922e73debSGreg Roach return str_pad($matches[0], 10, '0', STR_PAD_LEFT); 124022e73debSGreg Roach }, $value), 124122e73debSGreg Roach 'full' => '<span dir="auto">' . e($value) . '</span>', 124222e73debSGreg Roach // This is used for display 124322e73debSGreg Roach 'fullNN' => $value, 124422e73debSGreg Roach // This goes into the database 124522e73debSGreg Roach ]; 124622e73debSGreg Roach } 124722e73debSGreg Roach 124822e73debSGreg Roach /** 124922e73debSGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 125022e73debSGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 125122e73debSGreg Roach * Parameters: the level 1 fact containing the name. 125222e73debSGreg Roach * Return value: an array of name structures, each containing 125322e73debSGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 125422e73debSGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 125522e73debSGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 125622e73debSGreg Roach * 125722e73debSGreg Roach * @param int $level 125822e73debSGreg Roach * @param string $fact_type 125922e73debSGreg Roach * @param Collection $facts 126022e73debSGreg Roach * 126122e73debSGreg Roach * @return void 126222e73debSGreg Roach */ 126322e73debSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 126422e73debSGreg Roach { 126522e73debSGreg Roach $sublevel = $level + 1; 126622e73debSGreg Roach $subsublevel = $sublevel + 1; 126722e73debSGreg Roach foreach ($facts as $fact) { 126822e73debSGreg Roach if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) { 126922e73debSGreg Roach foreach ($matches as $match) { 127022e73debSGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 127122e73debSGreg Roach if ($match[1] === 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) { 127222e73debSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 127322e73debSGreg Roach } else { 127422e73debSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 127522e73debSGreg Roach } 127622e73debSGreg Roach if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { 127722e73debSGreg Roach foreach ($submatches as $submatch) { 127822e73debSGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 127922e73debSGreg Roach } 128022e73debSGreg Roach } 128122e73debSGreg Roach } 128222e73debSGreg Roach } 128322e73debSGreg Roach } 128422e73debSGreg Roach } 128522e73debSGreg Roach 128622e73debSGreg Roach /** 128722e73debSGreg Roach * Split the record into facts 128822e73debSGreg Roach * 128922e73debSGreg Roach * @return void 129022e73debSGreg Roach */ 129122e73debSGreg Roach private function parseFacts(): void 129222e73debSGreg Roach { 129322e73debSGreg Roach // Split the record into facts 129422e73debSGreg Roach if ($this->gedcom) { 129522e73debSGreg Roach $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom); 129622e73debSGreg Roach array_shift($gedcom_facts); 129722e73debSGreg Roach } else { 129822e73debSGreg Roach $gedcom_facts = []; 129922e73debSGreg Roach } 130022e73debSGreg Roach if ($this->pending) { 130122e73debSGreg Roach $pending_facts = preg_split('/\n(?=1)/s', $this->pending); 130222e73debSGreg Roach array_shift($pending_facts); 130322e73debSGreg Roach } else { 130422e73debSGreg Roach $pending_facts = []; 130522e73debSGreg Roach } 130622e73debSGreg Roach 130722e73debSGreg Roach $this->facts = []; 130822e73debSGreg Roach 130922e73debSGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 131022e73debSGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 131122e73debSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 131222e73debSGreg Roach $fact->setPendingDeletion(); 131322e73debSGreg Roach } 131422e73debSGreg Roach $this->facts[] = $fact; 131522e73debSGreg Roach } 131622e73debSGreg Roach foreach ($pending_facts as $pending_fact) { 131722e73debSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 131822e73debSGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 131922e73debSGreg Roach $fact->setPendingAddition(); 132022e73debSGreg Roach $this->facts[] = $fact; 132122e73debSGreg Roach } 132222e73debSGreg Roach } 132322e73debSGreg Roach } 132422e73debSGreg Roach 132522e73debSGreg Roach /** 132622e73debSGreg Roach * Work out whether this record can be shown to a user with a given access level 132722e73debSGreg Roach * 132822e73debSGreg Roach * @param int $access_level 132922e73debSGreg Roach * 133022e73debSGreg Roach * @return bool 133122e73debSGreg Roach */ 133222e73debSGreg Roach private function canShowRecord(int $access_level): bool 133322e73debSGreg Roach { 133422e73debSGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 133522e73debSGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 133622e73debSGreg Roach return true; 133722e73debSGreg Roach } 133822e73debSGreg Roach 133922e73debSGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 1340*7c4add84SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) { 134122e73debSGreg Roach return true; 134222e73debSGreg Roach } 134322e73debSGreg Roach 134422e73debSGreg Roach // Does this record have a RESN? 134522e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN confidential") !== false) { 134622e73debSGreg Roach return Auth::PRIV_NONE >= $access_level; 134722e73debSGreg Roach } 134822e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN privacy") !== false) { 134922e73debSGreg Roach return Auth::PRIV_USER >= $access_level; 135022e73debSGreg Roach } 135122e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN none") !== false) { 135222e73debSGreg Roach return true; 135322e73debSGreg Roach } 135422e73debSGreg Roach 135522e73debSGreg Roach // Does this record have a default RESN? 135622e73debSGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 135722e73debSGreg Roach if (isset($individual_privacy[$this->xref()])) { 135822e73debSGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 135922e73debSGreg Roach } 136022e73debSGreg Roach 136122e73debSGreg Roach // Privacy rules do not apply to admins 136222e73debSGreg Roach if (Auth::PRIV_NONE >= $access_level) { 136322e73debSGreg Roach return true; 136422e73debSGreg Roach } 136522e73debSGreg Roach 136622e73debSGreg Roach // Different types of record have different privacy rules 136722e73debSGreg Roach return $this->canShowByType($access_level); 136822e73debSGreg Roach } 1369a25f0a04SGreg Roach} 1370