From cebc19ef236aac2968d2ffccfcff9b975b63fa8d Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 23 Jun 2025 03:33:38 +0200 Subject: fullcalendar --- src/controller/ajax_calendar.php | 74 +++++++++++++++++++++++ src/controller/post.php | 1 + src/model/EventDTO.php | 49 +++++++++++++++ src/model/entities/Event.php | 125 +++++++++++++++++++++++++++++++++++++++ src/model/entities/Node.php | 2 +- src/view/templates/calendar.php | 20 +++++++ src/view/templates/main.php | 16 ++--- 7 files changed, 278 insertions(+), 9 deletions(-) create mode 100644 src/controller/ajax_calendar.php create mode 100644 src/model/EventDTO.php create mode 100644 src/model/entities/Event.php (limited to 'src') 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 @@ +setTimezone(new DateTimeZone('UTC')); + $end->setTimezone(new DateTimeZone('UTC')); + + // affichage format ISO à l'heure UTC + //$date->format('Y-m-d\TH:i:s\Z'); + + // on prend les évènements se finissant après le début ou commençant avant la fin de la fourchette + $dql = 'SELECT e FROM App\Entity\Event e WHERE e.end >= :start AND e.start <= :end'; + $bulk_data = $entityManager->createQuery($dql) + ->setParameter('start', $start) + ->setParameter('end', $end) + ->getResult(); + + $events = []; + foreach($bulk_data as $one_entry){ + $event = new EventDTO($one_entry); + $events[] = $event->toArray(); + } + + 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'){ + $event = new Event($json['title'], $json['start'], $json['end'], $json['allDay'], $json["description"], $json['color']); + + $entityManager->persist($event); + $entityManager->flush(); + + echo json_encode(['success' => true, 'id' => $event->getId()]); + } + elseif($_GET['action'] === 'update_event'){ + $event = $entityManager->find('App\Entity\Event', $json['id']); + $event->updateFromJSON($json); + $entityManager->flush(); + + echo json_encode(['success' => true]); + } + elseif($_GET['action'] === 'remove_event'){ + $event = $entityManager->find('App\Entity\Event', $json['id']); + $entityManager->remove($event); + $entityManager->flush(); + + echo json_encode(['success' => true]); + } + else{ + echo json_encode(['success' => false]); + } + die; +} \ No newline at end of file diff --git a/src/controller/post.php b/src/controller/post.php index deacafb..b0bc6a0 100644 --- a/src/controller/post.php +++ b/src/controller/post.php @@ -231,3 +231,4 @@ if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SESSION['admin'] === true) require '../src/controller/ajax.php'; } } +require '../src/controller/ajax_calendar.php'; \ No newline at end of file diff --git a/src/model/EventDTO.php b/src/model/EventDTO.php new file mode 100644 index 0000000..8d33733 --- /dev/null +++ b/src/model/EventDTO.php @@ -0,0 +1,49 @@ +id = $event->getId(); + $this->title = $event->getTitle(); + $this->description = $event->getDescription() ?? ''; // renvoie $event->getDescription() si existe et ne vaut pas "null" + $this->allDay = $event->isAllDay(); + $this->color = $event->getColor(); + + if($this->allDay){ + $this->start = $event->getStart()->format('Y-m-d'); + $this->end = $event->getEnd()->format('Y-m-d'); + } + else{ + $this->start = $event->getStart()->format('Y-m-d\TH:i:s\Z'); + $this->end = $event->getEnd()->format('Y-m-d\TH:i:s\Z'); + } + } + + public function toArray(): array + { + return [ + 'id' => $this->id, + 'title' => $this->title, + 'description' => $this->description, + 'start' => $this->start, + 'end' => $this->end, + 'allDay' => $this->allDay, + 'color' => $this->color, + ]; + } +} diff --git a/src/model/entities/Event.php b/src/model/entities/Event.php new file mode 100644 index 0000000..c85832f --- /dev/null +++ b/src/model/entities/Event.php @@ -0,0 +1,125 @@ + 255) + + #[ORM\Column(type: 'text')] + private string $description = ''; + // chatgpt: Dans un contexte API/Front comme FullCalendar, + // préférer une chaîne vide à une varaible "null" peut être plus pratique, + // car ça évite des contrôles côté JS. + + #[ORM\Column(type: 'datetime')] // chatgpt: Doctrine suppose UTC si pas de configuration spécifique + private \DateTimeInterface $start; // typage possible avec une interface, + //chatgpt: choix \DateTime par défaut, autorise \DateTimeImmutable + + #[ORM\Column(type: 'datetime')] + private \DateTimeInterface $end; + + #[ORM\Column(type: 'boolean')] + private bool $all_day; + + #[ORM\Column(type: 'string', length: 7, nullable: true)] + private ?string $color = null; + + public function __construct(string $title, string|\DateTimeInterface $start, string|\DateTimeInterface $end, bool $all_day, string $description = '', string $color = null){ + $this->title = $title; + $this->description = $description; + $this->start = gettype($start) === 'string' ? new \DateTime($start) : $start; + $this->end = gettype($end) === 'string' ? new \DateTime($end) : $end; + $this->all_day = $all_day; + $this->color = $color; + } + + public function updateFromJSON(array $json): void + { + $this->title = $json['title']; + $this->description = $json['description']; + $this->start = new \DateTime($json['start']); + $this->end = new \DateTime($json['end']); + $this->all_day = $json['allDay']; + $this->color = $json['color']; + } + + public function getId(): int + { + return $this->id; + } + + public function getTitle(): string + { + return $this->title; + } + /*public function setTitle(string $title): self + { + $this->title = $title; + return $this; + }*/ + + public function getDescription(): string + { + return $this->description; + } + /*public function setDescription(string $description = ''): self + { + $this->description = $description; + return $this; + }*/ + + public function getStart(): \DateTimeInterface + { + return $this->start; + } + /*public function setStart(\DateTimeInterface $start): self + { + $this->start = $start; + return $this; + }*/ + + public function getEnd(): \DateTimeInterface + { + return $this->end; + } + /*public function setEnd(\DateTimeInterface $end): self + { + $this->end = $end; + return $this; + }*/ + + public function isAllDay(): bool + { + return $this->all_day; + } + /*public function setAllDay(bool $all_day): self + { + $this->all_day = $all_day; + return $this; + }*/ + + public function getColor(): ?string + { + return $this->color; + } + /*public function setColor(?string $color): self + { + $this->color = $color; + return $this; + }*/ +} diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index 850f37d..711eb3e 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php @@ -56,7 +56,7 @@ class Node private array $children = []; // tableau de Node private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article" - static private array $default_attributes = ['css_array' => ['body', 'head', 'nav', 'foot'],'js_array' => ['main']]; + static private array $default_attributes = ['css_array' => ['body', 'head', 'nav', 'foot', 'calendar'],'js_array' => ['main']]; public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null) { diff --git a/src/view/templates/calendar.php b/src/view/templates/calendar.php index d85ade7..144df00 100644 --- a/src/view/templates/calendar.php +++ b/src/view/templates/calendar.php @@ -1,4 +1,24 @@
+ + + + + + +' . "\n"; +} +else{ + echo '' . "\n"; +} +?>

+
+
+ + + +
\ No newline at end of file diff --git a/src/view/templates/main.php b/src/view/templates/main.php index c2b631d..9908be7 100644 --- a/src/view/templates/main.php +++ b/src/view/templates/main.php @@ -23,6 +23,14 @@ +
+
+ + + + +
+

Ajouter un bloc de page

@@ -42,12 +50,4 @@
-
-
- - - - -
-
\ No newline at end of file -- cgit v1.2.3