Esempio n. 1
0
        private SearchProductCategoryModel RestoreSearchModel(SearchProductCategoryModel model)
        {
            foreach (var attributeModel in model.Attributes)
            {
                var attribute = _categoryProductAttributeService.GetCategoryProductAttributeById(attributeModel.Id);
                attributeModel.PictureThumbnailUrl = _pictureService.GetPictureUrl(attribute.ProductAttribute.PictureId.GetValueOrDefault(), 100, false);
                attributeModel.Description         = attribute.ProductAttribute.Description;
                switch (attributeModel.AttributeControlTypeId)
                {
                case (int)SearchAttributeControlType.CheckBox:
                case (int)SearchAttributeControlType.CheckBoxGroup:
                {
                    //if(attributeModel
                    break;
                }

                case (int)SearchAttributeControlType.DropDown:
                {
                    var attributeValues = attribute.CategoryProductAttributeValues;
                    if (attributeValues.Count > 0)
                    {
                        attributeModel.Values = new List <SearchProductAttributeValueModel>();

                        if (attributeModel.SelectedAttributeId == 0)
                        {
                            attributeModel.Values.Add(new SearchProductAttributeValueModel()
                                {
                                    Id        = 0,
                                    ValueText = attribute.MainAttribute ? attribute.ProductAttribute.Name : _localizationService.GetResource("ITBFA.Attribute.NoImportant"),
                                    Selected  = true
                                });
                        }
                        //else
                        //{
                        //    if (attributeModel.Values.FirstOrDefault(x => x.Id == 0) == null)
                        //    {
                        //        attributeModel.Values.Add(new SearchProductAttributeValueModel()
                        //        {
                        //            Id = 0,
                        //            ValueText = attribute.ProductAttribute.Name,
                        //            Selected = false
                        //        });
                        //    }
                        //}

                        if (attributeModel.Values.FirstOrDefault(x => x.Selected) == null)
                        {
                            attributeModel.Values.Add(new SearchProductAttributeValueModel()
                                {
                                    Id        = 0,
                                    ValueText = attribute.MainAttribute ? attribute.ProductAttribute.Name : _localizationService.GetResource("ITBFA.Attribute.NoImportant"),
                                    Selected  = true
                                });
                        }

                        foreach (var value in attributeValues.OrderBy(x => x.DisplayOrder))
                        {
                            var attributeValueModel = new SearchProductAttributeValueModel();
                            attributeValueModel.Id           = value.Id;
                            attributeValueModel.ValueText    = value.Name;
                            attributeValueModel.Selected     = value.Id == attributeModel.SelectedAttributeId;
                            attributeValueModel.Popularvalue = value.PopularValue;
                            attributeModel.Values.Add(attributeValueModel);
                        }
                    }
                    break;
                }

                case (int)SearchAttributeControlType.ToddlerIntBetween:
                case (int)SearchAttributeControlType.ToddlerMax:
                case (int)SearchAttributeControlType.ToddlerMin:
                {
                    var values = attribute.CategoryProductAttributeValues.Where(x => x.RealValue.HasValue)
                                 .Where(x => x.Products.Where(p => !p.Deleted).Any() || x.Products.Count == 0);
                    if (values.Count() == 0)
                    {
                        attributeModel.MinValue = 0;
                        attributeModel.MinValue = int.MaxValue;
                    }
                    else
                    {
                        values = values.OrderByDescending(x => x.RealValue).ToList();
                        attributeModel.MinValue = (int)values.Last().RealValue;
                        attributeModel.MaxValue = (int)values.First().RealValue;
                        attributeModel.Values   = new List <SearchProductAttributeValueModel>();
                        foreach (var val in attribute.CategoryProductAttributeValues.Where(x => x.PopularValue).Where(x => x.RealValue.HasValue))
                        {
                            var popularValue = new SearchProductAttributeValueModel();
                            popularValue.ValueDouble = ((int)val.RealValue).ToString();
                            attributeModel.Values.Add(popularValue);
                        }
                    }

                    break;
                }
                }
            }

            var currencies = _currencyService.GetAllCurrencies().Select(x => new CurrencyModel()
            {
                Id   = x.Id,
                Name = x.CurrencyCode
            }).ToList();

            model.Currencies = currencies;
            return(model);
        }
Esempio n. 2
0
        public ActionResult CategoryProductAttributeUpdate(GridCommand command, CategoryProductAttributeModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
            {
                return(AccessDeniedView());
            }

            var pva = _categoryProductAttributeService.GetCategoryProductAttributeById(model.Id);

            if (pva == null)
            {
                throw new ArgumentException("No product variant attribute found with the specified id");
            }

            //use ProductAttribute property (not ProductAttributeId) because appropriate property is stored in it
            pva.ProductAttributeId  = Int32.Parse(model.ProductAttribute);
            pva.TextPrompt          = model.TextPrompt;
            pva.IsRequired          = model.IsRequired;
            pva.MainAttribute       = model.MainAttribute;
            pva.ProductBoxAttribute = model.ProductBoxAttribute;
            pva.AdditionalAttribute = model.AdditionalAttribute;
            //use AttributeControlType property (not AttributeControlTypeId) because appropriate property is stored in it
            if (pva.AttributeControlTypeId != Int32.Parse(model.AttributeControlType))
            {
                foreach (var val in pva.CategoryProductAttributeValues.ToList())
                {
                    _categoryProductAttributeService.DeleteCategoryProductAttributeValue(val);
                }
            }
            pva.AttributeControlTypeId = Int32.Parse(model.AttributeControlType);

            pva.SearchControlTypeId = Int32.Parse(model.SearchAttributeControlType);
            pva.DisplayOrder        = model.DisplayOrder1;

            bool flag       = false;
            var  categories = _categoryService.GetAllCategories().Where(x => x.CategoryToCategoryProductAttributeGroups.Where(g => g.CategoryProductAttributeGroupId == pva.CategoryProductGroupId).FirstOrDefault() != null);

            foreach (var category in categories)
            {
                foreach (var group in category.CategoryToCategoryProductAttributeGroups.Where(x => x.CategoryProductAttributeGroupId != pva.CategoryProductGroupId)
                         .Select(x => x.CategoryProductAttributeGroup))
                {
                    var attr = group.CategoryProductAttributes.Where(x => x.ProductAttributeId == pva.ProductAttributeId).FirstOrDefault();
                    if (attr != null)
                    {
                        flag = true;
                    }
                }
            }
            //productAttribute.
            string conflictMessage = "";

            conflictMessage = ValidateForConflict(pva);
            if (!flag)
            {
                if (!String.IsNullOrEmpty(conflictMessage))
                {
                    ModelState.AddModelError("ProductAttribute", conflictMessage);
                    var modelStateErrors = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                    return(Content(modelStateErrors.FirstOrDefault()));
                }
                _categoryProductAttributeService.UpdateCategoryProductAttribute(pva);
            }
            else
            {
                ModelState.AddModelError("ProductAttribute", "Category attribute group conflict");
                var modelStateErrors = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                return(Content(modelStateErrors.FirstOrDefault()));
            }
            return(CategoryProductAttributeList(command, pva.CategoryProductGroupId));
        }