Blame view

common/modules/product/models/BrandSearch.php 6.37 KB
3f2bc3d0   Administrator   first commit
1
2
3
4
  <?php

  

  namespace common\modules\product\models;

  

a0be9a4d   Karnovsky A   30062016
5
6
  use common\modules\product\helpers\ProductHelper;

  use common\modules\rubrication\models\TaxOption;

3f2bc3d0   Administrator   first commit
7
8
9
10
11
12
13
14
15
16
  use Yii;

  use yii\base\Model;

  use yii\data\ActiveDataProvider;

  use common\modules\product\models\Brand;

  

  /**

   * BrandSearch represents the model behind the search form about `common\modules\product\models\Brand`.

   */

  class BrandSearch extends Brand

  {

ccc7a9d3   Karnovsky A   Karnovsky 12052016
17
      public $brand_name;

3f2bc3d0   Administrator   first commit
18
19
20
21
22
23
24
      /**

       * @inheritdoc

       */

      public function rules()

      {

          return [

              [['brand_id', 'brand_name_id'], 'integer'],

ccc7a9d3   Karnovsky A   Karnovsky 12052016
25
              [['alias', 'image', 'meta_title', 'meta_desc', 'meta_robots', 'seo_text', 'brand_name'], 'safe'],

3f2bc3d0   Administrator   first commit
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
          ];

      }

  

      /**

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

      {

c7852657   Karnovsky A   -
47
          $query = Brand::find(); // ->with('brandName')

3f2bc3d0   Administrator   first commit
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  

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

          }*/

  

ccc7a9d3   Karnovsky A   Karnovsky 12052016
63
64
65
66
67
68
69
          $dataProvider->setSort([

              'attributes' => [

                  'brand_name',

                  'alias'

              ]

          ]);

  

3f2bc3d0   Administrator   first commit
70
71
72
73
74
75
          // grid filtering conditions

          $query->andFilterWhere([

              'brand_id' => $this->brand_id,

              'brand_name_id' => $this->brand_name_id,

          ]);

  

c7852657   Karnovsky A   -
76
  

ccc7a9d3   Karnovsky A   Karnovsky 12052016
77
78
79
80
81
          $query->andFilterWhere(['ilike', 'alias', $this->alias])

              ->andFilterWhere(['ilike', 'image', $this->image])

              ->andFilterWhere(['ilike', 'meta_title', $this->meta_title])

              ->andFilterWhere(['ilike', 'meta_desc', $this->meta_desc])

              ->andFilterWhere(['ilike', 'meta_robots', $this->meta_robots])

c7852657   Karnovsky A   -
82
83
84
85
86
              ->andFilterWhere(['ilike', 'seo_text', $this->seo_text]);

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

              $query->joinWith('brandName');

              $query->andFilterWhere(['ilike', 'brand_name.value', $this->brand_name]);

          }

3f2bc3d0   Administrator   first commit
87
  

38828295   Karnovsky A   -
88
89
          $query->orderBy('brand_id', 'asc');

  

3f2bc3d0   Administrator   first commit
90
91
92
          return $dataProvider;

      }

  

