MonthList.cs :  » Bloggers » BlogEngine.NET » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » BlogEngine.NET 
BlogEngine.NET » Controls » MonthList.cs
#region Using

using System;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using BlogEngine.Core;

#endregion

namespace Controls{
  /// <summary>
  /// Builds a category list.
  /// </summary>
  public class MonthList : Control
  {
    private const double CacheTimeoutInHours = 1;

    static MonthList()
    {
      Post.Saved += Post_Saved;
    }

    static void Post_Saved(object sender, SavedEventArgs e)
    {
      // invalidate cache whenever a post is modified
      lock (_SyncRoot)
      {
        HttpRuntime.Cache.Remove(CacheKey);
      }

    }

    #region Private fields

    private readonly static object _SyncRoot = new object();
    private const string CacheKey = "BE_MonthListCacheKey";
    #endregion

    private static SortedDictionary<DateTime, int> GetPostsPerMonth()
    {
      lock (_SyncRoot)
      {
        SortedDictionary<DateTime, int> months = HttpRuntime.Cache[CacheKey] as SortedDictionary<DateTime, int>;
        if (months == null)
        {
          months = new SortedDictionary<DateTime, int>();
          // let dictionary expire after 1 hour
          HttpRuntime.Cache.Insert(CacheKey, months, null, DateTime.Now.AddHours(CacheTimeoutInHours), Cache.NoSlidingExpiration);

          foreach (Post post in Post.Posts)
          {
            if (post.IsVisibleToPublic)
            {
              DateTime month = new DateTime(post.DateCreated.Year, post.DateCreated.Month, 1);
              int count;
              // if the date is not in the dictionary, count will be set to 0
              months.TryGetValue(month, out count);
              ++count;
              months[month] = count;
            }
          }
        }
        return months;
      }
    }

    private string RenderMonths()
    {
      SortedDictionary<DateTime, int> months = GetPostsPerMonth();
      if (months.Keys.Count == 0)
        return "<p>" + Resources.labels.none + "</p>";

      HtmlGenericControl ul = new HtmlGenericControl("ul");
      ul.Attributes.Add("id", "monthList");
      HtmlGenericControl year = null;
      HtmlGenericControl list = null;
      int current = 0;

      foreach (DateTime date in months.Keys)
      {
        if (current == 0)
          current = date.Year;

        if (date.Year > current || ul.Controls.Count == 0)
        {
          list = new HtmlGenericControl("ul");
          list.ID = "year" + date.Year.ToString();

          year = new HtmlGenericControl("li");
          year.Attributes.Add("class", "year");
          year.Attributes.Add("onclick", "BlogEngine.toggleMonth('year" + date.Year + "')");
          year.InnerHtml = date.Year.ToString();
          year.Controls.Add(list);

          if (date.Year == DateTime.Now.Year)
            list.Attributes.Add("class", "open");

          ul.Controls.AddAt(0, year);
        }

        HtmlGenericControl li = new HtmlGenericControl("li");

        HtmlAnchor anc = new HtmlAnchor();
        anc.HRef = Utils.RelativeWebRoot + date.Year + "/" + date.ToString("MM") + "/default" + BlogSettings.Instance.FileExtension;
        anc.InnerHtml = DateTime.Parse(date.Year + "-" + date.Month + "-01").ToString("MMMM") + " (" + months[date] + ")";

        li.Controls.Add(anc);
        list.Controls.AddAt(0, li);
        current = date.Year;
      }

      System.IO.StringWriter sw = new System.IO.StringWriter();
      ul.RenderControl(new HtmlTextWriter(sw));
      return sw.ToString();
    }

    private SortedDictionary<string, Guid> SortGategories(Dictionary<Guid, string> categories)
    {
      SortedDictionary<string, Guid> dic = new SortedDictionary<string, Guid>();
      foreach (Guid key in categories.Keys)
      {
        dic.Add(categories[key], key);
      }

      return dic;
    }

    /// <summary>
    /// Renders the control.
    /// </summary>
    public override void RenderControl(HtmlTextWriter writer)
    {
      if (Post.Posts.Count > 0)
      {
        string html = RenderMonths();
        writer.Write(html);
        writer.Write(Environment.NewLine);
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.