xref: /webtrees/app/Http/RequestHandlers/MediaPage.php (revision 4991f2057a6647447a648c5d6743dab00378e98e)
1d7daee59SGreg Roach<?php
2d7daee59SGreg Roach
3d7daee59SGreg Roach/**
4d7daee59SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
27*4991f205SGreg 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
44*4991f205SGreg Roach    private LinkedRecordService $linked_record_service;
45*4991f205SGreg Roach
46d7daee59SGreg Roach    /**
47d7daee59SGreg Roach     * @param ClipboardService $clipboard_service
48*4991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
49d7daee59SGreg Roach     */
50*4991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
51d7daee59SGreg Roach    {
52d7daee59SGreg Roach        $this->clipboard_service     = $clipboard_service;
53*4991f205SGreg 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    {
636b9cb339SGreg Roach        $data_filesystem = Registry::filesystem()->data();
64c033a757SGreg Roach
65b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
66b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
67b55cbc6bSGreg Roach        $slug   = Validator::attributes($request)->string('slug', '');
680f5fd22fSGreg Roach        $record = Registry::mediaFactory()->make($xref, $tree);
690f5fd22fSGreg Roach        $record = Auth::checkMediaAccess($record);
70d7daee59SGreg Roach
71d7daee59SGreg Roach        // Redirect to correct xref/slug
72b55cbc6bSGreg Roach        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
730f5fd22fSGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
74d7daee59SGreg Roach        }
75d7daee59SGreg Roach
76*4991f205SGreg Roach        $linked_families    = $this->linked_record_service->linkedFamilies($record);
77*4991f205SGreg Roach        $linked_individuals = $this->linked_record_service->linkedIndividuals($record);
78*4991f205SGreg Roach        $linked_locations   = $this->linked_record_service->linkedLocations($record);
79*4991f205SGreg Roach        $linked_notes       = $this->linked_record_service->linkedNotes($record);
80*4991f205SGreg Roach        $linked_sources     = $this->linked_record_service->linkedSources($record);
81*4991f205SGreg Roach
82d7daee59SGreg Roach        return $this->viewResponse('media-page', [
830f5fd22fSGreg Roach            'clipboard_facts'    => $this->clipboard_service->pastableFacts($record),
84c033a757SGreg Roach            'data_filesystem'    => $data_filesystem,
85*4991f205SGreg Roach            'linked_families'    => $linked_families,
86*4991f205SGreg Roach            'linked_individuals' => $linked_individuals,
87*4991f205SGreg Roach            'linked_locations'   => $linked_locations->isEmpty() ? null : $linked_locations,
88*4991f205SGreg Roach            'linked_notes'       => $linked_notes,
89*4991f205SGreg Roach            'linked_sources'     => $linked_sources,
902406e0e0SGreg Roach            'meta_description'   => '',
91d7daee59SGreg Roach            'meta_robots'        => 'index,follow',
920f5fd22fSGreg Roach            'record'             => $record,
930f5fd22fSGreg Roach            'title'              => $record->fullName(),
94d7daee59SGreg Roach            'tree'               => $tree,
95d7daee59SGreg Roach        ]);
96d7daee59SGreg Roach    }
97d7daee59SGreg Roach}
98