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