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

<h1>Projects</h1>

<div style="width :700px">

<?php 

require_once("createthumb.php");
require_once("../includes/quickmysql.php");

    echo '<div style="float : left; width : 700px; height: 10px; padding:20px;">Add New Project</div>';
	echo '<form action="" method="post" enctype="multipart/form-data">';
	echo '<input type="hidden" name="insert" value="true" />';
	echo '<div style="float: left">';
	echo 'Title<br />';
	echo '<input type="text" name="title" size="50" value="" /><br />';
	echo 'Content<br/>';
	echo '<textarea name="desc" cols="50" rows="3"></textarea><br /><br />';
	echo '</div>';
	echo '<div style="float: left">';
	echo 'Image:<br />';
	echo '<input type="file" name="image"><br /><br />';
	echo '<input type="submit" name="submit" value="Save" /></div>';
	echo '<div style=" float : left; width : 700px; height: 10px; padding:20px;">Edit Existing Projects</div>';
	echo '</form>';
	
connect();

if (isset($_GET['delete'])){
  $query = "DELETE FROM projects WHERE id = " . $_GET['delete'] . ";";
  $result = mysql_query($query);
}

if (isset($_POST['submit'])){
  
  $image=$_FILES['image']['name'];
  $filename = stripslashes($image);
  
  if ($filename){
      $copied = copy($_FILES['image']['tmp_name'], "../images/" . $filename);
	  createThumbnail("image", 120, 120);
  }
	
	$title = mysql_real_escape_string($_POST['title']);
	$desc = mysql_real_escape_string($_POST['desc']);
	
	
	if (isset($_POST['insert'])){
	   $query = "INSERT INTO projects (`id`, `title`, `desc`, `image`) VALUES (NULL, '$title', '$desc', '$image');";
	}else{
	
	    // allows us to target a specific row from the database
	    $id = $_POST['id'];
	 
		$query = "UPDATE projects SET title = '$title', `desc` = '$desc' ";
		
		// don't update image field unless a file has been attached
		if ($filename){
		   $query .= ", image = '$image'";
		   
		}
		
		$query .= "WHERE id = '$id';";
	
	}

	$result = mysql_query($query);
	  
	if (!$result){
	  echo "There was an error saving the information. <br /><br />";
	}
}
  
  $query = "SELECT * FROM projects ORDER by id DESC;";
  $result = mysql_query($query) or die(mysql_error());
  
  $rows = mysql_num_rows($result);

  $data = array();
  if ($rows > 0){
   while($data = mysql_fetch_assoc($result)){
     removeSlashes($data);
     
    echo "<div>";
	echo '<form action="" method="post" enctype="multipart/form-data">';
	echo '<input type="hidden" name="id" value="'. $data["id"] .'" />';
	echo '<div style="float: left">';
	echo 'Title<br />';
	echo '<input type="text" name="title" size="50" value="' . $data["title"] .'" /><br />';
	echo 'Content<br/>';
	echo '<textarea name="desc" cols="50" rows="3">'. $data["desc"] . '</textarea><br /><br />';
	echo '</div>';
	echo '<div style="float: left">';
	echo 'Image:'. $data["image"] .'<br />';
	echo '<img src="../images/thumb_' .$data["image"] .'" width="40" height="40" /><br />';
	echo '<input type="file" name="image"><br /><br />';
	echo '<input type="submit" name="submit" value="Save" />';
	echo '<a href="?delete=' . $data["id"] . '"><button>Delete</button></a>';
	echo '</div></form></div>';
   }
  }

disconnect();
?>

</div>

<?php

require_once("footer.php"); ?>