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\Statistics\Repository; 20 21use Fisharebest\Webtrees\Date; 22use Fisharebest\Webtrees\Fact; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\Statistics\Repository\Interfaces\GedcomRepositoryInterface; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Database\Query\Builder; 28 29/** 30 * A repository providing methods for GEDCOM related statistics. 31 */ 32class GedcomRepository implements GedcomRepositoryInterface 33{ 34 /** 35 * @var Tree 36 */ 37 private $tree; 38 39 /** 40 * Constructor. 41 * 42 * @param Tree $tree 43 */ 44 public function __construct(Tree $tree) 45 { 46 $this->tree = $tree; 47 } 48 49 /** 50 * Get information from the GEDCOM's HEAD record. 51 * 52 * @return string[] 53 */ 54 private function gedcomHead(): array 55 { 56 $title = ''; 57 $version = ''; 58 $source = ''; 59 60 $head = GedcomRecord::getInstance('HEAD', $this->tree); 61 62 if ($head instanceof GedcomRecord) { 63 $sour = $head->facts(['SOUR'])->first(); 64 65 if ($sour instanceof Fact) { 66 $source = $sour->value(); 67 $title = $sour->attribute('NAME'); 68 $version = $sour->attribute('VERS'); 69 } 70 } 71 72 return [ 73 $title, 74 $version, 75 $source, 76 ]; 77 } 78 79 /** 80 * @inheritDoc 81 */ 82 public function gedcomFilename(): string 83 { 84 return $this->tree->name(); 85 } 86 87 /** 88 * @inheritDoc 89 */ 90 public function gedcomId(): int 91 { 92 return $this->tree->id(); 93 } 94 95 /** 96 * @inheritDoc 97 */ 98 public function gedcomTitle(): string 99 { 100 return e($this->tree->title()); 101 } 102 103 /** 104 * @inheritDoc 105 */ 106 public function gedcomCreatedSoftware(): string 107 { 108 return $this->gedcomHead()[0]; 109 } 110 111 /** 112 * @inheritDoc 113 */ 114 public function gedcomCreatedVersion(): string 115 { 116 $head = $this->gedcomHead(); 117 118 if ($head === null) { 119 return ''; 120 } 121 122 // Fix broken version string in Family Tree Maker 123 if (strpos($head[1], 'Family Tree Maker ') !== false) { 124 $p = strpos($head[1], '(') + 1; 125 $p2 = strpos($head[1], ')'); 126 $head[1] = substr($head[1], $p, $p2 - $p); 127 } 128 129 // Fix EasyTree version 130 if ($head[2] === 'EasyTree') { 131 $head[1] = substr($head[1], 1); 132 } 133 134 return $head[1]; 135 } 136 137 /** 138 * @inheritDoc 139 */ 140 public function gedcomDate(): string 141 { 142 $head = GedcomRecord::getInstance('HEAD', $this->tree); 143 144 if ($head instanceof GedcomRecord) { 145 $fact = $head->facts(['DATE'])->first(); 146 147 if ($fact instanceof Fact) { 148 return (new Date($fact->value()))->display(); 149 } 150 } 151 152 return ''; 153 } 154 155 /** 156 * @inheritDoc 157 */ 158 public function gedcomUpdated(): string 159 { 160 $row = DB::table('dates') 161 ->select(['d_year', 'd_month', 'd_day']) 162 ->where('d_julianday1', '=', function (Builder $query): void { 163 $query->selectRaw('MAX(d_julianday1)') 164 ->from('dates') 165 ->where('d_file', '=', $this->tree->id()) 166 ->where('d_fact', '=', 'CHAN'); 167 }) 168 ->first(); 169 170 if ($row) { 171 $date = new Date("{$row->d_day} {$row->d_month} {$row->d_year}"); 172 return $date->display(); 173 } 174 175 return $this->gedcomDate(); 176 } 177 178 /** 179 * @inheritDoc 180 */ 181 public function gedcomRootId(): string 182 { 183 return $this->tree->getPreference('PEDIGREE_ROOT_ID'); 184 } 185} 186