< Back to Code Samples
<?php
/****************************************************
* SCRIPT INCLUDE - Gets events feed from events.iu.edu and local DB
* USAGE: For best results, acquire content via AJAX requests.
* OUTPUT: The events block content, like this:
* <h2>Events</h2>
* <ol>
* <li>
* <a href="THE URL" title="DESCRIPTION">EVENT NAME</a>
* <div class="date">
* <span class="month">Date: THE DATE<br> Time: THE TIME <br></span>
</div>
* </li> (repeat as necessary)
* </ol>
* <p class="more"><a href="http://events.iu.edu/iue.shtml">More Events ></a></p><br>
* <a style="padding-left: 20px;" href="RSS LINK" title="Subscribe to the Events Feed">
* <img src="/pub/img/subscribe.jpg" alt="Subscribe" longdesc="Subscribe to the IU East Events Feed. This requires an RSS Feedreader.">
* </a>
*
****************************************************/
// CONSTANTS ///////////////////////
define("RSS_URL", 'http://events.iu.edu/webevent.cgi?cmd=rss_export;y=' . date('Y') . ';calID=334158;days=30');
define("OUTPUT_FILE", "eventfeed.xml"); // cache file.
define("WWWROOT", "PATH"); // the document root
define("MAX_EVENTS",4);
define('EVENT_FORMAT', '
<li><a href="%1$s" title="%2$s">%3$s</a>
<div class="date">
<span class="month">%4$s %5$s</span>
</div>
</li>
');
////////////////////////////////////
/*
* First, if the cache file does not exist, then immediately pull down the event feed.
*/
if (!file_exists(OUTPUT_FILE))
{
getEventFeed();
}
/*
* Otherwise, compare dates. If it hasn't been updated since yesterday, OR a manual reset is
* requested, then refresh the file.
*/
else {
$stats = stat(OUTPUT_FILE);
if (date('Y-m-d',$stats["mtime"]) < date('Y-m-d') || isset($_GET['reset']))
{ // Only get the event feed if a day has passed since the last one.
getEventFeed();
}
}
/*
* This next part is the literal portion that is actually outputted.
*/
?>
<!-- BEGIN Eventfeed -->
<h2>Events</h2>
<ol>
<?php
getEvents();
?>
</ol>
<p class="more"><a href="http://events.iu.edu/iue.shtml">More Events ></a></p><br />
<a style="padding-left: 20px;" href="http://events.iu.edu/webevent.cgi?cmd=rss_export;calID=334158;days=1" title="Subscribe to the Events Feed"><img src="/pub/img/subscribe.jpg" alt="Subscribe" longdesc="Subscribe to the IU East Events Feed. This requires an RSS Feedreader." /></a>
<!-- END EVENTS -->
<?php
/* ***********************************************************************
* Functions & Classes used here.
* *********************************************************************** */
//
// This whole set of XML parsing functions should *probably* be
// made into a class, but right now I just really need a working
// RSS parser. - AMH (9/6/07)
//
// Updated to use the XML Reader functions. Original code stripped.
// Renders much more reliably. - AMH (4/18/08)
//
/**
* Read the events feed from the XML file, and print it out.
* @see The events feed, printed as list items, according to EVENT_FORMAT constant.
*/
function getEvents() {
$depth = 0;
$xml = new XMLReader();
$xml->open(OUTPUT_FILE);
$items = array();
$current = "";
$count = 0;
while ($xml->read())
{
switch($xml->localName) {
case "item":
$count++;
if ($current == "") { $current = array(); }
if ($count %2 == 0) { array_push($items, $current); }
//else
break;
case "pubDate":
case "title":
case "event_link":
case "author":
if ($current == "") { continue; }
if ($xml->nodeType == XMLReader::ELEMENT) {
$node = $xml->expand();
$current[$xml->localName] = $node->textContent;
}
break;
case "description":
if ($xml->nodeType == XMLReader::ELEMENT) {
$node = $xml->expand();
$temp = new description($node->textContent);
$current[$xml->localName] = $temp;
}
break;
default:
// skip...
break;
}
} // (while) iterate through xml
$xml->close(); // all finished!
//
// $seen is used to determine whether an event has already been printed or not, to
// prevent duplicates. The university RSS events feed (not under our control) stupidly
// prints and re-prints the same event over and over again each time it occurs. This
// is a simply hack to ensure that all the events listed through this function are
// uniquely named, skipping the rest.
//
$seen = array();
foreach($items as $item) {
// Seen it? Skip it!
if (in_array($item['title'], $seen)) continue;
array_push($seen, $item['title']);
// Print it out according to EVENT_FORMAT.
printf(EVENT_FORMAT,
$item['event_link'],
$item['description']->getDesc(),
$item['title'],
$item['description']->getDate(),
$item['description']->getTime());
}
}
/**
* getEventFeed uses the cURL library to pull down the RSS feed from our centralized events
* database. They have not yet integrated web services into their software (peopleCube or something)
* so we still have to do it this way.
*/
function getEventFeed() {
$cp = curl_init(RSS_URL);
$fp = fopen(OUTPUT_FILE, "w");
//curl_setopt($cp, CURLOPT_FILE, $fp);
curl_setopt($cp, CURLOPT_HEADER, 0);
curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($cp);
// Strip out silly MS quotes.
$output = preg_replace('/[“”]/', '"', $output);
// Strip out extraneous characters.
$output = preg_replace('/[^s -~]/', '', $output);
// Write it to file.
fwrite($fp, $output);
// Close up shop.
curl_close($cp);
fclose($fp);
}
/**
* Description class. This class is used for storing the description information. It's kind
* of a hodge-podge blob and needs some formatting in order to be displayed correctly. This
* Class makes the code above look prettier. Thanks class!
*
*/
class description {
var $date;
var $time;
var $desc;
var $cost;
function __construct($theText) {
$theText = str_replace("<", "<", $theText);
$theText = str_replace(">", ">", $theText);
$theText = ereg_replace("[\r|\n]","",$theText);
$theText = str_replace('"', "'", $theText);
$temp = split("<delim/>",$theText);
$this->date = (count($temp) > 0) ? array_shift($temp) : "";
$this->time = (count($temp) > 0) ? array_shift($temp) : "";
$this->desc = (count($temp) > 0) ? array_shift($temp) : "";
$this->cost = (count($temp) > 0) ? array_shift($temp) : "";
}
function getDate() {
return $this->date;
}
function getTime() {
return $this->time;
}
function getDesc() {
return $this->desc;
}
function getCost() {
return $this->cost;
}
}
?>