<?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_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;
}

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


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

/*taken from the general site settings table*/

$check_product_image_count = array('nproduct_images');
$settings_product_image_count = getSiteGeneralSettings($check_product_image_count);

$invalid_image_flag = 0;

if (isset($_GET["pname"]) and $_GET["pname"] != "") {
    $pname = $_GET["pname"];
	}

if (isset($_GET["productid"]) and $_GET["productid"] != "") {
    $productid = $_GET["productid"];
	}

if (isset($_POST['product']) and $_POST['product'] != "") {
    $productid = $_POST['product'];
	}	
	
if(!empty($productid))
{

	$select_bulk_images = "SELECT nproduct_image_id FROM ".$tableprefix."product_view_images 
						   WHERE nimage_productid = ".GetSQLValueString($_POST['product'],"text");
						   
	$result_bulk_images = mysql_query($select_bulk_images) or die(mysql_error());
	
	$total_image_count = mysql_num_rows($result_bulk_images);
	
	



}
			
if(isset($_POST['add']))
{

			
		for($i=1;$i<=$settings_product_image_count[0];$i++)
		{
	
			$product_image 		= "txtImage_".$i;
			$productfiletype 	= $_FILES[$product_image]['type'];
		    $productfilename 	= $_FILES[$product_image]['name'];
			$producttempname	= $_FILES[$product_image]['tmp_name'];
			$product_description= "txtDescription_".$i;
			$dir_dest           = "../products/bulk_images/";
						
				if($_FILES[$product_image]['name'][0] != "")
				{
					if(!isValidWebImageType($productfiletype,$productfilename,$producttempname))
					{
																	
						$message .= TEXT_IMAGE.$i.MSG_INVAL_IMG_UPLD. "<br>";
						$invalid_image_flag = 1;
																		
					}
					else
					{
			
						$imagewidth_height_type_array = explode(":", ImageTypeBanner($_FILES[$product_image]['tmp_name']));
						$imagetype = $imagewidth_height_type_array[0];
						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) {
                                															
							$insert_product_images = "INSERT INTO ".$tableprefix."product_view_images(nimage_productid,vimage_name,vimage_description,vproduct_image_delete_status)
											    	  VALUES(".GetSQLValueString($_POST['product'],"text").","
											  				.GetSQLValueString($assignedname,"text").","
															.GetSQLValueString($_POST[$product_description],"text").",'N')";
							mysql_query($insert_product_images) or die(mysql_error());									  
		

                             } 
                         } 
									  
					}					  
								  
				}
							  
							  
							  
		}						
		
		$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()); 
				
				
		 }
	}	   
	
	header('Location:list_productview_images_artist.php?msg=Images Added 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_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"><?php echo TEXT_ADD_PROD_IMG;?></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="return checkProductImages();">
						<table cellpadding="0" cellspacing="0" width="100%" class="manage_table">
							<tr><td colspan="3">&nbsp;</td></tr>
							<tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo MSG_FIELD_MANDATORY;?></td></tr>
							
							<?php if(isset($_GET['pname']) &&  $_GET['pname']!=''  && isset($_GET['productid']) &&  $_GET['productid']!='')
							{
							?>
							<tr>
							 <td align="left" valign=top><?php echo TEXT_PRODUCT;?>&nbsp;<span class="required">*</span></td>
							 <td >&nbsp;</td>
							<td> <input type="text" class="textarea" name="pname" value="<?php echo $pname;?>"> 
								<input type="hidden" name="product" value="<?php echo $productid;?>"></td>
							</tr>
							<?php
							}
							else
							{
							?>
							<tr>
							  <td align="left" valign=top><?php echo TEXT_PRODUCT;?>&nbsp;<span class="required">*</span></td>
							  <td >&nbsp;</td>
								<td valign=top align="left">
								<select name="product" class="textarea" onChange="Javascript:document.frmProductImages.submit();">
								<option value="">--<?php echo TEXT_SEL_ONE;?>--</option>
		
<?php
		while($product_row = mysql_fetch_array($result_product))
			{
			
				$select_bulk_images_count = "SELECT nproduct_image_id FROM ".$tableprefix."product_view_images 
											 WHERE nimage_productid = ".$product_row['product_id'];
				
				$result_bulk_images_count = mysql_query($select_bulk_images_count) or die(mysql_error());
				
				if($product_row['product_id'] == $_POST['product'])
				{
				
					$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
							}
							?>
<?php
		
			
			$java_script = "<script>
			function checkProductImages()
			{";	
			
			$java_script .=" if(trimSpaces(document.frmProductImages.product.value) == '')
							 {
								alert('Product is a required field !!');
								return false;
							 }";
							 
			$total_image_count =  $settings_product_image_count[0] - $total_image_count;
			
			
			for($i=1;$i<2;$i++)
			{
						
				$bulk_image_name 		= "txtImage_".$i;
				$bulk_image_description = "txtDescription_".$i;
				
				$java_script .=" if(trimSpaces(document.frmProductImages.$bulk_image_name.value) == '')
								 {
									alert('Image$i is a required field !!');
									document.frmProductImages.$bulk_image_name.focus();
									return false;
								 }";
			}
			
			
			for($i=1;$i<=$total_image_count;$i++)
			{
				
				$bulk_image_name 		= "txtImage_".$i;
				$bulk_image_description = "txtDescription_".$i;
				
				if($i >= 2)
				{
					$required_message = "";
				}
				else
				{
					$required_message = "<span class='required'>*</span>";
				}
				
				
	
?>					
							
							
							
							<tr><td align="left" valign=top>I<?php echo TEXT_IMAGE;?>&nbsp;<?php echo $i?><?php echo $required_message?></td><td >&nbsp;</td>
								<td valign=top align="left"><input type="file" name="<?php echo $bulk_image_name?>">
								&nbsp;[jpg,gif,png]
								</td>
							</tr>
							
							<tr><td align="left" valign=top><?php echo TEXT_DESC;?></td><td >&nbsp;</td>
								<td valign=top align="left">
								<textarea name="<?php echo $bulk_image_description?>" rows="5" cols="15" class="textarea2"></textarea>
								</td>
							</tr>
							
<?php
			
			}
			
				$java_script .= "}</script>";
				
				
		if($total_image_count == 0)
		{		
				
?>					
		
		<tr><td colspan="3" align="center" class="text_information">Please contact the Administrator to set the 'Product Bulk Images Upload' count !!</td></tr>
		<tr>
			<td colspan="3" align="center">
				<input type="button" name="close" value="Back" class="button" onClick="Javascript:history.go(-1);">
			</td>
		</tr>
		
<?php

		}
		else
		{

?>		
		
							<tr><td colspan="3">&nbsp;</td></tr>
							<tr>
								<td colspan="3" align="center">
									<input type="submit" value="Add" name="add" class="button">
									<input type="button" name="close" value="Back" class="button" onClick="window.location='list_productview_images_artist.php'">
								</td>
							</tr>
<?php

		}

?>							
							
							<tr><td colspan="3">&nbsp;</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
	echo $java_script;

	include "../includes/footer.php";
	
	ob_end_flush();
?>