<?php
include('header.php');
	
	if (empty($_GET['do'])) {echo $do = '';} else {$do = $_GET['do'];}
	switch ($do) {
	
	case 'edit';
	$id = abs(intval($_GET['id']));
	if (isset($_POST['edit'])) {
	$category_title = $_POST['category_title'];
	if (empty($category_title)) {
	$message = $general->notification('error','Write The Category Title Please');
	} else {
	$update = mysql_query("UPDATE portfolio_categories SET category_title='$category_title' WHERE category_id='$id'");
	if ($update) {
	$message = $general->notification('success','Category Is Saved Successfully');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	$category = $general->portfolio_category($id);
	echo '<div class="page_title">
	<h3>Portfolio Categories - Edit Category</h3>
	<a href="portfolio_categories.php">Back To Portfolio Categories</a>
	</div>';
	if (isset($message)) {echo $message;}
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="5" cellspacing="0" style="margin-bottom:30px;">
	<tr>
	<td class="label_td" width="150">Category Title</td>
	<td class="input_td"><input type="text" name="category_title" size="20" value="'.$category['category_title'].'" /></td>
	</tr>
	<tr>
	<td colspan="2"><input type="submit" name="edit" value="Save" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	break;
	
	case 'delete';
	$id = abs(intval($_GET['id']));
	echo '<div class="page_title">
	<h3>Portfolio Categories - Delete Category</h3>
	<a href="portfolio_categories.php">Back To Portfolio Categories</a>
	</div>';
	if (isset($_POST['delete'])) {
	$delete_works = mysql_query("DELETE FROM portfolio WHERE work_category_id='$id'");
	$delete = mysql_query("DELETE FROM portfolio_categories WHERE category_id='$id'");
	if ($delete) {
	$message = $general->notification('success','Category Is Deleted Successfully. You Will Be Redirected To Categories Page In 3 Seconds.');
	echo "<meta http-equiv='refresh' content='3;URL=portfolio_categories.php'>";
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	$category = $general->portfolio_category($id);
	if (isset($message)) {echo $message;}
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="5" cellspacing="0" style="margin-bottom:30px;">
	<tr>
	<td class="label_td">Are You Sure To Delete Category : <b>'.$category['category_title'].'</b> And All Works Categorized Under It ?</td>
	</tr>
	<tr>
	<td><input type="submit" name="delete" value="Delete" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	break;
	
	default;
	if (isset($_POST['add'])) {
	$category_title = $_POST['category_title'];
	if (empty($category_title)) {
	$message = $general->notification('error','Write The Category Title Please');
	} else {
	$insert = mysql_query("INSERT INTO portfolio_categories (category_title) VALUES ('$category_title')");
	if ($insert) {
	$message = $general->notification('success','Category Is Added Successfully');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	echo '<div class="page_title">
	<h3>Portfolio Categories</h3></div>';
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="5" cellspacing="0" style="margin-bottom:30px;">
	<tr><td colspan="2" class="divider_td">Add New Category</td></tr>
	<tr>
		<td class="label_td" width="150">Category Title</td>
		<td class="input_td"><input type="text" name="category_title" size="20" /></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" name="add" value="Add Category" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	if (isset($message)) {echo $message;}
	echo '<table width="100%" cellpadding="10" cellspacing="0">';
	echo '<tr><td colspan="2" class="divider_td">Categories</td></tr>';
	$categories = $general->portfolio_categories();
	if ($categories != 0) {
	foreach ($categories as $category) {
	echo '<tr>
		<td class="label_td">'.$category['category_title'].'</td>
		<td class="label_td" width="150" align="right">
		<a href="?do=edit&id='.$category['category_id'].'" class="edit">Edit</a>
		<a href="?do=delete&id='.$category['category_id'].'" class="delete">Delete</a>
		</td>
	</tr>';
	}
	}
	echo '</table>';
	}
include('footer.php');
?>