<?php
ob_start();
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php

include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";
include "../includes/class.upload.php";


if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE); 
} 

if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}


include "../includes/htmltop.php";



$select_product = "SELECT product_id,product_name FROM ".$tableprefix."products WHERE 1 ORDER BY product_name ";
$result_product = mysql_query($select_product) or die(mysql_error());

$select_image_count = "SELECT count(*) AS image_count FROM ".$tableprefix."product_view_images
				       WHERE nimage_productid = ".$_GET['edit_id'];
$result_image_count = mysql_query($select_image_count) or die(mysql_error());		

if(mysql_num_rows($result_image_count) > 0)
{

	$image_count_row = mysql_fetch_assoc($result_image_count);
	
}			  

			
if(isset($_POST['update']))
{

		$image_flag = 0;
		
		$update_product_images = "UPDATE ".$tableprefix."product_view_images
							  	  SET nimage_productid = ".GetSQLValueString($_POST['product'],"text").
								  " WHERE nimage_productid = ".$_GET['edit_id'];

		mysql_query($update_product_images) or die(mysql_error());
		
		for($i=1;$i<=$image_count_row['image_count'];$i++)
		{
	
			$product_image = "txtImage_".$i;
			$productfiletype 	= $_FILES[$product_image]['type'];
		    $productfilename 	= $_FILES[$product_image]['name'];
			$producttempname	= $_FILES[$product_image]['tmp_name'];

			$image_description  = "txtImageDesc_".$i;
			$description_buffer = "txtDescBuffer_".$i;
			$imgid_buffer       = $_POST['imgid_'.$i];
			$dir_dest           = "../products/bulk_images/";

			$update_product_images = "UPDATE ".$tableprefix."product_view_images
									  SET vimage_description = ".GetSQLValueString($_POST[$image_description],"text").
									" WHERE vimage_description = ".GetSQLValueString($_POST[$description_buffer],"text").
									" AND nproduct_image_id=$imgid_buffer
									AND nimage_productid = ".$_GET['edit_id'];
			mysql_query($update_product_images) or die(mysql_error());


				if($_FILES[$product_image]['name'][0] != "")
				{
					if (!isValidWebImageType($productfiletype,$productfilename,$producttempname))
					{
						
						$message .= " * Image$i is no a valid image !! Upload an image (jpg/gif/png)" . "<br>";
						$image_flag = 1;
						
					}
					else
					{
			
					
						$imagewidth_height_type_array = explode(":", ImageTypeBanner($_FILES[$product_image]['tmp_name']));
						$imagetype = $imagewidth_height_type_array[0];
			
						$bulk_image_buffer = "txtImageBuffer_".$i;	
						
						
			
						if($_FILES[$product_image] != '')
						{
			
						$imagewidth_height_type_array = explode(":", ImageTypeBanner($_FILES[$product_image]['tmp_name']));
						$imagetype = $imagewidth_height_type_array[0];
			
						$bulk_image_buffer = "txtImageBuffer_".$i;	
						
						list($org_name,$ext) = explode(".",$_FILES[$product_image]['name']);
						$name_replaced = "product_image_" . time() . $i . str_replace(' ','',$org_name);
						$name_thumb    = "thumb_product_image_" . time() . $i .  str_replace(' ','',$org_name);
			
						$assignedname  = "product_image_" . time() . $i . str_replace(' ','',$_FILES[$product_image]['name']);
						$thumbimage    = "thumb_product_image_" . time() . $i . str_replace(' ','',$_FILES[$product_image]['name']);

							$handle = new Upload($_FILES[$product_image]);
							 
							if ($handle->uploaded) 
							{
								
							 $handle->image_resize            = true;
                             $handle->image_x                 = 192;
		                     $handle->image_y                 = 183;
		                     $handle->file_new_name_body      = $name_thumb;
                             $handle->Process($dir_dest);
		   	   
		                     $handle->image_resize            = true;
                             $handle->image_x                 = 283;
		                     $handle->image_y                 = 269;
		                     $handle->file_new_name_body      = $name_replaced;
                             $handle->Process($dir_dest);
	
							if ($handle->processed) {

														   
								$update_product_images = "UPDATE ".$tableprefix."product_view_images
							  	  						   SET vimage_name = ".GetSQLValueString($assignedname,"text").
														  " WHERE vimage_name = ".GetSQLValueString($_POST[$bulk_image_buffer],"text").
														  " AND nimage_productid = ".$_GET['edit_id'];			
															
								mysql_query($update_product_images) or die(mysql_error());
							}	
													
								//$image_flag = 1;
								
								if($_POST[$bulk_image_buffer] != '')
								{
									$buffer_image = $_POST[$bulk_image_buffer];
									unlink('../products/bulk_images/'.$buffer_image);
								
								}		
								
																									  
							}					
							
							
							
						}
						 
									  
					}					  
								  
				}
							  
		}
		
		$select_product_images = "SELECT p.product_image_small,p.product_image_big
							  		FROM ".$tableprefix."products p WHERE p.product_id =".$_POST['product']." ";
					  
	     $result_product_images = mysql_query($select_product_images) or die(mysql_error());
		 
		 $rowimg	=	mysql_fetch_array($result_product_images);
		 
		 $product_image_small	= $rowimg['product_image_small'];
		 $product_image_big	= $rowimg['product_image_big'];
		 
		 if($product_image_small=='')
		 {
		 		$product_image 			= 	$_FILES['txtImage_1']['name'];
				
				$picsmallname 		= ReplaceArrayImage($_FILES['txtImage_1']['name']);
				$picsmallorgname 	= $_FILES['txtImage_1']['name'];
				$picsmalltempname	= $_FILES['txtImage_1']['tmp_name'];
				$picbigname 		= ReplaceArrayImage($_FILES['txtImage_1']['name']);
			
				$picsmalltype 		= $_FILES['txtImage_1']['type'];
				$picbigtype 		= $_FILES['txtImage_1']['type'];
				$picbigorgname		= $_FILES['txtImage_1']['name'];
				$picbigtempname		= $_FILES['txtImage_1']['tmp_name'];
			
				if($picbigname != "")
				{
					$final_image_big = "productlarge_".time().$picbigname;
				}
				else
				{
					$final_image_big = "";
				}
				
				if($picsmallname != "")
				{
					$final_image_small = "productsmall_".time().$picsmallname;
				}
				else
				{
					$final_image_small = "";
				}
				
				if($picsmallname != "" || $picbigname != "")
		{
				//checking extension and replace
				$replaceArray=array('jpg','jpeg','gif');
				$findArray=array('JPG','JPEG','GIF');
				 
				if(picsmallname)
				{
				@list($piName,$ext)=@split("[.]",$final_image_small);
				$final_image_small=$piName.'.'.str_replace($findArray,$replaceArray,$ext);
				//checking extension and replace
		        }
				
				if(picbigname)
                {
				@list($piNameBig,$extbig) = @split("[.]",$final_image_big);
				$final_image_big=$piNameBig.'.'.str_replace($findArray,$replaceArray,$extbig);
                }
			
			
			
	          $dir_dest = "../products";
		
        // we instanciate the class for each element of $file
         $handle = new Upload($_FILES['txtImage_1']);

        // then we check if the file has been uploaded properly
        // in its *temporary* location in the server (often, it is /tmp)
        if ($handle->uploaded) {
		
		   $handle->image_resize            = true;
           $handle->image_x                 = 191;
		   $handle->image_y                 = 183;
		   $handle->file_new_name_body      = $piName.'_thumb';
           $handle->Process($dir_dest);
		   
		   $handle->file_new_name_body      = $piName;
           $handle->Process($dir_dest);
		   
		   $handle->image_resize            = true;
           $handle->image_x                 = 283;
		   $handle->image_y                 = 269;
		   $handle->file_new_name_body      = $piNameBig.'_thumb';
           $handle->Process($dir_dest);
		   
		   $handle->file_new_name_body      = $piNameBig;
           $handle->Process($dir_dest);
    }
				
				 $sql_update	="UPDATE ".$tableprefix."products SET 
							 product_image_small='".$final_image_small."' ,product_image_big ='".$final_image_big."'
							 WHERE product_id =".$_POST['product']."  ";
				
				 $rs_query=mysql_query($sql_update)	 or die(mysql_error()); 
				
				
		 }
	}	 
						
	if($image_flag == 0)
	{
				  
		header('Location:list_productview_images.php?msg=Details Updated Successfully !!');	
		
		
		
	}	
		
}
?>
<script>
function checkAddBannerForm()
{

  var form_name = new Array('txtBannerName','txtBannerLocation','txtBannerImage');		
  var display_name = new Array('Banner Name','Banner Location Url','Banner Image');
  var count = 3;	

  if(check_fields('frmAddBanner',form_name,display_name,count))	
  {
  
  	
	 //document.frmAddBanner.submit();	
   
  }
  else
  {
  
  		return false;
  
  }
   
}

