xref: /webtrees/app/Http/RequestHandlers/SubmissionPage.php (revision d5b0584dbc729158e086571dbfaad5c22f82f0ef)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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\Submission;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function array_search;
34use function assert;
35use function is_string;
36use function redirect;
37
38use const PHP_INT_MAX;
39
40/**
41 * Show a submission's page.
42 */
43class SubmissionPage implements RequestHandlerInterface
44{
45    use ViewResponseTrait;
46
47    // Show the submission's facts in this order:
48    private const FACT_ORDER = [
49        1 => 'SUBM',
50        'FAMF',
51        'TEMP',
52        'ANCE',
53        'DESC',
54        'ORDI',
55        'OBJE',
56        'RIN',
57        'NOTE',
58        'CHAN',
59    ];
60
61    /**
62     * @param ServerRequestInterface $request
63     *
64     * @return ResponseInterface
65     */
66    public function handle(ServerRequestInterface $request): ResponseInterface
67    {
68        $tree = $request->getAttribute('tree');
69        assert($tree instanceof Tree);
70
71        $xref = $request->getAttribute('xref');
72        assert(is_string($xref));
73
74        $submission = Submission::getInstance($xref, $tree);
75        $submission = Auth::checkSubmissionAccess($submission, false);
76
77        // Redirect to correct xref/slug
78        if ($submission->xref() !== $xref || $request->getAttribute('slug') !== $submission->slug()) {
79            return redirect($submission->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
80        }
81
82        return $this->viewResponse('gedcom-record-page', [
83            'facts'            => $this->facts($submission),
84            'record'           => $submission,
85            'families'         => new Collection(),
86            'individuals'      => new Collection(),
87            'media_objects'    => new Collection(),
88            'meta_description' => '',
89            'meta_robots'      => 'index,follow',
90            'notes'            => new Collection(),
91            'sources'          => new Collection(),
92            'title'            => $submission->fullName(),
93            'tree'             => $tree,
94        ]);
95    }
96
97    /**
98     * @param Submission $record
99     *
100     * @return Collection<Fact>
101     */
102    private function facts(Submission $record): Collection
103    {
104        return $record->facts()
105            ->sort(static function (Fact $x, Fact $y): int {
106                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
107                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
108
109                return $sort_x <=> $sort_y;
110            });
111    }
112}
113