public ActionResult Edit(CustomerModel model, bool continueEditing, FormCollection form) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var customer = _customerService.GetCustomerById(model.Id); if (customer == null || customer.Deleted) //No customer found with the specified id return RedirectToAction("List"); //validate customer roles var allCustomerRoles = _customerService.GetAllCustomerRoles(true); var newCustomerRoles = new List<CustomerRole>(); foreach (var customerRole in allCustomerRoles) if (model.SelectedCustomerRoleIds != null && model.SelectedCustomerRoleIds.Contains(customerRole.Id)) newCustomerRoles.Add(customerRole); var customerRolesError = ValidateCustomerRoles(newCustomerRoles); if (!String.IsNullOrEmpty(customerRolesError)) ModelState.AddModelError("", customerRolesError); bool allowManagingCustomerRoles = _permissionService.Authorize(StandardPermissionProvider.ManageCustomerRoles); if (ModelState.IsValid) { try { customer.AdminComment = model.AdminComment; customer.IsTaxExempt = model.IsTaxExempt; customer.Active = model.Active; //email if (!String.IsNullOrWhiteSpace(model.Email)) { _customerRegistrationService.SetEmail(customer, model.Email); } else { customer.Email = model.Email; } //username if (_customerSettings.UsernamesEnabled && _customerSettings.AllowUsersToChangeUsernames) { if (!String.IsNullOrWhiteSpace(model.Username)) { _customerRegistrationService.SetUsername(customer, model.Username); } else { customer.Username = model.Username; } } //VAT number if (_taxSettings.EuVatEnabled) { string prevVatNumber = customer.GetAttribute<string>(SystemCustomerAttributeNames.VatNumber); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.VatNumber, model.VatNumber); //set VAT number status if (!String.IsNullOrEmpty(model.VatNumber)) { if (!model.VatNumber.Equals(prevVatNumber, StringComparison.InvariantCultureIgnoreCase)) { _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.VatNumberStatusId, (int)_taxService.GetVatNumberStatus(model.VatNumber)); } } else { _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.VatNumberStatusId, (int)VatNumberStatus.Empty); } } _customerService.UpdateCustomer(customer); //form fields if (_dateTimeSettings.AllowCustomersToSetTimeZone) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.TimeZoneId, model.TimeZoneId); if (_customerSettings.GenderEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Gender, model.Gender); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.FirstName, model.FirstName); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.LastName, model.LastName); if (_customerSettings.DateOfBirthEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.DateOfBirth, model.DateOfBirth); if (_customerSettings.CompanyEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Company, model.Company); if (_customerSettings.StreetAddressEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress, model.StreetAddress); if (_customerSettings.StreetAddress2Enabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress2, model.StreetAddress2); if (_customerSettings.ZipPostalCodeEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.ZipPostalCode, model.ZipPostalCode); if (_customerSettings.CityEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.City, model.City); if (_customerSettings.CountryEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.CountryId, model.CountryId); if (_customerSettings.CountryEnabled && _customerSettings.StateProvinceEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StateProvinceId, model.StateProvinceId); if (_customerSettings.PhoneEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Phone, model.Phone); if (_customerSettings.FaxEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Fax, model.Fax); //customer roles if (allowManagingCustomerRoles) { foreach (var customerRole in allCustomerRoles) { if (model.SelectedCustomerRoleIds != null && model.SelectedCustomerRoleIds.Contains(customerRole.Id)) { //new role if (customer.CustomerRoles.Where(cr => cr.Id == customerRole.Id).Count() == 0) customer.CustomerRoles.Add(customerRole); } else { //removed role if (customer.CustomerRoles.Where(cr => cr.Id == customerRole.Id).Count() > 0) customer.CustomerRoles.Remove(customerRole); } } _customerService.UpdateCustomer(customer); } _eventPublisher.Publish(new ModelBoundEvent(model, customer, form)); //activity log _customerActivityService.InsertActivity("EditCustomer", _localizationService.GetResource("ActivityLog.EditCustomer"), customer.Id); NotifySuccess(_localizationService.GetResource("Admin.Customers.Customers.Updated")); return continueEditing ? RedirectToAction("Edit", customer.Id) : RedirectToAction("List"); } catch (Exception exc) { NotifyError(exc.Message, false); } } //If we got this far, something failed, redisplay form model.UsernamesEnabled = _customerSettings.UsernamesEnabled; model.AllowUsersToChangeUsernames = _customerSettings.AllowUsersToChangeUsernames; model.AllowCustomersToSetTimeZone = _dateTimeSettings.AllowCustomersToSetTimeZone; foreach (var tzi in _dateTimeHelper.GetSystemTimeZones()) model.AvailableTimeZones.Add(new SelectListItem() { Text = tzi.DisplayName, Value = tzi.Id, Selected = (tzi.Id == model.TimeZoneId) }); model.DisplayVatNumber = _taxSettings.EuVatEnabled; model.VatNumberStatusNote = ((VatNumberStatus)customer.GetAttribute<int>(SystemCustomerAttributeNames.VatNumberStatusId)) .GetLocalizedEnum(_localizationService, _workContext); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(customer.CreatedOnUtc, DateTimeKind.Utc); model.LastActivityDate = _dateTimeHelper.ConvertToUserTime(customer.LastActivityDateUtc, DateTimeKind.Utc); model.LastIpAddress = model.LastIpAddress; model.LastVisitedPage = customer.GetAttribute<string>(SystemCustomerAttributeNames.LastVisitedPage); //form fields model.GenderEnabled = _customerSettings.GenderEnabled; model.DateOfBirthEnabled = _customerSettings.DateOfBirthEnabled; model.CompanyEnabled = _customerSettings.CompanyEnabled; model.StreetAddressEnabled = _customerSettings.StreetAddressEnabled; model.StreetAddress2Enabled = _customerSettings.StreetAddress2Enabled; model.ZipPostalCodeEnabled = _customerSettings.ZipPostalCodeEnabled; model.CityEnabled = _customerSettings.CityEnabled; model.CountryEnabled = _customerSettings.CountryEnabled; model.StateProvinceEnabled = _customerSettings.StateProvinceEnabled; model.PhoneEnabled = _customerSettings.PhoneEnabled; model.FaxEnabled = _customerSettings.FaxEnabled; //countries and states if (_customerSettings.CountryEnabled) { model.AvailableCountries.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Address.SelectCountry"), Value = "0" }); foreach (var c in _countryService.GetAllCountries()) { model.AvailableCountries.Add(new SelectListItem() { Text = c.Name, Value = c.Id.ToString(), Selected = c.Id == model.CountryId }); } if (_customerSettings.StateProvinceEnabled) { //states var states = _stateProvinceService.GetStateProvincesByCountryId(model.CountryId).ToList(); if (states.Count > 0) { foreach (var s in states) model.AvailableStates.Add(new SelectListItem() { Text = s.Name, Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId) }); } else model.AvailableStates.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Address.OtherNonUS"), Value = "0" }); } } //customer roles model.AvailableCustomerRoles = _customerService .GetAllCustomerRoles(true) .Select(cr => cr.ToModel()) .ToList(); model.AllowManagingCustomerRoles = allowManagingCustomerRoles; //reward points gistory model.DisplayRewardPointsHistory = _rewardPointsSettings.Enabled; model.AddRewardPointsValue = 0; model.AddRewardPointsMessage = "Some comment here..."; //external authentication records model.AssociatedExternalAuthRecords = GetAssociatedExternalAuthRecords(customer); return View(model); }
public ActionResult Create(CustomerModel model, bool continueEditing, FormCollection form) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); if (!String.IsNullOrWhiteSpace(model.Email)) { var cust2 = _customerService.GetCustomerByEmail(model.Email); if (cust2 != null) ModelState.AddModelError("", "Email is already registered"); } if (!String.IsNullOrWhiteSpace(model.Username) & _customerSettings.UsernamesEnabled) { var cust2 = _customerService.GetCustomerByEmail(model.Username); if (cust2 != null) ModelState.AddModelError("", "Username is already registered"); } //validate customer roles var allCustomerRoles = _customerService.GetAllCustomerRoles(true); var newCustomerRoles = new List<CustomerRole>(); if (model.SelectedCustomerRoleIds != null) { foreach (var customerRole in allCustomerRoles) { if (model.SelectedCustomerRoleIds.Contains(customerRole.Id)) newCustomerRoles.Add(customerRole); } } var customerRolesError = ValidateCustomerRoles(newCustomerRoles); if (customerRolesError.HasValue()) ModelState.AddModelError("", customerRolesError); bool allowManagingCustomerRoles = _permissionService.Authorize(StandardPermissionProvider.ManageCustomerRoles); if (ModelState.IsValid) { var customer = new Customer() { CustomerGuid = Guid.NewGuid(), Email = model.Email, Username = model.Username, AdminComment = model.AdminComment, IsTaxExempt = model.IsTaxExempt, Active = model.Active, CreatedOnUtc = DateTime.UtcNow, LastActivityDateUtc = DateTime.UtcNow, }; _customerService.InsertCustomer(customer); //form fields if (_dateTimeSettings.AllowCustomersToSetTimeZone) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.TimeZoneId, model.TimeZoneId); if (_customerSettings.GenderEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Gender, model.Gender); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.FirstName, model.FirstName); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.LastName, model.LastName); if (_customerSettings.DateOfBirthEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.DateOfBirth, model.DateOfBirth); if (_customerSettings.CompanyEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Company, model.Company); if (_customerSettings.StreetAddressEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress, model.StreetAddress); if (_customerSettings.StreetAddress2Enabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress2, model.StreetAddress2); if (_customerSettings.ZipPostalCodeEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.ZipPostalCode, model.ZipPostalCode); if (_customerSettings.CityEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.City, model.City); if (_customerSettings.CountryEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.CountryId, model.CountryId); if (_customerSettings.CountryEnabled && _customerSettings.StateProvinceEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StateProvinceId, model.StateProvinceId); if (_customerSettings.PhoneEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Phone, model.Phone); if (_customerSettings.FaxEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Fax, model.Fax); //password if (!String.IsNullOrWhiteSpace(model.Password)) { var changePassRequest = new ChangePasswordRequest(model.Email, false, _customerSettings.DefaultPasswordFormat, model.Password); var changePassResult = _customerRegistrationService.ChangePassword(changePassRequest); if (!changePassResult.Success) { foreach (var changePassError in changePassResult.Errors) NotifyError(changePassError); } } //customer roles if (allowManagingCustomerRoles) { foreach (var customerRole in newCustomerRoles) customer.CustomerRoles.Add(customerRole); _customerService.UpdateCustomer(customer); } _eventPublisher.Publish(new ModelBoundEvent(model, customer, form)); //activity log _customerActivityService.InsertActivity("AddNewCustomer", _localizationService.GetResource("ActivityLog.AddNewCustomer"), customer.Id); NotifySuccess(_localizationService.GetResource("Admin.Customers.Customers.Added")); return continueEditing ? RedirectToAction("Edit", new { id = customer.Id }) : RedirectToAction("List"); } //If we got this far, something failed, redisplay form PrepareCustomerModelForCreate(model); return View(model); }
public ActionResult Edit(int id) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var customer = _customerService.GetCustomerById(id); if (customer == null || customer.Deleted) //No customer found with the specified id return RedirectToAction("List"); var model = new CustomerModel(); model.Id = customer.Id; model.Email = customer.Email; model.Username = customer.Username; model.AdminComment = customer.AdminComment; model.IsTaxExempt = customer.IsTaxExempt; model.Active = customer.Active; model.TimeZoneId = customer.GetAttribute<string>(SystemCustomerAttributeNames.TimeZoneId); model.UsernamesEnabled = _customerSettings.UsernamesEnabled; model.AllowUsersToChangeUsernames = _customerSettings.AllowUsersToChangeUsernames; model.AllowCustomersToSetTimeZone = _dateTimeSettings.AllowCustomersToSetTimeZone; foreach (var tzi in _dateTimeHelper.GetSystemTimeZones()) model.AvailableTimeZones.Add(new SelectListItem() { Text = tzi.DisplayName, Value = tzi.Id, Selected = (tzi.Id == model.TimeZoneId) }); model.DisplayVatNumber = _taxSettings.EuVatEnabled; model.VatNumber = customer.GetAttribute<string>(SystemCustomerAttributeNames.VatNumber); model.VatNumberStatusNote = ((VatNumberStatus)customer.GetAttribute<int>(SystemCustomerAttributeNames.VatNumberStatusId)) .GetLocalizedEnum(_localizationService, _workContext); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(customer.CreatedOnUtc, DateTimeKind.Utc); model.LastActivityDate = _dateTimeHelper.ConvertToUserTime(customer.LastActivityDateUtc, DateTimeKind.Utc); model.LastIpAddress = customer.LastIpAddress; model.LastVisitedPage = customer.GetAttribute<string>(SystemCustomerAttributeNames.LastVisitedPage); model.AffiliateId = customer.AffiliateId; if (customer.AffiliateId != 0) { var affiliate = _affiliateService.GetAffiliateById(customer.AffiliateId); if (affiliate != null && affiliate.Address != null) model.AffiliateFullName = affiliate.Address.GetFullName(); } //form fields model.FirstName = customer.GetAttribute<string>(SystemCustomerAttributeNames.FirstName); model.LastName = customer.GetAttribute<string>(SystemCustomerAttributeNames.LastName); model.Gender = customer.GetAttribute<string>(SystemCustomerAttributeNames.Gender); model.DateOfBirth = customer.GetAttribute<DateTime?>(SystemCustomerAttributeNames.DateOfBirth); 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); model.GenderEnabled = _customerSettings.GenderEnabled; model.DateOfBirthEnabled = _customerSettings.DateOfBirthEnabled; model.CompanyEnabled = _customerSettings.CompanyEnabled; model.StreetAddressEnabled = _customerSettings.StreetAddressEnabled; model.StreetAddress2Enabled = _customerSettings.StreetAddress2Enabled; model.ZipPostalCodeEnabled = _customerSettings.ZipPostalCodeEnabled; model.CityEnabled = _customerSettings.CityEnabled; model.CountryEnabled = _customerSettings.CountryEnabled; model.StateProvinceEnabled = _customerSettings.StateProvinceEnabled; model.PhoneEnabled = _customerSettings.PhoneEnabled; model.FaxEnabled = _customerSettings.FaxEnabled; //countries and states if (_customerSettings.CountryEnabled) { model.AvailableCountries.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Address.SelectCountry"), Value = "0" }); foreach (var c in _countryService.GetAllCountries()) { model.AvailableCountries.Add(new SelectListItem() { Text = c.Name, Value = c.Id.ToString(), Selected = c.Id == model.CountryId }); } if (_customerSettings.StateProvinceEnabled) { //states var states = _stateProvinceService.GetStateProvincesByCountryId(model.CountryId).ToList(); if (states.Count > 0) { foreach (var s in states) model.AvailableStates.Add(new SelectListItem() { Text = s.Name, Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId) }); } else model.AvailableStates.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Address.OtherNonUS"), Value = "0" }); } } //customer roles model.AvailableCustomerRoles = _customerService .GetAllCustomerRoles(true) .Select(cr => cr.ToModel()) .ToList(); model.SelectedCustomerRoleIds = customer.CustomerRoles.Select(cr => cr.Id).ToArray(); model.AllowManagingCustomerRoles = _permissionService.Authorize(StandardPermissionProvider.ManageCustomerRoles); //reward points gistory model.DisplayRewardPointsHistory = _rewardPointsSettings.Enabled; model.AddRewardPointsValue = 0; model.AddRewardPointsMessage = "Some comment here..."; //external authentication records model.AssociatedExternalAuthRecords = GetAssociatedExternalAuthRecords(customer); return View(model); }
public ActionResult ChangePassword(CustomerModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var customer = _customerService.GetCustomerById(model.Id); if (customer == null) //No customer found with the specified id return RedirectToAction("List"); if (ModelState.IsValid) { var changePassRequest = new ChangePasswordRequest(model.Email, false, _customerSettings.DefaultPasswordFormat, model.Password); var changePassResult = _customerRegistrationService.ChangePassword(changePassRequest); if (changePassResult.Success) NotifySuccess(_localizationService.GetResource("Admin.Customers.Customers.PasswordChanged")); else foreach (var error in changePassResult.Errors) NotifyError(error); } return RedirectToAction("Edit", customer.Id); }
public ActionResult Create() { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var model = new CustomerModel(); PrepareCustomerModelForCreate(model); //default value model.Active = true; return View(model); }
private void PrepareCustomerModelForCreate(CustomerModel model) { string timeZoneId = (model.TimeZoneId.HasValue() ? model.TimeZoneId : _dateTimeHelper.DefaultStoreTimeZone.Id); model.UsernamesEnabled = _customerSettings.UsernamesEnabled; model.AllowUsersToChangeUsernames = _customerSettings.AllowUsersToChangeUsernames; model.AllowCustomersToSetTimeZone = _dateTimeSettings.AllowCustomersToSetTimeZone; foreach (var tzi in _dateTimeHelper.GetSystemTimeZones()) { model.AvailableTimeZones.Add(new SelectListItem() { Text = tzi.DisplayName, Value = tzi.Id, Selected = (tzi.Id == timeZoneId) }); } model.DisplayVatNumber = false; //customer roles model.AvailableCustomerRoles = _customerService .GetAllCustomerRoles(true) .Select(cr => cr.ToModel()) .ToList(); if (model.SelectedCustomerRoleIds == null) { model.SelectedCustomerRoleIds = new int[0]; } model.AllowManagingCustomerRoles = _permissionService.Authorize(StandardPermissionProvider.ManageCustomerRoles); //form fields model.GenderEnabled = _customerSettings.GenderEnabled; model.DateOfBirthEnabled = _customerSettings.DateOfBirthEnabled; model.CompanyEnabled = _customerSettings.CompanyEnabled; model.StreetAddressEnabled = _customerSettings.StreetAddressEnabled; model.StreetAddress2Enabled = _customerSettings.StreetAddress2Enabled; model.ZipPostalCodeEnabled = _customerSettings.ZipPostalCodeEnabled; model.CityEnabled = _customerSettings.CityEnabled; model.CountryEnabled = _customerSettings.CountryEnabled; model.StateProvinceEnabled = _customerSettings.StateProvinceEnabled; model.PhoneEnabled = _customerSettings.PhoneEnabled; model.FaxEnabled = _customerSettings.FaxEnabled; if (_customerSettings.CountryEnabled) { model.AvailableCountries.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Address.SelectCountry"), Value = "0" }); foreach (var c in _countryService.GetAllCountries()) { model.AvailableCountries.Add(new SelectListItem() { Text = c.Name, Value = c.Id.ToString(), Selected = (c.Id == model.CountryId) }); } if (_customerSettings.StateProvinceEnabled) { //states var states = _stateProvinceService.GetStateProvincesByCountryId(model.CountryId).ToList(); if (states.Count > 0) { foreach (var s in states) { model.AvailableStates.Add(new SelectListItem() { Text = s.Name, Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId) }); } } else { model.AvailableStates.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Address.OtherNonUS"), Value = "0" }); } } } }
public ActionResult SendPm(CustomerModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var customer = _customerService.GetCustomerById(model.Id); if (customer == null) //No customer found with the specified id return RedirectToAction("List"); try { if (!_forumSettings.AllowPrivateMessages) throw new SmartException("Private messages are disabled"); if (customer.IsGuest()) throw new SmartException("Customer should be registered"); if (String.IsNullOrWhiteSpace(model.SendPm.Subject)) throw new SmartException("PM subject is empty"); if (String.IsNullOrWhiteSpace(model.SendPm.Message)) throw new SmartException("PM message is empty"); var privateMessage = new PrivateMessage { StoreId = _storeContext.CurrentStore.Id, ToCustomerId = customer.Id, FromCustomerId = _workContext.CurrentCustomer.Id, Subject = model.SendPm.Subject, Text = model.SendPm.Message, IsDeletedByAuthor = false, IsDeletedByRecipient = false, IsRead = false, CreatedOnUtc = DateTime.UtcNow }; _forumService.InsertPrivateMessage(privateMessage); NotifySuccess(_localizationService.GetResource("Admin.Customers.Customers.SendPM.Sent")); } catch (Exception exc) { NotifyError(exc.Message); } return RedirectToAction("Edit", new { id = customer.Id }); }
public ActionResult SendEmail(CustomerModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var customer = _customerService.GetCustomerById(model.Id); if (customer == null) //No customer found with the specified id return RedirectToAction("List"); try { if (String.IsNullOrWhiteSpace(customer.Email)) throw new SmartException("Customer email is empty"); if (!customer.Email.IsEmail()) throw new SmartException("Customer email is not valid"); if (String.IsNullOrWhiteSpace(model.SendEmail.Subject)) throw new SmartException("Email subject is empty"); if (String.IsNullOrWhiteSpace(model.SendEmail.Body)) throw new SmartException("Email body is empty"); var emailAccount = _emailAccountService.GetEmailAccountById(_emailAccountSettings.DefaultEmailAccountId); if (emailAccount == null) emailAccount = _emailAccountService.GetAllEmailAccounts().FirstOrDefault(); if (emailAccount == null) throw new SmartException("Email account can't be loaded"); var email = new QueuedEmail() { EmailAccountId = emailAccount.Id, FromName = emailAccount.DisplayName, From = emailAccount.Email, ToName = customer.GetFullName(), To = customer.Email, Subject = model.SendEmail.Subject, Body = model.SendEmail.Body, CreatedOnUtc = DateTime.UtcNow, }; _queuedEmailService.InsertQueuedEmail(email); NotifySuccess(_localizationService.GetResource("Admin.Customers.Customers.SendEmail.Queued")); } catch (Exception exc) { NotifyError(exc.Message); } return RedirectToAction("Edit", new { id = customer.Id }); }
public ActionResult MarkVatNumberAsValid(CustomerModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var customer = _customerService.GetCustomerById(model.Id); if (customer == null) //No customer found with the specified id return RedirectToAction("List"); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.VatNumberStatusId, (int)VatNumberStatus.Valid); return RedirectToAction("Edit", customer.Id); }
public ActionResult Create(CustomerModel model, bool continueEditing) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); if (!String.IsNullOrWhiteSpace(model.Email)) { var cust2 = _customerService.GetCustomerByEmail(model.Email); if (cust2 != null) ModelState.AddModelError("", "Email is already registered"); } if (!String.IsNullOrWhiteSpace(model.Username) & _customerSettings.UsernamesEnabled) { var cust2 = _customerService.GetCustomerByEmail(model.Username); if (cust2 != null) ModelState.AddModelError("", "Username is already registered"); } //validate customer roles var allCustomerRoles = _customerService.GetAllCustomerRoles(true); var newCustomerRoles = new List<CustomerRole>(); foreach (var customerRole in allCustomerRoles) if (model.SelectedCustomerRoleIds != null && model.SelectedCustomerRoleIds.Contains(customerRole.Id)) newCustomerRoles.Add(customerRole); var customerRolesError = ValidateCustomerRoles(newCustomerRoles); if (!String.IsNullOrEmpty(customerRolesError)) ModelState.AddModelError("", customerRolesError); bool allowManagingCustomerRoles = _permissionService.Authorize(StandardPermissionProvider.ManageCustomerRoles); if (ModelState.IsValid) { var customer = new Customer() { CustomerGuid = Guid.NewGuid(), Email = model.Email, Username = model.Username, AdminComment = model.AdminComment, IsTaxExempt = model.IsTaxExempt, Active = model.Active, CreatedOnUtc = DateTime.UtcNow, LastActivityDateUtc = DateTime.UtcNow, }; _customerService.InsertCustomer(customer); //form fields if (_dateTimeSettings.AllowCustomersToSetTimeZone) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.TimeZoneId, model.TimeZoneId); if (_customerSettings.GenderEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Gender, model.Gender); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.FirstName, model.FirstName); _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.LastName, model.LastName); if (_customerSettings.DateOfBirthEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.DateOfBirth, model.DateOfBirth); if (_customerSettings.CompanyEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Company, model.Company); if (_customerSettings.StreetAddressEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress, model.StreetAddress); if (_customerSettings.StreetAddress2Enabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress2, model.StreetAddress2); if (_customerSettings.ZipPostalCodeEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.ZipPostalCode, model.ZipPostalCode); if (_customerSettings.CityEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.City, model.City); if (_customerSettings.CountryEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.CountryId, model.CountryId); if (_customerSettings.CountryEnabled && _customerSettings.StateProvinceEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StateProvinceId, model.StateProvinceId); if (_customerSettings.PhoneEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Phone, model.Phone); if (_customerSettings.FaxEnabled) _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Fax, model.Fax); //password if (!String.IsNullOrWhiteSpace(model.Password)) { var changePassRequest = new ChangePasswordRequest(model.Email, false, _customerSettings.DefaultPasswordFormat, model.Password); var changePassResult = _customerRegistrationService.ChangePassword(changePassRequest); if (!changePassResult.Success) { foreach (var changePassError in changePassResult.Errors) NotifyError(changePassError); } } //customer roles if (allowManagingCustomerRoles) { foreach (var customerRole in newCustomerRoles) customer.CustomerRoles.Add(customerRole); _customerService.UpdateCustomer(customer); } //activity log _customerActivityService.InsertActivity("AddNewCustomer", _localizationService.GetResource("ActivityLog.AddNewCustomer"), customer.Id); NotifySuccess(_localizationService.GetResource("Admin.Customers.Customers.Added")); return continueEditing ? RedirectToAction("Edit", new { id = customer.Id }) : RedirectToAction("List"); } //If we got this far, something failed, redisplay form model.UsernamesEnabled = _customerSettings.UsernamesEnabled; model.AllowUsersToChangeUsernames = _customerSettings.AllowUsersToChangeUsernames; model.AllowCustomersToSetTimeZone = _dateTimeSettings.AllowCustomersToSetTimeZone; foreach (var tzi in _dateTimeHelper.GetSystemTimeZones()) model.AvailableTimeZones.Add(new SelectListItem() { Text = tzi.DisplayName, Value = tzi.Id, Selected = (tzi.Id == model.TimeZoneId) }); model.DisplayVatNumber = false; //customer roles model.AvailableCustomerRoles = _customerService .GetAllCustomerRoles(true) .Select(cr => cr.ToModel()) .ToList(); model.AllowManagingCustomerRoles = allowManagingCustomerRoles; //form fields model.GenderEnabled = _customerSettings.GenderEnabled; model.DateOfBirthEnabled = _customerSettings.DateOfBirthEnabled; model.CompanyEnabled = _customerSettings.CompanyEnabled; model.StreetAddressEnabled = _customerSettings.StreetAddressEnabled; model.StreetAddress2Enabled = _customerSettings.StreetAddress2Enabled; model.ZipPostalCodeEnabled = _customerSettings.ZipPostalCodeEnabled; model.CityEnabled = _customerSettings.CityEnabled; model.CountryEnabled = _customerSettings.CountryEnabled; model.StateProvinceEnabled = _customerSettings.StateProvinceEnabled; model.PhoneEnabled = _customerSettings.PhoneEnabled; model.FaxEnabled = _customerSettings.FaxEnabled; if (_customerSettings.CountryEnabled) { model.AvailableCountries.Add(new SelectListItem() { Text = _localizationService.GetResource("Address.SelectCountry"), Value = "0" }); foreach (var c in _countryService.GetAllCountries()) { model.AvailableCountries.Add(new SelectListItem() { Text = c.Name, Value = c.Id.ToString(), Selected = (c.Id == model.CountryId) }); } if (_customerSettings.StateProvinceEnabled) { //states var states = _stateProvinceService.GetStateProvincesByCountryId(model.CountryId).ToList(); if (states.Count > 0) { foreach (var s in states) model.AvailableStates.Add(new SelectListItem() { Text = s.Name, Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId) }); } else model.AvailableStates.Add(new SelectListItem() { Text = _localizationService.GetResource("Address.OtherNonUS"), Value = "0" }); } } return View(model); }