<?
include '/home/mmaaaco/public_html/wdm3304materials/wdm3304week1/dbconnect.php';
/*
CREATE TABLE IF NOT EXISTS `3304gallcats` (
  `catid` int(5) NOT NULL auto_increment,
  `catname` varchar(255) NOT NULL,
  PRIMARY KEY  (`catid`)
);

CREATE TABLE IF NOT EXISTS `3304gallpix` (
  `id` int(10) NOT NULL auto_increment,
  `pictitle` varchar(255) NOT NULL,
  `picdesc` text NOT NULL,
  `catid` int(5) NOT NULL,  <-- how else will we code this?
  PRIMARY KEY  (`id`)
);
*/


?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Admin</title>
<style type="text/css">
#newcategory {display:none;}
</style>
<script type="text/javascript">
function checknew() {
var choice = document.getElementById("choice").value;
if (choice == 'new') {
document.getElementById("newcategory").style.display = 'block';
}
else {
document.getElementById("newcategory").style.display = 'none';
}
}
</script>
</head>
<body>
<div id = "wrapper">
<div id = "leftform">
<?
$deleteid = $_GET['deleteid'];
 if (is_numeric($deleteid)){  //is the url value a number? 
 $delsql = $dbh->prepare("delete from 3304gallpix where id = ?");//prepare statement
 $delsql->bindValue(1,$deleteid);//bind the value of the variable to the ?
 $delsql->execute();//run it
if ($delsql) echo 'Record Deleted<br/>';//$delsql is boolean...did it work?
if (file_exists('../images/'.$deleteid.'.jpg')){
unlink('../images/'.$deleteid.'.jpg');
}
 }
//show the pic, delete and edit
$sql = $dbh->prepare("select id,pictitle from 3304gallpix");
$sql->execute();
while($row = $sql->fetch()){//fetches records in a loop 
$id = $row['id'];//get id to display in links
$picname = 'images/'.$id.'.jpg';
$pictitle = $row['pictitle'];//get id to display name
echo '<img src = "../thumbnail.php?pic='.$picname.'&ht=60&wd=60"/> '.$pictitle.' <a href = "index.php?deleteid='.$id.'" 
onclick="return confirm(\' Are you sure you want to delete '.addslashes($pictitle).'\');">Delete</a> 
 <a href = "index.php?editid='.$id.'"> Edit</a><br/>';  //what the user sees!!!!!!!
}




?>
</div>
<div id = "rightform">
<?
$submit = $_POST['submit'];
if ($submit == 'Upload Pic'){
	$catid = $_POST['catid'];
	$catname = $_POST['catname'];
	
	//if new category created block: 
	if ($catname){  //new category created
	//insert block
	$sql = 	$dbh->prepare(" insert into 3304gallcats (catname) values (?)");
	$sql->bindValue(1, $catname);
	$sql->execute();
	//get last cat id block to put in pics table
	$getlast = $dbh->prepare("select catid from 3304gallcats order by catid desc limit 1");
    $getlast->execute();
	$getlastrow = $getlast->fetch();
	$catid = $getlastrow['catid'];	//this is the new id for the pics table
	}
	
$pictitle = $_POST['pictitle'];
$picdesc = $_POST['picdesc'];
//insert into pics table block
$eric = $dbh->prepare("insert into 3304gallpix (pictitle,picdesc,catid) values (?,?,?)");
$eric->execute(array($pictitle,$picdesc,$catid));

//get last pic id for picname block
$eric = $dbh->prepare("select id from 3304gallpix order by id desc limit 1");
    $eric->execute();
	$getlastrow = $eric->fetch();
	$picid = $getlastrow['id'];		
	$newpicname = $picid.'.jpg';  //new picname (4.jpg)
	
	//move file with new picname into images folder
$picfile = $_FILES['pic']['tmp_name'];
$success = move_uploaded_file($picfile,'../images/'.$newpicname);
if ($success) echo 'Yeah!';

//file to folder 	
	
	
}

//get values of record edited
$editid = $_GET['editid'];
if ($editid){
	//select statements are nothing more than what you want  from where you want it
	
	$editsql = $dbh->prepare("select  pictitle,picdesc,catid from 3304gallpix where id = '$editid'");
	$editsql->execute();
	$record = $editsql->fetch();
		$uppicname = $editid.'.jpg';
	$uptitle = $record['pictitle'];
	$uppicdesc = $record['picdesc'];

	$upcatid = $record['catid'];	
	//get the row of data 
	
	
}




//form with title,desc for pics w upload & drop down to select category>
?>
   <table style="width:400px;margin-left:auto;margin-right:auto">
<form action="index.php" method="post" enctype="multipart/form-data">
<tr><td colspan="2"><h2>Add a Picture</h2></td></tr>

<tr><td>
*Pic Title:</td><td> <input type = "text" name = "pictitle" required value="<? print  $uptitle;?>"/></td></tr>
<tr><td>*Upload:</td><td><?
if (file_exists('../images/'.$editid.'.jpg')){
echo '<img src = "../thumbnail.php?pic=images/'.$editid.'.jpg&ht=60&wd=60"/>';
}
?><input type = "file" name = "pic" required/></td></tr>
<tr><td colspan="2">*Description</td></tr>
<tr><td colspan="2"><textarea name = "picdesc" required rows="10" cols="35"><? print  $uppicdesc;?></textarea></td></tr>

<tr><td colspan="2">  
<!-- check to see if new category selected-->
<select name = "catid" id = "choice"  onchange="checknew();">
<option value="">Choose Below</option>';

<?
//populate dropdown with existing categories table
$sql = $dbh->prepare("select * from 3304gallcats");
$sql->execute();
while ($carolyn  = $sql->fetch()){
	$catid = $carolyn['catid'];	
	if ($catid == $upcatid){ $selected = 'selected="selected"';	}
	 echo '<option value="'.$carolyn['catid'].'" '.$selected.'>'.$carolyn['catname'].'</option>';
   unset($selected);
}
?>
<option value="new">Choose new Category</option>
</select>
<br/>
<span id = "newcategory"><br/>Category Name:<input type = "text" name = "catname"/><br/></span>
</td></tr>
<tr><td></td><td><input type = "submit" name = "submit" value = "Upload Pic" /></td></tr>
 </form>
 </table>
 </div>
 </div>
</body>
</html>
