Product: FILE uploads

This commit is contained in:
Thanakorn S
2026-04-21 11:31:51 +07:00
parent 3b8f94f011
commit 8834d672ba
7 changed files with 138 additions and 40 deletions
+34 -4
View File
@@ -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));
+50 -13
View File
@@ -48,22 +48,34 @@
<input type="number" class="form-control" id="min_stock" placeholder="0" required>
</div>
</div>
<div class="mb-3">
<label for="category" class="form-label">Category</label>
<select class="form-select" id="category" required> </select>
</div>
<div class="mb-3">
<label for="product_image" class="form-label">Product Image</label>
<input type="file" class="form-control" id="product_image" accept="image/*">
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="category" class="form-label">Category</label>
<select class="form-select" id="category" required> </select>
</div>
<div class="col-md-6 mb-3">
<!-- Remove d-none from file input so it shows by default -->
<div class="mb-3">
<label class="form-label">Product Image</label>
<div id="current_image_display" class="d-none">
<div class="d-flex align-items-center gap-2">
<div class='input-group'>
<span class='btn btn-outline-secondary' type='button' id='change_image_btn'>Change</span>
<input type='text' class='form-control' id='current_file_label' disabled>
</div>
</div>
</div>
<input type="file" class="form-control" id="product_image" accept="image/*">
</div>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" rows="4"
placeholder="Enter product description"></textarea>
</div>
<div class="d-flex gap-2">
<button type="submit" class="btn btn-primary" onclick="add_product();">Create</button>
<button type="submit" class="btn btn-primary" onclick="manage_product();">Create</button>
</div>
</div>
</div>
@@ -81,13 +93,24 @@
<?php require "../include_ending.php";?>
<script>
function add_product() {
function manage_product() {
let formData = new FormData();
// Append the file (single image, not array)
let imageFile = $('#product_image')[0].files[0];
if (imageFile) {
formData.append('product_image', imageFile);
}
formData.append('keep_files', $('#current_file_label').val())
return ajax_request({
url: "<?php echo $server_url?>inventory/api/engine/manage_product.php",
autoPrepare: true,
checkRequired: 0,
debugMode: 0,
debugMode: 1,
formData: formData,
action: 'manage',
onSuccess: function(res) {
@@ -133,7 +156,21 @@
onSuccess: function(res) {
$.each(res.output, function(key, item) {
$(`#${key}`).val(item);
let el = $(`#${key}`);
if (el.attr('type') === 'file') return; // skip file inputs
el.val(item);
});
// Handle image field
if (res.output.product_image) {
$('#current_file_label').val(res.output.product_image);
$('#current_image_display').removeClass('d-none'); // show filename + change btn
$('#product_image').addClass('d-none'); // hide file input
}
$('#change_image_btn').on('click', function() {
$('#current_image_display').addClass('d-none');
$('#product_image').removeClass('d-none');
});
$(`button[type=submit]`).text('Update');