1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\FlashMessages; 23use Fisharebest\Webtrees\Functions\Functions; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Tree; 27use Nyholm\Psr7\UploadedFile; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Message\ServerRequestInterface; 30use Psr\Http\Message\StreamFactoryInterface; 31use Psr\Http\Server\RequestHandlerInterface; 32 33use function app; 34use function assert; 35use function basename; 36use function redirect; 37use function route; 38 39use const UPLOAD_ERR_OK; 40 41/** 42 * Import a GEDCOM file into a tree. 43 */ 44class ImportGedcomAction implements RequestHandlerInterface 45{ 46 /** 47 * @param ServerRequestInterface $request 48 * 49 * @return ResponseInterface 50 */ 51 public function handle(ServerRequestInterface $request): ResponseInterface 52 { 53 $tree = $request->getAttribute('tree'); 54 assert($tree instanceof Tree); 55 56 $data_filesystem = Registry::filesystem()->data(); 57 58 $params = (array) $request->getParsedBody(); 59 $source = $params['source']; 60 $keep_media = (bool) ($params['keep_media'] ?? false); 61 $WORD_WRAPPED_NOTES = (bool) ($params['WORD_WRAPPED_NOTES'] ?? false); 62 $GEDCOM_MEDIA_PATH = $params['GEDCOM_MEDIA_PATH']; 63 64 // Save these choices as defaults 65 $tree->setPreference('keep_media', $keep_media ? '1' : '0'); 66 $tree->setPreference('WORD_WRAPPED_NOTES', $WORD_WRAPPED_NOTES ? '1' : '0'); 67 $tree->setPreference('GEDCOM_MEDIA_PATH', $GEDCOM_MEDIA_PATH); 68 69 if ($source === 'client') { 70 $upload = $request->getUploadedFiles()['tree_name'] ?? null; 71 72 if ($upload instanceof UploadedFile) { 73 if ($upload->getError() === UPLOAD_ERR_OK) { 74 $tree->importGedcomFile($upload->getStream(), basename($upload->getClientFilename())); 75 } else { 76 FlashMessages::addMessage(Functions::fileUploadErrorText($upload->getError()), 'danger'); 77 } 78 } else { 79 FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger'); 80 } 81 } 82 83 if ($source === 'server') { 84 $basename = basename($params['tree_name'] ?? ''); 85 86 if ($basename) { 87 $resource = $data_filesystem->readStream($basename); 88 $stream = app(StreamFactoryInterface::class)->createStreamFromResource($resource); 89 $tree->importGedcomFile($stream, $basename); 90 } else { 91 FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger'); 92 } 93 } 94 95 $url = route(ManageTrees::class, ['tree' => $tree->name()]); 96 97 return redirect($url); 98 } 99} 100