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_gallery : System.Web.UI.UserControl
{
#region methods
///
/// Method to Bind Gallery Data
///
private void BindGalleryData()
{
try
{
/* CVH 2016-04-13 Don't only show active gallery items, otherwise no way to edit an item set inactive. Load all images, no filter. */
//ArrayList catList = xData.GetTypedByCriteriaSpecific("recId", typeof(oGallery), "isActive", "1", "sequence");
ArrayList catList = xData.GetTypedCollection("recId", typeof(oGallery), "sequence");
rptGallerys.DataSource = catList;
rptGallerys.DataBind();
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Bind Galleries
///
private void BindGalleryCategories()
{
ArrayList galCats = new ArrayList();
try
{
galCats = xData.GetTypedByCriteriaSpecific("recId", typeof(oGalleryCategory), "isActive", "1", "sequence");
ddGalleryCategories.DataSource = galCats;
ddGalleryCategories.DataTextField = "category";
ddGalleryCategories.DataValueField = "recId";
ddGalleryCategories.DataBind();
ddGalleryCategories.Items.Insert(0, new ListItem("Select Category", "0"));
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Bind Galleries
///
private void BindPictureTypes()
{
ArrayList pictureTypes = new ArrayList();
try
{
pictureTypes = xData.GetTypedCollection("recId", typeof(oPictureType));
ddGalleryTypes.DataSource = pictureTypes;
ddGalleryTypes.DataTextField = "type";
ddGalleryTypes.DataValueField = "recId";
ddGalleryTypes.DataBind();
}
catch (Exception ex)
{
exception.HandleException("Galleries:", 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("Galleries:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Populate Form Values
///
///
private void PopulatePageFormValues(ref oGallery _gallery)
{
try
{
//populate values
txtTitle.Value = _gallery.title;
txtDescription.Value = _gallery.description;
chkActive.Checked = _gallery.isActive;
if (_gallery.sequence > 0)
ddSequence.SelectedValue = _gallery.sequence.ToString();
if (_gallery.pictureTypeId > 0)
{
ddGalleryTypes.SelectedValue = _gallery.pictureTypeId.ToString();
}
if (_gallery.catId > 0)
{
ddGalleryCategories.SelectedValue = _gallery.catId.ToString();
}
if (_gallery.fileName != String.Empty)
{
lblCurrentGallery.Text = _gallery.fileName.Replace(" ", "");
//populate preview
foreach (oPictureType pType in xData.GetTypedByCriteriaSpecific("recId", typeof(oPictureType), "recId", _gallery.pictureTypeId.ToString()))
{
imgGalleryPreview.Visible = true;
imgGalleryPreview.ImageUrl = "~/upload/gallery/thumb/" + pType.type + "/" + _gallery.fileName;
}
}
else
{
imgGalleryPreview.Visible = false;
lblCurrentGallery.Text = "Current: none";
}
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Save Form Values
///
///
private void SavePageFormValues(ref oGallery _gallery)
{
try
{
_gallery.title = txtTitle.Value;
_gallery.sequence = int.Parse(ddSequence.SelectedValue);
_gallery.isActive = chkActive.Checked;
_gallery.description = txtDescription.Value;
_gallery.pictureTypeId = int.Parse(ddGalleryTypes.SelectedValue.ToString());
_gallery.catId = int.Parse(ddGalleryCategories.SelectedValue.ToString());
_gallery.dateSaved = DateTime.Now;
if (fuGallery.FileContent.Length > 0)
{
_gallery.fileName = utils.RandomString(9, true) + fuGallery.FileName.Substring(fuGallery.FileName.IndexOf("."));
//upload file
foreach (oPictureType pType in xData.GetTypedByCriteriaSpecific("recId", typeof(oPictureType), "recId", _gallery.pictureTypeId.ToString()))
{
utils.validateFolder(Server.MapPath("upload/gallery/image/" + pType.type + "/"));
utils.validateFolder(Server.MapPath("upload/gallery/thumb/" + pType.type + "/"));
string destDirLrg = Server.MapPath("upload/gallery/image/" + pType.type + "/");
string destDirSml = Server.MapPath("upload/gallery/thumb/" + pType.type + "/");
//upload file
fuGallery.SaveAs(destDirLrg + _gallery.fileName);
utils.ResizeCropImagePrecise(destDirLrg + _gallery.fileName, destDirSml, _gallery.fileName, 383, 264, false);
break;
}
}
else
_gallery.fileName = lblCurrentGallery.Text;
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Method to Toggle Panels
///
///
private void TogglePanels(string panelName)
{
switch (panelName)
{
case "pnlGalleryPage":
pnlGalleryPage.Visible = true;
pnlGalleryList.Visible = false;
break;
case "pnlGalleryList":
pnlGalleryPage.Visible = false;
pnlGalleryList.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)
{
BindGalleryData();
BindSequence();
BindPictureTypes();
BindGalleryCategories();
}
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// New Gallery Click
///
///
///
protected void btnNewGallery_Click(object sender, EventArgs e)
{
try
{
//show Content Specific
TogglePanels("pnlGalleryPage");
Session["GalleryId"] = 0;
utils.disposeSession("Gallery");
oGallery Gallery = new oGallery();
PopulatePageFormValues(ref Gallery);
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Edit the Gallery page
///
///
///
protected void lnkEdit_Click(object sender, EventArgs e)
{
oGallery _Gallery = new oGallery();
ArrayList catList = new ArrayList();
try
{
if (sender.GetType() == typeof(LinkButton))
{
//
int recId = int.Parse(((LinkButton)sender).CommandArgument);
if (recId > 0)
{
Session["GalleryId"] = recId;
//get cat
catList = xData.GetTypedByCriteriaSpecific("recId", typeof(oGallery), "recId", recId.ToString());
//enumerate list
foreach (oGallery cat in catList)
{
//assign object
_Gallery = cat;
//populate page form
PopulatePageFormValues(ref _Gallery);
Session["Gallery"] = _Gallery;
break;
}
//show Gallery Form
TogglePanels("pnlGalleryPage");
}
}
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Remove the Gallery
///
///
///
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(oGallery)))
{
//rebind pages grid
BindGalleryData();
pnlResult.Visible = true;
//display successful result
lblResult.Text = "the selected Gallery was deleted successfully.";
}
}
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Cancel
///
///
///
protected void btnCancel_Click(object sender, EventArgs e)
{
try
{
Session["GalleryId"] = 0;
utils.disposeSession("Gallery");
BindGalleryData();
//show cat list
TogglePanels("pnlGalleryList");
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Save Click
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
oGallery Gallery = new oGallery();
try
{
if (utils.verifySession("Gallery"))//update mode
{
Gallery = (oGallery)Session["Gallery"];
//save form values
SavePageFormValues(ref Gallery);
if (xData.UpdateTyped("recId", Gallery.recId.ToString(), typeof(oGallery), Gallery))
{
Session["GalleryId"] = Gallery.recId;
Session["Gallery"] = Gallery;
BindGalleryData();
PopulatePageFormValues(ref Gallery);
pnlResult.Visible = true;
lblResult.Text = "your changes were updated successfully.";
}
}
else
{
if (!xData.VerifyExists("recId", typeof(oGallery), "title", txtTitle.Value))
{
//save form values
SavePageFormValues(ref Gallery);
Gallery.recId = xData.SaveTyped("recId", typeof(oGallery), Gallery);
if (Gallery.recId > 0)
{
Session["GalleryId"] = Gallery.recId;
Session["Gallery"] = Gallery;
BindGalleryData();
PopulatePageFormValues(ref Gallery);
pnlResult.Visible = true;
lblResult.Text = "your Gallery was created successfully.";
}
}
else
{
pnlResult.Visible = true;
lblResult.Text = "A Gallery with this title already exists on the system";
}
}
}
catch (Exception ex)
{
exception.HandleException("Galleries:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
#endregion
}