Test Failed
Push — master ( f6c91d...7200d2 )
by Gianluca
16:47
created

Template::MoveSessionToCart()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 16
rs 9.8666
cc 4
nc 3
nop 1
1
<?php
2
namespace Mongi\Mongicommerce\Libraries;
3
4
use Illuminate\Http\Request;
5
use Mongi\Mongicommerce\Models\Cart;
6
use Mongi\Mongicommerce\Models\User;
7
use Mongi\Mongicommerce\Models\Detail;
8
use Mongi\Mongicommerce\Models\Product;
9
use Mongi\Mongicommerce\Models\Category;
10
use Mongi\Mongicommerce\Models\DetailValue;
11
use Mongi\Mongicommerce\Models\ProductItem;
12
use Mongi\Mongicommerce\Models\ProductItemDetail;
13
use Mongi\Mongicommerce\Models\ProductConfigurationField;
14
15
16
class Template
17
{
18
    public static function getStructureCategories(){
19
20
        $categories = Category::with('children')->whereNull('parent_id')->get();
21
        $tree = [];
22
        foreach($categories as $category){
23
            $tree[] = [
24
                'id' => $category->id,
25
                'text' => $category->name,
26
                'state' => ['opened'=>true],
27
                'children' =>  self::recursiveChildren($category->children)
28
            ];
29
        }
30
        return $tree;
31
    }
32
33
    private static function recursiveChildren($childrens){
34
        $childs = [];
35
        foreach ($childrens as $children){
36
            $childs[] = [
37
                'id' => $children->id,
38
                'text' => $children->name,
39
                'state' => ['opened'=>true],
40
                'children' =>  self::recursiveChildren($children->children)
41
            ];
42
        }
43
        return $childs;
44
    }
45
46
47
    /**
48
     * @param Int $id
49
     * @return Product
50
     */
51
    public static function getProducts($id = null){
52
        if($id === null){
53
            $products = Product::all();
54
        }else{
55
            $products = Category::find($id)->products;
56
        }
57
        return $products;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $products returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type Mongi\Mongicommerce\Models\Product.
Loading history...
58
    }
59
60
    public static function getDetailsFields(Product $product,$product_item_id){
61
        $element = '<div class="row">';
62
63
        foreach($product->details->groupBy('product_detail_id') as $key => $details){
64
65
            $element.= self::generateDetailHtml(Detail::find($key),$details->groupBy('product_detail_value_id'),$product->id,$product_item_id);
66
        }
67
        $element.= '<p class="show_error_product" style="color: red; display: none;">Prodotto non disponibile</p>';
68
        $element .= '</div>';
69
        return $element;
70
    }
71
72
    public static function generateHtmlField($type,$value,$label){
73
        if($type !== 'textarea'){
74
            $html = '';
75
            $html .= "<label>{$label}</label>";
76
            $html .= "<input class='form-control' type='{$type}' value='{$value}'>";
77
            return $html;
78
        }else{
79
            $html = '';
80
            $html .= "<label>{$label}</label>";
81
            $html .= "<textarea class='form-control'>{$value}</textarea>";
82
            return $html;
83
        }
84
    }
85
86
    public static function getConfigurationFields($product_item_id){
87
        $configurationFields = ProductConfigurationField::where('product_item_id',$product_item_id)->get();
88
        $element = '';
89
        foreach ($configurationFields as $field){
90
                $element .= '<div class="row">';
91
                $element .= self::generateHtmlField($field->field->type,$field->value,$field->field->name);
92
                $element .= '</div>';
93
            }
94
        return $element;
95
    }
96
97
    public static function buttonCart($product_item_id){
98
        return "<button onclick='addToCart(this)' data-product_item_id='{$product_item_id}' class='btn btn-primary mt-3'>Salva nel carrello</button>";
99
    }
100
101
    public static function generateDetailHtml($detail,$values,$product_id,$product_item_id){
102
103
        $type = $detail->type;
104
        $html = '';
105
        if($type === 'select'){
106
            $html = '<label>'.$detail->name.'</label>';
107
            $html .= '<select onchange="getVariationProduct()" data-product_id="'.$product_id.'" data-detail_id="'.$detail->id .'" class="form-control mongifield_into_product">';
108
            $selected = '';
109
110
            $details = ProductItemDetail::where('product_item_id',$product_item_id)->get();
111
            $filter = [];
112
            foreach ($details as $_detail){
113
                $filter[$_detail->product_detail_id] = $_detail->product_detail_value_id;
114
            }
115
            foreach ($values as $detail_value_id =>$value){
116
                if(isset($filter[$detail->id])){
117
                    if($filter[$detail->id] == $detail_value_id){
118
                        $selected = 'selected';
119
                    }else{
120
                        $selected = '';
121
                    }
122
                }
123
                $html .= '<option '.$selected.' value="'.$detail_value_id.'">'.DetailValue::find($detail_value_id)->value.'</option>';
124
            }
125
            $html .= '</select>';
126
        }
127
128
129
        return $html;
130
    }
131
132
    public static function MoveSessionToCart($user_id){
133
        $user = User::find($user_id);
134
        $elements_in_cart = session('cart');
135
        if(!empty($elements_in_cart)){
136
            if(Cart::where('user_id',$user->id)->count() <= 0){
137
                foreach($elements_in_cart as $product_id => $count){
138
                    $cart = new Cart();
139
                    $cart->user_id = $user->id;
140
                    $cart->product_item_id = $product_id;
141
                    $cart->quantity = $count;
142
                    $cart->save();
143
                }
144
            }
145
146
            session()->forget('products.ids');
147
            session()->forget('cart');
148
        }
149
    }
150
}
151