first commit

This commit is contained in:
2024-10-25 14:16:28 +02:00
commit 925276dbb2
33795 changed files with 4780077 additions and 0 deletions

View File

@@ -0,0 +1,831 @@
<?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);
}
}

View File

@@ -0,0 +1,106 @@
<?php
/**
* NOTICE OF LICENSE
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You can not resell and redistribute this file.
*
* @author Dalibor Stojcevski <dal_sto@yahoo.com>
* @copyright 2019 Dalibor Stojcevski
* @license Dalibor Stojcevski
*/
class Import_ApiAjaxModuleFrontController extends ModuleFrontController
{
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
private $fields = array();
public function initContent()
{
$response = $this->find_fields();
$json = Tools::jsonEncode($response);
$this->ajaxDie($json);
}
public function find_fields()
{
Db::getInstance()->execute("SET session wait_timeout=28800", FALSE);
Db::getInstance()->execute("SET session net_read_timeout=28800", FALSE);
Db::getInstance()->execute("SET session interactive_timeout=28800", FALSE);
$file_id = Tools::getValue('file_id');
$shop = 'default';
$json['error'] = '';
$php_array = array();
$query_file = Db::getInstance()->executeS("SELECT * FROM ". _DB_PREFIX_ . "ia_files WHERE file_id = '" . (int)$file_id . "' AND shop = '" . pSQL($shop) . "' LIMIT 1");
//$type = Tools::getValue('type');
//$link = html_entity_decode($this->request->post['import_api_link'], ENT_QUOTES, "UTF-8");
if ($query_file) {
require_once(_PS_MODULE_DIR_ . 'import_api/classes/filereader.php');
$FileReader = new FileReader();
$file = $query_file[0];
//$file['link'] = html_entity_decode($file['link'], ENT_QUOTES, "UTF-8");
list($php_array, $json['error']) = $FileReader->getArrayFromLink($file);
if($php_array === null){
$json['error'] = 'File is not in selected format';
}
} else {
$json['error'] = 'File not found';
}
if (!$json['error']){
if (count($php_array) > 10 && !empty(array_key_first($php_array))) { // if is not 0
$new_array = array();
foreach ($php_array as $key => $value ) {
$value['GENERATED_UNIQUE'] = $key;
$new_array[] = $value;
}
$php_array = $new_array;
unset($new_array);
}
$this->search_keys($php_array, 'FEED');
$json['field'] = $this->fields;
$json['fields'] = implode('##', $this->fields);
//$json['mapping'] = implode('##', $this->fields);
if ($this->fields) {
Db::getInstance()->execute("UPDATE ". _DB_PREFIX_ . "ia_files SET fields = '" . pSQL($json['fields']) . "', date_edited = '" . time() . "' WHERE file_id = " . (int)$file_id ." AND shop = '" . pSQL($shop) . "'");
}
}
return $json;
}
function search_keys($array, $parent){
$total = count($array);
foreach($array as $key => $value){
if(!is_array($value)){
if (is_int($key)) {
$path = $parent;
} else {
$path = $parent .'->'. $key;
}
$path = str_replace('->array()', '',$path);
if (!in_array($path, $this->fields))
$this->fields[] = $path;
} else {
if (is_int($key)) {
$key = "array()";
}
$this->search_keys($value, $parent .'->'. $key);
}
}
}
}

View File

