Blame view

common/models/ProjectSearch.php 6.39 KB
eb7e82fb   Administrator   29.02.16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  <?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

      {

  

          public $date_add_from;

          public $date_add_to;

492ed7f7   Yarik   test
18
          public $budget_approx;

eb7e82fb   Administrator   29.02.16
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [

                          'project_id',

                          'project_pid',

                          'user_add_id',

                          'payment_variant',

                          'deadline',

                          'contractual',

                      ],

                      'integer',

                  ],

                  [

                      [

                          'name',

                          'link',

                          'date_add',

                          'date_end',

                          'budget',

492ed7f7   Yarik   test
43
                          'budget_approx',

eb7e82fb   Administrator   29.02.16
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
                          'city',

                          'street',

                          'house',

                          'description',

                          'file',

                          'specializationString',

                          'date_add_from',

                          'date_add_to',

                      ],

                      'safe',

                  ],

                  [

                      [ 'view_count' ],

                      'number',

                  ],

                  [

                      [

                          'date_add_from',

                      ],

                      'default',

                      'value' => date('Y-m-d', 0),

                  ],

                  [

                      [

                          'date_add_to',

                      ],

                      'default',

                      'value' => date('Y-m-d'),

                  ],

              ];

          }

  

          /**

           * @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)

          {

ce4a4468   Yarik   test
94
              $query = Project::find()->with('comments');

eb7e82fb   Administrator   29.02.16
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
  

              // 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;

              }

  

              $query2 = [ ];

              if(!empty( $this->specializationString )) {

                  $query2 = Project::find()

                                   ->select([ 'project.project_id' ], 'DISTINCT')

                                   ->joinWith([ 'specializations' ])

                                   ->where([

                                       'like',

                                       'LOWER(specialization_name)',

                                       mb_strtolower($this->specializationString),

                                   ])

                                   ->asArray()

                                   ->column();

              }

  

              $query->andWhere([ 'user_id' => \Yii::$app->user->getId() ]);

  

              // grid filtering conditions

              $query->andFilterWhere([

                  'project_id'      => $this->project_id,

                  '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,

eb7e82fb   Administrator   29.02.16
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
              ]);

  

              $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,

                      ]

                  );

              }

  

              $query->andFilterWhere([

                  'like',

ce4a4468   Yarik   test
155
156
                  'LOWER(name)',

                  mb_strtolower($this->name),

eb7e82fb   Administrator   29.02.16
157
158
159
              ])

                    ->andFilterWhere([

                        'like',

eb7e82fb   Administrator   29.02.16
160
161
162
163
164
                        'budget',

                        $this->budget,

                    ])

                    ->andFilterWhere([

                        'like',

ce4a4468   Yarik   test
165
166
                        'LOWER(city)',

                        mb_strtolower($this->city),

eb7e82fb   Administrator   29.02.16
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
                    ])

                    ->andFilterWhere([

                        'like',

                        'street',

                        $this->street,

                    ])

                    ->andFilterWhere([

                        'like',

                        'house',

                        $this->house,

                    ])

                    ->andFilterWhere([

                        'like',

                        'description',

                        $this->description,

                    ])

                    ->andFilterWhere([

                        'like',

                        'file',

                        $this->file,

                    ]);

  

492ed7f7   Yarik   test
189
190
191
192
193
              if(!empty($this->budget_approx)) {

                  $query->andWhere(['between', 'budget', ($this->budget_approx - ($this->budget_approx/10)), ($this->budget_approx + ($this->budget_approx/10))]);

                  $query->andWhere(['not', ['contractual' => 1]]);

              }

  

ce4a4468   Yarik   test
194
195
196
197
198
199
              if($this->contractual == 1) {

                  $query->andWhere(['contractual' => 1]);

              } elseif($this->contractual == 2) {

                  $query->andWhere(['not', ['contractual' => 1]]);

              }

  

eb7e82fb   Administrator   29.02.16
200
201
202
              return $dataProvider;

          }

      }