154186344SGreg Roach<?php 254186344SGreg Roach 354186344SGreg Roach/** 454186344SGreg Roach * webtrees: online genealogy 5*a091ac74SGreg Roach * Copyright (C) 2020 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; 25*a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory; 2654186344SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 2754186344SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService; 2854186344SGreg Roachuse Fisharebest\Webtrees\Source; 2954186344SGreg Roachuse Fisharebest\Webtrees\Tree; 3054186344SGreg Roachuse Illuminate\Support\Collection; 3154186344SGreg Roachuse Psr\Http\Message\ResponseInterface; 3254186344SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3354186344SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3454186344SGreg Roach 3554186344SGreg Roachuse function array_search; 3654186344SGreg Roachuse function assert; 3754186344SGreg Roachuse function is_string; 3854186344SGreg Roachuse function redirect; 3954186344SGreg Roach 4054186344SGreg Roachuse const PHP_INT_MAX; 4154186344SGreg Roach 4254186344SGreg Roach/** 4354186344SGreg Roach * Show a source's page. 4454186344SGreg Roach */ 4554186344SGreg Roachclass SourcePage implements RequestHandlerInterface 4654186344SGreg Roach{ 4754186344SGreg Roach use ViewResponseTrait; 4854186344SGreg Roach 4954186344SGreg Roach // Show the source's facts in this order: 5054186344SGreg Roach private const FACT_ORDER = [ 5154186344SGreg Roach 1 => 'TITL', 5254186344SGreg Roach 'ABBR', 5354186344SGreg Roach 'AUTH', 5454186344SGreg Roach 'DATA', 5554186344SGreg Roach 'PUBL', 5654186344SGreg Roach 'TEXT', 5754186344SGreg Roach 'REPO', 5854186344SGreg Roach 'NOTE', 5954186344SGreg Roach 'OBJE', 6054186344SGreg Roach 'REFN', 6154186344SGreg Roach 'RIN', 6254186344SGreg Roach '_UID', 6354186344SGreg Roach 'CHAN', 6454186344SGreg Roach 'RESN', 6554186344SGreg Roach ]; 6654186344SGreg Roach 6754186344SGreg Roach /** @var ClipboardService */ 6854186344SGreg Roach private $clipboard_service; 6954186344SGreg Roach 7054186344SGreg Roach /** 7154186344SGreg Roach * SourcePage constructor. 7254186344SGreg Roach * 7354186344SGreg Roach * @param ClipboardService $clipboard_service 7454186344SGreg Roach */ 7554186344SGreg Roach public function __construct(ClipboardService $clipboard_service) 7654186344SGreg Roach { 7754186344SGreg Roach $this->clipboard_service = $clipboard_service; 7854186344SGreg Roach } 7954186344SGreg Roach 8054186344SGreg Roach /** 8154186344SGreg Roach * @param ServerRequestInterface $request 8254186344SGreg Roach * 8354186344SGreg Roach * @return ResponseInterface 8454186344SGreg Roach */ 8554186344SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 8654186344SGreg Roach { 8754186344SGreg Roach $tree = $request->getAttribute('tree'); 8854186344SGreg Roach assert($tree instanceof Tree); 8954186344SGreg Roach 9054186344SGreg Roach $xref = $request->getAttribute('xref'); 9154186344SGreg Roach assert(is_string($xref)); 9254186344SGreg Roach 93*a091ac74SGreg Roach $source = Factory::source()->make($xref, $tree); 9454186344SGreg Roach $source = Auth::checkSourceAccess($source, false); 9554186344SGreg Roach 9654186344SGreg Roach // Redirect to correct xref/slug 9754186344SGreg Roach if ($source->xref() !== $xref || $request->getAttribute('slug') !== $source->slug()) { 98e5d858f5SGreg Roach return redirect($source->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 9954186344SGreg Roach } 10054186344SGreg Roach 10154186344SGreg Roach return $this->viewResponse('source-page', [ 10254186344SGreg Roach 'clipboard_facts' => $this->clipboard_service->pastableFacts($source, new Collection()), 10354186344SGreg Roach 'facts' => $this->facts($source), 10454186344SGreg Roach 'families' => $source->linkedFamilies('SOUR'), 10554186344SGreg Roach 'individuals' => $source->linkedIndividuals('SOUR'), 1062406e0e0SGreg Roach 'meta_description' => '', 10754186344SGreg Roach 'meta_robots' => 'index,follow', 10854186344SGreg Roach 'notes' => $source->linkedNotes('SOUR'), 10954186344SGreg Roach 'media_objects' => $source->linkedMedia('SOUR'), 11054186344SGreg Roach 'source' => $source, 11154186344SGreg Roach 'title' => $source->fullName(), 11254186344SGreg Roach 'tree' => $tree, 11354186344SGreg Roach ]); 11454186344SGreg Roach } 11554186344SGreg Roach 11654186344SGreg Roach /** 11754186344SGreg Roach * @param Source $record 11854186344SGreg Roach * 119b5c8fd7eSGreg Roach * @return Collection<Fact> 12054186344SGreg Roach */ 12154186344SGreg Roach private function facts(Source $record): Collection 12254186344SGreg Roach { 12354186344SGreg Roach return $record->facts() 12454186344SGreg Roach ->sort(static function (Fact $x, Fact $y): int { 12554186344SGreg Roach $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 12654186344SGreg Roach $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 12754186344SGreg Roach 12854186344SGreg Roach return $sort_x <=> $sort_y; 12954186344SGreg Roach }); 13054186344SGreg Roach } 13154186344SGreg Roach} 132