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