|
| 1 | +using Umbraco.Cms.Core.Events; |
| 2 | +using Umbraco.Cms.Core.Extensions; |
| 3 | +using Umbraco.Cms.Core.Models; |
| 4 | +using Umbraco.Cms.Core.Notifications; |
| 5 | +using Umbraco.Cms.Core.Services; |
| 6 | +using Umbraco.Cms.Core.Services.ContentTypeEditing; |
| 7 | + |
| 8 | +namespace Umbraco.Cms.Core.Handlers; |
| 9 | + |
| 10 | +public class WarnDocumentTypeElementSwitchNotificationHandler : |
| 11 | + INotificationAsyncHandler<ContentTypeSavingNotification>, |
| 12 | + INotificationAsyncHandler<ContentTypeSavedNotification> |
| 13 | +{ |
| 14 | + private const string NotificationStateKey = |
| 15 | + "Umbraco.Cms.Core.Handlers.WarnDocumentTypeElementSwitchNotificationHandler"; |
| 16 | + |
| 17 | + private readonly IEventMessagesFactory _eventMessagesFactory; |
| 18 | + private readonly IContentTypeService _contentTypeService; |
| 19 | + private readonly IElementSwitchValidator _elementSwitchValidator; |
| 20 | + |
| 21 | + public WarnDocumentTypeElementSwitchNotificationHandler( |
| 22 | + IEventMessagesFactory eventMessagesFactory, |
| 23 | + IContentTypeService contentTypeService, |
| 24 | + IElementSwitchValidator elementSwitchValidator) |
| 25 | + { |
| 26 | + _eventMessagesFactory = eventMessagesFactory; |
| 27 | + _contentTypeService = contentTypeService; |
| 28 | + _elementSwitchValidator = elementSwitchValidator; |
| 29 | + } |
| 30 | + |
| 31 | + // To figure out whether a warning should be generated, we need both the state before and after saving |
| 32 | + public async Task HandleAsync(ContentTypeSavingNotification notification, CancellationToken cancellationToken) |
| 33 | + { |
| 34 | + IEnumerable<Guid> updatedKeys = notification.SavedEntities |
| 35 | + .Where(e => e.HasIdentity) |
| 36 | + .Select(e => e.Key); |
| 37 | + |
| 38 | + IEnumerable<IContentType> persistedItems = _contentTypeService.GetAll(updatedKeys); |
| 39 | + |
| 40 | + var stateInformation = persistedItems |
| 41 | + .ToDictionary( |
| 42 | + contentType => contentType.Key, |
| 43 | + contentType => new DocumentTypeElementSwitchInformation { WasElement = contentType.IsElement }); |
| 44 | + notification.State[NotificationStateKey] = stateInformation; |
| 45 | + } |
| 46 | + |
| 47 | + public async Task HandleAsync(ContentTypeSavedNotification notification, CancellationToken cancellationToken) |
| 48 | + { |
| 49 | + if (notification.State[NotificationStateKey] is not Dictionary<Guid, DocumentTypeElementSwitchInformation> |
| 50 | + stateInformation) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + EventMessages eventMessages = _eventMessagesFactory.Get(); |
| 56 | + |
| 57 | + foreach (IContentType savedDocumentType in notification.SavedEntities) |
| 58 | + { |
| 59 | + if (stateInformation.ContainsKey(savedDocumentType.Key) is false) |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + DocumentTypeElementSwitchInformation state = stateInformation[savedDocumentType.Key]; |
| 65 | + if (state.WasElement == savedDocumentType.IsElement) |
| 66 | + { |
| 67 | + // no change |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + await WarnIfAncestorsAreMisaligned(savedDocumentType, eventMessages); |
| 72 | + await WarnIfDescendantsAreMisaligned(savedDocumentType, eventMessages); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private async Task WarnIfAncestorsAreMisaligned(IContentType contentType, EventMessages eventMessages) |
| 77 | + { |
| 78 | + if (await _elementSwitchValidator.AncestorsAreAlignedAsync(contentType) == false) |
| 79 | + { |
| 80 | + // todo update this message when the format has been agreed upon on with the client |
| 81 | + eventMessages.Add(new EventMessage( |
| 82 | + "DocumentType saved", |
| 83 | + "One or more ancestors have a mismatching element flag", |
| 84 | + EventMessageType.Warning)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private async Task WarnIfDescendantsAreMisaligned(IContentType contentType, EventMessages eventMessages) |
| 89 | + { |
| 90 | + if (await _elementSwitchValidator.DescendantsAreAlignedAsync(contentType) == false) |
| 91 | + { |
| 92 | + // todo update this message when the format has been agreed upon on with the client |
| 93 | + eventMessages.Add(new EventMessage( |
| 94 | + "DocumentType saved", |
| 95 | + "One or more descendants have a mismatching element flag", |
| 96 | + EventMessageType.Warning)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private class DocumentTypeElementSwitchInformation |
| 101 | + { |
| 102 | + public bool WasElement { get; set; } |
| 103 | + } |
| 104 | +} |
0 commit comments