db = JFactory::getDBO(); $this->options = null; $this->refreshPage = false; $this->linkstyle = ' style="color:#297F93;text-decoration:none;" onmouseover="this.style.color=\'#3AABC6\';this.style.textDecoration=\'underline\';" onmouseout="this.style.color=\'#297F93\';this.style.textDecoration=\'none\';" '; $this->titlestyle = ' style="color:#297F93; text-decoration:underline;" '; $this->bullstyle = ' style="color:#E69700;" '; $this->pmarginstyle = 'style="margin-left:15px;"'; $this->titlefont = ' style="font-size:1.2em;" '; $this->copywarning = ' style="color:grey;font-size:0.8em" '; $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','product_min_per_order'); $fieldClass = hikashop_get('class.field'); $userFields = $fieldClass->getData('','product'); if(!empty($userFields)){ foreach($userFields as $k => $v){ if($v->field_type!='customtext'){ $this->fields[]=$k; } } } $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(); if(version_compare(JVERSION,'3.0','<')) { $columnsProductTable = $this->db->getTableFields(hikashop_table('product')); $this->columnsProductTable = array_keys($columnsProductTable[hikashop_table('product')]); } else { $this->columnsProductTable = array_keys($this->db->getTableColumns(hikashop_table('product'))); } $characteristic = hikashop_get('class.characteristic'); $characteristic->loadConversionTables($this); $this->volumeHelper = hikashop_get('helper.volume'); $this->weightHelper = hikashop_get('helper.weight'); $class = hikashop_get('class.category'); $this->mainProductCategory = 'product'; $class->getMainElement($this->mainProductCategory); $this->mainManufacturerCategory = 'manufacturer'; $class->getMainElement($this->mainManufacturerCategory); $this->mainTaxCategory = 'tax'; $class->getMainElement($this->mainTaxCategory); $this->db->setQuery('SELECT category_id FROM '. hikashop_table('category'). ' WHERE category_type=\'tax\' && category_parent_id='.(int)$this->mainTaxCategory.' ORDER BY category_ordering DESC'); $this->tax_category = $this->db->loadResult(); $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($template_product_id){ $productClass = hikashop_get('class.product'); if($productClass->getProducts($template_product_id,'import') && !empty($productClass->products)){ $key = key($productClass->products); $this->template = $productClass->products[$key]; } } } function importFromFolder($type,$delete,$uploadFolder){ $config =& hikashop_config(); if($type=='both'){ $allowed = explode(',',strtolower($config->get('allowedimages'))); }else{ $allowed = explode(',',strtolower($config->get('allowed'.$type))); } $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($delete && !$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'); if(!empty($this->template->variants)){ $this->countVariant = false; } $oldType = $type; 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; } $type = $oldType; $newProduct = new stdClass(); if($type=='both'){ $newProduct->files = $file; $newProduct->images = $file; }else{ $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 = (!HIKASHOP_PHP5) ? $variant : clone($variant); unset($copy->product_id); $copy->product_parent_id = $newProduct->product_code; $copy->product_code = $newProduct->product_code.'_'.$copy->product_code; $products[]=$copy; } } $this->_insertProducts($products); $folder = 'image'; if($type!='images'){ $folder = 'file'; } $uploadPath = $fileClass->getPath($folder); if($delete){ JFile::move($uploadFolder.$file,$uploadPath.$file); }else{ JFile::copy($uploadFolder.$file,$uploadPath.$file); } if($type=='both'){ $type='images'; $uploadPath2 = $fileClass->getPath('image'); JFile::copy($uploadPath.$file,$uploadPath2.$file); } if($type!='files'){ $imageHelper->resizeImage($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){ $this->addTemplate($product_id); $newProduct = new stdClass(); $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 = (!HIKASHOP_PHP5) ? $variant : clone($variant); $copy->product_parent_id = $newProduct->product_code; $copy->product_code = $copy->product_code.'_copy'.rand(); unset($copy->product_id); $products[]=$copy; } } JPluginHelper::importPlugin( 'hikashop' ); $dispatcher = JDispatcher::getInstance(); $do = true; $dispatcher->trigger( 'onBeforeProductCopy', array( & $this->template, & $products[0], & $do) ); if(!$do){ return false; } $this->_insertProducts($products); $dispatcher->trigger( 'onAfterProductCopy', array( & $this->template, & $products[0]) ); } return true; } function importFromFile(&$importFile, $process = true){ $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','
'); } if(!is_writable($uploadPath)){ @chmod($uploadPath,'0755'); if(!is_writable($uploadPath)){ $app->enqueueMessage(JText::sprintf( 'WRITABLE_FOLDER',$uploadPath), 'notice'); } } $attachment = new stdClass(); $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(!preg_match('#\.('.str_replace(array(',','.'),array('|','\.'),$config->get('allowedfiles')).')$#Ui',$attachment->filename,$extension) || preg_match('#\.(php.?|.?htm.?|pl|py|jsp|asp|sh|cgi)$#Ui',$attachment->filename)){ $app->enqueueMessage(JText::sprintf( 'ACCEPTED_TYPE',substr($attachment->filename,strrpos($attachment->filename,'.')+1),$config->get('allowedfiles')), 'notice'); return false; } //We create back the filename and remove all dot to avoid security issues with double type extensions : .php.ico $attachment->filename = str_replace(array('.',' '),'_',substr($attachment->filename,0,strpos($attachment->filename,$extension[0]))).$extension[0]; 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; }; if($process){ unlink($uploadPath . $attachment->filename); $toTest = array(); if(empty($this->charsetConvert)){ $encodingHelper = hikashop_get('helper.encoding'); $this->charsetConvert = $encodingHelper->detectEncoding($contentFile); } return $this->handleContent($contentFile); }else{ $filePath = $uploadPath . $attachment->filename; return $filePath; } } 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'); $errorcount = 0; while ($data = $this->_getProduct()) { $this->totalTry++; $newProduct = new stdClass(); foreach($data as $num => $value){ if(!empty($this->columns[$num])){ $field = $this->columns[$num]; if( strpos('|',$field) !== false ) { $field = str_replace('|','__tr__',$field); } //$newProduct->$field = trim($value,'\'" '); $newProduct->$field = preg_replace('#^[\'" ]{1}(.*)[\'" ]{1}$#','$1',$value); if(!empty($this->charsetConvert)){ $newProduct->$field = $encodingHelper->change($newProduct->$field,$this->charsetConvert,'UTF-8'); } } } $this->_checkData($newProduct,true); if(!empty($newProduct->product_code)){ $importProducts[] = $newProduct; if(count($this->currentProductVariants)){ foreach($this->currentProductVariants as $variant){ $importProducts[] = $variant; } } $this->totalValid++; }else{ $errorcount++; if($errorcount<20){ if(isset($this->importLines[$this->i-1]))$app->enqueueMessage(JText::sprintf('IMPORT_ERRORLINE',$this->importLines[$this->i-1]).' '.JText::_('PRODUCT_NOT_FOUND'),'notice'); }elseif($errorcount == 20){ $app->enqueueMessage('...','notice'); } } 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).') AND product_type=\'variant\''); if(!HIKASHOP_J25){ $variants_to_be_deleted = $this->db->loadResultArray(); } else { $variants_to_be_deleted = $this->db->loadColumn(); } 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($quoted && 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]),'notice'); $data = $this->_getProduct(); }elseif($errorcount == 20){ $app = JFactory::getApplication(); $app->enqueueMessage('...','notice'); } } } function _checkData(&$product,$main=false){ $this->currentProductVariants = array(); 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_weight)){ $product->product_weight = hikashop_toFloat($product->product_weight); } if(!empty($product->product_width)){ $product->product_width = hikashop_toFloat($product->product_width); } if(!empty($product->product_height)){ $product->product_height = hikashop_toFloat($product->product_height); } if(!empty($product->product_length)){ $product->product_length = hikashop_toFloat($product->product_length); } if(empty($product->product_type)){ if(empty($product->product_parent_id)){ $product->product_type='main'; }else{ if(!empty($product->product_parent_id) && !empty($product->product_code) && $product->product_parent_id == $product->product_code){ $app = JFactory::getApplication(); $app->enqueueMessage('The product '.$product->product_code.' has the same value in the product_parent_id and product_code fields which is not possible ( a main product cannot be a variant at the same time ). This product has been considered as a main product by HikaShop and has been imported as such.'); $product->product_type='main'; $product->product_parent_id=0; }else{ $product->product_type='variant'; } } }else{ if(!in_array($product->product_type,array('main','variant'))){ $product->product_type = 'main'; } } if($product->product_type=='main'){ if(!empty($product->product_parent_id)){ $app = JFactory::getApplication(); $app->enqueueMessage('The product '.@$product->product_code.' should have an empty value instead of the value '.$product->product_parent_id.' in the field product_parent_id as it is a main product (not a variant) and thus doesn\'t have any parent.','error'); } } 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($product->product_tax_id,0,!$this->createCategories,'tax'); if(empty($id) && $this->createCategories){ $id = $this->_createCategory($product->product_tax_id,0,'tax'); } $product->product_tax_id = $id; } } if(!empty($product->product_manufacturer_id) && !is_numeric($product->product_manufacturer_id)){ $id = $this->_getCategory($product->product_manufacturer_id,0,!$this->createCategories,'manufacturer'); if(empty($id) && $this->createCategories){ $id = $this->_createCategory($product->product_manufacturer_id,0,'manufacturer'); } $product->product_manufacturer_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; } } if(isset($product->product_quantity) && !is_numeric($product->product_quantity)){ $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(); }else{ $product->product_dimension_unit= strtolower($product->product_dimension_unit); } if(empty($product->product_weight_unit)){ $product->product_weight_unit=$this->weightHelper->getSymbol(); }else{ $product->product_weight_unit= strtolower($product->product_weight_unit); } 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_with_tax)){ $currencyHelper = hikashop_get('class.currency'); if(empty($product->product_tax_id)){ $product->product_tax_id = $currencyHelper->getTaxCategory(); } if($product->product_tax_id){ if(strpos($product->price_value_with_tax,'|')===false){ $product->price_value = $currencyHelper->getUntaxedPrice(hikashop_toFloat($product->price_value_with_tax),hikashop_getZone(),$product->product_tax_id); }else{ $price_value = explode('|',$product->price_value_with_tax); foreach($price_value as $k => $price_value_one){ $price_value[$k] = $currencyHelper->getUntaxedPrice($price_value_one,hikashop_getZone(),$product->product_tax_id); } $product->price_value = implode('|',$price_value); } } } if(!empty($product->price_value)){ $product->prices = array(); if(strpos($product->price_value,'|')===false){ $price = new stdClass(); $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_access)){ $price->price_access = 'all'; }else{ $price->price_access = $product->price_access; } 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; }else{ $price_value = explode('|',$product->price_value); if(!empty($product->price_min_quantity)){ $price_min_quantity = explode('|',$product->price_min_quantity); } if(!empty($product->price_access)){ $price_access = explode('|',$product->price_access); } if(!empty($product->price_currency_id)){ $price_currency_id = explode('|',$product->price_currency_id); } foreach($price_value as $k => $price_value_one){ $price = new stdClass(); $price->price_value = hikashop_toFloat($price_value_one); if(!empty($this->price_fee)){ $price->price_value += $price->price_value*hikashop_toFloat($this->price_fee)/100; } $price->price_min_quantity = (int)@$price_min_quantity[$k]; if($price->price_min_quantity==1){ $price->price_min_quantity=0; } if(empty($price_access[$k])){ $price->price_access = 'all'; }else{ $price->price_access = $price_access[$k]; } if(!empty($price_currency_id[$k])){ if(!is_numeric($price_currency_id[$k])){ $price_currency_id[$k] = $this->_getCurrency($price_currency_id[$k]); } $price->price_currency_id = $price_currency_id[$k]; }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://'||substr($file,0,8)=='https://'){ $parts = explode('/',$file); $name = array_pop($parts); if(!file_exists($this->uploadFolder.$name)){ $data = @file_get_contents($file); if(empty($data) && !empty($this->default_file)){ $name = $this->default_file; }else{ JFile::write($this->uploadFolder.$name,$data); } }else{ $size = $this->getSizeFile($file); if($size!=filesize($this->uploadFolder.$name)){ $name=$size.'_'.$name; if(!file_exists($this->uploadFolder.$name)){ JFile::write($this->uploadFolder.$name,file_get_contents($file)); } } } if(file_exists($this->uploadFolder.$name) && (filesize($this->uploadFolder.$name) > 0 || filesize($this->uploadFolder.$name) === false)){ $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://'||substr($image,0,8)=='https://'){ $parts = explode('/',$image); $name = array_pop($parts); if(!file_exists($this->uploadFolder.$name)){ JFile::write($this->uploadFolder.$name,file_get_contents($image)); }else{ $size = $this->getSizeFile($image); if($size!=filesize($this->uploadFolder.$name)){ $name=$size.'_'.$name; if(!file_exists($this->uploadFolder.$name)){ JFile::write($this->uploadFolder.$name,file_get_contents($image)); } } } if(file_exists($this->uploadFolder.$name) && (filesize($this->uploadFolder.$name) > 0 || filesize($this->uploadFolder.$name) === false)){ $product->images[$k] = $name; }else{ $unset[]=$k; } } } if(!empty($unset)){ foreach($unset as $k){ unset($product->images[$k]); } } } if(empty($product->product_name)){ if(!empty($product->files)){ if(!is_array($product->files)){ $this->_separate($product->files); } $product->product_name=substr($product->files[0],0,strrpos($product->files[0],'.')); }elseif(!empty($product->images)){ if(!is_array($product->images)){ $this->_separate($product->images); } $product->product_name=substr($product->images[0],0,strrpos($product->images[0],'.')); } } if(!empty($product->related) && !is_array($product->related)){ $this->_separate($product->related); } if(!empty($product->options) && !is_array($product->options)){ $this->_separate($product->options); } if($product->product_type=='variant'){ $product->categories = null; }else{ if(!empty($product->categories)){ if(!is_array($product->categories)){ $this->_separate($product->categories); } $parent_id=0; if($this->createCategories && !empty($product->parent_category)){ //check if parent category exists $this->_separate($product->parent_category); $parent_id = array(); foreach($product->parent_category as $k => $parent_category){ $parent_id[$k] = $this->_getCategory($parent_category,0,false,'product'); //if not, create the parent category as child of the root category if(empty($parent_id[$k])){ $parent_id[$k] = $this->_createCategory($parent_category); } } } if($this->createCategories && !empty($product->categories_image)){ $unset = array(); $this->_separate($product->categories_image); foreach($product->categories_image as $k => $image){ if(substr($image,0,7)=='http://'||substr($image,0,8)=='https://'){ $parts = explode('/',$image); $name = array_pop($parts); if(!file_exists($this->uploadFolder.$name)){ JFile::write($this->uploadFolder.$name,file_get_contents($image)); }else{ $size = $this->getSizeFile($image); if($size!=filesize($this->uploadFolder.$name)){ $name=$size.'_'.$name; if(!file_exists($this->uploadFolder.$name)){ JFile::write($this->uploadFolder.$name,file_get_contents($image)); } } } if(filesize($this->uploadFolder.$name)){ $product->categories_image[$k] = $name; }else{ $unset[]=$k; } } } if(!empty($unset)){ foreach($unset as $k){ unset($product->categories_image[$k]); } } } if($this->createCategories && !empty($product->categories_namekey)){ $this->_separate($product->categories_namekey); } foreach($product->categories as $k => $v){ if(!is_numeric($v) || !empty($product->categories_namekey)){ $pid = 0; if(is_array($parent_id)){ if(!empty($parent_id[$k])){ $pid = $parent_id[$k]; }elseif(!empty($parent_id[0])){ $pid = $parent_id[0]; } } $id = $this->_getCategory($v,0,!$this->createCategories,'product',$pid,@$product->categories_image[$k]); if(empty($id) && $this->createCategories){ $id = $this->_createCategory($v,$pid,'product',@$product->categories_image[$k],@$product->categories_namekey[$k]); } $product->categories[$k] = $id; } } } } if(!empty($product->categories_ordering)){ $this->_separate($product->categories_ordering); } if(empty($product->product_access)){ if(!empty($this->template)){ $product->product_access = @$this->template->product_access; }else{ $product->product_access = 'all'; } } if(!isset($product->product_contact) && !empty($this->template)){ $product->product_contact = @$this->template->product_contact; } if(!isset($product->product_group_after_purchase) && !empty($this->template)){ $product->product_group_after_purchase = @$this->template->product_group_after_purchase; } 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 = ','.implode(',',$accesses).','; } } //If one or several of the columns is a characteristic get its value id and add a variant entry if(!empty($this->characteristicColumns)){ foreach($this->characteristicColumns as $column){ if(isset($product->$column) && strlen($product->$column)>0){ //add main characteristic link for the main product if($product->product_type=='main' && !empty($this->characteristicsConversionTable[$column])){ if(!isset($product->variant_links)){ $product->variant_links=array(); } $product->variant_links[]=$this->characteristicsConversionTable[$column]; } //add the default value for the product and the values for the variants if(function_exists('mb_strtolower')){ $key = mb_strtolower(trim($product->$column,'" ')); }else{ $key = strtolower(trim($product->$column,'" ')); } if(!empty($this->characteristicsConversionTable[$column.'_'.$key])){ $key = $column.'_'.$key; } if(!empty($this->characteristicsConversionTable[$key])){ if(!isset($product->variant_links)){ $product->variant_links=array(); } $product->variant_links[]=$this->characteristicsConversionTable[$key]; } } } } // Start Added BRAINFORGE 21/11/12 if (!empty($product->product_id) && empty($product->product_code)){ $query = 'SELECT `product_code` FROM '.hikashop_table('product') . ' WHERE product_id='.(int)$product->product_id; $this->db->setQuery($query); $product->product_code = $this->db->loadResult(); } else // End Added BRAINFORGE 21/11/12 if(empty($product->product_code)&&!empty($product->product_name)){ $test=preg_replace('#[^a-z0-9_-]#i','',$product->product_name); if(empty($test)){ static $last_pid = null; if($last_pid===null){ $query = 'SELECT MAX(`product_id`) FROM '.hikashop_table('product'); $this->db->setQuery($query); $last_pid = (int)$this->db->loadResult(); } $last_pid++; $product->product_code = 'product_'.$last_pid; }else{ $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; } /* * Translation */ if( !empty($this->translateColumns) ) { foreach($this->translateColumns as $k => $v) { if( !empty($product->$v) ) { list($name,$lng) = explode('__tr__',$v); if( $lng == $this->locale ) { $product->$name =& $product->$v; } else { if( isset($this->translateLanguages[$lng]) ) { if( !isset($product->translations) ) { $product->translations = array(); } $obj = new stdClass(); $obj->language_id = $this->translateLanguages[$lng]; $obj->reference_table = 'hikashop_product'; $obj->reference_field = $name; $obj->value =& $product->$v; $obj->modified_by = 0; //TODO $obj->published = 1; $product->translations[] = $obj; } } } } } $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[]=''.$v.''; } $value = implode(',',$tmp); }else{ $value=''.$value.''; } } $trans_string = 'HIKASHOP_FEED_'.strtoupper($column); $trans = JText::_($trans_string); if($trans_string==$trans){ $trans=$column; } $product->$new_column.='You have already make an import. If you restart it, the import system will just import new elements
'; echo ''; return false; } $sql = "UPDATE `#__hikashop_config` SET config_value=1 WHERE config_namekey = '".$this->importName."_import_state';"; $this->db->setQuery($sql); $this->db->query(); $this->refreshPage = true; echo 'The import will restart and import new elements...
'; return true; } function importRebuildTree() { if( $this->db == null ) return false; $categoryClass = hikashop_get('class.category'); $query = 'SELECT category_namekey,category_left,category_right,category_depth,category_id,category_parent_id FROM `#__hikashop_category` ORDER BY category_left ASC'; $this->db->setQuery($query); $categories = $this->db->loadObjectList(); $root = null; $categoryClass->categories = array(); foreach($categories as $cat){ $categoryClass->categories[$cat->category_parent_id][]=$cat; if(empty($cat->category_parent_id)){ $root = $cat; } } $categoryClass->rebuildTree($root,0,1); } function copyFile($dir, $fsrc, $dst, $debug = false) { if ($debug) { var_dump($dir); var_dump($fsrc); var_dump($dst); } $src = $fsrc; if( file_exists($dir.$fsrc) ) $src = $dir.$fsrc; else if( file_exists(HIKASHOP_ROOT.$fsrc) ) $src = HIKASHOP_ROOT.$fsrc; if( file_exists($src) ) { if( !file_exists($dst) ) { $ret = JFile::copy($src, $dst); if( !$ret ) echo 'copywarning.'>The file "' . $src . '" could not be copied to "' . $dst . '"