public ActionResult Edit(ProductEditAdminViewModel viewModel, string saveAndView, int?addToBundleId, string addTobundle, int?addToBundleQty, int?removeAltCategoryId) { StoreFront storeFront = CurrentStoreFrontOrThrow; bool nameIsValid = GStoreDb.ValidateProductUrlName(this, viewModel.UrlName, storeFront.StoreFrontId, storeFront.ClientId, viewModel.ProductId); Product product = storeFront.Products.SingleOrDefault(p => p.ProductId == viewModel.ProductId); if (product == null) { AddUserMessage("Product not found", "Sorry, the Product you are trying to edit cannot be found. Product Id: [" + viewModel.ProductId + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Danger); if (viewModel.ReturnToFrontEnd) { return(RedirectToAction("ViewProductByName", "Catalog", new { area = "", urlName = viewModel.UrlName })); } if (storeFront.Authorization_IsAuthorized(CurrentUserProfileOrThrow, GStoreAction.Products_Manager)) { return(RedirectToAction("Manager")); } return(RedirectToAction("Index", "CatalogAdmin")); } if (ModelState.IsValid && nameIsValid) { ProcessFileUploads(viewModel, storeFront); bool addedToBundle = false; if (!string.IsNullOrEmpty(addTobundle) && addToBundleId.HasValue && addToBundleId.Value != 0) { ProductBundleItem newItem = GStoreDb.CreateProductBundleItemFastAdd(addToBundleId.Value, product, storeFront, CurrentUserProfileOrThrow, addToBundleQty ?? 1); addedToBundle = true; } product = GStoreDb.UpdateProduct(viewModel, storeFront, CurrentUserProfileOrThrow); AddUserMessage("Product updated successfully!", "Product updated successfully. Product '" + product.Name.ToHtml() + "' [" + product.ProductId + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Success); if (removeAltCategoryId.HasValue && removeAltCategoryId.Value != 0) { ProductCategoryAltProduct removeAltProduct = product.CategoryAltProducts.SingleOrDefault(p => p.ProductCategoryId == removeAltCategoryId.Value); if (removeAltProduct == null) { AddUserMessage("Error removing cross-sell Category", "Category Id [" + removeAltCategoryId.Value + "] could not be found in product alt categories.", UserMessageType.Danger); } else { string oldName = removeAltProduct.Category.Name; GStoreDb.ProductCategoryAltProducts.Delete(removeAltProduct); GStoreDb.SaveChanges(); AddUserMessage("Removed cross-sell Catergory", oldName.ToHtml() + " was removed.", UserMessageType.Info); } } if (removeAltCategoryId.HasValue) { return(RedirectToAction("Edit", new { id = product.ProductId, returnToFrontEnd = viewModel.ReturnToFrontEnd, Tab = viewModel.ActiveTab })); } if (!string.IsNullOrWhiteSpace(saveAndView) || addedToBundle) { return(RedirectToAction("Details", new { id = product.ProductId, returnToFrontEnd = viewModel.ReturnToFrontEnd, Tab = viewModel.ActiveTab })); } if (viewModel.ReturnToFrontEnd) { return(RedirectToAction("ViewProductByName", "Catalog", new { area = "", urlName = viewModel.UrlName })); } if (storeFront.Authorization_IsAuthorized(CurrentUserProfileOrThrow, GStoreAction.Products_Manager)) { return(RedirectToAction("Manager", new { productCategoryId = viewModel.ProductCategoryId })); } return(RedirectToAction("Index", "CatalogAdmin")); } if (Request.HasFiles()) { AddUserMessage("Files not uploaded", "You must correct the form values below and re-upload your files", UserMessageType.Danger); } viewModel.UpdateProduct(product); ViewData.Add("ReturnToFrontEnd", viewModel.ReturnToFrontEnd); return(View("CreateOrEdit", viewModel)); }
public ActionResult Edit(CategoryEditAdminViewModel viewModel, string Tab, string saveAndView, int?addCrossSellProductId, int?addCrossSellBundleId, int?removeAltProductId, int?removeAltBundleId) { StoreFrontConfiguration config = CurrentStoreFrontConfigOrThrow; StoreFront storeFront = config.StoreFront; bool nameIsValid = GStoreDb.ValidateProductCategoryUrlName(this, viewModel.UrlName, storeFront.StoreFrontId, storeFront.ClientId, viewModel.ProductCategoryId); ProductCategory productCategory = storeFront.ProductCategories.SingleOrDefault(pc => pc.ProductCategoryId == viewModel.ProductCategoryId); if (productCategory == null) { AddUserMessage("Category not found", "Sorry, the Category you are trying to edit cannot be found. Category Id: [" + viewModel.ProductCategoryId + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Danger); if (viewModel.ReturnToFrontEnd) { return(RedirectToAction("ViewCategoryByName", "Catalog", new { area = "", urlName = productCategory.UrlName })); } return(RedirectToAction("Manager")); } if (ModelState.IsValid && nameIsValid) { ProcessFileUploads(viewModel, storeFront); productCategory = GStoreDb.UpdateProductCategory(viewModel, storeFront, CurrentUserProfileOrThrow); AddUserMessage("Category updated successfully!", "Category updated successfully. Category '" + productCategory.Name.ToHtml() + "' [" + productCategory.ProductCategoryId + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Success); if (addCrossSellProductId.HasValue && addCrossSellProductId.Value != 0) { if (!productCategory.CategoryAltProducts.Any(p => p.ProductId == addCrossSellProductId.Value)) { Product crossSellProduct = storeFront.Products.SingleOrDefault(p => p.ProductId == addCrossSellProductId.Value); if (crossSellProduct == null) { AddUserMessage("Error adding cross-sell " + productCategory.ProductTypeSingleOrSystemDefault(config).ToHtml(), "Product Id [" + addCrossSellProductId.Value + "] could not be found in products.", UserMessageType.Danger); } else { ProductCategoryAltProduct newAltProduct = GStoreDb.CreateProductCategoryAltProductFastAdd(productCategory, crossSellProduct, storeFront, CurrentUserProfileOrThrow); AddUserMessage("Added cross-sell " + productCategory.ProductTypeSingleOrSystemDefault(config).ToHtml(), newAltProduct.Product.Name.ToHtml() + " added", UserMessageType.Info); } } } if (addCrossSellBundleId.HasValue && addCrossSellBundleId.Value != 0) { if (!productCategory.CategoryAltProductBundles.Any(b => b.ProductBundleId == addCrossSellBundleId.Value)) { ProductBundle crossSellProductBundle = storeFront.ProductBundles.SingleOrDefault(p => p.ProductBundleId == addCrossSellBundleId.Value); if (crossSellProductBundle == null) { AddUserMessage("Error adding cross-sell " + productCategory.BundleTypeSingleOrSystemDefault(config).ToHtml(), "Product Bundle Id [" + addCrossSellBundleId.Value + "] could not be found in product bundles.", UserMessageType.Danger); } else { ProductCategoryAltProductBundle newAltProductBundle = GStoreDb.CreateProductCategoryAltProductBundleFastAdd(productCategory, crossSellProductBundle, storeFront, CurrentUserProfileOrThrow); AddUserMessage("Added cross-sell " + productCategory.BundleTypeSingleOrSystemDefault(config).ToHtml(), newAltProductBundle.ProductBundle.Name.ToHtml() + " added", UserMessageType.Info); } } } if (removeAltProductId.HasValue && removeAltProductId.Value != 0) { ProductCategoryAltProduct removeAltProduct = productCategory.CategoryAltProducts.SingleOrDefault(p => p.ProductId == removeAltProductId.Value); if (removeAltProduct == null) { AddUserMessage("Error removing cross-sell " + productCategory.ProductTypeSingleOrSystemDefault(config).ToHtml(), "Product Id [" + removeAltProductId.Value + "] could not be found in category alt products.", UserMessageType.Danger); } else { string oldName = removeAltProduct.Product.Name; GStoreDb.ProductCategoryAltProducts.Delete(removeAltProduct); GStoreDb.SaveChanges(); AddUserMessage("Removed cross-sell " + productCategory.ProductTypeSingleOrSystemDefault(config).ToHtml(), oldName.ToHtml() + " was removed.", UserMessageType.Info); } } if (removeAltBundleId.HasValue && removeAltBundleId.Value != 0) { ProductCategoryAltProductBundle removeAltBundle = productCategory.CategoryAltProductBundles.SingleOrDefault(p => p.ProductBundleId == removeAltBundleId.Value); if (removeAltBundle == null) { AddUserMessage("Error removing cross-sell " + productCategory.BundleTypeSingleOrSystemDefault(config).ToHtml(), "Product Bundle Id [" + removeAltBundleId.Value + "] could not be found in category alt product bundles.", UserMessageType.Danger); } else { string oldName = removeAltBundle.ProductBundle.Name; GStoreDb.ProductCategoryAltProductBundles.Delete(removeAltBundle); GStoreDb.SaveChanges(); AddUserMessage("Removed cross-sell " + productCategory.BundleTypeSingleOrSystemDefault(config).ToHtml(), oldName.ToHtml() + " was removed.", UserMessageType.Info); } } if (addCrossSellProductId.HasValue || addCrossSellBundleId.HasValue || removeAltProductId.HasValue || removeAltBundleId.HasValue) { return(RedirectToAction("Edit", new { id = productCategory.ProductCategoryId, returnToFrontEnd = viewModel.ReturnToFrontEnd, Tab = viewModel.ActiveTab })); } if (!string.IsNullOrWhiteSpace(saveAndView)) { return(RedirectToAction("Details", new { id = productCategory.ProductCategoryId, returnToFrontEnd = viewModel.ReturnToFrontEnd, Tab = viewModel.ActiveTab })); } if (viewModel.ReturnToFrontEnd) { return(RedirectToAction("ViewCategoryByName", "Catalog", new { area = "", urlName = productCategory.UrlName })); } return(RedirectToAction("Manager")); } if (this.ModelState.ContainsKey("UrlName")) { ModelState["UrlName"].Value = new ValueProviderResult(viewModel.UrlName, viewModel.UrlName, null); } if (Request.HasFiles()) { AddUserMessage("Files not uploaded", "You must correct the form values below and re-upload your files", UserMessageType.Danger); } viewModel.FillListsIfEmpty(storeFront.Client, storeFront); viewModel.UpdateProductCategoryAndParent(productCategory); return(View("CreateOrEdit", viewModel)); }