diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index 3a386b8..7ae4546 100644 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -52,8 +52,6 @@ class ParserController extends BaseController public function actionIndex($mode = 0) { -// $path = 'common\components\parsers\CustomConverter'; -// CustomVarDamp::dumpAndDie(new $path()); $model = new UploadFileParsingForm(); // установим режим, 0 - ручная загрузка, 1 - автозагрузка $model->mode = $mode; diff --git a/backend/models/UploadFileParsingForm.php b/backend/models/UploadFileParsingForm.php index aeac2d0..3832072 100644 --- a/backend/models/UploadFileParsingForm.php +++ b/backend/models/UploadFileParsingForm.php @@ -48,7 +48,7 @@ class UploadFileParsingForm extends Model return [ ['importer_id', 'required', 'message' => 'Не указан поставщик!' ], ['file', 'required', 'message' => 'Не выбран файл!' ], - [['file'], 'file', 'extensions' => ['csv', 'xml'], 'checkExtensionByMimeType'=>false ], + [['file'], 'file', 'extensions' => ['csv', 'xlsx'], 'checkExtensionByMimeType'=>false ], ['importer_id', 'integer','max' => 999999, 'min' => 0 ], [['action','delete_prefix', 'delete_price', 'success'], 'boolean', 'except' => 'auto' ], // только для ручной загрузки ['delimiter', 'string', 'max' => 1], @@ -71,7 +71,6 @@ class UploadFileParsingForm extends Model public function readFile( $options = [] ){ $data = Yii::$app->multiparser->parse( $this->file_path, $options ); - CustomVarDamp::dumpAndDie($data); if( !is_array( $data ) || count($data) == 0 ){ throw new ErrorException("Ошибка чтения из файла прайса {$this->file_path}"); } diff --git a/common/components/PriceWriter.php b/common/components/PriceWriter.php index d6b27a5..cb6ab17 100644 --- a/common/components/PriceWriter.php +++ b/common/components/PriceWriter.php @@ -91,7 +91,7 @@ class PriceWriter try { //@todo add transaction - if ((int)$this->configuration['delete_price']) { + if ( isset($this->configuration['delete_price']) && (int)$this->configuration['delete_price'] ) { $details_model->delete_price = true; } //2. попытаемся вставить данные в БД с апдейтом по ключам diff --git a/common/components/parsers/config.php b/common/components/parsers/config.php index 704b901..7e25bdf 100644 --- a/common/components/parsers/config.php +++ b/common/components/parsers/config.php @@ -68,25 +68,15 @@ 'configuration' => ["details" => [] ],], ], - 'web' => - ['class' => 'yii\multiparser\XmlParser', - 'node' => 'Товар', - 'hasHeaderRow' => true, - 'keys' => [ - "BRAND" => 'Производитель', - "ARTICLE"=> 'Код', - "PRICE" => 'Розница', - "DESCR" => 'Наименование', - "BOX" => 'Колво', - "ADD_BOX"=> 'Ожидаемое', - "GROUP" => 'Группа' - ], - 'converter_conf' => [ - 'class' => 'common\components\parsers\CustomConverter', - 'configuration' => ["details" => [] - ],], - ], ], - + 'xlsx' => + ['web' => + ['class' => 'yii\multiparser\XlsxParser', + 'path_for_extract_files' => \Yii::getAlias('@temp_upload') . '/', + 'converter_conf' => [ + 'class' => 'common\components\parsers\CustomConverter', + 'configuration' => ["encode" => 'DESCR'],] + ], + ] ]; diff --git a/console/.gitignore b/console/.gitignore index 16203a8..bb46adf 100644 --- a/console/.gitignore +++ b/console/.gitignore @@ -1 +1,2 @@ -/old_migrations \ No newline at end of file +/old_migrations +/runtime \ No newline at end of file diff --git a/console/controllers/ParserController.php b/console/controllers/ParserController.php index cd04973..4ec57f5 100644 --- a/console/controllers/ParserController.php +++ b/console/controllers/ParserController.php @@ -59,8 +59,7 @@ class ParserController extends Controller } $data = \Yii::$app->multiparser->parse( $file_path, $parser_config ); if ( ! $data ) { - // @todo переделать, что бы ошибка автоматически останавливала сценарий - return false; + throw new ErrorException("Ошибка обработки файла прайса!"); } $writer = new PriceWriter(); @@ -105,7 +104,7 @@ class ParserController extends Controller ]; if ($this->parseFileConsole($file_path, $config)) { - unlink(\Yii::getAlias('@auto_upload') . '/' . $file_name . '.xml'); + //unlink(\Yii::getAlias('@auto_upload') . '/' . $file_name . '.xml'); \Yii::info("Загрузка файла - $file_path успешно завершена", 'parser'); } else { \Yii::error("Загрузка файла - $file_path завершена с ошибкой", 'parser'); diff --git a/console/runtime/.gitignore b/console/runtime/.gitignore index c96a04f..96b05bb 100644 --- a/console/runtime/.gitignore +++ b/console/runtime/.gitignore @@ -1,2 +1,2 @@ -* +/logs !.gitignore \ No newline at end of file diff --git a/vendor/yiisoft/multiparser/CsvParser.php b/vendor/yiisoft/multiparser/CsvParser.php index 0d28a0b..9c9f29c 100644 --- a/vendor/yiisoft/multiparser/CsvParser.php +++ b/vendor/yiisoft/multiparser/CsvParser.php @@ -20,9 +20,6 @@ class CsvParser extends Parser * если не указан и установлено свойство $hasHeaderRow - будет определен автоматически */ // public $keys; - определен в родительском классе - /** @var экземляр SplFileObject читаемого файла */ - public $file; - /** @var int - первая строка с которой начинать парсить */ public $first_line = 0; diff --git a/vendor/yiisoft/multiparser/Parser.php b/vendor/yiisoft/multiparser/Parser.php index 4db7fe1..b07b93b 100644 --- a/vendor/yiisoft/multiparser/Parser.php +++ b/vendor/yiisoft/multiparser/Parser.php @@ -18,6 +18,9 @@ abstract class Parser public $keys = NULL; public $hasHeaderRow = false; + /** @var экземляр SplFileObject читаемого файла */ + public $file; + public function setup() { $this->setupConverter(); diff --git a/vendor/yiisoft/multiparser/XlsxParser.php b/vendor/yiisoft/multiparser/XlsxParser.php new file mode 100644 index 0000000..4c4bc8a --- /dev/null +++ b/vendor/yiisoft/multiparser/XlsxParser.php @@ -0,0 +1,81 @@ +path_for_extract_files == '' ) { + $this->path_for_extract_files = sys_get_temp_dir(); + } + } + + + public function read() + { + //$this->extractFiles(); + CustomVarDamp::dumpAndDie($this->parse()); + + // return $this->parse(); + } + + protected function extractFiles () + { + $zip = new \ZipArchive; + if ( $zip->open( $this->file->getPathname() ) === TRUE ) { + $zip->extractTo( $this->path_for_extract_files ); + $zip->close(); + } else { + throw new \Exception('Ошибка чтения xlsx файла'); + } + } + + protected function parse () + { + $result = []; + // while ($sheet = @readdir($this->file)) { + foreach ( glob($this->path_for_extract_files . '/xl/worksheets/*.xml' ) as $sheet ) { + //проходим по всем файлам из директории /xl/worksheets/ + //CustomVarDamp::dumpAndDie($sheet); + if ($sheet != "." && $sheet != ".." && $sheet != '_rels') { + $xml = simplexml_load_file( $sheet ); + //по каждой строке + $row = 0; + + foreach ( $xml->sheetData->row as $item ) { + $result[$sheet][$row] = array(); + //по каждой ячейке строки + $cell = 0; + foreach ( $item as $child ) { + // $attr = $child->attributes(); + + if( isset($child->v) ) { + $value = (string)$child->v; + }else{ + $value = false; + } + + // $result[$sheet][$row][$cell] = isset($attr['t']) ? $sharedStringsArr[$value] : $value; + $result[$sheet][$row][$cell] = $value; + $cell++; + } + $row++; + } + } + } + return $result; + } +} \ No newline at end of file diff --git a/vendor/yiisoft/multiparser/XmlParser.php b/vendor/yiisoft/multiparser/XmlParser.php index 4a48938..f96840d 100644 --- a/vendor/yiisoft/multiparser/XmlParser.php +++ b/vendor/yiisoft/multiparser/XmlParser.php @@ -13,8 +13,7 @@ use common\components\CustomVarDamp; use common\components\CustomArrayHelper; class XmlParser extends Parser{ - /** @var экземляр SplFileObject читаемого файла */ - public $file; + public $node; public function read() @@ -86,7 +85,6 @@ class XmlParser extends Parser{ // дошли до конца рекурсии // преобразуем ряд согласно конфигурации if ( $this->keys !== NULL ) { - // назначим ключи из конфигурации, согласно массиву $keys $row = $this->compareArrayWithKeys( $row ); } -- libgit2 0.21.4