1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15*89f7189bSGreg 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; 21a25f0a04SGreg Roach 22886b77daSGreg Roachuse Closure; 2354186344SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SourcePage; 248091bfd1SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 252e5b4452SGreg Roach 26a25f0a04SGreg Roach/** 2776692c8bSGreg Roach * A GEDCOM source (SOUR) object. 28a25f0a04SGreg Roach */ 29c1010edaSGreg Roachclass Source extends GedcomRecord 30c1010edaSGreg Roach{ 3116d6367aSGreg Roach public const RECORD_TYPE = 'SOUR'; 3216d6367aSGreg Roach 3354186344SGreg Roach protected const ROUTE_NAME = SourcePage::class; 34a25f0a04SGreg Roach 3576692c8bSGreg Roach /** 36886b77daSGreg Roach * A closure which will create a record from a database row. 37886b77daSGreg Roach * 38bb03c9f0SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 - Use Factory::source() 39bb03c9f0SGreg Roach * 40d5ad3db0SGreg Roach * @param Tree $tree 41d5ad3db0SGreg Roach * 42886b77daSGreg Roach * @return Closure 43886b77daSGreg Roach */ 44d5ad3db0SGreg Roach public static function rowMapper(Tree $tree): Closure 45886b77daSGreg Roach { 466b9cb339SGreg Roach return Registry::sourceFactory()->mapper($tree); 47886b77daSGreg Roach } 48886b77daSGreg Roach 49886b77daSGreg Roach /** 50e71ef9d2SGreg Roach * Get an instance of a source object. For single records, 51e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 52e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 53e71ef9d2SGreg Roach * 54bb03c9f0SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 - Use Factory::source() 55bb03c9f0SGreg Roach * 56e71ef9d2SGreg Roach * @param string $xref 57e71ef9d2SGreg Roach * @param Tree $tree 58e71ef9d2SGreg Roach * @param string|null $gedcom 59e71ef9d2SGreg Roach * 60e71ef9d2SGreg Roach * @return Source|null 61e71ef9d2SGreg Roach */ 62ffc0a61fSGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Source 63c1010edaSGreg Roach { 646b9cb339SGreg Roach return Registry::sourceFactory()->make($xref, $tree, $gedcom); 65e71ef9d2SGreg Roach } 66e71ef9d2SGreg Roach 67e71ef9d2SGreg Roach /** 6876692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 6976692c8bSGreg Roach * 7076692c8bSGreg Roach * @param int $access_level 7176692c8bSGreg Roach * 7276692c8bSGreg Roach * @return bool 7376692c8bSGreg Roach */ 7435584196SGreg Roach protected function canShowByType(int $access_level): bool 75c1010edaSGreg Roach { 76a25f0a04SGreg Roach // Hide sources if they are attached to private repositories ... 77a25f0a04SGreg Roach preg_match_all('/\n1 REPO @(.+)@/', $this->gedcom, $matches); 78a25f0a04SGreg Roach foreach ($matches[1] as $match) { 796b9cb339SGreg Roach $repo = Registry::repositoryFactory()->make($match, $this->tree); 80a25f0a04SGreg Roach if ($repo && !$repo->canShow($access_level)) { 81a25f0a04SGreg Roach return false; 82a25f0a04SGreg Roach } 83a25f0a04SGreg Roach } 84a25f0a04SGreg Roach 85b3a775f6SGreg Roach // ... otherwise apply default behavior 86a25f0a04SGreg Roach return parent::canShowByType($access_level); 87a25f0a04SGreg Roach } 88a25f0a04SGreg Roach 8976692c8bSGreg Roach /** 9076692c8bSGreg Roach * Generate a private version of this record 9176692c8bSGreg Roach * 9276692c8bSGreg Roach * @param int $access_level 9376692c8bSGreg Roach * 9476692c8bSGreg Roach * @return string 9576692c8bSGreg Roach */ 963c90ed31SGreg Roach protected function createPrivateGedcomRecord(int $access_level): string 97c1010edaSGreg Roach { 98a25f0a04SGreg Roach return '0 @' . $this->xref . "@ SOUR\n1 TITL " . I18N::translate('Private'); 99a25f0a04SGreg Roach } 100a25f0a04SGreg Roach 10176692c8bSGreg Roach /** 10276692c8bSGreg Roach * Extract names from the GEDCOM record. 103c7ff4153SGreg Roach * 104c7ff4153SGreg Roach * @return void 10576692c8bSGreg Roach */ 106e364afe4SGreg Roach public function extractNames(): void 107c1010edaSGreg Roach { 108e364afe4SGreg Roach $this->extractNamesFromFacts(1, 'TITL', $this->facts(['TITL'])); 109a25f0a04SGreg Roach } 1108091bfd1SGreg Roach 1118091bfd1SGreg Roach /** 1128091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 1138091bfd1SGreg Roach */ 1148091bfd1SGreg Roach public function lock(): void 1158091bfd1SGreg Roach { 1168091bfd1SGreg Roach DB::table('sources') 1178091bfd1SGreg Roach ->where('s_file', '=', $this->tree->id()) 1188091bfd1SGreg Roach ->where('s_id', '=', $this->xref()) 1198091bfd1SGreg Roach ->lockForUpdate() 1208091bfd1SGreg Roach ->get(); 1218091bfd1SGreg Roach } 122a25f0a04SGreg Roach} 123