Your IP : 216.73.216.79


Current Path : /var/www/html/sprm/backend/controllers/
Upload File :
Current File : /var/www/html/sprm/backend/controllers/ReportMetaController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\ReportMeta;
use backend\models\ReportMetaSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\ReportGraph;
use backend\models\ReportTable;
use backend\models\dashboard\DashboardPanel;

/**
 * ReportMetaController implements the CRUD actions for ReportMeta model.
 */
class ReportMetaController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ReportMeta models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ReportMetaSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        // echo $id_report_meta=Yii::$app->request->get('id_report_meta');
        // $modelModal = ReportMeta::find()->where( [ 'id_report_meta' => $id_report_meta] )->one();

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            // 'id_report_meta' => $id_report_meta,
            // 'modelModal' => $modelModal,
            //'id_report_meta' => $id_report_meta,
        ]);
    }

    public function actionIndex1()
    {
        
        
        //print_r($modelModal);

        return $this->render('index1');
    }

    /**
     * Displays a single ReportMeta model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id_report_meta)
    {
        $this->layout = 'empty';

        $modelTable = ReportTable::find()->where(['id_report_meta'=>$id_report_meta])->all();
        //echo '<pre>';print_r($modelTable);echo '</pre>';exit;


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

    // public function actionReport($id_report_meta)
    // {
    //     $this->layout = 'empty';

    //     $model = ReportGraph::find()->where(['id_report_meta'=>$id_report_meta])->all();
    //     //echo '<pre>';print_r($model);echo '</pre>';exit;

    //     return $this->redirect('index.php?r=report-meta/view&ReportGraph[id_report_meta]='.$model->id_report_meta.'&ReportGraph[title_graph]='.$model->title_graph.'&ReportGraph[table_name]='.$model->table_name.'&ReportGraph[sql_statement]='.$model->sql_statement.'&ReportGraph[xAxis]='.$model->xAxis.'&ReportGraph[yAxis]='.$model->yAxis.'&ReportGraph[yAxis_replacement_name]='.$model->yAxis_replacement_name.'&ReportGraph[type_report]='.$model->type_report.'&ReportGraph[graph_type]='.$model->graph_type.'&ReportGraph[id_report_graph]='.$model->id_report_graph);


    //     // return $this->render('view', [
    //     //     'model' => $this->findModel($id_report_meta),
    //     // ]);
    // }

    // public function actionView($id)
    // {
    //     return $this->render('view', [
    //         'model' => $this->findModel($id),
    //     ]);
    // }

    /**
     * Creates a new ReportMeta model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ReportMeta();
        

        if ($model->load(Yii::$app->request->post())) {

            $model->role = implode(',',Yii::$app->request->post('ReportMeta')['role']);

            date_default_timezone_set("Asia/Kuala_Lumpur");
            $model->created_by=Yii::$app->user->identity->username;
            $model->created_at=date("Y-m-d H:i:s"); 
            
        $model->save();

            

            return $this->redirect(['report-meta/index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing ReportMeta model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) ) {

            $model->role = implode(',',Yii::$app->request->post('ReportMeta')['role']);
            date_default_timezone_set("Asia/Kuala_Lumpur");
            $model->updated_by=Yii::$app->user->identity->username;
            $model->updated_at=date("Y-m-d H:i:s");
            $model->save();

            return $this->redirect(['report-meta/index']);

            //return $this->redirect(['view', 'id_report_meta' => $model->id_report_meta]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing ReportMeta model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ReportMeta model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ReportMeta the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ReportMeta::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionGraph()
    {

        $model = new ReportGraph(); 

        $method = $_SERVER['REQUEST_METHOD'];
        $request = Yii::$app->request->getQueryParams();
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $input = json_decode(file_get_contents('php://input'), true);
        
        $request = Yii::$app->request->get('ReportGraph');
        $xAxis = $request['xAxis'];
        $yAxis = $request['yAxis'];
        $graph_type = $request['graph_type'];
        $table_name = $request['table_name'];
        $title_graph = $request['title_graph'];
        $yAxis_replacement_name = $request['yAxis_replacement_name'];
        $id_report_meta = $request['id_report_meta'];
        $sql_statement = $request['sql_statement'];
        $type_report = $request['type_report'];
        $id_report_graph = $request['id_report_graph'];

        $getDb=Yii::$app->db->schema->getTableNames(); 
        $dbSelect = $getDb[$table_name];
        
        $fromColumn=Yii::$app->db->getTableSchema($dbSelect)->getColumnNames();
        $var_xAxis = $fromColumn[$xAxis];
        $var_yAxis = $fromColumn[$yAxis];

                if(!empty($graph_type) && !empty($yAxis) && !empty($xAxis))
                {   
                    
                    $tbl=(new \yii\db\Query())->select($var_xAxis.' AS AXISX , COUNT('.$var_yAxis.') AS AXISY')
                    ->from($dbSelect)
                    ->groupBy($var_xAxis)
                    ->all();

                        $qArr = [];
                        foreach ($tbl as $key => $value) {
                          $qArr[$value['AXISX']] = $value['AXISY'];
                        }

                }else{
                    throw new \yii\web\HttpException(500,'Mising Required Parameter');
                }
                        
    
                return $tbl;

    }
}