xref: /webtrees/app/Http/RequestHandlers/CreateMediaObjectAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
1d4265d07SGreg Roach<?php
2d4265d07SGreg Roach
3d4265d07SGreg Roach/**
4d4265d07SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d4265d07SGreg Roach * This program is free software: you can redistribute it and/or modify
7d4265d07SGreg Roach * it under the terms of the GNU General Public License as published by
8d4265d07SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d4265d07SGreg Roach * (at your option) any later version.
10d4265d07SGreg Roach * This program is distributed in the hope that it will be useful,
11d4265d07SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d4265d07SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d4265d07SGreg Roach * GNU General Public License for more details.
14d4265d07SGreg 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/>.
16d4265d07SGreg Roach */
17d4265d07SGreg Roach
18d4265d07SGreg Roachdeclare(strict_types=1);
19d4265d07SGreg Roach
20d4265d07SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d4265d07SGreg Roach
22a3a671ccSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23d4265d07SGreg Roachuse Fisharebest\Webtrees\I18N;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
25d4265d07SGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
26d4265d07SGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
28d4265d07SGreg Roachuse Psr\Http\Message\ResponseInterface;
29d4265d07SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30d4265d07SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31d4265d07SGreg Roach
3278e8481fSGreg Roachuse function response;
33d4265d07SGreg Roach
34d4265d07SGreg Roach/**
35d4265d07SGreg Roach * Process a form to create a new media object.
36d4265d07SGreg Roach */
37d4265d07SGreg Roachclass CreateMediaObjectAction implements RequestHandlerInterface
38d4265d07SGreg Roach{
39c4943cffSGreg Roach    private MediaFileService $media_file_service;
40d4265d07SGreg Roach
41c4943cffSGreg Roach    private PendingChangesService $pending_changes_service;
42d4265d07SGreg Roach
43d4265d07SGreg Roach    /**
44d4265d07SGreg Roach     * CreateMediaObjectAction constructor.
45d4265d07SGreg Roach     *
46d4265d07SGreg Roach     * @param MediaFileService      $media_file_service
47d4265d07SGreg Roach     * @param PendingChangesService $pending_changes_service
48d4265d07SGreg Roach     */
49d4265d07SGreg Roach    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
50d4265d07SGreg Roach    {
51d4265d07SGreg Roach        $this->media_file_service      = $media_file_service;
52d4265d07SGreg Roach        $this->pending_changes_service = $pending_changes_service;
53d4265d07SGreg Roach    }
54d4265d07SGreg Roach
55d4265d07SGreg Roach    /**
56d4265d07SGreg Roach     * Process a form to create a new media object.
57d4265d07SGreg Roach     *
58d4265d07SGreg Roach     * @param ServerRequestInterface $request
59d4265d07SGreg Roach     *
60d4265d07SGreg Roach     * @return ResponseInterface
61d4265d07SGreg Roach     */
62d4265d07SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
63d4265d07SGreg Roach    {
64b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
65748dbe15SGreg Roach        $note        = Validator::parsedBody($request)->string('media-note');
66748dbe15SGreg Roach        $title       = Validator::parsedBody($request)->string('title');
67748dbe15SGreg Roach        $type        = Validator::parsedBody($request)->string('type');
6803f0aef8SGreg Roach        $restriction = Validator::parsedBody($request)->string('restriction');
6903f0aef8SGreg Roach
7003f0aef8SGreg Roach        $note        = Registry::elementFactory()->make('OBJE:NOTE')->canonical($note);
7103f0aef8SGreg Roach        $type        = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->canonical($type);
7203f0aef8SGreg Roach        $title       = Registry::elementFactory()->make('OBJE:FILE:TITL')->canonical($title);
7303f0aef8SGreg Roach        $restriction = Registry::elementFactory()->make('OBJE:RESN')->canonical($restriction);
74d4265d07SGreg Roach
75d4265d07SGreg Roach        $file = $this->media_file_service->uploadFile($request);
76d4265d07SGreg Roach
77d4265d07SGreg Roach        if ($file === '') {
78a3a671ccSGreg Roach            return response(['error_message' => I18N::translate('There was an error uploading your file.')], StatusCodeInterface::STATUS_NOT_ACCEPTABLE);
79d4265d07SGreg Roach        }
80d4265d07SGreg Roach
8145fc2659SGreg Roach        $gedcom = "0 @@ OBJE\n" . $this->media_file_service->createMediaFileGedcom($file, $type, $title, $note);
82d4265d07SGreg Roach
83e3132a19SGreg Roach        if ($restriction !== '') {
8403f0aef8SGreg Roach            $gedcom .= "\n1 RESN " . strtr($restriction, ["\n" => "\n2 CONT "]);
85d4265d07SGreg Roach        }
86d4265d07SGreg Roach
87d4265d07SGreg Roach        $record = $tree->createMediaObject($gedcom);
88d4265d07SGreg Roach
89d4265d07SGreg Roach        // Accept the new record to keep the filesystem synchronized with the genealogy.
90d4265d07SGreg Roach        $this->pending_changes_service->acceptRecord($record);
91d4265d07SGreg Roach
92c8d78f19SGreg Roach        // value and text are for autocomplete
93d4265d07SGreg Roach        // html is for interactive modals
94d4265d07SGreg Roach        return response([
95c8d78f19SGreg Roach            'value' => '@' . $record->xref() . '@',
96c8d78f19SGreg Roach            'text'  => view('selects/media', ['media' => $record]),
97d4265d07SGreg Roach            'html'  => view('modals/record-created', [
98d4265d07SGreg Roach                'title' => I18N::translate('The media object has been created'),
99d4265d07SGreg Roach                'name'  => $record->fullName(),
100d4265d07SGreg Roach                'url'   => $record->url(),
101d4265d07SGreg Roach            ]),
102d4265d07SGreg Roach        ]);
103d4265d07SGreg Roach    }
104d4265d07SGreg Roach}
105