Blame view

backend/components/traits/ParserTrait.php 3.06 KB
1e337311   Mihail   add parser trait ...
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
76
77
78
79
80
81
82
83
84
85
86
87
88
  <?php
  /**
   * Created by PhpStorm.
   * User: Tsurkanov
   * Date: 01.12.2015
   * Time: 9:42
   */
  namespace backend\components\traits;
  
  use common\components\parsers\DynamicFormHelper;
  use Yii;
  use yii\base\ErrorException;
  use yii\data\ArrayDataProvider;
  
  trait ParserTrait
  {
  
      /**
       * @param $mode - int: 0 - fetch from cache, - 1 - put in cache,  <2 - delete from cache
       * @param $data - array
       * @param $configuration - array
       * @throws \ErrorException
       */
      public function parserCacheHandler($mode, &$data = [], &$configuration = [])
      {
          switch ($mode) {
              case 0:
                  if (Yii::$app->getCache()->get('parser_data') && Yii::$app->getCache()->get('parser_configuration')) {
                      $data = json_decode(Yii::$app->getCache()->get('parser_data'), true);
                      $configuration = unserialize(Yii::$app->getCache()->get('parser_configuration'));
                  } else {
                      throw new \ErrorException('Ошибка кеша');
                  }
                  break;
  
              case 1:
                  Yii::$app->getCache()->set('parser_data', json_encode($data), 1800);
                  // сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных
                  Yii::$app->getCache()->set('parser_configuration', serialize($configuration), 1800);
                  break;
  
              default:
                  if (Yii::$app->getCache()->exists('parser_data'))
                      Yii::$app->getCache()->delete('parser_data');
  
                  if (Yii::$app->getCache()->exists('parser_configuration'))
                      Yii::$app->getCache()->delete('parser_configuration');
          }
  
      }
  
      public function renderResultView($data)
      {
          $provider = new ArrayDataProvider([
              'allModels' => $data,
              'pagination' => [
                  'pageSize' => 10,
              ],
          ]);
  
          // создадим модель на столько реквизитов сколько колонок в отпарсенном файле
          $last_index = end(array_flip($data[0]));
          $header_counts = $last_index + 1;
          $header_model = DynamicFormHelper::CreateDynamicModel($header_counts);
  
          // соберем массив данных из которых будет пользователь выбирать значения в конструкторе (выпадающий список)
          $basicColumns = $this->getBasicColumns();
  
          return $this->render('results',
              ['model' => $data,
                  'header_model' => $header_model,
                  // список колонок для выбора
                  'basic_column' => $basicColumns,
                  'dataProvider' => $provider]);
  
      }
  
      public function throwStringErrorException( $model , $exception, $errors_str = '' )
      {
          foreach ( $model->getErrors() as $error )
          {
              $errors_str .= ' ' . implode( array_values( $error ) );
          }
  
          throw new $exception( $errors_str );
      }
  
      }