public ActionResult Create(ProductBundleEditAdminViewModel viewModel, string createAndView)
        {
            StoreFront storeFront = CurrentStoreFrontOrThrow;
            bool urlIsValid = GStoreDb.ValidateProductBundleUrlName(this, viewModel.UrlName, storeFront.StoreFrontId, storeFront.ClientId, null);

            if (urlIsValid && ModelState.IsValid)
            {
                try
                {
                    ProcessFileUploads(viewModel, storeFront);
                    ProductBundle productBundle = GStoreDb.CreateProductBundle(viewModel, storeFront, CurrentUserProfileOrThrow);
                    AddUserMessage("Product Bundle Created!", "Product Bundle '" + productBundle.Name.ToHtml() + "' [" + productBundle.ProductBundleId + "] was created successfully for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Success);
                    if (!string.IsNullOrWhiteSpace(createAndView))
                    {
                        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");
                }
                catch (Exception ex)
                {
                    string errorMessage = "An error occurred while Creating Product Bundle '" + viewModel.Name + "' for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "] \nError: " + ex.GetType().FullName;

                    if (CurrentUserProfileOrThrow.AspNetIdentityUserIsInRoleSystemAdmin())
                    {
                        errorMessage += " \nException.ToString(): " + ex.ToString();
                    }
                    AddUserMessage("Error Creating Product Bundle!", errorMessage.ToHtmlLines(), UserMessageType.Danger);
                    ModelState.AddModelError("Ajax", errorMessage);
                }
            }
            else
            {
                AddUserMessage("Create Product Bundle Error", "There was an error with your entry for new Product Bundle '" + viewModel.Name.ToHtml() + "' for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]. Please correct it below and save.", UserMessageType.Danger);

            }

            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.IsCreatePage = true;
            ViewData.Add("ReturnToFrontEnd", viewModel.ReturnToFrontEnd);
            return View("CreateOrEdit", viewModel);
        }
        public ActionResult Create(int? id, bool returnToFrontEnd = false, string Tab = "")
        {
            ProductBundle productBundle = GStoreDb.ProductBundles.Create();
            productBundle.SetDefaultsForNew(CurrentStoreFrontOrThrow);
            if (id.HasValue)
            {
                ProductCategory parentProductCategory = CurrentStoreFrontOrThrow.ProductCategories.SingleOrDefault(pc => pc.ProductCategoryId == id.Value);
                if (parentProductCategory != null)
                {
                    productBundle.Category = parentProductCategory;
                    productBundle.ProductCategoryId = parentProductCategory.ProductCategoryId;
                }
            }

            ProductBundleEditAdminViewModel viewModel = new ProductBundleEditAdminViewModel(productBundle, CurrentUserProfileOrThrow, Tab, isCreatePage: true);
            viewModel.ReturnToFrontEnd = returnToFrontEnd;
            ViewData.Add("ReturnToFrontEnd", returnToFrontEnd);
            return View("CreateOrEdit", viewModel);
        }
        protected void ProcessFileUploads(ProductBundleEditAdminViewModel viewModel, StoreFront storeFront)
        {
            string virtualFolder = storeFront.CatalogProductBundleContentVirtualDirectoryToMap(Request.ApplicationPath);
            string fileFolder = Server.MapPath(virtualFolder);
            if (!System.IO.Directory.Exists(fileFolder))
            {
                System.IO.Directory.CreateDirectory(fileFolder);
            }

            HttpPostedFileBase imageFile = Request.Files["ImageName_File"];
            if (imageFile != null && imageFile.ContentLength != 0)
            {
                string newFileName = imageFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["ImageName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_Image." + imageFile.FileName.FileExtension();
                }

                try
                {
                    imageFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving bundle image file '" + imageFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.ImageName = newFileName;
                AddUserMessage("Image Uploaded!", "Bundle Image '" + imageFile.FileName.ToHtml() + "' " + imageFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }
        }
        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(int? id, string Tab, bool returnToFrontEnd = false)
        {
            if (!id.HasValue)
            {
                return HttpBadRequest("ProductBundleId = null");
            }

            StoreFront storeFront = CurrentStoreFrontOrThrow;
            ProductBundle productBundle = storeFront.ProductBundles.Where(p => p.ProductBundleId == id.Value).SingleOrDefault();
            if (productBundle == null)
            {
                AddUserMessage("Product Bundle not found", "Sorry, the Product Bundle you are trying to edit cannot be found. Product Bundle Id: [" + id.Value + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Danger);
                if (returnToFrontEnd)
                {
                    return RedirectToAction("Index", "Catalog", new { area = "" });
                }
                if (storeFront.Authorization_IsAuthorized(CurrentUserProfileOrThrow, GStoreAction.Bundles_Manager))
                {
                    return RedirectToAction("Manager", new { ProductCategoryId = productBundle.ProductCategoryId });
                }
                return RedirectToAction("Index", "CatalogAdmin");
            }

            ProductBundleEditAdminViewModel viewModel = new ProductBundleEditAdminViewModel(productBundle, CurrentUserProfileOrThrow, activeTab: Tab, isEditPage: true);
            viewModel.ReturnToFrontEnd = returnToFrontEnd;
            ViewData.Add("ReturnToFrontEnd", returnToFrontEnd);
            return View("CreateOrEdit", viewModel);
        }