Your IP : 216.73.216.79


Current Path : /var/www/html/drsa/backend/modules/menu/controllers/
Upload File :
Current File : /var/www/html/drsa/backend/modules/menu/controllers/BackendController.php

<?php

namespace backend\modules\menu\controllers;

use Yii;
use backend\modules\menu\models\backend\Menu;
use backend\modules\menu\models\backend\MenuSearch;
use backend\modules\menu\models\backend\RoleMapping;
use backend\modules\menu\components\MenuHelper;
use backend\modules\mimin\components\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;

/**
 * MenuController implements the CRUD actions for Menu model.
 */
class BackendController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all FrontendMenu models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new MenuSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
    }

    /* public function actionIndex() {
      $searchModel = new MenuSearch();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
      $model = new Menu();
      return $this->render('index', [
      'searchModel' => $searchModel,
      'dataProvider' => $dataProvider,
      'model' => $model
      ]);
      } */

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

    /**
     * Creates a new FrontendMenu model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Menu();
        $model->menu_status = 1;
        if ($model->load(Yii::$app->request->post())) {
            if (!empty($model->menu_active_route)) {
                $model->menu_active_route = json_encode($model->menu_active_route);
            }
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing Menu 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 (!empty($model->menu_active_route)) {
            $model->menu_active_route = json_decode($model->menu_active_route);
        }
        if ($model->load(Yii::$app->request->post())) {
            if (!empty($model->menu_active_route)) {
                $model->menu_active_route = json_encode($model->menu_active_route);
            }
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing Menu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete() {
        $id = $_POST['id'];
        $status = 'failed';
        if ($this->findModel($id)->delete())
            $status = 'success';
        return $status;
    }

    /**
     * Deletes an existing Menu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionRoledelete($id) {
        $model = RoleMapping::findOne($id)->delete();
        return $this->redirect(['assign-menu']);
    }

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

    public function actionLevel(int $id, string $role, string $pos, string $sort) {
        $model = RoleMapping::findOne($id);

        /* parent */
        $hsort = RoleMapping::find()->where([$pos, 'sort', $model->sort])->andWhere(['parent_id' => $model->parent_id, 'role_code' => $role])->orderby('sort ' . $sort)->one();
        /* position */
        $pos = ($pos == '<') ? 'top' : 'bottom';

        /* swap varible */
        if (!empty($hsort) && !empty($model)) {
            list($model->sort, $hsort->sort) = [$hsort->sort, $model->sort];

            Yii::$app->db->transaction(function() use($model, $hsort) {
                $model->save(false);
                $hsort->save(false);
            });

            Yii::$app->session->setFlash('info', Yii::t('app', 'Change position to the ' . $pos));
        } else {
            Yii::$app->session->setFlash('warning', 'Already at the ' . $pos . '!');
        }

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

    public function actionAssignMenu() {

        $model = new RoleMapping();

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

            $pid = (int) $model->parent_id ?? 0;

            /* find highest sort */
            $hsort = RoleMapping::find()->where(['role_code' => $model->role_code, 'parent_id' => $pid])->orderBy('sort DESC')->asArray()->one();
            $model->sort = 0;
            if (!empty($hsort['sort']))
                $model->sort = ($hsort['sort'] + 1);
            $model->save(false);

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

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

    public function actionStub() {
        $m = (new MenuHelper)->getAssignedMenu();
        echo json_encode($m, JSON_PRETTY_PRINT);
    }

}