You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
13 KiB

  1. using Swift.API.Common;
  2. using Swift.API.ThirdPartyApiServices;
  3. using Swift.DAL.MobileDao;
  4. using Swift.DAL.SwiftDAL;
  5. using Swift.web.Library;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Web;
  12. using System.Web.Script.Serialization;
  13. namespace Swift.web.MobileRemit.Admin.BroadcastMessage
  14. {
  15. public partial class ManageBroadCast : System.Web.UI.Page
  16. {
  17. private string ViewFunctionId = "10112600";
  18. private string AddEditFunctionId = "10112610";
  19. private readonly StaticDataDdl _sdd = new StaticDataDdl();
  20. private readonly RemittanceLibrary _sl = new RemittanceLibrary();
  21. private readonly MobileConfigDao _mobileDao = new MobileConfigDao();
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. Authenticate();
  25. var MethodName = Request.Form["MethodName"];
  26. if (!IsPostBack)
  27. {
  28. PopulateDDL();
  29. if (GetRowId() != "")
  30. {
  31. PopulateCustomerGroup("");
  32. PopulateData();
  33. }
  34. }
  35. }
  36. private void PopulateCustomerGroup(string broadCastType)
  37. {
  38. var broadCastId = GetRowId();
  39. DataTable dt = _mobileDao.GetCustomerGroup(GetStatic.GetUser(), broadCastId, broadCastType);
  40. if (dt == null || dt.Rows.Count == 0)
  41. {
  42. TBLCustomerGroupBody.InnerHtml = "<tr><td colspan=\"4\" align=\"center\">No data to display!</td></tr>";
  43. return;
  44. }
  45. int sNo = 1;
  46. StringBuilder sb = new StringBuilder();
  47. for (int i = 0; i < dt.Rows.Count; i++)
  48. {
  49. sb.AppendLine("<tr>");
  50. sb.AppendLine("<td>" + sNo.ToString() + "</td>");
  51. sb.AppendLine("<td><input type=\"checkbox\" name=\"chk_customer_group\" value=\"" + dt.Rows[i]["RowId"] + "\" id=\"chk_group_" + dt.Rows[i]["RowId"] + "\" " + (dt.Rows[i]["IsSelected"].ToString() == "Y" ? "checked=\"true\"" : "") + " />");
  52. sb.AppendLine("&nbsp;&nbsp;<label for=\"chk_group_" + dt.Rows[i]["RowId"] + "\"> " + dt.Rows[i]["GroupName"].ToString() + "</label></td>");
  53. sNo++;
  54. i++;
  55. if (i <= dt.Rows.Count - 1)
  56. {
  57. sb.AppendLine("<td>" + sNo.ToString() + "</td>");
  58. sb.AppendLine("<td><input type=\"checkbox\" name=\"chk_customer_group\" value=\"" + dt.Rows[i]["RowId"] + "\" id=\"chk_group_" + dt.Rows[i]["RowId"] + "\" " + (dt.Rows[i]["IsSelected"].ToString() == "Y" ? "checked=\"true\"" : "") + " />");
  59. sb.AppendLine("&nbsp;&nbsp;<label for=\"chk_group_" + dt.Rows[i]["RowId"] + "\"> " + dt.Rows[i]["GroupName"].ToString() + "</label></td>");
  60. }
  61. sNo++;
  62. sb.AppendLine("</tr>");
  63. }
  64. TBLCustomerGroupBody.InnerHtml = sb.ToString();
  65. txtSearchData.Value = "";
  66. }
  67. private void PopulateData()
  68. {
  69. DataRow dr = _mobileDao.GetBroadCastData(GetStatic.GetUser(), GetRowId());
  70. if (dr == null)
  71. {
  72. return;
  73. }
  74. DDLBroadCastType.SelectedValue = dr["BroadcastTypeId"].ToString();
  75. TXTTitle.Text = dr["Title"].ToString();
  76. TXTBody.Text = HttpUtility.HtmlDecode(dr["Body"].ToString());
  77. TXTImageURL.Text = dr["ImageURL"].ToString();
  78. TXTScheduleDate.Text = dr["ScheduleDate"].ToString();
  79. notificationType.Value = dr["notificationType"].ToString();
  80. msgType.Value = dr["messageType"].ToString();
  81. recipientDiv.Visible = false;
  82. }
  83. private void Authenticate()
  84. {
  85. _sl.CheckAuthentication(ViewFunctionId);
  86. btnSave.Enabled = _sl.HasRight(AddEditFunctionId);
  87. }
  88. private void PopulateDDL()
  89. {
  90. _sdd.SetStaticDdl(ref DDLBroadCastType, "8105", "", "Select");
  91. _sdd.SetDDL(ref ddlCustomerType, "exec proc_sendPageLoadData @flag='search-cust-by'", "VALUE", "TEXT", "", "");
  92. }
  93. protected void btnSave_Click(object sender, EventArgs e)
  94. {
  95. var _dbRes = SaveData();
  96. if (_dbRes != null)
  97. {
  98. GetStatic.SetMessage(_dbRes);
  99. Response.Redirect("BroadcastList.aspx");
  100. }
  101. }
  102. protected DbResult SaveData()
  103. {
  104. var matchString = String.Empty;
  105. var customerGroup = "";
  106. if (customerType.Checked == true)
  107. {
  108. customerGroup = Request.Form["chk_customer_group"];
  109. }
  110. var customerId = txtSearchData.Value;
  111. if (string.IsNullOrEmpty(DDLBroadCastType.SelectedValue))
  112. {
  113. GetStatic.AlertMessage(this, "BroadCast Type can not be empty!");
  114. return null;
  115. }
  116. if (string.IsNullOrEmpty(customerGroup) && customerType.Checked == true && (string.IsNullOrEmpty(GetRowId())))
  117. {
  118. GetStatic.AlertMessage(this, "Customer Group can not be empty!");
  119. return null;
  120. }
  121. if (string.IsNullOrEmpty(customerId) && customerType.Checked == false && (string.IsNullOrEmpty(GetRowId())))
  122. {
  123. GetStatic.AlertMessage(this, "Customer can not be empty!");
  124. return null;
  125. }
  126. if (string.IsNullOrEmpty(TXTTitle.Text))
  127. {
  128. GetStatic.AlertMessage(this, "BroadCast Title can not be empty!");
  129. return null;
  130. }
  131. if (string.IsNullOrEmpty(HttpUtility.HtmlEncode(TXTBody.Text)))
  132. {
  133. GetStatic.AlertMessage(this, "BroadCast Body can not be empty!");
  134. return null;
  135. }
  136. var text = Request.Unvalidated["TXTBody"];
  137. if (DDLBroadCastType.SelectedValue == "11431" || DDLBroadCastType.SelectedValue == "11429") //SMS/PUSH_NOTIFICATION
  138. {
  139. matchString = Regex.Replace(text, @"<[^>]*>", String.Empty).Replace(@"&nbsp;", String.Empty).Replace(@"<\/?[^>] + (>|$)", String.Empty);
  140. matchString = matchString.Replace("<\\S*?>", "");
  141. }
  142. else
  143. {
  144. matchString = text;
  145. }
  146. DbResult _dbRes = _mobileDao.SaveBroadCast(GetStatic.GetUser(), GetRowId(), DDLBroadCastType.SelectedValue, customerGroup, TXTTitle.Text
  147. , matchString, TXTImageURL.Text, TXTScheduleDate.Text, customerId, notificationType.Value, msgType.Value);
  148. return _dbRes;
  149. }
  150. protected string GetRowId()
  151. {
  152. return GetStatic.ReadQueryString("RowId", "");
  153. }
  154. protected void DDLBroadCastType_SelectedIndexChanged(object sender, EventArgs e)
  155. {
  156. if (string.IsNullOrEmpty(DDLBroadCastType.SelectedValue))
  157. {
  158. TBLCustomerGroupBody.InnerHtml = "<tr><td colspan=\"4\" align=\"center\">No data to display!</td></tr>";
  159. return;
  160. }
  161. PopulateCustomerGroup(DDLBroadCastType.SelectedValue);
  162. }
  163. protected void btnSaveAndSend_Click(object sender, EventArgs e)
  164. {
  165. var _dbRes = SaveData();
  166. if (_dbRes != null)
  167. {
  168. if (_dbRes.ErrorCode == "0")
  169. {
  170. JsonResponse res = SendNotificationTPAPI(_dbRes.Id);
  171. if (res.ResponseCode == "0")
  172. {
  173. string sql = $@"UPDATE BroadCastNotification SET isSent = 1, ModifiedDate=GETDATE(), ModifiedBy='{GetStatic.GetUser()}' WHERE rowId={_dbRes.Id}";
  174. StringBuilder sb = new StringBuilder(sql);
  175. _mobileDao.ExecuteDataset(sb.ToString());
  176. }
  177. }
  178. GetStatic.SetMessage(_dbRes);
  179. Response.Redirect("BroadcastList.aspx");
  180. }
  181. }
  182. private JsonResponse SendNotificationTPAPI(string rowId)
  183. {
  184. ExchangeRateAPIService NotiService = new ExchangeRateAPIService();
  185. JsonResponse res = new JsonResponse();
  186. var broadCastId = rowId;
  187. DataSet ds = _mobileDao.GetRepushData(GetStatic.GetUser(), broadCastId);
  188. if (ds == null)
  189. {
  190. res.SetResponse("1", "Error re-sending BroadCast!", null);
  191. }
  192. else
  193. {
  194. if (ds.Tables.Count != 2)
  195. {
  196. res.SetResponse("1", "Error re-sending BroadCast!", null);
  197. }
  198. else
  199. {
  200. if (ds.Tables[0] == null)
  201. {
  202. res.SetResponse("1", "Error re-sending BroadCast!", null);
  203. }
  204. else if (ds.Tables[1] == null)
  205. {
  206. res.SetResponse("1", "Error re-sending BoradCast!", null);
  207. }
  208. else
  209. {
  210. string ProcessId = Guid.NewGuid().ToString().Replace("-", "") + ":notiRequest";
  211. SendNotificationRequest notificationRequest = new SendNotificationRequest
  212. {
  213. ProcessId = ProcessId.Substring(ProcessId.Length - 40, 40),
  214. NotificationTypeId = ds.Tables[0].Rows[0]["BroadcastTypeId"].ToString(),
  215. IsBulkNotification = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsBulkNotification"].ToString()),
  216. UserName = GetStatic.GetUser(),
  217. ControlNo = broadCastId
  218. };
  219. var title = ds.Tables[0].Rows[0]["Title"].ToString();
  220. var body = HttpUtility.HtmlDecode(ds.Tables[0].Rows[0]["Body"].ToString());
  221. var navigateURL = ds.Tables[0].Rows[0]["NavigateURL"].ToString();
  222. var clickActivity = ds.Tables[0].Rows[0]["ClickActivity"].ToString();
  223. var messageType = ds.Tables[0].Rows[0]["MessageType"].ToString();
  224. List<AttachmentModel> embadedList = new List<AttachmentModel>();
  225. if (ds.Tables[0].Rows[0]["BroadcastTypeId"].ToString() == "11430" || ds.Tables[0].Rows[0]["BroadcastTypeId"].ToString() == "EMAIL") //Email
  226. {
  227. var matchString = Regex.Matches(body, "<img.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase);
  228. foreach (Match match in matchString)
  229. {
  230. Match result = Regex.Match(match.Groups[1].Value, @"data:.*?(?=;)");
  231. string fileName = Guid.NewGuid().ToString();
  232. embadedList.Add(new AttachmentModel
  233. {
  234. FileName = fileName,
  235. Byte64String = match.Groups[1].Value.Replace(result.Value + ";base64,", ""),
  236. FileType = result.Value.Split(':')[1]
  237. });
  238. body = body.Replace(match.Groups[1].Value, "cid:" + fileName);
  239. }
  240. }
  241. List<RecipientViewModel> recipients = new List<RecipientViewModel>();
  242. foreach (DataRow item1 in ds.Tables[1].Rows)
  243. {
  244. var receipent = new RecipientViewModel();
  245. receipent.Address = item1["SendTo"].ToString();
  246. receipent.DeviceType = item1["DeviceType"].ToString();
  247. receipent.NotificationContent = new NotificationDTO
  248. {
  249. Title = title,
  250. Body = body,
  251. NavigateURL = navigateURL,
  252. ClickActivity = clickActivity,
  253. MessageType = messageType
  254. };
  255. receipent.NotificationContent.EmbeddedResources = embadedList;
  256. recipients.Add(receipent);
  257. }
  258. notificationRequest.Recipients = recipients;
  259. //call thirdparty
  260. res = NotiService.PushNotification(notificationRequest);
  261. }
  262. }
  263. }
  264. return res;
  265. }
  266. public static string DataTableToJson(DataTable table)
  267. {
  268. if (table == null)
  269. return "";
  270. var list = new List<Dictionary<string, object>>();
  271. foreach (DataRow row in table.Rows)
  272. {
  273. var dict = new Dictionary<string, object>();
  274. foreach (DataColumn col in table.Columns)
  275. {
  276. dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col];
  277. }
  278. list.Add(dict);
  279. }
  280. var serializer = new JavaScriptSerializer();
  281. string json = serializer.Serialize(list);
  282. return json;
  283. }
  284. }
  285. }