using System; using System.Collections; using System.Text; using System.Linq; using CMS.CMSHelper; using CMS.ExtendedControls; using CMS.GlobalHelper; using CMS.IO; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.DocumentEngine; using CMS.UIControls; using CMS.PortalEngine; public partial class CMSModules_Content_Controls_Dialogs_Selectors_LinkMediaSelector_MediaView : MediaView { #region "Private variables" private int mNodeParentNodeId = 0; private SiteInfo mSiteObj = null; private TreeNode mTreeNodeObj = null; protected string mSaveText = null; #endregion #region "Public properties" /// /// Gets or sets the value which determineds whtether to show the Parent button or not. /// public bool ShowParentButton { get { return plcParentButton.Visible; } set { plcParentButton.Visible = value; } } /// /// Gets or sets a view mode used to display files. /// public override DialogViewModeEnum ViewMode { get { return base.ViewMode; } set { base.ViewMode = value; innermedia.ViewMode = value; } } /// /// Gets or sets the OutputFormat (needed for correct dialog type reckognition). /// public OutputFormatEnum OutputFormat { get { return innermedia.OutputFormat; } set { innermedia.OutputFormat = value; } } /// /// Gets or sets text of the information label. /// public string InfoText { get { return innermedia.InfoText; } set { innermedia.InfoText = value; } } /// /// Gets currently selected page size. /// public int CurrentTopN { get { return innermedia.CurrentTopN; } } /// /// Gets or sets ID of the parent node. /// public int AtachmentNodeParentID { get { return mNodeParentNodeId; } set { mNodeParentNodeId = value; } } /// /// Gets or sets ID of the parent of the curently selected node. /// public int NodeParentID { get { return ValidationHelper.GetInteger(hdnLastNodeParentID.Value, 0); } set { hdnLastNodeParentID.Value = value.ToString(); } } /// /// Gets the node attachments are related to. /// public TreeNode TreeNodeObj { get { return mTreeNodeObj; } set { mTreeNodeObj = value; innermedia.TreeNodeObj = value; } } /// /// Gets the site attachments are related to. /// public SiteInfo SiteObj { get { if (mSiteObj == null) { mSiteObj = TreeNodeObj != null ? SiteInfoProvider.GetSiteInfo(TreeNodeObj.NodeSiteID) : CMSContext.CurrentSite; } return mSiteObj; } set { mSiteObj = value; } } /// /// Indicates whether the content tree is displaying more than max tree nodes. /// public bool IsFullListingMode { get { return innermedia.IsFullListingMode; } set { innermedia.IsFullListingMode = value; } } #endregion #region "Page methods" protected void Page_Load(object sender, EventArgs e) { // If processing the request should not continue if (StopProcessing) { Visible = false; } else { Visible = true; // Initialize controls SetupControls(); } } #endregion #region "Public methods" /// /// Loads control's content. /// public void Reload() { // Initialize controls SetupControls(); ReloadData(); } /// /// Displays listing info message. /// /// Info message to display public void DisplayListingInfo(string infoMsg) { if (!string.IsNullOrEmpty(infoMsg)) { plcListingInfo.Visible = true; lblListingInfo.Text = infoMsg; } } #endregion public override bool Visible { get { return base.Visible; } set { base.Visible = value; } } #region "Private methods" /// /// Initializes all nested controls. /// private void SetupControls() { InitializeControlScripts(); // Initialize inner view control innermedia.ViewMode = ViewMode; innermedia.DataSource = DataSource; innermedia.SelectableContent = SelectableContent; innermedia.SourceType = SourceType; innermedia.IsLiveSite = IsLiveSite; innermedia.NodeParentID = AtachmentNodeParentID; innermedia.ResizeToHeight = ResizeToHeight; innermedia.ResizeToMaxSideSize = ResizeToMaxSideSize; innermedia.ResizeToWidth = ResizeToWidth; // Set grid definition according source type string gridName = string.Empty; if (SourceType == MediaSourceEnum.DocumentAttachments) { gridName = "~/CMSModules/Content/Controls/Dialogs/Selectors/LinkMediaSelector/AttachmentsListView.xml"; } else if (SourceType == MediaSourceEnum.MetaFile) { gridName = "~/CMSModules/Content/Controls/Dialogs/Selectors/LinkMediaSelector/MetaFileListView.xml"; } else { string[] linkCustomFormatCodes = new string[] { "linkdoc", "relationship", "selectpath" }; if ((OutputFormat == OutputFormatEnum.HTMLLink) || (OutputFormat == OutputFormatEnum.BBLink) || ((OutputFormat == OutputFormatEnum.Custom) && linkCustomFormatCodes.Contains(Config.CustomFormatCode))) { gridName = "~/CMSModules/Content/Controls/Dialogs/Selectors/LinkMediaSelector/ContentListView_Link.xml"; } else { gridName = "~/CMSModules/Content/Controls/Dialogs/Selectors/LinkMediaSelector/ContentListView.xml"; } } innermedia.ListViewControl.GridName = gridName; ((CMSAdminControls_UI_UniGrid_UniGrid)(innermedia.ListViewControl)).OnPageChanged += ListViewControl_OnPageChanged; // Set inner control binding columns if (SourceType != MediaSourceEnum.MetaFile) { innermedia.FileIdColumn = "AttachmentGUID"; innermedia.FileNameColumn = (SourceType == MediaSourceEnum.DocumentAttachments) ? "AttachmentName" : "DocumentName"; innermedia.FileExtensionColumn = "AttachmentExtension"; innermedia.FileSizeColumn = "AttachmentSize"; innermedia.FileWidthColumn = "AttachmentImageWidth"; innermedia.FileHeightColumn = "AttachmentImageHeight"; } else { innermedia.FileIdColumn = "MetaFileID"; innermedia.FileNameColumn = "MetaFileName"; innermedia.FileExtensionColumn = "MetaFileExtension"; innermedia.FileSizeColumn = "MetaFileSize"; innermedia.FileWidthColumn = "MetaFileImageWidth"; innermedia.FileHeightColumn = "MetaFileImageHeight"; } // Register for inner media events innermedia.GetArgumentSet += innermedia_GetArgumentSet; innermedia.GetListItemUrl += innermedia_GetListItemUrl; innermedia.GetTilesThumbsItemUrl += innermedia_GetTilesThumbsItemUrl; // Parent directory button if ((SourceType == MediaSourceEnum.Content) && ShowParentButton) { plcParentButton.Visible = true; imgParent.ImageUrl = GetImageUrl("Design/Controls/Dialogs/parent.png"); mSaveText = GetString("dialogs.mediaview.parentdocument"); btnParent.OnClientClick = String.Format("SetParentAction('{0}'); return false;", NodeParentID); } } /// /// Initializes scrips used by the control. /// private void InitializeControlScripts() { ScriptHelper.RegisterStartupScript(this, GetType(), "DialogsSelectAction", ScriptHelper.GetScript(@" function SetSelectAction(argument) { // Raise select action SetAction('select', argument); RaiseHiddenPostBack(); } function SetParentAction(argument) { // Raise select action SetAction('parentselect', argument); RaiseHiddenPostBack(); }")); } /// /// Loads data from data source property. /// private void ReloadData() { innermedia.Reload(true); } #endregion #region "Inner media view event handlers" /// /// Returns argument set according passed DataRow and flag indicating whether the set is obtained for selected item. /// /// DataRow with all the item data /// Indicates whether the set is required for an selected item private string innermedia_GetArgumentSet(IDataContainer data) { // Return required argument set return GetArgumentSet(data); } private string innermedia_GetListItemUrl(IDataContainer data, bool isPreview, bool notAttachment) { // Get set of important information string arg = GetArgumentSet(data); // Get URL of the list item image return GetItemUrl(arg, 0, 0, 0, notAttachment); } private string innermedia_GetTilesThumbsItemUrl(IDataContainer data, bool isPreview, int height, int width, int maxSideSize, bool notAttachment) { string url = string.Empty; string ext = (SourceType != MediaSourceEnum.MetaFile) ? data.GetValue("AttachmentExtension").ToString() : data.GetValue("MetaFileExtension").ToString(); string arg = GetArgumentSet(data); // If image is requested for preview if (isPreview) { if (!ImageHelper.IsImage(ext) || notAttachment) { string className = (SourceType == MediaSourceEnum.Content) ? data.GetValue("ClassName").ToString().ToLowerCSafe() : ""; if (className == "cms.file") { // File isn't image and no preview exists - get default file icon url = GetFileIconUrl(ext, string.Empty); } else if (((SourceType == MediaSourceEnum.DocumentAttachments) || (SourceType == MediaSourceEnum.Attachment) || (SourceType == MediaSourceEnum.MetaFile)) && !String.IsNullOrEmpty(ext)) { // Get file icon for attachment url = GetFileIconUrl(ext, string.Empty); } else { url = GetDocumentTypeIconUrl(className, "48x48"); } } else { // Try to get preview or image itself url = GetItemUrl(arg, height, width, maxSideSize, notAttachment); } } else { url = GetItemUrl(arg, 0, 0, 0, notAttachment); } return url; } private void ListViewControl_OnPageChanged(object sender, EventArgs e) { RaiseListReloadRequired(); } #endregion #region "Helper methods" /// /// Returns argument set for the passed file data row. /// /// Data row object holding all the data on current file public string GetArgumentSet(IDataContainer data) { string className = ValidationHelper.GetString(data.GetValue("ClassName"), String.Empty).ToLowerCSafe(); string name = string.Empty; // Get file name with extension switch (SourceType) { case MediaSourceEnum.DocumentAttachments: name = AttachmentHelper.GetFullFileName(Path.GetFileNameWithoutExtension(data.GetValue("AttachmentName").ToString()), data.GetValue("AttachmentExtension").ToString()); break; case MediaSourceEnum.MetaFile: name = MetaFileInfoProvider.GetFullFileName(Path.GetFileNameWithoutExtension(data.GetValue("MetaFileName").ToString()), data.GetValue("MetaFileExtension").ToString()); break; default: name = data.GetValue("DocumentName").ToString(); break; } StringBuilder sb = new StringBuilder(); // Common information for both content & attachments sb.Append("name|" + CMSDialogHelper.EscapeArgument(name)); // Load attachment info only for CMS.File document type if (((SourceType != MediaSourceEnum.Content) && (SourceType != MediaSourceEnum.MetaFile)) || (className == "cms.file")) { sb.Append("|AttachmentExtension|" + CMSDialogHelper.EscapeArgument(data.GetValue("AttachmentExtension"))); sb.Append("|AttachmentImageWidth|" + CMSDialogHelper.EscapeArgument(data.GetValue("AttachmentImageWidth"))); sb.Append("|AttachmentImageHeight|" + CMSDialogHelper.EscapeArgument(data.GetValue("AttachmentImageHeight"))); sb.Append("|AttachmentSize|" + CMSDialogHelper.EscapeArgument(data.GetValue("AttachmentSize"))); sb.Append("|AttachmentGUID|" + CMSDialogHelper.EscapeArgument(data.GetValue("AttachmentGUID"))); } else if (SourceType == MediaSourceEnum.MetaFile) { sb.Append("|MetaFileExtension|" + CMSDialogHelper.EscapeArgument(data.GetValue("MetaFileExtension"))); sb.Append("|MetaFileImageWidth|" + CMSDialogHelper.EscapeArgument(data.GetValue("MetaFileImageWidth"))); sb.Append("|MetaFileImageHeight|" + CMSDialogHelper.EscapeArgument(data.GetValue("MetaFileImageHeight"))); sb.Append("|MetaFileSize|" + CMSDialogHelper.EscapeArgument(data.GetValue("MetaFileSize"))); sb.Append("|MetaFileGUID|" + CMSDialogHelper.EscapeArgument(data.GetValue("MetaFileGUID"))); sb.Append("|SiteID|" + CMSDialogHelper.EscapeArgument(data.GetValue("MetaFileSiteID"))); } else { sb.Append("|AttachmentExtension||AttachmentImageWidth||AttachmentImageHeight||AttachmentSize||AttachmentGUID|"); } // Get source type specific information if (SourceType == MediaSourceEnum.Content) { sb.Append("|NodeSiteID|" + CMSDialogHelper.EscapeArgument(data.GetValue("NodeSiteID"))); sb.Append("|SiteName|" + CMSDialogHelper.EscapeArgument(data.GetValue("SiteName"))); sb.Append("|NodeGUID|" + CMSDialogHelper.EscapeArgument(data.GetValue("NodeGUID"))); sb.Append("|NodeID|" + CMSDialogHelper.EscapeArgument(data.GetValue("NodeID"))); sb.Append("|NodeAlias|" + CMSDialogHelper.EscapeArgument(data.GetValue("NodeAlias"))); sb.Append("|NodeAliasPath|" + CMSDialogHelper.EscapeArgument(data.GetValue("NodeAliasPath"))); sb.Append("|DocumentUrlPath|" + CMSDialogHelper.EscapeArgument(data.GetValue("DocumentUrlPath"))); sb.Append("|DocumentExtensions|" + CMSDialogHelper.EscapeArgument(data.GetValue("DocumentExtensions"))); sb.Append("|ClassName|" + CMSDialogHelper.EscapeArgument(data.GetValue("ClassName"))); sb.Append("|NodeLinkedNodeID|" + CMSDialogHelper.EscapeArgument(data.GetValue("NodeLinkedNodeID"))); } else if (SourceType != MediaSourceEnum.MetaFile) { string formGuid = data.ContainsColumn("AttachmentFormGUID") ? data.GetValue("AttachmentFormGUID").ToString() : Guid.Empty.ToString(); string siteId = data.ContainsColumn("AttachmentSiteID") ? data.GetValue("AttachmentSiteID").ToString() : "0"; sb.Append("|SiteID|" + CMSDialogHelper.EscapeArgument(siteId)); sb.Append("|FormGUID|" + CMSDialogHelper.EscapeArgument(formGuid)); sb.Append("|AttachmentDocumentID|" + CMSDialogHelper.EscapeArgument(data.GetValue("AttachmentDocumentID"))); } return sb.ToString(); } /// /// Returns arguments table for the passed argument. /// /// Argument containing information on current media item public static Hashtable GetArgumentsTable(string argument) { Hashtable table = new Hashtable(); string[] argArr = argument.Split('|'); try { // Fill table for (int i = 0; i < argArr.Length; i = i + 2) { table[argArr[i].ToLowerCSafe()] = CMSDialogHelper.UnEscapeArgument(argArr[i + 1]); } } catch { throw new Exception("[Media view]: Error loading arguments table."); } return table; } /// /// Returns URL of the media item according site settings. /// /// Argument containing information on current media item /// Item height in px /// Item width in px /// Maximum dimension for images displayed for tile and thumbnails view /// True if this is not an attachement public string GetItemUrl(string argument, int height, int width, int maxSideSize, bool notAttachment) { Hashtable argTable = GetArgumentsTable(argument); if (argTable.Count >= 2) { string url = string.Empty; // Get image URL switch (SourceType) { case MediaSourceEnum.Content: { // Get information from argument Guid nodeGuid = ValidationHelper.GetGuid(argTable["nodeguid"], Guid.Empty); string documentUrlPath = argTable["documenturlpath"].ToString(); string nodeAlias = argTable["nodealias"].ToString(); string nodeAliasPath = argTable["nodealiaspath"].ToString(); string documentExtensions = argTable["documentextensions"].ToString(); string documentClass = argTable["classname"].ToString(); bool nodeIsLink = (ValidationHelper.GetInteger(argTable["nodelinkednodeid"], 0) != 0); string siteName = argTable["sitename"].ToString(); // Get default url extension for current item string fileExt = !String.IsNullOrEmpty(documentClass) && (documentClass.ToLowerCSafe() == "cms.file") ? TreePathUtils.GetFilesUrlExtension(siteName) : TreePathUtils.GetUrlExtension(siteName); // If extension less try to get custom document extension if (String.IsNullOrEmpty(fileExt)) { fileExt = (!String.IsNullOrEmpty(documentExtensions) ? documentExtensions.Split(';')[0] : String.Empty); } // Get content item URL url = GetContentItemUrl(nodeGuid, documentUrlPath, nodeAlias, nodeAliasPath, nodeIsLink, height, width, maxSideSize, notAttachment, fileExt); } break; case MediaSourceEnum.MetaFile: { // Get information from argument Guid attachmentGuid = ValidationHelper.GetGuid(argTable["metafileguid"], Guid.Empty); string attachmentName = argTable["name"].ToString(); // Get item URL url = GetMetaFileItemUrl(attachmentGuid, attachmentName, height, width, maxSideSize); } break; default: { // Get information from argument Guid attachmentGuid = ValidationHelper.GetGuid(argTable["attachmentguid"], Guid.Empty); string attachmentName = argTable["name"].ToString(); string nodeAliasPath = string.Empty; if (TreeNodeObj != null) { nodeAliasPath = TreeNodeObj.NodeAliasPath; } // Get item URL url = GetAttachmentItemUrl(attachmentGuid, attachmentName, nodeAliasPath, height, width, maxSideSize); } break; } return url; } return string.Empty; } /// /// Ensures no item is selected. /// public void ResetSearch() { dialogSearch.ResetSearch(); } /// /// Ensures first page is displayed in the control displaying the content. /// public void ResetPageIndex() { innermedia.ResetPageIndex(); } /// /// Ensure no item is selected in list view. /// public void ResetListSelection() { innermedia.ResetListSelection(); } /// /// Returns URL, resolved if not in media selector, with size parameters added. /// /// Media file URL /// Height parameter that should be added to the URL /// Width parameter that should be added to the URL /// Max side size parameter that should be added to the URL public string AddURLDimensionsAndResolve(string url, int height, int width, int maxSideSize) { if (string.IsNullOrEmpty(url)) { return url; } string result = url; // If image dimensions are specified if (maxSideSize > 0) { result = URLHelper.AddParameterToUrl(result, "maxsidesize", maxSideSize.ToString()); } if (height > 0) { result = URLHelper.AddParameterToUrl(result, "height", height.ToString()); } if (width > 0) { result = URLHelper.AddParameterToUrl(result, "width", width.ToString()); } // Media selector should returns non-resolved URL in all cases bool isMediaSelector = (OutputFormat == OutputFormatEnum.URL) && (SelectableContent == SelectableContentEnum.OnlyMedia); return (isMediaSelector ? result : URLHelper.ResolveUrl(result, true, false)); } #endregion #region "Content methods" /// /// Returns URL of the media item according site settings. /// /// Node GUID of the current attachment node /// URL path of the current attachment document /// Maximum dimension for images displayed for tile and thumbnails view /// Node alias of the current attachment node /// Node alias path of the current attachment node /// Indicates if node is linked node. public string GetContentItemUrl(Guid nodeGuid, string documentUrlPath, string nodeAlias, string nodeAliasPath, bool nodeIsLink, int height, int width, int maxSideSize, bool notAttachment, string documentExtension) { string result = ""; if (documentExtension.Contains(";")) { documentExtension = documentExtension.Split(';')[0]; } // Generate URL if (UsePermanentUrls) { bool isLink = ((OutputFormat == OutputFormatEnum.BBLink) || (OutputFormat == OutputFormatEnum.HTMLLink)) || ((OutputFormat == OutputFormatEnum.URL) && (SelectableContent == SelectableContentEnum.AllContent)); if (String.IsNullOrEmpty(nodeAlias)) { nodeAlias = "default"; } if (notAttachment || isLink) { result = TreePathUtils.GetPermanentDocUrl(nodeGuid, nodeAlias, SiteObj.SiteName, null, documentExtension); } else { result = AttachmentInfoProvider.GetPermanentAttachmentUrl(nodeGuid, nodeAlias, documentExtension); } } else { string docUrlPath = nodeIsLink ? null : documentUrlPath; // Ensure live site view mode for URLs edited in on-site edit mode if (PortalContext.ViewMode == ViewModeEnum.EditLive) { PortalContext.SetRequestViewMode(ViewModeEnum.LiveSite); } result = TreePathUtils.GetUrl(nodeAliasPath, docUrlPath, SiteObj.SiteName, null, documentExtension); } // Make URL absolute if required int currentSiteId = CMSContext.CurrentSiteID; if (Config.UseFullURL || (currentSiteId != SiteObj.SiteID) || (currentSiteId != GetCurrentSiteId())) { result = URLHelper.GetAbsoluteUrl(result, SiteObj.DomainName, URLHelper.GetApplicationUrl(SiteObj.DomainName), null); } return AddURLDimensionsAndResolve(result, height, width, maxSideSize); } #endregion #region "Attachment methods" /// /// Returns URL for the attachment specified by arguments. /// /// GUID of the attachment /// Name of the attachment /// Attachement node alias /// Maximum size of the item if attachment is image public string GetAttachmentItemUrl(Guid attachmentGuid, string attachmentName, string attachmentNodeAlias, int height, int width, int maxSideSize) { string result = ""; if (UsePermanentUrls || string.IsNullOrEmpty(attachmentNodeAlias)) { result = AttachmentInfoProvider.GetAttachmentUrl(attachmentGuid, attachmentName); } else { //string safeFileName = URLHelper.GetSafeFileName(attachmentName, attachmentSite.SiteName); string safeFileName = URLHelper.GetSafeFileName(attachmentName, SiteObj.SiteName); result = AttachmentInfoProvider.GetAttachmentUrl(safeFileName, attachmentNodeAlias); } // If current site is different from attachment site make URL absolute (domain included) if (Config.UseFullURL || (CMSContext.CurrentSiteID != SiteObj.SiteID) || (CMSContext.CurrentSiteID != GetCurrentSiteId())) { result = URLHelper.GetAbsoluteUrl(result, SiteObj.DomainName, URLHelper.GetApplicationUrl(SiteObj.DomainName), null); } return AddURLDimensionsAndResolve(result, height, width, maxSideSize); } #endregion #region "MetaFile methods" /// /// Returns URL for the metafile specified by arguments. /// /// GUID of the metafile /// Name of the metafile /// Height parameter that should be added to the URL /// Width parameter that should be added to the URL /// Maximum size of the item if metafile is image public string GetMetaFileItemUrl(Guid attachmentGuid, string attachmentName, int height, int width, int maxSideSize) { string result = MetaFileURLProvider.GetMetaFileUrl(attachmentGuid, attachmentName); // If current site is different from attachment site make URL absolute (domain included) if (Config.UseFullURL || (CMSContext.CurrentSiteID != SiteObj.SiteID) || (CMSContext.CurrentSiteID != GetCurrentSiteId())) { result = URLHelper.GetAbsoluteUrl(result, SiteObj.DomainName, URLHelper.GetApplicationUrl(SiteObj.DomainName), null); } return AddURLDimensionsAndResolve(result, height, width, maxSideSize); } #endregion }