1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 51fe542e9SGreg Roach * Copyright (C) 2021 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 241fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 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; 33a25f0a04SGreg Roach 34b5961194SGreg Roachuse function addcslashes; 3522e73debSGreg Roachuse function app; 360f5fd22fSGreg Roachuse function array_combine; 370f5fd22fSGreg Roachuse function array_keys; 380f5fd22fSGreg Roachuse function array_map; 390f5fd22fSGreg Roachuse function array_search; 40f7440925SGreg Roachuse function array_shift; 41f7440925SGreg Roachuse function assert; 42f7440925SGreg Roachuse function count; 43f7440925SGreg Roachuse function date; 44f7440925SGreg Roachuse function e; 45f7440925SGreg Roachuse function explode; 46f7440925SGreg Roachuse function in_array; 47f7440925SGreg Roachuse function md5; 48f7440925SGreg Roachuse function preg_match; 49f7440925SGreg Roachuse function preg_match_all; 50f7440925SGreg Roachuse function preg_replace; 51f7440925SGreg Roachuse function preg_replace_callback; 52f7440925SGreg Roachuse function preg_split; 530f5fd22fSGreg Roachuse function range; 54f7440925SGreg Roachuse function route; 55dec352c1SGreg Roachuse function str_contains; 56d0889c63SGreg Roachuse function str_ends_with; 57f7440925SGreg Roachuse function str_pad; 58f7440925SGreg Roachuse function strtoupper; 59f7440925SGreg Roachuse function trim; 60f7440925SGreg Roach 61c2ed51d1SGreg Roachuse const PHP_INT_MAX; 62f7440925SGreg Roachuse const PREG_SET_ORDER; 63f7440925SGreg Roachuse const STR_PAD_LEFT; 6422e73debSGreg Roach 65a25f0a04SGreg Roach/** 6676692c8bSGreg Roach * A GEDCOM object. 67a25f0a04SGreg Roach */ 68c1010edaSGreg Roachclass GedcomRecord 69c1010edaSGreg Roach{ 7016d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 7116d6367aSGreg Roach 725818a371SGreg Roach protected const ROUTE_NAME = GedcomRecordPage::class; 735818a371SGreg Roach 7422e73debSGreg Roach /** @var string The record identifier */ 7522e73debSGreg Roach protected $xref; 76bb03c9f0SGreg Roach 7722e73debSGreg Roach /** @var Tree The family tree to which this record belongs */ 7822e73debSGreg Roach protected $tree; 79bb03c9f0SGreg Roach 8022e73debSGreg Roach /** @var string GEDCOM data (before any pending edits) */ 8122e73debSGreg Roach protected $gedcom; 82bb03c9f0SGreg Roach 8322e73debSGreg Roach /** @var string|null GEDCOM data (after any pending edits) */ 8422e73debSGreg Roach protected $pending; 85bb03c9f0SGreg Roach 8622e73debSGreg Roach /** @var Fact[] facts extracted from $gedcom/$pending */ 8722e73debSGreg Roach protected $facts; 88bb03c9f0SGreg Roach 8922e73debSGreg Roach /** @var string[][] All the names of this individual */ 9022e73debSGreg Roach protected $getAllNames; 91bb03c9f0SGreg Roach 9222e73debSGreg Roach /** @var int|null Cached result */ 9322e73debSGreg Roach protected $getPrimaryName; 94c4943cffSGreg Roach 9522e73debSGreg Roach /** @var int|null Cached result */ 9622e73debSGreg Roach protected $getSecondaryName; 97a25f0a04SGreg Roach 98a25f0a04SGreg Roach /** 99a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 100a25f0a04SGreg Roach * 101a25f0a04SGreg Roach * @param string $xref 102a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 103a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 104a25f0a04SGreg Roach * empty string for records with pending deletions 10524ec66ceSGreg Roach * @param Tree $tree 106a25f0a04SGreg Roach */ 107e364afe4SGreg Roach public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 108c1010edaSGreg Roach { 109a25f0a04SGreg Roach $this->xref = $xref; 110a25f0a04SGreg Roach $this->gedcom = $gedcom; 111a25f0a04SGreg Roach $this->pending = $pending; 11224ec66ceSGreg Roach $this->tree = $tree; 113a25f0a04SGreg Roach 114a25f0a04SGreg Roach $this->parseFacts(); 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach 117a25f0a04SGreg Roach /** 118886b77daSGreg Roach * A closure which will filter out private records. 119886b77daSGreg Roach * 120886b77daSGreg Roach * @return Closure 121886b77daSGreg Roach */ 1224146fabcSGreg Roach public static function accessFilter(): Closure 123886b77daSGreg Roach { 1246c2179e2SGreg Roach return static function (GedcomRecord $record): bool { 125886b77daSGreg Roach return $record->canShow(); 126886b77daSGreg Roach }; 127886b77daSGreg Roach } 128886b77daSGreg Roach 129886b77daSGreg Roach /** 130c156e8f5SGreg Roach * A closure which will compare records by name. 131c156e8f5SGreg Roach * 132c156e8f5SGreg Roach * @return Closure 133c156e8f5SGreg Roach */ 134c156e8f5SGreg Roach public static function nameComparator(): Closure 135c156e8f5SGreg Roach { 1366c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y): int { 137c156e8f5SGreg Roach if ($x->canShowName()) { 138c156e8f5SGreg Roach if ($y->canShowName()) { 13937646143SGreg Roach return I18N::comparator()($x->sortName(), $y->sortName()); 140c156e8f5SGreg Roach } 141c156e8f5SGreg Roach 142c156e8f5SGreg Roach return -1; // only $y is private 143c156e8f5SGreg Roach } 144c156e8f5SGreg Roach 145c156e8f5SGreg Roach if ($y->canShowName()) { 146c156e8f5SGreg Roach return 1; // only $x is private 147c156e8f5SGreg Roach } 148c156e8f5SGreg Roach 149c156e8f5SGreg Roach return 0; // both $x and $y private 150c156e8f5SGreg Roach }; 151c156e8f5SGreg Roach } 152c156e8f5SGreg Roach 153c156e8f5SGreg Roach /** 154c156e8f5SGreg Roach * A closure which will compare records by change time. 155c156e8f5SGreg Roach * 156c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 157c156e8f5SGreg Roach * 158c156e8f5SGreg Roach * @return Closure 159c156e8f5SGreg Roach */ 160c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 161c156e8f5SGreg Roach { 1626c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y) use ($direction): int { 1634459dc9aSGreg Roach return $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp()); 164c156e8f5SGreg Roach }; 165c156e8f5SGreg Roach } 166c156e8f5SGreg Roach 167c156e8f5SGreg Roach /** 16802467d32SGreg Roach * Get the GEDCOM tag for this record. 16902467d32SGreg Roach * 17002467d32SGreg Roach * @return string 17102467d32SGreg Roach */ 17202467d32SGreg Roach public function tag(): string 17302467d32SGreg Roach { 17402467d32SGreg Roach preg_match('/^0 @[^@]*@ (\w+)/', $this->gedcom(), $match); 17502467d32SGreg Roach 17602467d32SGreg Roach return $match[1] ?? static::RECORD_TYPE; 17702467d32SGreg Roach } 17802467d32SGreg Roach 17902467d32SGreg Roach /** 180a25f0a04SGreg Roach * Get the XREF for this record 181a25f0a04SGreg Roach * 182a25f0a04SGreg Roach * @return string 183a25f0a04SGreg Roach */ 184c0935879SGreg Roach public function xref(): string 185c1010edaSGreg Roach { 186a25f0a04SGreg Roach return $this->xref; 187a25f0a04SGreg Roach } 188a25f0a04SGreg Roach 189a25f0a04SGreg Roach /** 190000959d9SGreg Roach * Get the tree to which this record belongs 191000959d9SGreg Roach * 192000959d9SGreg Roach * @return Tree 193000959d9SGreg Roach */ 194f4afa648SGreg Roach public function tree(): Tree 195c1010edaSGreg Roach { 196518bbdc1SGreg Roach return $this->tree; 197000959d9SGreg Roach } 198000959d9SGreg Roach 199000959d9SGreg Roach /** 200a25f0a04SGreg Roach * Application code should access data via Fact objects. 201a25f0a04SGreg Roach * This function exists to support old code. 202a25f0a04SGreg Roach * 203a25f0a04SGreg Roach * @return string 204a25f0a04SGreg Roach */ 205e364afe4SGreg Roach public function gedcom(): string 206c1010edaSGreg Roach { 207b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach 210a25f0a04SGreg Roach /** 211a25f0a04SGreg Roach * Does this record have a pending change? 212a25f0a04SGreg Roach * 213cbc1590aSGreg Roach * @return bool 214a25f0a04SGreg Roach */ 2158f53f488SRico Sonntag public function isPendingAddition(): bool 216c1010edaSGreg Roach { 217a25f0a04SGreg Roach return $this->pending !== null; 218a25f0a04SGreg Roach } 219a25f0a04SGreg Roach 220a25f0a04SGreg Roach /** 221a25f0a04SGreg Roach * Does this record have a pending deletion? 222a25f0a04SGreg Roach * 223cbc1590aSGreg Roach * @return bool 224a25f0a04SGreg Roach */ 2258f53f488SRico Sonntag public function isPendingDeletion(): bool 226c1010edaSGreg Roach { 227a25f0a04SGreg Roach return $this->pending === ''; 228a25f0a04SGreg Roach } 229a25f0a04SGreg Roach 230a25f0a04SGreg Roach /** 231225e381fSGreg Roach * Generate a URL to this record. 232a25f0a04SGreg Roach * 233a25f0a04SGreg Roach * @return string 234a25f0a04SGreg Roach */ 2358f53f488SRico Sonntag public function url(): string 236c1010edaSGreg Roach { 237225e381fSGreg Roach return route(static::ROUTE_NAME, [ 238c0935879SGreg Roach 'xref' => $this->xref(), 239ee4364daSGreg Roach 'tree' => $this->tree->name(), 240194b0938SGreg Roach 'slug' => Registry::slugFactory()->make($this), 241225e381fSGreg Roach ]); 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach 244a25f0a04SGreg Roach /** 245a25f0a04SGreg Roach * Can the details of this record be shown? 246a25f0a04SGreg Roach * 247cbc1590aSGreg Roach * @param int|null $access_level 248a25f0a04SGreg Roach * 249cbc1590aSGreg Roach * @return bool 250a25f0a04SGreg Roach */ 25135584196SGreg Roach public function canShow(int $access_level = null): bool 252c1010edaSGreg Roach { 253f0b9c048SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 2544b9ff166SGreg Roach 255a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 256a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 257f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 258a25f0a04SGreg Roach return true; 259a25f0a04SGreg Roach } 260f0b9c048SGreg Roach 2617476e8a5SGreg Roach $cache_key = 'show-' . $this->xref . '-' . $this->tree->id() . '-' . $access_level; 262f0b9c048SGreg Roach 2636b9cb339SGreg Roach return Registry::cache()->array()->remember($cache_key, function () use ($access_level) { 264f0b9c048SGreg Roach return $this->canShowRecord($access_level); 265f0b9c048SGreg Roach }); 266a25f0a04SGreg Roach } 267a25f0a04SGreg Roach 268a25f0a04SGreg Roach /** 269a25f0a04SGreg Roach * Can the name of this record be shown? 270a25f0a04SGreg Roach * 271cbc1590aSGreg Roach * @param int|null $access_level 272a25f0a04SGreg Roach * 273cbc1590aSGreg Roach * @return bool 274a25f0a04SGreg Roach */ 27576f666f4SGreg Roach public function canShowName(int $access_level = null): bool 276c1010edaSGreg Roach { 277a25f0a04SGreg Roach return $this->canShow($access_level); 278a25f0a04SGreg Roach } 279a25f0a04SGreg Roach 280a25f0a04SGreg Roach /** 281a25f0a04SGreg Roach * Can we edit this record? 282a25f0a04SGreg Roach * 283cbc1590aSGreg Roach * @return bool 284a25f0a04SGreg Roach */ 2858f53f488SRico Sonntag public function canEdit(): bool 286c1010edaSGreg Roach { 2871450f098SGreg Roach if ($this->isPendingDeletion()) { 2881450f098SGreg Roach return false; 2891450f098SGreg Roach } 2901450f098SGreg Roach 2911450f098SGreg Roach if (Auth::isManager($this->tree)) { 2921450f098SGreg Roach return true; 2931450f098SGreg Roach } 2941450f098SGreg Roach 295dec352c1SGreg Roach return Auth::isEditor($this->tree) && !str_contains($this->gedcom, "\n1 RESN locked"); 296a25f0a04SGreg Roach } 297a25f0a04SGreg Roach 298a25f0a04SGreg Roach /** 299a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 300a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 301a25f0a04SGreg Roach * 302cbc1590aSGreg Roach * @param int $access_level 303a25f0a04SGreg Roach * 304a25f0a04SGreg Roach * @return string 305a25f0a04SGreg Roach */ 306e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 307c1010edaSGreg Roach { 308e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 309a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 310a25f0a04SGreg Roach return $this->gedcom; 311b2ce94c6SRico Sonntag } 312b2ce94c6SRico Sonntag 313b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 314a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 315a25f0a04SGreg Roach 316a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 317dc141db3SGreg Roach [$gedrec] = explode("\n", $this->gedcom . $this->pending, 2); 318a25f0a04SGreg Roach 319a25f0a04SGreg Roach // Check each of the facts for access 3208d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 321138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 322a25f0a04SGreg Roach } 323cbc1590aSGreg Roach 324a25f0a04SGreg Roach return $gedrec; 325b2ce94c6SRico Sonntag } 326b2ce94c6SRico Sonntag 327a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 328a25f0a04SGreg Roach // limited data, such as links to other records. 329a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 330a25f0a04SGreg Roach } 331a25f0a04SGreg Roach 332a25f0a04SGreg Roach /** 333a25f0a04SGreg Roach * Default for "other" object types 334c7ff4153SGreg Roach * 335c7ff4153SGreg Roach * @return void 336a25f0a04SGreg Roach */ 337e364afe4SGreg Roach public function extractNames(): void 338c1010edaSGreg Roach { 33976f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach 342a25f0a04SGreg Roach /** 343a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 344a25f0a04SGreg Roach * 345870ec5a3SGreg Roach * @return array<int,array<string,string>> 346a25f0a04SGreg Roach */ 3478f53f488SRico Sonntag public function getAllNames(): array 348c1010edaSGreg Roach { 349bdb3725aSGreg Roach if ($this->getAllNames === null) { 350bdb3725aSGreg Roach $this->getAllNames = []; 351a25f0a04SGreg Roach if ($this->canShowName()) { 352a25f0a04SGreg Roach // Ask the record to extract its names 353a25f0a04SGreg Roach $this->extractNames(); 354a25f0a04SGreg Roach // No name found? Use a fallback. 355ec124fd2SGreg Roach if ($this->getAllNames === []) { 356db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 357a25f0a04SGreg Roach } 358a25f0a04SGreg Roach } else { 359db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 360a25f0a04SGreg Roach } 361a25f0a04SGreg Roach } 362cbc1590aSGreg Roach 363bdb3725aSGreg Roach return $this->getAllNames; 364a25f0a04SGreg Roach } 365a25f0a04SGreg Roach 366a25f0a04SGreg Roach /** 367a25f0a04SGreg Roach * If this object has no name, what do we call it? 368a25f0a04SGreg Roach * 369a25f0a04SGreg Roach * @return string 370a25f0a04SGreg Roach */ 3718f53f488SRico Sonntag public function getFallBackName(): string 372c1010edaSGreg Roach { 373c0935879SGreg Roach return e($this->xref()); 374a25f0a04SGreg Roach } 375a25f0a04SGreg Roach 376a25f0a04SGreg Roach /** 377a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 378a25f0a04SGreg Roach * 379cbc1590aSGreg Roach * @return int 380a25f0a04SGreg Roach */ 3818f53f488SRico Sonntag public function getPrimaryName(): int 382c1010edaSGreg Roach { 383a25f0a04SGreg Roach static $language_script; 384a25f0a04SGreg Roach 385448d466cSGreg Roach $language_script ??= I18N::locale()->script()->code(); 386a25f0a04SGreg Roach 387bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 388a25f0a04SGreg Roach // Generally, the first name is the primary one.... 389bdb3725aSGreg Roach $this->getPrimaryName = 0; 390a25f0a04SGreg Roach // ...except when the language/name use different character sets 391a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 39269546be1SGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 393bdb3725aSGreg Roach $this->getPrimaryName = $n; 394a25f0a04SGreg Roach break; 395a25f0a04SGreg Roach } 396a25f0a04SGreg Roach } 397a25f0a04SGreg Roach } 398a25f0a04SGreg Roach 399bdb3725aSGreg Roach return $this->getPrimaryName; 400a25f0a04SGreg Roach } 401a25f0a04SGreg Roach 402a25f0a04SGreg Roach /** 403a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 404a25f0a04SGreg Roach * 405cbc1590aSGreg Roach * @return int 406a25f0a04SGreg Roach */ 4078f53f488SRico Sonntag public function getSecondaryName(): int 408c1010edaSGreg Roach { 4098f038c36SRico Sonntag if ($this->getSecondaryName === null) { 410a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 411bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 412a25f0a04SGreg Roach // ....except when there are names with different character sets 413a25f0a04SGreg Roach $all_names = $this->getAllNames(); 414a25f0a04SGreg Roach if (count($all_names) > 1) { 415a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 416a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 417e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 418bdb3725aSGreg Roach $this->getSecondaryName = $n; 419a25f0a04SGreg Roach break; 420a25f0a04SGreg Roach } 421a25f0a04SGreg Roach } 422a25f0a04SGreg Roach } 423a25f0a04SGreg Roach } 424cbc1590aSGreg Roach 425bdb3725aSGreg Roach return $this->getSecondaryName; 426a25f0a04SGreg Roach } 427a25f0a04SGreg Roach 428a25f0a04SGreg Roach /** 429a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 430a25f0a04SGreg Roach * 43176f666f4SGreg Roach * @param int|null $n 4327e96c925SGreg Roach * 4337e96c925SGreg Roach * @return void 434a25f0a04SGreg Roach */ 435e364afe4SGreg Roach public function setPrimaryName(int $n = null): void 436c1010edaSGreg Roach { 437bdb3725aSGreg Roach $this->getPrimaryName = $n; 438bdb3725aSGreg Roach $this->getSecondaryName = null; 439a25f0a04SGreg Roach } 440a25f0a04SGreg Roach 441a25f0a04SGreg Roach /** 442a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 443a25f0a04SGreg Roach * 444a25f0a04SGreg Roach * @return string 445a25f0a04SGreg Roach */ 44624f2a3afSGreg Roach public function __toString(): string 447c1010edaSGreg Roach { 44872cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 449a25f0a04SGreg Roach } 450a25f0a04SGreg Roach 451a25f0a04SGreg Roach /** 452c156e8f5SGreg Roach * /** 453a25f0a04SGreg Roach * Get variants of the name 454a25f0a04SGreg Roach * 455a25f0a04SGreg Roach * @return string 456a25f0a04SGreg Roach */ 457e364afe4SGreg Roach public function fullName(): string 458c1010edaSGreg Roach { 459a25f0a04SGreg Roach if ($this->canShowName()) { 460a25f0a04SGreg Roach $tmp = $this->getAllNames(); 461cbc1590aSGreg Roach 462a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 463a25f0a04SGreg Roach } 464b2ce94c6SRico Sonntag 465b2ce94c6SRico Sonntag return I18N::translate('Private'); 466a25f0a04SGreg Roach } 467a25f0a04SGreg Roach 468a25f0a04SGreg Roach /** 469a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 470a25f0a04SGreg Roach * 471a25f0a04SGreg Roach * @return string 472a25f0a04SGreg Roach */ 47339ca88baSGreg Roach public function sortName(): string 474c1010edaSGreg Roach { 475a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 476a25f0a04SGreg Roach $tmp = $this->getAllNames(); 477cbc1590aSGreg Roach 478a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 479a25f0a04SGreg Roach } 480a25f0a04SGreg Roach 481a25f0a04SGreg Roach /** 482a25f0a04SGreg Roach * Get the full name in an alternative character set 483a25f0a04SGreg Roach * 484e364afe4SGreg Roach * @return string|null 485a25f0a04SGreg Roach */ 486e364afe4SGreg Roach public function alternateName(): ?string 487c1010edaSGreg Roach { 488e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 489a25f0a04SGreg Roach $all_names = $this->getAllNames(); 490cbc1590aSGreg Roach 491a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 492a25f0a04SGreg Roach } 493b2ce94c6SRico Sonntag 494b2ce94c6SRico Sonntag return null; 495a25f0a04SGreg Roach } 496a25f0a04SGreg Roach 497a25f0a04SGreg Roach /** 498a25f0a04SGreg Roach * Format this object for display in a list 499a25f0a04SGreg Roach * 500a25f0a04SGreg Roach * @return string 501a25f0a04SGreg Roach */ 5028f53f488SRico Sonntag public function formatList(): string 503c1010edaSGreg Roach { 504b165e17cSGreg Roach $html = '<a href="' . e($this->url()) . '" class="list_item">'; 50539ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 506a25f0a04SGreg Roach $html .= $this->formatListDetails(); 507b165e17cSGreg Roach $html .= '</a>'; 508cbc1590aSGreg Roach 509a25f0a04SGreg Roach return $html; 510a25f0a04SGreg Roach } 511a25f0a04SGreg Roach 512a25f0a04SGreg Roach /** 513a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 514a25f0a04SGreg Roach * identifying characteristics of this record. 515a25f0a04SGreg Roach * 516a25f0a04SGreg Roach * @return string 517a25f0a04SGreg Roach */ 5188f53f488SRico Sonntag public function formatListDetails(): string 519c1010edaSGreg Roach { 520a25f0a04SGreg Roach return ''; 521a25f0a04SGreg Roach } 522a25f0a04SGreg Roach 523a25f0a04SGreg Roach /** 524a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 525a25f0a04SGreg Roach * 5268d0ebef0SGreg Roach * @param string[] $facts 527cbc1590aSGreg Roach * @param int $style 528a25f0a04SGreg Roach * 529a25f0a04SGreg Roach * @return string 530a25f0a04SGreg Roach */ 5318d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 532c1010edaSGreg Roach { 53330158ae7SGreg Roach foreach ($this->facts($facts, true) as $event) { 534a25f0a04SGreg Roach // Only display if it has a date or place (or both) 535e364afe4SGreg Roach if ($event->date()->isOK() && $event->place()->gedcomName() !== '') { 536d93f11b5SGreg Roach $joiner = ' — '; 537d93f11b5SGreg Roach } else { 538d93f11b5SGreg Roach $joiner = ''; 539d93f11b5SGreg Roach } 540e364afe4SGreg Roach if ($event->date()->isOK() || $event->place()->gedcomName() !== '') { 541a25f0a04SGreg Roach switch ($style) { 542a25f0a04SGreg Roach case 1: 5437b7d8067SGreg Roach return '<br><em>' . $event->label() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>'; 544a25f0a04SGreg Roach case 2: 5457b7d8067SGreg Roach return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>'; 546a25f0a04SGreg Roach } 547a25f0a04SGreg Roach } 548a25f0a04SGreg Roach } 549cbc1590aSGreg Roach 550a25f0a04SGreg Roach return ''; 551a25f0a04SGreg Roach } 552a25f0a04SGreg Roach 553a25f0a04SGreg Roach /** 554a25f0a04SGreg Roach * Find individuals linked to this record. 555a25f0a04SGreg Roach * 556a25f0a04SGreg Roach * @param string $link 557a25f0a04SGreg Roach * 558b5c8fd7eSGreg Roach * @return Collection<Individual> 559a25f0a04SGreg Roach */ 560907c1109SGreg Roach public function linkedIndividuals(string $link): Collection 561c1010edaSGreg Roach { 562907c1109SGreg Roach return DB::table('individuals') 5630b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 564907c1109SGreg Roach $join 565907c1109SGreg Roach ->on('l_file', '=', 'i_file') 566907c1109SGreg Roach ->on('l_from', '=', 'i_id'); 567ba1c12e8SGreg Roach }) 568ba1c12e8SGreg Roach ->where('i_file', '=', $this->tree->id()) 569ba1c12e8SGreg Roach ->where('l_type', '=', $link) 570ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 571907c1109SGreg Roach ->select(['individuals.*']) 572907c1109SGreg Roach ->get() 5736b9cb339SGreg Roach ->map(Registry::individualFactory()->mapper($this->tree)) 574907c1109SGreg Roach ->filter(self::accessFilter()); 575a25f0a04SGreg Roach } 576a25f0a04SGreg Roach 577a25f0a04SGreg Roach /** 578a25f0a04SGreg Roach * Find families linked to this record. 579a25f0a04SGreg Roach * 580a25f0a04SGreg Roach * @param string $link 581a25f0a04SGreg Roach * 582b5c8fd7eSGreg Roach * @return Collection<Family> 583a25f0a04SGreg Roach */ 584907c1109SGreg Roach public function linkedFamilies(string $link): Collection 585c1010edaSGreg Roach { 586907c1109SGreg Roach return DB::table('families') 5870b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 588907c1109SGreg Roach $join 589907c1109SGreg Roach ->on('l_file', '=', 'f_file') 590907c1109SGreg Roach ->on('l_from', '=', 'f_id'); 591ba1c12e8SGreg Roach }) 592ba1c12e8SGreg Roach ->where('f_file', '=', $this->tree->id()) 593ba1c12e8SGreg Roach ->where('l_type', '=', $link) 594ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 595907c1109SGreg Roach ->select(['families.*']) 596907c1109SGreg Roach ->get() 5976b9cb339SGreg Roach ->map(Registry::familyFactory()->mapper($this->tree)) 598907c1109SGreg Roach ->filter(self::accessFilter()); 599a25f0a04SGreg Roach } 600a25f0a04SGreg Roach 601a25f0a04SGreg Roach /** 602a25f0a04SGreg Roach * Find sources linked to this record. 603a25f0a04SGreg Roach * 604a25f0a04SGreg Roach * @param string $link 605a25f0a04SGreg Roach * 606b5c8fd7eSGreg Roach * @return Collection<Source> 607a25f0a04SGreg Roach */ 608907c1109SGreg Roach public function linkedSources(string $link): Collection 609c1010edaSGreg Roach { 610907c1109SGreg Roach return DB::table('sources') 6110b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 612907c1109SGreg Roach $join 613907c1109SGreg Roach ->on('l_file', '=', 's_file') 614907c1109SGreg Roach ->on('l_from', '=', 's_id'); 615ba1c12e8SGreg Roach }) 616ba1c12e8SGreg Roach ->where('s_file', '=', $this->tree->id()) 617ba1c12e8SGreg Roach ->where('l_type', '=', $link) 618ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 619907c1109SGreg Roach ->select(['sources.*']) 620907c1109SGreg Roach ->get() 6216b9cb339SGreg Roach ->map(Registry::sourceFactory()->mapper($this->tree)) 622907c1109SGreg Roach ->filter(self::accessFilter()); 623a25f0a04SGreg Roach } 624a25f0a04SGreg Roach 625a25f0a04SGreg Roach /** 626a25f0a04SGreg Roach * Find media objects linked to this record. 627a25f0a04SGreg Roach * 628a25f0a04SGreg Roach * @param string $link 629a25f0a04SGreg Roach * 630b5c8fd7eSGreg Roach * @return Collection<Media> 631a25f0a04SGreg Roach */ 632907c1109SGreg Roach public function linkedMedia(string $link): Collection 633c1010edaSGreg Roach { 634907c1109SGreg Roach return DB::table('media') 6350b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 636907c1109SGreg Roach $join 637907c1109SGreg Roach ->on('l_file', '=', 'm_file') 638907c1109SGreg Roach ->on('l_from', '=', 'm_id'); 639ba1c12e8SGreg Roach }) 640ba1c12e8SGreg Roach ->where('m_file', '=', $this->tree->id()) 641ba1c12e8SGreg Roach ->where('l_type', '=', $link) 642ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 643907c1109SGreg Roach ->select(['media.*']) 644907c1109SGreg Roach ->get() 6456b9cb339SGreg Roach ->map(Registry::mediaFactory()->mapper($this->tree)) 646907c1109SGreg Roach ->filter(self::accessFilter()); 647a25f0a04SGreg Roach } 648a25f0a04SGreg Roach 649a25f0a04SGreg Roach /** 650a25f0a04SGreg Roach * Find notes linked to this record. 651a25f0a04SGreg Roach * 652a25f0a04SGreg Roach * @param string $link 653a25f0a04SGreg Roach * 654b5c8fd7eSGreg Roach * @return Collection<Note> 655a25f0a04SGreg Roach */ 656907c1109SGreg Roach public function linkedNotes(string $link): Collection 657c1010edaSGreg Roach { 658907c1109SGreg Roach return DB::table('other') 6590b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 660907c1109SGreg Roach $join 661907c1109SGreg Roach ->on('l_file', '=', 'o_file') 662907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 663ba1c12e8SGreg Roach }) 664ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 6657a8cbeebSGreg Roach ->where('o_type', '=', Note::RECORD_TYPE) 666ba1c12e8SGreg Roach ->where('l_type', '=', $link) 667ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 668907c1109SGreg Roach ->select(['other.*']) 669907c1109SGreg Roach ->get() 6706b9cb339SGreg Roach ->map(Registry::noteFactory()->mapper($this->tree)) 671907c1109SGreg Roach ->filter(self::accessFilter()); 672a25f0a04SGreg Roach } 673a25f0a04SGreg Roach 674a25f0a04SGreg Roach /** 675a25f0a04SGreg Roach * Find repositories linked to this record. 676a25f0a04SGreg Roach * 677a25f0a04SGreg Roach * @param string $link 678a25f0a04SGreg Roach * 679b5c8fd7eSGreg Roach * @return Collection<Repository> 680a25f0a04SGreg Roach */ 681907c1109SGreg Roach public function linkedRepositories(string $link): Collection 682c1010edaSGreg Roach { 683907c1109SGreg Roach return DB::table('other') 6840b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 685907c1109SGreg Roach $join 686907c1109SGreg Roach ->on('l_file', '=', 'o_file') 687907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 688ba1c12e8SGreg Roach }) 689ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 6907a8cbeebSGreg Roach ->where('o_type', '=', Repository::RECORD_TYPE) 691ba1c12e8SGreg Roach ->where('l_type', '=', $link) 692ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 693907c1109SGreg Roach ->select(['other.*']) 694907c1109SGreg Roach ->get() 6956b9cb339SGreg Roach ->map(Registry::repositoryFactory()->mapper($this->tree)) 696907c1109SGreg Roach ->filter(self::accessFilter()); 697a25f0a04SGreg Roach } 698a25f0a04SGreg Roach 699a25f0a04SGreg Roach /** 70036254e3dSRichard Cissée * Find locations linked to this record. 70136254e3dSRichard Cissée * 70236254e3dSRichard Cissée * @param string $link 70336254e3dSRichard Cissée * 70436254e3dSRichard Cissée * @return Collection<Location> 70536254e3dSRichard Cissée */ 70636254e3dSRichard Cissée public function linkedLocations(string $link): Collection 70736254e3dSRichard Cissée { 70836254e3dSRichard Cissée return DB::table('other') 70936254e3dSRichard Cissée ->join('link', static function (JoinClause $join): void { 71036254e3dSRichard Cissée $join 71136254e3dSRichard Cissée ->on('l_file', '=', 'o_file') 71236254e3dSRichard Cissée ->on('l_from', '=', 'o_id'); 71336254e3dSRichard Cissée }) 71436254e3dSRichard Cissée ->where('o_file', '=', $this->tree->id()) 7157a8cbeebSGreg Roach ->where('o_type', '=', Location::RECORD_TYPE) 71636254e3dSRichard Cissée ->where('l_type', '=', $link) 71736254e3dSRichard Cissée ->where('l_to', '=', $this->xref) 71836254e3dSRichard Cissée ->select(['other.*']) 71936254e3dSRichard Cissée ->get() 7206b9cb339SGreg Roach ->map(Registry::locationFactory()->mapper($this->tree)) 72136254e3dSRichard Cissée ->filter(self::accessFilter()); 72236254e3dSRichard Cissée } 72336254e3dSRichard Cissée 72436254e3dSRichard Cissée /** 725a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 726a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 727a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 728a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 729a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 730a25f0a04SGreg Roach * 7318d0ebef0SGreg Roach * @param string[] $events 732a25f0a04SGreg Roach * 733a25f0a04SGreg Roach * @return Date[] 734a25f0a04SGreg Roach */ 7358d0ebef0SGreg Roach public function getAllEventDates(array $events): array 736c1010edaSGreg Roach { 73713abd6f3SGreg Roach $dates = []; 7381385b8caSGreg Roach foreach ($this->facts($events, false, null, true) as $event) { 7392decada7SGreg Roach if ($event->date()->isOK()) { 7402decada7SGreg Roach $dates[] = $event->date(); 741a25f0a04SGreg Roach } 742a25f0a04SGreg Roach } 743a25f0a04SGreg Roach 744a25f0a04SGreg Roach return $dates; 745a25f0a04SGreg Roach } 746a25f0a04SGreg Roach 747a25f0a04SGreg Roach /** 748a25f0a04SGreg Roach * Get all the places for a particular type of event 749a25f0a04SGreg Roach * 7508d0ebef0SGreg Roach * @param string[] $events 751a25f0a04SGreg Roach * 7524080d558SGreg Roach * @return Place[] 753a25f0a04SGreg Roach */ 7548d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 755c1010edaSGreg Roach { 75613abd6f3SGreg Roach $places = []; 7578d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 758138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 759a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 76016d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 761a25f0a04SGreg Roach } 762a25f0a04SGreg Roach } 763a25f0a04SGreg Roach } 764a25f0a04SGreg Roach 765a25f0a04SGreg Roach return $places; 766a25f0a04SGreg Roach } 767a25f0a04SGreg Roach 768a25f0a04SGreg Roach /** 769a25f0a04SGreg Roach * The facts and events for this record. 770a25f0a04SGreg Roach * 7718d0ebef0SGreg Roach * @param string[] $filter 772cbc1590aSGreg Roach * @param bool $sort 773cbc1590aSGreg Roach * @param int|null $access_level 774ce42304aSGreg Roach * @param bool $ignore_deleted 775a25f0a04SGreg Roach * 776b5c8fd7eSGreg Roach * @return Collection<Fact> 777a25f0a04SGreg Roach */ 778ce42304aSGreg Roach public function facts( 779ce42304aSGreg Roach array $filter = [], 780ce42304aSGreg Roach bool $sort = false, 781ce42304aSGreg Roach int $access_level = null, 782ce42304aSGreg Roach bool $ignore_deleted = false 783ce42304aSGreg Roach ): Collection { 784d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 7854b9ff166SGreg Roach 786d0889c63SGreg Roach // Convert BIRT into INDI:BIRT, etc. 787d0889c63SGreg Roach $filter = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, $filter); 788d0889c63SGreg Roach 7898af3e5c1SGreg Roach $facts = new Collection(); 790ce42304aSGreg Roach if ($this->canShow($access_level)) { 791a25f0a04SGreg Roach foreach ($this->facts as $fact) { 792d0889c63SGreg Roach if (($filter === [] || in_array($fact->tag(), $filter, true)) && $fact->canShow($access_level)) { 7938af3e5c1SGreg Roach $facts->push($fact); 794a25f0a04SGreg Roach } 795a25f0a04SGreg Roach } 796a25f0a04SGreg Roach } 797d17d7b9eSGreg Roach 798a25f0a04SGreg Roach if ($sort) { 7990f5fd22fSGreg Roach switch ($this->tag()) { 8000f5fd22fSGreg Roach case Family::RECORD_TYPE: 8010f5fd22fSGreg Roach case Individual::RECORD_TYPE: 802580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 8030f5fd22fSGreg Roach break; 8040f5fd22fSGreg Roach 8050f5fd22fSGreg Roach default: 8060f5fd22fSGreg Roach $subtags = Registry::elementFactory()->make($this->tag())->subtags(); 8070f5fd22fSGreg Roach $subtags = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, array_keys($subtags)); 8080f5fd22fSGreg Roach $subtags = array_combine(range(1, count($subtags)), $subtags); 8090f5fd22fSGreg Roach 8100f5fd22fSGreg Roach $facts = $facts 8110f5fd22fSGreg Roach ->sort(static function (Fact $x, Fact $y) use ($subtags): int { 8120f5fd22fSGreg Roach $sort_x = array_search($x->tag(), $subtags, true) ?: PHP_INT_MAX; 8130f5fd22fSGreg Roach $sort_y = array_search($y->tag(), $subtags, true) ?: PHP_INT_MAX; 8140f5fd22fSGreg Roach 8150f5fd22fSGreg Roach return $sort_x <=> $sort_y; 8160f5fd22fSGreg Roach }); 8170f5fd22fSGreg Roach break; 8180f5fd22fSGreg Roach } 819a25f0a04SGreg Roach } 820cbc1590aSGreg Roach 821ce42304aSGreg Roach if ($ignore_deleted) { 822ce42304aSGreg Roach $facts = $facts->filter(static function (Fact $fact): bool { 823ce42304aSGreg Roach return !$fact->isPendingDeletion(); 824ce42304aSGreg Roach }); 825ce42304aSGreg Roach } 826ce42304aSGreg Roach 82739ca88baSGreg Roach return new Collection($facts); 828a25f0a04SGreg Roach } 829a25f0a04SGreg Roach 830a25f0a04SGreg Roach /** 8310f5fd22fSGreg Roach * @return array<string,string> 8320f5fd22fSGreg Roach */ 8330f5fd22fSGreg Roach public function missingFacts(): array 8340f5fd22fSGreg Roach { 8350f5fd22fSGreg Roach $missing_facts = []; 8360f5fd22fSGreg Roach 8370f5fd22fSGreg Roach foreach (Registry::elementFactory()->make($this->tag())->subtags() as $subtag => $repeat) { 8380f5fd22fSGreg Roach [, $max] = explode(':', $repeat); 8390f5fd22fSGreg Roach $max = $max === 'M' ? PHP_INT_MAX : (int) $max; 8400f5fd22fSGreg Roach 84198f93f3aSGreg Roach if ($this->facts([$subtag], false, null, true)->count() < $max) { 8420f5fd22fSGreg Roach $missing_facts[$subtag] = $subtag; 8430f5fd22fSGreg Roach $missing_facts[$subtag] = Registry::elementFactory()->make($this->tag() . ':' . $subtag)->label(); 8440f5fd22fSGreg Roach } 8450f5fd22fSGreg Roach } 8460f5fd22fSGreg Roach 8470f5fd22fSGreg Roach uasort($missing_facts, I18N::comparator()); 8480f5fd22fSGreg Roach 8490f5fd22fSGreg Roach if ($this->tree->getPreference('MEDIA_UPLOAD') < Auth::accessLevel($this->tree)) { 8500f5fd22fSGreg Roach unset($missing_facts['OBJE']); 8510f5fd22fSGreg Roach } 8520f5fd22fSGreg Roach 8530f5fd22fSGreg Roach // We have special code for this. 8540f5fd22fSGreg Roach unset($missing_facts['FILE']); 8550f5fd22fSGreg Roach 8560f5fd22fSGreg Roach return $missing_facts; 8570f5fd22fSGreg Roach } 8580f5fd22fSGreg Roach 8590f5fd22fSGreg Roach /** 8604459dc9aSGreg Roach * Get the last-change timestamp for this record 861a25f0a04SGreg Roach * 8624459dc9aSGreg Roach * @return Carbon 863a25f0a04SGreg Roach */ 8644459dc9aSGreg Roach public function lastChangeTimestamp(): Carbon 865c1010edaSGreg Roach { 8664459dc9aSGreg Roach /** @var Fact|null $chan */ 867820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 868a25f0a04SGreg Roach 8694459dc9aSGreg Roach if ($chan instanceof Fact) { 870a25f0a04SGreg Roach // The record does have a CHAN event 8712decada7SGreg Roach $d = $chan->date()->minimumDate(); 8724459dc9aSGreg Roach 873138ca96cSGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->gedcom(), $match)) { 8744459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2], (int) $match[3]); 875e364afe4SGreg Roach } 876e364afe4SGreg Roach 877e364afe4SGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->gedcom(), $match)) { 8784459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2]); 879b2ce94c6SRico Sonntag } 880b2ce94c6SRico Sonntag 8814459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day()); 882a25f0a04SGreg Roach } 883b2ce94c6SRico Sonntag 884a25f0a04SGreg Roach // The record does not have a CHAN event 8854459dc9aSGreg Roach return Carbon::createFromTimestamp(0); 886a25f0a04SGreg Roach } 887a25f0a04SGreg Roach 888a25f0a04SGreg Roach /** 889a25f0a04SGreg Roach * Get the last-change user for this record 890a25f0a04SGreg Roach * 891a25f0a04SGreg Roach * @return string 892a25f0a04SGreg Roach */ 893e364afe4SGreg Roach public function lastChangeUser(): string 894c1010edaSGreg Roach { 895820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 896a25f0a04SGreg Roach 897a25f0a04SGreg Roach if ($chan === null) { 898a25f0a04SGreg Roach return I18N::translate('Unknown'); 899b2ce94c6SRico Sonntag } 900b2ce94c6SRico Sonntag 9013425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 902baacc364SGreg Roach if ($chan_user === '') { 903a25f0a04SGreg Roach return I18N::translate('Unknown'); 904b2ce94c6SRico Sonntag } 905b2ce94c6SRico Sonntag 906a25f0a04SGreg Roach return $chan_user; 907a25f0a04SGreg Roach } 908a25f0a04SGreg Roach 909a25f0a04SGreg Roach /** 910a25f0a04SGreg Roach * Add a new fact to this record 911a25f0a04SGreg Roach * 912a25f0a04SGreg Roach * @param string $gedcom 913cbc1590aSGreg Roach * @param bool $update_chan 9147e96c925SGreg Roach * 9157e96c925SGreg Roach * @return void 916a25f0a04SGreg Roach */ 917e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 918c1010edaSGreg Roach { 919fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 920a25f0a04SGreg Roach } 921a25f0a04SGreg Roach 922a25f0a04SGreg Roach /** 923a25f0a04SGreg Roach * Delete a fact from this record 924a25f0a04SGreg Roach * 925a25f0a04SGreg Roach * @param string $fact_id 926cbc1590aSGreg Roach * @param bool $update_chan 9277e96c925SGreg Roach * 9287e96c925SGreg Roach * @return void 929a25f0a04SGreg Roach */ 930e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 931c1010edaSGreg Roach { 932db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 933a25f0a04SGreg Roach } 934a25f0a04SGreg Roach 935a25f0a04SGreg Roach /** 936a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 937a25f0a04SGreg Roach * 938a25f0a04SGreg Roach * @param string $fact_id 939a25f0a04SGreg Roach * @param string $gedcom 940cbc1590aSGreg Roach * @param bool $update_chan 941a25f0a04SGreg Roach * 9427e96c925SGreg Roach * @return void 9437e96c925SGreg Roach * @throws Exception 944a25f0a04SGreg Roach */ 945e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 946c1010edaSGreg Roach { 94771f696adSGreg Roach // Not all record types allow a CHAN event. 94871f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 94971f696adSGreg Roach 950a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 951a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 952a25f0a04SGreg Roach $gedcom = trim($gedcom); 953a25f0a04SGreg Roach 954a25f0a04SGreg Roach if ($this->pending === '') { 9557e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 956a25f0a04SGreg Roach } 9578d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 9587e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 959a25f0a04SGreg Roach } 960a25f0a04SGreg Roach 961a25f0a04SGreg Roach if ($this->pending) { 962a25f0a04SGreg Roach $old_gedcom = $this->pending; 963a25f0a04SGreg Roach } else { 964a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 965a25f0a04SGreg Roach } 966a25f0a04SGreg Roach 967a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 96865e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 969a25f0a04SGreg Roach 970a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 97119aed3a1SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE, true) as $fact) { 972905ab80aSGreg Roach if ($fact->id() === $fact_id) { 973db7bb364SGreg Roach if ($gedcom !== '') { 974a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 975a25f0a04SGreg Roach } 976fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 977d0889c63SGreg Roach } elseif (!str_ends_with($fact->tag(), ':CHAN') || !$update_chan) { 978138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 979a25f0a04SGreg Roach } 980a25f0a04SGreg Roach } 981a25f0a04SGreg Roach 982a25f0a04SGreg Roach // Adding a new fact 983fc3ccce4SGreg Roach if ($fact_id === '') { 984a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 985a25f0a04SGreg Roach } 986a25f0a04SGreg Roach 987dec352c1SGreg Roach if ($update_chan && !str_contains($new_gedcom, "\n1 CHAN")) { 98819aed3a1SGreg Roach $today = strtoupper(date('d M Y')); 98919aed3a1SGreg Roach $now = date('H:i:s'); 99019aed3a1SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName(); 99119aed3a1SGreg Roach } 99219aed3a1SGreg Roach 993e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 994a25f0a04SGreg Roach // Save the changes 995d09b6323SGreg Roach DB::table('change')->insert([ 996d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 997d09b6323SGreg Roach 'xref' => $this->xref, 998d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 999d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 1000d09b6323SGreg Roach 'user_id' => Auth::id(), 100113abd6f3SGreg Roach ]); 1002a25f0a04SGreg Roach 1003a25f0a04SGreg Roach $this->pending = $new_gedcom; 1004a25f0a04SGreg Roach 10051fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 100622e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1007a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 1008a25f0a04SGreg Roach $this->pending = null; 1009a25f0a04SGreg Roach } 1010a25f0a04SGreg Roach } 1011a25f0a04SGreg Roach $this->parseFacts(); 1012a25f0a04SGreg Roach } 1013a25f0a04SGreg Roach 1014a25f0a04SGreg Roach /** 1015a25f0a04SGreg Roach * Update this record 1016a25f0a04SGreg Roach * 1017a25f0a04SGreg Roach * @param string $gedcom 1018cbc1590aSGreg Roach * @param bool $update_chan 10197e96c925SGreg Roach * 10207e96c925SGreg Roach * @return void 1021a25f0a04SGreg Roach */ 1022e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 1023c1010edaSGreg Roach { 102471f696adSGreg Roach // Not all record types allow a CHAN event. 102571f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 102671f696adSGreg Roach 1027a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1028a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1029a25f0a04SGreg Roach $gedcom = trim($gedcom); 1030a25f0a04SGreg Roach 1031a25f0a04SGreg Roach // Update the CHAN record 1032a25f0a04SGreg Roach if ($update_chan) { 1033a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 103453432476SGreg Roach $today = strtoupper(date('d M Y')); 103553432476SGreg Roach $now = date('H:i:s'); 103653432476SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName(); 1037a25f0a04SGreg Roach } 1038a25f0a04SGreg Roach 1039a25f0a04SGreg Roach // Create a pending change 1040d09b6323SGreg Roach DB::table('change')->insert([ 1041d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1042d09b6323SGreg Roach 'xref' => $this->xref, 1043d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1044d09b6323SGreg Roach 'new_gedcom' => $gedcom, 1045d09b6323SGreg Roach 'user_id' => Auth::id(), 104613abd6f3SGreg Roach ]); 1047a25f0a04SGreg Roach 1048a25f0a04SGreg Roach // Clear the cache 1049a25f0a04SGreg Roach $this->pending = $gedcom; 1050a25f0a04SGreg Roach 1051a25f0a04SGreg Roach // Accept this pending change 10521fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 105322e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1054a25f0a04SGreg Roach $this->gedcom = $gedcom; 1055a25f0a04SGreg Roach $this->pending = null; 1056a25f0a04SGreg Roach } 1057a25f0a04SGreg Roach 1058a25f0a04SGreg Roach $this->parseFacts(); 1059a25f0a04SGreg Roach 1060847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1061a25f0a04SGreg Roach } 1062a25f0a04SGreg Roach 1063a25f0a04SGreg Roach /** 1064a25f0a04SGreg Roach * Delete this record 1065b874da82SGreg Roach * 1066b874da82SGreg Roach * @return void 1067a25f0a04SGreg Roach */ 1068e364afe4SGreg Roach public function deleteRecord(): void 1069c1010edaSGreg Roach { 1070a25f0a04SGreg Roach // Create a pending change 10714c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 1072d09b6323SGreg Roach DB::table('change')->insert([ 1073d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1074d09b6323SGreg Roach 'xref' => $this->xref, 1075d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1076d09b6323SGreg Roach 'new_gedcom' => '', 1077d09b6323SGreg Roach 'user_id' => Auth::id(), 107813abd6f3SGreg Roach ]); 10794c0b5256SGreg Roach } 1080a25f0a04SGreg Roach 10814c0b5256SGreg Roach // Auto-accept this pending change 10821fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 108322e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1084a25f0a04SGreg Roach } 1085a25f0a04SGreg Roach 1086847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1087a25f0a04SGreg Roach } 1088a25f0a04SGreg Roach 1089a25f0a04SGreg Roach /** 1090a25f0a04SGreg Roach * Remove all links from this record to $xref 1091a25f0a04SGreg Roach * 1092a25f0a04SGreg Roach * @param string $xref 1093cbc1590aSGreg Roach * @param bool $update_chan 10947e96c925SGreg Roach * 10957e96c925SGreg Roach * @return void 1096a25f0a04SGreg Roach */ 1097e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 1098c1010edaSGreg Roach { 1099a25f0a04SGreg Roach $value = '@' . $xref . '@'; 1100a25f0a04SGreg Roach 110130158ae7SGreg Roach foreach ($this->facts() as $fact) { 110284586c02SGreg Roach if ($fact->value() === $value) { 1103905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 11048d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 1105138ca96cSGreg Roach $gedcom = $fact->gedcom(); 1106a25f0a04SGreg Roach foreach ($matches as $match) { 1107a25f0a04SGreg Roach $next_level = $match[1] + 1; 1108a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 1109a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 1110a25f0a04SGreg Roach } 1111905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 1112a25f0a04SGreg Roach } 1113a25f0a04SGreg Roach } 1114a25f0a04SGreg Roach } 1115bf4eb542SGreg Roach 1116bf4eb542SGreg Roach /** 1117bf4eb542SGreg Roach * Fetch XREFs of all records linked to a record - when deleting an object, we must 1118bf4eb542SGreg Roach * also delete all links to it. 1119bf4eb542SGreg Roach * 1120bf4eb542SGreg Roach * @return GedcomRecord[] 1121bf4eb542SGreg Roach */ 1122bf4eb542SGreg Roach public function linkingRecords(): array 1123bf4eb542SGreg Roach { 1124b5961194SGreg Roach $like = addcslashes($this->xref(), '\\%_'); 1125b5961194SGreg Roach 1126bf4eb542SGreg Roach $union = DB::table('change') 1127bf4eb542SGreg Roach ->where('gedcom_id', '=', $this->tree()->id()) 1128b5961194SGreg Roach ->where('new_gedcom', 'LIKE', '%@' . $like . '@%') 1129b5961194SGreg Roach ->where('new_gedcom', 'NOT LIKE', '0 @' . $like . '@%') 113083242252SGreg Roach ->whereIn('change_id', function (Builder $query): void { 113183242252SGreg Roach $query->select(new Expression('MAX(change_id)')) 113283242252SGreg Roach ->from('change') 113383242252SGreg Roach ->where('gedcom_id', '=', $this->tree->id()) 113483242252SGreg Roach ->where('status', '=', 'pending') 11357f5c2944SGreg Roach ->groupBy(['xref']); 113683242252SGreg Roach }) 1137bf4eb542SGreg Roach ->select(['xref']); 1138bf4eb542SGreg Roach 1139bf4eb542SGreg Roach $xrefs = DB::table('link') 1140bf4eb542SGreg Roach ->where('l_file', '=', $this->tree()->id()) 1141bf4eb542SGreg Roach ->where('l_to', '=', $this->xref()) 114247256fc5SGreg Roach ->select(['l_from']) 1143bf4eb542SGreg Roach ->union($union) 1144bf4eb542SGreg Roach ->pluck('l_from'); 1145bf4eb542SGreg Roach 1146bf4eb542SGreg Roach return $xrefs->map(function (string $xref): GedcomRecord { 11476b9cb339SGreg Roach $record = Registry::gedcomRecordFactory()->make($xref, $this->tree); 1148ffc0a61fSGreg Roach assert($record instanceof GedcomRecord); 1149ffc0a61fSGreg Roach 1150ffc0a61fSGreg Roach return $record; 1151bf4eb542SGreg Roach })->all(); 1152bf4eb542SGreg Roach } 115322e73debSGreg Roach 115422e73debSGreg Roach /** 115522e73debSGreg Roach * Each object type may have its own special rules, and re-implement this function. 115622e73debSGreg Roach * 115722e73debSGreg Roach * @param int $access_level 115822e73debSGreg Roach * 115922e73debSGreg Roach * @return bool 116022e73debSGreg Roach */ 116122e73debSGreg Roach protected function canShowByType(int $access_level): bool 116222e73debSGreg Roach { 116322e73debSGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 116422e73debSGreg Roach 116522e73debSGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 116622e73debSGreg Roach // Restriction found 116722e73debSGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 116822e73debSGreg Roach } 116922e73debSGreg Roach 117022e73debSGreg Roach // No restriction found - must be public: 117122e73debSGreg Roach return true; 117222e73debSGreg Roach } 117322e73debSGreg Roach 117422e73debSGreg Roach /** 117522e73debSGreg Roach * Generate a private version of this record 117622e73debSGreg Roach * 117722e73debSGreg Roach * @param int $access_level 117822e73debSGreg Roach * 117922e73debSGreg Roach * @return string 118022e73debSGreg Roach */ 118122e73debSGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 118222e73debSGreg Roach { 1183228ede81SGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE; 118422e73debSGreg Roach } 118522e73debSGreg Roach 118622e73debSGreg Roach /** 118722e73debSGreg Roach * Convert a name record into sortable and full/display versions. This default 118822e73debSGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 118922e73debSGreg Roach * 119022e73debSGreg Roach * @param string $type 119122e73debSGreg Roach * @param string $value 119222e73debSGreg Roach * @param string $gedcom 119322e73debSGreg Roach * 119422e73debSGreg Roach * @return void 119522e73debSGreg Roach */ 119622e73debSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 119722e73debSGreg Roach { 119822e73debSGreg Roach $this->getAllNames[] = [ 119922e73debSGreg Roach 'type' => $type, 1200dbf91548SGreg Roach 'sort' => preg_replace_callback('/(\d+)/', static function (array $matches): string { 120122e73debSGreg Roach return str_pad($matches[0], 10, '0', STR_PAD_LEFT); 120222e73debSGreg Roach }, $value), 1203*315eb316SGreg Roach 'full' => '<bdi>' . e($value) . '</bdi>', 120422e73debSGreg Roach // This is used for display 120522e73debSGreg Roach 'fullNN' => $value, 120622e73debSGreg Roach // This goes into the database 120722e73debSGreg Roach ]; 120822e73debSGreg Roach } 120922e73debSGreg Roach 121022e73debSGreg Roach /** 121122e73debSGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 121222e73debSGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 121322e73debSGreg Roach * Parameters: the level 1 fact containing the name. 121422e73debSGreg Roach * Return value: an array of name structures, each containing 121522e73debSGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 121622e73debSGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 121722e73debSGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 121822e73debSGreg Roach * 121922e73debSGreg Roach * @param int $level 122022e73debSGreg Roach * @param string $fact_type 1221d7634abbSGreg Roach * @param Collection<Fact> $facts 122222e73debSGreg Roach * 122322e73debSGreg Roach * @return void 122422e73debSGreg Roach */ 122522e73debSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 122622e73debSGreg Roach { 122722e73debSGreg Roach $sublevel = $level + 1; 122822e73debSGreg Roach $subsublevel = $sublevel + 1; 122922e73debSGreg Roach foreach ($facts as $fact) { 123022e73debSGreg Roach if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) { 123122e73debSGreg Roach foreach ($matches as $match) { 123222e73debSGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 1233dec352c1SGreg Roach if ($match[1] === 'NAME' && str_contains($match[3], "\n2 TYPE married")) { 123422e73debSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 123522e73debSGreg Roach } else { 123622e73debSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 123722e73debSGreg Roach } 123822e73debSGreg Roach if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { 123922e73debSGreg Roach foreach ($submatches as $submatch) { 124022e73debSGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 124122e73debSGreg Roach } 124222e73debSGreg Roach } 124322e73debSGreg Roach } 124422e73debSGreg Roach } 124522e73debSGreg Roach } 124622e73debSGreg Roach } 124722e73debSGreg Roach 124822e73debSGreg Roach /** 124922e73debSGreg Roach * Split the record into facts 125022e73debSGreg Roach * 125122e73debSGreg Roach * @return void 125222e73debSGreg Roach */ 125322e73debSGreg Roach private function parseFacts(): void 125422e73debSGreg Roach { 125522e73debSGreg Roach // Split the record into facts 125622e73debSGreg Roach if ($this->gedcom) { 1257d823340dSGreg Roach $gedcom_facts = preg_split('/\n(?=1)/', $this->gedcom); 125822e73debSGreg Roach array_shift($gedcom_facts); 125922e73debSGreg Roach } else { 126022e73debSGreg Roach $gedcom_facts = []; 126122e73debSGreg Roach } 126222e73debSGreg Roach if ($this->pending) { 1263d823340dSGreg Roach $pending_facts = preg_split('/\n(?=1)/', $this->pending); 126422e73debSGreg Roach array_shift($pending_facts); 126522e73debSGreg Roach } else { 126622e73debSGreg Roach $pending_facts = []; 126722e73debSGreg Roach } 126822e73debSGreg Roach 126922e73debSGreg Roach $this->facts = []; 127022e73debSGreg Roach 127122e73debSGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 127222e73debSGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 127322e73debSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 127422e73debSGreg Roach $fact->setPendingDeletion(); 127522e73debSGreg Roach } 127622e73debSGreg Roach $this->facts[] = $fact; 127722e73debSGreg Roach } 127822e73debSGreg Roach foreach ($pending_facts as $pending_fact) { 127922e73debSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 128022e73debSGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 128122e73debSGreg Roach $fact->setPendingAddition(); 128222e73debSGreg Roach $this->facts[] = $fact; 128322e73debSGreg Roach } 128422e73debSGreg Roach } 128522e73debSGreg Roach } 128622e73debSGreg Roach 128722e73debSGreg Roach /** 128822e73debSGreg Roach * Work out whether this record can be shown to a user with a given access level 128922e73debSGreg Roach * 129022e73debSGreg Roach * @param int $access_level 129122e73debSGreg Roach * 129222e73debSGreg Roach * @return bool 129322e73debSGreg Roach */ 129422e73debSGreg Roach private function canShowRecord(int $access_level): bool 129522e73debSGreg Roach { 129622e73debSGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 129722e73debSGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 129822e73debSGreg Roach return true; 129922e73debSGreg Roach } 130022e73debSGreg Roach 130122e73debSGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 13021fe542e9SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) { 130322e73debSGreg Roach return true; 130422e73debSGreg Roach } 130522e73debSGreg Roach 130622e73debSGreg Roach // Does this record have a RESN? 1307dec352c1SGreg Roach if (str_contains($this->gedcom, "\n1 RESN confidential")) { 130822e73debSGreg Roach return Auth::PRIV_NONE >= $access_level; 130922e73debSGreg Roach } 1310dec352c1SGreg Roach if (str_contains($this->gedcom, "\n1 RESN privacy")) { 131122e73debSGreg Roach return Auth::PRIV_USER >= $access_level; 131222e73debSGreg Roach } 1313dec352c1SGreg Roach if (str_contains($this->gedcom, "\n1 RESN none")) { 131422e73debSGreg Roach return true; 131522e73debSGreg Roach } 131622e73debSGreg Roach 131722e73debSGreg Roach // Does this record have a default RESN? 131822e73debSGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 131922e73debSGreg Roach if (isset($individual_privacy[$this->xref()])) { 132022e73debSGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 132122e73debSGreg Roach } 132222e73debSGreg Roach 132322e73debSGreg Roach // Privacy rules do not apply to admins 132422e73debSGreg Roach if (Auth::PRIV_NONE >= $access_level) { 132522e73debSGreg Roach return true; 132622e73debSGreg Roach } 132722e73debSGreg Roach 132822e73debSGreg Roach // Different types of record have different privacy rules 132922e73debSGreg Roach return $this->canShowByType($access_level); 133022e73debSGreg Roach } 13318091bfd1SGreg Roach 13328091bfd1SGreg Roach /** 13338091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 13348091bfd1SGreg Roach */ 13358091bfd1SGreg Roach public function lock(): void 13368091bfd1SGreg Roach { 13378091bfd1SGreg Roach DB::table('other') 13388091bfd1SGreg Roach ->where('o_file', '=', $this->tree->id()) 13398091bfd1SGreg Roach ->where('o_id', '=', $this->xref()) 13408091bfd1SGreg Roach ->lockForUpdate() 13418091bfd1SGreg Roach ->get(); 13428091bfd1SGreg Roach } 1343a25f0a04SGreg Roach} 1344