Created
May 8, 2012 02:49
-
-
Save SitefinityGuru/2632194 to your computer and use it in GitHub Desktop.
Custom Taxonomy Control to filter taxonomy by Blog ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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