. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function assert; /** * Process a form to create a new source. */ class CreateSourceAction implements RequestHandlerInterface { /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree); $params = (array) $request->getParsedBody(); $title = $params['source-title']; $abbreviation = $params['source-abbreviation']; $author = $params['source-author']; $publication = $params['source-publication']; $repository = $params['source-repository']; $call_number = $params['source-call-number']; $text = $params['source-text']; $privacy_restriction = $params['privacy-restriction']; $edit_restriction = $params['edit-restriction']; // Fix whitespace $title = trim(preg_replace('/\s+/', ' ', $title)); $abbreviation = trim(preg_replace('/\s+/', ' ', $abbreviation)); $author = trim(preg_replace('/\s+/', ' ', $author)); $publication = trim(preg_replace('/\s+/', ' ', $publication)); $repository = trim(preg_replace('/\s+/', ' ', $repository)); $call_number = trim(preg_replace('/\s+/', ' ', $call_number)); // Convert line endings to GEDCOM continuations $text = strtr($text, ["\r\n" => "\n"]); $text = strtr($text, ["\n" => "\n2 CONT "]); $gedcom = "0 @@ SOUR\n\n1 TITL " . $title; if ($abbreviation !== '') { $gedcom .= "\n1 ABBR " . $abbreviation; } if ($author !== '') { $gedcom .= "\n1 AUTH " . $author; } if ($publication !== '') { $gedcom .= "\n1 PUBL " . $publication; } if ($text !== '') { $gedcom .= "\n1 TEXT " . $text; } if ($repository !== '') { $gedcom .= "\n1 REPO @" . $repository . '@'; if ($call_number !== '') { $gedcom .= "\n2 CALN " . $call_number; } } if (in_array($privacy_restriction, ['none', 'privacy', 'confidential'], true)) { $gedcom .= "\n1 RESN " . $privacy_restriction; } if ($edit_restriction === 'locked') { $gedcom .= "\n1 RESN " . $edit_restriction; } $record = $tree->createRecord($gedcom); // id and text are for select2 / autocomplete // html is for interactive modals return response([ 'id' => $record->xref(), 'text' => view('selects/source', [ 'source' => $record, ]), 'html' => view('modals/record-created', [ 'title' => I18N::translate('The source has been created'), 'name' => $record->fullName(), 'url' => $record->url(), ]), ]); } }