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\DetailValue; |
11
|
|
|
use Mongi\Mongicommerce\Models\Product; |
12
|
|
|
use Mongi\Mongicommerce\Models\ProductItem; |
13
|
|
|
use Mongi\Mongicommerce\Models\ProductItemDetail; |
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
|
|
|
if(Category::find($id)){ |
56
|
|
|
$belong_to_this_category = Category::find($id)->children->pluck('id')->toArray(); |
|
|
|
|
57
|
|
|
$result = array_merge($belong_to_this_category, [0 =>$id]); |
58
|
|
|
$products = Product::whereIn('category_id',$result)->get(); |
59
|
|
|
}else{ |
60
|
|
|
$products = []; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return $products; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function getDetailsFields(Product $product){ |
67
|
|
|
$element = '<div class="row">'; |
68
|
|
|
|
69
|
|
|
foreach($product->details->groupBy('product_detail_id') as $key => $details){ |
70
|
|
|
|
71
|
|
|
$element.= self::generateDetailHtml(Detail::find($key),$details->groupBy('product_detail_value_id'),$product->id); |
72
|
|
|
} |
73
|
|
|
$element.= '<button class="btn btn-primary mt-3">Salva nel carrello</button>'; |
74
|
|
|
$element.= '<p class="show_error_product" style="color: red; display: none;">Prodotto non disponibile</p>'; |
75
|
|
|
$element .= '</div>'; |
76
|
|
|
return $element; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function generateDetailHtml($detail,$values,$product_id){ |
80
|
|
|
|
81
|
|
|
$type = $detail->type; |
82
|
|
|
$html = ''; |
83
|
|
|
$filters = Session()->get('filters'); |
84
|
|
|
if($type === 'select'){ |
85
|
|
|
$html = '<label>'.$detail->name.'</label>'; |
86
|
|
|
$html .= '<select onchange="getVariationProduct()" data-product_id="'.$product_id.'" data-detail_id="'.$detail->id .'" class="form-control mongifield_into_product">'; |
87
|
|
|
$html .= '<option value="">Seleziona</option>'; |
88
|
|
|
$selected = ''; |
89
|
|
|
|
90
|
|
|
foreach ($values as $detail_value_id =>$value){ |
91
|
|
|
|
92
|
|
|
if(isset($filters[$detail->id])){ |
93
|
|
|
if($filters[$detail->id] == $detail_value_id){ |
94
|
|
|
$selected = 'selected'; |
95
|
|
|
}else{ |
96
|
|
|
$selected = ''; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
$html .= '<option '.$selected.' value="'.$detail_value_id.'">'.DetailValue::find($detail_value_id)->value.'</option>'; |
100
|
|
|
} |
101
|
|
|
$html .= '</select>'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
return $html; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|