UploadFileParsingForm.php 1.7 KB
<?php
namespace backend\models;

use yii\base\Model;
use yii\web\UploadedFile;
use Yii;
use common\components\CustomVarDamp;

/**
 * UploadForm is the model behind the upload form.
 */
class UploadFileParsingForm extends Model
{
    /**
     * @var UploadedFile file attribute
     */
    public $file;
    public $importer_id;
    public $action;
    public $delimiter;
    public $delete_price;
    public $delete_prefix;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            ['importer_id', 'required', 'message' => 'Не указан поставщик!' ],
            ['file', 'required', 'message' => 'Не выбран файл!' ],
            //@todo - not working this file validator!!! - fixed
            [['file'], 'file'],// 'extensions' => ['csv', 'xml'] ],
 //               'wrongMimeType' => 'Указан неподдерживаемый тип файла. Можно выбирать csv, xml файлы.' ],
            ['importer_id', 'integer','max' => 999999, 'min' => 0 ],
            [['action','delete_prefix', 'delete_price'], 'boolean'],
            ['delimiter', 'string', 'max' => 1],
            ['delimiter', 'default', 'value' => ';']

        ];
    }

    public function attributeLabels()
    {
        return [
            'file' => Yii::t('app', 'Источник'),
            'importer_id' => Yii::t('app', 'Поставщик'),
            'delimiter' => Yii::t('app', 'Разделитель'),
        ];
    }

    public function readFile($filePath){

        $data = Yii::$app->multiparser->parse($filePath);
        if( !is_array($data) ){
            $data = ['No results'];
        }

        return $data;
    }

}