using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CMS.CMSHelper; using CMS.Controls; using CMS.GlobalHelper; using CMS.MediaLibrary; using CMS.PortalControls; public partial class CMSWebParts_MediaLibrary_MediaGalleryFileList : CMSAbstractWebPart { #region "Variables" // Indicates whether control was binded private bool binded = false; // Datasource instance private CMSBaseDataSource mDataSourceControl = null; private string mMediaLibraryRoot = null; private string mMediaLibraryUrl = null; private bool? mDisplayDetail = null; #endregion #region "Properties" /// /// Gets or sets the name of the media library name. /// public string MediaLibraryName { get { string libraryName = ValidationHelper.GetString(GetValue("MediaLibraryName"), ""); if ((string.IsNullOrEmpty(libraryName) || libraryName == MediaLibraryInfoProvider.CURRENT_LIBRARY) && (MediaLibraryContext.CurrentMediaLibrary != null)) { return MediaLibraryContext.CurrentMediaLibrary.LibraryName; } return libraryName; } set { SetValue("MediaLibraryName", value); } } /// /// Returns true if file ID query string is pressent. /// public bool? DisplayDetail { get { return mDisplayDetail ?? (mDisplayDetail = (QueryHelper.GetInteger(FileIDQueryStringKey, 0) > 0)); } } /// /// Gets or sets the file id querysting parameter. /// public string FileIDQueryStringKey { get { return ValidationHelper.GetString(GetValue("FileIDQueryStringKey"), String.Empty); } set { SetValue("FileIDQueryStringKey", value); } } /// /// Gets or sets the name of the transforamtion which is used for displaying the results. /// public string TransformationName { get { return ValidationHelper.GetString(GetValue("TransformationName"), String.Empty); } set { SetValue("TransformationName", value); } } /// /// Gets or sets the name of the transforamtion which is used for displaying selected file. /// public string SelectedItemTransformationName { get { return ValidationHelper.GetString(GetValue("SelectedItemTransformationName"), String.Empty); } set { SetValue("SelectedItemTransformationName", value); } } /// /// Gets or sets FooterTemplate property. /// public string FooterTransformationName { get { return ValidationHelper.GetString(GetValue("FooterTransformationName"), String.Empty); } set { SetValue("FooterTransformationName", value); } } /// /// Gets or sets HeaderTemplate property. /// public string HeaderTransformationName { get { return ValidationHelper.GetString(GetValue("HeaderTransformationName"), String.Empty); } set { SetValue("HeaderTransformationName", value); } } /// /// Gets or sets SeparatorTemplate property. /// public string SeparatorTransformationName { get { return ValidationHelper.GetString(GetValue("SeparatorTransformationName"), String.Empty); } set { SetValue("SeparatorTransformationName", value); } } /// /// Gets or sets HideControlForZeroRows property. /// public bool HideControlForZeroRows { get { return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), true); } set { SetValue("HideControlForZeroRows", value); } } /// /// Gets or sets ZeroRowsText property. /// public string ZeroRowsText { get { return ValidationHelper.GetString(GetValue("ZeroRowsText"), String.Empty); } set { SetValue("ZeroRowsText", value); } } /// /// Gets or sets the name of the transforamtion which is used for displaying selected file. /// public string DataSourceName { get { return ValidationHelper.GetString(GetValue("DataSourceName"), String.Empty); } set { SetValue("DataSourceName", value); } } /// /// Control with data source. /// public CMSBaseDataSource DataSourceControl { get { // Check if control is empty and load it with the data if (mDataSourceControl == null) { if (!String.IsNullOrEmpty(DataSourceName)) { mDataSourceControl = CMSControlsHelper.GetFilter(DataSourceName) as CMSBaseDataSource; } } return mDataSourceControl; } set { mDataSourceControl = value; } } /// /// Preview preffix for identification preview file. /// public string PreviewSuffix { get { return ValidationHelper.GetString(GetValue("PreviewSuffix"), String.Empty); } set { SetValue("PreviewSuffix", value); } } /// /// Icon set name. /// public string IconSet { get { return ValidationHelper.GetString(GetValue("IconSet"), String.Empty); } set { SetValue("IconSet", value); } } /// /// Indicates if active content (video, flash etc.) should be displayed. /// public bool DisplayActiveContent { get { return ValidationHelper.GetBoolean(GetValue("DisplayActiveContent"), false); } set { SetValue("DisplayActiveContent", value); } } #endregion /// /// OnContentLoaded override. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Page_Load event handler. /// protected void Page_Load(object sender, EventArgs e) { SetupControlLoad(); } /// /// OnPreRender override. /// protected override void OnPreRender(EventArgs e) { if ((DataSourceControl != null) && (!DataHelper.DataSourceIsEmpty(DataSourceControl.DataSource)) && (!binded)) { repItems.DataSource = DataSourceControl.DataSource; repItems.DataBind(); } base.OnPreRender(e); } /// /// Initializes the control properties. /// protected void SetupControlLoad() { // Handle filter change event if (DataSourceControl != null) { DataSourceControl.OnFilterChanged += DataSourceControl_OnFilterChanged; } MediaLibraryInfo mli = MediaLibraryInfoProvider.GetMediaLibraryInfo(MediaLibraryName, CMSContext.CurrentSiteName); if (mli != null) { mMediaLibraryRoot = MediaLibraryHelper.GetMediaRootFolderPath(CMSContext.CurrentSiteName) + mli.LibraryFolder; mMediaLibraryUrl = URLHelper.GetAbsoluteUrl("~/" + CMSContext.CurrentSiteName + "/media/" + mli.LibraryFolder); } repItems.ItemDataBound += repItems_ItemDataBound; } /// /// Initializes the control properties. /// protected void SetupControl() { if (StopProcessing) { // Do nothing } else { MediaLibraryInfo mli = MediaLibraryInfoProvider.GetMediaLibraryInfo(MediaLibraryName, CMSContext.CurrentSiteName); if (mli != null) { // If dont have 'Manage' permission if (!MediaLibraryInfoProvider.IsUserAuthorizedPerLibrary(mli, "read")) { // Check 'File create' permission if (!MediaLibraryInfoProvider.IsUserAuthorizedPerLibrary(mli, "libraryaccess")) { repItems.Visible = false; messageElem.ErrorMessage = MediaLibraryHelper.GetAccessDeniedMessage("libraryaccess"); messageElem.DisplayMessage = true; return; } } int fid = QueryHelper.GetInteger(FileIDQueryStringKey, 0); if (fid > 0) { if (!String.IsNullOrEmpty(SelectedItemTransformationName)) { repItems.ItemTemplate = CMSDataProperties.LoadTransformation(this, SelectedItemTransformationName, false); } } else { if (!String.IsNullOrEmpty(TransformationName)) { repItems.ItemTemplate = CMSDataProperties.LoadTransformation(this, TransformationName, false); } } if (!String.IsNullOrEmpty(FooterTransformationName)) { repItems.FooterTemplate = CMSDataProperties.LoadTransformation(this, FooterTransformationName, false); } if (!String.IsNullOrEmpty(HeaderTransformationName)) { repItems.HeaderTemplate = CMSDataProperties.LoadTransformation(this, HeaderTransformationName, false); } if (!String.IsNullOrEmpty(SeparatorTransformationName)) { repItems.SeparatorTemplate = CMSDataProperties.LoadTransformation(this, SeparatorTransformationName, false); } repItems.DataBindByDefault = false; repItems.OnPageChanged += repItems_OnPageChanged; // Add repeater to the filter collection CMSControlsHelper.SetFilter(ValidationHelper.GetString(GetValue("WebPartControlID"), ClientID), repItems); repItems.HideControlForZeroRows = HideControlForZeroRows; repItems.ZeroRowsText = ZeroRowsText; } } } /// /// OnFilterChanged event handler. /// private void DataSourceControl_OnFilterChanged() { // Reload data if (DataSourceControl != null) { repItems.DataSource = DataSourceControl.DataSource; repItems.DataBind(); binded = true; } } private void repItems_OnPageChanged(object sender, EventArgs e) { // Reload data if (DataSourceControl != null) { repItems.DataSource = DataSourceControl.DataSource; repItems.DataBind(); binded = true; } } private void repItems_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.Controls.Count > 0) { // Try find control with id 'filePreview' MediaFilePreview ctrlFilePreview = e.Item.Controls[0].FindControl("filePreview") as MediaFilePreview; if (ctrlFilePreview != null) { //Set control ctrlFilePreview.IconSet = IconSet; ctrlFilePreview.PreviewSuffix = PreviewSuffix; if (DisplayDetail == true) { // If showing detail show active control ctrlFilePreview.DisplayActiveContent = true; } else { ctrlFilePreview.DisplayActiveContent = DisplayActiveContent; } } } } /// /// Reload data. /// public override void ReloadData() { base.ReloadData(); SetupControl(); SetupControlLoad(); repItems.ReloadData(true); } }