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.

186 lines
5.2 KiB

  1. <?php
  2. namespace backend\modules\shop\models\searchs;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\ArrayHelper;
  6. use backend\modules\shop\models\ars\Comment;
  7. /**
  8. * CommentSearch represents the model behind the search form of `backend\modules\shop\models\ars\Comment`.
  9. */
  10. class CommentSearch extends Comment
  11. {
  12. /**
  13. * @return array
  14. * 增加创建时间查询字段
  15. */
  16. public function attributes()
  17. {
  18. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function rules()
  24. {
  25. return [
  26. [['id', 'user_id', 'order_goods_id', 'star', 'status', 'updated_at', 'created_at'], 'integer'],
  27. [['content'], 'safe'],
  28. ['created_at_range','safe'],
  29. ];
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function scenarios()
  35. {
  36. // bypass scenarios() implementation in the parent class
  37. return Model::scenarios();
  38. }
  39. /**
  40. * @return array
  41. * 列格式
  42. */
  43. public function columns()
  44. {
  45. return [
  46. 'id',
  47. 'user_id',
  48. [
  49. 'attribute' => 'order_goods_id',
  50. 'value' => function ($model) {
  51. return $model->orderGoods->goods_name;
  52. },
  53. 'label' => '商品名称'
  54. ],
  55. 'star',
  56. 'content',
  57. [
  58. 'attribute' => 'status',
  59. 'value' => function ($model) {
  60. return Comment::$commentStatus[$model->status];
  61. },
  62. 'label' => '状态'
  63. ],
  64. 'created_at:datetime',
  65. [
  66. 'class' => 'iron\grid\ActionColumn',
  67. 'align' => 'center',
  68. 'config' => [
  69. [
  70. 'name' => 'view',
  71. 'icon' => 'list',
  72. 'title' => '详情',
  73. ],
  74. [
  75. 'name' => 'display',
  76. 'icon' => 'eye',
  77. 'title' => '显示',
  78. 'hide' => [
  79. 'attributes' => [
  80. 'status'
  81. ],
  82. 'values' => [
  83. Comment::STATUS_DISPLAY
  84. ]
  85. ]
  86. ],
  87. [
  88. 'name' => 'hide',
  89. 'icon' => 'eye',
  90. 'title' => '隐藏',
  91. 'hide' => [
  92. 'attributes' => [
  93. 'status'
  94. ],
  95. 'values' => [
  96. Comment::STATUS_HIDE
  97. ]
  98. ]
  99. ],
  100. ]
  101. ],
  102. ];
  103. }
  104. /**
  105. * @param $params
  106. * @return ActiveDataProvider
  107. * 不分页的所有数据
  108. */
  109. public function allData($params)
  110. {
  111. $query = Comment::find();
  112. $dataProvider = new ActiveDataProvider([
  113. 'query' => $query,
  114. 'pagination' => false,
  115. 'sort' => false
  116. ]);
  117. $this->load($params);
  118. return $this->filter($query, $dataProvider);
  119. }
  120. /**
  121. * Creates data provider instance with search query applied
  122. *
  123. * @param array $params
  124. *
  125. * @return ActiveDataProvider
  126. */
  127. public function search($params)
  128. {
  129. $query = Comment::find();
  130. // add conditions that should always apply here
  131. $dataProvider = new ActiveDataProvider([
  132. 'query' => $query,
  133. 'pagination' => [
  134. 'pageSizeLimit' => [1, 200]
  135. ],
  136. 'sort' => [
  137. 'defaultOrder' => [
  138. 'id' => SORT_DESC,
  139. ]
  140. ],
  141. ]);
  142. $this->load($params);
  143. return $this->filter($query, $dataProvider);
  144. }
  145. /**
  146. * @param $query
  147. * @param $dataProvider
  148. * @return ActiveDataProvider
  149. * 条件筛选
  150. */
  151. private function filter($query, $dataProvider){
  152. if (!$this->validate()) {
  153. // uncomment the following line if you do not want to return any records when validation fails
  154. // $query->where('0=1');
  155. return $dataProvider;
  156. }
  157. // grid filtering conditions
  158. $query->andFilterWhere([
  159. 'id' => $this->id,
  160. 'user_id' => $this->user_id,
  161. 'order_goods_id' => $this->order_goods_id,
  162. 'star' => $this->star,
  163. 'status' => $this->status,
  164. 'updated_at' => $this->updated_at,
  165. 'created_at' => $this->created_at,
  166. ]);
  167. $query->andFilterWhere(['like', 'content', $this->content]);
  168. if ($this->created_at_range) {
  169. $arr = explode(' ~ ', $this->created_at_range);
  170. $start = strtotime($arr[0]);
  171. $end = strtotime($arr[1]) + 3600 * 24;
  172. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  173. }
  174. return $dataProvider;
  175. }
  176. }