<?php 
require_once("header.php");
require_once("session_check.php");
require_once("../FCKeditor/fckeditor.php");

//CODE FOR ADD BLOG
if(isset($_POST['add']))
{
	//UPLOAD IMAGE
	if($_FILES['blog_photo']['name']!="")
	{	
		$photopath = pathinfo($_FILES['blog_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['blog_photo']['tmp_name'];
		$blog_photo = time().".".$extension;
		$destination = "../uploads/blog/".$blog_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$blog_photo = '';
	}

	$sql_insert = "INSERT INTO `pr_blogs` SET `blog_title` = '".addslashes($_POST['blog_title'])."',`blog_details` = '".addslashes($_POST['blog_details'])."',`blog_date` = '".date('Y-m-d',time())."',`blog_photo` = '".$blog_photo."',`blog_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Blog added successfully.';
	header("location: blog_list.php");
	exit;
}

//CODE FOR UPDATE BLOG
if(isset($_POST['edit']))
{
	//UPLOAD IMAGE
	if($_FILES['blog_photo']['name']!="")
	{	
		unlink("../uploads/blog/".$_POST['hid_blog_photo']);
		
		$photopath = pathinfo($_FILES['blog_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['blog_photo']['tmp_name'];
		$blog_photo = time().".".$extension;
		$destination = "../uploads/blog/".$blog_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$blog_photo = $_POST['hid_blog_photo'];
	}

	$sql_update = "UPDATE `pr_blogs` SET `blog_title` = '".addslashes($_POST['blog_title'])."',`blog_details` = '".addslashes($_POST['blog_details'])."',`blog_photo` = '".$blog_photo."' WHERE `blog_id` = '".$_POST['hid_blog_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Blog updated successfully.';
	header("location: blog_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_blogs` WHERE `blog_id` = '".$_REQUEST['blog_id']."'"));
	$blog_title = stripslashes($fetch_record['blog_title']);
	$blog_photo = stripslashes($fetch_record['blog_photo']);
	$blog_details = stripslashes($fetch_record['blog_details']);
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('blog_title').value == '')
	{
		alert("Please enter title");
		document.getElementById('blog_title').focus();
		return false;
	}
	
	if(document.getElementById('blog_photo').value == '' && document.getElementById('hid_blog_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-documents"></div>
					<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
					<h1>Update Blog</h1>
					<?php } else { ?>
					<h1>Add Blog</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="hid_blog_id" id="hid_blog_id" value="<?php echo $_GET['blog_id'];?>" />
				<input type="hidden" name="hid_blog_photo" id="hid_blog_photo" value="<?php echo $blog_photo;?>" />
				<div class="block-fluid">
					<div class="row-form clearfix">
						<div class="span3">Title <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="blog_title" id="blog_title" value="<?php echo $blog_title;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Photo <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="blog_photo" id="blog_photo" /><br><font color="#ff0000;">[Banner size should be width 370px and height 325px]]</font>
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br />
						<img src="../uploads/blog/<?php echo $blog_photo;?>" alt="" height="325" width="370" />
						<?php } ?>
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Description</div>
						<div class="span9">
						<?php 
						$oFCKeditor = new FCKeditor('blog_details');
						$oFCKeditor->Height=400;
						$oFCKeditor->Width=700;
						$oFCKeditor->BasePath = '../FCKeditor/';
						$oFCKeditor->Value= $blog_details;
						$oFCKeditor->Create(); 
					    ?>
						</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='blog_list.php'">Back To Blogs List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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