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 2203f0aef8SGreg Roachuse Fisharebest\Webtrees\Registry; 23ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService; 24ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService; 25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 26ddb44b4cSGreg Roachuse Psr\Http\Message\ResponseInterface; 27ddb44b4cSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 28ddb44b4cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 29ddb44b4cSGreg Roach 30ddb44b4cSGreg Roach/** 31ddb44b4cSGreg Roach * Create a new media object. 32ddb44b4cSGreg Roach */ 33ddb44b4cSGreg Roachclass CreateMediaObjectFromFile implements RequestHandlerInterface 34ddb44b4cSGreg Roach{ 35c4943cffSGreg Roach private MediaFileService $media_file_service; 36ddb44b4cSGreg Roach 37c4943cffSGreg Roach private PendingChangesService $pending_changes_service; 38ddb44b4cSGreg Roach 39ddb44b4cSGreg Roach /** 40ddb44b4cSGreg Roach * @param MediaFileService $media_file_service 41ddb44b4cSGreg Roach * @param PendingChangesService $pending_changes_service 42ddb44b4cSGreg Roach */ 43ddb44b4cSGreg Roach public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service) 44ddb44b4cSGreg Roach { 45ddb44b4cSGreg Roach $this->media_file_service = $media_file_service; 46ddb44b4cSGreg Roach $this->pending_changes_service = $pending_changes_service; 47ddb44b4cSGreg Roach } 48ddb44b4cSGreg Roach 49ddb44b4cSGreg Roach /** 50ddb44b4cSGreg Roach * @param ServerRequestInterface $request 51ddb44b4cSGreg Roach * 52ddb44b4cSGreg Roach * @return ResponseInterface 53ddb44b4cSGreg Roach */ 54ddb44b4cSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 55ddb44b4cSGreg Roach { 56b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 57748dbe15SGreg Roach $file = Validator::parsedBody($request)->string('file'); 58748dbe15SGreg Roach $type = Validator::parsedBody($request)->string('type'); 59748dbe15SGreg Roach $title = Validator::parsedBody($request)->string('title'); 60748dbe15SGreg Roach $note = Validator::parsedBody($request)->string('note'); 61ddb44b4cSGreg Roach 62dbfc3341SGreg Roach $file = Registry::elementFactory()->make('OBJE:FILE')->canonical($file); 6303f0aef8SGreg Roach $note = Registry::elementFactory()->make('OBJE:NOTE')->canonical($note); 6403f0aef8SGreg Roach $type = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->canonical($type); 6503f0aef8SGreg Roach $title = Registry::elementFactory()->make('OBJE:FILE:TITL')->canonical($title); 6603f0aef8SGreg Roach 67ddb44b4cSGreg Roach $gedcom = "0 @@ OBJE\n" . $this->media_file_service->createMediaFileGedcom($file, $type, $title, $note); 68ddb44b4cSGreg Roach 6903f0aef8SGreg Roach $record = $tree->createRecord($gedcom); 70ddb44b4cSGreg Roach 71ddb44b4cSGreg Roach // Accept the new record. Rejecting it would leave the filesystem out-of-sync with the genealogy 7203f0aef8SGreg Roach $this->pending_changes_service->acceptRecord($record); 73ddb44b4cSGreg Roach 7403f0aef8SGreg Roach return redirect($record->url()); 75ddb44b4cSGreg Roach } 76ddb44b4cSGreg Roach} 77