190949315SGreg Roach<?php 290949315SGreg Roach 390949315SGreg Roach/** 490949315SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 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\FlashMessages; 2390949315SGreg Roachuse Fisharebest\Webtrees\I18N; 24*748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator; 2590949315SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2690949315SGreg Roachuse Psr\Http\Message\ResponseInterface; 2790949315SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 2890949315SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 2990949315SGreg Roach 3090949315SGreg Roachuse function e; 3190949315SGreg Roachuse function redirect; 3290949315SGreg Roachuse function round; 3390949315SGreg Roachuse function route; 3490949315SGreg Roach 3590949315SGreg Roach/** 3690949315SGreg Roach * Controller for maintaining geographic data. 3790949315SGreg Roach */ 3890949315SGreg Roachclass MapDataSave implements RequestHandlerInterface 3990949315SGreg Roach{ 4090949315SGreg Roach /** 4190949315SGreg Roach * @param ServerRequestInterface $request 4290949315SGreg Roach * 4390949315SGreg Roach * @return ResponseInterface 4490949315SGreg Roach */ 4590949315SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 4690949315SGreg Roach { 47*748dbe15SGreg Roach $parent_id = Validator::parsedBody($request)->string('parent_id'); 48*748dbe15SGreg Roach $place_id = Validator::parsedBody($request)->string('place_id'); 49*748dbe15SGreg Roach $latitude = Validator::parsedBody($request)->string('new_place_lati'); 50*748dbe15SGreg Roach $longitude = Validator::parsedBody($request)->string('new_place_long'); 51*748dbe15SGreg Roach $name = Validator::parsedBody($request)->string('new_place_name'); 5290949315SGreg Roach 53*748dbe15SGreg Roach $name = mb_substr($name, 0, 120); 542a93faa6SGreg Roach $place_id = $place_id === '' ? null : $place_id; 552a93faa6SGreg Roach $parent_id = $parent_id === '' ? null : $parent_id; 562a93faa6SGreg Roach 5790949315SGreg Roach if ($latitude === '' || $longitude === '') { 5890949315SGreg Roach $latitude = null; 5990949315SGreg Roach $longitude = null; 6090949315SGreg Roach } else { 6190949315SGreg Roach // 5 decimal places (locate to within about 1 metre) 6290949315SGreg Roach $latitude = round((float) $latitude, 5); 6390949315SGreg Roach $longitude = round((float) $longitude, 5); 6490949315SGreg Roach 6590949315SGreg Roach // 0,0 is only allowed at the top level 6690949315SGreg Roach if ($parent_id !== null && $latitude === 0.0 && $longitude === 0.0) { 6790949315SGreg Roach $latitude = null; 6890949315SGreg Roach $longitude = null; 6990949315SGreg Roach } 7090949315SGreg Roach } 7190949315SGreg Roach 72101f22beSGreg Roach if ($place_id === null) { 732a93faa6SGreg Roach $exists_query = DB::table('place_location')->where('place', '=', $name); 742a93faa6SGreg Roach 752a93faa6SGreg Roach if ($parent_id === null) { 762a93faa6SGreg Roach $exists_query->whereNull('parent_id'); 772a93faa6SGreg Roach } else { 782a93faa6SGreg Roach $exists_query->where('parent_id', '=', $parent_id); 792a93faa6SGreg Roach } 802a93faa6SGreg Roach 812a93faa6SGreg Roach if (!$exists_query->exists()) { 8290949315SGreg Roach DB::table('place_location')->insert([ 8390949315SGreg Roach 'parent_id' => $parent_id, 8490949315SGreg Roach 'place' => $name, 8590949315SGreg Roach 'latitude' => $latitude, 8690949315SGreg Roach 'longitude' => $longitude, 8790949315SGreg Roach ]); 882a93faa6SGreg Roach 892a93faa6SGreg Roach $message = I18N::translate('The location has been created', e($name)); 902a93faa6SGreg Roach FlashMessages::addMessage($message, 'success'); 912a93faa6SGreg Roach } 9290949315SGreg Roach } else { 9390949315SGreg Roach DB::table('place_location') 9490949315SGreg Roach ->where('id', '=', $place_id) 9590949315SGreg Roach ->update([ 9690949315SGreg Roach 'place' => $name, 9790949315SGreg Roach 'latitude' => $latitude, 9890949315SGreg Roach 'longitude' => $longitude, 9990949315SGreg Roach ]); 10090949315SGreg Roach 10190949315SGreg Roach $message = I18N::translate('The details for “%s” have been updated.', e($name)); 10290949315SGreg Roach FlashMessages::addMessage($message, 'success'); 1032a93faa6SGreg Roach } 10490949315SGreg Roach 10590949315SGreg Roach $url = route(MapDataList::class, ['parent_id' => $parent_id]); 10690949315SGreg Roach 10790949315SGreg Roach return redirect($url); 10890949315SGreg Roach } 10990949315SGreg Roach} 110