<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2005-2006 ARMIA INC                                    |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts EasyCreate 1.1                 |
// +----------------------------------------------------------------------+
// | Authors: sudheesh<sudheesh@armia.com>        		              	  |
// |          									                          |
// +----------------------------------------------------------------------+

/* User can select our gallery images here
   in this page top fieldset contains list of gallery category and 
    bottom fieldset contains the list of images under selected gallery category
*/
  include "../includes/session.php";
  include "../includes/config.php";
  include "../includes/function.php";
 
  $tmpsiteid=$_SESSION['session_currenttempsiteid'];
  $templateid=$_SESSION['session_currenttemplateid'];
  $userid=$_SESSION["session_userid"];
  $siteid=$_SESSION['session_siteid'];
  if($_SESSION['session_published']=="yes"){
			     $urllocation="../workarea/sites/$siteid/images/";
  }else{
			     $urllocation="../workarea/tempsites/$tmpsiteid/images/";
  }
  $tmpsiteid=$_SESSION['session_currenttempsiteid'];
  $templateid=$_SESSION['session_currenttemplateid'];
  $userid=$_SESSION["session_userid"];
  /* when user click on gallery category 'selectedcatid' will be passed as hidden variable.
  */  
  $gallerrycatid=addslashes($_POST['selectedcatid']);
  $gallerycatname=addslashes(urldecode($_POST['selectedcatname']));
  if($gallerrycatid==""){
	      $numbegin=$_GET['numBegin'];
		  $start=$_GET['start'];
		  $begin=$_GET['begin'];
		  $num=$_GET['num']; 
		  $gallerrycatid=addslashes($_GET['gcatid']);
		  $gallerycatname=addslashes(urldecode($_GET['gcatname'])); 
  }
 if($_POST['selectedimage'] !=""){
    	 $filename=$_POST['selectedimage'];
		 $assignedname=$filename;
		 copy("../systemgallery/".$filename,$urllocation.$assignedname);
		 chmod($urllocation.$assignedname,0777);
		 if($_SESSION['session_published'] !="yes")
		 		watermarkimage($urllocation.$assignedname);
		 echo "<script>returnValue = new Object(); returnValue.url=\"./images/".$assignedname."\"; window.close();</script>";
		
  }		 
$currentloc="../";	  
include "../includes/applicationheader.php";
?>

	<SCRIPT>
	window.name="targ";
	  function canceltblpro(){
	  returnValue = new Object();
	  returnValue.url = "";
	        window.close();
	  }
	  function savetblpro(){
			document.frmImageUpload.submit();
	  }
	
	  function selectthisimags(id){
	  	 document.getElementById("selectedimage").value=id;
	     document.frmImageUpload.submit();
	  }	
	  function showimagesincategory(catname,catid){
	     document.getElementById("selectedcatname").value=catname;
	     document.getElementById("selectedcatid").value=catid;
	     document.frmselectcategory.submit();
	  }	
	  
	</SCRIPT>
