-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateProductDetails.php
More file actions
151 lines (140 loc) · 6.55 KB
/
updateProductDetails.php
File metadata and controls
151 lines (140 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Access-Control-Allow-Headers: Origin, Content-Type');
session_start();
include '../admin/conn.php';
require_once '../secrets.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$productName = $_POST["productName"];
$price = $_POST["price"];
$tags = $_POST["tags"];
$tColors = $_POST["tColors"];
$lColors = $_POST["lColors"];
$cColors = $_POST["cColors"];
$hColors = $_POST["hColors"];
// Accept new semicolon-separated id lists for categories and subcategories.
$categories = isset($_POST['categories']) ? $_POST['categories'] : (isset($_POST['subcategories']) ? $_POST['subcategories'] : '');
$subcategories = isset($_POST['subcategories']) && isset($_POST['categories']) ? $_POST['subcategories'] : '';
$defaultStyle = $_POST["default_style"];
$styleSize = isset($_POST['style_size']) ? $_POST['style_size'] : '';
if (empty($styleSize)) {
echo json_encode("ERR: style_size required");
exit();
}
$styleLocation = $_POST["default_style_location"];
$customDetailsRequired = $_POST["customFieldRequired"];
// prefer explicit product_id from POST, fallback to session
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : (isset($_SESSION["product_id"]) ? intval($_SESSION["product_id"]) : 0);
if ($product_id === 0) {
echo json_encode(array('success' => false, 'error' => 'product_id not provided'));
exit;
}
$sizeAvailable = $_POST["sizeAvailable"];
// fetch existing filenames
$fstmt = $conn->prepare("SELECT filename_front, filename_back FROM products WHERE product_id = ? LIMIT 1");
$fstmt->bind_param("i", $product_id);
$fstmt->execute();
$fres = $fstmt->get_result();
$existingFront = '';
$existingBack = '';
if ($fres && $fres->num_rows > 0) {
$frow = $fres->fetch_assoc();
$existingFront = $frow['filename_front'];
$existingBack = $frow['filename_back'];
}
$newFront = $existingFront;
$newBack = $existingBack;
// handle explicit removal flags (delete current file and clear DB filename)
if (isset($_POST['remove_front']) && $_POST['remove_front'] == '1') {
// Prevent deleting the side that is configured as the default style location
if ($styleLocation === 'front') {
echo json_encode(array('success' => false, 'error' => 'Cannot delete front design because default style location is front'));
exit;
}
if ($existingFront && $existingFront !== '') {
$oldPath = UPLOAD_DIR . $existingFront;
if (file_exists($oldPath)) {@unlink($oldPath);}
}
$newFront = '';
}
if (isset($_POST['remove_back']) && $_POST['remove_back'] == '1') {
// Prevent deleting the side that is configured as the default style location
if ($styleLocation === 'back') {
echo json_encode(array('success' => false, 'error' => 'Cannot delete back design because default style location is back'));
exit;
}
if ($existingBack && $existingBack !== '') {
$oldPath = UPLOAD_DIR . $existingBack;
if (file_exists($oldPath)) {@unlink($oldPath);}
}
$newBack = '';
}
// handle uploaded files if provided
if (isset($_FILES['frontFile']) && $_FILES['frontFile']['error'] === UPLOAD_ERR_OK) {
$file = $_FILES['frontFile'];
$orig = basename($file['name']);
// check DB for existing use of this filename by other products
$check = $conn->prepare("SELECT product_id FROM products WHERE (filename_front = ? OR filename_back = ?) AND product_id != ? LIMIT 1");
$check->bind_param("ssi", $orig, $orig, $product_id);
$check->execute();
$cres = $check->get_result();
if ($cres && $cres->num_rows > 0) {
echo json_encode(array('success' => false, 'error' => 'Filename "' . $orig . '" is already used by another product'));
exit;
}
$target = UPLOAD_DIR . $orig;
// delete the old product file first (per requirement)
if ($existingFront && $existingFront !== '') {
$oldPath = UPLOAD_DIR . $existingFront;
if (file_exists($oldPath)) {@unlink($oldPath);}
}
// if a file already exists at the target filename, remove it so we cleanly replace
if (file_exists($target)) {@unlink($target);}
if (!move_uploaded_file($file['tmp_name'], $target)) {
echo json_encode(array('success' => false, 'error' => 'Cannot upload front file'));
exit;
}
$newFront = $orig;
}
if (isset($_FILES['backFile']) && $_FILES['backFile']['error'] === UPLOAD_ERR_OK) {
$file = $_FILES['backFile'];
$orig = basename($file['name']);
// check DB for existing use of this filename by other products
$check = $conn->prepare("SELECT product_id FROM products WHERE (filename_front = ? OR filename_back = ?) AND product_id != ? LIMIT 1");
$check->bind_param("ssi", $orig, $orig, $product_id);
$check->execute();
$cres = $check->get_result();
if ($cres && $cres->num_rows > 0) {
echo json_encode(array('success' => false, 'error' => 'Filename "' . $orig . '" is already used by another product'));
exit;
}
$target = UPLOAD_DIR . $orig;
// delete the old product file first (per requirement)
if ($existingBack && $existingBack !== '') {
$oldPath = UPLOAD_DIR . $existingBack;
if (file_exists($oldPath)) {@unlink($oldPath);}
}
// if a file already exists at the target filename, remove it so we cleanly replace
if (file_exists($target)) {@unlink($target);}
if (!move_uploaded_file($file['tmp_name'], $target)) {
echo json_encode(array('success' => false, 'error' => 'Cannot upload back file'));
exit;
}
$newBack = $orig;
}
// Update product including possible new filenames
$query = $conn->prepare("UPDATE products
SET product_name = ?, price = ?, tag_list = ?, tColors = ?, lColors = ?, cColors = ?, hColors = ?, categories = ?, subcategories = ?, default_style = ?, style_size=?, default_style_location = ?, CustomDetailsRequired = ?, sizesAvailable = ?, filename_front = ?, filename_back = ?
WHERE product_id = ?;");
$query->bind_param("ssssssssssssssssi", $productName, $price, $tags, $tColors, $lColors, $cColors, $hColors, $categories, $subcategories, $defaultStyle, $styleSize, $styleLocation, $customDetailsRequired, $sizeAvailable, $newFront, $newBack, $product_id);
if (!$query->execute()) {
// If insertion fails, return error message
echo json_encode(array('success' => false, 'error' => 'DB update failed: ' . $query->error));
}
else {
echo json_encode(array('success' => true));
}
}
?>