11635452cSGreg Roach<?php 21635452cSGreg Roach 31635452cSGreg Roach/** 41635452cSGreg Roach * webtrees: online genealogy 5*55134edcSGreg Roach * Copyright (C) 2020 webtrees development team 61635452cSGreg Roach * This program is free software: you can redistribute it and/or modify 71635452cSGreg Roach * it under the terms of the GNU General Public License as published by 81635452cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 91635452cSGreg Roach * (at your option) any later version. 101635452cSGreg Roach * This program is distributed in the hope that it will be useful, 111635452cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 121635452cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131635452cSGreg Roach * GNU General Public License for more details. 141635452cSGreg Roach * You should have received a copy of the GNU General Public License 151635452cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 161635452cSGreg Roach */ 171635452cSGreg Roach 181635452cSGreg Roachdeclare(strict_types=1); 191635452cSGreg Roach 201635452cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 211635452cSGreg Roach 221635452cSGreg Roachuse Fig\Http\Message\StatusCodeInterface; 231635452cSGreg Roachuse Fisharebest\Webtrees\Auth; 241635452cSGreg Roachuse Fisharebest\Webtrees\Fact; 251635452cSGreg Roachuse Fisharebest\Webtrees\Header; 26*55134edcSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 271635452cSGreg Roachuse Fisharebest\Webtrees\Tree; 281635452cSGreg Roachuse Illuminate\Support\Collection; 291635452cSGreg Roachuse Psr\Http\Message\ResponseInterface; 301635452cSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 311635452cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 321635452cSGreg Roach 331635452cSGreg Roachuse function array_search; 341635452cSGreg Roachuse function assert; 351635452cSGreg Roachuse function is_string; 361635452cSGreg Roachuse function redirect; 371635452cSGreg Roach 381635452cSGreg Roachuse const PHP_INT_MAX; 391635452cSGreg Roach 401635452cSGreg Roach/** 411635452cSGreg Roach * Show a header's page. 421635452cSGreg Roach */ 431635452cSGreg Roachclass HeaderPage implements RequestHandlerInterface 441635452cSGreg Roach{ 451635452cSGreg Roach use ViewResponseTrait; 461635452cSGreg Roach 471635452cSGreg Roach // Show the header's facts in this order: 481635452cSGreg Roach private const FACT_ORDER = [ 491635452cSGreg Roach 1 => 'SOUR', 501635452cSGreg Roach 'DEST', 511635452cSGreg Roach 'DATE', 521635452cSGreg Roach 'SUBM', 531635452cSGreg Roach 'SUBN', 541635452cSGreg Roach 'FILE', 551635452cSGreg Roach 'COPR', 561635452cSGreg Roach 'GEDC', 571635452cSGreg Roach 'CHAR', 581635452cSGreg Roach 'LANG', 591635452cSGreg Roach 'PLAC', 601635452cSGreg Roach 'NOTE', 611635452cSGreg Roach ]; 621635452cSGreg Roach 631635452cSGreg Roach /** 641635452cSGreg Roach * @param ServerRequestInterface $request 651635452cSGreg Roach * 661635452cSGreg Roach * @return ResponseInterface 671635452cSGreg Roach */ 681635452cSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 691635452cSGreg Roach { 701635452cSGreg Roach $tree = $request->getAttribute('tree'); 711635452cSGreg Roach assert($tree instanceof Tree); 721635452cSGreg Roach 731635452cSGreg Roach $xref = $request->getAttribute('xref'); 741635452cSGreg Roach assert(is_string($xref)); 751635452cSGreg Roach 761635452cSGreg Roach $header = Header::getInstance($xref, $tree); 771635452cSGreg Roach $header = Auth::checkHeaderAccess($header, false); 781635452cSGreg Roach 791635452cSGreg Roach // Redirect to correct xref/slug 801635452cSGreg Roach if ($header->xref() !== $xref || $request->getAttribute('slug') !== $header->slug()) { 811635452cSGreg Roach return redirect($header->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 821635452cSGreg Roach } 831635452cSGreg Roach 841635452cSGreg Roach return $this->viewResponse('gedcom-record-page', [ 851635452cSGreg Roach 'facts' => $this->facts($header), 861635452cSGreg Roach 'record' => $header, 871635452cSGreg Roach 'families' => new Collection(), 881635452cSGreg Roach 'individuals' => new Collection(), 891635452cSGreg Roach 'media_objects' => new Collection(), 901635452cSGreg Roach 'meta_description' => '', 911635452cSGreg Roach 'meta_robots' => 'index,follow', 921635452cSGreg Roach 'notes' => new Collection(), 931635452cSGreg Roach 'sources' => new Collection(), 941635452cSGreg Roach 'title' => $header->fullName(), 951635452cSGreg Roach 'tree' => $tree, 961635452cSGreg Roach ]); 971635452cSGreg Roach } 981635452cSGreg Roach 991635452cSGreg Roach /** 1001635452cSGreg Roach * @param Header $record 1011635452cSGreg Roach * 1021635452cSGreg Roach * @return Collection<Fact> 1031635452cSGreg Roach */ 1041635452cSGreg Roach private function facts(Header $record): Collection 1051635452cSGreg Roach { 1061635452cSGreg Roach return $record->facts() 1071635452cSGreg Roach ->sort(static function (Fact $x, Fact $y): int { 1081635452cSGreg Roach $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 1091635452cSGreg Roach $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX; 1101635452cSGreg Roach 1111635452cSGreg Roach return $sort_x <=> $sort_y; 1121635452cSGreg Roach }); 1131635452cSGreg Roach } 1141635452cSGreg Roach} 115