xref: /webtrees/app/Http/RequestHandlers/AddMediaFileAction.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
1ddb44b4cSGreg Roach<?php
2ddb44b4cSGreg Roach
3ddb44b4cSGreg Roach/**
4ddb44b4cSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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
2252ed2878SGreg Roachuse Fisharebest\Webtrees\Auth;
23ddb44b4cSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24ddb44b4cSGreg Roachuse Fisharebest\Webtrees\I18N;
25ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Registry;
26ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
27ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29ddb44b4cSGreg Roachuse Psr\Http\Message\ResponseInterface;
30ddb44b4cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31ddb44b4cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32ddb44b4cSGreg Roach
33ddb44b4cSGreg Roachuse function redirect;
34ddb44b4cSGreg Roach
35ddb44b4cSGreg Roach/**
36ddb44b4cSGreg Roach * Add a new media file to a media object.
37ddb44b4cSGreg Roach */
38ddb44b4cSGreg Roachclass AddMediaFileAction implements RequestHandlerInterface
39ddb44b4cSGreg Roach{
40c4943cffSGreg Roach    private MediaFileService $media_file_service;
41ddb44b4cSGreg Roach
42c4943cffSGreg Roach    private PendingChangesService $pending_changes_service;
43ddb44b4cSGreg Roach
44ddb44b4cSGreg Roach    /**
45ddb44b4cSGreg Roach     * AddMediaFileAction constructor.
46ddb44b4cSGreg Roach     *
47ddb44b4cSGreg Roach     * @param MediaFileService      $media_file_service
48ddb44b4cSGreg Roach     * @param PendingChangesService $pending_changes_service
49ddb44b4cSGreg Roach     */
50ddb44b4cSGreg Roach    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
51ddb44b4cSGreg Roach    {
52ddb44b4cSGreg Roach        $this->media_file_service      = $media_file_service;
53ddb44b4cSGreg Roach        $this->pending_changes_service = $pending_changes_service;
54ddb44b4cSGreg Roach    }
55ddb44b4cSGreg Roach
56ddb44b4cSGreg Roach    /**
57ddb44b4cSGreg Roach     * Add a media file to an existing media object.
58ddb44b4cSGreg Roach     *
59ddb44b4cSGreg Roach     * @param ServerRequestInterface $request
60ddb44b4cSGreg Roach     *
61ddb44b4cSGreg Roach     * @return ResponseInterface
62ddb44b4cSGreg Roach     */
63ddb44b4cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
64ddb44b4cSGreg Roach    {
65b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
66b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
67ddb44b4cSGreg Roach        $media  = Registry::mediaFactory()->make($xref, $tree);
6852ed2878SGreg Roach        $media  = Auth::checkMediaAccess($media, true);
69*748dbe15SGreg Roach        $title  = Validator::parsedBody($request)->string('title');
70*748dbe15SGreg Roach        $type   = Validator::parsedBody($request)->string('type');
71ddb44b4cSGreg Roach
72ddb44b4cSGreg Roach        $file = $this->media_file_service->uploadFile($request);
73ddb44b4cSGreg Roach
74ddb44b4cSGreg Roach        if ($file === '') {
75ddb44b4cSGreg Roach            FlashMessages::addMessage(I18N::translate('There was an error uploading your file.'));
76ddb44b4cSGreg Roach
77ddb44b4cSGreg Roach            return redirect($media->url());
78ddb44b4cSGreg Roach        }
79ddb44b4cSGreg Roach
80ddb44b4cSGreg Roach        $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, '');
81ddb44b4cSGreg Roach
82ddb44b4cSGreg Roach        $media->createFact($gedcom, true);
83ddb44b4cSGreg Roach
84ddb44b4cSGreg Roach        // Accept the changes, to keep the filesystem in sync with the GEDCOM data.
85ddb44b4cSGreg Roach        $this->pending_changes_service->acceptRecord($media);
86ddb44b4cSGreg Roach
87ddb44b4cSGreg Roach        return redirect($media->url());
88ddb44b4cSGreg Roach    }
89ddb44b4cSGreg Roach}
90