Blame view

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