2020-06-01 21:13:02 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2020-06-05 22:22:02 +02:00
|
|
|
use DateTime;
|
2020-06-01 21:13:02 +02:00
|
|
|
use MapGuesser\Database\Query\Select;
|
2020-06-09 00:56:00 +02:00
|
|
|
use MapGuesser\Interfaces\Authentication\IUser;
|
2020-06-07 23:37:01 +02:00
|
|
|
use MapGuesser\Interfaces\Authorization\ISecured;
|
2020-06-01 21:13:02 +02:00
|
|
|
use MapGuesser\Interfaces\Database\IResultSet;
|
2020-06-09 00:56:00 +02:00
|
|
|
use MapGuesser\Interfaces\Request\IRequest;
|
2020-06-01 21:13:02 +02:00
|
|
|
use MapGuesser\Interfaces\Response\IContent;
|
2020-06-19 18:52:07 +02:00
|
|
|
use MapGuesser\PersistentData\Model\Map;
|
2020-06-20 00:03:49 +02:00
|
|
|
use MapGuesser\PersistentData\Model\Place;
|
2020-06-19 18:52:07 +02:00
|
|
|
use MapGuesser\PersistentData\PersistentDataManager;
|
2020-06-06 02:18:12 +02:00
|
|
|
use MapGuesser\Repository\MapRepository;
|
2020-06-02 01:16:59 +02:00
|
|
|
use MapGuesser\Repository\PlaceRepository;
|
2020-06-01 21:13:02 +02:00
|
|
|
use MapGuesser\Response\HtmlContent;
|
2020-06-02 21:44:01 +02:00
|
|
|
use MapGuesser\Response\JsonContent;
|
2020-06-01 21:13:02 +02:00
|
|
|
use MapGuesser\Util\Geo\Bounds;
|
2020-06-05 22:22:02 +02:00
|
|
|
use MapGuesser\Util\Geo\Position;
|
2020-06-01 21:13:02 +02:00
|
|
|
|
2020-06-07 23:37:01 +02:00
|
|
|
class MapAdminController implements ISecured
|
2020-06-01 21:13:02 +02:00
|
|
|
{
|
2020-06-10 23:14:19 +02:00
|
|
|
private static string $unnamedMapName = '[unnamed map]';
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
private IRequest $request;
|
|
|
|
|
2020-06-19 18:52:07 +02:00
|
|
|
private PersistentDataManager $pdm;
|
|
|
|
|
2020-06-06 02:18:12 +02:00
|
|
|
private MapRepository $mapRepository;
|
|
|
|
|
2020-06-02 01:16:59 +02:00
|
|
|
private PlaceRepository $placeRepository;
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
public function __construct(IRequest $request)
|
2020-06-02 01:16:59 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$this->request = $request;
|
2020-06-19 18:52:07 +02:00
|
|
|
$this->pdm = new PersistentDataManager();
|
2020-06-06 02:18:12 +02:00
|
|
|
$this->mapRepository = new MapRepository();
|
2020-06-02 01:16:59 +02:00
|
|
|
$this->placeRepository = new PlaceRepository();
|
|
|
|
}
|
|
|
|
|
2020-06-07 23:37:01 +02:00
|
|
|
public function authorize(): bool
|
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$user = $this->request->user();
|
2020-06-07 23:37:01 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
return $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN);
|
2020-06-07 23:37:01 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
public function getMapEditor(): IContent
|
2020-06-02 01:16:59 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$mapId = (int) $this->request->query('mapId');
|
2020-06-01 21:13:02 +02:00
|
|
|
|
2020-06-10 23:14:19 +02:00
|
|
|
if ($mapId) {
|
|
|
|
$map = $this->mapRepository->getById($mapId);
|
2020-06-20 00:03:49 +02:00
|
|
|
$places = $this->getPlaces($map);
|
2020-06-10 23:14:19 +02:00
|
|
|
} else {
|
2020-06-19 18:52:07 +02:00
|
|
|
$map = new Map();
|
|
|
|
$map->setName(self::$unnamedMapName);
|
2020-06-10 23:14:19 +02:00
|
|
|
$places = [];
|
|
|
|
}
|
2020-06-01 21:13:02 +02:00
|
|
|
|
2020-06-19 18:52:07 +02:00
|
|
|
$data = ['mapId' => $mapId, 'mapName' => $map->getName(), 'mapDescription' => str_replace('<br>', "\n", $map->getDescription()), 'bounds' => $map->getBounds()->toArray(), 'places' => &$places];
|
2020-06-01 21:13:02 +02:00
|
|
|
return new HtmlContent('admin/map_editor', $data);
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:38:30 +02:00
|
|
|
public function getPlace(): IContent
|
2020-06-02 01:16:59 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$placeId = (int) $this->request->query('placeId');
|
2020-06-02 21:44:01 +02:00
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$place = $this->placeRepository->getById($placeId);
|
2020-06-02 21:44:01 +02:00
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$data = ['panoId' => $place->getFreshPanoId()];
|
2020-06-02 21:44:01 +02:00
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:38:30 +02:00
|
|
|
public function saveMap(): IContent
|
|
|
|
{
|
|
|
|
$mapId = (int) $this->request->query('mapId');
|
|
|
|
|
2020-06-14 02:36:23 +02:00
|
|
|
\Container::$dbConnection->startTransaction();
|
|
|
|
|
2020-06-19 18:52:07 +02:00
|
|
|
if ($mapId) {
|
|
|
|
$map = $this->mapRepository->getById($mapId);
|
|
|
|
} else {
|
|
|
|
$map = new Map();
|
|
|
|
$map->setName(self::$unnamedMapName);
|
|
|
|
$this->pdm->saveToDb($map);
|
2020-06-10 23:14:19 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 22:22:02 +02:00
|
|
|
if (isset($_POST['added'])) {
|
|
|
|
$addedIds = [];
|
|
|
|
foreach ($_POST['added'] as $placeRaw) {
|
|
|
|
$placeRaw = json_decode($placeRaw, true);
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$place = new Place();
|
|
|
|
$place->setMap($map);
|
|
|
|
$place->setLat((float) $placeRaw['lat']);
|
|
|
|
$place->setLng((float) $placeRaw['lng']);
|
|
|
|
|
|
|
|
if ($placeRaw['panoId'] === -1) {
|
|
|
|
$place->setPanoIdCachedTimestampDate(new DateTime('-1 day'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pdm->saveToDb($place);
|
|
|
|
|
|
|
|
$addedIds[] = ['tempId' => $placeRaw['id'], 'id' => $place->getId()];
|
2020-06-05 22:22:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$addedIds = [];
|
|
|
|
}
|
2020-06-05 19:38:30 +02:00
|
|
|
|
2020-06-05 22:22:02 +02:00
|
|
|
if (isset($_POST['edited'])) {
|
|
|
|
foreach ($_POST['edited'] as $placeRaw) {
|
|
|
|
$placeRaw = json_decode($placeRaw, true);
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$place = $this->placeRepository->getById((int) $placeRaw['id']);
|
|
|
|
$place->setLat((float) $placeRaw['lat']);
|
|
|
|
$place->setLng((float) $placeRaw['lng']);
|
|
|
|
$place->setPanoIdCachedTimestampDate(new DateTime('-1 day'));
|
|
|
|
|
|
|
|
$this->pdm->saveToDb($place);
|
2020-06-05 22:22:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_POST['deleted'])) {
|
|
|
|
foreach ($_POST['deleted'] as $placeRaw) {
|
|
|
|
$placeRaw = json_decode($placeRaw, true);
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$place = $this->placeRepository->getById((int) $placeRaw['id']);
|
|
|
|
|
|
|
|
$this->pdm->deleteFromDb($place);
|
2020-06-05 22:22:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$mapBounds = $this->calculateMapBounds($map);
|
2020-06-05 22:22:02 +02:00
|
|
|
|
2020-06-19 18:52:07 +02:00
|
|
|
$map->setBounds($mapBounds);
|
|
|
|
$map->setArea($mapBounds->calculateApproximateArea());
|
2020-06-05 22:22:02 +02:00
|
|
|
|
2020-06-06 02:18:12 +02:00
|
|
|
if (isset($_POST['name'])) {
|
2020-06-19 18:52:07 +02:00
|
|
|
$map->setName($_POST['name'] ? $_POST['name'] : self::$unnamedMapName);
|
2020-06-06 02:18:12 +02:00
|
|
|
}
|
|
|
|
if (isset($_POST['description'])) {
|
2020-06-19 18:52:07 +02:00
|
|
|
$map->setDescription(str_replace(["\n", "\r\n"], '<br>', $_POST['description']));
|
2020-06-06 02:18:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:52:07 +02:00
|
|
|
$this->pdm->saveToDb($map);
|
2020-06-05 22:22:02 +02:00
|
|
|
|
2020-06-14 02:36:23 +02:00
|
|
|
\Container::$dbConnection->commit();
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$data = ['mapId' => $map->getId(), 'added' => $addedIds];
|
2020-06-05 19:38:30 +02:00
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
2020-06-14 02:36:23 +02:00
|
|
|
|
|
|
|
public function deleteMap() {
|
|
|
|
$mapId = (int) $this->request->query('mapId');
|
|
|
|
|
2020-06-19 18:52:07 +02:00
|
|
|
$map = $this->mapRepository->getById($mapId);
|
|
|
|
|
2020-06-14 02:36:23 +02:00
|
|
|
\Container::$dbConnection->startTransaction();
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$this->deletePlaces($map);
|
2020-06-14 02:36:23 +02:00
|
|
|
|
2020-06-19 18:52:07 +02:00
|
|
|
$this->pdm->deleteFromDb($map);
|
2020-06-14 02:36:23 +02:00
|
|
|
|
|
|
|
\Container::$dbConnection->commit();
|
|
|
|
|
|
|
|
$data = ['success' => true];
|
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
private function deletePlaces(Map $map): void
|
2020-06-14 02:36:23 +02:00
|
|
|
{
|
2020-06-20 00:03:49 +02:00
|
|
|
//TODO: relations?
|
2020-06-14 02:36:23 +02:00
|
|
|
$select = new Select(\Container::$dbConnection, 'places');
|
2020-06-20 00:03:49 +02:00
|
|
|
$select->columns(Place::getFields());
|
|
|
|
$select->where('map_id', '=', $map->getId());
|
2020-06-14 02:36:23 +02:00
|
|
|
|
|
|
|
$result = $select->execute();
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
while ($placeData = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
|
|
|
$place = new Place();
|
|
|
|
|
|
|
|
$this->pdm->fillWithData($placeData, $place);
|
|
|
|
|
|
|
|
$this->pdm->deleteFromDb($place);
|
2020-06-14 02:36:23 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 19:38:30 +02:00
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
private function calculateMapBounds(Map $map): Bounds
|
2020-06-05 22:22:02 +02:00
|
|
|
{
|
2020-06-20 00:03:49 +02:00
|
|
|
//TODO: from repository or relations
|
2020-06-05 22:22:02 +02:00
|
|
|
$select = new Select(\Container::$dbConnection, 'places');
|
|
|
|
$select->columns(['lat', 'lng']);
|
2020-06-20 00:03:49 +02:00
|
|
|
$select->where('map_id', '=', $map->getId());
|
2020-06-05 22:22:02 +02:00
|
|
|
|
|
|
|
$result = $select->execute();
|
|
|
|
|
|
|
|
$bounds = new Bounds();
|
|
|
|
while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
|
|
|
$bounds->extend(new Position($place['lat'], $place['lng']));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bounds;
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
private function &getPlaces(Map $map): array
|
2020-06-01 21:13:02 +02:00
|
|
|
{
|
2020-06-20 00:03:49 +02:00
|
|
|
//TODO: from repository or relations
|
2020-06-01 21:13:02 +02:00
|
|
|
$select = new Select(\Container::$dbConnection, 'places');
|
2020-06-02 01:16:59 +02:00
|
|
|
$select->columns(['id', 'lat', 'lng', 'pano_id_cached', 'pano_id_cached_timestamp']);
|
2020-06-20 00:03:49 +02:00
|
|
|
$select->where('map_id', '=', $map->getId());
|
2020-06-01 21:13:02 +02:00
|
|
|
|
2020-06-02 01:16:59 +02:00
|
|
|
$result = $select->execute();
|
|
|
|
|
|
|
|
$places = [];
|
|
|
|
|
|
|
|
while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
2020-06-05 00:21:36 +02:00
|
|
|
//$panoId = ???
|
|
|
|
//$pov = ???
|
2020-06-02 01:16:59 +02:00
|
|
|
$noPano = $place['pano_id_cached_timestamp'] && $place['pano_id_cached'] === null;
|
|
|
|
|
2020-06-05 00:21:36 +02:00
|
|
|
$places[$place['id']] = [
|
|
|
|
'id' => $place['id'],
|
|
|
|
'lat' => $place['lat'],
|
|
|
|
'lng' => $place['lng'],
|
|
|
|
'panoId' => null,
|
|
|
|
'pov' => ['heading' => 0.0, 'pitch' => 0.0, 'zoom' => 0.0],
|
|
|
|
'noPano' => $noPano
|
|
|
|
];
|
2020-06-02 01:16:59 +02:00
|
|
|
}
|
2020-06-01 21:13:02 +02:00
|
|
|
|
|
|
|
return $places;
|
|
|
|
}
|
|
|
|
}
|