예제 #1
0
 /**
  * Creates a new Menu 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()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
예제 #2
0
 /**
  * Creates a new Menu model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionAdd()
 {
     //菜单权限检测
     Yii::$app->util->adminAuth() ? '' : $this->redirect('/admin/login');
     $model = new Menu();
     if ($model->load(Yii::$app->request->post())) {
         $model->create_time = time();
         if ($model->save()) {
             return $this->redirect(['index']);
         } else {
             return $this->render('add', ['model' => $model]);
         }
     } else {
         $data = $model->menu();
         $parent = [];
         foreach ($data as $v) {
             if ($v['parent_id'] == 0) {
                 $parent[$v['id']] = $v['menu_name'];
             }
         }
         $parent[0] = '顶级菜单';
         return $this->render('add', ['model' => $model, 'parent' => $parent]);
     }
 }
예제 #3
0
 /**
  * 快捷菜单新建
  * @return  {[type]}                          [description]
  * @version 1.0      2016-01-25T12:24:11+0800     * @author cnzhangxl@foxmail.com
  */
 public function actionMyCreate()
 {
     $model = new Menu(['scenario' => 'my_create']);
     $model->mg_id = Yii::$app->user->id;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         Menu::removeMyMenuCache();
         return $this->redirect(['my']);
     } else {
         return $this->render('my_create', ['model' => $model]);
     }
 }
예제 #4
0
 public function actionDataTables()
 {
     $responseType = Yii::$app->request->get('type');
     $returnData = [];
     switch ($responseType) {
         case "fetch":
             $returnData = Menu::find()->select(['id', 'name', 'parent', 'route', 'order', 'data'])->where(['parent' => Yii::$app->request->get('id')])->orderBy(['order' => SORT_ASC])->all();
             return Json::encode($returnData);
         case "crud":
             $requestAction = Yii::$app->request->post('action');
             switch ($requestAction) {
                 case "create":
                     $requestID = key(Yii::$app->request->post('data'));
                     $requestData = Yii::$app->request->post('data')[$requestID];
                     $model = new Menu();
                     //块赋值
                     $model->attributes = $requestData;
                     //返回错误信息给datatable
                     if (!$model->validate()) {
                         $fieldErrors = [];
                         foreach ($model->errors as $name => $status) {
                             $fieldErrors[] = ['name' => $name, 'status' => Json::encode($status)];
                         }
                         return Json::encode(['fieldErrors' => $fieldErrors, 'data' => []]);
                     }
                     if ($model->save()) {
                         $requestData['id'] = $model->id;
                         $data = [];
                         $data[] = $requestData;
                         return Json::encode(['data' => $data]);
                     } else {
                         var_dump($model->errors);
                         $error = '恢复操作发生意外!';
                         throw new Exception($error);
                     }
                 case "edit":
                     $editID = array_keys(Yii::$app->request->post('data'));
                     //执行事务,保存必须都成功了才行
                     $transaction = Menu::getDb()->beginTransaction();
                     //返回值
                     $data = [];
                     try {
                         foreach ($editID as $requestID) {
                             $requestData = Yii::$app->request->post('data')[$requestID];
                             $model = Menu::findOne($requestID);
                             //块赋值
                             $model->attributes = $requestData;
                             //返回错误信息给datatable
                             if (!$model->validate()) {
                                 $fieldErrors = [];
                                 foreach ($model->errors as $name => $status) {
                                     $fieldErrors[] = ['name' => $name, 'status' => Json::encode($status)];
                                 }
                                 return Json::encode(['fieldErrors' => $fieldErrors, 'data' => []]);
                             }
                             $model->save();
                             //单个字段更新的时候
                             if (count($requestData) == 1 || !is_array($requestData)) {
                                 $requestData = Menu::find()->select(['id', 'name', 'parent', 'route', 'order', 'data'])->where(['id' => $requestID])->one();
                             } else {
                                 $requestData['id'] = $requestID;
                             }
                             $data[] = $requestData;
                         }
                         //提交事务
                         $transaction->commit();
                         return Json::encode(['data' => $data]);
                     } catch (\Exception $e) {
                         $transaction->rollBack();
                         throw $e;
                     }
                 case "remove":
                     //多选则删除全部 用deleteAll()不会触发 event: EVENT_BEFORE_DELETE 和 EVENT_AFTER_DELETE
                     foreach (Yii::$app->request->post('data') as $removeID => $removeData) {
                         Menu::findOne($removeID)->delete();
                     }
                     return Json::encode($returnData);
                 default:
                     return Json::encode($returnData);
             }
         default:
             throw new \Exception('参数错误!');
     }
 }