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