I’ve made some modifications to this snippet if anyone is interested.
1. Most notably I’ve changed the &email parameter to &calAddress. This allows the snippet to support private calendars. Using the modified snippet you simply grab the XML url of your calendar (which is found at the bottom of the calendar details tab in your Google calendar settings) and past it into the &calAddress parameter. Multiple addresses can be separated by commas.
2. I’ve also added a &cacheDuration parameter to allow users to easily change the default cache duration of simple pie. If you don’t want to wait 60 minutes for your calendar’s feed to be reread set this to something smaller. The time is in seconds so, &cacheDuration=`30` would make the cache only last for 30 seconds (great for testing... useless for deployment: try something more like &cacheDuration=`900`, which is 15minutes.)
<?php
// Resource Name: GoogleEvents
// Author: Jesse Rochman
// Forum Alias: Jesse R.
// Version: 1.4.2
// Compatible with .961
// Released: 1/9/2007
// Modified: 3/1/2007 by KyleJ
// Modified: 3/2/2007 by Jesse
//Modified: 11/19/2007 by Jesse
//Modified: 1/9/2008 by Jesse
//Modified: 12/18/2008 by Brandon Booth ([email protected]) added &cacheDuration parameter to allow setting of the cache duration. Defaults to 30 minutes
// Changed &email parameter to &calAddress parameter so that private addresses could be accessed. Instead of putting your email address in
// Now you insert the full address of your calendar. This is found in the calendar details tab of your calendar settings. Simply click on the XML button
// of either your public or private calendar address near the bottom. Copy the url and past it into this variable. Multiple addresses can be separated by commas (,)
// Inspired by James Cridlands Google Calendar Code
//Calendar Class is from Giorgos Tsiledakis Active Calendar
$basePath = $modx->config['base_path'];
$siteURL = $modx->config['site_url'];
//Set your google calendar address
$calAddress = isset($calAddress)? $calAddress : '';
//Set month and day names. You can translate here.
$dayNames1 = array('S','M','T','W','T','F','S');
$dayNames3 = array('Sun','Mon','Tue','Wed','Thur','Fri','Sat');
$dayNamesFull = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$monthNames = array('January','February','March','April','May','June','July','August','September','October','November','December');
if (!isset ($dayNameSize)) {
$dayNameSize = $dayNames3;
} elseif (strtolower($dayNameSize) == 'full') {
$dayNameSize = $dayNamesFull;
} elseif ($dayNameSize == 1) {
$dayNameSize = $dayNames1;
} else {
$dayNameSize = $dayNames3;
}
//This will determine whether to output the calendar, the list, or both.
$outputType = isset($outputType)? $outputType : "both";
if(isset($gInternational) && $gInternational) {
$dateFormat = isset($dateFormat)? $dateFormat : "%D"; // mm/dd/yy see http://us2.php.net/strftime for details.
$timeFormat = isset($timeFormat)? $timeFormat : "%T";
} else {
// Format for date and time
$dateFormat = isset($dateFormat)? $dateFormat : "l, F, jS, Y"; // Thursday, 10 March - see http://www.php.net/date for details
$timeFormat = isset($timeFormat)? $timeFormat : "g:ia";
}
//Order of Output. True for Reverse Chronilogical and False for Chronilogical
$order = isset($order)? $order : 'a';
//Show Events For This Many Days Out
$lastEvent = isset($lastEvent)? $lastEvent : 30;
//Show only this many events
$maxEvents = isset($maxEvents)? $maxEvents : 30;
//Skip this many events. This way you can make two snippet calls and jump around
$skipEvents = isset($skipEvents)? $skipEvents : 0;
// Cache location for your XML file
$cache_location= $basePath.'assets/snippets/GoogleEvents/cache';
// Here is where your copy of Simplepie lives
require_once ($basePath.'assets/snippets/GoogleEvents/php/simplepie.inc');
// Here is where included functions get added
include_once ($basePath.'assets/snippets/GoogleEvents/includes/GoogleEvents.inc.php');
//Sets a big or small calendar
$calendarType = isset($calendarType)? $calendarType : '';
//Determine the first day of the week
$firstDay = isset($firstDay)? $firstDay : 0;
// Change this to 'true' to see some debug code
$debug = isset($debug)? $debug : 0;
$cacheDuration = isset($cacheDuration) ? $cacheDuration : '1800';
//Config ends
//Template if the feed is empty or unavailable
$tplEmpty = isset($tplEmpty)? $modx->getChunk($tplEmpty) : "<p>The feed is unavailable. This could be caused by the server being down, an incorrect feed address, all your events are in the past, or if you have no events in your feed.</p>";
if ($outputType == 'calendar' || $outputType == 'both') {
include_once('assets/snippets/GoogleEvents/source/activecalendar.php');
//For the calendar script
//Variables for Calendar
$myurl='?id='.$modx->documentIdentifier; // the links url is this page
$yearID=false; // init false to display current year
$monthID=false; // init false to display current month
$dayID=false; // init false to display current day
extract($_GET); // get the new values (if any) of $yearID,$monthID,$dayID
$arrowBack="<img src=\"assets/snippets/GoogleEvents/data/img/back.png\" alt=\"<<\" />"; // use png arrow back
$arrowForw="<img src=\"assets/snippets/GoogleEvents/data/img/forward.png\" alt=\">>\" />"; // use png arrow forward
$cal = new activeCalendar($yearID,$monthID,$dayID);
}
if ($outputType == 'list' || $outputType == 'both') {
//Grab the templates. If one isn't passed to the program, then setup a default template.
$defaultTemplate = '<div><h2><a href="[+linkURL+]">[+title+]</a></h2><p>[+startDate+] at [+startTime+]<br />Description: [+content+]</p></div>';
$numberOfEvents = 0;
$tpl = $modx->getChunk($tpl);
$tplOddRow = $modx->getChunk($tplOddRow);
$tplEvenRow = $modx->getChunk($tplEvenRow);
$tplFirstRow = $modx->getChunk($tplFirstRow);
$tplLastRow = $modx->getChunk($tplLastRow);
if ($tpl) {
$defaultTemplate = $tpl;
}
if (!$tplOddRow) {
$tplOddRow = $defaultTemplate;
}
if (!$tplEvenRow) {
$tplEvenRow = $defaultTemplate;
}
if (!$tplFirstRow) {
$tplFirstRow = $defaultTemplate;
}
if (!$tplLastRow) {
$tplLastRow = $defaultTemplate;
}
}
$startMax = date('Y-m-d',mktime(0, 0, 0, date("m") , date("d")+$lastEvent, date("Y")));
$calAddresses = explode(',',$calAddress);
$tempCounter = 0;
$addresses = array();
foreach ($calAddresses as $addy) {
//replace 'basic' at the end of all google cal feeds with 'full' so that the formatting string will work.
$addy = substr_replace($addy,'full',-5,5);
$addresses[$tempCounter] = $addy."?start-min=".date("Y-m-d").'&start-max='.$startMax."&orderby=starttime&sortorder=$order&singleevents=true";
$addressDebug .= '<a href="' . $addresses[$tempCounter] . '">' . $addresses[$tempCounter] . '</a> ';
$tempCounter++;
}
//Template if the feed is empty or unavailable
$emptyTpl = $modx->getChunk($tplEmpty);
echo $emptyTpl;
if (!$tplEmpty) $tplEmpty = "<p>The feed is unavailable. This could be caused by the server being down, an incorrect feed address, all your events are in the past, or if you have no events in your feed. The feed(s) you tried to access include: $addressDebug</p>";
$feed = new SimplePie();
if ($debug) {
echo "<p>Debug mode is on.</p>";
foreach ($addresses as $address) {
echo "<p>Getting <a href='$address'>XML feed</a></p>";
}
$feed->enable_cache(false);
}
$feed->set_feed_url($addresses);
$feed->set_cache_location($cache_location);
$feed->set_cache_duration($cacheDuration);
$feed->enable_order_by_date(false);
$feed->init();
$feed->handle_content_type();
$temp = array();
if($feed->get_items()) {
foreach($feed->get_items() as $item) {
// We want to grab the Google-namespaced <gd:when> tag.
$when = $item->get_item_tags('http://schemas.google.com/g/2005', 'when');
// Once we grab the tag, let's parse it.
$startDate = $when[0]['attribs']['']['startTime'];
$sortDate = getGoogleDate($startDate);
if(isset($gInternational) && $gInternational) {
$startDate = strftime($dateFormat,$sortDate);
$startTime = strftime($timeFormat, $sortDate);
} else {
$startDate = date($dateFormat,$sortDate);
$startTime = date($timeFormat, $sortDate);
}
$startYear = date('Y',$sortDate);
$startMonth = date('n',$sortDate);
$startDay = date('j',$sortDate);
$endDate = $when[0]['attribs']['']['endTime'];
$tempEndDate = getGoogleDate($endDate);
if(isset($gInternational) && $gInternational) {
$endDate = strftime($dateFormat,$tempEndDate);
$endTime = strftime($timeFormat, $tempEndDate);
} else {
$endDate = date($dateFormat,$tempEndDate);
$endTime = date($timeFormat, $tempEndDate);
}
// Now, let's grab the Google-namespaced <gd:where> tag.
$gd_where = $item->get_item_tags('http://schemas.google.com/g/2005', 'where');
$where = $gd_where[0]['attribs']['']['valueString'];
// Extract the "Event Description" information from the item summary, and ignore everything else
$eventDescription = $item->get_description();
// Extract the link for Google Calendar
$url=$item->get_link();
if ($debug) {
echo "
<p><b>Parsed Date:</b><br />
Starting Day: $startDate<br />
Starting Time: $startTime<br />
Ending Day: $endDate<br />
Ending Time: $endTime<br />
Time Greater Than 0? $sortDate<br />
Description: $eventDescription<br />
URL:<a href=$url>$url</a></p>";
}
// Stick into a nice array if it has a valid timestamp (not a recurring event).
$events[]=array('time'=>$sortDate,'where'=>$where,'title'=>$item->get_title(),'url'=>$url,'eventDescription'=>$eventDescription,'startDate'=>$startDate,'startTime'=>$startTime,'endDate'=>$endDate,'endTime'=>$endTime,'startYear'=>$startYear,'startDay'=>$startDay,'startMonthCal'=>$startMonth,'endTimeStamp'=>$tempEndDate);
}
// Sort the bookings into time order
if ($order != "d"){
// Sort the events into chronilogical order.
sort($events);
} else {
rsort($events);
}
$counterMaxEvents = 0;
$counterSkipEvents = 0;
foreach($events as $event) {
if ($outputType == 'list' || $outputType == 'both') {
$numberOfEvents++;
if ($counterSkipEvents >= $skipEvents){
//Figure out what template to use
if($numberOfEvents == 1 || $counterSkipEvents == $skipEvents) {
$template = $tplFirstRow;
} elseif ($numberOfEvents == count($events) || $counterMaxEvents == ($maxEvents-1)) {
$template = $tplLastRow;
} elseif (($numberOfEvents & 1) == 0) {
$template = $tplEvenRow;
} else {
$template = $tplOddRow;
}
$counterSkipEvents++;
//Output the data in the appropriate template
if ($counterMaxEvents < $maxEvents) {
$output = str_replace('[+title+]',$event['title'],$template);
$output = str_replace('[+startDate+]',$event['startDate'],$output);
$output = str_replace('[+startTime+]',$event['startTime'],$output);
$output = str_replace('[+endDate+]',$event['endDate'],$output);
$output = str_replace('[+endTime+]',$event['endTime'],$output);
$output = str_replace('[+where+]',$event['where'],$output);
$output = str_replace('[+content+]',$event['eventDescription'],$output);
$output = str_replace('[+eventNumber+]',$numberOfEvents,$output);
$output = str_replace('[+linkURL+]',$event['url'],$output);
$output = str_replace('[+sTimestamp+]',$event['time'],$output);
$output = str_replace('[+eTimestamp+]',$event['endTimeStamp'],$output);
echo $output;
$counterMaxEvents++;
}
} else {
$counterSkipEvents++;
}
}
if($outputType == 'calendar' || $outputType == 'both') {
$cal->setEvent($event['startYear'],$event['startMonthCal'],$event['startDay'],"event",$event['url']);
if($calendarType == 'wide') {
$multipleLinesEvent=array($event['title'],$event['startTime'].'-'.$event['endTime']);
$cal->setEventContent($event['startYear'],$event['startMonthCal'],$event['startDay'],$multipleLinesEvent,$event['url']);
}
}
}
if($outputType == 'calendar' || $outputType == 'both') {
//outPutCalendar($fullMonths, $shortMonths,$mDays, $arrangedEvents);
$cal->setDayNames($dayNameSize);
$cal->setMonthNames($monthNames);
$cal->setFirstWeekDay($firstDay);
$cal->enableMonthNav($myurl,$arrowBack,$arrowForw); // enables navigation controls
print $cal->showMonth();
}
return '';
} else {
return $tplEmpty;
}
?>