<? 

// Replace the auto header
header('Content-type: text/xml'); 

// You gotta have this file to connect to the database
// and provide all the default RSS values
@include('init_rss.php');


// FUNCTION FOR GRABBING COUNTRIES
function convertCountry($which) {
	// Query the database for country list
	$country_query = "SELECT name FROM hero_countries WHERE id = '".$which."'";
	$country_result = mysql_query($country_query);
	$country_array = mysql_fetch_array($country_result);
	return $country_array[0];
}// end function: convertCountry()



echo '<?xml version="1.0"?>';

?>
<rss version="2.0">
<channel>
	<title>-- WARM ELECTRONIC RECORDINGS -- TOUR</title>
	<link>http://www.thewarmsupercomputer.com/index_testor.php</link>
	<description>The only place to go on the internet for YOUR BAND's tour dates.</description>
	<?
	
	// PRETEND THAT THE FEED IS PUBLISHED DAILY
	$pub_date = date("D, d M Y H:i:s T", (time() - (24 * 60 * 60)));
	echo '<pubDate>'.$pub_date.'</pubDate>';
	
	
	// DISCOVER THE LAST PUBLISH DATE...
	$the_q = "SELECT id,date_posted FROM hero_tours_dates WHERE status = 2 ORDER BY date_posted DESC LIMIT 2";
	$the_r = mysql_query($the_q);
	$last_build_date = date("D, d M Y H:i:s T",  mysql_result($the_r,0,'date_posted')   );
	
	echo '<lastBuildDate>'.$last_build_date.'</lastBuildDate>';
	
	
	echo '<language>'.$rss_lang.'</language>';
	echo '<copyright>'.$rss_copyright.'</copyright>';
	echo '<managingEditor>'.$rss_managing_editor.'</managingEditor>';
	echo '<webMaster>'.$rss_webmaster.'</webMaster>';
	echo '<generator>'.$rss_generator.'</generator>';
	
	if (isset($rss_category)) {
		echo '<category>'.$rss_category.'</category>';
	}
	if (isset($rss_time_to_live)) {
		echo '<ttl>'.$rss_time_to_live.'</ttl>';
	}
	
	
	
	$the_q = "SELECT * FROM hero_tours_dates WHERE status = 2 AND date > '".time()."' ORDER BY date DESC LIMIT 50";
	$the_r = mysql_query($the_q);
	$the_total = @mysql_num_rows($the_r);
	
	// run loop
	for ($i=0; $i < $the_total; $i++) {
	
		// POPULATE VARIABLES
		$cur = mysql_fetch_array($the_r);
		
		
		// CHECK TO MAKE SURE THAT THE TOUR GROUP IS APPROVED
		$group_q = "SELECT id, name FROM hero_tours WHERE id = '".$cur['tour_id']."' AND status = '2' AND archive_status = '0'";
		$group_r = mysql_query($group_q);
		
		if (@mysql_num_rows($group_r) > 0) {
			
			
			// id
			$cur_id = $cur['id'];
			
			// date
			$cur_date = date("D, d M Y H:i:s T",$cur["date_posted"]);
			
			// tour name
			$cur_name = htmlentities(strip_tags(stripslashes(  mysql_result($group_r,0,'name')   )));
			
			
			
			
			// POPULATE TOUR VARIABLES *******************************************
				$tour_date = date("M. d, Y",$cur["date"]);
				
				$tour_city = htmlentities(stripslashes($cur["city"]));
				
				$tour_state = htmlentities(stripslashes($cur["state"]));
				
				$tour_country = convertCountry($cur["country"]);
				
				$tour_venue = htmlentities(stripslashes($cur["venue"]));
				
				$tour_venue_link = htmlentities(stripslashes($cur["venue_link"]));
				
				$tour_description = stripslashes($cur['description']);
				
				$tour_purchase_tix = htmlentities(stripslashes($cur["link"]));
				
				$tour_soldout = $cur['sold_out'];
			// END TOUR VARIABLES ************************************************
			
			
			
			echo "<item>";
			
			echo '<title>'. $cur_name . ' - ' .$tour_date . ' - ' . $tour_city;
			
			if ($tour_state != '') { echo ', '.$tour_state; }
			
			if ($tour_country != 'United States') {
				echo ', '.$tour_country;
			}
			
			//echo ' - '.$cur_name;
			
			echo '</title>';
			
			echo '<link>http://www.thewarmsupercomputer.com/tour.php</link>';
			
			echo '<description>';
			
			$desc_string = '';
			
			if ($tour_venue != '') {
				
				$desc_string .= 'VENUE:<br />';
				
				if ($tour_venue_link != '' && $tour_venue_link != 'http://') {
					$desc_string .= '<a href="'.$tour_venue_link.'" target="_blank">';
				}
				
				$desc_string .= $tour_venue;
				
				if ($tour_venue_link != '' && $tour_venue_link != 'http://') {
					$desc_string .= '</a>';
				}
			}
			
			if ($tour_description != '') {
			
				$desc_string .= '<br /><br />ADDITIONAL INFO:<br />' . $tour_description; 
			
			}
			
			
			if ($tour_soldout != 1 && $tour_purchase_tix != '' && $tour_purchase_tix != 'http://') {
				$desc_string .= '<br /><br /><a href="'.$tour_purchase_tix.'" target="_blank">Click to purchase tickets</a>.';
			}


			
			echo htmlentities($desc_string);
			
			echo '</description>';
				 
			echo '<pubDate>'.$cur_date.'</pubDate>';
				 
			echo '<source url="http://www.thewarmsupercomputer.com/rss_feeds/tour.xml">YOUR BAND - TOUR</source>';
			
			echo "</item>";
			
		} // end if	
	}// end for
	
	?>
</channel>
</rss>
