xref: /webtrees/app/Http/RequestHandlers/GedcomRecordPage.php (revision 4991f2057a6647447a648c5d6743dab00378e98e)
15818a371SGreg Roach<?php
25818a371SGreg Roach
35818a371SGreg Roach/**
45818a371SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
65818a371SGreg Roach * This program is free software: you can redistribute it and/or modify
75818a371SGreg Roach * it under the terms of the GNU General Public License as published by
85818a371SGreg Roach * the Free Software Foundation, either version 3 of the License, or
95818a371SGreg Roach * (at your option) any later version.
105818a371SGreg Roach * This program is distributed in the hope that it will be useful,
115818a371SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
125818a371SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135818a371SGreg Roach * GNU General Public License for more details.
145818a371SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
165818a371SGreg Roach */
175818a371SGreg Roach
185818a371SGreg Roachdeclare(strict_types=1);
195818a371SGreg Roach
205818a371SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
215818a371SGreg Roach
225818a371SGreg Roachuse Fisharebest\Webtrees\Auth;
235818a371SGreg Roachuse Fisharebest\Webtrees\Family;
241635452cSGreg Roachuse Fisharebest\Webtrees\Header;
255818a371SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
265818a371SGreg Roachuse Fisharebest\Webtrees\Individual;
27e8ded2caSGreg Roachuse Fisharebest\Webtrees\Location;
285818a371SGreg Roachuse Fisharebest\Webtrees\Media;
295818a371SGreg Roachuse Fisharebest\Webtrees\Note;
306b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
315818a371SGreg Roachuse Fisharebest\Webtrees\Repository;
32*4991f205SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
33*4991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
345818a371SGreg Roachuse Fisharebest\Webtrees\Source;
351635452cSGreg Roachuse Fisharebest\Webtrees\Submission;
36ffc0a61fSGreg Roachuse Fisharebest\Webtrees\Submitter;
37b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
380f5fd22fSGreg Roachuse Illuminate\Support\Collection;
395818a371SGreg Roachuse Psr\Http\Message\ResponseInterface;
405818a371SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
415818a371SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
425818a371SGreg Roach
435818a371SGreg Roachuse function redirect;
445818a371SGreg Roach
455818a371SGreg Roach/**
465818a371SGreg Roach * Display non-standard genealogy records.
475818a371SGreg Roach */
485818a371SGreg Roachclass GedcomRecordPage implements RequestHandlerInterface
495818a371SGreg Roach{
505818a371SGreg Roach    use ViewResponseTrait;
515818a371SGreg Roach
525818a371SGreg Roach    // These standard genealogy record types have their own pages.
535818a371SGreg Roach    private const STANDARD_RECORDS = [
545818a371SGreg Roach        Family::class,
551635452cSGreg Roach        Header::class,
565818a371SGreg Roach        Individual::class,
57e8ded2caSGreg Roach        Location::class,
585818a371SGreg Roach        Media::class,
595818a371SGreg Roach        Note::class,
605818a371SGreg Roach        Repository::class,
615818a371SGreg Roach        Source::class,
621635452cSGreg Roach        Submission::class,
63ffc0a61fSGreg Roach        Submitter::class,
645818a371SGreg Roach    ];
655818a371SGreg Roach
66*4991f205SGreg Roach    private ClipboardService $clipboard_service;
67*4991f205SGreg Roach
68*4991f205SGreg Roach    private LinkedRecordService $linked_record_service;
69*4991f205SGreg Roach
70*4991f205SGreg Roach    /**
71*4991f205SGreg Roach     * @param ClipboardService $clipboard_service
72*4991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
73*4991f205SGreg Roach     */
74*4991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
75*4991f205SGreg Roach    {
76*4991f205SGreg Roach        $this->clipboard_service     = $clipboard_service;
77*4991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
78*4991f205SGreg Roach    }
79*4991f205SGreg Roach
805818a371SGreg Roach    /**
815818a371SGreg Roach     * Show a gedcom record's page.
825818a371SGreg Roach     *
835818a371SGreg Roach     * @param ServerRequestInterface $request
845818a371SGreg Roach     *
855818a371SGreg Roach     * @return ResponseInterface
865818a371SGreg Roach     */
875818a371SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
885818a371SGreg Roach    {
89b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
90b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
916b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
925818a371SGreg Roach        $record = Auth::checkRecordAccess($record);
935818a371SGreg Roach
945818a371SGreg Roach        // Standard genealogy records have their own pages.
955818a371SGreg Roach        if ($record->xref() !== $xref || in_array(get_class($record), self::STANDARD_RECORDS, true)) {
965818a371SGreg Roach            return redirect($record->url());
975818a371SGreg Roach        }
985818a371SGreg Roach
99*4991f205SGreg Roach        $linked_families     = $this->linked_record_service->linkedFamilies($record);
100*4991f205SGreg Roach        $linked_individuals  = $this->linked_record_service->linkedIndividuals($record);
101*4991f205SGreg Roach        $linked_locations    = $this->linked_record_service->linkedLocations($record);
102*4991f205SGreg Roach        $linked_media        = $this->linked_record_service->linkedMedia($record);
103*4991f205SGreg Roach        $linked_notes        = $this->linked_record_service->linkedNotes($record);
104*4991f205SGreg Roach        $linked_repositories = $this->linked_record_service->linkedRepositories($record);
105*4991f205SGreg Roach        $linked_sources      = $this->linked_record_service->linkedSources($record);
106*4991f205SGreg Roach        $linked_submitters   = $this->linked_record_service->linkedSubmitters($record);
107*4991f205SGreg Roach
1080f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
109*4991f205SGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
110*4991f205SGreg Roach            'linked_families'      => $linked_families->isEmpty() ? null : $linked_families,
111*4991f205SGreg Roach            'linked_individuals'   => $linked_individuals->isEmpty() ? null : $linked_individuals,
112*4991f205SGreg Roach            'linked_locations'     => $linked_locations->isEmpty() ? null : $linked_locations,
113*4991f205SGreg Roach            'linked_media_objects' => $linked_media->isEmpty() ? null : $linked_media,
114*4991f205SGreg Roach            'linked_notes'         => $linked_notes->isEmpty() ? null : $linked_notes,
115*4991f205SGreg Roach            'linked_repositories'  => $linked_repositories->isEmpty() ? null : $linked_repositories,
116*4991f205SGreg Roach            'linked_sources'       => $linked_sources->isEmpty() ? null : $linked_sources,
117*4991f205SGreg Roach            'linked_submitters'    => $linked_submitters->isEmpty() ? null : $linked_submitters,
1185818a371SGreg Roach            'record'               => $record,
1195818a371SGreg Roach            'title'                => $record->fullName(),
1205818a371SGreg Roach            'tree'                 => $tree,
1215818a371SGreg Roach        ]);
1225818a371SGreg Roach    }
1235818a371SGreg Roach}
124