PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Json/JsonComments.cs

#
C# | 245 lines | 125 code | 33 blank | 87 comment | 19 complexity | b6fd63938c971d02559aa00004960a0f MD5 | raw file
  1namespace BlogEngine.Core.Json
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.Linq;
  6
  7    /// <summary>
  8    /// Comment type.
  9    /// </summary>
 10    public enum CommentType
 11    {
 12        /// <summary>
 13        ///     Pending Comment Type
 14        /// </summary>
 15        Pending,
 16
 17        /// <summary>
 18        ///     Approved Comment Type
 19        /// </summary>
 20        Approved,
 21
 22        /// <summary>
 23        ///     Pingbacks and trackbacks Comment Type
 24        /// </summary>
 25        Pingback,
 26
 27        /// <summary>
 28        ///     Spam Comment Type
 29        /// </summary>
 30        Spam
 31    }
 32
 33    /// <summary>
 34    /// List of comments
 35    /// </summary>
 36    public static class JsonComments
 37    {
 38        /// <summary>
 39        /// The current page.
 40        /// </summary>
 41        private static int currentPage = 1;    
 42
 43        /// <summary>
 44        /// The comm cnt.
 45        /// </summary>
 46        private static int commCnt;
 47
 48        /// <summary>
 49        /// List of comments based on type for a single page.
 50        /// </summary>
 51        /// <param name="commentType">
 52        /// The comment type.
 53        /// </param>
 54        /// <param name="page">
 55        /// The current page.
 56        /// </param>
 57        /// <returns>
 58        /// A list of JSON comments.
 59        /// </returns>
 60        public static List<JsonComment> GetComments(CommentType commentType, int page)
 61        {
 62            return GetComments(commentType, BlogSettings.Instance.CommentsPerPage, page);
 63        }
 64
 65        /// <summary>
 66        /// List of comments based on type for a single page.
 67        /// </summary>
 68        /// <param name="commentType">
 69        /// The comment type.
 70        /// </param>
 71        /// <param name="pageSize">
 72        /// The number of comments per page.
 73        /// </param>
 74        /// <param name="page">
 75        /// The current page.
 76        /// </param>
 77        /// <returns>
 78        /// A list of JSON comments.
 79        /// </returns>
 80        public static List<JsonComment> GetComments(CommentType commentType, int pageSize, int page)
 81        {
 82            var cntTo = page * pageSize;
 83            var cntFrom = cntTo - pageSize;
 84            var cnt = 0;
 85
 86            var allComments = new List<Comment>();
 87            var pageComments = new List<JsonComment>();
 88
 89            foreach (var p in Post.Posts)
 90            {
 91                switch (commentType)
 92                {
 93                    case CommentType.Pending:
 94                        allComments.AddRange(p.NotApprovedComments);
 95                        break;
 96                    case CommentType.Pingback:
 97                        allComments.AddRange(p.Pingbacks);
 98                        break;
 99                    case CommentType.Spam:
100                        allComments.AddRange(p.SpamComments);
101                        break;
102                    default:
103                        allComments.AddRange(p.ApprovedComments);
104                        break;
105                }
106            }
107
108            allComments.Sort((x, y) => DateTime.Compare(y.DateCreated, x.DateCreated));
109
110            // TODO: find a way to better handle deleting last comment on the last page
111            // below is not working properly; after moving to the next page and deleting
112            // another comment there, things get nasty
113            if (allComments.Count > 0 && allComments.Count == cntFrom && page > 1)
114            {
115                // removed last comment(s) on the page
116                // need to shift one page back
117
118                //var context = System.Web.HttpContext.Current;
119                //if (context.Request.Cookies["CommentPagerCurrentPage"] != null)
120                //    context.Request.Cookies["CommentPagerCurrentPage"].Value = (page - 1).ToString();
121
122                //return GetComments(commentType, page - 1);
123            }
124
125            foreach (var c in allComments)
126            {
127                cnt++;
128                if (cnt <= cntFrom || cnt > cntTo)
129                {
130                    continue;
131                }
132
133                pageComments.Add(CreateJsonCommentFromComment(c));
134            }
135
136            currentPage = page;
137            commCnt = cnt;
138
139            return pageComments;
140        }
141
142        /// <summary>
143        /// Single commnet by ID
144        /// </summary>
145        /// <param name="id">
146        /// Comment id
147        /// </param>
148        /// <returns>
149        /// A JSON Comment
150        /// </returns>
151        public static JsonComment GetComment(Guid id)
152        {
153            return (from p in Post.Posts
154                    from c in p.AllComments
155                    where c.Id == id
156                    select CreateJsonCommentFromComment(c)).FirstOrDefault();
157        }
158
159        private static JsonComment CreateJsonCommentFromComment(Comment c)
160        {
161            var jc = new JsonComment
162                {
163                    Id = c.Id,
164                    Email = c.Email,
165                    Author = c.Author,
166                    Title = c.Title,
167                    Teaser = c.Teaser,
168                    Website = c.Website == null ? "" : c.Website.ToString(),
169                    AuthorAvatar = c.Avatar,
170                    Content = c.Content,
171                    Ip = c.IP,
172                    Date = c.DateCreated.ToString("dd MMM yyyy"),
173                    Time = c.DateCreated.ToString("t")
174                };
175            return jc;
176        }
177
178        /// <summary>
179        /// Builds pager control for comments page
180        /// </summary>
181        /// <param name="page">
182        /// Current page
183        /// </param>
184        /// <param name="srvs">
185        /// The Srvs..
186        /// </param>
187        /// <returns>
188        /// HTML with next and previous buttons
189        /// </returns>
190        public static string GetPager(int page)
191        {
192            if (commCnt == 0)
193            {
194                return string.Empty;
195            }
196
197            var prvLnk = string.Empty;
198            var nxtLnk = string.Empty;
199            var firstLnk = string.Empty;
200            var lastLnk = string.Empty;
201
202            const string linkFormat = "<a href=\"#\" id=\"{0}\" onclick=\"return LoadComments({1});\" class=\"{0}\"></a>";
203
204            var PageSize = BlogSettings.Instance.CommentsPerPage;
205            var pgs = Convert.ToDecimal(commCnt) / Convert.ToDecimal(PageSize);
206            var p = pgs - (int)pgs;
207            var lastPage = p > 0 ? (int)pgs + 1 : (int)pgs;
208
209            var cntFrom = ((page * PageSize) - (PageSize - 1));
210            var cntTo = (page * PageSize);
211            
212            // adjust for the last (or single) page
213            if (commCnt < cntTo) cntTo = commCnt;
214
215            // when last comment on the last page deleted
216            // this will reset "from" counter
217            if (cntFrom > cntTo) cntFrom = cntFrom - PageSize;
218
219            if (commCnt > 0 && commCnt == cntFrom && page > 1)
220            {
221                // removed last comment(s) on the page
222                // need to shift one page back
223                //return GetPager(page - 1);
224            }
225
226            var currentScope = cntFrom + " - " + cntTo;
227
228            var pageLink = string.Format("<span>Showing {0} of {1}</span>", currentScope, commCnt);
229
230            if (currentPage > 1)
231            {
232                prvLnk = string.Format(linkFormat, "prevLink", currentPage - 1);
233                firstLnk = string.Format(linkFormat, "firstLink", 1);
234            }
235
236            if (page < lastPage)
237            {
238                nxtLnk = string.Format(linkFormat, "nextLink", currentPage + 1);
239                lastLnk = string.Format(linkFormat, "lastLink", lastPage);
240            }
241
242            return firstLnk + prvLnk + pageLink + nxtLnk + lastLnk;
243        }
244    }
245}