Your IP : 216.73.216.79


Current Path : /var/www/html/ekursus/backend/controllers/
Upload File :
Current File : /var/www/html/ekursus/backend/controllers/MainMenuController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\MainMenu;
use backend\models\MainMenuSearch;
use yii\web\Controller;
use common\components\AlertMsg;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;

/**
 * RefController implements the CRUD actions for Ref model.
 */
class MainMenuController 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 Ref models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new MainMenuSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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


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

    /**
     * Creates a new Ref model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
//        $mMain = MainMenu::findOne($id);
        $id = !isset($id) ? '' : $id;
        $model = new MainMenu();
        
        if ($model->load(Yii::$app->request->post())) {
            $model->cat = 'MAIN';

            
            if ($model->save(false)) {
                $msg = 'Menu Utama Telah Ditambah';
                AlertMsg::alertByRole($msg, AlertMsg::SUPER_ADMIN);
                $log = new \backend\models\Log();
                $log->module = Yii::$app->controller->id;
                $log->details = Yii::$app->controller->action->id . ":" . $model->id;

                Yii::$app->session->setFlash('success', Yii::t('app', 'Menu Utama telah berjaya ditambah.'));
                return $this->redirect(['index', 'id' => $id]);
            }
        } else {
            return $this->render('create', [
                        'model' => $model,
                        // 'id' => $id,
            ]);
        }
    }

    /**
     * Updates an existing Ref model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    

    public function actionUpdate($id) {
        $model = MainMenu::findOne($id);
//        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $log = new \backend\models\Log();
            $log->module = Yii::$app->controller->id;
            $log->details = Yii::$app->controller->action->id . ":" . $model->id;

            $log->save();

            Yii::$app->session->setFlash('success', Yii::t('app', 'Menu Utama berjaya dikemaskini'));
            return $this->redirect(['update', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
                        'id' => $id,
            ]);
        }
    }


    public function actionDelete($id) {
        $this->findModel($id)->delete();
        $log = new \backend\models\Log();
        $log->module = Yii::$app->controller->id;
        $log->details = Yii::$app->controller->action->id . ":" . $id;

        $log->save();
        return $this->redirect(['index']);
    }
    
    public function actionListData($id, $parentid, $cat) {
        $mRef = $this->findModel($id);

        $searchModel = new RefSearch();
        $dataProvider = $searchModel->searchListData(Yii::$app->request->queryParams);

        return $this->render('list_data', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
                    'mRef' => $mRef,
                    'id' => $id,
                    'parentid' => $parentid,
                    'cat' => $cat,
        ]);
    }

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

}