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.

162 lines
4.5 KiB

  1. <?php
  2. namespace antgoods\models\searchs;
  3. use yii\base\Model;
  4. use yii\bootstrap4\Html;
  5. use yii\data\ActiveDataProvider;
  6. use yii\helpers\ArrayHelper;
  7. use antgoods\models\ars\ShopCategory;
  8. /**
  9. * ShopCategorySearch represents the model behind the search form of `antgoods\models\ars\ShopCategory`.
  10. */
  11. class ShopCategorySearch extends ShopCategory
  12. {
  13. /**
  14. * @return array
  15. * 增加创建时间查询字段
  16. */
  17. public function attributes()
  18. {
  19. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function rules()
  25. {
  26. return [
  27. [['id', 'pid', 'goods_count', 'sort_order', 'is_show', 'is_delete', 'created_at', 'updated_at', 'icon'], 'integer'],
  28. [['name', 'keywords', 'desc', 'filter_attr'], 'safe'],
  29. ['created_at_range','safe'],
  30. ];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function scenarios()
  36. {
  37. // bypass scenarios() implementation in the parent class
  38. return Model::scenarios();
  39. }
  40. /**
  41. * @return array
  42. * 列格式
  43. */
  44. public function columns()
  45. {
  46. return [
  47. [
  48. 'class' => 'blobt\grid\CheckboxColumn',
  49. 'width' => '2%',
  50. 'align' => 'center'
  51. ],
  52. 'id',
  53. 'name',
  54. ['attribute' => 'icon',
  55. 'contentOptions' => [
  56. 'align' => 'center',
  57. ],
  58. 'width'=>'10%',
  59. 'format' => 'raw',
  60. 'value' => function ($model) {
  61. return $model->iconFile ?
  62. Html::img(['/'.$model->iconFile->path], ['style' => 'width:80px'])
  63. : '<div class="table_not_setting">未设置</div>';
  64. }
  65. ],
  66. 'sort_order',
  67. [
  68. 'class' => 'iron\grid\ActionColumn',
  69. 'align' => 'center',
  70. ],
  71. ];
  72. }
  73. /**
  74. * @param $params
  75. * @return ActiveDataProvider
  76. * 不分页的所有数据
  77. */
  78. public function allData($params)
  79. {
  80. $query = ShopCategory::find();
  81. $dataProvider = new ActiveDataProvider([
  82. 'query' => $query,
  83. 'pagination' => false,
  84. 'sort' => false
  85. ]);
  86. $this->load($params);
  87. return $this->filter($query, $dataProvider);
  88. }
  89. /**
  90. * Creates data provider instance with search query applied
  91. *
  92. * @param array $params
  93. *
  94. * @return ActiveDataProvider
  95. */
  96. public function search($params)
  97. {
  98. $query = ShopCategory::find()
  99. ->where(['is_delete' => ShopCategory::IS_DELETE_NO]);
  100. // add conditions that should always apply here
  101. $dataProvider = new ActiveDataProvider([
  102. 'query' => $query,
  103. 'pagination' => [
  104. 'pageSizeLimit' => [1, 200]
  105. ],
  106. 'sort' => [
  107. 'defaultOrder' => [
  108. 'id' => SORT_DESC,
  109. ]
  110. ],
  111. ]);
  112. $this->load($params);
  113. return $this->filter($query, $dataProvider);
  114. }
  115. /**
  116. * @param $query
  117. * @param $dataProvider
  118. * @return ActiveDataProvider
  119. * 条件筛选
  120. */
  121. private function filter($query, $dataProvider){
  122. if (!$this->validate()) {
  123. // uncomment the following line if you do not want to return any records when validation fails
  124. // $query->where('0=1');
  125. return $dataProvider;
  126. }
  127. // grid filtering conditions
  128. $query->andFilterWhere([
  129. 'id' => $this->id,
  130. 'pid' => $this->pid,
  131. 'goods_count' => $this->goods_count,
  132. 'sort_order' => $this->sort_order,
  133. 'icon' => $this->icon,
  134. 'is_show' => $this->is_show,
  135. 'is_delete' => $this->is_delete,
  136. 'created_at' => $this->created_at,
  137. 'updated_at' => $this->updated_at,
  138. ]);
  139. $query->andFilterWhere(['like', 'name', $this->name])
  140. ->andFilterWhere(['like', 'keywords', $this->keywords])
  141. ->andFilterWhere(['like', 'desc', $this->desc])
  142. ->andFilterWhere(['like', 'filter_attr', $this->filter_attr]);
  143. if ($this->created_at_range) {
  144. $arr = explode(' ~ ', $this->created_at_range);
  145. $start = strtotime($arr[0]);
  146. $end = strtotime($arr[1]) + 3600 * 24;
  147. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  148. }
  149. return $dataProvider;
  150. }
  151. }