<?php
	// Config and functions
	include('includes/config.php');
	
	// General functions
	require($config['RootAddress'] ."/includes/scripts/general_functions.php");
?>
<?php echo"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
	<loc><?=$config['BaseAddress']?></loc>
	<changefreq>monthly</changefreq>
	<priority>1.0</priority>
</url>

<?php
	//Run some SQl
	$StaticPageSearchQ = "SELECT sp.static_page_id, sp.page_name AS `name`, sp.page_url, sp.breed_type_id, ";
	$StaticPageSearchQ .= "CASE WHEN sp.meta_description IS NOT NULL THEN sp.meta_description ELSE 'NOT SET' END AS meta_description, ";
	$StaticPageSearchQ .= "CASE WHEN sp.display_number IS NULL THEN 10000 ELSE sp.display_number END AS order_num, ";
	$StaticPageSearchQ .= "CASE WHEN sp.update_date IS NOT NULL THEN FROM_UNIXTIME(sp.update_date) ELSE 'Not Updated' END AS last_update, ";
	$StaticPageSearchQ .= "IF(spco.static_page_cms_option_id IS NOT NULL,1,0) AS show_profiles ";
	$StaticPageSearchQ .= "FROM (static_page sp) ";
	$StaticPageSearchQ .= "LEFT JOIN static_page_cms_option spco ON spco.static_page_id = sp.static_page_id AND spco.deleted_date IS NULL AND spco.cms_option_id = 3 ";
	$StaticPageSearchQ .= "WHERE sp.end_date IS NULL ";
	$StaticPageSearchQ .= "AND sp.master_static_page_id IS NULL ";
	$StaticPageSearchQ .= "AND sp.xml_sitemap_display = 1 ";
	$StaticPageSearchQ .= "ORDER BY order_num ASC, name ";
		
	//Running
	$StaticPageSearchR = mysql_query($StaticPageSearchQ);
	$StaticPageSearchNum = mysql_num_rows($StaticPageSearchR);
		
	// Error checking
	if(mysql_error()) {
		echo mysql_error() ."<br />". $StaticPageSearchQ;
	}

	####################################
	###### DO WE HAVE DATA? ############
	while($StaticPageData = mysql_fetch_assoc($StaticPageSearchR)) {

?>

<url>
	<loc><?=$config['BaseAddress']?>/<?=$StaticPageData['page_url'];?></loc>
	<priority>0.8</priority>
</url>

<?php
	// Did this have a link to the people profiles
	if($StaticPageData['show_profiles'] == 1) {
		
		$ProfileQ = "SELECT bp.beast_profile_id, bp.beast_name, ";
		$ProfileQ .= "CONCAT(mt.folder_path,'/',bpm.media_path) AS media_path, bpm.alt_tag ";
		$ProfileQ .= "FROM beast_profile bp ";
		$ProfileQ .= "LEFT JOIN beast_profile_media bpm ON bpm.beast_profile_id = bp.beast_profile_id AND bpm.deleted_date IS NULL AND bpm.is_main = 1 ";
		$ProfileQ .= "LEFT JOIN media_type mt ON bpm.media_type_id = mt.media_type_id ";
		$ProfileQ .= "WHERE bp.deleted_date IS NULL AND bp.non_stock_beast IS NULL ";
		
		if($StaticPageData['breed_type_id']) {
			$ProfileQ .= "AND bp.breed_type_id = ". $StaticPageData['breed_type_id'] ." ";
		}
		
		//Decide if it needs an extra clause
		if($StaticPageData['page_url'] == "for_sale.php") {
			$ProfileQ .= "AND bp.is_for_sale = 1 ";
		}
		if($StaticPageData['page_url'] == "semen.php") {
			$ProfileQ .= "AND bp.is_semen_page = 1 ";
		}

		$ProfileQ .= "ORDER BY bp.display_order, bp.beast_name ";
		$ProfileR = mysql_query($ProfileQ);
		$ProfileNum = mysql_num_rows($ProfileR);
		
		// Loop the profiles
		while($ProfileData = mysql_fetch_assoc($ProfileR)) {
	?>
	<url>
		<loc><?=$config['BaseAddress'];?>/stock/<?=makeProfilePageSEF($ProfileData['beast_name'],$ProfileData['beast_profile_id'],".php");?></loc>
		<priority>0.6</priority>
	</url>
	<?php
		} //END links to profiles
	} //END showing person profiles


		/// First set of sub-pages
		if(findChildPage($StaticPageData['static_page_id'],"xml_sitemap_count") > 0) {
			
			// Get the data
			$SubData = findChildPage($StaticPageData['static_page_id'],"xml_sitemap_data");
			
			//Loop it
			while($SubPage = mysql_fetch_assoc($SubData)) {

		?>
		
		<url>
			<loc><?=$config['BaseAddress'];?>/<?=makePageSEF($StaticPageData['name'],NULL);?>/<?=stripslashes($SubPage['page_url']);?></loc>
			<priority>0.6</priority>
		</url>
		
		<?php
			// Did this have a link to the people profiles
			if($SubPage['show_profiles'] == 1 && $SubPage['gender_type_id']) {
									
				$ProfileQ = "SELECT bp.beast_profile_id, bp.beast_name, ";
				$ProfileQ .= "CONCAT(mt.folder_path,'/',bpm.media_path) AS media_path, bpm.alt_tag ";
				$ProfileQ .= "FROM beast_profile bp ";
				$ProfileQ .= "LEFT JOIN beast_profile_media bpm ON bpm.beast_profile_id = bp.beast_profile_id AND bpm.deleted_date IS NULL AND bpm.is_main = 1 ";
				$ProfileQ .= "LEFT JOIN media_type mt ON bpm.media_type_id = mt.media_type_id ";
				$ProfileQ .= "WHERE bp.deleted_date IS NULL AND bp.non_stock_beast IS NULL ";
				
				if($SubPage['breed_type_id']) {
					$ProfileQ .= "AND bp.breed_type_id = ". $SubPage['breed_type_id'] ." ";
				}
				
				//Decide if it needs an extra clause
				if($SubPage['page_url'] == "for_sale.php") {
					$ProfileQ .= "AND bp.is_for_sale = 1 ";
				}
				if($SubPage['page_url'] == "semen.php") {
					$ProfileQ .= "AND bp.is_semen_page = 1 ";
				}
				
				$ProfileQ .= "AND bp.gender_type_id = ". $SubPage['gender_type_id'] ." ";
				$ProfileQ .= "ORDER BY bp.display_order, bp.beast_name ";
				$ProfileQ .= "LIMIT 60 ";
				$ProfileR = mysql_query($ProfileQ);
				$ProfileNum = mysql_num_rows($ProfileR);
				
				// Loop the profiles
				while($ProfileData = mysql_fetch_assoc($ProfileR)) {
			?>
			<url>
				<loc><?=$config['BaseAddress'];?>/stock/<?=makeProfilePageSEF($ProfileData['beast_name'],$ProfileData['beast_profile_id'],".php");?></loc>
				<priority>0.6</priority>
			</url>
			<?php
				} //END links to profiles
			} //END showing person profiles

				//// ARTICLE PAGE LOOPS
				// Possible one level furtehr of sub pages
				if(findChildPage($SubPage['static_page_id'],"xml_sitemap_count") > 0) {
					
					// Get the article based data
					$ArticleData = findChildPage($SubPage['static_page_id'],"xml_sitemap_data");
					
					//Loop it
					while($ArticlePage = mysql_fetch_assoc($ArticleData)) {
					
					?>
		
					<url>
						<loc><?=$config['BaseAddress'];?>/<?=makePageSEF($StaticPageData['name'],NULL);?>/<?=makePageSEF($SubPage['name'],NULL);?>/<?=stripslashes($ArticlePage['page_url']);?></loc>
						<priority>0.4</priority>
					</url>
					
					<?php
					
					} //END article page loops
				} // END had further child pages
		
			} //END first sub -page loop
			
		} // END searching for sub pages

	} //End the loop
?>

</urlset>