xref: /webtrees/app/Http/RequestHandlers/MapDataImportPage.php (revision 52550490b7095dd69811f3ec21ed5a3ca1a8968d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Registry;
25use Fisharebest\Webtrees\Services\MapDataService;
26use Illuminate\Database\Eloquent\Collection;
27use League\Flysystem\FilesystemException;
28use League\Flysystem\StorageAttributes;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function pathinfo;
34
35use const PATHINFO_EXTENSION;
36
37/**
38 * Import geographic data.
39 */
40class MapDataImportPage implements RequestHandlerInterface
41{
42    use ViewResponseTrait;
43
44    /**
45     * @param ServerRequestInterface $request
46     *
47     * @return ResponseInterface
48     */
49    public function handle(ServerRequestInterface $request): ResponseInterface
50    {
51        $this->layout = 'layouts/administration';
52
53        $data_filesystem      = Registry::filesystem()->data();
54        $data_filesystem_name = Registry::filesystem()->dataName();
55
56        try {
57            $files = $data_filesystem->listContents('places')
58                ->filter(static function (StorageAttributes $attributes): bool {
59                    $extension = pathinfo($attributes->path(), PATHINFO_EXTENSION);
60
61                    return $extension === 'csv' || $extension === 'geojson';
62                })
63                ->map(static fn (StorageAttributes $attributes): string => pathinfo($attributes->path(), PATHINFO_BASENAME))
64                ->toArray();
65        } catch (FilesystemException) {
66            $files = [];
67        }
68
69        $files = Collection::make($files)->sort();
70
71        return $this->viewResponse('admin/map-import-form', [
72            'folder' => $data_filesystem_name . MapDataService::PLACES_FOLDER,
73            'title'  => I18N::translate('Import geographic data'),
74            'files'  => $files,
75        ]);
76    }
77}
78