Files
interblue.pl/modules/pshowimporter/controllers/admin/PShowImporterImportController.php
2024-10-25 14:16:28 +02:00

515 lines
21 KiB
PHP

<?php
/**
* File from http://PrestaShow.pl
*
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @authors PrestaShow.pl <kontakt@prestashow.pl>
* @copyright 2015 PrestaShow.pl
* @license http://PrestaShow.pl/license
*/
require_once dirname(__FILE__) . "/../../config.php";
class PShowImporterImportController extends PShowAdminController
{
public $default_action = 'index';
public $select_menu_tab = 'subtab-PShowImporterMain';
public $return = false;
public static $filename;
public static $config;
public function __construct()
{
parent::__construct();
define('IMPORTING_NOW', true);
// prevent from logging out
if (class_exists('Employee') && method_exists('Employee', 'setLastConnectionDate')) {
Employee::setLastConnectionDate($this->context->employee->id);
}
unset(Context::getContext()->cookie->last_activity);
$this->controller_displayName = $this->l('Sample Controller');
$this->addCSS(__PS_BASE_URI__ . 'modules/pshowimporter/views/css/import.css');
if (!file_exists(_IMPORT_STATUS_PATH_ . "filename")) {
self::$filename = Tools::getValue('file');
if (!self::$filename) {
self::$filename = PShow_Queue::getInstance()->getFromQueue();
}
if (!self::$filename) {
PShow_Log::addGlobal(
"Not found filename to import in URL and in queue"
);
@unlink(getModulePath(__FILE__) . 'autostart_import');
$this->return = $this->backToFilesList(1);
return;
}
$this->filepath = false;
$pathes = glob(_MODULE_UPLOAD_PATH_ . self::$filename . '.xml');
foreach ($pathes as $path) {
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), explode(',', _IMPORT_FILE_EXTENSIONS_)))
continue;
$this->filepath = $path;
break;
}
if (!$this->filepath) {
PShow_Log::addGlobal(
"Not found file to import by filename: " . self::$filename
);
$this->return = $this->backToFilesList(2);
return;
}
} else {
self::$filename = PShow_File::file_get_contents(_IMPORT_STATUS_PATH_ . 'filename');
$this->filepath = false;
$pathes = glob(_MODULE_UPLOAD_PATH_ . self::$filename . '.xml');
foreach ($pathes as $path) {
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), explode(',', _IMPORT_FILE_EXTENSIONS_)))
continue;
$this->filepath = $path;
break;
}
if (!$this->filepath) {
PShow_Log::addGlobal(
"Not found file to import by filename: " . self::$filename
);
@unlink(getModulePath(__FILE__) . 'autostart_import');
$this->return = $this->backToFilesList(3);
return;
}
}
$this->addCSS(__PS_BASE_URI__ . 'modules/pshowimporter/views/css/full-screen-loader.css');
}
public function backToFilesList($param)
{
if (Tools::getValue('ajax')) {
die('importFinished');
}
Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterMain', true));
}
public function indexAction()
{
$this->action_displayName = $this->l('Informations');
if ($this->return) {
return $this->return;
}
Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true) . "&page=prepare&file=" . self::$filename);
}
public function stopAction()
{
PShow_Import::getInstance(self::$filename);
if (PShow_Import::getInstance() instanceof PShow_Import) {
if (PShow_Config::isChecked('gen_combinations', true) && file_exists(_IMPORT_STATUS_PATH_ . 'imported_products') && count(array_filter(explode(',', file_get_contents(_IMPORT_STATUS_PATH_ . 'imported_products'))))) {
// generate combinations for imported products
return Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true) . '&page=gencomb');
} else {
PShow_Import::getInstance()->disableProductsNonExistingInTheFile();
PShow_Import::getInstance()->resetQuantityForNonExistingInTheFile();
PShow_Import::getInstance()->fixCategoriesDepth();
array_map("unlink", glob(_IMPORT_STATUS_PATH_ . "*"));
if (file_exists(getModulePath(__FILE__) . "direct_import_now")) {
unlink(getModulePath(__FILE__) . "direct_import_now");
}
}
}
PShow_Log::add(_IMPORT_LOG_PATH_ . self::$filename . ".log", PShow_Log::IMPORT_STOP);
Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterMain', true));
}
public static function isSomethingToImport()
{
if (!file_exists(_IMPORT_STATUS_PATH_ . "actual_row")) {
return false;
}
if (PShow_Config::get('rowsCount') === false) {
return false;
}
$rowsCount = PShow_Config::get('rowsCount');
$actualRow = (int) PShow_File::file_get_contents(_IMPORT_STATUS_PATH_ . "actual_row");
return (($rowsCount - $actualRow) > 0);
}
public function prepareAction()
{
if ($this->return) {
return $this->return;
}
PShow_Import::getInstance(self::$filename);
if (!(PShow_Import::getInstance() instanceof PShow_Import)) {
PShow_Log::addGlobal(
"Errors during import preparing..."
);
}
Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true) . "&page=import");
}
public function getstatusAction()
{
$data = Tools::jsonEncode(array(
'filename' => self::$filename,
'actual_row' => PShow_Import::getInstanceOfRunningImport()->status['actual_row'],
'first_line' => 1,
'counter_imported_rows' => PShow_Import::getInstanceOfRunningImport()->status['counter_imported_rows'],
'counter_error_rows' => PShow_Import::getInstanceOfRunningImport()->status['counter_error_rows'],
'counter_skipped_rows' => PShow_Import::getInstanceOfRunningImport()->status['counter_skipped_rows'],
'isSomethingToImport' => (bool) self::isSomethingToImport(),
'error' => array(), //$alerts,
'importedThisTime' => 0, //(isset($imported_rows) ? $imported_rows : 0),
'time' => 0, //round((getmicrotime() - $start_time), 2),
// 'memory_usage' => $memory_usage,
// 'cpu_usage' => ($cpu_usage[0]) . '%',
//'memory_limit' => (ini_get('memory_limit')),
));
die($data);
}
public function getlogAction()
{
$filepath = _IMPORT_LOG_PATH_ . self::$filename . '.log';
die(PShow_File::getLogFile($filepath));
}
public function gencombAction()
{
$this->action_displayName = "Generating combinations";
PShow_Import::getInstance(self::$filename);
$imported_products = file_get_contents(_IMPORT_STATUS_PATH_ . 'imported_products');
$imported_products = explode(',', $imported_products);
$imported_products = array_map('intval', $imported_products);
if (count($imported_products) <= 0) {
unlink(_IMPORT_STATUS_PATH_ . 'imported_products');
} else if (Tools::getValue('import')) {
$id_product = array_pop($imported_products);
PShow_Combination_Generator::getInstance()->generateCombinationsForProduct($id_product);
file_put_contents(_IMPORT_STATUS_PATH_ . 'imported_products', implode(',', $imported_products));
}
$data = array(
'numberOfPendingProducts' => count($imported_products),
'filename' => self::$filename
);
if (Tools::getValue('ajax')) {
die(Tools::jsonEncode($data));
}
$this->context->smarty->assign($data);
}
public function setMedia($isNewTheme = false)
{
parent::setMedia();
$this->addCSS(__PS_BASE_URI__ . 'modules/pshowimporter/views/css/font-awesome.min.css');
$this->addCSS(__PS_BASE_URI__ . 'modules/pshowimporter/views/css/import_ps15.css');
$this->addJS(__PS_BASE_URI__ . 'modules/pshowimporter/views/js/import_ps15.js');
}
public function importAction()
{
$this->alerts[] = array('warning', $this->l('Import some first rows and check is it correct imported before importing whole file'));
/**
* Queue support
*/
$files_list = PShow_File::getFilesList();
$queue = PShow_Queue::getInstance()->getQueue();
$queue_list = array();
foreach ($queue as $filename) {
foreach ($files_list as $file) {
if ($file['time'] . '-' . $file['filename'] == $filename) {
array_push($queue_list, $file);
break;
}
}
}
$this->context->smarty->assign('import_queue', $queue);
$this->context->smarty->assign('queue_list', $queue_list);
$this->context->smarty->assign('autostart_import', file_exists(getModulePath(__FILE__) . 'autostart_import'));
$this->action_displayName = $this->l('Informations');
if (isset($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], 'gencomb') !== false) {
$this->alerts[] = array('success', 'Combinations generated successfully...');
}
if (Shop::getTotalShops() > 1 && !class_exists('PShow_Import_Object_Multistore')) {
$this->alerts[] = array(
'danger',
$this->l('You are using multistore.')
. ' <b>' . $this->l('The module may operate incorrectly.') . '</b> '
. $this->l('For a special addition to the multistore options, go to this address')
. ': <a href="http://prestashow.pl/pl//26-.html">http://prestashow.pl/pl//26-.html</a>'
);
}
global $smarty, $start_time;
$this->timer = $start_time;
if ($this->return) {
return $this->return;
}
if (!(PShow_Import::getInstance() instanceof PShow_Import)) {
if (!(PShow_Import::getInstance(self::$filename) instanceof PShow_Import)) {
PShow_Log::addGlobal("Unable to start import: " . (string) self::$filename);
return;
}
}
if (Tools::getValue('clearcounter')) {
PShow_Import::getInstance()->prepareImport(self::$filename);
Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true) . "&page=import");
return;
}
PShow_Import::getInstance()->refreshStatus();
$smarty->assign('number_of_threads', (int) PShow_Import::getInstance()->config[0]['number_of_threads']);
if (PShow_Config::isChecked('setZeroForNonExisting', true)) {
$this->alerts[] = array('warning', $this->l('You have selected to reset the status of products not existing in the file.')
. ' <strong>' . $this->l('If you stop the import before the end, products not yet imported will be treated as non-existing in the file.') . '</strong>');
}
if (PShow_Config::isChecked('disableNonExisting', true)) {
$this->alerts[] = array('warning', $this->l('You have selected to disable products not existing in the file.')
. ' <strong>' . $this->l('If you stop the import before the end, products not yet imported will be treated as non-existing in the file.') . '</strong>');
}
$alerts = array();
$alert = false;
$smarty->assign('firstTimeStart', (int) (PShow_Import::getInstance()->status['actual_row'] + 1 == PShow_Import::getInstance()->config[0]['firstLine']));
if (!self::isSomethingToImport()) {
if (PShow_Config::isChecked('gen_combinations', true) && file_exists(_IMPORT_STATUS_PATH_ . 'imported_products') && count(array_filter(explode(',', file_get_contents(_IMPORT_STATUS_PATH_ . 'imported_products'))))) {
// generate combinations for imported products
if (Tools::getValue('ajax')) {
die('genCombination');
}
return Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true) . '&page=gencomb');
} else {
PShow_Import::getInstance()->disableProductsNonExistingInTheFile();
PShow_Import::getInstance()->resetQuantityForNonExistingInTheFile();
PShow_Import::getInstance()->fixCategoriesDepth();
$smarty->assign("alert", array('success', $this->l('Import finished')));
if (Tools::getValue('clear')) {
PShow_Log::add(_IMPORT_LOG_PATH_ . self::$filename . ".log", PShow_Log::IMPORT_FINISH);
PShow_Import::getInstance()->clearStatus();
}
if (count($queue_list)) {
file_put_contents(getModulePath(__FILE__) . 'autostart_import', '1');
if (Tools::getValue('ajax')) {
die('refresh_now');
}
return Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true));
}
}
} elseif (Tools::getValue('importfirst')) {
file_put_contents(_IMPORT_STATUS_PATH_ . "actual_row", 0);
$import = $this->importRow();
if ($import === true) {
$alert = array('success', 'Row imported successfully');
} else {
$alert = array('warning', $import);
}
PShow_Import::getInstance()->refreshStatus();
} elseif (Tools::getValue('skipobject')) {
file_put_contents(_IMPORT_STATUS_PATH_ . "actual_row", ((int) PShow_Import::getInstance()->status['actual_row'] + ((int) Tools::getValue('skipobject'))));
if ((int) Tools::getValue('skipobject')) {
file_put_contents(_IMPORT_STATUS_PATH_ . "counter_skipped_rows", PShow_File::file_get_contents(_IMPORT_STATUS_PATH_ . "counter_skipped_rows") + ((int) Tools::getValue('skipobject')));
}
$alert = array('success', 'Row skipped successfully');
PShow_Import::getInstance()->refreshStatus();
} elseif (Tools::getValue('importnextrow')) {
//PShow_Import::getInstance()->disableProductsNonExistingInTheFile();
if (Tools::getValue('ajax')) {
$imported_rows = 0;
$rows_to_import = PShow_Import::getInstance()->config[0]['rowsCount'] - PShow_Import::getInstance()->status['actual_row'];
do {
PShow_Import::getInstance()->refreshStatus();
$rows_to_import = PShow_Import::getInstance()->config[0]['rowsCount'] - PShow_Import::getInstance()->status['actual_row'];
++$imported_rows;
$import = $this->importRow();
if ($import !== true) {
$alerts[] = $import;
}
if ((int) PShow_Import::getInstance()->config[0]['number_of_threads'] > 1) {
break;
}
} while ($this->mod_settings['ajax_import_onebyone'] !== "1" && (microtime(true) - $start_time) < _IMPORT_MAX_AJAX_REQUEST_TIME_ && ($rows_to_import - 1) > 0);
} else {
$import = $this->importRow();
if ($import === true) {
$alert = array('success', 'Row imported successfully');
} else {
$alert = array('warning', $import);
}
}
PShow_Import::getInstance()->refreshStatus();
// disable maintenance mode
if ((bool) Configuration::get('pshowimporter_maintenance_mode') && PShow_Import::getInstance()->maintenance_mode_enabled) {
PShow_Log::addImportLog("disabled maintenance mode");
Configuration::updateValue('PS_SHOP_ENABLE', '1');
}
}
if (!self::isSomethingToImport()) {
if (PShow_Config::isChecked('gen_combinations', true) && file_exists(_IMPORT_STATUS_PATH_ . 'imported_products') && count(array_filter(explode(',', file_get_contents(_IMPORT_STATUS_PATH_ . 'imported_products'))))) {
// generate combinations for imported products
if (Tools::getValue('ajax')) {
die('genCombination');
}
return Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true) . '&page=gencomb');
} else {
PShow_Import::getInstance()->disableProductsNonExistingInTheFile();
PShow_Import::getInstance()->resetQuantityForNonExistingInTheFile();
PShow_Import::getInstance()->fixCategoriesDepth();
PShow_Import::getInstance()->clearStatus();
$smarty->assign("alert", array('success', $this->l('Import finished')));
if (count($queue_list)) {
file_put_contents(getModulePath(__FILE__) . 'autostart_import', '1');
if (Tools::getValue('ajax')) {
die('refresh_now');
}
return Tools::redirectAdmin($this->context->link->getAdminLink('PShowImporterImport', true));
}
}
}
$isSomethingToImport = self::isSomethingToImport();
if (Tools::getValue('ajax')) {
PShow_Log::closeAllFiles();
// $size = memory_get_usage();
// $size = max(0, (int) $size);
// $units = array('B', 'KB', 'MB', 'GB', 'TB');
// $power = $size > 0 ? floor(log($size, 1024)) : 0;
// $memory_usage = number_format($size / pow(1024, $power), 2, '.', ',') . $units[$power];
//
// $cpu_usage = sys_getloadavg();
$data = Tools::jsonEncode(array(
//'filename' => self::$filename,
'actual_row' => PShow_Import::getInstance()->status['actual_row'],
//'first_line' => 1,
//'counter_imported_rows' => PShow_Import::getInstance()->status['counter_imported_rows'],
//'counter_error_rows' => PShow_Import::getInstance()->status['counter_error_rows'],
//'counter_skipped_rows' => PShow_Import::getInstance()->status['counter_skipped_rows'],
//'isSomethingToImport' => (bool) $isSomethingToImport,
'error' => $alerts,
'importedThisTime' => (isset($imported_rows) ? $imported_rows : 0),
'time' => round((getmicrotime() - $start_time), 2),
// 'memory_usage' => $memory_usage,
// 'cpu_usage' => ($cpu_usage[0]) . '%',
//'memory_limit' => (ini_get('memory_limit'))
));
//$data = Tools::jsonEncode(array());
die($data);
}
if (!array_key_exists('firstLine', PShow_Import::getInstance()->config[0]))
PShow_Import::getInstance()->config[0]['firstLine'] = 1;
if ($alert)
$smarty->assign('alert', $alert);
if (PShow_Import::getInstance()->status['actual_row'] == (PShow_Import::getInstance()->config[0]['firstLine'] - 1))
PShow_Import::getInstance()->status['actual_row'] = 0;
$smarty->assign('filename', self::$filename);
$smarty->assign('importStatus', PShow_Import::getInstance()->status);
$smarty->assign('first_line', PShow_Import::getInstance()->config[0]['firstLine']);
$smarty->assign('log_dir', _IMPORT_LOG_PATH_ . self::$filename);
$smarty->assign('log_content', PShow_File::getLogFile(_IMPORT_LOG_PATH_ . self::$filename . '.log'));
$smarty->assign('isSomethingToImport', $isSomethingToImport);
$smarty->assign('rowsCount', PShow_Import::getInstance()->config[0]['rowsCount']);
$smarty->assign('cached_rows', 0);
// (int) file_get_contents(_IMPORT_STATUS_PATH_.'cached_from_file'));
if (Tools::getValue('debug') !== false) {
echo '<style>.page-head,header{display: none !important;}</style>';
echo '<div style="background:#E08F95;color:#fff;padding:20px;">'
. '<strong>OBJECT IMPORT DEBUG:</strong><br><br>'
. implode('<br><br>', PShow_Import::$LOG) . '</div>';
}
PShow_Log::closeAllFiles();
}
protected function importRow()
{
$import = PShow_Import::getInstance();
return $import->importToDatabase();
}
}