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.4 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\ExpressTemplate;
  7. /**
  8. * ExpressTemplateSearch represents the model behind the search form of ` backend\modules\shop\models\ars\ExpressTemplate`.
  9. */
  10. class ExpressTemplateSearch extends ExpressTemplate
  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', 'calculation_type', 'updated_at', 'created_at'], 'integer'],
  27. [['name'], '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. [
  47. 'class' => 'blobt\grid\CheckboxColumn',
  48. 'width' => '2%',
  49. 'align' => 'center'
  50. ],
  51. 'id',
  52. 'name',
  53. [
  54. 'attribute' => 'calculation_type',
  55. 'value' => function ($model) {
  56. return ExpressTemplate::$calculationType[$model->calculation_type];
  57. }
  58. ],
  59. [
  60. 'class' => 'iron\grid\ActionColumn',
  61. 'align' => 'center',
  62. 'config' => [
  63. [
  64. 'name' => 'update',
  65. 'icon' => 'pencil',
  66. 'title' => '修改'
  67. ],
  68. [
  69. 'name' => 'express-area-list',
  70. 'icon' => 'hard-drive',
  71. 'title' => '配送区域'
  72. ],
  73. [
  74. 'name' => 'delete',
  75. 'icon' => 'trash',
  76. 'title' => '删除',
  77. 'contents' => '确定删除?'
  78. ]
  79. ],
  80. ],
  81. ];
  82. }
  83. /**
  84. * @param $params
  85. * @return ActiveDataProvider
  86. * 不分页的所有数据
  87. */
  88. public function allData($params)
  89. {
  90. $query = ExpressTemplate::find();
  91. $dataProvider = new ActiveDataProvider([
  92. 'query' => $query,
  93. 'pagination' => false,
  94. 'sort' => false
  95. ]);
  96. $this->load($params);
  97. return $this->filter($query, $dataProvider);
  98. }
  99. /**
  100. * Creates data provider instance with search query applied
  101. *
  102. * @param array $params
  103. *
  104. * @return ActiveDataProvider
  105. */
  106. public function search($params)
  107. {
  108. $query = ExpressTemplate::find();
  109. // add conditions that should always apply here
  110. $dataProvider = new ActiveDataProvider([
  111. 'query' => $query,
  112. 'pagination' => [
  113. 'pageSizeLimit' => [1, 200]
  114. ],
  115. 'sort' => [
  116. 'defaultOrder' => [
  117. 'id' => SORT_DESC,
  118. ]
  119. ],
  120. ]);
  121. $this->load($params);
  122. return $this->filter($query, $dataProvider);
  123. }
  124. /**
  125. * @param $query
  126. * @param $dataProvider
  127. * @return ActiveDataProvider
  128. * 条件筛选
  129. */
  130. private function filter($query, $dataProvider){
  131. if (!$this->validate()) {
  132. // uncomment the following line if you do not want to return any records when validation fails
  133. // $query->where('0=1');
  134. return $dataProvider;
  135. }
  136. // grid filtering conditions
  137. $query->andFilterWhere([
  138. 'id' => $this->id,
  139. 'calculation_type' => $this->calculation_type,
  140. 'updated_at' => $this->updated_at,
  141. 'created_at' => $this->created_at,
  142. ]);
  143. $query->andFilterWhere(['like', 'name', $this->name]);
  144. if ($this->created_at_range) {
  145. $arr = explode(' ~ ', $this->created_at_range);
  146. $start = strtotime($arr[0]);
  147. $end = strtotime($arr[1]) + 3600 * 24;
  148. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  149. }
  150. return $dataProvider;
  151. }
  152. }