mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
#19552: Fixing VersionCreatedUtc assignment
Work Item: 19552 --HG-- branch : 1.x extra : rebase_source : 42a102ed01ed45e3ef344c94b584c32c030544df
This commit is contained in:
@@ -75,6 +75,7 @@ namespace Orchard.Core.Common.DateEditor {
|
||||
|
||||
// the date time is entered locally for the configured timezone
|
||||
part.CreatedUtc = TimeZoneInfo.ConvertTimeToUtc(createdUtc, Services.WorkContext.CurrentTimeZone);
|
||||
part.VersionCreatedUtc = part.CreatedUtc;
|
||||
}
|
||||
else {
|
||||
updater.AddModelError(Prefix, T("{0} is an invalid date and time", parseDateTime));
|
||||
|
@@ -5,47 +5,91 @@ using JetBrains.Annotations;
|
||||
using Orchard.Blogs.Models;
|
||||
using Orchard.Blogs.Services;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Data;
|
||||
|
||||
namespace Orchard.Blogs.Handlers {
|
||||
[UsedImplicitly]
|
||||
public class BlogPartArchiveHandler : ContentHandler {
|
||||
private readonly IRepository<BlogPartArchiveRecord> _blogArchiveRepository;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly IContentManager _contentManager;
|
||||
// contains the creation time of a blog part before it has been changed
|
||||
private readonly Dictionary<BlogPostPart, DateTime> _previousCreatedUtc = new Dictionary<BlogPostPart,DateTime>();
|
||||
private readonly Dictionary<int, DateTime> _previousCreatedUtc = new Dictionary<int,DateTime>();
|
||||
|
||||
public BlogPartArchiveHandler(
|
||||
IRepository<BlogPartArchiveRecord> blogArchiveRepository,
|
||||
IBlogPostService blogPostService,
|
||||
IWorkContextAccessor workContextAccessor) {
|
||||
IWorkContextAccessor workContextAccessor,
|
||||
IContentManager contentManager) {
|
||||
_blogArchiveRepository = blogArchiveRepository;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_contentManager = contentManager;
|
||||
|
||||
OnVersioning<BlogPostPart>((context, bp1, bp2) => {
|
||||
var commonPart = bp1.As<CommonPart>();
|
||||
if(commonPart == null || !commonPart.CreatedUtc.HasValue || !bp1.IsPublished || !bp2.IsPublished)
|
||||
return;
|
||||
//OnVersioning<BlogPostPart>((context, bp1, bp2) => {
|
||||
// var commonPart = bp1.As<ICommonPart>();
|
||||
// if (commonPart == null || !commonPart.CreatedUtc.HasValue)
|
||||
// return;
|
||||
|
||||
_previousCreatedUtc[bp2] = commonPart.CreatedUtc.Value;
|
||||
});
|
||||
// if (context.BuildingItemVersionRecord.Published == context.ExistingContentItem.IsPublished()) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
OnPublished<BlogPostPart>((context, bp) => RecalculateBlogArchive(blogArchiveRepository, bp));
|
||||
OnUnpublished<BlogPostPart>((context, bp) => ReduceBlogArchive(blogArchiveRepository, bp));
|
||||
OnRemoved<BlogPostPart>((context, bp) => ReduceBlogArchive(blogArchiveRepository, bp));
|
||||
// var previousPublishedVersion = contentManager.Get(commonPart.Id, VersionOptions.Published);
|
||||
|
||||
// // retrieve the creation date when it was published
|
||||
// if (previousPublishedVersion != null) {
|
||||
// var versionCommonPart = previousPublishedVersion.As<ICommonPart>();
|
||||
// if (versionCommonPart.VersionCreatedUtc.HasValue) {
|
||||
// _previousCreatedUtc[commonPart.Id] = versionCommonPart.VersionCreatedUtc.Value;
|
||||
// }
|
||||
// }
|
||||
|
||||
//});
|
||||
|
||||
//OnUpdating<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
|
||||
OnPublishing<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
|
||||
OnRemoving<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
|
||||
OnUnpublishing<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
|
||||
|
||||
OnPublished<BlogPostPart>((context, bp) => IncreaseBlogArchive(bp));
|
||||
OnUnpublished<BlogPostPart>((context, bp) => ReduceBlogArchive(bp));
|
||||
OnRemoved<BlogPostPart>((context, bp) => ReduceBlogArchive(bp));
|
||||
}
|
||||
|
||||
private void ReduceBlogArchive(IRepository<BlogPartArchiveRecord> blogArchiveRepository, BlogPostPart blogPostPart) {
|
||||
blogArchiveRepository.Flush();
|
||||
private void SavePreviousPublishDate(int contentItemId) {
|
||||
if (_previousCreatedUtc.ContainsKey(contentItemId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var commonPart = blogPostPart.As<CommonPart>();
|
||||
var previousPublishedVersion = _contentManager.Get(contentItemId, VersionOptions.Published);
|
||||
|
||||
// retrieve the creation date when it was published
|
||||
if (previousPublishedVersion != null) {
|
||||
var versionCommonPart = previousPublishedVersion.As<ICommonPart>();
|
||||
if (versionCommonPart.VersionCreatedUtc.HasValue) {
|
||||
_previousCreatedUtc[contentItemId] = versionCommonPart.VersionCreatedUtc.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ReduceBlogArchive(BlogPostPart blogPostPart) {
|
||||
_blogArchiveRepository.Flush();
|
||||
|
||||
// don't reduce archive count if the content item is not published
|
||||
if (!blogPostPart.HasPublished) {
|
||||
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
|
||||
var previousArchiveRecord = _blogArchiveRepository.Table
|
||||
.FirstOrDefault(x => x.BlogPart == blogPostPart.BlogPart.Record
|
||||
&& x.Month == datetime.Month
|
||||
&& x.Year == datetime.Year);
|
||||
@@ -56,30 +100,30 @@ namespace Orchard.Blogs.Handlers {
|
||||
if (previousArchiveRecord.PostCount > 1)
|
||||
previousArchiveRecord.PostCount--;
|
||||
else
|
||||
blogArchiveRepository.Delete(previousArchiveRecord);
|
||||
_blogArchiveRepository.Delete(previousArchiveRecord);
|
||||
}
|
||||
|
||||
private void RecalculateBlogArchive(IRepository<BlogPartArchiveRecord> blogArchiveRepository, BlogPostPart blogPostPart) {
|
||||
blogArchiveRepository.Flush();
|
||||
private void IncreaseBlogArchive(BlogPostPart blogPostPart) {
|
||||
_blogArchiveRepository.Flush();
|
||||
|
||||
var commonPart = blogPostPart.As<CommonPart>();
|
||||
if(commonPart == null || !commonPart.CreatedUtc.HasValue)
|
||||
return;
|
||||
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) ? _previousCreatedUtc[blogPostPart] : DateTime.MinValue;
|
||||
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 = newCreatedUtc.HasValue ? TimeZoneInfo.ConvertTimeFromUtc(newCreatedUtc.Value, timeZone) : newCreatedUtc;
|
||||
newCreatedUtc = TimeZoneInfo.ConvertTimeFromUtc(newCreatedUtc.Value, timeZone);
|
||||
|
||||
var newMonth = newCreatedUtc.HasValue ? newCreatedUtc.Value.Month : 0;
|
||||
var newYear = newCreatedUtc.HasValue ? newCreatedUtc.Value.Year : 0;
|
||||
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) {
|
||||
@@ -87,11 +131,11 @@ namespace Orchard.Blogs.Handlers {
|
||||
}
|
||||
|
||||
// decrement previous archive record
|
||||
var previousArchiveRecord = blogArchiveRepository.Table
|
||||
.Where(x => x.BlogPart == blogPostPart.BlogPart.Record
|
||||
&& x.Month == previousMonth
|
||||
&& x.Year == previousYear)
|
||||
.FirstOrDefault();
|
||||
var previousArchiveRecord = _blogArchiveRepository
|
||||
.Table
|
||||
.FirstOrDefault(x => x.BlogPart == blogPostPart.BlogPart.Record
|
||||
&& x.Month == previousMonth
|
||||
&& x.Year == previousYear);
|
||||
|
||||
if (previousArchiveRecord != null && previousArchiveRecord.PostCount > 0) {
|
||||
previousArchiveRecord.PostCount--;
|
||||
@@ -99,20 +143,20 @@ namespace Orchard.Blogs.Handlers {
|
||||
|
||||
// if previous count is now zero, delete the record
|
||||
if (previousArchiveRecord != null && previousArchiveRecord.PostCount == 0) {
|
||||
blogArchiveRepository.Delete(previousArchiveRecord);
|
||||
_blogArchiveRepository.Delete(previousArchiveRecord);
|
||||
}
|
||||
|
||||
// increment new archive record
|
||||
var newArchiveRecord = blogArchiveRepository.Table
|
||||
.Where(x => x.BlogPart == blogPostPart.BlogPart.Record
|
||||
&& x.Month == newMonth
|
||||
&& x.Year == newYear)
|
||||
.FirstOrDefault();
|
||||
var newArchiveRecord = _blogArchiveRepository
|
||||
.Table
|
||||
.FirstOrDefault(x => x.BlogPart == blogPostPart.BlogPart.Record
|
||||
&& x.Month == newMonth
|
||||
&& x.Year == newYear);
|
||||
|
||||
// if record can't be found create it
|
||||
if (newArchiveRecord == null) {
|
||||
newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPostPart.BlogPart.Record, Year = newYear, Month = newMonth, PostCount = 0 };
|
||||
blogArchiveRepository.Create(newArchiveRecord);
|
||||
_blogArchiveRepository.Create(newArchiveRecord);
|
||||
}
|
||||
|
||||
newArchiveRecord.PostCount++;
|
||||
|
Reference in New Issue
Block a user