-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusAdapter.php
96 lines (78 loc) · 3.29 KB
/
StatusAdapter.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
<?php
set_include_path( get_include_path() . PATH_SEPARATOR . dirname(__FILE__) );
require_once("Google/Client.php");
require_once("Google/Service/Calendar.php");
class StatusAdapter extends DbAdapter{
private $statusTableName = "status";
private $spaceOpenPropName = "space_is_open";
public function __construct(){
parent::__construct();
}
public function IsSpaceOpen(){
// return array('status' => false, 'updated' => '');
$res = $this->connection->query("SELECT value, updated FROM {$this->statusTableName} WHERE prop_name='{$this->spaceOpenPropName}'");
$row = $res->fetch_assoc();
if( $row != null ){
return array('status' =>Helpers::asBoolean($row["value"]), 'updated' => date('l, F jS @ g:ia',strtotime($row["updated"])), 'raw_updated' => $row['updated']);
} else {
throw new Exception("Failed to retrieve open status from database");
}
}
public function SetIsSpaceOpen($isOpen){
$boolValue = Helpers::asBoolean($isOpen);
$dateTime = Helpers::asDateTime();
if (!$isOpen) {
$last_update = $this->IsSpaceOpen();
if ($last_update['status'] == true) {
$start_time = date('c', strtotime($last_update['raw_updated']));
$end_time = date('c');
$GoogleClient = new Google_Client();
// $GoogleClient->setUseObjects(true);
$GoogleClient->setApplicationName('lmn-doorbot');
$GoogleClient->setClientId('656101111249-uk80lnjrgh6b60lp7ki8o0t61j5shqk8.apps.googleusercontent.com');
$GoogleClient->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
"656101111249-uk80lnjrgh6b60lp7ki8o0t61j5shqk8@developer.gserviceaccount.com",
array(
"https://www.googleapis.com/auth/calendar"
),
file_get_contents("GoogleCred.p12")
)
);
$service = new Google_Service_Calendar($GoogleClient);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Twitter Switch OPEN');
$event->setLocation('LMN');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($start_time);
$start->setTimeZone('America/Detroit');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($end_time);
$end->setTimeZone('America/Detroit');
$event->setEnd($end);
$calendar_id = "lansingmakersnetwork.org_ntpart4sekc3m68fvedrni1asc@group.calendar.google.com";
$new_event = null;
try {
$new_event = $service->events->insert($calendar_id, $event);
$new_event_id= $new_event->getId();
} catch (Google_ServiceException $e) {
throw new Exception("Failed to create Google Event");
}
$event = $service->events->get($calendar_id, $new_event->getId());
/**
if ($event != null) {
echo "Inserted:";
echo "EventID=".$event->getId();
echo "Summary=".$event->getSummary();
echo "Status=".$event->getStatus();
} else {
echo "Error creating event";
} **/
}
}
if( !$this->connection->query("UPDATE {$this->statusTableName} SET value='$boolValue', updated='$dateTime' WHERE prop_name='{$this->spaceOpenPropName}'") ){
throw new Exception("Failed to update open status");
}
}
}