Blame view

common/modules/product/models/ProductSearch.php 5.9 KB
d8c1a2e0   Yarik   Big commit artbox
1
  <?php

d55d2fe0   Yarik   Multilanguage
2
      

af036678   Yarik   Image behaviors
3
4
5
6
7
      namespace common\modules\product\models;

      

      use yii\base\Model;

      use yii\data\ActiveDataProvider;

      use yii\db\ActiveQuery;

36d1807a   Yarik   Big commit.
8
      

d8c1a2e0   Yarik   Big commit artbox
9
      /**

af036678   Yarik   Image behaviors
10
11
       * ProductSearch represents the model behind the search form about

       * `common\modules\product\models\Product`.

d8c1a2e0   Yarik   Big commit artbox
12
       */

af036678   Yarik   Image behaviors
13
      class ProductSearch extends Product

d8c1a2e0   Yarik   Big commit artbox
14
      {

af036678   Yarik   Image behaviors
15
          

8af13427   Yarik   For leha commit.
16
          public $categoryId;

af036678   Yarik   Image behaviors
17
          

8af13427   Yarik   For leha commit.
18
          public $productName;

af036678   Yarik   Image behaviors
19
          

8af13427   Yarik   For leha commit.
20
          public $variantCount;

af036678   Yarik   Image behaviors
21
22
23
24
          

          public function behaviors()

          {

              $behaviors = parent::behaviors();

4428da8c   Yarik   Almost all databa...
25
              if (isset( $behaviors[ 'language' ] )) {

af036678   Yarik   Image behaviors
26
27
28
                  unset( $behaviors[ 'language' ] );

              }

              return $behaviors;

d8c1a2e0   Yarik   Big commit artbox
29
          }

af036678   Yarik   Image behaviors
30
31
32
33
34
35
36
37
          

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

00731fef   Yarik   Before vitex test
38
                      [

8af13427   Yarik   For leha commit.
39
                          'productName',

00731fef   Yarik   Before vitex test
40
                      ],

af036678   Yarik   Image behaviors
41
42
43
44
45
                      'safe',

                  ],

                  [

                      [

                          'brand_id',

8af13427   Yarik   For leha commit.
46
47
                          'id',

                          'categoryId',

af036678   Yarik   Image behaviors
48
49
50
51
52
53
54
                      ],

                      'integer',

                  ],

                  [

                      [

                          'is_top',

                          'is_new',

4428da8c   Yarik   Almost all databa...
55
                          'is_discount',

af036678   Yarik   Image behaviors
56
57
58
59
                      ],

                      'boolean',

                  ],

              ];

d8c1a2e0   Yarik   Big commit artbox
60
          }

af036678   Yarik   Image behaviors
61
          

00731fef   Yarik   Before vitex test
62
63
64
65
          public function attributeLabels()

          {

              $labels = parent::attributeLabels();

              $new_labels = [

8af13427   Yarik   For leha commit.
66
                  'categoryId'   => 'Category ID',

4428da8c   Yarik   Almost all databa...
67
                  'brand_id'     => 'Brand ID',

8af13427   Yarik   For leha commit.
68
69
                  'productName'  => 'Product name',

                  'variantCount' => 'Variant count',

00731fef   Yarik   Before vitex test
70
71
72
73
              ];

              return array_merge($labels, $new_labels);

          }

          

af036678   Yarik   Image behaviors
74
75
76
77
78
79
80
          /**

           * @inheritdoc

           */

          public function scenarios()

          {

              // bypass scenarios() implementation in the parent class

              return Model::scenarios();

d8c1a2e0   Yarik   Big commit artbox
81
          }

af036678   Yarik   Image behaviors
82
83
84
85
86
87
88
89
90
91
          

          /**

           * Creates data provider instance with search query applied

           *

           * @param array $params

           *

           * @return ActiveDataProvider

           */

          public function search($params)

          {

af036678   Yarik   Image behaviors
92
              $query = Product::find();

4428da8c   Yarik   Almost all databa...
93
94
95
96
97
98
              $query->select(

                  [

                      'product.*',

                      'COUNT(product_variant.id) as count',

                  ]

              );

00731fef   Yarik   Before vitex test
99
              

4428da8c   Yarik   Almost all databa...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
              $query->joinWith(

                  [

                      'categories',

                      'lang',

                  ]

              )

                    ->joinWith(

                        [

                            'brand' => function ($query) {

                                /**

                                 * @var ActiveQuery $query

                                 */

                                $query->joinWith('lang');

                            },

                        ]

                    )

00731fef   Yarik   Before vitex test
116
                    ->joinWith('variants');

af036678   Yarik   Image behaviors
117
              

4428da8c   Yarik   Almost all databa...
118
119
120
121
122
123
124
              $query->groupBy(

                  [

                      'product.id',

                      'brand_lang.title',

                      'product_lang.title',

                  ]

              );

af036678   Yarik   Image behaviors
125
              

4428da8c   Yarik   Almost all databa...
126
127
128
129
130
              $dataProvider = new ActiveDataProvider(

                  [

                      'query' => $query,

                  ]

              );

af036678   Yarik   Image behaviors
131
              

4428da8c   Yarik   Almost all databa...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
              $dataProvider->setSort(

                  [

                      'attributes' => [

                          'id',

                          'productName'  => [

                              'asc'  => [ 'product_lang.title' => SORT_ASC ],

                              'desc' => [ 'product_lang.title' => SORT_DESC ],

                          ],

                          'brand_id'     => [

                              'asc'     => [ 'brand_lang.title' => SORT_ASC ],

                              'desc'    => [ 'brand_lang.title' => SORT_DESC ],

                              'default' => SORT_DESC,

                          ],

                          'variantCount' => [

                              'asc'  => [ 'count' => SORT_ASC ],

                              'desc' => [ 'count' => SORT_DESC ],

                          ],

af036678   Yarik   Image behaviors
149
                      ],

4428da8c   Yarik   Almost all databa...
150
151
                  ]

              );

772a3ca4   Yarik   Big commit
152
153
154
155
156
157
158
159
      

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

              }

af036678   Yarik   Image behaviors
160
              

4428da8c   Yarik   Almost all databa...
161
162
163
164
165
166
              if (isset( $this->is_top )) {

                  $query->andWhere(

                      [

                          'is_top' => (bool) $this->is_top,

                      ]

                  );

af036678   Yarik   Image behaviors
167
              }

4428da8c   Yarik   Almost all databa...
168
169
170
171
172
173
              if (isset( $this->is_new )) {

                  $query->andWhere(

                      [

                          'is_new' => (bool) $this->is_new,

                      ]

                  );

af036678   Yarik   Image behaviors
174
              }

4428da8c   Yarik   Almost all databa...
175
176
177
178
179
180
              if (isset( $this->is_discount )) {

                  $query->andWhere(

                      [

                          'is_discount' => (bool) $this->is_discount,

                      ]

                  );

af036678   Yarik   Image behaviors
181
              }

4428da8c   Yarik   Almost all databa...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
              $query->andFilterWhere(

                  [

                      'product.brand_id'             => $this->brand_id,

                      'product.id'                   => $this->id,

                      'product_category.category_id' => $this->categoryId,

                  ]

              );

              $query->andFilterWhere(

                  [

                      'like',

                      'product_lang.title',

                      $this->productName,

                  ]

              );

af036678   Yarik   Image behaviors
196
197
              

              return $dataProvider;

d8c1a2e0   Yarik   Big commit artbox
198
          }

d8c1a2e0   Yarik   Big commit artbox
199
      }