xref: /webtrees/app/Http/RequestHandlers/RepositoryPage.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Fact;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\Repository;
26use Fisharebest\Webtrees\Services\ClipboardService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function array_search;
34use function assert;
35use function is_string;
36use function redirect;
37
38use const PHP_INT_MAX;
39
40/**
41 * Show a repository's page.
42 */
43class RepositoryPage implements RequestHandlerInterface
44{
45    use ViewResponseTrait;
46
47    // Show the repository's facts in this order:
48    private const FACT_ORDER = [
49        1 => 'NAME',
50        'ADDR',
51        'NOTE',
52        'WWW',
53        'REFN',
54        'RIN',
55        '_UID',
56        'CHAN',
57        'RESN',
58    ];
59
60    /** @var ClipboardService */
61    private $clipboard_service;
62
63    /**
64     * RepositoryPage constructor.
65     *
66     * @param ClipboardService $clipboard_service
67     */
68    public function __construct(ClipboardService $clipboard_service)
69    {
70        $this->clipboard_service = $clipboard_service;
71    }
72
73    /**
74     * @param ServerRequestInterface $request
75     *
76     * @return ResponseInterface
77     */
78    public function handle(ServerRequestInterface $request): ResponseInterface
79    {
80        $tree = $request->getAttribute('tree');
81        assert($tree instanceof Tree);
82
83        $xref = $request->getAttribute('xref');
84        assert(is_string($xref));
85
86        $repository = Repository::getInstance($xref, $tree);
87        $repository = Auth::checkRepositoryAccess($repository, false);
88
89        // Redirect to correct xref/slug
90        if ($repository->xref() !== $xref ||  $request->getAttribute('slug') !== $repository->slug()) {
91            return redirect($repository->url());
92        }
93
94        return $this->viewResponse('repository-page', [
95            'clipboard_facts' => $this->clipboard_service->pastableFacts($repository, new Collection()),
96            'facts'           => $this->facts($repository),
97            'meta_robots'     => 'index,follow',
98            'repository'      => $repository,
99            'sources'         => $repository->linkedSources('REPO'),
100            'title'           => $repository->fullName(),
101            'tree'            => $tree,
102        ]);
103    }
104
105    /**
106     * @param Repository $record
107     *
108     * @return Collection
109     */
110    private function facts(Repository $record): Collection
111    {
112        return $record->facts()
113            ->sort(static function (Fact $x, Fact $y): int {
114                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
115                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
116
117                return $sort_x <=> $sort_y;
118            });
119    }
120}
121