154186344SGreg Roach<?php 254186344SGreg Roach 354186344SGreg Roach/** 454186344SGreg Roach * webtrees: online genealogy 554186344SGreg Roach * Copyright (C) 2019 webtrees development team 654186344SGreg Roach * This program is free software: you can redistribute it and/or modify 754186344SGreg Roach * it under the terms of the GNU General Public License as published by 854186344SGreg Roach * the Free Software Foundation, either version 3 of the License, or 954186344SGreg Roach * (at your option) any later version. 1054186344SGreg Roach * This program is distributed in the hope that it will be useful, 1154186344SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1254186344SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1354186344SGreg Roach * GNU General Public License for more details. 1454186344SGreg Roach * You should have received a copy of the GNU General Public License 1554186344SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1654186344SGreg Roach */ 1754186344SGreg Roach 1854186344SGreg Roachdeclare(strict_types=1); 1954186344SGreg Roach 2054186344SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 2154186344SGreg Roach 22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 2354186344SGreg Roachuse Fisharebest\Webtrees\Auth; 2454186344SGreg Roachuse Fisharebest\Webtrees\Fact; 2554186344SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 2654186344SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService; 2754186344SGreg Roachuse Fisharebest\Webtrees\Source; 2854186344SGreg Roachuse Fisharebest\Webtrees\Tree; 2954186344SGreg Roachuse Illuminate\Support\Collection; 3054186344SGreg Roachuse Psr\Http\Message\ResponseInterface; 3154186344SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3254186344SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3354186344SGreg Roach 3454186344SGreg Roachuse function array_search; 3554186344SGreg Roachuse function assert; 3654186344SGreg Roachuse function is_string; 3754186344SGreg Roachuse function redirect; 3854186344SGreg Roach 3954186344SGreg Roachuse const PHP_INT_MAX; 4054186344SGreg Roach 4154186344SGreg Roach/** 4254186344SGreg Roach * Show a source's page. 4354186344SGreg Roach */ 4454186344SGreg Roachclass SourcePage implements RequestHandlerInterface 4554186344SGreg Roach{ 4654186344SGreg Roach use ViewResponseTrait; 4754186344SGreg Roach 4854186344SGreg Roach // Show the source's facts in this order: 4954186344SGreg Roach private const FACT_ORDER = [ 5054186344SGreg Roach 1 => 'TITL', 5154186344SGreg Roach 'ABBR', 5254186344SGreg Roach 'AUTH', 5354186344SGreg Roach 'DATA', 5454186344SGreg Roach 'PUBL', 5554186344SGreg Roach 'TEXT', 5654186344SGreg Roach 'REPO', 5754186344SGreg Roach 'NOTE', 5854186344SGreg Roach 'OBJE', 5954186344SGreg Roach 'REFN', 6054186344SGreg Roach 'RIN', 6154186344SGreg Roach '_UID', 6254186344SGreg Roach 'CHAN', 6354186344SGreg Roach 'RESN', 6454186344SGreg Roach ]; 6554186344SGreg Roach 6654186344SGreg Roach /** @var ClipboardService */ 6754186344SGreg Roach private $clipboard_service; 6854186344SGreg Roach 6954186344SGreg Roach /** 7054186344SGreg Roach * SourcePage constructor. 7154186344SGreg Roach * 7254186344SGreg Roach * @param ClipboardService $clipboard_service 7354186344SGreg Roach */ 7454186344SGreg Roach public function __construct(ClipboardService $clipboard_service) 7554186344SGreg Roach { 7654186344SGreg Roach $this->clipboard_service = $clipboard_service; 7754186344SGreg Roach } 7854186344SGreg Roach 7954186344SGreg Roach /** 8054186344SGreg Roach * @param ServerRequestInterface $request 8154186344SGreg Roach * 8254186344SGreg Roach * @return ResponseInterface 8354186344SGreg Roach */ 8454186344SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 8554186344SGreg Roach { 8654186344SGreg Roach $tree = $request->getAttribute('tree'); 8754186344SGreg Roach assert($tree instanceof Tree); 8854186344SGreg Roach 8954186344SGreg Roach $xref = $request->getAttribute('xref'); 9054186344SGreg Roach assert(is_string($xref)); 9154186344SGreg Roach 9254186344SGreg Roach $source = Source::getInstance($xref, $tree); 9354186344SGreg Roach $source = Auth::checkSourceAccess($source, false); 9454186344SGreg Roach 9554186344SGreg Roach // Redirect to correct xref/slug 9654186344SGreg Roach if ($source->xref() !== $xref || $request->getAttribute('slug') !== $source->slug()) { 97e5d858f5SGreg Roach return redirect($source->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 9854186344SGreg Roach } 9954186344SGreg Roach 10054186344SGreg Roach return $this->viewResponse('source-page', [ 10154186344SGreg Roach 'clipboard_facts' => $this->clipboard_service->pastableFacts($source, new Collection()), 10254186344SGreg Roach 'facts' => $this->facts($source), 10354186344SGreg Roach 'families' => $source->linkedFamilies('SOUR'), 10454186344SGreg Roach 'individuals' => $source->linkedIndividuals('SOUR'), 10554186344SGreg Roach 'meta_robots' => 'index,follow', 10654186344SGreg Roach 'notes' => $source->linkedNotes('SOUR'), 10754186344SGreg Roach 'media_objects' => $source->linkedMedia('SOUR'), 10854186344SGreg Roach 'source' => $source, 10954186344SGreg Roach 'title' => $source->fullName(), 11054186344SGreg Roach 'tree' => $tree, 11154186344SGreg Roach ]); 11254186344SGreg Roach } 11354186344SGreg Roach 11454186344SGreg Roach /** 11554186344SGreg Roach * @param Source $record 11654186344SGreg Roach * 117*b5c8fd7eSGreg Roach * @return Collection<Fact> 11854186344SGreg Roach */ 11954186344SGreg Roach private function facts(Source $record): Collection 12054186344SGreg Roach { 12154186344SGreg Roach return $record->facts() 12254186344SGreg Roach ->sort(static function (Fact $x, Fact $y): int { 12354186344SGreg Roach $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 12454186344SGreg Roach $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 12554186344SGreg Roach 12654186344SGreg Roach return $sort_x <=> $sort_y; 12754186344SGreg Roach }); 12854186344SGreg Roach } 12954186344SGreg Roach} 130