SearchPerformerForm.php
5.98 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace frontend\models;
use common\models\Portfolio;
use common\models\User;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
/**
* ContactForm is the model behind the contact form.
*/
class SearchPerformerForm extends Model
{
public $city;
public $specialization;
public $working_conditions;
public $type;
public $additional_parameters;
public $rating;
public $online;
public $search;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['specialization','city', 'type','additional_parameters','working_conditions', 'rating','online','search'], 'safe'],
[
['rating'],
'default',
'value' => 0,
]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'city' => 'Город',
'specialization' => 'Специализация',
'type' => 'Тип исполнителя',
'additional_parameters' => 'Тип исполнителя',
'working_conditions' => 'Условия работы',
'rating' => 'Рейтинг',
'online' => 'Статус',
'search' => 'Найти'
];
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$this->load($params);
$query = User::find()
->select(['user.*', 'company_info.*','user_info.*'])
->distinct(true)
->joinWith(['userInfo','specializations','companyInfo'])
->where(['user_info.is_freelancer' => 1]);
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$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' => 'Название',
],
'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' => 'Город',
],
],
]);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'user_info.city' => $this->city,
'specialization.specialization_id' => $this->specialization,
'user.type' => $this->type,
]);
$query->andFilterWhere([
'>=', 'user_info.rating', $this->rating,
]);
if($this->online == 1) {
$query->andWhere([
'>=',
'user_info.date_visit',
date('Y-m-d H:i:s.u', time() - 1800),
]);
}
if(!empty($this->working_conditions)){
foreach($this->working_conditions as $working_conditions){
if($working_conditions=='guarantee'){
$query->andWhere([
'<>',
'user_info.guarantee',
''
]);
}else if($working_conditions=='prepayment'){
$query->andWhere([
'user_info.prepayment'=>0,
]);
}else {
$query->andFilterWhere([
'user_info.'.$working_conditions => 1,
]);
}
}
}
if(!empty($this->additional_parameters)){
foreach($this->additional_parameters as $additional_parameters){
switch ($additional_parameters) {
case 'with_portfolio':
$query->andFilterWhere([
'user.id'=>ArrayHelper::toArray(Portfolio::find()->select('user_id')->column()),
]);
break;
case 'with_comments':
$query->andFilterWhere([
'user.id'=>ArrayHelper::toArray(Portfolio::find()->select('user_id')->column()),
]);
break;
case 'only_free':
$query->andFilterWhere([
'user_info.busy'=>0,
]);
break;
}
}
}
if(!empty( $this->search)){
$query->andFilterWhere([
'or',
['like', 'user.firstname', $this->search],
['like', 'user.lastname', $this->search],
['like', 'company_info.name', $this->search]
]);
}
return $dataProvider;
}
}