Test Failed
Push — master ( a84a97...6a5aa6 )
by Gianluca
07:16
created

Template   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 66
c 1
b 0
f 0
dl 0
loc 105
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getProducts() 0 13 3
A recursiveChildren() 0 11 2
A getStructureCategories() 0 13 2
A getDetailsFields() 0 10 2
B generateDetailHtml() 0 43 9
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Libraries;
5
6
7
use Illuminate\Http\Request;
8
use Mongi\Mongicommerce\Models\Category;
9
use Mongi\Mongicommerce\Models\Detail;
10
use Mongi\Mongicommerce\Models\Product;
11
use Mongi\Mongicommerce\Models\ProductItem;
12
use Mongi\Mongicommerce\Models\ProductItemDetail;
13
14
15
class Template
16
{
17
    public static function getStructureCategories(){
18
19
        $categories = Category::with('children')->whereNull('parent_id')->get();
20
        $tree = [];
21
        foreach($categories as $category){
22
            $tree[] = [
23
                'id' => $category->id,
24
                'text' => $category->name,
25
                'state' => ['opened'=>true],
26
                'children' =>  self::recursiveChildren($category->children)
27
            ];
28
        }
29
        return $tree;
30
    }
31
32
    private static function recursiveChildren($childrens){
33
        $childs = [];
34
        foreach ($childrens as $children){
35
            $childs[] = [
36
                'id' => $children->id,
37
                'text' => $children->name,
38
                'state' => ['opened'=>true],
39
                'children' =>  self::recursiveChildren($children->children)
40
            ];
41
        }
42
        return $childs;
43
    }
44
45
46
    /**
47
     * @param Int $id
48
     * @return Product
49
     */
50
    public static function getProducts($id = null){
51
        if($id === null){
52
            $products = Product::all();
53
        }else{
54
            if(Category::find($id)){
55
                $belong_to_this_category = Category::find($id)->children->pluck('id')->toArray();
0 ignored issues
show
Bug introduced by
The property children does not exist on Mongi\Mongicommerce\Models\Category. Did you mean children_rec?
Loading history...
56
                $result = array_merge($belong_to_this_category, [0 =>$id]);
57
                $products = Product::whereIn('category_id',$result)->get();
58
            }else{
59
                $products = [];
60
            }
61
        }
62
        return $products;
63
    }
64
65
    public static function getDetailsFields(Product $product){
66
        $element = '<div class="row">';
67
68
        foreach($product->details->groupBy('product_detail_id') as $key => $details){
69
            $element.= self::generateDetailHtml(Detail::find($key),$details,$product->id);
70
        }
71
        $element.= '<button class="btn btn-primary mt-3">Salva nel carrello</button>';
72
        $element.= '<p class="show_error_product" style="color: red; display: none;">Prodotto non disponibile</p>';
73
        $element .= '</div>';
74
        return $element;
75
    }
76
77
    public static function generateDetailHtml($detail,$values,$product_id){
78
        $type = $detail->type;
79
        $html = '';
80
        $filters = Session()->get('filters');
81
        if($type === 'select'){
82
            $html = '<label>'.$detail->name.'</label>';
83
            $html .= '<select onchange="getVariationProduct()" data-product_id="'.$product_id.'" data-detail_id="'.$detail->id .'" class="form-control mongifield_into_product">';
84
            $html .= '<option value="">Seleziona</option>';
85
            $selected = '';
86
            foreach ($values as $value){
87
                if(isset($filters[$detail->id])){
88
                    if($filters[$detail->id] == $value->detail->id){
89
                        $selected = 'selected';
90
                    }else{
91
                        $selected = '';
92
                    }
93
                }
94
                $html .= '<option '.$selected.' value="'.$value->detail->id.'">'.$value->detail->value.'</option>';
95
            }
96
            $html .= '</select>';
97
        }
98
99
        if($type === 'checkbox'){
100
            $html = '';
101
            foreach($values as $value){
102
                $html .= '<div class="custom-control custom-checkbox mongifield">';
103
                $html .= '<input data-detail_id="'.$detail->id .'" type="checkbox" class="custom-control-input mongifield" id="defaultUnchecked_'.$value->id.'">';
104
                $html .= '<label class="custom-control-label" for="defaultUnchecked_'.$value->id.'">'.$value->value.'</label>';
105
                $html .= '</div>';
106
            }
107
        }
108
109
        if($type === 'radio'){
110
            $html = '';
111
            foreach($values as $value){
112
                $html .= '<div class="custom-control custom-radio mongifield">';
113
                $html .= '<input name="radio_'.$value->detail_id.'" data-detail_id="'.$detail->id .'" type="radio" class="custom-control-input mongifield" id="defaultradio_'.$value->id.'">';
114
                $html .= '<label class="custom-control-label" for="defaultradio_'.$value->id.'">'.$value->value.'</label>';
115
                $html .= '</div>';
116
            }
117
        }
118
119
        return $html;
120
    }
121
}
122