diff --git a/src/Umbraco.Core/Events/NewEventArgs.cs b/src/Umbraco.Core/Events/NewEventArgs.cs index 6f6cf298d9..8e6cd64db1 100644 --- a/src/Umbraco.Core/Events/NewEventArgs.cs +++ b/src/Umbraco.Core/Events/NewEventArgs.cs @@ -10,7 +10,7 @@ namespace Umbraco.Core.Events ParentId = parentId; } - public NewEventArgs(TEntity eventObject, bool canCancel, string @alias, IContent parent) + public NewEventArgs(TEntity eventObject, bool canCancel, string @alias, TEntity parent) : base(eventObject, canCancel) { Alias = alias; @@ -23,7 +23,7 @@ namespace Umbraco.Core.Events ParentId = parentId; } - public NewEventArgs(TEntity eventObject, string @alias, IContent parent) + public NewEventArgs(TEntity eventObject, string @alias, TEntity parent) : base(eventObject) { Alias = alias; @@ -51,6 +51,6 @@ namespace Umbraco.Core.Events /// /// Gets or Sets the parent IContent object. /// - public IContent Parent { get; private set; } + public TEntity Parent { get; private set; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/IMediaService.cs b/src/Umbraco.Core/Services/IMediaService.cs index 8c7295bcb3..6f9d87d576 100644 --- a/src/Umbraco.Core/Services/IMediaService.cs +++ b/src/Umbraco.Core/Services/IMediaService.cs @@ -20,6 +20,17 @@ namespace Umbraco.Core.Services /// IMedia CreateMedia(string name, int parentId, string mediaTypeAlias, int userId = 0); + /// + /// Creates an object using the alias of the + /// that this Media is based on. + /// + /// Name of the Media object + /// Parent for the new Media item + /// Alias of the + /// Optional id of the user creating the media item + /// + IMedia CreateMedia(string name, IMedia parent, string mediaTypeAlias, int userId = 0); + /// /// Gets an object by Id /// @@ -104,14 +115,16 @@ namespace Umbraco.Core.Services /// /// The to save /// Id of the User saving the Media - void Save(IMedia media, int userId = 0); + /// Optional boolean indicating whether or not to raise events. + void Save(IMedia media, int userId = 0, bool raiseEvents = true); /// /// Saves a collection of objects /// /// Collection of to save /// Id of the User saving the Media - void Save(IEnumerable medias, int userId = 0); + /// Optional boolean indicating whether or not to raise events. + void Save(IEnumerable medias, int userId = 0, bool raiseEvents = true); /// /// Gets an object by its 'UniqueId' diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 93f41baab9..2c531de719 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -50,13 +50,13 @@ namespace Umbraco.Core.Services var mediaTypes = repository.GetByQuery(query); if (!mediaTypes.Any()) - throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found", + throw new Exception(string.Format("No MediaType matching the passed in Alias: '{0}' was found", mediaTypeAlias)); mediaType = mediaTypes.First(); if (mediaType == null) - throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null", + throw new Exception(string.Format("MediaType matching the passed in Alias: '{0}' was null", mediaTypeAlias)); } @@ -74,6 +74,49 @@ namespace Umbraco.Core.Services return media; } + /// + /// Creates an object using the alias of the + /// that this Media is based on. + /// + /// Name of the Media object + /// Parent for the new Media item + /// Alias of the + /// Optional id of the user creating the media item + /// + public IMedia CreateMedia(string name, IMedia parent, string mediaTypeAlias, int userId = 0) + { + IMediaType mediaType = null; + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) + { + var query = Query.Builder.Where(x => x.Alias == mediaTypeAlias); + var mediaTypes = repository.GetByQuery(query); + + if (!mediaTypes.Any()) + throw new Exception(string.Format("No MediaType matching the passed in Alias: '{0}' was found", + mediaTypeAlias)); + + mediaType = mediaTypes.First(); + + if (mediaType == null) + throw new Exception(string.Format("MediaType matching the passed in Alias: '{0}' was null", + mediaTypeAlias)); + } + + var media = new Models.Media(name, parent, mediaType); + + if (Creating.IsRaisedEventCancelled(new NewEventArgs(media, mediaTypeAlias, parent), this)) + return media; + + media.CreatorId = userId; + + Created.RaiseEvent(new NewEventArgs(media, false, mediaTypeAlias, parent), this); + + Audit.Add(AuditTypes.New, "", media.CreatorId, media.Id); + + return media; + } + /// /// Gets an object by Id /// @@ -471,10 +514,14 @@ namespace Umbraco.Core.Services /// /// The to save /// Id of the User saving the Content - public void Save(IMedia media, int userId = 0) + /// Optional boolean indicating whether or not to raise events. + public void Save(IMedia media, int userId = 0, bool raiseEvents = true) { - if (Saving.IsRaisedEventCancelled(new SaveEventArgs(media), this)) + if(raiseEvents) + { + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(media), this)) return; + } var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaRepository(uow)) @@ -491,7 +538,8 @@ namespace Umbraco.Core.Services : Convert.ToInt32(uow.Database.Insert(poco)); } - Saved.RaiseEvent(new SaveEventArgs(media, false), this); + if(raiseEvents) + Saved.RaiseEvent(new SaveEventArgs(media, false), this); Audit.Add(AuditTypes.Save, "Save Media performed by user", media.CreatorId, media.Id); } @@ -501,10 +549,14 @@ namespace Umbraco.Core.Services /// /// Collection of to save /// Id of the User saving the Content - public void Save(IEnumerable medias, int userId = 0) + /// Optional boolean indicating whether or not to raise events. + public void Save(IEnumerable medias, int userId = 0, bool raiseEvents = true) { - if (SavingCollection.IsRaisedEventCancelled(new SaveEventArgs>(medias), this)) + if(raiseEvents) + { + if (SavingCollection.IsRaisedEventCancelled(new SaveEventArgs>(medias), this)) return; + } var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaRepository(uow)) @@ -519,7 +571,8 @@ namespace Umbraco.Core.Services uow.Commit(); } - SavedCollection.RaiseEvent(new SaveEventArgs>(medias, false), this); + if(raiseEvents) + SavedCollection.RaiseEvent(new SaveEventArgs>(medias, false), this); Audit.Add(AuditTypes.Save, "Save Media items performed by user", userId, -1); }