<?php
	include("inside/config.php");
	session_start();
	$user=$_SESSION['username'];
	if(empty($user))
	{
		echo "<script>window.location='login.php'</script>";
	}
	$id=$_REQUEST['id'];
	$sql=mysql_query("select * from `articles` where `article_id`='$id'");
	$rs=mysql_fetch_array($sql);
if($_REQUEST['action']=="update")
	{
		$id=$_REQUEST['id'];
		$heading=mysql_real_escape_string($_REQUEST['ftitle']);
		$pagename=$_REQUEST['fpage'];
		$imgname =($_FILES['fpic']['name']);
		$name=time();
	    $target = "pictures/";
    	$target = $target .$name. basename( $_FILES['fpic']['name']);

		$content=mysql_real_escape_string($_REQUEST['fcontent']);
		//check if picture uploded and page name = assign from drop down
		//check if picture is uploded 
	if($_FILES['fpic']['tmp_name']!=''){
			if(move_uploaded_file($_FILES['fpic']['tmp_name'],$target))
			{
			mysql_query("update `articles` set `article_heading`='$heading', `page_name`='$pagename' , `article_content`='$content', `article_picture`='$target' where `article_id`='$id'") or die(mysql_error());
		echo "<script>window.location='index.php'</script>";
			}
			else
		{
		$type = "error";	
		$msg = "error in uploading file";
		}
		}		
		//just update article Title and content
	if($_FILES['fpic']['tmp_name']=='') {
				$sql_up=mysql_query("update `articles` set `article_heading`='$heading' , `page_name`='$pagename', `article_content`='$content' where `article_id`='$id'") or die(mysql_error());
		echo "<script>window.location='index.php'</script>";
				
				}
					
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title>Admin Panel : Frontier Public High School</title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
<?php include('inside/jsfunctions.php'); ?>
</head>
<body>
<?php include('inside/header.php'); ?>
<!-- Container -->
<div id="container">
	<div class="shell">
		
		<!-- Small Nav -->
		<div class="small-nav">
			<a href="#">Dashboard</a>
			<span>&gt;</span>
			Edit Articles
		</div>
		<!-- End Small Nav -->
		
		<!-- Message OK -->		
		<?php if($type=="ok"){ ?>
        <div class="msg msg-ok" id="msgok">
			<p><strong><?php echo $msg; ?></strong></p>
			<a href="#" class="close" onclick="removeOk();">close</a>
		</div>
		<!-- End Message OK -->		
		<?php } ?>
		<!-- Message Error -->
		<?php if($type=="error"){ ?>
		<div class="msg msg-error" id="error">
			<p><strong><?php echo $msg; ?></strong></p>
			<a href="#" class="close" onclick="removeError();">close</a>
		</div>
        <?php } ?>
		<!-- End Message Error -->
		<br />
		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>
			
			<!-- Content -->
			<div id="content">
				
				<!-- Box --><!-- End Box -->
				
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>Editing Article</h2>
					</div>
					<!-- End Box Head -->
					
					<form action="edit-article.php?action=update&id=<?php echo $id; ?>" method="post" enctype="multipart/form-data">
						
						<!-- Form -->
						<div class="form">
								<p>
								  <label>Article Title </label>
									<input name="ftitle" type="text" class="field size1" id="ftitle" value="<?php echo $rs['article_heading'];?> " />
								</p>	
						  <p class="inline-field">
									<span class="req"></span>
                              <label>Article Page <span>(Change if Required)</span></label>
							<select name="fpage" class="field size3a" id="fpage">
							  <option value="Home"<?php $value="Home";if($value == $rs['page_name']){ echo "selected=\"selected\"";} ?>>Home</option>
							  <option value="Fee Structure" <?php $value="Fee Structure"; if($value == $rs['page_name']){ echo "selected=\"selected\"";} ?>>Fee Structure</option>
							  <option value="Contact"<?php $value="Contact"; if($value == $rs['page_name']){ echo "selected=\"selected\"";} ?>>Contact</option>
							  <option value="Mission & Vission"<?php $value="Mission & Vission"; if($value == $rs['page_name']){ echo "selected=\"selected\"";} ?>>Mission & Vission</option>
                              <option value="News"<?php $value="News"; if($value == $rs['page_name']){ echo "selected=\"selected\"";} ?>>News</option>
                            </select>
						  </p>
						  <p>
								  <label>Picture <span>(New Picture will replace old one)</span></label>
							<input name="fpic" type="file" id="fpic" />
                            <a href="<?php echo $rs['article_picture'] ?>" target="_blank">click Here to see old Picture </a>
						  </p>		
								<p>
								  <label>Content <span>(Required Field)</span></label>
									<textarea name="fcontent" cols="30" rows="10" class="field size1" id="fcontent"><?php echo $rs['article_content'];?></textarea>
						  </p>	
							
						</div>
						<!-- End Form -->
						
						<!-- Form Buttons -->
						<div class="buttons">
							<input type="reset" class="button" value="reset" />
							<input type="submit" class="button" value="submit" />
						</div>
						<!-- End Form Buttons -->
					</form>
				</div>
				<!-- End Box -->

			</div>
			<!-- End Content -->
			
			<!-- Sidebar -->
			<div id="sidebar">
				
				<!-- Box -->
			  <div class="box">
					
					<!-- Box Head -->
					<div class="box-head">
						<h2>Management</h2>
					</div>
					<!-- End Box Head-->
					
					<div class="box-content">
				</div>
				</div>
				<!-- End Box -->
			</div>
			<!-- End Sidebar -->
			
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->
<?php include("inside/footer.php"); ?>
</body>
</html>