xref: /webtrees/app/Http/RequestHandlers/GedcomRecordPage.php (revision 1da5dadd02c9f21e0a345e21a19b2c63ae792ed3)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://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\Header;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Location;
28use Fisharebest\Webtrees\Media;
29use Fisharebest\Webtrees\Note;
30use Fisharebest\Webtrees\Registry;
31use Fisharebest\Webtrees\Repository;
32use Fisharebest\Webtrees\Services\ClipboardService;
33use Fisharebest\Webtrees\Services\LinkedRecordService;
34use Fisharebest\Webtrees\Source;
35use Fisharebest\Webtrees\Submission;
36use Fisharebest\Webtrees\Submitter;
37use Fisharebest\Webtrees\Validator;
38use Psr\Http\Message\ResponseInterface;
39use Psr\Http\Message\ServerRequestInterface;
40use Psr\Http\Server\RequestHandlerInterface;
41
42use function get_class;
43use function in_array;
44use function redirect;
45
46/**
47 * Display non-standard genealogy records.
48 */
49class GedcomRecordPage implements RequestHandlerInterface
50{
51    use ViewResponseTrait;
52
53    // These standard genealogy record types have their own pages.
54    private const STANDARD_RECORDS = [
55        Family::class,
56        Header::class,
57        Individual::class,
58        Location::class,
59        Media::class,
60        Note::class,
61        Repository::class,
62        Source::class,
63        Submission::class,
64        Submitter::class,
65    ];
66
67    private ClipboardService $clipboard_service;
68
69    private LinkedRecordService $linked_record_service;
70
71    /**
72     * @param ClipboardService $clipboard_service
73     * @param LinkedRecordService $linked_record_service
74     */
75    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
76    {
77        $this->clipboard_service     = $clipboard_service;
78        $this->linked_record_service = $linked_record_service;
79    }
80
81    /**
82     * Show a gedcom record's page.
83     *
84     * @param ServerRequestInterface $request
85     *
86     * @return ResponseInterface
87     */
88    public function handle(ServerRequestInterface $request): ResponseInterface
89    {
90        $tree   = Validator::attributes($request)->tree();
91        $xref   = Validator::attributes($request)->isXref()->string('xref');
92        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
93        $record = Auth::checkRecordAccess($record);
94
95        // Standard genealogy records have their own pages.
96        if ($record->xref() !== $xref || in_array(get_class($record), self::STANDARD_RECORDS, true)) {
97            return redirect($record->url());
98        }
99
100        $linked_families     = $this->linked_record_service->linkedFamilies($record);
101        $linked_individuals  = $this->linked_record_service->linkedIndividuals($record);
102        $linked_locations    = $this->linked_record_service->linkedLocations($record);
103        $linked_media        = $this->linked_record_service->linkedMedia($record);
104        $linked_notes        = $this->linked_record_service->linkedNotes($record);
105        $linked_repositories = $this->linked_record_service->linkedRepositories($record);
106        $linked_sources      = $this->linked_record_service->linkedSources($record);
107        $linked_submitters   = $this->linked_record_service->linkedSubmitters($record);
108
109        return $this->viewResponse('record-page', [
110            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
111            'linked_families'      => $linked_families->isEmpty() ? null : $linked_families,
112            'linked_individuals'   => $linked_individuals->isEmpty() ? null : $linked_individuals,
113            'linked_locations'     => $linked_locations->isEmpty() ? null : $linked_locations,
114            'linked_media_objects' => $linked_media->isEmpty() ? null : $linked_media,
115            'linked_notes'         => $linked_notes->isEmpty() ? null : $linked_notes,
116            'linked_repositories'  => $linked_repositories->isEmpty() ? null : $linked_repositories,
117            'linked_sources'       => $linked_sources->isEmpty() ? null : $linked_sources,
118            'linked_submitters'    => $linked_submitters->isEmpty() ? null : $linked_submitters,
119            'record'               => $record,
120            'title'                => $record->fullName(),
121            'tree'                 => $tree,
122        ])->withHeader('Link', '<' . $record->url() . '>; rel="canonical"');
123    }
124}
125