Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Утилиты. Интерфейс для импорта #883

Merged
merged 20 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _build/data/transport.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
],

'ms2_utility_import_fields' => [
'value' => 'article,pagetitle,price',
'value' => 'pagetitle,parent,price,article',
'xtype' => 'textfield',
'area' => 'ms2_import',
],
Expand Down
9 changes: 3 additions & 6 deletions core/components/minishop2/controllers/mgr/utilites.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public function getLanguageTopics()
*/
public function loadCustomCssJs()
{

$this->addCss($this->miniShop2->config['cssUrl'] . 'mgr/utilites/gallery.css');

$this->addJavascript($this->miniShop2->config['jsUrl'] . 'mgr/minishop2.js');
$this->addJavascript($this->miniShop2->config['jsUrl'] . 'mgr/utilites/panel.js');
$this->addJavascript($this->miniShop2->config['jsUrl'] . 'mgr/utilites/gallery/panel.js');
Expand All @@ -43,7 +41,7 @@ public function loadCustomCssJs()
$config['utility_gallery_source_id'] = $productSource;
$config['utility_gallery_source_name'] = $source->get('name');

$properties = $source->get('properties');
$properties = $source->get('properties');
$propertiesString = '';
foreach (json_decode($properties['thumbnails']['value'], true) as $key => $value) {
$propertiesString .= "<strong>$key: </strong>" . json_encode($value) . "<br>";
Expand All @@ -55,14 +53,13 @@ public function loadCustomCssJs()
$config['utility_gallery_total_products'] = $this->modx->getCount('msProduct', ['class_key' => 'msProduct']);
$config['utility_gallery_total_products_files'] = $this->modx->getCount('msProductFile', ['parent' => 0]);

// get params for import
$config['utility_import_fields'] = $this->getOption('ms2_utility_import_fields', null, 'article,pagetitle,price', true);
// get params for import
$config['utility_import_fields'] = $this->getOption('ms2_utility_import_fields', null, 'pagetitle,parent,price,article', true);
$config['utility_import_fields_delimiter'] = $this->getOption('ms2_utility_import_fields_delimiter', null, ';', true);

$this->addHtml(
'<script>
miniShop2.config = ' . json_encode($config) . ';

Ext.onReady(function() {
MODx.add({xtype: "minishop2-utilites"});
});
Expand Down
222 changes: 222 additions & 0 deletions core/components/minishop2/import/importCSV.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php

class ImportCSV
{
/**
* @var modX
*/
private $modx;

public function __construct(modX $modx)
{
$this->modx = $modx;
// Time limit
set_time_limit(600);
$tmp = 'Trying to set time limit = 600 sec: ';
$tmp .= ini_get('max_execution_time') == 600 ? 'done' : 'error';
$this->modx->log(modX::LOG_LEVEL_INFO, $tmp);
}

public function process($params)
{
$file = @$params['file'];
$fields = @$params['fields'];
$update = !empty($params['update']);
$key = @$params['key'];
$is_debug = !empty($params['debug']);
$delimeter = @$params['delimeter'];


// Check required options
if (empty($fields)) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'You must specify the parameter "fields". It needed for parse of your file.');
return false;
}
if (empty($key)) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'You must specify the parameter "key". It needed for check for duplicates.');
return false;
}

$keys = array_map('trim', explode(',', strtolower($fields)));
$tv_enabled = false;
foreach ($keys as $v) {
if (preg_match('/^tv(\d+)$/', $v)) {
$tv_enabled = true;
break;
}
}
if (empty($delimeter)) {
$delimeter = ';';
}

// Check file
if (empty($file)) {
$error = 'You must specify an file in the ';
$error .= XPDO_CLI_MODE ? 'first parameter of console call' : '$_GET["file"] parameter';
$error .= '!';
$this->modx->log(modX::LOG_LEVEL_ERROR, $error);
return false;
} elseif (!preg_match('/\.csv$/i', $file)) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Wrong file extension. File must be an *.csv.');
return false;
}

$file = str_replace('//', '/', MODX_BASE_PATH . $file);
if (!file_exists($file)) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'File not found at ' . $file . '.');
return false;
}

// Import!
$handle = fopen($file, "r");
$rows = $created = $updated = 0;

