private static void RecalculateBlogArchive(IRepository<BlogPartArchiveRecord> blogArchiveRepository, IBlogPostService blogPostService, BlogPostPart blogPostPart)
        {
            blogArchiveRepository.Flush();

            // remove all current blog archive records
            var blogArchiveRecords =
                from bar in blogArchiveRepository.Table
                where bar.BlogPart == blogPostPart.BlogPart.Record
                select bar;
            blogArchiveRecords.ToList().ForEach(blogArchiveRepository.Delete);

            // get all blog posts for the current blog
            var posts = blogPostService.Get(blogPostPart.BlogPart, VersionOptions.Published);

            // create a dictionary of all the year/month combinations and their count of posts that are published in this blog
            var inMemoryBlogArchives = new Dictionary<DateTime, int>();
            foreach (var post in posts) {
                if (!post.Has<CommonPart>())
                    continue;

                var commonPart = post.As<CommonPart>();
                var key = new DateTime(commonPart.CreatedUtc.Value.Year, commonPart.CreatedUtc.Value.Month, 1);

                if (inMemoryBlogArchives.ContainsKey(key))
                    inMemoryBlogArchives[key]++;
                else
                    inMemoryBlogArchives[key] = 1;
            }

            // create the new blog archive records based on the in memory values
            foreach (KeyValuePair<DateTime, int> item in inMemoryBlogArchives) {
                blogArchiveRepository.Create(new BlogPartArchiveRecord {BlogPart = blogPostPart.BlogPart.Record, Year = item.Key.Year, Month = item.Key.Month, PostCount = item.Value});
            }
        }
        private void ReduceBlogArchive(BlogPostPart blogPostPart) {
            _blogArchiveRepository.Flush();

            // don't reduce archive count if the content item is not published
            if (!_previousCreatedUtc.ContainsKey(blogPostPart.Id)) {
                return;
            }

            var commonPart = blogPostPart.As<ICommonPart>();
            if (commonPart == null || !commonPart.CreatedUtc.HasValue)
                return;

            var timeZone = _workContextAccessor.GetContext().CurrentTimeZone;
            var datetime = TimeZoneInfo.ConvertTimeFromUtc(commonPart.CreatedUtc.Value, timeZone);

            var previousArchiveRecord = _blogArchiveRepository.Table
                .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id
                                    && x.Month == datetime.Month
                                    && x.Year == datetime.Year);

            if(previousArchiveRecord == null)
                return;

            if (previousArchiveRecord.PostCount > 1)
                previousArchiveRecord.PostCount--;
            else
                _blogArchiveRepository.Delete(previousArchiveRecord);
        }
        private void ProcessBlogPostsCount(BlogPostPart blogPostPart) {
            CommonPart commonPart = blogPostPart.As<CommonPart>();
            if (commonPart != null &&
                commonPart.Record.Container != null) {

                _blogService.ProcessBlogPostsCount(commonPart.Container.Id);
            }
        }
        private void IncreaseBlogArchive(BlogPostPart blogPostPart) {
            _blogArchiveRepository.Flush();
            
            var commonPart = blogPostPart.As<ICommonPart>();
            if(commonPart == null || !commonPart.CreatedUtc.HasValue)
                return;

            // get the time zone for the current request
            var timeZone = _workContextAccessor.GetContext().CurrentTimeZone;

            var previousCreatedUtc = _previousCreatedUtc.ContainsKey(blogPostPart.Id) ? _previousCreatedUtc[blogPostPart.Id] : DateTime.MinValue;
            previousCreatedUtc = TimeZoneInfo.ConvertTimeFromUtc(previousCreatedUtc, timeZone);

            var previousMonth = previousCreatedUtc.Month;
            var previousYear = previousCreatedUtc.Year;

            var newCreatedUtc = commonPart.CreatedUtc;
            newCreatedUtc = TimeZoneInfo.ConvertTimeFromUtc(newCreatedUtc.Value, timeZone);

            var newMonth = newCreatedUtc.Value.Month;
            var newYear = newCreatedUtc.Value.Year;

            // if archives are the same there is nothing to do
            if (previousMonth == newMonth && previousYear == newYear) {
                return;
            }
            
            // decrement previous archive record
            var previousArchiveRecord = _blogArchiveRepository
                .Table
                .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id
                                     && x.Month == previousMonth
                                     && x.Year == previousYear);

            if (previousArchiveRecord != null && previousArchiveRecord.PostCount > 0) {
                previousArchiveRecord.PostCount--;
            }

            // if previous count is now zero, delete the record
            if (previousArchiveRecord != null && previousArchiveRecord.PostCount == 0) {
                _blogArchiveRepository.Delete(previousArchiveRecord);
            }
            
            // increment new archive record
            var newArchiveRecord = _blogArchiveRepository
                .Table
                .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id
                                     && x.Month == newMonth
                                     && x.Year == newYear);

            // if record can't be found create it
            if (newArchiveRecord == null) {
                newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPostPart.BlogPart.ContentItem.Record, Year = newYear, Month = newMonth, PostCount = 0 };
                _blogArchiveRepository.Create(newArchiveRecord);
            }

            newArchiveRecord.PostCount++;            
        }
