Blame view

console/controllers/ParserController.php 2.21 KB
2edfb901   Mihail   add PriceWriter a...
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 30.09.2015
   * Time: 14:38
   */
  use yii\console\Controller;
93e39994   Mihail   fixed parser and ...
9
10
11
  use yii\helpers\Console;
  use common\components\PriceWriter;
  use backend\models\ImportersFiles;
2edfb901   Mihail   add PriceWriter a...
12
13
14
15
  
  class ParserController extends Controller{
      public function actionParseCSV ()
      {
93e39994   Mihail   fixed parser and ...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
          \common\components\CustomVarDamp::dumpAndDie(45);
          if( $arr_id_files = Yii::$app->cache->get( 'files_to_parse' ) ) {
              $arr_id_files = json_decode( $arr_id_files );
              foreach ( $arr_id_files as $file_name ) {
                  $file_path = Yii::getAlias('@auto_upload') . '/' . $file_name . '.csv';
                  $config = ['record_id' => $file_name,
                              'importer_id' => ImportersFiles::findOne(['id' => $file_name])->id,
                      'parser_config' => ['keys' => ['DESCR', 'ARTICLE', 'BRAND', 'PRICE', 'BOX']]
                  ];
                  if( $this->parseFileConsole( $file_path, $config ) ){
                      unlink( $file_path );
                      if (isset( $arr_id_files[$file_path] ) ) {
                          unset($arr_id_files[$file_path]);
                      }
                  } else {
                     // Yii::$app->log->
                      // не дошли до конца по этому остаки вернем в кеш
                      Yii::$app->cache->set( 'files_to_parse',json_encode( $arr_id_files ) );
                  }
              }
              if ( !count( $arr_id_files ) ) {
                  Yii::$app->cache->delete( 'files_to_parse' );
              }
          }
2edfb901   Mihail   add PriceWriter a...
40
41
42
43
44
      }
      public function actionParseXML ()
      {
  
      }
93e39994   Mihail   fixed parser and ...
45
46
47
48
49
50
51
      protected function parseFileConsole( $file_path, $configuration ){
          $parser_config = [];
          if ( isset( $configuration['parser_config'] ) ) {
              $parser_config = $configuration['parser_config'];
          }
  
          $data = Yii::$app->multiparser->parse( $file_path, $parser_config );
2edfb901   Mihail   add PriceWriter a...
52
  
93e39994   Mihail   fixed parser and ...
53
54
55
56
57
58
59
60
          $writer = new PriceWriter();
          $writer->configuration = $configuration;
          $writer->data = $data;
          $writer->mode = 1; //console-режим
          if ( $writer->writeDataToDB() ){
              Console::output('It is working');
              return true;
          }
2edfb901   Mihail   add PriceWriter a...
61
  
93e39994   Mihail   fixed parser and ...
62
          return false;
2edfb901   Mihail   add PriceWriter a...
63
64
      }
  }