<?php
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  $format="";
 
if(is_array($HTTP_POST_VARS["importformat"]))
{
	$importformat=$HTTP_POST_VARS["importformat"];
	//$product_match=$HTTP_POST_VARS["product_match"];
	$format=$importformat[0];
	switch($importformat[0])
	{
		case "F":    //freeway format
		case "O":	//oscommrece
		case "Z":	//zencart
			$import = new upload('txt_import');
			$import->set_destination(DIR_FS_CATALOG_IMAGES);
			if ($import->parse() && $import->save())
			{
				$import_filename=$import->filename;
				//$import_filename="oscommrece.xls";
				$filename=DIR_FS_CATALOG_IMAGES .  $import_filename;
				$handle = fopen($filename, "r");
				$contents = fread($handle, filesize($filename));
				fclose($handle);
				$category_parent_array=array();
				$product_array=array();
				$overwrite=$HTTP_POST_VARS['overwrite'];
				import_product_details($contents,$importformat[0]);
				unlink($filename);
				tep_redirect(tep_href_link(FILENAME_PRODUCTS_IMPORT,"success=1"));
			}
			else
				$db_error="Upload file to import";
			break;
		case "E":	//easy populate
			global $separator;
			$separator = "\t"; // tab is default
	}
}
 tep_get_last_access_file();
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script>
 function checkformatSell(item)
 {
		var importformat=document.products_import.elements["importformat[]"];
		for (icnt=0;icnt<importformat.length;icnt++){	
			if (importformat[icnt].checked && importformat[icnt].value!=item.value) {
				importformat[icnt].checked=false;
				break;
			}
		}
 }
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr> 
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
		  <?php if(isset($HTTP_GET_VARS["success"])) {?>
		  	<tr class="dataTableRowOdd"><td class="dataTableContent" colspan="2" height="15"><font color="#006633"><?php echo TEXT_SUCCESS;?></font></td></tr>
		  <?php }?>
		  <tr>
		  	<td colspan="2">
			<?php echo tep_draw_form('products_import',FILENAME_PRODUCTS_IMPORT,'','post','enctype="multipart/form-data"');?>
			<table cellpadding="2" cellspacing="0" border="0" width="100%">
		  <tr> 
		  	<td>
				<Table width="60%" cellpadding="5" border="0">
					<tr><Td class="dataTableContent" colspan=4><b><?php echo TEXT_PRODUCT_FORMAT;?></b>&nbsp;<font color="#CC3300"><?php echo $db_error;?></font></Td></tr>
					<tr>
						<Td class="dataTableContent" width="5%"><?php echo tep_draw_checkbox_field("importformat[]","F",($format=="F" || $format=='')?true:false,'','onclick="javascript:checkformatSell(this)"');?>&nbsp;<?php echo TEXT_FREEWAY;?></Td>
						<Td class="dataTableContent" width="5%"><?php echo tep_draw_checkbox_field("importformat[]","O",($format=="O")?true:false,'','onclick="javascript:checkformatSell(this)"');?>&nbsp;<?php echo TEXT_OSCOMMRECE;?></Td>
						<Td class="dataTableContent" width="5%"><?php echo tep_draw_checkbox_field("importformat[]","Z",($format=="Z")?true:false,'','onclick="javascript:checkformatSell(this)"');?>&nbsp;<?php echo TEXT_ZENCART;?></Td>
						<Td class="dataTableContent" width="5%"><?php //echo tep_draw_checkbox_field("overwrite",'Y');?>&nbsp;<?php //echo TEXT_OVERWRITE;?></Td>
				    </tr>
						<td colspan="4" class="dataTableContent"><?php echo tep_draw_checkbox_field("overwrite",'Y').'&nbsp;'.TEXT_OVERWRITE;?></td>
					<tr>
					</tr>
					<tr>
						<td colspan="4" class="dataTableContent"><?php echo TEXT_IMPORT;?></td>
					</tr>
					<tr><td colspan=4><?php echo tep_draw_file_field("txt_import");?></td></tr>
				</Table>
				 </td> 
				  </tr>
				<tr height="20">
					<td colspan="2" class="cell_bg_report_header"></td>
				</tr>
				<tr><td colspan="2" align="right"><?php echo tep_image_button('button_reset.gif', IMAGE_RESET,' style="cursor:pointer" onClick="javascript:location.href=\'products_export.php\';"');  ?>&nbsp;&nbsp;
				<?php echo tep_image_submit('button_import.gif','Import'); ?></td></tr>
		 </table></form>
		</td>
	</tr>	  
    </table></td>
     </tr> 