while (($csv = fgetcsv($handle, 0, $delimeter)) !== false) {
$rows++;
$data = $gallery = [];
$this->modx->error->reset();
$this->modx->log(modX::LOG_LEVEL_INFO, "Raw data for import: \n" . print_r($csv, 1));

foreach ($keys as $k => $v) {
if (!isset($csv[$k])) {
exit('Field "' . $v . '" not exists in file. Please fix import file or parameter "fields".');
}
if ($v == 'gallery') {
$gallery[] = $csv[$k];
} elseif (isset($data[$v]) && !is_array($data[$v])) {
$data[$v] = [$data[$v], $csv[$k]];
} elseif (isset($data[$v])) {
$data[$v][] = $csv[$k];
} else {
$data[$v] = $csv[$k];
}
}
$is_product = false;

// Set default values
if (empty($data['class_key'])) {
$data['class_key'] = 'msProduct';
}
if (empty($data['context_key'])) {
if (isset($data['parent']) && $parent = $this->modx->getObject('modResource', ['id' => $data['parent']])) {
$data['context_key'] = $parent->get('context_key');
} elseif (isset($this->modx->resource) && isset($this->modx->context)) {
$data['context_key'] = $this->modx->context->key;
} else {
$data['context_key'] = 'web';
}
}
$data['tvs'] = $tv_enabled;
$this->modx->log(modX::LOG_LEVEL_INFO, "Array with importing data: \n" . print_r($data, 1));

// Duplicate check
$q = $this->modx->newQuery($data['class_key']);
$q->select($data['class_key'] . '.id');
if (strtolower($data['class_key']) === 'msproduct') {
$q->innerJoin('msProductData', 'Data', $data['class_key'] . '.id = Data.id');
$is_product = true;
}
$tmp = $this->modx->getFields($data['class_key']);
if (isset($tmp[$key])) {
$q->where([$key => $data[$key]]);
} elseif ($is_product) {
$q->where(['Data.' . $key => $data[$key]]);
}
$q->prepare();
$this->modx->log(modX::LOG_LEVEL_INFO, "SQL query for check for duplicate: \n" . $q->toSql());

/** @var modResource $exists */
if ($exists = $this->modx->getObject($data['class_key'], $q)) {
$this->modx->log(modX::LOG_LEVEL_INFO, "Key $key = $data[$key] has duplicate.");
if (!$update) {
$this->modx->log(modX::LOG_LEVEL_ERROR, "Skipping line with $key = \"$data[$key]\" because update is disabled.");
if ($is_debug) {
$this->modx->log(
modX::LOG_LEVEL_INFO,
'You in debug mode, so we process only 1 row. Time: ' . number_format(
microtime(true) - $this->modx->startTime,
7
) . " s"
);
return true;
} else {
continue;
}
} else {
$action = 'update';
$data['id'] = $exists->id;
}
} else {
$action = 'create';
}

$this->modx->log(1, print_r([
$action, $data
], 1));

// Create or update resource
/** @var modProcessorResponse $response */
$response = $this->modx->runProcessor('resource/' . $action, $data);
if ($response->isError()) {
$this->modx->log(modX::LOG_LEVEL_ERROR, "Error on $action: \n" . print_r($response->getAllErrors(), 1));
} else {
if ($action == 'update') {
$updated++;
} else {
$created++;
}

$resource = $response->getObject();
$this->modx->log(modX::LOG_LEVEL_INFO, "Successful $action: \n" . print_r($resource, 1));

// Process gallery images, if exists
if (!empty($gallery)) {
$this->modx->log(modX::LOG_LEVEL_INFO, "Importing images: \n" . print_r($gallery, 1));
foreach ($gallery as $v) {
if (empty($v)) {
continue;
}
$image = str_replace('//', '/', MODX_BASE_PATH . $v);
if (!file_exists($image)) {
$this->modx->log(
modX::LOG_LEVEL_ERROR,
"Could not import image \"$v\" to gallery. File \"$image\" not found on server."
);
} else {
$response = $this->modx->runProcessor(
'gallery/upload',
['id' => $resource['id'], 'name' => $v, 'file' => $image],
['processors_path' => MODX_CORE_PATH . 'components/minishop2/processors/mgr/']
);
if ($response->isError()) {
$this->modx->log(
modX::LOG_LEVEL_ERROR,
"Error on upload \"$v\": \n" . print_r($response->getAllErrors(), 1)
);
} else {
$this->modx->log(
modX::LOG_LEVEL_INFO,
"Successful upload \"$v\": \n" . print_r($response->getObject(), 1)
);
}
}
}
}
}

if ($is_debug) {
$this->modx->log(
modX::LOG_LEVEL_INFO,
'You in debug mode, so we process only 1 row. Time: ' . number_format(
microtime(true) - $this->modx->startTime,
7
) . " s"
);
return true;
}
}
fclose($handle);

