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.

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