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; 5355105892SGreg Roachuse function str_starts_with; 54f7440925SGreg Roachuse function strtoupper; 5572f8afdbSGreg Roachuse function strtr; 56f7440925SGreg Roachuse function trim; 57b315f3e1SGreg Roachuse function view; 58b315f3e1SGreg Roach 59c2ed51d1SGreg Roachuse const PHP_INT_MAX; 60f7440925SGreg Roachuse const PREG_SET_ORDER; 61f7440925SGreg Roachuse const STR_PAD_LEFT; 6222e73debSGreg Roach 63a25f0a04SGreg Roach/** 6476692c8bSGreg Roach * A GEDCOM object. 65a25f0a04SGreg Roach */ 66c1010edaSGreg Roachclass GedcomRecord 67c1010edaSGreg Roach{ 6816d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 6916d6367aSGreg Roach 705818a371SGreg Roach protected const ROUTE_NAME = GedcomRecordPage::class; 715818a371SGreg Roach 729599771eSGreg Roach protected string $xref; 73bb03c9f0SGreg Roach 749599771eSGreg Roach protected Tree $tree; 75bb03c9f0SGreg Roach 769599771eSGreg Roach // GEDCOM data (before any pending edits) 779599771eSGreg Roach protected string $gedcom; 78bb03c9f0SGreg Roach 799599771eSGreg Roach // GEDCOM data (after any pending edits) 80*1ff45046SGreg Roach protected string|null $pending; 81bb03c9f0SGreg Roach 829599771eSGreg Roach /** @var array<Fact> Facts extracted from $gedcom/$pending */ 839599771eSGreg Roach protected array $facts; 84bb03c9f0SGreg Roach 8509482a55SGreg Roach /** @var array<array<string>> All the names of this individual */ 869599771eSGreg Roach protected array $getAllNames = []; 87bb03c9f0SGreg Roach 8822e73debSGreg Roach /** @var int|null Cached result */ 89*1ff45046SGreg Roach private int|null $getPrimaryName = null; 90c4943cffSGreg Roach 9122e73debSGreg Roach /** @var int|null Cached result */ 92*1ff45046SGreg Roach private int|null $getSecondaryName = null; 93a25f0a04SGreg Roach 94a25f0a04SGreg Roach /** 95a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 96a25f0a04SGreg Roach * 97a25f0a04SGreg Roach * @param string $xref 98a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 99a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 100a25f0a04SGreg Roach * empty string for records with pending deletions 10124ec66ceSGreg Roach * @param Tree $tree 102a25f0a04SGreg Roach */ 103*1ff45046SGreg Roach public function __construct(string $xref, string $gedcom, string|null $pending, Tree $tree) 104c1010edaSGreg Roach { 105a25f0a04SGreg Roach $this->xref = $xref; 106a25f0a04SGreg Roach $this->gedcom = $gedcom; 107a25f0a04SGreg Roach $this->pending = $pending; 10824ec66ceSGreg Roach $this->tree = $tree; 1099599771eSGreg Roach $this->facts = $this->parseFacts(); 110a25f0a04SGreg Roach } 111a25f0a04SGreg Roach 112a25f0a04SGreg Roach /** 113886b77daSGreg Roach * A closure which will filter out private records. 114886b77daSGreg Roach * 115c6921a17SGreg Roach * @return Closure(GedcomRecord):bool 116886b77daSGreg Roach */ 1174146fabcSGreg Roach public static function accessFilter(): Closure 118886b77daSGreg Roach { 119f25fc0f9SGreg Roach return static fn(GedcomRecord $record): bool => $record->canShow(); 120886b77daSGreg Roach } 121886b77daSGreg Roach 122886b77daSGreg Roach /** 123c156e8f5SGreg Roach * A closure which will compare records by name. 124c156e8f5SGreg Roach * 125c6921a17SGreg Roach * @return Closure(GedcomRecord,GedcomRecord):int 126c156e8f5SGreg Roach */ 127c156e8f5SGreg Roach public static function nameComparator(): Closure 128c156e8f5SGreg Roach { 1296c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y): int { 130c156e8f5SGreg Roach if ($x->canShowName()) { 131c156e8f5SGreg Roach if ($y->canShowName()) { 13237646143SGreg Roach return I18N::comparator()($x->sortName(), $y->sortName()); 133c156e8f5SGreg Roach } 134c156e8f5SGreg Roach 135c156e8f5SGreg Roach return -1; // only $y is private 136c156e8f5SGreg Roach } 137c156e8f5SGreg Roach 138c156e8f5SGreg Roach if ($y->canShowName()) { 139c156e8f5SGreg Roach return 1; // only $x is private 140c156e8f5SGreg Roach } 141c156e8f5SGreg Roach 142c156e8f5SGreg Roach return 0; // both $x and $y private 143c156e8f5SGreg Roach }; 144c156e8f5SGreg Roach } 145c156e8f5SGreg Roach 146c156e8f5SGreg Roach /** 147c156e8f5SGreg Roach * A closure which will compare records by change time. 148c156e8f5SGreg Roach * 149c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 150c156e8f5SGreg Roach * 151c6921a17SGreg Roach * @return Closure(GedcomRecord,GedcomRecord):int 152c156e8f5SGreg Roach */ 153c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 154c156e8f5SGreg Roach { 155f25fc0f9SGreg Roach return static fn(GedcomRecord $x, GedcomRecord $y): int => $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp()); 156c156e8f5SGreg Roach } 157c156e8f5SGreg Roach 158c156e8f5SGreg Roach /** 15902467d32SGreg Roach * Get the GEDCOM tag for this record. 16002467d32SGreg Roach * 16102467d32SGreg Roach * @return string 16202467d32SGreg Roach */ 16302467d32SGreg Roach public function tag(): string 16402467d32SGreg Roach { 16502467d32SGreg Roach preg_match('/^0 @[^@]*@ (\w+)/', $this->gedcom(), $match); 16602467d32SGreg Roach 16702467d32SGreg Roach return $match[1] ?? static::RECORD_TYPE; 16802467d32SGreg Roach } 16902467d32SGreg Roach 17002467d32SGreg Roach /** 171a25f0a04SGreg Roach * Get the XREF for this record 172a25f0a04SGreg Roach * 173a25f0a04SGreg Roach * @return string 174a25f0a04SGreg Roach */ 175c0935879SGreg Roach public function xref(): string 176c1010edaSGreg Roach { 177a25f0a04SGreg Roach return $this->xref; 178a25f0a04SGreg Roach } 179a25f0a04SGreg Roach 180a25f0a04SGreg Roach /** 181000959d9SGreg Roach * Get the tree to which this record belongs 182000959d9SGreg Roach * 183000959d9SGreg Roach * @return Tree 184000959d9SGreg Roach */ 185f4afa648SGreg Roach public function tree(): Tree 186c1010edaSGreg Roach { 187518bbdc1SGreg Roach return $this->tree; 188000959d9SGreg Roach } 189000959d9SGreg Roach 190000959d9SGreg Roach /** 191a25f0a04SGreg Roach * Application code should access data via Fact objects. 192a25f0a04SGreg Roach * This function exists to support old code. 193a25f0a04SGreg Roach * 194a25f0a04SGreg Roach * @return string 195a25f0a04SGreg Roach */ 196e364afe4SGreg Roach public function gedcom(): string 197c1010edaSGreg Roach { 198b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 199a25f0a04SGreg Roach } 200a25f0a04SGreg Roach 201a25f0a04SGreg Roach /** 202a25f0a04SGreg Roach * Does this record have a pending change? 203a25f0a04SGreg Roach * 204cbc1590aSGreg Roach * @return bool 205a25f0a04SGreg Roach */ 2068f53f488SRico Sonntag public function isPendingAddition(): bool 207c1010edaSGreg Roach { 208a25f0a04SGreg Roach return $this->pending !== null; 209a25f0a04SGreg Roach } 210a25f0a04SGreg Roach 211a25f0a04SGreg Roach /** 212a25f0a04SGreg Roach * Does this record have a pending deletion? 213a25f0a04SGreg Roach * 214cbc1590aSGreg Roach * @return bool 215a25f0a04SGreg Roach */ 2168f53f488SRico Sonntag public function isPendingDeletion(): bool 217c1010edaSGreg Roach { 218a25f0a04SGreg Roach return $this->pending === ''; 219a25f0a04SGreg Roach } 220a25f0a04SGreg Roach 221a25f0a04SGreg Roach /** 222225e381fSGreg Roach * Generate a URL to this record. 223a25f0a04SGreg Roach * 224a25f0a04SGreg Roach * @return string 225a25f0a04SGreg Roach */ 2268f53f488SRico Sonntag public function url(): string 227c1010edaSGreg Roach { 228225e381fSGreg Roach return route(static::ROUTE_NAME, [ 229c0935879SGreg Roach 'xref' => $this->xref(), 230ee4364daSGreg Roach 'tree' => $this->tree->name(), 231194b0938SGreg Roach 'slug' => Registry::slugFactory()->make($this), 232225e381fSGreg Roach ]); 233a25f0a04SGreg Roach } 234a25f0a04SGreg Roach 235a25f0a04SGreg Roach /** 236a25f0a04SGreg Roach * Can the details of this record be shown? 237a25f0a04SGreg Roach * 238cbc1590aSGreg Roach * @param int|null $access_level 239a25f0a04SGreg Roach * 240cbc1590aSGreg Roach * @return bool 241a25f0a04SGreg Roach */ 2422c6f1bd5SGreg Roach public function canShow(int|null $access_level = null): bool 243c1010edaSGreg Roach { 2443529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 2454b9ff166SGreg Roach 246a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 247a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 248f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 249a25f0a04SGreg Roach return true; 250a25f0a04SGreg Roach } 251f0b9c048SGreg Roach 2527476e8a5SGreg Roach $cache_key = 'show-' . $this->xref . '-' . $this->tree->id() . '-' . $access_level; 253f0b9c048SGreg Roach 254f25fc0f9SGreg Roach return Registry::cache()->array()->remember($cache_key, fn() => $this->canShowRecord($access_level)); 255a25f0a04SGreg Roach } 256a25f0a04SGreg Roach 257a25f0a04SGreg Roach /** 258a25f0a04SGreg Roach * Can the name of this record be shown? 259a25f0a04SGreg Roach * 260cbc1590aSGreg Roach * @param int|null $access_level 261a25f0a04SGreg Roach * 262cbc1590aSGreg Roach * @return bool 263a25f0a04SGreg Roach */ 2642c6f1bd5SGreg Roach public function canShowName(int|null $access_level = null): bool 265c1010edaSGreg Roach { 266a25f0a04SGreg Roach return $this->canShow($access_level); 267a25f0a04SGreg Roach } 268a25f0a04SGreg Roach 269a25f0a04SGreg Roach /** 270a25f0a04SGreg Roach * Can we edit this record? 271a25f0a04SGreg Roach * 272cbc1590aSGreg Roach * @return bool 273a25f0a04SGreg Roach */ 2748f53f488SRico Sonntag public function canEdit(): bool 275c1010edaSGreg Roach { 2761450f098SGreg Roach if ($this->isPendingDeletion()) { 2771450f098SGreg Roach return false; 2781450f098SGreg Roach } 2791450f098SGreg Roach 2801450f098SGreg Roach if (Auth::isManager($this->tree)) { 2811450f098SGreg Roach return true; 2821450f098SGreg Roach } 2831450f098SGreg Roach 28455105892SGreg Roach $fact = $this->facts(['RESN'])->first(); 28555105892SGreg Roach $locked = $fact instanceof Fact && str_ends_with($fact->attribute('RESN'), RestrictionNotice::VALUE_LOCKED); 28655105892SGreg Roach 28755105892SGreg Roach return Auth::isEditor($this->tree) && !$locked; 288a25f0a04SGreg Roach } 289a25f0a04SGreg Roach 290a25f0a04SGreg Roach /** 291a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 292a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 293a25f0a04SGreg Roach * 294cbc1590aSGreg Roach * @param int $access_level 295a25f0a04SGreg Roach * 296a25f0a04SGreg Roach * @return string 297a25f0a04SGreg Roach */ 298e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 299c1010edaSGreg Roach { 300e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 301a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 302a25f0a04SGreg Roach return $this->gedcom; 303b2ce94c6SRico Sonntag } 304b2ce94c6SRico Sonntag 305b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 306a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 307a25f0a04SGreg Roach 308a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 309dc141db3SGreg Roach [$gedrec] = explode("\n", $this->gedcom . $this->pending, 2); 310a25f0a04SGreg Roach 311a25f0a04SGreg Roach // Check each of the facts for access 3128d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 313138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 314a25f0a04SGreg Roach } 315cbc1590aSGreg Roach 316a25f0a04SGreg Roach return $gedrec; 317b2ce94c6SRico Sonntag } 318b2ce94c6SRico Sonntag 319a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 320a25f0a04SGreg Roach // limited data, such as links to other records. 321a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 322a25f0a04SGreg Roach } 323a25f0a04SGreg Roach 324a25f0a04SGreg Roach /** 325a25f0a04SGreg Roach * Default for "other" object types 326c7ff4153SGreg Roach * 327c7ff4153SGreg Roach * @return void 328a25f0a04SGreg Roach */ 329e364afe4SGreg Roach public function extractNames(): void 330c1010edaSGreg Roach { 33176f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 332a25f0a04SGreg Roach } 333a25f0a04SGreg Roach 334a25f0a04SGreg Roach /** 335a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 336a25f0a04SGreg Roach * 337870ec5a3SGreg Roach * @return array<int,array<string,string>> 338a25f0a04SGreg Roach */ 3398f53f488SRico Sonntag public function getAllNames(): array 340c1010edaSGreg Roach { 3419599771eSGreg Roach if ($this->getAllNames === []) { 342a25f0a04SGreg Roach if ($this->canShowName()) { 343a25f0a04SGreg Roach // Ask the record to extract its names 344a25f0a04SGreg Roach $this->extractNames(); 345a25f0a04SGreg Roach // No name found? Use a fallback. 346ec124fd2SGreg Roach if ($this->getAllNames === []) { 347db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 348a25f0a04SGreg Roach } 349a25f0a04SGreg Roach } else { 350db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 351a25f0a04SGreg Roach } 352a25f0a04SGreg Roach } 353cbc1590aSGreg Roach 354bdb3725aSGreg Roach return $this->getAllNames; 355a25f0a04SGreg Roach } 356a25f0a04SGreg Roach 357a25f0a04SGreg Roach /** 358a25f0a04SGreg Roach * If this object has no name, what do we call it? 359a25f0a04SGreg Roach * 360a25f0a04SGreg Roach * @return string 361a25f0a04SGreg Roach */ 3628f53f488SRico Sonntag public function getFallBackName(): string 363c1010edaSGreg Roach { 364c0935879SGreg Roach return e($this->xref()); 365a25f0a04SGreg Roach } 366a25f0a04SGreg Roach 367a25f0a04SGreg Roach /** 368a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 369a25f0a04SGreg Roach * 370cbc1590aSGreg Roach * @return int 371a25f0a04SGreg Roach */ 3728f53f488SRico Sonntag public function getPrimaryName(): int 373c1010edaSGreg Roach { 374a25f0a04SGreg Roach static $language_script; 375a25f0a04SGreg Roach 376448d466cSGreg Roach $language_script ??= I18N::locale()->script()->code(); 377a25f0a04SGreg Roach 378bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 379a25f0a04SGreg Roach // Generally, the first name is the primary one.... 380bdb3725aSGreg Roach $this->getPrimaryName = 0; 381a25f0a04SGreg Roach // ...except when the language/name use different character sets 382a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 3839d15b78eSGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 384bdb3725aSGreg Roach $this->getPrimaryName = $n; 385a25f0a04SGreg Roach break; 386a25f0a04SGreg Roach } 387a25f0a04SGreg Roach } 388a25f0a04SGreg Roach } 389a25f0a04SGreg Roach 390bdb3725aSGreg Roach return $this->getPrimaryName; 391a25f0a04SGreg Roach } 392a25f0a04SGreg Roach 393a25f0a04SGreg Roach /** 394a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 395a25f0a04SGreg Roach * 396cbc1590aSGreg Roach * @return int 397a25f0a04SGreg Roach */ 3988f53f488SRico Sonntag public function getSecondaryName(): int 399c1010edaSGreg Roach { 4008f038c36SRico Sonntag if ($this->getSecondaryName === null) { 401a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 402bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 403a25f0a04SGreg Roach // ....except when there are names with different character sets 404a25f0a04SGreg Roach $all_names = $this->getAllNames(); 405a25f0a04SGreg Roach if (count($all_names) > 1) { 406a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 407a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 408e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 409bdb3725aSGreg Roach $this->getSecondaryName = $n; 410a25f0a04SGreg Roach break; 411a25f0a04SGreg Roach } 412a25f0a04SGreg Roach } 413a25f0a04SGreg Roach } 414a25f0a04SGreg Roach } 415cbc1590aSGreg Roach 416bdb3725aSGreg Roach return $this->getSecondaryName; 417a25f0a04SGreg Roach } 418a25f0a04SGreg Roach 419a25f0a04SGreg Roach /** 420a2c8afeaSAlejandro Criado-Pérez * Allow the choice of primary name to be overridden, e.g. in a search result 421a25f0a04SGreg Roach * 42276f666f4SGreg Roach * @param int|null $n 4237e96c925SGreg Roach * 4247e96c925SGreg Roach * @return void 425a25f0a04SGreg Roach */ 4262c6f1bd5SGreg Roach public function setPrimaryName(int|null $n = null): void 427c1010edaSGreg Roach { 428bdb3725aSGreg Roach $this->getPrimaryName = $n; 429bdb3725aSGreg Roach $this->getSecondaryName = null; 430a25f0a04SGreg Roach } 431a25f0a04SGreg Roach 432a25f0a04SGreg Roach /** 433a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 434a25f0a04SGreg Roach * 435a25f0a04SGreg Roach * @return string 436a25f0a04SGreg Roach */ 43724f2a3afSGreg Roach public function __toString(): string 438c1010edaSGreg Roach { 43972cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 440a25f0a04SGreg Roach } 441a25f0a04SGreg Roach 442a25f0a04SGreg Roach /** 443c156e8f5SGreg Roach * /** 444a25f0a04SGreg Roach * Get variants of the name 445a25f0a04SGreg Roach * 446a25f0a04SGreg Roach * @return string 447a25f0a04SGreg Roach */ 448e364afe4SGreg Roach public function fullName(): string 449c1010edaSGreg Roach { 450a25f0a04SGreg Roach if ($this->canShowName()) { 451a25f0a04SGreg Roach $tmp = $this->getAllNames(); 452cbc1590aSGreg Roach 453a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 454a25f0a04SGreg Roach } 455b2ce94c6SRico Sonntag 456b2ce94c6SRico Sonntag return I18N::translate('Private'); 457a25f0a04SGreg Roach } 458a25f0a04SGreg Roach 459a25f0a04SGreg Roach /** 460a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 461a25f0a04SGreg Roach * 462a25f0a04SGreg Roach * @return string 463a25f0a04SGreg Roach */ 46439ca88baSGreg Roach public function sortName(): string 465c1010edaSGreg Roach { 466a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 467a25f0a04SGreg Roach $tmp = $this->getAllNames(); 468cbc1590aSGreg Roach 469a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 470a25f0a04SGreg Roach } 471a25f0a04SGreg Roach 472a25f0a04SGreg Roach /** 473a25f0a04SGreg Roach * Get the full name in an alternative character set 474a25f0a04SGreg Roach * 475e364afe4SGreg Roach * @return string|null 476a25f0a04SGreg Roach */ 477*1ff45046SGreg Roach public function alternateName(): string|null 478c1010edaSGreg Roach { 479e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 480a25f0a04SGreg Roach $all_names = $this->getAllNames(); 481cbc1590aSGreg Roach 482a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 483a25f0a04SGreg Roach } 484b2ce94c6SRico Sonntag 485b2ce94c6SRico Sonntag return null; 486a25f0a04SGreg Roach } 487a25f0a04SGreg Roach 488a25f0a04SGreg Roach /** 489a25f0a04SGreg Roach * Format this object for display in a list 490a25f0a04SGreg Roach * 491a25f0a04SGreg Roach * @return string 492a25f0a04SGreg Roach */ 4938f53f488SRico Sonntag public function formatList(): string 494c1010edaSGreg Roach { 495a79e52aaSGreg Roach $html = '<a href="' . e($this->url()) . '">'; 49639ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 497b165e17cSGreg Roach $html .= '</a>'; 498a79e52aaSGreg Roach $html .= $this->formatListDetails(); 499cbc1590aSGreg Roach 500a25f0a04SGreg Roach return $html; 501a25f0a04SGreg Roach } 502a25f0a04SGreg Roach 503a25f0a04SGreg Roach /** 504a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 505a25f0a04SGreg Roach * identifying characteristics of this record. 506a25f0a04SGreg Roach * 507a25f0a04SGreg Roach * @return string 508a25f0a04SGreg Roach */ 5098f53f488SRico Sonntag public function formatListDetails(): string 510c1010edaSGreg Roach { 511a25f0a04SGreg Roach return ''; 512a25f0a04SGreg Roach } 513a25f0a04SGreg Roach 514a25f0a04SGreg Roach /** 515a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 516a25f0a04SGreg Roach * 51709482a55SGreg Roach * @param array<string> $facts 518cbc1590aSGreg Roach * @param int $style 519a25f0a04SGreg Roach * 520a25f0a04SGreg Roach * @return string 521a25f0a04SGreg Roach */ 5228d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 523c1010edaSGreg Roach { 524a79e52aaSGreg Roach $fact = $this->facts($facts, true)->first(); 525a79e52aaSGreg Roach 526a79e52aaSGreg Roach if ($fact === null) { 527a79e52aaSGreg Roach return ''; 528a25f0a04SGreg Roach } 529cbc1590aSGreg Roach 530a79e52aaSGreg Roach // Only display if it has a date or place (or both) 531a79e52aaSGreg Roach $attributes = []; 532a79e52aaSGreg Roach 533a79e52aaSGreg Roach if ($fact->date()->isOK()) { 534a79e52aaSGreg Roach $attributes[] = view('fact-date', ['cal_link' => 'false', 'fact' => $fact, 'record' => $fact->record(), 'time' => false]); 535a79e52aaSGreg Roach } 536a79e52aaSGreg Roach 537a79e52aaSGreg Roach if ($fact->place()->gedcomName() !== '' && $style === 2) { 538a79e52aaSGreg Roach $attributes[] = $fact->place()->shortName(); 539a79e52aaSGreg Roach } 540a79e52aaSGreg Roach 541a79e52aaSGreg Roach if ($attributes === []) { 542a25f0a04SGreg Roach return ''; 543a25f0a04SGreg Roach } 544a25f0a04SGreg Roach 545a79e52aaSGreg Roach return '<div><em>' . I18N::translate('%1$s: %2$s', $fact->label(), implode(' — ', $attributes)) . '</em></div>'; 546a79e52aaSGreg Roach } 547a79e52aaSGreg Roach 548a25f0a04SGreg Roach /** 549a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 550a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 551a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 552a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 553a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 554a25f0a04SGreg Roach * 55509482a55SGreg Roach * @param array<string> $events 556a25f0a04SGreg Roach * 55709482a55SGreg Roach * @return array<Date> 558a25f0a04SGreg Roach */ 5598d0ebef0SGreg Roach public function getAllEventDates(array $events): array 560c1010edaSGreg Roach { 56113abd6f3SGreg Roach $dates = []; 5621385b8caSGreg Roach foreach ($this->facts($events, false, null, true) as $event) { 5632decada7SGreg Roach if ($event->date()->isOK()) { 5642decada7SGreg Roach $dates[] = $event->date(); 565a25f0a04SGreg Roach } 566a25f0a04SGreg Roach } 567a25f0a04SGreg Roach 568a25f0a04SGreg Roach return $dates; 569a25f0a04SGreg Roach } 570a25f0a04SGreg Roach 571a25f0a04SGreg Roach /** 572a25f0a04SGreg Roach * Get all the places for a particular type of event 573a25f0a04SGreg Roach * 57409482a55SGreg Roach * @param array<string> $events 575a25f0a04SGreg Roach * 57609482a55SGreg Roach * @return array<Place> 577a25f0a04SGreg Roach */ 5788d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 579c1010edaSGreg Roach { 58013abd6f3SGreg Roach $places = []; 5818d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 582138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 583a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 58416d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 585a25f0a04SGreg Roach } 586a25f0a04SGreg Roach } 587a25f0a04SGreg Roach } 588a25f0a04SGreg Roach 589a25f0a04SGreg Roach return $places; 590a25f0a04SGreg Roach } 591a25f0a04SGreg Roach 592a25f0a04SGreg Roach /** 593a25f0a04SGreg Roach * The facts and events for this record. 594a25f0a04SGreg Roach * 59509482a55SGreg Roach * @param array<string> $filter 596cbc1590aSGreg Roach * @param bool $sort 597cbc1590aSGreg Roach * @param int|null $access_level 598ce42304aSGreg Roach * @param bool $ignore_deleted 599a25f0a04SGreg Roach * 60036779af1SGreg Roach * @return Collection<int,Fact> 601a25f0a04SGreg Roach */ 602ce42304aSGreg Roach public function facts( 603ce42304aSGreg Roach array $filter = [], 604ce42304aSGreg Roach bool $sort = false, 6052c6f1bd5SGreg Roach int|null $access_level = null, 606ce42304aSGreg Roach bool $ignore_deleted = false 607ce42304aSGreg Roach ): Collection { 6083529c469SGreg Roach $access_level ??= Auth::accessLevel($this->tree); 6094b9ff166SGreg Roach 610d0889c63SGreg Roach // Convert BIRT into INDI:BIRT, etc. 611d0889c63SGreg Roach $filter = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, $filter); 612d0889c63SGreg Roach 6138af3e5c1SGreg Roach $facts = new Collection(); 614ce42304aSGreg Roach if ($this->canShow($access_level)) { 615a25f0a04SGreg Roach foreach ($this->facts as $fact) { 616d0889c63SGreg Roach if (($filter === [] || in_array($fact->tag(), $filter, true)) && $fact->canShow($access_level)) { 6178af3e5c1SGreg Roach $facts->push($fact); 618a25f0a04SGreg Roach } 619a25f0a04SGreg Roach } 620a25f0a04SGreg Roach } 621d17d7b9eSGreg Roach 622a25f0a04SGreg Roach if ($sort) { 6230f5fd22fSGreg Roach switch ($this->tag()) { 6240f5fd22fSGreg Roach case Family::RECORD_TYPE: 6250f5fd22fSGreg Roach case Individual::RECORD_TYPE: 626580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 6270f5fd22fSGreg Roach break; 6280f5fd22fSGreg Roach 6290f5fd22fSGreg Roach default: 6300f5fd22fSGreg Roach $subtags = Registry::elementFactory()->make($this->tag())->subtags(); 6310f5fd22fSGreg Roach $subtags = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, array_keys($subtags)); 6321259996fSGreg Roach 6331259996fSGreg Roach if ($subtags !== []) { 6341259996fSGreg Roach // Renumber keys from 1. 6350f5fd22fSGreg Roach $subtags = array_combine(range(1, count($subtags)), $subtags); 6361259996fSGreg Roach } 6371259996fSGreg Roach 6380f5fd22fSGreg Roach 6390f5fd22fSGreg Roach $facts = $facts 6400f5fd22fSGreg Roach ->sort(static function (Fact $x, Fact $y) use ($subtags): int { 6410f5fd22fSGreg Roach $sort_x = array_search($x->tag(), $subtags, true) ?: PHP_INT_MAX; 6420f5fd22fSGreg Roach $sort_y = array_search($y->tag(), $subtags, true) ?: PHP_INT_MAX; 6430f5fd22fSGreg Roach 6440f5fd22fSGreg Roach return $sort_x <=> $sort_y; 6450f5fd22fSGreg Roach }); 6460f5fd22fSGreg Roach break; 6470f5fd22fSGreg Roach } 648a25f0a04SGreg Roach } 649cbc1590aSGreg Roach 650ce42304aSGreg Roach if ($ignore_deleted) { 651f25fc0f9SGreg Roach $facts = $facts->filter(static fn(Fact $fact): bool => !$fact->isPendingDeletion()); 652ce42304aSGreg Roach } 653ce42304aSGreg Roach 6545fef3bfaSGreg Roach return $facts; 655a25f0a04SGreg Roach } 656a25f0a04SGreg Roach 657a25f0a04SGreg Roach /** 6580f5fd22fSGreg Roach * @return array<string,string> 6590f5fd22fSGreg Roach */ 6600f5fd22fSGreg Roach public function missingFacts(): array 6610f5fd22fSGreg Roach { 6620f5fd22fSGreg Roach $missing_facts = []; 6630f5fd22fSGreg Roach 6640f5fd22fSGreg Roach foreach (Registry::elementFactory()->make($this->tag())->subtags() as $subtag => $repeat) { 6650f5fd22fSGreg Roach [, $max] = explode(':', $repeat); 6660f5fd22fSGreg Roach $max = $max === 'M' ? PHP_INT_MAX : (int) $max; 6670f5fd22fSGreg Roach 66898f93f3aSGreg Roach if ($this->facts([$subtag], false, null, true)->count() < $max) { 6690f5fd22fSGreg Roach $missing_facts[$subtag] = $subtag; 6700f5fd22fSGreg Roach $missing_facts[$subtag] = Registry::elementFactory()->make($this->tag() . ':' . $subtag)->label(); 6710f5fd22fSGreg Roach } 6720f5fd22fSGreg Roach } 6730f5fd22fSGreg Roach 6740f5fd22fSGreg Roach uasort($missing_facts, I18N::comparator()); 6750f5fd22fSGreg Roach 6765416d6ddSGreg Roach if (!Auth::canUploadMedia($this->tree, Auth::user())) { 6770f5fd22fSGreg Roach unset($missing_facts['OBJE']); 6780f5fd22fSGreg Roach } 6790f5fd22fSGreg Roach 6800f5fd22fSGreg Roach // We have special code for this. 6810f5fd22fSGreg Roach unset($missing_facts['FILE']); 6820f5fd22fSGreg Roach 6830f5fd22fSGreg Roach return $missing_facts; 6840f5fd22fSGreg Roach } 6850f5fd22fSGreg Roach 6860f5fd22fSGreg Roach /** 6874459dc9aSGreg Roach * Get the last-change timestamp for this record 688a25f0a04SGreg Roach * 68976d39c55SGreg Roach * @return TimestampInterface 690a25f0a04SGreg Roach */ 69176d39c55SGreg Roach public function lastChangeTimestamp(): TimestampInterface 692c1010edaSGreg Roach { 693820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 694a25f0a04SGreg Roach 6954459dc9aSGreg Roach if ($chan instanceof Fact) { 69603f2cc61SGreg Roach // The record has a CHAN event. 69786c727c1SGreg Roach $date = $chan->date()->minimumDate(); 69886c727c1SGreg Roach $ymd = sprintf('%04d-%02d-%02d', $date->year(), $date->month(), $date->day()); 6994459dc9aSGreg Roach 70086c727c1SGreg Roach if ($ymd !== '') { 70103f2cc61SGreg Roach // The CHAN event has a valid DATE. 70286c727c1SGreg Roach if (preg_match('/\n3 TIME (([01]\d|2[0-3]):([0-5]\d):([0-5]\d))/', $chan->gedcom(), $match) === 1) { 70386c727c1SGreg Roach return Registry::timestampFactory()->fromString($ymd . $match[1], 'Y-m-d H:i:s'); 704e364afe4SGreg Roach } 705e364afe4SGreg Roach 70686c727c1SGreg Roach if (preg_match('/\n3 TIME (([01]\d|2[0-3]):([0-5]\d))/', $chan->gedcom(), $match) === 1) { 70786c727c1SGreg Roach return Registry::timestampFactory()->fromString($ymd . $match[1], 'Y-m-d H:i'); 708b2ce94c6SRico Sonntag } 709b2ce94c6SRico Sonntag 71086c727c1SGreg Roach return Registry::timestampFactory()->fromString($ymd, 'Y-m-d'); 711a25f0a04SGreg Roach } 71203f2cc61SGreg Roach } 713b2ce94c6SRico Sonntag 714a25f0a04SGreg Roach // The record does not have a CHAN event 715d97083feSGreg Roach return Registry::timestampFactory()->make(0); 716a25f0a04SGreg Roach } 717a25f0a04SGreg Roach 718a25f0a04SGreg Roach /** 719a25f0a04SGreg Roach * Get the last-change user for this record 720a25f0a04SGreg Roach * 721a25f0a04SGreg Roach * @return string 722a25f0a04SGreg Roach */ 723e364afe4SGreg Roach public function lastChangeUser(): string 724c1010edaSGreg Roach { 725820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 726a25f0a04SGreg Roach 727a25f0a04SGreg Roach if ($chan === null) { 728a25f0a04SGreg Roach return I18N::translate('Unknown'); 729b2ce94c6SRico Sonntag } 730b2ce94c6SRico Sonntag 7313425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 732baacc364SGreg Roach if ($chan_user === '') { 733a25f0a04SGreg Roach return I18N::translate('Unknown'); 734b2ce94c6SRico Sonntag } 735b2ce94c6SRico Sonntag 736a25f0a04SGreg Roach return $chan_user; 737a25f0a04SGreg Roach } 738a25f0a04SGreg Roach 739a25f0a04SGreg Roach /** 740a25f0a04SGreg Roach * Add a new fact to this record 741a25f0a04SGreg Roach * 742a25f0a04SGreg Roach * @param string $gedcom 743cbc1590aSGreg Roach * @param bool $update_chan 7447e96c925SGreg Roach * 7457e96c925SGreg Roach * @return void 746a25f0a04SGreg Roach */ 747e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 748c1010edaSGreg Roach { 749fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 750a25f0a04SGreg Roach } 751a25f0a04SGreg Roach 752a25f0a04SGreg Roach /** 753a25f0a04SGreg Roach * Delete a fact from this record 754a25f0a04SGreg Roach * 755a25f0a04SGreg Roach * @param string $fact_id 756cbc1590aSGreg Roach * @param bool $update_chan 7577e96c925SGreg Roach * 7587e96c925SGreg Roach * @return void 759a25f0a04SGreg Roach */ 760e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 761c1010edaSGreg Roach { 762db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 763a25f0a04SGreg Roach } 764a25f0a04SGreg Roach 765a25f0a04SGreg Roach /** 766a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 767a25f0a04SGreg Roach * 768a25f0a04SGreg Roach * @param string $fact_id 769a25f0a04SGreg Roach * @param string $gedcom 770cbc1590aSGreg Roach * @param bool $update_chan 771a25f0a04SGreg Roach * 7727e96c925SGreg Roach * @return void 7737e96c925SGreg Roach * @throws Exception 774a25f0a04SGreg Roach */ 775e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 776c1010edaSGreg Roach { 77771f696adSGreg Roach // Not all record types allow a CHAN event. 77871f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 77971f696adSGreg Roach 780a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 781a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 782a25f0a04SGreg Roach $gedcom = trim($gedcom); 783a25f0a04SGreg Roach 784a25f0a04SGreg Roach if ($this->pending === '') { 7857e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 786a25f0a04SGreg Roach } 7878d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 7887e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 789a25f0a04SGreg Roach } 790a25f0a04SGreg Roach 791b6ec1ccfSGreg Roach if ($this->pending !== null && $this->pending !== '') { 792a25f0a04SGreg Roach $old_gedcom = $this->pending; 793a25f0a04SGreg Roach } else { 794a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 795a25f0a04SGreg Roach } 796a25f0a04SGreg Roach 797a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 79865e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 799a25f0a04SGreg Roach 800a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 80119aed3a1SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE, true) as $fact) { 802905ab80aSGreg Roach if ($fact->id() === $fact_id) { 803db7bb364SGreg Roach if ($gedcom !== '') { 804a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 805a25f0a04SGreg Roach } 806fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 80772f8afdbSGreg Roach } elseif ($update_chan && str_ends_with($fact->tag(), ':CHAN')) { 80872f8afdbSGreg Roach $new_gedcom .= "\n" . $this->updateChange($fact->gedcom()); 80972f8afdbSGreg Roach } else { 810138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 811a25f0a04SGreg Roach } 812a25f0a04SGreg Roach } 813a25f0a04SGreg Roach 814a25f0a04SGreg Roach // Adding a new fact 815fc3ccce4SGreg Roach if ($fact_id === '') { 816a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 817a25f0a04SGreg Roach } 818a25f0a04SGreg Roach 819dec352c1SGreg Roach if ($update_chan && !str_contains($new_gedcom, "\n1 CHAN")) { 82072f8afdbSGreg Roach $new_gedcom .= $this->updateChange("\n1 CHAN"); 82119aed3a1SGreg Roach } 82219aed3a1SGreg Roach 823e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 824a25f0a04SGreg Roach // Save the changes 825d09b6323SGreg Roach DB::table('change')->insert([ 826d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 827d09b6323SGreg Roach 'xref' => $this->xref, 828d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 829d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 8305ae3edd9SGreg Roach 'status' => 'pending', 831d09b6323SGreg Roach 'user_id' => Auth::id(), 83213abd6f3SGreg Roach ]); 833a25f0a04SGreg Roach 834a25f0a04SGreg Roach $this->pending = $new_gedcom; 835a25f0a04SGreg Roach 8361fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 837d35568b4SGreg Roach $pending_changes_service = Registry::container()->get(PendingChangesService::class); 838b55cbc6bSGreg Roach 839b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 840a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 841a25f0a04SGreg Roach $this->pending = null; 842a25f0a04SGreg Roach } 843a25f0a04SGreg Roach } 8449599771eSGreg Roach 8459599771eSGreg Roach $this->facts = $this->parseFacts(); 846a25f0a04SGreg Roach } 847a25f0a04SGreg Roach 848a25f0a04SGreg Roach /** 849a25f0a04SGreg Roach * Update this record 850a25f0a04SGreg Roach * 851a25f0a04SGreg Roach * @param string $gedcom 852cbc1590aSGreg Roach * @param bool $update_chan 8537e96c925SGreg Roach * 8547e96c925SGreg Roach * @return void 855a25f0a04SGreg Roach */ 856e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 857c1010edaSGreg Roach { 85871f696adSGreg Roach // Not all record types allow a CHAN event. 85971f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 86071f696adSGreg Roach 861a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 862a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 863a25f0a04SGreg Roach $gedcom = trim($gedcom); 864a25f0a04SGreg Roach 865a25f0a04SGreg Roach // Update the CHAN record 866a25f0a04SGreg Roach if ($update_chan) { 86772f8afdbSGreg Roach if (preg_match('/\n1 CHAN(\n[2-9].*)*/', $gedcom, $match)) { 86872f8afdbSGreg Roach $gedcom = strtr($gedcom, [$match[0] => $this->updateChange($match[0])]); 86972f8afdbSGreg Roach } else { 87072f8afdbSGreg Roach $gedcom .= $this->updateChange("\n1 CHAN"); 87172f8afdbSGreg Roach } 872a25f0a04SGreg Roach } 873a25f0a04SGreg Roach 874a25f0a04SGreg Roach // Create a pending change 875d09b6323SGreg Roach DB::table('change')->insert([ 876d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 877d09b6323SGreg Roach 'xref' => $this->xref, 878d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 879d09b6323SGreg Roach 'new_gedcom' => $gedcom, 880665d37b1SGreg Roach 'status' => 'pending', 881d09b6323SGreg Roach 'user_id' => Auth::id(), 88213abd6f3SGreg Roach ]); 883a25f0a04SGreg Roach 884a25f0a04SGreg Roach // Clear the cache 885a25f0a04SGreg Roach $this->pending = $gedcom; 886a25f0a04SGreg Roach 887a25f0a04SGreg Roach // Accept this pending change 8881fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 889d35568b4SGreg Roach $pending_changes_service = Registry::container()->get(PendingChangesService::class); 890b55cbc6bSGreg Roach 891b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 892a25f0a04SGreg Roach $this->gedcom = $gedcom; 893a25f0a04SGreg Roach $this->pending = null; 894a25f0a04SGreg Roach } 895a25f0a04SGreg Roach 8969599771eSGreg Roach $this->facts = $this->parseFacts(); 897a25f0a04SGreg Roach 898847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 899a25f0a04SGreg Roach } 900a25f0a04SGreg Roach 901a25f0a04SGreg Roach /** 902a25f0a04SGreg Roach * Delete this record 903b874da82SGreg Roach * 904b874da82SGreg Roach * @return void 905a25f0a04SGreg Roach */ 906e364afe4SGreg Roach public function deleteRecord(): void 907c1010edaSGreg Roach { 908a25f0a04SGreg Roach // Create a pending change 9094c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 910d09b6323SGreg Roach DB::table('change')->insert([ 911d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 912d09b6323SGreg Roach 'xref' => $this->xref, 913d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 914d09b6323SGreg Roach 'new_gedcom' => '', 915665d37b1SGreg Roach 'status' => 'pending', 916d09b6323SGreg Roach 'user_id' => Auth::id(), 91713abd6f3SGreg Roach ]); 9184c0b5256SGreg Roach } 919a25f0a04SGreg Roach 9204c0b5256SGreg Roach // Auto-accept this pending change 9211fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 922d35568b4SGreg Roach $pending_changes_service = Registry::container()->get(PendingChangesService::class); 923b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 924a25f0a04SGreg Roach } 925a25f0a04SGreg Roach 926847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 927a25f0a04SGreg Roach } 928a25f0a04SGreg Roach 929a25f0a04SGreg Roach /** 930a25f0a04SGreg Roach * Remove all links from this record to $xref 931a25f0a04SGreg Roach * 932a25f0a04SGreg Roach * @param string $xref 933cbc1590aSGreg Roach * @param bool $update_chan 9347e96c925SGreg Roach * 9357e96c925SGreg Roach * @return void 936a25f0a04SGreg Roach */ 937e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 938c1010edaSGreg Roach { 939a25f0a04SGreg Roach $value = '@' . $xref . '@'; 940a25f0a04SGreg Roach 94130158ae7SGreg Roach foreach ($this->facts() as $fact) { 94284586c02SGreg Roach if ($fact->value() === $value) { 943905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 9448d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 945138ca96cSGreg Roach $gedcom = $fact->gedcom(); 946a25f0a04SGreg Roach foreach ($matches as $match) { 947ef475b14SGreg Roach $next_level = 1 + (int) $match[1]; 948a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 949a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 950a25f0a04SGreg Roach } 951905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 952a25f0a04SGreg Roach } 953a25f0a04SGreg Roach } 954a25f0a04SGreg Roach } 955bf4eb542SGreg Roach 956bf4eb542SGreg Roach /** 95722e73debSGreg Roach * Each object type may have its own special rules, and re-implement this function. 95822e73debSGreg Roach * 95922e73debSGreg Roach * @param int $access_level 96022e73debSGreg Roach * 96122e73debSGreg Roach * @return bool 96222e73debSGreg Roach */ 96322e73debSGreg Roach protected function canShowByType(int $access_level): bool 96422e73debSGreg Roach { 96522e73debSGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 96622e73debSGreg Roach 96722e73debSGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 96822e73debSGreg Roach // Restriction found 96922e73debSGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 97022e73debSGreg Roach } 97122e73debSGreg Roach 97222e73debSGreg Roach // No restriction found - must be public: 97322e73debSGreg Roach return true; 97422e73debSGreg Roach } 97522e73debSGreg Roach 97622e73debSGreg Roach /** 97722e73debSGreg Roach * Generate a private version of this record 97822e73debSGreg Roach * 97922e73debSGreg Roach * @param int $access_level 98022e73debSGreg Roach * 98122e73debSGreg Roach * @return string 98222e73debSGreg Roach */ 98322e73debSGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 98422e73debSGreg Roach { 985228ede81SGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE; 98622e73debSGreg Roach } 98722e73debSGreg Roach 98822e73debSGreg Roach /** 98922e73debSGreg Roach * Convert a name record into sortable and full/display versions. This default 99022e73debSGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 99122e73debSGreg Roach * 99222e73debSGreg Roach * @param string $type 99322e73debSGreg Roach * @param string $value 99422e73debSGreg Roach * @param string $gedcom 99522e73debSGreg Roach * 99622e73debSGreg Roach * @return void 99722e73debSGreg Roach */ 99822e73debSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 99922e73debSGreg Roach { 100022e73debSGreg Roach $this->getAllNames[] = [ 100122e73debSGreg Roach 'type' => $type, 1002f25fc0f9SGreg Roach 'sort' => preg_replace_callback('/(\d+)/', static fn(array $matches): string => str_pad($matches[0], 10, '0', STR_PAD_LEFT), $value), 1003315eb316SGreg Roach 'full' => '<bdi>' . e($value) . '</bdi>', 100422e73debSGreg Roach // This is used for display 100522e73debSGreg Roach 'fullNN' => $value, 100622e73debSGreg Roach // This goes into the database 100722e73debSGreg Roach ]; 100822e73debSGreg Roach } 100922e73debSGreg Roach 101022e73debSGreg Roach /** 101122e73debSGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 101222e73debSGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 101322e73debSGreg Roach * Parameters: the level 1 fact containing the name. 101422e73debSGreg Roach * Return value: an array of name structures, each containing 101522e73debSGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 101622e73debSGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 101722e73debSGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 101822e73debSGreg Roach * 101922e73debSGreg Roach * @param int $level 102022e73debSGreg Roach * @param string $fact_type 102136779af1SGreg Roach * @param Collection<int,Fact> $facts 102222e73debSGreg Roach * 102322e73debSGreg Roach * @return void 102422e73debSGreg Roach */ 102522e73debSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 102622e73debSGreg Roach { 102722e73debSGreg Roach $sublevel = $level + 1; 102822e73debSGreg Roach $subsublevel = $sublevel + 1; 102922e73debSGreg Roach foreach ($facts as $fact) { 1030c8ff5fdcSGreg Roach if (preg_match_all('/^' . $level . ' (' . $fact_type . ') (.+)((\n[' . $sublevel . '-9].+)*)/m', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 103122e73debSGreg Roach foreach ($matches as $match) { 103222e73debSGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 1033cc9d82dcSGreg Roach if ($match[1] === 'NAME' && str_contains(strtoupper($match[3]), "\n2 TYPE MARRIED")) { 103422e73debSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 103522e73debSGreg Roach } else { 103622e73debSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 103722e73debSGreg Roach } 1038c8ff5fdcSGreg Roach if ($match[3] && preg_match_all('/^' . $sublevel . ' (ROMN|FONE|_\w+) (.+)((\n[' . $subsublevel . '-9].+)*)/m', $match[3], $submatches, PREG_SET_ORDER)) { 103922e73debSGreg Roach foreach ($submatches as $submatch) { 10402d21e9c2SGreg Roach if ($submatch[1] !== '_RUFNAME') { 104122e73debSGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 104222e73debSGreg Roach } 104322e73debSGreg Roach } 104422e73debSGreg Roach } 104522e73debSGreg Roach } 104622e73debSGreg Roach } 104722e73debSGreg Roach } 10482d21e9c2SGreg Roach } 104922e73debSGreg Roach 105022e73debSGreg Roach /** 105122e73debSGreg Roach * Split the record into facts 105222e73debSGreg Roach * 10539599771eSGreg Roach * @return array<Fact> 105422e73debSGreg Roach */ 10559599771eSGreg Roach private function parseFacts(): array 105622e73debSGreg Roach { 105722e73debSGreg Roach // Split the record into facts 1058b6ec1ccfSGreg Roach if ($this->gedcom !== '') { 1059d823340dSGreg Roach $gedcom_facts = preg_split('/\n(?=1)/', $this->gedcom); 106022e73debSGreg Roach array_shift($gedcom_facts); 106122e73debSGreg Roach } else { 106222e73debSGreg Roach $gedcom_facts = []; 106322e73debSGreg Roach } 1064b6ec1ccfSGreg Roach if ($this->pending !== null && $this->pending !== '') { 1065d823340dSGreg Roach $pending_facts = preg_split('/\n(?=1)/', $this->pending); 106622e73debSGreg Roach array_shift($pending_facts); 106722e73debSGreg Roach } else { 106822e73debSGreg Roach $pending_facts = []; 106922e73debSGreg Roach } 107022e73debSGreg Roach 10719599771eSGreg Roach $facts = []; 107222e73debSGreg Roach 107322e73debSGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 107422e73debSGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 107522e73debSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 107622e73debSGreg Roach $fact->setPendingDeletion(); 107722e73debSGreg Roach } 10789599771eSGreg Roach $facts[] = $fact; 107922e73debSGreg Roach } 108022e73debSGreg Roach foreach ($pending_facts as $pending_fact) { 108122e73debSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 108222e73debSGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 108322e73debSGreg Roach $fact->setPendingAddition(); 10849599771eSGreg Roach $facts[] = $fact; 108522e73debSGreg Roach } 108622e73debSGreg Roach } 10879599771eSGreg Roach 10889599771eSGreg Roach return $facts; 108922e73debSGreg Roach } 109022e73debSGreg Roach 109122e73debSGreg Roach /** 109222e73debSGreg Roach * Work out whether this record can be shown to a user with a given access level 109322e73debSGreg Roach * 109422e73debSGreg Roach * @param int $access_level 109522e73debSGreg Roach * 109622e73debSGreg Roach * @return bool 109722e73debSGreg Roach */ 109822e73debSGreg Roach private function canShowRecord(int $access_level): bool 109922e73debSGreg Roach { 110022e73debSGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 110122e73debSGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 110222e73debSGreg Roach return true; 110322e73debSGreg Roach } 110422e73debSGreg Roach 110522e73debSGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 11061fe542e9SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) { 110722e73debSGreg Roach return true; 110822e73debSGreg Roach } 110922e73debSGreg Roach 111055105892SGreg Roach // Does this record have a restriction notice? 1111da7adf56SGreg Roach // Cannot use $this->>fact(), as that function calls this one. 111255105892SGreg Roach if (preg_match('/\n1 RESN (.+)/', $this->gedcom(), $match)) { 111355105892SGreg Roach $element = new RestrictionNotice(''); 111455105892SGreg Roach $restriction = $element->canonical($match[1]); 111555105892SGreg Roach 111655105892SGreg Roach if (str_starts_with($restriction, RestrictionNotice::VALUE_CONFIDENTIAL)) { 111722e73debSGreg Roach return Auth::PRIV_NONE >= $access_level; 111822e73debSGreg Roach } 111955105892SGreg Roach if (str_starts_with($restriction, RestrictionNotice::VALUE_PRIVACY)) { 112022e73debSGreg Roach return Auth::PRIV_USER >= $access_level; 112122e73debSGreg Roach } 112255105892SGreg Roach if (str_starts_with($restriction, RestrictionNotice::VALUE_NONE)) { 112322e73debSGreg Roach return true; 112422e73debSGreg Roach } 112555105892SGreg Roach } 112622e73debSGreg Roach 112722e73debSGreg Roach // Does this record have a default RESN? 112822e73debSGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 112922e73debSGreg Roach if (isset($individual_privacy[$this->xref()])) { 113022e73debSGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 113122e73debSGreg Roach } 113222e73debSGreg Roach 113322e73debSGreg Roach // Privacy rules do not apply to admins 113422e73debSGreg Roach if (Auth::PRIV_NONE >= $access_level) { 113522e73debSGreg Roach return true; 113622e73debSGreg Roach } 113722e73debSGreg Roach 113822e73debSGreg Roach // Different types of record have different privacy rules 113922e73debSGreg Roach return $this->canShowByType($access_level); 114022e73debSGreg Roach } 11418091bfd1SGreg Roach 11428091bfd1SGreg Roach /** 11438091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11448091bfd1SGreg Roach */ 11458091bfd1SGreg Roach public function lock(): void 11468091bfd1SGreg Roach { 11478091bfd1SGreg Roach DB::table('other') 11488091bfd1SGreg Roach ->where('o_file', '=', $this->tree->id()) 11498091bfd1SGreg Roach ->where('o_id', '=', $this->xref()) 11508091bfd1SGreg Roach ->lockForUpdate() 11518091bfd1SGreg Roach ->get(); 11528091bfd1SGreg Roach } 115372f8afdbSGreg Roach 115472f8afdbSGreg Roach /** 115572f8afdbSGreg Roach * Change records may contain notes and other fields. Just update the date/time/author. 115672f8afdbSGreg Roach * 115772f8afdbSGreg Roach * @param string $gedcom 115872f8afdbSGreg Roach * 115972f8afdbSGreg Roach * @return string 116072f8afdbSGreg Roach */ 11615cba5dc7SGreg Roach private function updateChange(string $gedcom): string 11625cba5dc7SGreg Roach { 116372f8afdbSGreg Roach $gedcom = preg_replace('/\n2 (DATE|_WT_USER).*(\n[3-9].*)*/', '', $gedcom); 116472f8afdbSGreg Roach $today = strtoupper(date('d M Y')); 116572f8afdbSGreg Roach $now = date('H:i:s'); 116672f8afdbSGreg Roach $author = Auth::user()->userName(); 116772f8afdbSGreg Roach 116872f8afdbSGreg Roach return $gedcom . "\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . $author; 116972f8afdbSGreg Roach } 1170a25f0a04SGreg Roach} 1171