xref: /webtrees/app/Http/RequestHandlers/SourcePage.php (revision 15c4f62c40de0c794536302daf7d5ec0548a1973)
154186344SGreg Roach<?php
254186344SGreg Roach
354186344SGreg Roach/**
454186344SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
654186344SGreg Roach * This program is free software: you can redistribute it and/or modify
754186344SGreg Roach * it under the terms of the GNU General Public License as published by
854186344SGreg Roach * the Free Software Foundation, either version 3 of the License, or
954186344SGreg Roach * (at your option) any later version.
1054186344SGreg Roach * This program is distributed in the hope that it will be useful,
1154186344SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1254186344SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1354186344SGreg Roach * GNU General Public License for more details.
1454186344SGreg 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/>.
1654186344SGreg Roach */
1754186344SGreg Roach
1854186344SGreg Roachdeclare(strict_types=1);
1954186344SGreg Roach
2054186344SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2154186344SGreg Roach
22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2354186344SGreg Roachuse Fisharebest\Webtrees\Auth;
2454186344SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2654186344SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
274991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2954186344SGreg Roachuse Psr\Http\Message\ResponseInterface;
3054186344SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3154186344SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3254186344SGreg Roach
3354186344SGreg Roachuse function redirect;
3454186344SGreg Roach
3554186344SGreg Roach/**
3654186344SGreg Roach * Show a source's page.
3754186344SGreg Roach */
3854186344SGreg Roachclass SourcePage implements RequestHandlerInterface
3954186344SGreg Roach{
4054186344SGreg Roach    use ViewResponseTrait;
4154186344SGreg Roach
42c4943cffSGreg Roach    private ClipboardService $clipboard_service;
4354186344SGreg Roach
444991f205SGreg Roach    private LinkedRecordService $linked_record_service;
454991f205SGreg Roach
4654186344SGreg Roach    /**
4754186344SGreg Roach     * @param ClipboardService $clipboard_service
484991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
4954186344SGreg Roach     */
504991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
5154186344SGreg Roach    {
5254186344SGreg Roach        $this->clipboard_service     = $clipboard_service;
534991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
5454186344SGreg Roach    }
5554186344SGreg Roach
5654186344SGreg Roach    /**
5754186344SGreg Roach     * @param ServerRequestInterface $request
5854186344SGreg Roach     *
5954186344SGreg Roach     * @return ResponseInterface
6054186344SGreg Roach     */
6154186344SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
6254186344SGreg Roach    {
63b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
64b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
65b55cbc6bSGreg Roach        $slug   = Validator::attributes($request)->string('slug', '');
660f5fd22fSGreg Roach        $record = Registry::sourceFactory()->make($xref, $tree);
670f5fd22fSGreg Roach        $record = Auth::checkSourceAccess($record, false);
6854186344SGreg Roach
6954186344SGreg Roach        // Redirect to correct xref/slug
70b55cbc6bSGreg Roach        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
710f5fd22fSGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
7254186344SGreg Roach        }
7354186344SGreg Roach
744991f205SGreg Roach        $linked_locations = $this->linked_record_service->linkedLocations($record);
754991f205SGreg Roach
760f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
770f5fd22fSGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
784991f205SGreg Roach            'linked_families'      => $this->linked_record_service->linkedFamilies($record),
794991f205SGreg Roach            'linked_individuals'   => $this->linked_record_service->linkedIndividuals($record),
804991f205SGreg Roach            'linked_locations'     => $linked_locations->isEmpty() ? null : $linked_locations,
814991f205SGreg Roach            'linked_media_objects' => $this->linked_record_service->linkedMedia($record),
824991f205SGreg Roach            'linked_notes'         => $this->linked_record_service->linkedNotes($record),
834991f205SGreg Roach            'linked_repositories'  => null,
840f5fd22fSGreg Roach            'linked_sources'       => null,
855962a3c2SGreg Roach            'linked_submitters'    => null,
862406e0e0SGreg Roach            'meta_description'     => '',
8754186344SGreg Roach            'meta_robots'          => 'index,follow',
880f5fd22fSGreg Roach            'record'               => $record,
890f5fd22fSGreg Roach            'title'                => $record->fullName(),
9054186344SGreg Roach            'tree'                 => $tree,
91*15c4f62cSGreg Roach        ])->withHeader('Link', '<' . $record->url() . '>; rel="canonical"');
9254186344SGreg Roach    }
9354186344SGreg Roach}
94