MAPG-119 handle the case if no mapId is present when editing map
This commit is contained in:
parent
5237c3f454
commit
6fc83c9459
@ -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']);
|
||||
});
|
||||
|
@ -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');
|
||||
|
||||
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('<br>', "\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"], '<br>', $_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');
|
||||
|
Loading…
Reference in New Issue
Block a user