<?php
/* 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";
if(isset($_SESSION["sess_artistid"]) and $_SESSION["sess_artistid"]!= ""){
	$artistid = $_SESSION["sess_artistid"];
}else{
	header("Location:login.php");
	exit;
}
if(checkArtistExpired($artistid)){
    header("Location:seller_plan_upgrade.php?sellerid=".$artistid);
    exit;
}

$sellerid=$_GET['sellerid'];
include('../includes/image.class.php');

$img = new Zubrag_image;

$maximagesizestringinPHP = ini_get("upload_max_filesize");
$maximagesizeinPHP = $maximagesizestringinPHP * (1024 * 1024);

$flag="true";

if(isset($_POST['btnSave']) && $_POST['btnSave']!='')
{ // check button for save 
$message="";
	$sellerid=$_POST['sellerid'];
	$recordsper=0;
		if (!isNotNull($_FILES['txtUploadZip']['name']))
		{
			$message .= "* Upload a Zip  file ! <br>";
		}
		$uploadfile =$_FILES['txtUploadZip']['name']; 
		$types=array('zip','ZIP','rar');
		preg_match('/\.([a-zA-Z]+?)$/', $uploadfile, $matches);
		$prodimagedir="../products";
		if (!is_readable($prodimagedir) || !is_writable($prodimagedir) || !is_executable($prodimagedir)) {
					$error = true;
					$message .= " * Change the permission of 'products' folder in the root to 777 <br>";
				}
		if(!in_array(strtolower($matches[1]),$types))
					{ // check file format is valid or not 
						$message .= "* Invalid file format for zip file ! <br>";
					} // end format check 
		
		if (!empty($_FILES['txtUploadZip']['name']))
		{ // moving files to folder
		
			$filename=$_FILES['txtUploadZip']['name'];
			
			$tempFile=$_FILES['txtUploadZip']['tmp_name'];
			if (!is_uploaded_file($_FILES['txtUploadZip']['tmp_name'])) 
			{
        $message .= "*  You can Upload maxium of <b>$maximagesizestringinPHP</b> at a time.Check your file size. <br>";
		}
		 if ($_FILES['txtUploadZip']['size'] >$maximagesizeinPHP)
		  {
       			 $message = "*  Size exceeds. Maximum allowed size is $maximagesizestringinPHP.<br>";
		  }
			if($message=='')
			{
			move_uploaded_file($_FILES['txtUploadZip']['tmp_name'], "../products/" . $_FILES['txtUploadZip']['name']);
        	$UnzipFlag = MyUnzip($_FILES['txtUploadZip']['name']);
        	if ($UnzipFlag) 
			{
            $FlagError = 1;
          $dirpath = "../products/".$filename;
			if (file_exists($dirpath)) 
			{
			 	unlink($dirpath);
			}
		} // end if Unzip
// Reading Files from Directory 
		
		$exp=explode(".",$_FILES['txtUploadZip']['name']);
		$zipfilename=$exp[0];
		$dirpaths="../products/zip/$zipfilename";
		
		if(is_dir($dirpaths) and file_exists($dirpaths))
		{
				
				$image_file_path=$dirpaths;
			
		}	
		
		else
		{
				$image_file_path="../products/zip";
		}
				
		if(file_exists($image_file_path))
		{ // check file folder exists
		$d = dir($image_file_path) or die("Wrong path: $image_file_path");
		while (false !== ($entry = $d->read())) {
		if($entry != '.' && $entry != '..' && !is_dir($entry)) 
		{
		$Images[] = $entry;
		}
		
			} // end while 
		
		$d->close();
		
		if(is_array($Images) && !empty($Images))
		{
				foreach($Images as $value)
				{ // forreach starts
					$imgname=$value;
				$exp=explode(".",$value);
				$newimage=ReplaceArrayImage($value); 
				$pname=$exp[0];
			if($pname!="Thumbs")
			{ // check thumb image exists
			$sql_t="select * from ".$tableprefix."products where product_name='".$pname."'  and product_artist_id='".$sellerid."' 
			 		 and product_image_small is null and  product_image_big is null ";
		  		 $rs_t=mysql_query($sql_t) or die(mysql_error());
				$num_rows=mysql_num_rows($rs_t);
				if($num_rows>0)
				{
				    $final_image_big = "productlarge_".time().$newimage;
					$final_image_small = "productsmall_".time().$newimage;
					$picsmalldest = $prodimagedir . $final_image_small;
					$picbigdest = $prodimagedir .$final_image_big;
					$picsmallname = $final_image_small;
					$picbigname = $final_image_big;
					
					//checking extension and replace
					$replaceArray=array('jpg','jpeg','gif');
					$findArray=array('JPG','JPEG','GIF');
					
					@list($piName,$ext)=@split("[.]",$final_image_small);
					$final_image_small=$piName.'.'.str_replace($findArray,$replaceArray,$ext);
			
					@list($piName,$ext)=@split("[.]",$final_image_big);
					$final_image_big=$piName.'.'.str_replace($findArray,$replaceArray,$ext);
					//checking extension and replace
		
				copy($image_file_path."/".$value,"../products/".$final_image_small);
				copy($image_file_path."/".$value,"../products/".$final_image_big);
				chmod("../products/$final_image_small", 0777);
				chmod("../products/$final_image_big", 0777);
				resizeImg("../products/".$final_image_small, 191 ,183, false, 100, 0,"_thumb");
				resizeImg("../products/".$final_image_big, 283 ,269, false, 100, 0,"_thumb");
				//generateThumbNail("../products/".$final_image_small,"../products/".$final_image_small,120,150);
				
				while($rr=mysql_fetch_array($rs_t))
				{
					$pid=$rr['product_id'];
					
					$sql_update="update ".$tableprefix."products set product_image_small='".$final_image_small."' 
					,product_image_big='".$final_image_big."'  where product_artist_id='".$sellerid."' and product_id='".$pid."'  ";
					$rs_query=mysql_query($sql_update) or die(mysql_error());
					$imageArray[]=$imgname;
				}	// end while 
		} // end if number of rows check		
		
	if($num_rows==0)
		{
			$imageArrays[]=$imgname;
			$flag="false";
			
		}
		if(mysql_affected_rows()>0)
		{
			$recordsper++;
			//header("Location:bulkupload.php?stat=Y");
		}	
	 } // end if thumb check 	
		// End if 		
	} // end foreach 
	
	
}  //end is_array check 

else
{
	$message.="Can not Extaract Folder , Error in Uploding.<br>";

}	 

	// Delete Images from Folder
		if(is_dir($dirpaths) and file_exists($dirpaths))
			{
				delDir($dirpaths);
			}	
		if(is_dir($image_file_path) and file_exists($image_file_path))
			{
				delFiles($image_file_path);
			}	
				// end delete 
} // end file folder check 	

	else
{
	$message.="Image folder doesnot exists , Error in Uploding.<br>";

}	 

		} // end  if message check 
    } // end if 	 zip upload chek 
	
} // end if button check 

include "../includes/htmltop.php";
?>
<script language="JavaScript" type="text/JavaScript">

function clickSearch()
{

	 document.frmProductBulkImages.submit();	
 
}

function blockImages(val)
{


	if(val == 'N')
	{

		return confirm('Are You Sure You Want To Disable The Product Bulk Images !!')
	
	}
	else if(val == 'Y')
	{
	
		return confirm('Are You Sure You Want To Enable The Product Bulk Images !!')
	
	}	
	

}

function deleteImages()
{
			
	return confirm('Are You Sure You Want To Delete The Product Bulk Images !!')
}

</script>

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

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

?>
				
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/artisttopmenu.php")
?>		
		
																					
								
					<div align="left" class="content_area">
					
					<!--content area start -->
							
					<div align="left" class="page_title">Bulk Products Image Upload</div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">																			
										
				
						<form action="" method="post" enctype="multipart/form-data" name="frmbulkupload">
							
						<?php if($recordsper==0 && $flag=="true")
						{ // display upload if error free
						?>	
						<table width="98%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
						
						<tr>
						  <td height="30" colspan="3"><strong>Upload  images for products </strong>(Product imagename should be same as productname)</td>
						  </tr>
						<tr>
						  <td height="45" colspan="3">Maximum Upload File Size : <b><?php echo $maximagesizestringinPHP;?></b></td>
						  </tr>
						<tr>
						  <td width="33%">Upload Zip File </td>
						  <td width="1%">&nbsp;</td>
						  <td width="66%"><input name="txtUploadZip" type="file" id="txtUploadZip"></td>
						</tr>
						<tr>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
						  <td><input type="hidden" name="sellerid" value="<?php echo $sellerid;?>"></td>
						</tr>
						<tr>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
						  <td>&nbsp;</td>
						</tr>
						<tr>
						  <td align="right"><input type="button" name="btnBack" value="Back To Bulk Product Upload" class="button" onClick="window.location.href='mybulkupload.php'"></td>
						  <td>&nbsp;</td>
						  <td height="50"><input type="submit" name="btnSave" value="Upload" class="button"></td>
						  </tr>
						</table>
				<?php } // end if  error free check ?>
				
				<?php if($recordsper>0  && $flag=="false")
						{ // check products uploaded or not 
						?>
						<table width="98%" border="0" cellpadding="8" cellspacing="4" class="manage_table">
						<tr>
						  <td height="46" colspan="3" align="left" class="text_information"><?php echo $recordsper?> Image(s)
						  saved successfully.</td>
						  </tr>
						<?php if(is_array($imageArrays) && count($imageArrays)>0){ ?>
						  <tr>
						  <td colspan="3" align="left" class="text_information">Following Image(s) Failed To Save. Productname Does not Exists!. Image name should be same as productname you have currently uploaded. </td>
						  </tr>
						  <?php 
						  $dd=1;
						  foreach($imageArrays as $pnames)
						  {
						  ?>
						   <tr>
						  <td colspan="3" align="left"><?php echo $dd. ")  ". $pnames;?></td>
						  </tr>
						  <?php
						  	$dd++;
						      } // end for each 
						   } // end if error 
					 ?>
						    <tr>
						  <td width="22%" height="50" align="left"><input type="button" name="btnBack" value="Back To Bulk Image Upload" class="button" onClick="window.location.href='mybulkimageupload.php?sellerid=<?php echo $sellerid;?>'"></td>
						  <td width="30%" height="50" align="left">&nbsp;</td>
						  <td width="48%" height="50" align="left">&nbsp;</td>
						 </tr>
						   
						  </table>
						  
						<?php }  // end if ?>  
						
				<?php if($recordsper==0  && $flag=="false")
						{ // check no images uploaded 
						?>
						<table width="98%" border="0" cellpadding="8" cellspacing="4" class="manage_table">
						<tr>
						  <td height="46" colspan="3" align="left" class="text_information"><?php echo $recordsper?> Image(s)
						  saved successfully.</td>
						  </tr>
						<?php if(is_array($imageArrays) && count($imageArrays)>0){ ?>
						  <tr>
						  <td colspan="3" align="left" class="text_information">Following Image(s) Failed To Save. Productname Does not Exists!. Image name should be same as productname you have currently uploaded. </td>
						  </tr>
						  <?php 
						  $dd=1;
						  foreach($imageArrays as $pnames)
						  {
						  ?>
						   <tr>
						  <td colspan="3" align="left"><?php echo $dd. ")  ". $pnames;?></td>
						  </tr>
						  <?php
						  	$dd++;
						      } // end for each 
						   } // end if error 
					 ?>
						    <tr>
						  <td width="22%" height="50" align="left"><input type="button" name="btnBack" value="Back To Image Upload" class="button" onClick="window.location.href='mybulkimageupload.php?sellerid=<?php echo $sellerid;?>'"></td>
						  <td width="30%" height="50" align="left">&nbsp;</td>
						  <td width="48%" height="50" align="left">&nbsp;</td>
						 </tr>
						   
						  </table>
						  
						<?php }  // end if ?>  
						
						
						<?php if($recordsper>0  && $flag=="true")
						{ // check no images uploaded 
						?>
						<table width="98%" border="0" cellpadding="8" cellspacing="4" class="manage_table">
						<tr>
						  <td height="46" colspan="3" align="left" class="text_information"><?php echo $recordsper;?> Image(s)
						  saved successfully.</td>
						  </tr>
						<?php if(is_array($imageArray) && count($imageArray)>0){ ?>
						  <tr>
						  <td colspan="3" align="left" class="text_information">Following Image(s) Saved Successfully !. </td>
						  </tr>
						  <?php 
						  $rr=1;
						  foreach($imageArray as $pnames1)
						  {
						  ?>
						   <tr>
						  <td colspan="3" align="left"><?php echo $rr. ")  ". $pnames1;?></td>
						  </tr>
						  <?php
						  	$rr++;
						      } // end for each 
						   } // end if error 
					 ?>
						    <tr>
						  <td width="22%" height="50" align="left"><input type="button" name="btnBack" value="Back To Product Upload" class="button" onClick="window.location.href='mybulkupload.php'"></td>
						  <td width="30%" height="50" align="left">&nbsp;</td>
						  <td width="48%" height="50" align="left">&nbsp;</td>
						 </tr>
						   
						  </table>
						  
						<?php }  // end if ?> 
					  </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";
?>
<script language="javascript">

function valiadtion()

{
		if(document.frmbulkupload.txtBulkUpload.value=="")
		{
		
			alert("Please Upload Your file");
			document.frmbulkupload.txtBulkUpload.focus();
			return false;
		
		}
		
		else
		{
		
		return true;
		
	}	

}

</script>