xref: /webtrees/app/Http/RequestHandlers/ExportGedcomServer.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
16d576906SGreg Roach<?php
26d576906SGreg Roach
36d576906SGreg Roach/**
46d576906SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66d576906SGreg Roach * This program is free software: you can redistribute it and/or modify
76d576906SGreg Roach * it under the terms of the GNU General Public License as published by
86d576906SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96d576906SGreg Roach * (at your option) any later version.
106d576906SGreg Roach * This program is distributed in the hope that it will be useful,
116d576906SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126d576906SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136d576906SGreg Roach * GNU General Public License for more details.
146d576906SGreg 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/>.
166d576906SGreg Roach */
176d576906SGreg Roach
186d576906SGreg Roachdeclare(strict_types=1);
196d576906SGreg Roach
206d576906SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216d576906SGreg Roach
226d576906SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
236d576906SGreg Roachuse Fisharebest\Webtrees\Html;
246d576906SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256d576906SGreg Roachuse Fisharebest\Webtrees\I18N;
266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2769c05a6eSGreg Roachuse Fisharebest\Webtrees\Services\GedcomExportService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29f0448b68SGreg Roachuse League\Flysystem\FilesystemException;
30f0448b68SGreg Roachuse League\Flysystem\UnableToWriteFile;
316d576906SGreg Roachuse Psr\Http\Message\ResponseInterface;
326d576906SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
336d576906SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
346d576906SGreg Roach
356d576906SGreg Roachuse function fclose;
366d576906SGreg Roachuse function pathinfo;
376d576906SGreg Roachuse function redirect;
386d576906SGreg Roachuse function route;
396d576906SGreg Roachuse function strtolower;
406d576906SGreg Roach
416d576906SGreg Roachuse const PATHINFO_EXTENSION;
426d576906SGreg Roach
436d576906SGreg Roach/**
446d576906SGreg Roach * Save a GEDCOM file on the server.
456d576906SGreg Roach */
466d576906SGreg Roachclass ExportGedcomServer implements RequestHandlerInterface
476d576906SGreg Roach{
486d576906SGreg Roach    use ViewResponseTrait;
496d576906SGreg Roach
50c4943cffSGreg Roach    private GedcomExportService $gedcom_export_service;
5169c05a6eSGreg Roach
5269c05a6eSGreg Roach    /**
5369c05a6eSGreg Roach     * @param GedcomExportService $gedcom_export_service
5469c05a6eSGreg Roach     */
5569c05a6eSGreg Roach    public function __construct(GedcomExportService $gedcom_export_service)
5669c05a6eSGreg Roach    {
5769c05a6eSGreg Roach        $this->gedcom_export_service = $gedcom_export_service;
5869c05a6eSGreg Roach    }
5969c05a6eSGreg Roach
606d576906SGreg Roach    /**
616d576906SGreg Roach     * @param ServerRequestInterface $request
626d576906SGreg Roach     *
636d576906SGreg Roach     * @return ResponseInterface
646d576906SGreg Roach     */
656d576906SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
666d576906SGreg Roach    {
67b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
686d576906SGreg Roach
696b9cb339SGreg Roach        $data_filesystem = Registry::filesystem()->data();
70a04bb9a2SGreg Roach
71748dbe15SGreg Roach        $filename = Validator::parsedBody($request)->string('filename');
726d576906SGreg Roach
736d576906SGreg Roach        // Force a ".ged" suffix
746d576906SGreg Roach        if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) !== 'ged') {
756d576906SGreg Roach            $filename .= '.ged';
766d576906SGreg Roach        }
776d576906SGreg Roach
786d576906SGreg Roach        try {
79ea517a3bSGreg Roach            $resource = $this->gedcom_export_service->export($tree, true);
80ea517a3bSGreg Roach            $data_filesystem->writeStream($filename, $resource);
81ea517a3bSGreg Roach            fclose($resource);
826d576906SGreg Roach
836d576906SGreg Roach            /* I18N: %s is a filename */
846d576906SGreg Roach            FlashMessages::addMessage(I18N::translate('The family tree has been exported to %s.', Html::filename($filename)), 'success');
85f0448b68SGreg Roach        } catch (FilesystemException | UnableToWriteFile $ex) {
866d576906SGreg Roach            FlashMessages::addMessage(
876d576906SGreg Roach                I18N::translate('The file %s could not be created.', Html::filename($filename)) . '<hr><samp dir="ltr">' . $ex->getMessage() . '</samp>',
886d576906SGreg Roach                'danger'
896d576906SGreg Roach            );
906d576906SGreg Roach        }
916d576906SGreg Roach
926fd01894SGreg Roach        $url = route(ManageTrees::class, ['tree' => $tree->name()]);
936d576906SGreg Roach
946d576906SGreg Roach        return redirect($url);
956d576906SGreg Roach    }
966d576906SGreg Roach}
97