<body>
<table width="100%" cellpadding=0 cellpadding=0 align=center border=0>
   <tr>
   
    <td class=errormessage align=center>
	<?php
	 if(!validateSizePerUser($_SESSION["session_userid"],$size_per_user,$allowed_space)) {
           echo "The space taken by you has exceeded the allowed limit.<br>(Space taken by you: " . human_read($size_per_user) . " <br>Allowed space: " . human_read($allowed_space) . ")<br>Delete 
		   unused images or any/all of the sites created by you to proceed further.<br>&nbsp;<br>";
		   exit;
     }
	?>
	</td>
   </tr>
   <tr>
     <td align=center><a class="editorlinks" href="#" onclick="canceltblpro();">Cancel without selecting image</a></td></td>
   </tr>
   
   
   <tr>
      <td>
	              
	                <FIELDSET>
					  
	                   <legend class=maintextbold>Select category</legend>
	                   <form  method="post"   name="frmselectcategory" target="targ" action="ourgallerynew.php">
							 
							 <?php
							  $qry="select * from tbl_gallery_category order by vcat_name ";
							  $rs=mysql_query($qry);
							  
							 ?>
							
							 <table width="100%" bgcolor="#FFFFFF" border=0 align=center class=background>
							 <input type=hidden id="selectedcatname" name="selectedcatname">
							 <input type=hidden id="selectedcatid" name="selectedcatid">
						     <?php
							       /* list the gallery category,two  ctegory in one row */ 
								   if(mysql_num_rows($rs)>0){
								        $i=0;
								        while($row=mysql_fetch_array($rs)){
										if($i==0) {
											echo "<tr>";
										}
										if($i==2) {
										     $i=0;
										 	 echo "</tr><tr>";
										}
										$catname=urlencode($row['vcat_name']);
										
							 ?>
							              
										   <td>
										     <a class=anchor href="#" onclick="showimagesincategory('<?php echo $catname?>','<?php echo $row["ngcat_id"]?>')"><?php echo stripslashes(htmlentities($row['vcat_name']))?></a>
										    <!-- <a class=anchor href="./ourgallerynew.php?catname=<?php echo $catname?>&catid=<?php echo $row['ngcat_id']?>"><?php echo $row['vcat_name']?></a>-->
										   </td>
										   
										    
								     
							             
							 <?php	
								        $i++;
								   		}
										if($i<=1){
										  echo "<td colspan='3'>&nbsp;</td></tr>";
										}else if($i==2){
										  echo "</tr>";
										}
										
								   }else{
							?>
							      <tr><td class=maintext>No categories found</td></tr>
							<?php	     	
								   }
							 ?>
							   
						 </table>
						 </form>
						 </FIELDSET>
	  
	  </td>
   
   </tr>
  
  <?php
     //check user selected a gallery category.*/
    if($gallerrycatid>0){
  ?>
      <tr>
      <td >
	                  <FIELDSET >
					      <legend class=maintextbold>Images in <?php echo stripslashes(htmlentities($gallerycatname))?></legend>
						  <form  method="post"   name="frmImageUpload" target="targ">
						      <table  width="100%" align=center border=0>
						       <?php
							       /* list all the images under selected gallery category.4 images in one row*/
							       $sql=" select * from tbl_gallery where ngcat_id='".$gallerrycatid."'";
								   $rs = mysql_query($sql);
								   if(mysql_num_rows($rs)>0){
								   echo("<tr><td colspan=3 class=maintextbold><br>Click on the image you want to select<br></td></tr><tr><td colspan=3>&nbsp</td></tr>");
								        $i=0;
								        while($row=mysql_fetch_array($rs)){
										if($i==0) {
											echo "<tr>";
										}
										if($i==4) {
										     $i=0;
										 	 echo "</tr><tr>";
										}
										$catname=urlencode($row['vcat_name']);
										$file=basename($row['vimg_url']);
										
							 ?>
							              
										  <td width="30%" >
										     <FIELDSET >
										      <TABLE border=0>
											     <tr>
												   <td ><img style="cursor:hand;" id="<?php echo htmlentities($file)?>" src="../systemgallery/<?php echo htmlentities($file)?>" width=100 height=100 onclick="selectthisimags(this.id)"></td>
												 </tr>
											  </TABLE>
											  </fieldset>
								              
								         </td>
										   
										    
								     
							             
							 <?php	
								        $i++;
								   		}
										if($i<=3){
										  echo "<td >&nbsp;</td></tr>";
										}else if($i==4){
										  echo "</tr>";
										}
										
								   }else{
							?>
							       <tr><td class=maintext colspan=2 align=center>No Images found</td></tr>
							<?php	     	
								   }
							 ?>
						    
							   
							      
								 <input type=hidden name=selectedimage id=selectedimage value="<?php echo($url); ?>">
								  </form>
							  </table>
						  
						  
					  </FIELDSET>
	  </td>
	</tr>
  <?php	
	}
  ?>
  
</table>	  				  	  
<?php
  include "./includes/editorfooter.php";
?> 
