Issues (71)

src/Libraries/Template.php (1 issue)

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
    public static function  getCategoryTree($parent_id = null, $spacing = '', $tree_array = array()) {
47
        $categories = Category::select('id', 'name', 'parent_id')->where('parent_id' ,'=', $parent_id)->orderBy('parent_id')->get();
48
        foreach ($categories as $item){
49
            $tree_array[] = ['id' => $item->id, 'name' =>$spacing . $item->name] ;
50
            $tree_array = self::getCategoryTree($item->id, $spacing . '- ', $tree_array);
51
        }
52
        return $tree_array;
53
    }
54
55
    /**
56
     * @param Int $id
57
     * @return Product
58
     */
59
    public static function getProducts($id = null){
60
        if($id === null){
61
            $products = Product::all();
62
        }else{
63
            $products = Category::where('id',$id)->orWhere('parent_id',$id)->first()->products;
64
        }
65
        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...
66
    }
67
68
    public static function getDetailsFields(Product $product,$product_item_id){
69
        $element = '<div class="row col">';
70
71
        foreach($product->details->groupBy('product_detail_id') as $key => $details){
72
73
            $element.= self::generateDetailHtml(Detail::find($key),$details->groupBy('product_detail_value_id'),$product->id,$product_item_id);
74
        }
75
        $element.= '<p class="show_error_product" style="color: red; display: none;">Prodotto non disponibile</p>';
76
        $element .= '</div>';
77
        return $element;
78
    }
79
80
    public static function generateHtmlField($type,$value,$label){
81
        if($type !== 'textarea'){
82
            $html = '';
83
            $html .= "<label>{$label}</label>";
84
            $html .= "<input disabled class='form-control' type='{$type}' value='{$value}'>";
85
            return $html;
86
        }else{
87
            $html = '';
88
            $html .= "<label>{$label}</label>";
89
            $html .= "<textarea disabled class='form-control'>{$value}</textarea>";
90
            return $html;
91
        }
92
    }
93
94
    public static function getConfigurationFields($product_item_id){
95
        $configurationFields = ProductConfigurationField::where('product_item_id',$product_item_id)->get();
96
        $element = '<div class="col row">';
97
        foreach ($configurationFields as $field){
98
                $element .= self::generateHtmlField($field->field->type,$field->value,$field->field->name);
99
100
            }
101
            $element .= '</div>';
102
        return $element;
103
    }
104
105
    public static function buttonCart($product_item_id){
106
        return "<button onclick='addToCart(this)' data-product_item_id='{$product_item_id}' class='btn btn-primary mt-3'>Salva nel carrello</button>";
107
    }
108
109
    public static function generateDetailHtml($detail,$values,$product_id,$product_item_id){
110
111
        $type = $detail->type;
112
        $html = '';
113
        if($type === 'select'){
114
            $html = '<label>'.$detail->name.'</label>';
115
            $html .= '<select onchange="getVariationProduct()" data-product_id="'.$product_id.'" data-detail_id="'.$detail->id .'" class="form-control mongifield_into_product">';
116
            $selected = '';
117
118
            $details = ProductItemDetail::where('product_item_id',$product_item_id)->get();
119
            $filter = [];
120
            foreach ($details as $_detail){
121
                $filter[$_detail->product_detail_id] = $_detail->product_detail_value_id;
122
            }
123
            foreach ($values as $detail_value_id =>$value){
124
                if(isset($filter[$detail->id])){
125
                    if($filter[$detail->id] == $detail_value_id){
126
                        $selected = 'selected';
127
                    }else{
128
                        $selected = '';
129
                    }
130
                }
131
                $html .= '<option '.$selected.' value="'.$detail_value_id.'">'.DetailValue::find($detail_value_id)->value.'</option>';
132
            }
133
            $html .= '</select>';
134
        }
135
136
137
        return $html;
138
    }
139
140
    public static function MoveSessionToCart($user_id){
141
        $user = User::find($user_id);
142
        $elements_in_cart = session('cart');
143
        if(!empty($elements_in_cart)){
144
            if(Cart::where('user_id',$user->id)->count() <= 0){
145
                foreach($elements_in_cart as $product_id => $count){
146
                    $cart = new Cart();
147
                    $cart->user_id = $user->id;
148
                    $cart->product_item_id = $product_id;
149
                    $cart->quantity = $count;
150
                    $cart->save();
151
                }
152
            }
153
154
            session()->forget('products.ids');
155
            session()->forget('cart');
156
        }
157
    }
158
}
159