Blame view

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