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
4.8 KiB

  1. <?php
  2. namespace antgoods\models\searchs;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\ArrayHelper;
  6. use antgoods\models\ars\Category;
  7. use yii\bootstrap4\Html;
  8. /**
  9. * CategorySearch represents the model behind the search form of `antgoods\models\ars\Category`.
  10. */
  11. class CategorySearch extends Category
  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'], '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. 'config' => [
  71. [
  72. 'name' => 'update',
  73. 'icon' => 'pencil',
  74. 'title' => '修改'
  75. ],
  76. [
  77. 'name' => 'delete',
  78. 'icon' => 'trash',
  79. 'title' => '删除',
  80. 'contents' => '确定删除?'
  81. ]
  82. ],
  83. ],
  84. ];
  85. }
  86. /**
  87. * @param $params
  88. * @return ActiveDataProvider
  89. * 不分页的所有数据
  90. */
  91. public function allData($params)
  92. {
  93. $query = Category::find();
  94. $dataProvider = new ActiveDataProvider([
  95. 'query' => $query,
  96. 'pagination' => false,
  97. 'sort' => false
  98. ]);
  99. $this->load($params);
  100. return $this->filter($query, $dataProvider);
  101. }
  102. /**
  103. * Creates data provider instance with search query applied
  104. *
  105. * @param array $params
  106. *
  107. * @return ActiveDataProvider
  108. */
  109. public function search($params)
  110. {
  111. $query = Category::find()->where(['is_delete' => Category::IS_DELETE_NO]);
  112. // add conditions that should always apply here
  113. $dataProvider = new ActiveDataProvider([
  114. 'query' => $query,
  115. 'pagination' => [
  116. 'pageSizeLimit' => [1, 200]
  117. ],
  118. 'sort' => [
  119. 'defaultOrder' => [
  120. 'id' => SORT_DESC,
  121. ]
  122. ],
  123. ]);
  124. $this->load($params);
  125. return $this->filter($query, $dataProvider);
  126. }
  127. /**
  128. * @param $query
  129. * @param $dataProvider
  130. * @return ActiveDataProvider
  131. * 条件筛选
  132. */
  133. private function filter($query, $dataProvider){
  134. $query->andFilterWhere(['is_delete' => Category::IS_DELETE_NO]);
  135. if (!$this->validate()) {
  136. // uncomment the following line if you do not want to return any records when validation fails
  137. // $query->where('0=1');
  138. return $dataProvider;
  139. }
  140. // grid filtering conditions
  141. $query->andFilterWhere([
  142. 'id' => $this->id,
  143. 'pid' => $this->pid,
  144. 'goods_count' => $this->goods_count,
  145. 'sort_order' => $this->sort_order,
  146. 'is_show' => $this->is_show,
  147. 'is_delete' => $this->is_delete,
  148. 'created_at' => $this->created_at,
  149. 'updated_at' => $this->updated_at,
  150. 'icon' => $this->icon,
  151. ]);
  152. $query->andFilterWhere(['like', 'name', $this->name]);
  153. if ($this->created_at_range) {
  154. $arr = explode(' ~ ', $this->created_at_range);
  155. $start = strtotime($arr[0]);
  156. $end = strtotime($arr[1]) + 3600 * 24;
  157. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  158. }
  159. return $dataProvider;
  160. }
  161. }