<?php
namespace App\Controller\Api;
use App\Entity\Customer;
use App\Entity\ExerciseCalendar;
use App\Entity\NoteCalendar;
use App\Services\Api\CalendarManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class CalendarController extends ApiController
{
/** @var CalendarManager $calendarManager */
protected $calendarManager;
/**
* @param CalendarManager $calendarManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, CalendarManager $calendarManager)
{
parent::__construct($em, $parameterBag);
$this->calendarManager = $calendarManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/calendar/get", name="api_calendar", methods={"POST"})
*/
public function list(Request $request)
{
$exercises = [];
$month = $request->get('month');
$year = $request->get('year');
/** @var Customer $customer */
$customer = $this->getUser();
$season = $customer->getSeasonActive();
if (!empty($season)) {
$from = date("Y-m-d", strtotime("${year}-${month}-01"));
$until = date("Y-m-t", strtotime($from));
$sql = "SELECT
'exercise' AS `type`,
ec.id,
ec.date
FROM exercise_calendar ec
WHERE
ec.season_id = :season_id1 AND
ec.date BETWEEN :from1 AND :until1
UNION
SELECT
'note' AS `type`,
nc.id,
nc.date
FROM note_calendar nc
WHERE
nc.season_id = :season_id2 AND
nc.date BETWEEN :from2 AND :until2
";
$statement = $this->em->getConnection()->prepare($sql);
// Set parameters
$statement->bindValue('season_id1', $season->getId());
$statement->bindValue('from1', $from);
$statement->bindValue('until1', $until);
$statement->bindValue('season_id2', $season->getId());
$statement->bindValue('from2', $from);
$statement->bindValue('until2', $until);
$statement->execute();
$results = $statement->fetchAllAssociative();
$output = [];
foreach ($results as $result) {
switch ($result['type']) {
case 'exercise':
$item = $this->em->getRepository(ExerciseCalendar::class)->find($result['id']);
$result['item'] = [
"id" => $item->getId(),
"date" => $item->getDate()->format("Y-m-d"),
"exercise_duration" => is_null($item->getExercise())? 0 : $item->getExercise()->getDuration()
];
$output[] = $result;
break;
case 'note':
$item = $this->em->getRepository(NoteCalendar::class)->find($result['id']);
$result['item'] = $item->__toArray();
$output[] = $result;
break;
default:
break;
}
}
}
return $this->response($exercises);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/calendar/note/{date}/get", name="api_calendar_note_get", methods={"POST"})
*/
public function calendarNoteGet($date, Request $request)
{
$notes = [];
/** @var Customer $customer */
$customer = $this->getUser();
$season = $customer->getSeasonActive();
if (!empty($season)) {
$filter = [
'season' => $season->getId(),
'date' => \DateTime::createFromFormat("Y-m-d", $date),
];
/** @var NoteCalendar $exerciseCalendar */
foreach ($this->em->getRepository(NoteCalendar::class)->findBy($filter) as $item) {
$notes[] = $item->__toArray();
}
}
return $this->response($notes);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/calendar/note/add", name="api_calendar_note_add", methods={"POST"})
*/
public function addNote(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataForNoteFromRequest($request);
$response = $this->calendarManager->addNote($data);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/calendar/note/{id}/edit", name="api_calendar_note_edit", methods={"POST"})
*/
public function editNote($id, Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataForNoteFromRequest($request);
$data['id'] = $id;
$response = $this->calendarManager->editNote($data);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/calendar/note/{id}/delete", name="api_calendar_note_delete", methods={"POST"})
*/
public function removeNote($id, Request $request)
{
$response = $this->calendarManager->removeNote($id);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
protected function getDataForNoteFromRequest(Request $request)
{
$data = [
"note" => $request->get('note'),
"type" => $request->get('type'),
"date" => $request->get('date'),
];
return $data;
}
}