<?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: jimmy<jimmy.jos@armia.com>        		              	  |
// |          									                          |
// +----------------------------------------------------------------------+
//Initially the page is shown with a list of images from which the user is asked to select
//All the images inside usergallery/userid/images/ are listed
//Once the user selects an image by clicking on it the page is posted back with 
//the filename in POST["selectedimage"]
//IF type == "new" then
//	(i)	  image is copied from usergallery/userid/images/ to workarea/tempsites/tempsiteid/images/
//	(ii)  permissions set to 777 for the workarea/tempsites/tempsiteid/images/imageName	
//	(iii) watermarkimage() function is called to watermark workarea/tempsites/tempsiteid/images/imageName
//	(iv)  using javascript we call a function with a 100ms delay after seting the return url as 
//			workarea/tempsites/tempsiteid/images/imageName
//ELSE
//	(i)	  image is copied from usergallery/userid/images/ to workarea/sites/siteid/images/
//	(ii)  permissions set to 777 for the workarea/sites/siteid/images/imageName	
//	(iii)  using javascript we call a function with a 100ms delay after seting the return url as 
//			workarea/sites/siteid/images/imageName
//END IF
  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="edit";
   
  $basicLocation=fetchBasicLocation(1);
   $basicLocation=substr($_SERVER['SCRIPT_URI'],0,strpos($_SERVER['SCRIPT_URI'],"/code/")); //fetchBasicLocation(1);
  if($var_type == "new") {
	  $rootserver=$basicLocation . "/workarea/tempsites";
	  $workLocation = "../../../../../workarea/tempsites/$tmpsiteid/images/";	
  }
  else {
	  $rootserver = $basicLocation . "/workarea/sites";
	  $workLocation="../../../../../workarea/sites/$tmpsiteid/images/";
  }
 if($_POST['selectedimage'] !=""){
		 $filename=$_POST['selectedimage'];
		 $workLocation .= $filename;
		 copy("../../../../../usergallery/$userid/images/".$filename,$workLocation);
		 chmod($workLocation,0777);
		 if($var_type == "new") {
		 	 watermarkimage($workLocation);
		 }
		 ?> "<SCRIPT>
	selectFile('<?php echo $rootserver."/".$tmpsiteid."/images/".$filename; ?>');
	</SCRIPT>

	<?php
	     echo  "<script> returnValue = new Object(); 
		 setTimeout(\"closeMe()\",100);
		 function closeMe() {
 	     returnValue.url=\"$rootserver/$tmpsiteid/images/".$filename."\";
		 window.close();
		 } </script>";
		
  }	
  $currentloc="../../../../../";	  
include "../../../../../includes/applicationheader.php";	 
?>

	<SCRIPT>
	 window.name="targ";
	 returnValue = new Object();
	returnValue.url = ""; 
	 function closeGallery(){
	 		returnValue = new Object();
			returnValue.url = ""; 
	        window.close();
	  }
	 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();
	  }	
	</SCRIPT>
</head>
<body>
<table width="100%" cellpadding=2 align=center>
   <tr>
      <td>
	                  <FIELDSET>
					      <legend class=maintextbold>User Gallery - Click on the image you want to select...</legend>
						    <table>
							<tr>
								<td align="center" colspan=4>
									<a href="#" onClick="javascript:closeGallery();" class="maintext">Cancel without selecting image</a>  
								</td>
							</tr>
							 <form method="post"   name="frmImageUpload" target="targ">
							  <input type="hidden" name="type" id="type" value="<?php echo($var_type); ?>">
						     <?php
								        $dirpath="../../../../../usergallery/$userid/images/";
								        $handle = opendir($dirpath);
								        $i=0;
								        while( false !== ($file = readdir($handle))){
										  if ($file != "." && $file != ".." && $file !="Thumbs.db" && $file!="thumbimages") {
														if($i==0) {
															echo "<tr>";
														}
														if($i==4) {
														     $i=0;
														 	 echo "</tr><tr>";
														}
											
										
							 ?>
							              
												  <td>
												     <FIELDSET>
												      <TABLE>
													     <tr>
														   <td> <img id="<?php echo $file; ?>" src="<?php echo $dirpath; ?><?php echo $file; ?>" width=100 height=100 onclick="selectthisimags(this.id)"></td>
														 </tr>
													  </TABLE>
													  </fieldset>
										              
										         </td>
										   
										    
								     
							             
							 <?php	
								                     $i++;
													
								   		}
										
										
								   }
								   if($i<=3){
									  echo "<td colspan='3'>&nbsp;</td></tr>";
								  }else if($i==4){
									  echo "</tr>";
							   	  }
							?>
							      
								 <input type=hidden name=selectedimage id=selectedimage value="">
								  </form>
							  </table>
						  
						  
					  </FIELDSET>
	  </td>
	</tr>
</table>	  				  	  
<?php
  include "./includes/editorfooter.php";
?> 

