xref: /webtrees/app/Http/RequestHandlers/AddMediaFileAction.php (revision 81b729d3a9a6e0a0e8b96285d1ad7955d2d0c659)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Services\MediaFileService;
26use Fisharebest\Webtrees\Services\PendingChangesService;
27use Fisharebest\Webtrees\Tree;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function assert;
33use function is_string;
34use function redirect;
35use function route;
36
37/**
38 * Add a new media file to a media object.
39 */
40class AddMediaFileAction implements RequestHandlerInterface
41{
42    private MediaFileService $media_file_service;
43
44    private PendingChangesService $pending_changes_service;
45
46    /**
47     * AddMediaFileAction constructor.
48     *
49     * @param MediaFileService      $media_file_service
50     * @param PendingChangesService $pending_changes_service
51     */
52    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
53    {
54        $this->media_file_service      = $media_file_service;
55        $this->pending_changes_service = $pending_changes_service;
56    }
57
58    /**
59     * Add a media file to an existing media object.
60     *
61     * @param ServerRequestInterface $request
62     *
63     * @return ResponseInterface
64     */
65    public function handle(ServerRequestInterface $request): ResponseInterface
66    {
67        $tree = $request->getAttribute('tree');
68        assert($tree instanceof Tree);
69
70        $xref = $request->getAttribute('xref');
71        assert(is_string($xref));
72
73        $media = Registry::mediaFactory()->make($xref, $tree);
74
75        $params = (array) $request->getParsedBody();
76        $title  = $params['title'] ?? '';
77        $type   = $params['type'] ?? '';
78
79        if ($media === null || $media->isPendingDeletion() || !$media->canEdit()) {
80            return redirect(route(TreePage::class, ['tree' => $tree->name()]));
81        }
82
83        $file = $this->media_file_service->uploadFile($request);
84
85        if ($file === '') {
86            FlashMessages::addMessage(I18N::translate('There was an error uploading your file.'));
87
88            return redirect($media->url());
89        }
90
91        $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, '');
92
93        $media->createFact($gedcom, true);
94
95        // Accept the changes, to keep the filesystem in sync with the GEDCOM data.
96        $this->pending_changes_service->acceptRecord($media);
97
98        return redirect($media->url());
99    }
100}
101