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

use yii\base\Model;
use yii\web\UploadedFile;
use backend\components\ParserHandler;
use Yii;

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

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['file'], 'file', 'extensions' => ['csv', 'xml'] ],
            ['importer', 'safe'],
            ['action', 'boolean']
//            ['delimiter', 'string', 'max' => 1],
//            ['delimiter', 'default', 'value' => ';'],

        ];
    }

    public function attributeLabels()
    {
        return [
            'file' => Yii::t('app', 'Источник'),
            'first_line' => Yii::t('app', 'Первая значимая строка'),
            'first_column' => Yii::t('app', 'Первый значимый столбец'),
        ];
    }

    public function readFile($filePath){
        $parser = new ParserHandler( $filePath, $this );
        $data = $parser->run();

        if( !is_array($data) ){
            $data = ['No results'];
        }
        return $data;
    }
}