Files
drmaterac.pl/modules/import_api/controllers/admin/AdminImport_ApiController.php
2025-01-06 20:47:25 +01:00

831 lines
34 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
* With the purchase or the installation of the software in your application
* you accept the license agreement.
*
* You can not resell and redistribute this file.
*
* @author Dalibor Stojcevski <dal_sto@yahoo.com>
* @copyright 2019 Dalibor Stojcevski
* @license Dalibor Stojcevski
*/
class AdminImport_ApiController extends ModuleAdminController
{
public $bootstrap = true;
protected $_postErrors = array();
protected $fmr_fields;
public function __construct()
{
if ($this->context == null) {
$this->context = Context::getContext();
}
$this->toolbar_title = 'Sources';
$this->meta_title = 'Import api';
parent::__construct();
require_once(_PS_MODULE_DIR_ . 'import_api/classes/queue.php');
$this->queue = new Queue();
$this->frm_fields = ['unique', 'reference', 'name', 'description', 'short_description', 'price', 'wholesale_price', 'quantity', 'images', 'cover', 'brand', 'category', 'category_parent', 'ean13', 'upc', 'condition', 'location', 'additional_shipping_cost', 'width', 'height', 'depth', 'weight', 'active', 'feature', 'feature_value', 'attribute1', 'attribute_value1', 'attribute2', 'attribute_value2', 'attribute3', 'attribute_value3', 'attribute_price', 'attribute_quantity', 'attribute_ean', 'attribute_weight', 'minimal_quantity'];
$this->replace_fields = ['reference', 'name', 'description', 'short_description', 'price', 'wholesale_price', 'quantity', 'images', 'cover', 'brand', 'category', 'ean13', 'upc', 'condition', 'location', 'width', 'height', 'depth', 'weight', 'feature', 'feature_value', 'attribute1', 'attribute_value1', 'attribute2', 'attribute_value2', 'attribute3', 'attribute_value3', 'minimal_quantity'];
$this->frm_settings = array('top_category' => '', 'default_category' => '', 'default_brand' => '', 'price_multiplier' => '', 'combination_price_multiplier' => '', 'add_combination_price' => 0, 'category_path' => 0, 'id_tax_rules_group' => -1, 'same_update' => 0, 'not_existing' => 0, 'only_update' => 0, 'synchronize_field' => 'automatic');
$this->frm_update_settings = array('quantity' => 1, 'price' => 0, 'wholesale_price' => 0, 'manufacturer' => 0, 'category' => 0, 'cover' => 0, 'images' => 0, 'name' => 0, 'description' => 0, 'short_description' => 0,'reference' => 0, 'ean13' => 0, 'ups' => 0, 'condition' => 0, 'location'=> 0, 'additional_shipping_cost'=> 0, 'width' => 0 , 'height' => 0 , 'depth' => 0 , 'weight' => 0, 'features' => 0, 'attributes' => 0, 'active' => 0, 'id_tax_rules_group' => 0, 'minimal_quantity' => 0);
$this->filter_fields = ['reference', 'name', 'price', 'wholesale_price', 'quantity', 'images', 'cover', 'brand', 'category', 'ean13', 'upc', 'condition', 'location', 'width', 'height', 'depth', 'weight', 'feature', 'feature_value', 'attribute1', 'attribute_value1', 'attribute2', 'attribute_value2', 'attribute3', 'attribute_value3', 'minimal_quantity'];
if (Tools::getValue('add_source')) {
$this->addSource();
} elseif (Tools::getValue('delete') && Tools::getValue('file_id')) {
$this->deleteProducts(Tools::getValue('file_id'));
} elseif (Tools::getValue('file_id')) {
if (((bool)Tools::isSubmit('submitImportSettings')) == true) {
$this->_postSettingsValidation();
$message = '';
if (!count($this->_postErrors)) {
$this->_saveSettings(Tools::getValue('file_id'));
} else {
foreach ($this->_postErrors as $err) {
$this->errors[] = $err;
}
}
}
$this->processFile(Tools::getValue('file_id'), Tools::getValue('type'));
} else {
$this->getSourceList();
}
//return;
}
public function addSource()
{
if (Tools::isSubmit('submitAddSource')) {
$this->postFileProcess();
}
$this->context->smarty->assign('form_link', $this->context->link->getAdminLink('AdminImport_Api&add_source=1&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
$file_tpl = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'import_api/views/templates/admin/file_upload.tpl');
$this->content .= $file_tpl;
}
public function deleteProducts($file_id)
{
$query_file = Db::getInstance()->executeS("SELECT * FROM ". _DB_PREFIX_ . "ia_files WHERE file_id = '" . (int)$file_id . "' LIMIT 1");
if (empty($query_file)) {
$this->errors[] = $this->my_translation('File not found');
return;
} else {
$file = $query_file[0];
}
$this->toolbar_title = 'Delete';
$query_total = Db::getInstance()->executeS("SELECT COUNT(*) as total FROM ". _DB_PREFIX_ . "ia_products WHERE file_id = '" . (int)$file_id . "'");
$total = $query_total ? $query_total[0]['total'] : 0;
$file_name = $file['name'] ? $file['name'] : $file['link'];
$this->context->smarty->assign('token', $file['mask'] . $file['date_added']);
$this->context->smarty->assign('get_import_link', $this->context->link->getModuleLink('import_api', 'import', array('file_id' => $file_id)));
$this->context->smarty->assign('file_name', $file_name);
$this->context->smarty->assign('total', $total);
$delete_tpl = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'import_api/views/templates/admin/delete.tpl');
$this->content .= $delete_tpl;
}
public function getSourceList()
{
if (Tools::getValue('status_file_id') && Tools::getValue('change_status') !== false) {
Db::getInstance()->execute("UPDATE ". _DB_PREFIX_ . "ia_files SET status = '" . (int)Tools::getValue('change_status') ."' WHERE file_id = " . (int)Tools::getValue('status_file_id'));
}
$query_files = Db::getInstance()->executeS("SELECT f.* FROM ". _DB_PREFIX_ . "ia_files f ORDER BY date_edited DESC, file_id DESC");
$files = array();
if ($query_files) {
foreach ($query_files as $file) {
$other_status = $file['status'] ? 0 : 1;
$other_status_text = $file['status'] ? 'Disable' : 'Enable';
if (!Tools::getValue('show_disabled') && !$file['status']) {
continue;
}
$files[] = array(
'file_id' => $file['file_id'],
'name' => $file['name'] ? $file['name'] : $file['link'],
'date_added' => date('F j, Y', $file['date_added']),
'process_link' => $this->context->link->getAdminLink('AdminImport_Api&file_id=' . $file['file_id'] . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false),
'change_status_link' => $this->context->link->getAdminLink('AdminImport_Api&change_status=' . $other_status . '&status_file_id=' . $file['file_id'] . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false),
'other_status_text' => $other_status_text
);
}
$this->context->smarty->assign('files', $files);
$this->context->smarty->assign('show_disabled_link', $this->context->link->getAdminLink('AdminImport_Api&show_disabled=1&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
$list_tpl = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'import_api/views/templates/admin/list.tpl');
$this->content .= $list_tpl;
} else {
$this->addSource();
}
}
public function processFile($file_id, $type = 'import')
{
$this->addJquery();
$this->addJS(_MODULE_DIR_. 'import_api/views/js/dataTables.js');
$this->addJS(_MODULE_DIR_. 'import_api/views/js/dataTables.bootstrap4.js');
$this->addCSS(_MODULE_DIR_. 'import_api/views/css/dataTables.bootstrap4.css');
$query_file_settings = Db::getInstance()->executeS("SELECT f.*, fs.mapping FROM ". _DB_PREFIX_ . "ia_files f LEFT JOIN ". _DB_PREFIX_ . "ia_file_settings fs ON(fs.file_id = f.file_id) WHERE f.file_id = '" . (int)$file_id . "' LIMIT 1");
if (empty($query_file_settings)) {
$this->errors[] = $this->my_translation('File not found');
} else {
$file_settings = $query_file_settings[0];
$file_name = $file_settings['name'] ? $file_settings['name'] : $file_settings['link'];
if ($file_settings['mapping'] && $type == 'import') {
$this->toolbar_title = 'Import';
$this->context->smarty->assign('get_import_link', $this->context->link->getModuleLink('import_api', 'import', array('file_id'=> $file_id)));
$this->context->smarty->assign('show_all_link', $this->context->link->getAdminLink('AdminImport_Api&type=import&showall=1&file_id=' . $file_id . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
$this->context->smarty->assign('delete_link', $this->context->link->getAdminLink('AdminImport_Api&delete=1&file_id=' . $file_id . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
if (Tools::getValue('showall')) {
$limit = 0;
} else {
$limit = 600;
}
$products = $this->queue->getQueuedForTable($file_id, $limit);
$this->context->smarty->assign('settings_link', $this->context->link->getAdminLink('AdminImport_Api&type=settings&file_id=' . $file_id . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
$this->context->smarty->assign('products', $products);
$this->context->smarty->assign('limit', $limit);
$this->context->smarty->assign('auto_queue', 0);
$this->context->smarty->assign('file_name', $file_name);
$output = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'import_api/views/templates/admin/import.tpl');
$queue_tpl = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'import_api/views/templates/admin/temp.tpl');
$this->content .= $queue_tpl;
$this->content .= $output;
} else {
$this->toolbar_title = 'Settings';
$this->context->smarty->assign('file_settings', $file_settings);
$this->context->smarty->assign('file_id', $file_id);
$this->context->smarty->assign('file_name', $file_name);
$this->context->smarty->assign('import_link', $this->context->link->getAdminLink('AdminImport_Api&type=import&file_id=' . $file_id . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
$this->context->smarty->assign('get_fields_link', $this->context->link->getModuleLink('import_api', 'ajax', array('file_id'=> $file_id)));
$output = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'import_api/views/templates/admin/configure.tpl');
//$import_html = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'import_api/views/templates/admin/import.tpl');
$this->content .= $output. $this->renderSettingsForm($file_settings);
//$this->content .= $output;
}
}
}
public static function displayHumanReadableSize($size)
{
return Tools::formatBytes($size);
}
public function initPageHeaderToolbar()
{
$this->page_header_toolbar_btn['new_attachment'] = array(
'href' => self::$currentIndex . '&add_source=1&token=' . $this->token,
'desc' => $this->my_translation('Add new source'),
'icon' => 'process-icon-new',
);
$this->page_header_toolbar_btn['view_list'] = array(
'href' => self::$currentIndex . '&token=' . $this->token,
'desc' => $this->my_translation('View list'),
'icon' => 'process-icon-list',
);
parent::initPageHeaderToolbar();
}
public function postFileProcess()
{
$shop = 'default';
$source = !empty($_POST['import_api_type']) ? $_POST['import_api_type'] : '';
$headers = !empty($_POST['import_api_headers']) ? $_POST['import_api_headers'] : '';
$delimiter = !empty($_POST['import_api_delimiter']) ? $_POST['import_api_delimiter'] : '';
$mask = uniqid();
if (!empty($_POST['import_api_link'])) {
$query_file = Db::getInstance()->executeS("SELECT * FROM ". _DB_PREFIX_ . "ia_files WHERE link = '" . pSQL($_POST['import_api_link']) . "' AND shop = '" . pSQL($shop) . "' AND status = 1 LIMIT 1");
if ($query_file) {
$file_id = $query_file[0]['file_id'];
Db::getInstance()->execute("UPDATE ". _DB_PREFIX_ . "ia_files SET link = '" . pSQL($_POST['import_api_link']) . "', delimiter = '" . pSQL($delimiter) . "', headers = '" . pSQL($headers) . "', source = '" . pSQL($source) . "', date_edited = '" . time() . "' WHERE file_id = " . (int)$file_id ." AND shop = '" . pSQL($shop) . "'");
} else {
Db::getInstance()->execute("INSERT INTO ". _DB_PREFIX_ . "ia_files SET link = '" . pSQL($_POST['import_api_link']) . "', shop = '" . pSQL($shop) . "', delimiter = '" . pSQL($delimiter) . "', headers = '" . pSQL($headers) . "', source = '" . pSQL($source) . "', mask = '" . pSQL($mask) ."', date_added = '" . time() . "'");
}
} elseif (!empty($_FILES['doc']['tmp_name'])) {
$name = $_FILES['doc']['name'];
$tmp_name = $_FILES['doc']['tmp_name'];
$type = $_FILES['doc']['type'];
$file_ext = pathinfo($name, PATHINFO_EXTENSION);
if (in_array($file_ext, ['zip'])) {
$this->errors[] = $this->my_translation("Sorry, app can't read zip files. Extract zip and upload content from inside");
} else {
$target_file = _PS_MODULE_DIR_ . 'import_api/views/img/' . $mask . '_' . $name;
$base_link = Tools::getHttpHost(true).__PS_BASE_URI__;
$target_url = $base_link. 'modules/import_api/views/img/' . $mask . '_' . $name;;
if (move_uploaded_file($tmp_name, $target_file)) {
Db::getInstance()->execute("INSERT INTO ". _DB_PREFIX_ . "ia_files SET link = '" . pSQL($target_url) ."', mime_type = '" . pSQL($type) ."', source = '" . pSQL($source) ."', delimiter = '" . pSQL($delimiter) . "', headers = '" . pSQL($headers) . "', name = '" . pSQL($name) ."', mask = '" . pSQL($mask) ."', shop = '" . pSQL($shop) . "', date_added = '" . time() . "'");
} else {
$this->errors[] = $this->my_translation("Sorry, there was an error uploading your file.");
}
}
} else {
$this->errors[] = $this->my_translation("Enter link or upload file.");
}
if (!$this->errors) {
if (!isset($file_id)) {
$file_id = Db::getInstance()->Insert_ID();
}
if (!empty($_POST['import_api_link'])) {
if( strpos($_POST['import_api_link'], 'convert') !== false) {
$fields_file_path = _PS_MODULE_DIR_ . 'import_api/fields.txt';
$file_settings_file_path = _PS_MODULE_DIR_ . 'import_api/file_settings.txt';
if (file_exists($fields_file_path)) {
$f = file_get_contents($fields_file_path);
Db::getInstance()->execute("UPDATE ". _DB_PREFIX_ . "ia_files SET fields = '" . pSQL($f) . "', date_edited = '" . time() . "' WHERE file_id = " . (int)$file_id);
}
if (file_exists($file_settings_file_path)) {
$sql_raw = file_get_contents($file_settings_file_path);
$sql = sprintf($sql_raw, _DB_PREFIX_, $file_id);
Db::getInstance()->execute($sql);
}
}
}
Tools::redirectAdmin($this->context->link->getAdminLink('AdminImport_Api&type=settings&file_id=' . $file_id . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
}
}
protected function renderSettingsForm($settings, $tab = 'general')
{
$helper = new HelperForm();
$this->tab = $tab;
$helper->show_toolbar = false;
$helper->table = $this->table;
//$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitImportSettings';
$helper->currentIndex = $this->context->link->getAdminLink('AdminImport_Api', false)
.'&type=settings&file_id='. $settings['file_id'];
$helper->token = Tools::getAdminTokenLite('AdminImport_Api');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm($settings)));
}
protected function getConfigForm($settings)
{
$import_api_fields = $settings['fields'] ? html_entity_decode($settings['fields'], ENT_QUOTES, 'UTF-8') : '';
$source_fields = explode('##', $import_api_fields);
$options_not_existing[] = array(
'id_option' => 0,
'name' => $this->my_translation('Keep it'),
);
$options_not_existing[] = array(
'id_option' => 1,
'name' => $this->my_translation('Set quantity 0'),
);
$options_not_existing[] = array(
'id_option' => 2,
'name' => $this->my_translation('Set status disabled'),
);
$options_not_existing[] = array(
'id_option' => 3,
'name' => $this->my_translation('Delete'),
);
$options_synchronize_field[] = array(
'id_option' => 'automatic',
'name' => $this->my_translation('From unique field')
);
$options_synchronize_field[] = array(
'id_option' => 'reference',
'name' => $this->my_translation('Reference')
);
$options_synchronize_field[] = array(
'id_option' => 'ean13',
'name' => $this->my_translation('Ean13')
);
$options_synchronize_field[] = array(
'id_option' => 'upc',
'name' => $this->my_translation('Upc')
);
$options_synchronize_field[] = array(
'id_option' => 'name',
'name' => $this->my_translation('Product name')
);
$options_synchronize_field[] = array(
'id_option' => 'id_product',
'name' => $this->my_translation('Prestashop product id')
);
$options_yes_no[] = array(
'id_option' => 0,
'name' => $this->my_translation('No'),
);
$options_yes_no[] = array(
'id_option' => 1,
'name' => $this->my_translation('Yes'),
);
$options[] = array(
'id_option' => 0,
'name' => $this->my_translation('--Please select--'),
);
$filter_options[] = array(
'id_option' => 'equal',
'name' => $this->my_translation('Equal'),
);
$filter_options[] = array(
'id_option' => 'not_equal',
'name' => $this->my_translation('Not equal'),
);
$filter_options[] = array(
'id_option' => 'greater',
'name' => $this->my_translation('Greater'),
);
$filter_options[] = array(
'id_option' => 'less',
'name' => $this->my_translation('Less'),
);
$filter_options[] = array(
'id_option' => 'not_empty',
'name' => $this->my_translation('Not empty'),
);
$filter_options[] = array(
'id_option' => 'regexp',
'name' => $this->my_translation('REGEXP'),
);
if($import_api_fields){
foreach($source_fields as $field){
$options[] = array(
'id_option' => $field,
'name' => $field,
);
}
}
$def_tax[] = array('id_tax_rules_group' => -1, 'name' => 'Default');
$def_tax[] = array('id_tax_rules_group' => 0, 'name' => 'No tax');
$taxes = array_merge($def_tax, TaxRulesGroup::getTaxRulesGroups());
$fields[] = array(
'type' => 'hidden',
'id' => 'fields',
'name' => 'import_api_fields',
);
$fields[] = array(
'type' => 'hidden',
'id' => 'import_api_link',
'name' => 'import_api_link',
);
$fields[] = array(
'type' => 'hidden',
'id' => 'import_api_type',
'name' => 'import_api_type',
);
foreach($this->frm_fields as $fmr_field){
$fields[] = array(
'type' => 'select',
'class' => 'api_field',
'tab' => 'general',
'required' => in_array($fmr_field, ['unique', 'name']) ? true : false,
'label' => $this->my_translation($fmr_field),
'name' => 'import_api_field['. $fmr_field .']',
'options' => array(
'query' => $options,
'id' => 'id_option',
'name' => 'name'
)
);
}
foreach($this->frm_fields as $fmr_field){
$fields[] = array(
'type' => 'text',
'tab' => 'modifications',
'label' => $this->my_translation($fmr_field),
'name' => 'import_api_modification['. $fmr_field .']',
);
$fields[] = array(
'type' => 'text',
'tab' => 'combinations',
'label' => $this->my_translation($fmr_field),
'name' => 'import_api_split['. $fmr_field .']',
);
}
foreach($this->filter_fields as $f_field) {
$fields[] = array(
'type' => 'textarea',
'tab' => 'filter',
'rows' => 10,
'hint' => 'Enter each value in new line',
'label' => $this->my_translation($f_field),
'name' => 'import_api_filter['. $f_field .']',
'description' => 'Enter values, each in new line',
);
$fields[] = array(
'type' => 'select',
'tab' => 'filter',
'label' => $this->my_translation('Filter options for ' . $f_field),
'name' => 'import_api_filter_options['. $f_field .']',
'options' => array(
'query' => $filter_options,
'id' => 'id_option',
'name' => 'name'
)
);
}
foreach($this->replace_fields as $r_field) {
$fields[] = array(
'type' => 'textarea',
'tab' => 'replace',
'rows' => 10,
'hint' => 'Enter each value in new line in format OLD_TEXT##NEW_TEXT',
'label' => $this->my_translation($r_field),
'name' => 'import_api_replace['. $r_field .']',
'description' => 'Enter values in format old_word##new_word',
);
}
// Settings fields
$fields[] = array(
'type' => 'text',
'tab' => 'settings',
'label' => $this->my_translation('Top category'),
'name' => 'import_api_settings[top_category]',
);
$fields[] = array(
'type' => 'text',
'tab' => 'settings',
'label' => $this->my_translation('Default category'),
'name' => 'import_api_settings[default_category]',
);
$fields[] = array(
'type' => 'text',
'tab' => 'settings',
'label' => $this->my_translation('Default brand'),
'name' => 'import_api_settings[default_brand]',
);
$fields[] = array(
'type' => 'text',
'tab' => 'settings',
'label' => $this->my_translation('Price multiplier'),
'name' => 'import_api_settings[price_multiplier]',
);
$fields[] = array(
'type' => 'text',
'tab' => 'settings',
'label' => $this->my_translation('Combination price multiplier'),
'name' => 'import_api_settings[combination_price_multiplier]',
);
$fields[] = array(
'type' => 'select',
'tab' => 'settings',
'label' => $this->my_translation('Add combination price to product price'),
'name' => 'import_api_settings[add_combination_price]',
'options' => array(
'query' => $options_yes_no,
'id' => 'id_option',
'name' => 'name'
)
);
$fields[] = array(
'type' => 'select',
'tab' => 'settings',
'label' => $this->my_translation('Create category path from array'),
'name' => 'import_api_settings[category_path]',
'options' => array(
'query' => $options_yes_no,
'id' => 'id_option',
'name' => 'name'
)
);
$fields[] = array(
'type' => 'select',
'tab' => 'settings',
'label' => $this->my_translation('Tax rules group'),
'name' => 'import_api_settings[id_tax_rules_group]',
'options' => array(
'query' => $taxes,
'id' => 'id_tax_rules_group',
'name' => 'name'
)
);
$fields[] = array(
'type' => 'select',
'tab' => 'settings',
'label' => $this->my_translation('Update even if file is not changed'),
'name' => 'import_api_settings[same_update]',
'options' => array(
'query' => $options_yes_no,
'id' => 'id_option',
'name' => 'name'
)
);
$fields[] = array(
'type' => 'select',
'tab' => 'settings',
'label' => $this->my_translation('If product not exist in file anymore'),
'name' => 'import_api_settings[not_existing]',
'options' => array(
'query' => $options_not_existing,
'id' => 'id_option',
'name' => 'name'
)
);
$fields[] = array(
'type' => 'select',
'tab' => 'settings',
'label' => $this->my_translation('Update only'),
'name' => 'import_api_settings[only_update]',
'options' => array(
'query' => $options_yes_no,
'id' => 'id_option',
'name' => 'name'
)
);
$fields[] = array(
'type' => 'select',
'tab' => 'settings',
'label' => $this->my_translation('Update field'),
'name' => 'import_api_settings[synchronize_field]',
'options' => array(
'query' => $options_synchronize_field,
'id' => 'id_option',
'name' => 'name'
)
);
foreach($this->frm_update_settings as $key => $setting) {
$fields[] = array(
'type' => 'select',
'tab' => 'update',
'label' => $this->my_translation('Update '. $key),
'name' => 'import_api_settings[update_' . $key .']',
'options' => array(
'query' => $options_yes_no,
'id' => 'id_option',
'name' => 'name'
)
);
}
return array(
'form' => array(
'legend' => array(
'title' => $this->my_translation('Configuration'),
'icon' => 'icon-cogs',
),
'input' => $fields,
'submit' => array(
'title' => $this->my_translation('Save'),
),
'tabs' => array(
'general' => $this->my_translation('Assign'),
'combinations' => $this->my_translation('Split'),
'modifications' => $this->my_translation('Modifications'),
'filter' => $this->my_translation('Filter'),
'replace' => $this->my_translation('Replace'),
'settings' => $this->my_translation('Settings'),
'update' => $this->my_translation('Update fields'),
'test' => $this->my_translation('Test'),
),
),
);
}
protected function getConfigFormValues()
{
$file_id = Tools::getValue('file_id');
$return_settings = array();
if (((bool)Tools::isSubmit('submitImportSettings')) == true) {
$config_fields = Tools::getValue('import_api_field');
$config_modification = Tools::getValue('import_api_modification');
$config_split = Tools::getValue('import_api_split');
$config_replace = Tools::getValue('import_api_replace');
$config_filter = Tools::getValue('import_api_filter');
$config_filter_options = Tools::getValue('import_api_filter_options');
$config_settings = Tools::getValue('import_api_settings');
$return_settings['import_api_fields'] = Tools::getValue('import_api_fields');
$return_settings['import_api_link'] = Tools::getValue('import_api_link');
$return_settings['import_api_type'] = Tools::getValue('import_api_type');
} else {
$query_file_settings = Db::getInstance()->executeS("SELECT fs.* FROM ". _DB_PREFIX_ . "ia_file_settings fs WHERE fs.file_id = '" . (int)$file_id . "' LIMIT 1");
if ($query_file_settings) {
$file_settings =$query_file_settings[0];
$config_fields = json_decode($file_settings['mapping'], true);
$config_modification = json_decode($file_settings['modification'], true);
$config_split = json_decode($file_settings['split'], true);
$config_replace = json_decode($file_settings['replace'], true);
$config_filter = json_decode($file_settings['filter'], true);
$config_filter_options = json_decode($file_settings['filter_options'], true);
$config_settings = json_decode($file_settings['settings'], true);
}
$return_settings['import_api_fields'] = '';
$return_settings['import_api_link'] = '';
$return_settings['import_api_type'] = '';
}
foreach($this->frm_fields as $fmr_field){
$return_settings['import_api_field[' . $fmr_field . ']'] = !empty($config_fields[$fmr_field]) ? html_entity_decode($config_fields[$fmr_field], ENT_QUOTES, 'UTF-8') : '';
$return_settings['import_api_modification[' . $fmr_field . ']'] = !empty($config_modification[$fmr_field]) ? html_entity_decode($config_modification[$fmr_field], ENT_QUOTES, 'UTF-8') : '';
$return_settings['import_api_split[' . $fmr_field . ']'] = !empty($config_split[$fmr_field]) ? html_entity_decode($config_split[$fmr_field], ENT_QUOTES, 'UTF-8') : '';
}
foreach($this->replace_fields as $r_field){
$return_settings['import_api_replace[' . $r_field . ']'] = !empty($config_replace[$r_field]) ? html_entity_decode($config_replace[$r_field], ENT_QUOTES, 'UTF-8') : '';
}
foreach($this->filter_fields as $f_field){
$return_settings['import_api_filter[' . $f_field . ']'] = !empty($config_filter[$f_field]) ? html_entity_decode($config_filter[$f_field], ENT_QUOTES, 'UTF-8') : '';
$return_settings['import_api_filter_options[' . $f_field . ']'] = !empty($config_filter_options[$f_field]) ? html_entity_decode($config_filter_options[$f_field], ENT_QUOTES, 'UTF-8') : '';
}
for($i = 1; $i <= 3; $i++){
$return_settings['import_api_field[field' . $i. ']'] = !empty($config_fields['field' . $i]) ? html_entity_decode($config_fields['field' . $i], ENT_QUOTES, 'UTF-8') : '';
}
for($i = 1; $i <= 3; $i++){
$return_settings['import_api_modification[modification' . $i . ']'] = !empty($config_modification['modification' . $i]) ? html_entity_decode($config_modification['modification' . $i], ENT_QUOTES, 'UTF-8') : '';
}
foreach($this->frm_settings as $setting => $value){
$return_settings['import_api_settings[' . $setting . ']'] = isset($config_settings[$setting]) ? $config_settings[$setting] : $value;
}
foreach($this->frm_update_settings as $setting => $value){
$return_settings['import_api_settings[update_' . $setting . ']'] = isset($config_settings['update_' . $setting]) ? $config_settings['update_' . $setting] : $value;
}
return $return_settings;
}
protected function _postSettingsValidation()
{
if (!Tools::getValue('import_api_field')) {
$this->_postErrors[] = $this->my_translation('You must assign unique field and name field.');
} else {
$fields = Tools::getValue('import_api_field');
if(empty($fields['unique'])){
$this->_postErrors[] = $this->my_translation('You must assign unique field');
}
if(empty($fields['name'])){
//$this->_postErrors[] = $this->my_translation('You must assign name field');
}
}
}
protected function _saveSettings($file_id, $shop = 'default')
{
$mapping = '';
$modification = '';
$split = '';
$replace = '';
$filter = '';
$filter_options = '';
$settings = '';
$update = false;
$query_file_settings = Db::getInstance()->executeS("SELECT fs.* FROM ". _DB_PREFIX_ . "ia_file_settings fs WHERE fs.file_id = '" . (int)$file_id . "' LIMIT 1");
if ($query_file_settings) {
$file_settings = $query_file_settings[0];
$mapping = $file_settings['mapping'];
$modification = $file_settings['modification'];
$split = $file_settings['split'];
$replace = $file_settings['replace'];
$filter = $file_settings['filter'];
$filter_options = $file_settings['filter_options'];
$settings = $file_settings['settings'];
$update = true;
}
$implode = array();
$new_mapping = json_encode(Tools::getValue('import_api_field'));
$new_modification = json_encode(Tools::getValue('import_api_modification'));
$new_split = json_encode(Tools::getValue('import_api_split'));
$new_replace = json_encode(Tools::getValue('import_api_replace'));
$new_filter = json_encode(Tools::getValue('import_api_filter'));
$new_filter_options = json_encode(Tools::getValue('import_api_filter_options'));
$new_settings = json_encode(Tools::getValue('import_api_settings'));
if (Tools::getValue('import_api_field') && $new_mapping != $mapping) {
$implode[] = "mapping='" . pSQL($new_mapping). "'";
}
if (Tools::getValue('import_api_modification') && $new_modification != $modification) {
$implode[] = "modification='" . pSQL($new_modification). "'";
}
if (Tools::getValue('import_api_split') && $new_split != $split) {
$implode[] = "split='" . pSQL($new_split). "'";
}
if (Tools::getValue('import_api_replace') && $new_replace != $replace) {
$implode[] = "`replace`='" . pSQL($new_replace). "'";
}
if (Tools::getValue('import_api_filter') && $new_filter != $filter) {
$implode[] = "`filter`='" . pSQL($new_filter). "'";
}
if (Tools::getValue('import_api_filter_options') && $new_filter_options != $filter_options) {
$implode[] = "filter_options='" . pSQL($new_filter_options). "'";
}
if (Tools::getValue('import_api_settings') && $new_settings != $settings) {
$implode[] = "settings='" . pSQL($new_settings). "'";
}
if ($implode) {
$implode[] = "date_updated = '" . time() ."'";
$implode_string = implode(',', $implode);
if ($update) {
Db::getInstance()->execute("UPDATE ". _DB_PREFIX_ . "ia_file_settings SET " .$implode_string. " WHERE file_id = '" .(int)$file_id . "' AND shop = '" . pSQL($shop) . "'");
} else {
Db::getInstance()->execute("INSERT INTO ". _DB_PREFIX_ . "ia_file_settings SET " .$implode_string. ", file_id = '" .(int)$file_id . "', shop = '" . pSQL($shop) . "'");
}
//print_r("UPDATE ". _DB_PREFIX_ . "ia_file_settings SET " .$implode_string. " WHERE file_id = '" .(int)$file_id . "' AND shop = '" . $shop . "'");
$this->queue->deleteUnnecessary($file_id, $shop);
Tools::redirectAdmin($this->context->link->getAdminLink('AdminImport_Api&type=import&file_id=' . $file_id . '&token=' . Tools::getAdminTokenLite('AdminImport_Api'), false));
}
}
public function my_translation($string) {
return $this->module->l($string);
}
}