Blame view

console/controllers/ParserController.php 2.21 KB
f0dbd829   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;
1fe29bbe   Mihail   fixed parser and ...
9
10
11
  use yii\helpers\Console;
  use common\components\PriceWriter;
  use backend\models\ImportersFiles;
f0dbd829   Mihail   add PriceWriter a...
12
13
14
15
  
  class ParserController extends Controller{
      public function actionParseCSV ()
      {
1fe29bbe   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' );
              }
          }
f0dbd829   Mihail   add PriceWriter a...
40
41
42
43
44
      }
      public function actionParseXML ()
      {
  
      }
1fe29bbe   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 );
f0dbd829   Mihail   add PriceWriter a...
52
  
1fe29bbe   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;
          }
f0dbd829   Mihail   add PriceWriter a...
61
  
1fe29bbe   Mihail   fixed parser and ...
62
          return false;
f0dbd829   Mihail   add PriceWriter a...
63
64
      }
  }