BlockController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace common\modules\area\backend\controllers;
  3. use common\modules\area\models\Block;
  4. use Yii;
  5. use yii\data\ActiveDataProvider;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\web\Response;
  9. use yii\widgets\ActiveForm;
  10. class BlockController extends Controller
  11. {
  12. /**
  13. * Lists all Block models.
  14. *
  15. * @return mixed
  16. */
  17. public function actionIndex()
  18. {
  19. $query = Block::find();
  20. $dataProvider = new ActiveDataProvider([
  21. 'query' => $query,
  22. ]);
  23. return $this->render('index', [
  24. 'dataProvider' => $dataProvider,
  25. ]);
  26. }
  27. /**
  28. * Creates a new Area model.
  29. * If creation is successful, the browser will be redirected to the 'view' page.
  30. * @param string $type
  31. * @return mixed
  32. */
  33. public function actionCreate($type = 'text')
  34. {
  35. $model = new Block();
  36. $model->loadDefaultValues();
  37. $model->type = $type;
  38. $model->widget = $model->getWidget();
  39. if ($model->load(Yii::$app->request->post())) {
  40. if ($model->save()) {
  41. Yii::$app->session->setFlash('success', '操作成功');
  42. } else {
  43. Yii::$app->session->setFlash('error', current($model->getFirstErrors()));
  44. }
  45. return $this->refresh();
  46. }
  47. return $this->render('create',[
  48. 'model' => $model,
  49. ]);
  50. }
  51. /**
  52. * Creates a new Area model.
  53. * If creation is successful, the browser will be redirected to the 'view' page.
  54. * @param integer $id
  55. * @return mixed
  56. */
  57. public function actionUpdate($id)
  58. {
  59. $model = $this->findModel($id);
  60. if ($model->load(Yii::$app->request->post())) {
  61. if ($model->save()) {
  62. Yii::$app->session->setFlash('success', Yii::t('common', 'updated success'));
  63. } else {
  64. Yii::$app->session->setFlash('error', Yii::t('common', 'updated error. {0}', $model->formatErrors()));
  65. }
  66. return $this->refresh();
  67. }
  68. return $this->render('update',['model' => $model]);
  69. }
  70. /**
  71. * Deletes an existing Block model.
  72. * If deletion is successful, the browser will be redirected to the 'index' page.
  73. *
  74. * @param int $id
  75. *
  76. * @return mixed
  77. */
  78. public function actionDelete($id)
  79. {
  80. $this->findModel($id)->delete();
  81. return $this->redirect(['index']);
  82. }
  83. /**
  84. * Finds the Area model based on its primary key value.
  85. * If the model is not found, a 404 HTTP exception will be thrown.
  86. *
  87. * @param integer $id
  88. * @return Block the loaded model
  89. * @throws NotFoundHttpException if the model cannot be found
  90. */
  91. public function findModel($id)
  92. {
  93. if (($model = Block::findOne($id)) !== null) {
  94. return $model;
  95. } else {
  96. throw new NotFoundHttpException('The requested page does not exist.');
  97. }
  98. }
  99. protected function performAjaxValidation($model)
  100. {
  101. if (Yii::$app->request->isAjax && !Yii::$app->request->isPjax) {
  102. if ($model->load(Yii::$app->request->post())) {
  103. Yii::$app->response->format = Response::FORMAT_JSON;
  104. echo json_encode(ActiveForm::validate($model));
  105. Yii::$app->end();
  106. }
  107. }
  108. }
  109. }