Skip to content

Instantly share code, notes, and snippets.

@denysbutenko
Created February 3, 2013 01:18
Show Gist options
  • Save denysbutenko/1da20f4a6201d65ec1fc to your computer and use it in GitHub Desktop.
Save denysbutenko/1da20f4a6201d65ec1fc to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
define('MODX_API_MODE', true);
require 'index.php';
// Включаем обработку ошибок
$modx->getService('error','error.modError');
$modx->setLogLevel(modX::LOG_LEVEL_INFO);
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
// Подключаем класс miniShop
$miniShop = $modx->getService('minishop','miniShop',$modx->getOption('minishop.core_path',null,$modx->getOption('core_path').'components/minishop/').'model/minishop/');
if (!($miniShop instanceof miniShop)) return '';
//Загружаем данные из файла
$file = file_get_contents('price.json');
$template = 12;
$category = 95;
$wid = 1;
$newgoods = json_decode($file,true);
echo 'price.json as assoc array decoded.'."\n";
if (!empty($newgoods)) {
$i = 0;
$idx = 0;
foreach($newgoods as $good){
$modx->error->message = null;
$modx->error->errors = array();
$gallery = $tags = array();
$product = array('parent' => $category, 'template' => $template);
foreach($good as $key => $value){
$product[$key] = $value;
}
$ms = array(
'wid' => $wid
,'article' => $product['sku']
,'img' => ' '
,'weight' => 1
,'remains' => 1
,'reserved' => NULL
,'add1' => $product['manufacturer']
,'add2' => $product['price_uah']
,'add3' => $product['url']
);
$tags = explode(' ', $product['pagetitle']);
/*foreach ($config as $k => $v) {
if ($v == 'none') {continue;}
else if (strpos($v, 'ms_') !== false) {$ms[substr($v,3)] = $csv[$k];}
else if (strpos($v, 'tv_') !== false) {$tvs[substr($v,3)] = $csv[$k];}
else if ($v == 'tag') {$tags[] = $csv[$k];}
else {$product[$v] = $csv[$k];}
}*/
//print_r($product);
foreach($good['image_paths'] as $image){
$gallery[] = $image;
}
if ($res = $modx->getObject('modResource', array('parent' => $category, 'pagetitle' => $product['pagetitle']))) {
$id = $res->get('id');
$newproduct = array_merge($res->toArray(), $product, $ms);
$newproduct['tags'] = $tags;
$response = $modx->runProcessor(
'mgr/goods/update'
,$newproduct
,array('processors_path' => MODX_CORE_PATH.'components/minishop/processors/')
);
if ($response->isError()) {
return $modx->error->failure('Error on row '.$i.': '.$response->getMessage());
}
$modx->removeCollection('ModGallery', array('gid' => $id));
$i++;
echo 'Product '.$id.' updated.';
}
else {
$res = $modx->newObject('modResource');
$res->fromArray($product);
$newproduct = array_merge($res->toArray(), $product, $ms);
$newproduct['tags'] = $tags;
$response = $modx->runProcessor(
'mgr/goods/create'
,$newproduct
,array('processors_path' => MODX_CORE_PATH.'components/minishop/processors/')
);
if ($response->isError()) {
return $modx->error->failure('Error on row '.$i.': '.$response->getMessage());
}
else {
$id = $response->response['object']['gid'];
if (!$res = $modx->getObject('modResource', $id)) {
return $modx->error->failure('Error on row '.$i.': '.print_r($response->response['object'],1));
}
echo 'Product '.$id.' updated.';
}
}
//foreach($good['desc'] as $desc){
// foreach($desc as $key => $value){
// if (!empty($value)) {
// $res->setTVValue($key, $value);
// }
// }
//}
foreach ($gallery as $v) {
if (!empty($v)) {
$tmp = pathinfo($v);
//return $modx->error->failure('Error on row '.$i.': '.print_r($tmp,1));
if (empty($tmp['extension'])) {
$response = $modx->runProcessor(
'mgr/goods/gallery/load'
,array('gid' => $id, 'dir' => $wid, 'dir' => $v)
,array('processors_path' => MODX_CORE_PATH.'components/minishop/processors/')
);
}
else {
$response = $modx->runProcessor(
'mgr/goods/gallery/create'
,array('gid' => $id, 'wid' => $wid, 'file' => 'images/'.$v, 'fileorder' => $idx)
,array('processors_path' => MODX_CORE_PATH.'components/minishop/processors/')
);
}
$idx++;
}
}
$i++;
}
echo 'Finished.';
} else {
echo 'Error. Failed decode json file.';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment