Completed
Push — master ( e1052c...4a8100 )
by Greg
07:45
created

LocationPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fig\Http\Message\StatusCodeInterface;
23
use Fisharebest\Webtrees\Auth;
24
use Fisharebest\Webtrees\Fact;
25
use Fisharebest\Webtrees\Http\ViewResponseTrait;
26
use Fisharebest\Webtrees\Location;
27
use Fisharebest\Webtrees\Registry;
28
use Fisharebest\Webtrees\Services\ClipboardService;
29
use Fisharebest\Webtrees\Tree;
30
use Illuminate\Support\Collection;
31
use Psr\Http\Message\ResponseInterface;
32
use Psr\Http\Message\ServerRequestInterface;
33
use Psr\Http\Server\RequestHandlerInterface;
34
35
use function array_search;
36
use function assert;
37
use function is_string;
38
use function redirect;
39
40
use const PHP_INT_MAX;
41
42
/**
43
 * Show a location's page.
44
 */
45
class 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
    /**
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
        $location = Registry::locationFactory()->make($xref, $tree);
78
        $location = Auth::checkLocationAccess($location, false);
79
80
        // Redirect to correct xref/slug
81
        if ($location->xref() !== $xref || $request->getAttribute('slug') !== $location->slug()) {
82
            return redirect($location->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
83
        }
84
85
        return $this->viewResponse('gedcom-record-page', [
86
            'facts'         => $this->facts($location),
87
            'families'      => $location->linkedFamilies('_LOC'),
88
            'individuals'   => $location->linkedIndividuals('_LOC'),
89
            'notes'         => new Collection(),
90
            'media_objects' => new Collection(),
91
            'record'        => $location,
92
            'sources'       => new Collection(),
93
            'title'         => $location->fullName(),
94
            'tree'          => $tree,
95
        ]);
96
    }
97
98
    /**
99
     * @param Location $location
100
     *
101
     * @return Collection<Fact>
102
     */
103
    private function facts(Location $location): Collection
104
    {
105
        return $location->facts()
106
            ->sort(static function (Fact $x, Fact $y): int {
107
                $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
108
                $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
109
110
                return $sort_x <=> $sort_y;
111
            });
112
    }
113
}
114