/// <summary> /// Prepare paged vendor attribute list model /// </summary> /// <param name="searchModel">Vendor attribute search model</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the vendor attribute list model /// </returns> public virtual async Task <VendorAttributeListModel> PrepareVendorAttributeListModelAsync(VendorAttributeSearchModel searchModel) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } //get vendor attributes var vendorAttributes = (await _vendorAttributeService.GetAllVendorAttributesAsync()).ToPagedList(searchModel); //prepare list model var model = await new VendorAttributeListModel().PrepareToGridAsync(searchModel, vendorAttributes, () => { return(vendorAttributes.SelectAwait(async attribute => { //fill in model values from the entity var attributeModel = attribute.ToModel <VendorAttributeModel>(); //fill in additional values (not existing in the entity) attributeModel.AttributeControlTypeName = await _localizationService.GetLocalizedEnumAsync(attribute.AttributeControlType); return attributeModel; })); }); return(model); }
/// <summary> /// Validates vendor attributes /// </summary> /// <param name="attributesXml">Attributes in XML format</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the warnings /// </returns> public virtual async Task <IList <string> > GetAttributeWarningsAsync(string attributesXml) { var warnings = new List <string>(); //ensure it's our attributes var attributes1 = await ParseVendorAttributesAsync(attributesXml); //validate required vendor attributes (whether they're chosen/selected/entered) var attributes2 = await _vendorAttributeService.GetAllVendorAttributesAsync(); foreach (var a2 in attributes2) { if (!a2.IsRequired) { continue; } var found = false; //selected vendor attributes foreach (var a1 in attributes1) { if (a1.Id != a2.Id) { continue; } var valuesStr = ParseValues(attributesXml, a1.Id); foreach (var str1 in valuesStr) { if (string.IsNullOrEmpty(str1.Trim())) { continue; } found = true; break; } } if (found) { continue; } //if not found var notFoundWarning = string.Format(await _localizationService.GetResourceAsync("ShoppingCart.SelectAttribute"), await _localizationService.GetLocalizedAsync(a2, a => a.Name)); warnings.Add(notFoundWarning); } return(warnings); }
/// <summary> /// Prepare vendor attribute models /// </summary> /// <param name="vendorAttributesXml">Vendor attributes in XML format</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the list of the vendor attribute model /// </returns> protected virtual async Task <IList <VendorAttributeModel> > PrepareVendorAttributesAsync(string vendorAttributesXml) { var result = new List <VendorAttributeModel>(); var vendorAttributes = await _vendorAttributeService.GetAllVendorAttributesAsync(); foreach (var attribute in vendorAttributes) { var attributeModel = new VendorAttributeModel { Id = attribute.Id, Name = await _localizationService.GetLocalizedAsync(attribute, x => x.Name), IsRequired = attribute.IsRequired, AttributeControlType = attribute.AttributeControlType, }; if (attribute.ShouldHaveValues()) { //values var attributeValues = await _vendorAttributeService.GetVendorAttributeValuesAsync(attribute.Id); foreach (var attributeValue in attributeValues) { var valueModel = new VendorAttributeValueModel { Id = attributeValue.Id, Name = await _localizationService.GetLocalizedAsync(attributeValue, x => x.Name), IsPreSelected = attributeValue.IsPreSelected }; attributeModel.Values.Add(valueModel); } } switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: case AttributeControlType.Checkboxes: { if (!string.IsNullOrEmpty(vendorAttributesXml)) { //clear default selection foreach (var item in attributeModel.Values) { item.IsPreSelected = false; } //select new values var selectedValues = await _vendorAttributeParser.ParseVendorAttributeValuesAsync(vendorAttributesXml); foreach (var attributeValue in selectedValues) { foreach (var item in attributeModel.Values) { if (attributeValue.Id == item.Id) { item.IsPreSelected = true; } } } } } break; case AttributeControlType.ReadonlyCheckboxes: { //do nothing //values are already pre-set } break; case AttributeControlType.TextBox: case AttributeControlType.MultilineTextbox: { if (!string.IsNullOrEmpty(vendorAttributesXml)) { var enteredText = _vendorAttributeParser.ParseValues(vendorAttributesXml, attribute.Id); if (enteredText.Any()) { attributeModel.DefaultValue = enteredText[0]; } } } break; case AttributeControlType.ColorSquares: case AttributeControlType.ImageSquares: case AttributeControlType.Datepicker: case AttributeControlType.FileUpload: default: //not supported attribute control types break; } result.Add(attributeModel); } return(result); }
/// <summary> /// Prepare vendor attribute models /// </summary> /// <param name="models">List of vendor attribute models</param> /// <param name="vendor">Vendor</param> /// <returns>A task that represents the asynchronous operation</returns> protected virtual async Task PrepareVendorAttributeModelsAsync(IList <VendorModel.VendorAttributeModel> models, Vendor vendor) { if (models == null) { throw new ArgumentNullException(nameof(models)); } //get available vendor attributes var vendorAttributes = await _vendorAttributeService.GetAllVendorAttributesAsync(); foreach (var attribute in vendorAttributes) { var attributeModel = new VendorModel.VendorAttributeModel { Id = attribute.Id, Name = attribute.Name, IsRequired = attribute.IsRequired, AttributeControlType = attribute.AttributeControlType }; if (attribute.ShouldHaveValues()) { //values var attributeValues = await _vendorAttributeService.GetVendorAttributeValuesAsync(attribute.Id); foreach (var attributeValue in attributeValues) { var attributeValueModel = new VendorModel.VendorAttributeValueModel { Id = attributeValue.Id, Name = attributeValue.Name, IsPreSelected = attributeValue.IsPreSelected }; attributeModel.Values.Add(attributeValueModel); } } //set already selected attributes if (vendor != null) { var selectedVendorAttributes = await _genericAttributeService.GetAttributeAsync <string>(vendor, NopVendorDefaults.VendorAttributes); switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: case AttributeControlType.Checkboxes: { if (!string.IsNullOrEmpty(selectedVendorAttributes)) { //clear default selection foreach (var item in attributeModel.Values) { item.IsPreSelected = false; } //select new values var selectedValues = await _vendorAttributeParser.ParseVendorAttributeValuesAsync(selectedVendorAttributes); foreach (var attributeValue in selectedValues) { foreach (var item in attributeModel.Values) { if (attributeValue.Id == item.Id) { item.IsPreSelected = true; } } } } } break; case AttributeControlType.ReadonlyCheckboxes: { //do nothing //values are already pre-set } break; case AttributeControlType.TextBox: case AttributeControlType.MultilineTextbox: { if (!string.IsNullOrEmpty(selectedVendorAttributes)) { var enteredText = _vendorAttributeParser.ParseValues(selectedVendorAttributes, attribute.Id); if (enteredText.Any()) { attributeModel.DefaultValue = enteredText[0]; } } } break; case AttributeControlType.Datepicker: case AttributeControlType.ColorSquares: case AttributeControlType.ImageSquares: case AttributeControlType.FileUpload: default: //not supported attribute control types break; } } models.Add(attributeModel); } }
/// <returns>A task that represents the asynchronous operation</returns> protected virtual async Task <string> ParseVendorAttributesAsync(IFormCollection form) { if (form == null) { throw new ArgumentNullException(nameof(form)); } var attributesXml = string.Empty; var vendorAttributes = await _vendorAttributeService.GetAllVendorAttributesAsync(); foreach (var attribute in vendorAttributes) { var controlId = $"{NopVendorDefaults.VendorAttributePrefix}{attribute.Id}"; StringValues ctrlAttributes; switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: ctrlAttributes = form[controlId]; if (!StringValues.IsNullOrEmpty(ctrlAttributes)) { var selectedAttributeId = int.Parse(ctrlAttributes); if (selectedAttributeId > 0) { attributesXml = _vendorAttributeParser.AddVendorAttribute(attributesXml, attribute, selectedAttributeId.ToString()); } } break; case AttributeControlType.Checkboxes: var cblAttributes = form[controlId]; if (!StringValues.IsNullOrEmpty(cblAttributes)) { foreach (var item in cblAttributes.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { var selectedAttributeId = int.Parse(item); if (selectedAttributeId > 0) { attributesXml = _vendorAttributeParser.AddVendorAttribute(attributesXml, attribute, selectedAttributeId.ToString()); } } } break; case AttributeControlType.ReadonlyCheckboxes: //load read-only (already server-side selected) values var attributeValues = await _vendorAttributeService.GetVendorAttributeValuesAsync(attribute.Id); foreach (var selectedAttributeId in attributeValues .Where(v => v.IsPreSelected) .Select(v => v.Id) .ToList()) { attributesXml = _vendorAttributeParser.AddVendorAttribute(attributesXml, attribute, selectedAttributeId.ToString()); } break; case AttributeControlType.TextBox: case AttributeControlType.MultilineTextbox: ctrlAttributes = form[controlId]; if (!StringValues.IsNullOrEmpty(ctrlAttributes)) { var enteredText = ctrlAttributes.ToString().Trim(); attributesXml = _vendorAttributeParser.AddVendorAttribute(attributesXml, attribute, enteredText); } break; case AttributeControlType.Datepicker: case AttributeControlType.ColorSquares: case AttributeControlType.ImageSquares: case AttributeControlType.FileUpload: //not supported vendor attributes default: break; } } return(attributesXml); }