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\Http\ViewResponseTrait; 26use Fisharebest\Webtrees\Registry; 27use Fisharebest\Webtrees\Services\ClipboardService; 28use Fisharebest\Webtrees\Source; 29use Fisharebest\Webtrees\Tree; 30use Illuminate\Support\Collection; 31use Psr\Http\Message\ResponseInterface; 32use Psr\Http\Message\ServerRequestInterface; 33use Psr\Http\Server\RequestHandlerInterface; 34 35use function array_search; 36use function assert; 37use function is_string; 38use function redirect; 39 40use const PHP_INT_MAX; 41 42/** 43 * Show a source's page. 44 */ 45class SourcePage implements RequestHandlerInterface 46{ 47 use ViewResponseTrait; 48 49 // Show the source's facts in this order: 50 private const FACT_ORDER = [ 51 1 => 'SOUR:TITL', 52 'SOUR:ABBR', 53 'SOUR:AUTH', 54 'SOUR:DATA', 55 'SOUR:PUBL', 56 'SOUR:TEXT', 57 'SOUR:REPO', 58 'SOUR:NOTE', 59 'SOUR:OBJE', 60 'SOUR:REFN', 61 'SOUR:RIN', 62 'SOUR:_UID', 63 'SOUR:CHAN', 64 'SOUR:RESN', 65 ]; 66 67 /** @var ClipboardService */ 68 private $clipboard_service; 69 70 /** 71 * SourcePage constructor. 72 * 73 * @param ClipboardService $clipboard_service 74 */ 75 public function __construct(ClipboardService $clipboard_service) 76 { 77 $this->clipboard_service = $clipboard_service; 78 } 79 80 /** 81 * @param ServerRequestInterface $request 82 * 83 * @return ResponseInterface 84 */ 85 public function handle(ServerRequestInterface $request): ResponseInterface 86 { 87 $tree = $request->getAttribute('tree'); 88 assert($tree instanceof Tree); 89 90 $xref = $request->getAttribute('xref'); 91 assert(is_string($xref)); 92 93 $source = Registry::sourceFactory()->make($xref, $tree); 94 $source = Auth::checkSourceAccess($source, false); 95 96 // Redirect to correct xref/slug 97 $slug = Registry::slugFactory()->make($source); 98 99 if ($source->xref() !== $xref || $request->getAttribute('slug') !== $slug) { 100 return redirect($source->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 101 } 102 103 return $this->viewResponse('source-page', [ 104 'clipboard_facts' => $this->clipboard_service->pastableFacts($source), 105 'facts' => $this->facts($source), 106 'families' => $source->linkedFamilies('SOUR'), 107 'individuals' => $source->linkedIndividuals('SOUR'), 108 'meta_description' => '', 109 'meta_robots' => 'index,follow', 110 'notes' => $source->linkedNotes('SOUR'), 111 'media_objects' => $source->linkedMedia('SOUR'), 112 'source' => $source, 113 'title' => $source->fullName(), 114 'tree' => $tree, 115 ]); 116 } 117 118 /** 119 * @param Source $record 120 * 121 * @return Collection<Fact> 122 */ 123 private function facts(Source $record): Collection 124 { 125 return $record->facts() 126 ->sort(static function (Fact $x, Fact $y): int { 127 $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 128 $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 129 130 return $sort_x <=> $sort_y; 131 }); 132 } 133} 134