xref: /webtrees/app/Http/RequestHandlers/SubmissionPage.php (revision a091ac74647eab281b25090b737835eeea14ae10)
11635452cSGreg Roach<?php
21635452cSGreg Roach
31635452cSGreg Roach/**
41635452cSGreg Roach * webtrees: online genealogy
5*a091ac74SGreg 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;
25*a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
261635452cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
271635452cSGreg Roachuse Fisharebest\Webtrees\Submission;
281635452cSGreg Roachuse Fisharebest\Webtrees\Tree;
291635452cSGreg Roachuse Illuminate\Support\Collection;
301635452cSGreg Roachuse Psr\Http\Message\ResponseInterface;
311635452cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
321635452cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
331635452cSGreg Roach
341635452cSGreg Roachuse function array_search;
351635452cSGreg Roachuse function assert;
361635452cSGreg Roachuse function is_string;
371635452cSGreg Roachuse function redirect;
381635452cSGreg Roach
391635452cSGreg Roachuse const PHP_INT_MAX;
401635452cSGreg Roach
411635452cSGreg Roach/**
421635452cSGreg Roach * Show a submission's page.
431635452cSGreg Roach */
441635452cSGreg Roachclass SubmissionPage implements RequestHandlerInterface
451635452cSGreg Roach{
461635452cSGreg Roach    use ViewResponseTrait;
471635452cSGreg Roach
481635452cSGreg Roach    // Show the submission's facts in this order:
491635452cSGreg Roach    private const FACT_ORDER = [
501635452cSGreg Roach        1 => 'SUBM',
511635452cSGreg Roach        'FAMF',
521635452cSGreg Roach        'TEMP',
531635452cSGreg Roach        'ANCE',
541635452cSGreg Roach        'DESC',
551635452cSGreg Roach        'ORDI',
561635452cSGreg Roach        'OBJE',
571635452cSGreg Roach        'RIN',
581635452cSGreg Roach        'NOTE',
591635452cSGreg Roach        'CHAN',
601635452cSGreg Roach    ];
611635452cSGreg Roach
621635452cSGreg Roach    /**
631635452cSGreg Roach     * @param ServerRequestInterface $request
641635452cSGreg Roach     *
651635452cSGreg Roach     * @return ResponseInterface
661635452cSGreg Roach     */
671635452cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
681635452cSGreg Roach    {
691635452cSGreg Roach        $tree = $request->getAttribute('tree');
701635452cSGreg Roach        assert($tree instanceof Tree);
711635452cSGreg Roach
721635452cSGreg Roach        $xref = $request->getAttribute('xref');
731635452cSGreg Roach        assert(is_string($xref));
741635452cSGreg Roach
75*a091ac74SGreg Roach        $submission = Factory::submission()->make($xref, $tree);
761635452cSGreg Roach        $submission = Auth::checkSubmissionAccess($submission, false);
771635452cSGreg Roach
781635452cSGreg Roach        // Redirect to correct xref/slug
791635452cSGreg Roach        if ($submission->xref() !== $xref || $request->getAttribute('slug') !== $submission->slug()) {
801635452cSGreg Roach            return redirect($submission->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
811635452cSGreg Roach        }
821635452cSGreg Roach
831635452cSGreg Roach        return $this->viewResponse('gedcom-record-page', [
841635452cSGreg Roach            'facts'            => $this->facts($submission),
851635452cSGreg Roach            'record'           => $submission,
861635452cSGreg Roach            'families'         => new Collection(),
871635452cSGreg Roach            'individuals'      => new Collection(),
881635452cSGreg Roach            'media_objects'    => new Collection(),
891635452cSGreg Roach            'meta_description' => '',
901635452cSGreg Roach            'meta_robots'      => 'index,follow',
911635452cSGreg Roach            'notes'            => new Collection(),
921635452cSGreg Roach            'sources'          => new Collection(),
931635452cSGreg Roach            'title'            => $submission->fullName(),
941635452cSGreg Roach            'tree'             => $tree,
951635452cSGreg Roach        ]);
961635452cSGreg Roach    }
971635452cSGreg Roach
981635452cSGreg Roach    /**
991635452cSGreg Roach     * @param Submission $record
1001635452cSGreg Roach     *
1011635452cSGreg Roach     * @return Collection<Fact>
1021635452cSGreg Roach     */
1031635452cSGreg Roach    private function facts(Submission $record): Collection
1041635452cSGreg Roach    {
1051635452cSGreg Roach        return $record->facts()
1061635452cSGreg Roach            ->sort(static function (Fact $x, Fact $y): int {
1071635452cSGreg Roach                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
1081635452cSGreg Roach                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
1091635452cSGreg Roach
1101635452cSGreg Roach                return $sort_x <=> $sort_y;
1111635452cSGreg Roach            });
1121635452cSGreg Roach    }
1131635452cSGreg Roach}
114