| Current Path : /var/www/html/donate/backend/modules/menu/controllers/ |
| Current File : /var/www/html/donate/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;
/**
* 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);
$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)
{
$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);
}
}