ParserController.php 3.58 KB
<?php
namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use backend\components\base\BaseController;
use yii\filters\VerbFilter;
use backend\models\UploadFileParsingForm;
use yii\web\UploadedFile;
use yii\data\ArrayDataProvider;
use yii\multiparser\DynamicFormHelper;
use backend\components\parsers\CustomParserConfigurator;

use common\components\CustomVarDamp;

/**
 * Parser controller
 */

class ParserController extends BaseController
{
    public $layout = "/column";
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index','results','write'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'logout' => ['post'],
//                ],
//            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }



    public function actionIndex()
    {
        $model = new UploadFileParsingForm();

        return $this->render('index', ['model' => $model]);
    }

    public function actionResults(){

        $model = new UploadFileParsingForm();
        $data = [];
        if ($model->load(Yii::$app->request->post())) {
            $model->file = UploadedFile::getInstance($model, 'file');

            if ($model->validate()) {
                $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension;

                $model->file->saveAs( $filePath );
                $data = $model->readFile($filePath);

                Yii::$app->getCache()->set( 'parser_data', json_encode($data) );

            }

        } else if( Yii::$app->getCache()->get( 'parser_data' )) {

            $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true );

        }

        $provider = new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

       // CustomVarDamp::dumpAndDie($data);
        $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) );

     //   CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column'));
        return $this->render('results',
            ['model' => $data,
                'header_model' => $header_model,
                'basic_column' =>  Yii::$app->multiparser->getConfiguration('csv','basic_column'),
                'dataProvider' => $provider]);
    }

public function actionWrite()
{
    //CustomVarDamp::dumpAndDie(Yii::$app->request->post());

    $arr_attributes = Yii::$app->request->post()['DynamicModel'];
    $model = DynamicFormHelper::CreateDynamicModel( $arr_attributes );
    foreach ($arr_attributes as $key => $value) {
        $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]);
    }

    //CustomVarDamp::dumpAndDie($model);
    if ($model->validate()) {
        $arr = $model->toArray();
        $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true );

       // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr));
        CustomVarDamp::dumpAndDie($arr);
    }



}

}