xref: /webtrees/app/Http/RequestHandlers/EditMediaFileModal.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1ddb44b4cSGreg Roach<?php
2ddb44b4cSGreg Roach
3ddb44b4cSGreg Roach/**
4ddb44b4cSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6ddb44b4cSGreg Roach * This program is free software: you can redistribute it and/or modify
7ddb44b4cSGreg Roach * it under the terms of the GNU General Public License as published by
8ddb44b4cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ddb44b4cSGreg Roach * (at your option) any later version.
10ddb44b4cSGreg Roach * This program is distributed in the hope that it will be useful,
11ddb44b4cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ddb44b4cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ddb44b4cSGreg Roach * GNU General Public License for more details.
14ddb44b4cSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ddb44b4cSGreg Roach */
17ddb44b4cSGreg Roach
18ddb44b4cSGreg Roachdeclare(strict_types=1);
19ddb44b4cSGreg Roach
20ddb44b4cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21ddb44b4cSGreg Roach
22ddb44b4cSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Auth;
24fa5cbab5SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
25fa5cbab5SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
26ddb44b4cSGreg Roachuse Fisharebest\Webtrees\I18N;
27ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Registry;
28ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
30ddb44b4cSGreg Roachuse Psr\Http\Message\ResponseInterface;
31ddb44b4cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32ddb44b4cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33ddb44b4cSGreg Roach
34ddb44b4cSGreg Roachuse function response;
35ddb44b4cSGreg Roachuse function view;
36ddb44b4cSGreg Roach
37ddb44b4cSGreg Roach/**
38ddb44b4cSGreg Roach * Edit a media file.
39ddb44b4cSGreg Roach */
40ddb44b4cSGreg Roachclass EditMediaFileModal implements RequestHandlerInterface
41ddb44b4cSGreg Roach{
42c4943cffSGreg Roach    private MediaFileService $media_file_service;
43ddb44b4cSGreg Roach
44ddb44b4cSGreg Roach    /**
45ddb44b4cSGreg Roach     * @param MediaFileService $media_file_service
46ddb44b4cSGreg Roach     */
47ddb44b4cSGreg Roach    public function __construct(MediaFileService $media_file_service)
48ddb44b4cSGreg Roach    {
49ddb44b4cSGreg Roach        $this->media_file_service = $media_file_service;
50ddb44b4cSGreg Roach    }
51ddb44b4cSGreg Roach
52ddb44b4cSGreg Roach    /**
53ddb44b4cSGreg Roach     * Edit an existing media file.
54ddb44b4cSGreg Roach     *
55ddb44b4cSGreg Roach     * @param ServerRequestInterface $request
56ddb44b4cSGreg Roach     *
57ddb44b4cSGreg Roach     * @return ResponseInterface
58ddb44b4cSGreg Roach     */
59ddb44b4cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
60ddb44b4cSGreg Roach    {
61b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
62b55cbc6bSGreg Roach        $xref    = Validator::attributes($request)->isXref()->string('xref');
63b55cbc6bSGreg Roach        $fact_id = Validator::attributes($request)->string('fact_id');
64ddb44b4cSGreg Roach        $media   = Registry::mediaFactory()->make($xref, $tree);
65ddb44b4cSGreg Roach
66ddb44b4cSGreg Roach        try {
67ddb44b4cSGreg Roach            $media = Auth::checkMediaAccess($media);
68fa5cbab5SGreg Roach        } catch (HttpNotFoundException | HttpAccessDeniedException $ex) {
69ddb44b4cSGreg Roach            return response(view('modals/error', [
70ddb44b4cSGreg Roach                'title' => I18N::translate('Edit a media file'),
71ddb44b4cSGreg Roach                'error' => $ex->getMessage(),
72ddb44b4cSGreg Roach            ]), StatusCodeInterface::STATUS_FORBIDDEN);
73ddb44b4cSGreg Roach        }
74ddb44b4cSGreg Roach
75ddb44b4cSGreg Roach        foreach ($media->mediaFiles() as $media_file) {
76ddb44b4cSGreg Roach            if ($media_file->factId() === $fact_id) {
77455a30feSGreg Roach                $media_types = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values();
78455a30feSGreg Roach
79ddb44b4cSGreg Roach                return response(view('modals/edit-media-file', [
80ddb44b4cSGreg Roach                    'media_file'      => $media_file,
81ddb44b4cSGreg Roach                    'max_upload_size' => $this->media_file_service->maxUploadFilesize(),
82ddb44b4cSGreg Roach                    'media'           => $media,
83455a30feSGreg Roach                    'media_types'     => $media_types,
849458f20aSGreg Roach                    'unused_files'    => $this->media_file_service->unusedFiles($tree),
85ddb44b4cSGreg Roach                    'tree'            => $tree,
86ddb44b4cSGreg Roach                ]));
87ddb44b4cSGreg Roach            }
88ddb44b4cSGreg Roach        }
89ddb44b4cSGreg Roach
90ddb44b4cSGreg Roach        return response('', StatusCodeInterface::STATUS_NOT_FOUND);
91ddb44b4cSGreg Roach    }
92ddb44b4cSGreg Roach}
93