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