diff options
| author | Greg Roach <fisharebest@webtrees.net> | 2019-10-31 21:14:50 +0000 |
|---|---|---|
| committer | Greg Roach <fisharebest@webtrees.net> | 2019-10-31 22:17:06 +0000 |
| commit | 852ede8c85e501a9df6f3c9d31e5933428e18cc2 (patch) | |
| tree | 2601be740fe0381b55874330b868ad3cf25ecb7b /app/Http/RequestHandlers/CreateSourceAction.php | |
| parent | c46acbbbf753da0acbc2bf49466c56c08f03cf99 (diff) | |
| download | webtrees-852ede8c85e501a9df6f3c9d31e5933428e18cc2.tar.gz webtrees-852ede8c85e501a9df6f3c9d31e5933428e18cc2.tar.bz2 webtrees-852ede8c85e501a9df6f3c9d31e5933428e18cc2.zip | |
Refactor controllers as request handlers
Diffstat (limited to 'app/Http/RequestHandlers/CreateSourceAction.php')
| -rw-r--r-- | app/Http/RequestHandlers/CreateSourceAction.php | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/app/Http/RequestHandlers/CreateSourceAction.php b/app/Http/RequestHandlers/CreateSourceAction.php new file mode 100644 index 0000000000..e76fbfceaf --- /dev/null +++ b/app/Http/RequestHandlers/CreateSourceAction.php @@ -0,0 +1,121 @@ +<?php + +/** + * webtrees: online genealogy + * Copyright (C) 2019 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +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 = $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 GEDDCOM continuations + $text = str_replace([ + "\r\n", + "\r", + "\n", + ], "\n1 CONT ", $text); + + $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(), + ]), + ]); + } +} |
