/controllers/admin/AdminImportController.php
PHP | 3415 lines | 2935 code | 298 blank | 182 comment | 730 complexity | e89c6481b6852a6dce33350b71f75a3c MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /*
- * 2007-2014 PrestaShop
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Open Software License (OSL 3.0)
- * that is bundled with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://opensource.org/licenses/osl-3.0.php
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@prestashop.com so we can send you a copy immediately.
- *
- * DISCLAIMER
- *
- * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
- * versions in the future. If you wish to customize PrestaShop for your
- * needs please refer to http://www.prestashop.com for more information.
- *
- * @author PrestaShop SA <contact@prestashop.com>
- * @copyright 2007-2014 PrestaShop SA
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- * International Registered Trademark & Property of PrestaShop SA
- */
- @ini_set('max_execution_time', 0);
- /** No max line limit since the lines can be more than 4096. Performance impact is not significant. */
- define('MAX_LINE_SIZE', 0);
- /** Used for validatefields diying without user friendly error or not */
- define('UNFRIENDLY_ERROR', false);
- /** this value set the number of columns visible on each page */
- define('MAX_COLUMNS', 6);
- /** correct Mac error on eof */
- @ini_set('auto_detect_line_endings', '1');
- class AdminImportControllerCore extends AdminController
- {
- public static $column_mask;
- public $entities = array();
- public $available_fields = array();
- public $required_fields = array();
- public $cache_image_deleted = array();
- public static $default_values = array();
- public static $validators = array(
- 'active' => array('AdminImportController', 'getBoolean'),
- 'tax_rate' => array('AdminImportController', 'getPrice'),
- /** Tax excluded */
- 'price_tex' => array('AdminImportController', 'getPrice'),
- /** Tax included */
- 'price_tin' => array('AdminImportController', 'getPrice'),
- 'reduction_price' => array('AdminImportController', 'getPrice'),
- 'reduction_percent' => array('AdminImportController', 'getPrice'),
- 'wholesale_price' => array('AdminImportController', 'getPrice'),
- 'ecotax' => array('AdminImportController', 'getPrice'),
- 'name' => array('AdminImportController', 'createMultiLangField'),
- 'description' => array('AdminImportController', 'createMultiLangField'),
- 'description_short' => array('AdminImportController', 'createMultiLangField'),
- 'meta_title' => array('AdminImportController', 'createMultiLangField'),
- 'meta_keywords' => array('AdminImportController', 'createMultiLangField'),
- 'meta_description' => array('AdminImportController', 'createMultiLangField'),
- 'link_rewrite' => array('AdminImportController', 'createMultiLangField'),
- 'available_now' => array('AdminImportController', 'createMultiLangField'),
- 'available_later' => array('AdminImportController', 'createMultiLangField'),
- 'category' => array('AdminImportController', 'split'),
- 'online_only' => array('AdminImportController', 'getBoolean')
- );
- public $separator;
- public $multiple_value_separator;
- public function __construct()
- {
- $this->bootstrap = true;
- $this->entities = array(
- $this->l('Categories'),
- $this->l('Products'),
- $this->l('Combinations'),
- $this->l('Customers'),
- $this->l('Addresses'),
- $this->l('Manufacturers'),
- $this->l('Suppliers'),
- $this->l('Alias'),
- );
- // @since 1.5.0
- if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
- {
- $this->entities = array_merge(
- $this->entities,
- array(
- $this->l('Supply Orders'),
- $this->l('Supply Order Details'),
- )
- );
- }
- $this->entities = array_flip($this->entities);
- switch ((int)Tools::getValue('entity'))
- {
- case $this->entities[$this->l('Combinations')]:
- $this->required_fields = array(
- 'id_product',
- 'group',
- 'attribute'
- );
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id_product' => array('label' => $this->l('Product ID').'*'),
- 'group' => array(
- 'label' => $this->l('Attribute (Name:Type:Position)').'*'
- ),
- 'attribute' => array(
- 'label' => $this->l('Value (Value:Position)').'*'
- ),
- 'supplier_reference' => array('label' => $this->l('Supplier reference')),
- 'reference' => array('label' => $this->l('Reference')),
- 'ean13' => array('label' => $this->l('EAN13')),
- 'upc' => array('label' => $this->l('UPC')),
- 'wholesale_price' => array('label' => $this->l('Wholesale price')),
- 'price' => array('label' => $this->l('Impact on price')),
- 'ecotax' => array('label' => $this->l('Ecotax')),
- 'quantity' => array('label' => $this->l('Quantity')),
- 'minimal_quantity' => array('label' => $this->l('Minimal quantity')),
- 'weight' => array('label' => $this->l('Impact on weight')),
- 'default_on' => array('label' => $this->l('Default (0 = No, 1 = Yes)')),
- 'image_position' => array(
- 'label' => $this->l('Image position')
- ),
- 'image_url' => array('label' => $this->l('Image URL')),
- 'delete_existing_images' => array(
- 'label' => $this->l('Delete existing images (0 = No, 1 = Yes).')
- ),
- 'shop' => array(
- 'label' => $this->l('ID / Name of shop'),
- 'help' => $this->l('Ignore this field if you don\'t use the Multistore tool. If you leave this field empty, the default shop will be used.'),
- ),
- 'advanced_stock_management' => array(
- 'label' => $this->l('Advanced Stock Management'),
- 'help' => $this->l('Enable Advanced Stock Management on product (0 = No, 1 = Yes)')
- ),
- 'depends_on_stock' => array(
- 'label' => $this->l('Depends on stock'),
- 'help' => $this->l('0 = Use quantity set in product, 1 = Use quantity from warehouse.')
- ),
- 'warehouse' => array(
- 'label' => $this->l('Warehouse'),
- 'help' => $this->l('ID of the warehouse to set as storage.')
- ),
- );
- self::$default_values = array(
- 'reference' => '',
- 'supplier_reference' => '',
- 'ean13' => '',
- 'upc' => '',
- 'wholesale_price' => 0,
- 'price' => 0,
- 'ecotax' => 0,
- 'quantity' => 0,
- 'minimal_quantity' => 1,
- 'weight' => 0,
- 'default_on' => 0,
- 'advanced_stock_management' => 0,
- 'depends_on_stock' => 0,
- );
- break;
- case $this->entities[$this->l('Categories')]:
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id' => array('label' => $this->l('ID')),
- 'active' => array('label' => $this->l('Active (0/1)')),
- 'name' => array('label' => $this->l('Name')),
- 'parent' => array('label' => $this->l('Parent category')),
- 'is_root_category' => array(
- 'label' => $this->l('Root category (0/1)'),
- 'help' => $this->l('A category root is where a category tree can begin. This is used with multistore.')
- ),
- 'description' => array('label' => $this->l('Description')),
- 'meta_title' => array('label' => $this->l('Meta title')),
- 'meta_keywords' => array('label' => $this->l('Meta keywords')),
- 'meta_description' => array('label' => $this->l('Meta description')),
- 'link_rewrite' => array('label' => $this->l('URL rewritten')),
- 'image' => array('label' => $this->l('Image URL')),
- 'shop' => array(
- 'label' => $this->l('ID / Name of shop'),
- 'help' => $this->l('Ignore this field if you don\'t use the Multistore tool. If you leave this field empty, the default shop will be used.'),
- ),
- );
- self::$default_values = array(
- 'active' => '1',
- 'parent' => Configuration::get('PS_HOME_CATEGORY'),
- 'link_rewrite' => ''
- );
- break;
- case $this->entities[$this->l('Products')]:
- self::$validators['image'] = array(
- 'AdminImportController',
- 'split'
- );
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id' => array('label' => $this->l('ID')),
- 'active' => array('label' => $this->l('Active (0/1)')),
- 'name' => array('label' => $this->l('Name')),
- 'category' => array('label' => $this->l('Categories (x,y,z...)')),
- 'price_tex' => array('label' => $this->l('Price tax excluded')),
- 'price_tin' => array('label' => $this->l('Price tax included')),
- 'id_tax_rules_group' => array('label' => $this->l('Tax rules ID')),
- 'wholesale_price' => array('label' => $this->l('Wholesale price')),
- 'on_sale' => array('label' => $this->l('On sale (0/1)')),
- 'reduction_price' => array('label' => $this->l('Discount amount')),
- 'reduction_percent' => array('label' => $this->l('Discount percent')),
- 'reduction_from' => array('label' => $this->l('Discount from (yyyy-mm-dd)')),
- 'reduction_to' => array('label' => $this->l('Discount to (yyyy-mm-dd)')),
- 'reference' => array('label' => $this->l('Reference #')),
- 'supplier_reference' => array('label' => $this->l('Supplier reference #')),
- 'supplier' => array('label' => $this->l('Supplier')),
- 'manufacturer' => array('label' => $this->l('Manufacturer')),
- 'ean13' => array('label' => $this->l('EAN13')),
- 'upc' => array('label' => $this->l('UPC')),
- 'ecotax' => array('label' => $this->l('Ecotax')),
- 'width' => array('label' => $this->l('Width')),
- 'height' => array('label' => $this->l('Height')),
- 'depth' => array('label' => $this->l('Depth')),
- 'weight' => array('label' => $this->l('Weight')),
- 'quantity' => array('label' => $this->l('Quantity')),
- 'minimal_quantity' => array('label' => $this->l('Minimal quantity')),
- 'visibility' => array('label' => $this->l('Visibility')),
- 'additional_shipping_cost' => array('label' => $this->l('Additional shipping cost')),
- 'unity' => array('label' => $this->l('Unit for the unit price')),
- 'unit_price' => array('label' => $this->l('Unit price')),
- 'description_short' => array('label' => $this->l('Short description')),
- 'description' => array('label' => $this->l('Description')),
- 'tags' => array('label' => $this->l('Tags (x,y,z...)')),
- 'meta_title' => array('label' => $this->l('Meta title')),
- 'meta_keywords' => array('label' => $this->l('Meta keywords')),
- 'meta_description' => array('label' => $this->l('Meta description')),
- 'link_rewrite' => array('label' => $this->l('URL rewritten')),
- 'available_now' => array('label' => $this->l('Text when in stock')),
- 'available_later' => array('label' => $this->l('Text when backorder allowed')),
- 'available_for_order' => array('label' => $this->l('Available for order (0 = No, 1 = Yes)')),
- 'available_date' => array('label' => $this->l('Product available date')),
- 'date_add' => array('label' => $this->l('Product creation date')),
- 'show_price' => array('label' => $this->l('Show price (0 = No, 1 = Yes)')),
- 'image' => array('label' => $this->l('Image URLs (x,y,z...)')),
- 'delete_existing_images' => array(
- 'label' => $this->l('Delete existing images (0 = No, 1 = Yes)')
- ),
- 'features' => array('label' => $this->l('Feature (Name:Value:Position:Customized)')),
- 'online_only' => array('label' => $this->l('Available online only (0 = No, 1 = Yes)')),
- 'condition' => array('label' => $this->l('Condition')),
- 'customizable' => array('label' => $this->l('Customizable (0 = No, 1 = Yes)')),
- 'uploadable_files' => array('label' => $this->l('Uploadable files (0 = No, 1 = Yes)')),
- 'text_fields' => array('label' => $this->l('Text fields (0 = No, 1 = Yes)')),
- 'out_of_stock' => array('label' => $this->l('Action when out of stock')),
- 'shop' => array(
- 'label' => $this->l('ID / Name of shop'),
- 'help' => $this->l('Ignore this field if you don\'t use the Multistore tool. If you leave this field empty, the default shop will be used.'),
- ),
- 'advanced_stock_management' => array(
- 'label' => $this->l('Advanced Stock Management'),
- 'help' => $this->l('Enable Advanced Stock Management on product (0 = No, 1 = Yes).')
- ),
- 'depends_on_stock' => array(
- 'label' => $this->l('Depends on stock'),
- 'help' => $this->l('0 = Use quantity set in product, 1 = Use quantity from warehouse.')
- ),
- 'warehouse' => array(
- 'label' => $this->l('Warehouse'),
- 'help' => $this->l('ID of the warehouse to set as storage.')
- ),
- );
- self::$default_values = array(
- 'id_category' => array((int)Configuration::get('PS_HOME_CATEGORY')),
- 'id_category_default' => (int)Configuration::get('PS_HOME_CATEGORY'),
- 'active' => '1',
- 'width' => 0.000000,
- 'height' => 0.000000,
- 'depth' => 0.000000,
- 'weight' => 0.000000,
- 'visibility' => 'both',
- 'additional_shipping_cost' => 0.00,
- 'unit_price' => 0,
- 'quantity' => 0,
- 'minimal_quantity' => 1,
- 'price' => 0,
- 'id_tax_rules_group' => 0,
- 'description_short' => array((int)Configuration::get('PS_LANG_DEFAULT') => ''),
- 'link_rewrite' => array((int)Configuration::get('PS_LANG_DEFAULT') => ''),
- 'online_only' => 0,
- 'condition' => 'new',
- 'available_date' => date('Y-m-d'),
- 'date_add' => date('Y-m-d H:i:s'),
- 'customizable' => 0,
- 'uploadable_files' => 0,
- 'text_fields' => 0,
- 'out_of_stock' => '2',
- 'advanced_stock_management' => 0,
- 'depends_on_stock' => 0,
- );
- break;
- case $this->entities[$this->l('Customers')]:
- //Overwrite required_fields AS only email is required whereas other entities
- $this->required_fields = array('email', 'passwd', 'lastname', 'firstname');
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id' => array('label' => $this->l('ID')),
- 'active' => array('label' => $this->l('Active (0/1)')),
- 'id_gender' => array('label' => $this->l('Titles ID (Mr = 1, Ms = 2, else 0)')),
- 'email' => array('label' => $this->l('Email *')),
- 'passwd' => array('label' => $this->l('Password *')),
- 'birthday' => array('label' => $this->l('Birthday (yyyy-mm-dd)')),
- 'lastname' => array('label' => $this->l('Last Name *')),
- 'firstname' => array('label' => $this->l('First Name *')),
- 'newsletter' => array('label' => $this->l('Newsletter (0/1)')),
- 'optin' => array('label' => $this->l('Opt-in (0/1)')),
- 'group' => array('label' => $this->l('Groups (x,y,z...)')),
- 'id_default_group' => array('label' => $this->l('Default group ID')),
- 'id_shop' => array(
- 'label' => $this->l('ID / Name of shop'),
- 'help' => $this->l('Ignore this field if you don\'t use the Multistore tool. If you leave this field empty, the default shop will be used.'),
- ),
- );
- self::$default_values = array(
- 'active' => '1',
- 'id_shop' => Configuration::get('PS_SHOP_DEFAULT'),
- );
- break;
- case $this->entities[$this->l('Addresses')]:
- //Overwrite required_fields
- $this->required_fields = array(
- 'alias',
- 'lastname',
- 'firstname',
- 'address1',
- 'postcode',
- 'country',
- 'customer_email',
- 'city'
- );
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id' => array('label' => $this->l('ID')),
- 'alias' => array('label' => $this->l('Alias *')),
- 'active' => array('label' => $this->l('Active (0/1)')),
- 'customer_email' => array('label' => $this->l('Customer email *')),
- 'id_customer' => array('label' => $this->l('Customer ID')),
- 'manufacturer' => array('label' => $this->l('Manufacturer')),
- 'supplier' => array('label' => $this->l('Supplier')),
- 'company' => array('label' => $this->l('Company')),
- 'lastname' => array('label' => $this->l('Last Name *')),
- 'firstname' => array('label' => $this->l('First Name *')),
- 'address1' => array('label' => $this->l('Address 1 *')),
- 'address2' => array('label' => $this->l('Address 2')),
- 'postcode' => array('label' => $this->l('Zip/postal code *')),
- 'city' => array('label' => $this->l('City *')),
- 'country' => array('label' => $this->l('Country *')),
- 'state' => array('label' => $this->l('State')),
- 'other' => array('label' => $this->l('Other')),
- 'phone' => array('label' => $this->l('Phone')),
- 'phone_mobile' => array('label' => $this->l('Mobile Phone')),
- 'vat_number' => array('label' => $this->l('VAT number')),
- );
- self::$default_values = array(
- 'alias' => 'Alias',
- 'postcode' => 'X'
- );
- break;
- case $this->entities[$this->l('Manufacturers')]:
- case $this->entities[$this->l('Suppliers')]:
- //Overwrite validators AS name is not MultiLangField
- self::$validators = array(
- 'description' => array('AdminImportController', 'createMultiLangField'),
- 'short_description' => array('AdminImportController', 'createMultiLangField'),
- 'meta_title' => array('AdminImportController', 'createMultiLangField'),
- 'meta_keywords' => array('AdminImportController', 'createMultiLangField'),
- 'meta_description' => array('AdminImportController', 'createMultiLangField'),
- );
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id' => array('label' => $this->l('ID')),
- 'active' => array('label' => $this->l('Active (0/1)')),
- 'name' => array('label' => $this->l('Name')),
- 'description' => array('label' => $this->l('Description')),
- 'short_description' => array('label' => $this->l('Short description')),
- 'meta_title' => array('label' => $this->l('Meta title')),
- 'meta_keywords' => array('label' => $this->l('Meta keywords')),
- 'meta_description' => array('label' => $this->l('Meta description')),
- 'image' => array('label' => $this->l('Image URL')),
- 'shop' => array(
- 'label' => $this->l('ID / Name of group shop'),
- 'help' => $this->l('Ignore this field if you don\'t use the Multistore tool. If you leave this field empty, the default shop will be used.'),
- ),
- );
- self::$default_values = array(
- 'shop' => Shop::getGroupFromShop(Configuration::get('PS_SHOP_DEFAULT')),
- );
- break;
- case $this->entities[$this->l('Alias')]:
- //Overwrite required_fields
- $this->required_fields = array(
- 'alias',
- 'search',
- );
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id' => array('label' => $this->l('ID')),
- 'alias' => array('label' => $this->l('Alias *')),
- 'search' => array('label' => $this->l('Search *')),
- 'active' => array('label' => $this->l('Active')),
- );
- self::$default_values = array(
- 'active' => '1',
- );
- break;
- }
-
- // @since 1.5.0
- if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
- switch ((int)Tools::getValue('entity'))
- {
- case $this->entities[$this->l('Supply Orders')]:
- // required fields
- $this->required_fields = array(
- 'id_supplier',
- 'id_warehouse',
- 'reference',
- 'date_delivery_expected',
- );
- // available fields
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'id' => array('label' => $this->l('ID')),
- 'id_supplier' => array('label' => $this->l('Supplier ID *')),
- 'id_lang' => array('label' => $this->l('Lang ID')),
- 'id_warehouse' => array('label' => $this->l('Warehouse ID *')),
- 'id_currency' => array('label' => $this->l('Currency ID *')),
- 'reference' => array('label' => $this->l('Supply Order Reference *')),
- 'date_delivery_expected' => array('label' => $this->l('Delivery Date (Y-M-D)*')),
- 'discount_rate' => array('label' => $this->l('Discount Rate')),
- 'is_template' => array('label' => $this->l('Template')),
- );
- // default values
- self::$default_values = array(
- 'id_lang' => (int)Configuration::get('PS_LANG_DEFAULT'),
- 'id_currency' => Currency::getDefaultCurrency()->id,
- 'discount_rate' => '0',
- 'is_template' => '0',
- );
- break;
- case $this->entities[$this->l('Supply Order Details')]:
- // required fields
- $this->required_fields = array(
- 'supply_order_reference',
- 'id_product',
- 'unit_price_te',
- 'quantity_expected',
- );
- // available fields
- $this->available_fields = array(
- 'no' => array('label' => $this->l('Ignore this column')),
- 'supply_order_reference' => array('label' => $this->l('Supply Order Reference *')),
- 'id_product' => array('label' => $this->l('Product ID *')),
- 'id_product_attribute' => array('label' => $this->l('Product Attribute ID')),
- 'unit_price_te' => array('label' => $this->l('Unit Price (tax excl.)*')),
- 'quantity_expected' => array('label' => $this->l('Quantity Expected *')),
- 'discount_rate' => array('label' => $this->l('Discount Rate')),
- 'tax_rate' => array('label' => $this->l('Tax Rate')),
- );
- // default values
- self::$default_values = array(
- 'discount_rate' => '0',
- 'tax_rate' => '0',
- );
- break;
-
- }
- $this->separator = ($separator = Tools::substr(strval(trim(Tools::getValue('separator'))), 0, 1)) ? $separator : ';';
- $this->multiple_value_separator = ($separator = Tools::substr(strval(trim(Tools::getValue('multiple_value_separator'))), 0, 1)) ? $separator : ',';
- parent::__construct();
- }
- public function setMedia()
- {
- $bo_theme = ((Validate::isLoadedObject($this->context->employee)
- && $this->context->employee->bo_theme) ? $this->context->employee->bo_theme : 'default');
- if (!file_exists(_PS_BO_ALL_THEMES_DIR_.$bo_theme.DIRECTORY_SEPARATOR
- .'template'))
- $bo_theme = 'default';
- // We need to set parent media first, so that jQuery is loaded before the dependant plugins
- parent::setMedia();
- $this->addJs(__PS_BASE_URI__.$this->admin_webpath.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js');
- $this->addJs(__PS_BASE_URI__.$this->admin_webpath.'/themes/'.$bo_theme.'/js/jquery.fileupload.js');
- $this->addJs(__PS_BASE_URI__.$this->admin_webpath.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js');
- $this->addJs(__PS_BASE_URI__.$this->admin_webpath.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js');
- $this->addJs(__PS_BASE_URI__.'js/vendor/spin.js');
- $this->addJs(__PS_BASE_URI__.'js/vendor/ladda.js');
- }
- public function renderForm()
- {
- if (!is_dir(AdminImportController::getPath()))
- return !($this->errors[] = Tools::displayError('The import directory does not exist.'));
- if (!is_writable(AdminImportController::getPath()))
- $this->displayWarning($this->l('The import directory must be writable (CHMOD 755 / 777).'));
- if (isset($this->warnings) && count($this->warnings))
- {
- $warnings = array();
- foreach ($this->warnings as $warning)
- $warnings[] = $warning;
- }
- $files_to_import = scandir(AdminImportController::getPath());
- uasort($files_to_import, array('AdminImportController', 'usortFiles'));
- foreach ($files_to_import as $k => &$filename)
- //exclude . .. .svn and index.php and all hidden files
- if (preg_match('/^\..*|index\.php/i', $filename))
- unset($files_to_import[$k]);
- unset($filename);
- $this->fields_form = array('');
- $this->toolbar_scroll = false;
- $this->toolbar_btn = array();
- // adds fancybox
- $this->addJqueryPlugin(array('fancybox'));
- $entity_selected = 0;
- if (isset($this->entities[$this->l(Tools::ucfirst(Tools::getValue('import_type')))]))
- {
- $entity_selected = $this->entities[$this->l(Tools::ucfirst(Tools::getValue('import_type')))];
- $this->context->cookie->entity_selected = (int)$entity_selected;
- }
- elseif (isset($this->context->cookie->entity_selected))
- $entity_selected = (int)$this->context->cookie->entity_selected;
- $csv_selected = '';
- if (isset($this->context->cookie->csv_selected) && @filemtime(AdminImportController::getPath(
- urldecode($this->context->cookie->csv_selected))))
- $csv_selected = urldecode($this->context->cookie->csv_selected);
- else
- $this->context->cookie->csv_selected = $csv_selected;
- $id_lang_selected = '';
- if (isset($this->context->cookie->iso_lang_selected) && $this->context->cookie->iso_lang_selected)
- $id_lang_selected = (int)Language::getIdByIso(urldecode($this->context->cookie->iso_lang_selected));
- $separator_selected = $this->separator;
- if (isset($this->context->cookie->separator_selected) && $this->context->cookie->separator_selected)
- $separator_selected = urldecode($this->context->cookie->separator_selected);
- $multiple_value_separator_selected = $this->multiple_value_separator;
- if (isset($this->context->cookie->multiple_value_separator_selected) && $this->context->cookie->multiple_value_separator_selected)
- $multiple_value_separator_selected = urldecode($this->context->cookie->multiple_value_separator_selected);
- //get post max size
- $post_max_size = ini_get('post_max_size');
- $bytes = trim($post_max_size);
- $last = strtolower($post_max_size[strlen($post_max_size) - 1]);
- switch ($last)
- {
- case 'g': $bytes *= 1024;
- case 'm': $bytes *= 1024;
- case 'k': $bytes *= 1024;
- }
- if (!isset($bytes) || $bytes == '')
- $bytes = 20971520; // 20Mb
- $this->tpl_form_vars = array(
- 'post_max_size' => (int)$bytes,
- 'module_confirmation' => Tools::isSubmit('import') && (isset($this->warnings) && !count($this->warnings)),
- 'path_import' => AdminImportController::getPath(),
- 'entities' => $this->entities,
- 'entity_selected' => $entity_selected,
- 'csv_selected' => $csv_selected,
- 'separator_selected' => $separator_selected,
- 'multiple_value_separator_selected' => $multiple_value_separator_selected,
- 'files_to_import' => $files_to_import,
- 'languages' => Language::getLanguages(false),
- 'id_language' => ($id_lang_selected) ? $id_lang_selected : $this->context->language->id,
- 'available_fields' => $this->getAvailableFields(),
- 'truncateAuthorized' => (Shop::isFeatureActive() && $this->context->employee->isSuperAdmin()) || !Shop::isFeatureActive(),
- 'PS_ADVANCED_STOCK_MANAGEMENT' => Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'),
- );
- return parent::renderForm();
- }
- public function ajaxProcessuploadCsv()
- {
- $filename_prefix = date('YmdHis').'-';
- if (isset($_FILES['file']) && !empty($_FILES['file']['error']))
- {
- switch ($_FILES['file']['error'])
- {
- case UPLOAD_ERR_INI_SIZE:
- $_FILES['file']['error'] = Tools::displayError('The uploaded file exceeds the upload_max_filesize directive in php.ini. If your server configuration allows it, you may add a directive in your .htaccess.');
- break;
- case UPLOAD_ERR_FORM_SIZE:
- $_FILES['file']['error'] = Tools::displayError('The uploaded file exceeds the post_max_size directive in php.ini.
- If your server configuration allows it, you may add a directive in your .htaccess, for example:')
- .'<br/><a href="'.$this->context->link->getAdminLink('AdminMeta').'" >
- <code>php_value post_max_size 20M</code> '.
- Tools::displayError('(click to open "Generators" page)').'</a>';
- break;
- break;
- case UPLOAD_ERR_PARTIAL:
- $_FILES['file']['error'] = Tools::displayError('The uploaded file was only partially uploaded.');
- break;
- break;
- case UPLOAD_ERR_NO_FILE:
- $_FILES['file']['error'] = Tools::displayError('No file was uploaded.');
- break;
- break;
- }
- }
- elseif (!preg_match('/.*\.csv$/i', $_FILES['file']['name']))
- $_FILES['file']['error'] = Tools::displayError('The extension of your file should be .csv.');
- elseif (!@filemtime($_FILES['file']['tmp_name']) ||
- !@move_uploaded_file($_FILES['file']['tmp_name'], AdminImportController::getPath().$filename_prefix.str_replace("\0", '', $_FILES['file']['name'])))
- $_FILES['file']['error'] = $this->l('An error occurred while uploading / copying the file.');
- else
- {
- @chmod(AdminImportController::getPath().$filename_prefix.$_FILES['file']['name'], 0664);
- $_FILES['file']['filename'] = $filename_prefix.str_replace('\0', '', $_FILES['file']['name']);
- }
- die(Tools::jsonEncode($_FILES));
- }
- public function renderView()
- {
- $this->addJS(_PS_JS_DIR_.'adminImport.js');
- $handle = $this->openCsvFile();
- $nb_column = $this->getNbrColumn($handle, $this->separator);
- $nb_table = ceil($nb_column / MAX_COLUMNS);
- $res = array();
- foreach ($this->required_fields as $elem)
- $res[] = '\''.$elem.'\'';
- $data = array();
- for ($i = 0; $i < $nb_table; $i++)
- $data[$i] = $this->generateContentTable($i, $nb_column, $handle, $this->separator);
- $this->context->cookie->entity_selected = (int)Tools::getValue('entity');
- $this->context->cookie->iso_lang_selected = urlencode(Tools::getValue('iso_lang'));
- $this->context->cookie->separator_selected = urlencode($this->separator);
- $this->context->cookie->multiple_value_separator_selected = urlencode($this->multiple_value_separator);
- $this->context->cookie->csv_selected = urlencode(Tools::getValue('csv'));
- $this->tpl_view_vars = array(
- 'import_matchs' => Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'import_match'),
- 'fields_value' => array(
- 'csv' => Tools::getValue('csv'),
- 'convert' => Tools::getValue('convert'),
- 'entity' => (int)Tools::getValue('entity'),
- 'iso_lang' => Tools::getValue('iso_lang'),
- 'truncate' => Tools::getValue('truncate'),
- 'forceIDs' => Tools::getValue('forceIDs'),
- 'regenerate' => Tools::getValue('regenerate'),
- 'match_ref' => Tools::getValue('match_ref'),
- 'separator' => $this->separator,
- 'multiple_value_separator' => $this->multiple_value_separator
- ),
- 'nb_table' => $nb_table,
- 'nb_column' => $nb_column,
- 'res' => implode(',', $res),
- 'max_columns' => MAX_COLUMNS,
- 'no_pre_select' => array('price_tin', 'feature'),
- 'available_fields' => $this->available_fields,
- 'data' => $data
- );
- return parent::renderView();
- }
- public function initToolbar()
- {
- switch ($this->display)
- {
- case 'import':
- // Default cancel button - like old back link
- $back = Tools::safeOutput(Tools::getValue('back', ''));
- if (empty($back))
- $back = self::$currentIndex.'&token='.$this->token;
- $this->toolbar_btn['cancel'] = array(
- 'href' => $back,
- 'desc' => $this->l('Cancel')
- );
- // Default save button - action dynamically handled in javascript
- $this->toolbar_btn['save-import'] = array(
- 'href' => '#',
- 'desc' => $this->l('Import .CSV data')
- );
- break;
- }
- }
- protected function generateContentTable($current_table, $nb_column, $handle, $glue)
- {
- $html = '<table id="table'.$current_table.'" style="display: none;" class="table table-bordered"><thead><tr>';
- // Header
- for ($i = 0; $i < $nb_column; $i++)
- if (MAX_COLUMNS * (int)$current_table <= $i && (int)$i < MAX_COLUMNS * ((int)$current_table + 1))
- $html .= '<th>
- <select id="type_value['.$i.']"
- name="type_value['.$i.']"
- class="type_value">
- '.$this->getTypeValuesOptions($i).'
- </select>
- </th>';
- $html .= '</tr></thead><tbody>';
- AdminImportController::setLocale();
- for ($current_line = 0; $current_line < 10 && $line = fgetcsv($handle, MAX_LINE_SIZE, $glue); $current_line++)
- {
- /* UTF-8 conversion */
- if (Tools::getValue('convert'))
- $line = $this->utf8EncodeArray($line);
- $html .= '<tr id="table_'.$current_table.'_line_'.$current_line.'">';
- foreach ($line as $nb_c => $column)
- if ((MAX_COLUMNS * (int)$current_table <= $nb_c) && ((int)$nb_c < MAX_COLUMNS * ((int)$current_table + 1)))
- $html .= '<td>'.htmlentities(Tools::substr($column, 0, 200), ENT_QUOTES, 'UTF-8').'</td>';
- $html .= '</tr>';
- }
- $html .= '</tbody></table>';
- AdminImportController::rewindBomAware($handle);
- return $html;
- }
- public function init()
- {
- parent::init();
- if (Tools::isSubmit('submitImportFile'))
- $this->display = 'import';
- }
- public function initContent()
- {
- $this->initTabModuleList();
- // toolbar (save, cancel, new, ..)
- $this->initToolbar();
- $this->initPageHeaderToolbar();
- if ($this->display == 'import')
- {
- if (Tools::getValue('csv'))
- $this->content .= $this->renderView();
- else
- {
- $this->errors[] = $this->l('You must upload a file in order to proceed to the next step');
- $this->content .= $this->renderForm();
- }
- }
- else
- $this->content .= $this->renderForm();
- $this->context->smarty->assign(array(
- 'content' => $this->content,
- 'url_post' => self::$currentIndex.'&token='.$this->token,
- 'show_page_header_toolbar' => $this->show_page_header_toolbar,
- 'page_header_toolbar_title' => $this->page_header_toolbar_title,
- 'page_header_toolbar_btn' => $this->page_header_toolbar_btn
- ));
- }
- protected static function rewindBomAware($handle)
- {
- // A rewind wrapper that skips BOM signature wrongly
- if (!is_resource($handle))
- return false;
- rewind($handle);
- if (($bom = fread($handle, 3)) != "\xEF\xBB\xBF")
- rewind($handle);
- }
- protected static function getBoolean($field)
- {
- return (boolean)$field;
- }
- protected static function getPrice($field)
- {
- $field = ((float)str_replace(',', '.', $field));
- $field = ((float)str_replace('%', '', $field));
- return $field;
- }
- protected static function split($field)
- {
- if (empty($field))
- return array();
- $separator = Tools::getValue('multiple_value_separator');
- if (is_null($separator) || trim($separator) == '')
- $separator = ',';
- do $uniqid_path = _PS_UPLOAD_DIR_.uniqid(); while (file_exists($uniqid_path));
- file_put_contents($uniqid_path, $field);
- $tab = '';
- if (!empty($uniqid_path))
- {
- $fd = fopen($uniqid_path, 'r');
- $tab = fgetcsv($fd, MAX_LINE_SIZE, $separator);
- fclose($fd);
- if (file_exists($uniqid_path))
- @unlink($uniqid_path);
- }
- if (empty($tab) || (!is_array($tab)))
- return array();
- return $tab;
- }
- protected static function createMultiLangField($field)
- {
- $languages = Language::getLanguages(false);
- $res = array();
- foreach ($languages as $lang)
- $res[$lang['id_lang']] = $field;
- return $res;
- }
- protected function getTypeValuesOptions($nb_c)
- {
- $i = 0;
- $no_pre_select = array('price_tin', 'feature');
- $options = '';
- foreach ($this->available_fields as $k => $field)
- {
- $options .= '<option value="'.$k.'"';
- if ($k === 'price_tin')
- ++$nb_c;
- if ($i === ($nb_c + 1) && (!in_array($k, $no_pre_select)))
- $options .= ' selected="selected"';
- $options .= '>'.$field['label'].'</option>';
- ++$i;
- }
- return $options;
- }
- /*
- * Return fields to be display AS piece of advise
- *
- * @param $in_array boolean
- * @return string or return array
- */
- public function getAvailableFields($in_array = false)
- {
- $i = 0;
- $fields = array();
- $keys = array_keys($this->available_fields);
- array_shift($keys);
- foreach ($this->available_fields as $k => $field)
- {
- if ($k === 'no')
- continue;
- if ($k === 'price_tin')
- $fields[$i - 1] = '<div>'.$this->available_fields[$keys[$i - 1]]['label'].' '.$this->l('or').' '.$field['label'].'</div>';
- else
- {
- if (isset($field['help']))
- $html = ' <a href="#" class="help-tooltip" data-toggle="tooltip" title="'.$field['help'].'"><i class="icon-info-sign"></i></a>';
- else
- $html = '';
- $fields[] = '<div>'.$field['label'].$html.'</div>';
- }
- ++$i;
- }
- if ($in_array)
- return $fields;
- else
- return implode("\n\r", $fields);
- }
- protected function receiveTab()
- {
- $type_value = Tools::getValue('type_value') ? Tools::getValue('type_value') : array();
- foreach ($type_value as $nb => $type)
- if ($type != 'no')
- self::$column_mask[$type] = $nb;
- }
- public static function getMaskedRow($row)
- {
- $res = array();
- if (is_array(self::$column_mask))
- foreach (self::$column_mask as $type => $nb)
- $res[$type] = isset($row[$nb]) ? $row[$nb] : null;
- return $res;
- }
- protected static function setDefaultValues(&$info)
- {
- foreach (self::$default_values as $k => $v)
- if (!isset($info[$k]) || $info[$k] == '')
- $info[$k] = $v;
- }
- protected static function setEntityDefaultValues(&$entity)
- {
- $members = get_object_vars($entity);
- foreach (self::$default_values as $k => $v)
- if ((array_key_exists($k, $members) && $entity->$k === null) || !array_key_exists($k, $members))
- $entity->$k = $v;
- }
- protected static function fillInfo($infos, $key, &$entity)
- {
- $infos = trim($infos);
- if (isset(self::$validators[$key][1]) && self::$validators[$key][1] == 'createMultiLangField' && Tools::getValue('iso_lang'))
- {
- $id_lang = Language::getIdByIso(Tools::getValue('iso_lang'));
- $tmp = call_user_func(self::$validators[$key], $infos);
- foreach ($tmp as $id_lang_tmp => $value)
- if (empty($entity->{$key}[$id_lang_tmp]) || $id_lang_tmp == $id_lang)
- $entity->{$key}[$id_lang_tmp] = $value;
- }
- else
- if (!empty($infos) || $infos == '0') // ($infos == '0') => if you want to disable a product by using "0" in active because empty('0') return true
- $entity->{$key} = isset(self::$validators[$key]) ? call_user_func(self::$validators[$key], $infos) : $infos;
- return true;
- }
- /**
- * @param $array
- * @param $funcname
- * @param mixed $user_data
- * @return bool
- */
- public static function arrayWalk(&$array, $funcname, &$user_data = false)
- {
- if (!is_callable($funcname)) return false;
- foreach ($array as $k => $row)
- if (!call_user_func_array($funcname, array($row, $k, $user_data)))
- return false;
- return true;
- }
- /**
- * copyImg copy an image located in $url and save it in a path
- * according to $entity->$id_entity .
- * $id_image is used if we need to add a watermark
- *
- * @param int $id_entity id of product or category (set in entity)
- * @param int $id_image (default null) id of the image if watermark enabled.
- * @param string $url path or url to use
- * @param string entity 'products' or 'categories'
- * @return boolean
- */
- protected static function copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = true)
- {
- $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
- $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
- switch ($entity)
- {
- default:
- case 'products':
- $image_obj = new Image($id_image);
- $path = $image_obj->getPathForCreation();
- break;
- case 'categories':
- $path = _PS_CAT_IMG_DIR_.(int)$id_entity;
- break;
- case 'manufacturers':
- $path = _PS_MANU_IMG_DIR_.(int)$id_entity;
- break;
- case 'suppliers':
- $path = _PS_SUPP_IMG_DIR_.(int)$id_entity;
- break;
- }
- $url = str_replace(' ', '%20', trim($url));
- // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
- if (!ImageManager::checkImageMemoryLimit($url))
- return false;
- // 'file_exists' doesn't work on distant file, and getimagesize makes the import slower.
- // Just hide the warning, the processing will be the same.
- if (Tools::copy($url, $tmpfile))
- {
- ImageManager::resize($tmpfile, $path.'.jpg');
- $images_types = ImageType::getImagesTypes($entity);
- if ($regenerate)
- foreach ($images_types as $image_type)
- {
- ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'], $image_type['height']);
- if (in_array($image_type['id_image_type'], $watermark_types))
- Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
- }
- }
- else
- {
- unlink($tmpfile);
- return false;
- }
- unlink($tmpfile);
- return true;
- }
- public function categoryImport()
- {
- $cat_moved = array();
- $this->receiveTab();
- $handle = $this->openCsvFile();
- $default_language_id = (int)Configuration::get('PS_LANG_DEFAULT');
- $id_lang = Language::getIdByIso(Tools::getValue('iso_lang'));
- if (!Validate::isUnsignedId($id_lang))
- $id_lang = $default_language_id;
- AdminImportController::setLocale();
- for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); $current_line++)
- {
- if (Tools::getValue('convert'))
- $line = $this->utf8EncodeArray($line);
- $info = AdminImportController::getMaskedRow($line);
- $tab_categ = array(Configuration::get('PS_HOME_CATEGORY'), Configuration::get('PS_ROOT_CATEGORY'));
- if (isset($info['id']) && in_array((int)$info['id'], $tab_categ))
- {
- $this->errors[] = Tools::displayError('The category ID cannot be the same as the Root category ID or the Home category ID.');
- continue;
- }
- AdminImportController::setDefaultValues($info);
- if (Tools::getValue('forceIDs') && isset($info['id']) && (int)$info['id'])
- $category = new Category((int)$info['id']);
- else
- {
- if (isset($info['id']) && (int)$info['id'] && Category::existsInDatabase((int)$info['id'], 'category'))
- $category = new Category((int)$info['id']);
- else
- $category = new Category();
- }
- AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $category);
- if (isset($category->parent) && is_numeric($category->parent))
- {
- if (isset($cat_moved[$category->parent]))
- $category->parent = $cat_moved[$category->parent];
- $category->id_parent = $category->parent;
- }
- elseif (isset($category->parent) && is_string($category->parent))
- {
- $category_parent = Category::searchByName($id_lang, $category->parent, true);
- if ($category_parent['id_category'])
- {
- $category->id_parent = (int)$category_parent['id_category'];
- $category->level_depth = (int)$category_parent['level_depth'] + 1;
- }
- else
- {
- $category_to_create = new Category();
- $category_to_create->name = AdminImportController::createMultiLangField($category->parent);
- $category_to_create->active = 1;
- $category_link_rewrite = Tools::link_rewrite($category_to_create->name[$id_lang]);
- $category_to_create->link_rewrite = AdminImportController::createMultiLangField($category_link_rewrite);
- $category_to_create->id_parent = Configuration::get('PS_HOME_CATEGORY'); // Default parent is home for unknown category to create
- if (($field_error = $category_to_create->validateFields(UNFRIENDLY_ERROR, true)) === true &&
- ($lang_field_error = $category_to_create->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $category_to_create->add())
- $category->id_parent = $category_to_create->id;
- else
- {
- $this->errors[] = sprintf(
- Tools::displayError('%1$s (ID: %2$s) cannot be saved'),
- $category_to_create->name[$id_lang],
- (isset($category_to_create->id) && !empty($category_to_create->id))? $category_to_create->id : 'null'
- );
- $this->errors[] = ($field_error !== true ? $field_error : '').(isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '').
- Db::getInstance()->getMsgError();
- }
- }
- }
- if (isset($category->link_rewrite) && !empty($category->link_rewrite[$default_language_id]))
- $valid_link = Validate::isLinkRewrite($category->link_rewrite[$default_language_id]);
- else
- $valid_link = false;
- if (!Shop::isFeatureActive())
- $category->id_shop_default = 1;
- else
- $category->id_shop_default = (int)Context::getContext()->shop->id;
- $bak = $category->link_rewrite[$default_language_id];
- if ((isset($category->link_rewrite) && empty($category->link_rewrite[$default_language_id])) || !$valid_link)
- {
- $category->link_rewrite = Tools::link_rewrite($category->name[$default_language_id]);
- if ($category->link_rewrite == '')
- {
- $category->link_rewrite = 'friendly-url-autogeneration-failed';
- $this->warnings[] = sprintf(Tools::displayError('URL rewriting failed to auto-generate a friendly URL for: %s'), $category->name[$default_language_id]);
- }
- $category->link_rewrite = AdminImportController::createMultiLangField($category->link_rewrite);
- }
- if (!$valid_link)
- $this->warnings[] = sprintf(
- Tools::displayError('Rewrite link for %1$s (ID: %2$s) was re-written as %3$s.'),
- $bak,
- (isset($info['id']) && !empty($info['id']))? $info['id'] : 'null',
- $category->link_rewrite[$default_language_id]
- );
- $res = false;
- if (($field_error = $category->validateFields(UNFRIENDLY_ERROR, true)) === true &&
- ($lang_field_error = $category->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && empty($this->errors))
- {
- $category_already_created = Category::searchByNameAndParentCategoryId(
- $id_lang,
- $category->name[$id_lang],
- $category->id_parent
- );
- // If category already in base, get id category back
- if ($category_already_created['id_category'])
- {
- $cat_moved[$category->id] = (int)$category_already_created['id_category'];
- $category->id = (int)$category_already_created['id_category'];
- }
-
- if ($category->id && $category->id == $category->id_parent)
- {
- $this->errors[] = Tools::displayError('A category cannot be its own parent');
- continue;
- }
- /* No automatic nTree regeneration for import */
- $category->doNotRegenerateNTree = true;
- // If id category AND id category already in base, trying to update
- $categories_home_root = array(Configuration::get('PS_ROOT_CATEGORY'), Configuration::get('PS_HOME_CATEGORY'));
- if ($category->id && $category->categoryExists($category->id) && !in_array($category->id, $categories_home_root))
- $res = $category->update();
- if ($category->id == Configuration::get('PS_ROOT_CATEGORY'))
- $this->errors[] = Tools::displayError('The root category cannot be modified.');
- // If no id_category or update failed
- $category->force_id = (bool)Tools::getValue('forceIDs');
- if (!$res)
- $res = $category->add();
- }
- //copying images of categories
- if (isset($category->image) && !empty($category->image))
- if (!(AdminImportController::copyImg($category->id, null, $category->image, 'categories', !Tools::getValue('regenerate'))))
- $this->warnings[] = $category->image.' '.Tools::displayError('cannot be copied.');
- // If both failed, mysql error
- if (!$res)
- {
- $this->errors[] = sprintf(
- Tools::displayError('%1$s (ID: %2$s) cannot be saved'),
- (isset($info['name']) && !empty($info['name']))? Tools::safeOutput($info['name']) : 'No Name',
- (isset($info['id']) && !empty($info['id']))? Tools::safeOutput($info['id']) : 'No ID'
- );
- $error_tmp = ($field_error !== true ? $field_error : '').(isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '').Db::getInstance()->getMsgError();
- if ($error_tmp != '')
- $this->errors[] = $error_tmp;
- }
- else
- {
- // Associate category to shop
- if (Shop::isFeatureActive())
- {
- Db::getInstance()->execute('
- DELETE FROM '._DB_PREFIX_.'category_shop
- WHERE id_category = '.(int)$category->id
- );
- if (!Shop::isFeatureActive())
- $info['shop'] = 1;
- elseif (!isset($info['shop']) || empty($info['shop']))
- $info['shop'] = implode($this->multiple_value_separator, Shop::getContextListShopID());
- // Get shops for each attributes
- $info['shop'] = explode($this->multiple_value_separator, $info['shop']);
-
- foreach ($info['shop'] as $shop)
- if (!empty($shop) && !is_numeric($shop))
- $category->addShop(Shop::getIdByName($shop));
- elseif (!empty($shop))
- $category->addShop($shop);
- }
- }
- }
- /* Import has finished, we can regenerate the categories nested tree */
- Category::regenerateEntireNtree();
- $this->closeCsvFile($handle);
- }
- public function productImport()
- {
- $this->receiveTab();
- $handle = $this->openCsvFile();
- $default_language_id = (int)Configuration::get('PS_LANG_DEFAULT');
- $id_lang = Language::getIdByIso(Tools::getValue('iso_lang'));
- if (!Validate::isUnsignedId($id_lang))
- $id_lang = $default_language_id;
- AdminImportController::setLocale();
- $shop_ids = Shop::getCompleteListOfShopsID();
- for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); $current_line++)
- {
- if (Tools::getValue('convert'))
- $line = $this->utf8EncodeArray($line);
- $info = AdminImportController::getMaskedRow($line);
- if (Tools::getValue('forceIDs') && isset($info['id']) && (int)$info['id'])
- $product = new Product((int)$info['id']);
- elseif (Tools::getValue('match_ref') && array_key_exists('reference', $info))
- {
- $datas = Db::getInstance()->getRow('
- SELECT p.`id_product`
- FROM `'._DB_PREFIX_.'product` p
- '.Shop::addSqlAssociation('product', 'p').'
- WHERE p.`reference` = "'.pSQL($info['reference']).'"
- ');
- if (isset($datas['id_product']) && $datas['id_product'])
- $product = new Product((int)$datas['id_product']);
- else
- $product = new Product();
- }
- elseif (array_key_exists('id', $info) && (int)$info['id'] && Product::existsInDatabase((int)$info['id'], 'product'))
- $product = new Product((int)$info['id']);
- else
- $product = new Product();
- if (isset($product->id) && $product->id && Product::existsInDatabase((int)$product->id, 'product'))
- {
- $product->loadStockData();
- $category_data = Product::getProductCategories((int)$product->id);
- foreach ($category_data as $tmp)
- $product->category[] = $tmp;
- }
- AdminImportController::setEntityDefaultValues($product);
- AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $product);
- if (!Shop::isFeatureActive())
- $product->shop = 1;
- elseif (!isset($product->shop) || empty($product->shop))
- $product->shop = implode($this->multiple_value_separator, Shop::getContextListShopID());
- if (!Shop::isFeatureActive())
- $product->id_shop_default = 1;
- else
- $product->id_shop_default = (int)Context::getContext()->shop->id;
- // link product to shops
- $product->id_shop_list = array();
- foreach (explode($this->multiple_value_separator, $product->shop) as $shop)
- if (!empty($shop) && !is_numeric($shop))
- $product->id_shop_list[] =…
Large files files are truncated, but you can click here to view the full file