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

/BlogEngine/DotNetSlave.BusinessLogic/Web/Controls/CommentViewBase.cs

#
C# | 259 lines | 143 code | 28 blank | 88 comment | 7 complexity | 60fc0cf725c71e78e8c48dfca8302c04 MD5 | raw file
  1namespace BlogEngine.Core.Web.Controls
  2{
  3    using System;
  4    using System.Globalization;
  5    using System.Linq;
  6    using System.Text;
  7    using System.Web.Security;
  8    using System.Web.UI;
  9
 10    /// <summary>
 11    /// Inherit from this class when you are building the
 12    ///     commentview.ascx user control in your custom theme.
 13    /// </summary>
 14    /// <remarks>
 15    /// The class exposes a lot of functionality to the custom
 16    ///     comment control in the theme folder.
 17    /// </remarks>
 18    public class CommentViewBase : UserControl
 19    {
 20        #region Constants and Fields
 21
 22        /// <summary>
 23        /// The flag image.
 24        /// </summary>
 25        private const string FlagImage =
 26            "<span class=\"adr\"><img src=\"{0}pics/flags/{1}.png\" class=\"country-name flag\" title=\"{2}\" alt=\"{2}\" /></span>";
 27
 28/*
 29        /// <summary>
 30        /// The link.
 31        /// </summary>
 32        private const string Link = "<a href=\"{0}{1}\" rel=\"nofollow\">{2}</a>";
 33*/
 34
 35/*
 36        /// <summary>
 37        /// The link regex.
 38        /// </summary>
 39        private static readonly Regex LinkRegex =
 40            new Regex(
 41                "((http://|www\\.)([A-Z0-9.-]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})", 
 42                RegexOptions.Compiled | RegexOptions.IgnoreCase);
 43*/
 44
 45        #endregion
 46
 47        #region Properties
 48
 49        /// <summary>
 50        ///     Gets or sets the Comment.
 51        /// </summary>
 52        /// <value>The comment.</value>
 53        public Comment Comment { get; set; }
 54
 55        /// <summary>
 56        ///     Gets or sets the Post from which the comment belongs.
 57        /// </summary>
 58        /// <value>The Post object.</value>
 59        public Post Post { get; set; }
 60
 61        /// <summary>
 62        ///     Gets the text of the comment.
 63        /// </summary>
 64        /// <value>The comment text.</value>
 65        public string Text
 66        {
 67            get
 68            {
 69                var arg = new ServingEventArgs(this.Comment.Content, ServingLocation.SinglePost);
 70                Comment.OnServing(this.Comment, arg);
 71                if (arg.Cancel)
 72                {
 73                    this.Visible = false;
 74                }
 75
 76                var body = arg.Body.Replace("\n", "<br />");
 77                body = body.Replace("\t", "&nbsp;&nbsp;");
 78                body = body.Replace("  ", "&nbsp;&nbsp;");
 79                return body;
 80            }
 81        }
 82
 83        /// <summary>
 84        ///     Gets a delete link to visitors that are authenticated
 85        ///     using the default membership provider.
 86        /// </summary>
 87        public string AdminLinks
 88        {
 89            get
 90            {
 91                if (Security.IsAuthorizedTo(Rights.ModerateComments))
 92                {
 93                    var sb = new StringBuilder();
 94                    sb.AppendFormat(" | <a class=\"email\" href=\"mailto:{0}\">{0}</a>", this.Comment.Email);
 95                    sb.AppendFormat(
 96                        " | <a href=\"http://www.domaintools.com/go/?service=whois&amp;q={0}/\">{0}</a>", 
 97                        this.Comment.IP);
 98
 99                    if (this.Comment.Comments.Count > 0)
100                    {
101                        var confirmDelete = string.Format(
102                            CultureInfo.InvariantCulture, 
103                            Utils.Translate("areYouSure"), 
104                            Utils.Translate("delete").ToLowerInvariant(), 
105                            Utils.Translate("theComment"));
106                        sb.AppendFormat(
107                            " | <a href=\"javascript:void(0);\" onclick=\"if (confirm('{1}?')) location.href='?deletecomment={0}'\">{2}</a>", 
108                            this.Comment.Id, 
109                            confirmDelete, 
110                            Utils.Translate("deleteKeepReplies"));
111
112                        var confirmRepliesDelete = string.Format(
113                            CultureInfo.InvariantCulture, 
114                            Utils.Translate("areYouSure"), 
115                            Utils.Translate("delete").ToLowerInvariant(), 
116                            Utils.Translate("theComment"));
117                        sb.AppendFormat(
118                            " | <a href=\"javascript:void(0);\" onclick=\"if (confirm('{1}?')) location.href='?deletecommentandchildren={0}'\">{2}</a>", 
119                            this.Comment.Id, 
120                            confirmRepliesDelete, 
121                            Utils.Translate("deletePlusReplies"));
122                    }
123                    else
124                    {
125                        var confirmDelete = string.Format(
126                            CultureInfo.InvariantCulture, 
127                            Utils.Translate("areYouSure"), 
128                            Utils.Translate("delete").ToLowerInvariant(), 
129                            Utils.Translate("theComment"));
130                        sb.AppendFormat(
131                            " | <a href=\"javascript:void(0);\" onclick=\"if (confirm('{1}?')) location.href='?deletecomment={0}'\">{2}</a>", 
132                            this.Comment.Id, 
133                            confirmDelete, 
134                            Utils.Translate("delete"));
135                    }
136
137                    if (!this.Comment.IsApproved)
138                    {
139                        sb.AppendFormat(
140                            " | <a href=\"?approvecomment={0}\">{1}</a>", this.Comment.Id, Utils.Translate("approve"));
141                    }
142
143                    return sb.ToString();
144                }
145
146                return string.Empty;
147            }
148        }
149
150        /// <summary>
151        ///     Gets the flag of the country from which the comment was written.
152        ///     <remarks>
153        ///         If the country hasn't been resolved from the authors IP address or
154        ///         the flag does not exist for that country, nothing is displayed.
155        ///     </remarks>
156        /// </summary>
157        public string Flag
158        {
159            get
160            {
161                if (!string.IsNullOrEmpty(this.Comment.Country))
162                {
163                    // return "<img src=\"" + Utils.RelativeWebRoot + "pics/flags/" + Comment.Country + ".png\" class=\"country-name flag\" title=\"" + Comment.Country + "\" alt=\"" + Comment.Country + "\" />";
164                    return string.Format(
165                        FlagImage, Utils.RelativeWebRoot, this.Comment.Country, FindCountry(this.Comment.Country));
166                }
167
168                return null;
169            }
170        }
171
172        /// <summary>
173        ///     Gets a link that lets a user reply to a specific comment
174        /// </summary>
175        public string ReplyToLink
176        {
177            get
178            {
179                return (((BlogSettings.Instance.IsCommentsEnabled && BlogSettings.Instance.IsCommentNestingEnabled) &&
180                         this.Post.HasCommentsEnabled) && this.Comment.IsApproved) &&
181                       (BlogSettings.Instance.DaysCommentsAreEnabled <= 0 ||
182                        this.Post.DateCreated.AddDays(BlogSettings.Instance.DaysCommentsAreEnabled) >= DateTime.Now.Date)
183                           ? string.Format(
184                               "<a href=\"javascript:void(0);\" class=\"reply-to-comment\" onclick=\"BlogEngine.replyToComment('{0}');\">{1}</a>", 
185                               this.Comment.Id, 
186                               Utils.Translate("replyToThis"))
187                           : string.Empty;
188            }
189        }
190
191        #endregion
192
193        #region Methods
194
195        /// <summary>
196        /// The comment will be processed when invoked, rather than waiting
197        /// for the Load event to occur.
198        /// </summary>
199        public virtual void RenderComment() { }
200
201        /// <summary>
202        /// For styling, indicates if the comment is odd or even.
203        /// </summary>
204        public bool IsOdd { get; set; }
205
206        /// <summary>
207        /// Displays the Gravatar image that matches the specified email.
208        /// </summary>
209        /// <param name="size">
210        /// The image size.
211        /// </param>
212        /// <returns>
213        /// The gravatar.
214        /// </returns>
215        public string Gravatar(int size)
216        {
217            return Avatar.GetAvatarImageTag(size, this.Comment.Email, this.Comment.Website, this.Comment.Avatar, this.Comment.Author);
218        }
219
220        /// <summary>
221        /// Examines the comment body for any links and turns them
222        ///     automatically into one that can be clicked.
223        /// </summary>
224        /// <param name="body">
225        /// The body string.
226        /// </param>
227        /// <returns>
228        /// The resolve links.
229        /// </returns>
230        [Obsolete("Use the Text property instead. This method will be removed in a future version.")]
231        public string ResolveLinks(string body)
232        {
233            return this.Text;
234        }
235
236        /// <summary>
237        /// Finds country.
238        /// </summary>
239        /// <param name="isoCode">
240        /// The iso code.
241        /// </param>
242        /// <returns>
243        /// The find country.
244        /// </returns>
245        private static string FindCountry(string isoCode)
246        {
247            foreach (var ri in
248                CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(ci => new RegionInfo(ci.Name)).Where(
249                    ri => ri.TwoLetterISORegionName.Equals(isoCode, StringComparison.OrdinalIgnoreCase)))
250            {
251                return ri.DisplayName;
252            }
253
254            return isoCode;
255        }
256
257        #endregion
258    }
259}