return true;
}
}
3 changes: 3 additions & 0 deletions core/components/minishop2/lexicon/ru/manager.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,6 @@
$_lang['ms2_weight'] = 'Вес';
$_lang['ms2_weight_price'] = 'Стоимость ед/вес';
$_lang['ms2_weight_price_help'] = 'Добавочная стоимость доставки за единицу веса. <br>Может быть использовано в кастомных классах.';
$_lang['ms2_utilites_scheduler_nf'] = 'У вас не установлен компонент Scheduler';
webinmd marked this conversation as resolved.
Show resolved Hide resolved
$_lang['ms2_utilites_scheduler_task_ce'] = 'Не удалось создать задание Scheduler';
$_lang['ms2_utilites_scheduler_success'] = 'Задание Scheduler создано';
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once dirname(__FILE__, 5) . '/import/importCSV.php';

/**
* Simple Import
* import products
Expand All @@ -19,7 +21,6 @@ class msUtilityImportProcessor extends modProcessor
public $miniShop;



/**
* @return bool|null|string
*/
Expand Down Expand Up @@ -49,7 +50,6 @@ public function getLanguageTopics()
*/
public function process()
{

$required = ['importfile', 'fields', 'delimiter'];

foreach ($required as $field) {
Expand All @@ -58,8 +58,6 @@ public function process()
}
}

$url = $this->modx->getOption('site_url') . 'core/components/minishop2/import/csv.php';

$importParams = [
'file' => $this->properties['importfile'],
'fields' => $this->properties['fields'],
Expand All @@ -69,17 +67,58 @@ public function process()
'delimiter' => $this->properties['delimiter']
];

$url .= '?' . http_build_query($importParams);
$scheduler = $this->getProperty('scheduler', 0);
if (empty($scheduler)) {
$importCSV = new ImportCSV($this->modx);
$result = $importCSV->process($importParams);
return $this->success($result, [
'message' => $result
]);
}


/** @var Scheduler $scheduler */
$path = $this->modx->getOption(
'scheduler.core_path',
null,
$this->modx->getOption('core_path') . 'components/scheduler/'
);
$scheduler = $this->modx->getService('scheduler', 'Scheduler', $path . 'model/scheduler/');
if (!$scheduler) {
$this->modx->log(1, 'not found Scheduler extra');
return $this->failure('ms2_utilites_scheduler_nf', []);
}
$task = $scheduler->getTask('minishop2', 'ms2_csv_import');
if (!$task) {
$task = $this->createImportTask();
}
if (empty($task)) {
return $this->failure('ms2_utilites_scheduler_task_ce', []);
}

$task->schedule('+1 second', $importParams);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $this->success('ms2_utilites_scheduler_success');
}

return $this->success($result, [
'message' => $result
/**
* Creating Sheduler's task for start import
* @return false|object|null
*/
private function createImportTask()
{
$task = $this->modx->newObject('sFileTask');
$task->fromArray([
'class_key' => 'sFileTask',
'content' => '/tasks/csvImport.php',
'namespace' => 'minishop2',
'reference' => 'ms2_csv_import',
'description' => 'MiniShop2 CSV import'
]);
if (!$task->save()) {
return false;
}
return $task;
}
}

Expand Down
14 changes: 14 additions & 0 deletions core/components/minishop2/tasks/csvImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/** @var modX $modx */
/** @var sFileTask $task */
/** @var sTaskRun $run */
/** @var array $scriptProperties */

require_once $modx->getOption('modx_core_path') . 'components/minishop2/import/importCSV.php';
$importCSV = new ImportCSV($this->modx);
$result = $importCSV->process($scriptProperties);

if (!$result) {
$run->addError('csv import error');
}