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.

172 lines
5.0 KiB

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