Location: PHPKode > projects > HikaShop > back/helpers/import.php
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.4.3
 * @author		hikashop.com
 * @copyright	(C) 2010 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class hikashopImportHelper{
	var $template = null;
	var $totalInserted = 0;
	var $totalTry = 0;
	var $totalValid = 0;
	var $listSeparators = array("\t",';',',','|');
	var $perBatch = 50;
	var $codes = array();
	var $characteristics = array();
	var $characteristicsConversionTable = array();
	var $characteristicColumns = array();
	var $countVariant = true;
	var $overwrite = false;
	var $products_already_in_db = array();
	var $new_variants_in_db = array();
	var $columnNamesConversionTable = array();
	var $createCategories = false;
	var $header_errors = true;
	var $force_published = true;
	var $tax_category=0;
	function HikashopImportHelper(){
		$this->fields = array('product_weight','product_description','product_meta_description','product_tax_id','product_vendor_id','product_manufacturer_id','product_url','product_keywords','product_weight_unit','product_dimension_unit','product_width','product_length','product_height','product_max_per_order');
		$fieldClass = hikashop::get('class.field');
		$userFields = $fieldClass->getData('','product');
		if(!empty($userFields)){
			$this->fields = array_merge($this->fields,array_keys($userFields));
		}
		$this->all_fields = array_merge($this->fields,array('product_name','product_published','product_code','product_created','product_modified','product_sale_start','product_sale_end','product_type','product_quantity'));
		$this->db =& JFactory::getDBO();
		$columnsProductTable = $this->db->getTableFields(hikashop::table('product'));
		$this->columnsProductTable = array_keys($columnsProductTable[hikashop::table('product')]);
		$query = 'SELECT * FROM '.hikashop::table('characteristic');
		$this->db->setQuery($query);
		$this->characteristics = $this->db->loadObjectList();
		if(!empty($this->characteristics)){
			foreach($this->characteristics as $characteristic){
				$this->characteristicsConversionTable[strtolower(trim($characteristic->characteristic_value,'" '))]=$characteristic->characteristic_id;
			}
		}
		$this->volumeHelper =& hikashop::get('helper.volume');
		$this->weightHelper =& hikashop::get('helper.weight');
		$class = hikashop::get('class.category');
		$this->mainProductCategory = 'product';
		$class->getMainElement($this->mainProductCategory);
		$config =& hikashop::config();
		$uploadFolder = ltrim(JPath::clean(html_entity_decode($config->get('uploadfolder'))),DS);
		$uploadFolder = rtrim($uploadFolder,DS).DS;
		$this->uploadFolder = JPATH_ROOT.DS.$uploadFolder;
		$this->uploadFolder_url = str_replace(DS,'/',$uploadFolder);
		$this->uploadFolder_url = HIKASHOP_LIVE.$this->uploadFolder_url;
		jimport('joomla.filesystem.file');
	}
	function addTemplate($template_product_id){
		if(hikashop::level(2)){
			if($template_product_id){
				$productClass = hikashop::get('class.product');
				if($productClass->getProducts($template_product_id) && !empty($productClass->products)){
					$key = key($productClass->products);
					$this->template =& $productClass->products[$key];
				}
			}
		}
	}
	function importFromFolder($type,$delete,$uploadFolder){
		$config =& hikashop::config();
		$allowed = explode(',',strtolower($config->get('allowed'.$type)));
		$folder = 'image';
		if($type=='files'){
			$folder = 'file';
		}
		$uploadFolder = rtrim(JPath::clean(html_entity_decode($uploadFolder)),DS.' ').DS;
		if(!preg_match('#^([A-Z]:)?/.*#',$uploadFolder)){
			if(!$uploadFolder[0]=='/' || !is_dir($uploadFolder)){
				$uploadFolder = JPath::clean(HIKASHOP_ROOT.DS.trim($uploadFolder,DS.' ').DS);
			}
		}
		$fileClass = hikashop::get('class.file');
		if(!$fileClass->checkFolder($uploadFolder)){
			return false;
		}
		jimport('joomla.filesystem.folder');
		jimport('joomla.filesystem.file');
		$app =& JFactory::getApplication();
		$files = JFolder::files($uploadFolder);
		if(!empty($files)){
			$imageHelper = hikashop::get('helper.image');
			$uploadPath = $fileClass->getPath($folder);
			if(!empty($this->template->variants)){
				$this->countVariant = false;
			}
			foreach($files as $file){
				if(in_array($file,array('index.html','.htaccess'))) continue;
				$extension = strtolower(substr($file,strrpos($file,'.')+1));
				if(!in_array($extension,$allowed)){
					$app->enqueueMessage(JText::sprintf('FILE_SKIPPED',$file));
					continue;
				}
				$newProduct = null;
				$newProduct->$type = $file;
				$this->_checkData($newProduct);
				$this->totalTry++;
				if(!empty($newProduct->product_code)){
					$this->totalValid++;
					$products = array($newProduct);
					if(!empty($this->template->variants)){
						foreach($this->template->variants as $variant){
							$copy = (PHP_VERSION < 5) ? $variant : clone($variant);
							$copy->product_parent_id = $newProduct->product_code;
							$copy->product_code = $newProduct->product_code.'_'.$copy->product_code;
							$products[]=$copy;
						}
					}
					$this->_insertProducts($products);
					if($delete){
						JFile::move($uploadFolder.$file,$uploadPath.$file);
					}else{
						JFile::copy($uploadFolder.$file,$uploadPath.$file);
					}
					if($type!='files'){
						$imageHelper->resizeImage($uploadPath.$file);
					}
				}
			}
			$this->_deleteUnecessaryVariants();
		}
		$app->enqueueMessage(JText::sprintf('IMPORT_REPORT',$this->totalTry,$this->totalInserted,$this->totalTry - $this->totalValid,$this->totalValid - $this->totalInserted));
	}
	function copyProduct($product_id){
		if(!hikashop::level(2)){
			return false;
		}
		$this->addTemplate($product_id);
		$newProduct = null;
		$newProduct->product_code = $this->template->product_code.'_copy'.rand();
		$this->_checkData($newProduct);
		if(!empty($newProduct->product_code)){
			$products = array($newProduct);
			if(!empty($this->template->variants)){
				foreach($this->template->variants as $variant){
					$copy = (PHP_VERSION < 5) ? $variant : clone($variant);
					$copy->product_parent_id = $newProduct->product_code;
					$copy->product_code = $copy->product_code.'_copy'.rand();
					$products[]=$copy;
				}
			}
			$this->_insertProducts($products);
		}
		return true;
	}
	function importFromFile(&$importFile){
		$app =& JFactory::getApplication();
		if(empty($importFile['name'])){
			$app->enqueueMessage(JText::_('BROWSE_FILE'),'notice');
			return false;
		}
		$this->charsetConvert = JRequest::getString('charsetconvert','');
		jimport('joomla.filesystem.file');
		$config =& hikashop::config();
		$allowedFiles = explode(',',strtolower($config->get('allowedfiles')));
		$uploadFolder = JPath::clean(html_entity_decode($config->get('uploadfolder')));
		$uploadFolder = trim($uploadFolder,DS.' ').DS;
		$uploadPath = JPath::clean(HIKASHOP_ROOT.$uploadFolder);
		if(!is_dir($uploadPath)){
			jimport('joomla.filesystem.folder');
			JFolder::create($uploadPath);
			JFile::write($uploadPath.'index.html','<html><body bgcolor="#FFFFFF"></body></html>');
		}
		if(!is_writable($uploadPath)){
			@chmod($uploadPath,'0755');
			if(!is_writable($uploadPath)){
				$app->enqueueMessage(JText::sprintf( 'WRITABLE_FOLDER',$uploadPath), 'notice');
			}
		}
		$attachment = null;
		$attachment->filename = strtolower(JFile::makeSafe($importFile['name']));
		$attachment->size = $importFile['size'];
		$attachment->extension = strtolower(substr($attachment->filename,strrpos($attachment->filename,'.')+1));
		if(!in_array($attachment->extension,$allowedFiles)){
			$app->enqueueMessage(JText::sprintf( 'ACCEPTED_TYPE',$attachment->extension,$config->get('allowedfiles')), 'notice');
			return false;
		}
		if ( !move_uploaded_file($importFile['tmp_name'], $uploadPath . $attachment->filename)) {
			if(!JFile::upload($importFile['tmp_name'], $uploadPath . $attachment->filename)){
				$app->enqueueMessage(JText::sprintf( 'FAIL_UPLOAD',$importFile['tmp_name'],$uploadPath . $attachment->filename), 'error');
			}
		}
		hikashop::increasePerf();
		$contentFile = file_get_contents($uploadPath . $attachment->filename);
		if(!$contentFile){
			$app->enqueueMessage(JText::sprintf( 'FAIL_OPEN',$uploadPath . $attachment->filename), 'error');
			return false;
		};
		unlink($uploadPath . $attachment->filename);
		return $this->handleContent($contentFile);
	}
	function handleContent(&$contentFile){
		$app =& JFactory::getApplication();
		$contentFile = str_replace(array("\r\n","\r"),"\n",$contentFile);
		$this->importLines = explode("\n", $contentFile);
		$this->i = 0;
		while(empty($this->header)){
			$this->header = trim($this->importLines[$this->i]);
			$this->i++;
		}
		if(!$this->_autoDetectHeader()){
			return false;
		}
		$this->numberColumns = count($this->columns);
		$importProducts = array();
		$encodingHelper = hikashop::get('helper.encoding');
		while ($data = $this->_getProduct()) {
			$this->totalTry++;
			$newProduct = null;
			foreach($data as $num => $value){
				$field = $this->columns[$num];
				$newProduct->$field = trim($value,'\'" ');
				if(!empty($this->charsetConvert)){
					$newProduct->$field = $encodingHelper->change($newProduct->$field,$this->charsetConvert,'UTF-8');
				}
			}
			$this->_checkData($newProduct);
			if(!empty($newProduct->product_code)){
				$importProducts[] = $newProduct;
				$this->totalValid++;
			}
			if( $this->totalValid%$this->perBatch == 0){
				$this->_insertProducts($importProducts);
				$importProducts = array();
			}
		}
		if(!empty($importProducts)){
			$this->_insertProducts($importProducts);
		}
		$this->_deleteUnecessaryVariants();
		$app->enqueueMessage(JText::sprintf('IMPORT_REPORT',$this->totalTry,$this->totalInserted,$this->totalTry - $this->totalValid,$this->totalValid - $this->totalInserted));
		return true;
	}
	function _deleteUnecessaryVariants(){
		if(!empty($this->products_already_in_db)){
			$this->db->setQuery('SELECT product_id FROM '.hikashop::table('product').' WHERE product_parent_id IN ('.implode(',',$this->products_already_in_db).') AND product_id NOT IN ('.implode(',',$this->new_variants_in_db).')');
			$variants_to_be_deleted = $this->db->loadResultArray();
			if(!empty($variants_to_be_deleted)){
				$productClass = hikashop::get('class.product');
				$productClass->delete($variants_to_be_deleted);
			}
		}
	}
	function &_getProduct(){
		$false = false;
		if(!isset($this->importLines[$this->i])){
			return $false;
		}
		if(empty($this->importLines[$this->i])){
			$this->i++;
			return $this->_getProduct();
		}
		$quoted = false;
		$dataPointer=0;
		$data = array('');		
		while($data!==false && isset($this->importLines[$this->i]) && (count($data) < $this->numberColumns||$quoted)){
			$k = 0;
			$total = strlen($this->importLines[$this->i]);
			while($k < $total){
				switch($this->importLines[$this->i][$k]){
					case '"':
						if($k && isset($this->importLines[$this->i][$k+1]) && $this->importLines[$this->i][$k+1]=='"'){
							$data[$dataPointer].='"';
							$k++;
						}elseif($quoted){
							$quoted = false;
						}elseif(empty($data[$dataPointer])){
							$quoted = true;
						}else{
							$data[$dataPointer].='"';
						}
						break;
					case $this->separator:
						if(!$quoted){
							$data[]='';
							$dataPointer++;
							break;
						}
					default:
						$data[$dataPointer].=$this->importLines[$this->i][$k];
						break;
				}
				$k++;
			}
			$this->_checkLineData($data);
			if(count($data) < $this->numberColumns||$quoted){
				$data[$dataPointer].="\r\n";
			}
			$this->i++;
		}
		if($data!=false) $this->_checkLineData($data,false);
		return $data;
	}
	function _checkLineData(&$data,$type=true){
		if($type){
			$not_ok = count($data) > $this->numberColumns;
		}else{
			$not_ok = count($data) != $this->numberColumns;
		}
		if($not_ok){
			static $errorcount = 0;
			if(empty($errorcount)){
				$app =& JFactory::getApplication();
				$app->enqueueMessage(JText::sprintf('IMPORT_ARGUMENTS',$this->numberColumns),'error');
			}
			$errorcount++;
			if($errorcount<20){
				$app =& JFactory::getApplication();
				$app->enqueueMessage(JText::sprintf('IMPORT_ERRORLINE',$this->importLines[$this->i-1]),'notice');
			}elseif($errorcount == 20){
				$app =& JFactory::getApplication();
				$app->enqueueMessage('...','notice');
			}
			$data = $this->_getProduct();
		}
	}
	function _checkData(&$product){
		if(empty($product->product_created)){
			$product->product_created = time();
		}elseif(!is_numeric($product->product_created)){
			$product->product_created = strtotime($product->product_created);
		}
		if(empty($product->product_modified)){
			$product->product_modified = time();
		}elseif(!is_numeric($product->product_modified)){
			$product->product_modified = strtotime($product->product_modified);
		}
		if(empty($product->product_sale_start)){
			if(!empty($this->template->product_sale_start)){
				$product->product_sale_start = $this->template->product_sale_start;
			}
		}elseif(!is_numeric($product->product_sale_start)){
			$product->product_sale_start = strtotime($product->product_sale_start);
		}
		if(empty($product->product_sale_end)){
			if(!empty($this->template->product_sale_end)){
				$product->product_sale_end = $this->template->product_sale_end;
			}
		}elseif(!is_numeric($product->product_sale_end)){
			$product->product_sale_end = strtotime($product->product_sale_end);
		}
		if(!empty($product->product_parent_id)){
			if($product->product_parent_id==$product->product_code){
				$product->product_parent_id='';
			}
		}
		if(empty($product->product_type)){
			if(empty($product->product_parent_id)){
				$product->product_type='main';
			}else{
				$product->product_type='variant';
			}
		}
		if(!isset($product->product_tax_id) || strlen($product->product_tax_id)<1){
			$product->product_tax_id = $this->tax_category;
		}else{
			if(!is_numeric($product->product_tax_id)){
				$id = $this->_getCategory($v,0,!$this->createCategories,'tax');
				if(empty($id) && $this->createCategories){
					$id = $this->_createCategory($product->product_tax_id,'tax');
				}
				$product->product_tax_id = $id;
			}
		}
		if(!isset($product->product_quantity) || strlen($product->product_quantity)<1){
			if(!empty($this->template->product_quantity)){
				$product->product_quantity=$this->template->product_quantity;
			}else{
				$product->product_quantity=-1;
			}
		}
		foreach($this->fields as $field){
			if(empty($product->$field)&&!empty($this->template->$field)){
				$product->$field=$this->template->$field;
			}
		}
		if(empty($product->product_dimension_unit)){
			$product->product_dimension_unit=$this->volumeHelper->getSymbol();
		}
		if(empty($product->product_weight_unit)){
			$product->product_weight_unit=$this->weightHelper->getSymbol();
		}
		if(empty($product->product_name)){
			if(!empty($product->files)){
				$this->_separate($product->files);
				$product->product_name=substr($product->files[0],0,strrpos($product->files[0],'.'));
			}elseif(!empty($product->images)){
				$this->_separate($product->images);
				$product->product_name=substr($product->images[0],0,strrpos($product->images[0],'.'));
			}
		}
		if(!empty($product->product_published)){
			$product->product_published=1;
		}
		if(!isset($product->product_published)){
			if(!empty($this->template)){
				$product->product_published = $this->template->product_published;
			}
		}
		if(!empty($product->price_value)){
			$product->prices = array();
			$price = null;
			$price->price_value = hikashop::toFloat($product->price_value);
			if(!empty($this->price_fee)){
				$price->price_value += $price->price_value*hikashop::toFloat($this->price_fee)/100;
			}
			$price->price_min_quantity = (int)@$product->price_min_quantity;
			if($price->price_min_quantity==1){
				$price->price_min_quantity=0;
			}
			if(!empty($product->price_currency_id)){
				if(!is_numeric($product->price_currency_id)){
					$product->price_currency_id = $this->_getCurrency($product->price_currency_id);
				}
				$price->price_currency_id = $product->price_currency_id;
			}else{
				$config =& hikashop::config();
				$price->price_currency_id = $config->get('main_currency',1);
			}
			$product->prices[]=$price;
		}
		if(!empty($product->files) && !is_array($product->files)){
			$this->_separate($product->files);
			$unset = array();
			foreach($product->files as $k => $file){
				if(substr($file,0,7)=='http://'){
					$parts = explode('/',$file);
					$name = array_pop($parts);
					if(!file_exists($this->uploadFolder.$name)){
						JFile::write($this->uploadFolder.$name,file_get_contents($file));	
					}
					if(filesize($this->uploadFolder.$name)){
						$product->files[$k] = $name;
					}else{
						$unset[]=$k;
					}
				}
			}
			if(!empty($unset)){
				foreach($unset as $k){
					unset($product->files[$k]);
				}
			}
		}
		if(!empty($product->images) && !is_array($product->images)){
			$this->_separate($product->images);
			$unset = array();
			foreach($product->images as $k => $image){
				if(substr($image,0,7)=='http://'){
					$parts = explode('/',$image);
					$name = array_pop($parts);
					if(!file_exists($this->uploadFolder.$name)){
						JFile::write($this->uploadFolder.$name,file_get_contents($image));	
					}
					if(filesize($this->uploadFolder.$name)){
						$product->images[$k] = $name;
					}else{
						$unset[]=$k;
					}
				}
			}
			if(!empty($unset)){
				foreach($unset as $k){
					unset($product->images[$k]);
				}
			}
		}
		if(!empty($product->related) && !is_array($product->related)){
			$this->_separate($product->related);
		}
		if($product->product_type=='variant'){
			$product->categories = null;
		}else{
			if(empty($product->categories)){
				if(empty($this->template->categories)){
					$product->categories = array($this->mainProductCategory);
				}else{
					$product->categories = $this->template->categories;
				}
			}else{
				if(!is_array($product->categories)){
					$this->_separate($product->categories);
				}
				$parent_id=0;
				if($this->createCategories && !empty($product->parent_category)){
					$parent_id = $this->_getCategory($product->parent_category,0,false,'product');
					if(empty($parent_id)){
						$parent_id = $this->_createCategory($product->parent_category);
					}					
				}
				foreach($product->categories as $k => $v){
					if(!is_numeric($v)){
						$id = $this->_getCategory($v,0,!$this->createCategories,'product',$parent_id);
						if(empty($id) && $this->createCategories){
							$id = $this->_createCategory($v,$parent_id);
						}
						$product->categories[$k] = $id;
					}
				}
			}
		}
		if(empty($product->product_access)){
			if(!empty($this->template)){
				$product->product_access = $this->template->product_access;
			}else{
				$product->product_access = 'all';
			}
		}
		if(hikashop::level(2) && !empty($product->product_access)){
			if(!is_array($product->product_access)){
				if(!in_array($product->product_access,array('none','all'))){
					if(!is_array($product->product_access)){
						$this->_separate($product->product_access);
					}
				}
			}
			if(is_array($product->product_access)){
				$accesses = array();
				foreach($product->product_access as $access){
					if(empty($access))continue;
					if(!is_numeric($access)){
						$access = $this->_getAccess($access);
						if(empty($access))continue;
					}
					$accesses[] = $access;
				}
				$product->product_access = $accesses;
			}
		}
		if(!empty($this->characteristicColumns)){
			foreach($this->characteristicColumns as $column){
				if(isset($product->$column) && strlen($product->$column)>0){
					if($product->product_type=='main' && !empty($this->characteristicsConversionTable[$column])){
						if(!isset($product->variant_links)){
							$product->variant_links=array();
						}
						$product->variant_links[]=$this->characteristicsConversionTable[$column];
					}
					$key = strtolower(trim($product->$column,'" '));
					if(!empty($this->characteristicsConversionTable[$key])){
						if(!isset($product->variant_links)){
							$product->variant_links=array();
						}
						$product->variant_links[]=$this->characteristicsConversionTable[$key];
					}
				}
			}
		}
		if(empty($product->product_code)&&!empty($product->product_name)) $product->product_code = preg_replace('#[^a-z0-9_]#i','_',$product->product_name);
		if(empty($product->product_name)&&!empty($this->template->product_name)){
			$product->product_name = $this->template->product_name;
		}
		$unset = array();
		foreach(get_object_vars($product) as $column=>$value){
			if(!empty($this->columnNamesConversionTable[$column]) && is_array($this->columnNamesConversionTable[$column])){
				if(!empty($this->columnNamesConversionTable[$column]['append'])){
					$new_column = $this->columnNamesConversionTable[$column]['append'];
					if(in_array($column,array('files','images'))){
						if(is_array($value)){
							$tmp=array();
							foreach($value as $v){
								$tmp[]='<a href="'.$this->uploadFolder_url.$v.'">'.$v.'</a>';
							}
							$value = implode(',',$tmp);
						}else{
							$value='<a href="'.$this->uploadFolder_url.$value.'">'.$value.'</a>';
						}
					}
					$trans_string = 'HIKASHOP_FEED_'.strtoupper($column);
					$trans = JText::_($trans_string);
					if($trans_string==$trans){
						$trans=$column;
					}
					$product->$new_column.='<div id="hikashop_product_'.$column.'">'.$trans.':'.$value.'</div>';
					$unset[]=$column;
				}
				if(!empty($this->columnNamesConversionTable[$column]['copy'])){
					$new_column = $this->columnNamesConversionTable[$column]['copy'];
					$product->$new_column=$value;
				}
			}
		}
		if(!empty($unset)){
			foreach($unset as $u){
				unset($product->$u);
			}
		}
	}
	function _createCategory($category,$parent_id=0,$type='product'){
		$obj=null;
		$obj->category_name = $category;
		$obj->category_type = $type;
		if(empty($parent_id)){
			$parent_id = $this->mainProductCategory;
		}
		$obj->category_parent_id = $parent_id;
		$class = hikashop::get('class.category');
		$new_id = $class->save($obj,false);
		$this->_getCategory($obj->category_namekey,$new_id,true,$type,$parent_id);
		$this->_getCategory($obj->category_name,$new_id,true,$type,$parent_id);
		return $new_id;
	}
	function _getCategory($code,$newId=0,$error=true,$type='product',$parent_id=0){
		static $data=array();
		$namekey = $code;
		$parent_condition = '';
		if(!empty($parent_id)){
			$namekey.='__'.$parent_id;
			$parent_condition = ' AND category_parent_id='.$parent_id;
		}
		if(!empty($newId)){
			$data[$code] = $newId;
			$data[$namekey] = $newId;
		}
		if(!isset($data[$namekey])){
			$query = 'SELECT category_id FROM '.hikashop::table('category').' WHERE category_namekey='.$this->db->Quote($code).' AND category_type='.$this->db->Quote($type).$parent_condition;
			$this->db->setQuery($query);
			$data[$namekey] = $this->db->loadResult();
			if(empty($data[$namekey])){
				$query = 'SELECT category_id FROM '.hikashop::table('category').' WHERE category_name='.$this->db->Quote($code).' AND category_type='.$this->db->Quote($type).$parent_condition;
				$this->db->setQuery($query);
				$data[$namekey] = $this->db->loadResult();
				if(empty($data[$namekey])){
					if($error){
						$app =& JFactory::getApplication();
						$app->enqueueMessage('The category "'.$code.'" could not be found in the database. Products imported and using this category will be linked to the main product category.');
						$name = 'main'.ucfirst($type).'Category';
						$data[$namekey] = @$this->$name;
					}else{
						$data[$namekey] = 0;
					}
				}
			}
		}
		return $data[$namekey];
	}
	function _getRelated($code){
		static $data=array();
		if(!isset($data[$code])){
			$query = 'SELECT product_id FROM '.hikashop::table('product').' WHERE product_code='.$this->db->Quote($code);
			$this->db->setQuery($query);
			$id = $this->db->loadResult();
			if(empty($id)){
				return $code;
			}else{
				$data[$code] = $id;
			}
		}
		return $data[$code];
	}
	function _getAccess($access){
		static $data=array();
		if(!isset($data[$access])){
			if(version_compare(JVERSION,'1.6','<')){
				$query = 'SELECT id FROM '.hikashop::table('core_acl_aro_groups',false).' WHERE name='.$this->db->Quote($access);
			}else{
				$query = 'SELECT id FROM '.hikashop::table('usergroups',false).' WHERE title='.$this->db->Quote($access);
			}
			$this->db->setQuery($query);
			$data[$access] = (int)$this->db->loadResult();
		}
		return $data[$access];
	}
	function _getCurrency($code){
		static $data=array();
		if(!isset($data[$code])){
			$query = 'SELECT currency_id FROM '.hikashop::table('currency').' WHERE currency_code='.$this->db->Quote(strtoupper($code));
			$this->db->setQuery($query);
			$data[$code] = $this->db->loadResult();
		}
		return $data[$code];
	}
	function _insertPrices(&$products){
		$values = array();
		$totalValid=0;
		$insert = 'INSERT IGNORE INTO '.hikashop::table('price').' (`price_value`,`price_currency_id`,`price_min_quantity`,`price_product_id`) VALUES (';
		$ids = array();
		foreach($products as $product){
			if(empty($product->prices) && empty($product->hikashop_update)){
				if(!empty($this->template->prices)){
					foreach($this->template->prices as $price){
						$value = array($this->db->Quote($price->price_value),(int)$price->price_currency_id,(int)$price->price_min_quantity,$product->product_id);
						$values[] = implode(',',$value);
						$totalValid++;
						if( $totalValid%$this->perBatch == 0){
							$this->db->setQuery($insert.implode('),(',$values).')');
							$this->db->query();
							$totalValid=0;
							$values=array();
						}
					}
				}
			}elseif(!empty($product->prices)){
				$ids[]=(int)$product->product_id;
				foreach($product->prices as $price){
					$value = array($this->db->Quote($price->price_value),(int)$price->price_currency_id,(int)$price->price_min_quantity,$product->product_id);
					$values[] = implode(',',$value);
					$totalValid++;
					if( $totalValid%$this->perBatch == 0){
						if(!empty($ids)){
							$this->db->setQuery('DELETE FROM '.hikashop::table('price').' WHERE price_product_id IN ('.implode(',',$ids).')');
							$this->db->query();
							$ids=array();
						}
						$this->db->setQuery($insert.implode('),(',$values).')');
						$this->db->query();
						$totalValid=0;
						$values=array();
					}
				}
			}
		}
		if(!empty($values)){
			if(!empty($ids)){
				$this->db->setQuery('DELETE FROM '.hikashop::table('price').' WHERE price_product_id IN ('.implode(',',$ids).')');
				$this->db->query();
			}
			$this->db->setQuery($insert.implode('),(',$values).')');
			$this->db->query();
		}
	}
	function _insertCategories(&$products){
		$values = array();
		$totalValid=0;
		$insert = 'INSERT IGNORE INTO '.hikashop::table('product_category').' (`category_id`,`product_id`) VALUES (';
		$ids = array();
		foreach($products as $product){
			if(empty($product->categories) && empty($product->hikashop_update)){
				if(!empty($this->template->categories)){
					foreach($this->template->categories as $id){
						$value = array((int)$id,$product->product_id);
						$values[] = implode(',',$value);
						$totalValid++;
						if( $totalValid%$this->perBatch == 0){
							$this->db->setQuery($insert.implode('),(',$values).')');
							$this->db->query();
							$totalValid=0;
							$values=array();
						}
					}
				}
			}elseif(!empty($product->categories)){
				$ids[] = (int)$product->product_id;
				foreach($product->categories as $id){
					$value = array((int)$id,$product->product_id);
					$values[] = implode(',',$value);
					$totalValid++;
					if( $totalValid%$this->perBatch == 0){
						if(!empty($ids)){
							$this->db->setQuery('DELETE FROM '.hikashop::table('product_category').' WHERE product_id IN ('.implode(',',$ids).')');
							$this->db->query();
							$ids=array();
						}
						$this->db->setQuery($insert.implode('),(',$values).')');
						$this->db->query();
						$totalValid=0;
						$values=array();
					}
				}
			}
		}
		if(!empty($values)){
			if(!empty($ids)){
				$this->db->setQuery('DELETE FROM '.hikashop::table('product_category').' WHERE product_id IN ('.implode(',',$ids).')');
				$this->db->query();
			}
			$this->db->setQuery($insert.implode('),(',$values).')');
			$this->db->query();
		}
	}
	function _insertRelated(&$products){
		$values = array();
		$totalValid=0;
		$insert = 'INSERT IGNORE INTO '.hikashop::table('product_related').' (`product_related_id`,`product_related_type`,`product_id`) VALUES (';
		$ids=array();
		foreach($products as $product){
			if(empty($product->related) && empty($product->hikashop_update)){
				if(!empty($this->template->related)){
					foreach($this->template->related as $id){
						$value = array((int)$id,$this->db->Quote('related'),$product->product_id);
						$values[] = implode(',',$value);
						$totalValid++;
						if( $totalValid%$this->perBatch == 0){
							$this->db->setQuery($insert.implode('),(',$values).')');
							$this->db->query();
							$totalValid=0;
							$values=array();
						}
					}
				}
			}elseif(!empty($product->related)){
				$ids[] = (int)$product->product_id;
				foreach($product->related as $k => $id){
					if(!is_numeric($id)){
						$id = $this->_getRelated($id);
						$product->related[$k] = $id;
					}
					$value = array((int)$id,$this->db->Quote('related'),$product->product_id);
					$values[] = implode(',',$value);
					$totalValid++;
					if( $totalValid%$this->perBatch == 0){
						if(!empty($ids)){
							$this->db->setQuery('DELETE FROM '.hikashop::table('product_related').' WHERE product_id IN ('.implode(',',$ids).')');
							$this->db->query();
							$ids=array();
						}
						$this->db->setQuery($insert.implode('),(',$values).')');
						$this->db->query();
						$totalValid=0;
						$values=array();
					}
				}
			}
		}
		if(!empty($values)){
			if(!empty($ids)){
				$this->db->setQuery('DELETE FROM '.hikashop::table('product_related').' WHERE product_id IN ('.implode(',',$ids).')');
				$this->db->query();
			}
			$this->db->setQuery($insert.implode('),(',$values).')');
			$this->db->query();
		}
	}
	function _insertVariants(&$products){
		$values = array();
		$totalValid=0;
		$insert = 'INSERT IGNORE INTO '.hikashop::table('variant').' (`variant_characteristic_id`,`variant_product_id`) VALUES (';
		$ids = array();
		foreach($products as $product){
			if(empty($product->variant_links)&&!empty($this->template->variant_links) && empty($product->hikashop_update)){
				$product->variant_links = $this->template->variant_links;
			}
			if(!empty($product->variant_links)){
				$ids[] = (int)$product->product_id;
				foreach($product->variant_links as $link){
					$value = array((int)$link,$product->product_id);
					$values[] = implode(',',$value);
					$totalValid++;
					if( $totalValid%$this->perBatch == 0){
						if(!empty($ids)){
							$this->db->setQuery('DELETE FROM '.hikashop::table('variant').' WHERE variant_product_id IN ('.implode(',',$ids).')');
							$this->db->query();
							$ids=array();
						}
						$this->db->setQuery($insert.implode('),(',$values).')');
						$this->db->query();
						$totalValid=0;
						$values=array();
					}
				}
			}
		}
		if(!empty($values)){
			if(!empty($ids)){
				$this->db->setQuery('DELETE FROM '.hikashop::table('variant').' WHERE variant_product_id IN ('.implode(',',$ids).')');
				$this->db->query();
			}
			$this->db->setQuery($insert.implode('),(',$values).')');
			$this->db->query();
		}
	}
	function _insertTranslations(&$products){
		if(empty($this->template->translations) || !empty($product->hikashop_update)) return true;
		$values = array();
		$totalValid=0;
		$translation = reset($this->template->translations);
		$value = array();
		foreach(get_object_vars($translation) as $key => $field){
			$value[] = $key;
		}
		$insert = 'INSERT IGNORE INTO '.hikashop::table('jf_content',false).' ('.implode(',',$value).') VALUES (';
		foreach($products as $product){
			foreach($this->template->translations as $translation){
				$translation->reference_id = $product->product_id;
				$value = array();
				foreach(get_object_vars($translation) as $field){
					$value[] = $this->db->Quote($field);
				}
				$values[] = implode(',',$value);
				$totalValid++;
				if( $totalValid%$this->perBatch == 0){
					$this->db->setQuery($insert.implode('),(',$values).')');
					$this->db->query();
					$totalValid=0;
					$values=array();
				}
			}
		}
		if(!empty($values)){
			$this->db->setQuery($insert.implode('),(',$values).')');
			$this->db->query();
		}
	}
	function _insertFiles(&$products,$type='files'){
		$db_type = 'product';
		if($type=='files'){
			$db_type='file';
		}
		$values = array();
		$totalValid=0;
		$ids=array();
		$insert = 'INSERT IGNORE INTO '.hikashop::table('file').' (`file_name`,`file_description`,`file_path`,`file_type`,`file_ref_id`) VALUES (';
		foreach($products as $product){
			if(empty($product->$type) && empty($product->hikashop_update)){
				if(!empty($this->template->$type)){
					foreach($this->template->$type as $file){
						$value = array($this->db->Quote($file->file_name),$this->db->Quote($file->file_description),$this->db->Quote($file->file_path),$this->db->Quote($db_type),$product->product_id);
						$values[] = implode(',',$value);
						$totalValid++;
						if( $totalValid%$this->perBatch == 0){
							$this->db->setQuery($insert.implode('),(',$values).')');
							$this->db->query();
							$totalValid=0;
							$values=array();
						}
					}					
				}
			}elseif(!empty($product->$type)){
				$ids[]=(int)$product->product_id;		
				foreach($product->$type as $file){
					if(is_string($file)){
						$value = array($this->db->Quote(str_replace('_',' ',substr($file,0,strrpos($file,'.')))),$this->db->Quote(''),$this->db->Quote($file),$this->db->Quote($db_type),$product->product_id);
					}else{
						$value = array($this->db->Quote($file->file_name),$this->db->Quote($file->file_description),$this->db->Quote($file->file_path),$this->db->Quote($db_type),$product->product_id);
					}
					$values[] = implode(',',$value);
					$totalValid++;
					if( $totalValid%$this->perBatch == 0){
						if(!empty($ids)){
							$this->db->setQuery('DELETE FROM '.hikashop::table('file').' WHERE file_ref_id IN ('.implode(',',$ids).') AND file_type=\''.$db_type.'\'');
							$this->db->query();
							$ids = array();
						}
						$this->db->setQuery($insert.implode('),(',$values).')');
						$this->db->query();
						$totalValid=0;
						$values=array();
					}
				}
			}
		}
		if(!empty($values)){
			if(!empty($ids)){
				$this->db->setQuery('DELETE FROM '.hikashop::table('file').' WHERE file_ref_id IN ('.implode(',',$ids).') AND file_type=\''.$db_type.'\'');
				$this->db->query();
			}
			$this->db->setQuery($insert.implode('),(',$values).')');
			$this->db->query();
		}
	}
	function _separate(&$files){
		$separator='';
		foreach($this->listSeparators as $sep){
			if(strpos($files,$sep) !== false){
				$separator = $sep;
				break;
			}
		}
		if(!empty($separator)){
			$files = explode($separator,$files);
		}else{
			$files = array($files);
		}
	}
	function _autoDetectHeader(){
		$app =& JFactory::getApplication();
		$this->separator = ',';
		$this->header = str_replace("\xEF\xBB\xBF","",$this->header);
		foreach($this->listSeparators as $sep){
			if(strpos($this->header,$sep) !== false){
				$this->separator = $sep;
				break;
			}
		}
		$this->columns = explode($this->separator,$this->header);
		$columnsTable = $this->db->getTableFields(hikashop::table('product'));
		$columns = reset($columnsTable);
		$columns['price_value']='price_value';
		$columns['price_currency_id']='price_currency_id';
		$columns['price_quantity']='price_quantity';
		$columns['files']='files';
		$columns['images']='images';
		$columns['parent_category']='parent_category';
		$columns['categories']='categories';
		$columns['related']='related';
		if(hikashop::level(2)){
			$columns['product_access']='product_access';
		}
		foreach($this->columns as $i => $oneColumn){
			$this->columns[$i] = strtolower(trim($oneColumn,'" '));
			if(!isset($columns[$this->columns[$i]])){
				if(isset($this->columnNamesConversionTable[$this->columns[$i]])){
					if(is_array($this->columnNamesConversionTable[$this->columns[$i]])){
						$this->columnNamesConversionTable[$this->columnNamesConversionTable[$this->columns[$i]]['name']]=$this->columnNamesConversionTable[$this->columns[$i]];
						$this->columns[$i]=$this->columnNamesConversionTable[$this->columns[$i]]['name'];
					}else{
						$this->columns[$i]=$this->columnNamesConversionTable[$this->columns[$i]];
					}
				}else{
					if(isset($this->characteristicsConversionTable[$this->columns[$i]])){
						$this->characteristicColumns[] = $this->columns[$i];
					}else{
						$possibilities = array_diff(array_keys($columns),array('product_id'));
						if(!empty($this->characteristics)){
							foreach($this->characteristics as $char){
								if(empty($char->characteristic_parent_id)){
									$possibilities[]=strtolower(trim($char->characteristic_value,' "'));
								}
							}
						}
						if($this->header_errors){
							$app->enqueueMessage(JText::sprintf('IMPORT_ERROR_FIELD',$this->columns[$i],implode(' | ',$possibilities)),'error');
						}
					}
				}
			}
		}
		return true;
	}
	function _insertProducts(&$products){		
		$this->_insertOneTypeOfProducts($products,'main');
		foreach($products as $k => $variant){
			if($variant->product_type!='main'){
				$parent_code = $variant->product_parent_id;
				if(is_numeric($parent_code)){
					foreach($products as $k2 => $main){
						if($variant->product_parent_id == $main->product_id){
							$parent_code=$main->product_code;
						}
					}
				}
				if(!empty($this->codes[$parent_code])){
					$products[$k]->product_parent_id = @$this->codes[$parent_code]->product_id;
				}
				if(empty($products[$k]->product_parent_id)){
					unset($products[$k]->product_parent_id);
				}
			}
		}
		$this->_insertOneTypeOfProducts($products,'variant');
		$this->_insertVariants($products);
		$this->_insertPrices($products);
		$this->_insertFiles($products,'images');
		$this->_insertFiles($products,'files');
		$this->_insertCategories($products);
		$this->_insertRelated($products);
		$this->_insertTranslations($products);
	}
	function _insertOneTypeOfProducts(&$products,$type='main'){
		if(empty($products)) return true;
		$lines = array();
		$totalValid=0;
		$fields = array();
		$all_fields = $this->all_fields;
		if($type!='main'){
			$all_fields[]='product_parent_id';
		}
		$all_fields[]='product_id';
		foreach($this->columnsProductTable as $field){
			if(!in_array($field,$all_fields)){
				$all_fields[]=$field;
			}
		}
		foreach($all_fields as $field){
			$fields[]= '`'.$field.'`';
		}
		$fields = implode(', ',$fields);
		$insert = 'REPLACE INTO '.hikashop::table('product').' ('.$fields.') VALUES (';
		$codes = array();
		foreach($products as $product){
			if($product->product_type!=$type) continue;
			$codes[$product->product_code] = $this->db->Quote($product->product_code);
		}
		if(!empty($codes)){
			$query = 'SELECT * FROM '.hikashop::table('product'). ' WHERE product_code IN ('.implode(',',$codes).')';
			$this->db->setQuery($query);
			$already = $this->db->loadObjectList('product_id');
			if(!empty($already)){
				foreach($already as $code){
					$found = false;
					foreach($products as $k => $product){
						if($product->product_code==$code->product_code){
							$found = $k;
							break;
						}
					}
					if($found!==false){
						if($this->overwrite){
							$products[$found]->product_id = $code->product_id;
							$products[$found]->hikashop_update = true;
						}else{
							unset($products[$found]);
						}
					}
				}
			}
			$exist=0;
			if(!empty($codes)){
				foreach($products as $product){
					if($product->product_type!=$type || empty($codes[$product->product_code])) continue;
					$line = array();
					foreach($all_fields as $field){
						if(!isset($product->$field) && !empty($product->product_id) && isset($already[$product->product_id])){
							$product->$field = $already[$product->product_id]->$field;
						}
						if($field=='product_id'){
							if(empty($product->$field)){
								$line[] = 'NULL';
							}else{
								$exist++;
								unset($codes[$product->product_code]);
								$line[] = $this->db->Quote(@$product->$field);
							}
						}else{
							if($field=='product_published' && !isset($product->$field) && $this->force_published){
								$product->product_published=1;
							}
							$line[] = $this->db->Quote(@$product->$field);
						}
					}
					$lines[]=implode(',',$line);
					$totalValid++;
					if( $totalValid%$this->perBatch == 0){
						$this->db->setQuery($insert.implode('),(',$lines).')');
						$this->db->query();
						if($type=='main' || $this->countVariant){
							$this->totalInserted += count($lines);
						}
						$totalValid=0;
						$lines=array();
					}
				}
				if(!empty($lines)){
					$this->db->setQuery($insert.implode('),(',$lines).')');
					$this->db->query();
					if($type=='main' || $this->countVariant){
						$this->totalInserted += count($lines);
					}
				}
			}
			$this->totalInserted=$this->totalInserted-$exist;
			if(!empty($codes)){
				$query = 'SELECT product_code, product_id FROM '.hikashop::table('product'). ' WHERE product_code IN ('.implode(',',$codes).')';
				$this->db->setQuery($query);
				$newCodes = (array)$this->db->loadObjectList('product_code');
				foreach($newCodes as $k => $code){
					$this->codes[$k]=$code;
				}
				foreach($products as $k => $product){
					if($product->product_type==$type && !empty($this->codes[$product->product_code])){
						$products[$k]->product_id = @$this->codes[$product->product_code]->product_id;
						if($type=='variant'){
							$this->products_already_in_db[(int)@$products[$k]->product_parent_id]=(int)@$products[$k]->product_parent_id;
							$this->new_variants_in_db[(int)@$products[$k]->product_id]=(int)@$products[$k]->product_id;
						}
					}
				}
			}
		}
	}
}
Return current item: HikaShop