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