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

            if (urlIsValid && ModelState.IsValid)
            {
                try
                {
                    ProcessFileUploads(viewModel, storeFront);
                    ProductCategory productCategory = GStoreDb.CreateProductCategory(viewModel, storeFront, CurrentUserProfileOrThrow);
                    AddUserMessage("Category Created!", "Category '" + productCategory.Name.ToHtml() + "' [" + productCategory.ProductCategoryId + "] was created successfully for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Success);

                    if (!string.IsNullOrWhiteSpace(createAndView))
                    {
                        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");
                }
                catch (Exception ex)
                {
                    string errorMessage = "An error occurred while Creating Category '" + 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 Category Item!", errorMessage.ToHtmlLines(), UserMessageType.Danger);
                    ModelState.AddModelError("Ajax", errorMessage);
                }
            }
            else
            {
                if (this.ModelState.ContainsKey("UrlName"))
                {
                    ModelState["UrlName"].Value = new ValueProviderResult(viewModel.UrlName, viewModel.UrlName, null);
                }
                AddUserMessage("Create Category Error", "There was an error with your entry for new Category '" + 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.IsSimpleCreatePage = true;
            return View("CreateOrEdit", viewModel);
        }
        public ActionResult Create(int? id, bool returnToFrontEnd = false, string Tab = "")
        {
            ProductCategory productCategory = GStoreDb.ProductCategories.Create();
            productCategory.SetDefaultsForNew(CurrentStoreFrontOrThrow);
            if (id.HasValue)
            {
                ProductCategory parentProductCategory = CurrentStoreFrontOrThrow.ProductCategories.SingleOrDefault(pc => pc.ProductCategoryId == id.Value);
                if (parentProductCategory != null)
                {
                    productCategory.CloneFromParentForNew(parentProductCategory);
                }
            }

            CategoryEditAdminViewModel viewModel = new CategoryEditAdminViewModel(productCategory, CurrentUserProfileOrThrow, activeTab: Tab, isCreatePage: true);
            viewModel.ReturnToFrontEnd = returnToFrontEnd;
            return View("CreateOrEdit", viewModel);
        }
        public ActionResult Delete(int? id, string Tab, bool returnToFrontEnd = false)
        {
            if (!id.HasValue)
            {
                return HttpBadRequest("ProductCategoryId = null");
            }

            StoreFront storeFront = CurrentStoreFrontOrThrow;
            ProductCategory productCategory = storeFront.ProductCategories.Where(pc => pc.ProductCategoryId == id.Value).SingleOrDefault();
            if (productCategory == null)
            {
                AddUserMessage("Category not found", "Sorry, the Category you are trying to Delete cannot be found. Category id: [" + id.Value + "] for Store Front '" + storeFront.CurrentConfig().Name.ToHtml() + "' [" + storeFront.StoreFrontId + "]", UserMessageType.Danger);
                if (returnToFrontEnd)
                {
                    return RedirectToAction("ViewCategoryByName", "Catalog", new { area = "", urlName = productCategory.UrlName });
                }
                return RedirectToAction("Manager");
            }

            CategoryEditAdminViewModel viewModel = new CategoryEditAdminViewModel(productCategory, CurrentUserProfileOrThrow, isDeletePage: true, activeTab: Tab);
            viewModel.ReturnToFrontEnd = returnToFrontEnd;
            return View("Delete", viewModel);
        }
        protected void ProcessFileUploads(CategoryEditAdminViewModel viewModel, StoreFront storeFront)
        {
            string virtualFolder = storeFront.CatalogCategoryContentVirtualDirectoryToMap(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 category image file '" + imageFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.ImageName = newFileName;
                AddUserMessage("Image Uploaded!", "Category Image '" + imageFile.FileName.ToHtml() + "' " + imageFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }
        }
        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);
        }