xref: /webtrees/app/Http/RequestHandlers/MediaPage.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
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 League\Flysystem\FilesystemInterface;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function assert;
35use function is_string;
36use function redirect;
37
38/**
39 * Show a media object's page.
40 */
41class MediaPage implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    /** @var ClipboardService */
46    private $clipboard_service;
47
48    /**
49     * MediaPage constructor.
50     *
51     * @param ClipboardService $clipboard_service
52     */
53    public function __construct(ClipboardService $clipboard_service)
54    {
55        $this->clipboard_service = $clipboard_service;
56    }
57
58    /**
59     * @param ServerRequestInterface $request
60     *
61     * @return ResponseInterface
62     */
63    public function handle(ServerRequestInterface $request): ResponseInterface
64    {
65        $data_filesystem = $request->getAttribute('filesystem.data');
66        assert($data_filesystem instanceof FilesystemInterface);
67
68        $tree = $request->getAttribute('tree');
69        assert($tree instanceof Tree);
70
71        $xref = $request->getAttribute('xref');
72        assert(is_string($xref));
73
74        $media = Media::getInstance($xref, $tree);
75        $media = Auth::checkMediaAccess($media);
76
77        // Redirect to correct xref/slug
78        if ($media->xref() !== $xref || $request->getAttribute('slug') !== $media->slug()) {
79            return redirect($media->url());
80        }
81
82        return $this->viewResponse('media-page', [
83            'clipboard_facts' => $this->clipboard_service->pastableFacts($media, new Collection()),
84            'data_filesystem' => $data_filesystem,
85            'families'        => $media->linkedFamilies('OBJE'),
86            'facts'           => $this->facts($media),
87            'individuals'     => $media->linkedIndividuals('OBJE'),
88            'media'           => $media,
89            'meta_robots'     => 'index,follow',
90            'notes'           => $media->linkedNotes('OBJE'),
91            'sources'         => $media->linkedSources('OBJE'),
92            'title'           => $media->fullName(),
93            'tree'            => $tree,
94        ]);
95    }
96
97    /**
98     * @param Media $record
99     *
100     * @return Collection
101     */
102    private function facts(Media $record): Collection
103    {
104        return $record->facts()
105            ->filter(static function (Fact $fact): bool {
106                return $fact->getTag() !== 'FILE';
107            });
108    }
109}
110