/// <summary> /// Prepare paged user attribute list model /// </summary> /// <param name="searchModel">User attribute search model</param> /// <returns>User attribute list model</returns> public virtual UserAttributeListModel PrepareUserAttributeListModel(UserAttributeSearchModel searchModel) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } //get user attributes var userAttributes = _userAttributeService.GetAllUserAttributes(); //prepare list model var model = new UserAttributeListModel { Data = userAttributes.PaginationByRequestModel(searchModel).Select(attribute => { //fill in model values from the entity var attributeModel = attribute.ToModel <UserAttributeModel>(); return(attributeModel); }), Total = userAttributes.Count }; return(model); }
protected virtual string ParseCustomUserAttributes(IFormCollection form) { if (form == null) { throw new ArgumentNullException(nameof(form)); } var attributesXml = ""; var attributes = _userAttributeService.GetAllUserAttributes(); foreach (var attribute in attributes) { var controlId = $"user_attribute_{attribute.Id}"; } return(attributesXml); }
/// <summary> /// Prepare the custom user attribute models /// </summary> /// <param name="user">User</param> /// <param name="overrideAttributesXml">Overridden user attributes in XML format; pass null to use CustomUserAttributes of user</param> /// <returns>List of the user attribute model</returns> public virtual IList <UserAttributeModel> PrepareCustomUserAttributes(User user, string overrideAttributesXml = "") { if (user == null) { throw new ArgumentNullException(nameof(user)); } var result = new List <UserAttributeModel>(); var userAttributes = _userAttributeService.GetAllUserAttributes(); foreach (var attribute in userAttributes) { var attributeModel = new UserAttributeModel { Id = attribute.Id, Name = _localizationService.GetLocalized(attribute, x => x.Name), IsRequired = attribute.IsRequired, }; if (attribute.ShouldHaveValues()) { //values var attributeValues = _userAttributeService.GetUserAttributeValues(attribute.Id); foreach (var attributeValue in attributeValues) { var valueModel = new UserAttributeValueModel { Id = attributeValue.Id, Name = _localizationService.GetLocalized(attributeValue, x => x.Name), IsPreSelected = attributeValue.IsPreSelected }; attributeModel.Values.Add(valueModel); } } //set already selected attributes var selectedAttributesXml = !string.IsNullOrEmpty(overrideAttributesXml) ? overrideAttributesXml : _genericAttributeService.GetAttribute <string>(user, NopUserDefaults.CustomUserAttributes); result.Add(attributeModel); } return(result); }
/// <summary> /// Prepare user attribute models /// </summary> /// <param name="models">List of user attribute models</param> /// <param name="user">User</param> protected virtual void PrepareUserAttributeModels(IList <UserModel.UserAttributeModel> models, User user) { if (models == null) { throw new ArgumentNullException(nameof(models)); } //get available user attributes var userAttributes = _userAttributeService.GetAllUserAttributes(); foreach (var attribute in userAttributes) { var attributeModel = new UserModel.UserAttributeModel { Id = attribute.Id, Name = attribute.Name, IsRequired = attribute.IsRequired, }; if (attribute.ShouldHaveValues()) { //values var attributeValues = _userAttributeService.GetUserAttributeValues(attribute.Id); foreach (var attributeValue in attributeValues) { var attributeValueModel = new UserModel.UserAttributeValueModel { Id = attributeValue.Id, Name = attributeValue.Name, IsPreSelected = attributeValue.IsPreSelected }; attributeModel.Values.Add(attributeValueModel); } } //set already selected attributes if (user != null) { var selectedUserAttributes = _genericAttributeService .GetAttribute <string>(user, NopUserDefaults.CustomUserAttributes); } models.Add(attributeModel); } }
/// <summary> /// Validates user attributes /// </summary> /// <param name="attributesXml">Attributes in XML format</param> /// <returns>Warnings</returns> public virtual IList <string> GetAttributeWarnings(string attributesXml) { var warnings = new List <string>(); //ensure it's our attributes var attributes1 = ParseUserAttributes(attributesXml); //validate required user attributes (whether they're chosen/selected/entered) var attributes2 = _userAttributeService.GetAllUserAttributes(); foreach (var a2 in attributes2) { if (a2.IsRequired) { bool found = false; //selected user attributes foreach (var a1 in attributes1) { if (a1.Id == a2.Id) { var valuesStr = ParseValues(attributesXml, a1.Id); foreach (string str1 in valuesStr) { if (!String.IsNullOrEmpty(str1.Trim())) { found = true; break; } } } } //if not found if (!found) { var notFoundWarning = string.Format(_localizationService.GetResource("ShoppingCart.SelectAttribute"), a2.GetLocalized(a => a.Name)); warnings.Add(notFoundWarning); } } } return(warnings); }
/// <summary> /// Prepare the custom customer attribute models /// </summary> /// <param name="customer">Customer</param> /// <param name="overrideAttributesXml">Overridden customer attributes in XML format; pass null to use CustomCustomerAttributes of customer</param> /// <returns>List of the customer attribute model</returns> public virtual IList <UserAttributeModel> PrepareCustomCustomerAttributes(User customer, string overrideAttributesXml = "") { if (customer == null) { throw new ArgumentNullException("customer"); } var result = new List <UserAttributeModel>(); var customerAttributes = _customerAttributeService.GetAllUserAttributes(); foreach (var attribute in customerAttributes) { var attributeModel = new UserAttributeModel { Id = attribute.Id, Name = attribute.GetLocalized(x => x.Name), IsRequired = attribute.IsRequired, AttributeControlType = attribute.AttributeControlType, }; if (attribute.ShouldHaveValues()) { //values var attributeValues = _customerAttributeService.GetUserAttributeValues(attribute.Id); foreach (var attributeValue in attributeValues) { var valueModel = new CustomerAttributeValueModel { Id = attributeValue.Id, Name = attributeValue.GetLocalized(x => x.Name), IsPreSelected = attributeValue.IsPreSelected }; attributeModel.Values.Add(valueModel); } } //set already selected attributes var selectedAttributesXml = !String.IsNullOrEmpty(overrideAttributesXml) ? overrideAttributesXml : customer.GetAttribute <string>(SystemUserAttributeNames.CustomUserAttributes, _genericAttributeService); switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: case AttributeControlType.Checkboxes: { if (!String.IsNullOrEmpty(selectedAttributesXml)) { //clear default selection foreach (var item in attributeModel.Values) { item.IsPreSelected = false; } //select new values var selectedValues = _customerAttributeParser.ParseUserAttributeValues(selectedAttributesXml); 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(selectedAttributesXml)) { var enteredText = _customerAttributeParser.ParseValues(selectedAttributesXml, 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); }
protected virtual string ParseCustomUserAttributes(FormCollection form) { if (form == null) { throw new ArgumentNullException("form"); } string attributesXml = ""; var attributes = _userAttributeService.GetAllUserAttributes(); foreach (var attribute in attributes) { string controlId = string.Format("customer_attribute_{0}", attribute.Id); switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: { var ctrlAttributes = form[controlId]; if (!String.IsNullOrEmpty(ctrlAttributes)) { int selectedAttributeId = int.Parse(ctrlAttributes); if (selectedAttributeId > 0) { attributesXml = _userAttributeParser.AddUserAttribute(attributesXml, attribute, selectedAttributeId.ToString()); } } } break; case AttributeControlType.Checkboxes: { var cblAttributes = form[controlId]; if (!String.IsNullOrEmpty(cblAttributes)) { foreach (var item in cblAttributes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) ) { int selectedAttributeId = int.Parse(item); if (selectedAttributeId > 0) { attributesXml = _userAttributeParser.AddUserAttribute(attributesXml, attribute, selectedAttributeId.ToString()); } } } } break; case AttributeControlType.ReadonlyCheckboxes: { //load read-only (already server-side selected) values var attributeValues = _userAttributeService.GetUserAttributeValues(attribute.Id); foreach (var selectedAttributeId in attributeValues .Where(v => v.IsPreSelected) .Select(v => v.Id) .ToList()) { attributesXml = _userAttributeParser.AddUserAttribute(attributesXml, attribute, selectedAttributeId.ToString()); } } break; case AttributeControlType.TextBox: case AttributeControlType.MultilineTextbox: { var ctrlAttributes = form[controlId]; if (!String.IsNullOrEmpty(ctrlAttributes)) { string enteredText = ctrlAttributes.Trim(); attributesXml = _userAttributeParser.AddUserAttribute(attributesXml, attribute, enteredText); } } break; case AttributeControlType.Datepicker: case AttributeControlType.ColorSquares: case AttributeControlType.ImageSquares: case AttributeControlType.FileUpload: //not supported customer attributes default: break; } } return(attributesXml); }