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.

151 lines
3.9 KiB

  1. <?php
  2. namespace backend\controllers;
  3. use common\models\Category;
  4. use common\models\User;
  5. use Yii;
  6. use common\models\ars\Goods;
  7. use common\models\searchs\GoodsSearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. /**
  12. * GoodsController implements the CRUD actions for Goods model.
  13. */
  14. class GoodsController 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 Goods models.
  32. * @return mixed
  33. */
  34. public function actionIndex()
  35. {
  36. $searchModel = new GoodsSearch();
  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 Goods 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 Goods 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 Goods();
  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 Goods 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 Goods 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 Goods 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 Goods the loaded model
  105. * @throws NotFoundHttpException if the model cannot be found
  106. */
  107. protected function findModel($id)
  108. {
  109. if (($model = Goods::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 GoodsSearch();
  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. \iron\widget\Excel::export([
  128. 'models' => $dataProvider->getModels(),
  129. 'format' => 'Xlsx',
  130. 'asAttachment' => true,
  131. 'fileName' =>'Goods'. "-" .date('Y-m-d H/i/s', time()),
  132. 'columns' => $searchModel->columns()
  133. ]);
  134. }
  135. }