Product: FILE uploads
This commit is contained in:
@@ -3,11 +3,40 @@
|
||||
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
|
||||
$id = (int)$data['id'];
|
||||
|
||||
try {
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
|
||||
/** 1. FILE HANDLING **/
|
||||
$uploader = new FileUploader($include_url . "uploads/product/");
|
||||
|
||||
$id = (int)$data['id'];
|
||||
if (!empty($_FILES['product_image']['name'])) {
|
||||
// Cleanup old image when updating
|
||||
if ($id > 0) {
|
||||
$sql = "SELECT `product_image` FROM md_product WHERE company_id = :company_id AND id = :id";
|
||||
$sth = $pdo2->prepare($sql);
|
||||
$sth->execute([":company_id" => $company_id, ":id" => $id]);
|
||||
$existing_files = $sth->fetchColumn();
|
||||
|
||||
$uploader->cleanup($existing_files, '');
|
||||
}
|
||||
|
||||
$errors = $uploader->upload('product_image');
|
||||
if (!empty($errors)) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = $errors[0];
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
// Get the single uploaded filename directly
|
||||
$db_image_string = $uploader->getUploadedFiles()[0] ?? '';
|
||||
|
||||
} else {
|
||||
// No new file — keep existing, skip cleanup entirely
|
||||
$db_image_string = $data['keep_files'] ?? '';
|
||||
}
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $id, $db_image_string) {
|
||||
|
||||
// get existing log
|
||||
$sql = "SELECT `log` FROM md_product
|
||||
@@ -44,7 +73,7 @@
|
||||
":price" => $data["price"],
|
||||
":min_stock" => $data["min_stock"],
|
||||
":category" => $data["category"],
|
||||
":product_image"=> $data["product_image"],
|
||||
":product_image"=> $db_image_string,
|
||||
":description" => $data["description"],
|
||||
":status" => $data["status"],
|
||||
":log" => json_encode($table_log),
|
||||
@@ -65,7 +94,7 @@
|
||||
":price" => $data["price"],
|
||||
":min_stock" => $data["min_stock"],
|
||||
":category" => $data["category"],
|
||||
":product_image"=> $data["product_image"],
|
||||
":product_image"=> $db_image_string,
|
||||
":description" => $data["description"],
|
||||
":status" => $data["status"],
|
||||
":log" => json_encode($table_log),
|
||||
@@ -83,6 +112,7 @@
|
||||
} catch (Exception $e) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = "Something went wrong";
|
||||
$answer["error"] = $e->getMessage();
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
|
||||
Reference in New Issue
Block a user