</table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php
	function import_product_details($contents,$import_type)
	{
	  global $category_parent_array,$product_match,$db_error,$count,$attrib_sort,$languages_id,$options_array,$options_values_array;
	 	 $count=0;$attrib_sort=0;
		if($import_type=='O'){
			$contents = split(chr(14),$contents);
			$content_arr=split(chr(13),$contents[0]);
		}else{
			$content_arr=split(chr(13),$contents);
		}
		$get_options_name_query = tep_db_query("select products_options_name from products_options where language_id =". $languages_id);
		if(tep_db_num_rows($get_options_name_query)){
			while($name_result= tep_db_fetch_array($get_options_name_query)) $options_array[]=$name_result['products_options_name'];
		}
		$get_options_values_query = tep_db_query("select products_options_values_name from products_options_values where language_id =". $languages_id);
		if(tep_db_num_rows($get_options_values_query)){
			while($name_result=tep_db_fetch_array($get_options_values_query)) $options_values_array[]=$name_result['products_options_values_name'];
		}
		
		$products_name_query = tep_db_query("select ps.products_name from ".TABLE_PRODUCTS." p, ".TABLE_PRODUCTS_DESCRIPTION." ps where p.products_id = ps.products_id");
		while($products_name_result = tep_db_fetch_array($products_name_query)){
			$product_names[] = $products_name_result['products_name'];
		}

		for($icnt=0;$icnt<sizeof($content_arr);$icnt++)
		{ 
			$row=$content_arr[$icnt];
			if($db_error!="") break;
			if(trim($row)!="" )
			{
				$col_arr=split(",",$row);
				if(sizeof($col_arr)==0)	continue;
				$col_type=trim($col_arr[0]);
				switch($col_type)
				{
					case "category":
						import_category($col_arr,$import_type);
						break;
					case "product":
						import_product($col_arr,$import_type,$product_names);
						break;
					case "attribute":
						import_attribute($col_arr,$import_type);
						break;
					case "price_break":
						import_price_break($col_arr,$import_type);
						break;
				}
			}
		}
		if($import_type=='O'){
			import_options($contents[1]);
		}
	}
