using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.Forums; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.SiteProvider; public partial class CMSWebParts_Forums_ForumFavorites : CMSAbstractWebPart { #region "Private variables" protected string mDeleteImageUrl; #endregion #region "Public properties" /// /// Gets or sets the value that indicates whether editing actions are allowed. /// public bool AllowEditing { get { return ValidationHelper.GetBoolean(GetValue("AllowEditing"), true); } set { SetValue("AllowEditing", value); } } /// /// Gets or sets URL of the delete button image. /// public string DeleteImageUrl { get { return URLHelper.ResolveUrl(DataHelper.GetNotEmpty(GetValue("DeleteImageUrl"), GetImageUrl("CMSModules/CMS_Forums/delete.png"))); } set { mDeleteImageUrl = value; SetValue("DeleteImageUrl", value); } } /// /// Gets or sets the forums URL. /// public string ForumUrl { get { return ValidationHelper.GetString(GetValue("ForumUrl"), ""); } set { SetValue("ForumUrl", value); } } /// /// Gets or sets the site name of favorites. /// public string SiteName { get { return ValidationHelper.GetString(GetValue("SiteName"), ""); } set { SetValue("SiteName", value); } } /// /// Gets or sets the value that indicates whether control should be hidden if no data found. /// public bool HideControlForZeroRows { get { return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), rptFavorites.HideControlForZeroRows); } set { SetValue("HideControlForZeroRows", value); rptFavorites.HideControlForZeroRows = value; } } /// /// Gets or sets the text which is displayed when there is no data. /// public string ZeroRowsText { get { return ValidationHelper.GetString(GetValue("ZeroRowsText"), rptFavorites.ZeroRowsText); } set { SetValue("ZeroRowsText", value); rptFavorites.ZeroRowsText = value; } } #endregion #region "Methods" /// /// Content loaded event handler. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Reloads the control data. /// public override void ReloadData() { base.ReloadData(); SetupControl(); } /// /// Initializes the control properties. /// protected void SetupControl() { if (StopProcessing) { // Do nothing } else { string jsScript = "var favDelMessage ='" + GetString("general.confirmdelete") + "';"; jsScript += "function ForumFavoritesSetValue(id){ document.getElementById('" + hdnValue.ClientID + "').value = id; }"; ltlMessage.Text = ScriptHelper.GetScript(jsScript); mDeleteImageUrl = DeleteImageUrl; rptFavorites.ZeroRowsText = ZeroRowsText; rptFavorites.HideControlForZeroRows = HideControlForZeroRows; BindData(); } } /// /// OnPreRender override - Bind data. /// /// Event args protected override void OnPreRender(EventArgs e) { bool dataBinded = false; if (UseUpdatePanel) { int favoriteId = ValidationHelper.GetInteger(hdnValue.Value, 0); if (favoriteId > 0) { ForumUserFavoritesInfoProvider.DeleteForumUserFavoritesInfo(favoriteId); ClearCache(); BindData(); dataBinded = true; if (UpdatePanel != null) { UpdatePanel.Update(); } } } if (!dataBinded) { BindData(); } hdnValue.Value = null; base.OnPreRender(e); } /// /// Bind data to repeater. /// private void BindData() { if (CMSContext.CurrentUser != null) { int userId = CMSContext.CurrentUser.UserID; int siteId = CMSContext.CurrentSiteID; // If sitename was specified if (SiteName != String.Empty) { // Get site ID SiteInfo si = SiteInfoProvider.GetSiteInfo(SiteName); if (si != null) { siteId = si.SiteID; } } // Get user favorites DataSet ds = null; // Try to get data from cache using (CachedSection cs = new CachedSection(ref ds, CacheMinutes, true, CacheItemName, "forumfavorites", userId, siteId)) { if (cs.LoadData) { // Get the data ds = ForumUserFavoritesInfoProvider.GetFavorites(userId, siteId); // Save to the cache if (cs.Cached) { // Save to the cache cs.CacheDependency = GetCacheDependency(); } cs.Data = ds; } } // Bind data, even empty dataset - delete of last item rptFavorites.DataSource = ds; rptFavorites.DataBind(); // Hide control if no data if (DataHelper.DataSourceIsEmpty(ds)) { if (HideControlForZeroRows) { Visible = false; } else { plcEmptyDSTagEnd.Visible = true; plcEmptyDSTagBegin.Visible = true; } } else { plcEmptyDSTagEnd.Visible = false; plcEmptyDSTagBegin.Visible = false; } } } /// /// Returns link the the forum. /// /// Pots id /// Forum id protected string GetFavoriteLink(object favoritePostId, object favoriteForumId, object postForumID, object postIDPath) { int postId = ValidationHelper.GetInteger(favoritePostId, 0); int forumId = ValidationHelper.GetInteger(favoriteForumId, 0); int postForumId = ValidationHelper.GetInteger(postForumID, forumId); string postIdPath = ValidationHelper.GetString(postIDPath, ""); string link = "#"; // Post favorite if (postId > 0) { ForumInfo forumInfo = ForumInfoProvider.GetForumInfo(postForumId); if (forumInfo != null) { // If forum URL is not set, try to use base forum url string forumUrl = (ForumUrl == String.Empty) ? forumInfo.ForumBaseUrl : ForumUrl; int threadId = ForumPostInfoProvider.GetPostRootFromIDPath(postIdPath); if (String.IsNullOrEmpty(forumUrl)) { forumUrl = URLHelper.CurrentURL; } link = URLHelper.UpdateParameterInUrl(ResolveUrl(forumUrl), "forumid", postForumId.ToString()); link = URLHelper.UpdateParameterInUrl(link, "threadid", threadId.ToString()); link = URLHelper.RemoveParameterFromUrl(link, "thread"); link = URLHelper.RemoveParameterFromUrl(link, "mode"); link = URLHelper.RemoveParameterFromUrl(link, "postid"); link = URLHelper.RemoveParameterFromUrl(link, "replyto"); link = URLHelper.RemoveParameterFromUrl(link, "subscribeto"); } } // Forum favorite else if (forumId > 0) { ForumInfo forumInfo = ForumInfoProvider.GetForumInfo(forumId); if (forumInfo != null) { // If forum URL is not set, try to use base forum url string forumUrl = (ForumUrl == String.Empty) ? forumInfo.ForumBaseUrl : ForumUrl; if (String.IsNullOrEmpty(forumUrl)) { forumUrl = URLHelper.CurrentURL; } link = URLHelper.UpdateParameterInUrl(ResolveUrl(forumUrl), "forumid", forumId.ToString()); link = URLHelper.RemoveParameterFromUrl(link, "threadid"); link = URLHelper.RemoveParameterFromUrl(link, "thread"); link = URLHelper.RemoveParameterFromUrl(link, "mode"); link = URLHelper.RemoveParameterFromUrl(link, "postid"); link = URLHelper.RemoveParameterFromUrl(link, "replyto"); link = URLHelper.RemoveParameterFromUrl(link, "subscribeto"); } } return HTMLHelper.EncodeLinkReference(link); } /// /// Handles delete button action - deletes user favorite. /// protected void btnDelete_OnCommand(object sender, CommandEventArgs e) { if (e.CommandName == "delete") { int favoriteId = ValidationHelper.GetInteger(e.CommandArgument, 0); ForumUserFavoritesInfoProvider.DeleteForumUserFavoritesInfo(favoriteId); ClearCache(); BindData(); } } /// /// Clear cache. /// public override void ClearCache() { int userId = CMSContext.CurrentUser.UserID; int siteId = CMSContext.CurrentSiteID; // If sitename was specified if (SiteName != String.Empty) { // Get site ID SiteInfo si = SiteInfoProvider.GetSiteInfo(SiteName); if (si != null) { siteId = si.SiteID; } } string useCacheItemName = DataHelper.GetNotEmpty(CacheItemName, CacheHelper.BaseCacheKey + "|" + ClientID + "|" + userId + "|" + siteId) + "|forumfavorites"; CacheHelper.Remove(useCacheItemName); base.ClearCache(); } /// /// Gets the default cache dependencies for the data source. /// public override string GetDefaultCacheDependendencies() { // Get default dependencies string result = base.GetDefaultCacheDependendencies(); if (result != null) { result += "\n"; } result += "forums.forumuserfavorites|byuserid|" + CMSContext.CurrentUser.UserID; return result; } #endregion }