xref: /webtrees/app/Http/RequestHandlers/ExportGedcomPage.php (revision 69e77fbed1b3a0c2e8dc70521fc05e292d5d7e23)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Validator;
25use Psr\Http\Message\ResponseInterface;
26use Psr\Http\Message\ServerRequestInterface;
27use Psr\Http\Server\RequestHandlerInterface;
28
29use function date;
30use function e;
31use function extension_loaded;
32use function pathinfo;
33use function strtolower;
34use function substr;
35
36use const PATHINFO_EXTENSION;
37
38/**
39 * Show download forms/optiosn.
40 */
41class ExportGedcomPage implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    /**
46     * @param ServerRequestInterface $request
47     *
48     * @return ResponseInterface
49     */
50    public function handle(ServerRequestInterface $request): ResponseInterface
51    {
52        $tree  = Validator::attributes($request)->tree();
53        $title = I18N::translate('Export a GEDCOM file') . ' — ' . e($tree->title());
54
55        $this->layout = 'layouts/administration';
56
57        $filename = $tree->name();
58
59        // Force a ".ged" suffix
60        if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) === 'ged') {
61            $download_filename  = substr($filename, 0, -4);
62        } else {
63            $download_filename = $filename;
64        }
65
66        $download_filenames = [
67            $download_filename                  => $download_filename,
68            $download_filename . date('-Y-m-d') => $download_filename . date('-Y-m-d'),
69        ];
70
71        return $this->viewResponse('admin/trees-export', [
72            'download_filenames' => $download_filenames,
73            'filename'           => $filename,
74            'title'              => $title,
75            'tree'               => $tree,
76            'zip_available'      => extension_loaded('zip'),
77        ]);
78    }
79}
80