<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');

//Is a new category requested?
$newstatus=NULL;
if(isset($_POST['newCatName']) && $_POST['newCatName']!='') {
	if($_p->add_category(fetch::unescapePostVar($_POST['newCatName'])))
		$newstatus=true;
	else
		$newstatus=false;
}

//Is an edit requested?
$editstatus=NULL;
if($_POST['editt']!=''&&$_p->category_id_exists($_POST['editc'])) {
	if($_p->edit_category($_POST['editc'],$_POST['editt']))
		$editstatus=true;
	else
		$editstatus=false;
}
else if(isset($_POST['editt'])) {
	$editstatus=false;
}

//Is a delete requested?
$delstatus=NULL; //null-nothing, true-pass, false-fail
if(is_numeric($_GET['delc'])&&$_p->category_id_exists($_GET['delc'])) {
	if($_p->delete_category($_GET['delc']))
		$delstatus=true;
	else
		$delstatus=false;
}
else if(isset($_GET['delc'])) {
	$delstatus=false;
}

//Is a swap requested?
$swapstatus=NULL; //null-nothing, true-success, false-fail
if(isset($_GET['up'])) {
	//Find what category comes before
	$findCat=$_GET['up'];
	if(!$_p->category_id_exists($findcat)) {
		trigger_error('No such category exists',E_USER_ERROR);
		$swapstatus=false;
	}
	else {
		$categories=$_p->get_categories();
		$catCount=count($categories);
		if($catCount==0) {
			trigger_error('No categories to swap',E_USER_ERROR);
			$swapstatus=false;
		}
		else {
			$catOrder=0;
			$catBefore=false;
			foreach($categories as $catID=>$catInfo) {
				if($catID==$findCat) {
					if($catBefore===false) {
						trigger_error('No category above to swap with',E_USER_ERROR);
						$swapstatus=false;
					}
					else {
						$_p->category_swap($findCat,$catBefore);
						$swapstatus=true;
						break;
					}
				}
				$catBefore=$catID;
			}
		}
	}
}
if(isset($_GET['down'])) {
	//Find what category comes before
	$findCat=$_GET['down'];
	if(!$_p->category_id_exists($findcat)) {
		trigger_error('No such category exists',E_USER_ERROR);
		$swapstatus=false;
	}
	else {
		$categories=$_p->get_categories();
		$catCount=count($categories);
		if($catCount==0) {
			trigger_error('No categories to swap',E_USER_ERROR);
			$swapstatus=false;
		}
		else {
			$catOrder=0;
			$catAfter=false;
			foreach($categories as $catID=>$catInfo) {
				if($catAfter) {
					$catAfter=$catID;
					break;
				}
				if($catID==$findCat)
					$catAfter=true;
			}
			if($catAfter===true) {
				trigger_error('No category below to swap with',E_USER_ERROR);
				$swapstatus=false;
			}
			else if($catAfter===false) {
				trigger_error('Could not find initial category',E_USER_ERROR);
				$swapstatus=false;
			}
			else {
				$_p->category_swap($findCat,$catAfter);
				$swapstatus=true;
			}
		}
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Categories</title>
<link rel="stylesheet" href="admin.css" />
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a> &raquo; <a href="categories.php" class="breadcrumb">Manage Categories</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
		<h2>Manage Categories</h2>
		<?php
			//New/Deleting category status
			if(isset($_GET['del']) && $_p->category_id_exists($_GET['del'])) {
				echo('Confirm deleting the category "'.htmlentities($_p->cat_name($_GET['del']),ENT_QUOTES,'UTF-8').'"?<br />
				<a href="categories.php?delc='.$_GET['del'].'">Confirm</a> | <a href="categories.php">Cancel</a>');
			}
			if($delstatus===false)
				echo('An error occured when trying to delete the category (does it still exist, or is it the last one?)');
			if($newstatus===false)
				echo('An error occured when trying to create the category (does it already exist?)');
			if($swapstatus===false)
				echo('An error occured when trying to swap the categories (check the errors above)');
		
		
		//Editing category
		if(isset($_GET['edit'])&&$_p->category_id_exists($_GET['edit'])) {
			echo('
			<h3>Editing Category</h3>
			<form action="categories.php" method="post">
				Name:<br />
				<input type="text" name="editt" value="'.$_p->category_name($_GET['edit']).'" /><br />
				<input type="hidden" name="editc" value="'.$_GET['edit'].'" />
				<br />
				<input type="submit" value="Edit Category" />
			</form>
			');
		}
		?>
		<h3>New Category</h3>
		<form action="categories.php" method="post">
			Name:<br />
			<input type="text" name="newCatName" /><br />
			<br />
			<input type="submit" value="Create Category" />
		</form>
		<h3>Current Categories</h3>
		<?php
			//Category list
			$categories=$_p->get_categories();
			$catCount=count($categories);
			if($catCount==0)
				echo('No categories to display...');
			$not_last=$catCount>1;
			$catOrder=0;
			foreach($categories as $catID=>$catInfo) {
				echo($catID.' - '.$catInfo['name'].' <a href="categories.php?edit='.$catID.'">[Edit]</a>');
				if($not_last&&isset($_GET['del'])&&$catID==$_GET['del'])
					echo(' <a href="categories.php?delc='.$catID.'">[Confirm Delete]</a>');
				else if($not_last) {
					echo(' <a href="categories.php?del='.$catID.'">[Delete]</a>');
					if($catOrder!=0)
						echo(' <a href="categories.php?up='.$catID.'">[Up]</a>');
					if($catOrder!=$catCount-1)
						echo(' <a href="categories.php?down='.$catID.'">[Down]</a>');
				}
				
				echo('<br />');
				$catOrder++;
			}
		?>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>