PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/BaliEnterpriseSystems/BaliEnterpriseSystems/BestMassEMail.aspx.cs

https://github.com/sirivedula/BEST
C# | 351 lines | 321 code | 22 blank | 8 comment | 46 complexity | 0494bd539240b197350ca541ae8fab64 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Text;
  8. using BaliEnterpriseSystems.BestObjects;
  9. using System.Collections.Specialized;
  10. using System.Data.OleDb;
  11. using System.Data;
  12. namespace BaliEnterpriseSystems
  13. {
  14. public partial class BestMassEMail : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. }
  19. protected void Page_LoadComplete(object sender, EventArgs e)
  20. {
  21. if (HttpContext.Current.Session["CurrentUser"] == null)
  22. {
  23. Response.Redirect("Logout.aspx");
  24. }
  25. ltrSubMenu.Text = UtilMenu.UtilityMenu("massemail");
  26. if (!Utils.User.UserRoleByName("Mass EMail").allowView)
  27. {
  28. ltrValidateMsg.Text = "You do not have rights to view.";
  29. return;
  30. }
  31. string ms = Request.QueryString["ms"];
  32. ltrMScript.Text = Utils.MenuSelectScript(ms);
  33. string selWho = Request.Form["selWho"];
  34. string emailDesc = Request.Form["emailDesc"];
  35. string selFilter = Request.Form["selFilter"]??"";
  36. string filtervalue = Request.Form["filtervalue"];
  37. if (string.IsNullOrEmpty(selWho)) selWho = "Student";
  38. selWhoOpts.Text = "<option value=\"Student\" " + (selWho.Equals("Student")?"Selected" : "") + " >Student</option><option value=\"Tutor\" " + (selWho.Equals("Tutor")?"Selected" : "") + " >Tutor</option>";
  39. ltrFilters.Text = "<option value=\"\" " + (selFilter.Equals("") ? "Selected" : "") + " ></option><option value=\"School\" " + (selFilter.Equals("School") ? "Selected" : "") + " >School</option><option value=\"Grade\" " + (selFilter.Equals("Grade") ? "Selected" : "") + " >Grade</option>";
  40. FillEMailTemplates();
  41. ltrGrid.Text = EMailTargetHTML(selWho, selFilter, filtervalue);
  42. string fromdate = Request.Form["fromdate"] ?? "";
  43. string todate = Request.Form["todate"] ?? "";
  44. DateTime today = DateTime.Today;
  45. int curweek = ((int)today.DayOfWeek) * -1;
  46. if (string.IsNullOrEmpty(fromdate))
  47. {
  48. fromdate = today.AddDays(curweek).ToString("MM/dd/yyyy");
  49. }
  50. if (string.IsNullOrEmpty(todate))
  51. {
  52. todate = today.AddDays(curweek).AddDays(7).ToString("MM/dd/yyyy");
  53. }
  54. ltrLoadScript.Text = "<script type=\"text/javascript\">$(document).ready(function(){ $('#filtervalue').val('" + Utils.EnquoteJS(filtervalue) + @"');
  55. $('#fromdate').val('" + fromdate + "'); $('#todate').val('" + todate + @"');
  56. }); </script>";
  57. string sendClick = Request.Form["sendEMailClicked"]??"";
  58. if(sendClick.Equals("1"))
  59. {
  60. SendEMails();
  61. }
  62. }
  63. private void SendEMails()
  64. {
  65. /* Send EMails */
  66. string strRows = Request.Form["rowcount"];
  67. int rowCount;
  68. int.TryParse(strRows, out rowCount);
  69. List<BestField> bparams = new List<BestField>();
  70. string paramQs = "";
  71. for (int i = 0; i < rowCount; i++)
  72. {
  73. string chkStr = Request.Form["chk_" + i.ToString()]??"";
  74. if (chkStr.Equals("on"))
  75. {
  76. BestField bsf = new BestField() { fieldName = "guidfield", paramOledbType = System.Data.OleDb.OleDbType.Guid, fieldType = "System.Guid" };
  77. bsf.fieldValue = Request.Form["guid" + i.ToString()];
  78. bparams.Add(bsf);
  79. paramQs += "?,";
  80. }
  81. }
  82. paramQs = (paramQs.Length > 0) ? paramQs.Substring(0, paramQs.Length - 1) : "";
  83. string selWho = Request.Form["selWho"];
  84. if (bparams.Count > 0)
  85. {
  86. Dictionary<string, BMails> nvc = new Dictionary<string, BMails>();
  87. string emailBody = getEMailBody();
  88. if (selWho.Equals("Student"))
  89. {
  90. BestStudents bs = new BestStudents();
  91. bs.LoadRows("guidfield in (" + paramQs + ")", bparams);
  92. for (int i = 0; i < bs.TableRows.Count; i++)
  93. {
  94. bs.currentRowId = i;
  95. BMails bm = new BMails();
  96. bm.emailbody = ReplaceParams(bs, emailBody);
  97. bm.emailToName = bs.firstName + " " + bs.lastName;
  98. nvc.Add(bs.emailId, bm);
  99. }
  100. }
  101. else if (selWho.Equals("Tutor"))
  102. {
  103. BestTutors bt = new BestTutors();
  104. bt.LoadRows("guidfield in (" + paramQs + ")", bparams);
  105. for (int i = 0; i < bt.TableRows.Count; i++)
  106. {
  107. bt.currentRowId = i;
  108. BMails bm = new BMails();
  109. bm.emailbody = ReplaceParams(bt, emailBody);
  110. bm.emailToName = bt.firstName + " " + bt.lastName;
  111. nvc.Add(bt.emailId, bm);
  112. }
  113. }
  114. string emailSubject = Request.Form["emailSubject"] ?? "";
  115. string message = "Send EMail To:\n";
  116. foreach (string key in nvc.Keys)
  117. {
  118. BMails bm = (BMails)nvc[key];
  119. if (Utils.User.emailUtil.Send(key, emailSubject, bm.emailbody, bm.emailToName))
  120. {
  121. message += key + " " + bm.emailToName + "\n";
  122. }
  123. }
  124. ltrValidateMsg.Text = Utils.InfoMessage(message);
  125. }
  126. }
  127. public class BMails
  128. {
  129. public string emailbody { get; set; }
  130. public string emailToName { get; set; }
  131. }
  132. private string getEMailBody()
  133. {
  134. string result = "";
  135. string emailTemp = Request.Form["selEmailTemplate"];
  136. string emailDesc = Request.Form["emailDesc"];
  137. if (!string.IsNullOrEmpty(emailTemp))
  138. {
  139. BestField bsf = new BestField() { fieldName = "emailtemp", fieldType = "System.String", paramOledbType = System.Data.OleDb.OleDbType.VarChar, fieldSize = 50 };
  140. bsf.fieldValue = emailTemp;
  141. List<BestField> bparam = new List<BestField>();
  142. bparam.Add(bsf);
  143. BestEMailTemplate bet = new BestEMailTemplate();
  144. bet.LoadRows("emialTemplateName = ?", bparam);
  145. if (bet.HasRows)
  146. {
  147. result = bet.TableRows[0].Fields["templateDescription"].fieldValue;
  148. }
  149. }
  150. else if (!string.IsNullOrEmpty(emailDesc))
  151. {
  152. result = emailDesc;
  153. }
  154. return result;
  155. }
  156. private string ReplaceParams(BestTable bsrow, string emailbody)
  157. {
  158. string result = emailbody;
  159. string name = bsrow.CurrentRow.Fields["firstName"].fieldValue + " " + bsrow.CurrentRow.Fields["lastName"].fieldValue;
  160. //{tutor}
  161. //{student}
  162. result = result.Replace("{tutor}", name);
  163. result = result.Replace("{student}", name);
  164. string fromdate = Request.Form["fromdate"] ?? "";
  165. string todate = Request.Form["todate"] ?? "";
  166. if (!string.IsNullOrEmpty(fromdate))
  167. {
  168. //{WeekStartDate}
  169. result = result.Replace("{WeekStartDate}", fromdate);
  170. }
  171. if (!string.IsNullOrEmpty(todate))
  172. {
  173. //{WeekEndDate}
  174. result = result.Replace("{WeekEndDate}", todate);
  175. }
  176. //{WeeklySchedule}
  177. if (emailbody.IndexOf("{WeeklySchedule}") > 0)
  178. {
  179. string tsql = "select Date, [Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], stud1name, stud2name, stud3name, stud4name, stud5name from VBestSchedules where ";
  180. string guidstr = bsrow.CurrentRow.Fields["guidfield"].fieldValue;
  181. if (bsrow is BestStudents)
  182. {
  183. tsql += "(stuGuid1='" + guidstr + "' or stuGuid2='" + guidstr + "' or stuGuid3='" + guidstr + "' or stuGuid4='" + guidstr + "' or stuGuid5='" + guidstr + "')";
  184. }
  185. else if (bsrow is BestTutors)
  186. {
  187. tsql += " tutGuid='" + guidstr + "'";
  188. }
  189. tsql += @" and convert(datetime,date) between '" + fromdate + @"' and '" + todate + @"' order by convert(datetime, date), convert(numeric, schfrom)";
  190. StringBuilder sbSchd = new StringBuilder();
  191. BestDatabase db = new BestDatabase();
  192. OleDbCommand myCmd = db.dbCmd;
  193. myCmd.CommandText = tsql;
  194. OleDbDataReader tblReader = myCmd.ExecuteReader(CommandBehavior.KeyInfo);
  195. //Retrieve column schema into a DataTable.
  196. DataTable schemaTable = tblReader.GetSchemaTable();
  197. sbSchd.Append("<table cellpadding=\"0\" style=\"padding:1px;font-family:Tahoma;font-size:12px;border:1px solid #999;background-color:#A11117\"><tr>");
  198. //For each field in the table...
  199. int fieldCnt = 0;
  200. foreach (DataRow myField in schemaTable.Rows)
  201. {
  202. fieldCnt++; if (fieldCnt > 8) break;
  203. sbSchd.Append("<th style=\"color:#000;background-color:#E9BA15;padding:2px;\">" + myField[0] + "</th>");
  204. }
  205. sbSchd.Append("</tr>");
  206. int cnt = 0;
  207. while (tblReader.Read())
  208. {
  209. if (bsrow is BestStudents)
  210. {
  211. name = "";
  212. }
  213. else
  214. {
  215. name = " " + tblReader[8].ToString();
  216. name += !string.IsNullOrEmpty(tblReader[9].ToString()) ? ", " + tblReader[9].ToString() : "";
  217. name += !string.IsNullOrEmpty(tblReader[10].ToString()) ? ", " + tblReader[10].ToString() : "";
  218. name += !string.IsNullOrEmpty(tblReader[11].ToString()) ? ", " + tblReader[11].ToString() : "";
  219. name += !string.IsNullOrEmpty(tblReader[12].ToString()) ? ", " + tblReader[12].ToString() : "";
  220. }
  221. string trclass = (cnt % 2 == 0) ? "" : "trordd";
  222. sbSchd.Append("<tr style=\"font-family:Tahoma;color:#FFF;font-size:12px;\"><td>" + (tblReader[0].ToString()) + "</td>");
  223. sbSchd.Append("<td>" + Utils.ToTimeFormat(tblReader[1].ToString()) + (!string.IsNullOrEmpty(tblReader[1].ToString()) ? name : "") + "</td>");
  224. sbSchd.Append("<td>" + Utils.ToTimeFormat(tblReader[2].ToString()) + (!string.IsNullOrEmpty(tblReader[2].ToString()) ? name : "") + "</td>");
  225. sbSchd.Append("<td>" + Utils.ToTimeFormat(tblReader[3].ToString()) + (!string.IsNullOrEmpty(tblReader[3].ToString()) ? name : "") + "</td>");
  226. sbSchd.Append("<td>" + Utils.ToTimeFormat(tblReader[4].ToString()) + (!string.IsNullOrEmpty(tblReader[4].ToString()) ? name : "") + "</td>");
  227. sbSchd.Append("<td>" + Utils.ToTimeFormat(tblReader[5].ToString()) + (!string.IsNullOrEmpty(tblReader[5].ToString()) ? name : "") + "</td>");
  228. sbSchd.Append("<td>" + Utils.ToTimeFormat(tblReader[6].ToString()) + (!string.IsNullOrEmpty(tblReader[6].ToString()) ? name : "") + "</td>");
  229. sbSchd.Append("<td>" + Utils.ToTimeFormat(tblReader[7].ToString()) + (!string.IsNullOrEmpty(tblReader[7].ToString()) ? name : "") + "</td></tr>");
  230. cnt++;
  231. }
  232. tblReader.Close();
  233. sbSchd.Append("</table>");
  234. result = result.Replace("{WeeklySchedule}", sbSchd.ToString());
  235. }
  236. result = result.Replace("\n\r", "<br />");
  237. return result;
  238. }
  239. private void FillEMailTemplates()
  240. {
  241. string emailTemp = Request.Form["selEmailTemplate"]??"";
  242. StringBuilder opts = new StringBuilder();
  243. opts.Append("<option value=\"\"></option>");
  244. BestEMailTemplate bem = new BestEMailTemplate();
  245. bem.LoadRows();
  246. for (int row = 0; row < bem.TableRows.Count; row++)
  247. {
  248. bem.currentRowId = row;
  249. opts.Append("<option value=\"" + HttpUtility.HtmlEncode(bem.emialTemplateName) + "\"");
  250. if (bem.emialTemplateName.Equals(emailTemp))
  251. {
  252. opts.Append(" selected=\"selected\" ");
  253. }
  254. opts.Append(">" + HttpUtility.HtmlEncode(bem.emialTemplateName) + "</option>");
  255. }
  256. ltrEMailTempOpts.Text = opts.ToString();
  257. }
  258. private string EMailTargetHTML(string studTutor, string filter, string filterval)
  259. {
  260. StringBuilder result = new StringBuilder();
  261. result.Append("<table id=\"available\" class=\"bestgrid\" cellspacing=\"0\" cellpadding=\"0\" style=\"width:80%;\"><tr><th><input type=\"checkbox\" id=\"checkAll\" onclick=\"ToggleCheck();\" /></th><th>Id</th><th>First Name</th><th>Last Name</th><th>EMail Address</th></tr>");
  262. int rowcount = 0;
  263. if (studTutor.Equals("Student"))
  264. {
  265. BestStudents bs = new BestStudents();
  266. if (!string.IsNullOrEmpty(filter) && !string.IsNullOrEmpty(filterval))
  267. {
  268. BestField bfld = new BestField() { fieldName = "filterparam", paramOledbType = System.Data.OleDb.OleDbType.VarChar, fieldSize = 50, fieldType = "System.String" };
  269. bfld.fieldValue = filterval;
  270. List<BestField> bparam = new List<BestField>();
  271. bparam.Add(bfld);
  272. bparam.Add(Utils.User.CenterIdField);
  273. if (filter.Equals("School"))
  274. {
  275. bs.LoadRows("school = ? and CenterId=?", bparam,"firstname,lastname");
  276. }
  277. else if (filter.Equals("Grade"))
  278. {
  279. bs.LoadRows("grade = ? and CenterId=?", bparam, "firstname,lastname");
  280. }
  281. }
  282. else
  283. {
  284. bs.LoadRows("CenterId=?",Utils.User.CIdParam,"firstname, lastname");
  285. }
  286. rowcount = bs.TableRows.Count;
  287. for (int row = 0; row < bs.TableRows.Count; row++)
  288. {
  289. bs.currentRowId = row;
  290. string trclass = (row % 2 == 0) ? "" : " class=\"gridodd\" ";
  291. result.Append("<tr" + trclass + ">");
  292. if (!string.IsNullOrEmpty(bs.emailId))
  293. {
  294. result.Append("<td><input type=\"checkbox\" id=\"chk_" + row.ToString() + "\" name=\"chk_" + row.ToString() + "\" /><input type=\"hidden\" id=\"guid" + row.ToString() + "\" name=\"guid" + row.ToString() + "\" value=\"" + HttpUtility.HtmlEncode(bs.guidfield.ToString()) + "\" /></td>");
  295. }
  296. else
  297. {
  298. result.Append("<td>&nbsp;</td>");
  299. }
  300. result.Append("<td>" + HttpUtility.HtmlEncode(bs.StudentId) + "</td><td>" + HttpUtility.HtmlEncode(bs.firstName) + "</td><td>" + HttpUtility.HtmlEncode(bs.lastName) + "</td><td>" + HttpUtility.HtmlEncode(bs.emailId) + "</td></tr>");
  301. }
  302. }
  303. else if (studTutor.Equals("Tutor"))
  304. {
  305. BestTutors bt = new BestTutors();
  306. bt.LoadRows("CenterId=?", Utils.User.CIdParam, "firstname,lastname");
  307. rowcount = bt.TableRows.Count;
  308. for (int row = 0; row < bt.TableRows.Count; row++)
  309. {
  310. bt.currentRowId = row;
  311. string trclass = (row % 2 == 0) ? "" : " class=\"gridodd\" ";
  312. result.Append("<tr" + trclass + ">");
  313. if (!string.IsNullOrEmpty(bt.emailId))
  314. {
  315. result.Append("<td><input type=\"checkbox\" id=\"chk_" + row.ToString() + "\" name=\"chk_" + row.ToString() + "\" /><input type=\"hidden\" id=\"guid" + row.ToString() + "\" name=\"guid" + row.ToString() + "\" value=\"" + HttpUtility.HtmlEncode(bt.guidfield.ToString()) + "\" /></td>");
  316. }
  317. else
  318. {
  319. result.Append("<td>&nbsp;</td>");
  320. }
  321. result.Append("<td>" + HttpUtility.HtmlEncode(bt.TutorId) + "</td><td>" + HttpUtility.HtmlEncode(bt.firstName) + "</td><td>" + HttpUtility.HtmlEncode(bt.lastName) + "</td><td>" + HttpUtility.HtmlEncode(bt.emailId) + "</td></tr>");
  322. }
  323. }
  324. result.Append("<input type=\"hidden\" id=\"rowcount\" name=\"rowcount\" value=\"" + rowcount.ToString() + "\" />");
  325. result.Append("</table>");
  326. return result.ToString();
  327. }
  328. }
  329. }