示例#1
0
        public ContentComment SaveComment(ContentComment comment)
        {
            using (var context = new Data.CMSContext()) {
                ContentComment oldComment = context.ContentComments.SingleOrDefault(c => c.CommentUId == comment.CommentUId);

                if (oldComment == null)
                {
                    context.ContentComments.Add(comment);
                    int?position = context.ContentComments.Where(c => c.ContentUId == comment.ContentUId).Max(c => (int?)c.Position);
                    if (position == null)
                    {
                        comment.Position = 0;
                    }
                    else
                    {
                        comment.Position = position.Value + 1;
                    }
                }
                else
                {
                    context.ContentComments.Attach(oldComment);
                    context.Entry <ContentComment>(oldComment).CurrentValues.SetValues(comment);
                }

                context.SaveChanges();

                IncreaseCommentCount(comment);

                return(comment);
            }
        }
示例#2
0
 public ContentHead GetContentHead(string id)
 {
     using (var context = new Data.CMSContext())
     {
         return(context.ContentHeads.SingleOrDefault(c => c.ContentUId == id));
     }
 }
示例#3
0
        public List <string> GetTagCloud(string kind)
        {
            using (var context = new Data.CMSContext())
            {
                List <string> kinds = new List <string>();

                IQueryable <ContentTag> tags = context.ContentTags.Where(t => t.Tag != null && t.Tag != "");

                tags = tags.Where(t => context.ContentHeads.Any(c => c.Kind == kind));

                ContentTagRank[] tagTanks = tags
                                            .GroupBy(t => t.Tag)
                                            .Select(g => new ContentTagRank {
                    Tag = g.Key, Rank = g.Count()
                })
                                            .OrderByDescending(tr => tr.Rank)
                                            .ToArray <ContentTagRank>();


                foreach (var item in tagTanks)
                {
                    kinds.Add(item.Tag);
                }
                return(kinds.ToList());
            }
        }
示例#4
0
        public ContentHead SaveContent(ContentHead content)
        {
            using (var context = new Data.CMSContext())
            {
                ContentHead      oldContent   = GetContent(content.ContentUId);
                List <CrossLink> removedLinks = new List <CrossLink>();
                List <CrossLink> addedLinks   = content.CrossLinks.ToList();

                if (oldContent == null)
                {
                    context.ContentHeads.Add(content);
                }
                else
                {
                    context.ContentHeads.Attach(oldContent);
                    context.Entry <ContentHead>(oldContent).CurrentValues.SetValues(content);
                    context.Entry <ContentData>(oldContent.Data).CurrentValues.SetValues(content.Data);
                    if (oldContent.CustomInfo != null)
                    {
                        context.Entry <ContentCustomInfo>(oldContent.CustomInfo).CurrentValues.SetValues(content.CustomInfo);
                    }
                }

                //context.ApplyCollectionValues<CrossLink>(oldContent != null ? oldContent.CrossLinks : null, content.CrossLinks, (t1, t2) => { return t1.PageArea == t2.PageArea; });
                ApplyCollectionValuesCrossLinks(oldContent != null ? oldContent.CrossLinks : null, content.CrossLinks);

                context.ApplyCollectionValues <ContentTag>(oldContent != null ? oldContent.Tags : null, content.Tags, (t1, t2) => { return(t1.Tag == t2.Tag); });


                context.SaveChanges();
            }
            return(content);
        }
示例#5
0
        public IEnumerable<ContentComment> GetComments(string id, int afterPosition, int beforePosition, int top, string order, bool includeCensored)
        {
            using (var context = new Data.CMSContext()) {
                var comments = context.ContentComments.Where(c => c.ContentUId == id);

                if (includeCensored)
                    comments = comments.Where(c => c.Status == 1);

                if(order!=null && order.ToUpper()=="DESC")
                    comments = comments.OrderByDescending(c => c.Position);
                else
                    comments = comments.OrderBy(c => c.Position);

                if (afterPosition != -1)
                    comments = comments.Where(c => c.Position > afterPosition);

                if (beforePosition != -1)
                    comments = comments.Where(c => c.Position < beforePosition);

                if(top!=0)
                    comments = comments.Take(top);

                return comments.ToArray();

            }
        }
示例#6
0
 public string[] GetPublishedLocations(string kind)
 {
     using (var context = new Data.CMSContext())
     {
         return(context.ContentHeads.Where(c => c.Kind == kind).Select(c => c.Location).Distinct().ToArray());
     }
 }
