using System; using System.Data; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.DocumentEngine; public partial class CMSWebParts_TaggingCategories_TagCloud : CMSAbstractWebPart { #region "Variables" private string aliasPath = null; private string siteName = null; private string cultureCode = null; private TagGroupInfo tgi = null; #endregion #region "Public properties" /// /// Gets or sets the group of tags which should be displayed. /// public string TagGroupName { get { string tagGroupName = ValidationHelper.GetString(GetValue("TagGroupName"), String.Empty); // If tag group name not set if (String.IsNullOrEmpty(tagGroupName)) { int tagGroupId = 0; // Get tag group ID from current document or get its inherited value if (CMSContext.CurrentDocument.DocumentTagGroupID != 0) { tagGroupId = CMSContext.CurrentDocument.DocumentTagGroupID; } else { tagGroupId = ValidationHelper.GetInteger(CMSContext.CurrentDocument.GetInheritedValue("DocumentTagGroupID", SiteInfoProvider.CombineWithDefaultCulture(CMSContext.CurrentSiteName)), 0); } // Get tag group information TagGroupInfo tgi = TagGroupInfoProvider.GetTagGroupInfo(tagGroupId); if (tgi != null) { // Get tag group name tagGroupName = tgi.TagGroupName; } } return tagGroupName; } set { SetValue("TagGroupName", value); } } /// /// Gets or sets the SQL order by expression. /// public string OrderBy { get { string orderBy = ValidationHelper.GetString(GetValue("OrderBy"), ""); return (orderBy == "") ? "TagName ASC" : orderBy; } set { SetValue("OrderBy", value); } } /// /// Gets or sets the SQL TOP N number. /// public int SelectTopN { get { return ValidationHelper.GetInteger(GetValue("SelectTopN"), -1); } set { SetValue("SelectTopN", value); } } /// /// Gets or sets the HTML code which is placed between each two tags. /// public string TagSeparator { get { return ValidationHelper.GetString(GetValue("TagSeparator"), " "); } set { SetValue("TagSeparator", value); } } /// /// Gets or sets the miminal font size (for the tag with the lowest count). /// public int MinTagSize { get { return ValidationHelper.GetInteger(GetValue("MinTagSize"), 10); } set { SetValue("MinTagSize", value); } } /// /// Gets or sets the miminal font size (for the tag with the highest count). /// public int MaxTagSize { get { return ValidationHelper.GetInteger(GetValue("MaxTagSize"), 20); } set { SetValue("MaxTagSize", value); } } /// /// Gets or sets the site name. /// public string SiteName { get { return ValidationHelper.GetString(GetValue("SiteName"), ""); } set { SetValue("SiteName", value); } } /// /// Gets or sets the alias path. /// public string AliasPath { get { return ValidationHelper.GetString(GetValue("AliasPath"), ""); } set { SetValue("AliasPath", value); } } /// /// Gets or sets the culture code. /// public string CultureCode { get { return ValidationHelper.GetString(GetValue("CultureCode"), ""); } set { SetValue("CultureCode", value); } } /// /// Gets or sets the where condition. /// public string WhereCondition { get { return ValidationHelper.GetString(GetValue("WhereCondition"), ""); } set { SetValue("WhereCondition", value); } } /// /// Gets or sets combine with default culture. /// public bool CombineWithDefaultCulture { get { return ValidationHelper.GetBoolean(GetValue("CombineWithDefaultCulture"), SettingsKeyProvider.GetBoolValue(SiteName + ".CMSCombineWithDefaultCulture")); } set { SetValue("CombineWithDefaultCulture", value); } } /// /// Gets or sets select only published. /// public bool SelectOnlyPublished { get { return ValidationHelper.GetBoolean(GetValue("SelectOnlyPublished"), true); } set { SetValue("SelectOnlyPublished", value); } } /// /// Gets or sets max relative level. /// public int MaxRelativeLevel { get { return ValidationHelper.GetInteger(GetValue("MaxRelativeLevel"), -1); } set { SetValue("MaxRelativeLevel", value); } } /// /// Gets or sets the path of the document which will be used as a link URL for tags. /// public string DocumentListPath { get { return ValidationHelper.GetString(GetValue("DocumentListURL"), ""); } set { SetValue("DocumentListURL", value); } } /// /// Gets or sets the name of the query string parameter which will pass the TagID to the DocumentListURL. /// public string QueryStringName { get { string name = Convert.ToString(GetValue("QueryStringName")); if (String.IsNullOrEmpty(name)) { name = "tagid"; } return name; } set { SetValue("QueryStringName", value); } } /// /// Indicates if the control should be hidden when no data found. /// public bool HideControlForZeroRows { get { return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), false); } set { SetValue("HideControlForZeroRows", value); } } /// /// Indicates if the tags should be retrieved according to document filter settings. /// public bool UseDocumentFilter { get { object value = GetValue("UseDocumentFilter"); if (value == null) { return !((AliasPath == TreeProvider.ALL_DOCUMENTS) && (CultureCode == TreeProvider.ALL_CULTURES) && String.IsNullOrEmpty(WhereCondition) && (MaxRelativeLevel < 0)); } else { return ValidationHelper.GetBoolean(value, false); } } set { SetValue("UseDocumentFilter", value); } } /// /// Gets or sets the text which is displayed to the user when no data found. /// public string ZeroRowsText { get { return ValidationHelper.GetString(GetValue("ZeroRowsText"), ""); } set { SetValue("ZeroRowsText", value); } } #endregion #region "Overridden methods" /// /// Content loaded event handler. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Clears the cached items. /// public override void ClearCache() { string useCacheItemName = DataHelper.GetNotEmpty(CacheItemName, CacheHelper.BaseCacheKey + "|" + URLHelper.Url.ToString() + "|" + ClientID); CacheHelper.ClearCache(useCacheItemName); } /// /// Reload data. /// public override void ReloadData() { base.ReloadData(); SetupControl(); } #endregion /// /// Initializes the control properties. /// protected void SetupControl() { if (StopProcessing) { // Do nothing } else { try { // Prepare alias path aliasPath = AliasPath; if (String.IsNullOrEmpty(aliasPath)) { aliasPath = "/%"; } aliasPath = CMSContext.ResolveCurrentPath(aliasPath); // Prepare site name siteName = SiteName; if (String.IsNullOrEmpty(siteName)) { siteName = CMSContext.CurrentSiteName; } // Prepare culture code cultureCode = CultureCode; if (String.IsNullOrEmpty(cultureCode)) { cultureCode = CMSContext.PreferredCultureCode; } string renderedTags = null; // Try to get data from cache using (CachedSection cs = new CachedSection(ref renderedTags, CacheMinutes, true, CacheItemName, "tagcloud", TagGroupName, OrderBy, SelectTopN, DocumentListPath, TagSeparator, QueryStringName, MaxTagSize, MinTagSize, "documents", siteName, aliasPath, CacheHelper.GetCultureCacheKey(cultureCode), CombineWithDefaultCulture, WhereCondition, SelectOnlyPublished, MaxRelativeLevel)) { if (cs.LoadData) { // Get the correct range int maxSize = Math.Max(MaxTagSize, MinTagSize); int minSize = Math.Min(MaxTagSize, MinTagSize); // Get the tags SiteInfo si = SiteInfoProvider.GetSiteInfo(siteName); int siteId = 0; if (si != null) { siteId = si.SiteID; } // Get tag group info tgi = TagGroupInfoProvider.GetTagGroupInfo(TagGroupName, siteId); // Get the data DataSet ds = null; if (!UseDocumentFilter) { // Get the tag group if (tgi != null) { // Get the tags for group ds = TagInfoProvider.GetTags("TagGroupID = " + tgi.TagGroupID, OrderBy, SelectTopN); } } else { // Get the tags for documents string comleteWhere = TreeProvider.GetCompleteWhereCondition(siteName, aliasPath, cultureCode, CombineWithDefaultCulture, WhereCondition, SelectOnlyPublished, MaxRelativeLevel); ds = TagInfoProvider.GetTags(TagGroupName, siteId, comleteWhere, OrderBy, SelectTopN); } // DS must have at least three columns (fist for IDs, second for names, third for counts) if (!DataHelper.DataSourceIsEmpty(ds)) { // First we need to find the maximum and minimum int max = Int32.MinValue; int min = Int32.MaxValue; foreach (DataRow dr in ds.Tables[0].Rows) { int tagCount = ValidationHelper.GetInteger(dr["TagCount"], 0); max = Math.Max(tagCount, max); min = Math.Min(tagCount, min); } // Base URL of the links string url; if (String.IsNullOrEmpty(DocumentListPath)) { url = URLHelper.CurrentURL; } else { url = CMSContext.GetUrl(CMSContext.ResolveCurrentPath(DocumentListPath)); } url = URLHelper.ResolveUrl(url); // Now generate the tags int count = ds.Tables[0].Rows.Count; StringBuilder sb = new StringBuilder(count * 100); int index = 0; // Process the tags foreach (DataRow dr in ds.Tables[0].Rows) { if (index > 0) { sb.Append(TagSeparator + "\n"); } // Count the percentage and get the final size of the tag int tagCount = ValidationHelper.GetInteger(dr["TagCount"], 0); int val = (min == max ? 100 : (((tagCount - min) * 100) / (max - min))); int pixelSize = minSize + ((val * (maxSize - minSize)) / 100); // Create the link with query string parameter string paramUrl = URLHelper.AddParameterToUrl(url, QueryStringName, ValidationHelper.GetString(dr["TagID"], "")); sb.Append("" + HTMLHelper.HTMLEncode(dr["TagName"].ToString()) + ""); index++; } renderedTags = sb.ToString(); } // Save to cache if (cs.Cached) { cs.CacheDependency = GetCacheDependency(); } cs.Data = renderedTags; } } if (String.IsNullOrEmpty(renderedTags)) { // Ensure no data behavior if (HideControlForZeroRows) { Visible = false; } else { renderedTags = ZeroRowsText; } } // Display the tags ltlTags.Text = renderedTags; } catch (Exception ex) { // Display the error ltlTags.Text = "
" + ex.Message + "
"; } } } /// /// Gets the default cache dependencies for the data source. /// public override string GetDefaultCacheDependendencies() { string result = base.GetDefaultCacheDependendencies(); // Add the document dependencies (based on path and site name) if (UseDocumentFilter) { if (result != null) { result += "\n"; } result += TreeProvider.GetDocumentCacheDependencies(siteName, null, aliasPath, 0); } // Add tag group dependency if (tgi != null) { if (result != null) { result += "\n"; } result += "cms.taggroup|byid|" + tgi.TagGroupID; } return result; } }