1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20use Closure; 21use Illuminate\Database\Capsule\Manager as DB; 22use stdClass; 23 24/** 25 * A GEDCOM repository (REPO) object. 26 */ 27class Repository extends GedcomRecord 28{ 29 public const RECORD_TYPE = 'REPO'; 30 31 protected const ROUTE_NAME = 'repository'; 32 33 /** 34 * A closure which will create a record from a database row. 35 * 36 * @param Tree $tree 37 * 38 * @return Closure 39 */ 40 public static function rowMapper(Tree $tree): Closure 41 { 42 return function (stdClass $row) use ($tree): Repository { 43 return Repository::getInstance($row->o_id, $tree, $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) 61 { 62 $record = parent::getInstance($xref, $tree, $gedcom); 63 64 if ($record instanceof Repository) { 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 null|string 78 */ 79 protected static function fetchGedcomRecord(string $xref, int $tree_id) 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() 106 { 107 parent::extractNamesFromFacts(1, 'NAME', $this->facts(['NAME'])); 108 } 109} 110