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 <http://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 $params = (array) $request->getParsedBody(); 47 48 $place_id = $params['place_id'] ?? null; 49 $parent_id = $params['parent_id'] ?? null; 50 $latitude = $params['new_place_lati'] ?? ''; 51 $longitude = $params['new_place_long'] ?? ''; 52 $name = mb_substr($params['new_place_name'] ?? '', 0, 120); 53 54 if ($latitude === '' || $longitude === '') { 55 $latitude = null; 56 $longitude = null; 57 } else { 58 // 5 decimal places (locate to within about 1 metre) 59 $latitude = round((float) $latitude, 5); 60 $longitude = round((float) $longitude, 5); 61 62 // 0,0 is only allowed at the top level 63 if ($parent_id !== null && $latitude === 0.0 && $longitude === 0.0) { 64 $latitude = null; 65 $longitude = null; 66 } 67 } 68 69 if ($place_id === null) { 70 DB::table('place_location')->insert([ 71 'parent_id' => $parent_id, 72 'place' => $name, 73 'latitude' => $latitude, 74 'longitude' => $longitude, 75 ]); 76 } else { 77 DB::table('place_location') 78 ->where('id', '=', $place_id) 79 ->update([ 80 'place' => $name, 81 'latitude' => $latitude, 82 'longitude' => $longitude, 83 ]); 84 } 85 86 $message = I18N::translate('The details for “%s” have been updated.', e($name)); 87 FlashMessages::addMessage($message, 'success'); 88 89 $url = route(MapDataList::class, ['parent_id' => $parent_id]); 90 91 return redirect($url); 92 } 93} 94