xref: /webtrees/app/Http/RequestHandlers/LocationPage.php (revision edf6ae5b0d35e10e7e82da63bf2168c85a8df026)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://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\Location;
27use Fisharebest\Webtrees\Registry;
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 location's page.
43 */
44class LocationPage implements RequestHandlerInterface
45{
46    use ViewResponseTrait;
47
48    // Show the repository's facts in this order:
49    private const FACT_ORDER = [
50        1 => '_LOC:NAME',
51        '_LOC:TYPE',
52        '_LOC:_POST',
53        '_LOC:_GOV',
54        '_LOC:MAP',
55        '_LOC:_MAIDENHEAD',
56        '_LOC:RELI',
57        '_LOC:EVEN',
58        '_LOC:_LOC',
59        '_LOC:_DMGD',
60        '_LOC:_AIDN',
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        $location = Registry::locationFactory()->make($xref, $tree);
77        $location = Auth::checkLocationAccess($location, false);
78
79        // Redirect to correct xref/slug
80        $slug = Registry::slugFactory()->make($location);
81
82        if ($location->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
83            return redirect($location->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
84        }
85
86        return $this->viewResponse('gedcom-record-page', [
87            'facts'         => $this->facts($location),
88            'families'      => $location->linkedFamilies('_LOC'),
89            'individuals'   => $location->linkedIndividuals('_LOC'),
90            'notes'         => new Collection(),
91            'media_objects' => new Collection(),
92            'record'        => $location,
93            'sources'       => new Collection(),
94            'title'         => $location->fullName(),
95            'tree'          => $tree,
96        ]);
97    }
98
99    /**
100     * @param Location $location
101     *
102     * @return Collection<Fact>
103     */
104    private function facts(Location $location): Collection
105    {
106        return $location->facts()
107            ->sort(static function (Fact $x, Fact $y): int {
108                $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
109                $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
110
111                return $sort_x <=> $sort_y;
112            });
113    }
114}
115