summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2025-06-23 03:33:38 +0200
committerpolo <ordipolo@gmx.fr>2025-06-23 03:33:38 +0200
commitcebc19ef236aac2968d2ffccfcff9b975b63fa8d (patch)
tree5b8e08045a45063475f533bfae4b4524720fe7bd /src
parent8cf5ac1abf9e2a6134cb82d4582aecaa99b1331a (diff)
downloadcms-cebc19ef236aac2968d2ffccfcff9b975b63fa8d.zip
fullcalendar
Diffstat (limited to 'src')
-rw-r--r--src/controller/ajax_calendar.php74
-rw-r--r--src/controller/post.php1
-rw-r--r--src/model/EventDTO.php49
-rw-r--r--src/model/entities/Event.php125
-rw-r--r--src/model/entities/Node.php2
-rw-r--r--src/view/templates/calendar.php20
-rw-r--r--src/view/templates/main.php16
7 files changed, 278 insertions, 9 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
4declare(strict_types=1);
5
6use 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)
10if($_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
42elseif(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
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)
231 require '../src/controller/ajax.php'; 231 require '../src/controller/ajax.php';
232 } 232 }
233} 233}
234require '../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 @@
1<?php
2// src/model/EventDTO.php
3//
4// classe de données JSONifiable compatible avec fullcalendar
5// servira aussi pour l'import/export de fichiers .ics
6
7use App\Entity\Event;
8
9class EventDTO
10{
11 public int $id;
12 public string $title;
13 public string $start;
14 public string $end;
15 public bool $allDay;
16 public ?string $color;
17 public string $description;
18
19 public function __construct(Event $event)
20 {
21 $this->id = $event->getId();
22 $this->title = $event->getTitle();
23 $this->description = $event->getDescription() ?? ''; // renvoie $event->getDescription() si existe et ne vaut pas "null"
24 $this->allDay = $event->isAllDay();
25 $this->color = $event->getColor();
26
27 if($this->allDay){
28 $this->start = $event->getStart()->format('Y-m-d');
29 $this->end = $event->getEnd()->format('Y-m-d');
30 }
31 else{
32 $this->start = $event->getStart()->format('Y-m-d\TH:i:s\Z');
33 $this->end = $event->getEnd()->format('Y-m-d\TH:i:s\Z');
34 }
35 }
36
37 public function toArray(): array
38 {
39 return [
40 'id' => $this->id,
41 'title' => $this->title,
42 'description' => $this->description,
43 'start' => $this->start,
44 'end' => $this->end,
45 'allDay' => $this->allDay,
46 'color' => $this->color,
47 ];
48 }
49}
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 @@
1<?php
2// src/model/entities/Event.php
3
4declare(strict_types=1);
5
6namespace App\Entity;
7
8use Doctrine\ORM\Mapping as ORM;
9
10#[ORM\Entity]
11#[ORM\Table(name: TABLE_PREFIX . 'event')]
12class Event
13{
14 #[ORM\Id]
15 #[ORM\GeneratedValue]
16 #[ORM\Column(type: 'integer')]
17 private int $id;
18
19 #[ORM\Column(type: 'string', length: 255)] // type varchar(255)
20 private string $title;
21 // contrôle JS: if(title.length > 255)
22
23 #[ORM\Column(type: 'text')]
24 private string $description = '';
25 // chatgpt: Dans un contexte API/Front comme FullCalendar,
26 // préférer une chaîne vide à une varaible "null" peut être plus pratique,
27 // car ça évite des contrôles côté JS.
28
29 #[ORM\Column(type: 'datetime')] // chatgpt: Doctrine suppose UTC si pas de configuration spécifique
30 private \DateTimeInterface $start; // typage possible avec une interface,
31 //chatgpt: choix \DateTime par défaut, autorise \DateTimeImmutable
32
33 #[ORM\Column(type: 'datetime')]
34 private \DateTimeInterface $end;
35
36 #[ORM\Column(type: 'boolean')]
37 private bool $all_day;
38
39 #[ORM\Column(type: 'string', length: 7, nullable: true)]
40 private ?string $color = null;
41
42 public function __construct(string $title, string|\DateTimeInterface $start, string|\DateTimeInterface $end, bool $all_day, string $description = '', string $color = null){
43 $this->title = $title;
44 $this->description = $description;
45 $this->start = gettype($start) === 'string' ? new \DateTime($start) : $start;
46 $this->end = gettype($end) === 'string' ? new \DateTime($end) : $end;
47 $this->all_day = $all_day;
48 $this->color = $color;
49 }
50
51 public function updateFromJSON(array $json): void
52 {
53 $this->title = $json['title'];
54 $this->description = $json['description'];
55 $this->start = new \DateTime($json['start']);
56 $this->end = new \DateTime($json['end']);
57 $this->all_day = $json['allDay'];
58 $this->color = $json['color'];
59 }
60
61 public function getId(): int
62 {
63 return $this->id;
64 }
65
66 public function getTitle(): string
67 {
68 return $this->title;
69 }
70 /*public function setTitle(string $title): self
71 {
72 $this->title = $title;
73 return $this;
74 }*/
75
76 public function getDescription(): string
77 {
78 return $this->description;
79 }
80 /*public function setDescription(string $description = ''): self
81 {
82 $this->description = $description;
83 return $this;
84 }*/
85
86 public function getStart(): \DateTimeInterface
87 {
88 return $this->start;
89 }
90 /*public function setStart(\DateTimeInterface $start): self
91 {
92 $this->start = $start;
93 return $this;
94 }*/
95
96 public function getEnd(): \DateTimeInterface
97 {
98 return $this->end;
99 }
100 /*public function setEnd(\DateTimeInterface $end): self
101 {
102 $this->end = $end;
103 return $this;
104 }*/
105
106 public function isAllDay(): bool
107 {
108 return $this->all_day;
109 }
110 /*public function setAllDay(bool $all_day): self
111 {
112 $this->all_day = $all_day;
113 return $this;
114 }*/
115
116 public function getColor(): ?string
117 {
118 return $this->color;
119 }
120 /*public function setColor(?string $color): self
121 {
122 $this->color = $color;
123 return $this;
124 }*/
125}
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
56 56
57 private array $children = []; // tableau de Node 57 private array $children = []; // tableau de Node
58 private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article" 58 private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article"
59 static private array $default_attributes = ['css_array' => ['body', 'head', 'nav', 'foot'],'js_array' => ['main']]; 59 static private array $default_attributes = ['css_array' => ['body', 'head', 'nav', 'foot', 'calendar'],'js_array' => ['main']];
60 60
61 public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null) 61 public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null)
62 { 62 {
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 @@
1<?php declare(strict_types=1); ?> 1<?php declare(strict_types=1); ?>
2<section class="calendar" id="<?= $this->id_node ?>"> 2<section class="calendar" id="<?= $this->id_node ?>">
3 <script src='js/fullcalendar/packages/core/index.global.min.js'></script>
4 <script src='js/fullcalendar/packages/daygrid/index.global.min.js'></script>
5 <script src='js/fullcalendar/packages/timegrid/index.global.min.js'></script>
6 <script src='js/fullcalendar/packages/list/index.global.min.js'></script>
7 <script src='js/fullcalendar/packages/interaction/index.global.min.js'></script>
8 <script src='js/fullcalendar/packages/core/locales/fr.global.min.js'></script>
9<?php
10if($_SESSION['admin'] === true){
11 echo '<script src="js/calendar_admin.js"></script>' . "\n";
12}
13else{
14 echo '<script src="js/calendar.js"></script>' . "\n";
15}
16?>
3 <h3><?= $title ?></h3> 17 <h3><?= $title ?></h3>
18 <div id="calendar_zone">
19 <div id="calendar"></div>
20
21 <!-- si admin -->
22 <aside id="event_modal" class="modal"></aside>
23 </div>
4</section> \ No newline at end of file 24</section> \ 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 @@
23 </div> 23 </div>
24 </div> 24 </div>
25 </div> 25 </div>
26 <div class="delete_page_zone">
27 <form method="post" action="<?= new URL ?>">
28 <label>Supprimer cette page</label>
29 <input type="hidden" name="page_id" value="<?= Director::$page_path->getLast()->getId() ?>">
30 <input type="hidden" name="submit_hidden">
31 <input type="submit" value="Valider" onclick="return confirm('Voulez-vous vraiment supprimer cette page?');">
32 </form>
33 </div>
26 <div class="edit_bloc_zone"> 34 <div class="edit_bloc_zone">
27 <div class="new_bloc"> 35 <div class="new_bloc">
28 <p>Ajouter un bloc de page</p> 36 <p>Ajouter un bloc de page</p>
@@ -42,12 +50,4 @@
42 <?= $bloc_edit ?> 50 <?= $bloc_edit ?>
43 </div> 51 </div>
44 </div> 52 </div>
45 <div class="delete_page_zone">
46 <form method="post" action="<?= new URL ?>">
47 <label>Supprimer cette page</label>
48 <input type="hidden" name="page_id" value="<?= Director::$page_path->getLast()->getId() ?>">
49 <input type="hidden" name="submit_hidden">
50 <input type="submit" value="Valider" onclick="return confirm('Voulez-vous vraiment supprimer cette page?');">
51 </form>
52 </div>
53</section> \ No newline at end of file 53</section> \ No newline at end of file