|
| 1 | +using BlogEngine.Core.Data.Models; |
| 2 | +using BlogEngine.Core.Notes; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Linq.Dynamic; |
| 7 | +using System.Web.Hosting; |
| 8 | + |
| 9 | +namespace BlogEngine.Core.Data.ViewModels |
| 10 | +{ |
| 11 | + public class DashboardVM |
| 12 | + { |
| 13 | + private List<Post> _posts; |
| 14 | + private List<Page> _pages; |
| 15 | + private List<Comment> _comments; |
| 16 | + private List<TrashItem> _trash; |
| 17 | + |
| 18 | + public DashboardVM() |
| 19 | + { |
| 20 | + _posts = new List<Post>(); |
| 21 | + _pages = new List<Page>(); |
| 22 | + _comments = new List<Comment>(); |
| 23 | + _trash = new List<TrashItem>(); |
| 24 | + |
| 25 | + LoadProperties(); |
| 26 | + } |
| 27 | + |
| 28 | + #region Properties |
| 29 | + |
| 30 | + public List<Post> DraftPosts { get; set; } |
| 31 | + public int PostPublishedCnt { get; set; } |
| 32 | + public int PostDraftCnt { get; set; } |
| 33 | + |
| 34 | + public List<Comment> Comments |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + return _comments.AsQueryable().OrderBy("DateCreated desc").Take(5).ToList(); |
| 39 | + } |
| 40 | + } |
| 41 | + public int ApprovedCommentsCnt { get; set; } |
| 42 | + public int PendingCommentsCnt { get; set; } |
| 43 | + public int SpamCommentsCnt { get; set; } |
| 44 | + |
| 45 | + public List<Page> DraftPages { get; set; } |
| 46 | + public int PagePublishedCnt { get; set; } |
| 47 | + public int PageDraftCnt { get; set; } |
| 48 | + |
| 49 | + public List<TrashItem> Trash { get; set; } |
| 50 | + |
| 51 | + public List<QuickNote> Notes |
| 52 | + { |
| 53 | + get |
| 54 | + { |
| 55 | + return new QuickNotes(Security.CurrentUser.Identity.Name).Notes; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public List<SelectOption> Logs |
| 60 | + { |
| 61 | + get |
| 62 | + { |
| 63 | + return GetLogFile().ToList(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #endregion |
| 68 | + |
| 69 | + private void LoadProperties() |
| 70 | + { |
| 71 | + LoadPosts(); |
| 72 | + LoadPages(); |
| 73 | + LoadTrash(); |
| 74 | + } |
| 75 | + |
| 76 | + private void LoadPosts() |
| 77 | + { |
| 78 | + var posts = Post.ApplicablePosts.Where(p => p.IsVisible); |
| 79 | + DraftPosts = posts.Where(p => p.IsPublished == false).ToList(); |
| 80 | + PostDraftCnt = DraftPosts.Count; |
| 81 | + PostPublishedCnt = posts.Where(p => p.IsPublished).ToList().Count; |
| 82 | + |
| 83 | + foreach (var p in posts) |
| 84 | + { |
| 85 | + ApprovedCommentsCnt += p.Comments.Where(c => c.IsPublished && !c.IsDeleted).Count(); |
| 86 | + PendingCommentsCnt += p.Comments.Where(c => !c.IsPublished && !c.IsSpam && !c.IsDeleted).Count(); |
| 87 | + SpamCommentsCnt += p.Comments.Where(c => !c.IsPublished && c.IsSpam && !c.IsDeleted).Count(); |
| 88 | + _comments.AddRange(p.Comments); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void LoadPages() |
| 93 | + { |
| 94 | + var pages = Page.Pages.Where(p => p.IsVisible); |
| 95 | + DraftPages = pages.Where(p => p.IsPublished == false).ToList(); |
| 96 | + PageDraftCnt = DraftPages.Count; |
| 97 | + PagePublishedCnt = pages.Where(p => p.IsPublished).ToList().Count; |
| 98 | + } |
| 99 | + |
| 100 | + private void LoadTrash() |
| 101 | + { |
| 102 | + var posts = Post.ApplicablePosts.Where(p => p.IsDeleted); |
| 103 | + if (posts.Count() > 0) |
| 104 | + { |
| 105 | + foreach (var p in posts) |
| 106 | + { |
| 107 | + _trash.Add(new TrashItem |
| 108 | + { |
| 109 | + Id = p.Id, |
| 110 | + Title = System.Web.HttpContext.Current.Server.HtmlEncode(p.Title), |
| 111 | + RelativeUrl = p.RelativeLink, |
| 112 | + ObjectType = "Post", |
| 113 | + DateCreated = p.DateCreated.ToString("MM/dd/yyyy HH:mm") |
| 114 | + } |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + var pages = Page.Pages.Where(p => p.IsDeleted); |
| 119 | + if (pages.Count() > 0) |
| 120 | + { |
| 121 | + foreach (var page in pages) |
| 122 | + { |
| 123 | + _trash.Add(new TrashItem |
| 124 | + { |
| 125 | + Id = page.Id, |
| 126 | + Title = System.Web.HttpContext.Current.Server.HtmlEncode(page.Title), |
| 127 | + RelativeUrl = page.RelativeLink, |
| 128 | + ObjectType = "Page", |
| 129 | + DateCreated = page.DateCreated.ToString("MM/dd/yyyy HH:mm") |
| 130 | + } |
| 131 | + ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + var comms = _comments.Where(c => c.IsDeleted); |
| 136 | + if (comms.Count() > 0) |
| 137 | + { |
| 138 | + foreach (var c in comms) |
| 139 | + { |
| 140 | + _trash.Add(new TrashItem |
| 141 | + { |
| 142 | + Id = c.Id, |
| 143 | + Title = c.Author + ": " + c.Teaser, |
| 144 | + RelativeUrl = c.RelativeLink, |
| 145 | + ObjectType = "Comment", |
| 146 | + DateCreated = c.DateCreated.ToString("MM/dd/yyyy HH:mm") |
| 147 | + } |
| 148 | + ); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + IEnumerable<SelectOption> GetLogFile() |
| 154 | + { |
| 155 | + string fileLocation = HostingEnvironment.MapPath(Path.Combine(BlogConfig.StorageLocation, "logger.txt")); |
| 156 | + var items = new List<SelectOption>(); |
| 157 | + |
| 158 | + if (File.Exists(fileLocation)) |
| 159 | + { |
| 160 | + using (var sw = new StreamReader(fileLocation)) |
| 161 | + { |
| 162 | + string line; |
| 163 | + string logItem = ""; |
| 164 | + int count = 1; |
| 165 | + while ((line = sw.ReadLine()) != null) |
| 166 | + { |
| 167 | + if (line.Contains("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) |
| 168 | + { |
| 169 | + // new log item |
| 170 | + if (!string.IsNullOrEmpty(logItem)) |
| 171 | + { |
| 172 | + var item = new SelectOption(); |
| 173 | + item.OptionName = "Line" + count.ToString(); |
| 174 | + item.OptionValue = logItem; |
| 175 | + items.Add(item); |
| 176 | + logItem = ""; |
| 177 | + count++; |
| 178 | + } |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + // append line to log item |
| 183 | + logItem = logItem + line + "<br/>"; |
| 184 | + } |
| 185 | + } |
| 186 | + sw.Close(); |
| 187 | + return items; |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + return new List<SelectOption>(); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments