<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}

if (!file_exists('../custom/gallery')) {
    mkdir('../custom/gallery', 0777, true);
	//echo"created gallery folder";
}
if (!file_exists('../custom/gallery/thumbnails')) {
    mkdir('../custom/gallery/thumbnails', 0777, true);
	//echo"created thumbnail folder";
}
//require_once ('mysql_connect.php');
$action          = $_GET["a"];
$editgallery          = $_POST["gallery"];

if($_GET["upload"]=='1'){
	include('gallery_upload.php');
}
if(empty($editgallery)){
	$editgallery=$_REQUEST["gallery"];
}
//echo '$editgallery='.$editgallery;

if(empty($editgallery)){
	$editgallery = $_GET["key"];
}
	//echo 'Action = ' . $action; (for testing)
//require_once ('config.inc');
if ($action == "upload") {
    include('cpn-img.php');
    
}

if ($action == "remove") {
	//get image name for use later
	$query1 = "select filename from gallery_images where recordkey ='" . $_GET[image]."'" ;
	$pg=mysql_query($query1);
	while($pnt=mysql_fetch_array($pg)){
		$image_name = $pnt[filename];
	}
    $query1 = "delete from gallery_images where recordkey ='" . $_GET[image]."'" ;
		if(mysql_query($query1)){
			echo"<div id='submission-success-message-wrapper'><font color=red>****** Image successfully removed from gallery ******</font></div>";
		}
	//check if image in any other galleries. if not, remove from server
	$query1 = "select * from gallery_images where filename ='$image_name'" ;
	$pg=mysql_query($query1);
	if(mysql_num_rows($pg)==0){
		$image_path = "../custom/gallery/$image_name";
		unlink($image_path);
		$image_path = "../custom/gallery/thumbnails/$image_name";
		unlink($image_path);
	}
	
}

if ($action == "add") {
			
			$query = "Insert into gallery (gallery_name) Values ('" . $_POST['title'] . "')";

			if (!mysql_query($query)){
					echo $query;
  					die('Error: ' . mysql_error());
 			 } else {
				echo "<div align=center><font color=red>Gallery added successfully.</font></div>";
				
			}
		}


if (($action == "update")&&($_GET["upload"]!='1')) {
	$delete = $_POST['chkdelete'];
	$active = $_POST['chkactive'];

	if (empty($delete)) {
		//delete not checked, save other updated info

		$query = "UPDATE gallery SET gallery_name = '" . $_POST['title'] . "',  Active ='";
		if (empty($active)){
			$query = $query . "0'";
		} else {
			$query = $query . "1'";
		}
	

		$query = $query . " WHERE gallerykey = '" . $_GET['key'] ."'" ;
		//echo $query;
		mysql_query($query) or die(mysql_error());   //execute query
		
		//confirmation message that update worked...
		echo "<div id='submission-success-message-wrapper'><font color=red>****** Gallery updated successfully ******</font></div>";

	} else { 
		
		
		$query = "DELETE FROM gallery WHERE gallerykey = '" . $_GET["key"] ."'" ;
	
	

	mysql_query($query) or die(mysql_error());   //execute query

	//confirmation message that delete worked...
	echo "<font color=red>gallery deleted successfully.</font>";

	}
}	//end of update query
	
echo '<div id="add-new-wrapper_gallery"> <form action="' . $_SERVER['PHP_SELF'] . '?a=add" method="post">Add new gallery: (title)<br /><input type="text" name="title" size=33>
		<input id="save-button" type="submit" name="save" value="Save">
		</form></div>';
	//echo "<center><font color='red'>Note: this page is used to manage the galleries shown on your gallery page.<br>
	//If you want to change the other content on the gallery page, <a href='editpages.php?a=select&pagename=galleries'>click here</a>.</font></center>";
	
   //create selector to choose gallery to edit
 echo '<div><form action="' . $_SERVER['PHP_SELF'] . '?a=select" method="post">';
 echo'<div>Choose gallery to edit:<br /><select style="width:220px;" name="gallery"><option value="">Make Selection</option>';
 $pages="Select gallerykey, gallery_name from gallery";
 $pg=mysql_query($pages);
  while($pnt=mysql_fetch_array($pg)){
   echo '<option value='. $pnt[gallerykey];
   if($pnt[gallerykey]==$editgallery){echo ' selected="selected"';}
   echo'>' .$pnt[gallery_name] . '</option>';
}

mysql_close;
echo '</select> <input id="save-button" type="submit" name="select" value="Edit"></form>&nbsp; &nbsp; &nbsp;  - OR - <br /><br /></div>';

//filter based on selected gallery, or select most recently added
    if(!empty($editgallery)){
    $query = "SELECT * from gallery where gallerykey ='" . $editgallery ."'";
    } else{
      $query2="Select max(gallerykey) as gallerykey from gallery";
      $rs2 = mysql_query($query2);
      while($nt2=mysql_fetch_array($rs2)){
          $query = "SELECT * from gallery where gallerykey ='" . $nt2[gallerykey]."'" ;
      }
      mysql_close;
  }

	

	$rt=mysql_query($query);          // query executed 
	echo mysql_error();                    // if any error is there that will be printed to the screen



