Blame view

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