Example #5
0
        private void UpdateBlogPostCount(BlogPostPart blogPostPart) {
            CommonPart commonPart = blogPostPart.As<CommonPart>();
            if (commonPart != null &&
                commonPart.Record.Container != null) {

                BlogPart blogPart = blogPostPart.BlogPart ?? 
                    _blogService.Get(commonPart.Record.Container.Id, VersionOptions.Published).As<BlogPart>();

                // Ensure the "right" set of published posts for the blog is obtained
                blogPart.PostCount = _blogPostService.PostCount(blogPart);
            }
        }
        private void ReduceBlogArchive(IRepository<BlogPartArchiveRecord> blogArchiveRepository, BlogPostPart blogPostPart) {
            blogArchiveRepository.Flush();

            var commonPart = blogPostPart.As<CommonPart>();
            if (commonPart == null || !commonPart.CreatedUtc.HasValue)
                return;

            var timeZone = _workContextAccessor.GetContext().CurrentTimeZone;
            var datetime = TimeZoneInfo.ConvertTimeFromUtc(commonPart.CreatedUtc.Value, timeZone);

            var previousArchiveRecord = blogArchiveRepository.Table
                .FirstOrDefault(x => x.BlogPart == blogPostPart.BlogPart.Record
                                    && x.Month == datetime.Month
                                    && x.Year == datetime.Year);

            if(previousArchiveRecord == null)
                return;

            if (previousArchiveRecord.PostCount > 0)
                previousArchiveRecord.PostCount--;
            else
                blogArchiveRepository.Delete(previousArchiveRecord);
        }
 private static void SetModelProperties(BuildShapeContext context, BlogPostPart blogPost) {
     context.Shape.Blog = blogPost.BlogPart;
 }
 public static string BlogPostUnpublish(this UrlHelper urlHelper, BlogPostPart blogPostPart) {
     return urlHelper.Action("Unpublish", "BlogPostAdmin", new { blogId = blogPostPart.BlogPart.Id, postId = blogPostPart.Id, area = "Orchard.Blogs" });
 }
 public static string BlogPost(this UrlHelper urlHelper, BlogPostPart blogPostPart) {
     return urlHelper.Action("Item", "BlogPost", new { blogPath = blogPostPart.BlogPart.As<IRoutableAspect>().Path, postSlug = blogPostPart.As<IRoutableAspect>().GetEffectiveSlug(), area = "Orchard.Blogs" });
 }
Example #10
0
        private static XRpcStruct CreateBlogStruct(
            BlogPostPart blogPostPart,
            UrlHelper urlHelper)
        {
            var url = urlHelper.AbsoluteAction(() => urlHelper.BlogPost(blogPostPart));
            var blogStruct = new XRpcStruct()
                .Set("postid", blogPostPart.Id)
                .Set("title", blogPostPart.Title)
                .Set("wp_slug", blogPostPart.Slug)
                .Set("description", blogPostPart.Text)
                .Set("link", url)
                .Set("permaLink", url);

            if (blogPostPart.PublishedUtc != null) {
                blogStruct.Set("dateCreated", blogPostPart.PublishedUtc);
                blogStruct.Set("date_created_gmt", blogPostPart.PublishedUtc);
            }

            return blogStruct;
        }
Example #11
0
 public DateTime? GetScheduledPublishUtc(BlogPostPart blogPostPart) {
     var task = _publishingTaskManager.GetPublishTask(blogPostPart.ContentItem);
     return (task == null ? null : task.ScheduledUtc);
 }
Example #12
0
 public void Unpublish(BlogPostPart blogPostPart) {
     _contentManager.Unpublish(blogPostPart.ContentItem);
 }
Example #13
0
 public void Publish(BlogPostPart blogPostPart, DateTime scheduledPublishUtc) {
     _publishingTaskManager.Publish(blogPostPart.ContentItem, scheduledPublishUtc);
 }
Example #14
0
 public void Publish(BlogPostPart blogPostPart) {
     _publishingTaskManager.DeleteTasks(blogPostPart.ContentItem);
     _contentManager.Publish(blogPostPart.ContentItem);
 }
Example #15
0
 public void Delete(BlogPostPart blogPostPart) {
     _publishingTaskManager.DeleteTasks(blogPostPart.ContentItem);
     _contentManager.Remove(blogPostPart.ContentItem);
 }
Example #16
0
        private static XRpcStruct CreateBlogStruct(
            BlogPostPart blogPostPart,
            UrlHelper urlHelper)
        {
            var url = urlHelper.AbsoluteAction(() => urlHelper.ItemDisplayUrl(blogPostPart));

            if (blogPostPart.HasDraft()) {
                url = urlHelper.AbsoluteAction("Preview", "Item", new { area = "Contents", id = blogPostPart.ContentItem.Id });
            }

            var blogStruct = new XRpcStruct()
                .Set("postid", blogPostPart.Id)
                .Set("title", HttpUtility.HtmlEncode(blogPostPart.Title))

                .Set("description", blogPostPart.Text)
                .Set("link", url)
                .Set("permaLink", url);

            blogStruct.Set("wp_slug", blogPostPart.As<IAliasAspect>().Path);

            if (blogPostPart.PublishedUtc != null) {
                blogStruct.Set("dateCreated", blogPostPart.PublishedUtc);
                blogStruct.Set("date_created_gmt", blogPostPart.PublishedUtc);
            }

            return blogStruct;
        }