diff --git a/src/Umbraco.Core/Events/CopyEventArgs.cs b/src/Umbraco.Core/Events/CopyEventArgs.cs index 4717e206cd..6ce4d930ed 100644 --- a/src/Umbraco.Core/Events/CopyEventArgs.cs +++ b/src/Umbraco.Core/Events/CopyEventArgs.cs @@ -1,10 +1,27 @@ namespace Umbraco.Core.Events { - public class CopyEventArgs : System.ComponentModel.CancelEventArgs + public class CopyEventArgs : CancellableObjectEventArgs { + public CopyEventArgs(TEntity original, TEntity copy, bool canCancel, int parentId) : base(original, canCancel) + { + Copy = copy; + ParentId = parentId; + } + + public CopyEventArgs(TEntity entity, TEntity copy, int parentId) : base(entity) + { + Copy = copy; + ParentId = parentId; + } + + /// + /// The copied entity + /// + public TEntity Copy { get; set; } + /// /// 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 77368a1c8e..2a58bce907 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -1021,94 +1021,87 @@ namespace Umbraco.Core.Services /// The newly created object public IContent Copy(IContent content, int parentId, bool relateToOriginal, int userId = -1) { - var e = new CopyEventArgs { ParentId = parentId }; - if (Copying != null) - Copying(content, e); + var copy = ((Content)content).Clone(); + copy.ParentId = parentId; + copy.Name = copy.Name + " (1)"; - IContent copy = null; + if (Copying.IsRaisedEventCancelled(new CopyEventArgs(content, copy, parentId), this)) + return null; - if (!e.Cancel) + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateContentRepository(uow)) { - copy = ((Content)content).Clone(); - copy.ParentId = parentId; - copy.Name = copy.Name + " (1)"; + SetWriter(content, userId); - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateContentRepository(uow)) + repository.AddOrUpdate(copy); + uow.Commit(); + + var uploadFieldId = new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"); + if (content.Properties.Any(x => x.PropertyType.DataTypeControlId == uploadFieldId)) { - SetWriter(content, userId); + bool isUpdated = false; + var fs = FileSystemProviderManager.Current.GetFileSystemProvider(); - repository.AddOrUpdate(copy); - uow.Commit(); - - var uploadFieldId = new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"); - if (content.Properties.Any(x => x.PropertyType.DataTypeControlId == uploadFieldId)) - { - bool isUpdated = false; - var fs = FileSystemProviderManager.Current.GetFileSystemProvider(); + //Loop through properties to check if the content contains media that should be deleted + foreach (var property in content.Properties.Where(x => x.PropertyType.DataTypeControlId == uploadFieldId + && string.IsNullOrEmpty(x.Value.ToString()) == false)) + { + if (fs.FileExists(IOHelper.MapPath(property.Value.ToString()))) + { + var currentPath = fs.GetRelativePath(property.Value.ToString()); + var propertyId = copy.Properties.First(x => x.Alias == property.Alias).Id; + var newPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(currentPath)); - //Loop through properties to check if the content contains media that should be deleted - foreach (var property in content.Properties.Where(x => x.PropertyType.DataTypeControlId == uploadFieldId - && string.IsNullOrEmpty(x.Value.ToString()) == false)) - { - if (fs.FileExists(IOHelper.MapPath(property.Value.ToString()))) - { - var currentPath = fs.GetRelativePath(property.Value.ToString()); - var propertyId = copy.Properties.First(x => x.Alias == property.Alias).Id; - var newPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(currentPath)); + fs.CopyFile(currentPath, newPath); + copy.SetValue(property.Alias, fs.GetUrl(newPath)); - fs.CopyFile(currentPath, newPath); - copy.SetValue(property.Alias, fs.GetUrl(newPath)); + //Copy thumbnails + foreach (var thumbPath in fs.GetThumbnails(currentPath)) + { + var newThumbPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(thumbPath)); + fs.CopyFile(thumbPath, newThumbPath); + } + isUpdated = true; + } + } - //Copy thumbnails - foreach (var thumbPath in fs.GetThumbnails(currentPath)) - { - var newThumbPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(thumbPath)); - fs.CopyFile(thumbPath, newThumbPath); - } - isUpdated = true; - } - } - - if (isUpdated) - { - repository.AddOrUpdate(copy); - uow.Commit(); - } - } + if (isUpdated) + { + repository.AddOrUpdate(copy); + uow.Commit(); + } } - - //NOTE This 'Relation' part should eventually be delegated to a RelationService - if (relateToOriginal) - { - RelationType relationType = null; - using (var relationTypeRepository = _repositoryFactory.CreateRelationTypeRepository(uow)) - { - relationType = relationTypeRepository.Get(1); - } - - using (var relationRepository = _repositoryFactory.CreateRelationRepository(uow)) - { - var relation = new Relation(content.Id, copy.Id, relationType); - relationRepository.AddOrUpdate(relation); - uow.Commit(); - } - - Audit.Add(AuditTypes.Copy, - string.Format("Copied content with Id: '{0}' related to original content with Id: '{1}'", - copy.Id, content.Id), copy.WriterId, copy.Id); - } - - //Look for children and copy those as well - var children = GetChildren(content.Id); - foreach (var child in children) - { - Copy(child, copy.Id, relateToOriginal, userId); - } } - if (Copied != null) - Copied(copy, e); + //NOTE This 'Relation' part should eventually be delegated to a RelationService + if (relateToOriginal) + { + RelationType relationType = null; + using (var relationTypeRepository = _repositoryFactory.CreateRelationTypeRepository(uow)) + { + relationType = relationTypeRepository.Get(1); + } + + using (var relationRepository = _repositoryFactory.CreateRelationRepository(uow)) + { + var relation = new Relation(content.Id, copy.Id, relationType); + relationRepository.AddOrUpdate(relation); + uow.Commit(); + } + + Audit.Add(AuditTypes.Copy, + string.Format("Copied content with Id: '{0}' related to original content with Id: '{1}'", + copy.Id, content.Id), copy.WriterId, copy.Id); + } + + //Look for children and copy those as well + var children = GetChildren(content.Id); + foreach (var child in children) + { + Copy(child, copy.Id, relateToOriginal, userId); + } + + Copied.RaiseEvent(new CopyEventArgs(content, copy, false, parentId), this); Audit.Add(AuditTypes.Copy, "Copy Content performed by user", content.WriterId, content.Id); @@ -1305,12 +1298,12 @@ namespace Umbraco.Core.Services /// /// Occurs before Copy /// - public static event EventHandler Copying; + public static event TypedEventHandler> Copying; /// /// Occurs after Copy /// - public static event EventHandler Copied; + public static event TypedEventHandler> Copied; /// /// Occurs before Content is moved to Recycle Bin