Your IP : 216.73.216.79


Current Path : /var/www/html/sprm/backend/modules/mainmenu/controllers/
Upload File :
Current File : /var/www/html/sprm/backend/modules/mainmenu/controllers/FrontendController.php

<?php

namespace backend\modules\mainmenu\controllers;

use Yii;
use backend\modules\mainmenu\models\frontend\Menu;
use backend\modules\mainmenu\models\frontend\MenuSearch;
use backend\modules\mainmenu\models\frontend\RoleMapping;
// use backend\modules\mimin\components\AccessControl;
use yii\web\Controller; 
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;

/**
 * MenuController implements the CRUD actions for Menu model.
 */
class FrontendController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            '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);
        $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();
        
        if ($model->load(Yii::$app->request->post()) && $model->save(false)) {
            
            return $this->redirect(['index']);
        } else {
            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 ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {
            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)
    {
        $this->findModel($id)->delete();
        
        return $this->redirect(['index']);
    }
    
    
    /**
     * 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)
    {
        $role = RoleMapping::findOne($id);
        
        $siteid = $role->site_id;
        $pageid = $role->page_id;
        
        $role->delete();
        return $this->redirect(['assign-menu', 'site_id' => $siteid,'page_id' => $pageid]);
    }
    
    /**
     * 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', 'site_id' => $model->site_id, 'page_id' => $model->page_id]);
    }
    
    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 = ($hsort['sort'] + 1);
            $model->save(false);
            
            return $this->redirect(['assign-menu', 'site_id' => $model->site_id, 'page_id' => $model->page_id]);
        }
        
        return $this->render('assign',['model'=>$model]);
    }
}