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