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