PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/archive.aspx.cs

#
C# | 247 lines | 192 code | 43 blank | 12 comment | 23 complexity | c3647367ec8a4ce7636684e024479616 MD5 | raw file
  1#region Using
  2
  3using System;
  4using System.Web;
  5using System.Collections.Generic;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.HtmlControls;
  9using BlogEngine.Core;
 10
 11#endregion
 12
 13public partial class archive : BlogEngine.Core.Web.Controls.BlogBasePage
 14{
 15	/// <summary>
 16	/// Handles the Load event of the Page control.
 17	/// </summary>
 18	/// <param name="sender">The source of the event.</param>
 19	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 20	protected void Page_Load(object sender, EventArgs e)
 21	{
 22		if (!IsPostBack && !IsCallback)
 23		{
 24			CreateMenu();
 25			CreateArchive();
 26			AddTotals();
 27		}
 28
 29		Page.Title = Server.HtmlEncode(Resources.labels.archive);
 30		base.AddMetaTag("description", Resources.labels.archive + " | " + BlogSettings.Instance.Name);
 31	}
 32
 33	/// <summary>
 34	/// Creates the category top menu.
 35	/// </summary>
 36	private void CreateMenu()
 37	{
 38        var categories = new List<string>();
 39
 40		foreach (Category cat in Category.ApplicableCategories)
 41		{
 42            if (cat.Posts.Count > 0)
 43            {
 44                if (!categories.Contains(cat.Title))
 45                {
 46                    AddCategoryToMenu(cat.Title);
 47                    categories.Add(cat.Title);
 48                }
 49            }
 50		}
 51	}
 52
 53	private void AddCategoryToMenu(string title)
 54	{
 55		HtmlAnchor a = new HtmlAnchor();
 56		a.InnerHtml = Server.HtmlEncode(title);
 57		a.HRef = string.Format("{0}archive{1}#cat-{2}", Blog.CurrentInstance.RelativeWebRoot, BlogConfig.FileExtension, Utils.RemoveIllegalCharacters(title));
 58		a.Attributes.Add("rel", "directory");
 59
 60		HtmlGenericControl li = new HtmlGenericControl("li");
 61		li.Controls.Add(a);
 62		ulMenu.Controls.Add(li);
 63	}
 64
 65	/// <summary>
 66	/// Sorts the categories.
 67	/// </summary>
 68	/// <param name="categories">The categories.</param>
 69	private SortedDictionary<string, Guid> SortCategories(Dictionary<Guid, string> categories)
 70	{
 71		SortedDictionary<string, Guid> dic = new SortedDictionary<string, Guid>();
 72		foreach (Category cat in Category.Categories)
 73		{
 74			bool postsExist = cat.Posts.FindAll(delegate(Post post)
 75			{
 76				return post.IsVisible;
 77			}).Count > 0;
 78
 79			if (postsExist)
 80				dic.Add(cat.Title, cat.Id);
 81		}
 82
 83		return dic;
 84	}
 85
 86	private void CreateArchive()
 87	{
 88        var categories = new List<string>();
 89
 90		foreach (Category cat in Category.ApplicableCategories)
 91		{
 92            if (cat.Posts.Count > 0)
 93            {
 94                if (!categories.Contains(cat.Title))
 95                {
 96                    string name = cat.Title;
 97                    List<Post> list = cat.Posts.FindAll(delegate(Post p) { return p.IsVisible; });
 98
 99                    HtmlGenericControl h2 = CreateRowHeader(cat, name, list.Count);
100                    phArchive.Controls.Add(h2);
101
102                    HtmlTable table = CreateTable(name);
103                    foreach (Post post in list)
104                    {
105                        CreateTableRow(table, post);
106                    }
107
108                    phArchive.Controls.Add(table);
109                    categories.Add(cat.Title);
110                }
111            }
112		}
113
114		List<Post> noCatList = Post.ApplicablePosts.FindAll(delegate(Post p) { return p.Categories.Count == 0 && p.IsVisible; });
115		if (noCatList.Count > 0)
116		{
117			string name = Resources.labels.uncategorized;
118			HtmlGenericControl h2 = CreateRowHeader(null, name, noCatList.Count);
119			phArchive.Controls.Add(h2);
120
121			HtmlTable table = CreateTable(name);
122			foreach (Post post in noCatList)
123			{
124				CreateTableRow(table, post);
125			}
126
127			phArchive.Controls.Add(table);
128
129			AddCategoryToMenu(name);
130		}
131	}
132
133	private static HtmlGenericControl CreateRowHeader(Category cat, string name, int count)
134	{
135		HtmlGenericControl h2 = new HtmlGenericControl("h2");
136		h2.Attributes["id"] = "cat-" + Utils.RemoveIllegalCharacters(name);
137
138        if (cat != null)
139		{
140			HtmlAnchor feed = new HtmlAnchor();
141            feed.HRef = cat.FeedRelativeLink;
142
143			HtmlImage img = new HtmlImage();
144            img.Src = Utils.RelativeWebRoot + "Content/images/blog/rssButton.png";
145			img.Alt = "RSS";
146			feed.Controls.Add(img);
147			h2.Controls.Add(feed);
148		}
149
150		Control header = new LiteralControl(name + " (" + count + ")");
151		h2.Controls.Add(header);
152		return h2;
153	}
154
155	private static void CreateTableRow(HtmlTable table, Post post)
156	{
157		HtmlTableRow row = new HtmlTableRow();
158
159		HtmlTableCell date = new HtmlTableCell();
160		date.InnerHtml = post.DateCreated.ToString("yyyy-MM-dd");
161		date.Attributes.Add("class", "date");
162		row.Cells.Add(date);
163
164		HtmlTableCell title = new HtmlTableCell();
165		title.InnerHtml = string.Format("<a href=\"{0}\">{1}</a>", post.RelativeLink, post.Title);
166		title.Attributes.Add("class", "title");
167		row.Cells.Add(title);
168
169		if (BlogSettings.Instance.IsCommentsEnabled)
170		{
171			HtmlTableCell comments = new HtmlTableCell();
172
173            if(BlogSettings.Instance.ModerationType == BlogSettings.Moderation.Disqus)
174                comments.InnerHtml = string.Format("<span><a href=\"{0}#disqus_thread\">{1}</a></span>", post.PermaLink, Resources.labels.comments);
175            else
176			    comments.InnerHtml = post.ApprovedComments.Count.ToString();
177
178			comments.Attributes.Add("class", "comments");
179			row.Cells.Add(comments);
180		}
181
182		if (BlogSettings.Instance.EnableRating)
183		{
184			HtmlTableCell rating = new HtmlTableCell();
185			rating.InnerHtml = post.Raters == 0 ? "None" : Math.Round(post.Rating, 1).ToString();
186			rating.Attributes.Add("class", "rating");
187			row.Cells.Add(rating);
188		}
189
190		table.Rows.Add(row);
191	}
192
193	private HtmlTable CreateTable(string name)
194	{
195		HtmlTable table = new HtmlTable();
196		table.Attributes.Add("summary", name);
197
198		HtmlTableRow header = new HtmlTableRow();
199
200		HtmlTableCell date = new HtmlTableCell("th");
201        date.InnerHtml = Utils.Translate("date");
202		header.Cells.Add(date);
203
204		HtmlTableCell title = new HtmlTableCell("th");
205        title.InnerHtml = Utils.Translate("title");
206		header.Cells.Add(title);
207
208		if (BlogSettings.Instance.IsCommentsEnabled)
209		{
210			HtmlTableCell comments = new HtmlTableCell("th");
211			comments.InnerHtml = Utils.Translate("comments");
212			comments.Attributes.Add("class", "comments");
213			header.Cells.Add(comments);
214		}
215
216		if (BlogSettings.Instance.EnableRating)
217		{
218			HtmlTableCell rating = new HtmlTableCell("th");
219            rating.InnerHtml = Utils.Translate("rating");
220			rating.Attributes.Add("class", "rating");
221			header.Cells.Add(rating);
222		}
223
224		table.Rows.Add(header);
225
226		return table;
227	}
228
229	private void AddTotals()
230	{
231		int comments = 0;
232		int raters = 0;
233		List<Post> posts = Post.ApplicablePosts.FindAll(delegate(Post p) { return p.IsVisible; });
234		foreach (Post post in posts)
235		{
236			comments += post.ApprovedComments.Count;
237			raters += post.Raters;
238		}
239
240		ltPosts.Text = posts.Count + " " + Resources.labels.posts.ToLowerInvariant();
241		if (BlogSettings.Instance.IsCommentsEnabled && BlogSettings.Instance.ModerationType != BlogSettings.Moderation.Disqus)
242			ltComments.Text = "<span>" + comments + " " + Resources.labels.comments.ToLowerInvariant() + "</span><br />";
243
244		if (BlogSettings.Instance.EnableRating)
245			ltRaters.Text = raters + " " + Resources.labels.raters.ToLowerInvariant();
246	}
247}