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-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-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-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);
|
|
|
|
$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 = [];
|
|
|
|
}
|
2020-06-01 21:13:02 +02:00
|
|
|
|
2020-06-10 00:33:11 +02:00
|
|
|
$data = ['mapId' => $mapId, 'mapName' => $map['name'], 'mapDescription' => str_replace('<br>', "\n", $map['description']), 'bounds' => $bounds->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-10 23:14:19 +02:00
|
|
|
if (!$mapId) {
|
|
|
|
$mapId = $this->addNewMap();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
$map = [
|
|
|
|
'bound_south_lat' => $mapBounds->getSouthLat(),
|
|
|
|
'bound_west_lng' => $mapBounds->getWestLng(),
|
|
|
|
'bound_north_lat' => $mapBounds->getNorthLat(),
|
|
|
|
'bound_east_lng' => $mapBounds->getEastLng()
|
|
|
|
];
|
|
|
|
|
2020-06-06 02:18:12 +02:00
|
|
|
if (isset($_POST['name'])) {
|
2020-06-10 23:14:19 +02:00
|
|
|
$map['name'] = $_POST['name'] ? $_POST['name'] : self::$unnamedMapName;
|
2020-06-06 02:18:12 +02:00
|
|
|
}
|
|
|
|
if (isset($_POST['description'])) {
|
2020-06-10 00:33:11 +02:00
|
|
|
$map['description'] = str_replace(["\n", "\r\n"], '<br>', $_POST['description']);
|
2020-06-06 02:18:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 22:22:02 +02:00
|
|
|
$this->saveMapData($mapId, $map);
|
|
|
|
|
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-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-10 23:14:19 +02:00
|
|
|
private function addNewMap(): int
|
|
|
|
{
|
|
|
|
$modify = new Modify(\Container::$dbConnection, 'maps');
|
|
|
|
$modify->fill([
|
|
|
|
'name' => self::$unnamedMapName,
|
|
|
|
'description' => ''
|
|
|
|
]);
|
|
|
|
$modify->save();
|
|
|
|
|
|
|
|
return $modify->getId();
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:22:02 +02:00
|
|
|
private function saveMapData(int $mapId, array $map): void
|
|
|
|
{
|
|
|
|
$modify = new Modify(\Container::$dbConnection, 'maps');
|
|
|
|
$modify->setId($mapId);
|
|
|
|
$modify->fill($map);
|
|
|
|
$modify->save();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|