<?php
//User inserting an image from the system gallery is shown a list of categories
//On selecting a category, the list of images inside the category is shown, which on clicking on the image
//is posted back  using $_POST['selectedimage'] (filename).
//First we check for size limit for the user, which on failing the user is shown an alert box saying 'size limit exceeded' and the
//window is closed. 
//IF type == "new" then
//	work location is set as workarea/tempsites/tempsiteid/images/
//ELSE
//	work location is set as workarea/sites/siteid/images/
//END IF
//(i)	copy image from systemgallery/imageName to worklocation/imageName
//(ii)	set file permissions  as 777
//(iii)	IF type == "new" then watermarkimage() is called to watermark the image at   worklocation/imageName.
//(iv)	returnValue of the window is set as  worklocation/imageName and window is closed. 
  include "../../../../../includes/session.php";
  include "../../../../../includes/config.php";
  include "../../../../../includes/function.php";
    echo '<script type="text/javascript" src="../ajaxfilemanager/jscripts/ajaxfilemanager.js"></script>';
    echo '<script type="text/javascript" src="../ajaxfilemanager/jscripts/for_tinymce.js"></script>';
  $tmpsiteid=$_SESSION['session_currenttempsiteid'];
  $templateid=$_SESSION['session_currenttemplateid'];
  $userid=$_SESSION["session_userid"];
  $var_type=($_GET["type"] != "")?$_GET["type"]:$_POST["type"];
   $var_type="new";
   error_reporting(0);
  $basicLocation=fetchBasicLocation(1);
  $basicLocation=str_replace("/code/jscripts/tiny_mce/plugins","",$basicLocation);
   //$basicLocation=substr($_SERVER['SCRIPT_URI'],0,strpos($_SERVER['SCRIPT_URI'],"/code/")); //fetchBasicLocation(1);
  if($var_type == "new") {
	  $rootserver=$basicLocation . "/workarea/tempsites";
	  $workLocation = $_SERVER['DOCUMENT_ROOT'].'/davidpaul/'."workarea/tempsites/$tmpsiteid/images/";	
  }
  else {
	  $rootserver=$basicLocation . "/workarea/sites";
	  $workLocation=$_SERVER['DOCUMENT_ROOT'].'/davidpaul/'."workarea/sites/$tmpsiteid/images/";
  }
  $userid=$_SESSION["session_userid"];
  $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'] !=""){
         
		$size_per_user=0; 
		$allowed_space=0;		
		if(validateSizePerUser($userid,$size_per_user,$allowed_space,"../")) {
			 $filename=$_POST['selectedimage'];
			 $workLocation .= $filename;
			 copy($_SERVER['DOCUMENT_ROOT'].'/davidpaul/'."systemgallery/".$filename,$workLocation);
			 chmod($workLocation,0777);
			 if($var_type == "new") {
				// watermarkimage($workLocation);
			 }
			 
			 ?> "<SCRIPT>

			 //alert('<?php  echo $rootserver."/".$tmpsiteid."/images/".$filename; ?>');
	selectFile('<?php echo $rootserver."/".$tmpsiteid."/images/".$filename; ?>');
	</SCRIPT>

	<?php
			echo "<script>returnValue = new Object(); returnValue.url=\"http://clients.iscripts.com/davidpaul/$rootserver/$tmpsiteid/images/".$filename."\"; window.close();</script>";
		}
		else {
			echo "<script> returnValue = new Object(); returnValue.url=''; alert('Your space exceeded permitted limit." . 
					"(Space taken by you: " . human_read($size_per_user) . " " . 
					"Allowed space: " . human_read($allowed_space) . ") \\r\\n" . 
					"Delete unused images, or any/all of the sites created by you to proceed further.'); window.close(); </script>";
		}		
		
  }		 
$currentloc="../../../../../";	  
include "../../../../../includes/applicationheader.php";
?>

<SCRIPT>
window.name="targ";
function canceltblpro(){
		window.close();
  }
  function savetblpro(){
		document.frmImageUpload.submit();
		//opener.settablervalue(tborder,tborderclr,bgclr,cells,cellp,talign,twidth);
		//window.close();
  }

  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();
	 //alert(catid);
  }	
  
</SCRIPT>
</head>
<body>
<table width="100%" cellpadding=2  align=center>
   <tr>
      <td>
	              
	                <FIELDSET>
					  
	                   <legend class=maintextbold>Select category</legend>
	                   <form  method="post"   name="frmselectcategory" target="targ" action="ourgallery.php">
							 
							 <?php
							  $qry="select * from tbl_gallery_category order by vcat_name ";
							  $rs=mysql_query($qry);
							  
							 ?>
							
							 <table width="80%" bgcolor="#FFFFFF" border=0 align=center class="background">
							 <input type=hidden id="selectedcatname" name="selectedcatname">
							 <input type=hidden id="selectedcatid" name="selectedcatid">
							 <input type=hidden id="type" name="type" value="<?php echo $var_type; ?>">
						     <?php
								   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 stripslashes(htmlentities($row['vcat_name'])); ?></a>
										    <!-- <a class=anchor href="./ourgallery.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='2'>&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
    if($gallerrycatid>0){
  ?>
      <tr>
      <td>
	                  <FIELDSET>
					      <legend class=maintextbold>Images in <?php echo stripslashes(htmlentities($gallerycatname));?></legend>
						  <form  method="post"   name="frmImageUpload" target="targ">
						   <input type=hidden id="type" name="type" value="<?php echo $var_type; ?>">
						      <table border=0 width="70%" align=center>
						       <?php
							       $sql=" select * from tbl_gallery where ngcat_id='".$gallerrycatid."'";
								  
								   //$totalrows = mysql_num_rows(mysql_query($sql));
								   //$navigate = pageBrowser($totalrows, 10, 10, "&gcatname=$gallerycatname&gcatid=$gallerrycatid&",$numbegin,$start,$begin, $num);
									// execute the new query with the appended SQL bit returned by the function
								   //$sql = $sql . $navigate[0];
								   
								   $rs = mysql_query($sql);
								   
							       //$rs=mysql_query($qry);
								   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']);
										$file=basename($row['vimg_url']);
										
							 ?>
							              
										  <td>
										     <FIELDSET>
										      <TABLE>
											     <tr>
												   <td><img 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==1){
										  echo "<td colspan='2'>&nbsp;</td></tr>";
										}else if($i==2){
										  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>	  				  	  

</body>
</html>
<script>
//window.reset();
/*document.getElementById("trows").value="";
document.getElementById("tcolumns").value="";
document.getElementById("tbgclr").value="";
document.getElementById("tcellpading").value="";
document.getElementById("tcellspacing").value="";
document.getElementById("tborder").value="";
document.getElementById("tborderclr").value="";*/
</script>
