xref: /webtrees/app/Http/RequestHandlers/RepositoryPage.php (revision 6fcafd028e511367c3fcdbfaa31aa05a85bf85c0)
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\Registry;
27use Fisharebest\Webtrees\Repository;
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 repository's page.
44 */
45class RepositoryPage implements RequestHandlerInterface
46{
47    use ViewResponseTrait;
48
49    // Show the repository's facts in this order:
50    private const FACT_ORDER = [
51        1 => 'REPO:NAME',
52        'REPO:ADDR',
53        'REPO:NOTE',
54        'REPO:WWW',
55        'REPO:REFN',
56        'REPO:RIN',
57        'REPO:_UID',
58        'REPO:CHAN',
59        'REPO:RESN',
60    ];
61
62    /** @var ClipboardService */
63    private $clipboard_service;
64
65    /**
66     * RepositoryPage constructor.
67     *
68     * @param ClipboardService $clipboard_service
69     */
70    public function __construct(ClipboardService $clipboard_service)
71    {
72        $this->clipboard_service = $clipboard_service;
73    }
74
75    /**
76     * @param ServerRequestInterface $request
77     *
78     * @return ResponseInterface
79     */
80    public function handle(ServerRequestInterface $request): ResponseInterface
81    {
82        $tree = $request->getAttribute('tree');
83        assert($tree instanceof Tree);
84
85        $xref = $request->getAttribute('xref');
86        assert(is_string($xref));
87
88        $repository = Registry::repositoryFactory()->make($xref, $tree);
89        $repository = Auth::checkRepositoryAccess($repository, false);
90
91        // Redirect to correct xref/slug
92        $slug = Registry::slugFactory()->make($repository);
93
94        if ($repository->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
95            return redirect($repository->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
96        }
97
98        return $this->viewResponse('repository-page', [
99            'clipboard_facts'  => $this->clipboard_service->pastableFacts($repository),
100            'facts'            => $this->facts($repository),
101            'meta_description' => '',
102            'meta_robots'      => 'index,follow',
103            'repository'       => $repository,
104            'sources'          => $repository->linkedSources('REPO'),
105            'title'            => $repository->fullName(),
106            'tree'             => $tree,
107        ]);
108    }
109
110    /**
111     * @param Repository $record
112     *
113     * @return Collection<Fact>
114     */
115    private function facts(Repository $record): Collection
116    {
117        return $record->facts()
118            ->sort(static function (Fact $x, Fact $y): int {
119                $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
120                $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
121
122                return $sort_x <=> $sort_y;
123            });
124    }
125}
126