//echo "gallery=" . $editgallery;
echo '<div align="center"><table border=0 cellpadding=0 cellspacing=8>';
if(!empty($editgallery)){
//echo "gallery=".$editgallery;
	while($nt=mysql_fetch_array($rt)){
	//set variables for use outside of record loop
	

	$title = $nt[title];
	$description = $nt[body];
	
  echo '<a name="' . $nt[gallerykey] . '"></a>';
	echo '<tr><td><div align=left>Gallery #:<td valign=top><div align=left><input type="text" name="number" size="30" value="' . $nt[gallerykey] . '" disabled="disabled"> 
	Gallery code: [gallery' . $nt[gallerykey] . ']';


	echo '<form action="' . $_SERVER['PHP_SELF'] . '?a=update&key=' . $nt[gallerykey] . '" method="post">
	<tr><td><div align=left>Gallery name:<td valign="top"><div align=left><input type="text" name="title" size="60" value="' . $nt[gallery_name] . '">';

	
	//echo '<tr><td><div align=left>Description:<td valign=top><div align=left><textarea cols=42 rows=3 name="body">' . $nt[body] . ' </textarea>';  

echo '<tr><td colspan=2>';

echo '<tr><td><div align=left>Active:<td valign=top><div align=left><input type="checkbox" name="chkactive"';
	if(!empty($nt[active])){
		echo 'checked=true';
	} 
echo '>';

 echo '<tr><td valign=top><div align=left>Delete this gallery:<td valign=top><div align=left><input type="checkbox" name="chkdelete" value="true">';


	echo '<tr><td><td valign=top><div><input id="save-button" type="submit" name="save" value="Save"></form>';
	
	echo '<tr><td colspan=2><h2>Images in gallery:</h2>' ;
	$image_qry = "Select * from gallery_images where gallerykey='$editgallery'";
	$imrt=mysql_query($image_qry);
	$x=1;
	while($imnt=mysql_fetch_array($imrt)){
		//add dynamic css
		echo "<div id='gallery-thumbnail-wrapper'><div id='gallery-thumbnail'><img src='../custom/gallery/thumbnails/$imnt[filename]'></div><a href='$_SERVER[PHP_SELF]?a=remove&image=$imnt[recordkey]&gallery=$editgallery'><div class='gallery-icons'><img src='images/delete-icon.png' alt='delete'></a>
		<a id='gt_" . $x . "' href='#' class='popup' data-popup-id='".$x."'><img src='images/write-icon.png' alt='write'></a></div></div>";
		echo"<div id='popup_$x' class='toPopDiv gallery'> 
	        <div class='close'></div>
	       	<span class='ecs_tooltip'>Esc to close <span class='arrow'></span></span>
			<div id='popup_content'> <!--your content start-->
	            Image $x caption: <input id='cap_" . $imnt['recordkey'] . "' type='text' name='caption' value='$imnt[image_caption]'><button class='update-image' id='img_" . $x . "' data-img-id='".$imnt['recordkey']."' onclick='return false;'>Update</button><!--<input type='submit' name='save' value='Update'>-->
				<br />
	        </div> <!--your content end-->
	    </div> <!--toPopup end-->";
	$x++;
	}	
}
}
    echo '</table></div></table></div></div>';

?>
<div align="center"><br /><hr> 
<?php
if(!empty($editgallery)){

	echo '<form enctype="multipart/form-data" action="' . $_SERVER['PHP_SELF'] . '?a=update&key=' . $nt[gallerykey] . '&upload=1" method="post">';
?>
	<input type="hidden" id="server_address" value="' <?php echo $_SERVER['PHP_SELF'] ?> '" />
	<input type="hidden" name="MAX_FILE_SIZE" value="50000000"> 
	<input type="file" name="pix[]" size="60" id ="pix[]"><br> 
	<input type="file" name="pix[]" size="60" id ="pix[]"><br> 
	<input type="file" name="pix[]" size="60" id ="pix[]"><br> 
	<input type="file" name="pix[]" size="60" id ="pix[]"><br> 
	<input type="file" name="pix[]" size="60" id ="pix[]"><br> 
	<input type="file" name="pix[]" size="60" id ="pix[]"> 
	<input type="hidden" name="gallery" value="<?php echo $editgallery;?>">
	<p><input id="save-button" type="submit" name="Upload" value="Upload Picture"> 
	</form> 
<?php
} else {
	echo "<br><br><font color='red'>No gallery selected. Please select a gallery from the dropdown list or add a new gallery.</font>";
} 
echo '<br><br><div align="center"><br /><hr /><!--
<form action="' . $_SERVER['PHP_SELF'] . '?a=add" method="post">Add new gallery: (title) <br><input type="text" name="title" size=50>

		<input id="save-button" type="submit" name="save" value="Save">
		</form>-->
		<br></div>';

?>
