Unfortunately, this snippet only works with English feeds. This snippet has definite downfalls in that it tries to parse the plain text and doesn’t take advantage of the tags provided in the feed. At one time Kyle was talking about redoing this snippet utilizing FeedX. If that is done then this snippet should be more international friendly as the tags don’t change with the language being used. Regrettably, I don’t have the time to do this right now. Kyle, have you made any headway in this area?
I can see from the debug output, that this snippet is having difficulty parsing the date when it spans over multiple days. Try changing all these to multiple single day events (especially since you are using calendar output).
I have pulled out the English constants, and you can translate them, and that will get you started. I would also guess you need to change the function "strtotime" to "strftime" as "strtotime" requires an English date string. Once you get the date logic figured out, I think the rest should all work.
<?php
// Resource Name: GoogleEvents
// Author: Jesse Rochman
// Forum Alias: jesster444
// Version: 1.3.5
// Compatible with .951
// Released: 1/9/2007
// Modified: 3/1/2007 by KyleJ
// Modified: 3/2/2007 by Jesse
// Modified: 4/14/2007 by Jesse
// Inspired by James Cridlands Google Calendar Code
//Calendar Class is from Giorgos Tsiledakis Active Calendar
//Language Strings -- MUST TRANSLATE FOR SNIPPET TO WORK
$WHEN_TEXT = 'When:';
$WHERE_TEXT = 'Where:';
$EVENT_DESCRIPTION_TEXT = 'Event Description: ';
$CHARACTERS_IN_EVENT_DESCRIPTION_TEXT = 18;
//Some General Configurations
$basePath = $modx->config['base_path'];
$siteURL = $modx->config['site_url'];
$VERSION = '1.3.5';
// Your Gmail address
$email = isset($email)? $email : "[email protected]";
//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";
// 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 : 0;
//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
include_once ($basePath.'assets/snippets/GoogleEvents/php/simplepie.inc');
//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;
//Config ends
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")));
if ($debug) {echo "<p>Version: $VERSION<br />Debug mode is on.</p>";}
$calendar_xml_address = "http://www.google.com/calendar/feeds/".$email."/public/basic?start-min=".date("Y-m-d").'&start-max='.$startMax.'&singleevents=true';
if ($debug) {echo "<p>Getting <a href='$calendar_xml_address'>$calendar_xml_address</a></p>
<p>Language Constants:<br />$WHERE_TEXT | $WHEN_TEXT | $EVENT_DESCRIPTION_TEXT | $CHARACTERS_IN_EVENT_DESCRIPTION_TEXT</p>";}
$feed = new SimplePie();
$feed->feed_url($calendar_xml_address);
$feed->cache_location($cache_location);
//$feed->enable_caching(FALSE);
//$feed->enable_xmldump(true);
$feed->init();
$feed->handle_content_type();
if($feed->get_items()) {
foreach($feed->get_items() as $item) {
//echo '<pre>' . print_r($item,1) . '</pre>';
$description = $item->get_description();
if ($description) {
if ($debug) { echo "<p><b>Description to be parsed:</b> <br />".$description."</p>"; }
//Remove some of the html entity oddities that Google produces like & which displays "&"
$description = str_replace ("&","&",$description);
$description = str_replace ("'","'",$description);
$description = str_replace (""",""",$description);
// Extract the 'when' information from the item summary, and ignore everything else
$from = trim(textBetween($WHEN_TEXT,"<",$description));
if (strlen($from) > 16) {
$from = trim(textBetween($WHEN_TEXT,"&",$description));
}
//Put all event information into an array
$from = str_replace(',','',$from);
$fromInfo = explode(" ",$from);
if (count($fromInfo) < 8 ) {
$useInfo = array(
'startDow' => $fromInfo[0],
'startMon' => $fromInfo[1],
'startDay' => $fromInfo[2],
'startYear' => $fromInfo[3],
'startTime' => $fromInfo[4],
'endDow' => $fromInfo[0],
'endMon' => $fromInfo[1],
'endDay' => $fromInfo[2],
'endYear' => $fromInfo[3],
'endTime' => $fromInfo[6],
);
} else {
$useInfo = array(
'startDow' => $fromInfo[0],
'startMon' => $fromInfo[1],
'startDay' => $fromInfo[2],
'startYear' => $fromInfo[3],
'startTime' => $fromInfo[4],
'endDow' => $fromInfo[6],
'endMon' => $fromInfo[7],
'endDay' => $fromInfo[8],
'endYear' => $fromInfo[9],
'endTime' => $fromInfo[10],
);
}
//Add a leading zero to the day if none
if (strlen($useInfo['startDay']) < 2) { $useInfo['startDay'] = "0{$useInfo['startDay']}"; }
if (strlen($useInfo['endDay']) < 2) { $useInfo['endDay'] = "0{$useInfo['endDay']}"; }
//Make start time include mintues and leading 0 if necessary for correct time calculation
if (strlen($useInfo['startTime']) == 3) { $useInfo['startTime'] = '0' . substr($useInfo['startTime'],0,1) . ':00' . substr($fromInfo['startTime'],1,2); }
if (strlen($useInfo['startTime']) == 4) { $useInfo['startTime'] = substr($useInfo['startTime'],0,2) . ':00' . substr($useInfo['startTime'],2,2); }
if (strlen($useInfo['startTime']) == 6) { $useInfo['startTime'] = '0' . $useInfo['startTime']; }
//Make end time include mintues and leading 0 if necessary for correct time calculation
if (strlen($useInfo['endTime']) == 3) { $useInfo['endTime'] = '0' . substr($useInfo['endTime'],0,1) . ':00' . substr($useInfo['endTime'],1,2); }
if (strlen($useInfo['endTime']) == 4) { $useInfo['endTime'] = substr($useInfo['endTime'],0,2) . ':00' . substr($useInfo['endTime'],2,2); }
if (strlen($useInfo['endTime']) == 6) { $useInfo['endTime'] = '0' . $useInfo['endTime']; }
//Convert start date & time to a unix time stamp & fill other vars
$tempStartDate = strtotime("{$useInfo['startDay']} {$useInfo['startMon']} {$useInfo['startYear']} {$useInfo['startTime']}");
$startDate = date($dateFormat,$tempStartDate);
$startDay = $useInfo['startDay'];
$startYear = $useInfo['startYear'];
$startTime = $useInfo['startTime'] ? date($timeFormat,$tempStartDate) : '';
$startMonthCal = date('n',$tempStartDate);
//Convert end date & time to a unix time stamp & fill other vars
$tempEndTime = strtotime("{$useInfo['endDay']} {$useInfo['endMon']} {$useInfo['endYear']} {$useInfo['endTime']}");
$endDate = date($dateFormat,$tempEndTime);
$endTime = $useInfo['endTime'] ? date($timeFormat,$tempEndTime) : '';
$time = $tempStartDate;
if ($debug) {
echo "<p><b>Atempting to parse the following date:</b><br />$from</p>
<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? $time</p>";
}
// Extract the 'where' information from the item summary, and ignore everything else
$where = textBetween($WHERE_TEXT,"<",$description);
if (empty ($where)) $where = '';
if ($debug) {
echo "<p><b>This is what I think the "Where" string should be:</b><br />$where</p>";
}
// Extract the "Event Description" information from the item summary, and ignore everything else
if (strpos($description,$EVENT_DESCRIPTION_TEXT)) {
$eventDescription = substr($description,strpos($description,$EVENT_DESCRIPTION_TEXT)+$CHARACTERS_IN_EVENT_DESCRIPTION_TEXT);
} else {
$eventDescription = '';
}
// Extract the link for Google Calendar
$url=$item->get_links();
// Stick into a nice array if it has a valid timestamp (not a recurring event).
if ($time > 0) {
$events[]=array('time'=>$time,'where'=>$where,'title'=>$item->get_title(),'url'=>$url[0],'eventDescription'=>$eventDescription,'startDate'=>$startDate,'startTime'=>$startTime,'endDate'=>$endDate,'endTime'=>$endTime,'startYear'=>$startYear,'startDay'=>$startDay,'startMonthCal'=>$startMonthCal);
}
}
}
// Sort the bookings into time order
if (!$order){
// 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);
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 "<p>The feed is unavailable. This could be caused by the server being down, an incorrect feed address, or if you have no events in your feed. The feed address you tried to load was <a href='$calendar_xml_address'>$calendar_xml_address</a>.</p>";
}
function textBetween($s1,$s2,$s){
$s1 = strtolower($s1);
$s2 = strtolower($s2);
$L1 = strlen($s1);
$scheck = strtolower($s);
if($L1>0){$pos1 = strpos($scheck,$s1);} else {$pos1=0;}
if($pos1 !== false){
if($s2 == '') return substr($s,$pos1+$L1);
$pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
if($pos2!==false) return substr($s,$pos1+$L1,$pos2);
}
return '';
}
?>