ProductHelper.php
3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace common\modules\product\helpers;
use common\modules\product\models\Brand;
use common\modules\product\models\Category;
use common\modules\product\models\Product;
use common\modules\product\models\ProductVariant;
use yii\base\Object;
use Yii;
class ProductHelper extends Object {
    public static function getCategories() {
        return Category::find()->with('categoryName')->getTree();
    }
    public static function getBrands() {
        return Brand::find()->with('brandName');
    }
    /*
     * 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;
    }
    public static function addLastProsucts($product_id) {
        $last_products = self::getLastProducts();
        if (!in_array($product_id, $last_products)) {
            $last_products[] = intval($product_id);
            if (count($last_products) > 16) {
                array_shift($last_products);
            }
            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) {
            $_products = [];
            foreach(Product::find()->joinWith(['variant'])->where([Product::tableName() .'.product_id' => $last_products])->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->all() as $product) {
                $_products[$product->product_id] = $product;
            }
            foreach($last_products as &$last_product) {
                if (empty($_products[$last_product])) {
                    unset($last_product);
                    continue;
                }
                $last_product = $_products[$last_product];
            }
        }
        return array_reverse($last_products);
    }
    public static function getSpecialProducts($type, $count, $sort = null) {
        switch($type) {
            case 'top':
                $data = ['is_top' => true];
                break;
            case 'new':
                $data = ['is_new' => true];
                break;
            case 'promo':
                $data = ['akciya' => true];
                break;
        }
        return Product::find()->joinWith('variants')->where($data)->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->limit($count)/*->orderBy($sort)*/->all();
    }
}