1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees; 20 21use Closure; 22use Exception; 23use Illuminate\Database\Capsule\Manager as DB; 24use stdClass; 25 26/** 27 * A GEDCOM repository (REPO) object. 28 */ 29class Repository extends GedcomRecord 30{ 31 public const RECORD_TYPE = 'REPO'; 32 33 protected const ROUTE_NAME = 'repository'; 34 35 /** 36 * A closure which will create a record from a database row. 37 * 38 * @return Closure 39 */ 40 public static function rowMapper(): Closure 41 { 42 return static function (stdClass $row): Repository { 43 return Repository::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom); 44 }; 45 } 46 47 /** 48 * Get an instance of a repository object. For single records, 49 * we just receive the XREF. For bulk records (such as lists 50 * and search results) we can receive the GEDCOM data as well. 51 * 52 * @param string $xref 53 * @param Tree $tree 54 * @param string|null $gedcom 55 * 56 * @throws Exception 57 * 58 * @return Repository|null 59 */ 60 public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self 61 { 62 $record = parent::getInstance($xref, $tree, $gedcom); 63 64 if ($record instanceof self) { 65 return $record; 66 } 67 68 return null; 69 } 70 71 /** 72 * Fetch data from the database 73 * 74 * @param string $xref 75 * @param int $tree_id 76 * 77 * @return string|null 78 */ 79 protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 80 { 81 return DB::table('other') 82 ->where('o_id', '=', $xref) 83 ->where('o_file', '=', $tree_id) 84 ->where('o_type', '=', self::RECORD_TYPE) 85 ->value('o_gedcom'); 86 } 87 88 /** 89 * Generate a private version of this record 90 * 91 * @param int $access_level 92 * 93 * @return string 94 */ 95 protected function createPrivateGedcomRecord(int $access_level): string 96 { 97 return '0 @' . $this->xref . "@ REPO\n1 NAME " . I18N::translate('Private'); 98 } 99 100 /** 101 * Extract names from the GEDCOM record. 102 * 103 * @return void 104 */ 105 public function extractNames(): void 106 { 107 $this->extractNamesFromFacts(1, 'NAME', $this->facts(['NAME'])); 108 } 109} 110