xref: /webtrees/app/Http/RequestHandlers/HeaderPage.php (revision 7fe4a237ff223dffab67411f26be01da9d99f93b)
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\Header;
26use Fisharebest\Webtrees\Http\ViewResponseTrait;
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 header's page.
42 */
43class HeaderPage implements RequestHandlerInterface
44{
45    use ViewResponseTrait;
46
47    // Show the header's facts in this order:
48    private const FACT_ORDER = [
49        1 => 'SOUR',
50        'DEST',
51        'DATE',
52        'SUBM',
53        'SUBN',
54        'FILE',
55        'COPR',
56        'GEDC',
57        'CHAR',
58        'LANG',
59        'PLAC',
60        'NOTE',
61    ];
62
63    /**
64     * @param ServerRequestInterface $request
65     *
66     * @return ResponseInterface
67     */
68    public function handle(ServerRequestInterface $request): ResponseInterface
69    {
70        $tree = $request->getAttribute('tree');
71        assert($tree instanceof Tree);
72
73        $xref = $request->getAttribute('xref');
74        assert(is_string($xref));
75
76        $header = Header::getInstance($xref, $tree);
77        $header = Auth::checkHeaderAccess($header, false);
78
79        // Redirect to correct xref/slug
80        if ($header->xref() !== $xref || $request->getAttribute('slug') !== $header->slug()) {
81            return redirect($header->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
82        }
83
84        return $this->viewResponse('gedcom-record-page', [
85            'facts'            => $this->facts($header),
86            'record'           => $header,
87            'families'         => new Collection(),
88            'individuals'      => new Collection(),
89            'media_objects'    => new Collection(),
90            'meta_description' => '',
91            'meta_robots'      => 'index,follow',
92            'notes'            => new Collection(),
93            'sources'          => new Collection(),
94            'title'            => $header->fullName(),
95            'tree'             => $tree,
96        ]);
97    }
98
99    /**
100     * @param Header $record
101     *
102     * @return Collection<Fact>
103     */
104    private function facts(Header $record): Collection
105    {
106        return $record->facts()
107            ->sort(static function (Fact $x, Fact $y): int {
108                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
109                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
110
111                return $sort_x <=> $sort_y;
112            });
113    }
114}
115