Your IP : 216.73.216.79


Current Path : /var/www/html/school/backend/controllers/
Upload File :
Current File : /var/www/html/school/backend/controllers/RoleController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\Role;
use backend\models\RoleSearch;
use backend\models\Menu;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;

/**
 * RoleController implements the CRUD actions for Role model.
 */
class RoleController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

        $moduleArr = [];
        $module = Menu::find()->where("module_ind = '1'")->all();
        foreach ($module as $param) {
            $moduleArr[$param->id] = $param->menu_txt;
        }

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

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

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

        $moduleArr = [];
        $module = Menu::find()->where("module_ind = '1'")->all();
        foreach ($module as $param) {
            $moduleArr[$param->id] = $param->menu_txt;
        }

        if ($model->load(Yii::$app->request->post())) {
            $model->role_code = $_POST['Role']['role_code'];
            $model->menu_id = $_POST['Role']['menu_id'];
            if ($model->save()) {
                $log = new \backend\models\Log();
                $log->module = Yii::$app->controller->id;
                $log->details = Yii::$app->controller->action->id . ":" . $model->role_code;

                $log->save();
                return $this->redirect(['index', 'role_code' => $model->role_code, 'menu_id' => $model->menu_id]);
            }
        } else {
            return $this->render('create', [
                        'model' => $model,
                        'moduleArr' => $moduleArr,
            ]);
        }
    }

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

        $moduleArr = [];
        $module = Menu::find()->where("module_ind = '1'")->all();
        foreach ($module as $param) {
            $moduleArr[$param->id] = $param->menu_txt;
        }

        if ($model->load(Yii::$app->request->post())) {
            $model->role_code = $_POST['Role']['role_code'];
            $model->menu_id = $_POST['Role']['menu_id'];
            if ($model->save()) {
                $log = new \backend\models\Log();
                $log->module = Yii::$app->controller->id;
                $log->details = Yii::$app->controller->action->id . ":" . $model->role_code;

                $log->save();
                return $this->redirect(['update', 'role_code' => $model->role_code, 'menu_id' => $model->menu_id]);
            }
        } else {
            return $this->render('update', [
                        'model' => $model,
                        'moduleArr' => $moduleArr,
            ]);
        }
    }

    /**
     * Deletes an existing Role model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $role_code
     * @param integer $menu_id
     * @return mixed
     */
    public function actionDelete($role_code, $menu_id) {
        $this->findModel($role_code, $menu_id)->delete();
        $log = new \backend\models\Log();
        $log->module = Yii::$app->controller->id;
        $log->details = Yii::$app->controller->action->id . ":" . $role_code;

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

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

}