diff --git a/src/Controller/MapAdminController.php b/src/Controller/MapAdminController.php index 9f019ce..fcf2782 100644 --- a/src/Controller/MapAdminController.php +++ b/src/Controller/MapAdminController.php @@ -1,5 +1,7 @@ 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(); diff --git a/src/Database/Query/Modify.php b/src/Database/Query/Modify.php index efa3a00..6c84496 100755 --- a/src/Database/Query/Modify.php +++ b/src/Database/Query/Modify.php @@ -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])) { diff --git a/src/Repository/PlaceRepository.php b/src/Repository/PlaceRepository.php index 2b14283..831d221 100644 --- a/src/Repository/PlaceRepository.php +++ b/src/Repository/PlaceRepository.php @@ -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'); diff --git a/src/Util/Geo/Bounds.php b/src/Util/Geo/Bounds.php index 1edfb8b..47d4a1f 100644 --- a/src/Util/Geo/Bounds.php +++ b/src/Util/Geo/Bounds.php @@ -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;