/// <summary> /// Cập nhật thư viện hình ảnh sản phẩm /// </summary> /// <param name="productID"></param> private void _updateImageGallery(int productID, tbl_Account acc) { // Delete Image if (!String.IsNullOrEmpty(hdfDeleteImageGallery.Value)) { var imageIDList = hdfDeleteImageGallery.Value .Split(',') .Where(x => !String.IsNullOrEmpty(x)) .ToList(); foreach (var strID in imageIDList) { ProductImageController.Delete(Convert.ToInt32(strID)); } } // Add Image if (uploadImageGallery.HasFiles) { foreach (HttpPostedFile httpPostedFile in uploadImageGallery.PostedFiles) { var fileName = _uploadImage(productID, httpPostedFile); ProductImageController.Insert(productID, fileName, false, DateTime.Now, acc.Username); } } }
protected void btnSubmit_Click(object sender, EventArgs e) { string username = Request.Cookies["userLoginSystem"].Value; var acc = AccountController.GetByUsername(username); int cateID = ViewState["cateID"].ToString().ToInt(0); int ProductID = ViewState["ID"].ToString().ToInt(0); if (cateID > 0) { string ProductTitle = txtProductTitle.Text; string ProductContent = pContent.Content; string ProductSKU = ViewState["SKU"].ToString(); double ProductStock = 0; int StockStatus = 0; bool ManageStock = true; double Regular_Price = Convert.ToDouble(pRegular_Price.Text); double CostOfGood = Convert.ToDouble(pCostOfGood.Text); double Retail_Price = Convert.ToDouble(pRetailPrice.Text); bool IsHidden = chkIsHidden.Checked; int CategoryID = hdfParentID.Value.ToInt(); double MinimumInventoryLevel = 0; if (pMinimumInventoryLevel.Text != "") { MinimumInventoryLevel = Convert.ToDouble(pMinimumInventoryLevel.Text); } double MaximumInventoryLevel = 0; if (pMaximumInventoryLevel.Text != "") { MaximumInventoryLevel = Convert.ToDouble(pMaximumInventoryLevel.Text); } //Phần thêm ảnh đại diện sản phẩm string path = "/uploads/images/"; string ProductImage = ListProductThumbnail.Value; if (ProductThumbnailImage.UploadedFiles.Count > 0) { foreach (UploadedFile f in ProductThumbnailImage.UploadedFiles) { var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(f.FileName)); try { f.SaveAs(Server.MapPath(o)); ProductImage = o; } catch { } } } if (ProductImage != ListProductThumbnail.Value) { if (File.Exists(Server.MapPath(ListProductThumbnail.Value))) { File.Delete(Server.MapPath(ListProductThumbnail.Value)); } } // Delete Image Gallery string deleteImageGallery = hdfDeleteImageGallery.Value; if (deleteImageGallery != "") { string[] deletelist = deleteImageGallery.Split(','); for (int i = 0; i < deletelist.Length - 1; i++) { var img = ProductImageController.GetByID(Convert.ToInt32(deletelist[i])); if (img != null) { var product = ProductController.GetByID(ProductID); // Delete image if (!string.IsNullOrEmpty(img.ProductImage) && img.ProductImage != product.ProductImage) { string fileImage = Server.MapPath(img.ProductImage); File.Delete(fileImage); } string delete = ProductImageController.Delete(img.ID); } } } // Update product string kq = ProductController.Update(ProductID, CategoryID, 0, ProductTitle, ProductContent, ProductSKU, ProductStock, StockStatus, ManageStock, Regular_Price, CostOfGood, Retail_Price, ProductImage, 0, IsHidden, DateTime.Now, username, ddlSupplier.SelectedValue.ToInt(0), ddlSupplier.SelectedItem.ToString(), txtMaterials.Text, MinimumInventoryLevel, MaximumInventoryLevel); // Upload image gallery if (UploadImages.HasFiles) { foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles) { var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(uploadedFile.FileName)); uploadedFile.SaveAs(Server.MapPath(o)); ProductImageController.Insert(ProductID, o, false, DateTime.Now, username); } } if (kq.ToInt(0) > 0) { // Update Variable if (hdfsetStyle.Value == "2") { string variable = hdfVariableListInsert.Value; if (!string.IsNullOrEmpty(variable)) { string[] items = variable.Split(','); for (int i = 0; i < items.Length - 1; i++) { string item = items[i]; string[] itemElement = item.Split(';'); string datanameid = itemElement[0]; string[] datavalueid = itemElement[1].Split('|'); string datanametext = itemElement[2]; string datavaluetext = itemElement[3]; string productvariablesku = itemElement[4].Trim().ToUpper(); string regularprice = itemElement[5]; string costofgood = itemElement[6]; string retailprice = itemElement[7]; string[] datanamevalue = itemElement[8].Split('|'); string imageUpload = itemElement[8]; int _MaximumInventoryLevel = itemElement[9].ToInt(0); int _MinimumInventoryLevel = itemElement[10].ToInt(0); int stockstatus = itemElement[11].ToInt(); string imageSrc = itemElement[13]; string kq1 = ""; // Check variable var Variable = ProductVariableController.GetBySKU(productvariablesku); if (Variable != null) { // Update image string image = Variable.Image; if (imageSrc == "/App_Themes/Ann/image/placeholder.png") { // Delete old image if (!string.IsNullOrEmpty(Variable.Image)) { string fileImage = Server.MapPath(Variable.Image); File.Delete(fileImage); image = ""; } } else { if (imageSrc != Variable.Image) { HttpPostedFile postedFile = Request.Files["" + imageUpload + ""]; if (postedFile != null && postedFile.ContentLength > 0) { // Upload image var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(postedFile.FileName)); postedFile.SaveAs(Server.MapPath(o)); image = o; // Delete old image if (!string.IsNullOrEmpty(Variable.Image)) { string fileImage = Server.MapPath(Variable.Image); File.Delete(fileImage); } } else { // Delete old image if (!string.IsNullOrEmpty(Variable.Image)) { string fileImage = Server.MapPath(Variable.Image); File.Delete(fileImage); image = ""; } } } } // Update variable kq1 = ProductVariableController.Update(Variable.ID, ProductID, Variable.ParentSKU, productvariablesku, Convert.ToDouble(Variable.Stock), Convert.ToInt32(Variable.StockStatus), Convert.ToDouble(regularprice), Convert.ToDouble(costofgood), Convert.ToDouble(retailprice), image, true, false, DateTime.Now, username, Convert.ToInt32(Variable.SupplierID), Variable.SupplierName, _MinimumInventoryLevel, _MaximumInventoryLevel); // Delete all productVariableValue bool deleteVariableValue = ProductVariableValueController.DeleteByProductVariableID(Variable.ID); } else { string image = ""; HttpPostedFile postedFile = Request.Files["" + imageUpload + ""]; if (postedFile != null && postedFile.ContentLength > 0) { // Upload image var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(postedFile.FileName)); postedFile.SaveAs(Server.MapPath(o)); image = o; } // Insert new variable kq1 = ProductVariableController.Insert(ProductID, ProductSKU, productvariablesku, 0, stockstatus, Convert.ToDouble(regularprice), Convert.ToDouble(costofgood), Convert.ToDouble(retailprice), image, true, false, DateTime.Now, username, ddlSupplier.SelectedValue.ToInt(0), ddlSupplier.SelectedItem.ToString(), _MinimumInventoryLevel, _MaximumInventoryLevel); } // Update ProductVariableValue if (kq1.ToInt(0) > 0) { string[] Data = datanametext.Split('|'); string[] DataValue = datavaluetext.Split('|'); for (int k = 0; k < Data.Length - 1; k++) { int variablevalueID = datavalueid[k].ToInt(); string variableName = Data[k]; string variableValueName = DataValue[k]; ProductVariableValueController.Insert(kq1.ToInt(), productvariablesku, variablevalueID, variableName, variableValueName, false, DateTime.Now, username); } } } } } PJUtils.ShowMessageBoxSwAlert("Cập nhật sản phẩm thành công", "s", true, Page); } } }