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