MAPG-103 implement saving the modified map on server side
This commit is contained in:
parent
88e8c5c027
commit
5dd877d075
@ -1,5 +1,7 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use DateTime;
|
||||
use MapGuesser\Database\Query\Modify;
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\Interfaces\Authentication\IUser;
|
||||
use MapGuesser\Interfaces\Authorization\ISecured;
|
||||
@ -10,6 +12,7 @@ use MapGuesser\Repository\PlaceRepository;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
use MapGuesser\Util\Geo\Bounds;
|
||||
use MapGuesser\Util\Geo\Position;
|
||||
|
||||
class MapAdminController implements ISecured
|
||||
{
|
||||
@ -63,9 +66,52 @@ class MapAdminController implements ISecured
|
||||
{
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
|
||||
//TODO
|
||||
if (isset($_POST['added'])) {
|
||||
$addedIds = [];
|
||||
foreach ($_POST['added'] as $placeRaw) {
|
||||
$placeRaw = json_decode($placeRaw, true);
|
||||
|
||||
$data = ['added' => []];
|
||||
$addedIds[] = ['tempId' => $placeRaw['id'], $this->placeRepository->addToMap($mapId, [
|
||||
'lat' => (float) $placeRaw['lat'],
|
||||
'lng' => (float) $placeRaw['lng'],
|
||||
'pano_id_cached_timestamp' => $placeRaw['panoId'] === -1 ? (new DateTime('-1 day'))->format('Y-m-d H:i:s') : null
|
||||
])];
|
||||
}
|
||||
} else {
|
||||
$addedIds = [];
|
||||
}
|
||||
|
||||
if (isset($_POST['edited'])) {
|
||||
foreach ($_POST['edited'] as $placeRaw) {
|
||||
$placeRaw = json_decode($placeRaw, true);
|
||||
|
||||
$this->placeRepository->modify((int) $placeRaw['id'], [
|
||||
'lat' => (float) $placeRaw['lat'],
|
||||
'lng' => (float) $placeRaw['lng']
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['deleted'])) {
|
||||
foreach ($_POST['deleted'] as $placeRaw) {
|
||||
$placeRaw = json_decode($placeRaw, true);
|
||||
|
||||
$this->placeRepository->delete($placeRaw['id']);
|
||||
}
|
||||
}
|
||||
|
||||
$mapBounds = $this->calculateMapBounds($mapId);
|
||||
|
||||
$map = [
|
||||
'bound_south_lat' => $mapBounds->getSouthLat(),
|
||||
'bound_west_lng' => $mapBounds->getWestLng(),
|
||||
'bound_north_lat' => $mapBounds->getNorthLat(),
|
||||
'bound_east_lng' => $mapBounds->getEastLng()
|
||||
];
|
||||
|
||||
$this->saveMapData($mapId, $map);
|
||||
|
||||
$data = ['added' => $addedIds];
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
@ -82,12 +128,35 @@ class MapAdminController implements ISecured
|
||||
return $bounds;
|
||||
}
|
||||
|
||||
private function calculateMapBounds(int $mapId): Bounds
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection, 'places');
|
||||
$select->columns(['lat', 'lng']);
|
||||
$select->where('map_id', '=', $mapId);
|
||||
|
||||
$result = $select->execute();
|
||||
|
||||
$bounds = new Bounds();
|
||||
while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
||||
$bounds->extend(new Position($place['lat'], $place['lng']));
|
||||
}
|
||||
|
||||
return $bounds;
|
||||
}
|
||||
|
||||
private function saveMapData(int $mapId, array $map): void
|
||||
{
|
||||
$modify = new Modify(\Container::$dbConnection, 'maps');
|
||||
$modify->setId($mapId);
|
||||
$modify->fill($map);
|
||||
$modify->save();
|
||||
}
|
||||
|
||||
private function &getPlaces(int $mapId): array
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection, 'places');
|
||||
$select->columns(['id', 'lat', 'lng', 'pano_id_cached', 'pano_id_cached_timestamp']);
|
||||
$select->where('map_id', '=', $mapId);
|
||||
$select->orderBy('lng');
|
||||
|
||||
$result = $select->execute();
|
||||
|
||||
|
@ -59,6 +59,11 @@ class Modify
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->attributes[$this->idName];
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
if (isset($this->attributes[$this->idName])) {
|
||||
|
@ -48,6 +48,33 @@ class PlaceRepository
|
||||
];
|
||||
}
|
||||
|
||||
public function addToMap(int $mapId, array $place): int
|
||||
{
|
||||
$modify = new Modify(\Container::$dbConnection, 'places');
|
||||
$modify->set('map_id', $mapId);
|
||||
$modify->fill($place);
|
||||
$modify->save();
|
||||
|
||||
return $modify->getId();
|
||||
}
|
||||
|
||||
public function modify(int $id, array $place): void
|
||||
{
|
||||
$modify = new Modify(\Container::$dbConnection, 'places');
|
||||
$modify->setId($id);
|
||||
$modify->set('pano_id_cached', null);
|
||||
$modify->set('pano_id_cached_timestamp', null);
|
||||
$modify->fill($place);
|
||||
$modify->save();
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
$modify = new Modify(\Container::$dbConnection, 'places');
|
||||
$modify->setId($id);
|
||||
$modify->delete();
|
||||
}
|
||||
|
||||
private function selectFromDbById(int $placeId): array
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection, 'places');
|
||||
@ -66,7 +93,7 @@ class PlaceRepository
|
||||
$select->where('id', 'NOT IN', $exclude);
|
||||
$select->where('map_id', '=', $mapId);
|
||||
|
||||
$numberOfPlaces = $select->count();// TODO: what if 0
|
||||
$numberOfPlaces = $select->count(); // TODO: what if 0
|
||||
$randomOffset = random_int(0, $numberOfPlaces - 1);
|
||||
|
||||
$select->orderBy('id');
|
||||
|
@ -59,6 +59,26 @@ class Bounds
|
||||
}
|
||||
}
|
||||
|
||||
public function getSouthLat(): float
|
||||
{
|
||||
return $this->southLat;
|
||||
}
|
||||
|
||||
public function getWestLng(): float
|
||||
{
|
||||
return $this->westLng;
|
||||
}
|
||||
|
||||
public function getNorthLat(): float
|
||||
{
|
||||
return $this->northLat;
|
||||
}
|
||||
|
||||
public function getEastLng(): float
|
||||
{
|
||||
return $this->eastLng;
|
||||
}
|
||||
|
||||
public function calculateApproximateArea(): float
|
||||
{
|
||||
$dLat = $this->northLat - $this->southLat;
|
||||
|
Loading…
Reference in New Issue
Block a user