public UserDomainService(IUserRepository userRepository, UserManager userManager, IUserAvatarRepository userAvatarRepository , GenericAttributeDomianService genericAttributeService) { _userRepository = userRepository; _userAvatarRepository = userAvatarRepository; _userManager = userManager; _genericAttributeService = genericAttributeService; }
///// <summary> ///// Get full name ///// </summary> ///// <param name="customer">Customer</param> ///// <returns>Customer full name</returns> //public static string GetFullName(this User customer) //{ // if (customer == null) // throw new ArgumentNullException("customer"); // var firstName = customer.GetAttribute<string>(SystemCustomerAttributeNames.FirstName); // var lastName = customer.GetAttribute<string>(SystemCustomerAttributeNames.LastName); // string fullName = ""; // if (!String.IsNullOrWhiteSpace(firstName) && !String.IsNullOrWhiteSpace(lastName)) // fullName = string.Format("{0} {1}", firstName, lastName); // else // { // if (!String.IsNullOrWhiteSpace(firstName)) // fullName = firstName; // if (!String.IsNullOrWhiteSpace(lastName)) // fullName = lastName; // } // return fullName; //} ///// <summary> ///// Formats the customer name ///// </summary> ///// <param name="customer">Source</param> ///// <param name="stripTooLong">Strip too long customer name</param> ///// <param name="maxLength">Maximum customer name length</param> ///// <returns>Formatted text</returns> //public static string FormatUserName(this User customer, bool stripTooLong = false, int maxLength = 0) //{ // if (customer == null) // return string.Empty; // if (customer.IsGuest()) // { // return EngineContext.Current.Resolve<ILocalizationService>().GetResource("Customer.Guest"); // } // string result = string.Empty; // switch (EngineContext.Current.Resolve<CustomerSettings>().CustomerNameFormat) // { // case CustomerNameFormat.ShowEmails: // result = customer.Email; // break; // case CustomerNameFormat.ShowUsernames: // result = customer.Username; // break; // case CustomerNameFormat.ShowFullNames: // result = customer.GetFullName(); // break; // case CustomerNameFormat.ShowFirstName: // result = customer.GetAttribute<string>(SystemCustomerAttributeNames.FirstName); // break; // default: // break; // } // if (stripTooLong && maxLength > 0) // { // result = CommonUtil.EnsureMaximumLength(result, maxLength); // } // return result; //} /// <summary> /// Gets coupon codes /// </summary> /// <param name="customer">Customer</param> /// <returns>Coupon codes</returns> public static string[] ParseAppliedGiftCardCouponCodes(this User customer, GenericAttributeDomianService genericAttributeService) { if (customer == null) { throw new ArgumentNullException("customer"); } var existingGiftCartCouponCodes = customer.GetAttribute <string>(SystemCustomerAttributeNames.GiftCardCouponCodes, genericAttributeService); var couponCodes = new List <string>(); if (String.IsNullOrEmpty(existingGiftCartCouponCodes)) { return(couponCodes.ToArray()); } try { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(existingGiftCartCouponCodes); var nodeList1 = xmlDoc.SelectNodes(@"//GiftCardCouponCodes/CouponCode"); foreach (XmlNode node1 in nodeList1) { if (node1.Attributes != null && node1.Attributes["Code"] != null) { string code = node1.Attributes["Code"].InnerText.Trim(); couponCodes.Add(code); } } } catch (Exception exc) { Debug.Write(exc.ToString()); } return(couponCodes.ToArray()); }
/// <summary> /// Adds a coupon code /// </summary> /// <param name="customer">Customer</param> /// <param name="couponCode">Coupon code</param> /// <returns>New coupon codes document</returns> public static void ApplyGiftCardCouponCode(this User customer, string couponCode, GenericAttributeDomianService genericAttributeService) { if (customer == null) { throw new ArgumentNullException("customer"); } //var genericAttributeService = EngineContext.Current.Resolve<IGenericAttributeService>(); string result = string.Empty; try { var existingGiftCartCouponCodes = customer.GetAttribute <string>(SystemCustomerAttributeNames.GiftCardCouponCodes, genericAttributeService); couponCode = couponCode.Trim().ToLower(); var xmlDoc = new XmlDocument(); if (String.IsNullOrEmpty(existingGiftCartCouponCodes)) { var element1 = xmlDoc.CreateElement("GiftCardCouponCodes"); xmlDoc.AppendChild(element1); } else { xmlDoc.LoadXml(existingGiftCartCouponCodes); } var rootElement = (XmlElement)xmlDoc.SelectSingleNode(@"//GiftCardCouponCodes"); XmlElement gcElement = null; //find existing var nodeList1 = xmlDoc.SelectNodes(@"//GiftCardCouponCodes/CouponCode"); foreach (XmlNode node1 in nodeList1) { if (node1.Attributes != null && node1.Attributes["Code"] != null) { string couponCodeAttribute = node1.Attributes["Code"].InnerText.Trim(); if (couponCodeAttribute.ToLower() == couponCode.ToLower()) { gcElement = (XmlElement)node1; break; } } } //create new one if not found if (gcElement == null) { gcElement = xmlDoc.CreateElement("CouponCode"); gcElement.SetAttribute("Code", couponCode); rootElement.AppendChild(gcElement); } result = xmlDoc.OuterXml; } catch (Exception exc) { Debug.Write(exc.ToString()); } //apply new value genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.GiftCardCouponCodes, result); }
//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); } }