/// <summary> /// Gets a value indicating whether address is valid (can be saved) /// </summary> /// <param name="address">Address to validate</param> /// <returns>Result</returns> public virtual bool IsAddressValid(Address address) { if (address == null) { throw new ArgumentNullException("address"); } if (String.IsNullOrWhiteSpace(address.FirstName)) { return(false); } if (String.IsNullOrWhiteSpace(address.LastName)) { return(false); } if (String.IsNullOrWhiteSpace(address.Email)) { return(false); } if (AddressSettings.CompanyEnabled && AddressSettings.CompanyRequired && String.IsNullOrWhiteSpace(address.Company)) { return(false); } if (AddressSettings.StreetAddressEnabled && AddressSettings.StreetAddressRequired && String.IsNullOrWhiteSpace(address.Address1)) { return(false); } if (AddressSettings.StreetAddress2Enabled && AddressSettings.StreetAddress2Required && String.IsNullOrWhiteSpace(address.Address2)) { return(false); } if (AddressSettings.ZipPostalCodeEnabled && AddressSettings.ZipPostalCodeRequired && String.IsNullOrWhiteSpace(address.ZipPostalCode)) { return(false); } if (AddressSettings.CountryEnabled) { if (address.CountryId == null || address.CountryId.Value == 0) { return(false); } var country = _countryService.GetCountryById(address.CountryId.Value); if (country == null) { return(false); } if (AddressSettings.StateProvinceEnabled) { var states = _stateProvinceService.GetStateProvincesByCountryId(country.Id); if (states.Count > 0) { if (address.StateProvinceId == null || address.StateProvinceId.Value == 0) { return(false); } var state = states.FirstOrDefault(x => x.Id == address.StateProvinceId.Value); if (state == null) { return(false); } } } } if (AddressSettings.CityEnabled && AddressSettings.CityRequired && String.IsNullOrWhiteSpace(address.City)) { return(false); } if (AddressSettings.PhoneEnabled && AddressSettings.PhoneRequired && String.IsNullOrWhiteSpace(address.PhoneNumber)) { return(false); } if (AddressSettings.FaxEnabled && AddressSettings.FaxRequired && String.IsNullOrWhiteSpace(address.FaxNumber)) { return(false); } var attributes = _addressAttributeService.GetAllAddressAttributes(); if (attributes.Any(x => x.IsRequired)) { return(false); } return(true); }
//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); } }