1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg Roach * Copyright (C) 2022 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; 265818a371SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomRecordPage; 2722e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService; 28bf4eb542SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2983242252SGreg Roachuse Illuminate\Database\Query\Builder; 3083242252SGreg Roachuse Illuminate\Database\Query\Expression; 31ba1c12e8SGreg Roachuse Illuminate\Database\Query\JoinClause; 3239ca88baSGreg Roachuse Illuminate\Support\Collection; 33a25f0a04SGreg Roach 34b5961194SGreg Roachuse function addcslashes; 3522e73debSGreg Roachuse function app; 360f5fd22fSGreg Roachuse function array_combine; 370f5fd22fSGreg Roachuse function array_keys; 380f5fd22fSGreg Roachuse function array_map; 390f5fd22fSGreg Roachuse function array_search; 40f7440925SGreg Roachuse function array_shift; 41f7440925SGreg Roachuse function assert; 42f7440925SGreg Roachuse function count; 43f7440925SGreg Roachuse function date; 44f7440925SGreg Roachuse function e; 45f7440925SGreg Roachuse function explode; 46f7440925SGreg Roachuse function in_array; 47f7440925SGreg Roachuse function md5; 48f7440925SGreg Roachuse function preg_match; 49f7440925SGreg Roachuse function preg_match_all; 50f7440925SGreg Roachuse function preg_replace; 51f7440925SGreg Roachuse function preg_replace_callback; 52f7440925SGreg Roachuse function preg_split; 530f5fd22fSGreg Roachuse function range; 54f7440925SGreg Roachuse function route; 55dec352c1SGreg Roachuse function str_contains; 56d0889c63SGreg Roachuse function str_ends_with; 57f7440925SGreg Roachuse function str_pad; 58f7440925SGreg Roachuse function strtoupper; 59f7440925SGreg Roachuse function trim; 60b315f3e1SGreg Roachuse function view; 61b315f3e1SGreg Roach 62c2ed51d1SGreg Roachuse const PHP_INT_MAX; 63f7440925SGreg Roachuse const PREG_SET_ORDER; 64f7440925SGreg Roachuse const STR_PAD_LEFT; 6522e73debSGreg Roach 66a25f0a04SGreg Roach/** 6776692c8bSGreg Roach * A GEDCOM object. 68a25f0a04SGreg Roach */ 69c1010edaSGreg Roachclass GedcomRecord 70c1010edaSGreg Roach{ 7116d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 7216d6367aSGreg Roach 735818a371SGreg Roach protected const ROUTE_NAME = GedcomRecordPage::class; 745818a371SGreg Roach 759599771eSGreg Roach protected string $xref; 76bb03c9f0SGreg Roach 779599771eSGreg Roach protected Tree $tree; 78bb03c9f0SGreg Roach 799599771eSGreg Roach // GEDCOM data (before any pending edits) 809599771eSGreg Roach protected string $gedcom; 81bb03c9f0SGreg Roach 829599771eSGreg Roach // GEDCOM data (after any pending edits) 839599771eSGreg Roach protected ?string $pending; 84bb03c9f0SGreg Roach 859599771eSGreg Roach /** @var array<Fact> Facts extracted from $gedcom/$pending */ 869599771eSGreg Roach protected array $facts; 87bb03c9f0SGreg Roach 8809482a55SGreg Roach /** @var array<array<string>> All the names of this individual */ 899599771eSGreg Roach protected array $getAllNames = []; 90bb03c9f0SGreg Roach 9122e73debSGreg Roach /** @var int|null Cached result */ 929599771eSGreg Roach private ?int $getPrimaryName = null; 93c4943cffSGreg Roach 9422e73debSGreg Roach /** @var int|null Cached result */ 959599771eSGreg Roach private ?int $getSecondaryName = null; 96a25f0a04SGreg Roach 97a25f0a04SGreg Roach /** 98a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 99a25f0a04SGreg Roach * 100a25f0a04SGreg Roach * @param string $xref 101a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 102a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 103a25f0a04SGreg Roach * empty string for records with pending deletions 10424ec66ceSGreg Roach * @param Tree $tree 105a25f0a04SGreg Roach */ 106e364afe4SGreg Roach public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 107c1010edaSGreg Roach { 108a25f0a04SGreg Roach $this->xref = $xref; 109a25f0a04SGreg Roach $this->gedcom = $gedcom; 110a25f0a04SGreg Roach $this->pending = $pending; 11124ec66ceSGreg Roach $this->tree = $tree; 1129599771eSGreg Roach $this->facts = $this->parseFacts(); 113a25f0a04SGreg Roach } 114a25f0a04SGreg Roach 115a25f0a04SGreg Roach /** 116886b77daSGreg Roach * A closure which will filter out private records. 117886b77daSGreg Roach * 118886b77daSGreg Roach * @return Closure 119886b77daSGreg Roach */ 1204146fabcSGreg Roach public static function accessFilter(): Closure 121886b77daSGreg Roach { 1226c2179e2SGreg Roach return static function (GedcomRecord $record): bool { 123886b77daSGreg Roach return $record->canShow(); 124886b77daSGreg Roach }; 125886b77daSGreg Roach } 126886b77daSGreg Roach 127886b77daSGreg Roach /** 128c156e8f5SGreg Roach * A closure which will compare records by name. 129c156e8f5SGreg Roach * 130c156e8f5SGreg Roach * @return Closure 131c156e8f5SGreg Roach */ 132c156e8f5SGreg Roach public static function nameComparator(): Closure 133c156e8f5SGreg Roach { 1346c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y): int { 135c156e8f5SGreg Roach if ($x->canShowName()) { 136c156e8f5SGreg Roach if ($y->canShowName()) { 13737646143SGreg Roach return I18N::comparator()($x->sortName(), $y->sortName()); 138c156e8f5SGreg Roach } 139c156e8f5SGreg Roach 140c156e8f5SGreg Roach return -1; // only $y is private 141c156e8f5SGreg Roach } 142c156e8f5SGreg Roach 143c156e8f5SGreg Roach if ($y->canShowName()) { 144c156e8f5SGreg Roach return 1; // only $x is private 145c156e8f5SGreg Roach } 146c156e8f5SGreg Roach 147c156e8f5SGreg Roach return 0; // both $x and $y private 148c156e8f5SGreg Roach }; 149c156e8f5SGreg Roach } 150c156e8f5SGreg Roach 151c156e8f5SGreg Roach /** 152c156e8f5SGreg Roach * A closure which will compare records by change time. 153c156e8f5SGreg Roach * 154c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 155c156e8f5SGreg Roach * 156c156e8f5SGreg Roach * @return Closure 157c156e8f5SGreg Roach */ 158c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 159c156e8f5SGreg Roach { 1606c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y) use ($direction): int { 1614459dc9aSGreg Roach return $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp()); 162c156e8f5SGreg Roach }; 163c156e8f5SGreg Roach } 164c156e8f5SGreg Roach 165c156e8f5SGreg Roach /** 16602467d32SGreg Roach * Get the GEDCOM tag for this record. 16702467d32SGreg Roach * 16802467d32SGreg Roach * @return string 16902467d32SGreg Roach */ 17002467d32SGreg Roach public function tag(): string 17102467d32SGreg Roach { 17202467d32SGreg Roach preg_match('/^0 @[^@]*@ (\w+)/', $this->gedcom(), $match); 17302467d32SGreg Roach 17402467d32SGreg Roach return $match[1] ?? static::RECORD_TYPE; 17502467d32SGreg Roach } 17602467d32SGreg Roach 17702467d32SGreg Roach /** 178a25f0a04SGreg Roach * Get the XREF for this record 179a25f0a04SGreg Roach * 180a25f0a04SGreg Roach * @return string 181a25f0a04SGreg Roach */ 182c0935879SGreg Roach public function xref(): string 183c1010edaSGreg Roach { 184a25f0a04SGreg Roach return $this->xref; 185a25f0a04SGreg Roach } 186a25f0a04SGreg Roach 187a25f0a04SGreg Roach /** 188000959d9SGreg Roach * Get the tree to which this record belongs 189000959d9SGreg Roach * 190000959d9SGreg Roach * @return Tree 191000959d9SGreg Roach */ 192f4afa648SGreg Roach public function tree(): Tree 193c1010edaSGreg Roach { 194518bbdc1SGreg Roach return $this->tree; 195000959d9SGreg Roach } 196000959d9SGreg Roach 197000959d9SGreg Roach /** 198a25f0a04SGreg Roach * Application code should access data via Fact objects. 199a25f0a04SGreg Roach * This function exists to support old code. 200a25f0a04SGreg Roach * 201a25f0a04SGreg Roach * @return string 202a25f0a04SGreg Roach */ 203e364afe4SGreg Roach public function gedcom(): string 204c1010edaSGreg Roach { 205b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach 208a25f0a04SGreg Roach /** 209a25f0a04SGreg Roach * Does this record have a pending change? 210a25f0a04SGreg Roach * 211cbc1590aSGreg Roach * @return bool 212a25f0a04SGreg Roach */ 2138f53f488SRico Sonntag public function isPendingAddition(): bool 214c1010edaSGreg Roach { 215a25f0a04SGreg Roach return $this->pending !== null; 216a25f0a04SGreg Roach } 217a25f0a04SGreg Roach 218a25f0a04SGreg Roach /** 219a25f0a04SGreg Roach * Does this record have a pending deletion? 220a25f0a04SGreg Roach * 221cbc1590aSGreg Roach * @return bool 222a25f0a04SGreg Roach */ 2238f53f488SRico Sonntag public function isPendingDeletion(): bool 224c1010edaSGreg Roach { 225a25f0a04SGreg Roach return $this->pending === ''; 226a25f0a04SGreg Roach } 227a25f0a04SGreg Roach 228a25f0a04SGreg Roach /** 229225e381fSGreg Roach * Generate a URL to this record. 230a25f0a04SGreg Roach * 231a25f0a04SGreg Roach * @return string 232a25f0a04SGreg Roach */ 2338f53f488SRico Sonntag public function url(): string 234c1010edaSGreg Roach { 235225e381fSGreg Roach return route(static::ROUTE_NAME, [ 236c0935879SGreg Roach 'xref' => $this->xref(), 237ee4364daSGreg Roach 'tree' => $this->tree->name(), 238194b0938SGreg Roach 'slug' => Registry::slugFactory()->make($this), 239225e381fSGreg Roach ]); 240a25f0a04SGreg Roach } 241a25f0a04SGreg Roach 242a25f0a04SGreg Roach /** 243a25f0a04SGreg Roach * Can the details of this record be shown? 244a25f0a04SGreg Roach * 245cbc1590aSGreg Roach * @param int|null $access_level 246a25f0a04SGreg Roach * 247cbc1590aSGreg Roach * @return bool 248a25f0a04SGreg Roach */ 24935584196SGreg Roach public function canShow(int $access_level = null): bool 250c1010edaSGreg Roach { 251f0b9c048SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 2524b9ff166SGreg Roach 253a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 254a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 255f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 256a25f0a04SGreg Roach return true; 257a25f0a04SGreg Roach } 258f0b9c048SGreg Roach 2597476e8a5SGreg Roach $cache_key = 'show-' . $this->xref . '-' . $this->tree->id() . '-' . $access_level; 260f0b9c048SGreg Roach 2616b9cb339SGreg Roach return Registry::cache()->array()->remember($cache_key, function () use ($access_level) { 262f0b9c048SGreg Roach return $this->canShowRecord($access_level); 263f0b9c048SGreg Roach }); 264a25f0a04SGreg Roach } 265a25f0a04SGreg Roach 266a25f0a04SGreg Roach /** 267a25f0a04SGreg Roach * Can the name of this record be shown? 268a25f0a04SGreg Roach * 269cbc1590aSGreg Roach * @param int|null $access_level 270a25f0a04SGreg Roach * 271cbc1590aSGreg Roach * @return bool 272a25f0a04SGreg Roach */ 27376f666f4SGreg Roach public function canShowName(int $access_level = null): bool 274c1010edaSGreg Roach { 275a25f0a04SGreg Roach return $this->canShow($access_level); 276a25f0a04SGreg Roach } 277a25f0a04SGreg Roach 278a25f0a04SGreg Roach /** 279a25f0a04SGreg Roach * Can we edit this record? 280a25f0a04SGreg Roach * 281cbc1590aSGreg Roach * @return bool 282a25f0a04SGreg Roach */ 2838f53f488SRico Sonntag public function canEdit(): bool 284c1010edaSGreg Roach { 2851450f098SGreg Roach if ($this->isPendingDeletion()) { 2861450f098SGreg Roach return false; 2871450f098SGreg Roach } 2881450f098SGreg Roach 2891450f098SGreg Roach if (Auth::isManager($this->tree)) { 2901450f098SGreg Roach return true; 2911450f098SGreg Roach } 2921450f098SGreg Roach 293dec352c1SGreg Roach return Auth::isEditor($this->tree) && !str_contains($this->gedcom, "\n1 RESN locked"); 294a25f0a04SGreg Roach } 295a25f0a04SGreg Roach 296a25f0a04SGreg Roach /** 297a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 298a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 299a25f0a04SGreg Roach * 300cbc1590aSGreg Roach * @param int $access_level 301a25f0a04SGreg Roach * 302a25f0a04SGreg Roach * @return string 303a25f0a04SGreg Roach */ 304e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 305c1010edaSGreg Roach { 306e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 307a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 308a25f0a04SGreg Roach return $this->gedcom; 309b2ce94c6SRico Sonntag } 310b2ce94c6SRico Sonntag 311b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 312a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 313a25f0a04SGreg Roach 314a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 315dc141db3SGreg Roach [$gedrec] = explode("\n", $this->gedcom . $this->pending, 2); 316a25f0a04SGreg Roach 317a25f0a04SGreg Roach // Check each of the facts for access 3188d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 319138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 320a25f0a04SGreg Roach } 321cbc1590aSGreg Roach 322a25f0a04SGreg Roach return $gedrec; 323b2ce94c6SRico Sonntag } 324b2ce94c6SRico Sonntag 325a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 326a25f0a04SGreg Roach // limited data, such as links to other records. 327a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 328a25f0a04SGreg Roach } 329a25f0a04SGreg Roach 330a25f0a04SGreg Roach /** 331a25f0a04SGreg Roach * Default for "other" object types 332c7ff4153SGreg Roach * 333c7ff4153SGreg Roach * @return void 334a25f0a04SGreg Roach */ 335e364afe4SGreg Roach public function extractNames(): void 336c1010edaSGreg Roach { 33776f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 338a25f0a04SGreg Roach } 339a25f0a04SGreg Roach 340a25f0a04SGreg Roach /** 341a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 342a25f0a04SGreg Roach * 343870ec5a3SGreg Roach * @return array<int,array<string,string>> 344a25f0a04SGreg Roach */ 3458f53f488SRico Sonntag public function getAllNames(): array 346c1010edaSGreg Roach { 3479599771eSGreg Roach if ($this->getAllNames === []) { 348a25f0a04SGreg Roach if ($this->canShowName()) { 349a25f0a04SGreg Roach // Ask the record to extract its names 350a25f0a04SGreg Roach $this->extractNames(); 351a25f0a04SGreg Roach // No name found? Use a fallback. 352ec124fd2SGreg Roach if ($this->getAllNames === []) { 353db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 354a25f0a04SGreg Roach } 355a25f0a04SGreg Roach } else { 356db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 357a25f0a04SGreg Roach } 358a25f0a04SGreg Roach } 359cbc1590aSGreg Roach 360bdb3725aSGreg Roach return $this->getAllNames; 361a25f0a04SGreg Roach } 362a25f0a04SGreg Roach 363a25f0a04SGreg Roach /** 364a25f0a04SGreg Roach * If this object has no name, what do we call it? 365a25f0a04SGreg Roach * 366a25f0a04SGreg Roach * @return string 367a25f0a04SGreg Roach */ 3688f53f488SRico Sonntag public function getFallBackName(): string 369c1010edaSGreg Roach { 370c0935879SGreg Roach return e($this->xref()); 371a25f0a04SGreg Roach } 372a25f0a04SGreg Roach 373a25f0a04SGreg Roach /** 374a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 375a25f0a04SGreg Roach * 376cbc1590aSGreg Roach * @return int 377a25f0a04SGreg Roach */ 3788f53f488SRico Sonntag public function getPrimaryName(): int 379c1010edaSGreg Roach { 380a25f0a04SGreg Roach static $language_script; 381a25f0a04SGreg Roach 382448d466cSGreg Roach $language_script ??= I18N::locale()->script()->code(); 383a25f0a04SGreg Roach 384bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 385a25f0a04SGreg Roach // Generally, the first name is the primary one.... 386bdb3725aSGreg Roach $this->getPrimaryName = 0; 387a25f0a04SGreg Roach // ...except when the language/name use different character sets 388a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 38969546be1SGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 390bdb3725aSGreg Roach $this->getPrimaryName = $n; 391a25f0a04SGreg Roach break; 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach } 394a25f0a04SGreg Roach } 395a25f0a04SGreg Roach 396bdb3725aSGreg Roach return $this->getPrimaryName; 397a25f0a04SGreg Roach } 398a25f0a04SGreg Roach 399a25f0a04SGreg Roach /** 400a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 401a25f0a04SGreg Roach * 402cbc1590aSGreg Roach * @return int 403a25f0a04SGreg Roach */ 4048f53f488SRico Sonntag public function getSecondaryName(): int 405c1010edaSGreg Roach { 4068f038c36SRico Sonntag if ($this->getSecondaryName === null) { 407a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 408bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 409a25f0a04SGreg Roach // ....except when there are names with different character sets 410a25f0a04SGreg Roach $all_names = $this->getAllNames(); 411a25f0a04SGreg Roach if (count($all_names) > 1) { 412a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 413a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 414e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 415bdb3725aSGreg Roach $this->getSecondaryName = $n; 416a25f0a04SGreg Roach break; 417a25f0a04SGreg Roach } 418a25f0a04SGreg Roach } 419a25f0a04SGreg Roach } 420a25f0a04SGreg Roach } 421cbc1590aSGreg Roach 422bdb3725aSGreg Roach return $this->getSecondaryName; 423a25f0a04SGreg Roach } 424a25f0a04SGreg Roach 425a25f0a04SGreg Roach /** 426a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 427a25f0a04SGreg Roach * 42876f666f4SGreg Roach * @param int|null $n 4297e96c925SGreg Roach * 4307e96c925SGreg Roach * @return void 431a25f0a04SGreg Roach */ 432e364afe4SGreg Roach public function setPrimaryName(int $n = null): void 433c1010edaSGreg Roach { 434bdb3725aSGreg Roach $this->getPrimaryName = $n; 435bdb3725aSGreg Roach $this->getSecondaryName = null; 436a25f0a04SGreg Roach } 437a25f0a04SGreg Roach 438a25f0a04SGreg Roach /** 439a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 440a25f0a04SGreg Roach * 441a25f0a04SGreg Roach * @return string 442a25f0a04SGreg Roach */ 44324f2a3afSGreg Roach public function __toString(): string 444c1010edaSGreg Roach { 44572cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 446a25f0a04SGreg Roach } 447a25f0a04SGreg Roach 448a25f0a04SGreg Roach /** 449c156e8f5SGreg Roach * /** 450a25f0a04SGreg Roach * Get variants of the name 451a25f0a04SGreg Roach * 452a25f0a04SGreg Roach * @return string 453a25f0a04SGreg Roach */ 454e364afe4SGreg Roach public function fullName(): string 455c1010edaSGreg Roach { 456a25f0a04SGreg Roach if ($this->canShowName()) { 457a25f0a04SGreg Roach $tmp = $this->getAllNames(); 458cbc1590aSGreg Roach 459a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 460a25f0a04SGreg Roach } 461b2ce94c6SRico Sonntag 462b2ce94c6SRico Sonntag return I18N::translate('Private'); 463a25f0a04SGreg Roach } 464a25f0a04SGreg Roach 465a25f0a04SGreg Roach /** 466a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 467a25f0a04SGreg Roach * 468a25f0a04SGreg Roach * @return string 469a25f0a04SGreg Roach */ 47039ca88baSGreg Roach public function sortName(): string 471c1010edaSGreg Roach { 472a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 473a25f0a04SGreg Roach $tmp = $this->getAllNames(); 474cbc1590aSGreg Roach 475a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 476a25f0a04SGreg Roach } 477a25f0a04SGreg Roach 478a25f0a04SGreg Roach /** 479a25f0a04SGreg Roach * Get the full name in an alternative character set 480a25f0a04SGreg Roach * 481e364afe4SGreg Roach * @return string|null 482a25f0a04SGreg Roach */ 483e364afe4SGreg Roach public function alternateName(): ?string 484c1010edaSGreg Roach { 485e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 486a25f0a04SGreg Roach $all_names = $this->getAllNames(); 487cbc1590aSGreg Roach 488a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 489a25f0a04SGreg Roach } 490b2ce94c6SRico Sonntag 491b2ce94c6SRico Sonntag return null; 492a25f0a04SGreg Roach } 493a25f0a04SGreg Roach 494a25f0a04SGreg Roach /** 495a25f0a04SGreg Roach * Format this object for display in a list 496a25f0a04SGreg Roach * 497a25f0a04SGreg Roach * @return string 498a25f0a04SGreg Roach */ 4998f53f488SRico Sonntag public function formatList(): string 500c1010edaSGreg Roach { 501b165e17cSGreg Roach $html = '<a href="' . e($this->url()) . '" class="list_item">'; 50239ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 503a25f0a04SGreg Roach $html .= $this->formatListDetails(); 504b165e17cSGreg Roach $html .= '</a>'; 505cbc1590aSGreg Roach 506a25f0a04SGreg Roach return $html; 507a25f0a04SGreg Roach } 508a25f0a04SGreg Roach 509a25f0a04SGreg Roach /** 510a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 511a25f0a04SGreg Roach * identifying characteristics of this record. 512a25f0a04SGreg Roach * 513a25f0a04SGreg Roach * @return string 514a25f0a04SGreg Roach */ 5158f53f488SRico Sonntag public function formatListDetails(): string 516c1010edaSGreg Roach { 517a25f0a04SGreg Roach return ''; 518a25f0a04SGreg Roach } 519a25f0a04SGreg Roach 520a25f0a04SGreg Roach /** 521a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 522a25f0a04SGreg Roach * 52309482a55SGreg Roach * @param array<string> $facts 524cbc1590aSGreg Roach * @param int $style 525a25f0a04SGreg Roach * 526a25f0a04SGreg Roach * @return string 527a25f0a04SGreg Roach */ 5288d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 529c1010edaSGreg Roach { 53030158ae7SGreg Roach foreach ($this->facts($facts, true) as $event) { 531a25f0a04SGreg Roach // Only display if it has a date or place (or both) 532e364afe4SGreg Roach if ($event->date()->isOK() && $event->place()->gedcomName() !== '') { 533d93f11b5SGreg Roach $joiner = ' — '; 534d93f11b5SGreg Roach } else { 535d93f11b5SGreg Roach $joiner = ''; 536d93f11b5SGreg Roach } 537e364afe4SGreg Roach if ($event->date()->isOK() || $event->place()->gedcomName() !== '') { 538a25f0a04SGreg Roach switch ($style) { 539a25f0a04SGreg Roach case 1: 540b315f3e1SGreg Roach return '<br><em>' . $event->label() . ' ' . view('fact-date', ['cal_link' => 'false', 'fact' => $event, 'record' => $event->record(), 'time' => false]) . '</em>'; 541a25f0a04SGreg Roach case 2: 542b315f3e1SGreg Roach return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . view('fact-date', ['cal_link' => 'false', 'fact' => $event, 'record' => $event->record(), 'time' => false]) . $joiner . $event->place()->shortName() . '</dd></dl>'; 543a25f0a04SGreg Roach } 544a25f0a04SGreg Roach } 545a25f0a04SGreg Roach } 546cbc1590aSGreg Roach 547a25f0a04SGreg Roach return ''; 548a25f0a04SGreg Roach } 549a25f0a04SGreg Roach 550a25f0a04SGreg Roach /** 551a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 552a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 553a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 554a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 555a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 556a25f0a04SGreg Roach * 55709482a55SGreg Roach * @param array<string> $events 558a25f0a04SGreg Roach * 55909482a55SGreg Roach * @return array<Date> 560a25f0a04SGreg Roach */ 5618d0ebef0SGreg Roach public function getAllEventDates(array $events): array 562c1010edaSGreg Roach { 56313abd6f3SGreg Roach $dates = []; 5641385b8caSGreg Roach foreach ($this->facts($events, false, null, true) as $event) { 5652decada7SGreg Roach if ($event->date()->isOK()) { 5662decada7SGreg Roach $dates[] = $event->date(); 567a25f0a04SGreg Roach } 568a25f0a04SGreg Roach } 569a25f0a04SGreg Roach 570a25f0a04SGreg Roach return $dates; 571a25f0a04SGreg Roach } 572a25f0a04SGreg Roach 573a25f0a04SGreg Roach /** 574a25f0a04SGreg Roach * Get all the places for a particular type of event 575a25f0a04SGreg Roach * 57609482a55SGreg Roach * @param array<string> $events 577a25f0a04SGreg Roach * 57809482a55SGreg Roach * @return array<Place> 579a25f0a04SGreg Roach */ 5808d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 581c1010edaSGreg Roach { 58213abd6f3SGreg Roach $places = []; 5838d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 584138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 585a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 58616d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 587a25f0a04SGreg Roach } 588a25f0a04SGreg Roach } 589a25f0a04SGreg Roach } 590a25f0a04SGreg Roach 591a25f0a04SGreg Roach return $places; 592a25f0a04SGreg Roach } 593a25f0a04SGreg Roach 594a25f0a04SGreg Roach /** 595a25f0a04SGreg Roach * The facts and events for this record. 596a25f0a04SGreg Roach * 59709482a55SGreg Roach * @param array<string> $filter 598cbc1590aSGreg Roach * @param bool $sort 599cbc1590aSGreg Roach * @param int|null $access_level 600ce42304aSGreg Roach * @param bool $ignore_deleted 601a25f0a04SGreg Roach * 60236779af1SGreg Roach * @return Collection<int,Fact> 603a25f0a04SGreg Roach */ 604ce42304aSGreg Roach public function facts( 605ce42304aSGreg Roach array $filter = [], 606ce42304aSGreg Roach bool $sort = false, 607ce42304aSGreg Roach int $access_level = null, 608ce42304aSGreg Roach bool $ignore_deleted = false 609ce42304aSGreg Roach ): Collection { 610d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 6114b9ff166SGreg Roach 612d0889c63SGreg Roach // Convert BIRT into INDI:BIRT, etc. 613d0889c63SGreg Roach $filter = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, $filter); 614d0889c63SGreg Roach 6158af3e5c1SGreg Roach $facts = new Collection(); 616ce42304aSGreg Roach if ($this->canShow($access_level)) { 617a25f0a04SGreg Roach foreach ($this->facts as $fact) { 618d0889c63SGreg Roach if (($filter === [] || in_array($fact->tag(), $filter, true)) && $fact->canShow($access_level)) { 6198af3e5c1SGreg Roach $facts->push($fact); 620a25f0a04SGreg Roach } 621a25f0a04SGreg Roach } 622a25f0a04SGreg Roach } 623d17d7b9eSGreg Roach 624a25f0a04SGreg Roach if ($sort) { 6250f5fd22fSGreg Roach switch ($this->tag()) { 6260f5fd22fSGreg Roach case Family::RECORD_TYPE: 6270f5fd22fSGreg Roach case Individual::RECORD_TYPE: 628580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 6290f5fd22fSGreg Roach break; 6300f5fd22fSGreg Roach 6310f5fd22fSGreg Roach default: 6320f5fd22fSGreg Roach $subtags = Registry::elementFactory()->make($this->tag())->subtags(); 6330f5fd22fSGreg Roach $subtags = array_map(fn (string $tag): string => $this->tag() . ':' . $tag, array_keys($subtags)); 6340f5fd22fSGreg Roach $subtags = array_combine(range(1, count($subtags)), $subtags); 6350f5fd22fSGreg Roach 6360f5fd22fSGreg Roach $facts = $facts 6370f5fd22fSGreg Roach ->sort(static function (Fact $x, Fact $y) use ($subtags): int { 6380f5fd22fSGreg Roach $sort_x = array_search($x->tag(), $subtags, true) ?: PHP_INT_MAX; 6390f5fd22fSGreg Roach $sort_y = array_search($y->tag(), $subtags, true) ?: PHP_INT_MAX; 6400f5fd22fSGreg Roach 6410f5fd22fSGreg Roach return $sort_x <=> $sort_y; 6420f5fd22fSGreg Roach }); 6430f5fd22fSGreg Roach break; 6440f5fd22fSGreg Roach } 645a25f0a04SGreg Roach } 646cbc1590aSGreg Roach 647ce42304aSGreg Roach if ($ignore_deleted) { 648ce42304aSGreg Roach $facts = $facts->filter(static function (Fact $fact): bool { 649ce42304aSGreg Roach return !$fact->isPendingDeletion(); 650ce42304aSGreg Roach }); 651ce42304aSGreg Roach } 652ce42304aSGreg Roach 6535fef3bfaSGreg Roach return $facts; 654a25f0a04SGreg Roach } 655a25f0a04SGreg Roach 656a25f0a04SGreg Roach /** 6570f5fd22fSGreg Roach * @return array<string,string> 6580f5fd22fSGreg Roach */ 6590f5fd22fSGreg Roach public function missingFacts(): array 6600f5fd22fSGreg Roach { 6610f5fd22fSGreg Roach $missing_facts = []; 6620f5fd22fSGreg Roach 6630f5fd22fSGreg Roach foreach (Registry::elementFactory()->make($this->tag())->subtags() as $subtag => $repeat) { 6640f5fd22fSGreg Roach [, $max] = explode(':', $repeat); 6650f5fd22fSGreg Roach $max = $max === 'M' ? PHP_INT_MAX : (int) $max; 6660f5fd22fSGreg Roach 66798f93f3aSGreg Roach if ($this->facts([$subtag], false, null, true)->count() < $max) { 6680f5fd22fSGreg Roach $missing_facts[$subtag] = $subtag; 6690f5fd22fSGreg Roach $missing_facts[$subtag] = Registry::elementFactory()->make($this->tag() . ':' . $subtag)->label(); 6700f5fd22fSGreg Roach } 6710f5fd22fSGreg Roach } 6720f5fd22fSGreg Roach 6730f5fd22fSGreg Roach uasort($missing_facts, I18N::comparator()); 6740f5fd22fSGreg Roach 6755416d6ddSGreg Roach if (!Auth::canUploadMedia($this->tree, Auth::user())) { 6760f5fd22fSGreg Roach unset($missing_facts['OBJE']); 6770f5fd22fSGreg Roach } 6780f5fd22fSGreg Roach 6790f5fd22fSGreg Roach // We have special code for this. 6800f5fd22fSGreg Roach unset($missing_facts['FILE']); 6810f5fd22fSGreg Roach 6820f5fd22fSGreg Roach return $missing_facts; 6830f5fd22fSGreg Roach } 6840f5fd22fSGreg Roach 6850f5fd22fSGreg Roach /** 6864459dc9aSGreg Roach * Get the last-change timestamp for this record 687a25f0a04SGreg Roach * 68876d39c55SGreg Roach * @return TimestampInterface 689a25f0a04SGreg Roach */ 69076d39c55SGreg Roach public function lastChangeTimestamp(): TimestampInterface 691c1010edaSGreg Roach { 6924459dc9aSGreg Roach /** @var Fact|null $chan */ 693820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 694a25f0a04SGreg Roach 6954459dc9aSGreg Roach if ($chan instanceof Fact) { 696a25f0a04SGreg Roach // The record does have a CHAN event 697d97083feSGreg Roach $d = $chan->date()->minimumDate()->format('%Y-%m-%d'); 6984459dc9aSGreg Roach 699d97083feSGreg Roach if (preg_match('/\n3 TIME( (\d\d):(\d\d):(\d\d))/', $chan->gedcom(), $match)) { 700d97083feSGreg Roach return Registry::timestampFactory()->fromString($d . $match[1], 'Y-m-d H:i:s'); 701e364afe4SGreg Roach } 702e364afe4SGreg Roach 703d97083feSGreg Roach if (preg_match('/\n3 TIME ((\d\d):(\d\d))/', $chan->gedcom(), $match)) { 704d97083feSGreg Roach return Registry::timestampFactory()->fromString($d . $match[1], 'Y-m-d H:i'); 705b2ce94c6SRico Sonntag } 706b2ce94c6SRico Sonntag 707d97083feSGreg Roach return Registry::timestampFactory()->fromString($d, 'Y-m-d'); 708a25f0a04SGreg Roach } 709b2ce94c6SRico Sonntag 710a25f0a04SGreg Roach // The record does not have a CHAN event 711d97083feSGreg Roach return Registry::timestampFactory()->make(0); 712a25f0a04SGreg Roach } 713a25f0a04SGreg Roach 714a25f0a04SGreg Roach /** 715a25f0a04SGreg Roach * Get the last-change user for this record 716a25f0a04SGreg Roach * 717a25f0a04SGreg Roach * @return string 718a25f0a04SGreg Roach */ 719e364afe4SGreg Roach public function lastChangeUser(): string 720c1010edaSGreg Roach { 721820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 722a25f0a04SGreg Roach 723a25f0a04SGreg Roach if ($chan === null) { 724a25f0a04SGreg Roach return I18N::translate('Unknown'); 725b2ce94c6SRico Sonntag } 726b2ce94c6SRico Sonntag 7273425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 728baacc364SGreg Roach if ($chan_user === '') { 729a25f0a04SGreg Roach return I18N::translate('Unknown'); 730b2ce94c6SRico Sonntag } 731b2ce94c6SRico Sonntag 732a25f0a04SGreg Roach return $chan_user; 733a25f0a04SGreg Roach } 734a25f0a04SGreg Roach 735a25f0a04SGreg Roach /** 736a25f0a04SGreg Roach * Add a new fact to this record 737a25f0a04SGreg Roach * 738a25f0a04SGreg Roach * @param string $gedcom 739cbc1590aSGreg Roach * @param bool $update_chan 7407e96c925SGreg Roach * 7417e96c925SGreg Roach * @return void 742a25f0a04SGreg Roach */ 743e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 744c1010edaSGreg Roach { 745fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 746a25f0a04SGreg Roach } 747a25f0a04SGreg Roach 748a25f0a04SGreg Roach /** 749a25f0a04SGreg Roach * Delete a fact from this record 750a25f0a04SGreg Roach * 751a25f0a04SGreg Roach * @param string $fact_id 752cbc1590aSGreg Roach * @param bool $update_chan 7537e96c925SGreg Roach * 7547e96c925SGreg Roach * @return void 755a25f0a04SGreg Roach */ 756e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 757c1010edaSGreg Roach { 758db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 759a25f0a04SGreg Roach } 760a25f0a04SGreg Roach 761a25f0a04SGreg Roach /** 762a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 763a25f0a04SGreg Roach * 764a25f0a04SGreg Roach * @param string $fact_id 765a25f0a04SGreg Roach * @param string $gedcom 766cbc1590aSGreg Roach * @param bool $update_chan 767a25f0a04SGreg Roach * 7687e96c925SGreg Roach * @return void 7697e96c925SGreg Roach * @throws Exception 770a25f0a04SGreg Roach */ 771e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 772c1010edaSGreg Roach { 77371f696adSGreg Roach // Not all record types allow a CHAN event. 77471f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 77571f696adSGreg Roach 776a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 777a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 778a25f0a04SGreg Roach $gedcom = trim($gedcom); 779a25f0a04SGreg Roach 780a25f0a04SGreg Roach if ($this->pending === '') { 7817e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 782a25f0a04SGreg Roach } 7838d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 7847e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 785a25f0a04SGreg Roach } 786a25f0a04SGreg Roach 787a25f0a04SGreg Roach if ($this->pending) { 788a25f0a04SGreg Roach $old_gedcom = $this->pending; 789a25f0a04SGreg Roach } else { 790a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 791a25f0a04SGreg Roach } 792a25f0a04SGreg Roach 793a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 79465e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 795a25f0a04SGreg Roach 796a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 79719aed3a1SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE, true) as $fact) { 798905ab80aSGreg Roach if ($fact->id() === $fact_id) { 799db7bb364SGreg Roach if ($gedcom !== '') { 800a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 801a25f0a04SGreg Roach } 802fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 803d0889c63SGreg Roach } elseif (!str_ends_with($fact->tag(), ':CHAN') || !$update_chan) { 804138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 805a25f0a04SGreg Roach } 806a25f0a04SGreg Roach } 807a25f0a04SGreg Roach 808a25f0a04SGreg Roach // Adding a new fact 809fc3ccce4SGreg Roach if ($fact_id === '') { 810a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 811a25f0a04SGreg Roach } 812a25f0a04SGreg Roach 813dec352c1SGreg Roach if ($update_chan && !str_contains($new_gedcom, "\n1 CHAN")) { 81419aed3a1SGreg Roach $today = strtoupper(date('d M Y')); 81519aed3a1SGreg Roach $now = date('H:i:s'); 81619aed3a1SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName(); 81719aed3a1SGreg Roach } 81819aed3a1SGreg Roach 819e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 820a25f0a04SGreg Roach // Save the changes 821d09b6323SGreg Roach DB::table('change')->insert([ 822d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 823d09b6323SGreg Roach 'xref' => $this->xref, 824d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 825d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 826d09b6323SGreg Roach 'user_id' => Auth::id(), 82713abd6f3SGreg Roach ]); 828a25f0a04SGreg Roach 829a25f0a04SGreg Roach $this->pending = $new_gedcom; 830a25f0a04SGreg Roach 8311fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 832b55cbc6bSGreg Roach $pending_changes_service = app(PendingChangesService::class); 833b55cbc6bSGreg Roach assert($pending_changes_service instanceof PendingChangesService); 834b55cbc6bSGreg Roach 835b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 836a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 837a25f0a04SGreg Roach $this->pending = null; 838a25f0a04SGreg Roach } 839a25f0a04SGreg Roach } 8409599771eSGreg Roach 8419599771eSGreg Roach $this->facts = $this->parseFacts(); 842a25f0a04SGreg Roach } 843a25f0a04SGreg Roach 844a25f0a04SGreg Roach /** 845a25f0a04SGreg Roach * Update this record 846a25f0a04SGreg Roach * 847a25f0a04SGreg Roach * @param string $gedcom 848cbc1590aSGreg Roach * @param bool $update_chan 8497e96c925SGreg Roach * 8507e96c925SGreg Roach * @return void 851a25f0a04SGreg Roach */ 852e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 853c1010edaSGreg Roach { 85471f696adSGreg Roach // Not all record types allow a CHAN event. 85571f696adSGreg Roach $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true); 85671f696adSGreg Roach 857a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 858a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 859a25f0a04SGreg Roach $gedcom = trim($gedcom); 860a25f0a04SGreg Roach 861a25f0a04SGreg Roach // Update the CHAN record 862a25f0a04SGreg Roach if ($update_chan) { 863a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 86453432476SGreg Roach $today = strtoupper(date('d M Y')); 86553432476SGreg Roach $now = date('H:i:s'); 86653432476SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . $today . "\n3 TIME " . $now . "\n2 _WT_USER " . Auth::user()->userName(); 867a25f0a04SGreg Roach } 868a25f0a04SGreg Roach 869a25f0a04SGreg Roach // Create a pending change 870d09b6323SGreg Roach DB::table('change')->insert([ 871d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 872d09b6323SGreg Roach 'xref' => $this->xref, 873d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 874d09b6323SGreg Roach 'new_gedcom' => $gedcom, 875d09b6323SGreg Roach 'user_id' => Auth::id(), 87613abd6f3SGreg Roach ]); 877a25f0a04SGreg Roach 878a25f0a04SGreg Roach // Clear the cache 879a25f0a04SGreg Roach $this->pending = $gedcom; 880a25f0a04SGreg Roach 881a25f0a04SGreg Roach // Accept this pending change 8821fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 883b55cbc6bSGreg Roach $pending_changes_service = app(PendingChangesService::class); 884b55cbc6bSGreg Roach assert($pending_changes_service instanceof PendingChangesService); 885b55cbc6bSGreg Roach 886b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 887a25f0a04SGreg Roach $this->gedcom = $gedcom; 888a25f0a04SGreg Roach $this->pending = null; 889a25f0a04SGreg Roach } 890a25f0a04SGreg Roach 8919599771eSGreg Roach $this->facts = $this->parseFacts(); 892a25f0a04SGreg Roach 893847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 894a25f0a04SGreg Roach } 895a25f0a04SGreg Roach 896a25f0a04SGreg Roach /** 897a25f0a04SGreg Roach * Delete this record 898b874da82SGreg Roach * 899b874da82SGreg Roach * @return void 900a25f0a04SGreg Roach */ 901e364afe4SGreg Roach public function deleteRecord(): void 902c1010edaSGreg Roach { 903a25f0a04SGreg Roach // Create a pending change 9044c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 905d09b6323SGreg Roach DB::table('change')->insert([ 906d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 907d09b6323SGreg Roach 'xref' => $this->xref, 908d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 909d09b6323SGreg Roach 'new_gedcom' => '', 910d09b6323SGreg Roach 'user_id' => Auth::id(), 91113abd6f3SGreg Roach ]); 9124c0b5256SGreg Roach } 913a25f0a04SGreg Roach 9144c0b5256SGreg Roach // Auto-accept this pending change 9151fe542e9SGreg Roach if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { 916b55cbc6bSGreg Roach $pending_changes_service = app(PendingChangesService::class); 917b55cbc6bSGreg Roach assert($pending_changes_service instanceof PendingChangesService); 918b55cbc6bSGreg Roach 919b55cbc6bSGreg Roach $pending_changes_service->acceptRecord($this); 920a25f0a04SGreg Roach } 921a25f0a04SGreg Roach 922847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 923a25f0a04SGreg Roach } 924a25f0a04SGreg Roach 925a25f0a04SGreg Roach /** 926a25f0a04SGreg Roach * Remove all links from this record to $xref 927a25f0a04SGreg Roach * 928a25f0a04SGreg Roach * @param string $xref 929cbc1590aSGreg Roach * @param bool $update_chan 9307e96c925SGreg Roach * 9317e96c925SGreg Roach * @return void 932a25f0a04SGreg Roach */ 933e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 934c1010edaSGreg Roach { 935a25f0a04SGreg Roach $value = '@' . $xref . '@'; 936a25f0a04SGreg Roach 93730158ae7SGreg Roach foreach ($this->facts() as $fact) { 93884586c02SGreg Roach if ($fact->value() === $value) { 939905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 9408d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 941138ca96cSGreg Roach $gedcom = $fact->gedcom(); 942a25f0a04SGreg Roach foreach ($matches as $match) { 943a25f0a04SGreg Roach $next_level = $match[1] + 1; 944a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 945a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 946a25f0a04SGreg Roach } 947905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 948a25f0a04SGreg Roach } 949a25f0a04SGreg Roach } 950a25f0a04SGreg Roach } 951bf4eb542SGreg Roach 952bf4eb542SGreg Roach /** 95322e73debSGreg Roach * Each object type may have its own special rules, and re-implement this function. 95422e73debSGreg Roach * 95522e73debSGreg Roach * @param int $access_level 95622e73debSGreg Roach * 95722e73debSGreg Roach * @return bool 95822e73debSGreg Roach */ 95922e73debSGreg Roach protected function canShowByType(int $access_level): bool 96022e73debSGreg Roach { 96122e73debSGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 96222e73debSGreg Roach 96322e73debSGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 96422e73debSGreg Roach // Restriction found 96522e73debSGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 96622e73debSGreg Roach } 96722e73debSGreg Roach 96822e73debSGreg Roach // No restriction found - must be public: 96922e73debSGreg Roach return true; 97022e73debSGreg Roach } 97122e73debSGreg Roach 97222e73debSGreg Roach /** 97322e73debSGreg Roach * Generate a private version of this record 97422e73debSGreg Roach * 97522e73debSGreg Roach * @param int $access_level 97622e73debSGreg Roach * 97722e73debSGreg Roach * @return string 97822e73debSGreg Roach */ 97922e73debSGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 98022e73debSGreg Roach { 981228ede81SGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE; 98222e73debSGreg Roach } 98322e73debSGreg Roach 98422e73debSGreg Roach /** 98522e73debSGreg Roach * Convert a name record into sortable and full/display versions. This default 98622e73debSGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 98722e73debSGreg Roach * 98822e73debSGreg Roach * @param string $type 98922e73debSGreg Roach * @param string $value 99022e73debSGreg Roach * @param string $gedcom 99122e73debSGreg Roach * 99222e73debSGreg Roach * @return void 99322e73debSGreg Roach */ 99422e73debSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 99522e73debSGreg Roach { 99622e73debSGreg Roach $this->getAllNames[] = [ 99722e73debSGreg Roach 'type' => $type, 998dbf91548SGreg Roach 'sort' => preg_replace_callback('/(\d+)/', static function (array $matches): string { 99922e73debSGreg Roach return str_pad($matches[0], 10, '0', STR_PAD_LEFT); 100022e73debSGreg Roach }, $value), 1001315eb316SGreg Roach 'full' => '<bdi>' . e($value) . '</bdi>', 100222e73debSGreg Roach // This is used for display 100322e73debSGreg Roach 'fullNN' => $value, 100422e73debSGreg Roach // This goes into the database 100522e73debSGreg Roach ]; 100622e73debSGreg Roach } 100722e73debSGreg Roach 100822e73debSGreg Roach /** 100922e73debSGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 101022e73debSGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 101122e73debSGreg Roach * Parameters: the level 1 fact containing the name. 101222e73debSGreg Roach * Return value: an array of name structures, each containing 101322e73debSGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 101422e73debSGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 101522e73debSGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 101622e73debSGreg Roach * 101722e73debSGreg Roach * @param int $level 101822e73debSGreg Roach * @param string $fact_type 101936779af1SGreg Roach * @param Collection<int,Fact> $facts 102022e73debSGreg Roach * 102122e73debSGreg Roach * @return void 102222e73debSGreg Roach */ 102322e73debSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 102422e73debSGreg Roach { 102522e73debSGreg Roach $sublevel = $level + 1; 102622e73debSGreg Roach $subsublevel = $sublevel + 1; 102722e73debSGreg Roach foreach ($facts as $fact) { 1028c8ff5fdcSGreg Roach if (preg_match_all('/^' . $level . ' (' . $fact_type . ') (.+)((\n[' . $sublevel . '-9].+)*)/m', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 102922e73debSGreg Roach foreach ($matches as $match) { 103022e73debSGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 1031dec352c1SGreg Roach if ($match[1] === 'NAME' && str_contains($match[3], "\n2 TYPE married")) { 103222e73debSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 103322e73debSGreg Roach } else { 103422e73debSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 103522e73debSGreg Roach } 1036c8ff5fdcSGreg Roach if ($match[3] && preg_match_all('/^' . $sublevel . ' (ROMN|FONE|_\w+) (.+)((\n[' . $subsublevel . '-9].+)*)/m', $match[3], $submatches, PREG_SET_ORDER)) { 103722e73debSGreg Roach foreach ($submatches as $submatch) { 103822e73debSGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 103922e73debSGreg Roach } 104022e73debSGreg Roach } 104122e73debSGreg Roach } 104222e73debSGreg Roach } 104322e73debSGreg Roach } 104422e73debSGreg Roach } 104522e73debSGreg Roach 104622e73debSGreg Roach /** 104722e73debSGreg Roach * Split the record into facts 104822e73debSGreg Roach * 10499599771eSGreg Roach * @return array<Fact> 105022e73debSGreg Roach */ 10519599771eSGreg Roach private function parseFacts(): array 105222e73debSGreg Roach { 105322e73debSGreg Roach // Split the record into facts 105422e73debSGreg Roach if ($this->gedcom) { 1055d823340dSGreg Roach $gedcom_facts = preg_split('/\n(?=1)/', $this->gedcom); 105622e73debSGreg Roach array_shift($gedcom_facts); 105722e73debSGreg Roach } else { 105822e73debSGreg Roach $gedcom_facts = []; 105922e73debSGreg Roach } 106022e73debSGreg Roach if ($this->pending) { 1061d823340dSGreg Roach $pending_facts = preg_split('/\n(?=1)/', $this->pending); 106222e73debSGreg Roach array_shift($pending_facts); 106322e73debSGreg Roach } else { 106422e73debSGreg Roach $pending_facts = []; 106522e73debSGreg Roach } 106622e73debSGreg Roach 10679599771eSGreg Roach $facts = []; 106822e73debSGreg Roach 106922e73debSGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 107022e73debSGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 107122e73debSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 107222e73debSGreg Roach $fact->setPendingDeletion(); 107322e73debSGreg Roach } 10749599771eSGreg Roach $facts[] = $fact; 107522e73debSGreg Roach } 107622e73debSGreg Roach foreach ($pending_facts as $pending_fact) { 107722e73debSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 107822e73debSGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 107922e73debSGreg Roach $fact->setPendingAddition(); 10809599771eSGreg Roach $facts[] = $fact; 108122e73debSGreg Roach } 108222e73debSGreg Roach } 10839599771eSGreg Roach 10849599771eSGreg Roach return $facts; 108522e73debSGreg Roach } 108622e73debSGreg Roach 108722e73debSGreg Roach /** 108822e73debSGreg Roach * Work out whether this record can be shown to a user with a given access level 108922e73debSGreg Roach * 109022e73debSGreg Roach * @param int $access_level 109122e73debSGreg Roach * 109222e73debSGreg Roach * @return bool 109322e73debSGreg Roach */ 109422e73debSGreg Roach private function canShowRecord(int $access_level): bool 109522e73debSGreg Roach { 109622e73debSGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 109722e73debSGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 109822e73debSGreg Roach return true; 109922e73debSGreg Roach } 110022e73debSGreg Roach 110122e73debSGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 11021fe542e9SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) { 110322e73debSGreg Roach return true; 110422e73debSGreg Roach } 110522e73debSGreg Roach 110622e73debSGreg Roach // Does this record have a RESN? 1107dec352c1SGreg Roach if (str_contains($this->gedcom, "\n1 RESN confidential")) { 110822e73debSGreg Roach return Auth::PRIV_NONE >= $access_level; 110922e73debSGreg Roach } 1110dec352c1SGreg Roach if (str_contains($this->gedcom, "\n1 RESN privacy")) { 111122e73debSGreg Roach return Auth::PRIV_USER >= $access_level; 111222e73debSGreg Roach } 1113dec352c1SGreg Roach if (str_contains($this->gedcom, "\n1 RESN none")) { 111422e73debSGreg Roach return true; 111522e73debSGreg Roach } 111622e73debSGreg Roach 111722e73debSGreg Roach // Does this record have a default RESN? 111822e73debSGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 111922e73debSGreg Roach if (isset($individual_privacy[$this->xref()])) { 112022e73debSGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 112122e73debSGreg Roach } 112222e73debSGreg Roach 112322e73debSGreg Roach // Privacy rules do not apply to admins 112422e73debSGreg Roach if (Auth::PRIV_NONE >= $access_level) { 112522e73debSGreg Roach return true; 112622e73debSGreg Roach } 112722e73debSGreg Roach 112822e73debSGreg Roach // Different types of record have different privacy rules 112922e73debSGreg Roach return $this->canShowByType($access_level); 113022e73debSGreg Roach } 11318091bfd1SGreg Roach 11328091bfd1SGreg Roach /** 11338091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 11348091bfd1SGreg Roach */ 11358091bfd1SGreg Roach public function lock(): void 11368091bfd1SGreg Roach { 11378091bfd1SGreg Roach DB::table('other') 11388091bfd1SGreg Roach ->where('o_file', '=', $this->tree->id()) 11398091bfd1SGreg Roach ->where('o_id', '=', $this->xref()) 11408091bfd1SGreg Roach ->lockForUpdate() 11418091bfd1SGreg Roach ->get(); 11428091bfd1SGreg Roach } 1143a25f0a04SGreg Roach} 1144