Example #1
0
        public virtual ActionResult ProductTemplateUpdate(ProductTemplateModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenance))
            {
                return(AccessDeniedView());
            }

            if (!ModelState.IsValid)
            {
                return(Json(new DataSourceResult {
                    Errors = ModelState.SerializeErrors()
                }));
            }

            var template = _productTemplateService.GetProductTemplateById(model.Id);

            if (template == null)
            {
                throw new ArgumentException("No template found with the specified id");
            }
            template = model.ToEntity(template);
            _productTemplateService.UpdateProductTemplate(template);

            return(new NullJsonResult());
        }
Example #2
0
        public ActionResult ProductTemplateUpdate(ProductTemplateModel model, GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenance))
            {
                return(AccessDeniedView());
            }

            if (!ModelState.IsValid)
            {
                //display the first model error
                var modelStateErrors = this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                return(Content(modelStateErrors.FirstOrDefault()));
            }

            var template = _productTemplateService.GetProductTemplateById(model.Id);

            if (template == null)
            {
                throw new ArgumentException("No template found with the specified id");
            }
            template = model.ToEntity(template);
            _productTemplateService.UpdateProductTemplate(template);

            return(ProductTemplates(command));
        }
Example #3
0
        public virtual IActionResult ProductTemplateUpdate(ProductTemplateModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenance))
            {
                return(AccessDeniedView());
            }

            if (!ModelState.IsValid)
            {
                return(ErrorJson(ModelState.SerializeErrors()));
            }

            //try to get a product template with the specified id
            var template = _productTemplateService.GetProductTemplateById(model.Id)
                           ?? throw new ArgumentException("No template found with the specified id");

            template = model.ToEntity(template);
            _productTemplateService.UpdateProductTemplate(template);

            return(new NullJsonResult());
        }
        public IActionResult ProductTemplateUpdate(ProductTemplateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new DataSourceResult {
                    Errors = ModelState.SerializeErrors()
                }));
            }
            var template = _productTemplateService.GetProductTemplateById(model.Id);

            if (template == null)
            {
                throw new ArgumentException("No template found with the specified id");
            }
            if (ModelState.IsValid)
            {
                template = model.ToEntity(template);
                _productTemplateService.UpdateProductTemplate(template);
                return(new NullJsonResult());
            }
            return(ErrorForKendoGridJson(ModelState));
        }
Example #5
0
        public async Task <string> Handle(GetProductTemplateViewPath request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(request.ProductTemplateId))
            {
                throw new ArgumentNullException("ProductTemplateId");
            }

            var templateCacheKey        = string.Format(ModelCacheEventConst.PRODUCT_TEMPLATE_MODEL_KEY, request.ProductTemplateId);
            var productTemplateViewPath = await _cacheManager.GetAsync(templateCacheKey, async() =>
            {
                var template = await _productTemplateService.GetProductTemplateById(request.ProductTemplateId);
                if (template == null)
                {
                    template = (await _productTemplateService.GetAllProductTemplates()).FirstOrDefault();
                }
                if (template == null)
                {
                    throw new Exception("No default template could be loaded");
                }
                return(template.ViewPath);
            });

            return(productTemplateViewPath);
        }
        public virtual string PrepareProductTemplateViewPath(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            var templateCacheKey        = string.Format(ModelCacheEventConsumer.PRODUCT_TEMPLATE_MODEL_KEY, product.ProductTemplateId);
            var productTemplateViewPath = _cacheManager.Get(templateCacheKey, () =>
            {
                var template = _productTemplateService.GetProductTemplateById(product.ProductTemplateId);
                if (template == null)
                {
                    template = _productTemplateService.GetAllProductTemplates().FirstOrDefault();
                }
                if (template == null)
                {
                    throw new Exception("No default template could be loaded");
                }
                return(template.ViewPath);
            });

            return(productTemplateViewPath);
        }
