//address /// <summary> /// Prepare address model /// </summary> /// <param name="model">Model</param> /// <param name="address">Address</param> /// <param name="excludeProperties">A value indicating whether to exclude properties</param> /// <param name="addressSettings">Address settings</param> /// <param name="localizationService">Localization service (used to prepare a select list)</param> /// <param name="stateProvinceService">State service (used to prepare a select list). null to don't prepare the list.</param> /// <param name="addressAttributeService">Address attribute service. null to don't prepare the list.</param> /// <param name="addressAttributeParser">Address attribute parser. null to don't prepare the list.</param> /// <param name="addressAttributeFormatter">Address attribute formatter. null to don't prepare the formatted custom attributes.</param> /// <param name="loadCountries">A function to load countries (used to prepare a select list). null to don't prepare the list.</param> /// <param name="prePopulateWithCustomerFields">A value indicating whether to pre-populate an address with customer fields entered during registration. It's used only when "address" parameter is set to "null"</param> /// <param name="customer">Customer record which will be used to pre-populate address. Used only when "prePopulateWithCustomerFields" is "true".</param> /// <param name="overrideAttributesXml">When specified we do not use attributes of an address; if null, then already saved ones are used</param> public static void PrepareModel(this AddressDto model, Address address, bool excludeProperties, AddressSettings addressSettings, //ILocalizationService localizationService = null, GenericAttributeDomianService genericAttributeDomianService, StateProvinceDomainService stateProvinceService = null, AddressAttributeDomainService addressAttributeService = null, IAddressAttributeParser addressAttributeParser = null, IAddressAttributeFormatter addressAttributeFormatter = null, Func <IList <Country> > loadCountries = null, bool prePopulateWithCustomerFields = false, User customer = null, string overrideAttributesXml = "") { if (model == null) { throw new ArgumentNullException("model"); } if (addressSettings == null) { throw new ArgumentNullException("addressSettings"); } if (!excludeProperties && address != null) { model.Id = address.Id; model.FirstName = address.FirstName; model.LastName = address.LastName; model.Email = address.Email; model.Company = address.Company; model.CountryId = address.CountryId; model.CountryName = address.Country != null ? address.Country.Name : null; model.StateProvinceId = address.StateProvinceId; model.StateProvinceName = address.StateProvince != null ? address.StateProvince.Name : null; model.City = address.City; model.Address1 = address.Address1; model.Address2 = address.Address2; model.ZipPostalCode = address.ZipPostalCode; model.PhoneNumber = address.PhoneNumber; model.FaxNumber = address.FaxNumber; } if (address == null && prePopulateWithCustomerFields) { if (customer == null) { throw new Exception("Customer cannot be null when prepopulating an address"); } model.Email = customer.EmailAddress; model.FirstName = customer.GetAttribute <string>(SystemCustomerAttributeNames.FirstName, genericAttributeDomianService); model.LastName = customer.GetAttribute <string>(SystemCustomerAttributeNames.LastName, genericAttributeDomianService); model.Company = customer.GetAttribute <string>(SystemCustomerAttributeNames.Company, genericAttributeDomianService); model.Address1 = customer.GetAttribute <string>(SystemCustomerAttributeNames.StreetAddress, genericAttributeDomianService); model.Address2 = customer.GetAttribute <string>(SystemCustomerAttributeNames.StreetAddress2, genericAttributeDomianService); model.ZipPostalCode = customer.GetAttribute <string>(SystemCustomerAttributeNames.ZipPostalCode, genericAttributeDomianService); model.City = customer.GetAttribute <string>(SystemCustomerAttributeNames.City, genericAttributeDomianService); //ignore country and state for prepopulation. it can cause some issues when posting pack with errors, etc //model.CountryId = customer.GetAttribute<int>(SystemCustomerAttributeNames.CountryId); //model.StateProvinceId = customer.GetAttribute<int>(SystemCustomerAttributeNames.StateProvinceId); model.PhoneNumber = customer.GetAttribute <string>(SystemCustomerAttributeNames.Phone, genericAttributeDomianService); model.FaxNumber = customer.GetAttribute <string>(SystemCustomerAttributeNames.Fax, genericAttributeDomianService); } //countries and states if (addressSettings.CountryEnabled && loadCountries != null) { //if (localizationService == null) // throw new ArgumentNullException("localizationService"); model.AvailableCountries.Add(new SelectListItemDto { Text = InfoMsg.Address_SelectCountry, Value = "0" }); foreach (var c in loadCountries()) { model.AvailableCountries.Add(new SelectListItemDto { Text = c.Name, Value = c.Id.ToString(), Selected = c.Id == model.CountryId }); } if (addressSettings.StateProvinceEnabled) { //states if (stateProvinceService == null) { throw new ArgumentNullException("stateProvinceService"); } //var languageId = EngineContext.Current.Resolve<IWorkContext>().WorkingLanguage.Id; var states = stateProvinceService .GetStateProvincesByCountryId(model.CountryId.HasValue ? model.CountryId.Value : 0) .ToList(); if (states.Count > 0) { model.AvailableStates.Add(new SelectListItemDto { Text = InfoMsg.Address_SelectState, Value = "0" }); foreach (var s in states) { model.AvailableStates.Add(new SelectListItemDto { Text = s.Name, Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId) }); } } else { bool anyCountrySelected = model.AvailableCountries.Any(x => x.Selected); model.AvailableStates.Add(new SelectListItemDto { Text = anyCountrySelected ? InfoMsg.Address_OtherNonUS : InfoMsg.Address_SelectState, Value = "0" }); } } } //form fields model.CompanyEnabled = addressSettings.CompanyEnabled; model.CompanyRequired = addressSettings.CompanyRequired; model.StreetAddressEnabled = addressSettings.StreetAddressEnabled; model.StreetAddressRequired = addressSettings.StreetAddressRequired; model.StreetAddress2Enabled = addressSettings.StreetAddress2Enabled; model.StreetAddress2Required = addressSettings.StreetAddress2Required; model.ZipPostalCodeEnabled = addressSettings.ZipPostalCodeEnabled; model.ZipPostalCodeRequired = addressSettings.ZipPostalCodeRequired; model.CityEnabled = addressSettings.CityEnabled; model.CityRequired = addressSettings.CityRequired; model.CountryEnabled = addressSettings.CountryEnabled; model.StateProvinceEnabled = addressSettings.StateProvinceEnabled; model.PhoneEnabled = addressSettings.PhoneEnabled; model.PhoneRequired = addressSettings.PhoneRequired; model.FaxEnabled = addressSettings.FaxEnabled; model.FaxRequired = addressSettings.FaxRequired; //customer attribute services if (addressAttributeService != null && addressAttributeParser != null) { PrepareCustomAddressAttributes(model, address, addressAttributeService, addressAttributeParser, overrideAttributesXml); } if (addressAttributeFormatter != null && address != null) { model.FormattedCustomAddressAttributes = addressAttributeFormatter.FormatAttributes(address.CustomAttributes); } }
private static void PrepareCustomAddressAttributes(this AddressDto model, Address address, AddressAttributeDomainService addressAttributeService, IAddressAttributeParser addressAttributeParser, string overrideAttributesXml = "") { if (addressAttributeService == null) { throw new ArgumentNullException("addressAttributeService"); } if (addressAttributeParser == null) { throw new ArgumentNullException("addressAttributeParser"); } var attributes = addressAttributeService.GetAllAddressAttributes(); foreach (var attribute in attributes) { var attributeModel = new AddressAttributeDto { Id = attribute.Id, Name = attribute.Name, IsRequired = attribute.IsRequired, AttributeControlType = attribute.AttributeControlType, }; if (attribute.ShouldHaveValues()) { //values var attributeValues = addressAttributeService.GetAddressAttributeValues(attribute.Id); foreach (var attributeValue in attributeValues) { var attributeValueModel = new AddressAttributeValueDto { Id = attributeValue.Id, Name = attributeValue.Name, IsPreSelected = attributeValue.IsPreSelected }; attributeModel.Values.Add(attributeValueModel); } } //set already selected attributes var selectedAddressAttributes = !String.IsNullOrEmpty(overrideAttributesXml) ? overrideAttributesXml : (address != null ? address.CustomAttributes : null); switch (attribute.AttributeControlType) { case AttributeControlType.DropdownList: case AttributeControlType.RadioList: case AttributeControlType.Checkboxes: { if (!String.IsNullOrEmpty(selectedAddressAttributes)) { //clear default selection foreach (var item in attributeModel.Values) { item.IsPreSelected = false; } //select new values var selectedValues = addressAttributeParser.ParseAddressAttributeValues(selectedAddressAttributes); 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(selectedAddressAttributes)) { var enteredText = addressAttributeParser.ParseValues(selectedAddressAttributes, attribute.Id); if (enteredText.Count > 0) { attributeModel.DefaultValue = enteredText[0]; } } } break; case AttributeControlType.ColorSquares: case AttributeControlType.Datepicker: case AttributeControlType.FileUpload: default: //not supported attribute control types break; } model.CustomAddressAttributes.Add(attributeModel); } }