Skip to content

Instantly share code, notes, and snippets.

@dmolsen
Created December 15, 2010 18:44
Show Gist options
  • Save dmolsen/742403 to your computer and use it in GitHub Desktop.
Save dmolsen/742403 to your computer and use it in GitHub Desktop.
This shows an example adapter class for including content in the calendar module from Google Calendar. I've ripped out most of the generic functions just because it's long.
<?php
/**
* Copyright (c) 2010 West Virginia University
*
* Licensed under the MIT License
* Redistributions of files must retain the above copyright notice.
*
*/
// set-up Zend gData
$path = $install_path.'lib/ZendGdata-1.8.4PL1/library';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
class CalendarAdapter extends ModuleAdapter {
// standardized method to set-up connection with Google Cal
private static function setUpConnection() {
# credentials for the google calendar
$username = "";
$password = "";
# connection method
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($username.'@gmail.com',$password,$service);
$gdataCal = new Zend_Gdata_Calendar($client);
return $gdataCal;
}
// match the feed from Google Calendar with the actual key names we use in templates
private static function convertFeed($eventFeed) {
$convertedFeed = array();
foreach ($eventFeed as $event) {
$id = $event->id->text;
$when = $event->getWhen();
$startTime = $when[0]->startTime;
$endTime = $when[0]->endTime;
$date_str = strftime('%A, %B %e, %Y',strtotime($startTime));
$date_str_for_storage = strftime('%D',strtotime($startTime));
$date_str_for_compare = strftime('%Y%m%d',strtotime($startTime));
if (!(strlen($startTime) == 10)) {
$time_of_day = strftime('%l:%M%P',strtotime($startTime));
if ($endTime != '') {
$time_of_day .= "-".strftime('%l:%M%P',strtotime($endTime));
}
}
$title = $event->title->text;
$description = $event->getContent()->text;
list($description,$event_link) = getExtraData($description,'link',true);
list($description,$contact_phone) = getExtraData($description,'contact_phone',false);
list($description,$contact_email) = getExtraData($description,'contact_email',false);
list($description,$contact_name) = getExtraData($description,'contact_name',false);
$where = $event->getWhere();
$where = $where->valueString;
$convertedFeed[] = array('id' => $id,
'title' => $title,
'starttime' => $startTime,
'endtime' => $endTime,
'datefull' => $date_str,
'timefull' => $time_of_day,
'datestorage' => $date_str_for_storage,
'datecompare' => $date_str_for_compare,
'where' => $where,
'description' => $description,
'link' => $event_link,
'contactphone' => $contact_phone,
'contactemail' => $contact_email,
'contactname' => $contact_name);
}
return $convertedFeed;
}
// get all of the events for the supplied category
public static function getCategoryEvents($categoryid) {
// set-up an authenticated object with Google Calendar for the search
$gdataCal = self::setUpConnection();
$query = $gdataCal->newEventQuery();
$query->setUser($categoryid); // have to figure out how to do IDs
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setSortorder('a');
$query->setmaxresults('30');
try {
$eventFeed = $gdataCal->getCalendarEventFeed($query);
} catch (Exception $e) {
return false;
}
// convert the data returned from Google Calendar to the required format
$convertedFeed = self::convertFeed($eventFeed);
return $convertedFeed;
}
public static function getDayEvents($starttime,$endtime) {
// functionality here
}
public static function searchEvents($query) {
// functionality here
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment