xref: /webtrees/app/Http/RequestHandlers/ImportGedcomAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
22b5c53c7fSGreg Roachuse Fisharebest\Webtrees\Exceptions\FileUploadException;
236fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
246fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
265cd281f4SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
28f0448b68SGreg Roachuse League\Flysystem\FilesystemException;
29f0448b68SGreg Roachuse League\Flysystem\UnableToReadFile;
306fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
316fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3200c45d23SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
336fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
346fd01894SGreg Roach
356fd01894SGreg Roachuse function basename;
366fd01894SGreg Roachuse function redirect;
376fd01894SGreg Roachuse function route;
386fd01894SGreg Roach
398dc9e1f9SGreg Roachuse const UPLOAD_ERR_NO_FILE;
406fd01894SGreg Roachuse const UPLOAD_ERR_OK;
416fd01894SGreg Roach
426fd01894SGreg Roach/**
436fd01894SGreg Roach * Import a GEDCOM file into a tree.
446fd01894SGreg Roach */
456fd01894SGreg Roachclass ImportGedcomAction implements RequestHandlerInterface
466fd01894SGreg Roach{
47ca47b2adSGreg Roach    private StreamFactoryInterface $stream_factory;
48ca47b2adSGreg Roach
49c4943cffSGreg Roach    private TreeService $tree_service;
505cd281f4SGreg Roach
5192a78a2fSGreg Roach    /**
5292a78a2fSGreg Roach     * @param StreamFactoryInterface $stream_factory
5392a78a2fSGreg Roach     * @param TreeService            $tree_service
5492a78a2fSGreg Roach     */
55ca47b2adSGreg Roach    public function __construct(StreamFactoryInterface $stream_factory, TreeService $tree_service)
565cd281f4SGreg Roach    {
575cd281f4SGreg Roach        $this->tree_service   = $tree_service;
58ca47b2adSGreg Roach        $this->stream_factory = $stream_factory;
595cd281f4SGreg Roach    }
605cd281f4SGreg Roach
616fd01894SGreg Roach    /**
626fd01894SGreg Roach     * @param ServerRequestInterface $request
636fd01894SGreg Roach     *
646fd01894SGreg Roach     * @return ResponseInterface
65f0448b68SGreg Roach     * @throws FilesystemException
66f0448b68SGreg Roach     * @throws UnableToReadFile
676fd01894SGreg Roach     */
686fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
696fd01894SGreg Roach    {
70b55cbc6bSGreg Roach        $tree               = Validator::attributes($request)->tree();
716c5ec5a2SGreg Roach        $keep_media         = Validator::parsedBody($request)->boolean('keep_media', false);
726c5ec5a2SGreg Roach        $word_wrapped_notes = Validator::parsedBody($request)->boolean('WORD_WRAPPED_NOTES', false);
736c5ec5a2SGreg Roach        $gedcom_media_path  = Validator::parsedBody($request)->string('GEDCOM_MEDIA_PATH');
746c5ec5a2SGreg Roach        $encodings          = ['' => ''] + Registry::encodingFactory()->list();
756c5ec5a2SGreg Roach        $encoding           = Validator::parsedBody($request)->isInArrayKeys($encodings)->string('encoding');
766c5ec5a2SGreg Roach        $source             = Validator::parsedBody($request)->isInArray(['client', 'server'])->string('source');
776fd01894SGreg Roach
786fd01894SGreg Roach        // Save these choices as defaults
796fd01894SGreg Roach        $tree->setPreference('keep_media', $keep_media ? '1' : '0');
806c5ec5a2SGreg Roach        $tree->setPreference('WORD_WRAPPED_NOTES', $word_wrapped_notes ? '1' : '0');
816c5ec5a2SGreg Roach        $tree->setPreference('GEDCOM_MEDIA_PATH', $gedcom_media_path);
826fd01894SGreg Roach
836fd01894SGreg Roach        if ($source === 'client') {
846c5ec5a2SGreg Roach            $client_file = $request->getUploadedFiles()['client_file'] ?? null;
856fd01894SGreg Roach
866c5ec5a2SGreg Roach            if ($client_file === null || $client_file->getError() === UPLOAD_ERR_NO_FILE) {
878dc9e1f9SGreg Roach                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
886c5ec5a2SGreg Roach
896c5ec5a2SGreg Roach                return redirect(route(ImportGedcomPage::class, ['tree' => $tree->name()]));
906fd01894SGreg Roach            }
916c5ec5a2SGreg Roach
926c5ec5a2SGreg Roach            if ($client_file->getError() !== UPLOAD_ERR_OK) {
936c5ec5a2SGreg Roach                throw new FileUploadException($client_file);
946c5ec5a2SGreg Roach            }
956c5ec5a2SGreg Roach
966c5ec5a2SGreg Roach            $this->tree_service->importGedcomFile($tree, $client_file->getStream(), basename($client_file->getClientFilename()), $encoding);
978dc9e1f9SGreg Roach        }
986fd01894SGreg Roach
996fd01894SGreg Roach        if ($source === 'server') {
1006c5ec5a2SGreg Roach            $server_file = Validator::parsedBody($request)->string('server_file');
1016fd01894SGreg Roach
1026c5ec5a2SGreg Roach            if ($server_file === '') {
1038dc9e1f9SGreg Roach                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
1046c5ec5a2SGreg Roach
1056c5ec5a2SGreg Roach                return redirect(route(ImportGedcomPage::class, ['tree' => $tree->name()]));
1066c5ec5a2SGreg Roach            }
1076c5ec5a2SGreg Roach
1086c5ec5a2SGreg Roach            $resource = Registry::filesystem()->data()->readStream($server_file);
109ca47b2adSGreg Roach            $stream   = $this->stream_factory->createStreamFromResource($resource);
1106c5ec5a2SGreg Roach            $this->tree_service->importGedcomFile($tree, $stream, $server_file, $encoding);
1116fd01894SGreg Roach        }
1126fd01894SGreg Roach
1136c5ec5a2SGreg Roach        return redirect(route(ManageTrees::class, ['tree' => $tree->name()]));
1146fd01894SGreg Roach    }
1156fd01894SGreg Roach}
116