PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSModules/Ecommerce/Pages/Tools/Customers/Customer_New.aspx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 263 lines | 194 code | 42 blank | 27 comment | 39 complexity | 201045fe523080207f645d7cf6a8d616 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using CMS.GlobalHelper;
  12. using CMS.DataEngine;
  13. using CMS.SiteProvider;
  14. using CMS.Ecommerce;
  15. using CMS.CMSHelper;
  16. using CMS.UIControls;
  17. public partial class CMSModules_Ecommerce_Pages_Tools_Customers_Customer_New : CMSCustomersPage
  18. {
  19. protected int customerid = 0;
  20. protected void Page_Load(object sender, EventArgs e)
  21. {
  22. // Check UI element
  23. if (!CMSContext.CurrentUser.IsAuthorizedPerUIElement("CMS.Ecommerce", "NewCustomer"))
  24. {
  25. RedirectToCMSDeskUIElementAccessDenied("CMS.Ecommerce", "NewCustomer");
  26. }
  27. this.pnlGeneral.GroupingText = GetString("com.customeredit.general");
  28. this.pnlUserInfo.GroupingText = GetString("com.customeredit.userinfo");
  29. // control initializations
  30. lblCustomerLastName.Text = GetString("Customers_Edit.CustomerLastNameLabel");
  31. lblCustomerFirstName.Text = GetString("Customers_Edit.CustomerFirstNameLabel");
  32. lblCustomerCompany.Text = GetString("Customers_Edit.CustomerCompanyLabel");
  33. lblCountry.Text = GetString("Customers_Edit.CustomerCountry");
  34. lblUserName.Text = GetString("Customers_Edit.UserName");
  35. lblPassword1.Text = GetString("Customer_Edit_Login_Edit.lblPassword1");
  36. lblPassword2.Text = GetString("Customer_Edit_Login_Edit.lblPassword2");
  37. rqvUserName.ErrorMessage = GetString("Customer_Edit_Login_Edit.rqvUserName");
  38. rqvPassword2.ErrorMessage = GetString("Customer_Edit_Login_Edit.rqvPassword2");
  39. btnOk.Text = GetString("General.OK");
  40. string currentCustomer = GetString("Customers_Edit.NewItemCaption");
  41. // Get customer id from querystring
  42. customerid = QueryHelper.GetInteger("customerid", 0);
  43. if (customerid > 0)
  44. {
  45. CustomerInfo customerObj = CustomerInfoProvider.GetCustomerInfo(customerid);
  46. EditedObject = customerObj;
  47. if (customerObj != null)
  48. {
  49. // Fill editing form
  50. if (!RequestHelper.IsPostBack())
  51. {
  52. LoadData(customerObj);
  53. // Show that the customer was created or updated successfully
  54. if (QueryHelper.GetString("saved", "") == "1")
  55. {
  56. lblInfo.Visible = true;
  57. lblInfo.Text = GetString("General.ChangesSaved");
  58. }
  59. }
  60. }
  61. }
  62. else
  63. {
  64. if (!RequestHelper.IsPostBack())
  65. {
  66. this.drpCountry.CountryID = UniSelector.US_NONE_RECORD;
  67. }
  68. }
  69. InitializeMasterPage(currentCustomer);
  70. InitializeLoginControls();
  71. AddMenuButtonSelectScript("NewCustomer", "");
  72. }
  73. /// <summary>
  74. /// Initializes master page elements.
  75. /// </summary>
  76. private void InitializeMasterPage(string currentCustomer)
  77. {
  78. // initializes page title control
  79. string[,] breadcrumbs = new string[2, 3];
  80. breadcrumbs[0, 0] = GetString("Customers_Edit.ItemListLink");
  81. breadcrumbs[0, 1] = "~/CMSModules/Ecommerce/Pages/Tools/Customers/Customer_List.aspx";
  82. breadcrumbs[0, 2] = "";
  83. breadcrumbs[1, 0] = currentCustomer;
  84. breadcrumbs[1, 1] = "";
  85. breadcrumbs[1, 2] = "";
  86. this.CurrentMaster.Title.Breadcrumbs = breadcrumbs;
  87. // Set master title
  88. this.CurrentMaster.Title.TitleText = GetString("Customers_Edit.NewItemCaption");
  89. this.CurrentMaster.Title.TitleImage = GetImageUrl("Objects/Ecommerce_Customer/new.png");
  90. this.CurrentMaster.Title.HelpTopicName = "new_customer";
  91. this.CurrentMaster.Title.HelpName = "helpTopic";
  92. }
  93. /// <summary>
  94. /// Load data of edited customer.
  95. /// </summary>
  96. /// <param name="customerObj">Customer object</param>
  97. protected void LoadData(CustomerInfo customerObj)
  98. {
  99. txtCustomerLastName.Text = customerObj.CustomerLastName;
  100. txtCustomerFirstName.Text = customerObj.CustomerFirstName;
  101. txtCustomerCompany.Text = customerObj.CustomerCompany;
  102. drpCountry.CountryID = customerObj.CustomerCountryID;
  103. drpCountry.StateID = customerObj.CustomerStateID;
  104. }
  105. /// <summary>
  106. /// Sets data to database.
  107. /// </summary>
  108. protected void btnOK_Click(object sender, EventArgs e)
  109. {
  110. if (!ECommerceContext.IsUserAuthorizedToModifyCustomer())
  111. {
  112. RedirectToAccessDenied("CMS.Ecommerce", "EcommerceModify OR ModifyCustomers");
  113. }
  114. string errorMessage = "";
  115. if ((txtCustomerCompany.Text == null || txtCustomerCompany.Text.Trim() == "") &&
  116. ((txtCustomerFirstName.Text == null || txtCustomerFirstName.Text.Trim() == "") ||
  117. (txtCustomerLastName.Text == null || txtCustomerLastName.Text.Trim() == "")))
  118. {
  119. errorMessage = GetString("Customers_Edit.errorInsert");
  120. }
  121. if (errorMessage == "")
  122. {
  123. if (this.chkCreateLogin.Checked)
  124. {
  125. errorMessage = new Validator().NotEmpty(txtUserName.Text.Trim(), GetString("Customer_Edit_Login_Edit.rqvUserName"))
  126. .NotEmpty(passStrength.Text.Trim(), GetString("Customer_Edit_Login_Edit.rqvPassword1"))
  127. .NotEmpty(txtPassword2.Text.Trim(), GetString("Customer_Edit_Login_Edit.rqvPassword2")).Result;
  128. // Check policy
  129. if ((errorMessage == "") && !passStrength.IsValid())
  130. {
  131. errorMessage = UserInfoProvider.GetPolicyViolationMessage(CMSContext.CurrentSiteName);
  132. }
  133. // Compare passwords
  134. if ((errorMessage == "") && (passStrength.Text != txtPassword2.Text))
  135. {
  136. errorMessage = GetString("Customer_Edit_Login_Edit.DifferentPasswords");
  137. }
  138. }
  139. }
  140. if (errorMessage == "")
  141. {
  142. if (this.chkCreateLogin.Checked)
  143. {
  144. // If user already has the record in the CMS_User table
  145. UserInfo existingUser = UserInfoProvider.GetUserInfo(txtUserName.Text.Trim());
  146. if (existingUser != null)
  147. {
  148. errorMessage = GetString("Customer_Edit_Login_Edit.UserExist");
  149. }
  150. }
  151. }
  152. if (errorMessage == "")
  153. {
  154. CustomerInfo customerObj = CustomerInfoProvider.GetCustomerInfo(customerid);
  155. // If customer doesn't already exist, create new one
  156. if (customerObj == null)
  157. {
  158. customerObj = new CustomerInfo();
  159. customerObj.CustomerSiteID = CMSContext.CurrentSiteID;
  160. }
  161. customerObj.CustomerLastName = txtCustomerLastName.Text.Trim();
  162. customerObj.CustomerFirstName = txtCustomerFirstName.Text.Trim();
  163. customerObj.CustomerCompany = txtCustomerCompany.Text.Trim();
  164. customerObj.CustomerCountryID = drpCountry.CountryID;
  165. customerObj.CustomerStateID = drpCountry.StateID;
  166. customerObj.CustomerEnabled = true;
  167. //customerObj.CustomerCreated = ucCustomerCreated.SelectedDateTime;
  168. using (CMSTransactionScope tr = new CMSTransactionScope())
  169. {
  170. CustomerInfoProvider.SetCustomerInfo(customerObj);
  171. // If create login checked
  172. if (this.chkCreateLogin.Checked)
  173. {
  174. UserInfo ui = new UserInfo();
  175. ui.UserName = txtUserName.Text.Trim();
  176. ui.FullName = customerObj.CustomerFirstName + " " + customerObj.CustomerLastName;
  177. ui.IsGlobalAdministrator = false;
  178. ui.SetValue("UserEnabled", true);
  179. UserInfoProvider.SetPassword(ui, passStrength.Text);
  180. // Add user to current site
  181. UserInfoProvider.AddUserToSite(ui.UserName, CMSContext.CurrentSiteName);
  182. customerObj.CustomerUserID = ui.UserID;
  183. CustomerInfoProvider.SetCustomerInfo(customerObj);
  184. }
  185. // Commit transaction
  186. tr.Commit();
  187. }
  188. URLHelper.Redirect("Customer_Edit_Frameset.aspx?customerid=" + Convert.ToString(customerObj.CustomerID) + "&saved=1");
  189. }
  190. else
  191. {
  192. lblError.Visible = true;
  193. lblError.Text = errorMessage;
  194. }
  195. }
  196. private void InitializeLoginControls()
  197. {
  198. // Initialize login controls
  199. chkCreateLogin.Text = GetString("Customer_Edit_Login_Edit.lblHasLogin");
  200. chkCreateLogin.Attributes["onclick"] = "ShowHideLoginControls()";
  201. // Register script to show / hide SKU controls
  202. string script =
  203. "function ShowHideLoginControls() { \n" +
  204. " checkbox = document.getElementById('" + this.chkCreateLogin.ClientID + "'); \n" +
  205. " line1 = document.getElementById('loginLine1'); \n" +
  206. " line2 = document.getElementById('loginLine2'); \n" +
  207. " line3 = document.getElementById('loginLine3'); \n" +
  208. " if ((checkbox != null) && (checkbox.checked)) { \n" +
  209. " line1.style.display = '';" +
  210. " line2.style.display = '';" +
  211. " line3.style.display = '';" +
  212. " } else { \n" +
  213. " line1.style.display = 'none';" +
  214. " line2.style.display = 'none';" +
  215. " line3.style.display = 'none';" +
  216. " } \n" +
  217. "} \n";
  218. this.ltlScript.Text += ScriptHelper.GetScript(script);
  219. this.ltlScript.Text += ScriptHelper.GetScript("ShowHideLoginControls()");
  220. }
  221. }