AreaController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace common\modules\area\backend\controllers;
  3. use common\enums\BooleanEnum;
  4. use common\modules\area\models\Area;
  5. use common\modules\area\models\Block;
  6. use Yii;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. /**
  10. * AreaController implements the CRUD actions for Area model.
  11. */
  12. class AreaController extends Controller
  13. {
  14. public function actionIndex()
  15. {
  16. return $this->render("index", [
  17. "blocks" => Block::find()->all(),
  18. "areas" => Area::find()->all()
  19. ]);
  20. }
  21. /**
  22. * Creates a new Area model.
  23. * If creation is successful, the browser will be redirected to the 'view' page.
  24. *
  25. * @return mixed
  26. */
  27. public function actionCreate()
  28. {
  29. $model = new Area();
  30. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  31. return $this->redirect(['index']);
  32. } else {
  33. return $this->render('create', [
  34. 'model' => $model,
  35. ]);
  36. }
  37. }
  38. /**
  39. * Updates an existing Area model.
  40. * If update is successful, the browser will be redirected to the 'view' page.
  41. *
  42. * @param int $id
  43. *
  44. * @return mixed
  45. */
  46. public function actionUpdate($id)
  47. {
  48. $model = $this->findModel($id);
  49. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  50. return $this->redirect(['index']);
  51. } else {
  52. return $this->render('update', [
  53. 'model' => $model,
  54. ]);
  55. }
  56. }
  57. /**
  58. * Deletes an existing Area model.
  59. * If deletion is successful, the browser will be redirected to the 'index' page.
  60. *
  61. * @param int $id
  62. *
  63. * @return mixed
  64. */
  65. public function actionDelete($id)
  66. {
  67. $this->findModel($id)->delete();
  68. return $this->redirect(['index']);
  69. }
  70. public function actionUpdateBlocks()
  71. {
  72. Yii::$app->response->format = 'json';
  73. $id = \Yii::$app->getRequest()->post("id");
  74. $blocks = \Yii::$app->getRequest()->post("blocks");
  75. $model = $this->findModel($id);
  76. $model->blocks = $blocks;
  77. if($model->save()) {
  78. return [
  79. 'status' => true,
  80. 'msg' => '更新成功'
  81. ];
  82. } else {
  83. return [
  84. 'status' => false,
  85. 'msg' => '更新失败'
  86. ];
  87. }
  88. }
  89. public function actionUpdateBlocksDelete()
  90. {
  91. Yii::$app->response->format = 'json';
  92. $id = \Yii::$app->getRequest()->post("id");
  93. $blocks = \Yii::$app->getRequest()->post("blocks");
  94. $block = \Yii::$app->getRequest()->post("block");
  95. $model = $this->findModel($id);
  96. $model->blocks = $blocks;
  97. $block = Block::findOne($block);
  98. $block->used = BooleanEnum::FLASE;
  99. if($model->save() && $block->save()) {
  100. return [
  101. 'status' => true,
  102. 'msg' => '更新成功'
  103. ];
  104. } else {
  105. return [
  106. 'status' => false,
  107. 'msg' => '更新失败'
  108. ];
  109. }
  110. }
  111. public function actionUpdateBlocksCreate()
  112. {
  113. Yii::$app->response->format = 'json';
  114. $id = \Yii::$app->getRequest()->post("id");
  115. $blocks = \Yii::$app->getRequest()->post("blocks");
  116. $block = \Yii::$app->getRequest()->post("block");
  117. $model = $this->findModel($id);
  118. $model->blocks = $blocks;
  119. $block = Block::findOne($block);
  120. $block->used = BooleanEnum::TRUE;
  121. if($model->save() && $block->save()) {
  122. return [
  123. 'status' => true,
  124. 'msg' => '更新成功'
  125. ];
  126. } else {
  127. return [
  128. 'status' => false,
  129. 'msg' => '更新失败'
  130. ];
  131. }
  132. }
  133. /**
  134. * Finds the Area model based on its primary key value.
  135. * If the model is not found, a 404 HTTP exception will be thrown.
  136. *
  137. * @param integer $id
  138. * @return Area the loaded model
  139. * @throws NotFoundHttpException if the model cannot be found
  140. */
  141. public function findModel($id)
  142. {
  143. if (($model = Area::findOne($id)) !== null) {
  144. return $model;
  145. } else {
  146. throw new NotFoundHttpException('The requested page does not exist.');
  147. }
  148. }
  149. }