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