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

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use backend\models\UploadFileParsingForm;
use yii\web\UploadedFile;
use yii\data\ArrayDataProvider;

use common\components\debug\CustomVarDamp;

/**
 * Parser controller
 */

class ParserController extends Controller
{
    public $layout = "/column";
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index','results'],
                        '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();

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

            if ($model->file && $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),200 );

                $provider = new ArrayDataProvider([
                    'allModels' => $data,
                    'pagination' => [
                        'pageSize' => 10,
                    ],
//                    'sort' => [
//                        'attributes' => ['id', 'name'],
//                    ],
                ]);

                return $this->render('results',
                    ['model' => $data,
                        'dataProvider' => $provider]);
            }

        } 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,
                ],
//                    'sort' => [
//                        'attributes' => ['id', 'name'],
//                    ],
            ]);

            return $this->render('results',
                ['model' => $data,
                    'dataProvider' => $provider]);
        }

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



}