using System; using System.Collections.Generic; 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.Controls; using CMS.GlobalHelper; using CMS.SettingsProvider; public partial class CMSModules_MediaLibrary_Controls_Filters_MediaLibrarySort : CMSAbstractDataFilterControl { #region "Variables" private static List columns = new List() { "filename", "filecreatedwhen", "filesize" }; private string mFileIDQueryStringKey = null; private string mSortQueryStringKey = null; private int mFilterMethod = 0; #endregion #region "Properties" /// /// Sort by value. /// public string SortBy { get { return ValidationHelper.GetString(ViewState["SortBy"], String.Empty); } set { ViewState["SortBy"] = (object)value; } } /// /// Gets or sets the file id querystring parameter. /// public string FileIDQueryStringKey { get { return mFileIDQueryStringKey; } set { mFileIDQueryStringKey = value; } } /// /// Gets or sets the sort querystring parameter. /// public string SortQueryStringKey { get { return mSortQueryStringKey; } set { mSortQueryStringKey = value; } } /// /// Gets or sets the filter method. /// public int FilterMethod { get { return mFilterMethod; } set { mFilterMethod = value; } } #endregion protected override void OnInit(EventArgs e) { base.OnInit(e); SetupControls(); } protected void Page_Load(object sender, EventArgs e) { if (QueryHelper.GetInteger(FileIDQueryStringKey, 0) > 0) { StopProcessing = true; Visible = false; } else { if (FilterMethod != 0) { OrderBy = SortBy; } else { string[] orderBy = QueryHelper.GetString(SortQueryStringKey, "").Split(';'); if ((orderBy.Length == 2) && columns.Contains(orderBy[0].ToLowerCSafe()) && ((orderBy[1].ToLowerCSafe() == "asc") || (orderBy[1].ToLowerCSafe() == "desc"))) { OrderBy = String.Format("{0} {1}", orderBy[0], orderBy[1]); } } RaiseOnFilterChanged(); } } #region "Links handlers" protected void lnkName_Click(object sender, EventArgs e) { if (FilterMethod == 1) { if ((SortBy != null) && (SortBy.EndsWithCSafe("ASC"))) { SortBy = "FileName DESC"; } else { SortBy = "FileName ASC"; } OrderBy = SortBy; RaiseOnFilterChanged(); } else { if (!String.IsNullOrEmpty(SortQueryStringKey)) { string sort = QueryHelper.GetString(SortQueryStringKey, String.Empty); if (sort.StartsWithCSafe("FileName")) { if (sort.EndsWithCSafe("ASC")) { RedirectToUpdatedUrl("FileName;DESC"); } else { RedirectToUpdatedUrl("FileName;ASC"); } } else { RedirectToUpdatedUrl("FileName;ASC"); } } } } protected void lnkDate_Click(object sender, EventArgs e) { if (FilterMethod == 1) { if ((SortBy != null) && (SortBy.EndsWithCSafe("ASC"))) { SortBy = "FileCreatedWhen DESC"; } else { SortBy = "FileCreatedWhen ASC"; } OrderBy = SortBy; RaiseOnFilterChanged(); } else { if (!String.IsNullOrEmpty(SortQueryStringKey)) { string sort = QueryHelper.GetString(SortQueryStringKey, String.Empty); if (sort.StartsWithCSafe("FileCreatedWhen")) { if (sort.EndsWithCSafe("ASC")) { RedirectToUpdatedUrl("FileCreatedWhen;DESC"); } else { RedirectToUpdatedUrl("FileCreatedWhen;ASC"); } } else { RedirectToUpdatedUrl("FileCreatedWhen;ASC"); } } } } protected void lnkSize_Click(object sender, EventArgs e) { if (FilterMethod == 1) { if ((SortBy != null) && (SortBy.EndsWithCSafe("ASC"))) { SortBy = "FileSize DESC"; } else { SortBy = "FileSize ASC"; } OrderBy = SortBy; RaiseOnFilterChanged(); } else { if (!String.IsNullOrEmpty(SortQueryStringKey)) { string sort = QueryHelper.GetString(SortQueryStringKey, String.Empty); if (sort.StartsWithCSafe("FileSize")) { if (sort.EndsWithCSafe("ASC")) { RedirectToUpdatedUrl("FileSize;DESC"); } else { RedirectToUpdatedUrl("FileSize;ASC"); } } else { RedirectToUpdatedUrl("FileSize;ASC"); } } } } #endregion #region "Private methods" /// /// Setup controls. /// private void SetupControls() { if (StopProcessing) { // Do nothing } else { lnkDate.Text = ResHelper.GetString("media.library.sort.date"); lnkName.Text = ResHelper.GetString("media.library.sort.name"); lnkSize.Text = ResHelper.GetString("media.library.sort.size"); } } /// /// Redirect to updated url. /// /// Value private void RedirectToUpdatedUrl(string value) { URLHelper.Redirect(URLHelper.UpdateParameterInUrl(URLHelper.CurrentURL, SortQueryStringKey, value)); } #endregion }