xref: /webtrees/app/Http/RequestHandlers/MapDataImportPage.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
190949315SGreg Roach<?php
290949315SGreg Roach
390949315SGreg Roach/**
490949315SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
690949315SGreg Roach * This program is free software: you can redistribute it and/or modify
790949315SGreg Roach * it under the terms of the GNU General Public License as published by
890949315SGreg Roach * the Free Software Foundation, either version 3 of the License, or
990949315SGreg Roach * (at your option) any later version.
1090949315SGreg Roach * This program is distributed in the hope that it will be useful,
1190949315SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1290949315SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1390949315SGreg Roach * GNU General Public License for more details.
1490949315SGreg 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/>.
1690949315SGreg Roach */
1790949315SGreg Roach
1890949315SGreg Roachdeclare(strict_types=1);
1990949315SGreg Roach
2090949315SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2190949315SGreg Roach
2290949315SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2390949315SGreg Roachuse Fisharebest\Webtrees\I18N;
2490949315SGreg Roachuse Fisharebest\Webtrees\Registry;
2590949315SGreg Roachuse Fisharebest\Webtrees\Services\MapDataService;
2690949315SGreg Roachuse Illuminate\Database\Eloquent\Collection;
27f0448b68SGreg Roachuse League\Flysystem\FilesystemException;
28f7cf8a15SGreg Roachuse League\Flysystem\StorageAttributes;
2990949315SGreg Roachuse Psr\Http\Message\ResponseInterface;
3090949315SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3190949315SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3290949315SGreg Roach
33f7cf8a15SGreg Roachuse function pathinfo;
3490949315SGreg Roach
35f7cf8a15SGreg Roachuse const PATHINFO_EXTENSION;
36f7cf8a15SGreg Roach
3790949315SGreg Roach/**
3890949315SGreg Roach * Import geographic data.
3990949315SGreg Roach */
4090949315SGreg Roachclass MapDataImportPage implements RequestHandlerInterface
4190949315SGreg Roach{
4290949315SGreg Roach    use ViewResponseTrait;
4390949315SGreg Roach
4490949315SGreg Roach    /**
4590949315SGreg Roach     * @param ServerRequestInterface $request
4690949315SGreg Roach     *
4790949315SGreg Roach     * @return ResponseInterface
4890949315SGreg Roach     */
4990949315SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
5090949315SGreg Roach    {
5190949315SGreg Roach        $this->layout = 'layouts/administration';
5290949315SGreg Roach
5390949315SGreg Roach        $data_filesystem      = Registry::filesystem()->data();
5490949315SGreg Roach        $data_filesystem_name = Registry::filesystem()->dataName();
5590949315SGreg Roach
56f0448b68SGreg Roach        try {
57f7cf8a15SGreg Roach            $files = $data_filesystem->listContents('places')
58f7cf8a15SGreg Roach                ->filter(static function (StorageAttributes $attributes): bool {
59f7cf8a15SGreg Roach                    $extension = pathinfo($attributes->path(), PATHINFO_EXTENSION);
6090949315SGreg Roach
6190949315SGreg Roach                    return $extension === 'csv' || $extension === 'geojson';
6290949315SGreg Roach                })
63*f25fc0f9SGreg Roach                ->map(static fn (StorageAttributes $attributes): string => pathinfo($attributes->path(), PATHINFO_BASENAME))
64f0448b68SGreg Roach                ->toArray();
6528d026adSGreg Roach        } catch (FilesystemException) {
66f0448b68SGreg Roach            $files = [];
67f0448b68SGreg Roach        }
68f7cf8a15SGreg Roach
69f7cf8a15SGreg Roach        $files = Collection::make($files)->sort();
7090949315SGreg Roach
7190949315SGreg Roach        return $this->viewResponse('admin/map-import-form', [
7290949315SGreg Roach            'folder' => $data_filesystem_name . MapDataService::PLACES_FOLDER,
7390949315SGreg Roach            'title'  => I18N::translate('Import geographic data'),
7490949315SGreg Roach            'files'  => $files,
7590949315SGreg Roach        ]);
7690949315SGreg Roach    }
7790949315SGreg Roach}
78