PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/BlogEngine.NET/widgets/Visitor info/widget.ascx.cs

#
C# | 167 lines | 103 code | 30 blank | 34 comment | 16 complexity | 7003005cb02168d208362f5e838b720c MD5 | raw file
  1// --------------------------------------------------------------------------------------------------------------------
  2// <summary>
  3//   The widget.
  4// </summary>
  5// --------------------------------------------------------------------------------------------------------------------
  6
  7namespace Widgets.VisitorInfo
  8{
  9    using System;
 10    using System.Collections.Generic;
 11
 12    using App_Code.Controls;
 13
 14    using BlogEngine.Core;
 15
 16    /// <summary>
 17    /// The widget.
 18    /// </summary>
 19    public partial class Widget : WidgetBase
 20    {
 21        #region Constants and Fields
 22
 23        /// <summary>
 24        /// The number of comments.
 25        /// </summary>
 26        private int numberOfComments;
 27
 28        #endregion
 29
 30        #region Properties
 31
 32        /// <summary>
 33        /// Gets a value indicating whether IsEditable.
 34        /// </summary>
 35        public override bool IsEditable
 36        {
 37            get
 38            {
 39                return false;
 40            }
 41        }
 42
 43        /// <summary>
 44        /// Gets Name.
 45        /// </summary>
 46        public override string Name
 47        {
 48            get
 49            {
 50                return "Visitor info";
 51            }
 52        }
 53
 54        #endregion
 55
 56        #region Public Methods
 57
 58        /// <summary>
 59        /// This method works as a substitute for Page_Load. You should use this method for
 60        /// data binding etc. instead of Page_Load.
 61        /// </summary>
 62        public override void LoadWidget()
 63        {
 64            Visible = false;
 65            var cookie = Request.Cookies["comment"];
 66
 67            if (cookie == null)
 68            {
 69                return;
 70            }
 71
 72            var name = cookie.Values["name"];
 73            var email = cookie.Values["email"];
 74            var website = cookie.Values["url"];
 75
 76            if (name == null)
 77            {
 78                return;
 79            }
 80
 81            name = name.Replace("+", " ");
 82            WriteHtml(name, email, website);
 83
 84            Uri url;
 85            if (Request.QueryString["apml"] == null && Uri.TryCreate(website, UriKind.Absolute, out url))
 86            {
 87                phScript.Visible = true;
 88
 89                // ltWebsite.Text = url.ToString();
 90            }
 91
 92            Visible = true;
 93        }
 94
 95        #endregion
 96
 97        #region Methods
 98
 99        /// <summary>
100        /// Gets the commented posts.
101        /// </summary>
102        /// <param name="email">The email.</param>
103        /// <param name="website">The website.</param>
104        /// <returns>A list of Post</returns>
105        private List<Post> GetCommentedPosts(string email, string website)
106        {
107            var list = new List<Post>();
108            foreach (var post in Post.Posts)
109            {
110                var comments = post.Comments.FindAll(
111                    c => email.Equals(c.Email, StringComparison.OrdinalIgnoreCase) ||
112                         (c.Website != null &&
113                          c.Website.ToString().Equals(website, StringComparison.OrdinalIgnoreCase)));
114
115                if (comments.Count <= 0)
116                {
117                    continue;
118                }
119                
120                numberOfComments += comments.Count;
121                var index = post.Comments.IndexOf(comments[comments.Count - 1]);
122                if (index < post.Comments.Count - 1 &&
123                    post.Comments[post.Comments.Count - 1].DateCreated > DateTime.Now.AddDays(-7))
124                {
125                    list.Add(post);
126                }
127            }
128
129            return list;
130        }
131
132        /// <summary>
133        /// Writes the HTML.
134        /// </summary>
135        /// <param name="name">The name to write.</param>
136        /// <param name="email">The email.</param>
137        /// <param name="website">The website.</param>
138        private void WriteHtml(string name, string email, string website)
139        {
140            if (name.Contains(" "))
141            {
142                name = name.Substring(0, name.IndexOf(" "));
143            }
144
145            var avatar = Avatar.GetAvatar(16, email, null, null, name);
146            var avatarLink = avatar == null || avatar.Url == null ? string.Empty : avatar.Url.ToString();
147            Title = string.Format(
148                String.Concat("<img src=\"{0}\" alt=\"{1}\" align=\"top\" /> ", Resources.labels.visitorHi, " {1}"), avatarLink, Server.HtmlEncode(name));
149            pName.InnerHtml = "<strong>" + Resources.labels.welcomeBack + "</strong>";
150            var list = GetCommentedPosts(email, website);
151
152            if (list.Count > 0)
153            {
154                var link = string.Format(
155                    "<a href=\"{0}\">{1}</a>", list[0].RelativeLink, Server.HtmlEncode(list[0].Title));
156                pComment.InnerHtml = string.Format(Resources.labels.commentsAddedSince, link);
157            }
158
159            if (numberOfComments > 0)
160            {
161                pComment.InnerHtml += string.Format(Resources.labels.writtenCommentsTotal, numberOfComments);
162            }
163        }
164
165        #endregion
166    }
167}