xref: /webtrees/app/Http/RequestHandlers/GedcomRecordPage.php (revision 842a873c0b92c28d4af3fa9db0e9156057005569)
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 Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\Header;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Media;
28use Fisharebest\Webtrees\Note;
29use Fisharebest\Webtrees\Registry;
30use Fisharebest\Webtrees\Repository;
31use Fisharebest\Webtrees\Source;
32use Fisharebest\Webtrees\Submission;
33use Fisharebest\Webtrees\Submitter;
34use Fisharebest\Webtrees\Tree;
35use Psr\Http\Message\ResponseInterface;
36use Psr\Http\Message\ServerRequestInterface;
37use Psr\Http\Server\RequestHandlerInterface;
38
39use function assert;
40use function is_string;
41use function redirect;
42
43/**
44 * Display non-standard genealogy records.
45 */
46class GedcomRecordPage implements RequestHandlerInterface
47{
48    use ViewResponseTrait;
49
50    // These standard genealogy record types have their own pages.
51    private const STANDARD_RECORDS = [
52        Family::class,
53        Header::class,
54        Individual::class,
55        Media::class,
56        Note::class,
57        Repository::class,
58        Source::class,
59        Submission::class,
60        Submitter::class,
61    ];
62
63    /**
64     * Show a gedcom record's page.
65     *
66     * @param ServerRequestInterface $request
67     *
68     * @return ResponseInterface
69     */
70    public function handle(ServerRequestInterface $request): ResponseInterface
71    {
72        $tree = $request->getAttribute('tree');
73        assert($tree instanceof Tree);
74
75        $xref = $request->getAttribute('xref');
76        assert(is_string($xref));
77
78        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
79        $record = Auth::checkRecordAccess($record);
80
81        // Standard genealogy records have their own pages.
82        if ($record->xref() !== $xref || in_array(get_class($record), self::STANDARD_RECORDS, true)) {
83            return redirect($record->url());
84        }
85
86        $record_type = $record->tag();
87
88        return $this->viewResponse('gedcom-record-page', [
89            'facts'         => $record->facts(),
90            'families'      => $record->linkedFamilies($record_type),
91            'individuals'   => $record->linkedIndividuals($record_type),
92            'notes'         => $record->linkedNotes($record_type),
93            'media_objects' => $record->linkedMedia($record_type),
94            'record'        => $record,
95            'sources'       => $record->linkedSources($record_type),
96            'title'         => $record->fullName(),
97            'tree'          => $tree,
98        ]);
99    }
100}
101