using framework_business; using framework_library; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class controls_admin_loyaltygroups : System.Web.UI.UserControl { #region methods /// /// Method to Bind Group Data /// private void BindGroupData() { try { ArrayList groupList = xData.GetTypedByCriteriaSpecific("recId", typeof(oLoyaltyGroup), "isActive", "1", ""); rptLoyaltyGroups.DataSource = groupList; rptLoyaltyGroups.DataBind(); } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Bind products /// private void BindLoyaltyStores() { ArrayList stores = new ArrayList(); try { stores = xData.GetTypedByCriteriaSpecific("recId", typeof(oLoyaltyStore), "isActive", "1"); ddStore.DataSource = stores; ddStore.DataTextField = "storeName"; ddStore.DataValueField = "recId"; ddStore.DataBind(); ddStore.Items.Insert(0, new ListItem("Select a Store", "0")); } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("Error.aspx", false); } } /// /// Populate Form Values /// /// private void PopulatePageFormValues(ref oLoyaltyGroup _group) { try { txtName.Value = _group.groupName; txtCode.Value = _group.groupCode; chkActive.Checked = _group.isActive; txtNextSequence.Value = _group.nextSequence.ToString(); if (ddStore.Items.FindByValue(_group.loyaltyStoreId.ToString()) != null) ddStore.SelectedValue = _group.loyaltyStoreId.ToString(); } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Save Form Values /// /// private void SavePageFormValues(ref oLoyaltyGroup _group) { try { _group.loyaltyStoreId = Convert.ToInt32(ddStore.SelectedValue); _group.groupName = txtName.Value; _group.groupCode = txtCode.Value; _group.nextSequence = Convert.ToInt32(txtNextSequence.Value); _group.isActive = chkActive.Checked; } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Method to Toggle Panels /// /// private void TogglePanels(string panelName) { switch (panelName) { case "pnlGroupPage": pnlGroupPage.Visible = true; pnlGroupList.Visible = false; break; case "pnlGroupList": pnlGroupPage.Visible = false; pnlGroupList.Visible = true; break; } //clear result pnlResult.Visible = false; } #endregion #region events /// /// Page Load Event /// /// /// protected void Page_Load(object sender, EventArgs e) { try { if (!Page.IsPostBack) { BindGroupData(); BindLoyaltyStores(); } } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// New group Click /// /// /// protected void btnNewGroup_Click(object sender, EventArgs e) { try { //show Content Specific TogglePanels("pnlGroupPage"); Session["groupId"] = 0; utils.disposeSession("group"); oLoyaltyGroup group = new oLoyaltyGroup(); PopulatePageFormValues(ref group); } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Edit the group page /// /// /// protected void lnkEdit_Click(object sender, EventArgs e) { oLoyaltyGroup _group = new oLoyaltyGroup(); ArrayList groupList = new ArrayList(); try { if (sender.GetType() == typeof(LinkButton)) { int recId = int.Parse(((LinkButton)sender).CommandArgument); if (recId > 0) { Session["groupId"] = recId; //get group groupList = xData.GetTypedByCriteriaSpecific("recId", typeof(oLoyaltyGroup), "recId", recId.ToString()); //enumerate list foreach (oLoyaltyGroup group in groupList) { //assign object _group = group; //populate page form PopulatePageFormValues(ref _group); Session["group"] = _group; break; } //show group Form TogglePanels("pnlGroupPage"); } } } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Remove the Group /// /// /// protected void lnkRemove_Click(object sender, EventArgs e) { try { if (sender.GetType() == typeof(LinkButton)) { int recId = int.Parse(((LinkButton)sender).CommandArgument); //Delete group if (xData.DeleteTyped("recId", recId.ToString(), typeof(oLoyaltyGroup))) { //rebind pages grid BindGroupData(); pnlResult.Visible = true; //display successful result lblResult.Text = "the selected group was deleted successfully."; } } } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Cancel /// /// /// protected void btnCancel_Click(object sender, EventArgs e) { try { Session["groupId"] = 0; utils.disposeSession("group"); BindGroupData(); //show group list TogglePanels("pnlGroupList"); } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Save Click /// /// /// protected void btnSave_Click(object sender, EventArgs e) { oLoyaltyGroup group = new oLoyaltyGroup(); try { if (checkValidNextSequence()) { if (utils.verifySession("group"))//update mode { group = (oLoyaltyGroup)Session["group"]; //save form values SavePageFormValues(ref group); if (xData.UpdateTyped("recId", group.recId.ToString(), typeof(oLoyaltyGroup), group)) { Session["groupId"] = group.recId; Session["group"] = group; BindGroupData(); pnlResult.Visible = true; lblResult.Text = "your changes were updated successfully."; } } else { if (!xData.VerifyExists("recId", typeof(oLoyaltyGroup), "groupName,loyaltyStoreId", txtName.Value + "," + ddStore.SelectedValue.ToString())) { group.isActive = true; //save form values SavePageFormValues(ref group); group.recId = xData.SaveTyped("recId", typeof(oLoyaltyGroup), group); if (group.recId > 0) { Session["groupId"] = group.recId; Session["group"] = group; BindGroupData(); pnlResult.Visible = true; lblResult.Text = "your group was created successfully."; } } else { pnlResult.Visible = true; lblResult.Text = "A Group with this name already exists on the system"; } } } } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } private bool checkValidNextSequence() { bool returnValue = true; try { string setNextSequence = txtNextSequence.Value; string groupId = "0"; if (utils.verifySession("groupId")) groupId = Session["groupId"].ToString(); foreach (oLoyaltyMember member in xData.GetTypedByCriteriaSpecific("recId",typeof(oLoyaltyMember),"loyaltyGroupId",groupId)) { if (member.loyaltyNumber.Substring(7, 5) == setNextSequence) { returnValue = false; pnlResult.Visible = true; lblResult.Text = "A member with the loyalty number already exists"; } } } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); } return returnValue; } /// /// Back Button /// /// /// protected void btnBack_Click(object sender, EventArgs e) { try { if (utils.verifySession("prevousId")) Session["parentId"] = Session["prevousId"]; BindGroupData(); } catch (Exception ex) { exception.HandleException("loyaltyGroups:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); } } #endregion }