示例#7
0
        public ContentComment SaveComment(ContentComment comment)
        {
            using (var context = new Data.CMSContext()) {

                ContentComment oldComment = context.ContentComments.SingleOrDefault(c => c.CommentUId == comment.CommentUId);

                if (oldComment == null) {
                    context.ContentComments.Add(comment);
                    int? position = context.ContentComments.Where(c => c.ContentUId == comment.ContentUId).Max(c => (int?) c.Position);
                    if (position == null)
                        comment.Position = 0;
                    else
                        comment.Position = position.Value + 1;
                }
                else {
                    context.ContentComments.Attach(oldComment);
                    context.Entry<ContentComment>(oldComment).CurrentValues.SetValues(comment);
                }

                context.SaveChanges();

                IncreaseCommentCount(comment);

                return comment;
            }
        }
        public File GetFile(string fileUId, bool includeData = true)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable <File> file = context.Files;

                if (includeData)
                {
                    file = context.Files.Include("Data");
                }

                var f = file.SingleOrDefault(x => x.FileUId == fileUId);

                if (EncryptFiles && f.Data != null)
                {
                    if (f.Data.StoredData != null)
                    {
                        f.Data.StoredData = CryptUtil.DecryptBytes(f.Data.StoredData);
                    }

                    if (f.Data.StoredThumbData != null)
                    {
                        f.Data.StoredThumbData = CryptUtil.DecryptBytes(f.Data.StoredThumbData);
                    }
                }

                return(f);
            }
        }
示例#9
0
        public ContentTagRank[] GetTagCloud(string location, string[] kinds)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable <ContentTag> tags = context.ContentTags.Where(t => t.Tag != null && t.Tag != "");


                if (location != null)
                {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                    {
                        location = location + "/";
                    }
                    tags = tags.Where(t => context.ContentHeads.Any(c => c.ContentUId == t.ContentUId && c.Location == location));
                }

                if (kinds != null)
                {
                    tags = tags.Where(t => context.ContentHeads.Any(c => c.ContentUId == t.ContentUId && kinds.Contains(c.Kind.ToLower())));
                }

                ContentTagRank[] tagTanks = tags
                                            .GroupBy(t => t.Tag)
                                            .Select(g => new ContentTagRank {
                    Tag = g.Key, Rank = g.Count()
                })
                                            .OrderByDescending(tr => tr.Rank)
                                            .ToArray <ContentTagRank>();

                return(tagTanks);
            }
        }
示例#10
0
        public DateTime?GetLastPublishDate(string location, string[] kinds)
        {
            using (var context = new Data.CMSContext())
            {
                IQueryable <ContentHead> contents = context.ContentHeads;

                contents = OnlyPublishedContents(contents);

                if (!string.IsNullOrEmpty(location))
                {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                    {
                        location = location + "/";
                    }
                    contents = contents.Where(c => c.Location == location);
                }

                if (kinds != null)
                {
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));
                }

                return(contents.Max(c => (DateTime?)c.CreateDate));
            }
        }
示例#11
0
 private void IncreaseCommentCount(ContentComment comment)
 {
     using (var context = new Data.CMSContext()) {
         ContentCommentCount oldCount = context.ContentCommentCounts.SingleOrDefault(c => c.ContentUId == comment.ContentUId);
         if (oldCount == null)
             context.ContentCommentCounts.Add(new ContentCommentCount() { ContentUId = comment.ContentUId, Count = 1 });
         else
             oldCount.Count++;
         context.SaveChanges();
     }
 }
示例#12
0
 public void RemoveFile(string fileUId)
 {
     using (var context = new Data.CMSContext()) {
         File file = context.Files.SingleOrDefault(f => f.FileUId == fileUId);
         if (file == null)
         {
             return;
         }
         context.Files.Remove(file);
         context.SaveChanges();
     }
 }
示例#13
0
 public CMSService(
     Data.CMSContext context,
     IOptions <Box.Common.BoxSettings> boxSettings,
     LogService log,
     IHostingEnvironment env)
 {
     _context = context;
     Settings = boxSettings.Value;
     _log     = log;
     WebPath  = env.WebRootPath;
     AppPath  = env.ContentRootPath;
 }
示例#14
0
 public void RemoveContent(string contentUId)
 {
     using (var context = new Data.CMSContext()) {
         ContentHead content = context.ContentHeads.SingleOrDefault(c => c.ContentUId == contentUId);
         if (content == null)
         {
             return;
         }
         context.ContentHeads.Remove(content);
         context.SaveChanges();
     }
 }
示例#15
0
        public IEnumerable <ContentHead> GetRelatedContent(string[] tags, int top, string location, string[] kinds, bool includeData = false)
        {
            using (var context = new Data.CMSContext())
            {
                IQueryable <ContentHead> contents = null;

                if (!includeData)
                {
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("Tags").Include("PageViewCount").Include("CustomInfo");
                }
                else
                {
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("Tags").Include("PageViewCount").Include("CustomInfo").Include("Data");
                }

                // only published
                contents = OnlyPublishedContents(contents);

                // remove empty entries, and go lower
                tags = tags.Where(t => !String.IsNullOrEmpty(t)).ToArray();
                for (int i = 0; i < tags.Length; i++)
                {
                    tags[i] = tags[i].ToLower();
                }

                // get related using tags
                contents = contents.Where(c => c.Tags.Any(t => tags.Contains(t.Tag)));

                contents = OrderContents(contents, "Date");

                if (location != null)
                {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                    {
                        location = location + "/";
                    }
                    contents = contents.Where(c => c.Location == location);
                }

                if (kinds != null)
                {
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));
                }

                if (top != 0)
                {
                    contents = contents.Take(top);
                }

                return(contents.ToArray());
            }
        }
