CarouselItemController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\Carousel;
  4. use common\models\CarouselItem;
  5. use Yii;
  6. use yii\filters\VerbFilter;
  7. use yii\helpers\Url;
  8. use yii\web\Controller;
  9. use yii\web\HttpException;
  10. use yii\web\NotFoundHttpException;
  11. /**
  12. * CarouselItemController implements the CRUD actions for CarouselItem model.
  13. */
  14. class CarouselItemController extends Controller
  15. {
  16. public function getViewPath()
  17. {
  18. return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'carousel/item';
  19. }
  20. public function behaviors()
  21. {
  22. return [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. 'delete' => ['post'],
  27. ],
  28. ],
  29. ];
  30. }
  31. public function actions()
  32. {
  33. return [
  34. 'position' => [
  35. 'class' => 'backend\actions\Position',
  36. 'returnUrl' => function($model){
  37. return Url::to(['/carousel/update', 'id' => $model->carousel_id]);
  38. }
  39. ],
  40. 'switcher' => [
  41. 'class' => 'backend\widgets\grid\SwitcherAction'
  42. ]
  43. ];
  44. }
  45. /**
  46. * Creates a new CarouselItem model.
  47. * If creation is successful, the browser will be redirected to the 'view' page.
  48. * @param integer $carousel_id
  49. * @return mixed
  50. * @throws HttpException
  51. */
  52. public function actionCreate($carousel_id)
  53. {
  54. $model = new CarouselItem();
  55. $carousel = Carousel::findOne($carousel_id);
  56. if (!$carousel) {
  57. throw new HttpException(400);
  58. }
  59. $model->loadDefaultValues();
  60. $model->carousel_id = $carousel->id;
  61. if ($model->load(Yii::$app->request->post())) {
  62. if ($model->save()) {
  63. Yii::$app->getSession()->setFlash('success', '操作成功');
  64. return $this->redirect(['/carousel/update', 'id' => $model->carousel_id]);
  65. }
  66. }
  67. return $this->render('create', [
  68. 'model' => $model,
  69. 'carousel' => $carousel,
  70. ]);
  71. }
  72. /**
  73. * Updates an existing CarouselItem model.
  74. * If update is successful, the browser will be redirected to the 'view' page.
  75. * @param integer $id
  76. * @return mixed
  77. */
  78. public function actionUpdate($id)
  79. {
  80. $model = $this->findModel($id);
  81. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  82. Yii::$app->getSession()->setFlash('success', '操作成功');
  83. return $this->redirect(['/carousel/update', 'id' => $model->carousel_id]);
  84. }
  85. return $this->render('update', [
  86. 'model' => $model,
  87. ]);
  88. }
  89. /**
  90. * Deletes an existing CarouselItem model.
  91. * If deletion is successful, the browser will be redirected to the 'index' page.
  92. * @param integer $id
  93. * @return mixed
  94. */
  95. public function actionDelete($id)
  96. {
  97. $model = $this->findModel($id);
  98. if ($model->delete()) {
  99. return $this->redirect(['/carousel/update', 'id'=>$model->carousel_id]);
  100. };
  101. }
  102. /**
  103. * Finds the CarouselItem model based on its primary key value.
  104. * If the model is not found, a 404 HTTP exception will be thrown.
  105. * @param integer $id
  106. * @return CarouselItem the loaded model
  107. * @throws NotFoundHttpException if the model cannot be found
  108. */
  109. public function findModel($id)
  110. {
  111. if (($model = CarouselItem::findOne($id)) !== null) {
  112. return $model;
  113. } else {
  114. throw new NotFoundHttpException('The requested page does not exist.');
  115. }
  116. }
  117. }