xref: /webtrees/app/Http/RequestHandlers/SourcePage.php (revision 1792ff1cf1956b41f3e3c853cfb279a803a71ed2)
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 Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Fact;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Services\ClipboardService;
27use Fisharebest\Webtrees\Source;
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 source's page.
43 */
44class SourcePage implements RequestHandlerInterface
45{
46    use ViewResponseTrait;
47
48    // Show the source's facts in this order:
49    private const FACT_ORDER = [
50        1 => 'TITL',
51        'ABBR',
52        'AUTH',
53        'DATA',
54        'PUBL',
55        'TEXT',
56        'REPO',
57        'NOTE',
58        'OBJE',
59        'REFN',
60        'RIN',
61        '_UID',
62        'CHAN',
63        'RESN',
64    ];
65
66    /** @var ClipboardService */
67    private $clipboard_service;
68
69    /**
70     * SourcePage constructor.
71     *
72     * @param ClipboardService $clipboard_service
73     */
74    public function __construct(ClipboardService $clipboard_service)
75    {
76        $this->clipboard_service = $clipboard_service;
77    }
78
79    /**
80     * @param ServerRequestInterface $request
81     *
82     * @return ResponseInterface
83     */
84    public function handle(ServerRequestInterface $request): ResponseInterface
85    {
86        $tree = $request->getAttribute('tree');
87        assert($tree instanceof Tree);
88
89        $xref = $request->getAttribute('xref');
90        assert(is_string($xref));
91
92        $source = Source::getInstance($xref, $tree);
93        $source = Auth::checkSourceAccess($source, false);
94
95        // Redirect to correct xref/slug
96        if ($source->xref() !== $xref || $request->getAttribute('slug') !== $source->slug()) {
97            return redirect($source->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
98        }
99
100        return $this->viewResponse('source-page', [
101            'clipboard_facts' => $this->clipboard_service->pastableFacts($source, new Collection()),
102            'facts'           => $this->facts($source),
103            'families'        => $source->linkedFamilies('SOUR'),
104            'individuals'     => $source->linkedIndividuals('SOUR'),
105            'meta_robots'     => 'index,follow',
106            'notes'           => $source->linkedNotes('SOUR'),
107            'media_objects'   => $source->linkedMedia('SOUR'),
108            'source'          => $source,
109            'title'           => $source->fullName(),
110            'tree'            => $tree,
111        ]);
112    }
113
114    /**
115     * @param Source $record
116     *
117     * @return Collection<Fact>
118     */
119    private function facts(Source $record): Collection
120    {
121        return $record->facts()
122            ->sort(static function (Fact $x, Fact $y): int {
123                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
124                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
125
126                return $sort_x <=> $sort_y;
127            });
128    }
129}
130