using framework_business;
using framework_library;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class controls_admin_attachments : System.Web.UI.UserControl, IControlBase
{
#region properties
/* CVH 2016-04-28 Used to indicate that all attachments of the module type for the given surfaceItemId should loaded. (Not just the attachments linking to a specific NoteId.) */
public int HistorySurfaceItemId
{
get
{
if (ViewState["HistorySurfaceItemId"] == null)
return 0;
else
return int.Parse(ViewState["HistorySurfaceItemId"].ToString());
}
set
{
ViewState["HistorySurfaceItemId"] = value;
}
}
#endregion
#region methods
///
/// Method to Bind Attachment Data
///
private void BindAttachmentData()
{
try
{
//Setup heading
if (utils.verifySession("entityId") && utils.verifySession("moduleId"))
{
int moduleId = int.Parse(Session["moduleId"].ToString());
int entityId = int.Parse(Session["entityId"].ToString());
int typeId = 0;
string fieldName = String.Empty;
if (utils.verifySession("fieldNameFile"))
fieldName = Session["fieldNameFile"].ToString();
ArrayList attachmentList = new ArrayList();
if (this.HistorySurfaceItemId != 0)
{
ArrayList noteList = new ArrayList();
//get list of all notes linked to the surface item
if (fieldName != String.Empty)
{
foreach (oSurfaceField field in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceField), "surfaceFieldName,isActive", fieldName + ",1"))
{
noteList = xData.GetTypedByCriteriaSpecific("recId", typeof(oNote), "moduleId,entityId,fieldName", pNums.Module.Surface.GetHashCode() + "," + this.HistorySurfaceItemId.ToString() + "," + field.recId.ToString(), "recId DESC");
}
}
else
noteList = xData.GetTypedByCriteriaSpecific("recId", typeof(oNote), "moduleId,entityId", pNums.Module.Surface.GetHashCode() + "," + this.HistorySurfaceItemId.ToString(), "recId DESC");
foreach (oNote note in noteList)
{
foreach (oAttachment noteAtt in xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId", moduleId + "," + note.recId, "dateSaved DESC"))
{
attachmentList.Add(noteAtt);
}
}
}
else
{
pNums.Module mod = (pNums.Module)Enum.ToObject(typeof(pNums.Module), moduleId);
switch (mod)
{
case pNums.Module.Products:
if (utils.verifySession("product"))
{
oProduct prod = (oProduct)Session["product"];
lblEntityName.Text = prod.display;
break;
}
break;
case pNums.Module.Surface:
Session["surfaceItem"] = (oSurfaceItem)xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", entityId.ToString())[0];
Session["surface"] = (oSurface)xData.GetTypedByCriteriaSpecific("recId", typeof(oSurface), "recId", ((oSurfaceItem)Session["surfaceItem"]).surfaceId.ToString())[0];
lblEntityName.Text = ((oSurface)Session["surface"]).surface;
break;
case pNums.Module.Notes:
if (!utils.verifySession("note"))
{
ArrayList temp = xData.GetTypedByCriteriaSpecific("recId", typeof(oNote), "recId", entityId.ToString());
if (temp != null && temp.Count > 0)
Session["note"] = (oNote)temp[0];
}
if (utils.verifySession("note"))
lblEntityName.Text = ((oNote)Session["note"]).title;
break;
case pNums.Module.Task:
oTaskHistory taskHistory = (oTaskHistory)xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskHistory), "recId", entityId.ToString(), "", "pal_", true)[0];
oTask task = (oTask)xData.GetTypedByCriteriaSpecific("recId", typeof(oTask), "recId", taskHistory.taskId.ToString(), "", "pal_", true)[0];
Session["task"] = task;
lblEntityName.Text = "Task: " + task.name;
break;
default:
break;
}
//ArrayList attachmentList = new ArrayList();
if (mod == pNums.Module.Task)
{
List list = new List();
oDynamicParam param1 = new oDynamicParam();
param1.paramDisplayName = "moduleId";
param1.paramObject = moduleId;
list.Add(param1);
oDynamicParam param2 = new oDynamicParam();
param2.paramDisplayName = "taskId";
param2.paramObject = ((oTask)Session["task"]).recId;
list.Add(param2);
attachmentList = xData.GetTypedCollectionByProc("recId", typeof(oAttachment), "sp_TaskAttachments", list, "pal_", true);
}
else
if (fieldName != String.Empty)
{
if (typeId > 0)
{
attachmentList = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId,typeId,fieldName", moduleId + "," + entityId + "," + typeId + "," + fieldName, "sequence");
}
else
{
attachmentList = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId,fieldName", moduleId + "," + entityId + "," + fieldName, "sequence");
}
}
else
{
if (typeId > 0)
{
attachmentList = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId,typeId", moduleId + "," + entityId + "," + typeId, "sequence");
}
else
{
attachmentList = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId", moduleId + "," + entityId, "sequence");
}
}
}
if (attachmentList != null)
{
rptAttachments.DataSource = attachmentList;
rptAttachments.DataBind();
}
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Method to Bind the slide Order Drop down
///
private void BindAttachmnentTypes()
{
try
{
DataTable typeData = xData.GetTypedByCriteriaSpecificTable("recId", typeof(oAttachmentType), "isActive", "1", "attachmentType");
ddAttachmentType.DataSource = typeData;
ddAttachmentType.DataTextField = "attachmentType";
ddAttachmentType.DataValueField = "recId";
ddAttachmentType.DataBind();
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", 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 oAttachment _Attachment)
{
try
{
chkActive.Checked = _Attachment.isActive;
txtName.Value = _Attachment.display;
if (_Attachment.typeId > 0)
ddAttachmentType.SelectedValue = _Attachment.typeId.ToString();
if (ddAttachmentType.SelectedValue == pNums.AttachmentType.Document.GetHashCode().ToString())
{
pnlUploadDocument.Visible = true;
pnlUploadImage.Visible = false;
chkPrimary.Visible = false;
if (_Attachment.fileName != String.Empty)
{
lblCurrentFile.Text = _Attachment.fileName;
imgAttachmentPreview.Visible = false;
lnkPrviewDocument.Visible = true;
}
else
{
imgAttachmentPreview.Visible = false;
lblCurrentFile.Text = "none";
lnkPrviewDocument.Visible = false;
}
}
else
{
lnkPrviewDocument.Visible = false;
pnlUploadDocument.Visible = false;
pnlUploadImage.Visible = true;
chkPrimary.Visible = true;
//handle prod image population
if (_Attachment.fileName != String.Empty)
{
lblCurrentFile.Text = _Attachment.fileName;
imgAttachmentPreview.Visible = true;
if (_Attachment.sourcePath != String.Empty)
imgAttachmentPreview.ImageUrl = _Attachment.sourcePath + "/upload/" + _Attachment.folderName + "/image/" + _Attachment.fileName;
else
imgAttachmentPreview.ImageUrl = "~/upload/" + _Attachment.folderName + "/image/" + _Attachment.fileName;
}
else
{
imgAttachmentPreview.Visible = false;
lblCurrentFile.Text = "none";
}
}
pNums.Module mod = (pNums.Module)Enum.ToObject(typeof(pNums.Module), _Attachment.moduleId);
switch (mod)
{
case pNums.Module.Products:
if (utils.verifySession("product"))
{
oProduct prod = (oProduct)Session["product"];
if (prod.fileName == _Attachment.fileName)
{
chkPrimary.Checked = true;
}
break;
}
break;
case pNums.Module.Surface:
break;
case pNums.Module.Notes:
break;
default:
break;
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Save Form Values
///
///
private void SavePageFormValues(ref oAttachment _Attachment)
{
try
{
_Attachment.isActive = chkActive.Checked;
if (chkPrimary.Checked && _Attachment.typeId == pNums.AttachmentType.Image.GetHashCode())
{
pNums.Module mod = (pNums.Module)Enum.ToObject(typeof(pNums.Module), _Attachment.moduleId);
switch (mod)
{
case pNums.Module.Products:
if (utils.verifySession("product"))
{
oProduct prod = (oProduct)Session["product"];
if (prod.recId == _Attachment.entityId)
{
prod.fileName = _Attachment.fileName;
xData.UpdateTyped("recId", prod.recId.ToString(), typeof(oProduct), prod);
}
}
break;
case pNums.Module.Surface:
break;
case pNums.Module.Notes:
break;
default:
break;
}
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Method to Toggle Panels
///
///
private void TogglePanels(string panelName)
{
switch (panelName)
{
case "pnlAttachmentPage":
pnlAttachmentPage.Visible = true;
pnlAttachmentList.Visible = false;
break;
case "pnlAttachmentList":
pnlAttachmentPage.Visible = false;
pnlAttachmentList.Visible = true;
break;
}
//clear result
pnlResult.Visible = false;
}
///
/// Setup Attachment Screen
///
private void SetupAttachmentScreen()
{
try
{
TogglePanels("pnlAttachmentList");
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
///
/// Get Image URL
///
///
///
///
///
///
public string GetImage(object sourcePath, object folderName, object fileName, object type)
{
string result = String.Empty;
string srcPAth = String.Empty;
if (type != null)
{
//do something with the ImageID to return the image path as string
if (int.Parse(type.ToString()) == pNums.AttachmentType.Document.GetHashCode())
{
result = "/images/doc.png";
}
else
{
if (sourcePath != null && sourcePath.ToString() != String.Empty)
srcPAth = sourcePath.ToString();
result = srcPAth + "/upload/" + folderName.ToString() + "/thumb/" + fileName.ToString();
}
}
else
{
result = "/images/doc.png";
}
return result;
}
#endregion
#region events
///
/// Event fires from control base
///
public void ReloadControl()
{
try
{
if (utils.verifySession("HistorySurfaceItemId"))
{
this.HistorySurfaceItemId = int.Parse(Session["HistorySurfaceItemId"].ToString());
utils.disposeSession("HistorySurfaceItemId");
/* CVH 2016-05-03 Remove panel classes when attachments control is showing History of a surface item */
lblAttachmentsFor.Text = "";
lblEntityName.Text = "";
divPanelBody.Attributes.CssStyle.Add("padding-top", "0px");
divPanelHeading.Visible = false;
divPanelPrimary.Attributes.Remove("class");
}
SetupAttachmentScreen();
BindAttachmentData();
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
public void SaveControlData() { }
///
/// Page Load Event
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
BindSequence();
BindAttachmnentTypes();
SetupAttachmentScreen();
//BindAttachmentData();
}
/* CVH 2016-05-03 Remove panel classes when attachments control is showing History of a surface item */
if (this.HistorySurfaceItemId != 0)
{
lblAttachmentsFor.Text = "";
lblEntityName.Text = "";
divPanelBody.Attributes.CssStyle.Add("padding-top", "0px");
divPanelHeading.Visible = false;
divPanelPrimary.Attributes.Remove("class");
}
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(lnkPrviewDocument);
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Click preview document
///
///
///
protected void lnkPrviewDocument_Click(object sender, EventArgs e)
{
try
{
if (utils.verifySession("Attachment"))
{
oAttachment doc = (oAttachment)Session["Attachment"];
Response.Clear();
//Set the appropriate ContentType.
if (doc.fileName.ToLower().Contains(".xlsx"))
{ Response.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; }
else if (doc.fileName.ToLower().Contains(".xls"))
{ Response.ContentType = "Application/vnd.ms-excel"; }
else if (doc.fileName.ToLower().Contains(".docx"))
{ Response.ContentType = "Application/vnd.openxmlformats-officedocument.wordprocessingml.document"; }
else if (doc.fileName.ToLower().Contains(".doc"))
{ Response.ContentType = "Application/vnd.ms-word"; }
else if (doc.fileName.ToLower().Contains(".pptx"))
{ Response.ContentType = "Application/vnd.openxmlformats-officedocument.presentationml.presentation"; }
else if (doc.fileName.ToLower().Contains(".ppt"))
{ Response.ContentType = "Application/vnd.ms-powerpoint"; }
else if (doc.fileName.ToLower().Contains(".pdf"))
{ Response.ContentType = "Application/pdf"; }
else if (doc.fileName.ToLower().Contains(".zip"))
{ Response.ContentType = "Application/x-compressed"; }
Response.AppendHeader("Content-Disposition", "attachment; filename=" + doc.fileName);
string docFile = String.Empty;
//if (doc.sourcePath != String.Empty)
// docFile = doc.sourcePath + "/upload/" + doc.folderName + "/file/" + doc.fileName;
//else
docFile = Server.MapPath("~/upload/" + doc.folderName + "/file/" + doc.fileName);
if (File.Exists(docFile))
{
Response.TransmitFile(docFile);
}
else if (File.Exists(docFile.Replace("/file", "")))
{
Response.TransmitFile(docFile.Replace("/file", ""));
}
Response.Flush();
Response.SuppressContent = true;
ApplicationInstance.CompleteRequest();
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// New Attachment Click
///
///
///
protected void btnNewAttachment_Click(object sender, EventArgs e)
{
try
{
//show Content Specific
TogglePanels("pnlAttachmentPage");
Session["AttachmentId"] = 0;
utils.disposeSession("Attachment");
oAttachment Attachment = new oAttachment();
PopulatePageFormValues(ref Attachment);
if (utils.verifySession("moduleId"))
{
int moduleId = int.Parse(Session["moduleId"].ToString());
pNums.Module mod = (pNums.Module)Enum.ToObject(typeof(pNums.Module), moduleId);
switch (mod)
{
case pNums.Module.Products:
if (utils.verifySession("product"))
{
oProduct prod = (oProduct)Session["product"];
txtName.Value = "Attachment - " + prod.display;
}
break;
case pNums.Module.Surface:
if (utils.verifySession("surface"))
{
oSurface surface = (oSurface)Session["surface"];
txtName.Value = "Attachment - " + surface.surface;//not ideal - should rather try and get field that is more specific to the surface item
}
break;
case pNums.Module.Notes:
if (utils.verifySession("note"))
{
oNote note = (oNote)Session["note"];
txtName.Value = "Attachment - " + note.title;//not ideal - should rather try and get field that is more specific to the note item
}
break;
case pNums.Module.Task:
if (utils.verifySession("task"))
{
oTask task = (oTask)Session["task"];
txtName.Value = "Attachment - " + task.name;
}
break;
default:
break;
}
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Edit the Attachment page
///
///
///
protected void lnkEdit_Click(object sender, EventArgs e)
{
oAttachment _Attachment = new oAttachment();
ArrayList catList = new ArrayList();
try
{
if (sender.GetType() == typeof(LinkButton))
{
//
int recId = int.Parse(((LinkButton)sender).CommandArgument);
if (recId > 0)
{
Session["AttachmentId"] = recId;
int moduleId = 0;
if (utils.verifySession("moduleId"))
moduleId = int.Parse(Session["moduleId"].ToString());
if (moduleId == pNums.Module.Task.GetHashCode())
catList = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "recId", recId.ToString(), "", "pal_", true);
else
catList = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "recId", recId.ToString());
//enumerate list
foreach (oAttachment cat in catList)
{
//assign object
_Attachment = cat;
//populate page form
PopulatePageFormValues(ref _Attachment);
Session["Attachment"] = _Attachment;
break;
}
//show Attachment Form
TogglePanels("pnlAttachmentPage");
}
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Remove the Attachment
///
///
///
protected void lnkRemove_Click(object sender, EventArgs e)
{
try
{
if (sender.GetType() == typeof(LinkButton))
{
//
int recId = int.Parse(((LinkButton)sender).CommandArgument);
ArrayList attachments = new ArrayList();
int moduleId = 0;
if (utils.verifySession("moduleId"))
moduleId = int.Parse(Session["moduleId"].ToString());
if (moduleId == pNums.Module.Task.GetHashCode())//get attachment from the cloud
attachments = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "recId", recId.ToString(), "", "pal_", true);
else
attachments = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "recId", recId.ToString());
foreach (oAttachment attach in attachments)
{
bool deleted = false;
//Delete attachment record
if (moduleId == pNums.Module.Task.GetHashCode())
deleted = xData.DeleteTyped("recId", attach.recId.ToString(), typeof(oAttachment), "pal", true);
else
deleted = xData.DeleteTyped("recId", attach.recId.ToString(), typeof(oAttachment));
if (deleted)
{
if (attach.typeId == pNums.AttachmentType.Document.GetHashCode())
{
string filePath = String.Empty;
filePath = Server.MapPath("~/upload/" + attach.folderName + "/" + attach.fileName);
if (File.Exists(filePath))
{ File.Delete(filePath); }
}
else if (attach.typeId == pNums.AttachmentType.Image.GetHashCode())
{
string thumbPath = String.Empty;
thumbPath = Server.MapPath("~/upload/" + attach.folderName + "/thumb/" + attach.fileName);
string imagePath = String.Empty;
imagePath = Server.MapPath("~/upload/" + attach.folderName + "/image/" + attach.fileName);
if (File.Exists(thumbPath))
{ File.Delete(thumbPath); }
if (File.Exists(imagePath))
{ File.Delete(imagePath); }
}
//rebind pages grid
BindAttachmentData();
pnlResult.Visible = true;
//display successful result
lblResult.Text = "the selected Attachment was deleted successfully.";
}
break;
}
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Cancel
///
///
///
protected void btnCancel_Click(object sender, EventArgs e)
{
try
{
Session["AttachmentId"] = 0;
utils.disposeSession("Attachment");
BindAttachmentData();
//show cat list
TogglePanels("pnlAttachmentList");
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Save Click
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
oAttachment Attachment = new oAttachment();
try
{
ViewState["newNote"] = null;
if (utils.verifySession("entityId") && utils.verifySession("moduleId"))
{
if (utils.verifySession("Attachment"))//update mode
{
Attachment = (oAttachment)Session["Attachment"];
//save form values
SavePageFormValues(ref Attachment);
if (Attachment.recId > 0)
{
Attachment.display = txtName.Value;
if (Attachment.moduleId == pNums.Module.Task.GetHashCode())
xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment, "pal_", true);
else
xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment);
utils.disposeSession("Attachment");
}
}
BindAttachmentData();
SetupAttachmentScreen();
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Selected Index Changed Event
///
///
///
protected void ddAttachmentType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (ddAttachmentType.SelectedValue == pNums.AttachmentType.Document.GetHashCode().ToString())
{
pnlUploadDocument.Visible = true;
pnlUploadImage.Visible = false;
chkPrimary.Visible = false;
}
else
{
pnlUploadDocument.Visible = false;
pnlUploadImage.Visible = true;
chkPrimary.Visible = true;
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Upload the image
///
///
///
protected void radImageUpload_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
{
oAttachment Attachment = new oAttachment();
try
{
if (!utils.verifySession("user"))
{
Response.Redirect("/home", false);
return;
}
oUser user = (oUser)Session["user"];
if (utils.verifySession("entityId") && utils.verifySession("moduleId"))
{
if (utils.verifySession("Attachment"))//update mode
{
Attachment = (oAttachment)Session["Attachment"];
}
int moduleId = 0;
int entityId = 0;
string fieldName = String.Empty;
if (utils.verifySession("fieldNameFile"))
fieldName = Session["fieldNameFile"].ToString();
/* CVH 2016-05-03 When adding new attachment to History log, need to create a new note to link attachment to */
if (this.HistorySurfaceItemId == 0)
{
moduleId = int.Parse(Session["moduleId"].ToString());
entityId = int.Parse(Session["entityId"].ToString());
}
else
{
oNote note = new oNote();
if (ViewState["newNote"] == null)
{
int fieldId = 0;
foreach (oSurfaceField field in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceField), "surfaceFieldName,isActive", fieldName + ",1"))
{
fieldId = field.recId;
}
//save new note to link attachment to
note.caption = "New file(s) were added.";
note.dateSaved = System.DateTime.Now;
note.entityId = this.HistorySurfaceItemId; //entity is the surface Item Id
note.fieldName = fieldId.ToString();
note.isActive = true;
note.moduleId = moduleId; //module note
note.title = "File(s) added";
note.typeId = (int)pNums.NoteType.General;
note.userIdSaved = user.recId;
if (moduleId == pNums.Module.Task.GetHashCode())
note.recId = xData.SaveTyped("recId", typeof(oNote), note, "pal_", true);
else
note.recId = xData.SaveTyped("recId", typeof(oNote), note);
ViewState["newNote"] = note;
}
else
{
note = (oNote)ViewState["newNote"];
}
Session["note"] = note;
moduleId = pNums.Module.Notes.GetHashCode();
entityId = note.recId;
}
Attachment.entityId = entityId;
Attachment.moduleId = moduleId;
Attachment.fieldName = fieldName;
Attachment.typeId = int.Parse(ddAttachmentType.SelectedValue);
int userId = user.recId;
if (Attachment.recId > 0)
{
Attachment.userIdUpdated = userId;
Attachment.dateUpdated = System.DateTime.Now;
}
else
{
Attachment.userIdSaved = userId;
Attachment.dateSaved = System.DateTime.Now;
}
pNums.Module mod = (pNums.Module)Enum.ToObject(typeof(pNums.Module), Attachment.moduleId);
switch (mod)
{
case pNums.Module.Products:
Attachment.folderName = "products";
break;
case pNums.Module.Surface:
if (!utils.verifySession("surfaceItem"))
Session["surfaceItem"] = (oSurfaceItem)xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", entityId.ToString())[0];
if (!utils.verifySession("surface"))
Session["surface"] = (oSurface)xData.GetTypedByCriteriaSpecific("recId", typeof(oSurface), "recId", ((oSurfaceItem)Session["surfaceItem"]).surfaceId.ToString())[0];
string folderName = "surface/";
folderName += ((oSurface)Session["surface"]).recId.ToString();
folderName += "/";
folderName += ((oSurfaceItem)Session["surfaceItem"]).recId.ToString();
Attachment.folderName = folderName;
break;
case pNums.Module.Notes:
if (!utils.verifySession("note"))
{
string notefolderName = "note/";
notefolderName += ((oNote)Session["note"]).recId.ToString();
Attachment.folderName = notefolderName;
}
break;
case pNums.Module.Task:
if (!utils.verifySession("task"))
{
string taskFolderName = "task/";
taskFolderName += ((oTask)Session["task"]).recId.ToString();
Attachment.folderName = taskFolderName;
//GR include source path because the attachment record will be global
Attachment.sourcePath = ConfigurationManager.AppSettings["WebAddy"];
}
break;
default:
Attachment.folderName = "attachments";
break;
}
Attachment.display = e.File.FileName.Remove(e.File.FileName.LastIndexOf("."));
Attachment.fileName = Attachment.entityId + "_" + utils.RandomString(9, true) + e.File.FileName.Substring(e.File.FileName.LastIndexOf("."));
string imagePath = Server.MapPath("~/upload/" + Attachment.folderName + "/image/");
string thumbPath = Server.MapPath("~/upload/" + Attachment.folderName + "/thumb/");
string tempPath = Server.MapPath("~/upload/" + Attachment.folderName + "/temp/");
utils.validateFolder(imagePath);
utils.validateFolder(thumbPath);
utils.validateFolder(tempPath);
//upload file
e.File.SaveAs(tempPath + Attachment.fileName);
//resize and compress
utils.ResizeImage(tempPath + Attachment.fileName, imagePath, Attachment.fileName, 400, true);
//resize and crop
utils.ResizeCropImagePrecise(imagePath + Attachment.fileName, thumbPath, Attachment.fileName, 383, 264, false);
//save form values
SavePageFormValues(ref Attachment);
if (Attachment.recId > 0)
{
if (Attachment.moduleId == pNums.Module.Task.GetHashCode())//update attachment to cloud
xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment, "pal_", true);
else
xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment);
utils.disposeSession("Attachment");
}
else
{
if (Attachment.moduleId == pNums.Module.Task.GetHashCode())//save attachment to cloud
Attachment.recId = xData.SaveTyped("recId", typeof(oAttachment), Attachment, "pal_", true);
else
Attachment.recId = xData.SaveTyped("recId", typeof(oAttachment), Attachment);
if (Attachment.recId > 0)
{ }
}
btnSave.Enabled = true;
btnSave.CssClass = "btn btn-info";
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Document upload
///
///
///
protected void radDocUpload_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
{
oAttachment Attachment = new oAttachment();
try
{
if (!utils.verifySession("user"))
{
Response.Redirect("/home", false);
return;
}
oUser user = (oUser)Session["user"];
if (utils.verifySession("entityId") && utils.verifySession("moduleId"))
{
if (utils.verifySession("Attachment"))//update mode
{
Attachment = (oAttachment)Session["Attachment"];
}
int moduleId = 0;
int entityId = 0;
string fieldName = String.Empty;
if (utils.verifySession("fieldNameFile"))
fieldName = Session["fieldNameFile"].ToString();
/* CVH 2016-05-03 When adding new attachment to History log, need to create a new note to link attachment to */
if (this.HistorySurfaceItemId == 0)
{
moduleId = int.Parse(Session["moduleId"].ToString());
entityId = int.Parse(Session["entityId"].ToString());
}
else
{
oNote note = new oNote();
if (ViewState["newNote"] == null)
{
int fieldId = 0;
foreach (oSurfaceField field in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceField), "surfaceFieldName,isActive", fieldName + ",1"))
{
fieldId = field.recId;
}
//save new note to link attachment to
note.caption = "New file(s) were added.";
note.dateSaved = System.DateTime.Now;
note.entityId = this.HistorySurfaceItemId; //entity is the surface Item Id
note.fieldName = fieldId.ToString();
note.isActive = true;
note.moduleId = moduleId; //surface note
note.title = "File(s) added";
note.typeId = (int)pNums.NoteType.General;
note.userIdSaved = user.recId;
note.recId = xData.SaveTyped("recId", typeof(oNote), note);
ViewState["newNote"] = note;
}
else
{
note = (oNote)ViewState["newNote"];
}
Session["note"] = note;
moduleId = pNums.Module.Notes.GetHashCode();
entityId = note.recId;
}
Attachment.entityId = entityId;
Attachment.moduleId = moduleId;
Attachment.fieldName = fieldName;
Attachment.typeId = int.Parse(ddAttachmentType.SelectedValue);
int userId = user.recId;
if (Attachment.recId > 0)
{
Attachment.userIdUpdated = userId;
Attachment.dateUpdated = System.DateTime.Now;
}
else
{
Attachment.userIdSaved = userId;
Attachment.dateSaved = System.DateTime.Now;
}
pNums.Module mod = (pNums.Module)Enum.ToObject(typeof(pNums.Module), Attachment.moduleId);
switch (mod)
{
case pNums.Module.Products:
Attachment.folderName = "documents";
break;
case pNums.Module.Surface:
if (!utils.verifySession("surfaceItem"))
Session["surfaceItem"] = (oSurfaceItem)xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", entityId.ToString())[0];
if (!utils.verifySession("surface"))
Session["surface"] = (oSurface)xData.GetTypedByCriteriaSpecific("recId", typeof(oSurface), "recId", ((oSurfaceItem)Session["surfaceItem"]).surfaceId.ToString())[0];
string folderName = "surface/";
folderName += ((oSurface)Session["surface"]).recId.ToString();
folderName += "/";
folderName += ((oSurfaceItem)Session["surfaceItem"]).recId.ToString();
Attachment.folderName = folderName;
break;
case pNums.Module.Notes:
if (!utils.verifySession("note"))
{
string notefolderName = "note/";
notefolderName += ((oNote)Session["note"]).recId.ToString();
Attachment.folderName = notefolderName;
}
break;
case pNums.Module.Task:
if (!utils.verifySession("task"))
{
string taskFolderName = "task/";
taskFolderName += ((oTask)Session["task"]).recId.ToString();
Attachment.folderName = taskFolderName;
//GR include source path because the attachment record will be global
Attachment.sourcePath = ConfigurationManager.AppSettings["WebAddy"];
}
break;
default:
Attachment.folderName = "documents";
break;
}
Attachment.display = e.File.FileName.Remove(e.File.FileName.LastIndexOf("."));
Attachment.fileName = Attachment.entityId + "_" + utils.RandomString(9, true) + e.File.FileName.Substring(e.File.FileName.LastIndexOf("."));
string docPath = Server.MapPath("~/upload/" + Attachment.folderName + "/");
utils.validateFolder(docPath);
//upload file
e.File.SaveAs(docPath + Attachment.fileName);
//save form values
SavePageFormValues(ref Attachment);
if (Attachment.recId > 0)
{
if (Attachment.moduleId == pNums.Module.Task.GetHashCode())//update attachment to cloud
xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment, "pal_", true);
else
xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment);
utils.disposeSession("Attachment");
}
else
{
if (Attachment.moduleId == pNums.Module.Task.GetHashCode())//save attachment to cloud
Attachment.recId = xData.SaveTyped("recId", typeof(oAttachment), Attachment, "pal_", true);
else
Attachment.recId = xData.SaveTyped("recId", typeof(oAttachment), Attachment);
if (Attachment.recId > 0)
{ }
}
btnSave.Enabled = true;
btnSave.CssClass = "btn btn-info";
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
///
/// Item Data Bound
///
///
///
protected void rptAttachments_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
HiddenField hfType = e.Item.FindControl("hfType") as HiddenField;
if (hfType != null)
{
Label lblType = e.Item.FindControl("lblType") as Label;
if (hfType.Value == pNums.AttachmentType.Document.GetHashCode().ToString())
{
//Image imgThumbnail = e.Item.FindControl("imgThumbnail") as Image;
Label lblFileName = e.Item.FindControl("lblFileName") as Label;
lblType.Text = "Document";
//imgThumbnail.ImageUrl = "/upload/Documents/" + lblFileName.Text;
}
else
{
lblType.Text = "Image";
}
}
LinkButton lnkEdit = e.Item.FindControl("lnkEdit") as LinkButton;
HtmlGenericControl div = e.Item.FindControl("divAttachmentLink") as HtmlGenericControl;
if (lnkEdit != null && div != null)
{
oAttachment att = new oAttachment();
foreach (oAttachment attach in xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "recId", lnkEdit.CommandArgument))
{
att = attach;
break;
}
StringBuilder formbuilder = new StringBuilder();
string user = "";
foreach (ovUserShared usr in xData.GetTypedByCriteriaSpecific("recId", typeof(ovUserShared), "recId", att.userIdSaved.ToString(), "", "pal_"))
{
user = usr.name;
break;
}
string details = "(" + user + " - " + utils.fixDate(att.dateSaved) + ")";
string fileLocation = "";
string pathLocation = "";
string hyperlink = "";
if (att.typeId == (int)pNums.AttachmentType.Document)
{
fileLocation = Server.MapPath("~/upload/" + att.folderName + "/file/" + att.fileName);
pathLocation = att.sourcePath + "/upload/" + att.folderName + "/file/" + att.fileName;
if (!File.Exists(fileLocation))
{
fileLocation = fileLocation.Replace("/file", "");
pathLocation = pathLocation.Replace("/file", "");
}
hyperlink = "
";
}
else
{
fileLocation = Server.MapPath("~/upload/" + att.folderName + "/image/" + att.fileName);
pathLocation = att.sourcePath + "/upload/" + att.folderName + "/image/" + att.fileName;
hyperlink = "
";
}
formbuilder.AppendLine(hyperlink);
div.InnerHtml = formbuilder.ToString();
}
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Item Created event
///
///
///
protected void rptAttachments_ItemCreated(object sender, RepeaterItemEventArgs e)
{
//ScriptManager scriptMan = ScriptManager.GetCurrent(this.Page);
//LinkButton lnkEdit = e.Item.FindControl("lnkEdit") as LinkButton;
//LinkButton lnkRemove = e.Item.FindControl("lnkRemove") as LinkButton;
//if (lnkEdit != null)
//{
// lnkEdit.Click += lnkEdit_Click;
// scriptMan.RegisterAsyncPostBackControl(lnkEdit);
//}
//if (lnkRemove != null)
//{
// lnkRemove.Click += lnkRemove_Click;
// scriptMan.RegisterPostBackControl(lnkRemove);
//}
}
protected void btnBackToNotes_Click(object sender, EventArgs e)
{
try
{
CommandEventArgs args = new CommandEventArgs("LoadNotesList", "");
RaiseBubbleEvent(null, args);
}
catch (Exception ex)
{
exception.HandleException("Attachments:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
#endregion
}