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