Пример #1
0
        private async Task PrepareNewsletter(CustomerInfoModel model, GetInfo request)
        {
            //newsletter
            var newsletter = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreId(request.Customer.Email, request.Store.Id);

            if (newsletter == null)
            {
                newsletter = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByCustomerId(request.Customer.Id);
            }

            model.Newsletter = newsletter != null && newsletter.Active;

            var categories = (await _newsletterCategoryService.GetAllNewsletterCategory()).ToList();

            categories.ForEach(x => model.NewsletterCategories.Add(new NewsletterSimpleCategory()
            {
                Id          = x.Id,
                Description = x.GetLocalized(y => y.Description, request.Language.Id),
                Name        = x.GetLocalized(y => y.Name, request.Language.Id),
                Selected    = newsletter == null ? false : newsletter.Categories.Contains(x.Id),
            }));
        }
Пример #2
0
        public virtual async Task <CustomerInfoModel> PrepareInfoModel(CustomerInfoModel model, Customer customer,
                                                                       bool excludeProperties, string overrideCustomCustomerAttributesXml = "")
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }

            model.AllowCustomersToSetTimeZone = _dateTimeSettings.AllowCustomersToSetTimeZone;
            if (model.AllowCustomersToSetTimeZone)
            {
                foreach (var tzi in _dateTimeHelper.GetSystemTimeZones())
                {
                    model.AvailableTimeZones.Add(new SelectListItem {
                        Text = tzi.DisplayName, Value = tzi.Id, Selected = (excludeProperties ? tzi.Id == model.TimeZoneId : tzi.Id == _dateTimeHelper.CurrentTimeZone.Id)
                    });
                }
            }

            if (!excludeProperties)
            {
                model.VatNumber = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.VatNumber);

                model.FirstName = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.FirstName);

                model.LastName = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.LastName);

                model.Gender = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.Gender);

                var dateOfBirth = await customer.GetAttribute <DateTime?>(_genericAttributeService, SystemCustomerAttributeNames.DateOfBirth);

                if (dateOfBirth.HasValue)
                {
                    model.DateOfBirthDay   = dateOfBirth.Value.Day;
                    model.DateOfBirthMonth = dateOfBirth.Value.Month;
                    model.DateOfBirthYear  = dateOfBirth.Value.Year;
                }
                model.Company = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.Company);

                model.StreetAddress = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.StreetAddress);

                model.StreetAddress2 = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.StreetAddress2);

                model.ZipPostalCode = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.ZipPostalCode);

                model.City = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.City);

                model.CountryId = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.CountryId);

                model.StateProvinceId = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.StateProvinceId);

                model.Phone = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.Phone);

                model.Fax = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.Fax);

                //newsletter
                var newsletter = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreId(customer.Email, _storeContext.CurrentStore.Id);

                if (newsletter == null)
                {
                    newsletter = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByCustomerId(customer.Id);
                }

                model.Newsletter = newsletter != null && newsletter.Active;

                var categories = (await _newsletterCategoryService.GetAllNewsletterCategory()).ToList();
                categories.ForEach(x => model.NewsletterCategories.Add(new NewsletterSimpleCategory()
                {
                    Id          = x.Id,
                    Description = x.GetLocalized(y => y.Description, _workContext.WorkingLanguage.Id),
                    Name        = x.GetLocalized(y => y.Name, _workContext.WorkingLanguage.Id),
                    Selected    = newsletter == null ? false : newsletter.Categories.Contains(x.Id),
                }));

                model.Signature = await customer.GetAttribute <string>(_genericAttributeService, SystemCustomerAttributeNames.Signature);

                model.Email    = customer.Email;
                model.Username = customer.Username;
            }
            else
            {
                if (_customerSettings.UsernamesEnabled && !_customerSettings.AllowUsersToChangeUsernames)
                {
                    model.Username = customer.Username;
                }
            }

            //countries and states
            if (_customerSettings.CountryEnabled)
            {
                model.AvailableCountries.Add(new SelectListItem {
                    Text = _localizationService.GetResource("Address.SelectCountry"), Value = ""
                });
                foreach (var c in await _countryService.GetAllCountries(_workContext.WorkingLanguage.Id))
                {
                    model.AvailableCountries.Add(new SelectListItem
                    {
                        Text     = c.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id),
                        Value    = c.Id.ToString(),
                        Selected = c.Id == model.CountryId
                    });
                }

                if (_customerSettings.StateProvinceEnabled)
                {
                    //states
                    var states = await _stateProvinceService.GetStateProvincesByCountryId(model.CountryId, _workContext.WorkingLanguage.Id);

                    if (states.Any())
                    {
                        model.AvailableStates.Add(new SelectListItem {
                            Text = _localizationService.GetResource("Address.SelectState"), Value = ""
                        });

                        foreach (var s in states)
                        {
                            model.AvailableStates.Add(new SelectListItem {
                                Text = s.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id), Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId)
                            });
                        }
                    }
                    else
                    {
                        bool anyCountrySelected = model.AvailableCountries.Any(x => x.Selected);

                        model.AvailableStates.Add(new SelectListItem
                        {
                            Text  = _localizationService.GetResource(anyCountrySelected ? "Address.OtherNonUS" : "Address.SelectState"),
                            Value = ""
                        });
                    }
                }
            }
            model.DisplayVatNumber    = _taxSettings.EuVatEnabled;
            model.VatNumberStatusNote = ((VatNumberStatus)await customer.GetAttribute <int>(_genericAttributeService, SystemCustomerAttributeNames.VatNumberStatusId))
                                        .GetLocalizedEnum(_localizationService, _workContext);
            model.GenderEnabled                    = _customerSettings.GenderEnabled;
            model.DateOfBirthEnabled               = _customerSettings.DateOfBirthEnabled;
            model.DateOfBirthRequired              = _customerSettings.DateOfBirthRequired;
            model.CompanyEnabled                   = _customerSettings.CompanyEnabled;
            model.CompanyRequired                  = _customerSettings.CompanyRequired;
            model.StreetAddressEnabled             = _customerSettings.StreetAddressEnabled;
            model.StreetAddressRequired            = _customerSettings.StreetAddressRequired;
            model.StreetAddress2Enabled            = _customerSettings.StreetAddress2Enabled;
            model.StreetAddress2Required           = _customerSettings.StreetAddress2Required;
            model.ZipPostalCodeEnabled             = _customerSettings.ZipPostalCodeEnabled;
            model.ZipPostalCodeRequired            = _customerSettings.ZipPostalCodeRequired;
            model.CityEnabled                      = _customerSettings.CityEnabled;
            model.CityRequired                     = _customerSettings.CityRequired;
            model.CountryEnabled                   = _customerSettings.CountryEnabled;
            model.CountryRequired                  = _customerSettings.CountryRequired;
            model.StateProvinceEnabled             = _customerSettings.StateProvinceEnabled;
            model.StateProvinceRequired            = _customerSettings.StateProvinceRequired;
            model.PhoneEnabled                     = _customerSettings.PhoneEnabled;
            model.PhoneRequired                    = _customerSettings.PhoneRequired;
            model.FaxEnabled                       = _customerSettings.FaxEnabled;
            model.FaxRequired                      = _customerSettings.FaxRequired;
            model.NewsletterEnabled                = _customerSettings.NewsletterEnabled;
            model.UsernamesEnabled                 = _customerSettings.UsernamesEnabled;
            model.AllowUsersToChangeUsernames      = _customerSettings.AllowUsersToChangeUsernames;
            model.CheckUsernameAvailabilityEnabled = _customerSettings.CheckUsernameAvailabilityEnabled;
            model.SignatureEnabled                 = _forumSettings.ForumsEnabled && _forumSettings.SignaturesEnabled;

            //external authentication
            model.NumberOfExternalAuthenticationProviders = _externalAuthenticationService
                                                            .LoadActiveExternalAuthenticationMethods(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id).Count;
            foreach (var ear in await _externalAuthenticationService.GetExternalIdentifiersFor(customer))
            {
                var authMethod = _externalAuthenticationService.LoadExternalAuthenticationMethodBySystemName(ear.ProviderSystemName);
                if (authMethod == null || !authMethod.IsMethodActive(_externalAuthenticationSettings))
                {
                    continue;
                }

                model.AssociatedExternalAuthRecords.Add(new CustomerInfoModel.AssociatedExternalAuthModel
                {
                    Id    = ear.Id,
                    Email = ear.Email,
                    ExternalIdentifier = ear.ExternalDisplayIdentifier,
                    AuthMethodName     = authMethod.GetLocalizedFriendlyName(_localizationService, _workContext.WorkingLanguage.Id)
                });
            }

            //custom customer attributes
            var customAttributes = await PrepareCustomAttributes(customer, overrideCustomCustomerAttributesXml);

            foreach (var attribute in customAttributes)
            {
                model.CustomerAttributes.Add(attribute);
            }

            return(model);
        }