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.

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