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