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