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