2020-06-01 21:13:02 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2020-06-05 22:22:02 +02:00
|
|
|
use DateTime;
|
|
|
|
use MapGuesser\Database\Query\Modify;
|
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;
|
|
|
|
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);
|
|
|
|
$places = $this->getPlaces($mapId);
|
|
|
|
} 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-02 01:16:59 +02:00
|
|
|
$placeData = $this->placeRepository->getById($placeId);
|
2020-06-02 21:44:01 +02:00
|
|
|
|
2020-06-02 01:16:59 +02:00
|
|
|
$data = ['panoId' => $placeData['panoId']];
|
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);
|
|
|
|
$mapId = $map->getId();
|
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-10 23:58:09 +02:00
|
|
|
$addedIds[] = ['tempId' => $placeRaw['id'], 'id' => $this->placeRepository->addToMap($mapId, [
|
2020-06-05 22:22:02 +02:00
|
|
|
'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 = [];
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
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-10 23:14:19 +02:00
|
|
|
$data = ['mapId' => $mapId, '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();
|
|
|
|
|
|
|
|
$this->deletePlaces($mapId);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function deletePlaces(int $mapId): void
|
|
|
|
{
|
|
|
|
$select = new Select(\Container::$dbConnection, 'places');
|
|
|
|
$select->columns(['id']);
|
|
|
|
$select->where('map_id', '=', $mapId);
|
|
|
|
|
|
|
|
$result = $select->execute();
|
|
|
|
|
|
|
|
while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
|
|
|
$modify = new Modify(\Container::$dbConnection, 'places');
|
|
|
|
$modify->setId($place['id']);
|
|
|
|
$modify->delete();
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 19:38:30 +02:00
|
|
|
|
2020-06-05 22:22:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-01 21:13:02 +02:00
|
|
|
private function &getPlaces(int $mapId): array
|
|
|
|
{
|
|
|
|
$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-01 21:13:02 +02:00
|
|
|
$select->where('map_id', '=', $mapId);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|