@@ -11,34 +11,34 @@ class ProductController extends Controller
11
11
{
12
12
public function create (Request $ request )
13
13
{
14
- $ product = new Product ();
14
+ $ productModel = new Product ();
15
15
$ ser_cen_id = Application::$ app ->session ->get ('serviceCenter ' );
16
16
17
17
if (!$ ser_cen_id ) {
18
18
Application::$ app ->session ->setFlash ('error ' , 'Please log in to create a product. ' );
19
19
Application::$ app ->response ->redirect ('/service-centre-login ' );
20
20
return ;
21
21
}
22
- $ product ->ser_cen_id = $ ser_cen_id ;
22
+ $ productModel ->ser_cen_id = $ ser_cen_id ;
23
23
24
24
if ($ request ->isPost ()) {
25
- $ product ->loadData ($ request ->getBody ());
25
+ $ productModel ->loadData ($ request ->getBody ());
26
26
27
27
if (!empty ($ _FILES ['media ' ]['name ' ])) {
28
28
$ uploadDir = 'assets/uploads/ ' ;
29
- $ product ->media = $ _FILES ['media ' ]['name ' ];
30
- $ targetFile = $ uploadDir . basename ($ product ->media );
29
+ $ productModel ->media = $ _FILES ['media ' ]['name ' ];
30
+ $ targetFile = $ uploadDir . basename ($ productModel ->media );
31
31
32
32
if (!move_uploaded_file ($ _FILES ['media ' ]['tmp_name ' ], $ targetFile )) {
33
33
Application::$ app ->session ->setFlash ('error ' , 'Failed to upload file. ' );
34
34
return $ this ->render ('service-centre/create-product ' , [
35
- 'model ' => $ product ,
35
+ 'model ' => $ productModel ,
36
36
'products ' => [] // Ensure products is passed even if empty
37
37
]);
38
38
}
39
39
}
40
40
41
- if ($ product ->validate () && $ product ->save ()) {
41
+ if ($ productModel ->validate () && $ productModel ->save ()) {
42
42
Application::$ app ->session ->setFlash ('success ' , 'Product created successfully. ' );
43
43
Application::$ app ->response ->redirect ('/service-center-create-product ' );
44
44
return ;
@@ -48,71 +48,90 @@ public function create(Request $request)
48
48
// Fetch products for the logged-in service center
49
49
// Output the dump in the HTML
50
50
51
- // $products = $product ->getProductByServiceCenter($ser_cen_id);
51
+ // $productModels = $productModel ->getProductByServiceCenter($ser_cen_id);
52
52
53
53
return $ this ->render ('/service-centre/create-product ' , [
54
54
55
- 'model ' => $ product ,
56
- // 'products' => $products
55
+ 'model ' => $ productModel ,
56
+ // 'products' => $productModels
57
57
// Pass the products to the view
58
58
]);
59
59
}
60
60
61
61
public function filterProductsById ()
62
62
{
63
63
$ ser_cen_id = Application::$ app ->session ->get ('serviceCenter ' );
64
- $ products = (new Product )->getProductByServiceCenter ($ ser_cen_id );
64
+ $ productModels = (new Product )->getProductByServiceCenter ($ ser_cen_id );
65
65
$ this ->setLayout ('auth ' );
66
66
return $ this ->render ('service-centre/create-product ' , [
67
- 'products ' => $ products
67
+ 'products ' => $ productModels
68
68
]);
69
69
}
70
70
71
+
71
72
public function index ()
72
73
{
73
- $ products = (new Product )->getAllProducts (); // Fetch all products from the database
74
+ $ productModels = (new Product )->getAllProducts (); // Fetch all products from the database
74
75
$ this ->setLayout ('auth ' ); // Set layout if needed
75
76
return $ this ->render ('/service-centre/market-place-home ' , [
76
- 'products ' => $ products // Pass products to the view
77
+ 'products ' => $ productModels // Pass products to the view
77
78
]);
78
79
}
79
80
80
81
public function update (Request $ request )
81
82
{
82
- $ product = new Product ();
83
+ $ productModel = new Product ();
83
84
$ ser_cen_id = Application::$ app ->session ->get ('serviceCenter ' );
84
-
85
85
if (!$ ser_cen_id ) {
86
- Application::$ app ->session ->setFlash ('error ' , 'Please log in to create a product. ' );
86
+ Application::$ app ->session ->setFlash ('error ' , 'Please log in to update a product. ' );
87
87
Application::$ app ->response ->redirect ('/service-centre-login ' );
88
88
}
89
+ if ($ request ->isGet ()){
90
+ $ product_id = $ request ->getBody ()['product_id ' ] ?? null ;
91
+
92
+ if ($ product_id ) {
93
+ $ product = $ productModel ->getProductByIdAndServiceCenter ($ ser_cen_id , $ product_id );
94
+ if ($ product ) {
95
+ $ this ->setLayout ('auth ' );
96
+ return $ this ->render ('service-centre/update-product ' , [
97
+ 'product ' => $ product
98
+ ]);
99
+ }
100
+ }
101
+ else {
102
+ Application::$ app ->session ->setFlash ('error ' , 'invalid product id. ' );
103
+ Application::$ app ->response ->redirect ('/service-centre-login ' );
104
+ }
105
+
106
+ }
89
107
90
108
if ($ request ->isPost ()) {
91
- $ product ->loadData ($ request ->getBody ());
92
- $ product ->ser_cen_id = $ ser_cen_id ;
93
-
109
+ $ productModel ->loadData ($ request ->getBody ());
110
+ $ productModel ->ser_cen_id = $ ser_cen_id ;
94
111
if (!empty ($ _FILES ['media ' ]['name ' ])) {
95
- $ uploadDir = 'assets/uploads/ ' ;
96
- $ fileName = uniqid () . '_ ' . basename ($ _FILES ['media ' ]['name ' ]);
97
- $ targetFile = $ uploadDir . $ fileName ;
98
-
99
- if (!move_uploaded_file ($ _FILES ['media ' ]['tmp_name ' ], $ targetFile )) {
100
- Application::$ app ->session ->setFlash ('error ' , 'Failed to upload file. ' );
101
- Application::$ app ->response ->redirect ('/service-center-create-product ' );
102
- return ;
103
- }
104
-
105
- $ product ->media = $ fileName ;
112
+ $ productModel ->media = $ _FILES ['media ' ]['name ' ];
113
+ move_uploaded_file ($ _FILES ['media ' ]['tmp_name ' ], 'assets/uploads/ ' . $ productModel ->media );
106
114
}
107
- if ($ product ->editProduct ()){
115
+ if ($ productModel ->editProduct ()){
108
116
Application::$ app ->session ->setFlash ('success ' , 'Product updated successfully. ' );
109
117
Application::$ app ->response ->redirect ('/service-center-create-product ' );
118
+ return ;
110
119
}
111
- else {
112
- Application::$ app ->session ->setFlash ('error ' , 'Failed to update product. ' );
113
- }
114
- Application::$ app ->response ->redirect ('/service-center-create-product ' );
115
120
}
121
+
122
+ }
123
+
124
+ public function delete (Request $ request ){
125
+ $ product_id = $ request ->getBody ()['product_id ' ];
126
+ $ ser_cen_id = Application::$ app ->session ->get ('serviceCenter ' );
127
+
128
+ if ((new Product ())->deleteProduct ($ product_id , $ ser_cen_id )){
129
+ Application::$ app ->session ->setFlash ('success ' , 'Product deleted successfully. ' );
130
+ }
131
+ else {
132
+ Application::$ app ->session ->setFlash ('error ' , 'Failed to delete product. ' );
133
+ }
134
+ Application::$ app ->response ->redirect ('/service-center-create-product ' );
116
135
}
117
136
118
137
0 commit comments