-
Notifications
You must be signed in to change notification settings - Fork 13
/
.gcal.php
86 lines (78 loc) · 2.77 KB
/
.gcal.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
<?php
require_once 'vendor/autoload.php';
require_once '.functions.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=gcal_creds.json');
Class GoogleCalendar {
private $service;
private $calendar_id = '[email protected]';
function __construct() {
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setApplicationName('RPIA Calendar');
$client->addScope(Google_Service_Calendar::CALENDAR);
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$this->service = new Google_Service_Calendar($client);
}
function createEvent($name, $start, $end, $loc, $id, $game) {
$event = new Google_Service_Calendar_Event(array(
'summary' => $name,
'location' => $loc,
'start' => array(
'dateTime' => $start,
'timeZone' => 'America/New_York',
),
'end' => array(
'dateTime' => $end,
'timeZone' => 'America/New_York',
),
));
$event = $this->service->events->insert($this->calendar_id, $event);
$eventId = $event->getId();
$connection = openDatabaseConnection();
if ($game) {
$stmt = $connection->prepare('UPDATE games SET gcalEventId=:eventId WHERE id=:id');
} else {
$stmt = $connection->prepare('UPDATE events SET gcalEventId=:eventId WHERE id=:id');
}
$stmt->bindParam(':eventId', $eventId);
$stmt->bindParam(':id', $id);
$stmt->execute();
$connection = null;
return $event;
}
function deleteEvent($id, $game) {
$eventId = $this->getGcalEventId($id, $game);
$this->service->events->delete($this->calendar_id, $eventId);
}
function updateEvent($name, $start, $end, $loc, $id, $game) {
$eventId = $this->getGcalEventId($id, $game);
$event = new Google_Service_Calendar_Event(array(
'summary' => $name,
'location' => $loc,
'start' => array(
'dateTime' => $start,
'timeZone' => 'America/New_York',
),
'end' => array(
'dateTime' => $end,
'timeZone' => 'America/New_York',
),
));
$event = $this->service->events->update($this->calendar_id, $eventId, $event);
return $event;
}
private function getGcalEventId($id, $game) {
$connection = openDatabaseConnection();
if ($game) {
$stmt = $connection->prepare('SELECT gcalEventId FROM games WHERE id=:id');
} else {
$stmt = $connection->prepare('SELECT gcalEventId FROM events WHERE id=:id');
}
$stmt->bindParam(':id', $id);
$stmt->execute();
$result = $stmt->fetch();
$connection = null;
return $result['gcalEventId'];
}
}
?>