Example #1
0
        public async Task <IActionResult> CustomerUser(
            int storeScope,
            CustomerSettings customerSettings,
            AddressSettings addressSettings,
            PrivacySettings privacySettings)
        {
            var model = new CustomerUserSettingsModel();

            await MapperFactory.MapAsync(customerSettings, model.CustomerSettings);

            await MapperFactory.MapAsync(addressSettings, model.AddressSettings);

            await MapperFactory.MapAsync(privacySettings, model.PrivacySettings);

            AddLocales(model.Locales, (locale, languageId) =>
            {
                locale.Salutations = addressSettings.GetLocalizedSetting(x => x.Salutations, languageId, storeScope, false, false);
            });

            return(View(model));
        }
Example #2
0
        /// <summary>
        /// Maps an address entity to an address model.
        /// </summary>
        /// <param name="from"><see cref="Address"/></param>
        /// <param name="to"><see cref="AddressModel"/></param>
        /// <param name="parameters">Expects excludeProperties of type <see cref="bool"/> and countries of type <see cref="List<Country>"/>. Both properties can also be ommited.</param>
        public override async Task MapAsync(Address from, AddressModel to, dynamic parameters = null)
        {
            var excludeProperties = parameters?.ExcludeProperties == true;
            var countries         = parameters?.Countries as IEnumerable <Country>;

            // Form fields
            MiniMapper.Map(_addressSettings, to);

            if (!excludeProperties && from != null)
            {
                MiniMapper.Map(from, to);

                to.EmailMatch  = from.Email;
                to.CountryName = from.Country?.GetLocalized(x => x.Name);
                if (from.StateProvinceId.HasValue && from.StateProvince != null)
                {
                    to.StateProvinceName = from.StateProvince.GetLocalized(x => x.Name);
                }

                to.FormattedAddress = await _addressService.FormatAddressAsync(from, true);
            }

            // Countries and states
            if (_addressSettings.CountryEnabled && countries != null && countries.Any())
            {
                to.AvailableCountries.Add(new SelectListItem {
                    Text = _services.Localization.GetResource("Address.SelectCountry"), Value = "0"
                });
                foreach (var c in countries)
                {
                    to.AvailableCountries.Add(new SelectListItem
                    {
                        Text     = c.GetLocalized(x => x.Name),
                        Value    = c.Id.ToString(),
                        Selected = c.Id == to.CountryId
                    });
                }

                if (_addressSettings.StateProvinceEnabled)
                {
                    var states = await _db.StateProvinces
                                 .AsNoTracking()
                                 .Where(x => x.CountryId == (to.CountryId ?? 0))
                                 .ToListAsync();

                    if (states.Any())
                    {
                        foreach (var s in states)
                        {
                            to.AvailableStates.Add(new SelectListItem
                            {
                                Text     = s.GetLocalized(x => x.Name),
                                Value    = s.Id.ToString(),
                                Selected = (s.Id == to.StateProvinceId)
                            });
                        }
                    }
                    else
                    {
                        to.AvailableStates.Add(new SelectListItem
                        {
                            Text  = _services.Localization.GetResource("Address.OtherNonUS"),
                            Value = "0"
                        });
                    }
                }
            }

            string salutations = _addressSettings.GetLocalizedSetting(x => x.Salutations);

            foreach (var sal in salutations.SplitSafe(","))
            {
                to.AvailableSalutations.Add(new SelectListItem {
                    Value = sal, Text = sal
                });
            }
        }
        /// <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="loadCountries">A function to load countries  (used to prepare a select list). null to don't prepare the list.</param>
        public static void PrepareModel(this AddressModel model,
                                        Address address,
                                        bool excludeProperties,
                                        AddressSettings addressSettings,
                                        ILocalizationService localizationService   = null,
                                        IStateProvinceService stateProvinceService = null,
                                        Func <IList <Country> > loadCountries      = null)
        {
            Guard.NotNull(model, nameof(model));
            Guard.NotNull(addressSettings, nameof(addressSettings));

            // Form fields
            MiniMapper.Map(addressSettings, model);

            if (!excludeProperties && address != null)
            {
                MiniMapper.Map(address, model);

                model.EmailMatch  = address.Email;
                model.CountryName = address.Country?.GetLocalized(x => x.Name);
                if (address.StateProvinceId.HasValue && address.StateProvince != null)
                {
                    model.StateProvinceName = address.StateProvince.GetLocalized(x => x.Name);
                }
                model.FormattedAddress = Core.Infrastructure.EngineContext.Current.Resolve <IAddressService>().FormatAddress(address, true);
            }

            // Countries and states
            if (addressSettings.CountryEnabled && loadCountries != null)
            {
                if (localizationService == null)
                {
                    throw new ArgumentNullException("localizationService");
                }

                model.AvailableCountries.Add(new SelectListItem {
                    Text = localizationService.GetResource("Address.SelectCountry"), Value = "0"
                });
                foreach (var c in loadCountries())
                {
                    model.AvailableCountries.Add(new SelectListItem
                    {
                        Text     = c.GetLocalized(x => x.Name),
                        Value    = c.Id.ToString(),
                        Selected = c.Id == model.CountryId
                    });
                }

                if (addressSettings.StateProvinceEnabled)
                {
                    // States
                    if (stateProvinceService == null)
                    {
                        throw new ArgumentNullException("stateProvinceService");
                    }

                    var states = stateProvinceService
                                 .GetStateProvincesByCountryId(model.CountryId ?? 0)
                                 .ToList();
                    if (states.Count > 0)
                    {
                        foreach (var s in states)
                        {
                            model.AvailableStates.Add(new SelectListItem
                            {
                                Text     = s.GetLocalized(x => x.Name),
                                Value    = s.Id.ToString(),
                                Selected = (s.Id == model.StateProvinceId)
                            });
                        }
                    }
                    else
                    {
                        model.AvailableStates.Add(new SelectListItem
                        {
                            Text  = localizationService.GetResource("Address.OtherNonUS"),
                            Value = "0"
                        });
                    }
                }
            }

            if (localizationService != null)
            {
                string salutations = addressSettings.GetLocalizedSetting(x => x.Salutations);
                foreach (var sal in salutations.SplitSafe(","))
                {
                    model.AvailableSalutations.Add(new SelectListItem {
                        Value = sal, Text = sal
                    });
                }
            }
        }