1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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; 2476d39c55SGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampInterface; 251fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 2655105892SGreg Roachuse Fisharebest\Webtrees\Elements\RestrictionNotice; 275818a371SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomRecordPage; 2822e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService; 2939ca88baSGreg Roachuse Illuminate\Support\Collection; 30a25f0a04SGreg Roach 310f5fd22fSGreg Roachuse function array_combine; 320f5fd22fSGreg Roachuse function array_keys; 330f5fd22fSGreg Roachuse function array_map; 340f5fd22fSGreg Roachuse function array_search; 35f7440925SGreg Roachuse function array_shift; 36f7440925SGreg Roachuse function count; 37f7440925SGreg Roachuse function date; 38f7440925SGreg Roachuse function e; 39f7440925SGreg Roachuse function explode; 40a79e52aaSGreg Roachuse function implode; 41f7440925SGreg Roachuse function in_array; 42f7440925SGreg Roachuse function md5; 43f7440925SGreg Roachuse function preg_match; 44f7440925SGreg Roachuse function preg_match_all; 45f7440925SGreg Roachuse function preg_replace; 46f7440925SGreg Roachuse function preg_replace_callback; 47f7440925SGreg Roachuse function preg_split; 480f5fd22fSGreg Roachuse function range; 49f7440925SGreg Roachuse function route; 50dec352c1SGreg Roachuse function str_contains; 51d0889c63SGreg Roachuse function str_ends_with; 52f7440925SGreg Roachuse function str_pad; 53*6830c5c1SGreg Roachuse function str_replace; 5455105892SGreg Roachuse function str_starts_with; 55f7440925SGreg Roachuse function strtoupper; 5672f8afdbSGreg Roachuse function strtr; 57f7440925SGreg Roachuse function trim; 58b315f3e1SGreg Roachuse function view; 59b315f3e1SGreg Roach 60c2ed51d1SGreg Roachuse const PHP_INT_MAX; 61f7440925SGreg Roachuse const PREG_SET_ORDER; 62f7440925SGreg Roachuse const STR_PAD_LEFT; 6322e73debSGreg Roach 64a25f0a04SGreg Roach/** 6576692c8bSGreg Roach * A GEDCOM object. 66a25f0a04SGreg Roach */ 67c1010edaSGreg Roachclass GedcomRecord 68c1010edaSGreg Roach{ 6916d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 7016d6367aSGreg Roach 715818a371SGreg Roach protected const ROUTE_NAME = GedcomRecordPage::class; 725818a371SGreg Roach 739599771eSGreg Roach protected string $xref; 74bb03c9f0SGreg Roach 759599771eSGreg Roach protected Tree $tree; 76bb03c9f0SGreg Roach 779599771eSGreg Roach // GEDCOM data (before any pending edits) 789599771eSGreg Roach protected string $gedcom; 79bb03c9f0SGreg Roach 809599771eSGreg Roach // GEDCOM data (after any pending edits) 811ff45046SGreg Roach protected string|null $pending; 82bb03c9f0SGreg Roach 839599771eSGreg Roach /** @var array<Fact> Facts extracted from $gedcom/$pending */ 849599771eSGreg Roach protected array $facts; 85bb03c9f0SGreg Roach 8609482a55SGreg Roach /** @var array<array<string>> All the names of this individual */ 879599771eSGreg Roach protected array $getAllNames = []; 88bb03c9f0SGreg Roach 8922e73debSGreg Roach /** @var int|null Cached result */ 901ff45046SGreg Roach private int|null $getPrimaryName = null; 91c4943cffSGreg Roach 9222e73debSGreg Roach /** @var int|null Cached result */ 931ff45046SGreg Roach private int|null $getSecondaryName = null; 94a25f0a04SGreg Roach 95a25f0a04SGreg Roach /** 96a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 97a25f0a04SGreg Roach * 98a25f0a04SGreg Roach * @param string $xref 99a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 100a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 101a25f0a04SGreg Roach * empty string for records with pending deletions 10224ec66ceSGreg Roach * @param Tree $tree 103a25f0a04SGreg Roach */ 1041ff45046SGreg Roach public function __construct(string $xref, string $gedcom, string|null $pending, Tree $tree) 105c1010edaSGreg Roach { 106a25f0a04SGreg Roach $this->xref = $xref; 107a25f0a04SGreg Roach $this->gedcom = $gedcom; 108a25f0a04SGreg Roach $this->pending = $pending; 10924ec66ceSGreg Roach $this->tree = $tree; 1109599771eSGreg Roach $this->facts = $this->parseFacts(); 111a25f0a04SGreg Roach } 112a25f0a04SGreg Roach 113a25f0a04SGreg Roach /** 114886b77daSGreg Roach * A closure which will filter out private records. 115886b77daSGreg Roach * 116c6921a17SGreg Roach * @return Closure(GedcomRecord):bool 117886b77daSGreg Roach */ 1184146fabcSGreg Roach public static function accessFilter(): Closure 119886b77daSGreg Roach { 120f25fc0f9SGreg Roach return static fn (GedcomRecord $record): bool => $record->canShow(); 121886b77daSGreg Roach } 122886b77daSGreg Roach 123886b77daSGreg Roach /** 124c156e8f5SGreg Roach * A closure which will compare records by name. 125c156e8f5SGreg Roach * 126c6921a17SGreg Roach * @return Closure(GedcomRecord,GedcomRecord):int 127c156e8f5SGreg Roach */ 128c156e8f5SGreg Roach public static function nameComparator(): Closure 129c156e8f5SGreg Roach { 1306c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y): int { 131c156e8f5SGreg Roach if ($x->canShowName()) { 132c156e8f5SGreg Roach if ($y->canShowName()) { 13337646143SGreg Roach return I18N::comparator()($x->sortName(), $y->sortName()); 134c156e8f5SGreg Roach } 135c156e8f5SGreg Roach 136c156e8f5SGreg Roach return -1; // only $y is private 137c156e8f5SGreg Roach } 138c156e8f5SGreg Roach 139c156e8f5SGreg Roach if ($y->canShowName()) { 140c156e8f5SGreg Roach return 1; // only $x is private 141c156e8f5SGreg Roach } 142c156e8f5SGreg Roach 143c156e8f5SGreg Roach return 0; // both $x and $y private 144c156e8f5SGreg Roach }; 145c156e8f5SGreg Roach } 146c156e8f5SGreg Roach 147c156e8f5SGreg Roach /** 148c156e8f5SGreg Roach * A closure which will compare records by change time. 149c156e8f5SGreg Roach * 150c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 151c156e8f5SGreg Roach * 152c6921a17SGreg Roach * @return Closure(GedcomRecord,GedcomRecord):int 153c156e8f5SGreg Roach */ 154c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 155c156e8f5SGreg Roach { 156f25fc0f9SGreg Roach return static fn (GedcomRecord $x, GedcomRecord $y): int => $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp()); 157c156e8f5SGreg Roach } 158c156e8f5SGreg Roach 159c156e8f5SGreg Roach /** 16002467d32SGreg Roach * Get the GEDCOM tag for this record. 16102467d32SGreg Roach * 16202467d32SGreg Roach * @return string 16302467d32SGreg Roach */ 16402467d32SGreg Roach public function tag(): string 16502467d32SGreg Roach { 16602467d32SGreg Roach preg_match('/^0 @[^@]*@ (\w+)/', $this->gedcom(), $match); 16702467d32SGreg Roach 16802467d32SGreg Roach return $match[1] ?? static::RECORD_TYPE; 16902467d32SGreg Roach } 17002467d32SGreg Roach 17102467d32SGreg Roach /** 172a25f0a04SGreg Roach * Get the XREF for this record 173a25f0a04SGreg Roach * 174a25f0a04SGreg Roach * @return string 175a25f0a04SGreg Roach */ 176c0935879SGreg Roach public function xref(): string 177c1010edaSGreg Roach { 178a25f0a04SGreg Roach return $this->xref; 179a25f0a04SGreg Roach } 180a25f0a04SGreg Roach 181a25f0a04SGreg Roach /** 182000959d9SGreg Roach * Get the tree to which this record belongs 183000959d9SGreg Roach * 184000959d9SGreg Roach * @return Tree 185000959d9SGreg Roach */ 186f4afa648SGreg Roach public function tree(): Tree 187c1010edaSGreg Roach { 188518bbdc1SGreg Roach return $this->tree; 189000959d9SGreg Roach } 190000959d9SGreg Roach 191000959d9SGreg Roach /** 192a25f0a04SGreg Roach * Application code should access data via Fact objects. 193a25f0a04SGreg Roach * This function exists to support old code. 194a25f0a04SGreg Roach * 195a25f0a04SGreg Roach * @return string 196a25f0a04SGreg Roach */ 197e364afe4SGreg Roach public function gedcom(): string 198c1010edaSGreg Roach { 199b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 200a25f0a04SGreg Roach } 201a25f0a04SGreg Roach 202a25f0a04SGreg Roach /** 203a25f0a04SGreg Roach * Does this record have a pending change? 204a25f0a04SGreg Roach * 205cbc1590aSGreg Roach * @return bool 206a25f0a04SGreg Roach */ 2078f53f488SRico Sonntag public function isPendingAddition(): bool 208c1010edaSGreg Roach { 209a25f0a04SGreg Roach return $this->pending !== null; 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach 212a25f0a04SGreg Roach /** 213a25f0a04SGreg Roach * Does this record have a pending deletion? 214a25f0a04SGreg Roach * 215cbc1590aSGreg Roach * @return bool 216a25f0a04SGreg Roach */ 2178f53f488SRico Sonntag public function isPendingDeletion(): bool 218c1010edaSGreg Roach { 219a25f0a04SGreg Roach return $this->pending === ''; 220a25f0a04SGreg Roach } 221a25f0a04SGreg Roach 222a25f0a04SGreg Roach /** 223225e381fSGreg Roach * Generate a URL to this record. 224a25f0a04SGreg Roach * 225a25f0a04SGreg Roach * @return string 226a25f0a04SGreg Roach */ 2278f53f488SRico Sonntag public function url(): string 228c1010edaSGreg Roach { 229225e381fSGreg Roach return route(static::ROUTE_NAME, [ 230c0935879SGreg Roach 'xref' => $this->xref(), 231ee4364daSGreg Roach 'tree' => $this->tree->name(), 232194b0938SGreg Roach 'slug' => Registry::slugFactory()->make($this), 233225e381fSGreg Roach ]); 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach 236a25f0a04SGreg Roach /** 237a25f0a04SGreg Roach * Can the details of this record be shown? 238a25f0a04SGreg Roach * 239cbc1590aSGreg Roach * @param int|null $access_level 240a25f0a04SGreg Roach * 241cbc1590aSGreg Roach * @return bool 242a25f0a04SGreg Roach */ 2432c6f1bd5SGreg Roach public function canShow(int|null $access_level = null): bool 244c1010edaSGreg Roach { 2453529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 2464b9ff166SGreg Roach 247a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 248a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 249f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 250a25f0a04SGreg Roach return true; 251a25f0a04SGreg Roach } 252f0b9c048SGreg Roach 2537476e8a5SGreg Roach $cache_key = 'show-' . $this->xref . '-' . $this->tree->id() . '-' . $access_level; 254f0b9c048SGreg Roach 255f25fc0f9SGreg Roach return Registry::cache()->array()->remember($cache_key, fn () => $this->canShowRecord($access_level)); 256a25f0a04SGreg Roach } 257a25f0a04SGreg Roach 258a25f0a04SGreg Roach /** 259a25f0a04SGreg Roach * Can the name of this record be shown? 260a25f0a04SGreg Roach * 261cbc1590aSGreg Roach * @param int|null $access_level 262a25f0a04SGreg Roach * 263cbc1590aSGreg Roach * @return bool 264a25f0a04SGreg Roach */ 2652c6f1bd5SGreg Roach public function canShowName(int|null $access_level = null): bool 266c1010edaSGreg Roach { 267a25f0a04SGreg Roach return $this->canShow($access_level); 268a25f0a04SGreg Roach } 269a25f0a04SGreg Roach 270a25f0a04SGreg Roach /** 271a25f0a04SGreg Roach * Can we edit this record? 272a25f0a04SGreg Roach * 273cbc1590aSGreg Roach * @return bool 274a25f0a04SGreg Roach */ 2758f53f488SRico Sonntag public function canEdit(): bool 276c1010edaSGreg Roach { 2771450f098SGreg Roach if ($this->isPendingDeletion()) { 2781450f098SGreg Roach return false; 2791450f098SGreg Roach } 2801450f098SGreg Roach 2811450f098SGreg Roach if (Auth::isManager($this->tree)) { 2821450f098SGreg Roach return true; 2831450f098SGreg Roach } 2841450f098SGreg Roach 28555105892SGreg Roach $fact = $this->facts(['RESN'])->first(); 2862aca1e4cSGreg Roach $locked = $fact instanceof Fact && str_ends_with($fact->value(), RestrictionNotice::VALUE_LOCKED); 28755105892SGreg Roach 28855105892SGreg Roach return Auth::isEditor($this->tree) && !$locked; 289a25f0a04SGreg Roach } 290a25f0a04SGreg Roach 291a25f0a04SGreg Roach /** 292a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 293a25f0a04SGreg Roach */ 294e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 295c1010edaSGreg Roach { 296e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 297a25f0a04SGreg Roach return $this->gedcom; 298b2ce94c6SRico Sonntag } 299b2ce94c6SRico Sonntag 300*6830c5c1SGreg Roach if (!$this->canShow($access_level)) { 301*6830c5c1SGreg Roach return ''; 302*6830c5c1SGreg Roach } 303*6830c5c1SGreg Roach 304*6830c5c1SGreg Roach // The record is not private, but parts of it may be. 305a25f0a04SGreg Roach 306a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 307*6830c5c1SGreg Roach [$gedcom] = explode("\n", $this->gedcom . $this->pending, 2); 308a25f0a04SGreg Roach 309a25f0a04SGreg Roach // Check each of the facts for access 3108d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 311*6830c5c1SGreg Roach $gedcom .= "\n" . $fact->gedcom(); 312a25f0a04SGreg Roach } 313cbc1590aSGreg Roach 314*6830c5c1SGreg Roach // Remove links to missing and private records 315*6830c5c1SGreg Roach $patterns = [ 316*6830c5c1SGreg Roach '/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(?:\n[2-9].*)*/', 317*6830c5c1SGreg Roach '/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9].*)*/', 318*6830c5c1SGreg Roach '/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(?:\n[4-9].*)*/', 319*6830c5c1SGreg Roach '/\n4 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(?:\n[5-9].*)*/', 320*6830c5c1SGreg Roach ]; 321*6830c5c1SGreg Roach 322*6830c5c1SGreg Roach foreach ($patterns as $pattern) { 323*6830c5c1SGreg Roach preg_match_all($pattern, $gedcom, $matches, PREG_SET_ORDER); 324*6830c5c1SGreg Roach 325*6830c5c1SGreg Roach foreach ($matches as $match) { 326*6830c5c1SGreg Roach $xref = $match[1]; 327*6830c5c1SGreg Roach $record = Registry::gedcomRecordFactory()->make($xref, $this->tree); 328*6830c5c1SGreg Roach 329*6830c5c1SGreg Roach if ($record === null || !$record->canShow($access_level)) { 330*6830c5c1SGreg Roach $gedcom = str_replace($match[0], '', $gedcom); 331*6830c5c1SGreg Roach } 332*6830c5c1SGreg Roach } 333b2ce94c6SRico Sonntag } 334b2ce94c6SRico Sonntag 335*6830c5c1SGreg Roach return $gedcom; 336a25f0a04SGreg Roach } 337a25f0a04SGreg Roach 338a25f0a04SGreg Roach /** 339a25f0a04SGreg Roach * Default for "other" object types 340c7ff4153SGreg Roach * 341c7ff4153SGreg Roach * @return void 342a25f0a04SGreg Roach */ 343e364afe4SGreg Roach public function extractNames(): void 344c1010edaSGreg Roach { 34576f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 346a25f0a04SGreg Roach } 347a25f0a04SGreg Roach 348a25f0a04SGreg Roach /** 349a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 350a25f0a04SGreg Roach * 351870ec5a3SGreg Roach * @return array<int,array<string,string>> 352a25f0a04SGreg Roach */ 3538f53f488SRico Sonntag public function getAllNames(): array 354c1010edaSGreg Roach { 3559599771eSGreg Roach if ($this->getAllNames === []) { 356a25f0a04SGreg Roach if ($this->canShowName()) { 357a25f0a04SGreg Roach // Ask the record to extract its names 358a25f0a04SGreg Roach $this->extractNames(); 359a25f0a04SGreg Roach // No name found? Use a fallback. 360ec124fd2SGreg Roach if ($this->getAllNames === []) { 361db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 362a25f0a04SGreg Roach } 363a25f0a04SGreg Roach } else { 364db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach } 367cbc1590aSGreg Roach 368bdb3725aSGreg Roach return $this->getAllNames; 369a25f0a04SGreg Roach } 370a25f0a04SGreg Roach 371a25f0a04SGreg Roach /** 372a25f0a04SGreg Roach * If this object has no name, what do we call it? 373a25f0a04SGreg Roach * 374a25f0a04SGreg Roach * @return string 375a25f0a04SGreg Roach */ 3768f53f488SRico Sonntag public function getFallBackName(): string 377c1010edaSGreg Roach { 378c0935879SGreg Roach return e($this->xref()); 379a25f0a04SGreg Roach } 380a25f0a04SGreg Roach 381a25f0a04SGreg Roach /** 382a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 383a25f0a04SGreg Roach * 384cbc1590aSGreg Roach * @return int 385a25f0a04SGreg Roach */ 3868f53f488SRico Sonntag public function getPrimaryName(): int 387c1010edaSGreg Roach { 388a25f0a04SGreg Roach static $language_script; 389a25f0a04SGreg Roach 390448d466cSGreg Roach $language_script ??= I18N::locale()->script()->code(); 391a25f0a04SGreg Roach 392bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 393a25f0a04SGreg Roach // Generally, the first name is the primary one.... 394bdb3725aSGreg Roach $this->getPrimaryName = 0; 395a25f0a04SGreg Roach // ...except when the language/name use different character sets 396a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 3979d15b78eSGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 398bdb3725aSGreg Roach $this->getPrimaryName = $n; 399a25f0a04SGreg Roach break; 400a25f0a04SGreg Roach } 401a25f0a04SGreg Roach } 402a25f0a04SGreg Roach } 403a25f0a04SGreg Roach 404bdb3725aSGreg Roach return $this->getPrimaryName; 405a25f0a04SGreg Roach } 406a25f0a04SGreg Roach 407a25f0a04SGreg Roach /** 408a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 409a25f0a04SGreg Roach * 410cbc1590aSGreg Roach * @return int 411a25f0a04SGreg Roach */ 4128f53f488SRico Sonntag public function getSecondaryName(): int 413c1010edaSGreg Roach { 4148f038c36SRico Sonntag if ($this->getSecondaryName === null) { 415a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 416bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 417a25f0a04SGreg Roach // ....except when there are names with different character sets 418a25f0a04SGreg Roach $all_names = $this->getAllNames(); 419a25f0a04SGreg Roach if (count($all_names) > 1) { 420a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 421a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 422e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 423bdb3725aSGreg Roach $this->getSecondaryName = $n; 424a25f0a04SGreg Roach break; 425a25f0a04SGreg Roach } 426a25f0a04SGreg Roach } 427a25f0a04SGreg Roach } 428a25f0a04SGreg Roach } 429cbc1590aSGreg Roach 430bdb3725aSGreg Roach return $this->getSecondaryName; 431a25f0a04SGreg Roach } 432a25f0a04SGreg Roach 433a25f0a04SGreg Roach /** 434a2c8afeaSAlejandro Criado-Pérez * Allow the choice of primary name to be overridden, e.g. in a search result 435a25f0a04SGreg Roach * 43676f666f4SGreg Roach * @param int|null $n 4377e96c925SGreg Roach * 4387e96c925SGreg Roach * @return void 439a25f0a04SGreg Roach */ 4402c6f1bd5SGreg Roach public function setPrimaryName(int|null $n = null): void 441c1010edaSGreg Roach { 442bdb3725aSGreg Roach $this->getPrimaryName = $n; 443bdb3725aSGreg Roach $this->getSecondaryName = null; 444a25f0a04SGreg Roach } 445a25f0a04SGreg Roach 446a25f0a04SGreg Roach /** 447a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 448a25f0a04SGreg Roach * 449a25f0a04SGreg Roach * @return string 450a25f0a04SGreg Roach */ 45124f2a3afSGreg Roach public function __toString(): string 452c1010edaSGreg Roach { 45372cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 454a25f0a04SGreg Roach } 455a25f0a04SGreg Roach 456a25f0a04SGreg Roach /** 457c156e8f5SGreg Roach * /** 458a25f0a04SGreg Roach * Get variants of the name 459a25f0a04SGreg Roach * 460a25f0a04SGreg Roach * @return string 461a25f0a04SGreg Roach */ 462e364afe4SGreg Roach public function fullName(): string 463c1010edaSGreg Roach { 464a25f0a04SGreg Roach if ($this->canShowName()) { 465a25f0a04SGreg Roach $tmp = $this->getAllNames(); 466cbc1590aSGreg Roach 467a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 468a25f0a04SGreg Roach } 469b2ce94c6SRico Sonntag 470b2ce94c6SRico Sonntag return I18N::translate('Private'); 471a25f0a04SGreg Roach } 472a25f0a04SGreg Roach 473a25f0a04SGreg Roach /** 474a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 475a25f0a04SGreg Roach * 476a25f0a04SGreg Roach * @return string 477a25f0a04SGreg Roach */ 47839ca88baSGreg Roach public function sortName(): string 479c1010edaSGreg Roach { 480a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 481a25f0a04SGreg Roach $tmp = $this->getAllNames(); 482cbc1590aSGreg Roach 483a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 484a25f0a04SGreg Roach } 485a25f0a04SGreg Roach 486a25f0a04SGreg Roach /** 487a25f0a04SGreg Roach * Get the full name in an alternative character set 488a25f0a04SGreg Roach * 489e364afe4SGreg Roach * @return string|null 490a25f0a04SGreg Roach */ 4911ff45046SGreg Roach public function alternateName(): string|null 492c1010edaSGreg Roach { 493e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 494a25f0a04SGreg Roach $all_names = $this->getAllNames(); 495cbc1590aSGreg Roach 496a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 497a25f0a04SGreg Roach } 498b2ce94c6SRico Sonntag 499b2ce94c6SRico Sonntag return null; 500a25f0a04SGreg Roach } 501a25f0a04SGreg Roach 502a25f0a04SGreg Roach /** 503a25f0a04SGreg Roach * Format this object for display in a list 504a25f0a04SGreg Roach * 505a25f0a04SGreg Roach * @return string 506a25f0a04SGreg Roach */ 5078f53f488SRico Sonntag public function formatList(): string 508c1010edaSGreg Roach { 509a79e52aaSGreg Roach $html = '<a href="' . e($this->url()) . '">'; 51039ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 511b165e17cSGreg Roach $html .= '</a>'; 512a79e52aaSGreg Roach $html .= $this->formatListDetails(); 513cbc1590aSGreg Roach 514a25f0a04SGreg Roach return $html; 515a25f0a04SGreg Roach } 516a25f0a04SGreg Roach 517a25f0a04SGreg Roach /** 518a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 519a25f0a04SGreg Roach * identifying characteristics of this record. 520a25f0a04SGreg Roach * 521a25f0a04SGreg Roach * @return string 522a25f0a04SGreg Roach */ 5238f53f488SRico Sonntag public function formatListDetails(): string 524c1010edaSGreg Roach { 525a25f0a04SGreg Roach return ''; 526a25f0a04SGreg Roach } 527a25f0a04SGreg Roach 528a25f0a04SGreg Roach /** 529a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 530a25f0a04SGreg Roach * 53109482a55SGreg Roach * @param array<string> $facts 532cbc1590aSGreg Roach * @param int $style 533a25f0a04SGreg Roach * 534a25f0a04SGreg Roach * @return string 535a25f0a04SGreg Roach */ 5368d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 537c1010edaSGreg Roach { 538a79e52aaSGreg Roach $fact = $this->facts($facts, true)->first(); 539a79e52aaSGreg Roach 540a79e52aaSGreg Roach if ($fact === null) { 541a79e52aaSGreg Roach return ''; 542a25f0a04SGreg Roach } 543cbc1590aSGreg Roach 544a79e52aaSGreg Roach // Only display if it has a date or place (or both) 545a79e52aaSGreg Roach $attributes = []; 546a79e52aaSGreg Roach 547a79e52aaSGreg Roach if ($fact->date()->isOK()) { 548a79e52aaSGreg Roach $attributes[] = view('fact-date', ['cal_link' => 'false', 'fact' => $fact, 'record' => $fact->record(), 'time' => false]); 549a79e52aaSGreg Roach } 550a79e52aaSGreg Roach 551a79e52aaSGreg Roach if ($fact->place()->gedcomName() !== '' && $style === 2) { 552a79e52aaSGreg Roach $attributes[] = $fact->place()->shortName(); 553a79e52aaSGreg Roach } 554a79e52aaSGreg Roach 555a79e52aaSGreg Roach if ($attributes === []) { 556a25f0a04SGreg Roach return ''; 557a25f0a04SGreg Roach } 558a25f0a04SGreg Roach 559a79e52aaSGreg Roach return '<div><em>' . I18N::translate('%1$s: %2$s', $fact->label(), implode(' — ', $attributes)) . '</em></div>'; 560a79e52aaSGreg Roach } 561a79e52aaSGreg Roach 562a25f0a04SGreg Roach /** 563a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 564a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 565a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 566a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 567a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 568a25f0a04SGreg Roach * 56909482a55SGreg Roach * @param array<string> $events 570a25f0a04SGreg Roach * 57109482a55SGreg Roach * @return array<Date> 572a25f0a04SGreg Roach */ 5738d0ebef0SGreg Roach public function getAllEventDates(array $events): array 574c1010edaSGreg Roach { 57513abd6f3SGreg Roach $dates = []; 5761385b8caSGreg Roach foreach ($this->facts($events, false, null, true) as $event) { 5772decada7SGreg Roach if ($event->date()->isOK()) { 5782decada7SGreg Roach $dates[] = $event->date(); 579a25f0a04SGreg Roach } 580a25f0a04SGreg Roach } 581a25f0a04SGreg Roach 582a25f0a04SGreg Roach return $dates; 583a25f0a04SGreg Roach } 584a25f0a04SGreg Roach 585a25f0a04SGreg Roach /** 586a25f0a04SGreg Roach * Get all the places for a particular type of event 587a25f0a04SGreg Roach * 58809482a55SGreg Roach * @param array<string> $events 589a25f0a04SGreg Roach * 59009482a55SGreg Roach * @return array<Place> 591a25f0a04SGreg Roach */ 5928d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 593c1010edaSGreg Roach { 59413abd6f3SGreg Roach $places = []; 5958d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 596138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 597a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 59816d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 599a25f0a04SGreg Roach } 600a25f0a04SGreg Roach } 601a25f0a04SGreg Roach } 602a25f0a04SGreg Roach 603a25f0a04SGreg Roach return $places; 604a25f0a04SGreg Roach } 605a25f0a04SGreg Roach 606a25f0a04SGreg Roach /** 607a25f0a04SGreg Roach * The facts and events for this record. 608a25f0a04SGreg Roach * 60909482a55SGreg Roach * @param array<string> $filter 610cbc1590aSGreg Roach * @param bool $sort 611cbc1590aSGreg Roach * @param int|null $access_level 612ce42304aSGreg Roach * @param bool $ignore_deleted 613a25f0a04SGreg Roach * 61436779af1SGreg Roach * @return Collection<int,Fact> 615a25f0a04SGreg Roach */ 616ce42304aSGreg Roach public function facts( 617ce42304aSGreg Roach array $filter = [], 618ce42304aSGreg Roach bool $sort = false, 6192c6f1bd5SGreg Roach int|null $access_level = null, 620ce42304aSGreg Roach bool $ignore_deleted = false 621ce42304aSGreg Roach ): Collection { 6223529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 6234b9ff166SGreg Roach 624d0889c63SGreg Roach // Convert BIRT into INDI:BIRT, etc. 625d0889c63SGreg Roach $filter = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, $filter); 626d0889c63SGreg Roach 6278af3e5c1SGreg Roach $facts = new Collection(); 628ce42304aSGreg Roach if ($this->canShow($access_level)) { 629a25f0a04SGreg Roach foreach ($this->facts as $fact) { 630d0889c63SGreg Roach if (($filter === [] || in_array($fact->tag(), $filter, true)) && $fact->canShow($access_level)) { 6318af3e5c1SGreg Roach $facts->push($fact); 632a25f0a04SGreg Roach } 633a25f0a04SGreg Roach } 634a25f0a04SGreg Roach } 635d17d7b9eSGreg Roach 636a25f0a04SGreg Roach if ($sort) { 6370f5fd22fSGreg Roach switch ($this->tag()) { 6380f5fd22fSGreg Roach case Family::RECORD_TYPE: 6390f5fd22fSGreg Roach case Individual::RECORD_TYPE: 640580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 6410f5fd22fSGreg Roach break; 6420f5fd22fSGreg Roach 6430f5fd22fSGreg Roach default: 6440f5fd22fSGreg Roach $subtags = Registry::elementFactory()->make($this->tag())->subtags(); 6450f5fd22fSGreg Roach $subtags = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, array_keys($subtags)); 6461259996fSGreg Roach 6471259996fSGreg Roach if ($subtags !== []) { 6481259996fSGreg Roach // Renumber keys from 1. 6490f5fd22fSGreg Roach $subtags = array_combine(range(1, count($subtags)), $subtags); 6501259996fSGreg Roach } 6511259996fSGreg Roach 6520f5fd22fSGreg Roach $facts = $facts 6530f5fd22fSGreg Roach ->sort(static function (Fact $x, Fact $y) use ($subtags): int { 6540f5fd22fSGreg Roach $sort_x = array_search($x->tag(), $subtags, true) ?: PHP_INT_MAX; 6550f5fd22fSGreg Roach $sort_y = array_search($y->tag(), $subtags, true) ?: PHP_INT_MAX; 6560f5fd22fSGreg Roach 6570f5fd22fSGreg Roach return $sort_x <=> $sort_y; 6580f5fd22fSGreg Roach }); 6590f5fd22fSGreg Roach break; 6600f5fd22fSGreg Roach } 661a25f0a04SGreg Roach } 662cbc1590aSGreg Roach 663ce42304aSGreg Roach if ($ignore_deleted) { 664f25fc0f9SGreg Roach $facts = $facts->filter(static fn (Fact $fact): bool => !$fact->isPendingDeletion()); 665ce42304aSGreg Roach } 666ce42304aSGreg Roach 6675fef3bfaSGreg Roach return $facts; 668a25f0a04SGreg Roach } 669a25f0a04SGreg Roach 670a25f0a04SGreg Roach /** 6710f5fd22fSGreg Roach * @return array<string,string> 6720f5fd22fSGreg Roach */ 6730f5fd22fSGreg Roach public function missingFacts(): array 6740f5fd22fSGreg Roach { 6750f5fd22fSGreg Roach $missing_facts = []; 6760f5fd22fSGreg Roach 6770f5fd22fSGreg Roach foreach (Registry::elementFactory()->make($this->tag())->subtags() as $subtag => $repeat) { 6780f5fd22fSGreg Roach [, $max] = explode(':', $repeat); 6790f5fd22fSGreg Roach $max = $max === 'M' ? PHP_INT_MAX : (int) $max; 6800f5fd22fSGreg Roach 68198f93f3aSGreg Roach if ($this->facts([$subtag], false, null, true)->count() < $max) { 6820f5fd22fSGreg Roach $missing_facts[$subtag] = $subtag; 6830f5fd22fSGreg Roach $missing_facts[$subtag] = Registry::elementFactory()->make($this->tag() . ':' . $subtag)->label(); 6840f5fd22fSGreg Roach } 6850f5fd22fSGreg Roach } 6860f5fd22fSGreg Roach 6870f5fd22fSGreg Roach uasort($missing_facts, I18N::comparator()); 6880f5fd22fSGreg Roach 6895416d6ddSGreg Roach if (!Auth::canUploadMedia($this->tree, Auth::user())) { 6900f5fd22fSGreg Roach unset($missing_facts['OBJE']); 6910f5fd22fSGreg Roach } 6920f5fd22fSGreg Roach 6930f5fd22fSGreg Roach // We have special code for this. 6940f5fd22fSGreg Roach unset($missing_facts['FILE']); 6950f5fd22fSGreg Roach 6960f5fd22fSGreg Roach return $missing_facts; 6970f5fd22fSGreg Roach } 6980f5fd22fSGreg Roach 6990f5fd22fSGreg Roach /** 7004459dc9aSGreg Roach * Get the last-change timestamp for this record 701a25f0a04SGreg Roach * 70276d39c55SGreg Roach * @return TimestampInterface 703a25f0a04SGreg Roach */ 70476d39c55SGreg Roach public function lastChangeTimestamp(): TimestampInterface 705c1010edaSGreg Roach { 706820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 707a25f0a04SGreg Roach 7084459dc9aSGreg Roach if ($chan instanceof Fact) { 70903f2cc61SGreg Roach // The record has a CHAN event. 71086c727c1SGreg Roach $date = $chan->date()->minimumDate(); 71186c727c1SGreg Roach $ymd = sprintf('%04d-%02d-%02d', $date->year(), $date->month(), $date->day()); 7124459dc9aSGreg Roach 71386c727c1SGreg Roach if ($ymd !== '') { 71403f2cc61SGreg Roach // The CHAN event has a valid DATE. 71586c727c1SGreg Roach if (preg_match('/\n3 TIME (([01]\d|2[0-3]):([0-5]\d):([0-5]\d))/', $chan->gedcom(), $match) === 1) { 71686c727c1SGreg Roach return Registry::timestampFactory()->fromString($ymd . $match[1], 'Y-m-d H:i:s'); 717e364afe4SGreg Roach } 718e364afe4SGreg Roach 71986c727c1SGreg Roach if (preg_match('/\n3 TIME (([01]\d|2[0-3]):([0-5]\d))/', $chan->gedcom(), $match) === 1) { 72086c727c1SGreg Roach return Registry::timestampFactory()->fromString($ymd . $match[1], 'Y-m-d H:i'); 721b2ce94c6SRico Sonntag } 722b2ce94c6SRico Sonntag 72386c727c1SGreg Roach return Registry::timestampFactory()->fromString($ymd, 'Y-m-d'); 724a25f0a04SGreg Roach } 72503f2cc61SGreg Roach } 726b2ce94c6SRico Sonntag 727a25f0a04SGreg Roach // The record does not have a CHAN event 728d97083feSGreg Roach return Registry::timestampFactory()->make(0); 729a25f0a04SGreg Roach } 730a25f0a04SGreg Roach 731a25f0a04SGreg Roach /** 732a25f0a04SGreg Roach * Get the last-change user for this record 733a25f0a04SGreg Roach * 734a25f0a04SGreg Roach * @return string 735a25f0a04SGreg Roach */ 736e364afe4SGreg Roach public function lastChangeUser(): string 737c1010edaSGreg Roach { 738820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 739a25f0a04SGreg Roach 740a25f0a04SGreg Roach if ($chan === null) { 741a25f0a04SGreg Roach return I18N::translate('Unknown'); 742b2ce94c6SRico Sonntag } 743b2ce94c6SRico Sonntag 7443425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 745baacc364SGreg Roach if ($chan_user === '') { 746a25f0a04SGreg Roach return I18N::translate('Unknown'); 747b2ce94c6SRico Sonntag } 748b2ce94c6SRico Sonntag 749a25f0a04SGreg Roach return $chan_user; 750a25f0a04SGreg Roach } 751a25f0a04SGreg Roach 752a25f0a04SGreg Roach /** 753a25f0a04SGreg Roach * Add a new fact to this record 754a25f0a04SGreg Roach * 755a25f0a04SGreg Roach * @param string $gedcom 756cbc1590aSGreg Roach * @param bool $update_chan 7577e96c925SGreg Roach * 7587e96c925SGreg Roach * @return void 759a25f0a04SGreg Roach */ 760e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 761c1010edaSGreg Roach { 762fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 763a25f0a04SGreg Roach } 764a25f0a04SGreg Roach 765a25f0a04SGreg Roach /** 766a25f0a04SGreg Roach * Delete a fact from this record 767a25f0a04SGreg Roach * 768a25f0a04SGreg Roach * @param string $fact_id 769cbc1590aSGreg Roach * @param bool $update_chan 7707e96c925SGreg Roach * 7717e96c925SGreg Roach * @return void 772a25f0a04SGreg Roach */ 773e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 774c1010edaSGreg Roach { 775db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 776a25f0a04SGreg Roach } 777a25f0a04SGreg Roach 778a25f0a04SGreg Roach /** 779a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 780a25f0a04SGreg Roach * 781a25f0a04SGreg Roach * @param string $fact_id 782a25f0a04SGreg Roach * @param string $gedcom 783cbc1590aSGreg Roach * @param bool $update_chan 784a25f0a04SGreg Roach * 7857e96c925SGreg Roach * @return void 7867e96c925SGreg Roach * @throws Exception 787a25f0a04SGreg Roach */ 788e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 789c1010edaSGreg Roach { 79071f696adSGreg Roach // Not all record types allow a CHAN event. 79171f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 79271f696adSGreg Roach 793a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 794a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 795a25f0a04SGreg Roach $gedcom = trim($gedcom); 796a25f0a04SGreg Roach 797a25f0a04SGreg Roach if ($this->pending === '') { 7987e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 799a25f0a04SGreg Roach } 8008d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 8017e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 802a25f0a04SGreg Roach } 803a25f0a04SGreg Roach 804b6ec1ccfSGreg Roach if ($this->pending !== null && $this->pending !== '') { 805a25f0a04SGreg Roach $old_gedcom = $this->pending; 806a25f0a04SGreg Roach } else { 807a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 808a25f0a04SGreg Roach } 809a25f0a04SGreg Roach 810a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 81165e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 812a25f0a04SGreg Roach 813a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 81419aed3a1SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE, true) as $fact) { 815905ab80aSGreg Roach if ($fact->id() === $fact_id) { 816db7bb364SGreg Roach if ($gedcom !== '') { 817a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 818a25f0a04SGreg Roach } 819fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 82072f8afdbSGreg Roach } elseif ($update_chan && str_ends_with($fact->tag(), ':CHAN')) { 82172f8afdbSGreg Roach $new_gedcom .= "\n" . $this->updateChange($fact->gedcom()); 82272f8afdbSGreg Roach } else { 823138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 824a25f0a04SGreg Roach } 825a25f0a04SGreg Roach } 826a25f0a04SGreg Roach 827a25f0a04SGreg Roach // Adding a new fact 828fc3ccce4SGreg Roach if ($fact_id === '') { 829a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 830a25f0a04SGreg Roach } 831a25f0a04SGreg Roach 832dec352c1SGreg Roach if ($update_chan && !str_contains($new_gedcom, "\n1 CHAN")) { 83372f8afdbSGreg Roach $new_gedcom .= $this->updateChange("\n1 CHAN"); 83419aed3a1SGreg Roach } 83519aed3a1SGreg Roach 836e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 837a25f0a04SGreg Roach // Save the changes 838d09b6323SGreg Roach DB::table('change')->insert([ 839d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 840d09b6323SGreg Roach 'xref' => $this->xref, 841d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 842d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 8435ae3edd9SGreg Roach 'status' => 'pending', 844d09b6323SGreg Roach 'user_id' => Auth::id(), 84513abd6f3SGreg Roach ]); 846a25f0a04SGreg Roach 847a25f0a04SGreg Roach $this->pending = $new_gedcom; 848a25f0a04SGreg Roach 8491fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 850d35568b4SGreg Roach $pending_changes_service = Registry::container()->get(PendingChangesService::class); 851b55cbc6bSGreg Roach 852b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 853a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 854a25f0a04SGreg Roach $this->pending = null; 855a25f0a04SGreg Roach } 856a25f0a04SGreg Roach } 8579599771eSGreg Roach 8589599771eSGreg Roach $this->facts = $this->parseFacts(); 859a25f0a04SGreg Roach } 860a25f0a04SGreg Roach 861a25f0a04SGreg Roach /** 862a25f0a04SGreg Roach * Update this record 863a25f0a04SGreg Roach * 864a25f0a04SGreg Roach * @param string $gedcom 865cbc1590aSGreg Roach * @param bool $update_chan 8667e96c925SGreg Roach * 8677e96c925SGreg Roach * @return void 868a25f0a04SGreg Roach */ 869e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 870c1010edaSGreg Roach { 87171f696adSGreg Roach // Not all record types allow a CHAN event. 87271f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 87371f696adSGreg Roach 874a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 875a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 876a25f0a04SGreg Roach $gedcom = trim($gedcom); 877a25f0a04SGreg Roach 878a25f0a04SGreg Roach // Update the CHAN record 879a25f0a04SGreg Roach if ($update_chan) { 88072f8afdbSGreg Roach if (preg_match('/\n1 CHAN(\n[2-9].*)*/', $gedcom, $match)) { 88172f8afdbSGreg Roach $gedcom = strtr($gedcom, [$match[0] => $this->updateChange($match[0])]); 88272f8afdbSGreg Roach } else { 88372f8afdbSGreg Roach $gedcom .= $this->updateChange("\n1 CHAN"); 88472f8afdbSGreg Roach } 885a25f0a04SGreg Roach } 886a25f0a04SGreg Roach 887a25f0a04SGreg Roach // Create a pending change 888d09b6323SGreg Roach DB::table('change')->insert([ 889d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 890d09b6323SGreg Roach 'xref' => $this->xref, 891d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 892d09b6323SGreg Roach 'new_gedcom' => $gedcom, 893665d37b1SGreg Roach 'status' => 'pending', 894d09b6323SGreg Roach 'user_id' => Auth::id(), 89513abd6f3SGreg Roach ]); 896a25f0a04SGreg Roach 897a25f0a04SGreg Roach // Clear the cache 898a25f0a04SGreg Roach $this->pending = $gedcom; 899a25f0a04SGreg Roach 900a25f0a04SGreg Roach // Accept this pending change 9011fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 902d35568b4SGreg Roach $pending_changes_service = Registry::container()->get(PendingChangesService::class); 903b55cbc6bSGreg Roach 904b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 905a25f0a04SGreg Roach $this->gedcom = $gedcom; 906a25f0a04SGreg Roach $this->pending = null; 907a25f0a04SGreg Roach } 908a25f0a04SGreg Roach 9099599771eSGreg Roach $this->facts = $this->parseFacts(); 910a25f0a04SGreg Roach 911847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 912a25f0a04SGreg Roach } 913a25f0a04SGreg Roach 914a25f0a04SGreg Roach /** 915a25f0a04SGreg Roach * Delete this record 916b874da82SGreg Roach * 917b874da82SGreg Roach * @return void 918a25f0a04SGreg Roach */ 919e364afe4SGreg Roach public function deleteRecord(): void 920c1010edaSGreg Roach { 921a25f0a04SGreg Roach // Create a pending change 9224c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 923d09b6323SGreg Roach DB::table('change')->insert([ 924d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 925d09b6323SGreg Roach 'xref' => $this->xref, 926d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 927d09b6323SGreg Roach 'new_gedcom' => '', 928665d37b1SGreg Roach 'status' => 'pending', 929d09b6323SGreg Roach 'user_id' => Auth::id(), 93013abd6f3SGreg Roach ]); 9314c0b5256SGreg Roach } 932a25f0a04SGreg Roach 9334c0b5256SGreg Roach // Auto-accept this pending change 9341fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 935d35568b4SGreg Roach $pending_changes_service = Registry::container()->get(PendingChangesService::class); 936b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 937a25f0a04SGreg Roach } 938a25f0a04SGreg Roach 939847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 940a25f0a04SGreg Roach } 941a25f0a04SGreg Roach 942a25f0a04SGreg Roach /** 943a25f0a04SGreg Roach * Remove all links from this record to $xref 944a25f0a04SGreg Roach * 945a25f0a04SGreg Roach * @param string $xref 946cbc1590aSGreg Roach * @param bool $update_chan 9477e96c925SGreg Roach * 9487e96c925SGreg Roach * @return void 949a25f0a04SGreg Roach */ 950e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 951c1010edaSGreg Roach { 952a25f0a04SGreg Roach $value = '@' . $xref . '@'; 953a25f0a04SGreg Roach 95430158ae7SGreg Roach foreach ($this->facts() as $fact) { 95584586c02SGreg Roach if ($fact->value() === $value) { 956905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 9578d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 958138ca96cSGreg Roach $gedcom = $fact->gedcom(); 959a25f0a04SGreg Roach foreach ($matches as $match) { 960ef475b14SGreg Roach $next_level = 1 + (int) $match[1]; 961a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 962a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 963a25f0a04SGreg Roach } 964905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 965a25f0a04SGreg Roach } 966a25f0a04SGreg Roach } 967a25f0a04SGreg Roach } 968bf4eb542SGreg Roach 969bf4eb542SGreg Roach /** 97022e73debSGreg Roach * Each object type may have its own special rules, and re-implement this function. 97122e73debSGreg Roach * 97222e73debSGreg Roach * @param int $access_level 97322e73debSGreg Roach * 97422e73debSGreg Roach * @return bool 97522e73debSGreg Roach */ 97622e73debSGreg Roach protected function canShowByType(int $access_level): bool 97722e73debSGreg Roach { 97822e73debSGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 97922e73debSGreg Roach 98022e73debSGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 98122e73debSGreg Roach // Restriction found 98222e73debSGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 98322e73debSGreg Roach } 98422e73debSGreg Roach 98522e73debSGreg Roach // No restriction found - must be public: 98622e73debSGreg Roach return true; 98722e73debSGreg Roach } 98822e73debSGreg Roach 98922e73debSGreg Roach /** 99022e73debSGreg Roach * Convert a name record into sortable and full/display versions. This default 99122e73debSGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 99222e73debSGreg Roach * 99322e73debSGreg Roach * @param string $type 99422e73debSGreg Roach * @param string $value 99522e73debSGreg Roach * @param string $gedcom 99622e73debSGreg Roach * 99722e73debSGreg Roach * @return void 99822e73debSGreg Roach */ 99922e73debSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 100022e73debSGreg Roach { 100122e73debSGreg Roach $this->getAllNames[] = [ 100222e73debSGreg Roach 'type' => $type, 1003f25fc0f9SGreg Roach 'sort' => preg_replace_callback('/(\d+)/', static fn (array $matches): string => str_pad($matches[0], 10, '0', STR_PAD_LEFT), $value), 1004315eb316SGreg Roach 'full' => '<bdi>' . e($value) . '</bdi>', 100522e73debSGreg Roach // This is used for display 100622e73debSGreg Roach 'fullNN' => $value, 100722e73debSGreg Roach // This goes into the database 100822e73debSGreg Roach ]; 100922e73debSGreg Roach } 101022e73debSGreg Roach 101122e73debSGreg Roach /** 101222e73debSGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 101322e73debSGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 101422e73debSGreg Roach * Parameters: the level 1 fact containing the name. 101522e73debSGreg Roach * Return value: an array of name structures, each containing 101622e73debSGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 101722e73debSGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 101822e73debSGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 101922e73debSGreg Roach * 102022e73debSGreg Roach * @param int $level 102122e73debSGreg Roach * @param string $fact_type 102236779af1SGreg Roach * @param Collection<int,Fact> $facts 102322e73debSGreg Roach * 102422e73debSGreg Roach * @return void 102522e73debSGreg Roach */ 102622e73debSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 102722e73debSGreg Roach { 102822e73debSGreg Roach $sublevel = $level + 1; 102922e73debSGreg Roach $subsublevel = $sublevel + 1; 103022e73debSGreg Roach foreach ($facts as $fact) { 1031c8ff5fdcSGreg Roach if (preg_match_all('/^' . $level . ' (' . $fact_type . ') (.+)((\n[' . $sublevel . '-9].+)*)/m', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 103222e73debSGreg Roach foreach ($matches as $match) { 103322e73debSGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 1034cc9d82dcSGreg Roach if ($match[1] === 'NAME' && str_contains(strtoupper($match[3]), "\n2 TYPE MARRIED")) { 103522e73debSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 103622e73debSGreg Roach } else { 103722e73debSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 103822e73debSGreg Roach } 1039c8ff5fdcSGreg Roach if ($match[3] && preg_match_all('/^' . $sublevel . ' (ROMN|FONE|_\w+) (.+)((\n[' . $subsublevel . '-9].+)*)/m', $match[3], $submatches, PREG_SET_ORDER)) { 104022e73debSGreg Roach foreach ($submatches as $submatch) { 10412d21e9c2SGreg Roach if ($submatch[1] !== '_RUFNAME') { 104222e73debSGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 104322e73debSGreg Roach } 104422e73debSGreg Roach } 104522e73debSGreg Roach } 104622e73debSGreg Roach } 104722e73debSGreg Roach } 104822e73debSGreg Roach } 10492d21e9c2SGreg Roach } 105022e73debSGreg Roach 105122e73debSGreg Roach /** 105222e73debSGreg Roach * Split the record into facts 105322e73debSGreg Roach * 10549599771eSGreg Roach * @return array<Fact> 105522e73debSGreg Roach */ 10569599771eSGreg Roach private function parseFacts(): array 105722e73debSGreg Roach { 105822e73debSGreg Roach // Split the record into facts 1059b6ec1ccfSGreg Roach if ($this->gedcom !== '') { 1060d823340dSGreg Roach $gedcom_facts = preg_split('/\n(?=1)/', $this->gedcom); 106122e73debSGreg Roach array_shift($gedcom_facts); 106222e73debSGreg Roach } else { 106322e73debSGreg Roach $gedcom_facts = []; 106422e73debSGreg Roach } 1065b6ec1ccfSGreg Roach if ($this->pending !== null && $this->pending !== '') { 1066d823340dSGreg Roach $pending_facts = preg_split('/\n(?=1)/', $this->pending); 106722e73debSGreg Roach array_shift($pending_facts); 106822e73debSGreg Roach } else { 106922e73debSGreg Roach $pending_facts = []; 107022e73debSGreg Roach } 107122e73debSGreg Roach 10729599771eSGreg Roach $facts = []; 107322e73debSGreg Roach 107422e73debSGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 107522e73debSGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 107622e73debSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 107722e73debSGreg Roach $fact->setPendingDeletion(); 107822e73debSGreg Roach } 10799599771eSGreg Roach $facts[] = $fact; 108022e73debSGreg Roach } 108122e73debSGreg Roach foreach ($pending_facts as $pending_fact) { 108222e73debSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 108322e73debSGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 108422e73debSGreg Roach $fact->setPendingAddition(); 10859599771eSGreg Roach $facts[] = $fact; 108622e73debSGreg Roach } 108722e73debSGreg Roach } 10889599771eSGreg Roach 10899599771eSGreg Roach return $facts; 109022e73debSGreg Roach } 109122e73debSGreg Roach 109222e73debSGreg Roach /** 109322e73debSGreg Roach * Work out whether this record can be shown to a user with a given access level 109422e73debSGreg Roach * 109522e73debSGreg Roach * @param int $access_level 109622e73debSGreg Roach * 109722e73debSGreg Roach * @return bool 109822e73debSGreg Roach */ 109922e73debSGreg Roach private function canShowRecord(int $access_level): bool 110022e73debSGreg Roach { 110122e73debSGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 110222e73debSGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 110322e73debSGreg Roach return true; 110422e73debSGreg Roach } 110522e73debSGreg Roach 110622e73debSGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 11071fe542e9SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) { 110822e73debSGreg Roach return true; 110922e73debSGreg Roach } 111022e73debSGreg Roach 111155105892SGreg Roach // Does this record have a restriction notice? 1112da7adf56SGreg Roach // Cannot use $this->>fact(), as that function calls this one. 111355105892SGreg Roach if (preg_match('/\n1 RESN (.+)/', $this->gedcom(), $match)) { 111455105892SGreg Roach $element = new RestrictionNotice(''); 111555105892SGreg Roach $restriction = $element->canonical($match[1]); 111655105892SGreg Roach 111755105892SGreg Roach if (str_starts_with($restriction, RestrictionNotice::VALUE_CONFIDENTIAL)) { 111822e73debSGreg Roach return Auth::PRIV_NONE >= $access_level; 111922e73debSGreg Roach } 112055105892SGreg Roach if (str_starts_with($restriction, RestrictionNotice::VALUE_PRIVACY)) { 112122e73debSGreg Roach return Auth::PRIV_USER >= $access_level; 112222e73debSGreg Roach } 112355105892SGreg Roach if (str_starts_with($restriction, RestrictionNotice::VALUE_NONE)) { 112422e73debSGreg Roach return true; 112522e73debSGreg Roach } 112655105892SGreg Roach } 112722e73debSGreg Roach 112822e73debSGreg Roach // Does this record have a default RESN? 112922e73debSGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 113022e73debSGreg Roach if (isset($individual_privacy[$this->xref()])) { 113122e73debSGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 113222e73debSGreg Roach } 113322e73debSGreg Roach 113422e73debSGreg Roach // Privacy rules do not apply to admins 113522e73debSGreg Roach if (Auth::PRIV_NONE >= $access_level) { 113622e73debSGreg Roach return true; 113722e73debSGreg Roach } 113822e73debSGreg Roach 113922e73debSGreg Roach // Different types of record have different privacy rules 114022e73debSGreg Roach return $this->canShowByType($access_level); 114122e73debSGreg Roach } 11428091bfd1SGreg Roach 11438091bfd1SGreg Roach /** 11448091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11458091bfd1SGreg Roach */ 11468091bfd1SGreg Roach public function lock(): void 11478091bfd1SGreg Roach { 11488091bfd1SGreg Roach DB::table('other') 11498091bfd1SGreg Roach ->where('o_file', '=', $this->tree->id()) 11508091bfd1SGreg Roach ->where('o_id', '=', $this->xref()) 11518091bfd1SGreg Roach ->lockForUpdate() 11528091bfd1SGreg Roach ->get(); 11538091bfd1SGreg Roach } 115472f8afdbSGreg Roach 115572f8afdbSGreg Roach /** 115672f8afdbSGreg Roach * Change records may contain notes and other fields. Just update the date/time/author. 115772f8afdbSGreg Roach * 115872f8afdbSGreg Roach * @param string $gedcom 115972f8afdbSGreg Roach * 116072f8afdbSGreg Roach * @return string 116172f8afdbSGreg Roach */ 11625cba5dc7SGreg Roach private function updateChange(string $gedcom): string 11635cba5dc7SGreg Roach { 116472f8afdbSGreg Roach $gedcom = preg_replace('/\n2 (DATE|_WT_USER).*(\n[3-9].*)*/', '', $gedcom); 116572f8afdbSGreg Roach $today = strtoupper(date('d M Y')); 116672f8afdbSGreg Roach $now = date('H:i:s'); 116772f8afdbSGreg Roach $author = Auth::user()->userName(); 116872f8afdbSGreg Roach 116972f8afdbSGreg Roach return $gedcom . "\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . $author; 117072f8afdbSGreg Roach } 1171a25f0a04SGreg Roach} 1172