public virtual IActionResult Edit(CategoryModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
            {
                return(AccessDeniedView());
            }

            //try to get a category with the specified id
            var category = _categoryService.GetCategoryById(model.Id);

            if (category == null || category.Deleted)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevPictureId = category.PictureId;

                //if parent category changes, we need to clear cache for previous parent category
                if (category.ParentCategoryId != model.ParentCategoryId)
                {
                    var prefix = _cacheKeyService.PrepareKeyPrefix(NopCatalogDefaults.CategoriesByParentCategoryPrefixCacheKey, category.ParentCategoryId);
                    _staticCacheManager.RemoveByPrefix(prefix);
                    prefix = _cacheKeyService.PrepareKeyPrefix(NopCatalogDefaults.CategoriesChildIdentifiersPrefixCacheKey, category.ParentCategoryId);
                    _staticCacheManager.RemoveByPrefix(prefix);
                }

                category = model.ToEntity(category);
                category.UpdatedOnUtc = DateTime.UtcNow;
                _categoryService.UpdateCategory(category);

                //search engine name
                model.SeName = _urlRecordService.ValidateSeName(category, model.SeName, category.Name, true);
                _urlRecordService.SaveSlug(category, model.SeName, 0);

                //locales
                UpdateLocales(category, model);

                //discounts
                var allDiscounts = _discountService.GetAllDiscounts(DiscountType.AssignedToCategories, showHidden: true);
                foreach (var discount in allDiscounts)
                {
                    if (model.SelectedDiscountIds != null && model.SelectedDiscountIds.Contains(discount.Id))
                    {
                        //new discount
                        if (_categoryService.GetDiscountAppliedToCategory(category.Id, discount.Id) is null)
                        {
                            _categoryService.InsertDiscountCategoryMapping(new DiscountCategoryMapping {
                                DiscountId = discount.Id, EntityId = category.Id
                            });
                        }
                    }
                    else
                    {
                        //remove discount
                        if (_categoryService.GetDiscountAppliedToCategory(category.Id, discount.Id) is DiscountCategoryMapping mapping)
                        {
                            _categoryService.DeleteDiscountCategoryMapping(mapping);
                        }
                    }
                }

                _categoryService.UpdateCategory(category);

                //delete an old picture (if deleted or updated)
                if (prevPictureId > 0 && prevPictureId != category.PictureId)
                {
                    var prevPicture = _pictureService.GetPictureById(prevPictureId);
                    if (prevPicture != null)
                    {
                        _pictureService.DeletePicture(prevPicture);
                    }
                }

                //update picture seo file name
                UpdatePictureSeoNames(category);

                //ACL
                SaveCategoryAcl(category, model);

                //stores
                SaveStoreMappings(category, model);

                //activity log
                _customerActivityService.InsertActivity("EditCategory",
                                                        string.Format(_localizationService.GetResource("ActivityLog.EditCategory"), category.Name), category);

                _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Catalog.Categories.Updated"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = category.Id }));
            }

            //prepare model
            model = _categoryModelFactory.PrepareCategoryModel(model, category, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #2
0
        protected virtual string GetKey(string systemName, Customer customer, int storeId)
        {
            var roles = customer == null?Array.Empty <int>() : _customerService.GetCustomerRoleIds(customer);

            return(_cacheKeyService.PrepareKeyPrefix(KEY_FORMAT, systemName, roles, storeId));
        }