From d3aca80447c4feabbe4e6e5f994c2601fa3e588e Mon Sep 17 00:00:00 2001 From: Matteo Piovanelli Date: Thu, 17 Dec 2020 19:05:21 +0100 Subject: [PATCH] =?UTF-8?q?Added=20an=20event=20activity=20for=20workflows?= =?UTF-8?q?=20that=20activates=20on=20the=20first=20Upd=E2=80=A6=20(#8438)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Activities/ContentActivity.cs | 10 ++++++ .../Handlers/WorkflowContentHandler.cs | 35 +++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs b/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs index eb69fe0ba..67c8437bb 100644 --- a/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs +++ b/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs @@ -78,6 +78,16 @@ namespace Orchard.Workflows.Activities { } } + public class ContentFirstUpdatedActivity : ContentActivity { + public override string Name { + get { return "ContentFirstUpdated"; } + } + + public override LocalizedString Description { + get { return T("Content is updated for the first time."); } + } + } + public class ContentPublishedActivity : ContentActivity { public override string Name { get { return "ContentPublished"; } diff --git a/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs b/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs index 378b83e0a..76a32f42b 100644 --- a/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs @@ -6,8 +6,16 @@ using Orchard.Workflows.Services; namespace Orchard.Workflows.Handlers { public class WorkflowContentHandler : ContentHandler { + // Used to memorize the ids of ContentItems for which we go through the + // OnCreated handler. + private HashSet _createdItems; + // Used to memorize the ids of ContentItems for which we go through the + // OnUpdated handler. + private HashSet _updatedItems; public WorkflowContentHandler(IWorkflowManager workflowManager) { + _createdItems = new HashSet(); + _updatedItems = new HashSet(); OnPublished( (context, part) => @@ -34,9 +42,15 @@ namespace Orchard.Workflows.Handlers { () => new Dictionary { { "Content", context.BuildingContentItem } })); OnCreated( - (context, part) => + (context, part) => { + + if (context.ContentItem != null) { // sanity check + _createdItems.Add(context.ContentItem.Id); + } + workflowManager.TriggerEvent("ContentCreated", context.ContentItem, - () => new Dictionary { { "Content", context.ContentItem } })); + () => new Dictionary { { "Content", context.ContentItem } }); + }); OnUpdated( (context, part) => { @@ -44,11 +58,28 @@ namespace Orchard.Workflows.Handlers { return; } + if (context.ContentItem != null) { // sanity check + if (!_updatedItems.Contains(context.ContentItem.Id)) { + // in case a further update is invoked, this would prevent + // the FirstUpdate event to be fired again + _updatedItems.Add(context.ContentItem.Id); + if (_createdItems.Contains(context.ContentItem.Id)) { + // first update after creation of item + workflowManager.TriggerEvent( + "ContentFirstUpdated", + context.ContentItem, + () => new Dictionary { { "Content", context.ContentItem } } + ); + } + } + } + workflowManager.TriggerEvent( "ContentUpdated", context.ContentItem, () => new Dictionary { { "Content", context.ContentItem } } ); + }); } }