xref: /webtrees/app/Http/RequestHandlers/ImportGedcomAction.php (revision 6fd01894a78d321fac365dd0291a2fc52129fa03)
1*6fd01894SGreg Roach<?php
2*6fd01894SGreg Roach
3*6fd01894SGreg Roach/**
4*6fd01894SGreg Roach * webtrees: online genealogy
5*6fd01894SGreg Roach * Copyright (C) 2020 webtrees development team
6*6fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
7*6fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
8*6fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*6fd01894SGreg Roach * (at your option) any later version.
10*6fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
11*6fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*6fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*6fd01894SGreg Roach * GNU General Public License for more details.
14*6fd01894SGreg Roach * You should have received a copy of the GNU General Public License
15*6fd01894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*6fd01894SGreg Roach */
17*6fd01894SGreg Roach
18*6fd01894SGreg Roachdeclare(strict_types=1);
19*6fd01894SGreg Roach
20*6fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*6fd01894SGreg Roach
22*6fd01894SGreg Roachuse Fisharebest\Webtrees\Factory;
23*6fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24*6fd01894SGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
25*6fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
26*6fd01894SGreg Roachuse Fisharebest\Webtrees\Services\AdminService;
27*6fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
28*6fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
29*6fd01894SGreg Roachuse Nyholm\Psr7\UploadedFile;
30*6fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
31*6fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32*6fd01894SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
33*6fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
34*6fd01894SGreg Roach
35*6fd01894SGreg Roachuse function app;
36*6fd01894SGreg Roachuse function assert;
37*6fd01894SGreg Roachuse function basename;
38*6fd01894SGreg Roachuse function redirect;
39*6fd01894SGreg Roachuse function route;
40*6fd01894SGreg Roach
41*6fd01894SGreg Roachuse const UPLOAD_ERR_OK;
42*6fd01894SGreg Roach
43*6fd01894SGreg Roach/**
44*6fd01894SGreg Roach * Import a GEDCOM file into a tree.
45*6fd01894SGreg Roach */
46*6fd01894SGreg Roachclass ImportGedcomAction implements RequestHandlerInterface
47*6fd01894SGreg Roach{
48*6fd01894SGreg Roach    /**
49*6fd01894SGreg Roach     * @param ServerRequestInterface $request
50*6fd01894SGreg Roach     *
51*6fd01894SGreg Roach     * @return ResponseInterface
52*6fd01894SGreg Roach     */
53*6fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
54*6fd01894SGreg Roach    {
55*6fd01894SGreg Roach        $tree = $request->getAttribute('tree');
56*6fd01894SGreg Roach        assert($tree instanceof Tree);
57*6fd01894SGreg Roach
58*6fd01894SGreg Roach        $data_filesystem = Factory::filesystem()->data();
59*6fd01894SGreg Roach
60*6fd01894SGreg Roach        $params             = (array) $request->getParsedBody();
61*6fd01894SGreg Roach        $source             = $params['source'];
62*6fd01894SGreg Roach        $keep_media         = (bool) ($params['keep_media'] ?? false);
63*6fd01894SGreg Roach        $WORD_WRAPPED_NOTES = (bool) ($params['WORD_WRAPPED_NOTES'] ?? false);
64*6fd01894SGreg Roach        $GEDCOM_MEDIA_PATH  = $params['GEDCOM_MEDIA_PATH'];
65*6fd01894SGreg Roach
66*6fd01894SGreg Roach        // Save these choices as defaults
67*6fd01894SGreg Roach        $tree->setPreference('keep_media', $keep_media ? '1' : '0');
68*6fd01894SGreg Roach        $tree->setPreference('WORD_WRAPPED_NOTES', $WORD_WRAPPED_NOTES ? '1' : '0');
69*6fd01894SGreg Roach        $tree->setPreference('GEDCOM_MEDIA_PATH', $GEDCOM_MEDIA_PATH);
70*6fd01894SGreg Roach
71*6fd01894SGreg Roach        if ($source === 'client') {
72*6fd01894SGreg Roach            $upload = $request->getUploadedFiles()['tree_name'] ?? null;
73*6fd01894SGreg Roach
74*6fd01894SGreg Roach            if ($upload instanceof UploadedFile) {
75*6fd01894SGreg Roach                if ($upload->getError() === UPLOAD_ERR_OK) {
76*6fd01894SGreg Roach                    $tree->importGedcomFile($upload->getStream(), basename($upload->getClientFilename()));
77*6fd01894SGreg Roach                } else {
78*6fd01894SGreg Roach                    FlashMessages::addMessage(Functions::fileUploadErrorText($upload->getError()), 'danger');
79*6fd01894SGreg Roach                }
80*6fd01894SGreg Roach            } else {
81*6fd01894SGreg Roach                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
82*6fd01894SGreg Roach            }
83*6fd01894SGreg Roach        }
84*6fd01894SGreg Roach
85*6fd01894SGreg Roach        if ($source === 'server') {
86*6fd01894SGreg Roach            $basename = basename($params['tree_name'] ?? '');
87*6fd01894SGreg Roach
88*6fd01894SGreg Roach            if ($basename) {
89*6fd01894SGreg Roach                $resource = $data_filesystem->readStream($basename);
90*6fd01894SGreg Roach                $stream   = app(StreamFactoryInterface::class)->createStreamFromResource($resource);
91*6fd01894SGreg Roach                $tree->importGedcomFile($stream, $basename);
92*6fd01894SGreg Roach            } else {
93*6fd01894SGreg Roach                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
94*6fd01894SGreg Roach            }
95*6fd01894SGreg Roach        }
96*6fd01894SGreg Roach
97*6fd01894SGreg Roach        $url = route(ManageTrees::class, ['tree' => $tree->name()]);
98*6fd01894SGreg Roach
99*6fd01894SGreg Roach        return redirect($url);
100*6fd01894SGreg Roach    }
101*6fd01894SGreg Roach}
102