<?php
include('header.php');
	
	if (empty($_GET['do'])) {echo $do = '';} else {$do = $_GET['do'];}
	switch ($do) {
	case 'add';
	echo '<div class="page_title">
	<h3>Portfolio - Add New Item</h3>
	<a href="portfolio.php">Back To Portfolio</a>
	</div>';
	if (isset($_POST['add'])) {
	$work_title = $_POST['work_title'];
	$work_url = $_POST['work_url'];
	$work_details = $_POST['work_details'];
	$work_category_id = implode(',',$_POST['work_category_id']);
	if (!empty($_FILES['image']['name'])) {
	$up = new fileDir('../uploads/portfolio/');
	$work_thumbnail = $up->upload($_FILES['image']);
	} else {
	$work_thumbnail = '';
	}
	if (empty($work_title)) {
	$message = $general->notification('error','Write The Item Title Please');
	} elseif (empty($work_url)) {
	$message = $general->notification('error','Write The Item Url Please');
	} else {
	$insert = mysql_query("INSERT INTO portfolio (work_title,work_url,work_details,work_category_id,work_thumbnail) VALUES ('$work_title','$work_url','$work_details','$work_category_id','$work_thumbnail')");
	if ($insert) {
	$message = $general->notification('success','Item Is Saved Successfully');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	if (isset($message)) {echo $message;} 
	echo '<form method="POST" action="" enctype="multipart/form-data">
	<table width="100%" cellpadding="5" cellspacing="0">
	<tr>
		<td class="label_td" width="150">Item Title <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="work_title" size="40" /></td>
	</tr>
	<tr>
		<td class="label_td" width="150">Item Url <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="work_url" size="50" /></td>
	</tr>
	<tr>
		<td class="label_td" width="150">Item Category <span style="color:red;">*</span></td>
		<td class="input_td">';
		$categories = $general->portfolio_categories();
		foreach ($categories as $category) {
		echo '<input type="checkbox" name="work_category_id[]" value="'.$category['category_title'].'" />'.$category['category_title'].'</br />';
		}
		echo '</td>
	</tr>
	<tr>
		<td class="label_td" width="150">Item Screenshot (thumbnail) </td>
		<td class="input_td"><input type="file" name="image" /></td>
	</tr>
	<tr>
		<td class="label_td" width="150">Item Details</td>
		<td class="input_td"><textarea name="work_details" rows="4" style="width:98%;"></textarea></td>
	</tr>
	<tr>
		<td><span style="color:red; font:normal 12px Tahoma;">* required field</span></td>
		<td align="right"><input type="submit" name="add" value="Add Item" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	break;
	
	case 'edit';
	$id = abs(intval($_GET['id']));
	echo '<div class="page_title">
	<h3>Portfolio - Edit Item</h3>
	<a href="portfolio.php">Back To Portfolio</a>
	</div>';
	if (isset($_POST['edit'])) {
	$work_title = $_POST['work_title'];
	$work_url = $_POST['work_url'];
	$work_details = $_POST['work_details'];
	$work_category_id = implode(',',$_POST['work_category_id']);
	if (!empty($_FILES['image']['name'])) {
	$up = new fileDir('../uploads/portfolio/');
	$work_thumbnail = $up->upload($_FILES['image']);
	$up->delete("$_POST[work_thumbnail]");
	} else {
	$work_thumbnail = $_POST['work_thumbnail'];
	}
	if (empty($work_title)) {
	$message = $general->notification('error','Write The Item Title Please');
	} elseif (empty($work_url)) {
	$message = $general->notification('error','Write The Item Url Please');
	} else {
	$insert = mysql_query("UPDATE portfolio SET work_title='$work_title',work_url='$work_url',work_details='$work_details',work_category_id='$work_category_id',work_thumbnail='$work_thumbnail' WHERE work_id='$id'");
	if ($insert) {
	$message = $general->notification('success','Item Is Saved Successfully');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	if (isset($message)) {echo $message;} 
	$work = $general->portfolio_item($id);
	echo '<form method="POST" action="" enctype="multipart/form-data">
	<table width="100%" cellpadding="5" cellspacing="0">
	<tr>
		<td class="label_td" width="150">Item Title <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="work_title" size="40" value="'.$work['work_title'].'" /></td>
	</tr>
	<tr>
		<td class="label_td" width="150">Item Url <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="work_url" size="50" value="'.$work['work_url'].'" /></td>
	</tr>
	<tr>
		<td class="label_td" width="150">Item Category <span style="color:red;">*</span></td>
		<td class="input_td">';
		$categories = $general->portfolio_categories();
		foreach ($categories as $category) {
		echo '<input type="checkbox" name="work_category_id[]" value="'.$category['category_title'].'"'; if (is_array(explode(',',$work['work_category_id'])) AND in_array($category['category_title'],explode(',',$work['work_category_id']))) {echo 'CHECKED';} echo '/>'.$category['category_title'].'</br />';
		}
		echo '</td>
	</tr>
	<tr>
		<td class="label_td" valign="top" width="150">Item Screenshot (thumbnail) </td>
		<td class="input_td"><input type="file" name="image" />';
		if (!empty($work['work_thumbnail'])) {
		echo '<img src="../uploads/portfolio/'.$work['work_thumbnail'].'" height="70" style="float:right;" />';
		}
		echo '</td>
	</tr>
	<tr>
		<td class="label_td" width="150">Item Details</td>
		<td class="input_td"><textarea name="work_details" rows="4" style="width:98%;">'.$work['work_details'].'</textarea></td>
	</tr>
	<tr>
		<td><span style="color:red; font:normal 12px Tahoma;">* required field</span></td>
		<td align="right">
		<input type="hidden" name="work_thumbnail" value="'.$work['work_thumbnail'].'" />
		<input type="submit" name="edit" value="Save Item" class="inputbutton grey" />
		</td>
	</tr>
	</table>
	</form>';
	break;
	
	case 'delete';
	$id = abs(intval($_GET['id']));
	echo '<div class="page_title">
	<h3>Portfolio - Delete Item</h3>
	<a href="portfolio.php">Back To Portfolio</a>
	</div>';
	if (isset($_POST['delete'])) {
	$delete = mysql_query("DELETE FROM portfolio WHERE work_id='$id'");
	if ($delete) {
	if (!empty($_POST['work_thumbnail'])) {
	@unlink('../uploads/portfolio/'.$_POST['work_thumbnail']);
	}
	$message = $general->notification('success','Portfolio Item Is Deleted Successfully. You Will Be Redirected To Portfolio Page In 3 Seconds.');
	echo "<meta http-equiv='refresh' content='3;URL=portfolio.php'>";
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	$work = $general->portfolio_item($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 Portfolio Item : <b>'.$work['work_title'].'</b> ?</td>
	</tr>
	<tr>
	<td>
	<input type="hidden" name="work_thumbnail" value="'.$work['work_thumbnail'].'" />
	<input type="submit" name="delete" value="Delete" class="inputbutton grey" />
	</td>
	</tr>
	</table>
	</form>';
	break;
	
	default;
	echo '<div class="page_title">
	<h3>Portfolio</h3>
	<a href="?do=add">Add New Item</a>
	</div>';
	$items_number = mysql_num_rows(mysql_query("SELECT work_id FROM portfolio"));
	if ($items_number == 0) {
	echo $general->notification('warning','There is no Items in Portfolio until now.');
	} else {
	$page = 1;
	$size = 10;
	if (isset($_GET['page'])){ $page = abs(intval($_GET['page'])); }
	$pagination = new Pagination();
	$pagination->setLink("?page=%s");
	$pagination->setPage($page);
	$pagination->setSize($size);
	$pagination->setTotalRecords($items_number);
	$sql = "SELECT * FROM portfolio ORDER BY work_id DESC ". $pagination->getLimitSql();
	$query = mysql_query($sql);
	echo '<table width="100%" cellpadding="10" cellspacing="0">';
	echo '<tr>
	<td class="divider_td">title</td>
	<td class="divider_td">category</td>
	<td class="divider_td"></td>
	</tr>';
	while ($row = mysql_fetch_array($query)) {
	echo '<tr>
		<td class="label_td">'.$row['work_title'].'</td>
		<td class="label_td">'.str_replace(',',' ',$row['work_category_id']).'</td>
		<td class="label_td" width="150" align="right">
		<a href="?do=edit&id='.$row['work_id'].'" class="edit">Edit</a>
		<a href="?do=delete&id='.$row['work_id'].'" class="delete">Delete</a>
		</td>
	</tr>';
	}
	echo '</table>
	'.$pagination->create_links();
	}
	}
include('footer.php');
?>