Adding publish date, version-specific create/modify/publish dates, and unpublish method on content manager

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045209
This commit is contained in:
loudej
2010-01-10 11:18:28 +00:00
parent a4fcbb482c
commit 864dd42a08
30 changed files with 456 additions and 136 deletions

View File

@@ -10,24 +10,9 @@ namespace Orchard.Core.Common.Models {
private readonly LazyField<IUser> _owner = new LazyField<IUser>();
private readonly LazyField<IContent> _container = new LazyField<IContent>();
public LazyField<IUser> OwnerField {
get { return _owner; }
}
public LazyField<IContent> ContainerField {
get { return _container; }
}
public LazyField<IUser> OwnerField { get { return _owner; } }
DateTime? ICommonAspect.CreatedUtc {
get { return Record.CreatedUtc;}
set { Record.CreatedUtc = value;}
}
DateTime? ICommonAspect.ModifiedUtc {
get { return Record.ModifiedUtc;}
set { Record.ModifiedUtc = value;}
}
public LazyField<IContent> ContainerField { get { return _container; } }
public IUser Owner {
get { return _owner.Value; }
@@ -38,5 +23,60 @@ namespace Orchard.Core.Common.Models {
get { return _container.Value; }
set { _container.Value = value; }
}
public DateTime? CreatedUtc {
get { return Record.CreatedUtc; }
set { Record.CreatedUtc = value; }
}
public DateTime? PublishedUtc {
get { return Record.PublishedUtc; }
set { Record.PublishedUtc = value; }
}
public DateTime? ModifiedUtc {
get { return Record.ModifiedUtc; }
set { Record.ModifiedUtc = value; }
}
CommonVersionRecord VersionRecord {
get {
var versionPart = this.As<ContentPart<CommonVersionRecord>>();
return versionPart == null ? null : versionPart.Record;
}
}
public DateTime? VersionCreatedUtc {
get {
return VersionRecord == null ? CreatedUtc : VersionRecord.CreatedUtc;
}
set {
if (VersionRecord != null) {
VersionRecord.CreatedUtc = value;
}
}
}
public DateTime? VersionPublishedUtc {
get {
return VersionRecord == null ? PublishedUtc : VersionRecord.PublishedUtc;
}
set {
if (VersionRecord != null) {
VersionRecord.PublishedUtc = value;
}
}
}
public DateTime? VersionModifiedUtc {
get {
return VersionRecord == null ? ModifiedUtc : VersionRecord.ModifiedUtc;
}
set {
if (VersionRecord != null) {
VersionRecord.ModifiedUtc = value;
}
}
}
}
}

View File

