mapguesser/src/Controller/MapAdminController.php

265 lines
8.6 KiB
PHP
Raw Normal View History

<?php namespace MapGuesser\Controller;
use DateTime;
2023-04-07 20:28:14 +02:00
use SokoWeb\Interfaces\Authentication\IUser;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
2023-04-07 20:28:14 +02:00
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Response\IContent;
2021-05-19 16:33:18 +02:00
use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge;
2021-05-19 16:33:18 +02:00
use MapGuesser\Repository\ChallengeRepository;
use MapGuesser\Repository\GuessRepository;
2020-06-06 02:18:12 +02:00
use MapGuesser\Repository\MapRepository;
2021-05-19 16:33:18 +02:00
use MapGuesser\Repository\PlaceInChallengeRepository;
use MapGuesser\Repository\PlaceRepository;
2021-05-19 16:33:18 +02:00
use MapGuesser\Repository\UserInChallengeRepository;
use MapGuesser\Repository\UserPlayedPlaceRepository;
2023-04-07 20:28:14 +02:00
use SokoWeb\Response\HtmlContent;
use SokoWeb\Response\JsonContent;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Util\Panorama\Pov;
class MapAdminController implements IAuthenticationRequired, ISecured
{
private static string $unnamedMapName = '[unnamed map]';
2020-06-06 02:18:12 +02:00
private MapRepository $mapRepository;
private PlaceRepository $placeRepository;
private UserPlayedPlaceRepository $userPlayedPlaceRepository;
2021-05-19 16:33:18 +02:00
private ChallengeRepository $challengeRepository;
private GuessRepository $guessRepository;
private PlaceInChallengeRepository $placeInChallengeRepository;
private UserInChallengeRepository $userInChallengeRepository;
2023-04-20 00:19:54 +02:00
public function __construct()
{
2020-06-06 02:18:12 +02:00
$this->mapRepository = new MapRepository();
$this->placeRepository = new PlaceRepository();
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
2021-05-19 16:33:18 +02:00
$this->challengeRepository = new ChallengeRepository();
$this->guessRepository = new GuessRepository();
$this->placeInChallengeRepository = new PlaceInChallengeRepository();
$this->userInChallengeRepository = new UserInChallengeRepository();
}
public function isAuthenticationRequired(): bool
{
return true;
}
public function authorize(): bool
{
2023-04-20 00:19:54 +02:00
return \Container::$request->user()->hasPermission(IUser::PERMISSION_ADMIN);
}
public function getMapEditor(): IContent
{
2023-04-20 00:19:54 +02:00
$mapId = (int) \Container::$request->query('mapId');
if ($mapId) {
$map = $this->mapRepository->getById($mapId);
$places = $this->getPlaces($map);
} else {
$map = new Map();
$map->setName(self::$unnamedMapName);
$places = [];
}
return new HtmlContent('admin/map_editor', [
'mapId' => $mapId,
'mapName' => $map->getName(),
'mapDescription' => str_replace('<br>', "\n", $map->getDescription()),
2022-05-26 14:21:05 +02:00
'mapUnlisted' => $map->getUnlisted(),
'bounds' => $map->getBounds()->toArray(),
'places' => &$places
]);
}
public function getPlace(): IContent
{
2023-04-20 00:19:54 +02:00
$placeId = (int) \Container::$request->query('placeId');
$place = $this->placeRepository->getById($placeId);
return new JsonContent(['panoId' => $place->getFreshPanoId()]);
}
public function saveMap(): IContent
{
2023-04-20 00:19:54 +02:00
$mapId = (int) \Container::$request->query('mapId');
if ($mapId) {
$map = $this->mapRepository->getById($mapId);
} else {
$map = new Map();
$map->setName(self::$unnamedMapName);
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->saveToDb($map);
}
if (isset($_POST['added'])) {
$addedIds = [];
foreach ($_POST['added'] as $placeRaw) {
$placeRaw = json_decode($placeRaw, true);
$place = new Place();
$place->setMap($map);
$place->setLat((float) $placeRaw['lat']);
$place->setLng((float) $placeRaw['lng']);
$place->setPov(new Pov(
(float) $placeRaw['pov']['heading'],
(float) $placeRaw['pov']['pitch'],
(float) $placeRaw['pov']['zoom']
));
if ($placeRaw['panoId'] === -1) {
$place->setPanoIdCachedTimestampDate(new DateTime('-1 day'));
}
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->saveToDb($place);
$addedIds[] = ['tempId' => $placeRaw['id'], 'id' => $place->getId()];
}
} else {
$addedIds = [];
}
if (isset($_POST['edited'])) {
foreach ($_POST['edited'] as $placeRaw) {
$placeRaw = json_decode($placeRaw, true);
$place = $this->placeRepository->getById((int) $placeRaw['id']);
$place->setLat((float) $placeRaw['lat']);
$place->setLng((float) $placeRaw['lng']);
$place->setPov(new Pov(
(float) $placeRaw['pov']['heading'],
(float) $placeRaw['pov']['pitch'],
(float) $placeRaw['pov']['zoom']
));
$place->setPanoIdCachedTimestampDate(new DateTime('-1 day'));
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->saveToDb($place);
}
}
if (isset($_POST['deleted'])) {
foreach ($_POST['deleted'] as $placeRaw) {
$placeRaw = json_decode($placeRaw, true);
$place = $this->placeRepository->getById((int) $placeRaw['id']);
$this->deletePlace($place);
}
}
$mapBounds = $this->calculateMapBounds($map);
$map->setBounds($mapBounds);
$map->setArea($mapBounds->calculateApproximateArea());
2020-06-06 02:18:12 +02:00
if (isset($_POST['name'])) {
$map->setName($_POST['name'] ? $_POST['name'] : self::$unnamedMapName);
2020-06-06 02:18:12 +02:00
}
if (isset($_POST['description'])) {
$map->setDescription(str_replace(["\n", "\r\n"], '<br>', $_POST['description']));
2020-06-06 02:18:12 +02:00
}
2022-05-26 14:21:05 +02:00
if (isset($_POST['unlisted'])) {
$map->setUnlisted((bool)$_POST['unlisted']);
}
2020-06-06 02:18:12 +02:00
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->saveToDb($map);
return new JsonContent(['mapId' => $map->getId(), 'added' => $addedIds]);
}
2020-06-14 02:36:23 +02:00
2020-07-05 21:47:32 +02:00
public function deleteMap(): IContent
{
2023-04-20 00:19:54 +02:00
$mapId = (int) \Container::$request->query('mapId');
2020-06-14 02:36:23 +02:00
$map = $this->mapRepository->getById($mapId);
$this->deletePlaces($map);
2020-06-14 02:36:23 +02:00
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->deleteFromDb($map);
2020-06-14 02:36:23 +02:00
return new JsonContent(['success' => true]);
2020-06-14 02:36:23 +02:00
}
private function deletePlace(Place $place): void
{
foreach ($this->userPlayedPlaceRepository->getAllByPlace($place) as $userPlayedPlace) {
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->deleteFromDb($userPlayedPlace);
}
2021-05-19 16:33:18 +02:00
foreach ($this->challengeRepository->getAllByPlace($place) as $challenge) {
$this->deleteChallenge($challenge);
}
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->deleteFromDb($place);
}
private function deletePlaces(Map $map): void
2020-06-14 02:36:23 +02:00
{
foreach ($this->placeRepository->getAllForMap($map) as $place) {
$this->deletePlace($place);
2020-06-14 02:36:23 +02:00
}
}
2021-05-19 16:33:18 +02:00
private function deleteChallenge(Challenge $challenge): void
{
foreach ($this->userInChallengeRepository->getAllByChallenge($challenge) as $userInChallenge) {
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->deleteFromDb($userInChallenge);
2021-05-19 16:33:18 +02:00
}
foreach ($this->guessRepository->getAllInChallenge($challenge, ['place_in_challange']) as $guess) {
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->deleteFromDb($guess);
2021-05-19 16:33:18 +02:00
}
foreach ($this->placeInChallengeRepository->getAllByChallenge($challenge) as $placeInChallenge) {
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->deleteFromDb($placeInChallenge);
2021-05-19 16:33:18 +02:00
}
2023-04-20 00:19:54 +02:00
\Container::$persistentDataManager->deleteFromDb($challenge);
2021-05-19 16:33:18 +02:00
}
private function calculateMapBounds(Map $map): Bounds
{
$bounds = new Bounds();
foreach ($this->placeRepository->getAllForMap($map) as $place) {
$bounds->extend($place->getPosition());
}
return $bounds;
}
private function &getPlaces(Map $map): array
{
$places = [];
foreach ($this->placeRepository->getAllForMap($map) as $place) {
$noPano = $place->getPanoIdCachedTimestampDate() !== null && $place->getPanoIdCached() === null;
$placeId = $place->getId();
$places[$placeId] = [
'id' => $placeId,
'lat' => $place->getLat(),
'lng' => $place->getLng(),
'panoId' => null,
'pov' => $place->getPov()->toArray(),
'noPano' => $noPano
];
}
return $places;
}
}