PageRenderTime 22ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Controls/AuthorList.cs

#
C# | 174 lines | 107 code | 34 blank | 33 comment | 9 complexity | c3d5898940321ff9e5d1978a0b447c81 MD5 | raw file
  1// --------------------------------------------------------------------------------------------------------------------
  2// <summary>
  3//   Builds an author list.
  4// </summary>
  5// --------------------------------------------------------------------------------------------------------------------
  6
  7namespace App_Code.Controls
  8{
  9    using System;
 10    using System.Web.Security;
 11    using System.Web.UI;
 12    using System.Web.UI.HtmlControls;
 13    using System.Collections.Generic;
 14    using BlogEngine.Core;
 15
 16    using Resources;
 17
 18    /// <summary>
 19    /// Builds an author list.
 20    /// </summary>
 21    public class AuthorList : Control
 22    {
 23        #region Constants and Fields
 24
 25        /// <summary>
 26        /// The html string.
 27        /// </summary>
 28        private static Dictionary<Guid, string> blogsHtml = new Dictionary<Guid, string>();
 29
 30        /// <summary>
 31        /// The show rss icon.
 32        /// </summary>
 33        private static Dictionary<Guid, bool> blogsShowRssIcon = new Dictionary<Guid, bool>();
 34
 35        #endregion
 36
 37        #region Constructors and Destructors
 38
 39        /// <summary>
 40        /// Initializes static members of the <see cref="AuthorList"/> class. 
 41        /// </summary>
 42        static AuthorList()
 43        {
 44            Post.Saved += (sender, args) => blogsHtml.Remove(Blog.CurrentInstance.Id);
 45        }
 46
 47        #endregion
 48
 49        #region Properties
 50
 51        /// <summary>
 52        ///     Gets or sets a value indicating whether or not to show feed icons next to the category links.
 53        /// </summary>
 54        public bool ShowRssIcon
 55        {
 56            get
 57            {
 58                Guid blogId = Blog.CurrentInstance.Id;
 59
 60                if (!blogsShowRssIcon.ContainsKey(blogId))
 61                    blogsShowRssIcon[blogId] = true;
 62
 63                return blogsShowRssIcon[blogId];
 64            }
 65
 66            set
 67            {
 68                if (ShowRssIcon == value)
 69                {
 70                    return;
 71                }
 72
 73                blogsShowRssIcon[Blog.CurrentInstance.Id] = value;
 74                blogsHtml.Remove(Blog.CurrentInstance.Id);
 75            }
 76        }
 77
 78        /// <summary>
 79        ///     Gets the rendered HTML in the private field and first
 80        ///     updates it when a post has been saved (new or updated).
 81        /// </summary>
 82        private string Html
 83        {
 84            get
 85            {
 86                Guid blogId = Blog.CurrentInstance.Id;
 87
 88                if (!blogsHtml.ContainsKey(blogId))
 89                    blogsHtml[blogId] = string.Empty;
 90
 91                return blogsHtml[blogId];
 92            }
 93        }
 94
 95        #endregion
 96
 97        #region Public Methods
 98
 99        /// <summary>
100        /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object and stores tracing information about the control if tracing is enabled.
101        /// </summary>
102        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
103        public override void RenderControl(HtmlTextWriter writer)
104        {
105            writer.Write(Html);
106            writer.Write(Environment.NewLine);
107        }
108
109        #endregion
110
111        #region Methods
112
113        /// <summary>
114        /// Loops through all users and builds the HTML
115        /// presentation.
116        /// </summary>
117        /// <returns>The authors.</returns>
118        private HtmlGenericControl BindAuthors()
119        {
120            if (Post.Posts.Count == 0)
121            {
122                var p = new HtmlGenericControl("p") { InnerHtml = labels.none };
123                return p;
124            }
125
126            var ul = new HtmlGenericControl("ul") { ID = "authorlist" };
127
128            foreach (MembershipUser user in Membership.GetAllUsers())
129            {
130                var postCount = Post.GetPostsByAuthor(user.UserName).Count;
131                if (postCount == 0)
132                {
133                    continue;
134                }
135
136                var li = new HtmlGenericControl("li");
137
138                if (ShowRssIcon)
139                {
140                    var img = new HtmlImage
141                        {
142                            Src = string.Format("{0}pics/rssButton.png", Utils.RelativeWebRoot),
143                            Alt = string.Format("RSS feed for {0}", user.UserName)
144                        };
145                    img.Attributes["class"] = "rssButton";
146
147                    var feedAnchor = new HtmlAnchor
148                        {
149                            HRef =
150                                string.Format("{0}syndication.axd?author={1}", Utils.RelativeWebRoot, Utils.RemoveIllegalCharacters(user.UserName))
151                        };
152                    feedAnchor.Attributes["rel"] = "nofollow";
153                    feedAnchor.Controls.Add(img);
154
155                    li.Controls.Add(feedAnchor);
156                }
157
158                var anc = new HtmlAnchor
159                    {
160                        HRef = string.Format("{0}author/{1}{2}", Utils.RelativeWebRoot, user.UserName, BlogConfig.FileExtension),
161                        InnerHtml = string.Format("{0} ({1})", user.UserName, postCount),
162                        Title = string.Format("Author: {0}", user.UserName)
163                    };
164
165                li.Controls.Add(anc);
166                ul.Controls.Add(li);
167            }
168
169            return ul;
170        }
171
172        #endregion
173    }
174}