From 926ea54c393dc80d1531224a5d0b1ac4c76e3c5c Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 27 Jul 2015 18:04:20 +0200 Subject: [PATCH] Developers can cancel an event with the CancelOperation method of the event with a custom message which will be used in place of the normal default cancellation message --- .../Events/CancellableEventArgs.cs | 11 ++ src/Umbraco.Core/Events/EventMessage.cs | 5 + src/Umbraco.Web/Editors/ContentController.cs | 116 +++++++++--------- .../Editors/ContentControllerBase.cs | 10 ++ src/Umbraco.Web/Editors/MediaController.cs | 4 +- 5 files changed, 83 insertions(+), 63 deletions(-) diff --git a/src/Umbraco.Core/Events/CancellableEventArgs.cs b/src/Umbraco.Core/Events/CancellableEventArgs.cs index c4eeb85ade..506ba1c22e 100644 --- a/src/Umbraco.Core/Events/CancellableEventArgs.cs +++ b/src/Umbraco.Core/Events/CancellableEventArgs.cs @@ -63,6 +63,17 @@ namespace Umbraco.Core.Events } } + /// + /// if this instance supports cancellation, this will set Cancel to true with an affiliated cancellation message + /// + /// + public void CancelOperation(EventMessage cancelationMessage) + { + Cancel = true; + cancelationMessage.IsDefaultEventMessage = true; + Messages.Add(cancelationMessage); + } + /// /// Returns the EventMessages object which is used to add messages to the message collection for this event /// diff --git a/src/Umbraco.Core/Events/EventMessage.cs b/src/Umbraco.Core/Events/EventMessage.cs index 98acc88cd9..473f955d45 100644 --- a/src/Umbraco.Core/Events/EventMessage.cs +++ b/src/Umbraco.Core/Events/EventMessage.cs @@ -18,5 +18,10 @@ namespace Umbraco.Core.Events public string Category { get; private set; } public string Message { get; private set; } public EventMessageType MessageType { get; private set; } + + /// + /// This is used to track if this message should be used as a default message so that Umbraco doesn't also append it's own default messages + /// + internal bool IsDefaultEventMessage { get; set; } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index ab72649fcf..76ab46ee22 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -288,9 +288,7 @@ namespace Umbraco.Web.Editors } else { - display.AddWarningNotification( - Services.TextService.Localize("speechBubbles/operationCancelledHeader"), - Services.TextService.Localize("speechBubbles/operationCancelledText")); + AddCancelMessage(display); } break; case ContentSaveAction.SendPublish: @@ -303,9 +301,7 @@ namespace Umbraco.Web.Editors } else { - display.AddWarningNotification( - Services.TextService.Localize("speechBubbles/operationCancelledHeader"), - Services.TextService.Localize("speechBubbles/operationCancelledText")); + AddCancelMessage(display); } break; case ContentSaveAction.Publish: @@ -319,57 +315,7 @@ namespace Umbraco.Web.Editors return display; } - /// - /// Checks if the user is currently in preview mode and if so will update the preview content for this item - /// - /// - private void UpdatePreviewContext(int contentId) - { - var previewId = Request.GetPreviewCookieValue(); - if (previewId.IsNullOrWhiteSpace()) return; - Guid id; - if (Guid.TryParse(previewId, out id)) - { - var d = new Document(contentId); - var pc = new PreviewContent(UmbracoUser, id, false); - pc.PrepareDocument(UmbracoUser, d, true); - pc.SavePreviewSet(); - } - } - - /// - /// Maps the dto property values to the persisted model - /// - /// - private void MapPropertyValues(ContentItemSave contentItem) - { - UpdateName(contentItem); - - //TODO: We need to support 'send to publish' - - contentItem.PersistedContent.ExpireDate = contentItem.ExpireDate; - contentItem.PersistedContent.ReleaseDate = contentItem.ReleaseDate; - //only set the template if it didn't change - var templateChanged = (contentItem.PersistedContent.Template == null && contentItem.TemplateAlias.IsNullOrWhiteSpace() == false) - || (contentItem.PersistedContent.Template != null && contentItem.PersistedContent.Template.Alias != contentItem.TemplateAlias) - || (contentItem.PersistedContent.Template != null && contentItem.TemplateAlias.IsNullOrWhiteSpace()); - if (templateChanged) - { - var template = Services.FileService.GetTemplate(contentItem.TemplateAlias); - if (template == null && contentItem.TemplateAlias.IsNullOrWhiteSpace() == false) - { - //ModelState.AddModelError("Template", "No template exists with the specified alias: " + contentItem.TemplateAlias); - LogHelper.Warn("No template exists with the specified alias: " + contentItem.TemplateAlias); - } - else - { - //NOTE: this could be null if there was a template and the posted template is null, this should remove the assigned template - contentItem.PersistedContent.Template = template; - } - } - - base.MapPropertyValues(contentItem); - } + /// /// Publishes a document with a given ID @@ -567,6 +513,58 @@ namespace Umbraco.Web.Editors return content; } + /// + /// Checks if the user is currently in preview mode and if so will update the preview content for this item + /// + /// + private void UpdatePreviewContext(int contentId) + { + var previewId = Request.GetPreviewCookieValue(); + if (previewId.IsNullOrWhiteSpace()) return; + Guid id; + if (Guid.TryParse(previewId, out id)) + { + var d = new Document(contentId); + var pc = new PreviewContent(UmbracoUser, id, false); + pc.PrepareDocument(UmbracoUser, d, true); + pc.SavePreviewSet(); + } + } + + /// + /// Maps the dto property values to the persisted model + /// + /// + private void MapPropertyValues(ContentItemSave contentItem) + { + UpdateName(contentItem); + + //TODO: We need to support 'send to publish' + + contentItem.PersistedContent.ExpireDate = contentItem.ExpireDate; + contentItem.PersistedContent.ReleaseDate = contentItem.ReleaseDate; + //only set the template if it didn't change + var templateChanged = (contentItem.PersistedContent.Template == null && contentItem.TemplateAlias.IsNullOrWhiteSpace() == false) + || (contentItem.PersistedContent.Template != null && contentItem.PersistedContent.Template.Alias != contentItem.TemplateAlias) + || (contentItem.PersistedContent.Template != null && contentItem.TemplateAlias.IsNullOrWhiteSpace()); + if (templateChanged) + { + var template = Services.FileService.GetTemplate(contentItem.TemplateAlias); + if (template == null && contentItem.TemplateAlias.IsNullOrWhiteSpace() == false) + { + //ModelState.AddModelError("Template", "No template exists with the specified alias: " + contentItem.TemplateAlias); + LogHelper.Warn("No template exists with the specified alias: " + contentItem.TemplateAlias); + } + else + { + //NOTE: this could be null if there was a template and the posted template is null, this should remove the assigned template + contentItem.PersistedContent.Template = template; + } + } + + base.MapPropertyValues(contentItem); + } + /// /// Ensures the item can be moved/copied to the new location /// @@ -638,9 +636,7 @@ namespace Umbraco.Web.Editors new[] {string.Format("{0} ({1})", status.ContentItem.Name, status.ContentItem.Id)}).Trim()); break; case PublishStatusType.FailedCancelledByEvent: - display.AddWarningNotification( - Services.TextService.Localize("publish"), - Services.TextService.Localize("speechBubbles/contentPublishedFailedByEvent")); + AddCancelMessage(display, "publish", "speechBubbles/contentPublishedFailedByEvent"); break; case PublishStatusType.FailedAwaitingRelease: display.AddWarningNotification( diff --git a/src/Umbraco.Web/Editors/ContentControllerBase.cs b/src/Umbraco.Web/Editors/ContentControllerBase.cs index d5dbdad620..e702da369e 100644 --- a/src/Umbraco.Web/Editors/ContentControllerBase.cs +++ b/src/Umbraco.Web/Editors/ContentControllerBase.cs @@ -172,5 +172,15 @@ namespace Umbraco.Web.Editors return (action.ToString().EndsWith("New")); } + protected void AddCancelMessage(INotificationModel display, string header = "speechBubbles/operationCancelledHeader", string message = "speechBubbles/operationCancelledText") + { + //if there's already a default event message, don't add our default one + var msgs = UmbracoContext.GetCurrentEventMessages(); + if (msgs != null && msgs.GetAll().Any(x => x.IsDefaultEventMessage)) return; + + display.AddWarningNotification( + Services.TextService.Localize(header), + Services.TextService.Localize(message)); + } } } diff --git a/src/Umbraco.Web/Editors/MediaController.cs b/src/Umbraco.Web/Editors/MediaController.cs index 1eb68c88a9..7058d73900 100644 --- a/src/Umbraco.Web/Editors/MediaController.cs +++ b/src/Umbraco.Web/Editors/MediaController.cs @@ -269,9 +269,7 @@ namespace Umbraco.Web.Editors } else { - display.AddWarningNotification( - Services.TextService.Localize("speechBubbles/operationCancelledHeader"), - Services.TextService.Localize("speechBubbles/operationCancelledText")); + AddCancelMessage(display); } break;