<?php
declare(strict_types=1);
namespace App\Controller\V1;
use App\Entity\Local\Response\SeatPlanResponse;
use App\Entity\Local\SeatPlanJson;
use App\Entity\Vista\SeatPlan;
use App\Manager\SeatPlanManager;
use App\Repository\OrderRepositoryInterface;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\Route;
use FOS\RestBundle\Controller\Annotations\View;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\SeatPlanRepository;
use App\Repository\SeatPlanLocalRepository;
use App\Repository\CinemaRepository;
use App\Repository\LocationRepository;
use App\Repository\SessionRepositoryInterface as SessionLocalRepository;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @Route("/seat-plan")
* @SWG\Tag(name="Order_v1")
*/
class SeatPlanController
{
/**
* @var SeatPlanManager
*/
protected $seatPlanManager;
/**
* @var OrderRepositoryInterface
*/
protected $orderRepository;
/** @var SeatPlanRepository */
protected $seatPlanRepository;
/** @var SeatPlanLocalRepository */
protected $seatPlanLocalRepository;
/** @var CinemaRepository */
protected $cinemaRepository;
/** @var LocationRepository */
protected $locationRepository;
/** @var SessionLocalRepository */
private $sessionLocalRepository;
/** @var SerializerInterface $serializer */
protected $serializer;
/**
* SeatPlanController constructor.
*
* @param SeatPlanManager $seatPlanManager
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(SeatPlanManager $seatPlanManager, OrderRepositoryInterface $orderRepository, SeatPlanRepository $seatPlanRepository,
SeatPlanLocalRepository $seatPlanLocalRepository, CinemaRepository $cinemaRepository, LocationRepository $locationRepository, SessionLocalRepository $sessionLocalRepository,
SerializerInterface $serializer) {
$this->seatPlanManager = $seatPlanManager;
$this->orderRepository = $orderRepository;
$this->seatPlanRepository = $seatPlanRepository;
$this->seatPlanLocalRepository = $seatPlanLocalRepository;
$this->cinemaRepository = $cinemaRepository;
$this->locationRepository = $locationRepository;
$this->sessionLocalRepository = $sessionLocalRepository;
$this->serializer = $serializer;
}
/**
* @Route("/{cinemaId}/{sessionId}", methods="GET")
* @Rest\QueryParam(name="orderId", allowBlank=true)
* @View()
*
* @SWG\Response(
* response=200,
* description="A seatPlan",
* @Model(type=\App\Entity\Local\Response\SeatPlanResponse::class))
*
* @param string $cinemaId
* @param string $sessionId
* @param string|null $orderId
* @return object
*/
public function getAction(string $cinemaId, string $sessionId, ?string $orderId = null) {
// http://10.15.20.177:8080/api/v1/seat-plan/1007/49019
// http://10.15.20.177:8080/api/v1/seat-plan/raw/processed/1007/1
$order = $orderId ? $this->orderRepository->findBySessionId($orderId) : null;
$selection = $order ? ($order->getSelection() ?: []) : [];
$result = $this->seatPlanManager->getSeatPlan($cinemaId, $sessionId, null);
// if($result->getRows() && $result->getRows()[0]->getSeats())error_log(sprintf("%s %s(%s) cinemaId $cinemaId sessionId $sessionId ", date('Y-m-d H:i:s'), __METHOD__, __LINE__) . var_export($result->getRows()[0]->getSeats()[0]->getPosition(), true));
$this->seatPlanManager->getNormalized($result->getRows(), null, $selection);
//$this->seatPlanManager->setColumnRender($result->getRows());
return $result;
}
/**
* @Route("/raw/{cinemaId}/{screenNumber}", methods="GET")
* @View()
*
* @SWG\Response(response=200,
* description="Success")
*
* @param string $cinemaId
* @param string $screenNumber
* @return Response
*/
public function getRaw(string $cinemaId, string $screenNumber) {
$session = $this->sessionLocalRepository->findByScreenNumber($cinemaId, $screenNumber);
$seatPlanOutput = '{}';
if ($session != null) {
$sessionId = $session['sessionId'];
$seatPlanInput = $this->seatPlanRepository->getSeatPlanRaw($cinemaId, $sessionId);
//error_log(sprintf("%s %s(%s) cinemaId $cinemaId screenNumber $screenNumber sessionId $sessionId ", date('Y-m-d H:i:s'), __METHOD__, __LINE__));
/** @var string $seatPlanExists */
$seatPlanExists = $this->seatPlanLocalRepository->findBy(['cinemaId' => $cinemaId, 'screenNumber' => $screenNumber]) != null ? "1" : "";
$seatPlanOutput = $this->seatPlanManager->processRaw($seatPlanInput, $cinemaId, $screenNumber, $seatPlanExists, $sessionId);
}
$response = new Response($seatPlanOutput);
$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
return $response;
}
/**
* @Route("/raw/processed/{cinemaId}/{screenNumber}", methods="GET")
* @View()
*
* @SWG\Response(response=200,
* description="Success")
*
* @param string $cinemaId
* @param string $screenNumber
* @return object
*/
public function getRawProcessed(string $cinemaId, string $screenNumber) {
$seatPlanJson = $this->seatPlanLocalRepository->findOneBy(['cinemaId' => $cinemaId, 'screenNumber' => $screenNumber]);
$seatPlanInputObj = null;
$seatPlanOutput = null;
if ($seatPlanExists = $seatPlanJson ? "1" : "") {
$seatPlanInput = $seatPlanJson->getSeatPlanJson();
$seatPlanInputObj = json_decode($seatPlanInput);
$seatPlanInputObj->seatPlanSaved = '1';
//error_log(sprintf("%s %s(%s) ", date('Y-m-d H:i:s'), __METHOD__, __LINE__) . var_export($seatPlanInputObj, true));
foreach($seatPlanInputObj->rows as &$row) {
if (isset($row->seats)) {
foreach($row->seats as &$seat) {
$seat->position->columnIndex = $seat->position->columnIndexRender;
}
}
}
$seatPlanOutput = json_encode($seatPlanInputObj);
//$response = new Response($seatPlanOutput);
//$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
//return $response;
}
if (($session = $this->sessionLocalRepository->findByScreenNumber($cinemaId, $screenNumber)) != null || $seatPlanOutput) {
$sessionId = $session ? $session['sessionId'] : null;
$seatPlanInputObj = $this->seatPlanManager->getSeatPlan($cinemaId, $sessionId, $seatPlanOutput);
$this->seatPlanManager->getNormalized($seatPlanInputObj->getRows(), null, []);
} else {
$seatPlanInputObj = new SeatPlanResponse();
}
$seatPlanInputObj->setSeatPlanSaved($seatPlanExists);
return $seatPlanInputObj;
}
/**
* @Route("/edited/{cinemaId}/{screenNumber}", methods="GET")
* @View()
*
* @SWG\Response(
* response=200,
* description="A seatPlan",
* @Model(type=\App\Entity\Local\SeatPlanJson::class))
*
* @param string $cinemaId
* @param string $screenNumber
* @return Response
*/
public function getEdited(string $cinemaId, string $screenNumber) {
$seatPlanJson = $this->seatPlanLocalRepository->findBy(['cinemaId' => $cinemaId, 'screenNumber' => $screenNumber]);
$seatPlanJson = $seatPlanJson ? $seatPlanJson[0]->getSeatPlanJson() : '{}';
$response = new Response($seatPlanJson);
$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
return $response;
}
/**
* @Route("/save/processed/{cinemaId}/{screenNumber}", methods="POST")
* @View()
*
* @SWG\Response(response=200,
* description="Success")
*
*
* @param string $cinemaId
* @param int $screenNumber
* @param Request $request
* @return Response
*/
public function saveSeatPlan(string $cinemaId, int $screenNumber, Request $request) {
$seatPlanJsons = $this->seatPlanLocalRepository->findBy(['cinemaId' => $cinemaId, 'screenNumber' => $screenNumber]);
if ($seatPlanJsons == null) {
$seatPlanJson = new SeatPlanJson();
$seatPlanJson->setCinemaId($cinemaId);
$seatPlanJson->setScreenNumber($screenNumber);
$seatPlanJson->setCreatedAt(new \DateTimeImmutable());
$seatPlanJson->setModifiedAt(new \DateTimeImmutable());
} else {
$seatPlanJson = $seatPlanJsons[0];
$seatPlanJson->setModifiedAt(new \DateTimeImmutable());
}
$seatPlanInput = $request->request->get('seatPlan');
$responseJsonObj = json_decode($seatPlanInput);
foreach($responseJsonObj->rows as &$row) {
if (isset($row->seats)) {
foreach($row->seats as &$seat) {
$seat->position->columnIndexRender = $seat->position->columnIndex;
}
}
}
$seatPlanOutput = json_encode($responseJsonObj);
$seatPlanJson->setSeatPlanJson($seatPlanOutput);
$this->seatPlanLocalRepository->save($seatPlanJson);
return new Response();
}
/**
* @Route("/delete/{cinemaId}/{screenNumber}", methods="DELETE")
* @View()
*
* @SWG\Response(response=200,
* description="Success")
*
* @param string $cinemaId
* @param int $screenNumber
* @return Response
*/
public function deleteSeatPlan(string $cinemaId, int $screenNumber) {
if ($seatPlanJsons = $this->seatPlanLocalRepository->findBy(['cinemaId' => $cinemaId, 'screenNumber' => $screenNumber])) {
$this->seatPlanLocalRepository->delete($seatPlanJsons[0]);
}
return new Response();
}
/**
* @Route("/cinemas", methods="GET")
* @View()
*
* @SWG\Response(response=200,
* description="Success")
*
* @return Response
*/
public function getSavedCinemas() {
$seatPlanCinemaIds = [];
$seatPlanCinemas = $this->seatPlanLocalRepository->getCinemasSaved();
foreach ($seatPlanCinemas as $seatPlanCinema) {
$seatPlanCinemaIds[$seatPlanCinema['cinemaId']] = null;
}
$json = '';
if ($seatPlanCinemaIds) {
$filter = ['id' => $this->locationRepository->findLocationIds()];
$cinemas = $this->cinemaRepository->findBy($filter, ['name' => 'ASC']);
foreach ($cinemas as $cinema) {
if (array_key_exists($cinema->getId(), $seatPlanCinemaIds)) {
if ($json) { $json .= ','; }
$json .= "{\"id\":\"{$cinema->getId()}\",\"name\":\"{$cinema->getName()}\"}";
}
}
}
//error_log(sprintf("%s %s(%s) ", date('Y-m-d H:i:s'), __METHOD__, __LINE__) . ' seatPlanCinemaIds ' . var_export($seatPlanCinemaIds, true) . ' cinemas ' . var_export($cinemas, true));
$response = new Response("[$json]");
$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
return $response;
}
/**
* @Route("/cinemas/{cinemaId}/halls", methods="GET")
* @View()
*
* @SWG\Response(response=200,
* description="Success")
*
* @param string $cinemaId
* @return Response
*/
public function getSavedCinemaHalls(string $cinemaId) {
$seatPlanCinemaHalls = $this->seatPlanLocalRepository->findBy(['cinemaId' => $cinemaId], ['screenNumber' => 'ASC']);
$json = '';
if ($seatPlanCinemaHalls) {
$screensAll = $this->sessionLocalRepository->findScreens($cinemaId);
$screens = [];
if ($screensAll) {
foreach ($screensAll as $screen) {
$screens[intval($screen['screenNumber'])] = $screen['screenName'];
}
}
//error_log(sprintf("%s %s(%s) ", date('Y-m-d H:i:s'), __METHOD__, __LINE__) . ' screens ' . var_export($screens, true) . ' screensAll ' . var_export($screensAll, true));
foreach ($seatPlanCinemaHalls as $seatPlanCinemaHall) {
if ($json) { $json .= ','; }
$screenName = array_key_exists(intval($seatPlanCinemaHall->getScreenNumber()), $screens) ? $screens[intval($seatPlanCinemaHall->getScreenNumber())]: null;
$json .= "{\"cinemaId\":\"{$seatPlanCinemaHall->getCinemaId()}\",\"screenNumber\":\"{$seatPlanCinemaHall->getScreenNumber()}\",\"screenName\":\"{$screenName}\"}";
}
}
$response = new Response("[$json]");
$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
return $response;
}
}