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