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