示例#16
0
        public ContentHead GetContent(string id, bool onlyPublished = false)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable <ContentHead> content = context.ContentHeads.Include("Data").Include("CrossLinks").Include("Tags").Include("CommentsCount").Include("CustomInfo").Where(c => c.ContentUId == id);

                if (onlyPublished)
                {
                    content = OnlyPublishedContents(content);
                }

                return(content.SingleOrDefault());
            }
        }
        /// <summary>
        /// Adds a news crosslink for the contect at the given area.
        /// </summary>
        /// <param name="contentUId">The content Id</param>
        /// <param name="area">The crosslink area</param>
        /// <param name="changeDisplayOrderBy">Used to change the crosslink display order</param>
        /// <returns></returns>
        public void AddCrossLink(string contentUId, string area, short changeDisplayOrderBy = 0)
        {
            short oldOrder = 0;

            using (var context = new Data.CMSContext()) {
                // max crosslink order
                short maxOrder = -1;
                if (context.CrossLinks.Any(c => c.PageArea == area))
                {
                    maxOrder = context.CrossLinks.Where(c => c.PageArea == area).Select(c => c.DisplayOrder).DefaultIfEmpty().Max();
                }

                CrossLink link = context.CrossLinks.SingleOrDefault(c => c.ContentUId == contentUId && c.PageArea == area);

                if (link == null)
                {
                    link = new CrossLink()
                    {
                        ContentUId = contentUId, PageArea = area, DisplayOrder = (short)(maxOrder + 1)
                    };
                    context.CrossLinks.Add(link);
                }

                // calcs the new crosslink order
                oldOrder = link.DisplayOrder;
                short order = (short)(link.DisplayOrder + changeDisplayOrderBy);

                // if is a order chage and it its ut of bounds, get out of here
                if (changeDisplayOrderBy < 0 && oldOrder == 0)
                {
                    return;
                }
                if (changeDisplayOrderBy > 0 && oldOrder == maxOrder)
                {
                    return;
                }

                // set the new order
                link.DisplayOrder = order;

                // change the other link display order
                CrossLink link2 = null;
                link2 = context.CrossLinks.SingleOrDefault(c => c.ContentUId != contentUId && c.PageArea == area && c.DisplayOrder == order);
                if (link2 != null)
                {
                    link2.DisplayOrder = oldOrder;
                }

                context.SaveChanges();
            }
        }
示例#18
0
 public ContentHead GetContentHeadByUrlAndKind(string url, string kind, bool onlyPublished)
 {
     url = url.ToLower();
     using (var context = new Data.CMSContext()) {
         IQueryable <ContentHead> content = context.ContentHeads.Where(c => c.Location + c.CanonicalName == url);
         if (kind != null)
         {
             content = content.Where(c => c.Kind == kind);
         }
         if (onlyPublished)
         {
             content = OnlyPublishedContents(content);
         }
         return(content.SingleOrDefault());
     }
 }
示例#19
0
        public IEnumerable <ContentHead> GetCrossLinksFrom(string pageArea, string order = "CrossLinkDisplayOrder", int top = 0, string[] kinds = null, bool includeData = false, string[] pageAreaFallbacks = null, bool onlyPublished = true)
        {
            using (var context = new Data.CMSContext())
            {
                IQueryable <ContentHead> contents = null;

                if (!includeData)
                {
                    contents = context.ContentHeads.Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo");
                }
                else
                {
                    contents = context.ContentHeads.Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo").Include("Data");
                }

                contents = contents.Where(c => c.CrossLinks.Any(x => x.PageArea == pageArea));

                if (onlyPublished)
                {
                    contents = OnlyPublishedContents(contents);
                }

                contents = OrderContents(contents, order, pageArea);

                if (kinds != null)
                {
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));
                }

                if (top != 0)
                {
                    contents = contents.Take(top);
                }

                var array = contents.ToArray();

                // if ther is no content, and there is any fallback, try it
                if (!array.Any() && pageAreaFallbacks != null && pageAreaFallbacks.Length >= 1)
                {
                    string   fallBackArea = pageAreaFallbacks.First();
                    string[] othersFall   = pageAreaFallbacks.Where(a => a != fallBackArea).ToArray();
                    return(GetCrossLinksFrom(fallBackArea, order, top, kinds, includeData, othersFall));
                }

                return(array);
            }
        }
示例#20
0
        public void LogPageView(string contentUId)
        {
            using (var context = new Data.CMSContext()) {
                ContentPageViewCount pageCount = context.ContentPageViewCounts.SingleOrDefault(c => c.ContentUId == contentUId);
                if (pageCount == null)
                {
                    context.ContentPageViewCounts.Add(pageCount = new ContentPageViewCount()
                    {
                        ContentUId = contentUId, Count = 0
                    });
                }

                pageCount.Count++;

                context.SaveChanges();
            }
        }
