From 6fc83c94594a5d218e78ba875957b8d9a6ba7a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Wed, 10 Jun 2020 23:14:19 +0200 Subject: [PATCH] MAPG-119 handle the case if no mapId is present when editing map --- public/index.php | 2 +- src/Controller/MapAdminController.php | 37 +++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/public/index.php b/public/index.php index 8c3d2bd..7f8b3d1 100644 --- a/public/index.php +++ b/public/index.php @@ -23,7 +23,7 @@ Container::$routeCollection->group('game', function (MapGuesser\Routing\RouteCol $routeCollection->post('guess-json', '{mapId}/guess.json', [MapGuesser\Controller\GameFlowController::class, 'evaluateGuess']); }); Container::$routeCollection->group('admin', function (MapGuesser\Routing\RouteCollection $routeCollection) { - $routeCollection->get('admin.mapEditor', 'mapEditor/{mapId}', [MapGuesser\Controller\MapAdminController::class, 'getMapEditor']); + $routeCollection->get('admin.mapEditor', 'mapEditor/{mapId?}', [MapGuesser\Controller\MapAdminController::class, 'getMapEditor']); $routeCollection->get('admin.place', 'place.json/{placeId}', [MapGuesser\Controller\MapAdminController::class, 'getPlace']); $routeCollection->post('admin.saveMap', 'saveMap/{mapId}/json', [MapGuesser\Controller\MapAdminController::class, 'saveMap']); }); diff --git a/src/Controller/MapAdminController.php b/src/Controller/MapAdminController.php index 2a240d4..76a1592 100644 --- a/src/Controller/MapAdminController.php +++ b/src/Controller/MapAdminController.php @@ -17,6 +17,8 @@ use MapGuesser\Util\Geo\Position; class MapAdminController implements ISecured { + private static string $unnamedMapName = '[unnamed map]'; + private IRequest $request; private MapRepository $mapRepository; @@ -41,9 +43,18 @@ class MapAdminController implements ISecured { $mapId = (int) $this->request->query('mapId'); - $map = $this->mapRepository->getById($mapId); - $bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']); - $places = $this->getPlaces($mapId); + if ($mapId) { + $map = $this->mapRepository->getById($mapId); + $bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']); + $places = $this->getPlaces($mapId); + } else { + $map = [ + 'name' => self::$unnamedMapName, + 'description' => '' + ]; + $bounds = Bounds::createDirectly(-90.0, -180.0, 90.0, 180.0); + $places = []; + } $data = ['mapId' => $mapId, 'mapName' => $map['name'], 'mapDescription' => str_replace('
', "\n", $map['description']), 'bounds' => $bounds->toArray(), 'places' => &$places]; return new HtmlContent('admin/map_editor', $data); @@ -63,6 +74,10 @@ class MapAdminController implements ISecured { $mapId = (int) $this->request->query('mapId'); + if (!$mapId) { + $mapId = $this->addNewMap(); + } + if (isset($_POST['added'])) { $addedIds = []; foreach ($_POST['added'] as $placeRaw) { @@ -107,7 +122,7 @@ class MapAdminController implements ISecured ]; if (isset($_POST['name'])) { - $map['name'] = $_POST['name'] ? $_POST['name'] : '[unnamed map]'; + $map['name'] = $_POST['name'] ? $_POST['name'] : self::$unnamedMapName; } if (isset($_POST['description'])) { $map['description'] = str_replace(["\n", "\r\n"], '
', $_POST['description']); @@ -115,7 +130,7 @@ class MapAdminController implements ISecured $this->saveMapData($mapId, $map); - $data = ['added' => $addedIds]; + $data = ['mapId' => $mapId, 'added' => $addedIds]; return new JsonContent($data); } @@ -135,6 +150,18 @@ class MapAdminController implements ISecured return $bounds; } + private function addNewMap(): int + { + $modify = new Modify(\Container::$dbConnection, 'maps'); + $modify->fill([ + 'name' => self::$unnamedMapName, + 'description' => '' + ]); + $modify->save(); + + return $modify->getId(); + } + private function saveMapData(int $mapId, array $map): void { $modify = new Modify(\Container::$dbConnection, 'maps');