xref: /webtrees/app/Http/RequestHandlers/GedcomRecordPage.php (revision d5b0584dbc729158e086571dbfaad5c22f82f0ef)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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\Family;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\GedcomRecord;
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 = GedcomRecord::getInstance($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        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ ([_A-Z0-9]+)/', $record->gedcom(), $match)) {
88            $record_type = $match[1];
89        } elseif (preg_match('/^0 ([_A-Z0-9]+)/', $record->gedcom(), $match)) {
90            $record_type = $match[1];
91        } else {
92            $record_type = $record::RECORD_TYPE;
93        }
94
95        return $this->viewResponse('gedcom-record-page', [
96            'facts'         => $record->facts(),
97            'families'      => $record->linkedFamilies($record_type),
98            'individuals'   => $record->linkedIndividuals($record_type),
99            'notes'         => $record->linkedNotes($record_type),
100            'media_objects' => $record->linkedMedia($record_type),
101            'record'        => $record,
102            'sources'       => $record->linkedSources($record_type),
103            'title'         => $record->fullName(),
104            'tree'          => $tree,
105        ]);
106    }
107}
108