xref: /webtrees/app/Http/RequestHandlers/MapDataImportPage.php (revision 090a06287954f43677f06ea778b3f67c029de8fe)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\StorageAttributes;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function pathinfo;
33use function strtolower;
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        $files = $data_filesystem->listContents('places')
57            ->filter(static function (StorageAttributes $attributes): bool {
58                $extension = pathinfo($attributes->path(), PATHINFO_EXTENSION);
59
60                return $extension === 'csv' || $extension === 'geojson';
61            })
62            ->map(static function (StorageAttributes $attributes): string {
63                return pathinfo($attributes->path(), PATHINFO_BASENAME);
64            });
65
66        $files = Collection::make($files)->sort();
67
68        return $this->viewResponse('admin/map-import-form', [
69            'folder' => $data_filesystem_name . MapDataService::PLACES_FOLDER,
70            'title'  => I18N::translate('Import geographic data'),
71            'files'  => $files,
72        ]);
73    }
74}
75