Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 109 additions & 6 deletions API/src/product/updateProductDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,123 @@
}
$styleLocation = $_POST["default_style_location"];
$customDetailsRequired = $_POST["customFieldRequired"];
$product_id = $_SESSION["product_id"];
// 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"];

// Attempt to insert new design into table
// 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 = ?
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("sssssssssssssss", $productName, $price, $tags, $tColors, $lColors, $cColors, $hColors, $categories, $subcategories, $defaultStyle, $styleSize, $styleLocation, $customDetailsRequired, $sizeAvailable, $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("ERR: Insertion failed to execute" . $query->error);
echo json_encode(array('success' => false, 'error' => 'DB update failed: ' . $query->error));
}
else {
echo json_encode(1);
echo json_encode(array('success' => true));
}
}

Expand Down
70 changes: 66 additions & 4 deletions react/src/products/EditProducts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function EditProducts() {
const [lColors, setLColors] = useState("");
const [cColors, setCColors] = useState("");
const [hColors, setHColors] = useState("");
const [frontFileName, setFrontFileName] = useState("");
const [backFileName, setBackFileName] = useState("");
const [frontFile, setFrontFile] = useState(null);
const [backFile, setBackFile] = useState(null);
const [allSubcategories, setAllSubcategories] = useState([]);
const [currentSubcategories, setCurrentSubcategories] = useState("");
const [allCategories, setAllCategories] = useState([]);
Expand All @@ -28,6 +32,9 @@ function EditProducts() {
const [customFieldRequired, setCustomFieldRequired] = useState(0);
const [sizesAvailable, setSizesAvailable] = useState(1);
const [failToUpdate, setFailToUpdate] = useState(false);
const [uploadError, setUploadError] = useState("");
const [removeFront, setRemoveFront] = useState(false);
const [removeBack, setRemoveBack] = useState(false);

// API Calls
useEffect(() => {
Expand Down Expand Up @@ -77,6 +84,8 @@ function EditProducts() {
setHColors(data.hColors);
setStyle(data.default_style);
setLocation(data.default_style_location);
setFrontFileName(data.filename_front || "");
setBackFileName(data.filename_back || "");
setStyleSize(data.style_size || "");
setProductIsSet(true);
setCustomFieldRequired(data.CustomDetailsRequired.toString());
Expand Down Expand Up @@ -165,14 +174,19 @@ function EditProducts() {
} else if (style === 'hoodie' && hColors.trim() === '') {
setFailToUpdate(true);
} else {
const formData = new FormData();
const formData = new FormData();
formData.append('productName', productName);
formData.append('product_id', productId);
formData.append('price', price);
formData.append('tags', tagList);
formData.append('tColors', tColors);
formData.append('lColors', lColors);
formData.append('cColors', cColors);
formData.append('hColors', hColors);
if (frontFile) formData.append('frontFile', frontFile);
if (backFile) formData.append('backFile', backFile);
if (removeFront) formData.append('remove_front', '1');
if (removeBack) formData.append('remove_back', '1');
// send semicolon-separated id lists for categories and subcategories
formData.append('categories', selectedCategoryIds.join(';'));
formData.append('subcategories', selectedSubcategoryIds.join(';'));
Expand All @@ -188,13 +202,20 @@ function EditProducts() {
})
.then((response) => response.json())
.then((data) => {
if(data) {
window.location.href="/products";
if (data && data.success) {
window.location.href = "/products";
} else {
setUploadError((data && data.error) ? data.error : 'Update failed');
}
});
})
.catch((err) => setUploadError('Update failed'));
}
};

const uploadImage = async (side) => {
// removed: uploadImage now handled on form submit
}

if (admin) {
return (
<div className="EditProducts">
Expand Down Expand Up @@ -464,6 +485,36 @@ function EditProducts() {
</div>
<br/>
<br/>
<h3><b>Front Design</b> <small>Current: {frontFileName ? frontFileName : 'None'}</small></h3>
<div className="containerRow">
<input type="file" accept="image/*" disabled={removeFront} onChange={(e) => setFrontFile(e.target.files[0])} />
{!removeFront ? (
location === 'front' ? (
<button type="button" className="delete-button" disabled title="Cannot delete the default-style image">Delete Front</button>
) : (
<button type="button" className="delete-button" onClick={() => setRemoveFront(true)}>Delete Front</button>
)
) : (
<button type="button" className="default-button" onClick={() => setRemoveFront(false)}>Undo Delete</button>
)}
</div>
<br/>
<br/>
<h3><b>Back Design</b> <small>Current: {backFileName ? backFileName : 'None'}</small></h3>
<div className="containerRow">
<input type="file" accept="image/*" disabled={removeBack} onChange={(e) => setBackFile(e.target.files[0])} />
{!removeBack ? (
location === 'back' ? (
<button type="button" className="delete-button" disabled title="Cannot delete the default-style image">Delete Back</button>
) : (
<button type="button" className="delete-button" onClick={() => setRemoveBack(true)}>Delete Back</button>
)
) : (
<button type="button" className="default-button" onClick={() => setRemoveBack(false)}>Undo Delete</button>
)}
</div>
<br/>
<br/>
<button type="submit" className="default-button">Update Product</button>
</form>
{failToUpdate &&
Expand All @@ -477,6 +528,17 @@ function EditProducts() {
</div>
</div>
}
{uploadError &&
<div className="confirmation-modal">
<div className="confirmation-dialog">
<h3>Upload Error</h3>
<p>{uploadError}</p>
<div className="confirmation-buttons">
<button className="delete-button" onClick={() => setUploadError("")}>Close</button>
</div>
</div>
</div>
}
</div>
</div>
</div>
Expand Down
Loading