diff options
| author | Greg Roach <greg@subaqua.co.uk> | 2020-09-16 11:47:02 +0100 |
|---|---|---|
| committer | Greg Roach <greg@subaqua.co.uk> | 2020-09-16 16:37:35 +0100 |
| commit | 6fd01894a78d321fac365dd0291a2fc52129fa03 (patch) | |
| tree | 4d4f463ef3fd78977aed9ec26515f0ee9e792216 /app/Http/RequestHandlers/ImportGedcomAction.php | |
| parent | 1e9c29b270de6e44b7ea24e3dd37c4b17859fdc5 (diff) | |
| download | webtrees-6fd01894a78d321fac365dd0291a2fc52129fa03.tar.gz webtrees-6fd01894a78d321fac365dd0291a2fc52129fa03.tar.bz2 webtrees-6fd01894a78d321fac365dd0291a2fc52129fa03.zip | |
Refactor controller-actions as request-handlers
Diffstat (limited to 'app/Http/RequestHandlers/ImportGedcomAction.php')
| -rw-r--r-- | app/Http/RequestHandlers/ImportGedcomAction.php | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/app/Http/RequestHandlers/ImportGedcomAction.php b/app/Http/RequestHandlers/ImportGedcomAction.php new file mode 100644 index 0000000000..0d89e26df6 --- /dev/null +++ b/app/Http/RequestHandlers/ImportGedcomAction.php @@ -0,0 +1,101 @@ +<?php + +/** + * webtrees: online genealogy + * Copyright (C) 2020 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\Factory; +use Fisharebest\Webtrees\FlashMessages; +use Fisharebest\Webtrees\Functions\Functions; +use Fisharebest\Webtrees\I18N; +use Fisharebest\Webtrees\Services\AdminService; +use Fisharebest\Webtrees\Services\TimeoutService; +use Fisharebest\Webtrees\Tree; +use Nyholm\Psr7\UploadedFile; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Server\RequestHandlerInterface; + +use function app; +use function assert; +use function basename; +use function redirect; +use function route; + +use const UPLOAD_ERR_OK; + +/** + * Import a GEDCOM file into a tree. + */ +class ImportGedcomAction implements RequestHandlerInterface +{ + /** + * @param ServerRequestInterface $request + * + * @return ResponseInterface + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + $tree = $request->getAttribute('tree'); + assert($tree instanceof Tree); + + $data_filesystem = Factory::filesystem()->data(); + + $params = (array) $request->getParsedBody(); + $source = $params['source']; + $keep_media = (bool) ($params['keep_media'] ?? false); + $WORD_WRAPPED_NOTES = (bool) ($params['WORD_WRAPPED_NOTES'] ?? false); + $GEDCOM_MEDIA_PATH = $params['GEDCOM_MEDIA_PATH']; + + // Save these choices as defaults + $tree->setPreference('keep_media', $keep_media ? '1' : '0'); + $tree->setPreference('WORD_WRAPPED_NOTES', $WORD_WRAPPED_NOTES ? '1' : '0'); + $tree->setPreference('GEDCOM_MEDIA_PATH', $GEDCOM_MEDIA_PATH); + + if ($source === 'client') { + $upload = $request->getUploadedFiles()['tree_name'] ?? null; + + if ($upload instanceof UploadedFile) { + if ($upload->getError() === UPLOAD_ERR_OK) { + $tree->importGedcomFile($upload->getStream(), basename($upload->getClientFilename())); + } else { + FlashMessages::addMessage(Functions::fileUploadErrorText($upload->getError()), 'danger'); + } + } else { + FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger'); + } + } + + if ($source === 'server') { + $basename = basename($params['tree_name'] ?? ''); + + if ($basename) { + $resource = $data_filesystem->readStream($basename); + $stream = app(StreamFactoryInterface::class)->createStreamFromResource($resource); + $tree->importGedcomFile($stream, $basename); + } else { + FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger'); + } + } + + $url = route(ManageTrees::class, ['tree' => $tree->name()]); + + return redirect($url); + } +} |