@@ -1,17 +1,17 @@
using System;
using JetBrains.Annotations;
using Orchard.Core.Common.Models;
using Orchard.Core.Common.Records;
using Orchard.Core.Common.ViewModels;
using Orchard.Data;
using Orchard.Localization;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.ViewModels;
using Orchard.Security;
using Orchard.Services;
namespace Orchard.Core.Common.Providers {
[UsedImplicitly]
public class CommonAspectHandler : ContentHandler {
private readonly IClock _clock;
private readonly IAuthenticationService _authenticationService;
@@ -20,7 +20,8 @@ namespace Orchard.Core.Common.Providers {
private readonly IContentManager _contentManager;
public CommonAspectHandler(
IRepository<CommonRecord> repository,
IRepository<CommonRecord> commonRepository,
IRepository<CommonVersionRecord> commonVersionRepository,
IClock clock,
IAuthenticationService authenticationService,
IAuthorizationService authorizationService,
@@ -34,10 +35,16 @@ namespace Orchard.Core.Common.Providers {
_contentManager = contentManager;
T = NullLocalizer.Instance;
Filters.Add(StorageFilter.For(repository));
Filters.Add(StorageFilter.For(commonRepository));
Filters.Add(StorageFilter.For(commonVersionRepository));
OnActivated<CommonAspect>(PropertySetHandlers);
OnActivated<CommonAspect>(DefaultTimestampsAndOwner);
OnActivated<CommonAspect>(AssignCreatingOwner);
OnActivated <ContentPart<CommonRecord>>(AssignCreatingDates);
OnActivated<ContentPart<CommonVersionRecord>>(AssignCreatingDates);
OnVersioned<ContentPart<CommonVersionRecord>>(AssignVersioningDates);
OnPublishing<ContentPart<CommonRecord>>(AssignPublishingDates);
OnPublishing<ContentPart<CommonVersionRecord>>(AssignPublishingDates);
OnLoaded<CommonAspect>(LazyLoadHandlers);
//OnGetDisplayViewModel<CommonAspect>();
@@ -47,21 +54,53 @@ namespace Orchard.Core.Common.Providers {
public Localizer T { get; set; }
void DefaultTimestampsAndOwner(ActivatedContentContext context, CommonAspect instance) {
// assign default create/modified dates
if (instance.Record.CreatedUtc == null) {
instance.Record.CreatedUtc = _clock.UtcNow;
}
if (instance.Record.ModifiedUtc == null) {
instance.Record.ModifiedUtc = _clock.UtcNow;
}
void AssignCreatingOwner(ActivatedContentContext context, CommonAspect part) {
// and use the current user as Owner
if (instance.Record.OwnerId == 0) {
((ICommonAspect)instance).Owner = _authenticationService.GetAuthenticatedUser();
if (part.Record.OwnerId == 0) {
part.Owner = _authenticationService.GetAuthenticatedUser();
}
}
void AssignCreatingDates(ActivatedContentContext context, ContentPart<CommonRecord> part) {
// assign default create/modified dates
part.Record.CreatedUtc = _clock.UtcNow;
part.Record.ModifiedUtc = _clock.UtcNow;
}
void AssignCreatingDates(ActivatedContentContext context, ContentPart<CommonVersionRecord> part) {
// assign default create/modified dates
part.Record.CreatedUtc = _clock.UtcNow;
part.Record.ModifiedUtc = _clock.UtcNow;
}
void AssignVersioningDates(VersionContentContext context, ContentPart<CommonVersionRecord> existing, ContentPart<CommonVersionRecord> building) {
// assign create/modified dates for the new version
building.Record.CreatedUtc = _clock.UtcNow;
building.Record.ModifiedUtc = _clock.UtcNow;
// publish date should be null until publish method called
building.Record.PublishedUtc = null;
}
void AssignPublishingDates(PublishContentContext context, ContentPart<CommonRecord> part) {
// don't assign dates when unpublishing
if (context.PublishingItemVersionRecord == null)
return;
// assign version-agnostic publish date
part.Record.PublishedUtc = _clock.UtcNow;
}
void AssignPublishingDates(PublishContentContext context, ContentPart<CommonVersionRecord> part) {
// don't assign dates when unpublishing
if (context.PublishingItemVersionRecord == null)
return;
// assign version-specific publish date
part.Record.PublishedUtc = _clock.UtcNow;
}
void LazyLoadHandlers(LoadContentContext context, CommonAspect aspect) {
// add handlers that will load content for id's just-in-time
aspect.OwnerField.Loader(() => _contentManager.Get<IUser>(aspect.Record.OwnerId));
@@ -109,7 +148,8 @@ namespace Orchard.Core.Common.Providers {
private void UpdateEditor(UpdateEditorModelContext context, CommonAspect instance) {
// this event is hooked so the modified timestamp is changed when an edit-post occurs.
// kind of a loose rule of thumb. may not be sufficient
instance.Record.ModifiedUtc = _clock.UtcNow;
instance.ModifiedUtc = _clock.UtcNow;
instance.VersionModifiedUtc = _clock.UtcNow;
var currentUser = _authenticationService.GetAuthenticatedUser();
if (!_authorizationService.CheckAccess(currentUser, Permissions.ChangeOwner)) {

View File

@@ -6,6 +6,7 @@ namespace Orchard.Core.Common.Records {
public virtual int OwnerId { get; set; }
public virtual ContentItemRecord Container { get; set; }
public virtual DateTime? CreatedUtc { get; set; }
public virtual DateTime? PublishedUtc { get; set; }
public virtual DateTime? ModifiedUtc { get; set; }
}
}

View File

@@ -0,0 +1,10 @@
using System;
using Orchard.ContentManagement.Records;
namespace Orchard.Core.Common.Records {
public class CommonVersionRecord : ContentPartVersionRecord {
public virtual DateTime? CreatedUtc { get; set; }
public virtual DateTime? PublishedUtc { get; set; }
public virtual DateTime? ModifiedUtc { get; set; }
}
}

View File

@@ -63,6 +63,7 @@
<ItemGroup>
<Compile Include="Common\Controllers\BodyDriver.cs" />
<Compile Include="Common\Permissions.cs" />
<Compile Include="Common\Records\CommonVersionRecord.cs" />
<Compile Include="Common\Utilities\LazyField.cs" />
<Compile Include="Common\Providers\CommonAspectHandler.cs" />
<Compile Include="Common\Models\CommonAspect.cs" />