You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.6 KiB

  1. <?php
  2. namespace backend\modules\shop\controllers;
  3. use Yii;
  4. use backend\modules\shop\models\ars\Comment;
  5. use backend\modules\shop\models\searchs\CommentSearch;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. use iron\widget\Excel;
  10. use yii\web\Response;
  11. /**
  12. * CommentController implements the CRUD actions for Comment model.
  13. */
  14. class CommentController extends Controller
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function behaviors()
  20. {
  21. return [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. ];
  29. }
  30. /**
  31. * Lists all Comment models.
  32. * @return mixed
  33. */
  34. public function actionIndex()
  35. {
  36. $searchModel = new CommentSearch();
  37. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  38. return $this->render('index', [
  39. 'searchModel' => $searchModel,
  40. 'dataProvider' => $dataProvider,
  41. 'columns' => $searchModel->columns()
  42. ]);
  43. }
  44. /**
  45. * Displays a single Comment model.
  46. * @param integer $id
  47. * @return mixed
  48. * @throws NotFoundHttpException if the model cannot be found
  49. */
  50. public function actionView($id)
  51. {
  52. return $this->render('view', [
  53. 'model' => $this->findModel($id),
  54. ]);
  55. }
  56. /**
  57. * Creates a new Comment model.
  58. * If creation is successful, the browser will be redirected to the 'view' page.
  59. * @return mixed
  60. */
  61. public function actionCreate()
  62. {
  63. $model = new Comment();
  64. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  65. return $this->redirect('index');
  66. }
  67. return $this->render('create', [
  68. 'model' => $model,
  69. ]);
  70. }
  71. /**
  72. * Updates an existing Comment model.
  73. * If update is successful, the browser will be redirected to the 'view' page.
  74. * @param integer $id
  75. * @return mixed
  76. * @throws NotFoundHttpException if the model cannot be found
  77. */
  78. public function actionUpdate($id)
  79. {
  80. $model = $this->findModel($id);
  81. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  82. return $this->redirect('index');
  83. }
  84. return $this->render('update', [
  85. 'model' => $model,
  86. ]);
  87. }
  88. /**
  89. * Deletes an existing Comment model.
  90. * If deletion is successful, the browser will be redirected to the 'index' page.
  91. * @param integer $id
  92. * @return mixed
  93. * @throws NotFoundHttpException if the model cannot be found
  94. */
  95. public function actionDelete($id)
  96. {
  97. $this->findModel($id)->delete();
  98. return $this->redirect(['index']);
  99. }
  100. /**
  101. * Finds the Comment model based on its primary key value.
  102. * If the model is not found, a 404 HTTP exception will be thrown.
  103. * @param integer $id
  104. * @return Comment the loaded model
  105. * @throws NotFoundHttpException if the model cannot be found
  106. */
  107. protected function findModel($id)
  108. {
  109. if (($model = Comment::findOne($id)) !== null) {
  110. return $model;
  111. }
  112. throw new NotFoundHttpException('The requested page does not exist.');
  113. }
  114. /**
  115. * @author iron
  116. * 文件导出
  117. */
  118. public function actionExport()
  119. {
  120. $searchModel = new CommentSearch();
  121. $params = Yii::$app->request->queryParams;
  122. if ($params['page-type'] == 'all') {
  123. $dataProvider = $searchModel->allData($params);
  124. } else {
  125. $dataProvider = $searchModel->search($params);
  126. }
  127. Excel::export([
  128. 'models' => $dataProvider->getModels(),
  129. 'format' => 'Xlsx',
  130. 'asAttachment' => true,
  131. 'fileName' =>'Comments'. "-" .date('Y-m-d H/i/s', time()),
  132. 'columns' => $searchModel->columns()
  133. ]);
  134. }
  135. /**
  136. * 显示评论
  137. * @param $id
  138. * @return Response
  139. * @throws NotFoundHttpException
  140. */
  141. public function actionDisplay($id)
  142. {
  143. $model = $this->findModel($id);
  144. $model->status = Comment::STATUS_DISPLAY;
  145. $model->save();
  146. return $this->redirect(['index']);
  147. }
  148. /**
  149. * 隐藏评论
  150. * @param $id
  151. * @return Response
  152. * @throws NotFoundHttpException
  153. */
  154. public function actionHide($id)
  155. {
  156. $model = $this->findModel($id);
  157. $model->status = Comment::STATUS_HIDE;
  158. $model->save();
  159. return $this->redirect(['index']);
  160. }
  161. }