PageRenderTime 26ms CodeModel.GetById 7ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/DotNetSlave.BusinessLogic/Json/JsonPosts.cs

#
C# | 233 lines | 159 code | 35 blank | 39 comment | 34 complexity | b7963725a88a3116ab46c9f5e7bbf8d6 MD5 | raw file
  1namespace BlogEngine.Core.Json
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.Linq;
  6    using System.Text;
  7
  8    /// <summary>
  9    /// Post type
 10    /// </summary>
 11    public enum PostType
 12    {
 13        /// <summary>
 14        /// All posts
 15        /// </summary>
 16        All,
 17        /// <summary>
 18        /// Drafts
 19        /// </summary>
 20        Draft,
 21        /// <summary>
 22        /// Published posts
 23        /// </summary>
 24        Published
 25    }
 26
 27    /// <summary>
 28    /// List of posts
 29    /// </summary>
 30    public static class JsonPosts
 31    {
 32        /// <summary>
 33        /// The current page.
 34        /// </summary>
 35        private static int currentPage = 1;
 36        
 37        /// <summary>
 38        /// The comm cnt.
 39        /// </summary>
 40        private static int postCnt;
 41
 42        /// <summary>
 43        /// Gets post list based on selection for current page
 44        /// </summary>
 45        /// <param name="page">Current page</param>
 46        /// <param name="pageSize">Page Size</param>
 47        /// <param name="postType">Selected post type: draft, published or all</param>
 48        /// <param name="filter">Secondary filter: category, tag, author or all</param>
 49        /// <param name="title">Value selected in secondary filter</param>
 50        /// <returns>List of posts</returns>
 51        public static List<JsonPost> GetPosts(int page, int pageSize, string postType, string filter, string title)
 52        {
 53            pageSize = Math.Max(pageSize, 1);
 54            var cntTo = page * pageSize;
 55            var cntFrom = cntTo - pageSize;
 56            var cnt = 0;
 57
 58            var allPosts = new List<Post>();
 59            var filteredPosts = new List<Post>();
 60            var pagePosts = new List<JsonPost>();
 61
 62            // first filter on selected post type
 63            switch (postType)
 64            {
 65                case "Published":
 66                    allPosts = (from p in Post.Posts.ToList() where p.IsPublished == true select p).ToList();
 67                    break;
 68                case "Draft":
 69                    allPosts = (from p in Post.Posts where p.IsPublished == false select p).ToList();
 70                    break;
 71                default:
 72                    allPosts = (from p in Post.Posts select p).ToList();
 73                    break;
 74            }
 75
 76            // now filter first results on secondary filter
 77            switch (filter)
 78            {
 79                case "Category":
 80                    filteredPosts = (from x in allPosts where x.Categories.Contains(Category.GetCategory(new Guid(title))) orderby x.DateCreated descending select x).ToList();
 81                    break;
 82                case "Tag":
 83                    filteredPosts = (from x in allPosts where x.Tags.Contains(title) orderby x.DateCreated descending select x).ToList();
 84                    break;
 85                case "Author":
 86                    filteredPosts = (from x in allPosts where x.Author.Equals(title) orderby x.DateCreated descending select x).ToList();
 87                    break;
 88                default:
 89                    filteredPosts = (from x in allPosts orderby x.DateCreated descending select x).ToList();
 90                    break;
 91            }
 92
 93            // convert each post into smaller Json friendly object 
 94            foreach (var x in filteredPosts)
 95            {
 96                cnt++;
 97                if (cnt <= cntFrom || cnt > cntTo)
 98                {
 99                    continue;
100                }
101
102                string tags = x.Tags.Aggregate("", (current, tag) => current + (tag + ","));
103
104                var jp = new JsonPost
105                {
106                    Id = x.Id,
107                    Author = GetAuthor(x.Author),
108                    Title = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, System.Web.HttpContext.Current.Server.HtmlEncode(x.Title)),
109                    Date = x.DateCreated.ToString("dd MMM yyyy"),
110                    Time = x.DateCreated.ToString("t"),
111                    Categories = GetCategories(x.Categories),
112                    Tags = GetTags(x.Tags),
113                    Comments = GetComments(x.Comments, x.RelativeLink),
114                    IsPublished = x.IsPublished,
115                    CanUserEdit = x.CanUserEdit,
116                    CanUserDelete = x.CanUserDelete
117                };
118                pagePosts.Add(jp);
119            }
120
121            currentPage = page;
122            postCnt = cnt;
123
124            return pagePosts;
125        }
126
127        /// <summary>
128        /// Builds pager control for posts page
129        /// </summary>
130        /// <param name="page">Current Page Number</param>
131        /// <param name="pageSize">Page Size</param>
132        /// <returns></returns>
133        public static string GetPager(int page, int pageSize)
134        {
135            if (postCnt == 0)
136            {
137                return string.Empty;
138            }
139            if (page < 1) page = 1;
140
141            var prvLnk = string.Empty;
142            var nxtLnk = string.Empty;
143            var firstLnk = string.Empty;
144            var lastLnk = string.Empty;
145            const string linkFormat = "<a href=\"#\" id=\"{0}\" onclick=\"return LoadPostsForPage('{1}');\" class=\"{0}\"></a>";
146
147            pageSize = Math.Max(pageSize, 1);
148            var pgs = Convert.ToDecimal(postCnt) / Convert.ToDecimal(pageSize);
149            var p = pgs - (int)pgs;
150            var lastPage = p > 0 ? (int)pgs + 1 : (int)pgs;
151
152            var postTo = page * pageSize;
153            if (postTo > postCnt) postTo = postCnt;
154
155            var currentScope = ((page * pageSize) - (pageSize - 1)).ToString() + " - " + postTo.ToString();
156
157            var pageLink = string.Format("Showing <span id=\"PagerCurrentPage\">{0}</span> of {1}", currentScope, postCnt);
158
159            if (currentPage > 1)
160            {
161                prvLnk = string.Format(linkFormat, "prevLink", page - 1);
162                firstLnk = string.Format(linkFormat, "firstLink", 1);
163            }
164
165            if (page < lastPage)
166            {
167                nxtLnk = string.Format(linkFormat, "nextLink", page + 1);
168                lastLnk = string.Format(linkFormat, "lastLink", lastPage);
169            }
170
171            return "<div id=\"ListPager\">" + firstLnk + prvLnk + pageLink + nxtLnk + lastLnk + "</div>";
172        }
173
174        #region Private Methods
175
176        static string GetCategories(ICollection<Category> categories)
177        {
178            if (categories == null || categories.Count == 0)
179                return string.Empty;
180
181            var html = categories.Aggregate("", (current, cat) => current + string.Format("<a href='#' onclick=\"ChangePostFilter('Category','{0}','{1}')\">{1}</a>, ", cat.Id, cat.Title));
182            return html.Trim().Substring(0, html.Trim().Length - 1);
183        }
184
185        static string GetTags(ICollection<string> tags)
186        {
187            if (tags == null || tags.Count == 0)
188                return string.Empty;
189
190            var html = tags.Aggregate("", (current, tag) => current + string.Format("<a href='#' onclick=\"ChangePostFilter('Tag','{0}','')\">{0}</a>, ", tag));
191            return html.Trim().Substring(0, html.Trim().Length - 1);
192        }
193
194        static string GetAuthor(string  author)
195        {
196            if (string.IsNullOrEmpty(author)) 
197                return string.Empty;
198            return string.Format("<a href='#' onclick=\"ChangePostFilter('Author','{0}','')\">{0}</a>", author);
199        }
200
201        static string GetComments(ICollection<Comment> comments, string postUrl)
202        {
203            int pending, approved;
204            int spam = comments.Count(c => c.IsSpam == true && c.IsDeleted == false);
205
206            string pLink = "<a href=\"{0}\" class=\"comCountPending tipsyhelp\" original-title=\"Pending comments\">({1})</a>";
207            string aLink = "<a href=\"{0}\" class=\"comCountApproved tipsyhelp\" original-title=\"Approved comments, visible to public\">{1}</a>";
208            string sLink = "<a href=\"{0}\" class=\"comCountSpam tipsyhelp\" original-title=\"Spam comments\">({1})</a>";
209
210            sLink = spam > 0 ? string.Format(sLink, postUrl + "#comments", spam) : "";
211
212            if (BlogSettings.Instance.EnableCommentsModeration)
213            {
214                pending = comments.Count(c => (c.IsApproved == false && c.IsSpam == false && c.IsDeleted == false));
215                approved = comments.Count(c => c.IsApproved == true && c.IsDeleted == false);
216
217                pLink = pending > 0 ? string.Format(pLink, postUrl + "#comment", pending) : "";
218                aLink = approved > 0 ? string.Format(aLink, postUrl + "#comment", approved) : "<span class=\"comNone tipsyhelp\" original-title=\"Approved comments, visible to public\">0</span>";
219
220                return aLink + pLink + sLink;
221            }
222            else
223            {
224                approved = comments.Count(c => c.IsSpam == false);
225                aLink = approved > 0 ? string.Format(aLink, postUrl + "#comment", approved) : "<span class=\"comNone tipsyhelp\" original-title=\"Approved comments, visible to public\">0</span>";
226                return aLink + sLink;
227            }
228        }
229        
230        #endregion
231
232    }
233}