PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/widgets/RecentPosts/widget.ascx.cs

#
C# | 200 lines | 120 code | 41 blank | 39 comment | 17 complexity | 542f682b71eceddbb81679e764b746db MD5 | raw file
  1// --------------------------------------------------------------------------------------------------------------------
  2// <summary>
  3//   The widget.
  4// </summary>
  5// --------------------------------------------------------------------------------------------------------------------
  6
  7namespace Widgets.RecentPosts
  8{
  9    using System;
 10    using System.Collections.Generic;
 11    using System.Collections.Specialized;
 12    using System.Globalization;
 13    using System.Text;
 14    using System.Web;
 15    using System.Web.UI;
 16
 17    using App_Code.Controls;
 18
 19    using BlogEngine.Core;
 20
 21    using Resources;
 22
 23    /// <summary>
 24    /// The widget.
 25    /// </summary>
 26    public partial class Widget : WidgetBase
 27    {
 28        #region Constants and Fields
 29
 30        /// <summary>
 31        /// The default number of posts.
 32        /// </summary>
 33        private const int DefaultNumberOfPosts = 10;
 34
 35        /// <summary>
 36        /// The default show comments.
 37        /// </summary>
 38        private const bool DefaultShowComments = true;
 39
 40        /// <summary>
 41        /// The default show rating.
 42        /// </summary>
 43        private const bool DefaultShowRating = true;
 44
 45        #endregion
 46
 47        #region Constructors and Destructors
 48
 49        /// <summary>
 50        /// Initializes static members of the <see cref="Widget"/> class.
 51        /// </summary>
 52        static Widget()
 53        {
 54            Post.Saved += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
 55            Post.CommentAdded += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
 56            Post.CommentRemoved += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
 57            Post.Rated += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
 58            BlogSettings.Changed += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
 59        }
 60
 61        #endregion
 62
 63        #region Properties
 64
 65        /// <summary>
 66        /// Gets a value indicating whether IsEditable.
 67        /// </summary>
 68        public override bool IsEditable
 69        {
 70            get
 71            {
 72                return true;
 73            }
 74        }
 75
 76        /// <summary>
 77        /// Gets Name.
 78        /// </summary>
 79        public override string Name
 80        {
 81            get
 82            {
 83                return "RecentPosts";
 84            }
 85        }
 86
 87        #endregion
 88
 89        #region Public Methods
 90
 91        /// <summary>
 92        /// This method works as a substitute for Page_Load. You should use this method for
 93        /// data binding etc. instead of Page_Load.
 94        /// </summary>
 95        public override void LoadWidget()
 96        {
 97            var settings = this.GetSettings();
 98            var numberOfPosts = DefaultNumberOfPosts;
 99            if (settings.ContainsKey("numberofposts"))
100            {
101                numberOfPosts = int.Parse(settings["numberofposts"]);
102            }
103
104            if (Blog.CurrentInstance.Cache["widget_recentposts"] == null)
105            {
106                var visiblePosts = Post.Posts.FindAll(p => p.IsVisibleToPublic);
107
108                var max = Math.Min(visiblePosts.Count, numberOfPosts);
109                var list = visiblePosts.GetRange(0, max);
110                Blog.CurrentInstance.Cache.Insert("widget_recentposts", list);
111            }
112
113            var content = RenderPosts((List<Post>)Blog.CurrentInstance.Cache["widget_recentposts"], settings);
114
115            var html = new LiteralControl(content);
116                
117                // new LiteralControl((string)Blog.CurrentInstance.Cache["widget_recentposts"]);
118            this.phPosts.Controls.Add(html);
119        }
120
121        #endregion
122
123        #region Methods
124
125        /// <summary>
126        /// Renders the posts.
127        /// </summary>
128        /// <param name="posts">The posts.</param>
129        /// <param name="settings">The settings.</param>
130        /// <returns>The rendered html.</returns>
131        private static string RenderPosts(List<Post> posts, StringDictionary settings)
132        {
133            if (posts.Count == 0)
134            {
135                // Blog.CurrentInstance.Cache.Insert("widget_recentposts", "<p>" + Resources.labels.none + "</p>");
136                return string.Format("<p>{0}</p>", labels.none);
137            }
138
139            var sb = new StringBuilder();
140            sb.Append("<ul class=\"recentPosts\" id=\"recentPosts\">");
141
142            var showComments = DefaultShowComments;
143            var showRating = DefaultShowRating;
144            if (settings.ContainsKey("showcomments"))
145            {
146                bool.TryParse(settings["showcomments"], out showComments);
147            }
148
149            if (settings.ContainsKey("showrating"))
150            {
151                bool.TryParse(settings["showrating"], out showRating);
152            }
153
154            foreach (var post in posts)
155            {
156                if (!post.IsVisibleToPublic)
157                {
158                    continue;
159                }
160
161                var rating = Math.Round(post.Rating, 1).ToString(CultureInfo.InvariantCulture);
162
163                const string LinkFormat = "<li><a href=\"{0}\">{1}</a>{2}{3}</li>";
164
165                var comments = string.Format("<span>{0}: {1}</span>", labels.comments, post.ApprovedComments.Count);
166
167                if (BlogSettings.Instance.ModerationType == BlogSettings.Moderation.Disqus)
168                {
169                    comments = string.Format(
170                        "<span><a href=\"{0}#disqus_thread\">{1}</a></span>", post.PermaLink, labels.comments);
171                }
172
173                var rate = string.Format("<span>{0}: {1} / {2}</span>", labels.rating, rating, post.Raters);
174
175                if (!showComments || !BlogSettings.Instance.IsCommentsEnabled)
176                {
177                    comments = null;
178                }
179
180                if (!showRating || !BlogSettings.Instance.EnableRating)
181                {
182                    rate = null;
183                }
184                else if (post.Raters == 0)
185                {
186                    rate = string.Format("<span>{0}</span>", labels.notRatedYet);
187                }
188
189                sb.AppendFormat(LinkFormat, post.RelativeLink, HttpUtility.HtmlEncode(post.Title), comments, rate);
190            }
191
192            sb.Append("</ul>");
193
194            // Blog.CurrentInstance.Cache.Insert("widget_recentposts", sb.ToString());
195            return sb.ToString();
196        }
197
198        #endregion
199    }
200}