From 8ee3440f204dfc6532a49a05719ba37a8c4df359 Mon Sep 17 00:00:00 2001 From: polo Date: Sat, 21 Jun 2025 01:44:16 +0200 Subject: =?UTF-8?q?=C3=A9changes=20client/serveur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/index.php | 1 + public/js/calendar.js | 6 ++- public/js/calendar_admin.js | 13 +++--- src/controller/calendar.php | 99 +++++++++++++++++++++++++++++++++++++++++++++ src/load-events.php | 67 ------------------------------ src/post-ajax.php | 14 ------- 6 files changed, 113 insertions(+), 87 deletions(-) create mode 100644 src/controller/calendar.php delete mode 100644 src/load-events.php delete mode 100644 src/post-ajax.php diff --git a/public/index.php b/public/index.php index abcef90..99220e8 100644 --- a/public/index.php +++ b/public/index.php @@ -1,6 +1,7 @@ diff --git a/public/js/calendar.js b/public/js/calendar.js index f1c55c4..2193272 100644 --- a/public/js/calendar.js +++ b/public/js/calendar.js @@ -1,3 +1,5 @@ +// js/calendar.js + document.addEventListener('DOMContentLoaded', function(){ const calendarEl = document.getElementById('calendar'); let selected_start_string = null; @@ -34,10 +36,12 @@ document.addEventListener('DOMContentLoaded', function(){ allDayContent: 'Journée', // texte dans la case "toute la journée" nowIndicator: true, // barre rouge pour maintenant - events: '../src/load-events.php', // fichier PHP qui retourne les événements + // params en plus: https://fullcalendar.io/docs/events-json-feed + events: 'index.php?action=get_events', // fichier PHP qui retourne les événements select: function(info){ selected_start_string = info.startStr; // variable "globale" + hideModal(); }, //unselect: function(event, view){}, diff --git a/public/js/calendar_admin.js b/public/js/calendar_admin.js index 8fe91a3..a99b069 100644 --- a/public/js/calendar_admin.js +++ b/public/js/calendar_admin.js @@ -1,3 +1,5 @@ +// js/calendar_admin.js + document.addEventListener('DOMContentLoaded', function(){ const calendarEl = document.getElementById('calendar'); let selected_start_string = null; @@ -35,7 +37,8 @@ document.addEventListener('DOMContentLoaded', function(){ allDayContent: 'Journée', // texte dans la case "toute la journée" nowIndicator: true, // barre rouge pour maintenant - events: '../src/load-events.php', // fichier PHP qui retourne les événements + // params en plus: https://fullcalendar.io/docs/events-json-feed + events: 'index.php?action=get_events', // fichier PHP qui retourne les événements select: function(info){ selected_start_string = info.startStr; // variable "globale" @@ -220,7 +223,7 @@ document.addEventListener('DOMContentLoaded', function(){ color: event_color }; - fetch('../src/post-ajax.php', { + fetch('index.php?action=new_event', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -255,7 +258,7 @@ document.addEventListener('DOMContentLoaded', function(){ color: event_color }; - fetch('../src/post-ajax.php', { + fetch('index.php?action=update_event', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -318,12 +321,12 @@ document.addEventListener('DOMContentLoaded', function(){ const event_id = document.getElementById('event_id').value; const event = calendar.getEventById(event_id); - fetch('../src/post-ajax.php', { + fetch('index.php?action=remove_event', { method: 'POST', headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify(event_id), + body: JSON.stringify({'id': event_id}), }) .then(response => response.json()) .then(data => { diff --git a/src/controller/calendar.php b/src/controller/calendar.php new file mode 100644 index 0000000..ab2832e --- /dev/null +++ b/src/controller/calendar.php @@ -0,0 +1,99 @@ +setTimezone(new DateTimeZone('UTC')); + $end->setTimezone(new DateTimeZone('UTC')); + + // recherche des évènement en BDD avec ceci: + // WHERE end >= :start AND start <= :end + // on prend les évènements se finissant après le début + // ou commençant avant la fin de la fourchette + + // affichage format ISO à l'heure UTC + //$date->format('Y-m-d\TH:i:s\Z'); + + // chatgpt suggère l'utilisation d'un DTO => une classe de données simple et tout "public" + // => pour des évènements obtenables autant depuis la BDD que de fichiers .ics par exemple + + $events = [ + [ + 'id' => 1, + 'title' => 'Évènement1', + 'start' => '2025-06-03T05:00:00Z', // Z indique que l'heure est en UTC + 'end' => '2025-06-03T09:00:00Z', + 'allDay' => false, + 'color' => '#ffa500', // couleur hexa, éviter les couleurs CSS qui ne fonctionnent pas dans value="" en HTML + //'url' => 'https://dev.nageurs-bigoudens.fr', // comportement: https://fullcalendar.io/docs/eventClick + 'description' => 'blablabla', + ], + [ + 'id' => 2, + 'title' => 'Évènement2', + 'start' => '2025-06-06T08:00:00Z', + 'end' => '2025-06-07T08:00:00Z', + 'allDay' => false, + 'color' => '#e01b24', + 'description' => 'truc machin', + ], + [ + 'id' => 3, + 'title' => 'Évènement3', + 'start' => '2025-06-08', + 'end' => '2025-06-09', + 'allDay' => true, // pas d'heure + 'color' => '#008000', + 'description' => 'ça va chier', + ], + // provoque une erreur, si allDay la fin ne peut être égale au début + /*[ + 'id' => 4, + 'title' => 'Évènement4', + 'start' => '2025-06-09', + 'end' => '2025-06-09', + 'allDay' => true, + 'color' => '#1a5fb4', + ],*/ + ]; + + header('Content-Type: application/json'); + echo json_encode($events); + die; +} + +// actions sur le calendrier +elseif(isset($_SESSION['admin']) && $_SESSION['admin'] === true + && $_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json') +{ + $data = file_get_contents('php://input'); + $json = json_decode($data, true); + + if($_GET['action'] === 'new_event'){ + // BDD + //print_r($json);die; + $id = 7; // généré par la BDD + echo json_encode(['success' => true, 'id' => $id]); + } + elseif($_GET['action'] === 'update_event'){ + // BDD + //print_r($json);die; + echo json_encode(['success' => true]); + } + elseif($_GET['action'] === 'remove_event'){ + // BDD + //echo $json['id']; die; + echo json_encode(['success' => true]); + } + else{ + echo json_encode(['success' => false]); + } + die; +} \ No newline at end of file diff --git a/src/load-events.php b/src/load-events.php deleted file mode 100644 index 5c71137..0000000 --- a/src/load-events.php +++ /dev/null @@ -1,67 +0,0 @@ -setTimezone(new DateTimeZone('UTC')); - $end->setTimezone(new DateTimeZone('UTC')); - - // recherche des évènement en BDD avec ceci: - // WHERE end >= :start AND start <= :end - // on prend les évènements se finissant après le début - // ou commençant avant la fin de la fourchette - - // affichage format ISO à l'heure UTC - //$date->format('Y-m-d\TH:i:s\Z'); - - // chatgpt suggère l'utilisation d'un DTO - // => une classe de données simple et tout "public" pour des évènements obtenables autant depuis la BDD que de fichiers .ics par exemple - - $events = [ - [ - 'id' => 1, - 'title' => 'Évènement1', - 'start' => '2025-06-03T05:00:00Z', // Z indique que l'heure est en UTC - 'end' => '2025-06-03T09:00:00Z', - 'allDay' => false, - 'color' => '#ffa500', // couleur hexa, éviter les couleurs CSS qui ne fonctionnent pas dans value="" en HTML - //'url' => 'https://dev.nageurs-bigoudens.fr', // comportement: https://fullcalendar.io/docs/eventClick - 'description' => 'blablabla', - ], - [ - 'id' => 2, - 'title' => 'Évènement2', - 'start' => '2025-06-06T08:00:00Z', - 'end' => '2025-06-07T08:00:00Z', - 'allDay' => false, - 'color' => '#e01b24', - 'description' => 'truc machin', - ], - [ - 'id' => 3, - 'title' => 'Évènement3', - 'start' => '2025-06-08', - 'end' => '2025-06-09', - 'allDay' => true, // pas d'heure - 'color' => '#008000', - 'description' => 'ça va chier', - ], - // provoque une erreur, si allDay la fin ne peut être égale au début - /*[ - 'id' => 4, - 'title' => 'Évènement4', - 'start' => '2025-06-09', - 'end' => '2025-06-09', - 'allDay' => true, - 'color' => '#1a5fb4', - ],*/ - ]; - - header('Content-Type: application/json'); - echo json_encode($events); -} - - diff --git a/src/post-ajax.php b/src/post-ajax.php deleted file mode 100644 index bedfed5..0000000 --- a/src/post-ajax.php +++ /dev/null @@ -1,14 +0,0 @@ - true, 'id' => $id]); - die; - } -} \ No newline at end of file -- cgit v1.2.3