Blame view

backend/models/UploadFileParsingForm.php 2.76 KB
58743b31   Mihail   init commit - bas...
1
  <?php
febcec0b   Mihail   final version par...
2
  namespace backend\models;
58743b31   Mihail   init commit - bas...
3
4
5
  
  use yii\base\Model;
  use yii\web\UploadedFile;
febcec0b   Mihail   final version par...
6
  use Yii;
7a80e74c   Mihail   add DynamicFormHe...
7
  use common\components\CustomVarDamp;
58743b31   Mihail   init commit - bas...
8
9
10
11
  
  /**
   * UploadForm is the model behind the upload form.
   */
5be26bf2   Mihail   edit upload form
12
  class UploadFileParsingForm extends Model
58743b31   Mihail   init commit - bas...
13
14
15
16
  {
      /**
       * @var UploadedFile file attribute
       */
2fb5a757   Mihail   add menu and chec...
17
      // атрибуты формы
58743b31   Mihail   init commit - bas...
18
      public $file;
d7f6bdbb   Mihail   add Importers fil...
19
      public $importer_id;
5c2732df   Mihail   add mail parser a...
20
      public $action;
165348a4   Mihail   draft commit
21
22
23
      public $delimiter;
      public $delete_price;
      public $delete_prefix;
2fb5a757   Mihail   add menu and chec...
24
25
26
27
  
      // служебные атрибуты
      public $file_path;
      public $success;
1fa22312   Mihail   add auto upload a...
28
      public $mode; //0 - режим ручной загрузки, 1 - режим автозагрузки
0b2baee3   Mihail   fix errors with w...
29
      public $record_id; // id таблицы в которую записывается информация о файле
58743b31   Mihail   init commit - bas...
30
31
32
33
  
      /**
       * @return array the validation rules.
       */
1fa22312   Mihail   add auto upload a...
34
35
36
37
38
39
40
41
42
43
44
      public function __construct($config = [])
      {
          parent::__construct($config);
          if ( $this->mode ) {
              // автозагрузка, проставим сценарий
              $this->scenario = 'auto';
          }
  
      }
  
  
58743b31   Mihail   init commit - bas...
45
46
47
      public function rules()
      {
          return [
d7f6bdbb   Mihail   add Importers fil...
48
              ['importer_id', 'required', 'message' => 'Не указан поставщик!' ],
165348a4   Mihail   draft commit
49
50
              ['file', 'required', 'message' => 'Не выбран файл!' ],
              //@todo - not working this file validator!!! - fixed
67d432ac   Mihail   final upload form...
51
              [['file'], 'file'],// 'extensions' => ['csv', 'xml'] ],
165348a4   Mihail   draft commit
52
   //               'wrongMimeType' => 'Указан неподдерживаемый тип файла. Можно выбирать csv, xml файлы.' ],
d7f6bdbb   Mihail   add Importers fil...
53
              ['importer_id', 'integer','max' => 999999, 'min' => 0 ],
1fa22312   Mihail   add auto upload a...
54
              [['action','delete_prefix', 'delete_price', 'success'], 'boolean', 'except' => 'auto' ], // только для ручной загрузки
165348a4   Mihail   draft commit
55
              ['delimiter', 'string', 'max' => 1],
0b2baee3   Mihail   fix errors with w...
56
              [['mode','record_id'], 'safe'],
51514d7d   Mihail   add writing data ...
57
              ['delimiter', 'default', 'value' => ';'],
2fb5a757   Mihail   add menu and chec...
58
              [ 'success', 'default', 'value' => false]
5c2732df   Mihail   add mail parser a...
59
  
febcec0b   Mihail   final version par...
60
61
62
63
64
65
66
          ];
      }
  
      public function attributeLabels()
      {
          return [
              'file' => Yii::t('app', 'Источник'),
d7f6bdbb   Mihail   add Importers fil...
67
              'importer_id' => Yii::t('app', 'Поставщик'),
165348a4   Mihail   draft commit
68
              'delimiter' => Yii::t('app', 'Разделитель'),
58743b31   Mihail   init commit - bas...
69
70
          ];
      }
2509e17e   Administrator   JSON
71
  
2fb5a757   Mihail   add menu and chec...
72
      public function readFile(){
0c8b9dfc   Mihail   add error excepti...
73
  
2fb5a757   Mihail   add menu and chec...
74
          $data = Yii::$app->multiparser->parse( $this->file_path );
2509e17e   Administrator   JSON
75
76
77
          if( !is_array($data) ){
              $data = ['No results'];
          }
d7f6bdbb   Mihail   add Importers fil...
78
  
2509e17e   Administrator   JSON
79
80
          return $data;
      }
d7f6bdbb   Mihail   add Importers fil...
81
  
1fa22312   Mihail   add auto upload a...
82
83
84
85
86
87
88
89
90
      public function fields()
      {
          return [
  
              'importer_id',
              'delimiter',
              'delete_price',
              'delete_prefix',
              'file_path',
0b2baee3   Mihail   fix errors with w...
91
92
              // id записи таблицы ImportersFiles,
            //  'id' => 'record_id',
1fa22312   Mihail   add auto upload a...
93
94
95
96
          ];
      }
  
  
58743b31   Mihail   init commit - bas...
97
  }