Skip to content

Instantly share code, notes, and snippets.

@Fi1osof
Created January 9, 2016 14:09
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 Fi1osof/05159325add272ef3640 to your computer and use it in GitHub Desktop.
Save Fi1osof/05159325add272ef3640 to your computer and use it in GitHub Desktop.
<?php
require_once dirname(dirname(__FILE__)) . '/console.class.php';
class modModimporterImportBusikiConsoleProcessor extends modModimporterImportConsoleProcessor{
public function initialize(){
$this->setDefaultProperties(array(
# Временно
"filename" => basename($this->getProperty("file")),
));
return parent::initialize();
}
protected function StepWriteTmpCategories(){
if(!$reader = & $this->getReader()){
return $this->failure('Не был получен ридер');
}
if(!$filename = $this->getProperty('filename')){
return $this->failure("Не был указан файл");
}
$schema = array(
"yml_catalog" => array(
"shop" => array(
"categories" => array(
"category" => array(
"parse" => true,
),
),
),
),
);
$depth = 0;
$parents = array();
$categoryId = false;
$objects = array();
$result = $reader->read(array(
"file" => $this->getImportPath().$filename,
), function(modImporterXmlReader $reader) use (& $schema, & $depth, & $categoryId, & $objects, & $parents){
$xmlReader = & $reader->getReader();
$node = $reader->getNodeName($xmlReader);
if(
!$reader->isNodeText($xmlReader)
AND $reader->getSchemaNodeByKey($schema, $node)
AND $reader->isNode($node, $xmlReader)
AND isset($schema["parse"]) && $schema["parse"]
){
if($xml = $reader->getXMLNode($xmlReader)){
$attributes = $xml->attributes();
$id = (string)$attributes['id'];
$data = array(
"tmp_parent" => (string)$attributes['parentId'],
"tmp_raw_data" => array(
"pagetitle" => (string)$xml,
),
);
$object = $this->createImportObject($id, $data, "category");
$object->save();
}
else{
$this->modx->log(1, "[".__CLASS__."] Ошибка разбора элемента");
}
$xmlReader->next();
return true;
}
return true;
});
if($result !== true AND $result !== false){
return $this->failure($result);
}
return parent::StepWriteTmpCategories();
}
protected function StepWriteTmpGoods(){
if(!$reader = & $this->getReader()){
return $this->failure('Не был получен ридер');
}
if(!$filename = $this->getProperty('filename')){
return $this->failure("Не был указан файл");
}
$schema = array(
"yml_catalog" => array(
"shop" => array(
"offers" => array(
"offer" => array(
"parse" => true,
),
),
),
),
);
/*
Ограничения по количеству операций за раз
*/
$limit = $this->getProperty("limit", 1000);
$count = 0;
if(!$inserted = (int)$this->getSessionValue("inserted")){
$inserted = 0;
}
$next_step = false;
$depth = 0;
$parents = array();
$categoryId = false;
$objects = array();
$result = $reader->read(array(
"file" => $this->getImportPath().$filename,
), function(modImporterXmlReader $reader) use (& $schema, & $depth, & $categoryId, & $objects, & $parents, $limit, &$count, &$inserted, &$next_step){
$xmlReader = & $reader->getReader();
$node = $reader->getNodeName($xmlReader);
if(
!$reader->isNodeText($xmlReader)
AND $reader->getSchemaNodeByKey($schema, $node)
AND $reader->isNode($node, $xmlReader)
AND isset($schema["parse"]) && $schema["parse"]
){
// Счетчик прочтенных элементов
$count++;
// Если меньше количества прочтенных, идем дальше
if($count <= $inserted){
$xmlReader->next();
return true;
}
if($xml = $reader->getXMLNode($xmlReader)){
$attributes = $xml->attributes();
$id = (string)$attributes['id'];
$data = array(
"tmp_raw_data" => array(
"pagetitle" => (string)$xml->model,
"description" => (string)$xml->description,
"category_id" => (string)$xml->categoryId,
),
);
$object = $this->createImportObject($id, $data, "product");
$object->save();
}
else{
$this->modx->log(1, "[".__CLASS__."] Ошибка разбора элемента");
}
$inserted++;
// Если счетчик достиг лимита, обрываем выполнение.
if($inserted%$limit === 0){
$this->setSessionValue("inserted", $inserted);
$next_step = true;
return false;
}
$xmlReader->next();
return true;
}
return true;
});
if($next_step){
return $this->progress("Прочитано {$inserted} товаров.", null, xPDO::LOG_LEVEL_DEBUG);
}
if($result !== true AND $result !== false){
return $this->failure($result);
}
return parent::StepWriteTmpGoods();
}
}
return 'modModimporterImportBusikiConsoleProcessor';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment