<?php 
	session_start();
	include ('../config.php');
	include ('secureadmin.php');
	
	$metatitle = "Edit Category - Admin Control Panel";
	include ('includes/document_head.php');
	
	$catid = $_GET['id'];
	
	
	if($catid == "new") {

?>
		<div id="wrapper">
			<?php include 'includes/topbar.php'?>		
			<?php include 'includes/sidebar.php'?>
			<div class="main_container container_16 clearfix">
				
					
			<div class="flat_area grid_16">
			
				<h2>Category Management - Add a new category</h2>
				<p>Use this page to create a new category. </p>
		
			
	<div class="box grid_16 round_all">
	<h2>Add Category</h2>
			
	<form style="padding-left: 15px;" name="submission" method="POST" action="categories.php">

	<b>Category Title:</b><br/>
	<input type="text" name="title" class="long">
	
	<br/><br/><b>Parent Category:</b><br/>
	
<?php 
	
// Populates the Dropdown list with all categories and subcats
	$query = "select * from categories where parentid is null;"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);
		
	echo '<select name="parentid">';
	echo "<option SELECTED value=\"NULL\">None</option>";
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result);
		echo "<option value=\"".$row['id']."\">".$row['name']."</option>";
	}
	echo '</select>';
?>

	<br /><br />
	<input name="add" type="hidden" id="add" />
		
	<button type="submit" id="submitstyle" name="save" class="button_colour round_all"><img height="24" width="24" alt="Bended Arrow Right" src="images/icons/small/white/Bended Arrow Right.png"><span>Add Category</span></button>

</form>

	</div>




<?
// SWITCHING BETWEEN NEW VS EDIT 

} else {

	$query = "select * from categories where id = ".$catid.";"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$info = mysql_fetch_assoc($result);
	// get this category's information
	$catname = $info['name'];
	$parentid = $info['parentid'];
	
	
?>
		<div id="wrapper">
			<?php include 'includes/topbar.php'?>		
			<?php include 'includes/sidebar.php'?>
			<div class="main_container container_16 clearfix">
				
					
			<div class="flat_area grid_16">
			
				<h2>Category Management - <?php echo $catname; ?></h2>
				<p>Use this page to edit or delete a specific category. </p>
		
			
	<div class="box grid_8 round_all">
	<h2>Edit Category</h2>
			
	<form style="padding-left: 15px;" name="submission" method="POST" action="categories.php">

	<b>Category Title:</b><br/>
	<input type="text" name="title" class="long" value="<?php echo $catname;?>">
	
	<br/><br/><b>Parent Category:</b><br/>
	
<?php 

// Function to determine which <option> is selected
	function selected ($categoryid, $rowid) { 
		if ($categoryid == $rowid) {
		return 'SELECTED';
		}
	}
	
// Populates the Dropdown list with all categories and subcats
	$query = "select * from categories where parentid is null;"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);
		
	if (!$parentid) {
		echo "This is a parent category - it cannot be converted into a subcategory";
	} else {
	echo '<select name="parentid">';
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result);
		echo "<option ".selected($parentid, $row['id'])." value=\"".$row['id']."\">".$row['name']."</option>";
	}
	echo '</select>';
	}
?>

	<br /><br />
	<input name="update" type="hidden" id="update" />
	
	<input name="catid" type="hidden" value="<?php echo $catid; ?>" />
	
	<button type="submit" id="submitstyle" name="save" class="button_colour round_all"><img height="24" width="24" alt="Bended Arrow Right" src="images/icons/small/white/Bended Arrow Right.png"><span>Submit</span></button>

</form>

	</div>




<div class="box grid_8 round_all">
	<h2>Delete Category</h2>
			
<p style="padding: 0 12px;"><strong>NOTE</strong>: when deleting a category, you must select a new category where all articles in the deleted category (and any subcategories) will be moved. </p>

<?php 
	$query = "select * from categories where parentid = ".$catid; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);

	if ($num_results) {
		echo "<p style=\"padding: 0 12px;\"><b>In order to delete this category, you must first delete it's subcategories.</b></p>";
	} else { ?>
<form style="padding-left: 15px;" name="submission" method="POST" action="categories.php">
	
		<b>New Category:</b><br/>
		<select name="newcat">
<?php 
	
// Populates the Dropdown list with all categories and subcats
	$query = "select * from categories where parentid is null;"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);
		
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result); 
		echo "<option ".selected($category, $row['id'])." value=\"".$row['id']."\">".$row['name']."</option><br/>"; 
			$query = "select * from categories where parentid =".$row['id'].";"; 
			$sub_result = mysql_query($query,$connection) or die(mysql_error());
			$sub_num_results = mysql_num_rows($sub_result);
			for ($x=0; $x <$sub_num_results; $x++) { 
				$subrow = mysql_fetch_assoc($sub_result); 
				echo "<option ".selected($category, $subrow['id'])." value=\"".$subrow['id']."\"> -- ".$subrow['name']."</option><br/>";
			}	

	}
	
?>	
	<input name="delete" type="hidden" id="delete" />
	
	<input name="catid" type="hidden" value="<?php echo $catid; ?>" />
	<br/><br/>
	<button type="submit" id="submitstyle" name="save" class="button_colour round_all"><img height="24" width="24" alt="Bended Arrow Right" src="images/icons/small/white/Bended Arrow Right.png"><span>DELETE</span></button>

<?php } // closing the subcategory delete else statement

} // closing the new cat vs edit cat else statement

 ?>

	</div>



			</div>
			
			
			
		</div>	
<?php include 'includes/closing_items.php'?>