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 */ 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\Http\ViewResponseTrait; 26use Fisharebest\Webtrees\Repository; 27use Fisharebest\Webtrees\Services\ClipboardService; 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 repository's page. 43 */ 44class RepositoryPage implements RequestHandlerInterface 45{ 46 use ViewResponseTrait; 47 48 // Show the repository's facts in this order: 49 private const FACT_ORDER = [ 50 1 => 'NAME', 51 'ADDR', 52 'NOTE', 53 'WWW', 54 'REFN', 55 'RIN', 56 '_UID', 57 'CHAN', 58 'RESN', 59 ]; 60 61 /** @var ClipboardService */ 62 private $clipboard_service; 63 64 /** 65 * RepositoryPage constructor. 66 * 67 * @param ClipboardService $clipboard_service 68 */ 69 public function __construct(ClipboardService $clipboard_service) 70 { 71 $this->clipboard_service = $clipboard_service; 72 } 73 74 /** 75 * @param ServerRequestInterface $request 76 * 77 * @return ResponseInterface 78 */ 79 public function handle(ServerRequestInterface $request): ResponseInterface 80 { 81 $tree = $request->getAttribute('tree'); 82 assert($tree instanceof Tree); 83 84 $xref = $request->getAttribute('xref'); 85 assert(is_string($xref)); 86 87 $repository = Repository::getInstance($xref, $tree); 88 $repository = Auth::checkRepositoryAccess($repository, false); 89 90 // Redirect to correct xref/slug 91 if ($repository->xref() !== $xref || $request->getAttribute('slug') !== $repository->slug()) { 92 return redirect($repository->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 93 } 94 95 return $this->viewResponse('repository-page', [ 96 'clipboard_facts' => $this->clipboard_service->pastableFacts($repository, new Collection()), 97 'facts' => $this->facts($repository), 98 'meta_robots' => 'index,follow', 99 'repository' => $repository, 100 'sources' => $repository->linkedSources('REPO'), 101 'title' => $repository->fullName(), 102 'tree' => $tree, 103 ]); 104 } 105 106 /** 107 * @param Repository $record 108 * 109 * @return Collection 110 */ 111 private function facts(Repository $record): Collection 112 { 113 return $record->facts() 114 ->sort(static function (Fact $x, Fact $y): int { 115 $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 116 $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 117 118 return $sort_x <=> $sort_y; 119 }); 120 } 121} 122