Skip to content

Instantly share code, notes, and snippets.

@brendo
Created March 29, 2012 14:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendo/2237764 to your computer and use it in GitHub Desktop.
Save brendo/2237764 to your computer and use it in GitHub Desktop.
Google Calendar Event
<?php
require_once EXTENSIONS . '/xmlimporter/extension.driver.php';
require_once EXTENSIONS . '/google_calendar_api/lib/class.json_to_xml.php';
require_once TOOLKIT . '/class.gateway.php';
Class eventgoogle_calendar_import extends Event {
const ROOTELEMENT = 'google-calendar-import';
const GOOGLE_ENDPOINT = 'https://www.googleapis.com/calendar/v3/calendars/%s/events?fields=%s&key=%s';
const GOOGLE_COLOURS_ENDPOINT = 'https://www.googleapis.com/calendar/v3/colors?fields=event&key=%s';
public $eParamFILTERS = array(
'admin-only',
'xss-fail'
);
public static function about(){
return array(
'name' => 'Google Calendar: Import Events',
'author' => array(
'name' => 'Brendan Abbott',
'website' => 'http://localhost/aitc.qld.edu.au',
'email' => 'brendan@randb.com.au'),
'version' => 'Symphony 2.2.5',
'release-date' => '2012-02-14T11:46:32+00:00'
);
}
public static function getSource(){
return '20';
}
public static function documentation(){
return 'Pulls in the latest events for the given Calendar ID (described by <code>$_GET[\'calendar-id\']</code>)';
}
public function load() {
if(isset($_GET['calendar-id'])) {
return $this->__trigger();
}
}
protected function __trigger(){
// Get the JSON from the Google Calendar API and convert to XML
$return = new XMLElement(self::ROOTELEMENT);
try {
$ch = new Gateway;
$url = sprintf(self::GOOGLE_ENDPOINT,
$_GET['calendar-id'], // Calendar ID
'items(description,end,id,start,summary,updated,colorId)', // Fields
Symphony::Configuration()->get('app-key', 'google-calendar-api') // API KEY
);
$ch->init($url);
$result = $ch->exec();
$info = $ch->getInfoLast();
if($info['http_code'] === 200) {
// Get the colours
$ch->init(sprintf(self::GOOGLE_COLOURS_ENDPOINT,
Symphony::Configuration()->get('app-key', 'google-calendar-api') // API KEY
));
$colours = $ch->exec();
// Replace the colorId with the actual Hex code
$colours = json_decode($colours);
$events = json_decode($result);
foreach($events->items as $event) {
$event->colorId = $colours->event->{$event->colorId}->background;
// iCal standard adds 1 day for a range.
// This code will - 1 second to make the range correct
$event->start->date = date('Y-m-d H:i:s', strtotime($event->start->date));
$event->end->date = date('Y-m-d H:i:s', strtotime($event->end->date . ' - 1 second'));
}
$result = json_encode($events);
$data = Json_to_xml::convert($result, false);
$return->setValue($data);
}
else {
throw new Exception(sprintf(
'Failed to reach %s, code returned %d',
$_GET['calendar-id'],
$info['http_code']
));
}
}
catch (Exception $ex) {
$return->setAttribute('status', 'failed');
$return->setValue("Import Failed :: " . $ex->getMessage());
}
return $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment