mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-19 18:27:55 +08:00
Working on providing support for some common aspects.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041960
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using Orchard.Models;
|
||||
|
||||
namespace Orchard.Core.Common.Models {
|
||||
public class BodyPart : Orchard.Models.ContentPart {
|
||||
public class BodyAspect : ContentPart {
|
||||
public string Body { get; set; }
|
||||
public string Format { get; set; }
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
using Orchard.Models.Driver;
|
||||
|
||||
namespace Orchard.Core.Common.Models {
|
||||
public class BodyPartProvider : ContentProvider {
|
||||
|
||||
}
|
||||
}
|
37
src/Orchard.Web/Core/Common/Models/CommonAspect.cs
Normal file
37
src/Orchard.Web/Core/Common/Models/CommonAspect.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Core.Common.Utilities;
|
||||
using Orchard.Models;
|
||||
using Orchard.Security;
|
||||
|
||||
namespace Orchard.Core.Common.Models {
|
||||
public class CommonAspect : ContentPart<CommonRecord> {
|
||||
private readonly LazyField<IUser> _owner = new LazyField<IUser>();
|
||||
private readonly LazyField<IContent> _container = new LazyField<IContent>();
|
||||
|
||||
public IUser Owner {
|
||||
get { return _owner.Value; }
|
||||
set {_owner.Value = value;}
|
||||
}
|
||||
|
||||
public IContent Container {
|
||||
get { return _container.Value; }
|
||||
set {_container.Value = value;}
|
||||
}
|
||||
|
||||
internal void OnGetOwner(Func<IUser> loader) {
|
||||
_owner.Loader(loader);
|
||||
}
|
||||
internal void OnSetOwner(Func<IUser,IUser> setter) {
|
||||
_owner.Setter(setter);
|
||||
}
|
||||
|
||||
|
||||
internal void OnGetContainer(Func<IContent> loader) {
|
||||
_container.Loader(loader);
|
||||
}
|
||||
internal void OnSetContainer(Func<IContent, IContent> setter) {
|
||||
_container.Setter(setter);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,51 +0,0 @@
|
||||
using System;
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Models;
|
||||
using Orchard.Security;
|
||||
|
||||
namespace Orchard.Core.Common.Models {
|
||||
public class CommonPart : ContentPart<CommonRecord> {
|
||||
private readonly Lazy<IUser> _owner = new Lazy<IUser>();
|
||||
private readonly Lazy<IContent> _container = new Lazy<IContent>();
|
||||
|
||||
public IUser Owner {
|
||||
get { return _owner.Value; }
|
||||
set { _owner.Value = value; }
|
||||
}
|
||||
|
||||
public IContent Container {
|
||||
get { return _container.Value; }
|
||||
set { _container.Value = value; }
|
||||
}
|
||||
|
||||
public void LoadOwner(Func<IUser> loader) {
|
||||
_owner.Loader(loader);
|
||||
}
|
||||
public void LoadContainer(Func<IContent> loader) {
|
||||
_container.Loader(loader);
|
||||
}
|
||||
}
|
||||
|
||||
public class Lazy<T> {
|
||||
private Func<T> _loader;
|
||||
private T _value;
|
||||
|
||||
public void Loader(Func<T> loader) {
|
||||
_loader = loader;
|
||||
}
|
||||
|
||||
public T Value {
|
||||
get {
|
||||
if (_loader != null) {
|
||||
_value = _loader();
|
||||
_loader = null;
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
set {
|
||||
_value = value;
|
||||
_loader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,56 +0,0 @@
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.Models;
|
||||
using Orchard.Models.Driver;
|
||||
using Orchard.Security;
|
||||
using Orchard.Services;
|
||||
|
||||
namespace Orchard.Core.Common.Models {
|
||||
public class CommonPartProvider : ContentProvider {
|
||||
private readonly IClock _clock;
|
||||
private readonly IAuthenticationService _authenticationService;
|
||||
private readonly IContentManager _contentManager;
|
||||
|
||||
public CommonPartProvider(
|
||||
IRepository<CommonRecord> repository,
|
||||
IClock clock,
|
||||
IAuthenticationService authenticationService,
|
||||
IContentManager contentManager) {
|
||||
|
||||
_clock = clock;
|
||||
_authenticationService = authenticationService;
|
||||
_contentManager = contentManager;
|
||||
|
||||
AddOnCreating<CommonPart>(SetCreateTimesAndAuthor);
|
||||
Filters.Add(new StorageFilter<CommonRecord>(repository));
|
||||
AddOnLoaded<CommonPart>(LoadOwnerModel);
|
||||
}
|
||||
|
||||
void SetCreateTimesAndAuthor(CreateContentContext context, CommonPart instance) {
|
||||
if (instance.Record.CreatedUtc == null) {
|
||||
instance.Record.CreatedUtc = _clock.UtcNow;
|
||||
}
|
||||
if (instance.Record.ModifiedUtc == null) {
|
||||
instance.Record.ModifiedUtc = _clock.UtcNow;
|
||||
}
|
||||
if (instance.Record.OwnerId == 0) {
|
||||
instance.Owner = _authenticationService.GetAuthenticatedUser();
|
||||
if (instance.Owner != null)
|
||||
instance.Record.OwnerId = instance.Owner.Id;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpdateEditors(UpdateContentContext context) {
|
||||
var part = context.ContentItem.As<CommonPart>();
|
||||
if (part==null)
|
||||
return;
|
||||
|
||||
part.Record.ModifiedUtc = _clock.UtcNow;
|
||||
}
|
||||
|
||||
void LoadOwnerModel(LoadContentContext context, CommonPart part) {
|
||||
part.LoadOwner(() => _contentManager.Get<IUser>(part.Record.OwnerId));
|
||||
part.LoadContainer(() => part.Record.Container == null ? null : _contentManager.Get(part.Record.Container.Id));
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,6 +2,6 @@ using Orchard.Core.Common.Records;
|
||||
using Orchard.Models;
|
||||
|
||||
namespace Orchard.Core.Common.Models {
|
||||
public class RoutablePart : ContentPart<RoutableRecord> {
|
||||
public class RoutableAspect : ContentPart<RoutableRecord> {
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.Models.Driver;
|
||||
|
||||
namespace Orchard.Core.Common.Models {
|
||||
public class RoutablePartProvider : ContentProvider {
|
||||
public RoutablePartProvider(IRepository<RoutableRecord> repository) {
|
||||
Filters.Add(new StorageFilter<RoutableRecord>(repository));
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
using Orchard.Models.Driver;
|
||||
|
||||
namespace Orchard.Core.Common.Providers {
|
||||
public class BodyAspectProvider : ContentProvider {
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,90 @@
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.Models;
|
||||
using Orchard.Models.Driver;
|
||||
using Orchard.Security;
|
||||
using Orchard.Services;
|
||||
|
||||
namespace Orchard.Core.Common.Providers {
|
||||
public class CommonAspectProvider : ContentProvider {
|
||||
private readonly IClock _clock;
|
||||
private readonly IAuthenticationService _authenticationService;
|
||||
private readonly IContentManager _contentManager;
|
||||
|
||||
public CommonAspectProvider(
|
||||
IRepository<CommonRecord> repository,
|
||||
IClock clock,
|
||||
IAuthenticationService authenticationService,
|
||||
IContentManager contentManager) {
|
||||
|
||||
_clock = clock;
|
||||
_authenticationService = authenticationService;
|
||||
_contentManager = contentManager;
|
||||
|
||||
Filters.Add(new StorageFilter<CommonRecord>(repository));
|
||||
AddOnCreating<CommonAspect>(DefaultTimestampsAndOwner);
|
||||
AddOnLoaded<CommonAspect>(LazyLoadHandlers);
|
||||
AddOnActivated<CommonAspect>(PropertySetHandlers);
|
||||
}
|
||||
|
||||
void DefaultTimestampsAndOwner(CreateContentContext 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;
|
||||
}
|
||||
|
||||
// and use the current user as Owner
|
||||
if (instance.Record.OwnerId == 0) {
|
||||
instance.Owner = _authenticationService.GetAuthenticatedUser();
|
||||
if (instance.Owner != null)
|
||||
instance.Record.OwnerId = instance.Owner.Id;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
aspect.ContainerField.Loader(() => aspect.Record.Container == null ? null : _contentManager.Get(aspect.Record.Container.Id));
|
||||
}
|
||||
|
||||
static void PropertySetHandlers(ActivatedContentContext context, CommonAspect aspect) {
|
||||
// add handlers that will update records when aspect properties are set
|
||||
|
||||
aspect.OwnerField.Setter(user => {
|
||||
if (user == null) {
|
||||
aspect.Record.OwnerId = 0;
|
||||
}
|
||||
else {
|
||||
aspect.Record.OwnerId = user.ContentItem.Id;
|
||||
}
|
||||
return user;
|
||||
});
|
||||
|
||||
aspect.ContainerField.Setter(container => {
|
||||
if (container == null) {
|
||||
aspect.Record.Container = null;
|
||||
}
|
||||
else {
|
||||
aspect.Record.Container = container.ContentItem.Record;
|
||||
}
|
||||
return container;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected override void UpdateEditors(UpdateContentContext context) {
|
||||
var part = context.ContentItem.As<CommonAspect>();
|
||||
if (part == null)
|
||||
return;
|
||||
|
||||
// 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
|
||||
part.Record.ModifiedUtc = _clock.UtcNow;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.Models.Driver;
|
||||
|
||||
namespace Orchard.Core.Common.Providers {
|
||||
public class RoutableAspectProvider : ContentProvider {
|
||||
public RoutableAspectProvider(IRepository<RoutableRecord> repository) {
|
||||
Filters.Add(new StorageFilter<RoutableRecord>(repository));
|
||||
}
|
||||
}
|
||||
}
|
40
src/Orchard.Web/Core/Common/Utilities/LazyField.cs
Normal file
40
src/Orchard.Web/Core/Common/Utilities/LazyField.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace Orchard.Core.Common.Utilities {
|
||||
public class LazyField<T> {
|
||||
private T _value;
|
||||
private Func<T> _loader;
|
||||
private Func<T, T> _setter;
|
||||
|
||||
public T Value {
|
||||
get {return GetValue();}
|
||||
set {SetValue(value);}
|
||||
}
|
||||
|
||||
public void Loader(Func<T> loader) {
|
||||
_loader = loader;
|
||||
}
|
||||
|
||||
public void Setter(Func<T, T> setter) {
|
||||
_setter = setter;
|
||||
}
|
||||
|
||||
private T GetValue() {
|
||||
if (_loader != null) {
|
||||
_value = _loader();
|
||||
_loader = null;
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
private void SetValue(T value) {
|
||||
_loader = null;
|
||||
if (_setter != null) {
|
||||
_value = _setter(value);
|
||||
}
|
||||
else {
|
||||
_value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -61,12 +61,13 @@
|
||||
<Reference Include="System.Web.Mobile" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Common\Models\CommonPartProvider.cs" />
|
||||
<Compile Include="Common\Models\CommonPart.cs" />
|
||||
<Compile Include="Common\Models\BodyPartProvider.cs" />
|
||||
<Compile Include="Common\Models\BodyPart.cs" />
|
||||
<Compile Include="Common\Models\RoutablePart.cs" />
|
||||
<Compile Include="Common\Models\RoutablePartProvider.cs" />
|
||||
<Compile Include="Common\Utilities\LazyField.cs" />
|
||||
<Compile Include="Common\Providers\CommonAspectProvider.cs" />
|
||||
<Compile Include="Common\Models\CommonAspect.cs" />
|
||||
<Compile Include="Common\Providers\BodyAspectProvider.cs" />
|
||||
<Compile Include="Common\Models\BodyAspect.cs" />
|
||||
<Compile Include="Common\Models\RoutableAspect.cs" />
|
||||
<Compile Include="Common\Providers\RoutableAspectProvider.cs" />
|
||||
<Compile Include="Common\Records\CommonRecord.cs" />
|
||||
<Compile Include="Common\Records\RoutableRecord.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@@ -47,7 +47,7 @@ namespace Orchard.Sandbox.Controllers
|
||||
public ActionResult Create(PageCreateViewModel model) {
|
||||
var page = _contentManager.Create<SandboxPage>("sandboxpage", item => {
|
||||
item.Record.Name = model.Name;
|
||||
item.As<CommonPart>().Container = CurrentSite.ContentItem;
|
||||
item.As<CommonAspect>().Container = CurrentSite.ContentItem;
|
||||
});
|
||||
return RedirectToAction("show", new { page.ContentItem.Id });
|
||||
}
|
||||
|
@@ -11,9 +11,9 @@ namespace Orchard.Sandbox.Models {
|
||||
|
||||
// define the "sandboxpage" content type
|
||||
Filters.Add(new ActivatingFilter<SandboxPage>("sandboxpage"));
|
||||
Filters.Add(new ActivatingFilter<CommonPart>("sandboxpage"));
|
||||
Filters.Add(new ActivatingFilter<RoutablePart>("sandboxpage"));
|
||||
Filters.Add(new ActivatingFilter<BodyPart>("sandboxpage"));
|
||||
Filters.Add(new ActivatingFilter<CommonAspect>("sandboxpage"));
|
||||
Filters.Add(new ActivatingFilter<RoutableAspect>("sandboxpage"));
|
||||
Filters.Add(new ActivatingFilter<BodyAspect>("sandboxpage"));
|
||||
Filters.Add(new StorageFilter<SandboxPageRecord>(pageRepository) { AutomaticallyCreateMissingRecord = true });
|
||||
|
||||
// add settings to site, and simple record-template gui
|
||||
|
Reference in New Issue
Block a user