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