src/Controller/Api/CalendarController.php line 113

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Customer;
  4. use App\Entity\ExerciseCalendar;
  5. use App\Entity\NoteCalendar;
  6. use App\Services\Api\CalendarManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class CalendarController extends ApiController
  15. {
  16.     /** @var CalendarManager $calendarManager */
  17.     protected $calendarManager;
  18.     /**
  19.      * @param CalendarManager $calendarManager
  20.      */
  21.     public function __construct(EntityManagerInterface $emParameterBagInterface $parameterBagCalendarManager $calendarManager)
  22.     {
  23.         parent::__construct($em$parameterBag);
  24.         $this->calendarManager $calendarManager;
  25.     }
  26.     /**
  27.      * @param Request $request
  28.      * @return JsonResponse
  29.      *
  30.      * @Route("/calendar/get", name="api_calendar", methods={"POST"})
  31.      */
  32.     public function list(Request $request)
  33.     {
  34.         $exercises = [];
  35.         $month $request->get('month');
  36.         $year $request->get('year');
  37.         /** @var Customer $customer */
  38.         $customer $this->getUser();
  39.         $season $customer->getSeasonActive();
  40.         if (!empty($season)) {
  41.             $from date("Y-m-d"strtotime("${year}-${month}-01"));
  42.             $until date("Y-m-t"strtotime($from));
  43.             $sql "SELECT 
  44.                 'exercise' AS `type`,
  45.                     ec.id,
  46.                     ec.date
  47.                 FROM exercise_calendar ec
  48.                 WHERE
  49.                     ec.season_id = :season_id1 AND
  50.                     ec.date BETWEEN :from1 AND :until1
  51.                 UNION
  52.                 SELECT 
  53.                     'note' AS `type`,
  54.                     nc.id,
  55.                     nc.date
  56.                 FROM note_calendar nc
  57.                 WHERE
  58.                     nc.season_id = :season_id2 AND
  59.                     nc.date BETWEEN :from2 AND :until2
  60.             ";
  61.             $statement $this->em->getConnection()->prepare($sql);
  62.             // Set parameters 
  63.             $statement->bindValue('season_id1'$season->getId());
  64.             $statement->bindValue('from1'$from);
  65.             $statement->bindValue('until1'$until);
  66.             $statement->bindValue('season_id2'$season->getId());
  67.             $statement->bindValue('from2'$from);
  68.             $statement->bindValue('until2'$until);
  69.             $statement->execute();
  70.     
  71.             $results $statement->fetchAllAssociative();
  72.             $output = [];
  73.             foreach ($results as $result) {
  74.                 switch ($result['type']) {
  75.                     case 'exercise':
  76.                         $item $this->em->getRepository(ExerciseCalendar::class)->find($result['id']);
  77.                         $result['item'] = [
  78.                             "id" => $item->getId(),
  79.                             "date" => $item->getDate()->format("Y-m-d"),
  80.                             "exercise_duration" => is_null($item->getExercise())? $item->getExercise()->getDuration()
  81.                         ];
  82.                         $output[] = $result;
  83.                         break;
  84.                     case 'note':
  85.                         $item $this->em->getRepository(NoteCalendar::class)->find($result['id']);
  86.                         $result['item'] = $item->__toArray();
  87.                         $output[] = $result;
  88.                         break;
  89.                     default:
  90.                         break;
  91.                 }
  92.             }
  93.         }
  94.         return $this->response($exercises);
  95.     }
  96.     /**
  97.      * @param Request $request
  98.      * @return JsonResponse
  99.      *
  100.      * @Route("/calendar/note/{date}/get", name="api_calendar_note_get", methods={"POST"})
  101.      */
  102.     public function calendarNoteGet($dateRequest $request)
  103.     {
  104.         $notes = [];
  105.         /** @var Customer $customer */
  106.         $customer $this->getUser();
  107.         $season $customer->getSeasonActive();
  108.         if (!empty($season)) {
  109.             $filter = [
  110.                 'season' => $season->getId(),
  111.                 'date' => \DateTime::createFromFormat("Y-m-d"$date),
  112.             ];
  113.             /** @var NoteCalendar $exerciseCalendar */
  114.             foreach ($this->em->getRepository(NoteCalendar::class)->findBy($filter) as $item) {
  115.                 $notes[] = $item->__toArray();
  116.             }
  117.         }
  118.         return $this->response($notes);
  119.     }
  120.     /**
  121.      * @param Request $request
  122.      * @return JsonResponse
  123.      *
  124.      * @Route("/calendar/note/add", name="api_calendar_note_add", methods={"POST"})
  125.      */
  126.     public function addNote(Request $request)
  127.     {
  128.         $request $this->transformJsonBody($request);
  129.         $data $this->getDataForNoteFromRequest($request);
  130.         $response $this->calendarManager->addNote($data);
  131.         $this->setStatusCode($response["code"]);
  132.         return $this->response($response);
  133.     }
  134.     /**
  135.      * @param Request $request
  136.      * @return JsonResponse
  137.      *
  138.      * @Route("/calendar/note/{id}/edit", name="api_calendar_note_edit", methods={"POST"})
  139.      */
  140.     public function editNote($idRequest $request)
  141.     {
  142.         $request $this->transformJsonBody($request);
  143.         $data $this->getDataForNoteFromRequest($request);
  144.         $data['id'] = $id;
  145.         $response $this->calendarManager->editNote($data);
  146.         $this->setStatusCode($response["code"]);
  147.         return $this->response($response);
  148.     }
  149.     /**
  150.      * @param Request $request
  151.      * @return JsonResponse
  152.      *
  153.      * @Route("/calendar/note/{id}/delete", name="api_calendar_note_delete", methods={"POST"})
  154.      */
  155.     public function removeNote($idRequest $request)
  156.     {
  157.         $response $this->calendarManager->removeNote($id);
  158.         $this->setStatusCode($response["code"]);
  159.         return $this->response($response);
  160.     }
  161.     protected function getDataForNoteFromRequest(Request $request)
  162.     {
  163.         $data = [
  164.             "note" => $request->get('note'),
  165.             "type" => $request->get('type'),
  166.             "date" => $request->get('date'),
  167.         ];
  168.         return $data;
  169.     }
  170.     
  171. }