public function doUpdate() {
/*require_once '/google-api-php-client/autoload.php';*/
require_once 'autoload.php';
session_start();
$client_id = $this->config['client_id'];
$service_account_name = $this->config['account_name'];
try {
$client = new Google_Client();
$client->setApplicationName($this->config['project_name']);
$service = new Google_Service_Calendar($client);
}
catch (Exception $e) {
die ('Folgender Fehler trat auf: ' . $e->getMessage());
}
if (isset($_SESSION['service_token']))
$client->setAccessToken($_SESSION['service_token']);
$key = file_get_contents($this->config['key_file']);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar'),
$key);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired())
$client->getAuth()->refreshTokenWithAssertion($cred);
$_SESSION['service_token'] = $client->getAccessToken();
//set startDate, endDate, eventParams
$now = new DateTime('NOW');
$clone = clone $now;
$clone = date_modify($clone, '+1 month');
//$clone = date_modify($clone, '+' . $this->config['number_of_days'] . ' days');
$startDate = date_format($now, DateTime::W3C);
$endDate = date_format($clone, DateTime::W3C);
$eventParams = array(
'timeMin' => $startDate,
'timeMax' => $endDate,
'singleEvents' => 'true');
//get Calendar Id´s
$calendarList = $service->calendarList->listCalendarList();
// Set begin of querys
$insertToken = "REPLACE INTO if_system (name, value) VALUES ";
$insertCalendarData = "INSERT INTO if_calendar (color,begin,end,title,location) VALUES ";
$deleteCalendarData = "DELETE FROM if_calendar WHERE ";
/******************** Update every given time without checking for SyncToken ************************/
$forceRefresh = $this->config['force_refresh'];
$updateCalendar = FALSE;
$query = "SELECT * FROM if_system WHERE name = 'CalendarUpdate'";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
if ( ($row['name'] == 'CalendarUpdate') && ((time() - $row['value']) >= ($forceRefresh*3600)) ) {
$updateCalendar = TRUE;
$query = "REPLACE INTO if_system (name,value) VALUES ('CalendarUpdate', '".time()."')";
mysql_query($query, $this->dbconn) or die('CalendarPlugin: '.mysql_error());
}
}else{
$updateCalendar = TRUE;
$query = "INSERT INTO if_system (name,value) VALUES ('CalendarUpdate', '".time()."')";
mysql_query($query, $this->dbconn) or die('CalendarPlugin: '.mysql_error());
}
/*********************************************************************************************************/
$i = 0;
while(TRUE) {
/***************** Loop through each Calendar, get id, and color **************************/
foreach ($calendarList->getItems() as $calendarListEntry) {
$calendarID = $calendarListEntry->getID();
$color = $calendarListEntry->getBackgroundColor();
/***************** Get SysncToken to skip this calender at next update if nothing has changed **********/
$events = $service->events->listEvents($calendarID, $eventParams);
//If the syncToken expires, the server will respond with a 410 GONE response code and the client should clear its storage and perform a full synchronization without any syncToken
try{
$NextSyncToken = $events->getNextSyncToken();
}
catch (Exception $e) {
if (strripos($e->getMessage(), "410") !== FALSE){
$NextSyncToken = 'FEHLER';
}else{
die ('Folgender Fehler trat auf: ' . $e->getMessage());
}
}
$SyncToken = '';
//echo $NextSyncToken.PHP_EOL;
if ($updateCalendar == FALSE) {
$query = "SELECT value FROM if_system WHERE name = '".$calendarID."'";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
$SyncToken = $row['value'];
}
}
$insertToken .= "('".$calendarID."','".$NextSyncToken."'), ";
/***************** SysncToken check done *****************************************************/
/**************** if SyncToken changed perform update ******************************/
if ($NextSyncToken != $SyncToken) {
$i++;
$deleteCalendarData .= "color = '".$color."' OR ";
while(TRUE) {
/************************ Get content from each Calendar *****************/
foreach ($events->getItems() as $event) {
//print_r($event);
if (!empty($event['modelData']['start']['dateTime'])) {
$begin = date_format(date_create($event['modelData']['start']['dateTime']), 'Y-m-d H:i:s');
$end = date_format(date_create($event['modelData']['end']['dateTime']), 'Y-m-d H:i:s');
}else{
$begin = $event['modelData']['start']['date'];
$end = $event['modelData']['end']['date'];
}
$title = $event->getSummary();
$insertCalendarData .= "('".$color."','".$begin."','".$end."','".$title."',''), ";
}
$pageToken = $events->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$events = $service->events->listEvents($calendarListEntry->getID(), $optParams);
}else{
break;
}
}
} /********** if SysncToken changed perform update END **********/
} /*********** Loop through each Calendar, get id, and color END *************************/
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
}else{
break;
}
}
/***************************** if SyncToken changed perform SQL-statement *********************/
if ( $i > 0 ) {
mysql_query($this->before_last('OR', $deleteCalendarData), $this->dbconn) or die('CalendarPlugin -> insertCalendarData: '.mysql_error());
mysql_query($this->before_last(',', $insertToken), $this->dbconn) or die('CalendarPlugin -> insertToken: '.mysql_error());
mysql_query($this->before_last(',', $insertCalendarData), $this->dbconn) or die('CalendarPlugin -> insertCalendarData: '.mysql_error());
}
}