1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 2176692c8bSGreg Roach 22886b77daSGreg Roachuse Closure; 237e96c925SGreg Roachuse Exception; 243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 255818a371SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomRecordPage; 2622e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService; 27bf4eb542SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2883242252SGreg Roachuse Illuminate\Database\Query\Builder; 2983242252SGreg Roachuse Illuminate\Database\Query\Expression; 30ba1c12e8SGreg Roachuse Illuminate\Database\Query\JoinClause; 3139ca88baSGreg Roachuse Illuminate\Support\Collection; 3279529c87SGreg Roachuse stdClass; 33a25f0a04SGreg Roach 3422e73debSGreg Roachuse function app; 3522e73debSGreg Roach 36a25f0a04SGreg Roach/** 3776692c8bSGreg Roach * A GEDCOM object. 38a25f0a04SGreg Roach */ 39c1010edaSGreg Roachclass GedcomRecord 40c1010edaSGreg Roach{ 4116d6367aSGreg Roach public const RECORD_TYPE = 'UNKNOWN'; 4216d6367aSGreg Roach 435818a371SGreg Roach protected const ROUTE_NAME = GedcomRecordPage::class; 445818a371SGreg Roach 4576692c8bSGreg Roach /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */ 46bec87e94SGreg Roach public static $gedcom_record_cache; 4779529c87SGreg Roach /** @var stdClass[][] Fetch all pending edits in one database query */ 48bec87e94SGreg Roach public static $pending_record_cache; 4922e73debSGreg Roach /** @var string The record identifier */ 5022e73debSGreg Roach protected $xref; 5122e73debSGreg Roach /** @var Tree The family tree to which this record belongs */ 5222e73debSGreg Roach protected $tree; 5322e73debSGreg Roach /** @var string GEDCOM data (before any pending edits) */ 5422e73debSGreg Roach protected $gedcom; 5522e73debSGreg Roach /** @var string|null GEDCOM data (after any pending edits) */ 5622e73debSGreg Roach protected $pending; 5722e73debSGreg Roach /** @var Fact[] facts extracted from $gedcom/$pending */ 5822e73debSGreg Roach protected $facts; 5922e73debSGreg Roach /** @var string[][] All the names of this individual */ 6022e73debSGreg Roach protected $getAllNames; 6122e73debSGreg Roach /** @var int|null Cached result */ 6222e73debSGreg Roach protected $getPrimaryName; 6322e73debSGreg Roach /** @var int|null Cached result */ 6422e73debSGreg Roach protected $getSecondaryName; 65a25f0a04SGreg Roach 66a25f0a04SGreg Roach /** 67a25f0a04SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 68a25f0a04SGreg Roach * 69a25f0a04SGreg Roach * @param string $xref 70a25f0a04SGreg Roach * @param string $gedcom an empty string for new/pending records 71a25f0a04SGreg Roach * @param string|null $pending null for a record with no pending edits, 72a25f0a04SGreg Roach * empty string for records with pending deletions 7324ec66ceSGreg Roach * @param Tree $tree 74a25f0a04SGreg Roach */ 75e364afe4SGreg Roach public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 76c1010edaSGreg Roach { 77a25f0a04SGreg Roach $this->xref = $xref; 78a25f0a04SGreg Roach $this->gedcom = $gedcom; 79a25f0a04SGreg Roach $this->pending = $pending; 8024ec66ceSGreg Roach $this->tree = $tree; 81a25f0a04SGreg Roach 82a25f0a04SGreg Roach $this->parseFacts(); 83a25f0a04SGreg Roach } 84a25f0a04SGreg Roach 85a25f0a04SGreg Roach /** 86886b77daSGreg Roach * A closure which will create a record from a database row. 87886b77daSGreg Roach * 88d5ad3db0SGreg Roach * @param Tree $tree 89d5ad3db0SGreg Roach * 90886b77daSGreg Roach * @return Closure 91886b77daSGreg Roach */ 92d5ad3db0SGreg Roach public static function rowMapper(Tree $tree): Closure 93886b77daSGreg Roach { 94d5ad3db0SGreg Roach return static function (stdClass $row) use ($tree): GedcomRecord { 95ffc0a61fSGreg Roach $record = GedcomRecord::getInstance($row->o_id, $tree, $row->o_gedcom); 96ffc0a61fSGreg Roach assert($record instanceof GedcomRecord); 97ffc0a61fSGreg Roach 98ffc0a61fSGreg Roach return $record; 99886b77daSGreg Roach }; 100886b77daSGreg Roach } 101886b77daSGreg Roach 102886b77daSGreg Roach /** 103886b77daSGreg Roach * A closure which will filter out private records. 104886b77daSGreg Roach * 105886b77daSGreg Roach * @return Closure 106886b77daSGreg Roach */ 1074146fabcSGreg Roach public static function accessFilter(): Closure 108886b77daSGreg Roach { 1096c2179e2SGreg Roach return static function (GedcomRecord $record): bool { 110886b77daSGreg Roach return $record->canShow(); 111886b77daSGreg Roach }; 112886b77daSGreg Roach } 113886b77daSGreg Roach 114886b77daSGreg Roach /** 115c156e8f5SGreg Roach * A closure which will compare records by name. 116c156e8f5SGreg Roach * 117c156e8f5SGreg Roach * @return Closure 118c156e8f5SGreg Roach */ 119c156e8f5SGreg Roach public static function nameComparator(): Closure 120c156e8f5SGreg Roach { 1216c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y): int { 122c156e8f5SGreg Roach if ($x->canShowName()) { 123c156e8f5SGreg Roach if ($y->canShowName()) { 12439ca88baSGreg Roach return I18N::strcasecmp($x->sortName(), $y->sortName()); 125c156e8f5SGreg Roach } 126c156e8f5SGreg Roach 127c156e8f5SGreg Roach return -1; // only $y is private 128c156e8f5SGreg Roach } 129c156e8f5SGreg Roach 130c156e8f5SGreg Roach if ($y->canShowName()) { 131c156e8f5SGreg Roach return 1; // only $x is private 132c156e8f5SGreg Roach } 133c156e8f5SGreg Roach 134c156e8f5SGreg Roach return 0; // both $x and $y private 135c156e8f5SGreg Roach }; 136c156e8f5SGreg Roach } 137c156e8f5SGreg Roach 138c156e8f5SGreg Roach /** 139c156e8f5SGreg Roach * A closure which will compare records by change time. 140c156e8f5SGreg Roach * 141c156e8f5SGreg Roach * @param int $direction +1 to sort ascending, -1 to sort descending 142c156e8f5SGreg Roach * 143c156e8f5SGreg Roach * @return Closure 144c156e8f5SGreg Roach */ 145c156e8f5SGreg Roach public static function lastChangeComparator(int $direction = 1): Closure 146c156e8f5SGreg Roach { 1476c2179e2SGreg Roach return static function (GedcomRecord $x, GedcomRecord $y) use ($direction): int { 1484459dc9aSGreg Roach return $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp()); 149c156e8f5SGreg Roach }; 150c156e8f5SGreg Roach } 151c156e8f5SGreg Roach 152c156e8f5SGreg Roach /** 153a25f0a04SGreg Roach * Get an instance of a GedcomRecord object. For single records, 154a25f0a04SGreg Roach * we just receive the XREF. For bulk records (such as lists 155a25f0a04SGreg Roach * and search results) we can receive the GEDCOM data as well. 156a25f0a04SGreg Roach * 157a25f0a04SGreg Roach * @param string $xref 15824ec66ceSGreg Roach * @param Tree $tree 159a25f0a04SGreg Roach * @param string|null $gedcom 160a25f0a04SGreg Roach * 161ffc0a61fSGreg Roach * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|Submitter|null 16222e73debSGreg Roach * @throws Exception 163a25f0a04SGreg Roach */ 16476f666f4SGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 165c1010edaSGreg Roach { 16672cf66d4SGreg Roach $tree_id = $tree->id(); 16724ec66ceSGreg Roach 168e71ef9d2SGreg Roach // Is this record already in the cache? 16924ec66ceSGreg Roach if (isset(self::$gedcom_record_cache[$xref][$tree_id])) { 170e71ef9d2SGreg Roach return self::$gedcom_record_cache[$xref][$tree_id]; 171a25f0a04SGreg Roach } 172a25f0a04SGreg Roach 173a25f0a04SGreg Roach // Do we need to fetch the record from the database? 174a25f0a04SGreg Roach if ($gedcom === null) { 17524ec66ceSGreg Roach $gedcom = static::fetchGedcomRecord($xref, $tree_id); 176a25f0a04SGreg Roach } 177a25f0a04SGreg Roach 178a25f0a04SGreg Roach // If we can edit, then we also need to be able to see pending records. 17994075df0SGreg Roach if (Auth::isEditor($tree)) { 18024ec66ceSGreg Roach if (!isset(self::$pending_record_cache[$tree_id])) { 181a25f0a04SGreg Roach // Fetch all pending records in one database query 18213abd6f3SGreg Roach self::$pending_record_cache[$tree_id] = []; 18385fc8064SGreg Roach $rows = DB::table('change') 18485fc8064SGreg Roach ->where('gedcom_id', '=', $tree_id) 18585fc8064SGreg Roach ->where('status', '=', 'pending') 18685fc8064SGreg Roach ->orderBy('change_id') 18785fc8064SGreg Roach ->select(['xref', 'new_gedcom']) 18885fc8064SGreg Roach ->get(); 189d17d7b9eSGreg Roach 190a25f0a04SGreg Roach foreach ($rows as $row) { 19124ec66ceSGreg Roach self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom; 192a25f0a04SGreg Roach } 193a25f0a04SGreg Roach } 194a25f0a04SGreg Roach 195d17d7b9eSGreg Roach $pending = self::$pending_record_cache[$tree_id][$xref] ?? null; 196a25f0a04SGreg Roach } else { 197a25f0a04SGreg Roach // There are no pending changes for this record 198a25f0a04SGreg Roach $pending = null; 199a25f0a04SGreg Roach } 200a25f0a04SGreg Roach 201a25f0a04SGreg Roach // No such record exists 202a25f0a04SGreg Roach if ($gedcom === null && $pending === null) { 203a25f0a04SGreg Roach return null; 204a25f0a04SGreg Roach } 205a25f0a04SGreg Roach 206fc3ccce4SGreg Roach // No such record, but a pending creation exists 207fc3ccce4SGreg Roach if ($gedcom === null) { 208fc3ccce4SGreg Roach $gedcom = ''; 209fc3ccce4SGreg Roach } 210fc3ccce4SGreg Roach 211a25f0a04SGreg Roach // Create the object 2128d0ebef0SGreg Roach if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ (' . Gedcom::REGEX_TAG . ')/', $gedcom . $pending, $match)) { 213a25f0a04SGreg Roach $xref = $match[1]; // Collation - we may have requested I123 and found i123 214a25f0a04SGreg Roach $type = $match[2]; 215a25f0a04SGreg Roach } elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) { 216a25f0a04SGreg Roach $xref = $match[1]; 217a25f0a04SGreg Roach $type = $match[1]; 218a25f0a04SGreg Roach } elseif ($gedcom . $pending) { 2197e96c925SGreg Roach throw new Exception('Unrecognized GEDCOM record: ' . $gedcom); 220a25f0a04SGreg Roach } else { 221a25f0a04SGreg Roach // A record with both pending creation and pending deletion 222a25f0a04SGreg Roach $type = static::RECORD_TYPE; 223a25f0a04SGreg Roach } 224a25f0a04SGreg Roach 225a25f0a04SGreg Roach switch ($type) { 226a25f0a04SGreg Roach case 'INDI': 22724ec66ceSGreg Roach $record = new Individual($xref, $gedcom, $pending, $tree); 228a25f0a04SGreg Roach break; 229a25f0a04SGreg Roach case 'FAM': 23024ec66ceSGreg Roach $record = new Family($xref, $gedcom, $pending, $tree); 231a25f0a04SGreg Roach break; 232a25f0a04SGreg Roach case 'SOUR': 23324ec66ceSGreg Roach $record = new Source($xref, $gedcom, $pending, $tree); 234a25f0a04SGreg Roach break; 235a25f0a04SGreg Roach case 'OBJE': 23624ec66ceSGreg Roach $record = new Media($xref, $gedcom, $pending, $tree); 237a25f0a04SGreg Roach break; 238a25f0a04SGreg Roach case 'REPO': 23924ec66ceSGreg Roach $record = new Repository($xref, $gedcom, $pending, $tree); 240a25f0a04SGreg Roach break; 241a25f0a04SGreg Roach case 'NOTE': 24224ec66ceSGreg Roach $record = new Note($xref, $gedcom, $pending, $tree); 243a25f0a04SGreg Roach break; 244ffc0a61fSGreg Roach case 'SUBM': 245ffc0a61fSGreg Roach $record = new Submitter($xref, $gedcom, $pending, $tree); 246ffc0a61fSGreg Roach break; 247a25f0a04SGreg Roach default: 24806ef8e02SGreg Roach $record = new self($xref, $gedcom, $pending, $tree); 249a25f0a04SGreg Roach break; 250a25f0a04SGreg Roach } 251a25f0a04SGreg Roach 252a25f0a04SGreg Roach // Store it in the cache 25324ec66ceSGreg Roach self::$gedcom_record_cache[$xref][$tree_id] = $record; 254a25f0a04SGreg Roach 255a25f0a04SGreg Roach return $record; 256a25f0a04SGreg Roach } 257a25f0a04SGreg Roach 258a25f0a04SGreg Roach /** 259a25f0a04SGreg Roach * Fetch data from the database 260a25f0a04SGreg Roach * 261a25f0a04SGreg Roach * @param string $xref 262cbc1590aSGreg Roach * @param int $tree_id 263a25f0a04SGreg Roach * 264e364afe4SGreg Roach * @return string|null 265a25f0a04SGreg Roach */ 266e364afe4SGreg Roach protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 267c1010edaSGreg Roach { 268a25f0a04SGreg Roach // We don't know what type of object this is. Try each one in turn. 26964d9078aSGreg Roach $data = Individual::fetchGedcomRecord($xref, $tree_id); 27085fc8064SGreg Roach if ($data !== null) { 271a25f0a04SGreg Roach return $data; 272a25f0a04SGreg Roach } 27364d9078aSGreg Roach $data = Family::fetchGedcomRecord($xref, $tree_id); 27485fc8064SGreg Roach if ($data !== null) { 275a25f0a04SGreg Roach return $data; 276a25f0a04SGreg Roach } 27764d9078aSGreg Roach $data = Source::fetchGedcomRecord($xref, $tree_id); 27885fc8064SGreg Roach if ($data !== null) { 279a25f0a04SGreg Roach return $data; 280a25f0a04SGreg Roach } 28164d9078aSGreg Roach $data = Repository::fetchGedcomRecord($xref, $tree_id); 28285fc8064SGreg Roach if ($data !== null) { 283a25f0a04SGreg Roach return $data; 284a25f0a04SGreg Roach } 28564d9078aSGreg Roach $data = Media::fetchGedcomRecord($xref, $tree_id); 28685fc8064SGreg Roach if ($data !== null) { 287a25f0a04SGreg Roach return $data; 288a25f0a04SGreg Roach } 28964d9078aSGreg Roach $data = Note::fetchGedcomRecord($xref, $tree_id); 29085fc8064SGreg Roach if ($data !== null) { 291a25f0a04SGreg Roach return $data; 292a25f0a04SGreg Roach } 293ffc0a61fSGreg Roach $data = Submitter::fetchGedcomRecord($xref, $tree_id); 294ffc0a61fSGreg Roach if ($data !== null) { 295ffc0a61fSGreg Roach return $data; 296ffc0a61fSGreg Roach } 297c1010edaSGreg Roach 298a25f0a04SGreg Roach // Some other type of record... 299d09b6323SGreg Roach return DB::table('other') 30085fc8064SGreg Roach ->where('o_file', '=', $tree_id) 30185fc8064SGreg Roach ->where('o_id', '=', $xref) 30285fc8064SGreg Roach ->value('o_gedcom'); 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach 305a25f0a04SGreg Roach /** 306a25f0a04SGreg Roach * Get the XREF for this record 307a25f0a04SGreg Roach * 308a25f0a04SGreg Roach * @return string 309a25f0a04SGreg Roach */ 310c0935879SGreg Roach public function xref(): string 311c1010edaSGreg Roach { 312a25f0a04SGreg Roach return $this->xref; 313a25f0a04SGreg Roach } 314a25f0a04SGreg Roach 315a25f0a04SGreg Roach /** 316000959d9SGreg Roach * Get the tree to which this record belongs 317000959d9SGreg Roach * 318000959d9SGreg Roach * @return Tree 319000959d9SGreg Roach */ 320f4afa648SGreg Roach public function tree(): Tree 321c1010edaSGreg Roach { 322518bbdc1SGreg Roach return $this->tree; 323000959d9SGreg Roach } 324000959d9SGreg Roach 325000959d9SGreg Roach /** 326a25f0a04SGreg Roach * Application code should access data via Fact objects. 327a25f0a04SGreg Roach * This function exists to support old code. 328a25f0a04SGreg Roach * 329a25f0a04SGreg Roach * @return string 330a25f0a04SGreg Roach */ 331e364afe4SGreg Roach public function gedcom(): string 332c1010edaSGreg Roach { 333b2ce94c6SRico Sonntag return $this->pending ?? $this->gedcom; 334a25f0a04SGreg Roach } 335a25f0a04SGreg Roach 336a25f0a04SGreg Roach /** 337a25f0a04SGreg Roach * Does this record have a pending change? 338a25f0a04SGreg Roach * 339cbc1590aSGreg Roach * @return bool 340a25f0a04SGreg Roach */ 3418f53f488SRico Sonntag public function isPendingAddition(): bool 342c1010edaSGreg Roach { 343a25f0a04SGreg Roach return $this->pending !== null; 344a25f0a04SGreg Roach } 345a25f0a04SGreg Roach 346a25f0a04SGreg Roach /** 347a25f0a04SGreg Roach * Does this record have a pending deletion? 348a25f0a04SGreg Roach * 349cbc1590aSGreg Roach * @return bool 350a25f0a04SGreg Roach */ 3518f53f488SRico Sonntag public function isPendingDeletion(): bool 352c1010edaSGreg Roach { 353a25f0a04SGreg Roach return $this->pending === ''; 354a25f0a04SGreg Roach } 355a25f0a04SGreg Roach 356a25f0a04SGreg Roach /** 357ee4364daSGreg Roach * Generate a "slug" to use in pretty URLs. 358ee4364daSGreg Roach * 359ee4364daSGreg Roach * @return string 360ee4364daSGreg Roach */ 361ee4364daSGreg Roach public function slug(): string 362ee4364daSGreg Roach { 363afa9f52aSGreg Roach return strip_tags($this->fullName()); 364ee4364daSGreg Roach } 365ee4364daSGreg Roach 366ee4364daSGreg Roach /** 367225e381fSGreg Roach * Generate a URL to this record. 368a25f0a04SGreg Roach * 369a25f0a04SGreg Roach * @return string 370a25f0a04SGreg Roach */ 3718f53f488SRico Sonntag public function url(): string 372c1010edaSGreg Roach { 373225e381fSGreg Roach return route(static::ROUTE_NAME, [ 374c0935879SGreg Roach 'xref' => $this->xref(), 375ee4364daSGreg Roach 'tree' => $this->tree->name(), 376ee4364daSGreg Roach 'slug' => $this->slug(), 377225e381fSGreg Roach ]); 378a25f0a04SGreg Roach } 379a25f0a04SGreg Roach 380a25f0a04SGreg Roach /** 381a25f0a04SGreg Roach * Can the details of this record be shown? 382a25f0a04SGreg Roach * 383cbc1590aSGreg Roach * @param int|null $access_level 384a25f0a04SGreg Roach * 385cbc1590aSGreg Roach * @return bool 386a25f0a04SGreg Roach */ 38735584196SGreg Roach public function canShow(int $access_level = null): bool 388c1010edaSGreg Roach { 389f0b9c048SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 3904b9ff166SGreg Roach 391a25f0a04SGreg Roach // We use this value to bypass privacy checks. For example, 392a25f0a04SGreg Roach // when downloading data or when calculating privacy itself. 393f0b9c048SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 394a25f0a04SGreg Roach return true; 395a25f0a04SGreg Roach } 396f0b9c048SGreg Roach 3977476e8a5SGreg Roach $cache_key = 'show-' . $this->xref . '-' . $this->tree->id() . '-' . $access_level; 398f0b9c048SGreg Roach 399c692965aSGreg Roach return app('cache.array')->remember($cache_key, function () use ($access_level) { 400f0b9c048SGreg Roach return $this->canShowRecord($access_level); 401f0b9c048SGreg Roach }); 402a25f0a04SGreg Roach } 403a25f0a04SGreg Roach 404a25f0a04SGreg Roach /** 405a25f0a04SGreg Roach * Can the name of this record be shown? 406a25f0a04SGreg Roach * 407cbc1590aSGreg Roach * @param int|null $access_level 408a25f0a04SGreg Roach * 409cbc1590aSGreg Roach * @return bool 410a25f0a04SGreg Roach */ 41176f666f4SGreg Roach public function canShowName(int $access_level = null): bool 412c1010edaSGreg Roach { 413a25f0a04SGreg Roach return $this->canShow($access_level); 414a25f0a04SGreg Roach } 415a25f0a04SGreg Roach 416a25f0a04SGreg Roach /** 417a25f0a04SGreg Roach * Can we edit this record? 418a25f0a04SGreg Roach * 419cbc1590aSGreg Roach * @return bool 420a25f0a04SGreg Roach */ 4218f53f488SRico Sonntag public function canEdit(): bool 422c1010edaSGreg Roach { 4231450f098SGreg Roach if ($this->isPendingDeletion()) { 4241450f098SGreg Roach return false; 4251450f098SGreg Roach } 4261450f098SGreg Roach 4271450f098SGreg Roach if (Auth::isManager($this->tree)) { 4281450f098SGreg Roach return true; 4291450f098SGreg Roach } 4301450f098SGreg Roach 4311450f098SGreg Roach return Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false; 432a25f0a04SGreg Roach } 433a25f0a04SGreg Roach 434a25f0a04SGreg Roach /** 435a25f0a04SGreg Roach * Remove private data from the raw gedcom record. 436a25f0a04SGreg Roach * Return both the visible and invisible data. We need the invisible data when editing. 437a25f0a04SGreg Roach * 438cbc1590aSGreg Roach * @param int $access_level 439a25f0a04SGreg Roach * 440a25f0a04SGreg Roach * @return string 441a25f0a04SGreg Roach */ 442e364afe4SGreg Roach public function privatizeGedcom(int $access_level): string 443c1010edaSGreg Roach { 444e364afe4SGreg Roach if ($access_level === Auth::PRIV_HIDE) { 445a25f0a04SGreg Roach // We may need the original record, for example when downloading a GEDCOM or clippings cart 446a25f0a04SGreg Roach return $this->gedcom; 447b2ce94c6SRico Sonntag } 448b2ce94c6SRico Sonntag 449b2ce94c6SRico Sonntag if ($this->canShow($access_level)) { 450a25f0a04SGreg Roach // The record is not private, but the individual facts may be. 451a25f0a04SGreg Roach 452a25f0a04SGreg Roach // Include the entire first line (for NOTE records) 45365e02381SGreg Roach [$gedrec] = explode("\n", $this->gedcom, 2); 454a25f0a04SGreg Roach 455a25f0a04SGreg Roach // Check each of the facts for access 4568d0ebef0SGreg Roach foreach ($this->facts([], false, $access_level) as $fact) { 457138ca96cSGreg Roach $gedrec .= "\n" . $fact->gedcom(); 458a25f0a04SGreg Roach } 459cbc1590aSGreg Roach 460a25f0a04SGreg Roach return $gedrec; 461b2ce94c6SRico Sonntag } 462b2ce94c6SRico Sonntag 463a25f0a04SGreg Roach // We cannot display the details, but we may be able to display 464a25f0a04SGreg Roach // limited data, such as links to other records. 465a25f0a04SGreg Roach return $this->createPrivateGedcomRecord($access_level); 466a25f0a04SGreg Roach } 467a25f0a04SGreg Roach 468a25f0a04SGreg Roach /** 469a25f0a04SGreg Roach * Default for "other" object types 470c7ff4153SGreg Roach * 471c7ff4153SGreg Roach * @return void 472a25f0a04SGreg Roach */ 473e364afe4SGreg Roach public function extractNames(): void 474c1010edaSGreg Roach { 47576f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 476a25f0a04SGreg Roach } 477a25f0a04SGreg Roach 478a25f0a04SGreg Roach /** 479a25f0a04SGreg Roach * Derived classes should redefine this function, otherwise the object will have no name 480a25f0a04SGreg Roach * 481a25f0a04SGreg Roach * @return string[][] 482a25f0a04SGreg Roach */ 4838f53f488SRico Sonntag public function getAllNames(): array 484c1010edaSGreg Roach { 485bdb3725aSGreg Roach if ($this->getAllNames === null) { 486bdb3725aSGreg Roach $this->getAllNames = []; 487a25f0a04SGreg Roach if ($this->canShowName()) { 488a25f0a04SGreg Roach // Ask the record to extract its names 489a25f0a04SGreg Roach $this->extractNames(); 490a25f0a04SGreg Roach // No name found? Use a fallback. 491bdb3725aSGreg Roach if (!$this->getAllNames) { 492db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, $this->getFallBackName(), ''); 493a25f0a04SGreg Roach } 494a25f0a04SGreg Roach } else { 495db7bb364SGreg Roach $this->addName(static::RECORD_TYPE, I18N::translate('Private'), ''); 496a25f0a04SGreg Roach } 497a25f0a04SGreg Roach } 498cbc1590aSGreg Roach 499bdb3725aSGreg Roach return $this->getAllNames; 500a25f0a04SGreg Roach } 501a25f0a04SGreg Roach 502a25f0a04SGreg Roach /** 503a25f0a04SGreg Roach * If this object has no name, what do we call it? 504a25f0a04SGreg Roach * 505a25f0a04SGreg Roach * @return string 506a25f0a04SGreg Roach */ 5078f53f488SRico Sonntag public function getFallBackName(): string 508c1010edaSGreg Roach { 509c0935879SGreg Roach return e($this->xref()); 510a25f0a04SGreg Roach } 511a25f0a04SGreg Roach 512a25f0a04SGreg Roach /** 513a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the primary one. 514a25f0a04SGreg Roach * 515cbc1590aSGreg Roach * @return int 516a25f0a04SGreg Roach */ 5178f53f488SRico Sonntag public function getPrimaryName(): int 518c1010edaSGreg Roach { 519a25f0a04SGreg Roach static $language_script; 520a25f0a04SGreg Roach 521a25f0a04SGreg Roach if ($language_script === null) { 52265cf5706SGreg Roach $language_script = $language_script ?? I18N::locale()->script()->code(); 523a25f0a04SGreg Roach } 524a25f0a04SGreg Roach 525bdb3725aSGreg Roach if ($this->getPrimaryName === null) { 526a25f0a04SGreg Roach // Generally, the first name is the primary one.... 527bdb3725aSGreg Roach $this->getPrimaryName = 0; 528a25f0a04SGreg Roach // ...except when the language/name use different character sets 529a25f0a04SGreg Roach foreach ($this->getAllNames() as $n => $name) { 53069546be1SGreg Roach if (I18N::textScript($name['sort']) === $language_script) { 531bdb3725aSGreg Roach $this->getPrimaryName = $n; 532a25f0a04SGreg Roach break; 533a25f0a04SGreg Roach } 534a25f0a04SGreg Roach } 535a25f0a04SGreg Roach } 536a25f0a04SGreg Roach 537bdb3725aSGreg Roach return $this->getPrimaryName; 538a25f0a04SGreg Roach } 539a25f0a04SGreg Roach 540a25f0a04SGreg Roach /** 541a25f0a04SGreg Roach * Which of the (possibly several) names of this record is the secondary one. 542a25f0a04SGreg Roach * 543cbc1590aSGreg Roach * @return int 544a25f0a04SGreg Roach */ 5458f53f488SRico Sonntag public function getSecondaryName(): int 546c1010edaSGreg Roach { 5478f038c36SRico Sonntag if ($this->getSecondaryName === null) { 548a25f0a04SGreg Roach // Generally, the primary and secondary names are the same 549bdb3725aSGreg Roach $this->getSecondaryName = $this->getPrimaryName(); 550a25f0a04SGreg Roach // ....except when there are names with different character sets 551a25f0a04SGreg Roach $all_names = $this->getAllNames(); 552a25f0a04SGreg Roach if (count($all_names) > 1) { 553a25f0a04SGreg Roach $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); 554a25f0a04SGreg Roach foreach ($all_names as $n => $name) { 555e364afe4SGreg Roach if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) { 556bdb3725aSGreg Roach $this->getSecondaryName = $n; 557a25f0a04SGreg Roach break; 558a25f0a04SGreg Roach } 559a25f0a04SGreg Roach } 560a25f0a04SGreg Roach } 561a25f0a04SGreg Roach } 562cbc1590aSGreg Roach 563bdb3725aSGreg Roach return $this->getSecondaryName; 564a25f0a04SGreg Roach } 565a25f0a04SGreg Roach 566a25f0a04SGreg Roach /** 567a25f0a04SGreg Roach * Allow the choice of primary name to be overidden, e.g. in a search result 568a25f0a04SGreg Roach * 56976f666f4SGreg Roach * @param int|null $n 5707e96c925SGreg Roach * 5717e96c925SGreg Roach * @return void 572a25f0a04SGreg Roach */ 573e364afe4SGreg Roach public function setPrimaryName(int $n = null): void 574c1010edaSGreg Roach { 575bdb3725aSGreg Roach $this->getPrimaryName = $n; 576bdb3725aSGreg Roach $this->getSecondaryName = null; 577a25f0a04SGreg Roach } 578a25f0a04SGreg Roach 579a25f0a04SGreg Roach /** 580a25f0a04SGreg Roach * Allow native PHP functions such as array_unique() to work with objects 581a25f0a04SGreg Roach * 582a25f0a04SGreg Roach * @return string 583a25f0a04SGreg Roach */ 584c1010edaSGreg Roach public function __toString() 585c1010edaSGreg Roach { 58672cf66d4SGreg Roach return $this->xref . '@' . $this->tree->id(); 587a25f0a04SGreg Roach } 588a25f0a04SGreg Roach 589a25f0a04SGreg Roach /** 590c156e8f5SGreg Roach * /** 591a25f0a04SGreg Roach * Get variants of the name 592a25f0a04SGreg Roach * 593a25f0a04SGreg Roach * @return string 594a25f0a04SGreg Roach */ 595e364afe4SGreg Roach public function fullName(): string 596c1010edaSGreg Roach { 597a25f0a04SGreg Roach if ($this->canShowName()) { 598a25f0a04SGreg Roach $tmp = $this->getAllNames(); 599cbc1590aSGreg Roach 600a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['full']; 601a25f0a04SGreg Roach } 602b2ce94c6SRico Sonntag 603b2ce94c6SRico Sonntag return I18N::translate('Private'); 604a25f0a04SGreg Roach } 605a25f0a04SGreg Roach 606a25f0a04SGreg Roach /** 607a25f0a04SGreg Roach * Get a sortable version of the name. Do not display this! 608a25f0a04SGreg Roach * 609a25f0a04SGreg Roach * @return string 610a25f0a04SGreg Roach */ 61139ca88baSGreg Roach public function sortName(): string 612c1010edaSGreg Roach { 613a25f0a04SGreg Roach // The sortable name is never displayed, no need to call canShowName() 614a25f0a04SGreg Roach $tmp = $this->getAllNames(); 615cbc1590aSGreg Roach 616a25f0a04SGreg Roach return $tmp[$this->getPrimaryName()]['sort']; 617a25f0a04SGreg Roach } 618a25f0a04SGreg Roach 619a25f0a04SGreg Roach /** 620a25f0a04SGreg Roach * Get the full name in an alternative character set 621a25f0a04SGreg Roach * 622e364afe4SGreg Roach * @return string|null 623a25f0a04SGreg Roach */ 624e364afe4SGreg Roach public function alternateName(): ?string 625c1010edaSGreg Roach { 626e364afe4SGreg Roach if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) { 627a25f0a04SGreg Roach $all_names = $this->getAllNames(); 628cbc1590aSGreg Roach 629a25f0a04SGreg Roach return $all_names[$this->getSecondaryName()]['full']; 630a25f0a04SGreg Roach } 631b2ce94c6SRico Sonntag 632b2ce94c6SRico Sonntag return null; 633a25f0a04SGreg Roach } 634a25f0a04SGreg Roach 635a25f0a04SGreg Roach /** 636a25f0a04SGreg Roach * Format this object for display in a list 637a25f0a04SGreg Roach * 638a25f0a04SGreg Roach * @return string 639a25f0a04SGreg Roach */ 6408f53f488SRico Sonntag public function formatList(): string 641c1010edaSGreg Roach { 642b165e17cSGreg Roach $html = '<a href="' . e($this->url()) . '" class="list_item">'; 64339ca88baSGreg Roach $html .= '<b>' . $this->fullName() . '</b>'; 644a25f0a04SGreg Roach $html .= $this->formatListDetails(); 645b165e17cSGreg Roach $html .= '</a>'; 646cbc1590aSGreg Roach 647a25f0a04SGreg Roach return $html; 648a25f0a04SGreg Roach } 649a25f0a04SGreg Roach 650a25f0a04SGreg Roach /** 651a25f0a04SGreg Roach * This function should be redefined in derived classes to show any major 652a25f0a04SGreg Roach * identifying characteristics of this record. 653a25f0a04SGreg Roach * 654a25f0a04SGreg Roach * @return string 655a25f0a04SGreg Roach */ 6568f53f488SRico Sonntag public function formatListDetails(): string 657c1010edaSGreg Roach { 658a25f0a04SGreg Roach return ''; 659a25f0a04SGreg Roach } 660a25f0a04SGreg Roach 661a25f0a04SGreg Roach /** 662a25f0a04SGreg Roach * Extract/format the first fact from a list of facts. 663a25f0a04SGreg Roach * 6648d0ebef0SGreg Roach * @param string[] $facts 665cbc1590aSGreg Roach * @param int $style 666a25f0a04SGreg Roach * 667a25f0a04SGreg Roach * @return string 668a25f0a04SGreg Roach */ 6698d0ebef0SGreg Roach public function formatFirstMajorFact(array $facts, int $style): string 670c1010edaSGreg Roach { 67130158ae7SGreg Roach foreach ($this->facts($facts, true) as $event) { 672a25f0a04SGreg Roach // Only display if it has a date or place (or both) 673e364afe4SGreg Roach if ($event->date()->isOK() && $event->place()->gedcomName() !== '') { 674d93f11b5SGreg Roach $joiner = ' — '; 675d93f11b5SGreg Roach } else { 676d93f11b5SGreg Roach $joiner = ''; 677d93f11b5SGreg Roach } 678e364afe4SGreg Roach if ($event->date()->isOK() || $event->place()->gedcomName() !== '') { 679a25f0a04SGreg Roach switch ($style) { 680a25f0a04SGreg Roach case 1: 6817b7d8067SGreg Roach return '<br><em>' . $event->label() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>'; 682a25f0a04SGreg Roach case 2: 6837b7d8067SGreg Roach return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>'; 684a25f0a04SGreg Roach } 685a25f0a04SGreg Roach } 686a25f0a04SGreg Roach } 687cbc1590aSGreg Roach 688a25f0a04SGreg Roach return ''; 689a25f0a04SGreg Roach } 690a25f0a04SGreg Roach 691a25f0a04SGreg Roach /** 692a25f0a04SGreg Roach * Find individuals linked to this record. 693a25f0a04SGreg Roach * 694a25f0a04SGreg Roach * @param string $link 695a25f0a04SGreg Roach * 696*b5c8fd7eSGreg Roach * @return Collection<Individual> 697a25f0a04SGreg Roach */ 698907c1109SGreg Roach public function linkedIndividuals(string $link): Collection 699c1010edaSGreg Roach { 700907c1109SGreg Roach return DB::table('individuals') 7010b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 702907c1109SGreg Roach $join 703907c1109SGreg Roach ->on('l_file', '=', 'i_file') 704907c1109SGreg Roach ->on('l_from', '=', 'i_id'); 705ba1c12e8SGreg Roach }) 706ba1c12e8SGreg Roach ->where('i_file', '=', $this->tree->id()) 707ba1c12e8SGreg Roach ->where('l_type', '=', $link) 708ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 709907c1109SGreg Roach ->select(['individuals.*']) 710907c1109SGreg Roach ->get() 711d5ad3db0SGreg Roach ->map(Individual::rowMapper($this->tree)) 712907c1109SGreg Roach ->filter(self::accessFilter()); 713a25f0a04SGreg Roach } 714a25f0a04SGreg Roach 715a25f0a04SGreg Roach /** 716a25f0a04SGreg Roach * Find families linked to this record. 717a25f0a04SGreg Roach * 718a25f0a04SGreg Roach * @param string $link 719a25f0a04SGreg Roach * 720*b5c8fd7eSGreg Roach * @return Collection<Family> 721a25f0a04SGreg Roach */ 722907c1109SGreg Roach public function linkedFamilies(string $link): Collection 723c1010edaSGreg Roach { 724907c1109SGreg Roach return DB::table('families') 7250b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 726907c1109SGreg Roach $join 727907c1109SGreg Roach ->on('l_file', '=', 'f_file') 728907c1109SGreg Roach ->on('l_from', '=', 'f_id'); 729ba1c12e8SGreg Roach }) 730ba1c12e8SGreg Roach ->where('f_file', '=', $this->tree->id()) 731ba1c12e8SGreg Roach ->where('l_type', '=', $link) 732ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 733907c1109SGreg Roach ->select(['families.*']) 734907c1109SGreg Roach ->get() 735d5ad3db0SGreg Roach ->map(Family::rowMapper($this->tree)) 736907c1109SGreg Roach ->filter(self::accessFilter()); 737a25f0a04SGreg Roach } 738a25f0a04SGreg Roach 739a25f0a04SGreg Roach /** 740a25f0a04SGreg Roach * Find sources linked to this record. 741a25f0a04SGreg Roach * 742a25f0a04SGreg Roach * @param string $link 743a25f0a04SGreg Roach * 744*b5c8fd7eSGreg Roach * @return Collection<Source> 745a25f0a04SGreg Roach */ 746907c1109SGreg Roach public function linkedSources(string $link): Collection 747c1010edaSGreg Roach { 748907c1109SGreg Roach return DB::table('sources') 7490b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 750907c1109SGreg Roach $join 751907c1109SGreg Roach ->on('l_file', '=', 's_file') 752907c1109SGreg Roach ->on('l_from', '=', 's_id'); 753ba1c12e8SGreg Roach }) 754ba1c12e8SGreg Roach ->where('s_file', '=', $this->tree->id()) 755ba1c12e8SGreg Roach ->where('l_type', '=', $link) 756ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 757907c1109SGreg Roach ->select(['sources.*']) 758907c1109SGreg Roach ->get() 759d5ad3db0SGreg Roach ->map(Source::rowMapper($this->tree)) 760907c1109SGreg Roach ->filter(self::accessFilter()); 761a25f0a04SGreg Roach } 762a25f0a04SGreg Roach 763a25f0a04SGreg Roach /** 764a25f0a04SGreg Roach * Find media objects linked to this record. 765a25f0a04SGreg Roach * 766a25f0a04SGreg Roach * @param string $link 767a25f0a04SGreg Roach * 768*b5c8fd7eSGreg Roach * @return Collection<Media> 769a25f0a04SGreg Roach */ 770907c1109SGreg Roach public function linkedMedia(string $link): Collection 771c1010edaSGreg Roach { 772907c1109SGreg Roach return DB::table('media') 7730b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 774907c1109SGreg Roach $join 775907c1109SGreg Roach ->on('l_file', '=', 'm_file') 776907c1109SGreg Roach ->on('l_from', '=', 'm_id'); 777ba1c12e8SGreg Roach }) 778ba1c12e8SGreg Roach ->where('m_file', '=', $this->tree->id()) 779ba1c12e8SGreg Roach ->where('l_type', '=', $link) 780ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 781907c1109SGreg Roach ->select(['media.*']) 782907c1109SGreg Roach ->get() 783d5ad3db0SGreg Roach ->map(Media::rowMapper($this->tree)) 784907c1109SGreg Roach ->filter(self::accessFilter()); 785a25f0a04SGreg Roach } 786a25f0a04SGreg Roach 787a25f0a04SGreg Roach /** 788a25f0a04SGreg Roach * Find notes linked to this record. 789a25f0a04SGreg Roach * 790a25f0a04SGreg Roach * @param string $link 791a25f0a04SGreg Roach * 792*b5c8fd7eSGreg Roach * @return Collection<Note> 793a25f0a04SGreg Roach */ 794907c1109SGreg Roach public function linkedNotes(string $link): Collection 795c1010edaSGreg Roach { 796907c1109SGreg Roach return DB::table('other') 7970b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 798907c1109SGreg Roach $join 799907c1109SGreg Roach ->on('l_file', '=', 'o_file') 800907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 801ba1c12e8SGreg Roach }) 802ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 803ba1c12e8SGreg Roach ->where('o_type', '=', 'NOTE') 804ba1c12e8SGreg Roach ->where('l_type', '=', $link) 805ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 806907c1109SGreg Roach ->select(['other.*']) 807907c1109SGreg Roach ->get() 808d5ad3db0SGreg Roach ->map(Note::rowMapper($this->tree)) 809907c1109SGreg Roach ->filter(self::accessFilter()); 810a25f0a04SGreg Roach } 811a25f0a04SGreg Roach 812a25f0a04SGreg Roach /** 813a25f0a04SGreg Roach * Find repositories linked to this record. 814a25f0a04SGreg Roach * 815a25f0a04SGreg Roach * @param string $link 816a25f0a04SGreg Roach * 817*b5c8fd7eSGreg Roach * @return Collection<Repository> 818a25f0a04SGreg Roach */ 819907c1109SGreg Roach public function linkedRepositories(string $link): Collection 820c1010edaSGreg Roach { 821907c1109SGreg Roach return DB::table('other') 8220b5fd0a6SGreg Roach ->join('link', static function (JoinClause $join): void { 823907c1109SGreg Roach $join 824907c1109SGreg Roach ->on('l_file', '=', 'o_file') 825907c1109SGreg Roach ->on('l_from', '=', 'o_id'); 826ba1c12e8SGreg Roach }) 827ba1c12e8SGreg Roach ->where('o_file', '=', $this->tree->id()) 828ba1c12e8SGreg Roach ->where('o_type', '=', 'REPO') 829ba1c12e8SGreg Roach ->where('l_type', '=', $link) 830ba1c12e8SGreg Roach ->where('l_to', '=', $this->xref) 831907c1109SGreg Roach ->select(['other.*']) 832907c1109SGreg Roach ->get() 833d5ad3db0SGreg Roach ->map(Repository::rowMapper($this->tree)) 834907c1109SGreg Roach ->filter(self::accessFilter()); 835a25f0a04SGreg Roach } 836a25f0a04SGreg Roach 837a25f0a04SGreg Roach /** 838a25f0a04SGreg Roach * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). 839a25f0a04SGreg Roach * This is used to display multiple events on the individual/family lists. 840a25f0a04SGreg Roach * Multiple events can exist because of uncertainty in dates, dates in different 841a25f0a04SGreg Roach * calendars, place-names in both latin and hebrew character sets, etc. 842a25f0a04SGreg Roach * It also allows us to combine dates/places from different events in the summaries. 843a25f0a04SGreg Roach * 8448d0ebef0SGreg Roach * @param string[] $events 845a25f0a04SGreg Roach * 846a25f0a04SGreg Roach * @return Date[] 847a25f0a04SGreg Roach */ 8488d0ebef0SGreg Roach public function getAllEventDates(array $events): array 849c1010edaSGreg Roach { 85013abd6f3SGreg Roach $dates = []; 8518d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 8522decada7SGreg Roach if ($event->date()->isOK()) { 8532decada7SGreg Roach $dates[] = $event->date(); 854a25f0a04SGreg Roach } 855a25f0a04SGreg Roach } 856a25f0a04SGreg Roach 857a25f0a04SGreg Roach return $dates; 858a25f0a04SGreg Roach } 859a25f0a04SGreg Roach 860a25f0a04SGreg Roach /** 861a25f0a04SGreg Roach * Get all the places for a particular type of event 862a25f0a04SGreg Roach * 8638d0ebef0SGreg Roach * @param string[] $events 864a25f0a04SGreg Roach * 8654080d558SGreg Roach * @return Place[] 866a25f0a04SGreg Roach */ 8678d0ebef0SGreg Roach public function getAllEventPlaces(array $events): array 868c1010edaSGreg Roach { 86913abd6f3SGreg Roach $places = []; 8708d0ebef0SGreg Roach foreach ($this->facts($events) as $event) { 871138ca96cSGreg Roach if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) { 872a25f0a04SGreg Roach foreach ($ged_places[1] as $ged_place) { 87316d0b7f7SRico Sonntag $places[] = new Place($ged_place, $this->tree); 874a25f0a04SGreg Roach } 875a25f0a04SGreg Roach } 876a25f0a04SGreg Roach } 877a25f0a04SGreg Roach 878a25f0a04SGreg Roach return $places; 879a25f0a04SGreg Roach } 880a25f0a04SGreg Roach 881a25f0a04SGreg Roach /** 882a25f0a04SGreg Roach * The facts and events for this record. 883a25f0a04SGreg Roach * 8848d0ebef0SGreg Roach * @param string[] $filter 885cbc1590aSGreg Roach * @param bool $sort 886cbc1590aSGreg Roach * @param int|null $access_level 887cbc1590aSGreg Roach * @param bool $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES. 888a25f0a04SGreg Roach * 889*b5c8fd7eSGreg Roach * @return Collection<Fact> 890a25f0a04SGreg Roach */ 89139ca88baSGreg Roach public function facts(array $filter = [], bool $sort = false, int $access_level = null, bool $override = false): Collection 892c1010edaSGreg Roach { 893d9e083e7SGreg Roach $access_level = $access_level ?? Auth::accessLevel($this->tree); 8944b9ff166SGreg Roach 8958af3e5c1SGreg Roach $facts = new Collection(); 896a25f0a04SGreg Roach if ($this->canShow($access_level) || $override) { 897a25f0a04SGreg Roach foreach ($this->facts as $fact) { 89822d65e5aSGreg Roach if (($filter === [] || in_array($fact->getTag(), $filter, true)) && $fact->canShow($access_level)) { 8998af3e5c1SGreg Roach $facts->push($fact); 900a25f0a04SGreg Roach } 901a25f0a04SGreg Roach } 902a25f0a04SGreg Roach } 903d17d7b9eSGreg Roach 904a25f0a04SGreg Roach if ($sort) { 905580a4d11SGreg Roach $facts = Fact::sortFacts($facts); 906a25f0a04SGreg Roach } 907cbc1590aSGreg Roach 90839ca88baSGreg Roach return new Collection($facts); 909a25f0a04SGreg Roach } 910a25f0a04SGreg Roach 911a25f0a04SGreg Roach /** 9124459dc9aSGreg Roach * Get the last-change timestamp for this record 913a25f0a04SGreg Roach * 9144459dc9aSGreg Roach * @return Carbon 915a25f0a04SGreg Roach */ 9164459dc9aSGreg Roach public function lastChangeTimestamp(): Carbon 917c1010edaSGreg Roach { 9184459dc9aSGreg Roach /** @var Fact|null $chan */ 919820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 920a25f0a04SGreg Roach 9214459dc9aSGreg Roach if ($chan instanceof Fact) { 922a25f0a04SGreg Roach // The record does have a CHAN event 9232decada7SGreg Roach $d = $chan->date()->minimumDate(); 9244459dc9aSGreg Roach 925138ca96cSGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->gedcom(), $match)) { 9264459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2], (int) $match[3]); 927e364afe4SGreg Roach } 928e364afe4SGreg Roach 929e364afe4SGreg Roach if (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->gedcom(), $match)) { 9304459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2]); 931b2ce94c6SRico Sonntag } 932b2ce94c6SRico Sonntag 9334459dc9aSGreg Roach return Carbon::create($d->year(), $d->month(), $d->day()); 934a25f0a04SGreg Roach } 935b2ce94c6SRico Sonntag 936a25f0a04SGreg Roach // The record does not have a CHAN event 9374459dc9aSGreg Roach return Carbon::createFromTimestamp(0); 938a25f0a04SGreg Roach } 939a25f0a04SGreg Roach 940a25f0a04SGreg Roach /** 941a25f0a04SGreg Roach * Get the last-change user for this record 942a25f0a04SGreg Roach * 943a25f0a04SGreg Roach * @return string 944a25f0a04SGreg Roach */ 945e364afe4SGreg Roach public function lastChangeUser(): string 946c1010edaSGreg Roach { 947820b62dfSGreg Roach $chan = $this->facts(['CHAN'])->first(); 948a25f0a04SGreg Roach 949a25f0a04SGreg Roach if ($chan === null) { 950a25f0a04SGreg Roach return I18N::translate('Unknown'); 951b2ce94c6SRico Sonntag } 952b2ce94c6SRico Sonntag 9533425616eSGreg Roach $chan_user = $chan->attribute('_WT_USER'); 954baacc364SGreg Roach if ($chan_user === '') { 955a25f0a04SGreg Roach return I18N::translate('Unknown'); 956b2ce94c6SRico Sonntag } 957b2ce94c6SRico Sonntag 958a25f0a04SGreg Roach return $chan_user; 959a25f0a04SGreg Roach } 960a25f0a04SGreg Roach 961a25f0a04SGreg Roach /** 962a25f0a04SGreg Roach * Add a new fact to this record 963a25f0a04SGreg Roach * 964a25f0a04SGreg Roach * @param string $gedcom 965cbc1590aSGreg Roach * @param bool $update_chan 9667e96c925SGreg Roach * 9677e96c925SGreg Roach * @return void 968a25f0a04SGreg Roach */ 969e364afe4SGreg Roach public function createFact(string $gedcom, bool $update_chan): void 970c1010edaSGreg Roach { 971fc3ccce4SGreg Roach $this->updateFact('', $gedcom, $update_chan); 972a25f0a04SGreg Roach } 973a25f0a04SGreg Roach 974a25f0a04SGreg Roach /** 975a25f0a04SGreg Roach * Delete a fact from this record 976a25f0a04SGreg Roach * 977a25f0a04SGreg Roach * @param string $fact_id 978cbc1590aSGreg Roach * @param bool $update_chan 9797e96c925SGreg Roach * 9807e96c925SGreg Roach * @return void 981a25f0a04SGreg Roach */ 982e364afe4SGreg Roach public function deleteFact(string $fact_id, bool $update_chan): void 983c1010edaSGreg Roach { 984db7bb364SGreg Roach $this->updateFact($fact_id, '', $update_chan); 985a25f0a04SGreg Roach } 986a25f0a04SGreg Roach 987a25f0a04SGreg Roach /** 988a25f0a04SGreg Roach * Replace a fact with a new gedcom data. 989a25f0a04SGreg Roach * 990a25f0a04SGreg Roach * @param string $fact_id 991a25f0a04SGreg Roach * @param string $gedcom 992cbc1590aSGreg Roach * @param bool $update_chan 993a25f0a04SGreg Roach * 9947e96c925SGreg Roach * @return void 9957e96c925SGreg Roach * @throws Exception 996a25f0a04SGreg Roach */ 997e364afe4SGreg Roach public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void 998c1010edaSGreg Roach { 999a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1000a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1001a25f0a04SGreg Roach $gedcom = trim($gedcom); 1002a25f0a04SGreg Roach 1003a25f0a04SGreg Roach if ($this->pending === '') { 10047e96c925SGreg Roach throw new Exception('Cannot edit a deleted record'); 1005a25f0a04SGreg Roach } 10068d0ebef0SGreg Roach if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) { 10077e96c925SGreg Roach throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')'); 1008a25f0a04SGreg Roach } 1009a25f0a04SGreg Roach 1010a25f0a04SGreg Roach if ($this->pending) { 1011a25f0a04SGreg Roach $old_gedcom = $this->pending; 1012a25f0a04SGreg Roach } else { 1013a25f0a04SGreg Roach $old_gedcom = $this->gedcom; 1014a25f0a04SGreg Roach } 1015a25f0a04SGreg Roach 1016a25f0a04SGreg Roach // First line of record may contain data - e.g. NOTE records. 101765e02381SGreg Roach [$new_gedcom] = explode("\n", $old_gedcom, 2); 1018a25f0a04SGreg Roach 1019a25f0a04SGreg Roach // Replacing (or deleting) an existing fact 10208d0ebef0SGreg Roach foreach ($this->facts([], false, Auth::PRIV_HIDE) as $fact) { 1021a25f0a04SGreg Roach if (!$fact->isPendingDeletion()) { 1022905ab80aSGreg Roach if ($fact->id() === $fact_id) { 1023db7bb364SGreg Roach if ($gedcom !== '') { 1024a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1025a25f0a04SGreg Roach } 1026fc3ccce4SGreg Roach $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact 1027e364afe4SGreg Roach } elseif ($fact->getTag() !== 'CHAN' || !$update_chan) { 1028138ca96cSGreg Roach $new_gedcom .= "\n" . $fact->gedcom(); 1029a25f0a04SGreg Roach } 1030a25f0a04SGreg Roach } 1031a25f0a04SGreg Roach } 1032a25f0a04SGreg Roach if ($update_chan) { 1033e5a6b4d4SGreg Roach $new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1034a25f0a04SGreg Roach } 1035a25f0a04SGreg Roach 1036a25f0a04SGreg Roach // Adding a new fact 1037fc3ccce4SGreg Roach if ($fact_id === '') { 1038a25f0a04SGreg Roach $new_gedcom .= "\n" . $gedcom; 1039a25f0a04SGreg Roach } 1040a25f0a04SGreg Roach 1041e364afe4SGreg Roach if ($new_gedcom !== $old_gedcom) { 1042a25f0a04SGreg Roach // Save the changes 1043d09b6323SGreg Roach DB::table('change')->insert([ 1044d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1045d09b6323SGreg Roach 'xref' => $this->xref, 1046d09b6323SGreg Roach 'old_gedcom' => $old_gedcom, 1047d09b6323SGreg Roach 'new_gedcom' => $new_gedcom, 1048d09b6323SGreg Roach 'user_id' => Auth::id(), 104913abd6f3SGreg Roach ]); 1050a25f0a04SGreg Roach 1051a25f0a04SGreg Roach $this->pending = $new_gedcom; 1052a25f0a04SGreg Roach 10537c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 105422e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1055a25f0a04SGreg Roach $this->gedcom = $new_gedcom; 1056a25f0a04SGreg Roach $this->pending = null; 1057a25f0a04SGreg Roach } 1058a25f0a04SGreg Roach } 1059a25f0a04SGreg Roach $this->parseFacts(); 1060a25f0a04SGreg Roach } 1061a25f0a04SGreg Roach 1062a25f0a04SGreg Roach /** 1063a25f0a04SGreg Roach * Update this record 1064a25f0a04SGreg Roach * 1065a25f0a04SGreg Roach * @param string $gedcom 1066cbc1590aSGreg Roach * @param bool $update_chan 10677e96c925SGreg Roach * 10687e96c925SGreg Roach * @return void 1069a25f0a04SGreg Roach */ 1070e364afe4SGreg Roach public function updateRecord(string $gedcom, bool $update_chan): void 1071c1010edaSGreg Roach { 1072a25f0a04SGreg Roach // MSDOS line endings will break things in horrible ways 1073a25f0a04SGreg Roach $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); 1074a25f0a04SGreg Roach $gedcom = trim($gedcom); 1075a25f0a04SGreg Roach 1076a25f0a04SGreg Roach // Update the CHAN record 1077a25f0a04SGreg Roach if ($update_chan) { 1078a25f0a04SGreg Roach $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom); 1079e5a6b4d4SGreg Roach $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName(); 1080a25f0a04SGreg Roach } 1081a25f0a04SGreg Roach 1082a25f0a04SGreg Roach // Create a pending change 1083d09b6323SGreg Roach DB::table('change')->insert([ 1084d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1085d09b6323SGreg Roach 'xref' => $this->xref, 1086d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1087d09b6323SGreg Roach 'new_gedcom' => $gedcom, 1088d09b6323SGreg Roach 'user_id' => Auth::id(), 108913abd6f3SGreg Roach ]); 1090a25f0a04SGreg Roach 1091a25f0a04SGreg Roach // Clear the cache 1092a25f0a04SGreg Roach $this->pending = $gedcom; 1093a25f0a04SGreg Roach 1094a25f0a04SGreg Roach // Accept this pending change 10957c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 109622e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1097a25f0a04SGreg Roach $this->gedcom = $gedcom; 1098a25f0a04SGreg Roach $this->pending = null; 1099a25f0a04SGreg Roach } 1100a25f0a04SGreg Roach 1101a25f0a04SGreg Roach $this->parseFacts(); 1102a25f0a04SGreg Roach 1103847d5489SGreg Roach Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1104a25f0a04SGreg Roach } 1105a25f0a04SGreg Roach 1106a25f0a04SGreg Roach /** 1107a25f0a04SGreg Roach * Delete this record 1108b874da82SGreg Roach * 1109b874da82SGreg Roach * @return void 1110a25f0a04SGreg Roach */ 1111e364afe4SGreg Roach public function deleteRecord(): void 1112c1010edaSGreg Roach { 1113a25f0a04SGreg Roach // Create a pending change 11144c0b5256SGreg Roach if (!$this->isPendingDeletion()) { 1115d09b6323SGreg Roach DB::table('change')->insert([ 1116d09b6323SGreg Roach 'gedcom_id' => $this->tree->id(), 1117d09b6323SGreg Roach 'xref' => $this->xref, 1118d09b6323SGreg Roach 'old_gedcom' => $this->gedcom(), 1119d09b6323SGreg Roach 'new_gedcom' => '', 1120d09b6323SGreg Roach 'user_id' => Auth::id(), 112113abd6f3SGreg Roach ]); 11224c0b5256SGreg Roach } 1123a25f0a04SGreg Roach 11244c0b5256SGreg Roach // Auto-accept this pending change 11257c4add84SGreg Roach if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') { 112622e73debSGreg Roach app(PendingChangesService::class)->acceptRecord($this); 1127a25f0a04SGreg Roach } 1128a25f0a04SGreg Roach 1129a25f0a04SGreg Roach // Clear the cache 1130d17d7b9eSGreg Roach self::$gedcom_record_cache = []; 1131d17d7b9eSGreg Roach self::$pending_record_cache = []; 1132a25f0a04SGreg Roach 1133847d5489SGreg Roach Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree); 1134a25f0a04SGreg Roach } 1135a25f0a04SGreg Roach 1136a25f0a04SGreg Roach /** 1137a25f0a04SGreg Roach * Remove all links from this record to $xref 1138a25f0a04SGreg Roach * 1139a25f0a04SGreg Roach * @param string $xref 1140cbc1590aSGreg Roach * @param bool $update_chan 11417e96c925SGreg Roach * 11427e96c925SGreg Roach * @return void 1143a25f0a04SGreg Roach */ 1144e364afe4SGreg Roach public function removeLinks(string $xref, bool $update_chan): void 1145c1010edaSGreg Roach { 1146a25f0a04SGreg Roach $value = '@' . $xref . '@'; 1147a25f0a04SGreg Roach 114830158ae7SGreg Roach foreach ($this->facts() as $fact) { 114984586c02SGreg Roach if ($fact->value() === $value) { 1150905ab80aSGreg Roach $this->deleteFact($fact->id(), $update_chan); 11518d0ebef0SGreg Roach } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) { 1152138ca96cSGreg Roach $gedcom = $fact->gedcom(); 1153a25f0a04SGreg Roach foreach ($matches as $match) { 1154a25f0a04SGreg Roach $next_level = $match[1] + 1; 1155a25f0a04SGreg Roach $next_levels = '[' . $next_level . '-9]'; 1156a25f0a04SGreg Roach $gedcom = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom); 1157a25f0a04SGreg Roach } 1158905ab80aSGreg Roach $this->updateFact($fact->id(), $gedcom, $update_chan); 1159a25f0a04SGreg Roach } 1160a25f0a04SGreg Roach } 1161a25f0a04SGreg Roach } 1162bf4eb542SGreg Roach 1163bf4eb542SGreg Roach /** 1164bf4eb542SGreg Roach * Fetch XREFs of all records linked to a record - when deleting an object, we must 1165bf4eb542SGreg Roach * also delete all links to it. 1166bf4eb542SGreg Roach * 1167bf4eb542SGreg Roach * @return GedcomRecord[] 1168bf4eb542SGreg Roach */ 1169bf4eb542SGreg Roach public function linkingRecords(): array 1170bf4eb542SGreg Roach { 1171bf4eb542SGreg Roach $union = DB::table('change') 1172bf4eb542SGreg Roach ->where('gedcom_id', '=', $this->tree()->id()) 1173fbbe964bSGreg Roach ->whereContains('new_gedcom', '@' . $this->xref() . '@') 1174bf4eb542SGreg Roach ->where('new_gedcom', 'NOT LIKE', '0 @' . $this->xref() . '@%') 117583242252SGreg Roach ->whereIn('change_id', function (Builder $query): void { 117683242252SGreg Roach $query->select(new Expression('MAX(change_id)')) 117783242252SGreg Roach ->from('change') 117883242252SGreg Roach ->where('gedcom_id', '=', $this->tree->id()) 117983242252SGreg Roach ->where('status', '=', 'pending') 11807f5c2944SGreg Roach ->groupBy(['xref']); 118183242252SGreg Roach }) 1182bf4eb542SGreg Roach ->select(['xref']); 1183bf4eb542SGreg Roach 1184bf4eb542SGreg Roach $xrefs = DB::table('link') 1185bf4eb542SGreg Roach ->where('l_file', '=', $this->tree()->id()) 1186bf4eb542SGreg Roach ->where('l_to', '=', $this->xref()) 118747256fc5SGreg Roach ->select(['l_from']) 1188bf4eb542SGreg Roach ->union($union) 1189bf4eb542SGreg Roach ->pluck('l_from'); 1190bf4eb542SGreg Roach 1191bf4eb542SGreg Roach return $xrefs->map(function (string $xref): GedcomRecord { 1192ffc0a61fSGreg Roach $record = GedcomRecord::getInstance($xref, $this->tree); 1193ffc0a61fSGreg Roach assert($record instanceof GedcomRecord); 1194ffc0a61fSGreg Roach 1195ffc0a61fSGreg Roach return $record; 1196bf4eb542SGreg Roach })->all(); 1197bf4eb542SGreg Roach } 119822e73debSGreg Roach 119922e73debSGreg Roach /** 120022e73debSGreg Roach * Each object type may have its own special rules, and re-implement this function. 120122e73debSGreg Roach * 120222e73debSGreg Roach * @param int $access_level 120322e73debSGreg Roach * 120422e73debSGreg Roach * @return bool 120522e73debSGreg Roach */ 120622e73debSGreg Roach protected function canShowByType(int $access_level): bool 120722e73debSGreg Roach { 120822e73debSGreg Roach $fact_privacy = $this->tree->getFactPrivacy(); 120922e73debSGreg Roach 121022e73debSGreg Roach if (isset($fact_privacy[static::RECORD_TYPE])) { 121122e73debSGreg Roach // Restriction found 121222e73debSGreg Roach return $fact_privacy[static::RECORD_TYPE] >= $access_level; 121322e73debSGreg Roach } 121422e73debSGreg Roach 121522e73debSGreg Roach // No restriction found - must be public: 121622e73debSGreg Roach return true; 121722e73debSGreg Roach } 121822e73debSGreg Roach 121922e73debSGreg Roach /** 122022e73debSGreg Roach * Generate a private version of this record 122122e73debSGreg Roach * 122222e73debSGreg Roach * @param int $access_level 122322e73debSGreg Roach * 122422e73debSGreg Roach * @return string 122522e73debSGreg Roach */ 122622e73debSGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 122722e73debSGreg Roach { 122822e73debSGreg Roach return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private'); 122922e73debSGreg Roach } 123022e73debSGreg Roach 123122e73debSGreg Roach /** 123222e73debSGreg Roach * Convert a name record into sortable and full/display versions. This default 123322e73debSGreg Roach * should be OK for simple record types. INDI/FAM records will need to redefine it. 123422e73debSGreg Roach * 123522e73debSGreg Roach * @param string $type 123622e73debSGreg Roach * @param string $value 123722e73debSGreg Roach * @param string $gedcom 123822e73debSGreg Roach * 123922e73debSGreg Roach * @return void 124022e73debSGreg Roach */ 124122e73debSGreg Roach protected function addName(string $type, string $value, string $gedcom): void 124222e73debSGreg Roach { 124322e73debSGreg Roach $this->getAllNames[] = [ 124422e73debSGreg Roach 'type' => $type, 124522e73debSGreg Roach 'sort' => preg_replace_callback('/([0-9]+)/', static function (array $matches): string { 124622e73debSGreg Roach return str_pad($matches[0], 10, '0', STR_PAD_LEFT); 124722e73debSGreg Roach }, $value), 124822e73debSGreg Roach 'full' => '<span dir="auto">' . e($value) . '</span>', 124922e73debSGreg Roach // This is used for display 125022e73debSGreg Roach 'fullNN' => $value, 125122e73debSGreg Roach // This goes into the database 125222e73debSGreg Roach ]; 125322e73debSGreg Roach } 125422e73debSGreg Roach 125522e73debSGreg Roach /** 125622e73debSGreg Roach * Get all the names of a record, including ROMN, FONE and _HEB alternatives. 125722e73debSGreg Roach * Records without a name (e.g. FAM) will need to redefine this function. 125822e73debSGreg Roach * Parameters: the level 1 fact containing the name. 125922e73debSGreg Roach * Return value: an array of name structures, each containing 126022e73debSGreg Roach * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. 126122e73debSGreg Roach * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' 126222e73debSGreg Roach * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' 126322e73debSGreg Roach * 126422e73debSGreg Roach * @param int $level 126522e73debSGreg Roach * @param string $fact_type 126622e73debSGreg Roach * @param Collection $facts 126722e73debSGreg Roach * 126822e73debSGreg Roach * @return void 126922e73debSGreg Roach */ 127022e73debSGreg Roach protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void 127122e73debSGreg Roach { 127222e73debSGreg Roach $sublevel = $level + 1; 127322e73debSGreg Roach $subsublevel = $sublevel + 1; 127422e73debSGreg Roach foreach ($facts as $fact) { 127522e73debSGreg Roach if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) { 127622e73debSGreg Roach foreach ($matches as $match) { 127722e73debSGreg Roach // Treat 1 NAME / 2 TYPE married the same as _MARNM 127822e73debSGreg Roach if ($match[1] === 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) { 127922e73debSGreg Roach $this->addName('_MARNM', $match[2], $fact->gedcom()); 128022e73debSGreg Roach } else { 128122e73debSGreg Roach $this->addName($match[1], $match[2], $fact->gedcom()); 128222e73debSGreg Roach } 128322e73debSGreg Roach if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { 128422e73debSGreg Roach foreach ($submatches as $submatch) { 128522e73debSGreg Roach $this->addName($submatch[1], $submatch[2], $match[3]); 128622e73debSGreg Roach } 128722e73debSGreg Roach } 128822e73debSGreg Roach } 128922e73debSGreg Roach } 129022e73debSGreg Roach } 129122e73debSGreg Roach } 129222e73debSGreg Roach 129322e73debSGreg Roach /** 129422e73debSGreg Roach * Split the record into facts 129522e73debSGreg Roach * 129622e73debSGreg Roach * @return void 129722e73debSGreg Roach */ 129822e73debSGreg Roach private function parseFacts(): void 129922e73debSGreg Roach { 130022e73debSGreg Roach // Split the record into facts 130122e73debSGreg Roach if ($this->gedcom) { 130222e73debSGreg Roach $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom); 130322e73debSGreg Roach array_shift($gedcom_facts); 130422e73debSGreg Roach } else { 130522e73debSGreg Roach $gedcom_facts = []; 130622e73debSGreg Roach } 130722e73debSGreg Roach if ($this->pending) { 130822e73debSGreg Roach $pending_facts = preg_split('/\n(?=1)/s', $this->pending); 130922e73debSGreg Roach array_shift($pending_facts); 131022e73debSGreg Roach } else { 131122e73debSGreg Roach $pending_facts = []; 131222e73debSGreg Roach } 131322e73debSGreg Roach 131422e73debSGreg Roach $this->facts = []; 131522e73debSGreg Roach 131622e73debSGreg Roach foreach ($gedcom_facts as $gedcom_fact) { 131722e73debSGreg Roach $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact)); 131822e73debSGreg Roach if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) { 131922e73debSGreg Roach $fact->setPendingDeletion(); 132022e73debSGreg Roach } 132122e73debSGreg Roach $this->facts[] = $fact; 132222e73debSGreg Roach } 132322e73debSGreg Roach foreach ($pending_facts as $pending_fact) { 132422e73debSGreg Roach if (!in_array($pending_fact, $gedcom_facts, true)) { 132522e73debSGreg Roach $fact = new Fact($pending_fact, $this, md5($pending_fact)); 132622e73debSGreg Roach $fact->setPendingAddition(); 132722e73debSGreg Roach $this->facts[] = $fact; 132822e73debSGreg Roach } 132922e73debSGreg Roach } 133022e73debSGreg Roach } 133122e73debSGreg Roach 133222e73debSGreg Roach /** 133322e73debSGreg Roach * Work out whether this record can be shown to a user with a given access level 133422e73debSGreg Roach * 133522e73debSGreg Roach * @param int $access_level 133622e73debSGreg Roach * 133722e73debSGreg Roach * @return bool 133822e73debSGreg Roach */ 133922e73debSGreg Roach private function canShowRecord(int $access_level): bool 134022e73debSGreg Roach { 134122e73debSGreg Roach // This setting would better be called "$ENABLE_PRIVACY" 134222e73debSGreg Roach if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) { 134322e73debSGreg Roach return true; 134422e73debSGreg Roach } 134522e73debSGreg Roach 134622e73debSGreg Roach // We should always be able to see our own record (unless an admin is applying download restrictions) 13477c4add84SGreg Roach if ($this->xref() === $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) { 134822e73debSGreg Roach return true; 134922e73debSGreg Roach } 135022e73debSGreg Roach 135122e73debSGreg Roach // Does this record have a RESN? 135222e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN confidential") !== false) { 135322e73debSGreg Roach return Auth::PRIV_NONE >= $access_level; 135422e73debSGreg Roach } 135522e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN privacy") !== false) { 135622e73debSGreg Roach return Auth::PRIV_USER >= $access_level; 135722e73debSGreg Roach } 135822e73debSGreg Roach if (strpos($this->gedcom, "\n1 RESN none") !== false) { 135922e73debSGreg Roach return true; 136022e73debSGreg Roach } 136122e73debSGreg Roach 136222e73debSGreg Roach // Does this record have a default RESN? 136322e73debSGreg Roach $individual_privacy = $this->tree->getIndividualPrivacy(); 136422e73debSGreg Roach if (isset($individual_privacy[$this->xref()])) { 136522e73debSGreg Roach return $individual_privacy[$this->xref()] >= $access_level; 136622e73debSGreg Roach } 136722e73debSGreg Roach 136822e73debSGreg Roach // Privacy rules do not apply to admins 136922e73debSGreg Roach if (Auth::PRIV_NONE >= $access_level) { 137022e73debSGreg Roach return true; 137122e73debSGreg Roach } 137222e73debSGreg Roach 137322e73debSGreg Roach // Different types of record have different privacy rules 137422e73debSGreg Roach return $this->canShowByType($access_level); 137522e73debSGreg Roach } 1376a25f0a04SGreg Roach} 1377