<?php
session_start();
?>

<style>

* {font-family:Arial, Helvetica, sans-serif}

.category-picture {
	text-align:left;
}

#menu-wrapper {width:auto; border:1px solid #467182; 
	font-weight:bold; cursor:pointer; 
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
	-ms-border-radius:7px;
    border-radius: 7px;
    border: solid 1px #e8e8e8;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
    -webkit-transition-duration: 0.2s;
    -moz-transition-duration: 0.2s;
    transition-duration: 0.2s;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
	color:#fff;
	background: #e9050d;
	text-align:left;padding-left:.5em;
	margin-bottom:0px;}
	
#menu-wrapper img {
	    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border-radius: 7px;
	}
h3 {font-family:Arial, Helvetica, sans-serif;}

</style>

<?php
//error_reporting(E_ALL);
if ($_SESSION['adminkey'] != "security") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}

  
//$headerimg = $_POST['headerimg'];
$action=$_POST['action'];
  
	echo "<div align='right'><a href='JavaScript:window.close()'>Close</a></div>";
	
	//connect to main rubber menu database first to look up client
	include ('includes/db1config.php');
    include ('../includes/mysql_connect.php');
	
	//function to get category header images:
	function buildImageList($theFolder) {

		// Execute code if the folder can be opened, or fail silently
		if ($contents = @ scandir($theFolder)) {
			// initialize an array for matching files
			$found = array();
			// Create an array of file types
			$fileTypes = array('jpg','jpeg','gif','png');
			// move through the folder, and add filename to $found array if type matches
			$found = array();
			foreach ($contents as $item) {
				$fileInfo = pathinfo($item);
				if (array_key_exists('extension', $fileInfo) && in_array($fileInfo['extension'],$fileTypes)) {
					$found[] = $item;
				}
			}

			// Check the $found array is not empty
			if ($found) {
			  // Sort in natural, case-insensitive order, and populate menu
				natcasesort($found);
				foreach ($found as $filename) {
					$img_name = "/images/cat-headers/" .$filename;
					echo "<span id='category-picture'><a href='cat-settings.php/?1=1&catkey=$_GET[catkey]&newimage=$filename'><img src='$img_name' style='border:none;margin:2px;'></a></span>";
				}
			}
		}
	}	//end of image list function
	


	//update category image and description if entered
	if ($action == "update") {
		//die("updating...");
		if (isset($_GET["catkey"])) {	
			$query = "UPDATE menuCategory SET description = '" . $_POST['description'] . "' WHERE recordKey = '" . $_GET['catkey'] ."'" ;
			//echo $query;
			mysql_query($query) or die(mysql_error());   //execute query

			//confirmation message that update worked...
			echo "<div align=center><font color=red>Category description updated successfully.</font></div>";
		}
	}	
	if(!empty($_GET[newimage])){
		$query = "UPDATE menuCategory SET headerImg = '" . $_GET[newimage] ."' WHERE recordKey = '" . $_GET['catkey'] ."'" ;
		mysql_query($query) or die(mysql_error());   //execute query

		//confirmation message that image update worked...
		echo "<div align=center><font color=red>Category image updated successfully.</font></div>";
	}
	
	$mysql = "select * from menuCategory where RecordKey = '$_GET[catkey]'";
	//echo "qry=". $mysql;
	$rt=mysql_query($mysql);  
	$img_directory = $site_folder . "/images/cat-headers";
	
	while($nt=mysql_fetch_array($rt)){
		if(($nt[category_type]=="Pizza")||($nt[CategoryName]=="Pizza")||($nt[CategoryName]=="Pizza Toppings")||($nt[CategoryName]=="Pizza Sauce")||($nt[CategoryName]=="Pizza Crust")){
			$pizza ='1';
		}
		if($_GET[action]!="editimage"){
			echo "<h3>Editing settings for category: $nt[CategoryName]</h3>
			<form action='cat-settings.php/?1=1&catkey=$_GET[catkey]' method='POST'>
			<input type='hidden' name='action' value='update'>";
			echo '<br>Category name: <a name="'.$nt[CategoryName]. '"></a><input type="text" name="category" size="30" value="' . $nt[CategoryName] . '"';
			if(($pizza=='1')||($nt[CategoryName]=='Options')||($nt[CategoryName]=='Fixings')){
					echo ' readonly=readonly';
			}
			echo '>';
			echo"<br>Category Description: <br>
			<textarea name='description' cols=60 rows=3>$nt[description]</textarea><br>
			<i><font size=2>This will show up below the category name on your menu.</font></i><br>";
			//active option
			if(($pizza=='1')||($nt[CategoryName]=='Options')||($nt[CategoryName]=='Fixings')){
				//don't allow changing status here on these categories
				echo '<input type="hidden" name="chkactive" value="true">';
			} else{
				echo '<br>Active: <input type="checkbox" name="chkactive"';
				if(!empty($nt[Active])){
					echo 'checked=true';
				}
				echo '>';
			}
			//add selector for caegory type
			if(($pizza!=1)&&($nt[CategoryName]!="Options")&&($nt[CategoryName]!="Fixings")){
				echo'<br>Category type: <select name="cattype">
				<option value="0"';
				if($nt[category_type]=='0'){echo " selected=selected";}
				echo'>Standard</option>
				<option value="1"';
				if($nt[category_type]=='1'){echo " selected=selected";}
				echo'>Pizza</option>
				</select>';
			}
					
			//delete category option
			if(($pizza=='1')||($nt[CategoryName]=='Options')||($nt[CategoryName]=='Fixings')){		
				echo '<input type="hidden" name="chkdelete" value="">';				
			}else{
				echo '<br>Delete: <input type="checkbox" name="chkdelete" value="false">';
			}
			echo "<br /><input id='save-button' type='submit' name='submit' value='Save Changes'></form><br>
			<br>";
		}
		echo "<center><h3>Current header image for category:<em> $nt[CategoryName]</em></h3>";
		if(!empty($nt[headerImg])){
			echo "<div id='menu-wrapper'><div align=right><img src='/images/cat-headers/".$nt[headerImg]."'></div></div>";
		} else {
			echo "None selected.";
		}
		
		echo"<br><br>";
		
		if($_GET[action]=="editimage"){
			//show available images with ability to select
			echo "Select an image below to replace this header image.<br>";
			buildImageList("../images/cat-headers/");
		} else {
			echo"<a href='cat-settings.php/?1=1&catkey=$_GET[catkey]&action=editimage'>Click here to select a different header image for this category</a>
			<br>";
			
		}
		echo "</center>";

	}
?>