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