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.

159 lines
4.3 KiB

  1. <?php
  2. namespace backend\modules\goods\controllers;
  3. use backend\modules\goods\logic\goods\GoodsManager;
  4. use Yii;
  5. use backend\modules\goods\models\ars\Attribute;
  6. use backend\modules\goods\models\searchs\AttributeSearch;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use iron\widget\Excel;
  11. /**
  12. * AttributeController implements the CRUD actions for Attribute model.
  13. */
  14. class AttributeController 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 Attribute models.
  32. * @return mixed
  33. */
  34. public function actionIndex()
  35. {
  36. $searchModel = new AttributeSearch();
  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 Attribute 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 Attribute 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 Attribute();
  64. $model->sort_order = 0;
  65. $model->cat_id = 0;
  66. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  67. if (GoodsManager::updateAttribute($model)) {
  68. return $this->redirect(['index']);
  69. }
  70. }
  71. return $this->render('create', [
  72. 'model' => $model,
  73. ]);
  74. }
  75. /**
  76. * Updates an existing Attribute model.
  77. * If update is successful, the browser will be redirected to the 'view' page.
  78. * @param integer $id
  79. * @return mixed
  80. * @throws NotFoundHttpException if the model cannot be found
  81. */
  82. public function actionUpdate($id)
  83. {
  84. $model = $this->findModel($id);
  85. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  86. if (GoodsManager::updateAttribute($model)) {
  87. return $this->redirect(['index']);
  88. }
  89. }
  90. return $this->render('update', [
  91. 'model' => $model,
  92. ]);
  93. }
  94. /**
  95. * Deletes an existing Attribute model.
  96. * If deletion is successful, the browser will be redirected to the 'index' page.
  97. * @param integer $id
  98. * @return mixed
  99. * @throws NotFoundHttpException if the model cannot be found
  100. */
  101. public function actionDelete($id)
  102. {
  103. $model = $this->findModel($id);
  104. $model->is_delete = Attribute::IS_DELETE_YES;
  105. $model->save();
  106. return $this->redirect(['index']);
  107. }
  108. /**
  109. * Finds the Attribute model based on its primary key value.
  110. * If the model is not found, a 404 HTTP exception will be thrown.
  111. * @param integer $id
  112. * @return Attribute the loaded model
  113. * @throws NotFoundHttpException if the model cannot be found
  114. */
  115. protected function findModel($id)
  116. {
  117. if (($model = Attribute::findOne($id)) !== null) {
  118. return $model;
  119. }
  120. throw new NotFoundHttpException('The requested page does not exist.');
  121. }
  122. /**
  123. * @author iron
  124. * 文件导出
  125. */
  126. public function actionExport()
  127. {
  128. $searchModel = new AttributeSearch();
  129. $params = Yii::$app->request->queryParams;
  130. if ($params['page-type'] == 'all') {
  131. $dataProvider = $searchModel->allData($params);
  132. } else {
  133. $dataProvider = $searchModel->search($params);
  134. }
  135. Excel::export([
  136. 'models' => $dataProvider->getModels(),
  137. 'format' => 'Xlsx',
  138. 'asAttachment' => true,
  139. 'fileName' =>'Attributes'. "-" .date('Y-m-d H/i/s', time()),
  140. 'columns' => $searchModel->columns()
  141. ]);
  142. }
  143. }