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\FlashMessages; 23use Fisharebest\Webtrees\I18N; 24use Illuminate\Database\Capsule\Manager as DB; 25use Psr\Http\Message\ResponseInterface; 26use Psr\Http\Message\ServerRequestInterface; 27use Psr\Http\Server\RequestHandlerInterface; 28 29use function e; 30use function redirect; 31use function round; 32use function route; 33 34/** 35 * Controller for maintaining geographic data. 36 */ 37class MapDataSave implements RequestHandlerInterface 38{ 39 /** 40 * @param ServerRequestInterface $request 41 * 42 * @return ResponseInterface 43 */ 44 public function handle(ServerRequestInterface $request): ResponseInterface 45 { 46 $parent_id = $request->getAttribute('parent_id'); 47 $place_id = $request->getAttribute('place_id'); 48 49 $params = (array) $request->getParsedBody(); 50 51 $latitude = $params['new_place_lati'] ?? ''; 52 $longitude = $params['new_place_long'] ?? ''; 53 $name = mb_substr($params['new_place_name'] ?? '', 0, 120); 54 55 if ($latitude === '' || $longitude === '') { 56 $latitude = null; 57 $longitude = null; 58 } else { 59 // 5 decimal places (locate to within about 1 metre) 60 $latitude = round((float) $latitude, 5); 61 $longitude = round((float) $longitude, 5); 62 63 // 0,0 is only allowed at the top level 64 if ($parent_id !== null && $latitude === 0.0 && $longitude === 0.0) { 65 $latitude = null; 66 $longitude = null; 67 } 68 } 69 70 if ($place_id === null) { 71 DB::table('place_location')->insert([ 72 'parent_id' => $parent_id, 73 'place' => $name, 74 'latitude' => $latitude, 75 'longitude' => $longitude, 76 ]); 77 } else { 78 DB::table('place_location') 79 ->where('id', '=', $place_id) 80 ->update([ 81 'place' => $name, 82 'latitude' => $latitude, 83 'longitude' => $longitude, 84 ]); 85 } 86 87 $message = I18N::translate('The details for “%s” have been updated.', e($name)); 88 FlashMessages::addMessage($message, 'success'); 89 90 $url = route(MapDataList::class, ['parent_id' => $parent_id]); 91 92 return redirect($url); 93 } 94} 95