forked from rpiambulance/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.add_event.php
108 lines (86 loc) · 3.06 KB
/
.add_event.php
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
<?php
require_once ".db_config.php";
require_once ".functions.php";
require_once ".gcal.php";
// empty response
$response = null;
// Google Calendar Object
$googleCalendar = new GoogleCalendar();
//array to hold errors
$errors = array();
// array to pass back data
$data = array();
// Get the input ===============================================================
$formData = file_get_contents('php://input');
$input = json_decode($formData, true);
$event_name = $input['event_name'];
$event_location = $input['event_location'];
$start_time = $input['startstamp'];
$end_time = $input['endstamp'];
$date = $input['datestamp'];
$limit = $input['limit'];
$mode = $input['mode'];
if($mode == 'edit') {
$id = $input['id'];
}
$connection = new PDO("mysql:host=$dhost;dbname=$dname", $duser, $dpassword);
if (checkIfAdmin($connection)){
try {
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!isset($dname)) {
$dname = 'ambulanc_web';
}
// Selecting Database
$connection->exec("USE `$dname`");
if($mode == 'add') {
$statement = $connection->query("SELECT MAX(id) as max FROM events");
$logid = $statement->fetchAll(PDO::FETCH_ASSOC)[0]['max'] + 1;
$statement = $connection->prepare("INSERT INTO events(id, `date`, start,
end, description, location, `limit`, hide) VALUES (:logid, :date, :start_time, :end_time,
:event_name, :event_location, :limit, 0)");
$statement->bindParam(":logid", $logid);
$statement->bindParam(":date", $date);
$statement->bindParam(":start_time", $start_time);
$statement->bindParam(":end_time", $end_time);
$statement->bindParam(":event_name", $event_name);
$statement->bindParam(":event_location", $event_location);
$statement->bindParam(":limit", $limit);
$result = $statement->execute();
} else {
$statement = $connection->prepare("UPDATE events SET
`date`=:date,
`start`=:start_time,
`end`=:end_time,
`description`=:event_name,
`location`=:event_location,
`limit`=:limit,
`hide`=0
WHERE id=:id");
$statement->bindParam(":id", $id);
$statement->bindParam(":date", $date);
$statement->bindParam(":start_time", $start_time);
$statement->bindParam(":end_time", $end_time);
$statement->bindParam(":event_name", $event_name);
$statement->bindParam(":event_location", $event_location);
$statement->bindParam(":limit", $limit);
$result = $statement->execute();
}
if($result) {
$data['success'] = true;
if ($mode == 'add') {
$googleCalendar->createEvent($event_name, $date . 'T' . $start_time, $date . 'T' . $end_time, $event_location, $logid, false);
} else {
$googleCalendar->updateEvent($event_name, $date . 'T' . $start_time, $date . 'T' . $end_time, $event_location, $id, false);
}
} else {
$data['success'] = false;
}
} catch(PDOException $e) {
$data['success'] = false;
$data['error'] = $e;
}
echo(json_encode($data));
} else {
echo "Nice try.";
}
?>