Example #7
0
        public ProductValidator(
            IEnumerable <IValidatorConsumer <ProductDto> > validators,
            ILocalizationService localizationService, IProductService productService, IProductTemplateService productTemplateService, IVendorService vendorService, CommonSettings commonSettings)
            : base(validators)
        {
            RuleFor(x => x.Name).NotEmpty().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.Name.Required"));
            RuleFor(x => x.ProductType).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.ProductType.Required"));
            RuleFor(x => x.BackorderMode).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.BackorderMode.Required"));
            RuleFor(x => x.DownloadActivationType).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.DownloadActivationType.Required"));
            RuleFor(x => x.IntervalUnitType).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.IntervalUnitType.Required"));
            RuleFor(x => x.GiftCardType).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.GiftCardType.Required"));
            RuleFor(x => x.LowStockActivity).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.LowStockActivity.Required"));
            RuleFor(x => x.ManageInventoryMethod).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.ManageInventoryMethod.Required"));
            RuleFor(x => x.RecurringCyclePeriod).IsInEnum().WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.RecurringCyclePeriod.Required"));

            if (!commonSettings.AllowEditProductEndedAuction)
            {
                RuleFor(x => x.AuctionEnded && x.ProductType == ProductType.Auction).Equal(false).WithMessage(localizationService.GetResource("Api.Catalog.Products.Cannoteditauction"));
            }

            RuleFor(x => x.ProductType == ProductType.Auction && !x.AvailableEndDateTimeUtc.HasValue).Equal(false).WithMessage(localizationService.GetResource("Api.Catalog.Products.Fields.AvailableEndDateTime.Required"));

            RuleFor(x => x).MustAsync(async(x, y, context) =>
            {
                if (!string.IsNullOrEmpty(x.ParentGroupedProductId))
                {
                    var product = await productService.GetProductById(x.ParentGroupedProductId);
                    if (product == null)
                    {
                        return(false);
                    }
                }
                return(true);
            }).WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.ParentGroupedProductId.NotExists"));

            RuleFor(x => x).MustAsync(async(x, y, context) =>
            {
                if (!string.IsNullOrEmpty(x.ProductTemplateId))
                {
                    var template = await productTemplateService.GetProductTemplateById(x.ProductTemplateId);
                    if (template == null)
                    {
                        return(false);
                    }
                }
                return(true);
            }).WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.ProductTemplateId.NotExists"));

            RuleFor(x => x).MustAsync(async(x, y, context) =>
            {
                if (!string.IsNullOrEmpty(x.VendorId))
                {
                    var vendor = await vendorService.GetVendorById(x.VendorId);
                    if (vendor == null)
                    {
                        return(false);
                    }
                }
                return(true);
            }).WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.VendorId.NotExists"));


            RuleFor(x => x).MustAsync(async(x, y, context) =>
            {
                if (!string.IsNullOrEmpty(x.Id))
                {
                    var product = await productService.GetProductById(x.Id);
                    if (product == null)
                    {
                        return(false);
                    }
                }
                return(true);
            }).WithMessage(localizationService.GetResource("Api.Catalog.Product.Fields.Id.NotExists"));
        }
