xref: /webtrees/app/Http/RequestHandlers/LocationPage.php (revision a8972480190614ca0d44723ad3201a5b85015140)
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\Services\ClipboardService;
29use Fisharebest\Webtrees\Tree;
30use Illuminate\Support\Collection;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34
35use function array_search;
36use function assert;
37use function is_string;
38use function redirect;
39
40use const PHP_INT_MAX;
41
42/**
43 * Show a location's page.
44 */
45class LocationPage implements RequestHandlerInterface
46{
47    use ViewResponseTrait;
48
49    // Show the repository's facts in this order:
50    private const FACT_ORDER = [
51        1 => '_LOC:NAME',
52        '_LOC:TYPE',
53        '_LOC:_POST',
54        '_LOC:_GOV',
55        '_LOC:MAP',
56        '_LOC:_MAIDENHEAD',
57        '_LOC:RELI',
58        '_LOC:EVEN',
59        '_LOC:_LOC',
60        '_LOC:_DMGD',
61        '_LOC:_AIDN',
62    ];
63
64    /** @var ClipboardService */
65    private $clipboard_service;
66
67    /**
68     * LocationPage constructor.
69     *
70     * @param ClipboardService $clipboard_service
71     */
72    public function __construct(ClipboardService $clipboard_service)
73    {
74        $this->clipboard_service = $clipboard_service;
75    }
76
77    /**
78     * @param ServerRequestInterface $request
79     *
80     * @return ResponseInterface
81     */
82    public function handle(ServerRequestInterface $request): ResponseInterface
83    {
84        $tree = $request->getAttribute('tree');
85        assert($tree instanceof Tree);
86
87        $xref = $request->getAttribute('xref');
88        assert(is_string($xref));
89
90        $location = Registry::locationFactory()->make($xref, $tree);
91        $location = Auth::checkLocationAccess($location, false);
92
93        // Redirect to correct xref/slug
94        if ($location->xref() !== $xref || $request->getAttribute('slug') !== $location->slug()) {
95            return redirect($location->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
96        }
97
98        return $this->viewResponse('gedcom-record-page', [
99            'facts'         => $this->facts($location),
100            'families'      => $location->linkedFamilies('_LOC'),
101            'individuals'   => $location->linkedIndividuals('_LOC'),
102            'notes'         => new Collection(),
103            'media_objects' => new Collection(),
104            'record'        => $location,
105            'sources'       => new Collection(),
106            'title'         => $location->fullName(),
107            'tree'          => $tree,
108        ]);
109    }
110
111    /**
112     * @param Location $location
113     *
114     * @return Collection<Fact>
115     */
116    private function facts(Location $location): Collection
117    {
118        return $location->facts()
119            ->sort(static function (Fact $x, Fact $y): int {
120                $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
121                $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
122
123                return $sort_x <=> $sort_y;
124            });
125    }
126}
127