/// <summary> /// Format vendor attributes /// </summary> /// <param name="attributesXml">Attributes in XML format</param> /// <param name="separator">Separator</param> /// <param name="htmlEncode">A value indicating whether to encode (HTML) values</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the formatted attributes /// </returns> public virtual async Task <string> FormatAttributesAsync(string attributesXml, string separator = "<br />", bool htmlEncode = true) { var result = new StringBuilder(); var currentLanguage = await _workContext.GetWorkingLanguageAsync(); var attributes = await _vendorAttributeParser.ParseVendorAttributesAsync(attributesXml); for (var i = 0; i < attributes.Count; i++) { var attribute = attributes[i]; var valuesStr = _vendorAttributeParser.ParseValues(attributesXml, attribute.Id); for (var j = 0; j < valuesStr.Count; j++) { var valueStr = valuesStr[j]; var formattedAttribute = string.Empty; if (!attribute.ShouldHaveValues()) { //no values if (attribute.AttributeControlType == AttributeControlType.MultilineTextbox) { //multiline textbox var attributeName = await _localizationService.GetLocalizedAsync(attribute, a => a.Name, currentLanguage.Id); //encode (if required) if (htmlEncode) { attributeName = WebUtility.HtmlEncode(attributeName); } formattedAttribute = $"{attributeName}: {_htmlFormatter.FormatText(valueStr, false, true, false, false, false, false)}"; //we never encode multiline textbox input } else if (attribute.AttributeControlType == AttributeControlType.FileUpload) { //file upload //not supported for vendor attributes } else { //other attributes (textbox, datepicker) formattedAttribute = $"{await _localizationService.GetLocalizedAsync(attribute, a => a.Name, currentLanguage.Id)}: {valueStr}"; //encode (if required) if (htmlEncode) { formattedAttribute = WebUtility.HtmlEncode(formattedAttribute); } } } else { if (int.TryParse(valueStr, out var attributeValueId)) { var attributeValue = await _vendorAttributeService.GetVendorAttributeValueByIdAsync(attributeValueId); if (attributeValue != null) { formattedAttribute = $"{await _localizationService.GetLocalizedAsync(attribute, a => a.Name, currentLanguage.Id)}: {await _localizationService.GetLocalizedAsync(attributeValue, a => a.Name, currentLanguage.Id)}"; } //encode (if required) if (htmlEncode) { formattedAttribute = WebUtility.HtmlEncode(formattedAttribute); } } } if (string.IsNullOrEmpty(formattedAttribute)) { continue; } if (i != 0 || j != 0) { result.Append(separator); } result.Append(formattedAttribute); } } return(result.ToString()); }
/// <summary> /// Prepare vendor attribute models /// </summary> /// <param name="vendorAttributesXml">Vendor attributes in XML format</param> /// <returns>List of the vendor attribute model</returns> protected virtual IList <VendorAttributeModel> PrepareVendorAttributes(string vendorAttributesXml) { var result = new List <VendorAttributeModel>(); var vendorAttributes = _vendorAttributeService.GetAllVendorAttributes(); foreach (var attribute in vendorAttributes) { var attributeModel = new VendorAttributeModel { Id = attribute.Id, Name = _localizationService.GetLocalized(attribute, x => x.Name), IsRequired = attribute.IsRequired, AttributeControlType = attribute.AttributeControlType, }; if (attribute.ShouldHaveValues()) { //values var attributeValues = _vendorAttributeService.GetVendorAttributeValues(attribute.Id); foreach (var attributeValue in attributeValues) { var valueModel = new VendorAttributeValueModel { Id = attributeValue.Id, Name = _localizationService.GetLocalized(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 = _vendorAttributeParser.ParseVendorAttributeValues(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> protected virtual void PrepareVendorAttributeModels(IList <VendorModel.VendorAttributeModel> models, Vendor vendor) { if (models == null) { throw new ArgumentNullException(nameof(models)); } //get available vendor attributes var vendorAttributes = _vendorAttributeService.GetAllVendorAttributes(); 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 = _vendorAttributeService.GetVendorAttributeValues(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 = _genericAttributeService.GetAttribute <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 = _vendorAttributeParser.ParseVendorAttributeValues(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); } }