Example #8
0
        protected ProductDetailsModel PrepareProductDetailsPageModel(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            var model = new ProductDetailsModel()
            {
                Id                   = product.Id,
                Name                 = product.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id),
                ShortDescription     = product.GetLocalized(x => x.ShortDescription, _workContext.WorkingLanguage.Id),
                FullDescription      = product.GetLocalized(x => x.FullDescription, _workContext.WorkingLanguage.Id),
                OrderingComments     = product.GetLocalized(x => x.AdminComment, _workContext.WorkingLanguage.Id, false),
                MetaKeywords         = product.GetLocalized(x => x.MetaKeywords, _workContext.WorkingLanguage.Id),
                MetaDescription      = product.GetLocalized(x => x.MetaDescription, _workContext.WorkingLanguage.Id),
                MetaTitle            = product.GetLocalized(x => x.MetaTitle, _workContext.WorkingLanguage.Id),
                SeName               = product.GetSeName(),
                MinimumOrderQuantity = product.MinimumOrderQuantity.HasValue ? product.MinimumOrderQuantity.Value : 1,
                Favorit              = _favoritsService.IsItemFavorit(_workContext.CurrentCustomer.Id, product.Id)
            };

            //template

            var templateCacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_TEMPLATE_MODEL_KEY, product.ProductTemplateId);

            model.ProductTemplateViewPath = _cacheManager.Get(templateCacheKey, () =>
            {
                var template = _productTemplateService.GetProductTemplateById(product.ProductTemplateId);
                if (template == null)
                {
                    template = _productTemplateService.GetAllProductTemplates().FirstOrDefault();
                }
                return(template.ViewPath);
            });

            //pictures
            model.DefaultPictureZoomEnabled = _mediaSettings.DefaultPictureZoomEnabled;
            var pictures = _pictureService.GetPicturesByProductId(product.Id);

            if (pictures.Count > 0)
            {
                //default picture
                model.DefaultPictureModel = new PictureModel()
                {
                    ImageUrl         = _pictureService.GetPictureUrl(pictures.FirstOrDefault()),
                    FullSizeImageUrl = _pictureService.GetPictureUrl(pictures.FirstOrDefault()),
                    Title            = string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"), model.Name),
                    AlternateText    = string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat"), model.Name),
                };
                //all pictures
                int i = 0;
                foreach (var picture in pictures)
                {
                    model.PictureModels.Add(new PictureModel()
                    {
                        ImageUrl         = _pictureService.GetPictureUrl(picture),
                        FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                        Title            = string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"), model.Name),
                        AlternateText    = string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat"), model.Name),
                        Default          = i == 0
                    });
                    i++;
                }
            }
            else
            {
                //no images. set the default one
                model.DefaultPictureModel = new PictureModel()
                {
                    ImageUrl         = _pictureService.GetDefaultPictureUrl(_mediaSettings.ProductDetailsPictureSize),
                    FullSizeImageUrl = _pictureService.GetDefaultPictureUrl(),
                    Title            = string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"), model.Name),
                    AlternateText    = string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat"), model.Name),
                };
            }

            List <CategoryProductAttributeGroup> _attrGroups         = product.ProductAttributes.Select(x => x.CategoryProductAttribute.CategoryProductGroup).Distinct().ToList();
            List <CategoryAttributeModel>        DisplayedAttributes = new List <CategoryAttributeModel>();

            foreach (var _aG in _attrGroups)
            {
                foreach (var cpa in _aG.CategoryProductAttributes)
                {
                    CategoryAttributeModel cam = new CategoryAttributeModel();
                    cam.Values = new List <CategoryProductAttributeValueModel>();
                    cam.Values.AddRange(cpa.CategoryProductAttributeValues.OrderBy(x => x.DisplayOrder)
                                        .ThenBy(x => x.Name)
                                        .Select(x =>
                    {
                        var md = new CategoryProductAttributeValueModel();
                        if (x.CategoryProductAttribute.AttributeControlType != AttributeControlType.TextBox)
                        {
                            md.Name = x.GetLocalized(z => z.Name, _workContext.WorkingLanguage.Id, false);
                        }
                        else
                        {
                            md.Name = x.Name;
                        }
                        md.IsPreSelected = product.ProductAttributes.Where(p => p.Id == x.Id).Count() > 0;
                        md.CategoryProductAttributeId = x.CategoryProductAttributeId;
                        md.Id                         = x.Id;
                        md.DisplayOrder               = x.DisplayOrder;
                        md.ColorSquaresRgb            = x.ColorSquaresRgb;
                        md.CategoryProductAttributeId = x.CategoryProductAttributeId;
                        return(md);
                    })
                                        .ToList());
                    //cam.Values.ForEach(i =>
                    //{
                    //    i.Name = i.GetLocalized(xi => xi.Name, _workContext.WorkingLanguage.Id, true);
                    //});

                    cam.Name        = cpa.ProductAttribute.GetLocalized(n => n.Name, _workContext.WorkingLanguage.Id, false);
                    cam.ControlType = cpa.AttributeControlType;
                    //cam.Values.ForEach(x =>
                    //{
                    //    x.IsPreSelected = product.ProductAttributes.Where(i => i.Id == x.Id).Count() > 0;
                    //});
                    //foreach (var val in cam.Values)
                    //{
                    //    val.IsPreSelected = product.ProductAttributes.Where(p => p.Id == val.Id).Count() > 0;
                    //}
                    var attrValue = cam.Values.Where(i => i.IsPreSelected).FirstOrDefault();
                    cam.SelectedValue = attrValue;
                    cam.DisplayOrder  = cpa.DisplayOrder;
                    //cam.SelectedValue.Name = attrValue.GetLocalized(v => v.Name, _workContext.WorkingLanguage.Id, true);
                    //cam.Values.ForEach(i => { i.Name = i.GetLocalized(xi => xi.Name, _workContext.WorkingLanguage.Id, true); });
                    DisplayedAttributes.Add(cam);
                }
            }
            model.CategoryAttributes = DisplayedAttributes.OrderBy(x => x.DisplayOrder).ToList();

            //product tags
            foreach (var item in product.ProductTags)
            {
                model.ProductTags.Add(new ProductTagModel()
                {
                    Name         = item.Name,
                    ProductCount = item.ProductCount,
                    Id           = item.Id
                });
            }
            model.CompanyInformationModel               = new CompanyInformationModel();
            model.CompanyInformationModel.CompanyName   = product.Customer.CompanyInformation.GetLocalized(x => x.CompanyName, _workContext.WorkingLanguage.Id, false);
            model.CompanyInformationModel.CompanySeName = product.Customer.CompanyInformation.GetSeName(_workContext.WorkingLanguage.Id);
            if (model.CompanyInformationModel.CompanyName == null)
            {
                var languages = _languageService.GetAllLanguages().ToList();
                model.CompanyInformationModel.CompanyName = product.Customer.CompanyInformation.GetLocalized(x => x.CompanyName, languages.Where(l => l.LanguageCulture == "es-MX").FirstOrDefault().Id, false);
                model.CompanyInformationModel.CompanyName = product.Customer.CompanyInformation.GetLocalized(x => x.CompanyName, languages.Where(l => l.LanguageCulture == "de-DE").FirstOrDefault().Id, false);
                model.CompanyInformationModel.CompanyName = product.Customer.CompanyInformation.GetLocalized(x => x.CompanyName, languages.Where(l => l.LanguageCulture == "en-US").FirstOrDefault().Id, false);
                model.CompanyInformationModel.CompanyName = product.Customer.CompanyInformation.GetLocalized(x => x.CompanyName, languages.Where(l => l.LanguageCulture == "ru-RU").FirstOrDefault().Id, false);
            }

            var prices     = _productPriceService.GetAllProductPrices(model.Id);
            var currencies = _currencyService.GetAllCurrencies().Where(c => c.Published).ToList();

            model.ProductPrices = new List <ProductDetailsModel.ProductPriceModel>();
            var prices_to_delete = prices.Where(p => !currencies.Contains(p.Currency)).ToList();

            prices = prices.Where(p => currencies.Contains(p.Currency)).ToList();
            foreach (var p in prices_to_delete)
            {
                _productPriceService.DeleteProductPriceById(p.Id);
            }
            model.ProductPrices = new List <ProductDetailsModel.ProductPriceModel>();
            foreach (var price in prices)
            {
                model.ProductPrices.Add(new ProductDetailsModel.ProductPriceModel()
                {
                    CurrencyId     = price.CurrencyId,
                    Id             = price.Id,
                    Price          = price.Price,
                    PriceUpdatedOn = price.PriceUpdatedOn,
                    PriceValue     = price.Price.ToString("N2"),
                    ProductId      = price.ProductId,
                    Currency       = new Core.Domain.Directory.Currency()
                    {
                        CurrencyCode = price.Currency.CurrencyCode
                    }
                });
            }

            return(model);
        }