示例#21
0
 private void UpdateShareCount(string id, long count)
 {
     using (var context = new Data.CMSContext()) {
         ContentShareCount oldCount = context.ContentSharesCounts.SingleOrDefault(c => c.ContentUId == id);
         if (oldCount == null)
         {
             context.ContentSharesCounts.Add(new ContentShareCount()
             {
                 ContentUId = id, Count = count
             });
         }
         else
         {
             oldCount.Count = count;
         }
         context.SaveChanges();
     }
 }
示例#22
0
 public void UpdateCommentCount(string id, int count)
 {
     using (var context = new Data.CMSContext()) {
         ContentCommentCount oldCount = context.ContentCommentCounts.SingleOrDefault(c => c.ContentUId == id);
         if (oldCount == null)
         {
             context.ContentCommentCounts.Add(new ContentCommentCount()
             {
                 ContentUId = id, Count = count
             });
         }
         else
         {
             oldCount.Count = count;
         }
         context.SaveChanges();
     }
 }
示例#23
0
 private void IncreaseCommentCount(ContentComment comment)
 {
     using (var context = new Data.CMSContext()) {
         ContentCommentCount oldCount = context.ContentCommentCounts.SingleOrDefault(c => c.ContentUId == comment.ContentUId);
         if (oldCount == null)
         {
             context.ContentCommentCounts.Add(new ContentCommentCount()
             {
                 ContentUId = comment.ContentUId, Count = 1
             });
         }
         else
         {
             oldCount.Count++;
         }
         context.SaveChanges();
     }
 }
示例#24
0
        public void SaveFile(File file, FileStorages storage)
        {
            using (var context = new Data.CMSContext()) {
                file.Data.StoredData      = EncryptFiles ? CryptUtil.EncryptBytes(file.Data.StoredData) : file.Data.StoredData;
                file.Data.StoredThumbData = EncryptFiles ? CryptUtil.EncryptBytes(file.Data.StoredThumbData) : file.Data.StoredThumbData;

                var oldfile = context.Files.SingleOrDefault(f => f.FileUId == file.FileUId);
                if (oldfile == null)
                {
                    context.Files.Add(file);
                }
                else
                {
                    context.Files.Remove(oldfile);
                    context.Files.Add(file);
                }
                context.SaveChanges();
            }
        }
示例#25
0
        public File GetFileThumb(string fileUId)
        {
            using (var context = new Data.CMSContext()) {
                var f         = context.Files.Where(x => x.FileUId == fileUId).SingleOrDefault();
                var thumbData = context.FileData.Where(x => x.FileUId == fileUId).Select(x => new { bytes = x.StoredThumbData }).SingleOrDefault();

                f.Data = new FileData()
                {
                    FileUId = f.FileUId, StoredThumbData = thumbData.bytes
                };

                if (EncryptFiles && f.Data != null && f.Data.StoredThumbData != null)
                {
                    f.Data.StoredThumbData = CryptUtil.DecryptBytes(f.Data.StoredThumbData);
                }

                return(f);
            }
        }
        public void RemoveCrossLink(string contentUId, string area)
        {
            using (var context = new Data.CMSContext()) {
                CrossLink link = context.CrossLinks.SingleOrDefault(c => c.ContentUId == contentUId && c.PageArea == area);
                if (link == null)
                {
                    return;
                }

                int order = link.DisplayOrder;

                context.CrossLinks.Remove(link);
                context.SaveChanges();

                // update orders
                context.Database.ExecuteSqlCommand(
                    "UPDATE CrossLinks SET DisplayOrder = DisplayOrder - 1 WHERE PageArea = {0} AND DisplayOrder > {1}", area, order);
            }
        }
示例#27
0
        /// <summary>
        /// Adds a news crosslink for the contect at the given area.
        /// </summary>
        /// <param name="contentUId">The content Id</param>
        /// <param name="area">The crosslink area</param>
        /// <param name="changeDisplayOrderBy">Used to change the crosslink display order</param>
        /// <returns></returns>
        public void AddCrossLink(string contentUId, string area, short changeDisplayOrderBy = 0)
        {
            short oldOrder = 0;

            using (var context = new Data.CMSContext()) {

                // max crosslink order
                short maxOrder = -1;
                if (context.CrossLinks.Any(c => c.PageArea == area)) {
                    maxOrder = context.CrossLinks.Where(c => c.PageArea == area).Select(c => c.DisplayOrder).DefaultIfEmpty().Max();
                }

                CrossLink link = context.CrossLinks.SingleOrDefault(c => c.ContentUId == contentUId && c.PageArea == area);

                if (link==null) {
                    link = new CrossLink() { ContentUId = contentUId, PageArea = area, DisplayOrder = (short)(maxOrder + 1) };
                    context.CrossLinks.Add(link);
                }

                // calcs the new crosslink order
                oldOrder = link.DisplayOrder;
                short order = (short)(link.DisplayOrder + changeDisplayOrderBy);

                // if is a order chage and it its ut of bounds, get out of here
                if (changeDisplayOrderBy < 0 && oldOrder == 0)
                    return;
                if (changeDisplayOrderBy > 0 && oldOrder == maxOrder)
                    return;

                // set the new order
                link.DisplayOrder = order;

                // change the other link display order
                CrossLink link2 = null;
                link2 = context.CrossLinks.SingleOrDefault(c => c.ContentUId != contentUId && c.PageArea == area && c.DisplayOrder == order);
                if (link2!=null)
                    link2.DisplayOrder = oldOrder;

                context.SaveChanges();

            }
        }
