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

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

	$sql_insert = "INSERT INTO `pr_programs` SET `program_title` = '".addslashes($_POST['program_title'])."',`program_details` = '".addslashes($_POST['program_details'])."',`program_photo` = '".$program_photo."',`program_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Activity added successfully.';
	header("location: activity_list.php");
	exit;
}

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

	$sql_update = "UPDATE `pr_programs` SET `program_title` = '".addslashes($_POST['program_title'])."',`program_details` = '".addslashes($_POST['program_details'])."',`program_photo` = '".$program_photo."' WHERE `program_id` = '".$_POST['hid_program_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Activity updated successfully.';
	header("location: activity_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_programs` WHERE `program_id` = '".$_REQUEST['program_id']."'"));
	$program_title = stripslashes($fetch_record['program_title']);
	$program_photo = stripslashes($fetch_record['program_photo']);
	$program_details = stripslashes($fetch_record['program_details']);
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('program_title').value == '')
	{
		alert("Please enter title");
		document.getElementById('program_title').focus();
		return false;
	}
	
	if(document.getElementById('program_photo').value == '' && document.getElementById('hid_program_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 Activity</h1>
					<?php } else { ?>
					<h1>Add Activity</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="hid_program_id" id="hid_program_id" value="<?php echo $_GET['program_id'];?>" />
				<input type="hidden" name="hid_program_photo" id="hid_program_photo" value="<?php echo $program_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="program_title" id="program_title" value="<?php echo $program_title;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Photo <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="program_photo" id="program_photo" /><br><font color="#ff0000;">[Banner size should be width 370px and height 240px]]</font>
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br />
						<img src="../uploads/program/<?php echo $program_photo;?>" alt="" height="240" width="370" />
						<?php } ?>
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Description</div>
						<div class="span9">
						<?php 
						$oFCKeditor = new FCKeditor('program_details');
						$oFCKeditor->Height=400;
						$oFCKeditor->Width=700;
						$oFCKeditor->BasePath = '../FCKeditor/';
						$oFCKeditor->Value= $program_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='activity_list.php'">Back To Activities List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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