<?php
	include("inside/config.php");
	session_start();
	$user=$_SESSION['username'];
	if(empty($user))
	{
		echo "<script>window.location='login.php'</script>";
	}
		
	$currentpage = "photogallery";
	$id=$_REQUEST['id'];
	$sql=mysql_query("select * from `photogallery` where `pic_id`='$id'");
	$row=mysql_fetch_array($sql);
	
	if($_REQUEST['action']=="add"){	
//This is the directory where images will be saved
   	$name=time();
    $target = "gallery/";
    $target = $target .$name. basename( $_FILES['fpic']['name']);
//This gets all the other information from the form
    	$id=$_REQUEST['id'];
		$description = $_REQUEST['fdesc'];
		$name=mysql_real_escape_string($_REQUEST['fname']);
		$pic=($_FILES['fpic']['name']);
//Writes the photo to the server
    if(move_uploaded_file($_FILES['fpic']['tmp_name'], $target))
    {
		//Writes the information to the database
   		mysql_query("insert into `photogallery`(`pic_title`,`pic_description`, `pic_lnk`)
		values('$name','$description','$target')");
		$type = "ok";
		$msg = "Picture Added Successfully!";
    }
    else {
 //Gives an error if its not
    $type = "error";
	$msg =  "Sorry, there was a problem uploading your file!";
    }
}
	
if($_REQUEST['action']=="delete")
	{
		$id=$_REQUEST['id'];
		$sqlDel=mysql_query("delete from `photogallery` where `pic_id`='$id'");
		$type = "ok";
		$msg = "Deletion Successful! ";
	}
?>
<!DOCTYPE html>
<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>
			Photo Gallery</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 -->
			<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2 class="left">Photo Gallery Management</h2>
				</div>
					<!-- End Box Head -->	

					<!-- Table -->
					<div class="table">
					  <table width="100%" border="0" cellspacing="0" cellpadding="0">
					    <tr>
					      <th>Sr #</th>
					      <th>Title</th>
					      <th>Description</th>
					      <th>Image link</th>
					      <th width="110" class="ac">Action  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
				        </tr>
					   
<?php						$serial = 0;
					  		$sql2=mysql_query("select * from `photogallery`");
					  		while($row2=mysql_fetch_array($sql2))
					  		{
					  		?>                        <tr>
  <td class="odd"><h3><?php $serial++; echo $serial; ?></h3></td>
					      <td><?php echo $row2['pic_title']; ?></td>
					      <td><?php echo $row2['pic_description']; ?></td>
					      <td><a href="<?php echo $row2['pic_lnk']; ?>" target="_blank">View Picture</a></td>
					      <td><a  class="ico del" href="photo-gallery.php?action=delete&id=<?php echo $row2['pic_id'];?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a>
           	    </td>
				        </tr>
                        <?php
					  		}
					  		?>
				      </table>
					  <!-- Pagging --><!-- End Pagging -->
						
			  </div>
					<!-- Table -->
					
			  </div>
			  <!-- End Box -->
				
			  <!-- Box -->
              <div class="box">
                  <!-- Box Head -->
                <div class="box-head">
                    <h2 class="left">Add New Picture in Gallery
                </h2>
                </div>
                  <!-- End Box Head -->
                  <form action="photo-gallery.php?action=add" method="post" enctype="multipart/form-data">
 					<table width="99%" border="0">
 					  <tr>
 					    <td>Name : 
 					      <label>
 					        <input name="fname" type="text" id="fname" required />
			            </label></td>
 					    <td>Picture : 
 					      <label>
 					        <input type="file" name="fpic" id="fpic" required />
			            </label></td>
				      </tr>
 					  <tr>
 					    <td colspan="2">Description : 
                        <label>
				        <input name="fdesc" type="text" id="fdesc" size="75" rows="1">
                        </label>
                        </td>
				      </tr>
				    </table>
<!-- 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">
						<a href="new-article.php" class="add-button"><span>Add new Article</span></a>
						<div class="cl">&nbsp;</div>

						<!-- Sort --><!-- End Sort -->
						
					</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>