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_products : System.Web.UI.UserControl
{
#region methods
///
/// Method to Bind Product Data
///
private void BindProductData()
{
try
{
//to do add filtering
ArrayList prodList = xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "isActive", "1", "sequence");
rptProducts.DataSource = prodList;
rptProducts.DataBind();
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Bind products
///
private void BindProductCategories()
{
ArrayList categories = new ArrayList();
try
{
categories = xData.GetTypedByCriteriaSpecific("recId", typeof(oProductCategory), "isActive,parentId", "1,0", "sequence");
ddCategories.DataSource = categories;
ddCategories.DataTextField = "category";
ddCategories.DataValueField = "recId";
ddCategories.DataBind();
ddCategories.Items.Insert(0, new ListItem("Select a Category", "0"));
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Bind products
///
private void BindProductTypes()
{
ArrayList types = new ArrayList();
try
{
int catId = 0;
int.TryParse(ddCategories.SelectedValue, out catId);
if (catId > 0)
{
types = xData.GetTypedByCriteriaSpecific("recId", typeof(oProductCategory), "isActive,parentId", "1," + catId, "sequence");
ddTypes.DataSource = types;
ddTypes.DataTextField = "category";
ddTypes.DataValueField = "recId";
ddTypes.DataBind();
ddTypes.Items.Insert(0, new ListItem("Select a Type", "0"));
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Bind products
///
private void BindProductBrands()
{
ArrayList brands = new ArrayList();
try
{
brands = xData.GetTypedByCriteriaSpecific("recId", typeof(oProductBrand), "isActive", "1", "sequence");
ddBrands.DataSource = brands;
ddBrands.DataTextField = "brand";
ddBrands.DataValueField = "recId";
ddBrands.DataBind();
ddBrands.Items.Insert(0, new ListItem("Select a Brand", "0"));
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Method to Bind the slide Order Drop down
///
private void BindSequence()
{
try
{
for (int i = 0; i < 20; i++)
{
//append sequence item
ddSequence.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Populate Form Values
///
///
private void PopulatePageFormValues(ref oProduct _product)
{
try
{
txtName.Value = _product.display;
txtDescription.Value = _product.description;
chkActive.Checked = _product.isActive;
chkFeatured.Checked = _product.isFeatured;
if (_product.sequence > 0)
ddSequence.Text = _product.sequence.ToString();
if (_product.catId > 0)
ddCategories.SelectedValue = _product.catId.ToString();
BindProductTypes();
if (_product.typeId > 0)
ddTypes.SelectedValue = _product.typeId.ToString();
if (_product.brandId > 0)
ddBrands.SelectedValue = _product.brandId.ToString();
if (_product.price > 0)
txtPrice.Value = utils.returnFormattedDecimal(Convert.ToString(_product.price));
else
txtPrice.Value = String.Empty;
txtSpecifications.Value = _product.specifications;
txtSize.Value = _product.size;
//handle prod image population
if (_product.fileName != String.Empty)
{
lblCurrentFile.Text = _product.fileName;
imgProductPreview.Visible = true;
imgProductPreview.ImageUrl = "~/upload/products/image/" + _product.fileName;
}
else
{
imgProductPreview.Visible = false;
lblCurrentFile.Text = "none";
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Save Form Values
///
///
private void SavePageFormValues(ref oProduct _product)
{
try
{
//save values
_product.name = utils.stripCharacters(txtName.Value).Replace(" ", "~");
_product.display = txtName.Value;
_product.description = txtDescription.Value;
_product.specifications = txtSpecifications.Value;
_product.isActive = chkActive.Checked;
_product.isFeatured = chkFeatured.Checked;
_product.sequence = int.Parse(ddSequence.Text);
if (ddCategories.SelectedValue != string.Empty && ddCategories.SelectedValue != "0")
_product.catId = int.Parse(ddCategories.SelectedValue);
if (ddTypes.SelectedValue != string.Empty && ddTypes.SelectedValue != "0")
_product.typeId = int.Parse(ddTypes.SelectedValue);
if (ddBrands.SelectedValue != string.Empty && ddBrands.SelectedValue != "0")
_product.brandId = int.Parse(ddBrands.SelectedValue);
decimal priceVal = 0;
decimal.TryParse(txtPrice.Value, out priceVal);
_product.price = priceVal;
_product.size = txtSize.Value;
//upload product image
//if (fuProduct.FileContent.Length > 0)
//{
// _product.fileName = utils.RandomString(9, true) + fuProduct.FileName.Substring(fuProduct.FileName.IndexOf("."));
// string imagePath = Server.MapPath("upload/products/image/");
// string thumbPath = Server.MapPath("upload/products/thumb/");
// string tempPath = Server.MapPath("upload/products/temp/");
// utils.validateFolder(imagePath);
// utils.validateFolder(thumbPath);
// utils.validateFolder(tempPath);
// //upload file
// fuProduct.SaveAs(tempPath + _product.fileName);
// //resize and compress
// utils.ResizeImage(tempPath + _product.fileName, imagePath, _product.fileName, 400, true);
// //resize and crop
// utils.ResizeCropImagePrecise(imagePath + _product.fileName, thumbPath, _product.fileName, 383, 264, false);
//}
//else
// _product.fileName = lblCurrentFile.Text;
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Method to Toggle Panels
///
///
private void TogglePanels(string panelName)
{
switch (panelName)
{
case "pnlProductPage":
pnlProductPage.Visible = true;
pnlProductList.Visible = false;
break;
case "pnlProductList":
pnlProductPage.Visible = false;
pnlProductList.Visible = true;
break;
}
//clear result
pnlResult.Visible = false;
}
///
/// Save Product Changes
///
///
private bool SaveChanges()
{
bool result = false;
oProduct Product = new oProduct();
try
{
if (utils.verifySession("Product"))//update mode
{
Product = (oProduct)Session["Product"];
//save form values
SavePageFormValues(ref Product);
if (xData.UpdateTyped("recId", Product.recId.ToString(), typeof(oProduct), Product))
{
Session["ProductId"] = Product.recId;
Session["Product"] = Product;
BindProductData();
PopulatePageFormValues(ref Product);
pnlResult.Visible = true;
result = true;
lblResult.Text = "your changes were updated successfully.";
}
}
else
{
if (!xData.VerifyExists("recId", typeof(oProduct), "name", txtName.Value))
{
Product.isActive = true;
//save form values
SavePageFormValues(ref Product);
Product.recId = xData.SaveTyped("recId", typeof(oProduct), Product);
if (Product.recId > 0)
{
Session["ProductId"] = Product.recId;
Session["Product"] = Product;
BindProductData();
PopulatePageFormValues(ref Product);
pnlResult.Visible = true;
result = true;
lblResult.Text = "your Product was created successfully.";
}
}
else
{
pnlResult.Visible = true;
lblResult.Text = "A Product with this name already exists on the system";
}
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
return result;
}
#endregion
#region events
///
/// Page Load Event
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
BindProductData();
BindProductCategories();
BindProductTypes();
BindProductBrands();
BindSequence();
if (utils.verifySession("productId"))
{
oProduct _Product = new oProduct();
ArrayList prodList = new ArrayList();
int prodId = int.Parse(Session["ProductId"].ToString());
//get prod
prodList = xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "recId", prodId.ToString());
//enumerate list
foreach (oProduct prod in prodList)
{
//assign object
_Product = prod;
//populate page form
PopulatePageFormValues(ref _Product);
Session["Product"] = _Product;
//show Product Form
TogglePanels("pnlProductPage");
}
}
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// New Product Click
///
///
///
protected void btnNewProduct_Click(object sender, EventArgs e)
{
try
{
//show Content Specific
TogglePanels("pnlProductPage");
Session["ProductId"] = 0;
utils.disposeSession("Product");
oProduct Product = new oProduct();
PopulatePageFormValues(ref Product);
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Edit the Product page
///
///
///
protected void lnkEdit_Click(object sender, EventArgs e)
{
oProduct _Product = new oProduct();
ArrayList prodList = new ArrayList();
try
{
if (sender.GetType() == typeof(LinkButton))
{
//
int recId = int.Parse(((LinkButton)sender).CommandArgument);
if (recId > 0)
{
Session["ProductId"] = recId;
//get prod
prodList = xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "recId", recId.ToString());
//enumerate list
foreach (oProduct prod in prodList)
{
//assign object
_Product = prod;
//populate page form
PopulatePageFormValues(ref _Product);
Session["Product"] = _Product;
break;
}
//show Product Form
TogglePanels("pnlProductPage");
}
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Remove the Product
///
///
///
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(oProduct)))
{
//rebind pages grid
BindProductData();
pnlResult.Visible = true;
//display successful result
lblResult.Text = "the selected Product was deleted successfully.";
}
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Cancel
///
///
///
protected void btnCancel_Click(object sender, EventArgs e)
{
try
{
Session["ProductId"] = 0;
utils.disposeSession("Product");
BindProductData();
//show cat list
TogglePanels("pnlProductList");
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Save Click
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
SaveChanges();
}
///
/// Upload Images press
///
///
///
protected void btnUploadImage_Click(object sender, EventArgs e)
{
if (SaveChanges())
{
//setup attachments
Session["moduleId"] = pNums.Module.Products.GetHashCode();
Session["entityId"] = Session["ProductId"];
/* CVH 2016-04-29 Moved Attachments modal to Products Control (from Master page) */
dynamic attachments = this.FindControl("attachments");
attachments.ReloadControl();
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myModal", "$('#modAttachments').modal();", true);
}
}
///
/// Selected Index Changed
///
///
///
protected void ddCategories_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
BindProductTypes();
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
#endregion
}