<?php 
require_once("header.php");
require_once("session_check.php");

//CODE FOR ADD PHOTO
if(isset($_POST['add']))
{
	//UPLOAD IMAGE
	if($_FILES['photo']['name']!="")
	{	
		$photopath = pathinfo($_FILES['photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['photo']['tmp_name'];
		$photo = time().".".$extension;
		$destination = "../uploads/gallery/".$photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$photo = '';
	}
	
	$sql_insert = "INSERT INTO `pr_photos` SET `category_id` = '".$_POST['category_id']."',`photo` = '".$photo."',`photo_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Photo added successfully.';
	header("location: photo_list.php");
	exit;
}

//CODE FOR UPDATE PHOTO
if(isset($_POST['edit']))
{
	//UPLOAD IMAGE
	if($_FILES['photo']['name']!="")
	{	
		unlink("../uploads/gallery/".$_POST['hid_photo']);
		
		$photopath = pathinfo($_FILES['photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['photo']['tmp_name'];
		$photo = time().".".$extension;
		$destination = "../uploads/gallery/".$photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$photo = $_POST['hid_photo'];
	}
	
	$sql_update = "UPDATE `pr_photos` SET `category_id` = '".$_POST['category_id']."',`photo` = '".$photo."' WHERE `photo_id` = '".$_REQUEST['photo_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'photo updated successfully.';
	header("location:photo_list.php");
	exit;
}

//FETCH DATA FROM DATABASE
if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") 
{
	$fetch_record = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pr_photos` WHERE `photo_id` = '".$_REQUEST['photo_id']."'"));
	$photo = stripslashes($fetch_record['photo']);
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('category_id').value == '')
	{
		alert("Please select category");
		return false;
	}
	
	if(document.getElementById('photo').value == '' && document.getElementById('hid_photo').value == '')
	{
		alert("Please upload photo");
		return false;
	}
}
</script>

<?php require_once("left_panel.php");?>
<div class="content">
	<div class="workplace">
		<div class="row-fluid">
			<div class="span12">
				<div class="head clearfix">
					<div class="isw-picture"></div>
					<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
					<h1>Update Photo</h1>
					<?php } else { ?>
					<h1>Add Photo</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="photo_id" id="photo_id" value="<?php echo $fetch_record['photo_id'];?>" />
				<input type="hidden" name="hid_photo" id="hid_photo" value="<?php echo $fetch_record['photo'];?>" />
				<div class="block-fluid">
				    <div class="row-form clearfix">
						<div class="span3">Category <font color="#FF0000">*</font></div>
						<div class="span9">
						    <select name="category_id" id="category_id">
						        <option value="">Select</option>
						        <?php
						        $sql_cat = "";
						        $exe_cat = mysqli_query($connection,$sql_cat);
						        while($arr_cat = mysqli_fetch_array($exe_cat)
						        {
						            ?>
						            <?php
						        }
						        ?>
						    </select>
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Photo <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="photo" id="photo" />
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br /><img src="../uploads/gallery/<?php echo $photo;?>" border="0" alt="" width="150" height="150" />
						<?php } ?>
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">&nbsp;</div>
						<div class="span9">
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<button class="btn" type="submit" name="edit">Save</button>
						<?php } else { ?>
						<button class="btn" type="submit" name="add">Save</button>
						<?php } ?>
						<button class="btn" type="button" name="back" onclick="window.location.href='photo_list.php'">Back To Photos List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

<?php require_once("footer.php"); ?>