CarouselController.php 3.2 KB

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