Blame view

common/models/ProjectSearch.php 5.77 KB
ddebd16b   Yarik   test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <?php
  
      namespace common\models;
  
      use Yii;
      use yii\base\Model;
      use yii\data\ActiveDataProvider;
      use common\models\Project;
  
      /**
       * ProjectSearch represents the model behind the search form about `common\models\Project`.
       */
      class ProjectSearch extends Project
      {
  
420d3dcd   Yarik   test
16
17
          public $date_add_from;
          public $date_add_to;
ddebd16b   Yarik   test
18
19
20
21
22
23
24
25
26
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'project_id',
ddebd16b   Yarik   test
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
                          'project_pid',
                          'user_add_id',
                          'payment_variant',
                          'deadline',
                          'contractual',
                      ],
                      'integer',
                  ],
                  [
                      [
                          'name',
                          'link',
                          'date_add',
                          'date_end',
                          'budget',
                          'city',
                          'street',
                          'house',
                          'description',
                          'file',
                          'specializationString',
420d3dcd   Yarik   test
48
49
                          'date_add_from',
                          'date_add_to',
ddebd16b   Yarik   test
50
51
52
53
54
55
56
                      ],
                      'safe',
                  ],
                  [
                      [ 'view_count' ],
                      'number',
                  ],
420d3dcd   Yarik   test
57
58
59
60
61
62
63
64
65
66
67
68
69
70
                  [
                      [
                          'date_add_from',
                      ],
                      'default',
                      'value' => date('Y-m-d', 0),
                  ],
                  [
                      [
                          'date_add_to',
                      ],
                      'default',
                      'value' => date('Y-m-d'),
                  ],
ddebd16b   Yarik   test
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
              ];
          }
  
          /**
           * @inheritdoc
           */
          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 = Project::find();
  
              // add conditions that should always apply here
  
              $dataProvider = new ActiveDataProvider([
                  'query' => $query,
              ]);
  
              $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;
              }
  
4c9663e0   Yarik   test
108
109
110
111
112
113
114
              $query2 = [ ];
              if(!empty( $this->specializationString )) {
                  $query2 = Project::find()
                                   ->select([ 'project.project_id' ], 'DISTINCT')
                                   ->joinWith([ 'specializations' ])
                                   ->where([
                                       'like',
420d3dcd   Yarik   test
115
116
                                       'LOWER(specialization_name)',
                                       mb_strtolower($this->specializationString),
4c9663e0   Yarik   test
117
118
119
120
                                   ])
                                   ->asArray()
                                   ->column();
              }
ddebd16b   Yarik   test
121
  
a02e2fdb   Yarik   test
122
123
              $query->andWhere([ 'user_id' => \Yii::$app->user->getId() ]);
  
ddebd16b   Yarik   test
124
125
126
              // grid filtering conditions
              $query->andFilterWhere([
                  'project_id'      => $this->project_id,
ddebd16b   Yarik   test
127
128
129
130
131
132
133
134
135
136
                  'project_pid'     => $this->project_pid,
                  'date_add'        => $this->date_add,
                  'date_end'        => $this->date_end,
                  'user_add_id'     => $this->user_add_id,
                  'view_count'      => $this->view_count,
                  'payment_variant' => $this->payment_variant,
                  'deadline'        => $this->deadline,
                  'contractual'     => $this->contractual,
              ]);
  
420d3dcd   Yarik   test
137
138
139
140
141
142
143
144
145
146
147
148
149
150
              $query->andFilterWhere([
                  'between',
                  'date_add',
                  $this->date_add_from,
                  (new \DateTime($this->date_add_to))->modify('+1 day')->format('Y-m-d'),
              ]);
  
              if(!empty($this->specializationString)) {
                  $query->andWhere(
                      [
                          'project_id' => $query2,
                      ]
                  );
              }
4c9663e0   Yarik   test
151
  
ddebd16b   Yarik   test
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
              $query->andFilterWhere([
                  'like',
                  'name',
                  $this->name,
              ])
                    ->andFilterWhere([
                        'like',
                        'link',
                        $this->link,
                    ])
                    ->andFilterWhere([
                        'like',
                        'budget',
                        $this->budget,
                    ])
                    ->andFilterWhere([
                        'like',
                        'city',
                        $this->city,
                    ])
                    ->andFilterWhere([
                        'like',
                        'street',
                        $this->street,
                    ])
                    ->andFilterWhere([
                        'like',
                        'house',
                        $this->house,
                    ])
                    ->andFilterWhere([
                        'like',
                        'description',
                        $this->description,
                    ])
                    ->andFilterWhere([
                        'like',
                        'file',
                        $this->file,
ddebd16b   Yarik   test
191
192
193
194
195
                    ]);
  
              return $dataProvider;
          }
      }