using framework_business; using framework_library; using System; using System.Collections; using System.Collections.Generic; using System.Data; 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_module_surfaceGallery : UserControl, ISurfaceControlBase { #region Methods public void ReloadControl(oSurfaceFieldData controlFieldData, bool alternateView = false, bool readOnly = false) { try { try { // Add ValidationGroup="Properties" to btnAddGalleryImage button ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; string parentName = parent.AppRelativeVirtualPath; int iLastIndex = parentName.LastIndexOf("/") + 1; string validate = parentName.Substring(iLastIndex).Replace("_form.ascx", ""); btnAddGalleryImage.ValidationGroup = validate; } catch { } SurfaceGalleryId = controlFieldData.surfaceId; SurfaceGalleryItemId = controlFieldData.surfaceItemId; foreach (oSurfaceField field in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceField), "recId", controlFieldData.surfaceFieldID.ToString())) { SurfaceGalleryFieldId = field.recId; lblGalleryTitle.Text = field.surfaceFieldDisplay; } if (alternateView) RecordLimit = 3; //BindImageGallery(); } catch (Exception ex) { exception.HandleException("surfaceGallery:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } public void SaveControlData(ref oSurfaceFieldData _surfaceFieldData) { if (SurfaceGalleryItemId == 0) SurfaceGalleryItemId = _surfaceFieldData.surfaceItemId; } private void BindImageGallery() { if (!(SurfaceGalleryItemId == 0)) { //typeID = 1 and moduleId = 54 DataTable dtTable = xData.GetTypedByCriteriaSpecificTable("recId", typeof(oAttachment), "moduleId, entityId, isActive, typeId", pNums.Module.SurfaceGallery.GetHashCode() + "," + SurfaceGalleryItemId + ",1," + pNums.AttachmentType.Image.GetHashCode(), "sequence"); rptMyRepeater.DataSource = dtTable; rptMyRepeater.DataBind(); } } #endregion protected void Page_PreRender(object sender, EventArgs e) { //var tmpImgBtnctrls = sfGallery.FindControls(true).Where(c => c.ID.StartsWith("imgBtn")); //foreach (LinkButton imgBtn in tmpImgBtnctrls) //{ // imgBtn.Click += new EventHandler(multiCastLinkbutton_Click); //} //var tmpLinkctrls = sfGallery.FindControls(true).Where(c => c.ID.StartsWith("lnkDelete")); //foreach (LinkButton lnkBtn in tmpLinkctrls) //{ // lnkBtn.Click += new EventHandler(multiCastLinkbutton_Click); //} } #region Event Handlers protected void Page_Load(object sender, EventArgs e) { BindImageGallery(); if (!Page.IsPostBack) { btnAddGalleryImage.Text = "Add Image"; btnAddGalleryImage.CssClass = "actionButton panelButton pull-right btn btn-primary"; } //litStyle.Text = string.Format("", ResolveUrl("~//css/imageGallery.css")); // http://localhost:21892/css/imageGallery.css } protected void rptImageGallery_ItemDataBound(object sender, RepeaterItemEventArgs e) { try { if (e.Item.ItemIndex != -1) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label lblImageId = (Label)e.Item.FindControl("lblImageId"); Label lblEditUser = (Label)e.Item.FindControl("lblEditUser"); Label lblImageGalleryHeaderEdit = (Label)e.Item.FindControl("lblImageGalleryHeaderEdit"); if (lblEditUser.Text.Length > 0) lblImageGalleryHeaderEdit.Visible = true; int noteId = Convert.ToInt32(lblImageId.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); } } protected void btnAddGalleryImage_Click(object sender, EventArgs e) { try { ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; if (SurfaceGalleryItemId == 0) { // If this is a new property, we need to save the gallery top down (Parent => Gallery) parent.SaveControl(false, false, false); } //radSurfaceImageGallery.Content = ""; radUploadSurfaceImageGallery.UploadedFiles.Clear(); chkPrimaryImage.Checked = false; //setup attachments Session["moduleId"] = pNums.Module.SurfaceGallery.GetHashCode(); Session["entityId"] = SurfaceGalleryItemId; string fieldName = String.Empty; if (utils.verifySession("fieldNameNote")) fieldName = Session["fieldNameNote"].ToString(); Session["fieldNameFile"] = SurfaceGalleryFieldId; dynamic attachments = (IControlBase)Page.Master.FindControl("attachments"); attachments.ReloadControl(); //ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myModal", "$('#modAttachments').modal();", true); ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceImageGalleryModal", "$('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceImageGallery').modal();", true); } catch (Exception ex) { exception.HandleException("surfaceGallery:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void btnSaveGalleryImage_Click(object sender, EventArgs e) { try { //BindImageGallery(); ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceImageGalleryModalClose", "TaskFileUploadCancel(); $('#" + pnlModal.ClientID + "').closest('div').find('.modSurfaceImageGallery').modal('hide');$('body').removeClass('modal-open');", true); } catch (Exception ex) { exception.HandleException("surfaceGallery:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } finally { txtImageDescription.Text = ""; ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; if (SurfaceGalleryItemId == 0) { // If this is a new property, we need to save the gallery top down (Parent => Gallery) parent.SaveControl(false, false, false); } else { // Reload the parent control if a new Primary Image has been uploaded if (chkPrimaryImage.Checked == true) { foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceGalleryItemId.ToString())) { parent.ReloadControl(item, false, false); } } } BindImageGallery(); } } protected void radUploadSurfaceImageGallery_FileUploaded(object sender, Telerik.Web.UI.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 = SurfaceGalleryItemId; Attachment.fieldName = SurfaceGalleryFieldId.ToString(); Attachment.moduleId = (int)pNums.Module.SurfaceGallery; Attachment.isActive = true; Attachment.typeId = (int)pNums.AttachmentType.Image; string taskFolderName = "imageGallery/"; taskFolderName += SurfaceGalleryItemId.ToString(); Attachment.folderName = taskFolderName; /* CVH 2016-04-15 Add date and user */ int userId = user.recId; Attachment.userIdSaved = userId; Attachment.dateSaved = DateTime.Now; if (txtImageDescription.Text.Length > 0) { Attachment.display = txtImageDescription.Text; } else 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 = System.Configuration.ConfigurationManager.AppSettings["WebAddy"]; if (chkPrimaryImage.Checked == true) { Attachment.isPrimary = true; } else { Attachment.isPrimary = false; } 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, 500, true); // 400 => 500 //resize and crop utils.ResizeCropImagePrecise(imagePath + Attachment.fileName, thumbPath, Attachment.fileName, 383, 264, false); // (383,264) => } else { throw new Exception("Only images are allowed to be uploaded from the Surface Gallery Control"); } //List lstParms = Attachment.GetType().GetProperties().ToList().ToDynamicParamList(Attachment); //xData.SaveTypedByProc("recId", typeof(oAttachment), null, "sproc_PROP1_SurfaceGallery_AddNewImage", lstParms); 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 private DataTable SurfaceGalleryImages { get { if (ViewState["surfaceGalleryImages"] != null) { return (DataTable)ViewState["surfaceGalleryImages"]; } else { return null; } } set { ViewState["surfaceGalleryImages"] = value; } } private int SurfaceGalleryItemId { get { if (ViewState["surfaceGalleryItemId"] != null) return (int)ViewState["surfaceGalleryItemId"]; else return 0; } set { ViewState["surfaceGalleryItemId"] = value; } } private int SurfaceGalleryFieldId { get { if (ViewState["surfaceGalleryFieldId"] != null) return Convert.ToInt32(ViewState["surfaceGalleryFieldId"]); else return 0; } set { ViewState["surfaceGalleryFieldId"] = value; } } private int SurfaceGalleryId { get { if (ViewState["surfaceGalleryId"] != null) return Convert.ToInt32(ViewState["surfaceGalleryId"]); else return 0; } set { ViewState["surfaceGalleryId"] = value; } } private int RecordLimit { get { if (ViewState["recordLimit"] == null) return 0; else return (int)ViewState["recordLimit"]; } set { ViewState["recordLimit"] = value; } } private int ImageId { get { if (ViewState["imageId"] != null) return (int)ViewState["imageId"]; else return 0; } set { ViewState["imageId"] = value; } } private oAttachment Attachment { get { if (ViewState["attachment"] != null) return (oAttachment)ViewState["attachment"]; else return null; } set { ViewState["attachment"] = value; } } #endregion protected void rptMyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { StringBuilder imageGallery = new StringBuilder(); List lstItems = ((DataRowView)e.Item.DataItem).Row.ItemArray.Select(c => c.ToString()).ToList(); string recordID = lstItems[0]; int isPrimary = 0; if (!(String.IsNullOrEmpty(lstItems[15].ToString()))) isPrimary = (Convert.ToBoolean(lstItems[15]) == true) ? 1 : 0; string display = lstItems[5]; string folderName = lstItems[3]; string fileName = lstItems[6]; //imageGallery.Append("
"); imageGallery.Append($""); imageGallery.Append($"\"{display}\""); imageGallery.Append(""); //imageGallery.Append("
"); //imageGallery.Append($"
{display}
"); //imageGallery.Append("
"); //imageGallery.Append("
"); Label lblImageDescription = (Label)e.Item.FindControl("lblImageDescription"); lblImageDescription.Text = display; if (isPrimary == 1) { //GR 2017-07-04 changed to LinkButtons that use palette style glyphicons and toggle titles LinkButton imageButton = (LinkButton)e.Item.FindControl("tmpImgBtn"); imageButton.ID = $"imgBtn{recordID}"; //imageButton.ImageUrl = "/images/checkYes.png"; imageButton.CssClass = "glyphicon glyphicon-ok palette-edit"; imageButton.CommandName = "togglePrimaryImageOff"; imageButton.CommandArgument = recordID; //imageButton.Attributes.Add("onmouseover", "this.src='/images/checkNo.png'"); //imageButton.Attributes.Add("onmouseout", "this.src='/images/checkYes.png'"); //imageButton.ToolTip = "Click to remove as primary image"; imageButton.Attributes.Add("data-toggle", "tooltip"); imageButton.Attributes.Add("title", "Click to remove as primary image"); //GR End of Edit //imageButton.Click += new EventHandler(multiCastLinkbutton_Click); } else { //GR 2017-07-04 changed to LinkButtons that use palette style glyphicons and toggle titles LinkButton imageButton = (LinkButton)e.Item.FindControl("tmpImgBtn"); imageButton.ID = $"imgBtn{recordID}"; //imageButton.ImageUrl = "/images/checkNo.png"; imageButton.CssClass = "glyphicon glyphicon-ok palette-remove"; imageButton.CommandName = "togglePrimaryImageOn"; imageButton.CommandArgument = recordID; //imageButton.Attributes.Add("onmouseover", "this.src='/images/checkYes.png'"); //imageButton.Attributes.Add("onmouseout", "this.src='/images/checkNo.png'"); //imageButton.ToolTip = "Click to set as primary image"; imageButton.Attributes.Add("data-toggle", "tooltip"); imageButton.Attributes.Add("title", "Click to set as primary image"); //GR End of Edit //imageButton.Click += new EventHandler(multiCastLinkbutton_Click); } if (isPrimary != 1) { // Do not allow the deletion of primary images //GR 2017-07-04 changed to LinkButtons that use palette style glyphicons and toggle titles LinkButton imageDeleteButton = (LinkButton)e.Item.FindControl("tmpLink"); imageDeleteButton.ID = $"lnkDelete{recordID}"; //imageDeleteButton.ImageUrl = "/images/delete.png"; imageDeleteButton.CssClass = "glyphicon glyphicon-remove palette-remove"; imageDeleteButton.CommandName = "delete"; imageDeleteButton.CommandArgument = recordID; //imageDeleteButton.Attributes.Add("onmouseover", "this.src='/images/delete_hover.png'"); //imageDeleteButton.Attributes.Add("onmouseout", "this.src='/images/delete.png'"); //imageDeleteButton.ToolTip = "Delete image"; imageDeleteButton.Attributes.Add("data-toggle", "tooltip"); imageDeleteButton.Attributes.Add("title", $"Delete video \"{display}\""); imageDeleteButton.Visible = true; //GR End of Edit //imageDeleteButton.Click += new EventHandler(multiCastLinkbutton_Click); } else { // If this is a primary image, remove the button //GR 2017-07-04 changed to LinkButtons that use palette style glyphicons and toggle titles LinkButton imageDeleteButton = (LinkButton)e.Item.FindControl("tmpLink"); imageDeleteButton.ID = $"lnkDelete{recordID}"; //imageDeleteButton.ImageUrl = "/images/delete.png"; imageDeleteButton.CssClass = "glyphicon glyphicon-remove palette-remove"; imageDeleteButton.CommandName = ""; imageDeleteButton.CommandArgument = ""; //imageDeleteButton.Attributes.Remove("onmouseover"); //imageDeleteButton.Attributes.Remove("onmouseout"); //imageDeleteButton.ToolTip = ""; imageDeleteButton.Visible = false; //GR End of Edit //imageDeleteButton.Click += new EventHandler(multiCastLinkbutton_Click); } Literal literal = (Literal)e.Item.FindControl("ltSurfaceImageGallery2"); literal.Text = imageGallery.ToString(); } private void DeleteGalleryImage(int imageID) { xData.DeleteTyped("recId", imageID.ToString(), typeof(oAttachment)); } private void TogglePrimaryImage(int toggle, int recordID) { DataTable tblAttachment = xData.GetTypedByCriteriaSpecificTable("recId", typeof(oAttachment), "recId", recordID.ToString()); 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; } int userId = user.recId; Attachment.recId = recordID; Attachment.entityId = Convert.ToInt32(tblAttachment.Rows[0]["entityId"].ToString()); // SurfaceGalleryItemId; Attachment.fieldName = tblAttachment.Rows[0]["fieldName"].ToString(); // SurfaceGalleryFieldId.ToString(); Attachment.moduleId = Convert.ToInt32(tblAttachment.Rows[0]["moduleId"].ToString()); // (int)pNums.Module.SurfaceGallery; Attachment.isActive = Convert.ToBoolean(tblAttachment.Rows[0]["isActive"].ToString()); Attachment.typeId = Convert.ToInt32(tblAttachment.Rows[0]["typeId"].ToString()); Attachment.folderName = tblAttachment.Rows[0]["folderName"].ToString(); Attachment.userIdSaved = Convert.ToInt32(tblAttachment.Rows[0]["userIdSaved"].ToString()); Attachment.dateSaved = Convert.ToDateTime(tblAttachment.Rows[0]["dateSaved"].ToString()); Attachment.userIdUpdated = userId; Attachment.isPrimary = (toggle == 1 ? true : false); // isPrimary Attachment.display = tblAttachment.Rows[0]["display"].ToString(); Attachment.fileName = tblAttachment.Rows[0]["fileName"].ToString(); Attachment.sourcePath = tblAttachment.Rows[0]["sourcePath"].ToString(); Attachment.dateUpdated = DateTime.Now; Attachment.sequence = Convert.ToInt32(tblAttachment.Rows[0]["sequence"].ToString()); xData.RunCustomQuery("UPDATE [pal_Attachment] SET isPrimary = 0 WHERE isPrimary = 1 AND entityId = " + Attachment.entityId + " AND isActive = 1"); xData.UpdateTyped("recId", Attachment.recId.ToString(), typeof(oAttachment), Attachment); //List lstParms = Attachment.GetType().GetProperties().ToList().ToDynamicParamList(Attachment); //xData.SaveTypedByProc("recId", typeof(oAttachment), null, "sproc_PROP1_SurfaceGallery_AddNewImage", lstParms); } catch (Exception ex) { exception.HandleException("surface:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); Response.Redirect("/error", false); } } /// /// GR 2017-07-04 Added Item created event to register the buttons as asynch postback controls /// /// /// protected void rptMyRepeater_ItemCreated(object sender, RepeaterItemEventArgs e) { try { //ScriptManager scriptMan = ScriptManager.GetCurrent(this.Page); //Repeater rpt = (Repeater)sender; //RepeaterItem item = e.Item; //LinkButton tmpImgBtn = e.Item.FindControl("tmpImgBtn") as LinkButton; //if (tmpImgBtn != null) //{ // tmpImgBtn.Click += multiCastLinkbutton_Click; // scriptMan.RegisterAsyncPostBackControl(tmpImgBtn); //} //LinkButton tmpLink = e.Item.FindControl("tmpLink") as LinkButton; //if (tmpLink != null) //{ // tmpLink.Click += multiCastLinkbutton_Click; // scriptMan.RegisterAsyncPostBackControl(tmpLink); //} } catch (Exception ex) { exception.HandleException("surface:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); Response.Redirect("/error", false); } } protected void multiCastLinkbutton_Click(object sender, EventArgs e) { try { LinkButton lnkBtn = (LinkButton)sender; int recordId = Convert.ToInt32(lnkBtn.CommandArgument);//(e.CommandArgument); string commandName = lnkBtn.CommandName; switch (commandName) //(e.CommandName) { case "delete": DeleteGalleryImage(recordId); break; case "togglePrimaryImageOn": TogglePrimaryImage(1, recordId); break; case "togglePrimaryImageOff": TogglePrimaryImage(0, recordId); break; } } catch (Exception ex) { exception.HandleException("surface:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); Response.Redirect("/error", false); } finally { ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; // Reload the parent control if a new Primary Image has been uploaded foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceGalleryItemId.ToString())) { parent.ReloadControl(item, false, false); } BindImageGallery(); } } }