Here's the code for the Google Calendar sync. Keep in mind that I still haven't added much error checking yet (and I did have to make a couple of small changes to the core to get things working (I'm hoping they can be included in future releases):
eme_events.php
LINE 2811
added event ID to array so that the google calendar edit URL can be added to the attributes for future updates/deletes
$event['event_id'] = $event_ID;
LINE 1405
Remove Google URL from duplicated event
Note, the code doesn't handle recurring events at all yet. The hooks are at the end of the code. This code is in my child theme's function.php.
/**********************************************************************/
// Event Manager Extended - Add Google update, insert and delete functions
function createGoogleCalConnection() {
$client = "";
if (is_admin()) {
set_include_path('/home/user/php/ZendGdata-1.11.3/library');
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Gdata_App_HttpException');
Zend_Loader::loadClass('Zend_Http_Client_Exception');
# Zend_Loader::loadClass('Zend_Http_Client');
# Zend_Loader::loadClass('Zend_Http_Client_Adapter_Proxy');
$user = 'user@domain.com';
$pass = 'password';
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
//$pdd_google_client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service);
try {
$client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service);
} catch (Zend_Gdata_App_AuthException $ae) {
echo 'Problem authenticating: ' . $ae->exception() . "\n";
return FALSE;
}
$gdataCal = new Zend_Gdata_Calendar($client);
return $gdataCal;
}
} // end createGoogleCalConnection
function pdd_insertGoogleEvent($g_event) {
$title = '';
$desc='';
$where = '';
$startDate = '';
$startTime = '';
$endDate = '';
$endTime = '';
$tzOffset = '';
$title = $g_event['title'];
$startDate = $g_event['when']['startDate'];
$endDate = $g_event['when']['endDate'];
$startTime = $g_event['when']['startTime'];
$endTime = $g_event['when']['endTime'];
$desc = $g_event['desc'];
$where = $g_event['where'];
$tzOffset = $g_event['tzOffset'];
echo "" . $title . ", " . $desc . ", " . $where . ", " . $startDate . ", " . $startTime . ", " . $endDate . ", " . $endTime . ", " . $tzOffset . "";
$gdataCal = createGoogleCalConnection();
$newEvent = $gdataCal->newEventEntry();
$newEvent->title = $gdataCal->newTitle($title);
$newEvent->where = array($gdataCal->newWhere($where));
$newEvent->content = $gdataCal->newContent("$desc");
$when = $gdataCal->newWhen();
$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
echo "Start: " . $when->startTime . "";
$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
echo "End: " . $when->endTime . "";
$newEvent->when = array($when);
//print_r($newEvent);
// Upload the event to the calendar server
// A copy of the event as it is recorded on the server is returned
$createdEvent = $gdataCal->insertEvent($newEvent);
return $createdEvent->id->text;
} // end function pdd_insertGoogleEvent
function pdd_updateGoogleEvent($g_event) {
//print_r($g_event);
$gdataCal = createGoogleCalConnection();
if ($gdataCal) {
// check to see if update or insert
if (!empty($g_event['google_url'])) {
$title = '';
$desc='';
$where = '';
$startDate = '';
$startTime = '';
$endDate = '';
$endTime = '';
$tzOffset = '';
$title = $g_event['title'];
$startDate = $g_event['when']['startDate'];
$endDate = $g_event['when']['endDate'];
$startTime = $g_event['when']['startTime'];
$endTime = $g_event['when']['endTime'];
$desc = $g_event['desc'];
$where = $g_event['where'];
$tzOffset = $g_event['tzOffset'];
// update
// retrieve event
// set new event properties and update event
try {
$gEvent = $gdataCal->getCalendarEventEntry($g_event['google_url']);
$gEvent->title = $gdataCal->newTitle($title);
$gEvent->content = $gdataCal->newContent("$desc");
$gEvent->where = array($gdataCal->newWhere($where));
$when = $gdataCal->newWhen();
if ($startTime == $endTime && $startDate == $endDate) {
$when->startTime = "{$startDate}";
unset($when->endTime);
} else {
$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
}
$gEvent->when = array($when);
$gEvent->save();
} catch (Zend_Gdata_App_Exception $e) {
die("Error: " . $e->getResponse());
}
echo 'Event successfully modified!';
}
}
} // end function pdd_insertupdateGoogleEvent
function pdd_map_to_google($event) {
// should only be called after standardizing the event variable
// not expecting attributes to be serialized
// Manipulate the data to fit Google's fields
$g_event = array();
// if this event has attriutes
if (!empty($event['event_attributes'])) {
$tmp_attributes = '';
$all_day_event = FALSE;
foreach($event['event_attributes'] as $key => $value) {
if ($key != 'GOOGLE_URL' && $key != 'TIME') {
$tmp_attributes .= " " . $key . ": " . $value . " | ";
}
// In the TIME attribute, TBA means that the event doesn't have a time yet
// and a dash means that it is all day
// in both cases it should appear as an all day event to google
// so only the start date is needed
if ($key == 'TIME') {
if ($value != '') { $all_day_event = TRUE; }
}
if ($key == 'GOOGLE_URL') { $g_event['google_url'] = $value; }
}
}
//$g_event['desc'] = $event['event_notes'] . $tmp_attributes;
$g_event['desc'] = '';
// TITLE
$g_event['title'] = $event['event_name'] . " - " . $event['event_notes'] . $tmp_attributes;;
$g_event['title'] = strip_html_tags($g_event['title']);
// WHERE
if ($event['location_id'] != 0 && !empty($event['location_id'])) {
$tmp_location = eme_get_location($event['location_id']);
$g_event['where'] = $tmp_location['location_name'];
}
// WHEN
// SET ARIZONA TIME ZONE
$g_event['tzOffset'] = '-07';
$g_event['when']['allDay'] = FALSE;
// check for all day event
if (empty($event['event_end_date']) || ($event['event_start_time'] == $event['event_end_time'])
|| ($all_day_event)) {
$g_event['when']['allDay'] = TRUE;
}
$g_event['when']['startTime'] = substr($event['event_start_time'],0,5);
$g_event['when']['startDate'] = $event['event_start_date'];
$g_event['when']['endTime'] = substr($event['event_end_time'],0,5);
$g_event['when']['endDate'] = $event['event_end_date'];
return $g_event;
} // end function pdd_map_to_google
/**********************************************************************/
function pdd_standardize_event($event) {
// sometimes event is returned with the attributes serialized and sometimes not
// this will unserialize them
$event_attributes = array();
if (!empty($event['event_attributes']) && !is_array($event['event_attributes'])) {
$event_attributes = unserialize($event['event_attributes']);
$event['event_attributes'] = $event_attributes;
}
return $event;
} // end function pdd_standardize_event
/**********************************************************************/
function pdd_insert_google_event($event) {
// make sure the event is coming in a standard form
$event = pdd_standardize_event($event);
// change event to google_event
$g_event = pdd_map_to_google($event);
// add event to google and get the event ID back
$google_saved_event_url = pdd_insertGoogleEvent($g_event);
// add saved event url to Attributes
$event_attributes = array();
if (!empty($event['event_attributes']) && !is_array($event['event_attributes'])) {
$event_attributes = unserialize($event['event_attributes']);
}
$event_attributes['GOOGLE_URL'] = $google_saved_event_url;
$event['event_attributes'] = serialize($event_attributes);
$where = array('event_id' => $event['event_id']);
eme_db_update_event($event,$where);
} // end function pdd_insert_google_event
/**********************************************************************/
function pdd_update_google_event($event) {
print_r($event);
// make sure the event is coming in a standard form
$event = pdd_standardize_event($event);
// change event to google_event
$g_event = pdd_map_to_google($event);
// add event to google and get the event ID back
$google_saved_event_url = pdd_updateGoogleEvent($g_event);
} // end function pdd_update_google_event
/**********************************************************************/
function pdd_delete_google_event($event) {
// make sure the event is coming in a standard form
$event = pdd_standardize_event($event);
// connect to the calendar
$gdataCal = createGoogleCalConnection();
if ($gdataCal) {
// Retrieve the edit URL for an event. When using this method, this URL
// should already be available somewhere locally, such as in a database.
// PDD - saved in attributes
$google_saved_event_url = '';
if (!empty($event['event_attributes']['GOOGLE_URL'])) {
$google_saved_event_url = $event['event_attributes']['GOOGLE_URL'];
// retrieve event
// delete event
try {
$g_event = $gdataCal->getCalendarEventEntry($google_saved_event_url);
$g_event->delete();
} catch (Zend_Gdata_App_Exception $e) {
return "Error: " . $e->getResponse();
}
return 'Event successfully deleted!';
}
} else { echo "http client not defined"; }
} // end function pdd_delete_google_event
add_action('eme_insert_event_action','pdd_insert_google_event');
add_action('eme_update_event_action','pdd_update_google_event');
add_action('eme_delete_event_action','pdd_delete_google_event');
Hope that helps.