Skip to content

Instantly share code, notes, and snippets.

@andronex
Last active November 15, 2019 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andronex/b7f8b2045612dec115ef3fbfc1faace9 to your computer and use it in GitHub Desktop.
Save andronex/b7f8b2045612dec115ef3fbfc1faace9 to your computer and use it in GitHub Desktop.
Экспорт/импорт товаров в/из файл(а) с закачкой на Я.Диск. Используется компонент msImportExport by @Prihod
<div class="form">
<div class="progress" style="display: none;">
<div class="bar" style="width: 0%;"><span></span></div>
</div>
<a href="#" id="runScript" data-url="yandex/ya_import.php" class="btn" data-action="ajax">Импорт из Яндекс.Диска</a>
</div>
<div class="form">
<div class="progress" style="display: none;">
<div class="bar" style="width: 0%;"><span></span></div>
</div>
<a href="#" id="runScript" data-url="yandex/ya_export.php" class="btn" data-action="ajax">Экспорт в Яндекс.Диск</a>
</div>
<script>
function showProcess (url, sucsess, offset, action, filename, el) {
$('#url, #refreshScript').hide();
el.closest('.form').find('.progress').show();
el.text('Работаем!');
el.closest('.form').find('.bar span').text(offset);
el.closest('.form').find('.bar').css('width', 0.5 * 100 + '%');
scriptOffset(url, offset, action, filename, el);
}
function scriptOffset (url, offset, action, filename, el) {
$.ajax({
url: url,
type: "POST",
data: {
"action":action
, "url":url
, "offset":offset
, "filename":filename
, "token":"d9ee61f842274dcfef532ad0630d1"
},
success: function(data){
data = $.parseJSON(data);
if(data.offset > 1) {
el.closest('.form').find('.bar span').text(data.offset);
scriptOffset(url, data.offset, action, data.filename, el);
//showProcess(url, data.sucsess, data.offset, action, data.filename, el);
} else {
el.closest('.form').find('.bar').css('width','100%');
el.closest('.form').find('.bar span').text(data.message);
el.text('Повторить');
}
}
});
}
document.addEventListener('DOMContentLoaded', function(){
$('.btn').click(function() {
var action = $(this).data('action');
var offset = $('#offset').val();
var url = $(this).data('url');
var el = $(this);
el.closest('.form').find('.progress').show();
el.hide();
el.closest('.form').find('.bar span').text(0);
el.closest('.form').find('.bar').css('width', 0.5 * 100 + '%');
scriptOffset(url, 0, action, '', el);
return false;
});
});
</script>
<style>
input {
font-size: 13px;
margin: 0;
padding: 0 3px;
vertical-align: middle;
border: 1px solid #CCCCCC;
border-radius: 3px 3px 3px 3px;
color: #808080;
display: inline-block;
font-size: 13px;
height: 26px;
line-height: 18px;
width: 243px;
-moz-transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1) inset;
}
.btn {
font-size: 13px;
padding: 5px 8px;
background-color: #70ab7b;
background-image: -moz-linear-gradient(center top , #049CDB, #0064CD);
background-repeat: repeat-x;
border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
color: #FFFFFF;
display: inline-block;
vertical-align: middle;
border-radius: 3px 3px 3px 3px;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
text-decoration: none;
}
.btn:hover {
background-position: 0 -15px;
}
.btn:active {
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25) inset, 0 1px 2px rgba(0, 0, 0, 0.05);
}
.progress {
font-size: 13px;
margin: 0;
vertical-align: middle;
background-color: #fff;
background-image: -moz-linear-gradient(center top , #F5F5F5, #F9F9F9);
background-repeat: repeat-x;
border-radius: 4px 4px 4px 4px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1) inset;
height: 28px;
width: 250px;
overflow: hidden;
display: inline-block;
}
.progress .bar {
background-color: #70ab7b;
background-image: -moz-linear-gradient(center top , #149BDF, #0480BE);
background-size: 40px 40px;
-moz-box-sizing: border-box;
-moz-transition: width 0.6s ease 0s;
background-repeat: repeat-x;
box-shadow: 0 -1px 0 rgba(0, 0, 0, 0.15) inset;
color: #FFFFFF;
float: left;
font-size: 12px;
height: 100%;
text-align: left;
padding: 5px 8px;
font-size: 13px;
text-shadow: 1px 1px #333;
white-space: nowrap;
}
div.form {
margin: 50px 0 0 0;
}
</style>
</div>
<?php
date_default_timezone_set('Europe/Samara');
//echo date("H:i:s").PHP_EOL;
//set_time_limit(0);
//ini_set('max_execution_time', 600);
//echo ini_get('max_execution_time');
//die;
//header("Content-type: text/html; charset=utf-8");
define('MODX_API_MODE', true);
require dirname(dirname(__FILE__)) . '/index.php';
$modx->getService('error','error.modError');
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->setLogTarget('FILE');
//echo '<pre>';
$token = $modx->getOption('msimportexport.token');
//if ( (!isset($_GET['token']) and !isset($_POST['token'])) or (isset($_GET['token']) and $token != $_GET['token']) or (isset($_POST['token']) and $token != $_POST['token'])) die('Incorrect token');
//if (!isset($_POST['action']) and $modx->getOption('msimportexport.cron_on') == 0) die('Auto-cron off');
// ссылка для скачивания свежего экспорта
//$download = MODX_SITE_URL.'assets/components/msimportexport/export.php?to=xlsx&type=products&preset=1&token='.$token;
//$filename = 'export_'.date('d_m_Y_H_i_s');
// сохраним в папку на хостинге
$path = dirname(dirname(__FILE__)).'/assets/components/msimportexport/export/';
//$path_file = $path.$filename;
$ctx = isset($_GET['ctx']) ? $_GET['ctx'] : $modx->getOption('msimportexport.export.ctx', null, 'web', true);
/** @var Msie $msie */
$msie = $modx->getService('msimportexport', 'MsieCustom', $modx->getOption('msimportexport.core_path', null, $modx->getOption('core_path') . 'components/msimportexport/') . 'model/msimportexport/', array());
$msie->initialize($ctx);
$prop = array(
'to' => 'xlsx',
'offset' => 0,
'preset' => 1,
'save' => 1,
'type' => 'products',
'limit' => 50,
'path' => $path
);
while ($prop['offset'] >= 0) {
// если импорт по кнопке, а не по крону
if ($_POST['action'] == 'ajax') {
$prop['offset'] = $_POST['offset'];
$prop['filename'] = $_POST['filename'];
}
if ($prop['offset'] == 0) {
// очищаем папку
$del = dirname(dirname(__FILE__)).'/assets/components/msimportexport/export';
if (file_exists($del)) {
foreach (glob($del.'/*') as $file) {
unlink($file);
}
}
}
//$modx->log(modX::LOG_LEVEL_ERROR, print_r($prop, 1));
$response = $msie->export($prop['to'], $prop['preset'], $prop['save'], $prop['type'], $prop['path'], $prop['filename'], '', '', $prop);
if ($_POST['action'] == 'ajax') {
$rows = $response['rows'] + $_POST['offset'];
if ($response['offset'] == -1) {
exportYa($response);
}
$output = Array('offset' => $response['offset'], 'message' => 'Экспортировано '.$rows, 'filename' => $response['filename']);
echo json_encode($output);
die;
}
$prop['offset'] = $response['offset'];
echo $prop['offset'] . " imported!\n";
ob_flush();
}
exportYa($response);
die;
/*$fp = fopen ($path_file, 'w+');
$curlOptions = array(
CURLOPT_URL => $download,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 900,
CURLOPT_FILE => $fp,
CURLOPT_FOLLOWLOCATION => 1,
);
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
$data = curl_exec($ch);
*/
function exportYa($prop) {
global $modx;
// закачаем на Яндекс.Диск
$ya_token = $modx->getOption('msimportexport.ya_oauth');
$headers = array(
"Host: cloud-api.yandex.net",
"Accept: */*",
"Content-Type: application/json",
"Authorization: OAuth {$ya_token}"
);
$url = 'https://cloud-api.yandex.net:443/v1/disk/resources/upload';
$download_ya = MODX_SITE_URL.'assets/components/msimportexport/export/'.$prop['filename'].'.'.$prop['to'];
$filename = $prop['filename'].'.'.$prop['to'];
$requestParams = array (
'path' => "goods/{$filename}",
'url' => $download_ya
);
$curlOptions = array(
CURLOPT_URL => $url.'?'.http_build_query($requestParams),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => 1
);
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
$data = curl_exec($ch);
$data = $modx->fromJSON($data);
if ($data['href'] != '') {
if ($_POST['action'] == 'ajax') {
//$output = Array('offset' => -1, 'message' => 'Экспортировано на Яндекс.Диск','filename' => $filename);
//echo json_encode($output);
//die;
} else {
echo 'Файл экспорта выгрузился на Яндекс.Диск';
}
}
}
//print_r($data);
die;
<?php
//phpinfo();
//die;
define('MODX_API_MODE', true);
require dirname(dirname(__FILE__)) . '/index.php';
$modx->getService('error','error.modError');
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->setLogTarget('FILE');
$token = $modx->getOption('msimportexport.token');
if ( (!isset($_GET['token']) and !isset($_POST['token'])) or (isset($_GET['token']) and $token != $_GET['token']) or (isset($_POST['token']) and $token != $_POST['token'])) die('Incorrect token');
if (!isset($_POST['action']) and $modx->getOption('msimportexport.cron_on') == 0) return;
$dir = $_SERVER['DOCUMENT_ROOT'].$modx->getOption('msimportexport.upload_path');
//echo '<pre>';
if (($_POST['action'] == 'ajax' and $_POST['offset'] == 0) or !isset($_POST['action'])) {
$url = "https://cloud-api.yandex.net:443/v1/disk/resources?path=goods&sort=modified";
$ya_token = $modx->getOption('msimportexport.ya_oauth');
$headers = array(
"Host: cloud-api.yandex.net",
"Accept: */*",
"Content-Type: application/json",
"Authorization: OAuth {$ya_token}"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
//print_r($data);
$data = $modx->fromJSON($data);
$data = $data['_embedded']['items'];
$file = array_pop($data);
file_put_contents($dir.$file['name'],file_get_contents($file['file']));
}
$fields = array(
'id',
'price',
'article',
'weight',
'pagetitle',
'longtitle',
'introtext',
'content',
'tv81',
'tv82',
'tv83',
'stock',
'vendor',
'made_in',
'tags',
'tv111',
'tv112',
'tv114',
'tv116',
-1,
'parent',
'properties',
'categories',
'tv106',
'from_crm',
'stock_count'
);
//$filename = $dir.'export_11_04_2018_03_04_37.xlsx';
if (!isset($file['name'])) {
$filename = $_POST['filename'];
} else {
$filename = $file['name'];
}
$prop = array('import_type' => 'products', 'filename' => $filename, 'preset' => 2, 'fields' => $fields, 'seek' => 0, 'key' => 'id');
if (!file_exists($dir.$filename)) {
$output = Array('offset' => -1, 'message' => 'Файл не существует '.$filename, 'filename' => $filename);
echo json_encode($output);
die;
}
while ($prop['seek'] >= 0) {
// если импорт по кнопке, а не по крону
if ($_POST['action'] == 'ajax') {
$prop['seek'] = $_POST['offset'];
}
// сброс блокировок ресурсов
$modx->runProcessor('system/remove_locks', array(), array());
$response = $modx->runProcessor('import/import', $prop, array(
'processors_path' => MODX_CORE_PATH . 'components/msimportexport/processors/mgr/'
));
if ($response->isError()) {
$modx->log(modX::LOG_LEVEL_ERROR, print_r($response->getObject(), 1));
//var_dump($response->getObject());
//die;
}
//$modx->log(modX::LOG_LEVEL_ERROR, print_r($response->getObject(), 1));
$obj = $response->getObject();
if ($_POST['action'] == 'ajax') {
$rows = $obj['rows'] + $_POST['offset'];
$output = Array('offset' => $obj['seek'], 'message' => 'Импортировано '.$rows, 'filename' => $filename);
echo json_encode($output);
die;
}
$prop['seek'] = $obj['seek'];
echo $prop['seek'] . " imported!\n";
ob_flush();
}
die;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment