1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using BlogEngine . Core ;
5+
6+ namespace BlogEngine . NET . Custom . Widgets
7+ {
8+ public class MonthList
9+ {
10+ class DateComparer_Desc : IComparer < DateTime >
11+ {
12+ int IComparer < DateTime > . Compare ( DateTime x , DateTime y ) { return - x . CompareTo ( y ) ; }
13+ }
14+ class IntComparer_Desc : IComparer < int >
15+ {
16+ int IComparer < int > . Compare ( int x , int y ) { return - x . CompareTo ( y ) ; }
17+ }
18+
19+ private static readonly IComparer < DateTime > date_Desc = new DateComparer_Desc ( ) ;
20+ private static readonly IComparer < int > int_Desc = new IntComparer_Desc ( ) ;
21+
22+ public MonthList ( ) { }
23+
24+ public SortedDictionary < int , List < MonthItem > > GetList ( )
25+ {
26+ var months = new SortedDictionary < DateTime , int > ( date_Desc ) ;
27+ var years = new SortedDictionary < int , List < MonthItem > > ( int_Desc ) ;
28+
29+ foreach ( var month in Post . ApplicablePosts . Where ( post => post . IsVisibleToPublic ) .
30+ Select ( post => new DateTime ( post . DateCreated . Year , post . DateCreated . Month , 1 ) ) )
31+ {
32+ int count ;
33+ months . TryGetValue ( month , out count ) ;
34+ ++ count ;
35+ months [ month ] = count ;
36+ }
37+ foreach ( KeyValuePair < DateTime , int > aIt in months )
38+ {
39+ if ( ! years . Keys . Contains ( aIt . Key . Year ) )
40+ {
41+ years . Add ( aIt . Key . Year , new List < MonthItem > ( ) ) ;
42+ }
43+ var item = new MonthItem ( ) ;
44+ item . Title = aIt . Key . ToString ( "MMMM" ) ;
45+ item . Url = string . Format ( "{0}{1}/{2}" , Utils . RelativeOrAbsoluteWebRoot , aIt . Key . Year , aIt . Key . Month . ToString ( "00" ) ) ;
46+ item . Count = aIt . Value ;
47+ years [ aIt . Key . Year ] . Add ( item ) ;
48+ }
49+ return years ;
50+ }
51+ }
52+
53+ public class MonthItem
54+ {
55+ public string Title { get ; set ; }
56+ public string Url { get ; set ; }
57+ public int Count { get ; set ; }
58+ }
59+ }
0 commit comments