</script>

		<body>
		
		<center>
		
		<div align="left" id="layout">
		
		
				
				
				<!--header area start -->
<?php

	include_once("../includes/topheader_admin.php")

?>
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/admintopmenu.php")
?>		
																					
								
					<div align="left" class="content_area">
					
					<!--content area start -->
							
					<div align="left" class="page_title">Edit Product Images</div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">																			
		
					<form name="frmProductImages" action="#" method="POST" enctype="multipart/form-data" onSubmit="">
					
					<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
						<tr><td colspan="3">&nbsp;</td></tr>
						<tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;Fields are mandatory</td></tr>
						<tr>
						  <td align="left" valign=top>Product&nbsp;<span class="required">*</span></td>
						  <td >&nbsp;</td>
							<td valign=top align="left">
							<select name="product" class="textarea2" >
																						
<?php

	while($product_row = mysql_fetch_array($result_product))
	{
	
		if($product_row['product_id'] == $_GET['edit_id'])
		{
		
			$product_selected = "selected";
		
		}
		else
		{
		
			$product_selected = "";
		
		}
	
?>	
																							
																							
							<option value="<?php echo $product_row['product_id']?>" <?php echo $product_selected?>><?php echo stripslashes($product_row['product_name'])?></option>
	
	
	
<?php	
	
	}

