Blame view

common/modules/product/helpers/ProductHelper.php 3.07 KB
a8370482   Alexander Karnovsky   init project
1
2
3
4
  <?php
  
  namespace common\modules\product\helpers;
  
b519af22   Karnovsky A   Base-product func...
5
6
  use common\modules\product\models\Brand;
  use common\modules\product\models\Category;
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
7
  use common\modules\product\models\Product;
e136edc8   Karnovsky A   ---
8
  use common\modules\product\models\ProductVariant;
a8370482   Alexander Karnovsky   init project
9
  use yii\base\Object;
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
10
  use Yii;
a8370482   Alexander Karnovsky   init project
11
12
  
  class ProductHelper extends Object {
a8370482   Alexander Karnovsky   init project
13
      public static function getCategories() {
14eadb86   Karnovsky A   Eager loading for...
14
          return Category::find()->with('categoryName')->getTree();
a8370482   Alexander Karnovsky   init project
15
16
17
      }
  
      public static function getBrands() {
14eadb86   Karnovsky A   Eager loading for...
18
          return Brand::find()->with('brandName');
a8370482   Alexander Karnovsky   init project
19
      }
6fa713ca   Karnovsky A   Catalog v 1.2
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  
      /*
       * Return custom filter-option link
       * @var array $filter
       * @var array $options
       * @return array
       */
      public static function getFilterForOption($filter, $key, $value, $remove = false) {
          $result = $filter;
          if (is_array($value)) {
              foreach($value as $value_key => $value_items) {
                  if (!is_array($value_items)) {
                      $value_items = [$value_items];
                  }
                  foreach($value_items as $value_item) {
                      if ($remove && isset($result[$key]) && ($i = array_search($value_item, $result[$key][$value_key])) !== FALSE) {
                          unset($result[$key][$value_key][$i]);
                          if (empty($result[$key][$value_key])) {
                              unset($result[$key][$value_key]);
                          }
                      } else {
                          if (!isset($result[$key][$value_key]) || array_search($value_item, $result[$key][$value_key]) === FALSE) {
                              $result[$key][$value_key][] = $value_item;
                          }
                      }
                  }
              }
          } else {
              if ($remove && isset($result[$key]) && ($i = array_search($value, $result[$key])) !== FALSE) {
                  unset($result[$key][$i]);
                  if (empty($result[$key])) {
                      unset($result[$key]);
                  }
              } else {
                  if (!isset($result[$key]) || array_search($value, $result[$key]) === FALSE) {
                      $result[$key][] = $value;
                  }
              }
          }
          return $result;
      }
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  
      public static function addLastProsucts($product_id) {
          $last_products = self::getLastProducts();
          if (!in_array($product_id, $last_products)) {
              $last_products[] = $product_id;
              Yii::$app->session->set('last_products', $last_products);
          }
      }
  
      public static function getLastProducts($as_object = false) {
          $last_products = Yii::$app->session->get('last_products', []);
          if ($as_object) {
              $last_products = Product::find()->where(['product_id' => $last_products])->all();
          }
          return $last_products;
      }
e136edc8   Karnovsky A   ---
77
78
79
80
  
      public static function getSpecialProducts($type, $count, $sort = null) {
          $data = [$type => true];
          return Product::find()
55b90fbf   Karnovsky A   Special product b...
81
              ->innerJoinWith('variants')
e136edc8   Karnovsky A   ---
82
83
84
85
86
87
              ->where($data)
  //            ->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])
              ->limit($count)
              /*->orderBy($sort)*/
              ->all();
      }
a8370482   Alexander Karnovsky   init project
88
  }