1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fig\Http\Message\StatusCodeInterface; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Fact; 25use Fisharebest\Webtrees\Header; 26use Fisharebest\Webtrees\Http\ViewResponseTrait; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Tree; 29use Illuminate\Support\Collection; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function array_search; 35use function assert; 36use function is_string; 37use function redirect; 38 39use const PHP_INT_MAX; 40 41/** 42 * Show a header's page. 43 */ 44class HeaderPage implements RequestHandlerInterface 45{ 46 use ViewResponseTrait; 47 48 // Show the header's facts in this order: 49 private const FACT_ORDER = [ 50 1 => 'HEAD:SOUR', 51 'HEAD:DEST', 52 'HEAD:DATE', 53 'HEAD:SUBM', 54 'HEAD:SUBN', 55 'HEAD:FILE', 56 'HEAD:COPR', 57 'HEAD:GEDC', 58 'HEAD:CHAR', 59 'HEAD:LANG', 60 'HEAD:PLAC', 61 'HEAD:NOTE', 62 ]; 63 64 /** 65 * @param ServerRequestInterface $request 66 * 67 * @return ResponseInterface 68 */ 69 public function handle(ServerRequestInterface $request): ResponseInterface 70 { 71 $tree = $request->getAttribute('tree'); 72 assert($tree instanceof Tree); 73 74 $xref = $request->getAttribute('xref'); 75 assert(is_string($xref)); 76 77 $header = Registry::headerFactory()->make($xref, $tree); 78 $header = Auth::checkHeaderAccess($header, false); 79 80 // Redirect to correct xref/slug 81 $slug = Registry::slugFactory()->make($header); 82 83 if ($header->xref() !== $xref || $request->getAttribute('slug') !== $slug) { 84 return redirect($header->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 85 } 86 87 return $this->viewResponse('gedcom-record-page', [ 88 'facts' => $this->facts($header), 89 'record' => $header, 90 'families' => new Collection(), 91 'individuals' => new Collection(), 92 'media_objects' => new Collection(), 93 'meta_description' => '', 94 'meta_robots' => 'index,follow', 95 'notes' => new Collection(), 96 'sources' => new Collection(), 97 'title' => $header->fullName(), 98 'tree' => $tree, 99 ]); 100 } 101 102 /** 103 * @param Header $record 104 * 105 * @return Collection<Fact> 106 */ 107 private function facts(Header $record): Collection 108 { 109 return $record->facts() 110 ->sort(static function (Fact $x, Fact $y): int { 111 $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 112 $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 113 114 return $sort_x <=> $sort_y; 115 }); 116 } 117} 118