Blame view

common/models/CustomerSearch.php 7.35 KB
658a5f37   Yarik   test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  <?php
  
      namespace common\models;
  
      use Yii;
      use yii\base\Model;
      use yii\data\ActiveDataProvider;
      use common\models\User;
  
      /**
       * CustomerSearch represents the model behind the search form about `common\models\UserInfo`.
       */
      class CustomerSearch extends User
      {
  
          public $type;
  
          public $rating;
  
          public $online;
  
          public $city;
  
          public $info;
  
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'type',
658a5f37   Yarik   test
35
36
37
38
39
                          'online',
                      ],
                      'integer',
                  ],
                  [
c0bffebb   Yarik   test
40
                      [ 'rating' ],
90924316   Yarik   test
41
42
43
44
45
                      'number',
                      'min' => 0,
                      'max' => 5,
                  ],
                  [
c0bffebb   Yarik   test
46
                      [ 'rating' ],
90924316   Yarik   test
47
48
49
50
                      'default',
                      'value' => 0,
                  ],
                  [
658a5f37   Yarik   test
51
52
53
54
55
56
57
58
59
60
61
62
                      [
                          'city',
                          'info',
                      ],
                      'safe',
                  ],
              ];
          }
  
          /**
           * @inheritdoc
           */
f94a00a6   Yarik   test
63
64
65
          public function attributeLabels()
          {
              return [
06ec2844   Administrator   28.03.16
66
67
68
69
70
                  'type'   => Yii::t('app', 'type'),
                  'rating' => Yii::t('app', 'rating'),
                  'online' => Yii::t('app', 'online'),
                  'city'   => Yii::t('app', 'city'),
                  'info'   => Yii::t('app', 'info'),
f94a00a6   Yarik   test
71
72
73
74
75
76
              ];
          }
  
          /**
           * @inheritdoc
           */
658a5f37   Yarik   test
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
          public function scenarios()
          {
              // bypass scenarios() implementation in the parent class
              return Model::scenarios();
          }
  
          /**
           * Creates data provider instance with search query applied
           *
           * @param array $params
           *
           * @return ActiveDataProvider
           */
          public function search($params)
          {
              $query = User::find()
                           ->joinWith('userInfo')
                           ->joinWith('companyInfo');
  
              $dataProvider = new ActiveDataProvider([
                  'query' => $query,
              ]);
  
b5de253e   Yarik   test
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
              $dataProvider->setSort([
                  'defaultOrder' => [
                      'name' => SORT_ASC,
                  ],
                  'attributes'   => [
                      'name'  => [
                          'asc'     => [
                              'company_info.name' => SORT_ASC,
                              'firstname'         => SORT_ASC,
                              'lastname'          => SORT_ASC,
                          ],
                          'desc'    => [
                              'company_info.name' => SORT_DESC,
                              'firstname'         => SORT_DESC,
                              'lastname'          => SORT_DESC,
                          ],
                          'default' => SORT_ASC,
                          'label'   => 'Название',
                      ],
                      'staff' => [
                          'asc'     => [
                              'company_info.staff' => SORT_ASC,
                          ],
                          'desc'    => [
                              'company_info.staff' => SORT_DESC,
                          ],
                          'default' => SORT_DESC,
                          'label'   => 'Количество сотрудников',
                      ],
                      'visit' => [
                          'asc'     => [
                              'user_info.date_visit' => SORT_ASC,
                          ],
                          'desc'    => [
                              'user_info.date_visit' => SORT_DESC,
                          ],
                          'default' => SORT_DESC,
                          'label'   => 'Последний визит',
                      ],
                      'city'  => [
                          'asc'     => [
                              'user_info.city' => SORT_ASC,
                          ],
                          'desc'    => [
                              'user_info.city' => SORT_DESC,
                          ],
                          'default' => SORT_ASC,
                          'label'   => 'Город',
                      ],
                  ],
              ]);
  
658a5f37   Yarik   test
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
              $this->load($params);
  
              if(!$this->validate()) {
                  // uncomment the following line if you do not want to return any records when validation fails
                  // $query->where('0=1');
                  return $dataProvider;
              }
  
              $query->andWhere([
                  'is_customer' => 1,
              ]);
  
              if($this->type == 2) {
                  $query->andWhere([
                      'not',
                      [ 'company_info.company_info_id' => NULL ],
                  ]);
  
                  $query->andWhere([
                      'type' => 2,
                  ]);
f94a00a6   Yarik   test
173
              } elseif($this->type == 1) {
658a5f37   Yarik   test
174
                  $query->andWhere([
983116f0   Yarik   test
175
                      'type' => 1,
658a5f37   Yarik   test
176
177
178
179
180
181
182
183
184
185
186
187
                  ]);
              }
  
              if($this->online == 1) {
                  $query->andWhere([
                      '>=',
                      'user_info.date_visit',
                      date('Y-m-d H:i:s.u', time() - 1800),
                  ]);
              }
  
              $query->andFilterWhere([
c0bffebb   Yarik   test
188
189
190
                  '>=',
                  'user_info.rating',
                  $this->rating,
2fd40ee7   Yarik   test
191
192
193
              ]);
  
              $query->andFilterWhere([
c0bffebb   Yarik   test
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
                  'user_info.city' => $this->city,
              ])
                    ->andFilterWhere([
                        'or',
                        [
                            'like',
                            'LOWER(username)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(lastname)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(firstname)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(middlename)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(company_info.name)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(company_info.street)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(user_info.country)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(user_info.city)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(user_info.about)',
                            mb_strtolower($this->info),
                        ],
                    ]);
658a5f37   Yarik   test
244
245
246
247
  
              return $dataProvider;
          }
      }