Blame view

common/modules/product/models/ProductSearch.php 5.24 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
16
17
18
19
          

          public $brand_id;

          

          public $category_id;

          

00731fef   Yarik   Before vitex test
20
21
22
          public $product_name;

          

          public $variant_count;

af036678   Yarik   Image behaviors
23
24
25
26
27
28
29
30
          

          public function behaviors()

          {

              $behaviors = parent::behaviors();

              if(isset( $behaviors[ 'language' ] )) {

                  unset( $behaviors[ 'language' ] );

              }

              return $behaviors;

d8c1a2e0   Yarik   Big commit artbox
31
          }

af036678   Yarik   Image behaviors
32
33
34
35
36
37
38
39
          

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

00731fef   Yarik   Before vitex test
40
41
42
                      [

                          'product_name',

                      ],

af036678   Yarik   Image behaviors
43
44
45
46
47
48
49
                      'safe',

                  ],

                  [

                      [

                          'brand_id',

                          'product_id',

                          'category_id',

af036678   Yarik   Image behaviors
50
51
52
53
54
55
56
57
58
59
60
61
                      ],

                      'integer',

                  ],

                  [

                      [

                          'is_top',

                          'is_new',

                          'akciya',

                      ],

                      'boolean',

                  ],

              ];

d8c1a2e0   Yarik   Big commit artbox
62
          }

af036678   Yarik   Image behaviors
63
          

00731fef   Yarik   Before vitex test
64
65
66
67
          public function attributeLabels()

          {

              $labels = parent::attributeLabels();

              $new_labels = [

91811036   Eugeny Galkovskiy   MESSAGES !!!! EVGEN!
68
69
70
71
                  'category_id'   => Yii::t('app', 'pr_category_id'),

                  'brand_id'      => Yii::t('app', 'pr_brand_id'),

                  'product_name'  => Yii::t('app', 'pr_product_name'),

                  'variant_count' => Yii::t('app', 'pr_variant_count'),

00731fef   Yarik   Before vitex test
72
73
74
75
              ];

              return array_merge($labels, $new_labels);

          }

          

af036678   Yarik   Image behaviors
76
77
78
79
80
81
82
          /**

           * @inheritdoc

           */

          public function scenarios()

          {

              // bypass scenarios() implementation in the parent class

              return Model::scenarios();

d8c1a2e0   Yarik   Big commit artbox
83
          }

af036678   Yarik   Image behaviors
84
85
86
87
88
89
90
91
92
93
94
95
          

          /**

           * Creates data provider instance with search query applied

           *

           * @param array $params

           *

           * @return ActiveDataProvider

           */

          public function search($params)

          {

              

              $query = Product::find();

00731fef   Yarik   Before vitex test
96
97
98
99
100
              $query->select([

                  'product.*',

                  'COUNT(product_variant.product_variant_id) as count',

              ]);

              

af036678   Yarik   Image behaviors
101
102
              $query->joinWith([

                  'categories',

00731fef   Yarik   Before vitex test
103
                  'lang',

af036678   Yarik   Image behaviors
104
105
106
107
108
109
110
111
              ])

                    ->joinWith([

                        'brand' => function($query) {

                            /**

                             * @var ActiveQuery $query

                             */

                            $query->joinWith('lang');

                        },

00731fef   Yarik   Before vitex test
112
113
                    ])

                    ->joinWith('variants');

af036678   Yarik   Image behaviors
114
              

00731fef   Yarik   Before vitex test
115
116
117
118
119
              $query->groupBy([

                  'product.product_id',

                  'brand_lang.name',

                  'product_lang.name',

              ]);

af036678   Yarik   Image behaviors
120
121
122
123
124
              

              $dataProvider = new ActiveDataProvider([

                  'query' => $query,

              ]);

              

af036678   Yarik   Image behaviors
125
126
              $dataProvider->setSort([

                  'attributes' => [

00731fef   Yarik   Before vitex test
127
128
129
130
131
132
                      'product_id',

                      'product_name'  => [

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

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

                      ],

                      'brand_id'      => [

af036678   Yarik   Image behaviors
133
134
135
                          'asc'     => [ 'brand_lang.name' => SORT_ASC ],

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

                          'default' => SORT_DESC,

00731fef   Yarik   Before vitex test
136
137
138
139
                      ],

                      'variant_count' => [

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

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

af036678   Yarik   Image behaviors
140
141
142
143
                      ],

                  ],

              ]);

              

38f7295d   Yarik   Product admin sea...
144
145
              $this->load($params);

              

af036678   Yarik   Image behaviors
146
              if(isset( $this->is_top )) {

4e55ce81   Yarik   Another one admin...
147
                  $query->andWhere([

af036678   Yarik   Image behaviors
148
149
150
151
                      'is_top' => (bool) $this->is_top,

                  ]);

              }

              if(isset( $this->is_new )) {

4e55ce81   Yarik   Another one admin...
152
                  $query->andWhere([

af036678   Yarik   Image behaviors
153
154
155
156
                      'is_new' => (bool) $this->is_new,

                  ]);

              }

              if(isset( $this->akciya )) {

4e55ce81   Yarik   Another one admin...
157
                  $query->andWhere([

af036678   Yarik   Image behaviors
158
159
160
                      'akciya' => (bool) $this->akciya,

                  ]);

              }

d8c1a2e0   Yarik   Big commit artbox
161
              $query->andFilterWhere([

af036678   Yarik   Image behaviors
162
163
164
                  'product.brand_id'             => $this->brand_id,

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

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

d8c1a2e0   Yarik   Big commit artbox
165
              ]);

00731fef   Yarik   Before vitex test
166
167
168
169
170
              $query->andFilterWhere([

                  'like',

                  'product_lang.name',

                  $this->product_name,

              ]);

af036678   Yarik   Image behaviors
171
172
              

              return $dataProvider;

d8c1a2e0   Yarik   Big commit artbox
173
          }

d8c1a2e0   Yarik   Big commit artbox
174
      }