public ActionResult Edit(int?id, ProductBundleEditAdminViewModel viewModel, List <ProductBundleItemEditAdminViewModel> bundleItems, string saveAndView, string addProductId, int?removeItemId, int?removeAltCategoryId) { if (viewModel == null || viewModel.ProductBundleId == 0) { return(HttpBadRequest("Product Bundle Id = 0")); } if (viewModel.ProductBundleId != (id ?? 0)) { return(HttpBadRequest("Product Bundle Id mismatch Request id: " + (id.HasValue ? id.Value.ToString() : "(null)") + " viewModel.ProductBundleId: " + viewModel.ProductBundleId)); } StoreFrontConfiguration config = CurrentStoreFrontConfigOrThrow; StoreFront storeFront = config.StoreFront; bool nameIsValid = GStoreDb.ValidateProductBundleUrlName(this, viewModel.UrlName, storeFront.StoreFrontId, storeFront.ClientId, viewModel.ProductBundleId); ProductBundle productBundle = storeFront.ProductBundles.SingleOrDefault(p => p.ProductBundleId == viewModel.ProductBundleId); if (productBundle == null) { AddUserMessage("Product Bundle not found", "Sorry, the Product Bundle you are trying to edit cannot be found. Product Bundle Id: [" + viewModel.ProductBundleId + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Danger); if (viewModel.ReturnToFrontEnd) { return(RedirectToAction("ViewBundleByName", "Catalog", new { area = "", urlName = viewModel.UrlName })); } if (storeFront.Authorization_IsAuthorized(CurrentUserProfileOrThrow, GStoreAction.Bundles_Manager)) { return(RedirectToAction("Manager")); } return(RedirectToAction("Index", "CatalogAdmin")); } if (bundleItems != null && bundleItems.Count != 0) { if (!bundleItems.All(bi => productBundle.ProductBundleItems.Any(pb => pb.ProductBundleItemId == bi.ProductBundleItemId))) { return(HttpBadRequest("bundle items do not all have the same ProductBundleId")); } } if (!string.IsNullOrEmpty(addProductId)) { int productIdToAdd = 0; if (!int.TryParse(addProductId, out productIdToAdd)) { return(HttpBadRequest("product id invalid")); } if (productBundle.ProductBundleItems.Any(pbi => pbi.ProductId == productIdToAdd)) { AddUserMessage("Product already exists in bundle", "Product id " + productIdToAdd + " already exists in this bundle.", UserMessageType.Info); } else { ProductBundleItem newItem = GStoreDb.CreateProductBundleItemFastAdd(productBundle, productIdToAdd, storeFront, CurrentUserProfileOrThrow, 1); AddUserMessage("Added Item!", "Product '" + newItem.Product.Name.ToHtml() + "' [" + newItem.ProductId + "] was added to the bundle '" + productBundle.Name.ToHtml() + "' [" + productBundle.ProductBundleId + "]", UserMessageType.Info); } } if (ModelState.IsValid && nameIsValid) { bool removedItem = false; ProcessFileUploads(viewModel, storeFront); productBundle = GStoreDb.UpdateProductBundle(viewModel, storeFront, CurrentUserProfileOrThrow); AddUserMessage("Product Bundle updated successfully!", "Product Bundle updated successfully. Product Bundle '" + productBundle.Name.ToHtml() + "' [" + productBundle.ProductBundleId + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Success); if (bundleItems != null && bundleItems.Count != 0) { GStoreDb.UpdateProductBundleItems(productBundle, bundleItems, storeFront, CurrentUserProfileOrThrow); } if (removeItemId.HasValue && removeItemId.Value != 0) { removedItem = true; RemoveItem(productBundle, removeItemId.Value); } if (removeAltCategoryId.HasValue && removeAltCategoryId.Value != 0) { ProductCategoryAltProductBundle removeAltProductBundle = productBundle.CategoryAltBundles.SingleOrDefault(p => p.ProductCategoryId == removeAltCategoryId.Value); if (removeAltProductBundle == null) { AddUserMessage("Error removing cross-sell Category", "Category Id [" + removeAltCategoryId.Value + "] could not be found in bundle alt categories.", UserMessageType.Danger); } else { string oldName = removeAltProductBundle.Category.Name; GStoreDb.ProductCategoryAltProductBundles.Delete(removeAltProductBundle); GStoreDb.SaveChanges(); AddUserMessage("Removed cross-sell Catergory", oldName.ToHtml() + " was removed.", UserMessageType.Info); } } if (removeAltCategoryId.HasValue) { return(RedirectToAction("Edit", new { id = productBundle.ProductBundleId, returnToFrontEnd = viewModel.ReturnToFrontEnd, Tab = viewModel.ActiveTab })); } if (!string.IsNullOrEmpty(addProductId) || removedItem) { return(RedirectToAction("Edit", new { id = productBundle.ProductBundleId, returnToFrontEnd = viewModel.ReturnToFrontEnd, Tab = viewModel.ActiveTab })); } if (!string.IsNullOrWhiteSpace(saveAndView)) { return(RedirectToAction("Details", new { id = productBundle.ProductBundleId, returnToFrontEnd = viewModel.ReturnToFrontEnd, Tab = viewModel.ActiveTab })); } if (viewModel.ReturnToFrontEnd) { return(RedirectToAction("ViewBundleByName", "Catalog", new { area = "", urlName = viewModel.UrlName })); } if (storeFront.Authorization_IsAuthorized(CurrentUserProfileOrThrow, GStoreAction.Bundles_Manager)) { return(RedirectToAction("Manager", new { ProductCategoryId = productBundle.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.UpdateProductBundle(productBundle); 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)); }