summaryrefslogtreecommitdiff
path: root/src/controller/ajax_calendar.php
blob: 79268f62a10592d15ea2fb4899ec1881e55e01bd (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
<?php
// src/controller/calendar.php

declare(strict_types=1);

use App\Entity\Event;

// 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'));

    // 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'){
        try{
            $event = new Event($json);
        }
        catch(InvalidArgumentException $e){
            echo json_encode(['success' => false, 'error' => $e->getMessage()]);
            http_response_code(400);
            die;
        }
        $entityManager->persist($event);
        $entityManager->flush();
		
		echo json_encode(['success' => true, 'id' => $event->getId()]);
	}
	elseif($_GET['action'] === 'update_event'){
        $event = $entityManager->find('App\Entity\Event', (int)$json['id']);
        try{
            $event->securedUpdateFromJSON($json);
        }
        catch(InvalidArgumentException $e){
            echo json_encode(['success' => false, 'error' => $e->getMessage()]);
            http_response_code(400);
            die;
        }
        $entityManager->flush();

		echo json_encode(['success' => true]);
	}
	elseif($_GET['action'] === 'remove_event'){
        $event = $entityManager->find('App\Entity\Event', (int)$json['id']);
        $entityManager->remove($event);
        $entityManager->flush();

		echo json_encode(['success' => true]);
	}
	else{
		echo json_encode(['success' => false]);
	}
	die;
}