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