示例#28
0
        public IEnumerable <ContentComment> GetComments(string id, int afterPosition, int beforePosition, int top, string order, bool includeCensored)
        {
            using (var context = new Data.CMSContext()) {
                var comments = context.ContentComments.Where(c => c.ContentUId == id);

                if (includeCensored)
                {
                    comments = comments.Where(c => c.Status == 1);
                }

                if (order != null && order.ToUpper() == "DESC")
                {
                    comments = comments.OrderByDescending(c => c.Position);
                }
                else
                {
                    comments = comments.OrderBy(c => c.Position);
                }

                if (afterPosition != -1)
                {
                    comments = comments.Where(c => c.Position > afterPosition);
                }

                if (beforePosition != -1)
                {
                    comments = comments.Where(c => c.Position < beforePosition);
                }

                if (top != 0)
                {
                    comments = comments.Take(top);
                }

                return(comments.ToArray());
            }
        }
示例#29
0
        public IEnumerable <File> GetFiles(string filter, int skip, int top, string folder, bool unUsed)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable <File> files = context.Files;

                if (unUsed)
                {
                    files = files.Where(x => !context.ContentDatas.Where(c => c.JSON.Contains(x.FileUId)).Any() && !context.ContentHeads.Where(w => w.ThumbFilePath.Contains(x.FileUId)).Any());
                }

                if (!String.IsNullOrEmpty(filter))
                {
                    filter = filter.ToLower();
                    files  = files.Where(f => f.FileName.ToLower().Contains(filter));
                }

                if (folder != null)
                {
                    files = files.Where(f => f.Folder == folder);
                }

                files = files.OrderBy(f => f.FileName);

                if (skip != 0)
                {
                    files = files.Skip(skip);
                }

                if (top != 0)
                {
                    files = files.Take(top);
                }

                return(files.ToArray());
            }
        }
示例#30
0
 public string[] GetPublishedLocations(string kind)
 {
     using (var context = new Data.CMSContext()) {
         return context.ContentHeads.Where(c => c.Kind==kind).Select(c => c.Location).Distinct().ToArray();
     }
 }
示例#31
0
        public IEnumerable<ContentHead> GetRelatedContent(string[] tags, int top, string location, string[] kinds, bool includeData = false)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable<ContentHead> contents = null;

                if (!includeData)
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("Tags").Include("PageViewCount").Include("CustomInfo");
                else
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("Tags").Include("PageViewCount").Include("CustomInfo").Include("Data");

                // only published
                contents = OnlyPublishedContents(contents);

                // remove empty entries, and go lower
                tags = tags.Where(t => !String.IsNullOrEmpty(t)).ToArray();
                for (int i = 0; i < tags.Length; i++)
                    tags[i] = tags[i].ToLower();

                // get related using tags
                contents = contents.Where(c => c.Tags.Any(t => tags.Contains(t.Tag)));

                contents = OrderContents(contents, "Date");

                if (location != null) {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                        location = location + "/";
                    contents = contents.Where(c => c.Location == location);
                }

                if (kinds != null)
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));

                if (top != 0)
                    contents = contents.Take(top);

                return contents.ToArray();
            }
        }
示例#32
0
 public void RemoveContent(string contentUId)
 {
     using (var context = new Data.CMSContext()) {
         ContentHead content = context.ContentHeads.SingleOrDefault(c => c.ContentUId == contentUId);
         if (content == null)
             return;
         context.ContentHeads.Remove(content);
         context.SaveChanges();
     }
 }
示例#33
0
        public void LogPageView(string contentUId)
        {
            using (var context = new Data.CMSContext()) {
                ContentPageViewCount pageCount = context.ContentPageViewCounts.SingleOrDefault(c => c.ContentUId == contentUId);
                if (pageCount == null)
                    context.ContentPageViewCounts.Add(pageCount = new ContentPageViewCount() { ContentUId = contentUId, Count = 0 });

                pageCount.Count++;

                context.SaveChanges();
            }
        }
示例#34
0
        public ContentHead GetContent(string id, bool onlyPublished = false)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable<ContentHead> content = context.ContentHeads.Include("Data").Include("CrossLinks").Include("Tags").Include("CommentsCount").Include("CustomInfo").Where(c => c.ContentUId == id);

                if (onlyPublished)
                    content = OnlyPublishedContents(content);

                return content.SingleOrDefault();
            }
        }
