xref: /webtrees/app/Http/RequestHandlers/MapDataImportPage.php (revision e99ac601cee7ced57ac8545ceba47ece1b15d883)
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\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 function (StorageAttributes $attributes): string {
64                    return pathinfo($attributes->path(), PATHINFO_BASENAME);
65                })
66                ->toArray();
67        } catch (FilesystemException $ex) {
68            $files = [];
69        }
70
71        $files = Collection::make($files)->sort();
72
73        return $this->viewResponse('admin/map-import-form', [
74            'folder' => $data_filesystem_name . MapDataService::PLACES_FOLDER,
75            'title'  => I18N::translate('Import geographic data'),
76            'files'  => $files,
77        ]);
78    }
79}
80