1d4265d07SGreg Roach<?php 2d4265d07SGreg Roach 3d4265d07SGreg Roach/** 4d4265d07SGreg Roach * webtrees: online genealogy 5d11be702SGreg 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; 23*e74a9b06SGreg Roachuse Fisharebest\Webtrees\Exceptions\FileUploadException; 24d4265d07SGreg Roachuse Fisharebest\Webtrees\I18N; 256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 26d4265d07SGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService; 27d4265d07SGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService; 28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 29d4265d07SGreg Roachuse Psr\Http\Message\ResponseInterface; 30d4265d07SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 31d4265d07SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 32d4265d07SGreg Roach 3378e8481fSGreg Roachuse function response; 34*e74a9b06SGreg Roachuse function view; 35d4265d07SGreg Roach 36d4265d07SGreg Roach/** 37d4265d07SGreg Roach * Process a form to create a new media object. 38d4265d07SGreg Roach */ 39d4265d07SGreg Roachclass CreateMediaObjectAction implements RequestHandlerInterface 40d4265d07SGreg Roach{ 41c4943cffSGreg Roach private MediaFileService $media_file_service; 42d4265d07SGreg Roach 43c4943cffSGreg Roach private PendingChangesService $pending_changes_service; 44d4265d07SGreg Roach 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 75*e74a9b06SGreg Roach try { 76d4265d07SGreg Roach $file = $this->media_file_service->uploadFile($request); 77*e74a9b06SGreg Roach } catch (FileUploadException $exception) { 78*e74a9b06SGreg Roach return response([ 79*e74a9b06SGreg Roach 'value' => '', 80*e74a9b06SGreg Roach 'text' => '', 81*e74a9b06SGreg Roach 'html' => view('components/alert-danger', [ 82*e74a9b06SGreg Roach 'alert' => $exception->getMessage(), 83*e74a9b06SGreg Roach ]), 84*e74a9b06SGreg Roach ]); 85*e74a9b06SGreg Roach } 86d4265d07SGreg Roach 87d4265d07SGreg Roach if ($file === '') { 88a3a671ccSGreg Roach return response(['error_message' => I18N::translate('There was an error uploading your file.')], StatusCodeInterface::STATUS_NOT_ACCEPTABLE); 89d4265d07SGreg Roach } 90d4265d07SGreg Roach 9145fc2659SGreg Roach $gedcom = "0 @@ OBJE\n" . $this->media_file_service->createMediaFileGedcom($file, $type, $title, $note); 92d4265d07SGreg Roach 93e3132a19SGreg Roach if ($restriction !== '') { 9403f0aef8SGreg Roach $gedcom .= "\n1 RESN " . strtr($restriction, ["\n" => "\n2 CONT "]); 95d4265d07SGreg Roach } 96d4265d07SGreg Roach 97d4265d07SGreg Roach $record = $tree->createMediaObject($gedcom); 98d4265d07SGreg Roach 99d4265d07SGreg Roach // Accept the new record to keep the filesystem synchronized with the genealogy. 100d4265d07SGreg Roach $this->pending_changes_service->acceptRecord($record); 101d4265d07SGreg Roach 102c8d78f19SGreg Roach // value and text are for autocomplete 103d4265d07SGreg Roach // html is for interactive modals 104d4265d07SGreg Roach return response([ 105c8d78f19SGreg Roach 'value' => '@' . $record->xref() . '@', 106c8d78f19SGreg Roach 'text' => view('selects/media', ['media' => $record]), 107d4265d07SGreg Roach 'html' => view('modals/record-created', [ 108d4265d07SGreg Roach 'title' => I18N::translate('The media object has been created'), 109d4265d07SGreg Roach 'name' => $record->fullName(), 110d4265d07SGreg Roach 'url' => $record->url(), 111d4265d07SGreg Roach ]), 112d4265d07SGreg Roach ]); 113d4265d07SGreg Roach } 114d4265d07SGreg Roach} 115