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 private ClipboardService $clipboard_service; 68 69 /** 70 * SourcePage constructor. 71 * 72 * @param ClipboardService $clipboard_service 73 */ 74 public function __construct(ClipboardService $clipboard_service) 75 { 76 $this->clipboard_service = $clipboard_service; 77 } 78 79 /** 80 * @param ServerRequestInterface $request 81 * 82 * @return ResponseInterface 83 */ 84 public function handle(ServerRequestInterface $request): ResponseInterface 85 { 86 $tree = $request->getAttribute('tree'); 87 assert($tree instanceof Tree); 88 89 $xref = $request->getAttribute('xref'); 90 assert(is_string($xref)); 91 92 $source = Registry::sourceFactory()->make($xref, $tree); 93 $source = Auth::checkSourceAccess($source, false); 94 95 // Redirect to correct xref/slug 96 $slug = Registry::slugFactory()->make($source); 97 98 if ($source->xref() !== $xref || $request->getAttribute('slug') !== $slug) { 99 return redirect($source->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 100 } 101 102 return $this->viewResponse('source-page', [ 103 'clipboard_facts' => $this->clipboard_service->pastableFacts($source), 104 'facts' => $this->facts($source), 105 'families' => $source->linkedFamilies('SOUR'), 106 'individuals' => $source->linkedIndividuals('SOUR'), 107 'meta_description' => '', 108 'meta_robots' => 'index,follow', 109 'notes' => $source->linkedNotes('SOUR'), 110 'media_objects' => $source->linkedMedia('SOUR'), 111 'source' => $source, 112 'title' => $source->fullName(), 113 'tree' => $tree, 114 ]); 115 } 116 117 /** 118 * @param Source $record 119 * 120 * @return Collection<Fact> 121 */ 122 private function facts(Source $record): Collection 123 { 124 return $record->facts() 125 ->sort(static function (Fact $x, Fact $y): int { 126 $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 127 $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 128 129 return $sort_x <=> $sort_y; 130 }); 131 } 132} 133