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