<?php
include('header.php');
	
	if (empty($_GET['do'])) {echo $do = '';} else {$do = $_GET['do'];}
	switch ($do) {
	case 'add';
	echo '<div class="page_title">
	<h3>Slider - Add New Item</h3>
	<a href="slider.php">Back To Slider</a>
	</div>';
	if (isset($_POST['add'])) {
	$slider_title = $_POST['slider_title'];
	$slider_url = $_POST['slider_url'];
	
	if (empty($_FILES['image']['name'])) {
	$message = $general->notification('error','Select The Item Thumbnail Please');
	} elseif (empty($slider_title)) {
	$message = $general->notification('error','Write The Item Title Please');
	} elseif (empty($slider_url)) {
	$message = $general->notification('error','Write The Item Url Please');
	} else {
	$up = new fileDir('../uploads/slider/');
	$slider_thumbnail = $up->upload($_FILES['image']);
	$insert = mysql_query("INSERT INTO slider (slider_title,slider_url,slider_thumbnail) VALUES ('$slider_title','$slider_url','$slider_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="250">Item Title <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="slider_title" size="40" /></td>
	</tr>
	<tr>
		<td class="label_td" width="250">Item Url <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="slider_url" size="50" /></td>
	</tr>
	<tr>
		<td class="label_td" width="250">Item Screenshot (thumbnail) <span style="color:red;">*</span> </td>
		<td class="input_td"><input type="file" name="image" /></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>Slider - Edit Item</h3>
	<a href="slider.php">Back To Slider</a>
	</div>';
	if (isset($_POST['edit'])) {
	$slider_title = $_POST['slider_title'];
	$slider_url = $_POST['slider_url'];
	if (!empty($_FILES['image']['name'])) {
	$up = new fileDir('../uploads/slider/');
	$slider_thumbnail = $up->upload($_FILES['image']);
	$up->delete("$_POST[slider_thumbnail]");
	} else {
	$slider_thumbnail = $_POST['slider_thumbnail'];
	}
	if (empty($slider_title)) {
	$message = $general->notification('error','Write The Item Title Please');
	} elseif (empty($slider_url)) {
	$message = $general->notification('error','Write The Item Url Please');
	} else {
	$insert = mysql_query("UPDATE portfolio SET slider_title='$slider_title',slider_url='$slider_url',slider_thumbnail='$slider_thumbnail' WHERE slider_id='$id'");
	if ($insert) {
	$message = $general->notification('success','Item Is Saved Successfully');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	if (isset($message)) {echo $message;} 
	$slider = $general->slider_item($id);
	echo '<form method="POST" action="" enctype="multipart/form-data">
	<table width="100%" cellpadding="5" cellspacing="0">
	<tr>
		<td class="label_td" width="250">Item Title <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="slider_title" size="40" value="'.$slider['slider_title'].'" /></td>
	</tr>
	<tr>
		<td class="label_td" width="250">Item Url <span style="color:red;">*</span></td>
		<td class="input_td"><input type="text" name="slider_url" size="50" value="'.$slider['slider_url'].'" /></td>
	</tr>
	<tr>
		<td class="label_td" valign="top" width="250">Item Screenshot (thumbnail) <span style="color:red;">*</span></td>
		<td class="input_td"><input type="file" name="image" /></td></tr><tr><td colspan="2" class="label_td">';
		if (!empty($slider['slider_thumbnail'])) {
		echo '<img src="../uploads/slider/'.$slider['slider_thumbnail'].'" style="max-width:730px;" />';
		}
		echo '</td>
	</tr>
	<tr>
		<td><span style="color:red; font:normal 12px Tahoma;">* required field</span></td>
		<td align="right">
		<input type="hidden" name="slider_thumbnail" value="'.$slider['slider_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>Slider - Delete Item</h3>
	<a href="slider.php">Back To Slider</a>
	</div>';
	if (isset($_POST['delete'])) {
	$delete = mysql_query("DELETE FROM slider WHERE slider_id='$id'");
	if ($delete) {
	if (!empty($_POST['slider_thumbnail'])) {
	@unlink('../uploads/slider/'.$_POST['slider_thumbnail']);
	}
	$message = $general->notification('success','Slider Item Is Deleted Successfully. You Will Be Redirected To Slider Page In 3 Seconds.');
	echo "<meta http-equiv='refresh' content='3;URL=slider.php'>";
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	$slider = $general->slider_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 Slider Item : <b>'.$slider['slider_title'].'</b> ?</td>
	</tr>
	<tr>
	<td>
	<input type="hidden" name="slider_thumbnail" value="'.$slider['slider_thumbnail'].'" />
	<input type="submit" name="delete" value="Delete" class="inputbutton grey" />
	</td>
	</tr>
	</table>
	</form>';
	break;
	
	default;
	echo '<div class="page_title">
	<h3>Slider</h3>
	<a href="?do=add">Add New Item</a>
	</div>';
	$items_number = mysql_num_rows(mysql_query("SELECT slider_id FROM slider"));
	if ($items_number == 0) {
	echo $general->notification('warning','There is no Slider Items 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 slider ORDER BY slider_order ASC ". $pagination->getLimitSql();
	$query = mysql_query($sql);
	echo '<div class="divider_td">Title</div>
	<div id="sort_slider" class="sort">';
	echo '<ul>';
	while ($row = mysql_fetch_array($query)) {
	echo '<li id="records_'.$row['slider_id'].'" title="Drag To Sort">
		<span class="category_title">'.$row['slider_title'].'</span>
		<span class="category_tools"><a href="?do=delete&id='.$row['slider_id'].'" class="delete">Delete</a></span>
		<span class="category_tools"><a href="?do=edit&id='.$row['slider_id'].'" class="edit">Edit</a></span>
		</li>';
	}
	echo '</ul></div>';
	}
	}
include('footer.php');
?>