示例#35
0
 public ContentHead GetContentHead(string id)
 {
     using (var context = new Data.CMSContext()) {
         return context.ContentHeads.SingleOrDefault(c => c.ContentUId == id);
     }
 }
示例#36
0
        public IEnumerable<ContentHead> GetContents(string filter, int skip, int top, string location, string[] kinds, string order = "Date", DateTime? createdFrom = null, DateTime? createdTo = null, bool includeData = false, bool onlyPublished = false, System.Linq.Expressions.Expression<Func<ContentHead, bool>> queryFilter = null)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable<ContentHead> contents = null;

                if (!includeData)
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo");
                else
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo").Include("Data");

                if (createdFrom.HasValue)
                    contents = contents.Where(c => c.ContentDate >= createdFrom.Value);

                if (createdTo.HasValue)
                    contents = contents.Where(c => c.ContentDate <= createdTo.Value);

                if (onlyPublished)
                    contents = OnlyPublishedContents(contents);

                if (queryFilter != null) {
                    contents = contents.Where(queryFilter);
                }

                if (!String.IsNullOrEmpty(filter)) {
                    filter = filter.ToLower();
                    contents = contents.Where(c =>
                        (c.CustomInfo!=null && (c.CustomInfo.Text1.ToLower().StartsWith(filter) || c.CustomInfo.Text2.ToLower().StartsWith(filter) || c.CustomInfo.Text3.ToLower().StartsWith(filter) || c.CustomInfo.Text4.ToLower().StartsWith(filter))) ||
                        c.Name.ToLower().Contains(filter) ||
                        c.Abstract.ToLower().Contains(filter) ||
                        c.Tags.Any(t => t.Tag.Contains(filter)) ||
                        c.CrossLinks.Any(x => x.PageArea.Contains(filter)));
                }

                contents = OrderContents(contents, order);

                if (location != null) {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                        location = location + "/";
                    contents = contents.Where(c => c.Location == location);
                }

                if (kinds != null && kinds.Any(k => k!=null))
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));

                if (skip != 0)
                    contents = contents.Skip(skip);

                if (top != 0)
                    contents = contents.Take(top);

                return contents.ToArray();
            }
        }
示例#37
0
        public File GetFile(string fileUId, bool includeData = true)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable<File> file = context.Files;

                if (includeData)
                    file = context.Files.Include("Data");

                var f = file.SingleOrDefault(x => x.FileUId == fileUId);

                if (EncryptFiles && f.Data!=null)
                {
                    if(f.Data.StoredData!=null)
                        f.Data.StoredData = CryptUtil.DecryptBytes(f.Data.StoredData);

                    if (f.Data.StoredThumbData != null)
                        f.Data.StoredThumbData = CryptUtil.DecryptBytes(f.Data.StoredThumbData);
                }

                return f;
            }
        }
示例#38
0
        public IEnumerable<File> GetFiles(string filter, int skip, int top, string folder, bool unUsed)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable<File> files = context.Files;

                if (unUsed)
                    files = files.Where(x => !context.ContentDatas.Where(c => c.JSON.Contains(x.FileUId)).Any() && !context.ContentHeads.Where(w => w.ThumbFilePath.Contains(x.FileUId)).Any());

                if (!String.IsNullOrEmpty(filter)) {
                    filter = filter.ToLower();
                    files = files.Where(f => f.FileName.ToLower().Contains(filter));
                }

                if (folder != null)
                    files = files.Where(f => f.Folder == folder);

                files = files.OrderBy(f => f.FileName);

                if (skip != 0)
                    files = files.Skip(skip);

                if (top != 0)
                    files = files.Take(top);

                return files.ToArray();

            }
        }
示例#39
0
        public File GetFileThumb(string fileUId)
        {
            using (var context = new Data.CMSContext()) {

                var f = context.Files.Where(x => x.FileUId == fileUId).SingleOrDefault();
                var thumbData = context.FileData.Where(x => x.FileUId == fileUId).Select(x => new { bytes = x.StoredThumbData }).SingleOrDefault();

                f.Data = new FileData() { FileUId = f.FileUId, StoredThumbData = thumbData.bytes };

                if (EncryptFiles && f.Data != null && f.Data.StoredThumbData != null) {
                    f.Data.StoredThumbData = CryptUtil.DecryptBytes(f.Data.StoredThumbData);
                }

                return f;
            }
        }
示例#40
0
 public void RemoveFile(string fileUId)
 {
     using (var context = new Data.CMSContext()) {
         File file = context.Files.SingleOrDefault(f => f.FileUId == fileUId);
         if (file == null)
             return;
         context.Files.Remove(file);
         context.SaveChanges();
     }
 }
