Blame view

helpers/ProductHelper.php 11.7 KB
2f25da09   Yarik   first commit
1
2
3
4
5
6
  <?php

      

      namespace artweb\artbox\ecommerce\helpers;

      

      use artweb\artbox\ecommerce\models\Category;

      use artweb\artbox\ecommerce\models\Product;

4e03534e   Administrator   add similar products
7
      use artweb\artbox\ecommerce\models\ProductVariant;

1f5140e3   Yarik   Brand
8
9
      use yii\base\InvalidConfigException;

      use yii\base\InvalidParamException;

2f25da09   Yarik   first commit
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
      use yii\base\Object;

      use Yii;

      use yii\db\ActiveQuery;

      use yii\helpers\ArrayHelper;

      

      class ProductHelper extends Object

      {

          

          /**

           * @todo ArtboxTree

           * @return array

           */

          public static function getCategories()

          {

              return Category::find()

                             ->getTree(null, 'lang');

          }

          

          /**

04c33f07   Administrator   add create_item t...
29
           * Add $product_id to last products in session.

2f25da09   Yarik   first commit
30
31
32
33
34
35
36
37
           *

           * @param int $product_id

           */

          public static function addLastProducts(int $product_id)

          {

              $last_products = self::getLastProducts();

              if (!in_array($product_id, $last_products)) {

                  $last_products[] = intval($product_id);

2f25da09   Yarik   first commit
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
                  Yii::$app->session->set('last_products', $last_products);

              }

          }

          

          /**

           * Get last products ids from session or last Product models with ProductVariant, which are in stock if

           * $as_object is true

           *

           * @param bool $as_object

           *

           * @return array

           */

          public static function getLastProducts(bool $as_object = false)

          {

              $last_products = Yii::$app->session->get('last_products', []);

              if ($as_object) {

                  $last_products = Product::find()

04c33f07   Administrator   add create_item t...
55
56
57
58
59
60
61
62
63
64
65
                                          ->innerJoinWith(

                                              [

                                                  'enabledVariants' => function ($query) {

                                                      /**

                                                       * @var ActiveQuery $query

                                                       */

                                                      $query->joinWith('lang')

                                                          ->with('images');

                                                  },

                                              ]

                                          )

2f25da09   Yarik   first commit
66
67
68
69
70
71
72
73
74
75
76
77
78
                                          ->where([ 'product.id' => $last_products ])

                                          ->andWhere(

                                              [

                                                  '!=',

                                                  'product_variant.stock',

                                                  0,

                                              ]

                                          )

                                          ->indexBy('id')

                                          ->all();

              }

              return array_reverse($last_products);

          }

04c33f07   Administrator   add create_item t...
79
80
81
82
83
84
85
86
87
88
89
90
91
  

  

          public static function trueWordForm($num, $form_for_1, $form_for_2, $form_for_5){

              $num = abs($num) % 100;

              $num_x = $num % 10;

              if ($num > 10 && $num < 20)

                  return $form_for_5;

              if ($num_x > 1 && $num_x < 5)

                  return $form_for_2;

              if ($num_x == 1)

                  return $form_for_1;

              return $form_for_5;

          }

2f25da09   Yarik   first commit
92
93
94
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
          

          /**

           * Get special Products array with ProductVariants, which are in stock

           * Available types:

           * * top

           * * new

           * * promo

           *

           * @param string $type

           * @param int    $count

           *

           * @return Product[]

           */

          public static function getSpecialProducts(string $type, int $count)

          {

              switch ($type) {

                  case 'top':

                      $data = [ 'is_top' => true ];

                      break;

                  case 'new':

                      $data = [ 'is_new' => true ];

                      break;

                  case 'promo':

                      $data = [ 'is_discount' => true ];

                      break;

                  default:

                      return [];

                      break;

              }

              return Product::find()

                            ->with('lang')

                            ->joinWith('variants.lang')

                            ->where($data)

                            ->andWhere(

                                [

                                    '!=',

                                    'productVariant.stock',

                                    0,

                                ]

                            )

                            ->limit($count)

                            ->all();

          }

          

          /**

           * Get ActiveQuery to get similar products to $product

           *

           * @param Product $product

           * @param int     $count

           *

           * @return ActiveQuery

           */

          public static function getSimilarProducts(Product $product, $count = 10): ActiveQuery

          {

              $query = Product::find();

              if (empty( $product->properties )) {

                  $query->where('0 = 1');

                  return $query;

              }

              $query->innerJoinWith('variants')

                    ->joinWith('categories')

                    ->where(

                        [

                            '!=',

                            'product_variant.stock',

                            0,

                        ]

                    )

                    ->andWhere(

                        [ 'product_category.category_id' => ArrayHelper::getColumn($product->categories, 'id') ]

                    );

              $options = [];

              foreach ($product->properties as $group) {

                  foreach ($group->options as $option) {

                      $options[] = $option->id;

                  }

              }

              if (!empty( $options )) {

                  $query->innerJoinWith('options')

                        ->andWhere([ 'product_option.option_id' => $options ]);

              } else {

                  $query->where('0 = 1');

                  return $query;

              }

              $query->andWhere(

                  [

                      '!=',

                      'product.id',

                      $product->id,

                  ]

              );

4e03534e   Administrator   add similar products
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  

  

              $query->andWhere([

                  '>=',

                  ProductVariant::tableName() . '.price',

                  ($product->variant->price * 0.8),

              ]);

  

  

              $query->andWhere([

                  '<=',

                  ProductVariant::tableName() . '.price',

                  ($product->variant->price * 1.2),

              ]);

  

  

  

2f25da09   Yarik   first commit
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
              $query->groupBy('product.id');

              $query->limit($count);

              return $query;

          }

          

          /**

           * Add last category id to session

           *

           * @param int $category_id

           */

          public static function addLastCategory(int $category_id)

          {

              \Yii::$app->session->set('last_category_id', $category_id);

          }

          

          /**

           * Get last category id from session

           *

           * @return int

           */

2ad65823   Alexey Boroda   -Added date range...
220
          public static function getLastCategory()

2f25da09   Yarik   first commit
221
222
223
          {

              return \Yii::$app->session->get('last_category_id');

          }

1f5140e3   Yarik   Brand
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
          

          /**

           * Group Product[] by Categories

           * Array of categories with 2 levels:

           * <code>

           * [

           *  'parent_id' => [

           *      'id',

           *      'name',

           *      'count',

           *      'children' => [

           *          'id' => [

           *              'id',

           *              'name',

           *              'count',

           *          ]

           *      ]

           *  ]

           * ]

           * </code>

           *

           * @param Product[] $products

           *

           * @return array

           * @throws InvalidConfigException

           */

          public static function groupByCategories(array $products): array

          {

              $categoryList = [];

              foreach ($products as $product) {

                  if (!( $product instanceof Product )) {

                      throw new InvalidParamException('$products must be array of ' . Product::className());

                  }

                  foreach ($product->categories as $category) {

                      /**

                       * @var Category|null $parentCategory

                       */

                      $parentCategory = $category->parentAR;

                      /* If category has parent add current category to parents children array,

                      else create current category as root category */

                      if ($parentCategory) {

                          /* If parent category already in $categoryList search current category in its array,

                          else create it in $categoryList and add current category to it */

                          if (array_key_exists($parentCategory->id, $categoryList)) {

                              /* If current category already in parent category array increament count by 1,

                              else add current category to parent category children array */

                              if (array_key_exists($category->id, $categoryList[ $parentCategory->id ][ 'children' ])) {

                                  $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ][ 'count' ] += 1;

                              } else {

                                  $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ] = [

                                      'id'    => $category->id,

                                      'name'  => $category->lang->title,

                                      'alias' => $category->lang->alias,

                                      'count' => 1,

                                  ];

                              }

                          } else {

                              $categoryList[ $parentCategory->id ] = [

                                  'id'       => $parentCategory->id,

                                  'name'     => $parentCategory->lang->title,

                                  'alias'    => $parentCategory->lang->alias,

                                  'children' => [

                                      $category->id => [

                                          'id'    => $category->id,

                                          'name'  => $category->lang->title,

                                          'alias' => $category->lang->alias,

                                          'count' => 1,

                                      ],

                                  ],

                                  'count'    => 0,

                              ];

                          }

                      } else {

                          /* If current category already in $categoryList increment its count by 1,

                          else add it to $categoryList */

                          if (array_key_exists($category->id, $categoryList)) {

                              $categoryList[ $category->id ][ 'count' ] += 1;

                          } else {

                              $categoryList[ $category->id ] = [

                                  'id'       => $category->id,

                                  'name'     => $category->lang->title,

                                  'alias'    => $category->lang->alias,

                                  'count'    => 1,

                                  'children' => [],

                              ];

                          }

                      }

                  }

              }

              return $categoryList;

          }

2f25da09   Yarik   first commit
315
      }