using framework_business; using framework_library; using System; using System.Collections.Generic; 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_module_surfaceGalleryVideo : UserControl, ISurfaceControlBase { #region Methods public void ReloadControl(oSurfaceFieldData controlFieldData, bool alternateView = false, bool readOnly = false) { try { try { // Add ValidationGroup="Properties" to btnAddGalleryVideo button ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; string parentName = parent.AppRelativeVirtualPath; int iLastIndex = parentName.LastIndexOf("/") + 1; string validate = parentName.Substring(iLastIndex).Replace("_form.ascx", ""); btnAddGalleryVideo.ValidationGroup = validate; } catch { } SurfaceVideoGalleryId = controlFieldData.surfaceId; SurfaceVideoGalleryItemId = controlFieldData.surfaceItemId; foreach (oSurfaceField field in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceField), "recId", controlFieldData.surfaceFieldID.ToString())) { SurfaceVideoGalleryFieldId = field.recId; lblVideoGalleryTitle.Text = field.surfaceFieldDisplay; } if (alternateView) RecordLimit = 3; //BindVideoGallery(); } catch (Exception ex) { exception.HandleException("surfaceGalleryVideo:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } public void SaveControlData(ref oSurfaceFieldData _surfaceFieldData) { if (SurfaceVideoGalleryItemId == 0) SurfaceVideoGalleryItemId = _surfaceFieldData.surfaceItemId; } private void BindVideoGallery() { if (SurfaceVideoGalleryItemId > 0) { //typeID = 1 and moduleId = 54 DataTable dtTable = xData.GetDynamicByCriteriaSpecific("recId", typeof(oAttachment), "moduleId, entityId, isActive, typeId", pNums.Module.SurfaceGalleryVideo.GetHashCode() + "," + SurfaceVideoGalleryItemId + ",1," + pNums.AttachmentType.Video.GetHashCode(), "sequence"); rptMyVideoRepeater.DataSource = dtTable; rptMyVideoRepeater.DataBind(); } } #endregion protected void Page_Load(object sender, EventArgs e) { BindVideoGallery(); if (!Page.IsPostBack) { btnAddGalleryVideo.Text = "Add Video"; btnAddGalleryVideo.CssClass = "actionButton panelButton pull-right btn btn-primary"; } } protected void rptMyVideoRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { StringBuilder videoGallery = new StringBuilder(); List lstItems = ((DataRowView)e.Item.DataItem).Row.ItemArray.Select(c => c.ToString()).ToList(); string recordID = lstItems[0]; string display = lstItems[5]; string folderName = lstItems[3]; string fileName = lstItems[6]; //videoGallery.Append($""); videoGallery.Append($""); videoGallery.Append($"\"{display}\""); videoGallery.Append(""); #region ... For Embedded YouTube Videos //string recordID = lstItems[0]; //string videoDescription = lstItems[5]; //string youTubeVideoID = lstItems[13]; //string youTubeSource = "http://www.youtube.com/embed/"; //string youTubeThumbSource = $"http://img.youtube.com/vi/"; //string thumbImage = $"\"{videoDescription}\""; //383, 264 //// Regarding THumbnail Images - https://html5box.com/html5gallery/install.php#addyoutube //videoGallery.Append($"{thumbImage}"); #endregion Label lblVideoDescription = (Label)e.Item.FindControl("lblVideoDescription"); lblVideoDescription.Text = display; LinkButton imageDeleteButton = (LinkButton)e.Item.FindControl("tmpVideoLink"); imageDeleteButton.ID = $"lnkDelete{recordID}"; imageDeleteButton.CssClass = "glyphicon glyphicon-remove palette-remove"; imageDeleteButton.CommandName = "delete"; imageDeleteButton.CommandArgument = recordID; imageDeleteButton.Attributes.Add("data-toggle", "tooltip"); imageDeleteButton.Attributes.Add("title", $"Delete video \"{display}\""); imageDeleteButton.Visible = true; Literal literal = (Literal)e.Item.FindControl("ltSurfaceVideoGallery2"); literal.Text = videoGallery.ToString(); } protected void rptMyVideoRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) { //try //{ // int recordId = Convert.ToInt32(e.CommandArgument); // switch (e.CommandName) // { // case "delete": // DeleteGalleryVideo(recordId); // break; // //case "togglePrimaryVideoOn": // // TogglePrimaryVideo(1, recordId); // // break; // //case "togglePrimaryVideoOff": // // TogglePrimaryVideo(0, recordId); // // break; // } //} //catch (Exception ex) //{ // exception.HandleException("surfaceGalleryVideo:", 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.NamingContainer; // foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceGalleryItemId.ToString())) // { // parent.ReloadControl(item, false, false); // } // BindVideoGallery(); //} } private void DeleteGalleryVideo(int videoId) { try { xData.DeleteTyped("recId", videoId.ToString(), typeof(oAttachment)); } catch (Exception ex) { exception.HandleException("surfaceGalleryVideo:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); Response.Redirect("/error", false); } } protected void btnAddGalleryVideo_Click(object sender, EventArgs e) { try { ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; if (SurfaceVideoGalleryItemId == 0) { // If this is a new property, we need to save the gallery top down (Parent => Gallery) parent.SaveControl(false, false, false); } radUploadSurfaceVideoGallery.UploadedFiles.Clear(); //chkPrimaryImage.Checked = false; //setup attachments Session["moduleId"] = pNums.Module.SurfaceGalleryVideo.GetHashCode(); Session["entityId"] = SurfaceVideoGalleryItemId; string fieldName = String.Empty; if (utils.verifySession("fieldNameNote")) fieldName = Session["fieldNameNote"].ToString(); Session["fieldNameFile"] = SurfaceVideoGalleryFieldId; dynamic attachments = (IControlBase)Page.Master.FindControl("attachments"); attachments.ReloadControl(); ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceVideoGalleryModal", "$('#" + pnlVideoModal.ClientID + "').closest('div').find('.modSurfaceVideoGallery').modal();", true); } catch (Exception ex) { exception.HandleException("surfaceGalleryVideo:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void radUploadSurfaceVideoGallery_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e) { VideoAttachment = 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; } VideoAttachment.entityId = SurfaceVideoGalleryItemId; VideoAttachment.fieldName = SurfaceVideoGalleryFieldId.ToString(); VideoAttachment.moduleId = (int)pNums.Module.SurfaceGalleryVideo; VideoAttachment.isActive = true; VideoAttachment.typeId = (int)pNums.AttachmentType.Video; string taskFolderName = "videoGallery/"; taskFolderName += SurfaceVideoGalleryItemId.ToString(); VideoAttachment.folderName = taskFolderName; /* CVH 2016-04-15 Add date and user */ int userId = user.recId; VideoAttachment.userIdSaved = userId; VideoAttachment.dateSaved = DateTime.Now; if (txtVideoDescription.Text.Length > 0) { VideoAttachment.display = txtVideoDescription.Text; } else VideoAttachment.display = e.File.FileName.Remove(e.File.FileName.LastIndexOf(".")); string randomFileName = utils.RandomString(9, true) + e.File.FileName.Substring(e.File.FileName.LastIndexOf(".")); string friendlyVideoFileName = VideoAttachment.display; foreach (var c in Path.GetInvalidFileNameChars()) { friendlyVideoFileName = friendlyVideoFileName.Replace(c.ToString(), ""); } VideoAttachment.fileName = $"{friendlyVideoFileName}_{VideoAttachment.entityId}_{randomFileName}"; // Attachment.fileName = Attachment.entityId + "_" + utils.RandomString(9, true) + e.File.FileName.Substring(e.File.FileName.LastIndexOf(".")); string videoPath = Server.MapPath("~/upload/" + VideoAttachment.folderName + "/video/"); string thumbPath = Server.MapPath("~/upload/" + VideoAttachment.folderName + "/thumb/"); string tempPath = Server.MapPath("~/upload/" + VideoAttachment.folderName + "/temp/"); string filePath = Server.MapPath("~/upload/" + VideoAttachment.folderName + "/file/"); utils.validateFolder(videoPath); utils.validateFolder(thumbPath); utils.validateFolder(tempPath); utils.validateFolder(filePath); VideoAttachment.sourcePath = System.Configuration.ConfigurationManager.AppSettings["WebAddy"]; //if (chkPrimaryImage.Checked == true) { Attachment.isPrimary = true; } else { Attachment.isPrimary = false; } if (VideoAttachment.typeId == pNums.AttachmentType.Video.GetHashCode()) { #region ... ////upload file //e.File.SaveAs(tempPath + Attachment.fileName); //// Generate Thumbnail Image //string thumbImageName = $"{Path.GetFileNameWithoutExtension(Attachment.fileName)}.jpg"; //string errorMessage = ""; //bool thumbnailGenerated = utils.GetVideoThumbnail(tempPath + Attachment.fileName, thumbPath + thumbImageName, out errorMessage); ////// If the thumbnail generated correctly, upload the file ////if (!thumbnailGenerated) ////{ //// string genericPlaceholderThumb = //// utils.ResizeCropImagePrecise(imagePath + Attachment.fileName, thumbPath, Attachment.fileName, 383, 264, false); ////} //e.File.SaveAs(videoPath + Attachment.fileName); ////resize and compress ////utils.ResizeImage(tempPath + Attachment.fileName, videoPath, Attachment.fileName, 500, true); // 400 => 500 ////resize and crop ////utils.ResizeCropImagePrecise(videoPath + Attachment.fileName, thumbPath, Attachment.fileName, 383, 264, false); // (383,264) => #endregion //upload file e.File.SaveAs(videoPath + VideoAttachment.fileName); } else { throw new Exception("Only videos are allowed to be uploaded from the Surface Gallery Control"); } VideoAttachment.recId = xData.SaveTyped("recId", typeof(oAttachment), VideoAttachment); //List lstParms = VideoAttachment.GetType().GetProperties().ToList().ToDynamicParamList(VideoAttachment); //xData.SaveTypedByProc("recId", typeof(oAttachment), null, "sproc_PROP1_SurfaceGallery_AddNewVideo", lstParms); } catch (Exception ex) { exception.HandleException("surfaceGalleryVideo:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); Response.Redirect("/error", false); } } protected void btnSaveGalleryVideo_Click(object sender, EventArgs e) { try { #region ... //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 = new oAttachment(); //Attachment.entityId = SurfaceGalleryItemId; //Attachment.fieldName = SurfaceGalleryFieldId.ToString(); //Attachment.moduleId = (int)pNums.Module.SurfaceGalleryVideo; //Attachment.isActive = true; //Attachment.typeId = (int)pNums.AttachmentType.Video; ////string taskFolderName = "videoGallery/"; ////taskFolderName += SurfaceGalleryItemId.ToString(); ///* CVH 2016-04-15 Add date and user */ //int userId = user.recId; //Attachment.userIdSaved = userId; //Attachment.dateSaved = DateTime.Now; //if (txtVideoDescription.Text.Length > 0) //{ // Attachment.display = txtVideoDescription.Text; //} //else // Attachment.display = "No Description"; //Attachment.fileName = txtVideoDescription.Text; //var url = txtVideoURL.Text; //var uri = new Uri(url); //// you can check host here => uri.Host <= "www.youtube.com" //// Also refer to h ttps://stackoverflow.com/questions/39777659/extract-the-video-id-from-youtube-url-in-net //var query = HttpUtility.ParseQueryString(uri.Query); //var videoId = string.Empty; //if (query.AllKeys.Contains("v")) //{ // videoId = query["v"]; //} //else //{ // videoId = uri.Segments.Last(); //} //Attachment.sourcePath = videoId; //xData.SaveTyped("recId", typeof(oAttachment), Attachment); //// Reload the parent control if a new Primary Image has been uploaded //ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; //foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceGalleryItemId.ToString())) //{ // parent.ReloadControl(item, false, false); //} #endregion //BindVideoGallery(); ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "mySurfaceVideoGalleryModalClose", "TaskFileUploadCancel(); $('#" + pnlVideoModal.ClientID + "').closest('div').find('.modSurfaceVideoGallery').modal('hide');$('body').removeClass('modal-open');", true); } catch (Exception ex) { exception.HandleException("surfaceGalleryVideo:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } finally { txtVideoDescription.Text = ""; if (SurfaceVideoGalleryItemId == 0) { // If this is a new property, we need to save the gallery top down (Parent => Gallery) ISurfaceBase parent = (ISurfaceBase)this.NamingContainer; parent.SaveControl(false, false, false); } BindVideoGallery(); } } #region TogglePrimaryImage (NOT IMPLEMENTED - FUTURE USE) 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()); // List lstParms = Attachment.GetType().GetProperties().ToList().ToDynamicParamList(Attachment); // xData.SaveTypedByProc("recId", typeof(oAttachment), null, "sproc_PROP1_SurfaceGallery_AddNewVideo", lstParms); //} //catch (Exception ex) //{ // exception.HandleException("surfaceGalleryVideo:", MethodBase.GetCurrentMethod().Name, ex, Session["attachment"]); // Response.Redirect("/error", false); //} } #endregion #region Properties private DataTable SurfaceGalleryVideo { get { if (ViewState["surfaceGalleryVideos"] != null) { return (DataTable)ViewState["surfaceGalleryVideos"]; } else { return null; } } set { ViewState["surfaceGalleryVideos"] = value; } } private int SurfaceVideoGalleryItemId { get { if (ViewState["surfaceGalleryItemId"] != null) return (int)ViewState["surfaceGalleryItemId"]; else return 0; } set { ViewState["surfaceGalleryItemId"] = value; } } private int SurfaceVideoGalleryFieldId { get { if (ViewState["surfaceGalleryFieldId"] != null) return Convert.ToInt32(ViewState["surfaceGalleryFieldId"]); else return 0; } set { ViewState["surfaceGalleryFieldId"] = value; } } private int SurfaceVideoGalleryId { 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 VideoId { get { if (ViewState["videoId"] != null) return (int)ViewState["videoId"]; else return 0; } set { ViewState["videoId"] = value; } } private oAttachment VideoAttachment { get { if (ViewState["attachment"] != null) return (oAttachment)ViewState["attachment"]; else return null; } set { ViewState["attachment"] = value; } } #endregion 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; // int recordId = Convert.ToInt32(e.CommandArgument); switch (commandName) { case "delete": DeleteGalleryVideo(recordId); break; //case "togglePrimaryVideoOn": // TogglePrimaryVideo(1, recordId); // break; //case "togglePrimaryVideoOff": // TogglePrimaryVideo(0, recordId); // break; } } catch (Exception ex) { exception.HandleException("surfaceGalleryVideo:", 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.NamingContainer; foreach (oSurfaceItem item in xData.GetTypedByCriteriaSpecific("recId", typeof(oSurfaceItem), "recId", SurfaceVideoGalleryItemId.ToString())) { parent.ReloadControl(item, false, false); } BindVideoGallery(); } } }