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