function deleteProduct_Action() { $product_id = $_GET['product_id']; $list_url = 'http://' . HOST . '/' . APPLICATION . '/index.php?page=home'; $product = new product(); $result = $product->deleteProduct($product_id); header("Location: {$list_url}"); }
$app->get('/products', function () { $product = new product(); $response = $product->getAllProduct(); echoResponse(200, $response); }); $app->post('/products', function () use($app) { $product = new product(); $data = json_decode($app->request->getBody(), TRUE); $id = $product->AddProduct($data); echoResponse(200, $id); }); $app->put('/products/:id', function ($id) use($app) { $product = new product(); $data = json_decode($app->request->getBody(), TRUE); $result = $product->updateProduct($id, $data); // $rows["message"] = "Product information updated successfully."; echoResponse(200, $result); }); $app->delete('/products/:id', function ($id) { $product = new product(); $id = $product->deleteProduct($id); echoResponse(200, $id); }); function echoResponse($status_code, $response) { global $app; $app->status($status_code); $app->contentType('application/json'); echo json_encode($response, JSON_NUMERIC_CHECK); } $app->run();
} elseif (isset($_POST['action']) && $_POST['action'] == 'update') { $file = $_FILES; $data = $_POST['data']['Item']; if (isset($file['name'])) { $data['image'] = $file['name']; } else { $data['image'] = ''; } if ($product->updateProduct($data)) { $_SESSION['message'] = 'Product has been updated successfully.'; header('Location: ' . FRONTEND . 'inventory.php'); exit; } } elseif (isset($_GET['action']) && $_GET['action'] == 'delete') { $id = $_GET['id']; if ($product->deleteProduct($id)) { $_SESSION['message'] = 'Product has been deleted successfully.'; header('Location: ' . FRONTEND . 'inventory.php'); exit; } } elseif (isset($_GET['action']) && $_GET['action'] == 'search') { $keyword = $_POST['keywords']; $products = $product->getSearchProducts($keyword); $smarty->assign('productcount', count($products)); $smarty->assign('products', $products); $smarty->assign('content', $smarty->fetch('product.tpl')); } else { $page = 1; if (isset($_GET['page'])) { $page = $_GET['page']; }