We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37437
    • 147 Posts
    I just implemented the Google Events snippet on a site being driven by MODX 1.0.1. The snippet call is as follows:

    [!GoogleEvents?dayNameSize=`1`&outputType=`list`&maxEvents=`20`&email=`[email protected]`&tpl=`Upcoming2` &order=`d`&dateformat=`D,Mj`!]


    Although I use the &dateformat parameter in the snippet call (see above), I can not seem to get the date to format properly. I would like date information to read as so: ’Wed, Oct 28’. Any idea on what I may be doing incorrectly here?

    Also, when I make updates to my Google calendar, there is a delay of maybe an hour before the event changes are reflected in the website event list. Does anyone know why this happens and if there is a way to minimize or eliminate the delay?

    Thanks always for the great forum support.
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Do you have a link to the Snippet code?

      Can you confirm that the delay only occurs when you retrieve the data via this Snippet?
        • 37437
        • 147 Posts
        For sure, the delay takes place via the snippet. On the home page of the website, I utilize the Google Events snippet. If I make a change to my calendar, the calendar data that displays on the home page does not seem to sync with the calendar for an hour or so. I also have the actual calendar embedded on another page of the site. The data that displays in the embedded calendar is immediately current when I make any updates to the calendar.
          • 37437
          • 147 Posts
          @Everett - Not quite sure what you mean when you ask if I have a link to the snippet. The snippet call I am using is posted in my original question. On the site, I have the snippet call embedded in a chunk, which in turn, is placed in the appropriate template with a tag like this {{google}}.

          What I am actually most concerned about right now is trying to get the dates of the events to format properly. As I indicate and display in my original posting, I use the ’&dateformat’ parameter in the snippet call, but I can not seem to get the date to format as I would like. Any suggestions on this would be most appreciated because I am sort of at a loss.

          Thanks again.
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            marconi -- that’s the Snippet call you posted. I wanted to see the Snippet code. The PHP itself. That’s why I wanted a link to where I could look at the code. Then I could see how that Snippet call is doing what it’s doing.
              • 37437
              • 147 Posts
              sorry everett. i just used the default code that came with the snippet. it is below:

              <?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
              // 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'];
              
              // 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";
              
              
              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;
              
              //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")));
              
              $emails = explode(',',$email);
              $tempCounter = 0;
              $addresses = array();
              foreach ($emails as $addy) {
              	$addresses[$tempCounter] = "http://www.google.com/calendar/feeds/".$addy."/public/full?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->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;
              	}
              ?>