Blame view

console/controllers/ParserController.php 2.2 KB
4828b892   Mihail   after merge with ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  <?php
  namespace console\controllers;
  
  use yii\console\Controller;
  use yii\helpers\Console;
  use common\components\PriceWriter;
  use backend\models\ImportersFiles;
  use backend\models\Importers;
  use yii\base\ErrorException;
  
  class ParserController extends Controller{
  
      public function actionParseCsv ()
      {
          Console::output('1');
          foreach (glob( \Yii::getAlias('@auto_upload') . '/*.csv' ) as $file_path) {
              $file_name = basename($file_path,".csv");
                  Console::output('2');
                  $importer_id = ImportersFiles::findOne(['id' => $file_name])->importer_id;
                  $keys = Importers::findOne( ['id' => $importer_id] )->keys;
  
                  $config = ['record_id' => $file_name,
                      'importer_id' => $importer_id,
                      'parser_config' => ['keys' => $keys,
                          'mode' => 'console']
                  ];
                  Console::output('3');
                  if( $this->parseFileConsole( $file_path, $config ) ){
                      Console::output('4');
                      unlink( $file_path );
                      if (isset( $arr_id_files[$file_path] ) ) {
                          unset( $arr_id_files[$file_path] );
                      }
                  }
              }
  
  
          //return $this->redirect('serverFiles');
      }
  
      public function actionParseXml ()
      {
  
      }
      protected function parseFileConsole( $file_path, $configuration ){
  
          if( !file_exists( $file_path ) )
              throw new ErrorException("$file_path does not exist!");
  
          $parser_config = [];
          if ( isset( $configuration['parser_config'] ) ) {
              $parser_config = $configuration['parser_config'];
          }
  
          $data = \Yii::$app->multiparser->parse( $file_path, $parser_config );
  
          $writer = new PriceWriter();
          $writer->configuration = $configuration;
          $writer->data = $data;
          $writer->mode = 1; //console-режим
          if ( $writer->writeDataToDB() ){
              Console::output('It is working');
              return true;
          }
  
          return false;
      }
  
      public function actionTest ()
      {
          Console::output('It is working ');
         \Yii::info('2','parser');
  
      }
  }