diff --git a/src/Umbraco.Core/Events/MoveEventArgs.cs b/src/Umbraco.Core/Events/MoveEventArgs.cs index 6226cde7d4..6818ed5e73 100644 --- a/src/Umbraco.Core/Events/MoveEventArgs.cs +++ b/src/Umbraco.Core/Events/MoveEventArgs.cs @@ -1,10 +1,20 @@ namespace Umbraco.Core.Events { - public class MoveEventArgs : System.ComponentModel.CancelEventArgs + public class MoveEventArgs : CancellableObjectEventArgs { + public MoveEventArgs(TEntity entity, bool canCancel, int parentId) : base(entity, canCancel) + { + ParentId = parentId; + } + + public MoveEventArgs(TEntity entity, int parentId) : base(entity) + { + ParentId = parentId; + } + /// /// Gets or Sets the Id of the objects new parent. /// - public int ParentId { get; set; } + public int ParentId { get; private set; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 9a3e3f6b92..77368a1c8e 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -914,38 +914,33 @@ namespace Umbraco.Core.Services /// Optional Id of the User deleting the Content public void MoveToRecycleBin(IContent content, int userId = -1) { + if (Trashing.IsRaisedEventCancelled(new MoveEventArgs(content, -20), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateContentRepository(uow)) { - var e = new MoveEventArgs { ParentId = -20 }; - if (Trashing != null) - Trashing(content, e); - - if (!e.Cancel) + //Make sure that published content is unpublished before being moved to the Recycle Bin + if (HasPublishedVersion(content.Id)) { - //Make sure that published content is unpublished before being moved to the Recycle Bin - if (HasPublishedVersion(content.Id)) - { - UnPublish(content, userId); - } - - //Move children to Recycle Bin before the 'possible parent' is moved there - var children = GetChildren(content.Id); - foreach (var child in children) - { - MoveToRecycleBin(child, userId); - } - - SetWriter(content, userId); - content.ChangeTrashedState(true); - repository.AddOrUpdate(content); - uow.Commit(); - - if (Trashed != null) - Trashed(content, e); - - Audit.Add(AuditTypes.Move, "Move Content to Recycle Bin performed by user", userId == -1 ? 0 : userId, content.Id); + UnPublish(content, userId); } + + //Move children to Recycle Bin before the 'possible parent' is moved there + var children = GetChildren(content.Id); + foreach (var child in children) + { + MoveToRecycleBin(child, userId); + } + + SetWriter(content, userId); + content.ChangeTrashedState(true); + repository.AddOrUpdate(content); + uow.Commit(); + + Trashed.RaiseEvent(new MoveEventArgs(content, false, -20), this); + + Audit.Add(AuditTypes.Move, "Move Content to Recycle Bin performed by user", userId == -1 ? 0 : userId, content.Id); } } @@ -964,39 +959,35 @@ namespace Umbraco.Core.Services { //TODO Verify that SortOrder + Path is updated correctly //TODO Add a check to see if parentId = -20 because then we should change the TrashState - var e = new MoveEventArgs { ParentId = parentId }; - if (Moving != null) - Moving(content, e); + + if (Moving.IsRaisedEventCancelled(new MoveEventArgs(content, parentId), this)) + return; + + SetWriter(content, userId); - if (!e.Cancel) + //If Content is being moved away from Recycle Bin, its state should be un-trashed + if (content.Trashed && parentId != -20) { - SetWriter(content, userId); - - //If Content is being moved away from Recycle Bin, its state should be un-trashed - if (content.Trashed && parentId != -20) - { - content.ChangeTrashedState(false, parentId); - } - else - { - content.ParentId = parentId; - } - - //If Content is published, it should be (re)published from its new location - if (content.Published) - { - SaveAndPublish(content, userId); - } - else - { - Save(content, userId); - } - - if (Moved != null) - Moved(content, e); - - Audit.Add(AuditTypes.Move, "Move Content performed by user", userId == -1 ? 0 : userId, content.Id); + content.ChangeTrashedState(false, parentId); } + else + { + content.ParentId = parentId; + } + + //If Content is published, it should be (re)published from its new location + if (content.Published) + { + SaveAndPublish(content, userId); + } + else + { + Save(content, userId); + } + + Moved.RaiseEvent(new MoveEventArgs(content, false, parentId), this); + + Audit.Add(AuditTypes.Move, "Move Content performed by user", userId == -1 ? 0 : userId, content.Id); } /// @@ -1324,22 +1315,22 @@ namespace Umbraco.Core.Services /// /// Occurs before Content is moved to Recycle Bin /// - public static event EventHandler Trashing; + public static event TypedEventHandler> Trashing; /// /// Occurs after Content is moved to Recycle Bin /// - public static event EventHandler Trashed; + public static event TypedEventHandler> Trashed; /// /// Occurs before Move /// - public static event EventHandler Moving; + public static event TypedEventHandler> Moving; /// /// Occurs after Move /// - public static event EventHandler Moved; + public static event TypedEventHandler> Moved; /// /// Occurs before Rollback diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 9da0192bf8..1a64f7d8a7 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -172,7 +172,7 @@ namespace Umbraco.Core.Services var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaRepository(uow)) { - var query = Query.Builder.Where(x => x.ParentId == -20); + var query = Query.Builder.Where(x => x.ParentId == -21); var medias = repository.GetByQuery(query); return medias; @@ -187,20 +187,15 @@ namespace Umbraco.Core.Services /// Id of the User moving the Media public void Move(IMedia media, int parentId, int userId = -1) { - var e = new MoveEventArgs { ParentId = parentId }; - if (Moving != null) - Moving(media, e); + if (Moving.IsRaisedEventCancelled(new MoveEventArgs(media, parentId), this)) + return; - if (!e.Cancel) - { - media.ParentId = parentId; - Save(media, userId); + media.ParentId = parentId; + Save(media, userId); - if (Moved != null) - Moved(media, e); + Moved.RaiseEvent(new MoveEventArgs(media, false, parentId), this); - Audit.Add(AuditTypes.Move, "Move Media performed by user", userId == -1 ? 0 : userId, media.Id); - } + Audit.Add(AuditTypes.Move, "Move Media performed by user", userId == -1 ? 0 : userId, media.Id); } /// @@ -212,24 +207,19 @@ namespace Umbraco.Core.Services { //TODO If media item has children those should also be moved to the recycle bin as well + if (Trashing.IsRaisedEventCancelled(new MoveEventArgs(media, -21), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaRepository(uow)) { - var e = new MoveEventArgs { ParentId = -20 }; - if (Trashing != null) - Trashing(media, e); + ((Core.Models.Media)media).ChangeTrashedState(true); + repository.AddOrUpdate(media); + uow.Commit(); - if (!e.Cancel) - { - ((Core.Models.Media)media).ChangeTrashedState(true); - repository.AddOrUpdate(media); - uow.Commit(); + Trashed.RaiseEvent(new MoveEventArgs(media, false, -21), this); - if (Trashed != null) - Trashed(media, e); - - Audit.Add(AuditTypes.Move, "Move Media to Recycle Bin performed by user", userId == -1 ? 0 : userId, media.Id); - } + Audit.Add(AuditTypes.Move, "Move Media to Recycle Bin performed by user", userId == -1 ? 0 : userId, media.Id); } } @@ -241,7 +231,7 @@ namespace Umbraco.Core.Services var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaRepository(uow)) { - var query = Query.Builder.Where(x => x.ParentId == -20); + var query = Query.Builder.Where(x => x.ParentId == -21); var contents = repository.GetByQuery(query); foreach (var content in contents) @@ -250,7 +240,7 @@ namespace Umbraco.Core.Services } uow.Commit(); - Audit.Add(AuditTypes.Delete, "Empty Recycle Bin performed by user", 0, -20); + Audit.Add(AuditTypes.Delete, "Empty Recycle Bin performed by user", 0, -21); } } @@ -460,22 +450,22 @@ namespace Umbraco.Core.Services /// /// Occurs before Content is moved to Recycle Bin /// - public static event EventHandler Trashing; + public static event TypedEventHandler> Trashing; /// /// Occurs after Content is moved to Recycle Bin /// - public static event EventHandler Trashed; + public static event TypedEventHandler> Trashed; /// /// Occurs before Move /// - public static event EventHandler Moving; + public static event TypedEventHandler> Moving; /// /// Occurs after Move /// - public static event EventHandler Moved; + public static event TypedEventHandler> Moved; #endregion } } \ No newline at end of file