@@ -0,0 +1,599 @@
<?php
/**
* NOTICE OF LICENSE
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You can not resell and redistribute this file.
*
* @author Dalibor Stojcevski <dal_sto@yahoo.com>
* @copyright 2019 Dalibor Stojcevski
* @license Dalibor Stojcevski
*/
ini_set('memory_limit', '-1');
class Import_ApiImportModuleFrontController extends ModuleFrontController
{
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
private $fields = array();
private $product_links = array();
private $import;
private $creator;
private $convertor;
public static $update_limit = 5;
public function initContent()
{
require_once(_PS_MODULE_DIR_ . 'import_api/classes/import.php');
require_once(_PS_MODULE_DIR_ . 'import_api/classes/creator.php');
require_once(_PS_MODULE_DIR_ . 'import_api/classes/filereader.php');
require_once(_PS_MODULE_DIR_ . 'import_api/classes/queue.php');
require_once(_PS_MODULE_DIR_ . 'import_api/classes/convertor.php');
Db::getInstance()->execute("SET session wait_timeout=28800", FALSE);
Db::getInstance()->execute("SET session net_read_timeout=28800", FALSE);
Db::getInstance()->execute("SET session interactive_timeout=28800", FALSE);
$json = array();
$view = Tools::getValue('id');
$file_id = Tools::getValue('file_id');
$this->queue = new Queue();
$this->creator = new Creator($file_id);
$action = Tools::getValue('action') ? Tools::getValue('action') : 'cron';
/* if ($this->product_links === null) {
$json['error'] = 'Invalid source url. Please check in browser first';
$this->ajaxDie(Tools::jsonEncode($json));
}*/
$this->getModuleSettings();
$this->convertor = new Convertor($this->settings, $this->creator);
if ($action == 'test') {
//$this->creator->addAttributesToProduct(array('0' => array('0' => array('attribute'=> "DColor", 'attribute_value' => "blue"), '1' => array('attribute'=> "DColor", 'attribute_value' => "green"))), 12685); exit;
$this->product_links = $this->getProductLinks($action);
$this->printTestJsonData($view);
} elseif (Tools::getValue('queue')) {
echo $this->queue($file_id);
exit;
} elseif ($action == 'selected') {
$this->importSelected($file_id);
}elseif ($action == 'delete') {
$this->deleteProducts($file_id);
} elseif ($action == 'view_one') {
$this->viewOne($file_id, Tools::getValue('index'));
} elseif (Tools::getValue('index')) {
$this->importOne($file_id, Tools::getValue('index'));
} else {
$this->importData($file_id, $action, Tools::getValue( 'limit' ) );
Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();
}
}
function queue($file_id)
{
$product_links = $this->getProductLinks();
$products = array();
foreach ($product_links as $link) {
$original_product = $this->importer->getAllData($link);
$original_product = $this->convertor->unArray($original_product);
$original_product = $this->convertor->replace($original_product);
$original_product = $this->convertor->filter($original_product);
$original_product = $this->convertor->clearInput($original_product);
if ($original_product['belong']) {
$products[$original_product['unique']] = $original_product;
}
}
$this->queue->saveTempToDb($products, $file_id);
return count($products);
}
function importData($file_id, $action = 'cron', $limit = 0 )
{
if (!ini_get('max_execution_time')) {
ini_set('max_execution_time', 100);
}
$json['notice'] = '';
$last_queue = $this->queue->getLastQueue($file_id);
if (!$last_queue || $last_queue['status'] == 4) {
$this->queue($file_id);
$last_queue = $this->queue->getLastQueue($file_id);
}
if (!$last_queue) {
exit('Queue creation error');
}
if ($last_queue['status'] == 3) {
$this->creator->processMissing($file_id, $last_queue['queue_id']);
$this->ajaxDie(Tools::jsonEncode($json));
exit;
}
$products = $this->queue->getQueued($file_id);
if (!$products) {
$this->queue($file_id);
$products = $this->queue->getQueued($file_id);
$last_queue = $this->queue->getLastQueue($file_id);
}
$queue_id = isset($products[0]) ? $products[0]['queue_id'] : 0;
$products_created = 0;
$products_updated = 0;
$json['total'] = count($products);
foreach ($products as $product) {
$original_product = json_decode($product['product'], true);
$product_id = 0;
if ($this->settings['unique_equivalent'] == 'id_product') {
$product_id = (int)$original_product['unique'];
} else {
$equivalent = $this->settings['unique_equivalent'];
$query = ($equivalent == 'name') ? $original_product['name'] : $original_product['unique'];
$product_id = $this->creator->getProductId($query, $this->settings['table_equivalent'], $equivalent);
}
$this->queue->deleteQueuedProduct($product['product_id']);
if ($product_id) {
$this->creator->editProduct($product_id, $original_product);
Db::getInstance()->execute("UPDATE " . _DB_PREFIX_ . "ia_products SET indx = '" . pSQL($original_product['unique']) . "', product = '" . pSQL(json_encode($original_product)) . "', source = 'admin', date_edited = '" . time() . "', queue_id = '" . (int)$queue_id . "', file_id = '" . (int)$file_id . "' WHERE product_id = '" . (int)$product_id . "' AND shop = 'default'");
$products_updated++;
} elseif (empty($this->settings['import_api_settings']['only_update'])) {
$frm_product = $this->convertor->convertToFrmProduct($original_product);
$product_id = $frm_product->id;
Db::getInstance()->execute("INSERT INTO " . _DB_PREFIX_ . "ia_products SET product_id = '" . $product_id . "', indx = '" . pSQL($original_product['unique']) . "', product = '" . pSQL(json_encode($original_product)) . "', shop = 'default', source = 'admin', date_added = '" . time() . "', queue_id = '" . (int)$queue_id . "', file_id = '" . (int)$file_id . "'");
$products_created++;
if ( $action == 'cron' )
echo '<p>Dodałem nowy produkt: ' . $product_id . '</p>';
}
if ( ( ( $products_created + $products_updated ) >= self::$update_limit ) or ( $limit and ( $products_created + $products_updated ) >= $limit ) )
{
$json['notice'] = 'time_out';
break;
}
}
if (!$json['notice']) {
$json['notice'] = 'missing';
Db::getInstance()->execute("UPDATE " . _DB_PREFIX_ . "ia_queues SET source = 'admin', status = '3', date_processed = '" . time() . "' WHERE queue_id = '" . (int)$queue_id . "'");
}
if ($action != 'cron') {
$json['products_created'] = $products_created;
$json['products_updated'] = $products_updated;
$this->ajaxDie(Tools::jsonEncode($json));
}
exit;
}
function importSelected($file_id)
{
$products = array();
$selected = Tools::getValue('indx') ? Tools::getValue('indx') : array();
foreach ($selected as $indx) {
$products[] = $this->queue->getQueuedProduct($indx, $file_id);
}
$queue_id = isset($products[0]) ? $products[0]['queue_id'] : 0;
$products_created = 0;
$products_updated = 0;
$json['notice'] = '';
$json['total'] = count($products);
$json['processed'] = array();
foreach ($products as $product) {
$original_product = json_decode($product['product'], true);
$json['processed'][] = $product['product_id'];
$product_id = 0;
if ($this->settings['unique_equivalent'] == 'id_product') {
$product_id = (int)$original_product['unique'];
} else {
$equivalent = $this->settings['unique_equivalent'];
$query = ($equivalent == 'name') ? $original_product['name'] : $original_product['unique'];
$product_id = $this->creator->getProductId($query, $this->settings['table_equivalent'], $equivalent);
}
$this->queue->deleteQueuedProduct($product['product_id']);
if ($product_id) {
$this->creator->editProduct($product_id, $original_product);
Db::getInstance()->execute("UPDATE " . _DB_PREFIX_ . "ia_products SET indx = '" . pSQL($indx) . "', product = '" . pSQL(json_encode($original_product)) . "', source = 'selected', date_edited = '" . time() . "', queue_id = '" . (int)$queue_id . "', file_id = '" . (int)$file_id . "' WHERE product_id = '" . (int)$product_id . "' AND shop = 'default'");
$products_updated++;
} elseif (empty($this->settings['import_api_settings']['only_update'])) {
$frm_product = $this->convertor->convertToFrmProduct($original_product);
$product_id = $frm_product->id;
Db::getInstance()->execute("INSERT INTO " . _DB_PREFIX_ . "ia_products SET product_id = '" . (int)$product_id . "', indx = '" . pSQL($indx) . "', product = '" . pSQL(json_encode($original_product)) . "', shop = 'default', source = 'selected', date_added = '" . time() . "', queue_id = '" . (int)$queue_id . "', file_id = '" . (int)$file_id . "'");
$products_created++;
}
if ( ( $products_created + $products_updated ) >= self::$update_limit )
{
$json['notice'] = 'time_out';
break;
}
}
$json['products_created'] = $products_created;
$json['products_updated'] = $products_updated;
$this->ajaxDie(Tools::jsonEncode($json));
exit;
}
function importOne($file_id, $indx)
{
$product = $this->queue->getQueuedProduct($indx, $file_id);
if (!$product) {
echo 'Product not found in queue';
exit;
}
$queue_id = isset($product[0]) ? $product['queue_id'] : 0;
$original_product = json_decode($product['product'], true);
$equivalent = $this->settings['unique_equivalent'];
$query = ($equivalent == 'name') ? $original_product['name'] : $original_product['unique'];
$product_id = $this->creator->getProductId($query, $this->settings['table_equivalent'], $equivalent);
$link = new Link();
if ($product_id) {
$exising = $this->creator->editProduct($product_id, $original_product);
Db::getInstance()->execute("UPDATE " . _DB_PREFIX_ . "ia_products SET indx = '" . pSQL($indx) . "', product = '" . pSQL(json_encode($original_product)) . "', source = 'selected', date_edited = '" . time() . "', queue_id = '" . (int)$queue_id . "', file_id = '" . (int)$file_id . "' WHERE product_id = '" . (int)$product_id . "' AND shop = 'default'");
echo 'Product is updated';
//echo 'link to product <a href="' . $link->getproductlink($product_id) . '">' . $link->getproductlink($product_id) . '</a>';
} else {
$frm_product = $this->convertor->convertToFrmProduct($original_product);
if ($frm_product) {
$product_id = $frm_product->id;
Db::getInstance()->execute("INSERT INTO " . _DB_PREFIX_ . "ia_products SET product_id = '" . (int)$product_id . "', indx = '" . pSQL($indx) . "', product = '" . pSQL(json_encode($original_product)) . "', shop = 'default', source = 'selected', date_added = '" . time() . "', queue_id = '" . (int)$queue_id . "', file_id = '" . (int)$file_id . "'");
echo 'Product is inserted';
//echo 'Link to product <a href="' . $link->getProductLink($product_id) . '">' . $link->getProductLink($product_id) . '</a>';
}
}
exit;
}
function viewOne($file_id, $indx)
{
$product = $this->queue->getQueuedProduct($indx, $file_id);
if (!$product) {
echo 'Product not found in queue';
exit;
}
$queue_id = isset($product[0]) ? $product['queue_id'] : 0;
$original_product = json_decode($product['product'], true);
$equivalent = $this->settings['unique_equivalent'];
$query = ($equivalent == 'name') ? $original_product['name'] : $original_product['unique'];
$product_id = $this->creator->getProductId($query, $this->settings['table_equivalent'], $equivalent);
$link = new Link();
if ($product_id) {
echo 'Product exists ' . $product_id;
//echo 'link to product <a href="' . $link->getproductlink($exising) . '">' . $link->getproductlink($exising) . '</a>';
} else {
echo 'Product is not inserted in Prestashop site ';
}
foreach ($original_product as $key => $value) {
if (!is_array($value)) {
echo $key. ':' . $value . ', ';
}
}
if (isset($original_product['cover'])) {
if (!$this->UR_exists($original_product['cover'])) {
echo 'image is not found. If you need to add server name go to Modifications and add https://servername/[[cover]]';
} else {
echo 'image: ' . $original_product['cover'];
}
}
exit;
}
private function printTestJsonData($view)
{
$json = array();
foreach ($this->product_links as $link) {
$json[] = $this->importer->getAllData($link, $view);
}
$this->ajaxDie(Tools::jsonEncode($json));
}
private function getProductLinks($action = 'import')
{
$begin_character = 'FEED';
$shop = 'default';
$file_id = Tools::getValue('file_id');
$settings = $this->getModuleSettings($file_id);
$query_file = Db::getInstance()->executeS("SELECT * FROM ". _DB_PREFIX_ . "ia_files WHERE file_id = '" . (int)$file_id . "' AND shop = '" . pSQL($shop) . "' LIMIT 1");
$file = $query_file[0];
$FileReader = new FileReader();
list($tmp_array, $error) = $FileReader->getArrayFromLink($file);
if (count($tmp_array) > 10 && !empty(array_key_first($tmp_array))) { // if is not 0
$new_array = array();
foreach ($tmp_array as $key => $value ) {
$value['GENERATED_UNIQUE'] = $key;
$new_array[] = $value;
}
$tmp_array = $new_array;
unset($new_array);
}
$php_array[$begin_character] = $tmp_array;
$parts = explode('->', $settings['unique_field']);
$identifier_field = $parts[count($parts) - 1];
$importer = new Import($settings);
$importer->setJsonArray($php_array);
$importer->findUniqueProductsIdentifier($php_array, $parts, $identifier_field);
$product_links = $importer->getProductLinks();
//var_dump($product_links); exit;
$this->importer = $importer;
$start = $limit = 0;
if ($action == 'test') {
$start = $settings['start'];
$limit = 20;
}
if (Tools::getValue('start')) {
$start = Tools::getValue('start');
}
if (Tools::getValue('limit')) {
$limit = Tools::getValue('limit');
}
if ($start && !$limit) {
$limit = count($product_links) - $start;
}
if ($limit) {
$product_links = array_slice($product_links, $start, $limit);
}
return $product_links;
}
function getModuleSettings($file_id = 0)
{
if (!$file_id) {
$file_id = Tools::getValue('file_id');
}
$shop = 'default';
$settings_fields = array('link', 'attribute_group', 'default_brand', 'default_category', 'top_category', 'weight_class_id', 'stock_status_id', 'tax', 'default_option', 'category_path', 'multiplier', 'id_tax_rules_group');
if (Tools::getValue('import_api_field')) {
$settings = array(
'import_api_field' => Tools::getValue('import_api_field'),
'import_api_modification' => Tools::getValue('import_api_modification'),
'import_api_combination' => Tools::getValue('import_api_combination'),
'import_api_settings' => Tools::getValue('import_api_settings'),
'import_api_link' => Tools::getValue('import_api_link'),
'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];
$filters = array();
$replaces = array();
if ($file_settings['filter']) {
$text_areas = json_decode($file_settings['filter'], true);
foreach ($text_areas as $key => $textarea_value) {
if ($textarea_value) {
$filters[$key] = array_map('trim',explode("\n", $textarea_value));
}
}
}
if ($file_settings['replace']) {
$text_areas = json_decode($file_settings['replace'], true);
foreach ($text_areas as $key => $textarea_value) {
$lines = array_map('trim',explode("\n", $textarea_value));
foreach($lines as $line) {
if ($line) {
$replaces[$key][] = explode('##', $line);
}
}
}
}
$settings = array(
'import_api_field' => json_decode($file_settings['mapping'], true),
'import_api_modification' => json_decode($file_settings['modification'], true),
'import_api_combination' => json_decode($file_settings['split'], true),
'import_api_filter' => $filters,
'import_api_replace' => $replaces,
'import_api_filter_options' => json_decode($file_settings['filter_options'], true),
'import_api_settings' => json_decode($file_settings['settings'], true),
);
}
}
if (!empty($settings['import_api_field']['unique'])) {
$settings['unique_field'] = html_entity_decode($settings['import_api_field']['unique'], ENT_QUOTES, 'UTF-8');
} else {
$json['error'] = 'you need to set unique field';
$this->ajaxDie(Tools::jsonEncode($json));
}
if (Tools::getValue('start')) {
$settings['start'] = Tools::getValue('start');
} else {
$settings['start'] = 0;
}
if ($settings['import_api_settings']['top_category']) {
$settings['top_category_id'] = $this->creator->getCategoryId($settings['import_api_settings']['top_category']);
} else {
$settings['top_category_id'] = Configuration::get('PS_HOME_CATEGORY');
}
if ($settings['import_api_settings']['default_category']) {
$settings['default_category_id'] = $this->creator->getCategoryId($settings['import_api_settings']['default_category']);
} else {
$settings['default_category_id'] = 0;
}
if ($settings['import_api_settings']['default_brand']) {
$settings['default_manufacturer_id'] = $this->creator->getManufacturerId($settings['import_api_settings']['default_brand']);
} else {
$settings['default_manufacturer_id'] = 0;
}
$settings['id_tax_rules_group'] = isset($settings['import_api_settings']['id_tax_rules_group']) ? $settings['import_api_settings']['id_tax_rules_group'] : -1;
$settings['unique_equivalent'] = 'name';
$settings['table_equivalent'] = 'product_lang';
if (isset($settings['import_api_settings']['synchronize_field']) && $settings['import_api_settings']['synchronize_field'] != 'automatic') {
$settings['unique_equivalent'] = $settings['import_api_settings']['synchronize_field'];
} else {
$possible_eq = ['reference', 'sku', 'ean13', 'upc'];
foreach ($possible_eq as $f) {
if (!empty($settings['import_api_field'][$f]) && $settings['import_api_field'][$f] == $settings['import_api_field']['unique']){
$settings['unique_equivalent'] = $f;
break;
}
}
}
if ($settings['unique_equivalent'] != 'name') {
$settings['table_equivalent'] = 'product';
}
$this->settings = $settings;
return $settings;
}
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)) {
$json['notice'] = 'File not found';
$this->ajaxDie(Tools::jsonEncode($json));
exit;
} else {
$file = $query_file[0];
}
if ($file['mask'] . $file['date_added'] != Tools::getValue('token')) {
$json['notice'] = 'File not found.....';
$this->ajaxDie(Tools::jsonEncode($json));
exit;
}
$max_execution_time = 50;
$products = $this->queue->getProducts($file_id);
$products_deleted = 0;
$json['notice'] = '';
foreach ($products as $product) {
$p = new Product($product['product_id']);
$p->delete();
$this->queue->deleteProduct($product['product_id']);
$products_deleted++;
if ( ( $products_created + $products_updated ) >= self::$update_limit )
{
$json['notice'] = 'time_out';
break;
}
}
$json['products_deleted'] = $products_deleted;
$this->ajaxDie(Tools::jsonEncode($json));
exit;
}
function UR_exists($url){
$headers = @get_headers($url);
return stripos($headers[0],"200 OK") ? true : false;
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 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/afl-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-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@@ -0,0 +1,35 @@
<?php
/*
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 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/afl-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-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;