<?php 

// This file parses event data from the App/Submit database and converts it to XML/RSS
// www.catoegroup.com

header("Content-type: text/xml"); 

$host = "localhost"; 
$user = "fnjforms"; 
$pass = "K4YoEfgpmN9X"; 
$database = "fnjapps_forms"; 

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database."); 

// old query: $query = "SELECT * FROM ap_form_2 WHERE element_6_1='1' ORDER BY element_1 DESC LIMIT 4"; 
$query = "SELECT * FROM ap_form_2 WHERE element_1>=CURDATE() AND element_6_1='1' ORDER BY element_1 ASC LIMIT 4";
$resultID = mysql_query($query, $linkID) or die("Data not found."); 
$num_rows = mysql_num_rows($resultID);

function format_phone($phone)
{
	$phone = preg_replace("/[^0-9]/", "", $phone);

	if(strlen($phone) == 7)
		return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
	elseif(strlen($phone) == 10)
		return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
	else
		return $phone;
}

$xml_output .= "<?xml version=\"1.0\"?>\n"; 
$xml_output .= "<rss version=\"2.0\">\n";
$xml_output .= " <channel>\n";
$xml_output .= "\t<title>Team FNJ Events</title>\n";
$xml_output .= "\t<link>http://www.fatandjuicy.com/calendar</link>\n";
$xml_output .= "\t<description> ... happening this month at Fat and Juicy!</description>\n";

if ($num_rows > 0) {
	for($x = 0 ; $x < mysql_num_rows($resultID); $x++){ 
			$row = mysql_fetch_assoc($resultID);
	//        // Escaping illegal characters 
				$row['element2'] = str_replace( array("’", "+", "®", "–", "—", "“", "”"), array("'", "and", "", "-", "-", "", ""), $row['element_2']); 
				$row_title = str_replace('+','',$row['element_4']);
	//        $row['head'] = str_replace("<", "<", $row['element_10']); 
	//        $row['head'] = str_replace(">", "&gt;", $row['element_10']); 
	//        $row['head'] = str_replace("\"", "&quot;", $row['element_10']);
			
		$xml_output .= "\t<item>\n"; 
		$xml_output .= "\t\t<pubDate>". date('m.d.y', strtotime($row['element_1'])) ."</pubDate>\n";
		$xml_output .= "\t\t<title><![CDATA[". $row_title ."]]></title>\n";
		$xml_output .= "\t\t<link>http://www.fatandjuicy.com/app/calendar/events-detail.php?event_id=". $row['id'] ."</link>\n";
		$xml_output .= "\t\t<description>". date('m.d.y', strtotime($row['element_1'])) ."</description>\n";
		$xml_output .= "\t\t<content>". date('l, F jS Y', strtotime($row['element_1'])) ." - <![CDATA[". htmlspecialchars($row['element2']) ."]]></content>\n";
		$xml_output .= "\t</item>\n"; 
	} 
} else {
		$xml_output .= "<item><title>Coming Soon</title><link>http://www.fatandjuicy.com/retail</link><description>We're cooking up some plans.... Oh yeah!</description><content>We're cooking up some plans.... Oh yeah!</content></item>";
}

$xml_output .= " </channel>\n";
$xml_output .= "</rss>\n";

echo $xml_output; 

mysql_close($linkID);

?> 