PageRenderTime 105ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSModules/Bizforms/Tools/BizForm_Edit_NotificationEmail.aspx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 415 lines | 281 code | 71 blank | 63 comment | 47 complexity | 99d2fe8b073c57b68ec9625fcf7fbab2 MD5 | raw file
  1. using System;
  2. using System.Web.UI.WebControls;
  3. using CMS.CMSHelper;
  4. using CMS.FormEngine;
  5. using CMS.GlobalHelper;
  6. using CMS.SettingsProvider;
  7. using CMS.UIControls;
  8. public partial class CMSModules_BizForms_Tools_BizForm_Edit_NotificationEmail : CMSBizFormPage
  9. {
  10. #region "Variables"
  11. protected string mSave = null;
  12. private int formId = 0;
  13. private DataClassInfo formClassObj = null;
  14. #endregion
  15. #region "Private properties"
  16. /// <summary>
  17. /// Indicates whether custom form layout is set or not.
  18. /// </summary>
  19. private bool IsLayoutSet
  20. {
  21. get
  22. {
  23. object obj = ViewState["IsLayoutSet"];
  24. return (obj == null) ? false : (bool)obj;
  25. }
  26. set
  27. {
  28. ViewState["IsLayoutSet"] = value;
  29. }
  30. }
  31. #endregion
  32. #region "Methods"
  33. protected void Page_Load(object sender, EventArgs e)
  34. {
  35. // Check 'ReadForm' and 'EditData' permission
  36. if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.form", "ReadForm"))
  37. {
  38. RedirectToCMSDeskAccessDenied("cms.form", "ReadForm");
  39. }
  40. // Get form id from url
  41. formId = QueryHelper.GetInteger("formid", 0);
  42. // Control initialization
  43. ltlConfirmDelete.Text = "<input type=\"hidden\" id=\"confirmdelete\" value=\"" + GetString("Bizform_Edit_Notificationemail.ConfirmDelete") + "\">";
  44. chkSendToEmail.Text = GetString("BizFormGeneral.chkSendToEmail");
  45. chkAttachDocs.Text = GetString("BizForm_Edit_NotificationEmail.AttachUploadedDocs");
  46. chkCustomLayout.Text = GetString("BizForm_Edit_NotificationEmail.CustomLayout");
  47. imgSave.ImageUrl = GetImageUrl("CMSModules/CMS_Content/EditMenu/save.png");
  48. mSave = GetString("general.save");
  49. // Initialize HTML editor
  50. InitHTMLEditor();
  51. BizFormInfo bfi = BizFormInfoProvider.GetBizFormInfo(formId);
  52. EditedObject = bfi;
  53. if (!RequestHelper.IsPostBack())
  54. {
  55. if (bfi != null)
  56. {
  57. // Get bizform class object
  58. formClassObj = DataClassInfoProvider.GetDataClass(bfi.FormClassID);
  59. // Fill list of available fields
  60. FillFieldsList();
  61. // Load email from/to address and email subject
  62. txtFromEmail.Text = ValidationHelper.GetString(bfi.FormSendFromEmail, "");
  63. txtToEmail.Text = ValidationHelper.GetString(bfi.FormSendToEmail, "");
  64. txtSubject.Text = ValidationHelper.GetString(bfi.FormEmailSubject, "");
  65. chkAttachDocs.Checked = bfi.FormEmailAttachUploadedDocs;
  66. chkSendToEmail.Checked = ((txtFromEmail.Text + txtToEmail.Text) != "");
  67. if (!chkSendToEmail.Checked)
  68. {
  69. txtFromEmail.Enabled = false;
  70. txtToEmail.Enabled = false;
  71. txtSubject.Enabled = false;
  72. chkAttachDocs.Enabled = false;
  73. chkCustomLayout.Visible = false;
  74. pnlCustomLayout.Visible = false;
  75. }
  76. else
  77. {
  78. // Enable or disable form
  79. EnableDisableForm(bfi.FormEmailTemplate);
  80. }
  81. }
  82. else
  83. {
  84. // Disable form by default
  85. EnableDisableForm(null);
  86. }
  87. }
  88. }
  89. protected void Page_PreRender(Object sender, EventArgs e)
  90. {
  91. if (!IsClientScriptRegistered())
  92. {
  93. if (!pnlCustomLayout.Visible && IsLayoutSet)
  94. {
  95. RegisterSaveDocumentWithDeleteConfirmation();
  96. }
  97. else
  98. {
  99. RegisterSaveDocument();
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// Register client script block for document saving via 'Ctrl+S'.
  105. /// </summary>
  106. protected void RegisterSaveDocument()
  107. {
  108. ScriptHelper.RegisterSaveShortcut(lnkSave, null, false);
  109. }
  110. /// <summary>
  111. /// Register client script block for document saving via 'Ctrl+S' with layout delete confirmation.
  112. /// </summary>
  113. protected void RegisterSaveDocumentWithDeleteConfirmation()
  114. {
  115. string saveScript = string.Format(@"function SaveDocument() {{ if (ConfirmDelete()) {{ {0} }} }}",
  116. this.Page.ClientScript.GetPostBackClientHyperlink(lnkSave, null));
  117. ScriptHelper.RegisterSaveShortcut(this, saveScript);
  118. }
  119. /// <summary>
  120. /// Returns true if "EditShortcuts" client script block is registered.
  121. /// </summary>
  122. protected bool IsClientScriptRegistered()
  123. {
  124. return ScriptHelper.IsClientScriptBlockRegistered(ScriptHelper.SAVE_DOCUMENT_SCRIPT_KEY);
  125. }
  126. /// <summary>
  127. /// On chkSendToEmail checked event handler.
  128. /// </summary>
  129. protected void chkSendToEmail_CheckedChanged(object sender, EventArgs e)
  130. {
  131. txtFromEmail.Enabled = chkSendToEmail.Checked;
  132. txtToEmail.Enabled = chkSendToEmail.Checked;
  133. txtSubject.Enabled = chkSendToEmail.Checked;
  134. chkAttachDocs.Enabled = chkSendToEmail.Checked;
  135. if (chkSendToEmail.Checked)
  136. {
  137. chkCustomLayout.Visible = true;
  138. if (chkCustomLayout.Checked)
  139. {
  140. pnlCustomLayout.Visible = true;
  141. lnkSave.OnClientClick = "";
  142. // Reload HTML editor content
  143. BizFormInfo bfi = BizFormInfoProvider.GetBizFormInfo(formId);
  144. if (bfi != null && bfi.FormEmailTemplate != null)
  145. {
  146. htmlEditor.ResolvedValue = bfi.FormEmailTemplate;
  147. }
  148. }
  149. }
  150. else
  151. {
  152. chkCustomLayout.Visible = false;
  153. pnlCustomLayout.Visible = false;
  154. // Add delete confirmation
  155. if (IsLayoutSet)
  156. {
  157. lnkSave.OnClientClick = "return ConfirmDelete();";
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Custom layout checkbox checked changed.
  163. /// </summary>
  164. protected void chkCustomLayout_CheckedChanged(object sender, EventArgs e)
  165. {
  166. pnlCustomLayout.Visible = !pnlCustomLayout.Visible;
  167. // Add delete confirmation
  168. if (!chkCustomLayout.Checked && IsLayoutSet)
  169. {
  170. lnkSave.OnClientClick = "return ConfirmDelete();";
  171. }
  172. // Remove delete confirmation and reload HTML editor content
  173. else if (chkCustomLayout.Checked)
  174. {
  175. lnkSave.OnClientClick = "";
  176. BizFormInfo bfi = BizFormInfoProvider.GetBizFormInfo(formId);
  177. if (bfi != null && bfi.FormEmailTemplate != null)
  178. {
  179. htmlEditor.ResolvedValue = bfi.FormEmailTemplate;
  180. }
  181. }
  182. }
  183. /// <summary>
  184. /// Fills list of available fields.
  185. /// </summary>
  186. private void FillFieldsList()
  187. {
  188. FormInfo fi = null;
  189. FormFieldInfo[] fields = null;
  190. if (formClassObj != null)
  191. {
  192. // Load form definition and get visible fields
  193. fi = FormHelper.GetFormInfo(formClassObj.ClassName, false);
  194. fields = fi.GetFields(true, true);
  195. lstAvailableFields.Items.Clear();
  196. if (fields != null)
  197. {
  198. // Add visible fields to the list
  199. foreach (FormFieldInfo ffi in fields)
  200. {
  201. lstAvailableFields.Items.Add(new ListItem(ffi.Name, ffi.Name));
  202. }
  203. }
  204. lstAvailableFields.SelectedIndex = 0;
  205. }
  206. }
  207. /// <summary>
  208. /// Enables or disables form according to form layout is defined or not.
  209. /// </summary>
  210. protected void EnableDisableForm(string formLayout)
  211. {
  212. // if form layout is set
  213. if (formLayout != null)
  214. {
  215. //enable form editing
  216. chkCustomLayout.Checked = true;
  217. pnlCustomLayout.Visible = true;
  218. // set text (form layout) to the editable window of the HTML editor
  219. htmlEditor.ResolvedValue = formLayout;
  220. // save info to viewstate
  221. IsLayoutSet = true;
  222. lnkSave.OnClientClick = "";
  223. }
  224. else
  225. {
  226. // form is not enabled by default
  227. chkCustomLayout.Checked = false;
  228. pnlCustomLayout.Visible = false;
  229. htmlEditor.Value = "";
  230. // save info to viewstate
  231. IsLayoutSet = false;
  232. lnkSave.OnClientClick = "";
  233. }
  234. }
  235. /// <summary>
  236. /// Displays specified info message and hide error message.
  237. /// </summary>
  238. /// <param name="infoMessage">Info message to display</param>
  239. protected void DisplayInfoMessage(string infoMessage)
  240. {
  241. lblError.Visible = false;
  242. lblInfo.Visible = true;
  243. lblInfo.Text = infoMessage;
  244. }
  245. /// <summary>
  246. /// Displays specified error message and hide info message.
  247. /// </summary>
  248. /// <param name="errorMessage">Error message to display</param>
  249. protected void DisplayErrorMessage(string errorMessage)
  250. {
  251. lblInfo.Visible = false;
  252. lblError.Visible = true;
  253. lblError.Text = errorMessage;
  254. }
  255. /// <summary>
  256. /// Initializes HTML editor's settings.
  257. /// </summary>
  258. protected void InitHTMLEditor()
  259. {
  260. htmlEditor.AutoDetectLanguage = false;
  261. htmlEditor.DefaultLanguage = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
  262. htmlEditor.MediaDialogConfig.UseFullURL = true;
  263. htmlEditor.LinkDialogConfig.UseFullURL = true;
  264. htmlEditor.QuickInsertConfig.UseFullURL = true;
  265. }
  266. /// <summary>
  267. /// Save button is clicked.
  268. /// </summary>
  269. protected void lnkSave_Click(object sender, EventArgs e)
  270. {
  271. // Check 'EditForm' permission
  272. if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.form", "EditForm"))
  273. {
  274. RedirectToCMSDeskAccessDenied("cms.form", "EditForm");
  275. }
  276. string errorMessage = "";
  277. BizFormInfo bfi = BizFormInfoProvider.GetBizFormInfo(formId);
  278. if (bfi != null)
  279. {
  280. if (chkSendToEmail.Checked)
  281. {
  282. // Validate form
  283. errorMessage = new Validator().NotEmpty(txtFromEmail.Text, GetString("BizFormGeneral.EmptyFromEmail"))
  284. .NotEmpty(txtToEmail.Text, GetString("BizFormGeneral.EmptyToEmail"))
  285. .NotEmpty(txtSubject.Text, GetString("BizFormGeneral.EmptyEmailSubject")).Result;
  286. // Check if to e-mail contains macro expression or e-mails separated by semicolon
  287. if (string.IsNullOrEmpty(errorMessage) && !MacroResolver.ContainsMacro(txtToEmail.Text.Trim()) && !ValidationHelper.AreEmails(txtToEmail.Text.Trim()))
  288. {
  289. errorMessage = GetString("BizFormGeneral.EmptyToEmail");
  290. }
  291. // Check if from e-mail contains macro expression or e-mails separated by semicolon
  292. if (string.IsNullOrEmpty(errorMessage) && !MacroResolver.ContainsMacro(txtFromEmail.Text.Trim()) && !ValidationHelper.IsEmail(txtFromEmail.Text.Trim()))
  293. {
  294. errorMessage = GetString("BizFormGeneral.EmptyFromEmail");
  295. }
  296. if (string.IsNullOrEmpty(errorMessage))
  297. {
  298. bfi.FormSendFromEmail = txtFromEmail.Text.Trim();
  299. bfi.FormSendToEmail = txtToEmail.Text.Trim();
  300. bfi.FormEmailSubject = txtSubject.Text.Trim();
  301. bfi.FormEmailAttachUploadedDocs = chkAttachDocs.Checked;
  302. if (chkCustomLayout.Checked)
  303. {
  304. bfi.FormEmailTemplate = htmlEditor.ResolvedValue.Trim();
  305. }
  306. else
  307. {
  308. bfi.FormEmailTemplate = null;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. bfi.FormSendFromEmail = null;
  315. bfi.FormSendToEmail = null;
  316. bfi.FormEmailSubject = null;
  317. bfi.FormEmailTemplate = null;
  318. txtToEmail.Text = "";
  319. txtFromEmail.Text = "";
  320. txtSubject.Text = "";
  321. chkAttachDocs.Checked = true;
  322. htmlEditor.ResolvedValue = "";
  323. }
  324. if (errorMessage == "")
  325. {
  326. try
  327. {
  328. BizFormInfoProvider.SetBizFormInfo(bfi);
  329. DisplayInfoMessage(GetString("General.ChangesSaved"));
  330. EnableDisableForm(bfi.FormEmailTemplate);
  331. }
  332. catch (Exception ex)
  333. {
  334. errorMessage = ex.Message;
  335. }
  336. }
  337. if (errorMessage != "")
  338. {
  339. DisplayErrorMessage(errorMessage);
  340. }
  341. }
  342. }
  343. #endregion
  344. }