xref: /webtrees/app/Http/RequestHandlers/GedcomRecordPage.php (revision 65de9aa72b30bed273ea408e46f1aa2452711d43)
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\Location;
28use Fisharebest\Webtrees\Media;
29use Fisharebest\Webtrees\Note;
30use Fisharebest\Webtrees\Registry;
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        Location::class,
57        Media::class,
58        Note::class,
59        Repository::class,
60        Source::class,
61        Submission::class,
62        Submitter::class,
63    ];
64
65    /**
66     * Show a gedcom record's page.
67     *
68     * @param ServerRequestInterface $request
69     *
70     * @return ResponseInterface
71     */
72    public function handle(ServerRequestInterface $request): ResponseInterface
73    {
74        $tree = $request->getAttribute('tree');
75        assert($tree instanceof Tree);
76
77        $xref = $request->getAttribute('xref');
78        assert(is_string($xref));
79
80        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
81        $record = Auth::checkRecordAccess($record);
82
83        // Standard genealogy records have their own pages.
84        if ($record->xref() !== $xref || in_array(get_class($record), self::STANDARD_RECORDS, true)) {
85            return redirect($record->url());
86        }
87
88        $record_type = $record->tag();
89
90        return $this->viewResponse('gedcom-record-page', [
91            'facts'         => $record->facts(),
92            'families'      => $record->linkedFamilies($record_type),
93            'individuals'   => $record->linkedIndividuals($record_type),
94            'notes'         => $record->linkedNotes($record_type),
95            'media_objects' => $record->linkedMedia($record_type),
96            'record'        => $record,
97            'sources'       => $record->linkedSources($record_type),
98            'title'         => $record->fullName(),
99            'tree'          => $tree,
100        ]);
101    }
102}
103