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.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; /* CVH TODO - Snapshot management popup - Delete function for old temporary images - maybe rename temp image to canvas1_tmp to limit results returned on search - Tooltip for task point - Need to centre align the overlay image (when smaller image from tablet) - Add number of attachments / notes in brackets - Add update panel to overlay control - Add task priority, task code (VIZ-01) */ public partial class controls_module_taskOverlay : System.Web.UI.UserControl//, IPostBackEventHandler { private const string taskImagePath = "~/upload/tasks/"; #region properties public int CanvasId { get { if (ViewState["canvasId"] == null) return 0; else return (int)ViewState["canvasId"]; } set { ViewState["canvasId"] = value; } } /* Name of temp snapshot file saved before redirecting */ public string SnapshotName { get { if (ViewState["snapshotName"] == null) return ""; else return ViewState["snapshotName"].ToString(); } set { ViewState["snapshotName"] = value; } } //public int SnapshotId //{ // get // { // if (ViewState["snapshotId"] == null) // return 0; // else // return int.Parse(ViewState["snapshotId"].ToString()); // } // set // { // ViewState["snapshotId"] = value; // } //} #endregion #region methods private void BindCustCodes() { oSetup _setup = handler.ReturnSetup(); DataTable dt = xData.GetTypedByCriteriaSpecificTable("recId", typeof(oCustomer), "isActive", "1", "code", "pal_", true); ddCustomerCode.DataSource = dt; ddCustomerCode.DataTextField = "customer"; ddCustomerCode.DataValueField = "code"; ddCustomerCode.DataBind(); ddCustomerCode.SelectedValue = _setup.code; ddCustomerCode.Enabled = false; } private void BindVersions() { try { //GR - Added to get customer code //get canvas name string canvasName = String.Empty; DataTable dt = new DataTable(); dt.Columns.Add("Display"); dt.Columns.Add("Value"); foreach (oCanvas canv in xData.GetTypedByCriteriaSpecific("recId", typeof(oCanvas), "recId", this.CanvasId.ToString())) { canvasName = canv.name; break; } foreach (oTaskSnapshot snap in xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskSnapshot), "isActive,canvasId,customerCode", "1," + this.CanvasId.ToString() + "," + ddCustomerCode.SelectedValue.ToString(), "version DESC", "pal_", true)) { DataRow row = dt.NewRow(); string displayName = ""; if (snap.canvasId == -1) displayName = "Home"; else displayName = canvasName; row["Display"] = displayName + " - v" + snap.version; row["Value"] = snap.recId; dt.Rows.Add(row); } /* DataTable dt = xData.GetCustomTypedTable( "SELECT CASE WHEN s.canvasId = -1 THEN 'Home' ELSE c.display END + ' - v' + CONVERT(VARCHAR,s.version) AS Display, s.recId AS Value " + "FROM pal_TaskSnapshot s " + "LEFT OUTER JOIN pal_Canvas c ON c.recId = s.canvasId " + "WHERE s.canvasId = " + this.CanvasId + " " + "AND s.isActive = 1 AND customerCode = '" + ddCustomerCode.SelectedValue.ToString() + "' " + "ORDER BY s.version DESC;", true); */ if (dt == null || dt.Rows.Count <= 0) throw new Exception("No image versions found."); //else //{ // foreach (DataRow row in dt.Rows) // { // row["Display"] = canvasName; // } //} ddSnapshotVersions.Items.Clear(); ddSnapshotVersions.DataSource = dt; ddSnapshotVersions.DataTextField = "Display"; ddSnapshotVersions.DataValueField = "Value"; ddSnapshotVersions.DataBind(); } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); //don't redirect } } private void LoadSnapshot(string imageFilename, string sourcePath) { try { if (sourcePath != String.Empty) imgOverlay.ImageUrl = sourcePath + "/upload/tasks/" + imageFilename; else imgOverlay.ImageUrl = taskImagePath + imageFilename; using (System.Drawing.Image img = System.Drawing.Image.FromFile(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename)) { divOverlayColour.Style.Add("Height", (img.Height + 20) + "px"); divOverlayColour.Style.Add("Width", (img.Width + 20) + "px"); divOverlayWrapper1.Style.Add("Height", (img.Height + 20) + "px"); divOverlayWrapper1.Style.Add("Width", (img.Width + 20) + "px"); divOverlayImg.Style.Add("Height", img.Height + "px"); divOverlayImg.Style.Add("Width", img.Width + "px"); imgOverlay.Height = img.Height; imgOverlay.Width = img.Width; } } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } private void LoadTasks(int snapshotId, int canvasId) { try { oSetup _setup = handler.ReturnSetup(); //remove current divs before adding new for (int i = divOverlayWrapper1.Controls.Count - 1; i > 0; i--) { Control c = (Control)divOverlayWrapper1.Controls[i]; if (c.ID != null && c.ID.StartsWith("divTask")) divOverlayWrapper1.Controls.Remove(c); } //get list of all users ArrayList users = xData.GetTypedByCriteriaSpecific("recId", typeof(oUser), "isActive,customerCode,userType", "1," + _setup.code + ",~>" + pNums.UserType.PowerUser.GetHashCode(), "surname", "pal_", true); if (_setup.code != "EVOL-1") { ArrayList evolUsers = xData.GetTypedByCriteriaSpecific("recId", typeof(oUser), "isActive, customerCode, userType", "1,EVOL-1," + pNums.UserType.AdminUser.GetHashCode(), "surname", "pal_", true); foreach (oUser evolUsr in evolUsers) { users.Add(evolUsr); } } //get list of all statuses ArrayList statuses = xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskStatus), "isActive", "1", "taskStatus", "pal_", true); int taskSeq = 0; foreach (oTask task in xData.GetTypedByCriteriaSpecific("recId", typeof(oTask), "canvasId,taskSnapshotId", canvasId + "," + snapshotId, "", "pal_", true)) { int adjust = taskSeq * 85; //width of the div point taskSeq++; LinkButton btn = new LinkButton(); //btn.Text = taskSeq.ToString(); btn.Text = task.taskNumber == "" ? "NONE" : task.taskNumber; btn.ID = "btnDivTask" + task.recId; btn.ForeColor = System.Drawing.Color.Red; btn.CommandArgument = task.recId.ToString(); btn.CommandName = btn.ID; btn.Command += btnDivTask_Click; //get task assignee name string assignee = ""; foreach (oUser usr in users) { if (usr.recId == task.assigneeUserId) assignee = usr.name; } //get task status name string statusDesc = ""; foreach (oTaskStatus status in statuses) { if (status.recId == task.taskStatusId) statusDesc = status.taskStatus; } //divs are moving about 15 points for each one added (think to do with div position relative). int left = task.taskSnapshotPosX - 35 - adjust; int top = task.taskSnapshotPosY - 5; HtmlGenericControl divPoint = new HtmlGenericControl(); divPoint.ID = "divTask" + task.recId; divPoint.Attributes.Add("class", "taskPoint task-tooltip"); divPoint.Attributes.Add("style", "left:" + left + "px;top:" + top + "px"); //divPoint.Attributes.Add("title", task.name); divPoint.Attributes.Add("data-toggle", "tooltip"); divPoint.Attributes.Add("data-html", "true"); divPoint.Attributes.Add("data-container", "#" + divOverlayWrapper1.ClientID); divPoint.Attributes.Add("data-placement", "auto top"); divPoint.Attributes.Add("title", "Subject: " + task.name + "
Assignee: " + assignee + "
Status: " + statusDesc); divPoint.Controls.Add(btn); divOverlayWrapper1.Controls.Add(divPoint); } } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } private void TaskClicked(int taskId) { try { //get task linked to div ArrayList taskList = xData.GetTypedByCriteriaSpecific("recId", typeof(oTask), "recId", taskId.ToString(), "", "pal_", true); if (taskList == null || taskList.Count <= 0) throw new Exception("Task linked to div could not be found."); oTask task = (oTask)taskList[0]; controls_module_task taskControl = (controls_module_task)FindControl("ucTask"); taskControl.Task = task; taskControl.CanvasId = task.canvasId; taskControl.SnapshotId = task.taskSnapshotId; taskControl.SnapshotX = task.taskSnapshotPosX; taskControl.SnapshotY = task.taskSnapshotPosY; taskControl.ActivePanel = "View"; taskControl.ReloadControl(); //open Task modal ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myEditTaskModal", "$('#modTask').modal();", true); } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } private void DeleteOldSnapshots() { try { if (utils.verifySession("user")) { int userId = ((oUser)Session["user"]).recId; ArrayList snapshots = xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskSnapshot), "canvasId,isActive,customerCode", this.CanvasId.ToString() + ",1," + ddCustomerCode.SelectedValue.ToString(), "recId", "pal_", true); for (int i = snapshots.Count - 1; i >= 0; i--) { oTaskSnapshot snap = (oTaskSnapshot)snapshots[i]; //if it is an old snapshot, or if the owner of the snapshot is the current user, check for tasks linked if (snap.dateCreated.Date < DateTime.Now.Date || snap.createdByUserId == userId) { ArrayList tasks = xData.GetTypedByCriteriaSpecific("recId", typeof(oTask), "canvasId,taskSnapshotId", snap.canvasId + "," + snap.recId, "", "pal_", true); if (tasks == null || tasks.Count <= 0) { File.Delete(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + snap.fileName); xData.DeleteTyped("recId", snap.recId.ToString(), typeof(oTaskSnapshot), "pal_", true); } } } } } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); } } #endregion #region events protected void Page_Load(object sender, EventArgs e) { try { //do also on Postback, otherwise div images are lost if (this.CanvasId == 0) { Response.Redirect("/home", false); } else if (!Page.IsPostBack) { BindCustCodes(); utils.validateFolder(Server.MapPath(taskImagePath)); if (!utils.verifySession("newTaskReload") || Session["newTaskReload"].ToString() == "") DeleteOldSnapshots(); //check for existing canvas image versions ArrayList list = xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskSnapshot), "customerCode,canvasId", ddCustomerCode.SelectedValue + "," + this.CanvasId.ToString(), "version DESC", "pal_", true); oTaskSnapshot snap = new oTaskSnapshot(); if (list == null || list.Count <= 0) { //if none found, find temporary snapshot and rename to v1 string imageFilename = "canvas" + this.CanvasId + "_v1.png"; if (!File.Exists(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename)) File.Copy(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + this.SnapshotName + ".png", System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename); //insert snapshot record snap.canvasId = this.CanvasId; int userId = 0; if (utils.verifySession("User")) { oUser usr = (oUser)Session["User"]; userId = usr.recId; } snap.customerCode = ddCustomerCode.SelectedValue; snap.sourcePath = ConfigurationManager.AppSettings["WebAddy"]; snap.createdByUserId = userId; snap.dateCreated = System.DateTime.Now; snap.fileName = imageFilename; snap.isActive = true; snap.version = 1; snap.recId = xData.SaveTyped("recId", typeof(oTaskSnapshot), snap, "pal_", true); } else if (utils.verifySession("newTaskReload") && Session["newTaskReload"].ToString() != "") { int snapshotId = 0; int.TryParse(Session["newTaskReload"].ToString(), out snapshotId); utils.disposeSession("newTaskReload"); if (snapshotId == 0) snap = (oTaskSnapshot)list[0]; else { foreach (oTaskSnapshot snapTemp in list) { if (snapTemp.recId == snapshotId) { snap = snapTemp; break; } } } } else { /* CVH 2016-06-21 Need to always show new snapshot by default, don't show highest saved version */ //snap = (oTaskSnapshot)list[0]; oTaskSnapshot snapMax = (oTaskSnapshot)list[0]; int newVer = snapMax.version + 1; string imageFilename = "canvas" + this.CanvasId + "_v" + newVer + ".png"; if (!File.Exists(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename)) File.Copy(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + this.SnapshotName + ".png", System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename); //insert snapshot record snap.canvasId = this.CanvasId; int userId = 0; if (utils.verifySession("User")) { oUser usr = (oUser)Session["User"]; userId = usr.recId; } snap.customerCode = ddCustomerCode.SelectedValue; snap.sourcePath = ConfigurationManager.AppSettings["WebAddy"]; snap.createdByUserId = userId; snap.dateCreated = System.DateTime.Now; snap.fileName = imageFilename; snap.isActive = true; snap.version = newVer; snap.recId = xData.SaveTyped("recId", typeof(oTaskSnapshot), snap, "pal_", true); } //bind version drop down list BindVersions(); if (ddSnapshotVersions.Items.FindByValue(snap.recId.ToString()) != null) ddSnapshotVersions.Items.FindByValue(snap.recId.ToString()).Selected = true; Session["overlaySnapshotId"] = snap.recId; LoadSnapshot(snap.fileName, snap.sourcePath); LoadTasks(snap.recId, snap.canvasId); } else { //need to load tasks //ArrayList list = xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskSnapshot), "recId", ddSnapshotVersions.SelectedItem.Value, "", "pal_", true); //if (list != null && list.Count > 0) //{ // oTaskSnapshot snap = (oTaskSnapshot)list[0]; // LoadTasks(snap.recId, this.CanvasId); //} } } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void Page_Init(object sender, EventArgs e) { try { int snapshotId = 0; if (utils.verifySession("overlaySnapshotId")) snapshotId = int.Parse(Session["overlaySnapshotId"].ToString()); if (snapshotId != 0) { //need to load tasks ArrayList list = xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskSnapshot), "recId", snapshotId.ToString(), "", "pal_", true); if (list != null && list.Count > 0) { oTaskSnapshot snap = (oTaskSnapshot)list[0]; LoadTasks(snap.recId, snap.canvasId); } } } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void imgOverlay_Click(object sender, ImageClickEventArgs e) { try { controls_module_task taskControl = (controls_module_task)FindControl("ucTask"); taskControl.Task = null; taskControl.CanvasId = this.CanvasId; taskControl.SnapshotId = int.Parse(ddSnapshotVersions.SelectedItem.Value); taskControl.SnapshotX = e.X; taskControl.SnapshotY = e.Y; taskControl.ActivePanel = "Form"; taskControl.ReloadControl(); //open Task modal ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myTaskModal", "$('#modTask').modal();", true); } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void btnClose_Click(object sender, EventArgs e) { try { if (this.CanvasId == -1) { Response.Redirect("../Default.aspx", false); } else { //get name for canvas ID and reroute ArrayList list = xData.GetTypedByCriteriaSpecific("recId", typeof(oCanvas), "recId", this.CanvasId.ToString()); if (list == null || list.Count <= 0) { Response.Redirect("/home", false); return; } //throw new Exception("Canvas name for Id " + this.CanvasId + " could not be determined."); oCanvas canvas = (oCanvas)list[0]; Response.Redirect("/pages/" + canvas.name, false); } } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void btnNewSnapshot_Click(object sender, EventArgs e) { try { if (this.CanvasId == 0) { Response.Redirect("/home", false); return; } //throw new Exception("Invalid canvas ID 0."); string imageFilename = ""; string canvasImageName = "canvas" + this.CanvasId + "_v"; //check for existing canvas image versions ArrayList list = xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskSnapshot), "canvasId,customerCode", this.CanvasId.ToString() + "," + ddCustomerCode.SelectedValue, "version DESC", "pal_", true); oTaskSnapshot snap = new oTaskSnapshot(); if (list == null || list.Count <= 0) { //there should always be a version 1 after page load, but if for some reason not, take temp snapshot and create version 1 //if none found, find temporary snapshot and rename to v1 imageFilename = "canvas" + this.CanvasId + "_v1.png"; if (!File.Exists(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename)) File.Copy(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + this.SnapshotName + ".png", System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename); //insert snapshot record snap.canvasId = this.CanvasId; int userId = 0; if (utils.verifySession("User")) { oUser usr = (oUser)Session["User"]; userId = usr.recId; } snap.customerCode = ddCustomerCode.SelectedValue; snap.sourcePath = ConfigurationManager.AppSettings["WebAddy"]; snap.createdByUserId = userId; snap.dateCreated = System.DateTime.Now; snap.fileName = imageFilename; snap.isActive = true; snap.version = 1; snap.recId = xData.SaveTyped("recId", typeof(oTaskSnapshot), snap, "pal_", true); } else { oTaskSnapshot snapMax = (oTaskSnapshot)list[0]; int newVer = snapMax.version + 1; imageFilename = canvasImageName + newVer + ".png"; if (!File.Exists(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename)) File.Copy(System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + this.SnapshotName + ".png", System.Web.Hosting.HostingEnvironment.MapPath(taskImagePath) + imageFilename); //insert snapshot record snap.canvasId = this.CanvasId; int userId = 0; if (utils.verifySession("User")) { oUser usr = (oUser)Session["User"]; userId = usr.recId; } snap.customerCode = ddCustomerCode.SelectedValue; snap.sourcePath = ConfigurationManager.AppSettings["WebAddy"]; snap.createdByUserId = userId; snap.dateCreated = System.DateTime.Now; snap.fileName = imageFilename; snap.isActive = true; snap.version = newVer; snap.recId = xData.SaveTyped("recId", typeof(oTaskSnapshot), snap, "pal_", true); } LoadSnapshot(snap.fileName, snap.sourcePath); LoadTasks(snap.recId, snap.canvasId); BindVersions(); ddSnapshotVersions.SelectedItem.Value = snap.recId.ToString(); Session["overlaySnapshotId"] = snap.recId; //upOverlay.Update(); } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } /// /// Selected index changed event /// /// /// protected void ddCustomerCode_SelectedIndexChanged(object sender, EventArgs e) { try { BindVersions(); } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void ddSnapshotVersions_SelectedIndexChanged(object sender, EventArgs e) { try { ArrayList list = xData.GetTypedByCriteriaSpecific("recId", typeof(oTaskSnapshot), "recId", ddSnapshotVersions.SelectedValue.ToString(), "", "pal_", true); if (list == null || list.Count <= 0) throw new Exception("Snapshot version was not found."); oTaskSnapshot snap = (oTaskSnapshot)list[0]; this.CanvasId = snap.canvasId; Session["overlaySnapshotId"] = snap.recId; LoadSnapshot(snap.fileName, snap.sourcePath); LoadTasks(snap.recId, snap.canvasId); } catch (Exception ex) { exception.HandleException("Overlay:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } void btnDivTask_Click(object sender, CommandEventArgs e) { //ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myTaskModal", "alert('div clicked!');", true); TaskClicked(int.Parse(e.CommandArgument.ToString())); } #endregion }