diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs index 3a53ceddb..5b3b04494 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs @@ -15,10 +15,10 @@ namespace Orchard.Blogs.Handlers { private readonly IWorkContextAccessor _workContextAccessor; private readonly IContentManager _contentManager; // contains the creation time of a blog part before it has been changed - private readonly Dictionary _previousCreatedUtc = new Dictionary(); + private readonly Dictionary _previousCreatedUtc = new Dictionary(); public BlogPartArchiveHandler( - IRepository blogArchiveRepository, + IRepository blogArchiveRepository, IBlogPostService blogPostService, IWorkContextAccessor workContextAccessor, IContentManager contentManager) { @@ -26,11 +26,11 @@ namespace Orchard.Blogs.Handlers { _workContextAccessor = workContextAccessor; _contentManager = contentManager; - OnUpdating((context, cp) => { if(context.ContentItem.Has()) SavePreviousCreatedDate(context.Id);}); + OnUpdating((context, cp) => { if (context.ContentItem.Has()) SavePreviousCreatedDate(context.Id); }); OnRemoving((context, bp) => SavePreviousCreatedDate(context.Id)); OnUnpublishing((context, bp) => SavePreviousCreatedDate(context.Id)); - OnPublished((context, bp) => IncreaseBlogArchive(bp)); + OnPublished((context, bp) => ManageBlogArchiveSync(bp)); OnUnpublished((context, bp) => ReduceBlogArchive(bp)); OnRemoved((context, bp) => ReduceBlogArchive(bp)); } @@ -71,58 +71,56 @@ namespace Orchard.Blogs.Handlers { && x.Month == datetime.Month && x.Year == datetime.Year); - if(previousArchiveRecord == null) + if (previousArchiveRecord == null) return; if (previousArchiveRecord.PostCount > 1) previousArchiveRecord.PostCount--; else _blogArchiveRepository.Delete(previousArchiveRecord); + + blogPostPart.ArchiveSync = null; } + private void ReduceBlogArchiveByDate(int BlogPartId, DateTime? dateBlogPost) { + _blogArchiveRepository.Flush(); + + if (BlogPartId!= 0 && dateBlogPost.HasValue) { + var previousArchiveRecord = _blogArchiveRepository.Table + .FirstOrDefault(x => x.BlogPart.Id == BlogPartId + && x.Month == dateBlogPost.Value.Month + && x.Year == dateBlogPost.Value.Year); + + if (previousArchiveRecord == null) + return; + + if (previousArchiveRecord.PostCount > 1) + previousArchiveRecord.PostCount--; + else + _blogArchiveRepository.Delete(previousArchiveRecord); + } + else { + return; + } + + } + private void IncreaseBlogArchive(BlogPostPart blogPostPart) { _blogArchiveRepository.Flush(); - + var commonPart = blogPostPart.As(); - if(commonPart == null || !commonPart.CreatedUtc.HasValue) + 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 @@ -136,7 +134,31 @@ namespace Orchard.Blogs.Handlers { _blogArchiveRepository.Create(newArchiveRecord); } - newArchiveRecord.PostCount++; + newArchiveRecord.PostCount++; + } + + private void ManageBlogArchiveSync(BlogPostPart blogPostPart) { + var commonPart = blogPostPart.As(); + if (commonPart == null || !commonPart.CreatedUtc.HasValue) + return; + + var timeZone = _workContextAccessor.GetContext().CurrentTimeZone; + var creationDate = TimeZoneInfo.ConvertTimeFromUtc(commonPart.CreatedUtc.Value, timeZone); + + if (blogPostPart.ArchiveSync == null) { + IncreaseBlogArchive(blogPostPart); + blogPostPart.ArchiveSync = creationDate; + } + else { + if (creationDate == blogPostPart.ArchiveSync) { + return; + } + else { + ReduceBlogArchiveByDate(blogPostPart.BlogPart.Id, blogPostPart.ArchiveSync); + IncreaseBlogArchive(blogPostPart); + blogPostPart.ArchiveSync = creationDate; + } + } } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Models/BlogPostPart.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Models/BlogPostPart.cs index 754e3e99d..d495fb3a9 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Models/BlogPostPart.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Models/BlogPostPart.cs @@ -49,5 +49,10 @@ namespace Orchard.Blogs.Models { public DateTime? PublishedUtc { get { return this.As().PublishedUtc; } } + + public DateTime? ArchiveSync { + get { return this.Retrieve(x => x.ArchiveSync); } + set { this.Store(x => x.ArchiveSync, value); } + } } } \ No newline at end of file