xref: /webtrees/app/Http/RequestHandlers/LocationPage.php (revision 3d0b7eeb53858e294409cf1b659df8a6460ea483)
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    /**
49     * @param ServerRequestInterface $request
50     *
51     * @return ResponseInterface
52     */
53    public function handle(ServerRequestInterface $request): ResponseInterface
54    {
55        $tree = $request->getAttribute('tree');
56        assert($tree instanceof Tree);
57
58        $xref = $request->getAttribute('xref');
59        assert(is_string($xref));
60
61        $record = Registry::locationFactory()->make($xref, $tree);
62        $record = Auth::checkLocationAccess($record, false);
63
64        // Redirect to correct xref/slug
65        $slug = Registry::slugFactory()->make($record);
66
67        if ($record->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
68            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
69        }
70
71        return $this->viewResponse('record-page', [
72            'clipboard_facts'      => new Collection(),
73            'linked_families'      => $record->linkedFamilies($record->tag()),
74            'linked_individuals'   => $record->linkedIndividuals($record->tag()),
75            'linked_media_objects' => null,
76            'linked_notes'         => null,
77            'linked_sources'       => null,
78            'record'               => $record,
79            'title'                => $record->fullName(),
80            'tree'                 => $tree,
81        ]);
82    }
83}
84