Refactoring Rules features

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros 2011-09-10 10:31:07 -07:00
parent 5b76da2837
commit cab247320a
11 changed files with 234 additions and 40 deletions

View File

@ -1,3 +1,3 @@
4d1cc7eaae172f400159e64a747fac69d9608aa8 src/Orchard.Web/Modules/Orchard.Forms
19fadd2cddbac89051c0e6fa0c8f6a2bc8d67b24 src/Orchard.Web/Modules/Orchard.Rules
7b7a9141401137d855a5a33d1652aa51a5636bd8 src/Orchard.Web/Modules/Orchard.Tokens
30c41b905ee07be02fe134f72c7a2449d85d71b3 src/Orchard.Web/Modules/Orchard.Forms
bdfbf617bbd125d3e76689fba17999c76b4b8ac0 src/Orchard.Web/Modules/Orchard.Rules
6093698fc61157076a8c26653a07e27da5280cfd src/Orchard.Web/Modules/Orchard.Tokens

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Environment.Extensions;
using Orchard.Events;
namespace Orchard.Core.Contents.Handlers {
public interface IRulesManager : IEventHandler
{
void TriggerEvent(string category, string type, Func<Dictionary<string, object>> tokensContext);
}
[OrchardFeature("Orchard.Core.Contents.Rules")]
public class RulePartHandler : ContentHandler
{
public RulePartHandler(IRulesManager rulesManager)
{
OnPublished<ContentPart>(
(context, part) =>
rulesManager.TriggerEvent("Content", "Published",
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
OnPublished<ContentPart>(
(context, part) =>
rulesManager.TriggerEvent("Content", "Published",
() => new Dictionary<string, object> { { "Content", context.ContentItem } } ));
OnRemoved<ContentPart>(
(context, part) =>
rulesManager.TriggerEvent("Content", "Removed",
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
OnVersioned<ContentPart>(
(context, part1, part2) =>
rulesManager.TriggerEvent("Content", "Versioned",
() => new Dictionary<string, object> { { "Content", part1.ContentItem } }));
OnCreated<ContentPart>(
(context, part) =>
rulesManager.TriggerEvent("Content", "Created",
() => new Dictionary<string, object> { { "Content", context.ContentItem } }));
}
}
}

View File

@ -5,5 +5,13 @@ Website: http://orchardproject.net
Version: 1.2.0
OrchardVersion: 1.2.0
Description: The contents module enables the creation of custom content types.
FeatureDescription: Default custom content type definition, creation and management.
Category: Core
Features:
Orchard.Core.Contents
Name: Contents
Description: Default custom content type definition, creation and management.
Category: Core
Orchard.Core.Contents.Rules:
Name: Contents Rules
Description: Rules for the Contents modules
Category: Rules
Dependency: Rules

View File

@ -0,0 +1,64 @@
using System;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;
using Orchard.Events;
using Orchard.Localization;
namespace Orchard.Core.Contents.Rules
{
public interface IEventProvider : IEventHandler
{
void Describe(dynamic describe);
}
[OrchardFeature("Orchard.Core.Contents.Rules")]
public class ContentEvents : IEventProvider
{
public Localizer T { get; set; }
public void Describe(dynamic describe)
{
Func<dynamic, bool> contentHasPart = ContentHasPart;
describe.For("Content", T("Content Items"), T("Content Items"))
.Element("Created", T("Content Created"), T("Content is actually created."), contentHasPart, (Func<dynamic, LocalizedString>) (context => T("When content with types ({0}) is created.", FormatPartsList(context))), "SelectContentTypes")
.Element("Versioned", T("Content Versioned"), T("Content is actually versioned."), contentHasPart, (Func<dynamic, LocalizedString>) (context => T("When content with types ({0}) is versioned.", FormatPartsList(context))), "SelectContentTypes")
.Element("Published", T("Content Published"), T("Content is actually published."), contentHasPart, (Func<dynamic, LocalizedString>) (context => T("When content with types ({0}) is published.", FormatPartsList(context))), "SelectContentTypes")
.Element("Removed", T("Content Removed"), T("Content is actually removed."), contentHasPart, (Func<dynamic, LocalizedString>) (context => T("When content with types ({0}) is removed.", FormatPartsList(context))), "SelectContentTypes");
}
private string FormatPartsList(dynamic context)
{
var contenttypes = context.Properties["contenttypes"];
if(String.IsNullOrEmpty(contenttypes))
{
return T("Any").Text;
}
return contenttypes;
}
private static bool ContentHasPart(dynamic context)
{
string contenttypes = context.Properties["contenttypes"];
var content = context.Tokens["Content"] as IContent;
// "" means 'any'
if(String.IsNullOrEmpty(contenttypes))
{
return true;
}
if(content == null)
{
return false;
}
var contentTypes = contenttypes.Split(new [] {','} );
return contentTypes.Any(contentType => content.ContentItem.TypeDefinition.Name == contentType);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Web.Mvc;
using Orchard.ContentManagement.MetaData;
using Orchard.DisplayManagement;
using Orchard.Environment.Extensions;
using Orchard.Events;
using Orchard.Localization;
namespace Orchard.Core.Contents.Rules
{
public interface IFormProvider : IEventHandler
{
void Describe(dynamic context);
}
[OrchardFeature("Orchard.Core.Contents.Rules")]
public class ContentForms : IFormProvider
{
private readonly IContentDefinitionManager _contentDefinitionManager;
protected dynamic Shape { get; set; }
public Localizer T { get; set; }
public ContentForms(
IShapeFactory shapeFactory,
IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
Shape = shapeFactory;
}
public void Describe(dynamic context)
{
Func<IShapeFactory, dynamic> form =
shape => {
var f = Shape.Form(
Id: "AnyOfContentTypes",
_Parts: Shape.SelectList(
Id: "contenttypes", Name: "contenttypes",
Title: T("Content types"),
Description: T("Select some content types."),
Size: 10,
Multiple: true
)
);
f._Parts.Add(new SelectListItem { Value = "", Text = T("Any").Text });
foreach (var contentType in _contentDefinitionManager.ListTypeDefinitions())
{
f._Parts.Add(new SelectListItem { Value = contentType.Name, Text = contentType.DisplayName });
}
return f;
};
context.Form("SelectContentTypes", form);
}
}
}

View File

@ -103,6 +103,9 @@
<Compile Include="Containers\ViewModels\ContainableViewModel.cs" />
<Compile Include="Containers\ViewModels\ContainerWidgetViewModel.cs" />
<Compile Include="Containers\ViewModels\ContainerViewModel.cs" />
<Compile Include="Contents\Handlers\RulesHandler.cs" />
<Compile Include="Contents\Rules\ContentEvents.cs" />
<Compile Include="Contents\Rules\ContentForms.cs" />
<Compile Include="Contents\Security\AuthorizationEventHandler.cs" />
<Compile Include="Common\Services\BbcodeFilter.cs" />
<Compile Include="Common\Services\ICommonService.cs" />

View File

@ -6,13 +6,13 @@ Version: 1.2.0
OrchardVersion: 1.2.0
Description: The comments system implemented by this module can be applied to arbitrary Orchard content types, such as blogs and pages. It includes comment validation and spam protection through the Akismet service.
Features:
Orchard.Comments:
Name: Comments
Description: Standard content item comments.
Dependencies: Settings
Category: Social
Orchard.Comments.Rules:
Name: Comments Rules
Description: Rules for the Comments modules
Dependencies: Orchard.Rules, Orchard.Forms
Category: Rules
Orchard.Comments:
Name: Comments
Description: Standard content item comments.
Dependencies: Settings
Category: Social
Orchard.Comments.Rules:
Name: Comments Rules
Description: Rules for the Comments modules
Category: Rules
Dependency: Rules

View File

@ -117,14 +117,6 @@
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
<Name>Orchard.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Forms\Orchard.Forms.csproj">
<Project>{642A49D7-8752-4177-80D6-BFBBCFAD3DE0}</Project>
<Name>Orchard.Forms</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Rules\Orchard.Rules.csproj">
<Project>{966EC390-3C7F-4D98-92A6-F0F30D02E9B1}</Project>
<Name>Orchard.Rules</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Views\Admin\Edit.cshtml" />

View File

@ -32,3 +32,4 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.2.0")]
[assembly: AssemblyFileVersion("1.2.0")]
[assembly:SecurityTransparent]

View File

@ -1,13 +1,17 @@
using System;
using Orchard.Comments.Models;
using Orchard.Environment.Extensions;
using Orchard.Rules.Models;
using Orchard.Rules.Services;
using Orchard.Events;
using Orchard.ContentManagement;
using Orchard.Localization;
namespace Orchard.Comments.Rules
{
public interface IActionProvider : IEventHandler
{
void Describe(dynamic describe);
}
[OrchardFeature("Orchard.Comments.Rules")]
public class CommentsActions : IActionProvider
{
@ -20,13 +24,18 @@ namespace Orchard.Comments.Rules
public Localizer T { get; set; }
public void Describe(DescribeActionContext context)
public void Describe(dynamic describe)
{
context.For("Comments", T("Comments"), T("Comments"))
.Element("Close", T("Close Comments"), T("Closes comments on a content item."), Close, actionContext => T("Close comments"), "ActionCloseComments");
Func<dynamic, LocalizedString> display = context => T("Close comments");
describe.For("Comments", T("Comments"), T("Comments"))
.Element("Close", T("Close Comments"), T("Closes comments on a content item."), (Action<dynamic>) Close, display, "ActionCloseComments");
}
private void Close(ActionContext context)
/// <summary>
/// Closes the comments on the content represented by "ContentId"
/// </summary>
private void Close(dynamic context)
{
var contentId = Convert.ToInt32(context.Properties["ContentId"]);
var content = _contentManager.Get(contentId);

View File

@ -1,10 +1,16 @@
using Orchard.DisplayManagement;
using System;
using Orchard.DisplayManagement;
using Orchard.Environment.Extensions;
using Orchard.Forms.Services;
using Orchard.Events;
using Orchard.Localization;
namespace Orchard.Comments.Rules
{
public interface IFormProvider : IEventHandler
{
void Describe(dynamic context);
}
[OrchardFeature("Orchard.Comments.Rules")]
public class CommentsForms : IFormProvider
{
@ -16,17 +22,19 @@ namespace Orchard.Comments.Rules
Shape = shapeFactory;
}
public void Describe(DescribeContext context)
public void Describe(dynamic context)
{
context.Form("ActionCloseComments",
Func<IShapeFactory, dynamic> form =
shape => Shape.Form(
Id: "ActionCloseComments",
_Type: Shape.Textbox(
Id: "ContentId", Name: "ContentId",
Title: T("Content Item Id"),
Description: T("Content Item Id."))
)
);
Id: "ActionCloseComments",
_Type: Shape.Textbox(
Id: "ContentId", Name: "ContentId",
Title: T("Content Item Id"),
Description: T("Content Item Id."))
);
context.Form("ActionCloseComments", form);
}
}
}