From 3de8fe79c0dbf8c53047dee41f0b163a94e1fe9d Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Mon, 10 Jun 2019 17:15:50 +0100 Subject: [PATCH] Recheck all child items when a folder is saved (renamed) #26 --- .../Handlers/ContentTypeHandler.cs | 3 +- .../SyncHandlers/Handlers/DataTypeHandler.cs | 4 +- .../SyncHandlers/Handlers/MediaTypeHandler.cs | 2 + .../Handlers/MemberTypeHandler.cs | 2 + .../SyncHandlers/SyncHandlerContainerBase.cs | 48 ++++++++++++++++++- uSync8.BackOffice/uSyncBackofficeComponent.cs | 2 +- 6 files changed, 57 insertions(+), 4 deletions(-) diff --git a/uSync8.BackOffice/SyncHandlers/Handlers/ContentTypeHandler.cs b/uSync8.BackOffice/SyncHandlers/Handlers/ContentTypeHandler.cs index daf012f2..ae39aad2 100644 --- a/uSync8.BackOffice/SyncHandlers/Handlers/ContentTypeHandler.cs +++ b/uSync8.BackOffice/SyncHandlers/Handlers/ContentTypeHandler.cs @@ -60,8 +60,9 @@ protected override void InitializeEvents(HandlerSettings settings) ContentTypeService.Saved += EventSavedItem; ContentTypeService.Deleted += EventDeletedItem; ContentTypeService.Moved += EventMovedItem; - } + ContentTypeService.SavedContainer += EventContainerSaved; + } protected override string GetItemFileName(IUmbracoEntity item, bool useGuid) { diff --git a/uSync8.BackOffice/SyncHandlers/Handlers/DataTypeHandler.cs b/uSync8.BackOffice/SyncHandlers/Handlers/DataTypeHandler.cs index 9c92201d..61a28318 100644 --- a/uSync8.BackOffice/SyncHandlers/Handlers/DataTypeHandler.cs +++ b/uSync8.BackOffice/SyncHandlers/Handlers/DataTypeHandler.cs @@ -43,7 +43,9 @@ protected override void InitializeEvents(HandlerSettings settings) { DataTypeService.Saved += EventSavedItem; DataTypeService.Deleted += EventDeletedItem; - DataTypeService.Moved += EventMovedItem; ; + DataTypeService.Moved += EventMovedItem; + + DataTypeService.SavedContainer += EventContainerSaved; } protected override string GetItemFileName(IUmbracoEntity item, bool useGuid) diff --git a/uSync8.BackOffice/SyncHandlers/Handlers/MediaTypeHandler.cs b/uSync8.BackOffice/SyncHandlers/Handlers/MediaTypeHandler.cs index 5eb98459..e85f204b 100644 --- a/uSync8.BackOffice/SyncHandlers/Handlers/MediaTypeHandler.cs +++ b/uSync8.BackOffice/SyncHandlers/Handlers/MediaTypeHandler.cs @@ -44,6 +44,8 @@ protected override void InitializeEvents(HandlerSettings settings) MediaTypeService.Saved += EventSavedItem; MediaTypeService.Deleted += EventDeletedItem; MediaTypeService.Moved += EventMovedItem; + + MediaTypeService.SavingContainer += EventContainerSaved; } protected override string GetItemFileName(IUmbracoEntity item, bool useGuid) diff --git a/uSync8.BackOffice/SyncHandlers/Handlers/MemberTypeHandler.cs b/uSync8.BackOffice/SyncHandlers/Handlers/MemberTypeHandler.cs index 992fd669..b5671fe0 100644 --- a/uSync8.BackOffice/SyncHandlers/Handlers/MemberTypeHandler.cs +++ b/uSync8.BackOffice/SyncHandlers/Handlers/MemberTypeHandler.cs @@ -46,6 +46,8 @@ protected override void InitializeEvents(HandlerSettings settings) MemberTypeService.Saved += EventSavedItem; MemberTypeService.Deleted += EventDeletedItem; MemberTypeService.Moved += EventMovedItem; + + MemberTypeService.SavedContainer += EventContainerSaved; } protected override void DeleteFolder(int id) diff --git a/uSync8.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs b/uSync8.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs index 2a8de4a0..5eb1be9a 100644 --- a/uSync8.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs +++ b/uSync8.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using Umbraco.Core.Events; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; @@ -41,7 +43,6 @@ protected SyncHandlerContainerBase(IEntityService entityService, IProfilingLogge protected IEnumerable CleanFolders(string folder, int parent) { var actions = new List(); - var folders = entityService.GetChildren(parent, this.itemContainerType); foreach (var fdlr in folders) { @@ -67,5 +68,50 @@ public virtual IEnumerable ProcessPostImport(string folder, IEnumer return CleanFolders(folder, -1); } + /// + /// will resave everything in a folder (and beneath) + /// we need to this when it's renamed + /// + /// + /// + /// + /// + protected IEnumerable UpdateFolder(int folderId, string folder, HandlerSettings config) + { + var actions = new List(); + var folders = entityService.GetChildren(folderId, this.itemContainerType); + foreach (var fdlr in folders) + { + actions.AddRange(UpdateFolder(fdlr.Id, folder, config)); + } + + var items = entityService.GetChildren(folderId, this.itemObjectType); + foreach(var item in items) + { + var obj = GetFromService(item.Id); + if (obj != null) + { + var attempt = Export(obj, folder, config); + if (attempt.Success) + { + CleanUp(obj, attempt.FileName, folder); + } + actions.Add(attempt); + } + } + + return actions; + + } + + protected void EventContainerSaved(IService service, SaveEventArgs e) + { + if (uSync8BackOffice.eventsPaused) return; + + foreach(var folder in e.SavedEntities) + { + UpdateFolder(folder.Id, Path.Combine(rootFolder, this.DefaultFolder), DefaultConfig); + } + } } } diff --git a/uSync8.BackOffice/uSyncBackofficeComponent.cs b/uSync8.BackOffice/uSyncBackofficeComponent.cs index 5d082df5..83978366 100644 --- a/uSync8.BackOffice/uSyncBackofficeComponent.cs +++ b/uSync8.BackOffice/uSyncBackofficeComponent.cs @@ -49,7 +49,7 @@ public void Initialize() { ServerVariablesParser.Parsing += ServerVariablesParser_Parsing; - if (runtimeState.Level <= RuntimeLevel.Run) + if (runtimeState.Level < RuntimeLevel.Run) { logger.Info("Umbraco is not in Run Mode {0} so uSync is not going to run", runtimeState.Level); return;