/modules/storecommander/ead6f6fce09/SC/lib/cat/win-export/cat_win-export_process.php
PHP | 1200 lines | 973 code | 63 blank | 164 comment | 228 complexity | d42b3780460363c02c5647d8360dd121 MD5 | raw file
- <?php
- /**
- * Store Commander
- *
- * @category administration
- * @author Store Commander - support@storecommander.com
- * @version 2015-09-15
- * @uses Prestashop modules
- * @since 2009
- * @copyright Copyright © 2009-2015, Store Commander
- * @license commercial
- * All rights reserved! Copying, duplication strictly prohibited
- *
- * *****************************************
- * * STORE COMMANDER *
- * * http://www.StoreCommander.com *
- * * V 2015-09-15 *
- * *****************************************
- *
- * Compatibility: PS version: 1.1 to 1.6.1
- *
- **/
-
- $debug=false;
-
- error_reporting(E_ALL ^ E_NOTICE);
- @ini_set('display_errors', 'on');
-
- if (!isset($CRON)) $CRON=0;
- if (!isset($CRONVERSION)) $CRONVERSION=1;
-
- if(!empty($CRON))
- {
- Context::getContext()->employee = new Employee( Tools::getValue('id_employee') );
- $sc_agent=new SC_Agent();
- }
-
- require_once(SC_DIR.'lib/cat/win-export/cat_win-export_tools.php');
-
-
- $action=Tools::getValue('action');
- $id_lang=intval(Tools::getValue('id_lang'));
- $mapping=Tools::getValue('mapping');
- $cache=array();
-
- $cacheCategory=array();
- $cacheCategoryPath=array();
- $categoriesProperties=array();
- $categoryNameByID=array();
- $cacheCarriers=array();
-
- switch($action){
- case 'conf_delete':
- $exp_opt_files=Tools::getValue('exp_opt_files','');
- if ($exp_opt_files=='') die(_l('You should mark at least one file to delete'));
- $exp_opt_files_array=preg_split('/;/',$exp_opt_files);
- foreach($exp_opt_files_array as $exp_opt_file)
- {
- if ($exp_opt_file!='')
- {
- if (@unlink(SC_TOOLS_DIR.'cat_export/'.$exp_opt_file.'.script.xml'))
- {
- echo $exp_opt_file." "._l('deleted')."\n";
- }else{
- echo _l("Unable to delete this file, please check write permissions:")." ".$exp_opt_file."\n";
- }
- }
- }
- break;
- case 'conf_add':
- $scriptname=Tools::getValue('scriptname','');
- readExportConfigXML('');
- writeExportConfigXML($scriptname.'.script.xml');
- break;
- case 'reset_export':
- $return = false;
- $export_id = intval(Tools::getValue('export_id', 0));
- if(!empty($export_id))
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET exporting = 0, id_next = 0, id_combination_next = 0 WHERE id_sc_export = '".intval($export_id)."'";
- Db::getInstance()->Execute($sql);
- $return = true;
- }
-
- if(!$return)
- echo '<strong style="color: #831f1f;">'._l('An error occured during reset. Please try again.').'</span>';
- else
- echo '<strong style="color: #266e00;">'._l('The export was successfully reset.').'</span>';
-
- break;
- case 'mapping_delete':
- $filename=str_replace('.map.xml','',Tools::getValue('filename'));
- @unlink(SC_TOOLS_DIR.'cat_export/'.$filename.'.map.xml');
- break;
- case 'mapping_saveas':
- $filename=str_replace('.map.xml','',Tools::getValue('filename'));
- $mapping=Tools::getValue('mapping');
- @unlink(SC_TOOLS_DIR.'cat_export/'.$filename.'.map.xml');
- $mapping=preg_split('/;/',$mapping);
- $content="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".'<mapping version="'.SC_EXPORT_VERSION.'">';
- $contentArray=array();
- foreach($mapping AS $map)
- {
- $val=explode('|||',$map);
- if (count($val)==8)
- {
- $contentArray[(int)$val[1]]='<field>';
- $contentArray[(int)$val[1]].='<id><![CDATA['.$val[0].']]></id>';
- $contentArray[(int)$val[1]].='<used><![CDATA['.$val[2].']]></used>';
- $contentArray[(int)$val[1]].='<name><![CDATA['.$val[3].']]></name>';
- $contentArray[(int)$val[1]].='<lang><![CDATA['.$val[4].']]></lang>';
- $contentArray[(int)$val[1]].='<options><![CDATA['.$val[5].']]></options>';
- //$contentArray[(int)$val[1]].='<filters><![CDATA['.$val[6].']]></filters>';
- $contentArray[(int)$val[1]].='<modifications><![CDATA['.$val[6].']]></modifications>';
- $contentArray[(int)$val[1]].='<column_name><![CDATA['.$val[7].']]></column_name>';
- $contentArray[(int)$val[1]].='</field>'."\n";
- }
- }
- ksort($contentArray);
- $content.=join('',$contentArray).'</mapping>';
- file_put_contents(SC_TOOLS_DIR.'cat_export/'.$filename.'.map.xml', $content);
- echo _l('Data saved!');
- break;
- case 'categselection_load':
- $filename=str_replace('.sel.xml','',Tools::getValue('filename')).'.sel.xml';
- $content='';
- if (file_exists(SC_TOOLS_DIR.'cat_categories_sel/'.$filename) && $feed = simplexml_load_file(SC_TOOLS_DIR.'cat_categories_sel/'.$filename))
- foreach($feed->category AS $category)
- $content.=(string)$category->id.';';
- echo $content;
- break;
- case 'categselection_saveas':
- $filename=Tools::getValue('filename');
- $categselection=Tools::getValue('categselection');
- @unlink(SC_TOOLS_DIR.'cat_categories_sel/'.$filename.'.sel.xml');
- $categselection=preg_split('/,/',$categselection);
- $content="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".'<categselection>';
- $contentArray=array();
- foreach($categselection AS $catsel)
- {
- $val=preg_split('/,/',$catsel);
- if (count($val)==1 && $val[0]!='')
- {
- $contentArray[(int)$val[0]]='<category>';
- $contentArray[(int)$val[0]].='<id><![CDATA['.$val[0].']]></id>';
- $contentArray[(int)$val[0]].='</category>'."\n";
- }
- }
- ksort($contentArray);
- $content.=join('',$contentArray).'</categselection>';
- file_put_contents(SC_TOOLS_DIR.'cat_categories_sel/'.$filename.'.sel.xml', $content);
- echo _l('Data saved!');
- break;
- case 'categselection_delete':
- $filename=Tools::getValue('filename');
- @unlink(SC_TOOLS_DIR.'cat_categories_sel/'.$filename.'.sel.xml');
- break;
- case 'export_process':
- global $switchObject,$switchObjectOption,$switchObjectLang,$p,$getIDlangByISO,$field,$id_product,$id_product_attribute; // variable for custom import fields check, used in extensions, do not remove
- // INIT VARS
- $AUTO_EXPORT=intval(Tools::getValue('auto_export',0));
- $export_limit=intval(Tools::getValue('export_limit',500));
- $first_interval=intval(Tools::getValue('first_interval',0));
-
- $time_start = microtime(true);
-
- $ALREADY_EXPORTING = false;
- $STOP_SCRIPT = false;
-
- $switchObject='';
- $switchObjectOption='';
- $switchObjectLang='';
- $link = new Link();
- $id_product=0;
- $id_product_attribute=0;
- $defaultLanguageId = intval(Configuration::get('PS_LANG_DEFAULT'));
- $defaultLanguage=new Language($defaultLanguageId);
- $getIDlangByISO=array();
- foreach($languages AS $lang)
- $getIDlangByISO[$lang['iso_code']]=$lang['id_lang'];
- $filename=Tools::getValue('filename',0);
- $sc_active=SCI::getConfigurationValue('SC_PLUG_DISABLECOMBINATIONS',0);
- $auto_filename=$filename;//str_replace(".script.xml","",$filename);
- if ($filename===0)
- {
- if(!$AUTO_EXPORT && !$CRON)
- die(_l('You have to select a file.'));
- elseif($CRON)
- {
- echo _l('You have to select a file.');
- $STOP_SCRIPT = true;
- }
- else
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('You have to select a file.', 1).'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- }
- if($STOP_SCRIPT)
- break;
-
- $arrIdAvailableLater = array();
- if(SCI::getConfigurationValue("SC_DELIVERYDATE_INSTALLED")=="1")
- {
- $sql = "SELECT * FROM "._DB_PREFIX_."sc_available_later";
- $res=Db::getInstance()->ExecuteS($sql);
- foreach($res as $row){
- $arrIdAvailableLater[$row['id_sc_available_later']][$row['id_lang']]=$row['available_later'];
- }
- }
-
- // fields to search in ps_product_lang
- $fields_lang=array('name','available_now','link_rewrite','meta_title','meta_description','meta_keywords','description_short','description','name_with_attributes');
- // fields specific to combination, should not be read in Product object
- $combination_fields=array('name_with_attributes');
-
- $not_auto_combination_fields=array();
- if(version_compare(_PS_VERSION_, '1.5.0.0', '>='))
- $not_auto_combination_fields[]='supplier_reference';
-
- $marginMatrix_forms=array(
- 0=>'{price}-{wholesale_price}',
- 1=>'({price}-{wholesale_price})*100/{wholesale_price}',
- 2=>'{price}/{wholesale_price}',
- 3=>'{price_inc_tax}/{wholesale_price}',
- 4=>'({price_inc_tax}-{wholesale_price})*100/{wholesale_price}',
- 5=>'({price}-{wholesale_price})*100/{price}'
- );
- $marginMatrix_form = $marginMatrix_forms[_s('CAT_PROD_GRID_MARGIN_OPERATION')];
-
- if (!$CRON && !$AUTO_EXPORT)
- showHeaders();
-
- if($AUTO_EXPORT || ($CRON && $CRONVERSION>=2))
- {
- $sql = "SELECT * FROM "._DB_PREFIX_."sc_export WHERE name='".pSQL($auto_filename)."'";
- $sc_export=Db::getInstance()->ExecuteS($sql);
- if(!empty($sc_export[0]["id_sc_export"]))
- {
- $sc_export = $sc_export[0];
- }
- else
- {
- $sql = "INSERT INTO "._DB_PREFIX_."sc_export (name, last_export, exporting, id_next, id_combination_next, total_lines)
- VALUES ('".pSQL($auto_filename)."',NULL,0,0,0,0)";
- Db::getInstance()->Execute($sql);
- $temp_id = Db::getInstance()->Insert_ID();
- if(!empty($temp_id))
- $sc_export = array("id_sc_export"=>$temp_id,"name"=>$auto_filename, "last_export"=>null, "exporting"=>0, "id_next"=>0,"id_combination_next"=>0,"total_lines"=>0);
- else
- {
- if($AUTO_EXPORT)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('Error during sc_export creation.', 1).'</span>',
- "debug" => $sql,
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- else
- {
- echo _l('Error during sc_export creation.');
- $STOP_SCRIPT= true;
- }
- }
- }
- }
- if($STOP_SCRIPT)
- break;
-
-
- /*$time_end = microtime(true);
- $time = $time_end - $time_start;
- echo "<br/><br/>after auto export : $time seconds";*/
-
- // GET MAPPING
- readExportConfigXML($filename);
- if ($exportConfig['fieldsep']=='dcomma') $exportConfig['fieldsep']=';';
- if ($exportConfig['fieldsep']=='dcommamac') $exportConfig['fieldsep']=';';
- if ($exportConfig['fieldsep']=='tab') $exportConfig['fieldsep']=' ';
- if ($exportConfig['enclosedby']=='quote') $exportConfig['enclosedby']='"';
- $selected_shops_id = (int)$exportConfig['shops'];
- if(empty($selected_shops_id))
- $selected_shops_id = SCI::getSelectedShop();
- if(empty($selected_shops_id))
- $selected_shops_id = (int)Configuration::get('PS_SHOP_DEFAULT');
-
- /* Server Params */
- $server_host = getHttpHost(false, true);
- $protocol = 'http://';
- $protocol_ssl = 'https://';
- $protocol_link = (Configuration::get('PS_SSL_ENABLED')) ? $protocol_ssl : $protocol;
- if (SCMS)
- {
- $shop=new Shop((int)$selected_shops_id);
- $_PS_BASE_URL_ = $protocol_link.$shop->domain.$shop->getBaseURI().'img/p/';
- }else{
- $_PS_BASE_URL_ = $protocol_link.$server_host._THEME_PROD_DIR_;
- }
-
- // READ MAPPING
- $fieldList=array();
- $mapping=array();
- if ($exportConfig['categoriessel']=='')
- {
- if(!$AUTO_EXPORT && !$CRON)
- die(_l('You have to set the category selection for the script')._l(':').' '.substr($filename,0,-11));
- elseif($CRON)
- {
- echo _l('You have to set the category selection for the script')._l(':').' '.substr($filename,0,-11);
- $STOP_SCRIPT = true;
- }
- else
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('You have to set the category selection for the script')._l(':').' '.substr($filename,0,-11).'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- }
- if ($exportConfig['mapping']=='')
- {
- if(!$AUTO_EXPORT && !$CRON)
- die(_l('You have to set the mapping for the script.')._l(':').' '.substr($filename,0,-11));
- elseif($CRON)
- {
- echo _l('You have to set the mapping for the script.')._l(':').' '.substr($filename,0,-11);
- $STOP_SCRIPT = true;
- }
- else
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('You have to set the mapping for the script.')._l(':').' '.substr($filename,0,-11).'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- }
- if ($exportConfig['mapping']!='' && $feed = @simplexml_load_file(SC_TOOLS_DIR.'cat_export/'.$exportConfig['mapping']))
- {
- foreach($feed->field AS $mfield)
- {
- if ((int)$mfield->used)
- {
- $mapping[]=array( 'name'=>(string)$mfield->name,
- 'lang'=>(string)$mfield->lang,
- 'options'=>(string)$mfield->options,
- 'filters'=>(string)$mfield->filters,
- 'modifications'=>(string)$mfield->modifications,
- 'column_name'=>(string)$mfield->column_name);
- $fieldList[]=(string)$mfield->name;
- }
- }
- }
-
- if ($exportConfig['exportfilename']=='')
- {
- if(!$AUTO_EXPORT && !$CRON)
- die(_l('You have to define a filename for the export.')._l(':').' '.substr($filename,0,-11));
- elseif($CRON)
- {
- echo _l('You have to define a filename for the export.')._l(':').' '.substr($filename,0,-11);
- $STOP_SCRIPT = true;
- }
- else
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('You have to define a filename for the export.',1)._l(':',1).' '.substr($filename,0,-11).'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- }
- if($STOP_SCRIPT)
- break;
-
- // CRON CHECK
- if($CRON && $CRONVERSION>=2)
- {
- if($sc_export["exporting"]==1)
- {
- $ALREADY_EXPORTING = true;
- }
- else
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET exporting = 1 WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
- }
- }
- /*$time_end = microtime(true);
- $time = $time_end - $time_start;
- echo "<br/><br/>after mapping : $time seconds";*/
-
- // AUTO EXPORT & FIRST INTERVAL
- if(($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && $first_interval)
- {
- if($sc_export["exporting"]==0)
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET exporting = 1, id_next=0, id_combination_next=0, last_export='".date("Y-m-d H:i:s")."' WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
-
- // INITIALISATION
- $sql = "DELETE FROM "._DB_PREFIX_."sc_export_product WHERE id_sc_export = '".(int)$sc_export["id_sc_export"]."'";
- Db::getInstance()->Execute($sql);
-
- file_put_contents(SC_CSV_EXPORT_DIR.$exportConfig['exportfilename'], "");
-
- $sc_export["exporting"] = 0;
- $sc_export["id_next"] = 0;
- $sc_export["id_combination_next"] = 0;
- }
- else
- {
- $ALREADY_EXPORTING = true;
- /*echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('This export is already in progress.',1).'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();*/
- }
- }
-
- //die("error");
-
- // MAIN QUERIES
- if ($exportConfig['exportbydefaultcategory']) // by default category
- {
- // TOTAL LINES
- /*if($AUTO_EXPORT && $first_interval)
- {
- $sql= ' SELECT p.id_product,c.id_category';
- $sql.=' FROM '._DB_PREFIX_.'product p';
- if(SCMS && $selected_shops_id > 0)
- $sql.=' INNER JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.id_product = ps.id_product AND ps.id_shop = "'.(int)$selected_shops_id.'")';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'category_product pdc ON (pdc.id_category='.( SCMS && $selected_shops_id > 0 ? 'ps':'p').'.id_category_default AND pdc.id_product=p.id_product)';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category=pdc.id_category)';
- $sql.=' WHERE 1';
- switch($exportConfig['categoriessel']){
- case 'all' :
- break;
- case 'all_enabled' :
- $sql.=' AND c.active=1'."\n";
- break;
- case 'all_disabled' :
- $sql.=' AND c.active=0'."\n";
- break;
- default:
- $filename=$exportConfig['categoriessel'];
- $categories=array();
- if ($feed = simplexml_load_file(SC_TOOLS_DIR.'cat_categories_sel/'.$filename))
- {
- foreach($feed->category AS $category)
- $categories[]=(string)$category->id;
- $sql.=' AND pdc.id_category IN ('.join(',',$categories).')';
- }
- break;
- }
- if(SCMS && $selected_shops_id>0)
- {
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND ps.active=1');
- }else{
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND p.active=1');
- }
- $sql.=' GROUP BY p.id_product,c.id_category';
- $total_products=Db::getInstance()->ExecuteS($sql);
- $sc_export["total_lines"] = count($total_products);
-
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET total_lines='".(int)$sc_export["total_lines"]."' WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
- }*/
-
- $sql= ' SELECT p.id_product,c.id_category';
- $sql.=' FROM '._DB_PREFIX_.'product p';
- if(SCMS && $selected_shops_id > 0)
- $sql.=' INNER JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.id_product = ps.id_product AND ps.id_shop = "'.(int)$selected_shops_id.'")';
- elseif(!SCMS && version_compare(_PS_VERSION_, '1.5.0.0', '>='))
- $sql.=' INNER JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.id_product = ps.id_product AND ps.id_shop = "'.SCI::getSelectedShop().'")';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'category_product pdc ON (pdc.id_category='.((SCMS && $selected_shops_id > 0) || (!SCMS && version_compare(_PS_VERSION_, '1.5.0.0', '>='))? 'ps':'p').'.id_category_default AND pdc.id_product=p.id_product)';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category=pdc.id_category)';
- $sql.=' WHERE 1';
- switch($exportConfig['categoriessel']){
- case 'all' :
- break;
- case 'all_enabled' :
- $sql.=' AND c.active=1'."\n";
- break;
- case 'all_disabled' :
- $sql.=' AND c.active=0'."\n";
- break;
- default:
- $filename=$exportConfig['categoriessel'];
- $categories=array();
- if ($feed = simplexml_load_file(SC_TOOLS_DIR.'cat_categories_sel/'.$filename))
- {
- foreach($feed->category AS $category)
- $categories[]=(string)$category->id;
- $sql.=' AND pdc.id_category IN ('.join(',',$categories).')';
- }
- break;
- }
- if((SCMS && $selected_shops_id > 0) || (!SCMS && version_compare(_PS_VERSION_, '1.5.0.0', '>=')))
- {
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND ps.active=1');
- }else{
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND p.active=1');
- }
- if($AUTO_EXPORT || ($CRON && $CRONVERSION>=2))
- {
- $sql.=' AND (
- p.id_product NOT IN (SELECT id_product FROM '._DB_PREFIX_.'sc_export_product WHERE id_sc_export = "'.(int)$sc_export["id_sc_export"].'" AND handled=1) ';
- if(!empty($sc_export["id_next"]) && $exportConfig['exportcombinations'])
- $sql.=' OR (p.id_product = "'.$sc_export["id_next"].'") ';
- $sql.=' ) ';
- }
- $sql.=' GROUP BY p.id_product,c.id_category';
- if($AUTO_EXPORT || ($CRON && $CRONVERSION>=2))
- {
- if(!empty($sc_export["id_next"]) && $exportConfig['exportcombinations'])
- $sql.=' ORDER BY p.id_product ASC
- LIMIT '.intval($export_limit+1);
- else
- $sql.=' ORDER BY p.id_product ASC
- LIMIT '.intval($export_limit);
- }
- else
- $sql.=' ORDER BY p.id_category_default, pdc.position';
- }else{ // for all categories
-
- // TOTAL LINES
- /*if($AUTO_EXPORT && $first_interval)
- {
- $sql= ' SELECT DISTINCT cp.id_product,cp.id_category';
- $sql.=' FROM '._DB_PREFIX_.'category_product cp';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category=cp.id_category)';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product=cp.id_product)';
- if(SCMS && $selected_shops_id>0)
- {
- $sql.=' INNER JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.id_product = ps.id_product AND ps.id_shop = "'.(int)$selected_shops_id.'")';
- }
- $sql.=' WHERE 1';
- switch($exportConfig['categoriessel']){
- case 'all' :
- break;
- case 'all_enabled' :
- $sql.=' AND c.active=1';
- break;
- case 'all_disabled' :
- $sql.=' AND c.active=0';
- break;
- default:
- $filename=$exportConfig['categoriessel'];
- $categories=array();
- if ($feed = simplexml_load_file(SC_TOOLS_DIR.'cat_categories_sel/'.$filename))
- {
- foreach($feed->category AS $category)
- $categories[]=(string)$category->id;
- $sql.=' AND cp.id_category IN ('.join(',',$categories).')';
- }
- break;
- }
- if(SCMS && $selected_shops_id>0)
- {
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND ps.active=1');
- }else{
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND p.active=1');
- }
- $sql.=' GROUP BY cp.id_product,cp.id_category';
- $total_products=Db::getInstance()->ExecuteS($sql);
- $sc_export["total_lines"] = count($total_products);
-
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET total_lines='".(int)$sc_export["total_lines"]."' WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
- }*/
-
- $sql= ' SELECT DISTINCT(cp.id_product),cp.id_category';
- $sql.=' FROM '._DB_PREFIX_.'category_product cp';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'category c ON (c.id_category=cp.id_category)';
- $sql.=' LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product=cp.id_product)';
- if(SCMS && $selected_shops_id > 0)
- $sql.=' INNER JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.id_product = ps.id_product AND ps.id_shop = "'.(int)$selected_shops_id.'")';
- elseif(!SCMS && version_compare(_PS_VERSION_, '1.5.0.0', '>='))
- $sql.=' INNER JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.id_product = ps.id_product AND ps.id_shop = "'.SCI::getSelectedShop().'")';
- $sql.=' WHERE 1';
- switch($exportConfig['categoriessel']){
- case 'all' :
- break;
- case 'all_enabled' :
- $sql.=' AND c.active=1';
- break;
- case 'all_disabled' :
- $sql.=' AND c.active=0';
- break;
- default:
- $filename=$exportConfig['categoriessel'];
- $categories=array();
- if ($feed = simplexml_load_file(SC_TOOLS_DIR.'cat_categories_sel/'.$filename))
- {
- foreach($feed->category AS $category)
- $categories[]=(string)$category->id;
- $sql.=' AND cp.id_category IN ('.join(',',$categories).')';
- }
- break;
- }
- if((SCMS && $selected_shops_id > 0) || (!SCMS && version_compare(_PS_VERSION_, '1.5.0.0', '>=')))
- {
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND ps.active=1');
- }else{
- $sql.=((int)($exportConfig['exportdisabledproducts'])?'':' AND p.active=1');
- }
- if($AUTO_EXPORT || ($CRON && $CRONVERSION>=2))
- {
- $sql.=' AND (
- cp.id_product NOT IN (SELECT id_product FROM '._DB_PREFIX_.'sc_export_product WHERE id_sc_export = "'.(int)$sc_export["id_sc_export"].'") ';
- if(!empty($sc_export["id_next"]) && $exportConfig['exportcombinations'])
- $sql.=' OR (cp.id_product = "'.$sc_export["id_next"].'") ';
- $sql.=' ) ';
- }
- $sql.=' GROUP BY cp.id_product';
- if($AUTO_EXPORT || ($CRON && $CRONVERSION>=2))
- {
- if(!empty($sc_export["id_next"]) && $exportConfig['exportcombinations'])
- $sql.=' ORDER BY cp.id_product ASC
- LIMIT '.intval($export_limit+1);
- else
- $sql.=' ORDER BY cp.id_product ASC
- LIMIT '.intval($export_limit);
- }
- else
- $sql.=' ORDER BY cp.id_category, cp.position';
- }
- $products=Db::getInstance()->ExecuteS($sql);
- if (!is_array($products))
- {
- if(!$CRON)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">MySQL error: '.Db::getInstance()->getMsgError().'</span>',
- "filename" => $auto_filename,
- "debug" => $debug,
- "first_interval" => $first_interval
- ));
- exit;
- }
- }
- // $debug .= Db::getInstance()->getMsgError();
- $linecount=0;
- $linecountreal=0;
- /*$time_end = microtime(true);
- $time = $time_end - $time_start;
- echo "<br/><br/>after products : $time seconds";*/
-
- // ALREADY EXPORTING
- if(($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && $ALREADY_EXPORTING && !$exportConfig['exportcombinations'])
- {
- if(!empty($products[0]["id_product"]))
- {
- $id_product_first = $products[0]["id_product"];
-
- if($id_product_first==$sc_export["id_next"])
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET exporting = 0 WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
-
- if($AUTO_EXPORT)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('An error occured during last export with product').' #'.$id_product_first.' <br/>'._l('Check this product before to try again.').'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- else
- {
- $STOP_SCRIPT = true;
- echo _l('An error occured during last export with product').' #'.$id_product_first.' <br/>'._l('Check this product before to try again.');
- }
- }
- else
- {
- if($AUTO_EXPORT)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('This export is already in progress.',1).' - <a href="javascript: void(0);" class="reset_export" id="export_'.(int)$sc_export["id_sc_export"].'">'._l('Reset',1).'</a></span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- else
- {
- $STOP_SCRIPT = true;
- echo _l('This export is already in progress.');
- }
-
- }
- }
- else
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET exporting = 0 WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
-
- if($AUTO_EXPORT)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('An error occured during export. Please try again.',1).'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- else
- {
- $STOP_SCRIPT = true;
- echo _l('An error occured during export. Please try again.');
- }
- }
- }
- if($STOP_SCRIPT)
- break;
-
- // WRITE FILE
- if(($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && $ALREADY_EXPORTING)
- $fp = null;
- elseif(($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && !$ALREADY_EXPORTING)
- $fp = fopen(SC_CSV_EXPORT_DIR.$exportConfig['exportfilename'], 'a');
- else
- $fp = fopen(SC_CSV_EXPORT_DIR.$exportConfig['exportfilename'], 'w');
-
- // First line
- if((!$AUTO_EXPORT && !($CRON && $CRONVERSION>=2)) || (($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && $first_interval && !$ALREADY_EXPORTING))
- {
- if ($exportConfig['firstlinecontent']!='')
- {
- if (trim($exportConfig['firstlinecontent'])!='-')
- fwrite($fp,((int)$exportConfig['iso']?utf8_decode($exportConfig['firstlinecontent']):$exportConfig['firstlinecontent'])."\n");
- }else{
- $fname=getExportCSVFields();
- $fname=array_flip($fname);
- $names=array();
- $image_link_num=1;
- $image_id_num=1;
- $image_legend_num=1;
- foreach($fieldList as $k => $mfield)
- {
- if(!empty($mapping[$k]['column_name']))
- $names[]=stripslashes($mapping[$k]['column_name']);
- else
- {
- if(SCAS && ($mfield=="quantity" || $mfield=="location" || $mfield=="quantity_physical" || $mfield=="quantity_usable" || $mfield=="quantity_real"))
- {
- $temp_name = $fname[$mfield];
- if(empty($mapping[$k]['options']) || $mapping[$k]['options']=="warehouse_none")
- $temp_name .= " "._l('No warehouse');
- else
- {
- $temp = intval(str_replace("warehouse_","",$mapping[$k]['options']));
- if(!empty($temp))
- {
- $warehouse = new Warehouse((int)$temp, (int)$id_lang);
- if(!empty($warehouse->name))
- $temp_name .= " "._l('Warehouse')." ".$warehouse->reference." - ".$warehouse->name;
- }
- }
- $names[]=stripslashes($temp_name);
- }
- elseif($mfield=="supplier_reference" || $mfield=="wholesale_price")
- {
- $temp_name = $fname[$mfield];
- if(empty($mapping[$k]['options']) || $mapping[$k]['options']=="supplier_none")
- $temp_name .= " "._l('Default values display products/combinations grids');
- else
- {
- $temp = ($mapping[$k]['options']);
- if(!empty($temp))
- {
- $temp_name .= " "._l('Supplier')." ".$temp;
- }
- }
- $names[]=stripslashes($temp_name);
- }
- elseif($mfield=="links_to_all_images")
- {
- $names[]=stripslashes($fname[$mfield]);
- }
- elseif($mfield=="image_link" || $mfield=="image_id" || $mfield=="image_legend")
- {
- $num = 1;
- if($mfield=="image_link")
- {
- $num = $image_link_num;
- $image_link_num++;
- }
- if($mfield=="image_id")
- {
- $num = $image_id_num;
- $image_id_num++;
- }
- if($mfield=="image_legend")
- {
- $num = $image_legend_num;
- $image_legend_num++;
- }
- $names[]=stripslashes($fname[$mfield].($mapping[$k]['options']!='' && $mapping[$k]['options']!="supplier_none"?' '.$mapping[$k]['options']:'').($mapping[$k]['lang']!=''?' '.strtoupper($mapping[$k]['lang']):'')." ".$num);
- }
- else
- {
- $names[]=stripslashes($fname[$mfield].($mapping[$k]['options']!='' && $mapping[$k]['options']!="supplier_none"?' '.$mapping[$k]['options']:'').($mapping[$k]['lang']!=''?' '.strtoupper($mapping[$k]['lang']):''));
- }
- }
- }
- fwrite($fp,((int)$exportConfig['iso']?utf8_decode(join($exportConfig['fieldsep'],$names)):join($exportConfig['fieldsep'],$names))."\n");
- }
- }
-
- // supplier
- if (sc_in_array('supplier_reference',$fieldList,"catWinExportProcess_fieldList") || sc_in_array('wholesale_price',$fieldList,"catWinExportProcess_fieldList"))
- {
- $suppliersListByLang=array();
-
- // DEFAULT LANG
- $sql='SELECT s.name,s.`id_supplier`
- FROM `'._DB_PREFIX_.'supplier` s
- '.((SCMS && $selected_shops_id>0)?' INNER JOIN `'._DB_PREFIX_.'supplier_shop` ss ON (s.`id_supplier` = ss.`id_supplier` AND ss.id_shop = "'.(int)$selected_shops_id.'") ':'').'';
- $suppliers=Db::getInstance()->ExecuteS($sql);
- foreach($suppliers AS $supplier)
- $suppliersListByLang[$supplier['name']]=$supplier['id_supplier'];
- }
-
- // PRODUCTS TREATMENT
- foreach($products AS $idp)
- {
- $id_product_attribute=0;
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET id_next = '".(int)$idp['id_product']."' WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
-
- // array_diff checks if we need to load product details (more queries to handle)
- if(SCMS && $selected_shops_id>0)
- $p=new Product($idp['id_product'], (count($fieldList)!=count(array_diff($fieldList,array('supplier','manufacturer','vat','link_to_product','unity','tags'))) ? true:false), null, (int)$selected_shops_id );
- else
- $p=new Product($idp['id_product'], (count($fieldList)!=count(array_diff($fieldList,array('supplier','manufacturer','vat','link_to_product','unity','tags'))) ? true:false) );
-
- $id_product = $idp['id_product'];
-
- // features
- if (sc_in_array('feature',$fieldList,"catWinExportProcess_fieldList") || sc_in_array('multiple_features',$fieldList,"catWinExportProcess_fieldList"))
- {
- $featuresListByLang=array();
-
- // DEFAULT LANG
- $featuresListNameDefault = array();
- $sql='SELECT fl.name,fp.`id_feature`
- FROM `'._DB_PREFIX_.'feature_product` fp
- LEFT JOIN `'._DB_PREFIX_.'feature_lang` fl ON fp.`id_feature` = fl.`id_feature`
- '.((SCMS && $selected_shops_id>0)?' INNER JOIN `'._DB_PREFIX_.'feature_shop` fs ON (fp.`id_feature` = fs.`id_feature` AND fs.id_shop = "'.(int)$selected_shops_id.'") ':'').'
- WHERE fp.`id_product` = '.intval($idp['id_product']).'
- AND fl.`id_lang` = '.intval($sc_agent->id_lang);
- $features=Db::getInstance()->ExecuteS($sql);
- foreach($features AS $feature)
- $featuresListNameDefault[$feature['id_feature']]=$feature['name'];
-
- // OTHER LANGS
- $langs = Language::getLanguages();
- foreach($langs as $lang)
- {
- $sql='SELECT fl.name,fvl.value,fp.`id_feature`
- FROM `'._DB_PREFIX_.'feature_product` fp
- LEFT JOIN `'._DB_PREFIX_.'feature_lang` fl ON fp.`id_feature` = fl.`id_feature`
- LEFT JOIN `'._DB_PREFIX_.'feature_value_lang` fvl ON fp.`id_feature_value` = fvl.`id_feature_value`
- '.((SCMS && $selected_shops_id>0)?' INNER JOIN `'._DB_PREFIX_.'feature_shop` fs ON (fp.`id_feature` = fs.`id_feature` AND fs.id_shop = "'.(int)$selected_shops_id.'") ':'').'
- WHERE fp.`id_product` = '.intval($idp['id_product']).'
- AND fl.`id_lang` = '.intval($lang['id_lang']).'
- AND fvl.`id_lang` = '.intval($lang['id_lang']);
- $features=Db::getInstance()->ExecuteS($sql);
- $featuresListByLang[$lang['id_lang']]=array();
- foreach($features AS $feature)
- {
- $featuresListByLang[$lang['id_lang']][$featuresListNameDefault[$feature['id_feature']]]=$feature['value'];
- }
- }
- }
-
- // combinations
- if(
- (!$AUTO_EXPORT && !($CRON && $CRONVERSION>=2))
- || (($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && $exportConfig['exportcombinations'] && $idp['id_product']!=$sc_export["id_next"])
- || (($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && !$exportConfig['exportcombinations'])
- )
- $combArray=array(0 => array()); // 0: initialize array for products without combinations
- else
- $combArray=array();
- if ($exportConfig['exportcombinations'])
- {
- // IN DEFAULT LANG
- $attributesByDefaultLang = array();
- $sql='SELECT agl.`name` AS group_name, ag.`id_attribute_group`
- FROM `'._DB_PREFIX_.'attribute_group` ag
- LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group`)
- WHERE agl.`id_lang` = '.intval($sc_agent->id_lang).'
- ORDER BY ag.`id_attribute_group`';
- $temp_attributes = Db::getInstance()->ExecuteS($sql);
- foreach($temp_attributes as $temp_attribute)
- {
- $attributesByDefaultLang[$temp_attribute['id_attribute_group']]=$temp_attribute['group_name'];
- }
-
- /* Build attributes combinaisons */
- $standardFields=array('price','wholesale_price','weight','reference','upc','unit_price_impact','supplier_reference','ean13','default_on','ecotax');
- if (version_compare(_PS_VERSION_, '1.5.0.0', '>='))
- $standardFields[] = 'available_date';
- if (!SCAS)
- $standardFields[] = 'location';
- sc_ext::readExportCSVConfigXML('addInCombiFields');
- //pa.id_product_attribute, pa.quantity, pa.'.(implode(", pa.", $standardFields)).'
- $sql='SELECT pa.*, ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, al.`name` AS attribute_name, a.`id_attribute`, a.`color`,al.id_lang '.((version_compare(_PS_VERSION_, '1.5.0.0', '>=') && $selected_shops_id>0)?',pas.*':"").'
- FROM `'._DB_PREFIX_.'product_attribute` pa
- LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
- LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a.`id_attribute` = pac.`id_attribute`
- LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
- LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute`)
- LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND al.`id_lang` = agl.`id_lang`)
- '.((version_compare(_PS_VERSION_, '1.5.0.0', '>=') && $selected_shops_id>0)?' INNER JOIN `'._DB_PREFIX_.'product_attribute_shop` pas ON (pa.`id_product_attribute` = pas.`id_product_attribute` AND pas.id_shop="'.(int)$selected_shops_id.'")':"").'
- WHERE pa.`id_product` = '.intval($p->id).' ';
- if($AUTO_EXPORT || ($CRON && $CRONVERSION>=2))
- {
- $sql.=' AND (
- pa.id_product_attribute NOT IN (SELECT id_product_attribute FROM '._DB_PREFIX_.'sc_export_product WHERE id_sc_export = "'.(int)$sc_export["id_sc_export"].'" AND id_product="'.intval($p->id).'") ';
- $sql.=' ) ';
- }
- $sql.=' ORDER BY pa.`id_product_attribute`';
-
- $combinaisons = Db::getInstance()->ExecuteS($sql);
- $groups = array();
- if (is_array($combinaisons))
- {
- $combinationImages = getCombinationImages((int)$p->id);
- foreach ($combinaisons AS $k => $combinaison)
- {
- $id_product_attribute = $combinaison['id_product_attribute'];
- if (!$exportConfig['exportoutofstock'])
- {
- if (version_compare(_PS_VERSION_, '1.5.0.0', '>='))
- $qty=StockAvailable::getQuantityAvailableByProduct($p->id, ($id_product_attribute==0 ? NULL:$id_product_attribute), (version_compare(_PS_VERSION_, '1.5.0.0', '>=') && $selected_shops_id>0?(int)$selected_shops_id:null));
- else
- $qty=intval($combinaison['quantity']);
- if ($qty<=0)
- continue;
- }
- //if (!$exportConfig['exportoutofstock'] && intval($combinaison['quantity'])==0) continue;
- $combArray[$combinaison['id_product_attribute']]['price_impact'] = $combinaison['price'];
- $combArray[$combinaison['id_product_attribute']]['pa_location'] = $combinaison['location'];
- $combArray[$combinaison['id_product_attribute']]['weight_impact'] = $combinaison['weight'];
- $combArray[$combinaison['id_product_attribute']]['attribute_color'] = $combinaison['color'];
- if(isset($combinaison['minimal_quantity']))
- $combArray[$combinaison['id_product_attribute']]['minimal_quantity'] = $combinaison['minimal_quantity'];
- if(!empty($sc_active))
- $combArray[$combinaison['id_product_attribute']]['sc_active'] = $combinaison['sc_active'];
- if(SCI::getConfigurationValue("SC_DELIVERYDATE_INSTALLED")=="1")
- $combArray[$combinaison['id_product_attribute']]['id_sc_available_later'] = $combinaison['id_sc_available_later'];
- $combArray[$combinaison['id_product_attribute']]['id_image'] = sc_array_key_exists($combinaison['id_product_attribute'],$combinationImages) ? $combinationImages[$combinaison['id_product_attribute']] : 0;
- $combArray[$combinaison['id_product_attribute']]['attributes'][$combinaison['id_lang']][] = array($combinaison['group_name'], $combinaison['attribute_name'], $combinaison['id_attribute'], $combinaison['id_attribute_group']);
- //$standardFields=array('price','wholesale_price','weight','reference','upc','unit_price_impact','supplier_reference','ean13','location','default_on','ecotax');
- foreach($standardFields AS $sfield)
- {
- if (sc_array_key_exists($sfield,$combinaison))
- $combArray[$combinaison['id_product_attribute']][$sfield] = $combinaison[$sfield];
- if (version_compare(_PS_VERSION_, '1.5.0.0', '>=') && $sfield=='supplier_reference')
- $combArray[$combinaison['id_product_attribute']][$sfield] = ProductSupplier::getProductSupplierReference((int)$p->id, (int)$combinaison['id_product_attribute'], (int)$p->id_supplier);
- }
- if(intval(_s("CAT_EXPORT_EAN13_COMBI"))!="1")
- {
- if(empty($combinaison['ean13']))
- {
- $combArray[$combinaison['id_product_attribute']]['ean13']="";
- }
- }
- if(intval(_s("CAT_EXPORT_UPC_COMBI"))!="1")
- {
- if(empty($combinaison['upc']))
- {
- $combArray[$combinaison['id_product_attribute']]['upc']="";
- }
- }
- if(intval(_s("CAT_EXPORT_REF_COMBI"))!="1")
- {
- if(empty($combinaison['reference']))
- {
- $combArray[$combinaison['id_product_attribute']]['reference']="";
- }
- }
- if ($combinaison['is_color_group'])
- $groups[$combinaison['id_attribute_group']] = $combinaison['group_name'];
- }
- if (count($combArray)>1) // unset products without combinations to skip next foreach
- unset($combArray[0]);
- foreach($combArray AS $id_product_attribute => $attributes)
- {
- if ($id_product_attribute!=0)
- foreach($attributes['attributes'] AS $id_lang => $values)
- {
- foreach($values AS $v)
- {
- if (!isset($combArray[$id_product_attribute]['name_with_attributes'][$id_lang]))
- $combArray[$id_product_attribute]['name_with_attributes'][$id_lang] = $p->name[intval($id_lang)].' ';
- $combArray[$id_product_attribute]['name_with_attributes'][$id_lang].=$v[0].':'.$v[1].' - ';
- if (!isset($combArray[$id_product_attribute]['attributeByGroup'][$id_lang]))
- $combArray[$id_product_attribute]['attributeByGroup'][$id_lang] = array();
- $combArray[$id_product_attribute]['attributeByGroup'][$id_lang][ $attributesByDefaultLang[$v[3]] ]=$v[1];
- if (!isset($combArray[$id_product_attribute]['attribute_color']))
- $combArray[$id_product_attribute]['attribute_color'] = '';
- if ($v[4]!='')
- $combArray[$id_product_attribute]['attribute_color'] .= ',#'.$v[4];
- // $group[0] : group name
- // $group[1] : attribute name
- // $group[2] : attribute id
- // $group[3] : id_attribute_group
- // $group[4] : color
- }
- }
- }
- }
- }
-
- // ALREADY EXPORTING
- if(($AUTO_EXPORT || ($CRON && $CRONVERSION>=2)) && $ALREADY_EXPORTING && $exportConfig['exportcombinations'])
- {
- $id_product_attribute_first = 0;
- ksort($combArray);
- foreach($combArray AS $id_product_attribute => $product_attribute)
- {
- if(!empty($id_product_attribute) && $idp['id_product']==$sc_export["id_next"])
- {
- $id_product_attribute_first = $id_product_attribute;
- break;
- }
- }
-
- if(!empty($id_product_attribute_first))
- {
- if($id_product_attribute_first==$sc_export["id_combination_next"])
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET exporting = 0 WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
-
- if($AUTO_EXPORT)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('An error occured during last export with combination').' #'.$id_product_attribute_first.' '._l('from product').' #'.$idp['id_product'].'<br/>'._l('Check this combination before to try again.').'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- else
- {
- $STOP_SCRIPT=true;
- echo _l('An error occured during last export with combination').' #'.$id_product_attribute_first.' '._l('from product').' #'.$idp['id_product'].'<br/>'._l('Check this combination before to try again.');
- }
- }
- else
- {
- if($AUTO_EXPORT)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('This export is already in progress.',1).' - <a href="javascript: void(0);" class="reset_export" id="export_'.(int)$sc_export["id_sc_export"].'">'._l('Reset',1).'</a></span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- else
- {
- $STOP_SCRIPT=true;
- echo _l('This export is already in progress.');
- }
- }
- }
- else
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET exporting = 0 WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
-
- if($AUTO_EXPORT)
- {
- echo json_encode(array(
- "type" => "error",
- "stop" => 1,
- "content" => '<strong style="color: #831f1f;">'._l('An error occured during last export with product').' #'.$idp['id_product'].' <br/>'._l('Check this product before to try again.').'</span>',
- "filename" => $auto_filename,
- "first_interval" => $first_interval
- ));
- die();
- }
- else
- {
- $STOP_SCRIPT=true;
- echo _l('An error occured during last export with product').' #'.$idp['id_product'].' <br/>'._l('Check this product before to try again.');
- }
- }
- }
- if($STOP_SCRIPT)
- break;
-
- $extension_vars = array();
-
- // COMBINATIONS TREATMENT
- foreach($combArray AS $id_product_attribute => $product_attribute)
- {
- if(
- !(
- ($AUTO_EXPORT || ($CRON && $CRONVERSION>=2))
- && $exportConfig['exportcombinations']
- && $p->id==$sc_export["id_next"]
- && !empty($id_product_attribute)
- && $id_product_attribute<=$sc_export["id_combination_next"]
- )
- )
- {
- if(!empty($id_product_attribute))
- {
- $sql = "UPDATE "._DB_PREFIX_."sc_export SET id_combination_next = '".(int)$id_product_attribute."' WHERE name = '".pSQL($auto_filename)."'";
- Db::getInstance()->Execute($sql);
- }
-
- $linecount++;
- if (!$exportConfig['exportoutofstock'])
- {
- if (version_compare(_PS_VERSION_, '1.5.0.0', '>='))
- $qty=StockAvailable::getQuantityAvailableByProduct($p->id, ($id_product_attribute==0 ? NULL:$id_product_attribute), (version_compare(_PS_VERSION_, '1.5.0.0', '>=') && $selected_shops_id>0?(int)$selected_shops_id:null));
- else
- $qty=$p->getQuantity($p->id,($id_product_attribute==0 ? NULL:$id_product_attribute));
- if ($qty<=0)
- {
- if(!empty($sc_export["id_sc_export"]))
- {
- $sql = "
- INSERT INTO "._DB_PREFIX_."sc_export_product (id_sc_export, id_product, id_product_attribute,exported)
- VALUES ('".(int)$sc_export["id_sc_export"]."','".(int)$idp['id_product']."','".(int)$id_product_attribute."','0')";
- Db::getInstance()->Execute($sql);
- }
- continue;
- }
- }
- if(!empty($sc_export["id_sc_export"]))
- {
- $sql = "
- INSERT INTO "._DB_PREFIX_."sc_export_product (id_sc_export, id_product, id_product_attribute,exported)
- VALUES ('".(int)$sc_export["id_sc_export"]."','".(int)$idp['id_product']."','".(int)$id_product_attribute."','1')";
- Db::getInstance()->Execute($sql);
- }
-
- $linecontent='';
- $cacheProductImages=array();
- $num_img_link = 1;
- $num_img_legend = 1;
- $num_img_id = 1;
- $legends = array();
- sc_ext::readExportCSVConfigXML('exportProcessInitRowVars');
-
- // foreach field in mapping
- foreach($mapping AS $n => $f)
- {
- $field='';
- // extensions
- $switchObject=$f['name']