diff options
Diffstat (limited to 'src/controller/ajax_calendar.php')
| -rw-r--r-- | src/controller/ajax_calendar.php | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/controller/ajax_calendar.php b/src/controller/ajax_calendar.php new file mode 100644 index 0000000..834c88b --- /dev/null +++ b/src/controller/ajax_calendar.php | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/calendar.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | use App\Entity\Event; | ||
| 7 | |||
| 8 | // chargement des évènements à la création du calendrier | ||
| 9 | // et au changement de dates affichées (boutons flèches mais pas changement de vue) | ||
| 10 | if($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_events' | ||
| 11 | && isset($_GET['start']) && isset($_GET['end']) && empty($_POST)) | ||
| 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 | // actions sur le calendrier | ||
| 42 | elseif(isset($_SESSION['admin']) && $_SESSION['admin'] === true | ||
| 43 | && $_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json') | ||
| 44 | { | ||
| 45 | $data = file_get_contents('php://input'); | ||
| 46 | $json = json_decode($data, true); | ||
| 47 | |||
| 48 | if($_GET['action'] === 'new_event'){ | ||
| 49 | $event = new Event($json['title'], $json['start'], $json['end'], $json['allDay'], $json["description"], $json['color']); | ||
| 50 | |||
| 51 | $entityManager->persist($event); | ||
| 52 | $entityManager->flush(); | ||
| 53 | |||
| 54 | echo json_encode(['success' => true, 'id' => $event->getId()]); | ||
| 55 | } | ||
| 56 | elseif($_GET['action'] === 'update_event'){ | ||
| 57 | $event = $entityManager->find('App\Entity\Event', $json['id']); | ||
| 58 | $event->updateFromJSON($json); | ||
| 59 | $entityManager->flush(); | ||
| 60 | |||
| 61 | echo json_encode(['success' => true]); | ||
| 62 | } | ||
| 63 | elseif($_GET['action'] === 'remove_event'){ | ||
| 64 | $event = $entityManager->find('App\Entity\Event', $json['id']); | ||
| 65 | $entityManager->remove($event); | ||
| 66 | $entityManager->flush(); | ||
| 67 | |||
| 68 | echo json_encode(['success' => true]); | ||
| 69 | } | ||
| 70 | else{ | ||
| 71 | echo json_encode(['success' => false]); | ||
| 72 | } | ||
| 73 | die; | ||
| 74 | } \ No newline at end of file | ||
