xref: /webtrees/app/Http/RequestHandlers/SubmissionPage.php (revision 1635452c5b89c89ae049ee7ed6a6c423bdd28af4)
1*1635452cSGreg Roach<?php
2*1635452cSGreg Roach
3*1635452cSGreg Roach/**
4*1635452cSGreg Roach * webtrees: online genealogy
5*1635452cSGreg Roach * Copyright (C) 2019 webtrees development team
6*1635452cSGreg Roach * This program is free software: you can redistribute it and/or modify
7*1635452cSGreg Roach * it under the terms of the GNU General Public License as published by
8*1635452cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*1635452cSGreg Roach * (at your option) any later version.
10*1635452cSGreg Roach * This program is distributed in the hope that it will be useful,
11*1635452cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*1635452cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*1635452cSGreg Roach * GNU General Public License for more details.
14*1635452cSGreg Roach * You should have received a copy of the GNU General Public License
15*1635452cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*1635452cSGreg Roach */
17*1635452cSGreg Roach
18*1635452cSGreg Roachdeclare(strict_types=1);
19*1635452cSGreg Roach
20*1635452cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*1635452cSGreg Roach
22*1635452cSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23*1635452cSGreg Roachuse Fisharebest\Webtrees\Auth;
24*1635452cSGreg Roachuse Fisharebest\Webtrees\Fact;
25*1635452cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26*1635452cSGreg Roachuse Fisharebest\Webtrees\Submission;
27*1635452cSGreg Roachuse Fisharebest\Webtrees\Tree;
28*1635452cSGreg Roachuse Illuminate\Support\Collection;
29*1635452cSGreg Roachuse Psr\Http\Message\ResponseInterface;
30*1635452cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31*1635452cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32*1635452cSGreg Roach
33*1635452cSGreg Roachuse function array_search;
34*1635452cSGreg Roachuse function assert;
35*1635452cSGreg Roachuse function is_string;
36*1635452cSGreg Roachuse function redirect;
37*1635452cSGreg Roach
38*1635452cSGreg Roachuse const PHP_INT_MAX;
39*1635452cSGreg Roach
40*1635452cSGreg Roach/**
41*1635452cSGreg Roach * Show a submission's page.
42*1635452cSGreg Roach */
43*1635452cSGreg Roachclass SubmissionPage implements RequestHandlerInterface
44*1635452cSGreg Roach{
45*1635452cSGreg Roach    use ViewResponseTrait;
46*1635452cSGreg Roach
47*1635452cSGreg Roach    // Show the submission's facts in this order:
48*1635452cSGreg Roach    private const FACT_ORDER = [
49*1635452cSGreg Roach        1 => 'SUBM',
50*1635452cSGreg Roach        'FAMF',
51*1635452cSGreg Roach        'TEMP',
52*1635452cSGreg Roach        'ANCE',
53*1635452cSGreg Roach        'DESC',
54*1635452cSGreg Roach        'ORDI',
55*1635452cSGreg Roach        'OBJE',
56*1635452cSGreg Roach        'RIN',
57*1635452cSGreg Roach        'NOTE',
58*1635452cSGreg Roach        'CHAN',
59*1635452cSGreg Roach    ];
60*1635452cSGreg Roach
61*1635452cSGreg Roach    /**
62*1635452cSGreg Roach     * @param ServerRequestInterface $request
63*1635452cSGreg Roach     *
64*1635452cSGreg Roach     * @return ResponseInterface
65*1635452cSGreg Roach     */
66*1635452cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
67*1635452cSGreg Roach    {
68*1635452cSGreg Roach        $tree = $request->getAttribute('tree');
69*1635452cSGreg Roach        assert($tree instanceof Tree);
70*1635452cSGreg Roach
71*1635452cSGreg Roach        $xref = $request->getAttribute('xref');
72*1635452cSGreg Roach        assert(is_string($xref));
73*1635452cSGreg Roach
74*1635452cSGreg Roach        $submission = Submission::getInstance($xref, $tree);
75*1635452cSGreg Roach        $submission = Auth::checkSubmissionAccess($submission, false);
76*1635452cSGreg Roach
77*1635452cSGreg Roach        // Redirect to correct xref/slug
78*1635452cSGreg Roach        if ($submission->xref() !== $xref || $request->getAttribute('slug') !== $submission->slug()) {
79*1635452cSGreg Roach            return redirect($submission->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
80*1635452cSGreg Roach        }
81*1635452cSGreg Roach
82*1635452cSGreg Roach        return $this->viewResponse('gedcom-record-page', [
83*1635452cSGreg Roach            'facts'            => $this->facts($submission),
84*1635452cSGreg Roach            'record'           => $submission,
85*1635452cSGreg Roach            'families'         => new Collection(),
86*1635452cSGreg Roach            'individuals'      => new Collection(),
87*1635452cSGreg Roach            'media_objects'    => new Collection(),
88*1635452cSGreg Roach            'meta_description' => '',
89*1635452cSGreg Roach            'meta_robots'      => 'index,follow',
90*1635452cSGreg Roach            'notes'            => new Collection(),
91*1635452cSGreg Roach            'sources'          => new Collection(),
92*1635452cSGreg Roach            'title'            => $submission->fullName(),
93*1635452cSGreg Roach            'tree'             => $tree,
94*1635452cSGreg Roach        ]);
95*1635452cSGreg Roach    }
96*1635452cSGreg Roach
97*1635452cSGreg Roach    /**
98*1635452cSGreg Roach     * @param Submission $record
99*1635452cSGreg Roach     *
100*1635452cSGreg Roach     * @return Collection<Fact>
101*1635452cSGreg Roach     */
102*1635452cSGreg Roach    private function facts(Submission $record): Collection
103*1635452cSGreg Roach    {
104*1635452cSGreg Roach        return $record->facts()
105*1635452cSGreg Roach            ->sort(static function (Fact $x, Fact $y): int {
106*1635452cSGreg Roach                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
107*1635452cSGreg Roach                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
108*1635452cSGreg Roach
109*1635452cSGreg Roach                return $sort_x <=> $sort_y;
110*1635452cSGreg Roach            });
111*1635452cSGreg Roach    }
112*1635452cSGreg Roach}
113