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