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\Statistics\Repository; 19 20use Fisharebest\Webtrees\Date; 21use Fisharebest\Webtrees\Functions\FunctionsPrint; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Statistics\Repository\Interfaces\FamilyDatesRepositoryInterface; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Database\Eloquent\Model; 28use Illuminate\Database\Query\Builder; 29 30/** 31 * A repository providing methods for family dates related statistics (birth, death, marriage, divorce). 32 */ 33class FamilyDatesRepository implements FamilyDatesRepositoryInterface 34{ 35 /** 36 * Sorting directions. 37 */ 38 private const SORT_MIN = 'MIN'; 39 private const SORT_MAX = 'MAX'; 40 41 /** 42 * Event facts. 43 */ 44 private const EVENT_BIRTH = 'BIRT'; 45 private const EVENT_DEATH = 'DEAT'; 46 private const EVENT_MARRIAGE = 'MARR'; 47 private const EVENT_DIVORCE = 'DIV'; 48 49 /** 50 * @var Tree 51 */ 52 private $tree; 53 54 /** 55 * Constructor. 56 * 57 * @param Tree $tree 58 */ 59 public function __construct(Tree $tree) 60 { 61 $this->tree = $tree; 62 } 63 64 /** 65 * Returns the first/last event record for the given event fact. 66 * 67 * @param string $fact 68 * @param string $operation 69 * 70 * @return Model|object|static|null 71 */ 72 private function eventQuery(string $fact, string $operation) 73 { 74 return DB::table('dates') 75 ->select(['d_gid as id', 'd_year as year', 'd_fact AS fact', 'd_type AS type']) 76 ->where('d_file', '=', $this->tree->id()) 77 ->where('d_fact', '=', $fact) 78 ->where('d_julianday1', '=', function (Builder $query) use ($operation, $fact) { 79 $query->selectRaw($operation . '(d_julianday1)') 80 ->from('dates') 81 ->where('d_file', '=', $this->tree->id()) 82 ->where('d_fact', '=', $fact) 83 ->where('d_julianday1', '<>', 0); 84 }) 85 ->first(); 86 } 87 88 /** 89 * Returns the formatted year of the first/last occuring event. 90 * 91 * @param string $type The fact to query 92 * @param string $operation The sorting operation 93 * 94 * @return string 95 */ 96 private function getFirstLastEvent(string $type, string $operation): string 97 { 98 $row = $this->eventQuery($type, $operation); 99 $result = I18N::translate('This information is not available.'); 100 101 if ($row) { 102 $record = GedcomRecord::getInstance($row->id, $this->tree); 103 104 if ($record && $record->canShow()) { 105 $result = $record->formatList(); 106 } else { 107 $result = I18N::translate('This information is private and cannot be shown.'); 108 } 109 } 110 111 return $result; 112 } 113 114 /** 115 * @inheritDoc 116 */ 117 public function firstBirth(): string 118 { 119 return $this->getFirstLastEvent(self::EVENT_BIRTH, self::SORT_MIN); 120 } 121 122 /** 123 * @inheritDoc 124 */ 125 public function lastBirth(): string 126 { 127 return $this->getFirstLastEvent(self::EVENT_BIRTH, self::SORT_MAX); 128 } 129 130 /** 131 * @inheritDoc 132 */ 133 public function firstDeath(): string 134 { 135 return $this->getFirstLastEvent(self::EVENT_DEATH, self::SORT_MIN); 136 } 137 138 /** 139 * @inheritDoc 140 */ 141 public function lastDeath(): string 142 { 143 return $this->getFirstLastEvent(self::EVENT_DEATH, self::SORT_MAX); 144 } 145 146 /** 147 * @inheritDoc 148 */ 149 public function firstMarriage(): string 150 { 151 return $this->getFirstLastEvent(self::EVENT_MARRIAGE, self::SORT_MIN); 152 } 153 154 /** 155 * @inheritDoc 156 */ 157 public function lastMarriage(): string 158 { 159 return $this->getFirstLastEvent(self::EVENT_MARRIAGE, self::SORT_MAX); 160 } 161 162 /** 163 * @inheritDoc 164 */ 165 public function firstDivorce(): string 166 { 167 return $this->getFirstLastEvent(self::EVENT_DIVORCE, self::SORT_MIN); 168 } 169 170 /** 171 * @inheritDoc 172 */ 173 public function lastDivorce(): string 174 { 175 return $this->getFirstLastEvent(self::EVENT_DIVORCE, self::SORT_MAX); 176 } 177 178 /** 179 * Returns the formatted year of the first/last occuring event. 180 * 181 * @param string $type The fact to query 182 * @param string $operation The sorting operation 183 * 184 * @return string 185 */ 186 private function getFirstLastEventYear(string $type, string $operation): string 187 { 188 $row = $this->eventQuery($type, $operation); 189 190 if (!$row) { 191 return ''; 192 } 193 194 if ($row->year < 0) { 195 $row->year = abs($row->year) . ' B.C.'; 196 } 197 198 return (new Date($row->type . ' ' . $row->year)) 199 ->display(); 200 } 201 202 /** 203 * @inheritDoc 204 */ 205 public function firstBirthYear(): string 206 { 207 return $this->getFirstLastEventYear(self::EVENT_BIRTH, self::SORT_MIN); 208 } 209 210 /** 211 * @inheritDoc 212 */ 213 public function lastBirthYear(): string 214 { 215 return $this->getFirstLastEventYear(self::EVENT_BIRTH, self::SORT_MAX); 216 } 217 218 /** 219 * @inheritDoc 220 */ 221 public function firstDeathYear(): string 222 { 223 return $this->getFirstLastEventYear(self::EVENT_DEATH, self::SORT_MIN); 224 } 225 226 /** 227 * @inheritDoc 228 */ 229 public function lastDeathYear(): string 230 { 231 return $this->getFirstLastEventYear(self::EVENT_DEATH, self::SORT_MAX); 232 } 233 234 /** 235 * @inheritDoc 236 */ 237 public function firstMarriageYear(): string 238 { 239 return $this->getFirstLastEventYear(self::EVENT_MARRIAGE, self::SORT_MIN); 240 } 241 242 /** 243 * @inheritDoc 244 */ 245 public function lastMarriageYear(): string 246 { 247 return $this->getFirstLastEventYear(self::EVENT_MARRIAGE, self::SORT_MAX); 248 } 249 250 /** 251 * @inheritDoc 252 */ 253 public function firstDivorceYear(): string 254 { 255 return $this->getFirstLastEventYear(self::EVENT_DIVORCE, self::SORT_MIN); 256 } 257 258 /** 259 * @inheritDoc 260 */ 261 public function lastDivorceYear(): string 262 { 263 return $this->getFirstLastEventYear(self::EVENT_DIVORCE, self::SORT_MAX); 264 } 265 266 /** 267 * Returns the formatted name of the first/last occuring event. 268 * 269 * @param string $type The fact to query 270 * @param string $operation The sorting operation 271 * 272 * @return string 273 */ 274 private function getFirstLastEventName(string $type, string $operation): string 275 { 276 $row = $this->eventQuery($type, $operation); 277 278 if ($row) { 279 $record = GedcomRecord::getInstance($row->id, $this->tree); 280 281 if ($record) { 282 return '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>'; 283 } 284 } 285 286 return ''; 287 } 288 289 /** 290 * @inheritDoc 291 */ 292 public function firstBirthName(): string 293 { 294 return $this->getFirstLastEventName(self::EVENT_BIRTH, self::SORT_MIN); 295 } 296 297 /** 298 * @inheritDoc 299 */ 300 public function lastBirthName(): string 301 { 302 return $this->getFirstLastEventName(self::EVENT_BIRTH, self::SORT_MAX); 303 } 304 305 /** 306 * @inheritDoc 307 */ 308 public function firstDeathName(): string 309 { 310 return $this->getFirstLastEventName(self::EVENT_DEATH, self::SORT_MIN); 311 } 312 313 /** 314 * @inheritDoc 315 */ 316 public function lastDeathName(): string 317 { 318 return $this->getFirstLastEventName(self::EVENT_DEATH, self::SORT_MAX); 319 } 320 321 /** 322 * @inheritDoc 323 */ 324 public function firstMarriageName(): string 325 { 326 return $this->getFirstLastEventName(self::EVENT_MARRIAGE, self::SORT_MIN); 327 } 328 329 /** 330 * @inheritDoc 331 */ 332 public function lastMarriageName(): string 333 { 334 return $this->getFirstLastEventName(self::EVENT_MARRIAGE, self::SORT_MAX); 335 } 336 337 /** 338 * @inheritDoc 339 */ 340 public function firstDivorceName(): string 341 { 342 return $this->getFirstLastEventName(self::EVENT_DIVORCE, self::SORT_MIN); 343 } 344 345 /** 346 * @inheritDoc 347 */ 348 public function lastDivorceName(): string 349 { 350 return $this->getFirstLastEventName(self::EVENT_DIVORCE, self::SORT_MAX); 351 } 352 353 /** 354 * Returns the formatted place of the first/last occuring event. 355 * 356 * @param string $type The fact to query 357 * @param string $operation The sorting operation 358 * 359 * @return string 360 */ 361 private function getFirstLastEventPlace(string $type, string $operation): string 362 { 363 $row = $this->eventQuery($type, $operation); 364 365 if ($row) { 366 $record = GedcomRecord::getInstance($row->id, $this->tree); 367 $fact = null; 368 369 if ($record) { 370 $fact = $record->firstFact($row->fact); 371 } 372 373 if ($fact) { 374 return FunctionsPrint::formatFactPlace($fact, true, true, true); 375 } 376 } 377 378 return I18N::translate('This information is private and cannot be shown.'); 379 } 380 381 /** 382 * @inheritDoc 383 */ 384 public function firstBirthPlace(): string 385 { 386 return $this->getFirstLastEventPlace(self::EVENT_BIRTH, self::SORT_MIN); 387 } 388 389 /** 390 * @inheritDoc 391 */ 392 public function lastBirthPlace(): string 393 { 394 return $this->getFirstLastEventPlace(self::EVENT_BIRTH, self::SORT_MAX); 395 } 396 397 /** 398 * @inheritDoc 399 */ 400 public function firstDeathPlace(): string 401 { 402 return $this->getFirstLastEventPlace(self::EVENT_DEATH, self::SORT_MIN); 403 } 404 405 /** 406 * @inheritDoc 407 */ 408 public function lastDeathPlace(): string 409 { 410 return $this->getFirstLastEventPlace(self::EVENT_DEATH, self::SORT_MAX); 411 } 412 413 /** 414 * @inheritDoc 415 */ 416 public function firstMarriagePlace(): string 417 { 418 return $this->getFirstLastEventPlace(self::EVENT_MARRIAGE, self::SORT_MIN); 419 } 420 421 /** 422 * @inheritDoc 423 */ 424 public function lastMarriagePlace(): string 425 { 426 return $this->getFirstLastEventPlace(self::EVENT_MARRIAGE, self::SORT_MAX); 427 } 428 429 /** 430 * @inheritDoc 431 */ 432 public function firstDivorcePlace(): string 433 { 434 return $this->getFirstLastEventPlace(self::EVENT_DIVORCE, self::SORT_MIN); 435 } 436 437 /** 438 * @inheritDoc 439 */ 440 public function lastDivorcePlace(): string 441 { 442 return $this->getFirstLastEventPlace(self::EVENT_DIVORCE, self::SORT_MAX); 443 } 444} 445