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