using System; using System.Data; using System.Web; using System.Web.UI; using CMS.CMSHelper; using CMS.Controls; using CMS.GlobalHelper; using CMS.PortalControls; public partial class CMSWebParts_Membership_Users_OnlineUsers : CMSAbstractWebPart { #region "Public properties" /// /// Select only top n users. /// public int SelectTopN { get { return ValidationHelper.GetInteger(GetValue("SelectTopN"), 0); } set { SetValue("SelectTopN", value); } } /// /// Select only users localized in specified path. /// public string Path { get { return ValidationHelper.GetString(GetValue("Path"), ""); } set { SetValue("Path", value); } } /// /// Gets or sets the transformation name. /// public string TransformationName { get { return ValidationHelper.GetString(GetValue("TransformationName"), ""); } set { SetValue("TransformationName", value); } } /// /// Gets or sets the additional info text. /// public string AdditionalInfoText { get { return ValidationHelper.GetString(GetValue("AdditionalInfoText"), ""); } set { SetValue("AdditionalInfoText", value); } } /// /// Gets or sets the no users on-line text. /// public string NoUsersOnlineText { get { return ValidationHelper.GetString(GetValue("NoUsersOnlineText"), ""); } set { SetValue("NoUsersOnlineText", value); } } /// /// Gets or sets columns to be retrieved from database. /// public string Columns { get { return ValidationHelper.GetString(GetValue("Columns"), ""); } set { SetValue("Columns", value); } } #endregion #region "Events" /// /// OnPreRender override method. /// protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); SetupControl(); } #endregion #region "Methods" /// /// Setups control properties. /// protected void SetupControl() { // Check StopProcessing property if (StopProcessing) { Visible = false; } else { SetContext(); DataSet users = null; bool transLoaded = false; // Load transformation if (!string.IsNullOrEmpty(TransformationName)) { repUsers.ItemTemplate = CMSDataProperties.LoadTransformation(this, TransformationName, false); transLoaded = true; } if ((transLoaded) || (!String.IsNullOrEmpty(Path))) { // Try to get data from cache using (CachedSection cs = new CachedSection(ref users, CacheMinutes, true, CacheItemName, "onlineusers", CMSContext.CurrentSiteName, SelectTopN, Columns, Path)) { if (cs.LoadData) { // Get the data users = SessionManager.GetOnlineUsers(null, null, SelectTopN, Columns, CMSContext.ResolveCurrentPath(Path), CMSContext.CurrentSiteName, false, false); // Prepare the cache dependency if (cs.Cached) { cs.CacheDependency = GetCacheDependency(); } cs.Data = users; } } // Data bind if (!DataHelper.DataSourceIsEmpty(users)) { // Set to repeater repUsers.DataSource = users; repUsers.DataBind(); } } int authenticated = 0; int publicUsers = 0; string numbers = string.Empty; // Get or generate cache item name string cacheItemNameNumbers = CacheItemName; if (!string.IsNullOrEmpty(cacheItemNameNumbers)) { cacheItemNameNumbers += "Number"; } // Try to get data from cache using (CachedSection cs = new CachedSection(ref numbers, CacheMinutes, true, cacheItemNameNumbers, "onlineusersnumber", CMSContext.CurrentSiteName, Path)) { if (cs.LoadData) { // Get the data SessionManager.GetUsersNumber(CurrentSiteName, CMSContext.ResolveCurrentPath(Path), false, false, out publicUsers, out authenticated); // Save to the cache if (cs.Cached) { cs.CacheDependency = GetCacheDependency(); } cs.Data = publicUsers.ToString() + ";" + authenticated.ToString(); } else if (!String.IsNullOrEmpty(numbers)) { // Retrieved from cache string[] nums = numbers.Split(';'); publicUsers = ValidationHelper.GetInteger(nums[0], 0); authenticated = ValidationHelper.GetInteger(nums[1], 0); } } // Check if at least one user is online if ((publicUsers + authenticated) == 0) { ltrAdditionaInfos.Text = NoUsersOnlineText; } else { ltrAdditionaInfos.Text = string.Format(AdditionalInfoText, publicUsers + authenticated, publicUsers, authenticated); } } ReleaseContext(); } /// /// Clears the cached items. /// public override void ClearCache() { string useCacheItemName = DataHelper.GetNotEmpty(CacheItemName, CacheHelper.GetCacheItemName("onlineusers", CMSContext.CurrentSiteName, SelectTopN, Columns, Path)); CacheHelper.ClearCache(useCacheItemName); // Get or generate cache item name for number string cacheItemNameNumbers = CacheItemName; if (!string.IsNullOrEmpty(cacheItemNameNumbers)) { cacheItemNameNumbers += "Number"; } string useCacheItemNameNumber = DataHelper.GetNotEmpty(cacheItemNameNumbers, CacheHelper.GetCacheItemName("onlineusersnumber", CMSContext.CurrentSiteName, Path)); CacheHelper.ClearCache(useCacheItemNameNumber); } #endregion }