Blame view

backend/controllers/ParserController.php 3.29 KB
3cf42f5c   Mihail   init commit - bas...
1
2
3
4
5
  <?php
  namespace backend\controllers;
  
  use Yii;
  use yii\filters\AccessControl;
693c46cb   Mihail   add base classes ...
6
  use backend\components\base\BaseController;
3cf42f5c   Mihail   init commit - bas...
7
  use yii\filters\VerbFilter;
b13b1c83   Mihail   final version par...
8
  use backend\models\UploadFileParsingForm;
3cf42f5c   Mihail   init commit - bas...
9
  use yii\web\UploadedFile;
fcd9278e   Mihail   parser csv v1
10
  use yii\data\ArrayDataProvider;
500b481a   Administrator   JSON
11
  
e774f057   Mihail   work with custome...
12
  use common\components\CustomVarDamp;
3cf42f5c   Mihail   init commit - bas...
13
14
  
  /**
b13b1c83   Mihail   final version par...
15
   * Parser controller
3cf42f5c   Mihail   init commit - bas...
16
   */
b13b1c83   Mihail   final version par...
17
  
693c46cb   Mihail   add base classes ...
18
  class ParserController extends BaseController
3cf42f5c   Mihail   init commit - bas...
19
  {
500b481a   Administrator   JSON
20
      public $layout = "/column";
3cf42f5c   Mihail   init commit - bas...
21
22
23
24
25
26
27
28
29
30
      /**
       * @inheritdoc
       */
      public function behaviors()
      {
          return [
              'access' => [
                  'class' => AccessControl::className(),
                  'rules' => [
                      [
e774f057   Mihail   work with custome...
31
                          'actions' => ['index','results','write'],
3cf42f5c   Mihail   init commit - bas...
32
33
34
35
36
                          'allow' => true,
                          'roles' => ['@'],
                      ],
                  ],
              ],
e774f057   Mihail   work with custome...
37
38
39
40
41
42
  //            'verbs' => [
  //                'class' => VerbFilter::className(),
  //                'actions' => [
  //                    'logout' => ['post'],
  //                ],
  //            ],
3cf42f5c   Mihail   init commit - bas...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function actions()
      {
          return [
              'error' => [
                  'class' => 'yii\web\ErrorAction',
              ],
          ];
      }
  
500b481a   Administrator   JSON
58
59
  
  
3cf42f5c   Mihail   init commit - bas...
60
61
      public function actionIndex()
      {
77422ce3   Mihail   edit upload form
62
          $model = new UploadFileParsingForm();
3cf42f5c   Mihail   init commit - bas...
63
  
500b481a   Administrator   JSON
64
65
66
67
68
69
70
          return $this->render('index', ['model' => $model]);
      }
  
      public function actionResults(){
  
          $model = new UploadFileParsingForm();
  
b13b1c83   Mihail   final version par...
71
          if ($model->load(Yii::$app->request->post())) {
3cf42f5c   Mihail   init commit - bas...
72
73
              $model->file = UploadedFile::getInstance($model, 'file');
  
3663f570   Mihail   draft commit
74
75
              if ($model->validate()) {
                  //CustomVarDamp::dumpAndDie(Yii::$app->request->post());
fcd9278e   Mihail   parser csv v1
76
                  $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension;
500b481a   Administrator   JSON
77
  
fcd9278e   Mihail   parser csv v1
78
                  $model->file->saveAs( $filePath );
500b481a   Administrator   JSON
79
80
                  $data = $model->readFile($filePath);
  
693c46cb   Mihail   add base classes ...
81
                  Yii::$app->getCache()->set( 'parser_data', json_encode($data), 200 );
500b481a   Administrator   JSON
82
  
2772f19c   Mihail   parser csv v2
83
84
85
86
87
88
89
                  $provider = new ArrayDataProvider([
                      'allModels' => $data,
                      'pagination' => [
                          'pageSize' => 10,
                      ],
  //                    'sort' => [
  //                        'attributes' => ['id', 'name'],
fcd9278e   Mihail   parser csv v1
90
  //                    ],
2772f19c   Mihail   parser csv v2
91
                  ]);
fcd9278e   Mihail   parser csv v1
92
  
2772f19c   Mihail   parser csv v2
93
                  return $this->render('results',
9ef73019   Mihail   parser
94
                      ['model' => $data,
e774f057   Mihail   work with custome...
95
                          'imp' => $model,
2772f19c   Mihail   parser csv v2
96
                          'dataProvider' => $provider]);
3cf42f5c   Mihail   init commit - bas...
97
              }
500b481a   Administrator   JSON
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  
          } else if( Yii::$app->getCache()->get( 'parser_data' )) {
  
              $data = json_decode(Yii::$app->getCache()->get( 'parser_data' ),true);
  
              $provider = new ArrayDataProvider([
                  'allModels' => $data,
                  'pagination' => [
                      'pageSize' => 10,
                  ],
  //                    'sort' => [
  //                        'attributes' => ['id', 'name'],
  //                    ],
              ]);
  
              return $this->render('results',
                  ['model' => $data,
e774f057   Mihail   work with custome...
115
                      'imp' => $model,
500b481a   Administrator   JSON
116
                      'dataProvider' => $provider]);
3cf42f5c   Mihail   init commit - bas...
117
118
          }
  
2772f19c   Mihail   parser csv v2
119
          return $this->render('index', ['model' => $model]);
3cf42f5c   Mihail   init commit - bas...
120
      }
e774f057   Mihail   work with custome...
121
122
123
  public function actionWrite()
  {
      CustomVarDamp::dumpAndDie(Yii::$app->request->post());
3cf42f5c   Mihail   init commit - bas...
124
  
e774f057   Mihail   work with custome...
125
  }
3cf42f5c   Mihail   init commit - bas...
126
127
  
  }