Blame view

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