1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\I18N; 23use Fisharebest\Webtrees\Registry; 24use Fisharebest\Webtrees\Validator; 25use Psr\Http\Message\ResponseInterface; 26use Psr\Http\Message\ServerRequestInterface; 27use Psr\Http\Server\RequestHandlerInterface; 28 29/** 30 * Process a form to create a new source. 31 */ 32class CreateSourceAction implements RequestHandlerInterface 33{ 34 /** 35 * @param ServerRequestInterface $request 36 * 37 * @return ResponseInterface 38 */ 39 public function handle(ServerRequestInterface $request): ResponseInterface 40 { 41 $tree = Validator::attributes($request)->tree(); 42 $title = Validator::parsedBody($request)->isNotEmpty()->string('source-title'); 43 $abbreviation = Validator::parsedBody($request)->string('source-abbreviation'); 44 $author = Validator::parsedBody($request)->string('source-author'); 45 $publication = Validator::parsedBody($request)->string('source-publication'); 46 $repository = Validator::parsedBody($request)->isXref()->string('source-repository', ''); 47 $call_number = Validator::parsedBody($request)->string('source-call-number'); 48 $text = Validator::parsedBody($request)->string('source-text'); 49 $restriction = Validator::parsedBody($request)->string('restriction'); 50 51 $title = Registry::elementFactory()->make('SOUR:TITL')->canonical($title); 52 $abbreviation = Registry::elementFactory()->make('SOUR:ABBR')->canonical($abbreviation); 53 $author = Registry::elementFactory()->make('SOUR:AUTH')->canonical($author); 54 $publication = Registry::elementFactory()->make('SOUR:PUBL')->canonical($publication); 55 $repository = Registry::elementFactory()->make('SOUR:REPO')->canonical($repository); 56 $call_number = Registry::elementFactory()->make('SOUR:REPO:CALN')->canonical($call_number); 57 $text = Registry::elementFactory()->make('SOUR:TEXT')->canonical($text); 58 $restriction = Registry::elementFactory()->make('SOUR:RESN')->canonical($restriction); 59 60 $gedcom = "0 @@ SOUR\n1 TITL " . strtr($title, ["\n" => "\n2 CONT "]); 61 62 if ($abbreviation !== '') { 63 $gedcom .= "\n1 ABBR " . strtr($abbreviation, ["\n" => "\n2 CONT "]); 64 } 65 66 if ($author !== '') { 67 $gedcom .= "\n1 AUTH " . strtr($author, ["\n" => "\n2 CONT "]); 68 } 69 70 if ($publication !== '') { 71 $gedcom .= "\n1 PUBL " . strtr($publication, ["\n" => "\n2 CONT "]); 72 } 73 74 if ($text !== '') { 75 $gedcom .= "\n1 TEXT " . strtr($text, ["\n" => "\n2 CONT "]); 76 } 77 78 if ($repository !== '') { 79 $gedcom .= "\n1 REPO @" . $repository . '@'; 80 81 if ($call_number !== '') { 82 $gedcom .= "\n2 CALN " . strtr($call_number, ["\n" => "\n3 CONT "]); 83 } 84 } 85 86 if ($restriction !== '') { 87 $gedcom .= "\n1 RESN " . strtr($restriction, ["\n" => "\n2 CONT "]); 88 } 89 90 $record = $tree->createRecord($gedcom); 91 92 // value and text are for autocomplete 93 // html is for interactive modals 94 return response([ 95 'value' => '@' . $record->xref() . '@', 96 'text' => view('selects/source', ['source' => $record]), 97 'html' => view('modals/record-created', [ 98 'title' => I18N::translate('The source has been created'), 99 'name' => $record->fullName(), 100 'url' => $record->url(), 101 ]), 102 ]); 103 } 104} 105