1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Mongi\Mongicommerce\Http\Controllers\admin; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Facades\File; |
9
|
|
|
use Mongi\Mongicommerce\Http\Controllers\Controller; |
10
|
|
|
use Mongi\Mongicommerce\Models\ConfigurationField; |
11
|
|
|
use Mongi\Mongicommerce\Models\Product; |
12
|
|
|
use Mongi\Mongicommerce\Models\ProductConfigurationField; |
13
|
|
|
use Mongi\Mongicommerce\Models\ProductItem; |
14
|
|
|
use Mongi\Mongicommerce\Models\ProductItemDetail; |
15
|
|
|
|
16
|
|
|
class AdminNewProductVariationController extends Controller |
17
|
|
|
{ |
18
|
|
|
public function page($id_product){ |
19
|
|
|
$product = Product::find($id_product); |
20
|
|
|
$items = $product->items; |
21
|
|
|
|
22
|
|
|
return view('mongicommerce::admin.pages.products.new_product_variation',['product' => $product,'items' => $items]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function createNewVariation(Request $r){ |
26
|
|
|
$r->validate([ |
27
|
|
|
'category_id' => 'required', |
28
|
|
|
'quantity' => 'required', |
29
|
|
|
'price' => 'required', |
30
|
|
|
'image' => 'required', |
31
|
|
|
'details' => 'required|min:4', |
32
|
|
|
]); |
33
|
|
|
$product_id = $r->get('product_id'); |
34
|
|
|
|
35
|
|
|
$details = json_decode($r->get('details'),true); |
|
|
|
|
36
|
|
|
$configuration_fields = json_decode($r->get('configuration_fields'),true); |
37
|
|
|
$quantity = $r->get('quantity'); |
38
|
|
|
$price = $r->get('price'); |
39
|
|
|
$product_name = $r->get('product_name'); |
40
|
|
|
$product_description = $r->get('product_description'); |
41
|
|
|
$get_image = $r->get('image'); |
42
|
|
|
$category_id = $r->get('category_id'); |
43
|
|
|
|
44
|
|
|
$product = Product::find($product_id); |
45
|
|
|
|
46
|
|
|
$product_item = new ProductItem(); |
47
|
|
|
$product_item->product_id = $product->id; |
48
|
|
|
$product_item->category_id = $category_id; |
49
|
|
|
$product_item->name = $product_name; |
50
|
|
|
$product_item->description = $product_description; |
51
|
|
|
$product_item->image = null; |
52
|
|
|
$product_item->price = $price; |
53
|
|
|
$product_item->quantity = $quantity; |
54
|
|
|
$product_item->save(); |
55
|
|
|
|
56
|
|
|
$base64_str = substr($get_image, strpos($get_image, ",")+1); |
|
|
|
|
57
|
|
|
$image = base64_decode($base64_str); |
58
|
|
|
$destinationPath = public_path().'/uploads/products_img/'.$product_item->product_id.'/'.$product_item->id.'/'; |
59
|
|
|
$destinationPathDB = url('/').'/uploads/products_img/'.$product_item->product_id.'/'.$product_item->id.'/'; |
60
|
|
|
|
61
|
|
|
if(!File::isDirectory($destinationPath)){ |
62
|
|
|
File::makeDirectory($destinationPath, $mode = 0777, true, true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$image_name = time().'.'.'jpeg'; |
66
|
|
|
$path_file = $destinationPath.$image_name; |
67
|
|
|
$dbPath = $destinationPathDB.$image_name; |
68
|
|
|
file_put_contents($path_file, $image); |
69
|
|
|
|
70
|
|
|
$product_item->image = $dbPath; |
71
|
|
|
$product_item->save(); |
72
|
|
|
|
73
|
|
|
foreach($details as $detail){ |
74
|
|
|
$detail_obj = (object) $detail; |
75
|
|
|
$product_detail = new ProductItemDetail(); |
76
|
|
|
$product_detail->product_item_id = $product_item->id; |
77
|
|
|
$product_detail->product_detail_id = $detail_obj->detail_id; |
78
|
|
|
$product_detail->product_detail_value_id = $detail_obj->detail_value; |
79
|
|
|
$product_detail->save(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ($configuration_fields as $conf_field){ |
83
|
|
|
$conf_fields_obj = (object) $conf_field; |
84
|
|
|
$configuration_field = new ProductConfigurationField(); |
85
|
|
|
$configuration_field->product_item_id = $product_item->id; |
86
|
|
|
$configuration_field->config_field_id = $conf_fields_obj->configuration_field_id; |
87
|
|
|
$configuration_field->value = $conf_fields_obj->configuration_field_value; |
88
|
|
|
$configuration_field->save(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
//ceare la tabella ProductConfigurationField |
93
|
|
|
//id, product_item_id, configuration_field_id, configuration_field_value |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|