?>						
						
						
						
						</select></td>
					</tr>

<?php

	
	/*$java_script = "<script>
	function checkProductImages()
	{";	*/
	
	
	$select_view_images = "SELECT vimage_name,vimage_description,nproduct_image_id
						   FROM ".$tableprefix."product_view_images
						   WHERE nimage_productid = ".$_GET['edit_id'];
	$result_view_images = mysql_query($select_view_images) or die(mysql_error());	
	
	$i = 1;
	
	if(mysql_num_rows($result_view_images) > 0)
	{
	
																								
		while($image_view_row = mysql_fetch_array($result_view_images))
		{															
		
				$bulk_image_name = "txtImage_".$i;
				$bulk_image_buffer = "txtImageBuffer_".$i;
				$bulk_image_description = "txtImageDesc_".$i;
				$description_buffer = "txtDescBuffer_".$i;
				$imgid_buffer = "imgid_".$i;
		
?>					
					
						<tr><td align="left">Image&nbsp;<?php echo $i?><span class="required">*</span></td><td>&nbsp;</td>
							<td align="left">
							<input type="file" name="<?php echo $bulk_image_name?>">
							&nbsp;[jpg,gif,png]
							&nbsp;<img src="../products/bulk_images/<?php echo $image_view_row['vimage_name']?>" height="40" width="60">
							<input type="hidden" name="<?php echo $bulk_image_buffer?>" value="<?php echo $image_view_row['vimage_name']?>">
							</td>
						</tr>
					
						<tr><td align="left" valign=top>Description</td><td >&nbsp;</td>
							<td valign=top align="left">
							<textarea name="<?php echo $bulk_image_description?>" rows="5" cols="15" class="textarea2"><?php echo $image_view_row['vimage_description']?></textarea>
							<input type="hidden" name="<?php echo $description_buffer?>" value="<?php echo $image_view_row['vimage_description']?>">
							<input type="hidden" name="<?php echo $imgid_buffer?>" value="<?php echo $image_view_row['nproduct_image_id']?>">

							</td>
						</tr>
<?php
	
	
				$i++;
			}	
			
		}	
		
		else
		{
										
?>					
							<tr>
									<td align="center" valign="middle" colspan="6"  class="text_information">No Records Found !!</td>
							</tr>					
<?php										

		}
	
		
?>					

							<tr>
								<td colspan="3" align="center">
									<input type="submit" value="Update" name="update" class="button">
									<input type="button" class="button" value="&nbsp;Back&nbsp;" name="back" onClick="window.location='list_productview_images.php'">
								</td>
							</tr>
				</table>
																										
				</form>
				
				</div>
						
						
				<!--content area end-->
								
				</div>
				
								
					<div align="left" class="clear_float"><img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				</div>
				
				
<?php
	include "../includes/footer.php";
	
	ob_end_flush();
?>