diff --git a/src/Umbraco.Core/Services/RelationService.cs b/src/Umbraco.Core/Services/RelationService.cs index 5da9fddef0..756401383c 100644 --- a/src/Umbraco.Core/Services/RelationService.cs +++ b/src/Umbraco.Core/Services/RelationService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Persistence; @@ -326,14 +327,18 @@ namespace Umbraco.Core.Services Save(relationType); var relation = new Relation(parent.Id, child.Id, relationType); + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return relation; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); - - return relation; } + + Saved.RaiseEvent(new SaveEventArgs(relation, false), this); + return relation; } /// @@ -350,14 +355,18 @@ namespace Umbraco.Core.Services throw new ArgumentNullException(string.Format("No RelationType with Alias '{0}' exists.", relationTypeAlias)); var relation = new Relation(parent.Id, child.Id, relationType); + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return relation; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); - - return relation; } + + Saved.RaiseEvent(new SaveEventArgs(relation, false), this); + return relation; } /// @@ -394,12 +403,17 @@ namespace Umbraco.Core.Services /// Relation to save public void Save(IRelation relation) { + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); } + + Saved.RaiseEvent(new SaveEventArgs(relation, false), this); } /// @@ -408,12 +422,17 @@ namespace Umbraco.Core.Services /// RelationType to Save public void Save(IRelationType relationType) { + if (SavingRelationType.IsRaisedEventCancelled(new SaveEventArgs(relationType), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) { repository.AddOrUpdate(relationType); uow.Commit(); } + + RelationTypeSaved.RaiseEvent(new SaveEventArgs(relationType, false), this); } /// @@ -422,12 +441,17 @@ namespace Umbraco.Core.Services /// Relation to Delete public void Delete(IRelation relation) { + if (Deleting.IsRaisedEventCancelled(new DeleteEventArgs(relation), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.Delete(relation); uow.Commit(); } + + Deleted.RaiseEvent(new DeleteEventArgs(relation, false), this); } /// @@ -436,12 +460,17 @@ namespace Umbraco.Core.Services /// RelationType to Delete public void Delete(IRelationType relationType) { + if (DeletingRelationType.IsRaisedEventCancelled(new DeleteEventArgs(relationType), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) { repository.Delete(relationType); uow.Commit(); } + + RelationTypeDeleted.RaiseEvent(new DeleteEventArgs(relationType, false), this); } /// @@ -450,18 +479,21 @@ namespace Umbraco.Core.Services /// to Delete Relations for public void DeleteRelationsOfType(IRelationType relationType) { + var relations = new List(); var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { var query = new Query().Where(x => x.RelationTypeId == relationType.Id); - var list = repository.GetByQuery(query).ToList(); + relations.AddRange(repository.GetByQuery(query).ToList()); - foreach (var relation in list) + foreach (var relation in relations) { repository.Delete(relation); } uow.Commit(); } + + Deleted.RaiseEvent(new DeleteEventArgs(relations, false), this); } #region Private Methods @@ -480,5 +512,47 @@ namespace Umbraco.Core.Services return relations; } #endregion + + #region Events Handlers + /// + /// Occurs before Deleting a Relation + /// + public static event TypedEventHandler> Deleting; + + /// + /// Occurs after a Relation is Deleted + /// + public static event TypedEventHandler> Deleted; + + /// + /// Occurs before Saving a Relation + /// + public static event TypedEventHandler> Saving; + + /// + /// Occurs after a Relation is Saved + /// + public static event TypedEventHandler> Saved; + + /// + /// Occurs before Deleting a RelationType + /// + public static event TypedEventHandler> DeletingRelationType; + + /// + /// Occurs after a RelationType is Deleted + /// + public static event TypedEventHandler> RelationTypeDeleted; + + /// + /// Occurs before Saving a RelationType + /// + public static event TypedEventHandler> SavingRelationType; + + /// + /// Occurs after a RelationType is Saved + /// + public static event TypedEventHandler> RelationTypeSaved; + #endregion } } \ No newline at end of file diff --git a/src/umbraco.cms/businesslogic/template/Template.cs b/src/umbraco.cms/businesslogic/template/Template.cs index dc5660d3e5..35e456363e 100644 --- a/src/umbraco.cms/businesslogic/template/Template.cs +++ b/src/umbraco.cms/businesslogic/template/Template.cs @@ -410,7 +410,6 @@ namespace umbraco.cms.businesslogic.template if (name.Length > 100) name = name.Substring(0, 95) + "..."; - SqlHelper.ExecuteNonQuery("INSERT INTO cmsTemplate (NodeId, Alias, design, master) VALUES (@nodeId, @alias, @design, @master)", SqlHelper.CreateParameter("@nodeId", node.Id),