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