/// <summary> /// Prepare address model /// </summary> /// <param name="model">Address model</param> /// <param name="address">Address</param> /// <returns>A task that represents the asynchronous operation</returns> public virtual async Task PrepareAddressModelAsync(AddressModel model, Address address = null) { if (model == null) { throw new ArgumentNullException(nameof(model)); } //prepare available countries await _baseAdminModelFactory.PrepareCountriesAsync(model.AvailableCountries); //prepare available states await _baseAdminModelFactory.PrepareStatesAndProvincesAsync(model.AvailableStates, model.CountryId); //prepare custom address attributes await _addressAttributeModelFactory.PrepareCustomAddressAttributesAsync(model.CustomAddressAttributes, address); if (address == null) { return; } model.FormattedCustomAddressAttributes = await _addressAttributeFormatter.FormatAttributesAsync(address.CustomAttributes); }
protected virtual async Task PrepareModelAddressHtmlAsync(AddressModel model, Address address) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var addressHtmlSb = new StringBuilder("<div>"); if (_addressSettings.CompanyEnabled && !string.IsNullOrEmpty(model.Company)) { addressHtmlSb.AppendFormat("{0}<br />", WebUtility.HtmlEncode(model.Company)); } if (_addressSettings.StreetAddressEnabled && !string.IsNullOrEmpty(model.Address1)) { addressHtmlSb.AppendFormat("{0}<br />", WebUtility.HtmlEncode(model.Address1)); } if (_addressSettings.StreetAddress2Enabled && !string.IsNullOrEmpty(model.Address2)) { addressHtmlSb.AppendFormat("{0}<br />", WebUtility.HtmlEncode(model.Address2)); } if (_addressSettings.CityEnabled && !string.IsNullOrEmpty(model.City)) { addressHtmlSb.AppendFormat("{0},", WebUtility.HtmlEncode(model.City)); } if (_addressSettings.CountyEnabled && !string.IsNullOrEmpty(model.County)) { addressHtmlSb.AppendFormat("{0},", WebUtility.HtmlEncode(model.County)); } if (_addressSettings.StateProvinceEnabled && !string.IsNullOrEmpty(model.StateProvinceName)) { addressHtmlSb.AppendFormat("{0},", WebUtility.HtmlEncode(model.StateProvinceName)); } if (_addressSettings.ZipPostalCodeEnabled && !string.IsNullOrEmpty(model.ZipPostalCode)) { addressHtmlSb.AppendFormat("{0}<br />", WebUtility.HtmlEncode(model.ZipPostalCode)); } if (_addressSettings.CountryEnabled && !string.IsNullOrEmpty(model.CountryName)) { addressHtmlSb.AppendFormat("{0}", WebUtility.HtmlEncode(model.CountryName)); } var customAttributesFormatted = await _addressAttributeFormatter.FormatAttributesAsync(address?.CustomAttributes); if (!string.IsNullOrEmpty(customAttributesFormatted)) { //already encoded addressHtmlSb.AppendFormat("<br />{0}", customAttributesFormatted); } addressHtmlSb.Append("</div>"); model.AddressHtml = addressHtmlSb.ToString(); }
/// <summary> /// Prepare address model /// </summary> /// <param name="model">Address model</param> /// <param name="address">Address entity</param> /// <param name="excludeProperties">Whether to exclude populating of model properties from the entity</param> /// <param name="addressSettings">Address settings</param> /// <param name="loadCountries">Countries loading function; pass null if countries do not need to load</param> /// <param name="prePopulateWithCustomerFields">Whether to populate model properties with the customer fields (used with the customer entity)</param> /// <param name="customer">Customer entity; required if prePopulateWithCustomerFields is true</param> /// <param name="overrideAttributesXml">Overridden address attributes in XML format; pass null to use CustomAttributes of the address entity</param> /// <returns>A task that represents the asynchronous operation</returns> public virtual async Task PrepareAddressModelAsync(AddressModel model, Address address, bool excludeProperties, AddressSettings addressSettings, Func <Task <IList <Country> > > loadCountries = null, bool prePopulateWithCustomerFields = false, Customer customer = null, string overrideAttributesXml = "") { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (addressSettings == null) { throw new ArgumentNullException(nameof(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 = await _countryService.GetCountryByAddressAsync(address) is Country country ? await _localizationService.GetLocalizedAsync(country, x => x.Name) : null; model.StateProvinceId = address.StateProvinceId; model.StateProvinceName = await _stateProvinceService.GetStateProvinceByAddressAsync(address) is StateProvince stateProvince ? await _localizationService.GetLocalizedAsync(stateProvince, x => x.Name) : null; model.County = address.County; 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.Email; model.FirstName = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.FirstNameAttribute); model.LastName = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.LastNameAttribute); model.Company = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.CompanyAttribute); model.Address1 = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.StreetAddressAttribute); model.Address2 = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.StreetAddress2Attribute); model.ZipPostalCode = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.ZipPostalCodeAttribute); model.City = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.CityAttribute); model.County = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.CountyAttribute); model.PhoneNumber = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.PhoneAttribute); model.FaxNumber = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.FaxAttribute); } //countries and states if (addressSettings.CountryEnabled && loadCountries != null) { var countries = await loadCountries(); if (_addressSettings.PreselectCountryIfOnlyOne && countries.Count == 1) { model.CountryId = countries[0].Id; } else { model.AvailableCountries.Add(new SelectListItem { Text = await _localizationService.GetResourceAsync("Address.SelectCountry"), Value = "0" }); } foreach (var c in countries) { model.AvailableCountries.Add(new SelectListItem { Text = await _localizationService.GetLocalizedAsync(c, x => x.Name), Value = c.Id.ToString(), Selected = c.Id == model.CountryId }); } if (addressSettings.StateProvinceEnabled) { var languageId = (await _workContext.GetWorkingLanguageAsync()).Id; var states = (await _stateProvinceService .GetStateProvincesByCountryIdAsync(model.CountryId ?? 0, languageId)) .ToList(); if (states.Any()) { model.AvailableStates.Add(new SelectListItem { Text = await _localizationService.GetResourceAsync("Address.SelectState"), Value = "0" }); foreach (var s in states) { model.AvailableStates.Add(new SelectListItem { Text = await _localizationService.GetLocalizedAsync(s, x => x.Name), Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId) }); } } else { var anyCountrySelected = model.AvailableCountries.Any(x => x.Selected); model.AvailableStates.Add(new SelectListItem { Text = await _localizationService.GetResourceAsync(anyCountrySelected ? "Address.Other" : "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.CountyEnabled = addressSettings.CountyEnabled; model.CountyRequired = addressSettings.CountyRequired; 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) { await PrepareCustomAddressAttributesAsync(model, address, overrideAttributesXml); } if (_addressAttributeFormatter != null && address != null) { model.FormattedCustomAddressAttributes = await _addressAttributeFormatter.FormatAttributesAsync(address.CustomAttributes); } }