function import_price_break($col_arr,$import_type){
	// print_r($col_arr);
	 global $languages_id,$overwrite;
	 $source=$col_arr[1];
	 $mode='insert';
	 $price_break = explode('@',$source);
	 // price_break[1] have the products_name
	//check whether the products name already exists
	if($price_break[1]!=''){
		$check_products_break_query=tep_db_query("select ppb.products_id from products_price_break ppb,products_description pd where ppb.products_id=pd.products_id and pd.products_name='". $price_break[1] ."' and language_id='". $languages_id ."'");
		if(tep_db_num_rows($check_products_break_query)>0){
			 $check_products_break_result=mysql_num_rows($check_products_break_query);
			$products_id=$check_products_break_result[0];
			}else{
				$get_products_id_query = tep_db_query("select products_id from products_description where products_name='". $price_break[1] ."' and language_id='". $languages_id ."'");
				if(tep_db_num_rows($get_products_id_query)>0){
		  			$products_name_id = mysql_fetch_row($get_products_id_query);
		    		$products_id = $products_name_id[0];
				}
			}
	}
	if($overwrite=='Y'){
		tep_db_query("delete from products_price_break where products_id='$products_id'");
	}
	 $one_row=explode('#',$price_break[0]);
	 $total_rows=count($one_row);
	 for($icnt=0;$icnt<$total_rows;$icnt++){
	 	$result=explode('^',$one_row[$icnt]);
		//echo "Insert into products_price_break values(". $products_id ." , ". $result[0] ." , ". $result[1] .")";
		if($products_id>0 && $mode=='insert') 	tep_db_query("Insert into products_price_break values(". $products_id ." , ". $result[0] ." , ". $result[1] .")");
	 }
} 	
	
	function import_options($opt_contents){
		global $languages_id;
			$options_array=explode("^",$opt_contents);
			$length=count($options_array);
			for($icnt=0;$icnt<$length;$icnt++){
				$result_array=explode("#",$options_array[$icnt]);
					$options_id=get_options_id($result_array[0]);
					$options_values =substr($result_array[1],0,-1);
					if(is_numeric(strpos($options_values,","))){
						$options_values_array = explode(",",$options_values);
						$length1=count($options_values_array);
						for($jcnt=0;$jcnt<$length1;$jcnt++){
							$options_values_id=get_options_values_id($options_values_array[$jcnt]);
							//echo 'Options_id:'.$options_id.'values:'.$options_values_id.'<br>';	
							if($options_id>0 && $options_values_id>0)
							tep_db_query("insert into products_options_values_to_products_options values(NULL,$options_id,$options_values_id)");
						}
					}	
			}
	}
	function get_options_values_id($val_name){
		global $languages_id;
		$sql="select products_options_values_id from products_options_values where products_options_values_name= '" . addslashes($val_name) . "' and language_id = '" . $languages_id . "' ";
		$get_products_values_id_query=tep_db_query($sql);
		$options_values_id=mysql_fetch_row($get_products_values_id_query);
		return (int)$options_values_id[0];
	}
	function get_options_id($options_name){
		global $languages_id;
		$sql="select products_options_id from products_options where products_options_name='" . addslashes($options_name) . "' and language_id = '" . $languages_id . "'";
		$get_options_id_query=tep_db_query($sql);
		$options_id=mysql_fetch_row($get_options_id_query);
		return (int)$options_id[0];
	}
	
	function import_category($col_arr,$import_type)
	{
	 global $category_parent_array,$languages_id,$overwrite;
	 	switch($import_type)
		{
			case "F":
			case "Z":
			 	$category_format="categories_name|parent_id|categories_description|categories_image";
				break;
			case "O":
	 			$category_format="categories_name|parent_id|categories_image";
				$category_details["categories_description"]="";
				break;
		}
		$temp_arr=split("\|",$category_format);
		for($i=1;$i<=sizeof($temp_arr);$i++)
		{
			if(isset($col_arr[$i]))
				$category_details[$temp_arr[$i-1]]=trim($col_arr[$i]);
		}
		if($category_details["parent_id"]=="")
		  $category_parent=0;
		else
		  $category_parent=array_search($category_details["parent_id"], $category_parent_array); 
		$category_details["parent_id"]=$category_parent;
		// v check current category, if exists already
		$check_category_query=tep_db_query("select c.categories_id,c.parent_id from categories c, categories_description cd  where cd.categories_id=c.categories_id and cd.categories_name='". tep_db_input($category_details['categories_name']) ."' and language_id= '". $languages_id ."'");
		if(tep_db_num_rows($check_category_query)>0){
			if($overwrite=='Y'){
				$mode='update';
				$update_category=mysql_fetch_row($check_category_query);
				$update_category_id=$update_category[0];
			}else{
				return ;
			}
		}else
		$mode='insert';
		
		$cat_img=$category_details["categories_image"];
		$pos=strrpos($cat_img,"/");
		if($pos!==false)
		{
			$img_name=substr($cat_img,$pos+1);
			$cat_path=substr($cat_img,0,strlen($cat_img)-strlen($img_name));
		}
		//------insert category-----------------
		$sql_Array=array("categories_image"=>$img_name,
					  "parent_id"=> $category_details["parent_id"],
					  "date_added"=>date('Y-m-d'),
					  "last_modified"=>date('Y-m-d'));
		
		if($mode=='update'){
			$sql_Array['parent_id']=$update_category[1];
		  tep_db_perform("categories",$sql_Array,"update","categories_id='". $update_category_id ."'");
			$category_id=$update_category_id;
		 }else{
			tep_db_perform("categories",$sql_Array);
			$category_id=tep_db_insert_id();
			$category_parent_array[$category_id]=$category_details["categories_name"];
		 }
		
		//----insert category desc------------------------
		$sql_Array1=array("categories_id"=>$category_id,
					  "language_id"=>1,
					  "categories_name"=>$category_details["categories_name"],
					  "categories_description"=>$category_details["categories_description"]);
					  
		if($mode=='update') tep_db_perform("categories_description",$sql_Array1,"update","categories_id='". $category_id ."'");
		else tep_db_perform("categories_description",$sql_Array1);
		//------------------------------------------
		
		if($img_name!='')  //upload the image
		{  
			if(file_exists($cat_path . $img_name)){
				$img_content=file_get_contents($cat_path . $img_name);
				if($img_content)
					file_put_contents(DIR_FS_CATALOG_IMAGES . $img_name,$img_content);
			}	
		}
  }
  function import_product($col_arr,$import_type,$products_names=array())
  { 
    global $category_parent_array,$product_array,$product_match,$db_error,$count,$languages_id,$overwrite;
	$product_details["products_id"]="";
	switch($import_type)
	{
		case "F":
			$product_format="categories_id|products_name|products_description|products_model|products_image_1|products_image_2|products_image_3|products_image_4|products_image_5|products_title_1|products_title_2|products_title_3|products_title_4|products_title_5|products_price|products_date_available|products_weight|products_status|products_tax_class_id|manufacturers_id|products_sort_order|products_quantity|is_attributes|products_sku|author_name|is_subscription|products_price_break";
			break;
		case "Z":
			$product_format="categories_id|products_name|products_description|products_model|products_image_1|products_price|products_date_available|products_weight|products_status|products_tax_class_id|manufacturers_id|products_sort_order|products_quantity";
			break;
		case "O":
			$product_format="categories_id|products_name|products_description|products_model|products_image_1|products_price|products_date_available|products_weight|products_status|products_tax_class_id|manufacturers_id|products_quantity|products_sort_order";
			break;
	}
	if(is_numeric($col_arr[1]))  //product_id is set
		$product_format="products_id|" . $product_format;
	$temp_arr=split("\|",$product_format);
	$mode="insert";
	for($i=1;$i<=sizeof($temp_arr);$i++)
	{
		if(isset($col_arr[$i]))
			$product_details[$temp_arr[$i-1]]=trim($col_arr[$i]);
			if($product_details['products_sort_order']==''){ $count++; $product_details['products_sort_order']=$count;}
	}//echo '<br>PRODUCT:';print_r($product_details);
		if(in_array($product_details['products_name'],$products_names)){
			return;
		}
	$products_check_query=tep_db_query("select products_id from products_description where products_name ='" . tep_db_input($product_details['products_name']) . "' and language_id='$languages_id'");
	if(tep_db_num_rows($products_check_query)>0){
		if($overwrite=='Y'){
			$mode='update';
		}
	}
	if(trim($product_details["products_id"])!="")
	{
		$query=tep_db_query("select products_model from products where products_id='" .trim($product_details["products_id"]) ."'");
		if(tep_db_num_rows($query)>0)  //product match
		{
			if($product_match=="Y")
				$mode="update";
			else
			{
				$db_error="Products (" . $product_details["products_name"] . ") with the same ID already exist. Export your current products and compare before importing again.";
				return;
			}
		}
	}
	for($in=1;$in<=5;$in++)
	{
		$prd_img=$product_details["products_image_" . $in];
		$img_name[$in]="";
		$prd_path[$in]="";
		$pos=strrpos($prd_img,"/");
		if($pos!==false)
		{
			$img_name[$in]=substr($prd_img,$pos+1);
			$prd_path[$in]=substr($prd_img,0,strlen($prd_img)-strlen($img_name[$in]));
		}
	}

	//------insert product-----------------
	$sql_Array=array("products_model"=>$product_details["products_model"],
					"products_image_1"=>$img_name[1],
					"products_image_2"=>$img_name[2],
					"products_image_3"=>$img_name[3],
					"products_image_4"=>$img_name[4],
					"products_image_5"=>$img_name[5],
					"products_title_1"=>$product_details["products_title_1"],
					"products_title_2"=>$product_details["products_title_2"],
					"products_title_3"=>$product_details["products_title_3"],
					"products_title_4"=>$product_details["products_title_4"],
					"products_title_5"=>$product_details["products_title_5"],
					"products_price"=>$product_details["products_price"],
					"products_date_available"=>$product_details["products_date_available"],
					"products_weight"=>$product_details["products_weight"],
					"products_status"=>$product_details["products_status"],
					"products_tax_class_id"=>$product_details["products_tax_class_id"],
					"manufacturers_id"=>$product_details["manufacturers_id"],
					"products_sort_order"=>$product_details["products_sort_order"],
					"products_quantity"=>$product_details["products_quantity"],
					"is_attributes"=>$product_details["is_attributes"],
					"products_sku"=>$product_details["products_sku"],
					"author_name"=>$product_details["author_name"],
					"is_subscription"=>$product_details["is_subscription"],
					"products_price_break"=>$product_details["products_price_break"],
					"products_date_added"=>date('Y-m-d'),
					"products_last_modified"=>date('Y-m-d'));
	//----insert product desc------------------------
	//print_r($sql_Array);
//	if($mode=='insert') $product_id = tep_db_insert_id();
	$product_details["products_description"] = str_replace('<br/>',chr(10),$product_details["products_description"]);
	$product_details["products_description"] = str_replace('~',',',$product_details["products_description"]);
	$sql_Array1=array("products_id"=>$product_id,
				  "products_description"=>$product_details["products_description"],
				  "language_id"=>1,
				  "products_name"=>$product_details["products_name"]);
	//------------------------------------------
	if($mode=="insert")
	{
		tep_db_perform("products",$sql_Array);
		$product_id=tep_db_insert_id();
		$sql_Array1["products_id"]=$product_id;
		tep_db_perform("products_description",$sql_Array1);
	}
	else
	{
		$product_result=mysql_num_rows($products_check_query);
		$product_id=$product_result[0];
		tep_db_perform("products",$sql_Array,"update","products_id='" . $product_id . "'");
		//$product_id=$product_details["products_id"];
		tep_db_perform("products_description",$sql_Array1,"update","language_id='". $languages_id ."' and products_id='" . $product_id . "'");
	}
	//------upload the image------------------------------------

	for($k=1;$k<=5;$k++)
	{
		if($img_name[$k]!='')
		{   
			if(file_exists($prd_path[$k] . $img_name[$k])){
			$img_content=file_get_contents($prd_path[$k] . $img_name[$k]);
			if($img_content){
				$split_image_name = explode('.',$img_name[$k]);
				$image_type = '.'.$split_image_name[1];
				$s_width = SMALL_IMAGE_WIDTH;
				tep_resize_image($prd_path[$k] . $img_name[$k],DIR_FS_CATALOG_IMAGES . "small/" . $img_name[$k],$image_type,$s_width);
		//		echo 'Path:'.DIR_FS_CATALOG_IMAGES .'&nbsp;&nbsp;IMG:'. $img_name[$k].'<br>';
				file_put_contents(DIR_FS_CATALOG_IMAGES . $img_name[$k],$img_content);
			  }	
			}
		}
	}
		//------------------------
	$product_array[$product_id]=$product_details["products_name"];
	if($product_details["categories_id"]!="" && $mode=='insert')
	{  
		$cat_arr=split("\^",$product_details["categories_id"]);
		tep_db_query("delete from products_to_categories where products_id='" . $product_id ."'");
		for($k=0;$k<sizeof($cat_arr);$k++)
		{
		   $category_id=0;	
		   $category_id=array_search($cat_arr[$k], $category_parent_array); 
		   if($category_id!=0)
		   {
				 $sql_Array1=array("products_id"=>$product_id,
				 "categories_id"=>$category_id);
			 tep_db_perform("products_to_categories",$sql_Array1);
		   }	  
		}
	}
  }
  function import_attribute($col_arr,$import_type)
  {
 	global $category_parent_array,$product_array,$attrib_sort,$languages_id,$options_array,$options_values_array,$incre_option,$incre_optionvalue,$overwrite;
	$attribute_details["products_attributes_id"]="";
	switch($import_type)
	{
	case "F":
    	// $attribute_format="products_id|options_id|options_values_id|options_values_price|price_prefix|products_options_sort_order|product_attributes_one_time|products_attributes_weight|products_attributes_weight_prefix|products_attributes_units|products_attributes_units_price|products_options_name|products_options_values_name";
  		 $attribute_format="products_id|options_id|options_values_id|options_values_price|price_prefix|products_options_sort_order|product_attributes_one_time|products_attributes_weight|products_attributes_weight_prefix|products_attributes_units|products_attributes_units_price|products_options_name|products_options_values_name";
		 break;
	case "Z":
  		 $attribute_format="products_id|options_id|options_values_id|options_values_price|price_prefix|products_options_sort_order|product_attributes_one_time|products_attributes_weight|products_attributes_weight_prefix";
		 break;
	case "O":
  		 $attribute_format="products_id|options_id|options_values_id|options_values_price|price_prefix|products_options_name|products_options_values_name|products_options_sort_order";
		 break;
	}
	if(is_numeric($col_arr[1]))  //product_id is set
		$attribute_format="products_attributes_id|" . $attribute_format;
	 $temp_arr=split("\|",$attribute_format);
	 for($i=1;$i<=sizeof($temp_arr);$i++)
	 {
		if(isset($col_arr[$i]))
			$attribute_details[$temp_arr[$i-1]]=trim($col_arr[$i]);
			if(empty($attribute_details['products_options_sort_order'])){ 
				$attrib_sort++; 
				$attribute_details['products_options_sort_order']=$attrib_sort;
			}
			if(empty($attribute_details['products_attributes_weight'])) $attribute_details['products_attributes_weight']='0';
			if(empty($attribute_details['products_attributes_units'])) $attribute_details['products_attributes_units']='0';
			if(empty($attribute_details['products_attributes_units_price'])) $attribute_details['products_attributes_units_price']='0';
	 }
	//------insert attribute-----------------
	$product_id=0;
	$product_id=array_search($attribute_details["products_id"], $product_array); 
	if( $import_type=='O' || $import_type=='F'){ 
			if(count($options_array)<1){
				$products_options_check_query = tep_db_query("select products_options_id from products_options where products_options_name = '" . $attribute_details['products_options_name'] . "'");
				if(tep_db_num_rows($products_options_check_query)<1){
					$incre_option = $incre_option+1;	
					$products_options_id=$incre_option;
					tep_db_query("Insert into products_options (products_options_id,language_id,products_options_name) values($products_options_id,'" . $languages_id . "','" . $attribute_details['products_options_name']  . "')");
					//$options_array[]=$attribute_details["products_options_name"];
				}else{
					$products_options_check_result=mysql_fetch_row($products_options_check_query);
					$products_options_id=$products_options_check_result[0];
				}
	//		}else if(!array_search($attribute_details["products_options_name"],$options_array)){ //echo '<br>';print_r($options_array);echo 'NAme:'.$attribute_details["products_options_name"].'<br>';
			}else if(!in_array($attribute_details["products_options_name"],$options_array) && count($options_array)>1){
				$get_products_options_id_query = tep_db_query("select max(products_options_id) as products_options_id from products_options ");
				$options_id_array = mysql_fetch_row($get_products_options_id_query);
				$products_options_id=$options_id_array[0];
				$products_options_id=$products_options_id +1;
				tep_db_query("Insert into products_options (products_options_id,language_id,products_options_name) values($products_options_id,'" . $languages_id . "','" . $attribute_details['products_options_name']  . "')");
				$options_array[]=$attribute_details["products_options_name"];
			}else{
				$products_options_id = get_options_id($attribute_details["products_options_name"]);
			}
	}	
	if( $import_type=='O' || $import_type=='F'){
			if(count($options_values_array)<1){
				$products_options_values_check_query=tep_db_query("select products_options_values_id from products_options_values where products_options_values_name ='" . $attribute_details['products_options_values_name'] . "'");
				if(tep_db_num_rows($products_options_values_check_query)<1){
					$incre_optionvalue = $incre_optionvalue+1;
					$products_options_values_id =$incre_optionvalue;
					tep_db_query("Insert into products_options_values values ($products_options_values_id, '". $languages_id ."', '". $attribute_details['products_options_values_name'] ."' ) ");
					//$options_values_array[]=$attribute_details["products_options_values_name"];
				}else{
					$products_options_values_check_result=mysql_fetch_row($products_options_values_check_query);
					$products_options_values_id=$products_options_values_check_result[0];
				}
			//}else if(!array_search($attribute_details['products_options_values_name'],$options_values_array) && count($options_values_array)>1){
			}else if(!in_array($attribute_details['products_options_values_name'],$options_values_array) && count($options_values_array)>1){
				$get_products_options_values_id_query=tep_db_query("select max(products_options_values_id) as products_options_values_id from products_options_values");
				$options_values_id = mysql_fetch_row($get_products_options_values_id_query);
				$products_options_values_id = $options_values_id[0];
				$products_options_values_id =$products_options_values_id +1;
				tep_db_query("Insert into products_options_values values ($products_options_values_id, '". $languages_id ."', '". $attribute_details['products_options_values_name'] ."' ) ");
				$options_values_array[]=$attribute_details["products_options_values_name"];
				//if($products_options_id>0 && $products_options_values_id>0) tep_db_query("Insert into products_options_values_to_products_options values (NULL,'". $products_options_id ."' , '". $products_options_values_id  ."')");
			}else{
				$products_options_values_id = get_options_values_id($attribute_details["products_options_values_name"]);
			}
	 }		
	if($import_type=='Z')	{ 
		$products_options_id=$attribute_details["options_id"];
		$products_options_values_id = $attribute_details["options_values_id"];
	}
	if($product_id>0)
	{
		$sql_Array=array("products_id"=>$product_id,
						"options_id"=>$products_options_id,
						"options_values_id"=>$products_options_values_id,
						"options_values_price"=>$attribute_details["options_values_price"],
						"price_prefix"=>$attribute_details["price_prefix"],
						"products_options_sort_order"=>$attribute_details["products_options_sort_order"],
				//		"product_attributes_one_time"=>$attribute_details["product_attributes_one_time"],
						"products_attributes_weight"=>$attribute_details["products_attributes_weight"],
						"products_attributes_weight_prefix"=>$attribute_details["products_attributes_weight_prefix"],
						"products_attributes_units"=>$attribute_details["products_attributes_units"],
						"products_attributes_units_price"=>$attribute_details["products_attributes_units_price"]);
		//print_r($sql_Array);
		if($attribute_details["products_attributes_id"]!="")
			tep_db_perform("products_attributes",$sql_Array,"update","products_attributes_id='" . $attribute_details["products_attributes_id"] . "'");
		else 
			tep_db_perform("products_attributes",$sql_Array);
			
	}
	//Add data to products_options_values_to_products_options
	if($import_type=='F' || $import_type=='O'){
		$options_check_query=tep_db_query("select * from products_options_values_to_products_options where products_options_values_id='$products_options_values_id'");
		if(tep_db_num_rows($options_check_query)<1){
			if($products_options_id>0 && $products_options_values_id>0) tep_db_query("Insert into products_options_values_to_products_options values (NULL,'". $products_options_id ."' , '". $products_options_values_id  ."')");
		}
	}
  }
  
  
?>