using Swift.API.Common; using Swift.API.ThirdPartyApiServices; using Swift.DAL.MobileDao; using Swift.DAL.SwiftDAL; using Swift.web.Library; using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.Script.Serialization; namespace Swift.web.MobileRemit.Admin.BroadcastMessage { public partial class ManageBroadCast : System.Web.UI.Page { private string ViewFunctionId = "10112600"; private string AddEditFunctionId = "10112610"; private readonly StaticDataDdl _sdd = new StaticDataDdl(); private readonly RemittanceLibrary _sl = new RemittanceLibrary(); private readonly MobileConfigDao _mobileDao = new MobileConfigDao(); protected void Page_Load(object sender, EventArgs e) { Authenticate(); var MethodName = Request.Form["MethodName"]; if (!IsPostBack) { PopulateDDL(); if (GetRowId() != "") { PopulateCustomerGroup(""); PopulateData(); } } } private void PopulateCustomerGroup(string broadCastType) { var broadCastId = GetRowId(); DataTable dt = _mobileDao.GetCustomerGroup(GetStatic.GetUser(), broadCastId, broadCastType); if (dt == null || dt.Rows.Count == 0) { TBLCustomerGroupBody.InnerHtml = "No data to display!"; return; } int sNo = 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < dt.Rows.Count; i++) { sb.AppendLine(""); sb.AppendLine("" + sNo.ToString() + ""); sb.AppendLine(""); sb.AppendLine("  "); sNo++; i++; if (i <= dt.Rows.Count - 1) { sb.AppendLine("" + sNo.ToString() + ""); sb.AppendLine(""); sb.AppendLine("  "); } sNo++; sb.AppendLine(""); } TBLCustomerGroupBody.InnerHtml = sb.ToString(); txtSearchData.Value = ""; } private void PopulateData() { DataRow dr = _mobileDao.GetBroadCastData(GetStatic.GetUser(), GetRowId()); if (dr == null) { return; } DDLBroadCastType.SelectedValue = dr["BroadcastTypeId"].ToString(); TXTTitle.Text = dr["Title"].ToString(); TXTBody.Text = HttpUtility.HtmlDecode(dr["Body"].ToString()); TXTImageURL.Text = dr["ImageURL"].ToString(); TXTScheduleDate.Text = dr["ScheduleDate"].ToString(); notificationType.Value = dr["notificationType"].ToString(); msgType.Value = dr["messageType"].ToString(); recipientDiv.Visible = false; } private void Authenticate() { _sl.CheckAuthentication(ViewFunctionId); btnSave.Enabled = _sl.HasRight(AddEditFunctionId); } private void PopulateDDL() { _sdd.SetStaticDdl(ref DDLBroadCastType, "8105", "", "Select"); _sdd.SetDDL(ref ddlCustomerType, "exec proc_sendPageLoadData @flag='search-cust-by'", "VALUE", "TEXT", "", ""); } protected void btnSave_Click(object sender, EventArgs e) { var _dbRes = SaveData(); if (_dbRes != null) { GetStatic.SetMessage(_dbRes); Response.Redirect("BroadcastList.aspx"); } } protected DbResult SaveData() { var matchString = String.Empty; var customerGroup = ""; if (customerType.Checked == true) { customerGroup = Request.Form["chk_customer_group"]; } var customerId = txtSearchData.Value; if (string.IsNullOrEmpty(DDLBroadCastType.SelectedValue)) { GetStatic.AlertMessage(this, "BroadCast Type can not be empty!"); return null; } if (string.IsNullOrEmpty(customerGroup) && customerType.Checked == true && (string.IsNullOrEmpty(GetRowId()))) { GetStatic.AlertMessage(this, "Customer Group can not be empty!"); return null; } if (string.IsNullOrEmpty(customerId) && customerType.Checked == false && (string.IsNullOrEmpty(GetRowId()))) { GetStatic.AlertMessage(this, "Customer can not be empty!"); return null; } if (string.IsNullOrEmpty(TXTTitle.Text)) { GetStatic.AlertMessage(this, "BroadCast Title can not be empty!"); return null; } if (string.IsNullOrEmpty(HttpUtility.HtmlEncode(TXTBody.Text))) { GetStatic.AlertMessage(this, "BroadCast Body can not be empty!"); return null; } var text = Request.Unvalidated["TXTBody"]; if (DDLBroadCastType.SelectedValue == "11431" || DDLBroadCastType.SelectedValue == "11429") //SMS/PUSH_NOTIFICATION { matchString = Regex.Replace(text, @"<[^>]*>", String.Empty).Replace(@" ", String.Empty).Replace(@"<\/?[^>] + (>|$)", String.Empty); matchString = matchString.Replace("<\\S*?>", ""); } else { matchString = text; } DbResult _dbRes = _mobileDao.SaveBroadCast(GetStatic.GetUser(), GetRowId(), DDLBroadCastType.SelectedValue, customerGroup, TXTTitle.Text , matchString, TXTImageURL.Text, TXTScheduleDate.Text, customerId, notificationType.Value, msgType.Value); return _dbRes; } protected string GetRowId() { return GetStatic.ReadQueryString("RowId", ""); } protected void DDLBroadCastType_SelectedIndexChanged(object sender, EventArgs e) { if (string.IsNullOrEmpty(DDLBroadCastType.SelectedValue)) { TBLCustomerGroupBody.InnerHtml = "No data to display!"; return; } PopulateCustomerGroup(DDLBroadCastType.SelectedValue); } protected void btnSaveAndSend_Click(object sender, EventArgs e) { var _dbRes = SaveData(); if (_dbRes != null) { if (_dbRes.ErrorCode == "0") { JsonResponse res = SendNotificationTPAPI(_dbRes.Id); if (res.ResponseCode == "0") { string sql = $@"UPDATE BroadCastNotification SET isSent = 1, ModifiedDate=GETDATE(), ModifiedBy='{GetStatic.GetUser()}' WHERE rowId={_dbRes.Id}"; StringBuilder sb = new StringBuilder(sql); _mobileDao.ExecuteDataset(sb.ToString()); } } GetStatic.SetMessage(_dbRes); Response.Redirect("BroadcastList.aspx"); } } private JsonResponse SendNotificationTPAPI(string rowId) { ExchangeRateAPIService NotiService = new ExchangeRateAPIService(); JsonResponse res = new JsonResponse(); var broadCastId = rowId; DataSet ds = _mobileDao.GetRepushData(GetStatic.GetUser(), broadCastId); if (ds == null) { res.SetResponse("1", "Error re-sending BroadCast!", null); } else { if (ds.Tables.Count != 2) { res.SetResponse("1", "Error re-sending BroadCast!", null); } else { if (ds.Tables[0] == null) { res.SetResponse("1", "Error re-sending BroadCast!", null); } else if (ds.Tables[1] == null) { res.SetResponse("1", "Error re-sending BoradCast!", null); } else { string ProcessId = Guid.NewGuid().ToString().Replace("-", "") + ":notiRequest"; SendNotificationRequest notificationRequest = new SendNotificationRequest { ProcessId = ProcessId.Substring(ProcessId.Length - 40, 40), NotificationTypeId = ds.Tables[0].Rows[0]["BroadcastTypeId"].ToString(), IsBulkNotification = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsBulkNotification"].ToString()), UserName = GetStatic.GetUser(), ControlNo = broadCastId }; var title = ds.Tables[0].Rows[0]["Title"].ToString(); var body = HttpUtility.HtmlDecode(ds.Tables[0].Rows[0]["Body"].ToString()); var navigateURL = ds.Tables[0].Rows[0]["NavigateURL"].ToString(); var clickActivity = ds.Tables[0].Rows[0]["ClickActivity"].ToString(); var messageType = ds.Tables[0].Rows[0]["MessageType"].ToString(); List embadedList = new List(); if (ds.Tables[0].Rows[0]["BroadcastTypeId"].ToString() == "11430" || ds.Tables[0].Rows[0]["BroadcastTypeId"].ToString() == "EMAIL") //Email { var matchString = Regex.Matches(body, "", RegexOptions.IgnoreCase); foreach (Match match in matchString) { Match result = Regex.Match(match.Groups[1].Value, @"data:.*?(?=;)"); string fileName = Guid.NewGuid().ToString(); embadedList.Add(new AttachmentModel { FileName = fileName, Byte64String = match.Groups[1].Value.Replace(result.Value + ";base64,", ""), FileType = result.Value.Split(':')[1] }); body = body.Replace(match.Groups[1].Value, "cid:" + fileName); } } List recipients = new List(); foreach (DataRow item1 in ds.Tables[1].Rows) { var receipent = new RecipientViewModel(); receipent.Address = item1["SendTo"].ToString(); receipent.DeviceType = item1["DeviceType"].ToString(); receipent.NotificationContent = new NotificationDTO { Title = title, Body = body, NavigateURL = navigateURL, ClickActivity = clickActivity, MessageType = messageType }; receipent.NotificationContent.EmbeddedResources = embadedList; recipients.Add(receipent); } notificationRequest.Recipients = recipients; //call thirdparty res = NotiService.PushNotification(notificationRequest); } } } return res; } public static string DataTableToJson(DataTable table) { if (table == null) return ""; var list = new List>(); foreach (DataRow row in table.Rows) { var dict = new Dictionary(); foreach (DataColumn col in table.Columns) { dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col]; } list.Add(dict); } var serializer = new JavaScriptSerializer(); string json = serializer.Serialize(list); return json; } } }