using framework_business;
using framework_library;
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;
using System.IO;
using System.Configuration;
public partial class controls_module_surfacenotes : UserControl, ISurfaceControlBase
{
#region methods
///
/// ReloadControl
///
///
public void ReloadControl(oSurfaceFieldData controlFieldData, bool alternateView = false, bool readOnly = false)
{
try
{
SurfaceId = controlFieldData.surfaceId;
SurfaceItemId = controlFieldData.surfaceItemId;
foreach (oSurfaceField field in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceField), "recId", controlFieldData.surfaceFieldID.ToString()))
{
SurfaceFieldId = field.recId;
lblNoteTitle.Text = field.surfaceFieldDisplay;
}
if (alternateView)
RecordLimit = 3;
BindNotes();
//if (this.Confidential)
// lblNoteTitle.Text = "CONFIDENTIAL";
}
catch (Exception ex)
{
exception.HandleException("notes:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Save Control Data
///
///
public void SaveControlData(ref oSurfaceFieldData _surfaceFieldData)
{
}
///
/// Populate surface note
///
private void PopulateSurfaceNote()
{
try
{
foreach (oNote note in xData.GetTypedByCriteriaSpecific("recId", typeof(oNote), "recId", NoteId.ToString()))
{
radSurfaceNote.Content = note.caption;
BindAttachments(note.recId, divSurfaceNoteFiles);
}
}
catch (Exception ex)
{
exception.HandleException("notes:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// BaindAttachments
///
///
///
private void BindAttachments(int recId, HtmlGenericControl filesDiv)
{
try
{
StringBuilder formBuilder = new StringBuilder();
//get all attachments linked to note
//bool firstAttachment = true;
//foreach (oAttachment att in xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId", (int)pNums.Module.Notes + "," + recId.ToString(), "", "pal_"))
//{
// if (firstAttachment)
// {
// formBuilder.AppendLine("
");
// firstAttachment = false;
// }
// string fileLocation = "";
// if (att.typeId == (int)pNums.AttachmentType.Document)
// fileLocation = att.sourcePath + "/upload/" + att.folderName + "/file/" + att.fileName;
// else
// fileLocation = att.sourcePath + "/upload/" + att.folderName + "/image/" + att.fileName;
// string hyperlink = "
" + att.display + "
";
// formBuilder.Append("
" + hyperlink + "
");
//}
//CVH 2017-05-24 Arraylist always blank, datatable works fine
//ArrayList files = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId", (int)pNums.Module.Notes + "," + recId.ToString(), "");
DataTable dtFiles = xData.GetDynamicByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId", (int)pNums.Module.Notes + "," + recId.ToString());
if (dtFiles != null && dtFiles.Rows.Count > 0)
{
formBuilder.AppendLine("");
foreach (DataRow row in dtFiles.Rows)
{
string user = "";
foreach (ovUserShared usr in xData.GetTypedByCriteriaSpecific("recId", typeof(ovUserShared), "recId", row["userIdSaved"].ToString()))
{
user = usr.name;
break;
}
string details = "(" + user + " - " + utils.fixDate(row["dateSaved"].ToString()) + ")";
string fileLocation = "";
string pathLocation = "";
string hyperlink = "";
if (row["typeId"].ToString() == pNums.AttachmentType.Document.GetHashCode().ToString())
{
fileLocation = Server.MapPath("/upload/" + row["folderName"].ToString() + "/file/" + row["fileName"].ToString());
pathLocation = row["sourcePath"].ToString() + "/upload/" + row["folderName"].ToString() + "/file/" + row["fileName"].ToString();
if (!File.Exists(fileLocation))
{
fileLocation = fileLocation.Replace("/file", "");
pathLocation = pathLocation.Replace("/file", "");
}
hyperlink = "
" + row["display"].ToString() + "";
}
else
{
fileLocation = Server.MapPath("/upload/" + row["folderName"].ToString() + "/image/" + row["fileName"].ToString());
pathLocation = row["sourcePath"].ToString() + "/upload/" + row["folderName"].ToString() + "/image/" + row["fileName"].ToString();
hyperlink = "
" + row["display"].ToString() + "";
}
formBuilder.AppendLine(hyperlink);
}
formBuilder.AppendLine("
");
}
filesDiv.InnerHtml = formBuilder.ToString();
}
catch (Exception ex)
{
exception.HandleException("Task:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Get File typeImage
///
///
///
private string GetFileTypeImage(string fileLocation)
{
string imgSrc = "/images/doc.png";
try
{
string uri = String.Empty;
if (fileLocation.Contains("http"))
uri = fileLocation;
else
uri = Server.MapPath(fileLocation);
Icon icon = Icon.ExtractAssociatedIcon(uri);
if (icon != null)
{
System.Drawing.Image img = icon.ToBitmap();
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
string base64 = Convert.ToBase64String(byteArray);
imgSrc = String.Format("data:image/png;base64,{0}", base64);
}
}
catch (Exception ex)
{
exception.HandleException("Task:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
return imgSrc;
}
///
/// BindNotes
///
private void BindNotes()
{
//CVH 2017-05-05 Don't bind notes if surface item is 0, currently there are notes saved not linked to the surface item, shouldn't show those when adding a new surface item. Going forward notes will not be saved with a 0 item, we're bubbling to surface
if (SurfaceItemId == 0 || SurfaceId == 0)
return;
SurfaceNotes = xData.GetTypedByCriteriaSpecificTableTop("recId", typeof(ovNoteDetail), "typeId,isActive,entityId,fieldName", "1,1," + SurfaceItemId.ToString() + "," + SurfaceFieldId.ToString() + "", "dateSaved DESC", RecordLimit, "pal_");
rptNotes.DataSource = SurfaceNotes;
rptNotes.DataBind();
upNotes.Update();
}
#endregion
#region events
///
/// Page Init
///
///
///
protected void Page_Init(object sender, EventArgs e)
{
}
///
/// Page Load
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
//BindNotes();
}
///
/// add new note
///
///
///
protected void btnNew_Click(object sender, EventArgs e)
{
try
{
radSurfaceNote.Content = "";
radUploadSurfaceNotes.UploadedFiles.Clear();
divSurfaceNoteFiles.InnerHtml = "";
NoteId = 0;
//CVH 2017-05-05 Open surface group again. If it defaults to collapsed, it collapses when clicking add and then modal doesn't show until you manually expand the group again.
string script = "";
if (SurfaceGroupId != "")
script = "$('#" + SurfaceGroupId + "').collapse('show'); $('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceNote').modal();";
else
script = "$('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceNote').modal();";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceNoteModal", script, true);
}
catch (Exception ex)
{
exception.HandleException("notes:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// save note
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
oUser usr = new oUser();
if (utils.verifySession("user"))
usr = (oUser)Session["user"];
if (NoteId == 0)
{
if (SurfaceItemId == 0)
{
if (SurfaceId == 0)
return;
oSurfaceItem newItem = new oSurfaceItem();
newItem.createdBy = usr.recId;
newItem.dateCreated = DateTime.Now;
newItem.isActive = true; //setting item as true, otherwise it could be confusing if item doesn't show in list
newItem.isDeleted = false;
newItem.surfaceId = SurfaceId;
newItem.recId = xData.SaveTyped("recId", typeof(oSurfaceItem), newItem);
SurfaceItemId = newItem.recId;
CommandEventArgs args = new CommandEventArgs("SaveParentSurfaceItem", newItem.recId);
RaiseBubbleEvent(null, args);
}
oNote note = new oNote();
note.moduleId = pNums.Module.Surface.GetHashCode();
note.typeId = pNums.NoteType.General.GetHashCode();
note.entityId = SurfaceItemId;
note.title = "Surface Item Note";
note.isActive = true;
note.caption = radSurfaceNote.Content;
note.dateSaved = DateTime.Now;
note.userIdSaved = usr.recId;
note.fieldName = SurfaceFieldId.ToString();
NoteId = xData.SaveTyped("recId", typeof(oNote), note);
}
else
{
foreach (oNote note in xData.GetTypedByCriteriaSpecific("recId", typeof(oNote), "recId", NoteId.ToString()))
{
note.caption = radSurfaceNote.Content;
note.dateUpdated = DateTime.Now;
note.userIdUpdated = usr.recId;
xData.UpdateTyped("recId", NoteId.ToString(), typeof(oNote), note);
}
}
if (Attachment != null && Attachment.entityId == 0)
{
Attachment.entityId = NoteId;
xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment);
}
BindNotes();
//CVH 2017-05-05 Open surface group again. If it defaults to collapsed, it collapses when clicking add and then modal doesn't show until you manually expand the group again.
string script = "";
if (SurfaceGroupId != "")
script = "$('#" + SurfaceGroupId + "').collapse('show'); TaskFileUploadCancel(); $('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceNote').modal('hide');$(document.body).removeClass('modal-open');";
else
script = "TaskFileUploadCancel(); $('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceNote').modal('hide');$(document.body).removeClass('modal-open');";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceNoteModalClose", script, true);
}
catch (Exception ex)
{
exception.HandleException("notes:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
protected void btnCancelSurfaceNote_Click(object sender, EventArgs e)
{
try
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceNoteModalClose", "TaskFileUploadCancel(); $('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceNote').modal('toggle');", true);
}
catch (Exception ex)
{
exception.HandleException("notes:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// edit item
///
///
///
protected void lnkEdit_Click(object sender, EventArgs e)
{
try
{
if (sender.GetType() == typeof(LinkButton))
{
LinkButton lnkButton = (LinkButton)sender;
NoteId = int.Parse(((LinkButton)sender).CommandArgument);
PopulateSurfaceNote();
//CVH 2017-05-05 Open surface group again. If it defaults to collapsed, it collapses when clicking add and then modal doesn't show until you manually expand the group again.
string script = "";
if (SurfaceGroupId != "")
script = "$('#" + SurfaceGroupId + "').collapse('show'); $('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceNote').modal();";
else
script = "$('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceNote').modal();";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceNoteModal", script, true);
//ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceNoteModal", "$('.modSurfaceNote').modal();", true);
}
}
catch (Exception ex)
{
exception.HandleException("notes:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Note Item Data Bound
///
///
///
protected void rptNotes_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemIndex != -1)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblNoteId = (Label)e.Item.FindControl("lblNoteId");
Label lblEditUser = (Label)e.Item.FindControl("lblEditUser");
Label lblNoteHeaderEdit = (Label)e.Item.FindControl("lblNoteHeaderEdit");
if (lblEditUser.Text.Length > 0)
lblNoteHeaderEdit.Visible = true;
int noteId = Convert.ToInt32(lblNoteId.Text);
HtmlGenericControl divSurfaceNoteFiles = (HtmlGenericControl)e.Item.FindControl("divSurfaceNoteFiles");
BindAttachments(noteId, divSurfaceNoteFiles);
}
}
}
catch (Exception ex)
{
exception.HandleException("Task:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// fil uploaded
///
///
///
protected void radUploadSurfaceNotes_FileUploaded(object sender, FileUploadedEventArgs e)
{
Attachment = new oAttachment();
try
{
oUser user = new oUser();
if (utils.verifySession("user"))
user = (oUser)Session["user"];
else
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "userAlert", "alert('No user login detected. Operation aborted.');", true);
return;
}
Attachment.entityId = NoteId;
//Attachment.fieldName = hfFieldNameNote.Value;
Attachment.moduleId = (int)pNums.Module.Notes;
Attachment.isActive = true;
Attachment.typeId = e.File.ContentType.StartsWith("image") ? (int)pNums.AttachmentType.Image : (int)pNums.AttachmentType.Document;
string taskFolderName = "surface/";
taskFolderName += SurfaceItemId.ToString();
Attachment.folderName = taskFolderName;
/* CVH 2016-04-15 Add date and user */
int userId = user.recId;
Attachment.userIdSaved = userId;
Attachment.dateSaved = DateTime.Now;
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/");
string filePath = Server.MapPath("~/upload/" + Attachment.folderName + "/file/");
utils.validateFolder(imagePath);
utils.validateFolder(thumbPath);
utils.validateFolder(tempPath);
utils.validateFolder(filePath);
Attachment.sourcePath = ConfigurationManager.AppSettings["WebAddy"];
if (Attachment.typeId == pNums.AttachmentType.Image.GetHashCode())
{
//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);
}
else
{
//upload file
e.File.SaveAs(filePath + Attachment.fileName);
}
Attachment.recId = xData.SaveTyped("recId", typeof(oAttachment), Attachment);
}
catch (Exception ex)
{
exception.HandleException("surface:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]);
Response.Redirect("/error", false);
}
}
#endregion
#region properties
///
/// MPL getter and setter
///
private DataTable SurfaceNotes
{
get
{
if (ViewState["surfaceNotes"] != null)
{
return (DataTable)ViewState["surfaceNotes"];
}
else
{
return null;
}
}
set
{
ViewState["surfaceNotes"] = value;
}
}
private int SurfaceItemId
{
get
{
if (ViewState["noteSurfaceItemId"] != null)
return (int)ViewState["noteSurfaceItemId"];
else
return 0;
}
set
{
ViewState["noteSurfaceItemId"] = value;
}
}
private int SurfaceFieldId
{
get
{
if (ViewState["noteSurfaceFieldId"] != null)
return Convert.ToInt32(ViewState["noteSurfaceFieldId"]);
else
return 0;
}
set
{
ViewState["noteSurfaceFieldId"] = value;
}
}
private int SurfaceId
{
get
{
if (ViewState["noteSurfaceId"] != null)
return Convert.ToInt32(ViewState["noteSurfaceId"]);
else
return 0;
}
set
{
ViewState["noteSurfaceId"] = value;
}
}
private int NoteId
{
get
{
if (ViewState["noteId"] != null)
return (int)ViewState["noteId"];
else
return 0;
}
set
{
ViewState["noteId"] = value;
}
}
private int RecordLimit
{
get
{
if (ViewState["recordLimit"] == null)
return 0;
else
return (int)ViewState["recordLimit"];
}
set
{
ViewState["recordLimit"] = value;
}
}
private oAttachment Attachment
{
get
{
if (ViewState["attachment"] != null)
return (oAttachment)ViewState["attachment"];
else
return null;
}
set
{
ViewState["attachment"] = value;
}
}
public string SurfaceGroupId
{
get
{
if (ViewState["surfaceGroupId"] != null)
return ViewState["surfaceGroupId"].ToString();
else
return null;
}
set
{
ViewState["surfaceGroupId"] = value;
}
}
public bool Confidential
{
get
{
if (ViewState["confidential"] != null)
return bool.Parse(ViewState["confidential"].ToString());
else
return false;
}
set
{
ViewState["confidential"] = value;
}
}
#endregion
}