xref: /webtrees/app/Http/RequestHandlers/MediaPage.php (revision 15c4f62c40de0c794536302daf7d5ec0548a1973)
1d7daee59SGreg Roach<?php
2d7daee59SGreg Roach
3d7daee59SGreg Roach/**
4d7daee59SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d7daee59SGreg Roach * This program is free software: you can redistribute it and/or modify
7d7daee59SGreg Roach * it under the terms of the GNU General Public License as published by
8d7daee59SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d7daee59SGreg Roach * (at your option) any later version.
10d7daee59SGreg Roach * This program is distributed in the hope that it will be useful,
11d7daee59SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d7daee59SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d7daee59SGreg Roach * GNU General Public License for more details.
14d7daee59SGreg 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/>.
16d7daee59SGreg Roach */
17d7daee59SGreg Roach
18d7daee59SGreg Roachdeclare(strict_types=1);
19d7daee59SGreg Roach
20d7daee59SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d7daee59SGreg Roach
22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23d7daee59SGreg Roachuse Fisharebest\Webtrees\Auth;
24d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26d7daee59SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
274991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29d7daee59SGreg Roachuse Psr\Http\Message\ResponseInterface;
30d7daee59SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31d7daee59SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32d7daee59SGreg Roach
33d7daee59SGreg Roachuse function redirect;
34d7daee59SGreg Roach
35d7daee59SGreg Roach/**
36d7daee59SGreg Roach * Show a media object's page.
37d7daee59SGreg Roach */
38d7daee59SGreg Roachclass MediaPage implements RequestHandlerInterface
39d7daee59SGreg Roach{
40d7daee59SGreg Roach    use ViewResponseTrait;
41d7daee59SGreg Roach
42c4943cffSGreg Roach    private ClipboardService $clipboard_service;
43d7daee59SGreg Roach
444991f205SGreg Roach    private LinkedRecordService $linked_record_service;
454991f205SGreg Roach
46d7daee59SGreg Roach    /**
47d7daee59SGreg Roach     * @param ClipboardService $clipboard_service
484991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
49d7daee59SGreg Roach     */
504991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
51d7daee59SGreg Roach    {
52d7daee59SGreg Roach        $this->clipboard_service     = $clipboard_service;
534991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
54d7daee59SGreg Roach    }
55d7daee59SGreg Roach
56d7daee59SGreg Roach    /**
57d7daee59SGreg Roach     * @param ServerRequestInterface $request
58d7daee59SGreg Roach     *
59d7daee59SGreg Roach     * @return ResponseInterface
60d7daee59SGreg Roach     */
61d7daee59SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
62d7daee59SGreg 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::mediaFactory()->make($xref, $tree);
670f5fd22fSGreg Roach        $record = Auth::checkMediaAccess($record);
68d7daee59SGreg Roach
69d7daee59SGreg 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);
72d7daee59SGreg Roach        }
73d7daee59SGreg Roach
744991f205SGreg Roach        $linked_families    = $this->linked_record_service->linkedFamilies($record);
754991f205SGreg Roach        $linked_individuals = $this->linked_record_service->linkedIndividuals($record);
764991f205SGreg Roach        $linked_locations   = $this->linked_record_service->linkedLocations($record);
774991f205SGreg Roach        $linked_notes       = $this->linked_record_service->linkedNotes($record);
784991f205SGreg Roach        $linked_sources     = $this->linked_record_service->linkedSources($record);
794991f205SGreg Roach
80d7daee59SGreg Roach        return $this->viewResponse('media-page', [
810f5fd22fSGreg Roach            'clipboard_facts'    => $this->clipboard_service->pastableFacts($record),
824991f205SGreg Roach            'linked_families'    => $linked_families,
834991f205SGreg Roach            'linked_individuals' => $linked_individuals,
844991f205SGreg Roach            'linked_locations'   => $linked_locations->isEmpty() ? null : $linked_locations,
854991f205SGreg Roach            'linked_notes'       => $linked_notes,
864991f205SGreg Roach            'linked_sources'     => $linked_sources,
872406e0e0SGreg Roach            'meta_description'   => '',
88d7daee59SGreg Roach            'meta_robots'        => 'index,follow',
890f5fd22fSGreg Roach            'record'             => $record,
900f5fd22fSGreg Roach            'title'              => $record->fullName(),
91d7daee59SGreg Roach            'tree'               => $tree,
92*15c4f62cSGreg Roach        ])->withHeader('Link', '<' . $record->url() . '>; rel="canonical"');
93d7daee59SGreg Roach    }
94d7daee59SGreg Roach}
95