MenuController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace common\modules\rbac\controllers;
  3. use common\modules\rbac\components\MenuHelper;
  4. use common\modules\rbac\models\Menu;
  5. use Yii;
  6. use yii\data\ActiveDataProvider;
  7. use yii\filters\VerbFilter;
  8. use yii\helpers\Url;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. /**
  12. * MenuController implements the CRUD actions for Menu model.
  13. *
  14. * @author Misbahul D Munir <misbahuldmunir@gmail.com>
  15. *
  16. * @since 1.0
  17. */
  18. class MenuController extends Controller
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function behaviors()
  24. {
  25. return [
  26. 'verbs' => [
  27. 'class' => VerbFilter::className(),
  28. 'actions' => [
  29. 'delete' => ['post'],
  30. ],
  31. ],
  32. ];
  33. }
  34. public function actions()
  35. {
  36. return [
  37. 'ajax-update-field' => [
  38. 'class' => 'common\\actions\\AjaxUpdateFieldAction',
  39. 'allowFields' => ['order'],
  40. 'findModel' => [$this, 'findModel']
  41. ],
  42. 'position' => [
  43. 'class' => 'backend\\actions\\Position',
  44. 'returnUrl' => Url::current()
  45. ]
  46. ];
  47. }
  48. /**
  49. * Lists all Menu models.
  50. *
  51. * @return mixed
  52. */
  53. public function actionIndex()
  54. {
  55. $query = Menu::find()->orderBy('order asc');
  56. $dataProvider = new ActiveDataProvider([
  57. 'query' => $query,
  58. 'pagination' => false
  59. ]);
  60. return $this->render('index', [
  61. 'dataProvider' => $dataProvider
  62. ]);
  63. }
  64. /**
  65. * Displays a single Menu model.
  66. *
  67. * @param int $id
  68. *
  69. * @return mixed
  70. */
  71. public function actionView($id)
  72. {
  73. return $this->render('view', [
  74. 'model' => $this->findModel($id),
  75. ]);
  76. }
  77. /**
  78. * Creates a new Menu model.
  79. * If creation is successful, the browser will be redirected to the 'view' page.
  80. *
  81. * @return mixed
  82. */
  83. public function actionCreate($id = null)
  84. {
  85. $model = new Menu();
  86. $model->parent = $id ? : null;
  87. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  88. Yii::$app->session->setFlash('success', '操作成功');
  89. return $this->redirect(['index']);
  90. } else {
  91. return $this->render('create', [
  92. 'model' => $model,
  93. ]);
  94. }
  95. }
  96. /**
  97. * Updates an existing Menu model.
  98. * If update is successful, the browser will be redirected to the 'view' page.
  99. *
  100. * @param int $id
  101. *
  102. * @return mixed
  103. */
  104. public function actionUpdate($id)
  105. {
  106. $model = $this->findModel($id);
  107. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  108. Yii::$app->session->setFlash('success', '操作成功');
  109. return $this->redirect(['index']);
  110. } else {
  111. return $this->render('update', [
  112. 'model' => $model,
  113. ]);
  114. }
  115. }
  116. /**
  117. * Deletes an existing Menu model.
  118. * If deletion is successful, the browser will be redirected to the 'index' page.
  119. *
  120. * @param int $id
  121. *
  122. * @return mixed
  123. */
  124. public function actionDelete($id)
  125. {
  126. if (Menu::find()->where(['parent' => $id])->count() > 0) {
  127. Yii::$app->session->setFlash('error', '请先删除该菜单下的所有子菜单');
  128. return $this->redirect(['index']);
  129. }
  130. $this->findModel($id)->delete();
  131. Yii::$app->session->setFlash('success', '操作成功');
  132. return $this->redirect(['index']);
  133. }
  134. /**
  135. * Finds the Menu model based on its primary key value.
  136. * If the model is not found, a 404 HTTP exception will be thrown.
  137. *
  138. * @param int $id
  139. *
  140. * @return Menu the loaded model
  141. *
  142. * @throws NotFoundHttpException if the model cannot be found
  143. */
  144. public function findModel($id)
  145. {
  146. if (($model = Menu::findOne($id)) !== null) {
  147. return $model;
  148. } else {
  149. throw new NotFoundHttpException('The requested page does not exist.');
  150. }
  151. }
  152. }