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

@@ -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" />