<?php
/*
 **************************************************************************************************
 *    Copyright (c) 2010-2012 Just Another Video script
 *
 * 	  This file is part of Just Another Video Script.
 *    Version 1.4.3
 *    info@justanothervideoscript.com
 **************************************************************************************************
 *    Come to website for support or buying additional plugins/modules.
 *    http://justanothervideoscript.com/
 **************************************************************************************************
 *    By using this software you agree that you have read and acknowledged our End-User License
 *    Agreement available at http://justanothervideoscript.com/license and can be bound by it.
 **********************************************************************************
 */
include_once("adminfunctions.inc");

if (isset($_POST['submit2'])) {
	$autositemap = $_POST['autositemap'] == 'on' ? 1 : 0 ;
	$db->query("UPDATE `setting` SET `value` = '".$autositemap."' WHERE `setting` = 'autositemap'") ;
}
$autositemap = getSetting(autositemap, $db) == 1 ? 'CHECKED' : '' ;

if (isset($_POST['submit'])) {
		$db->query("SELECT id,name FROM category") ;
		$rowCAT = $db->fetchAll() ;
		$numrows = $db->numrows() ;
		$submitmsg = '';
		$submitmsg .= "There are $numrows sitemaps made <br>" ;
			$siteoutput = '<?xml version="1.0" encoding="UTF-8"?>' ;
			$siteoutput .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' ;
			$datelast = gmdate("Y-m-d", time())."T".gmdate("H:i:s", time())."+01:00"  ;
	
			foreach ($rowCAT as $key=>$value) {
				$db->query("SELECT * FROM media WHERE category = '".$value['id']."' AND status = 'true' ORDER BY id DESC") ;
				$rowmedia = $db->fetchAll() ;
				$catname = $value['name'] ;
				if ($rowmedia) {
				$catname = str_replace(" ", "_", trim($catname)) ;
				
					sitemapper($rowmedia, $catname) ;
					$siteoutput .= '<sitemap><loc>'.$sitepath.'sitemap/'.$catname.'.xml</loc><lastmod>'.$datelast.'</lastmod></sitemap>' ;
					$submitmsg .= "<a href='".$sitepath."sitemap/".$catname.".xml'>".$sitepath."sitemap/".$catname.".xml</a><br />" ;
				} else {
					$submitmsg .= "Sitemap $catname was empty and ignored<br>" ;
				}
	
			 }
			$siteoutput .= '</sitemapindex>' ;
			if ($siteoutput) {
				$file = $rootpath.'sitemap/sitemap.xml' ;
				$fp = fopen($file, "w");
				fwrite($fp, $siteoutput);
				fclose($fp);
				$submitmsg .= "<a href='".$sitepath."sitemap/sitemap.xml'>".$sitepath."sitemap/sitemap.xml</a><br />" ;
			}
}
?>
<h2>Create Sitemap</h2>


<div class="settings">

	<fieldset>
	
		<form action="" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<td>Auto update sitemap on new approved video or delete video?:</td>
				<td><input type="checkbox" name="autositemap" <?=$autositemap?>></td>
			</tr>
			<tr>	
				<td><input class="submitbutton" type="submit" name="submit2" value="Submit" /></td>
			</tr>
		</table>
		</form>	
	
		<form action="" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<h4><?=$submitmsg?><h4>
			</tr>
			<tr>	
				<td><input class="submitbutton" type="submit" name="submit" value="Create sitemap" /></td>
			</tr>
		</table>
		</form>

	</fieldset>
</div>	


	


