<?php

namespace modules\cors\controllers;

use Yii;
use modules\cors\models\Licensecatsbe;
use modules\cors\models\LicensecatsbeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * LicensecatsController implements the CRUD actions for licensecatsbe model.
 */
class LicensecatsController extends Controller
{
 public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\filters\AccessControl::class,
                'rules' => [
                    [
                        'actions' => ['undelete', 'deleted'],
                        'allow' => true,
                        'matchCallback' => function ($rule, $action) {
                    	    if (!yii::$app->user->isGuest) {
                	            return yii::$app->user->identity->useradv->accesslevel >= 9;
							} else { 
						   		return $this->redirect(['site/login']);
	                        }
                        }
                    ],
                    [
                        'actions' => ['create','index','update', 'delete', 'view'],
                        'allow' => true,
                        'matchCallback' => function ($rule, $action) {
                    	    if (!yii::$app->user->isGuest) {
                	            return yii::$app->user->identity->useradv->accesslevel >= 5;
							} else { 
						   		return $this->redirect(['site/login']);
	                        }
                        }
                    ],
            	],
                'denyCallback' => function ($rule, $action) {
				   	if (yii::$app->user->isGuest) {
				   		return $this->redirect(['site/login']);
					} else {
				   		return $this->redirect(['site/accessdenied']);
					   	
					}
				}
            
            ],
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }
 	public function beforeAction($action) {
   		$id = yii::$app->request->get('id');
		Yii::$app->runaction('/audit/qcreate', ['id' => $id ? $id : null , 'controller' =>  Yii::$app->controller->id, 'action' => Yii::$app->controller->action->id, 'attributes' => null]);
		return parent::beforeAction($action);
	}

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

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
     /**
     * Lists Deleted licensecatsbe models.
     * @return mixed
     */
    public function actionDeleted()
    {
        $searchModel = new licensecatsbeSearch();
        $dataProvider = $searchModel->deleted(Yii::$app->request->queryParams);

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

    /**
     * Displays a single licensecatsbe model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	$model = licensecatsbe::find()->where(['=', 'created_at', $model->created_at])->andWhere(['=', 'created_by', $model->created_by])->one();
        
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing licensecatsbe model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
		$model = $this->findModel($id);
		$model->status = 'Deleted';
		$model->deleted_at = strtotime('now');
		$model->save();
        return $this->redirect(['index']);
    }

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