xref: /webtrees/app/Http/RequestHandlers/MediaPage.php (revision d7daee59eca171e896682012e6fe10d1f901501f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Fact;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\Media;
26use Fisharebest\Webtrees\Services\ClipboardService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function assert;
34use function is_string;
35use function redirect;
36
37/**
38 * Show a media object's page.
39 */
40class MediaPage implements RequestHandlerInterface
41{
42    use ViewResponseTrait;
43
44    /** @var ClipboardService */
45    private $clipboard_service;
46
47    /**
48     * MediaPage constructor.
49     *
50     * @param ClipboardService $clipboard_service
51     */
52    public function __construct(ClipboardService $clipboard_service)
53    {
54        $this->clipboard_service = $clipboard_service;
55    }
56
57    /**
58     * @param ServerRequestInterface $request
59     *
60     * @return ResponseInterface
61     */
62    public function handle(ServerRequestInterface $request): ResponseInterface
63    {
64        $tree = $request->getAttribute('tree');
65        assert($tree instanceof Tree);
66
67        $xref = $request->getAttribute('xref');
68        assert(is_string($xref));
69
70        $media = Media::getInstance($xref, $tree);
71        $media = Auth::checkMediaAccess($media);
72
73        // Redirect to correct xref/slug
74        if ($media->xref() !== $xref || $request->getAttribute('slug') !== $media->slug()) {
75            return redirect($media->url());
76        }
77
78        return $this->viewResponse('media-page', [
79            'clipboard_facts' => $this->clipboard_service->pastableFacts($media, new Collection()),
80            'families'        => $media->linkedFamilies('OBJE'),
81            'facts'           => $this->facts($media),
82            'individuals'     => $media->linkedIndividuals('OBJE'),
83            'media'           => $media,
84            'meta_robots'     => 'index,follow',
85            'notes'           => $media->linkedNotes('OBJE'),
86            'sources'         => $media->linkedSources('OBJE'),
87            'title'           => $media->fullName(),
88            'tree'            => $tree,
89        ]);
90    }
91
92    /**
93     * @param Media $record
94     *
95     * @return Collection
96     */
97    private function facts(Media $record): Collection
98    {
99        return $record->facts()
100            ->filter(static function (Fact $fact): bool {
101                return $fact->getTag() !== 'FILE';
102            });
103    }
104}
105