UploadFileRgForm.php
1.47 KB
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
<?php
namespace backend\models;
use yii\base\ErrorException;
use yii\base\Model;
use yii\web\UploadedFile;
use Yii;
use common\components\CustomVarDamp;
/**
 * UploadForm is the model behind the upload form.
 */
class UploadFileRgForm extends Model
{
    /**
     * @var UploadedFile file attribute
     */
    // атрибуты формы
    public $file;
    public $file_path;
    public $importer_id;
    public function rules()
    {
        return [
            ['file', 'required', 'message' => 'Не выбран файл!' ],
            ['importer_id', 'required', 'message' => 'Не указан поставщик!' ],
            [['file'], 'file', 'extensions' => 'xlsx', 'checkExtensionByMimeType'=>false ]
        ];
    }
    public function attributeLabels()
    {
        return [
            'file' => Yii::t('app', 'Источник'),
            'importer_id' => Yii::t('app', 'Поставщик'),
        ];
    }
    public function readFile( $options = [] ){
        $data = Yii::$app->multiparser->parse( $this->file_path, $options );
        if( !is_array( $data ) ){
            throw new ErrorException("Ошибка чтения из файла RG групп {$this->file_path}");
        }
        return $data;
    }
    function __destruct()
    {
        // файл больше не нужен - данные прочитаны и сохранены в кеш
       // if( file_exists($this->file_path) )
      //      unlink($this->file_path);
    }
}