Esempio n. 1
0
        public virtual ActionResult SendTestEmail(CampaignModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCampaigns))
            {
                return(AccessDeniedView());
            }

            var campaign = _campaignService.GetCampaignById(model.Id);

            if (campaign == null)
            {
                //No campaign found with the specified id
                return(RedirectToAction("List"));
            }

            model.AllowedTokens = string.Join(", ", _messageTokenProvider.GetListOfCampaignAllowedTokens());
            //stores
            PrepareStoresModel(model);
            //customer roles
            PrepareCustomerRolesModel(model);
            //email accounts
            PrepareEmailAccountsModel(model);

            if (!CommonHelper.IsValidEmail(model.TestEmail))
            {
                ErrorNotification(_localizationService.GetResource("Admin.Common.WrongEmail"), false);
                return(View(model));
            }

            try
            {
                var emailAccount = GetEmailAccount(model.EmailAccountId);
                var subscription = _newsLetterSubscriptionService.GetNewsLetterOrderByEmailAndStoreId(model.TestEmail, _storeContext.CurrentStore.Id);
                if (subscription != null)
                {
                    //there's a subscription. let's use it
                    var subscriptions = new List <NewsLetterSubscription>();
                    subscriptions.Add(subscription);
                    _campaignService.SendCampaign(campaign, emailAccount, subscriptions);
                }
                else
                {
                    //no subscription found
                    _campaignService.SendCampaign(campaign, emailAccount, model.TestEmail);
                }

                SuccessNotification(_localizationService.GetResource("Admin.Promotions.Campaigns.TestEmailSentToCustomers"), false);
                return(View(model));
            }
            catch (Exception exc)
            {
                ErrorNotification(exc, false);
            }

            //If we got this far, something failed, redisplay form
            return(View(model));
        }
        /// <summary>
        /// Sets a user email
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="newEmail">New email</param>
        /// <param name="requireValidation">Require validation of new email address</param>
        public virtual void SetEmail(Customer customer, string newEmail, bool requireValidation)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }

            if (newEmail == null)
            {
                throw new YStoryException("Email cannot be null");
            }

            newEmail = newEmail.Trim();
            string oldEmail = customer.Email;

            if (!CommonHelper.IsValidEmail(newEmail))
            {
                throw new YStoryException(_localizationService.GetResource("Account.EmailUsernameErrors.NewEmailIsNotValid"));
            }

            if (newEmail.Length > 100)
            {
                throw new YStoryException(_localizationService.GetResource("Account.EmailUsernameErrors.EmailTooLong"));
            }

            var customer2 = _customerService.GetCustomerByEmail(newEmail);

            if (customer2 != null && customer.Id != customer2.Id)
            {
                throw new YStoryException(_localizationService.GetResource("Account.EmailUsernameErrors.EmailAlreadyExists"));
            }

            if (requireValidation)
            {
                //re-validate email
                customer.EmailToRevalidate = newEmail;
                _customerService.UpdateCustomer(customer);

                //email re-validation message
                _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.EmailRevalidationToken, Guid.NewGuid().ToString());
                _workflowMessageService.SendCustomerEmailRevalidationMessage(customer, _workContext.WorkingLanguage.Id);
            }
            else
            {
                customer.Email = newEmail;
                _customerService.UpdateCustomer(customer);

                //update newsletter subscription (if required)
                if (!String.IsNullOrEmpty(oldEmail) && !oldEmail.Equals(newEmail, StringComparison.InvariantCultureIgnoreCase))
                {
                    foreach (var store in _storeService.GetAllStores())
                    {
                        var subscriptionOld = _newsLetterSubscriptionService.GetNewsLetterOrderByEmailAndStoreId(oldEmail, store.Id);
                        if (subscriptionOld != null)
                        {
                            subscriptionOld.Email = newEmail;
                            _newsLetterSubscriptionService.UpdateNewsLetterSubscription(subscriptionOld);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public virtual ActionResult SubscribeNewsletter(string email, bool subscribe)
        {
            string result;
            bool   success = false;

            if (!CommonHelper.IsValidEmail(email))
            {
                result = _localizationService.GetResource("Newsletter.Email.Wrong");
            }
            else
            {
                email = email.Trim();

                var subscription = _newsLetterSubscriptionService.GetNewsLetterOrderByEmailAndStoreId(email, _storeContext.CurrentStore.Id);
                if (subscription != null)
                {
                    if (subscribe)
                    {
                        if (!subscription.Active)
                        {
                            _workflowMessageService.SendNewsLetterSubscriptionActivationMessage(subscription, _workContext.WorkingLanguage.Id);
                        }
                        result = _localizationService.GetResource("Newsletter.SubscribeEmailSent");
                    }
                    else
                    {
                        if (subscription.Active)
                        {
                            _workflowMessageService.SendNewsLetterSubscriptionDeactivationMessage(subscription, _workContext.WorkingLanguage.Id);
                        }
                        result = _localizationService.GetResource("Newsletter.UnsubscribeEmailSent");
                    }
                }
                else if (subscribe)
                {
                    subscription = new NewsLetterSubscription
                    {
                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                        Email        = email,
                        Active       = false,
                        StoreId      = _storeContext.CurrentStore.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };
                    _newsLetterSubscriptionService.InsertNewsLetterSubscription(subscription);
                    _workflowMessageService.SendNewsLetterSubscriptionActivationMessage(subscription, _workContext.WorkingLanguage.Id);

                    result = _localizationService.GetResource("Newsletter.SubscribeEmailSent");
                }
                else
                {
                    result = _localizationService.GetResource("Newsletter.UnsubscribeEmailSent");
                }
                success = true;
            }

            return(Json(new
            {
                Success = success,
                Result = result,
            }));
        }
Esempio n. 4
0
        /// <summary>
        /// Prepare the customer info model
        /// </summary>
        /// <param name="model">Customer info model</param>
        /// <param name="customer">Customer</param>
        /// <param name="excludeProperties">Whether to exclude populating of model properties from the entity</param>
        /// <param name="overrideCustomCustomerAttributesXml">Overridden customer attributes in XML format; pass null to use CustomCustomerAttributes of customer</param>
        /// <returns>Customer info model</returns>
        public virtual CustomerInfoModel PrepareCustomerInfoModel(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;
            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 = customer.GetAttribute <string>(SystemCustomerAttributeNames.VatNumber);
                model.FirstName = customer.GetAttribute <string>(SystemCustomerAttributeNames.FirstName);
                model.LastName  = customer.GetAttribute <string>(SystemCustomerAttributeNames.LastName);
                model.Gender    = customer.GetAttribute <string>(SystemCustomerAttributeNames.Gender);
                var dateOfBirth = customer.GetAttribute <DateTime?>(SystemCustomerAttributeNames.DateOfBirth);
                if (dateOfBirth.HasValue)
                {
                    model.DateOfBirthDay   = dateOfBirth.Value.Day;
                    model.DateOfBirthMonth = dateOfBirth.Value.Month;
                    model.DateOfBirthYear  = dateOfBirth.Value.Year;
                }
                model.Company         = customer.GetAttribute <string>(SystemCustomerAttributeNames.Company);
                model.StreetAddress   = customer.GetAttribute <string>(SystemCustomerAttributeNames.StreetAddress);
                model.StreetAddress2  = customer.GetAttribute <string>(SystemCustomerAttributeNames.StreetAddress2);
                model.ZipPostalCode   = customer.GetAttribute <string>(SystemCustomerAttributeNames.ZipPostalCode);
                model.City            = customer.GetAttribute <string>(SystemCustomerAttributeNames.City);
                model.CountryId       = customer.GetAttribute <int>(SystemCustomerAttributeNames.CountryId);
                model.StateProvinceId = customer.GetAttribute <int>(SystemCustomerAttributeNames.StateProvinceId);
                model.Phone           = customer.GetAttribute <string>(SystemCustomerAttributeNames.Phone);
                model.Fax             = customer.GetAttribute <string>(SystemCustomerAttributeNames.Fax);

                //newsletter
                var newsletter = _newsLetterSubscriptionService.GetNewsLetterOrderByEmailAndStoreId(customer.Email, _storeContext.CurrentStore.Id);
                model.Newsletter = newsletter != null && newsletter.Active;

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

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

            if (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation)
            {
                model.EmailToRevalidate = customer.EmailToRevalidate;
            }

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

                if (_customerSettings.StateProvinceEnabled)
                {
                    //states
                    var states = _stateProvinceService.GetStateProvincesByCountryId(model.CountryId, _workContext.WorkingLanguage.Id).ToList();
                    if (states.Any())
                    {
                        model.AvailableStates.Add(new SelectListItem {
                            Text = _localizationService.GetResource("Address.SelectState"), Value = "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
                    {
                        bool anyCountrySelected = model.AvailableCountries.Any(x => x.Selected);

                        model.AvailableStates.Add(new SelectListItem
                        {
                            Text  = _localizationService.GetResource(anyCountrySelected ? "Address.OtherNonUS" : "Address.SelectState"),
                            Value = "0"
                        });
                    }
                }
            }
            model.DisplayVatNumber    = _taxSettings.EuVatEnabled;
            model.VatNumberStatusNote = ((VatNumberStatus)customer.GetAttribute <int>(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 = _openAuthenticationService
                                                            .LoadActiveExternalAuthenticationMethods(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id).Count;
            foreach (var ear in _openAuthenticationService.GetExternalIdentifiersFor(customer))
            {
                var authMethod = _openAuthenticationService.LoadExternalAuthenticationMethodBySystemName(ear.ProviderSystemName);
                if (authMethod == null || !authMethod.IsMethodActive(_externalAuthenticationSettings))
                {
                    continue;
                }

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

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

            customAttributes.ForEach(model.CustomerAttributes.Add);

            return(model);
        }