09af7db9   Karnovsky A   Fix filter
93
      public function getBrands($category = null, $params = [], $productQuery = null) {

a0be9a4d   Karnovsky A   30062016
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  //        $queryCount = ProductHelper::productCountQuery($category, $params, ['brands']);

  

          /*if (!empty($params['prices'])) {

              if ($params['prices']['min'] > 0) {

                  $queryCount->andWhere(['>=', ProductVariant::tableName() .'.price', $params['prices']['min']]);

              }

              if ($params['prices']['max'] > 0) {

                  $queryCount->andWhere(['<=', ProductVariant::tableName() .'.price', $params['prices']['max']]);

              }

          }*/

  //        if (!empty($params['options'])) {

  //            $queryCount->innerJoin(TaxOption::tableName(), TaxOption::tableName())

  //        }

  

3f2bc3d0   Administrator   first commit
108
109
          $query = Brand::find()

              ->select([

a0be9a4d   Karnovsky A   30062016
110
                  Brand::tableName() .'.*'

3f2bc3d0   Administrator   first commit
111
112
113
              ])

              ->innerJoin(Product::tableName(), Product::tableName() .'.brand_id='. Brand::tableName() .'.brand_id')

              ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id')

c7852657   Karnovsky A   -
114
              ->with(['brandName']);

a0be9a4d   Karnovsky A   30062016
115
  

45d2a21a   Karnovsky A   Brands count
116
  

1b898c16   Administrator   20.07.16
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  //        $queryCount = Product::find()

  //            ->select(['COUNT(product.product_id)'])

  //            ->where('product.brand_id = brand.brand_id');

  //        $queryCount->andWhere('(SELECT COUNT(pv.product_variant_id) FROM "product_variant" "pv" WHERE pv.stock != 0 AND pv.product_id = product.product_id) > 0');

  //        if (!empty($category)) {

  //            $queryCount->andWhere('(SELECT COUNT(pc.product_id) FROM product_category pc WHERE pc.product_id = product.product_id AND pc.category_id = '. $category->category_id .') > 0');

  //        }

  //        if (!empty($params['options'])) {

  //            $queryCount->innerJoin('product_option', 'product_option.product_id = product.product_id');

  //            $queryCount->innerJoin('tax_option', 'tax_option.tax_option_id = product_option.option_id');

  //            $queryCount->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id');

  //            foreach ($params['options'] as $group => $options) {

  //                $queryCount->andWhere([

  //                    'tax_group.alias' => $group,

  //                    'tax_option.alias' => $options

  //                ]);

  //            }

  ////            $query->addSelect("(SELECT COUNT(product_option.product_id) AS products FROM product_option INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id WHERE tax_group.alias LIKE '$group' AND tax_option.alias IN (" . implode(',', $options) . ")) AS _items_count");

  //        }

103e8126   Karnovsky A   Remove ITEMS_COUN...
136
  //        $query->addSelect(['_items_count' => $queryCount]);

a0be9a4d   Karnovsky A   30062016
137
  

09af7db9   Karnovsky A   Fix filter
138
139
140
141
142
  //        if ($productQuery) {

  //            $productQuery->select(['COUNT(product.product_id)']);

  //            $query->addSelect(['_items_count' => $productQuery]);

  //        }

  

a0be9a4d   Karnovsky A   30062016
143
  

9d539ac3   Karnovsky A   Brands and option...
144
145
146
          $query->innerJoin('product_variant', 'product_variant.product_id = '. Product::tableName() .'.product_id');

          $query->where(['!=', 'product_variant.stock', 0]);

          $query->groupBy(Product::tableName() .'.product_id');

3f2bc3d0   Administrator   first commit
147
          if (!empty($category)) {

9d539ac3   Karnovsky A   Brands and option...
148
              $query->andWhere([

3f2bc3d0   Administrator   first commit
149
150
151
152
                  ProductCategory::tableName() .'.category_id' => $category->category_id

              ]);

          }

          $query->groupBy(Brand::tableName() .'.brand_id');

3f2bc3d0   Administrator   first commit
153
  

8724ec1f   Karnovsky A   -
154
          return $query;

3f2bc3d0   Administrator   first commit
155
      }

38828295   Karnovsky A   -
156
157
158
159
160
161
162
163
164
165
166
167
  

      public static function findByAlias($alias) {

          /** @var CategoryQuery $query */

          $query = Brand::find()

              ->with('brandName')

              ->andFilterWhere(['alias' => $alias]);

          if (($model = $query->one()) !== null) {

              return $model;

          } else {

              throw new NotFoundHttpException('The requested page does not exist.');

          }

      }

3f2bc3d0   Administrator   first commit
168
  }