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;
using Telerik.Web.UI;
public partial class controls_admin_promotions : System.Web.UI.UserControl
{
#region methods
///
/// Method to Bind Promotion Data
///
private void BindPromotionData()
{
try
{
ArrayList catList = xData.GetTypedByCriteriaSpecific("recId", typeof(oPromotion), "isActive", "1");
rptPromotions.DataSource = catList;
rptPromotions.DataBind();
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Bind Promotions
///
private void BindLayoutTypes()
{
ArrayList LayoutTypes = new ArrayList();
try
{
LayoutTypes = xData.GetTypedCollection("recId", typeof(oLayoutType));
ddLayoutTypes.DataSource = LayoutTypes;
ddLayoutTypes.DataTextField = "layout";
ddLayoutTypes.DataValueField = "recId";
ddLayoutTypes.DataBind();
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Populate Form Values
///
///
private void PopulatePageFormValues(ref oPromotion _Promotion)
{
try
{
//populate values
txtPromotion.Value = _Promotion.title;
txtCaption.Value = _Promotion.caption;
if (_Promotion.layoutTypeId > 0)
{
ddLayoutTypes.SelectedValue = _Promotion.layoutTypeId.ToString();
}
chkHideTitle.Checked = _Promotion.hideTitle;
chkHideCaption.Checked = _Promotion.hideCaption;
chkShowOnHome.Checked = _Promotion.showOnHome;
string fileList = string.Empty;
foreach (oPromotionFile promotionFile in xData.GetTypedByCriteriaSpecific("recId", typeof(oPromotionFile), "promotionId", _Promotion.recId.ToString()))
{
fileList += promotionFile.fileName + "
";
}
lblCurrentFiles.Text = fileList;
if (_Promotion.startDate.Year > 1901)
txbStartDate.Text = _Promotion.startDate.ToString("dd/MM/yyyy");
if (_Promotion.endDate.Year > 1901)
txbEndDate.Text = _Promotion.endDate.ToString("dd/MM/yyyy");
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Save Form Values
///
///
private void SavePageFormValues(ref oPromotion _Promotion)
{
try
{
_Promotion.title = txtPromotion.Value;
_Promotion.caption = txtCaption.Value;
_Promotion.layoutTypeId = int.Parse(ddLayoutTypes.SelectedValue.ToString());
_Promotion.hideTitle = chkHideTitle.Checked;
_Promotion.hideCaption = chkHideCaption.Checked;
_Promotion.showOnHome = chkShowOnHome.Checked;
_Promotion.dateSaved = DateTime.Now;
_Promotion.startDate = utils.formatStringToDate(txbStartDate.Text);
_Promotion.endDate = utils.formatStringToDate(txbEndDate.Text);
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Method to Toggle Panels
///
///
private void TogglePanels(string panelName)
{
switch (panelName)
{
case "pnlPromotionPage":
pnlPromotionPage.Visible = true;
pnlPromotionList.Visible = false;
break;
case "pnlPromotionList":
pnlPromotionPage.Visible = false;
pnlPromotionList.Visible = true;
break;
}
//clear result
pnlResult.Visible = false;
}
///
/// Update Promotion Files
///
///
private void UpdatePromotionFiles(int recId, bool isUpdate = false)
{
try
{
if (radImageUpload.UploadedFiles.Count > 0)
{
foreach (UploadedFile file in radImageUpload.UploadedFiles)
{
oPromotionFile promotionFile = new oPromotionFile();
promotionFile.fileName = file.FileName.Replace(" ", "");
promotionFile.promotionId = recId;
promotionFile.isActive = true;
//upload file
string tempPath = Server.MapPath("upload/temp/") + promotionFile.fileName;
string destDir = Server.MapPath("upload/promotion/" + recId.ToString() + "/");
utils.validateFolder(Server.MapPath("upload/temp/"));
utils.validateFolder(destDir);
//upload file
promotionFile.path = recId.ToString() + "/" + promotionFile.fileName;
file.SaveAs(tempPath);
//resize the file
utils.ResizeImageMaxWidth(tempPath, destDir, promotionFile.fileName, true, true, 800);
foreach (oPromotionFile existingFile in xData.GetTypedByCriteriaSpecific("recId", typeof(oPromotionFile), "promotionId,fileName", recId.ToString() + "," + promotionFile.fileName))
{
promotionFile.recId = existingFile.recId;
}
if (promotionFile.recId > 0)
xData.UpdateTyped("recId", promotionFile.recId.ToString(), typeof(oPromotionFile), promotionFile);
else
xData.SaveTyped("recId", typeof(oPromotionFile), promotionFile);
}
//ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "clearAllInputs", "TaskFileUploadCancel();", true);
}
//ArrayList tempToRemove = new ArrayList();
//if (fuPromotion.HasFiles)
//{
// foreach (HttpPostedFile postedFile in fuPromotion.PostedFiles)
// {
// oPromotionFile promotionFile = new oPromotionFile();
// promotionFile.fileName = postedFile.FileName.Replace(" ", "");
// promotionFile.promotionId = recId;
// promotionFile.isActive = true;
// //upload file
// string tempPath = Server.MapPath("upload/temp/") + promotionFile.fileName;
// string destDir = Server.MapPath("upload/promotion/" + recId.ToString() + "/");
// utils.validateFolder(Server.MapPath("upload/temp/"));
// utils.validateFolder(destDir);
// //upload file
// promotionFile.path = recId.ToString() + "/" + promotionFile.fileName;
// fuPromotion.SaveAs(tempPath);
// //resize the file
// utils.ResizeImageMaxWidth(tempPath, destDir, promotionFile.fileName, false, true, 800);
// tempToRemove.Add(tempPath);
// foreach (oPromotionFile existingFile in xData.GetTypedByCriteriaSpecific("recId", typeof(oPromotionFile), "promotionId,fileName", recId.ToString() + "," + promotionFile.fileName))
// {
// promotionFile.recId = existingFile.recId;
// }
// if (promotionFile.recId > 0)
// xData.UpdateTyped("recId", promotionFile.recId.ToString(), typeof(oPromotionFile), promotionFile);
// else
// xData.SaveTyped("recId", typeof(oPromotionFile), promotionFile);
// }
//}
//fuPromotion = new FileUpload();
//foreach (string tempFile in tempToRemove)
//{
// utils.RemoveFile(tempFile);
//}
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
#endregion
#region events
///
/// Page Load Event
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
BindPromotionData();
BindLayoutTypes();
}
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// New Promotion Click
///
///
///
protected void btnNewPromotion_Click(object sender, EventArgs e)
{
try
{
//show Content Specific
TogglePanels("pnlPromotionPage");
Session["PromotionId"] = 0;
utils.disposeSession("Promotion");
oPromotion Promotion = new oPromotion();
PopulatePageFormValues(ref Promotion);
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Edit the Promotion page
///
///
///
protected void lnkEdit_Click(object sender, EventArgs e)
{
oPromotion _Promotion = new oPromotion();
ArrayList catList = new ArrayList();
try
{
if (sender.GetType() == typeof(LinkButton))
{
//
int recId = int.Parse(((LinkButton)sender).CommandArgument);
if (recId > 0)
{
Session["PromotionId"] = recId;
//get cat
catList = xData.GetTypedByCriteriaSpecific("recId", typeof(oPromotion), "recId", recId.ToString());
//enumerate list
foreach (oPromotion cat in catList)
{
//assign object
_Promotion = cat;
//populate page form
PopulatePageFormValues(ref _Promotion);
Session["Promotion"] = _Promotion;
break;
}
//show Promotion Form
TogglePanels("pnlPromotionPage");
}
}
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Remove the Promotion
///
///
///
protected void lnkRemove_Click(object sender, EventArgs e)
{
try
{
if (sender.GetType() == typeof(LinkButton))
{
//
int recId = int.Parse(((LinkButton)sender).CommandArgument);
//Delete cat
if (xData.DeleteTyped("recId", recId.ToString(), typeof(oPromotion)))
{
xData.DeleteTyped("promotionId", recId.ToString(), typeof(oPromotionFile));
//rebind pages grid
BindPromotionData();
pnlResult.Visible = true;
//display successful result
lblResult.Text = "the selected Promotion was deleted successfully.";
}
}
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Cancel
///
///
///
protected void btnCancel_Click(object sender, EventArgs e)
{
try
{
Session["PromotionId"] = 0;
utils.disposeSession("Promotion");
BindPromotionData();
//show cat list
TogglePanels("pnlPromotionList");
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Save Click
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
oPromotion Promotion = new oPromotion();
try
{
if (utils.verifySession("Promotion"))//update mode
{
Promotion = (oPromotion)Session["Promotion"];
//save form values
SavePageFormValues(ref Promotion);
if (xData.UpdateTyped("recId", Promotion.recId.ToString(), typeof(oPromotion), Promotion))
{
UpdatePromotionFiles(Promotion.recId, true);
Session["PromotionId"] = Promotion.recId;
Session["Promotion"] = Promotion;
BindPromotionData();
PopulatePageFormValues(ref Promotion);
pnlResult.Visible = true;
lblResult.Text = "your changes were updated successfully.";
}
}
else
{
if (!xData.VerifyExists("recId", typeof(oPromotion), "title", txtPromotion.Value))
{
Promotion.isActive = true;
//save form values
SavePageFormValues(ref Promotion);
Promotion.recId = xData.SaveTyped("recId", typeof(oPromotion), Promotion);
if (Promotion.recId > 0)
{
UpdatePromotionFiles(Promotion.recId, false);
Session["PromotionId"] = Promotion.recId;
Session["Promotion"] = Promotion;
BindPromotionData();
PopulatePageFormValues(ref Promotion);
pnlResult.Visible = true;
lblResult.Text = "your Promotion was created successfully.";
}
}
else
{
pnlResult.Visible = true;
lblResult.Text = "A Promotion with this name already exists on the system";
}
}
}
catch (Exception ex)
{
exception.HandleException("Promotions:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
#endregion
protected void radImageUpload_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
{
}
}