summaryrefslogtreecommitdiff
path: root/src/controller/calendar.php
blob: c15141a415bd9309735452d00e37d97f11865e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
// src/controller/calendar.php

function cleanInput(array $data): array
{
    $data['title'] = htmlspecialchars($data['title']);
    $data['description'] = htmlspecialchars($data['description']);
    try{
	$data['start'] = new Datetime($data['start']);
	$data['end'] = new Datetime($data['end']);
    }
    catch(Exception $e){
	throw new InvalidArgumentException('Bad date input');
    }
    $data['allDay'] = filter_var($data['allDay'] ?? null, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
    if(!is_bool($data['allDay'])){
	throw new InvalidArgumentException('Bad checkbox input');
    }
    $data['color'] = isset($data['color']) ? htmlspecialchars($data['color']) : null;
    return $data;
}

// chargement des évènements à la création du calendrier
// et au changement de dates affichées (boutons flèches mais pas changement de vue)
if($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_events'
    && isset($_GET['start']) && isset($_GET['end']) && empty($_POST))
{
    // bornes début et fin du calendrier affiché à l'heure locale
    // noter que la vue "planning" est similaire à la vue "semaine"
    $start = new DateTime($_GET['start']);
    $end = new DateTime($_GET['end']);
    $start->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',
	    'description' => 'blablabla',
	    '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
        ],
        [
	    'id' => 2,
	    'title' => 'Évènement2',
	    'description' => 'truc machin',
	    'start' => '2025-06-06T08:00:00Z',
	    'end' => '2025-06-07T08:00:00Z',
	    'allDay' => false,
	    'color' => '#e01b24',
        ],
        [
	    'id' => 3,
	    'title' => 'Évènement3',
	    'description' => 'ça va chier',
	    'start' => '2025-06-08',
	    'end' => '2025-06-09',
	    'allDay' => true, // pas d'heure
	    'color' => '#008000',
        ],
    ];

    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'){
	try{
	    $json = cleanInput($json);
	}
	catch(InvalidArgumentException $e){
	    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
	    http_response_code(400);
	    die;
	}
	//print_r($json);die;
	
	$id = 7; // généré par la BDD
	echo json_encode(['success' => true, 'id' => $id]);
    }
    elseif($_GET['action'] === 'update_event'){
	try{
	    $json = cleanInput($json);
	}
	catch(InvalidArgumentException $e){
	    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
	    http_response_code(400);
	    die;
	}
	//print_r($json);die;
	
	echo json_encode(['success' => true]);
    }
    elseif($_GET['action'] === 'remove_event'){
	$json['id'] = (int)$json['id'];
	//echo $json['id']; die;
	
	echo json_encode(['success' => true]);
    }
    else{
	echo json_encode(['success' => false]);
    }
    die;
}