xref: /webtrees/app/Http/RequestHandlers/GedcomRecordPage.php (revision 5818a37173d08520f4d5c2866a0c8010268e0d6d)
1*5818a371SGreg Roach<?php
2*5818a371SGreg Roach
3*5818a371SGreg Roach/**
4*5818a371SGreg Roach * webtrees: online genealogy
5*5818a371SGreg Roach * Copyright (C) 2019 webtrees development team
6*5818a371SGreg Roach * This program is free software: you can redistribute it and/or modify
7*5818a371SGreg Roach * it under the terms of the GNU General Public License as published by
8*5818a371SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*5818a371SGreg Roach * (at your option) any later version.
10*5818a371SGreg Roach * This program is distributed in the hope that it will be useful,
11*5818a371SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*5818a371SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*5818a371SGreg Roach * GNU General Public License for more details.
14*5818a371SGreg Roach * You should have received a copy of the GNU General Public License
15*5818a371SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*5818a371SGreg Roach */
17*5818a371SGreg Roach
18*5818a371SGreg Roachdeclare(strict_types=1);
19*5818a371SGreg Roach
20*5818a371SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*5818a371SGreg Roach
22*5818a371SGreg Roachuse Fisharebest\Webtrees\Auth;
23*5818a371SGreg Roachuse Fisharebest\Webtrees\Family;
24*5818a371SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
25*5818a371SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26*5818a371SGreg Roachuse Fisharebest\Webtrees\Individual;
27*5818a371SGreg Roachuse Fisharebest\Webtrees\Media;
28*5818a371SGreg Roachuse Fisharebest\Webtrees\Note;
29*5818a371SGreg Roachuse Fisharebest\Webtrees\Repository;
30*5818a371SGreg Roachuse Fisharebest\Webtrees\Source;
31*5818a371SGreg Roachuse Fisharebest\Webtrees\Tree;
32*5818a371SGreg Roachuse Psr\Http\Message\ResponseInterface;
33*5818a371SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34*5818a371SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
35*5818a371SGreg Roach
36*5818a371SGreg Roachuse function assert;
37*5818a371SGreg Roachuse function is_string;
38*5818a371SGreg Roachuse function redirect;
39*5818a371SGreg Roach
40*5818a371SGreg Roach/**
41*5818a371SGreg Roach * Display non-standard genealogy records.
42*5818a371SGreg Roach */
43*5818a371SGreg Roachclass GedcomRecordPage implements RequestHandlerInterface
44*5818a371SGreg Roach{
45*5818a371SGreg Roach    use ViewResponseTrait;
46*5818a371SGreg Roach
47*5818a371SGreg Roach    // These standard genealogy record types have their own pages.
48*5818a371SGreg Roach    private const STANDARD_RECORDS = [
49*5818a371SGreg Roach        Family::class,
50*5818a371SGreg Roach        Individual::class,
51*5818a371SGreg Roach        Media::class,
52*5818a371SGreg Roach        Note::class,
53*5818a371SGreg Roach        Repository::class,
54*5818a371SGreg Roach        Source::class,
55*5818a371SGreg Roach    ];
56*5818a371SGreg Roach
57*5818a371SGreg Roach    /**
58*5818a371SGreg Roach     * Show a gedcom record's page.
59*5818a371SGreg Roach     *
60*5818a371SGreg Roach     * @param ServerRequestInterface $request
61*5818a371SGreg Roach     *
62*5818a371SGreg Roach     * @return ResponseInterface
63*5818a371SGreg Roach     */
64*5818a371SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
65*5818a371SGreg Roach    {
66*5818a371SGreg Roach        $tree = $request->getAttribute('tree');
67*5818a371SGreg Roach        assert($tree instanceof Tree);
68*5818a371SGreg Roach
69*5818a371SGreg Roach        $xref = $request->getAttribute('xref');
70*5818a371SGreg Roach        assert(is_string($xref));
71*5818a371SGreg Roach
72*5818a371SGreg Roach        $record = GedcomRecord::getInstance($xref, $tree);
73*5818a371SGreg Roach        $record = Auth::checkRecordAccess($record);
74*5818a371SGreg Roach
75*5818a371SGreg Roach        // Standard genealogy records have their own pages.
76*5818a371SGreg Roach        if ($record->xref() !== $xref || in_array(get_class($record), self::STANDARD_RECORDS, true)) {
77*5818a371SGreg Roach            return redirect($record->url());
78*5818a371SGreg Roach        }
79*5818a371SGreg Roach
80*5818a371SGreg Roach        return $this->viewResponse('gedcom-record-page', [
81*5818a371SGreg Roach            'facts'         => $record->facts(),
82*5818a371SGreg Roach            'families'      => $record->linkedFamilies($record::RECORD_TYPE),
83*5818a371SGreg Roach            'individuals'   => $record->linkedIndividuals($record::RECORD_TYPE),
84*5818a371SGreg Roach            'meta_robots'   => 'index,follow',
85*5818a371SGreg Roach            'notes'         => $record->linkedNotes($record::RECORD_TYPE),
86*5818a371SGreg Roach            'media_objects' => $record->linkedMedia($record::RECORD_TYPE),
87*5818a371SGreg Roach            'record'        => $record,
88*5818a371SGreg Roach            'sources'       => $record->linkedSources($record::RECORD_TYPE),
89*5818a371SGreg Roach            'title'         => $record->fullName(),
90*5818a371SGreg Roach            'tree'          => $tree,
91*5818a371SGreg Roach        ]);
92*5818a371SGreg Roach    }
93*5818a371SGreg Roach}
94