diff options
Diffstat (limited to 'src/Controller/CalendarController.php')
| -rw-r--r-- | src/Controller/CalendarController.php | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/Controller/CalendarController.php b/src/Controller/CalendarController.php new file mode 100644 index 0000000..679ff8a --- /dev/null +++ b/src/Controller/CalendarController.php | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | <?php | ||
| 2 | // /src/Controller/CalendarController.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | namespace App\Controller; | ||
| 7 | |||
| 8 | use Doctrine\ORM\EntityManager; | ||
| 9 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
| 10 | use App\Entity\Event; | ||
| 11 | use App\Model\EventDTO; | ||
| 12 | |||
| 13 | class CalendarController | ||
| 14 | { | ||
| 15 | static public function getData(EntityManager $entityManager): JsonResponse | ||
| 16 | { | ||
| 17 | // bornes début et fin du calendrier affiché à l'heure locale | ||
| 18 | // noter que la vue "planning" est similaire à la vue "semaine" | ||
| 19 | $start = new \DateTime($_GET['start']); | ||
| 20 | $end = new \DateTime($_GET['end']); | ||
| 21 | $start->setTimezone(new \DateTimeZone('UTC')); | ||
| 22 | $end->setTimezone(new \DateTimeZone('UTC')); | ||
| 23 | |||
| 24 | // affichage format ISO à l'heure UTC | ||
| 25 | //$date->format('Y-m-d\TH:i:s\Z'); | ||
| 26 | |||
| 27 | // on prend les évènements se finissant après le début ou commençant avant la fin de la fourchette | ||
| 28 | $dql = 'SELECT e FROM App\Entity\Event e WHERE e.end >= :start AND e.start <= :end'; | ||
| 29 | $bulk_data = $entityManager->createQuery($dql) | ||
| 30 | ->setParameter('start', $start) | ||
| 31 | ->setParameter('end', $end) | ||
| 32 | ->getResult(); | ||
| 33 | |||
| 34 | $events = []; | ||
| 35 | foreach($bulk_data as $one_entry){ | ||
| 36 | $event = new EventDTO($one_entry); | ||
| 37 | $events[] = $event->toArray(); | ||
| 38 | } | ||
| 39 | |||
| 40 | return new JsonResponse($events); | ||
| 41 | } | ||
| 42 | |||
| 43 | static public function newEvent(array $json, EntityManager $entityManager): JsonResponse | ||
| 44 | { | ||
| 45 | try{ | ||
| 46 | $event = new Event($json); | ||
| 47 | } | ||
| 48 | catch(\InvalidArgumentException $e){ | ||
| 49 | return new JsonResponse(['success' => false, 'error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST); // code 400 | ||
| 50 | } | ||
| 51 | $entityManager->persist($event); | ||
| 52 | $entityManager->flush(); | ||
| 53 | |||
| 54 | return new JsonResponse(['success' => true, 'id' => $event->getId()]); | ||
| 55 | } | ||
| 56 | static public function updateEvent(array $json, EntityManager $entityManager): JsonResponse | ||
| 57 | { | ||
| 58 | $event = $entityManager->find('App\Entity\Event', (int)$json['id']); | ||
| 59 | try{ | ||
| 60 | $event->securedUpdateFromJSON($json); | ||
| 61 | } | ||
| 62 | catch(\InvalidArgumentException $e){ | ||
| 63 | return new JsonResponse(['success' => false, 'error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST); // code 400 | ||
| 64 | } | ||
| 65 | $entityManager->flush(); | ||
| 66 | |||
| 67 | return new JsonResponse(['success' => true]); | ||
| 68 | } | ||
| 69 | static public function removeEvent(array $json, EntityManager $entityManager): JsonResponse | ||
| 70 | { | ||
| 71 | $event = $entityManager->find('App\Entity\Event', (int)$json['id']); | ||
| 72 | $entityManager->remove($event); | ||
| 73 | $entityManager->flush(); | ||
| 74 | |||
| 75 | return new JsonResponse(['success' => true]); | ||
| 76 | } | ||
| 77 | } \ No newline at end of file | ||
