Created
March 29, 2012 14:05
-
-
Save brendo/2237764 to your computer and use it in GitHub Desktop.
Google Calendar Event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => '[email protected]'), | |
'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