示例#41
0
        public void SaveFile(File file, FileStorages storage)
        {
            using (var context = new Data.CMSContext()) {

                file.Data.StoredData = EncryptFiles ? CryptUtil.EncryptBytes(file.Data.StoredData) : file.Data.StoredData;
                file.Data.StoredThumbData = EncryptFiles ? CryptUtil.EncryptBytes(file.Data.StoredThumbData) : file.Data.StoredThumbData;

                var oldfile = context.Files.SingleOrDefault(f => f.FileUId == file.FileUId);
                if (oldfile == null) {
                    context.Files.Add(file);
                } else {
                    context.Files.Remove(oldfile);
                    context.Files.Add(file);
                }
                context.SaveChanges();
            }
        }
示例#42
0
        public ContentTagRank[] GetTagCloud(string location, string[] kinds)
        {
            using (var context = new Data.CMSContext()) {

                IQueryable<ContentTag> tags = context.ContentTags.Where(t => t.Tag!=null && t.Tag!="");

                if (location != null) {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                        location = location + "/";
                    tags = tags.Where(t => context.ContentHeads.Any(c => c.ContentUId == t.ContentUId && c.Location == location));
                }

                if (kinds != null)
                    tags = tags.Where(t => context.ContentHeads.Any(c => c.ContentUId == t.ContentUId && kinds.Contains(c.Kind.ToLower())));

                ContentTagRank[] tagTanks = tags
                    .GroupBy(t => t.Tag)
                    .Select(g => new ContentTagRank { Tag = g.Key, Rank = g.Count() })
                    .OrderByDescending(tr => tr.Rank)
                    .ToArray<ContentTagRank>();

                return tagTanks;

            }
        }
示例#43
0
        public DateTime? GetLastPublishDate(string location, string[] kinds)
        {
            using (var context = new Data.CMSContext()) {

                IQueryable<ContentHead> contents = context.ContentHeads;

                contents = OnlyPublishedContents(contents);

                if (!string.IsNullOrEmpty(location)) {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                        location = location + "/";
                    contents = contents.Where(c => c.Location == location);
                }

                if (kinds != null)
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));

                return contents.Max(c => (DateTime?) c.CreateDate);

            }
        }
示例#44
0
        public IEnumerable<ContentHead> GetCrossLinksFrom(string pageArea, string order = "CrossLinkDisplayOrder", int top = 0, string[] kinds = null, bool includeData = false, string[] pageAreaFallbacks = null, bool onlyPublished = true)
        {
            using (var context = new Data.CMSContext()) {
                IQueryable<ContentHead> contents = null;

                if (!includeData)
                    contents = context.ContentHeads.Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo");
                else
                    contents = context.ContentHeads.Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo").Include("Data");

                contents = contents.Where(c => c.CrossLinks.Any(x => x.PageArea == pageArea));

                if (onlyPublished)
                    contents = OnlyPublishedContents(contents);

                contents = OrderContents(contents, order, pageArea);

                if (kinds != null)
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));

                if (top != 0)
                    contents = contents.Take(top);

                var array = contents.ToArray();

                // if ther is no content, and there is any fallback, try it
                if (!array.Any() && pageAreaFallbacks != null && pageAreaFallbacks.Length>=1) {
                    string fallBackArea = pageAreaFallbacks.First();
                    string[] othersFall = pageAreaFallbacks.Where(a => a != fallBackArea).ToArray();
                    return GetCrossLinksFrom(fallBackArea, order, top, kinds, includeData, othersFall);
                }

                return array;
            }
        }
示例#45
0
 public SiteService(Data.CMSContext context, Services.CMSService cmsService)
 {
     _context    = context;
     _cmsService = cmsService;
 }
示例#46
0
        public ContentHead GetContentHeadByUrlAndKind(string url, string kind, bool onlyPublished)
        {
            url = url.ToLower();
            using (var context = new Data.CMSContext()) {
                IQueryable<ContentHead> content = context.ContentHeads.Where(c => c.Location + c.CanonicalName == url);
                if (kind != null)
                    content = content.Where(c => c.Kind == kind);
                if (onlyPublished)
                    content = OnlyPublishedContents(content);
                return content.SingleOrDefault();

            }
        }
 public CrossLink GetCrossLink(string contentUId, string area)
 {
     using (var context = new Data.CMSContext()) {
         return(context.CrossLinks.SingleOrDefault(c => c.ContentUId == contentUId && c.PageArea == area));
     }
 }
