Skip to content

Instantly share code, notes, and snippets.

@SitefinityGuru
Created May 8, 2012 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SitefinityGuru/2632194 to your computer and use it in GitHub Desktop.
Save SitefinityGuru/2632194 to your computer and use it in GitHub Desktop.
Custom Taxonomy Control to filter taxonomy by Blog ID
/// <summary>
/// Custom Sitefinity Taxonomy Control to display taxonomy items for a specific Blog
/// </summary>
public class BlogTaxonomyControl : TaxonomyControl
{
public Guid ParentBlogID { get; set; }
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// this control is specifically for blogs, set the ContentType to match
this.ContentType = typeof(Telerik.Sitefinity.Blogs.Model.BlogPost);
}
/// <summary>
/// Returns a dictionary where the key is the taxon and value is
/// the number is the count of the times that the taxon is used(marked)
/// </summary>
/// <returns></returns>
protected override Dictionary<ITaxon, uint> GetTaxaItemsCountForTaxonomy()
{
// get all taxonomy items from published blog posts
Guid taxonomyId = this.TaxonomyId;
IQueryable<TaxonomyStatistic> queryable = from t in this.CurrentTaxonomyManager.GetStatistics()
where ((t.TaxonomyId == taxonomyId) && (t.MarkedItemsCount >= 0)) &&
(t.StatisticType == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live) &&
(t.DataItemType == this.ContentType.ToString())
select t;
// is there a TaxaCount maximum?
if (this.TaxaCount > 0)
{
queryable = (from r in queryable
orderby r.MarkedItemsCount descending
select r).Take<TaxonomyStatistic>(this.TaxaCount);
}
// fill a dictionary with the taxa and counts
Dictionary<ITaxon, uint> dictionary = new Dictionary<ITaxon, uint>();
foreach (TaxonomyStatistic statistic in queryable)
{
// match the taxonomy items to the blog with the specified ID
if (ParentBlogID != Guid.Empty)
{
var posts = App.WorkWith().Blog(ParentBlogID).BlogPosts().Where(t => t.GetValue<TrackedList<Guid>>(this.FieldName).Contains(statistic.TaxonId)).Get();
if (posts.Count() == 0) continue;
}
// add to dictionary
ITaxon key = this.CurrentTaxonomyManager.GetTaxon(statistic.TaxonId);
if ((key != null) && !dictionary.ContainsKey(key))
{
if (key is HierarchicalTaxon)
{
HierarchicalTaxon taxon2 = key as HierarchicalTaxon;
if (!taxon2.AvailableLanguages.Contains<string>(key.Title.CurrentLanguage.Name) && ((taxon2.AvailableLanguages.Count<string>() != 1) || (taxon2.AvailableLanguages[0] != string.Empty)))
{
continue;
}
}
dictionary.Add(key, statistic.MarkedItemsCount);
}
}
return dictionary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment