xref: /webtrees/app/Http/RequestHandlers/GedcomRecordPage.php (revision 15c4f62c40de0c794536302daf7d5ec0548a1973)
15818a371SGreg Roach<?php
25818a371SGreg Roach
35818a371SGreg Roach/**
45818a371SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
324991f205SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
334991f205SGreg 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;
385818a371SGreg Roachuse Psr\Http\Message\ResponseInterface;
395818a371SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
405818a371SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
415818a371SGreg Roach
4210e06497SGreg Roachuse function get_class;
4310e06497SGreg Roachuse function in_array;
445818a371SGreg Roachuse function redirect;
455818a371SGreg Roach
465818a371SGreg Roach/**
475818a371SGreg Roach * Display non-standard genealogy records.
485818a371SGreg Roach */
495818a371SGreg Roachclass GedcomRecordPage implements RequestHandlerInterface
505818a371SGreg Roach{
515818a371SGreg Roach    use ViewResponseTrait;
525818a371SGreg Roach
535818a371SGreg Roach    // These standard genealogy record types have their own pages.
545818a371SGreg Roach    private const STANDARD_RECORDS = [
555818a371SGreg Roach        Family::class,
561635452cSGreg Roach        Header::class,
575818a371SGreg Roach        Individual::class,
58e8ded2caSGreg Roach        Location::class,
595818a371SGreg Roach        Media::class,
605818a371SGreg Roach        Note::class,
615818a371SGreg Roach        Repository::class,
625818a371SGreg Roach        Source::class,
631635452cSGreg Roach        Submission::class,
64ffc0a61fSGreg Roach        Submitter::class,
655818a371SGreg Roach    ];
665818a371SGreg Roach
674991f205SGreg Roach    private ClipboardService $clipboard_service;
684991f205SGreg Roach
694991f205SGreg Roach    private LinkedRecordService $linked_record_service;
704991f205SGreg Roach
714991f205SGreg Roach    /**
724991f205SGreg Roach     * @param ClipboardService $clipboard_service
734991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
744991f205SGreg Roach     */
754991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
764991f205SGreg Roach    {
774991f205SGreg Roach        $this->clipboard_service     = $clipboard_service;
784991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
794991f205SGreg Roach    }
804991f205SGreg Roach
815818a371SGreg Roach    /**
825818a371SGreg Roach     * Show a gedcom record's page.
835818a371SGreg Roach     *
845818a371SGreg Roach     * @param ServerRequestInterface $request
855818a371SGreg Roach     *
865818a371SGreg Roach     * @return ResponseInterface
875818a371SGreg Roach     */
885818a371SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
895818a371SGreg Roach    {
90b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
91b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
926b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
935818a371SGreg Roach        $record = Auth::checkRecordAccess($record);
945818a371SGreg Roach
955818a371SGreg Roach        // Standard genealogy records have their own pages.
965818a371SGreg Roach        if ($record->xref() !== $xref || in_array(get_class($record), self::STANDARD_RECORDS, true)) {
975818a371SGreg Roach            return redirect($record->url());
985818a371SGreg Roach        }
995818a371SGreg Roach
1004991f205SGreg Roach        $linked_families     = $this->linked_record_service->linkedFamilies($record);
1014991f205SGreg Roach        $linked_individuals  = $this->linked_record_service->linkedIndividuals($record);
1024991f205SGreg Roach        $linked_locations    = $this->linked_record_service->linkedLocations($record);
1034991f205SGreg Roach        $linked_media        = $this->linked_record_service->linkedMedia($record);
1044991f205SGreg Roach        $linked_notes        = $this->linked_record_service->linkedNotes($record);
1054991f205SGreg Roach        $linked_repositories = $this->linked_record_service->linkedRepositories($record);
1064991f205SGreg Roach        $linked_sources      = $this->linked_record_service->linkedSources($record);
1074991f205SGreg Roach        $linked_submitters   = $this->linked_record_service->linkedSubmitters($record);
1084991f205SGreg Roach
1090f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
1104991f205SGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
1114991f205SGreg Roach            'linked_families'      => $linked_families->isEmpty() ? null : $linked_families,
1124991f205SGreg Roach            'linked_individuals'   => $linked_individuals->isEmpty() ? null : $linked_individuals,
1134991f205SGreg Roach            'linked_locations'     => $linked_locations->isEmpty() ? null : $linked_locations,
1144991f205SGreg Roach            'linked_media_objects' => $linked_media->isEmpty() ? null : $linked_media,
1154991f205SGreg Roach            'linked_notes'         => $linked_notes->isEmpty() ? null : $linked_notes,
1164991f205SGreg Roach            'linked_repositories'  => $linked_repositories->isEmpty() ? null : $linked_repositories,
1174991f205SGreg Roach            'linked_sources'       => $linked_sources->isEmpty() ? null : $linked_sources,
1184991f205SGreg Roach            'linked_submitters'    => $linked_submitters->isEmpty() ? null : $linked_submitters,
1195818a371SGreg Roach            'record'               => $record,
1205818a371SGreg Roach            'title'                => $record->fullName(),
1215818a371SGreg Roach            'tree'                 => $tree,
122*15c4f62cSGreg Roach        ])->withHeader('Link', '<' . $record->url() . '>; rel="canonical"');
1235818a371SGreg Roach    }
1245818a371SGreg Roach}
125