示例#48
0
        public IEnumerable <ContentHead> GetContents(string filter, int skip, int top, string location, string[] kinds, string order = "Date", DateTime?createdFrom = null, DateTime?createdTo = null, bool includeData = false, bool onlyPublished = false, System.Linq.Expressions.Expression <Func <ContentHead, bool> > queryFilter = null)
        {
            using (var context = new Data.CMSContext())
            {
                IQueryable <ContentHead> contents = null;

                if (!includeData)
                {
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo");
                }
                else
                {
                    contents = context.ContentHeads.Include("CrossLinks").Include("CommentsCount").Include("ShareCount").Include("PageViewCount").Include("Tags").Include("CustomInfo").Include("Data");
                }

                if (createdFrom.HasValue)
                {
                    contents = contents.Where(c => c.ContentDate >= createdFrom.Value);
                }

                if (createdTo.HasValue)
                {
                    contents = contents.Where(c => c.ContentDate <= createdTo.Value);
                }

                if (onlyPublished)
                {
                    contents = OnlyPublishedContents(contents);
                }

                if (queryFilter != null)
                {
                    contents = contents.Where(queryFilter);
                }

                if (!String.IsNullOrEmpty(filter))
                {
                    filter   = filter.ToLower();
                    contents = contents.Where(c =>
                                              (c.CustomInfo != null && (c.CustomInfo.Text1.ToLower().StartsWith(filter) || c.CustomInfo.Text2.ToLower().StartsWith(filter) || c.CustomInfo.Text3.ToLower().StartsWith(filter) || c.CustomInfo.Text4.ToLower().StartsWith(filter))) ||
                                              c.Name.ToLower().Contains(filter) ||
                                              c.Abstract.ToLower().Contains(filter) ||
                                              c.Tags.Any(t => t.Tag.Contains(filter)) ||
                                              c.CrossLinks.Any(x => x.PageArea.Contains(filter)));
                }

                contents = OrderContents(contents, order);

                if (location != null)
                {
                    location = location.ToLower();
                    if (!location.EndsWith("/"))
                    {
                        location = location + "/";
                    }
                    contents = contents.Where(c => c.Location == location);
                }

                if (kinds != null && kinds.Any(k => k != null))
                {
                    contents = contents.Where(c => kinds.Contains(c.Kind.ToLower()));
                }

                if (skip != 0)
                {
                    contents = contents.Skip(skip);
                }

                if (top != 0)
                {
                    contents = contents.Take(top);
                }

                return(contents.ToArray());
            }
        }
示例#49
0
        public ContentHead SaveContent(ContentHead content)
        {
            using (var context = new Data.CMSContext()) {

                ContentHead oldContent = GetContent(content.ContentUId);
                List<CrossLink> removedLinks = new List<CrossLink>();
                List<CrossLink> addedLinks = content.CrossLinks.ToList();

                if (oldContent == null) {
                    context.ContentHeads.Add(content);
                }
                else {
                    context.ContentHeads.Attach(oldContent);
                    context.Entry<ContentHead>(oldContent).CurrentValues.SetValues(content);
                    context.Entry<ContentData>(oldContent.Data).CurrentValues.SetValues(content.Data);
                    if(oldContent.CustomInfo!=null)
                        context.Entry<ContentCustomInfo>(oldContent.CustomInfo).CurrentValues.SetValues(content.CustomInfo);
                }

                //context.ApplyCollectionValues<CrossLink>(oldContent != null ? oldContent.CrossLinks : null, content.CrossLinks, (t1, t2) => { return t1.PageArea == t2.PageArea; });
                ApplyCollectionValuesCrossLinks(oldContent != null ? oldContent.CrossLinks : null, content.CrossLinks);

                context.ApplyCollectionValues<ContentTag>(oldContent != null ? oldContent.Tags : null, content.Tags, (t1, t2) => { return t1.Tag == t2.Tag; });

                context.SaveChanges();
            }
            return content;
        }
示例#50
0
 public void UpdateCommentCount(string id, int count)
 {
     using (var context = new Data.CMSContext()) {
         ContentCommentCount oldCount = context.ContentCommentCounts.SingleOrDefault(c => c.ContentUId == id);
         if (oldCount == null)
             context.ContentCommentCounts.Add(new ContentCommentCount() { ContentUId = id, Count = count });
         else
             oldCount.Count = count;
         context.SaveChanges();
     }
 }
示例#51
0
 public CrossLink GetCrossLink(string contentUId, string area)
 {
     using (var context = new Data.CMSContext()) {
         return context.CrossLinks.SingleOrDefault(c => c.ContentUId == contentUId && c.PageArea == area);
     }
 }
示例#52
0
        public void RemoveCrossLink(string contentUId, string area)
        {
            using (var context = new Data.CMSContext()) {
                CrossLink link = context.CrossLinks.SingleOrDefault(c => c.ContentUId == contentUId && c.PageArea == area);
                if (link == null)
                    return;

                int order = link.DisplayOrder;

                context.CrossLinks.Remove(link);
                context.SaveChanges();

                // update orders
                context.Database.ExecuteSqlCommand(
                    "UPDATE CrossLinks SET DisplayOrder = DisplayOrder - 1 WHERE PageArea = {0} AND DisplayOrder > {1}", area, order);

            }
        }
示例#53
0
 private void UpdateShareCount(string id, long count)
 {
     using (var context = new Data.CMSContext()) {
         ContentShareCount oldCount = context.ContentSharesCounts.SingleOrDefault(c => c.ContentUId == id);
         if (oldCount == null)
             context.ContentSharesCounts.Add(new ContentShareCount() { ContentUId = id, Count = count });
         else
             oldCount.Count = count;
         context.SaveChanges();
     }
 }