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_admin_surfaceGallery : UserControl, ISurfaceControlBase { #region Methods public void ReloadControl(oSurfaceFieldData controlFieldData, bool alternateView = false, bool readOnly = false) { try { 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) { //throw new NotImplementedException(); } private void BindImageGallery() { //typeID = 1 and moduleId = 54 DataTable dtTable = xData.GetDynamicByCriteriaSpecific("recId", typeof(oAttachment), "moduleId, entityId, isActive, typeId", pNums.Module.SurfaceGallery.GetHashCode() + "," + SurfaceGalleryItemId + ",1," + pNums.AttachmentType.Image.GetHashCode(), "sequence"); rptMyRepeater.DataSource = dtTable; rptMyRepeater.DataBind(); //LiteralControl lt = (LiteralControl)rptMyRepeater.Items[0].FindControl("ltSurfaceImageGallery2"); //lt.Text = imageGallery.ToString(); } #endregion #region Event Handlers protected void Page_Load(object sender, EventArgs e) { //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 { //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 { // Reload the parent control if a new Primary Image has been uploaded if (chkPrimaryImage.Checked == true) { ISurfaceBase parent = (ISurfaceBase)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent; foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceGalleryItemId.ToString())) { parent.ReloadControl(item, false, false); } } 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 = ""; } } 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); } 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($"\"{display}\""); imageGallery.Append(""); if (isPrimary == 1) { ImageButton imageButton = (ImageButton)e.Item.FindControl("tmpImgBtn"); imageButton.ID = $"imgBtn{recordID}"; imageButton.ImageUrl = "/images/checkYes.png"; 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"; } else { ImageButton imageButton = (ImageButton)e.Item.FindControl("tmpImgBtn"); imageButton.ID = $"imgBtn{recordID}"; imageButton.ImageUrl = "/images/checkNo.png"; 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"; } LinkButton lnk = (LinkButton)e.Item.FindControl("tmpLink"); lnk.ID = $"lnkDelete{recordID}"; lnk.Text = "delete"; lnk.CommandName = "delete"; lnk.CommandArgument = recordID; lnk.ToolTip = "Delete image"; Literal literal = (Literal)e.Item.FindControl("ltSurfaceImageGallery2"); literal.Text = imageGallery.ToString(); #region ... /* if (dtTable.TableIsValid()) { foreach (DataRow dr in dtTable.AsEnumerable()) { string recordID = dr["recId"].ToString(); string linkButtonId = $"lnkDelete_{recordID}"; int isPrimary = 0; if (!(String.IsNullOrEmpty(dr["isPrimary"].ToString()))) isPrimary = Convert.ToInt32(dr["isPrimary"]); imageGallery.Append("
"); imageGallery.Append($""); imageGallery.Append($"\"{dr["display"]}\""); imageGallery.Append(""); if (isPrimary == 1) { imageGallery.Append("\"Primary"); //LinkButton lnkBtn = (LinkButton)rptMyRepeater.Items[0].FindControl("tmpLink"); //lnkBtn.ID = recordID; //lnkBtn.Text = "TeSt"; //lnkBtn.CommandName = "Test"; //lnkBtn.CommandArgument = "TestArgs"; } else { imageGallery.Append($"delete"); imageGallery.Append($"make primary"); } imageGallery.Append("
"); } } else { imageGallery.Append("
"); imageGallery.Append("No Images"); imageGallery.Append("
"); } ltSurfaceImageGallery.Text = imageGallery.ToString(); */ #endregion } protected void rptMyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) { try { int recordId = Convert.ToInt32(e.CommandArgument); switch (e.CommandName) { case "delete": DeleteGalleryImage(recordId); break; case "togglePrimaryImageOn": TogglePrimaryImage(true, recordId); break; case "togglePrimaryImageOff": TogglePrimaryImage(false, recordId); break; } } catch (Exception ex) { exception.HandleException("surface:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); Response.Redirect("/error", false); } finally { // Reload the parent control if a new Primary Image has been uploaded ISurfaceBase parent = (ISurfaceBase)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent; foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceGalleryItemId.ToString())) { parent.ReloadControl(item, false, false); } BindImageGallery(); //IEnumerable ctrlCol = parent.GetAllControls(); //var gal = ctrlCol.Where(x => x.ClientID.Equals("Core_Content_ctl08_li_fGallery")); //int io = ctrlCol.Count(); // Find fsurfaceTabs // Set
  • Core_Content_ctl08_li_fGallery class attribute = tb-item ui-sortable-handle active // Set the a href inside the
  • = aria-expanded="true" } } private void DeleteGalleryImage(int imageID) { xData.DeleteTyped("recId", imageID.ToString(), typeof(oAttachment)); } private void TogglePrimaryImage(bool 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; } //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()); // true; Attachment.typeId = Convert.ToInt32(tblAttachment.Rows[0]["typeId"].ToString()); // (int)pNums.AttachmentType.Image; Attachment.folderName = tblAttachment.Rows[0]["folderName"].ToString(); // taskFolderName; int userId = user.recId; Attachment.userIdSaved = Convert.ToInt32(tblAttachment.Rows[0]["userIdSaved"].ToString()); // userId; Attachment.dateSaved = Convert.ToDateTime(tblAttachment.Rows[0]["dateSaved"].ToString()); // DateTime.Now; Attachment.userIdUpdated = userId; // userIdUpdated Attachment.isPrimary = toggle; // isPrimary Attachment.display = tblAttachment.Rows[0]["display"].ToString(); // e.File.FileName.Remove(e.File.FileName.LastIndexOf(".")); Attachment.fileName = tblAttachment.Rows[0]["fileName"].ToString(); // Attachment.entityId + "_" + utils.RandomString(9, true) + e.File.FileName.Substring(e.File.FileName.LastIndexOf(".")); Attachment.sourcePath = tblAttachment.Rows[0]["sourcePath"].ToString(); // System.Configuration.ConfigurationManager.AppSettings["WebAddy"]; Attachment.dateUpdated = DateTime.Now; Attachment.sequence = Convert.ToInt32(tblAttachment.Rows[0]["sequence"].ToString()); //if (chkPrimaryImage.Checked == true) { Attachment.isPrimary = 1; } else { Attachment.isPrimary = 0; } //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 //{ // 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); } catch (Exception ex) { exception.HandleException("surface:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); Response.Redirect("/error", false); } } }