From 6457a1a2cae53e4d47f1ebcc3b396bdfb951eb01 Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 9 Jan 2015 19:15:01 +1100 Subject: [PATCH] Fixes: U4-6088 Publicize all partial view methods on IFileService & U4-6087 Fix Partial View macro repository usage so that when a macro is created from the FileService, the MacroService events are raised Conflicts: src/Umbraco.Core/Umbraco.Core.csproj --- src/Umbraco.Core/Models/IPartialView.cs | 7 + src/Umbraco.Core/Models/Macro.cs | 7 +- src/Umbraco.Core/Models/PartialView.cs | 2 +- .../Repositories/FileRepository.cs | 4 +- .../Interfaces/IPartialViewMacroRepository.cs | 13 - .../Interfaces/IPartialViewRepository.cs | 5 +- .../PartialViewMacroRepository.cs | 27 +- .../Repositories/PartialViewRepository.cs | 6 +- .../Persistence/RepositoryFactory.cs | 4 +- src/Umbraco.Core/Services/FileService.cs | 289 +++++++----------- src/Umbraco.Core/Services/IFileService.cs | 12 + src/Umbraco.Core/Services/MacroService.cs | 2 + src/Umbraco.Core/Umbraco.Core.csproj | 2 +- .../umbraco/create/PartialViewTasksBase.cs | 19 +- 14 files changed, 165 insertions(+), 234 deletions(-) create mode 100644 src/Umbraco.Core/Models/IPartialView.cs delete mode 100644 src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewMacroRepository.cs diff --git a/src/Umbraco.Core/Models/IPartialView.cs b/src/Umbraco.Core/Models/IPartialView.cs new file mode 100644 index 0000000000..01127ce22a --- /dev/null +++ b/src/Umbraco.Core/Models/IPartialView.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Core.Models +{ + public interface IPartialView : IFile + { + + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/Macro.cs b/src/Umbraco.Core/Models/Macro.cs index eb8e0b7a66..0c86f5d1fe 100644 --- a/src/Umbraco.Core/Models/Macro.cs +++ b/src/Umbraco.Core/Models/Macro.cs @@ -9,6 +9,7 @@ using System.Runtime.Serialization; using System.Text.RegularExpressions; using Umbraco.Core.IO; using Umbraco.Core.Models.EntityBase; +using Umbraco.Core.Strings; namespace Umbraco.Core.Models { @@ -48,7 +49,7 @@ namespace Umbraco.Core.Models Id = id; UseInEditor = useInEditor; CacheDuration = cacheDuration; - Alias = alias; + Alias = alias.ToCleanString(CleanStringType.Alias); Name = name; ControlType = controlType; ControlAssembly = controlAssembly; @@ -87,7 +88,7 @@ namespace Umbraco.Core.Models { UseInEditor = useInEditor; CacheDuration = cacheDuration; - Alias = alias; + Alias = alias.ToCleanString(CleanStringType.Alias); Name = name; ControlType = controlType; ControlAssembly = controlAssembly; @@ -207,7 +208,7 @@ namespace Umbraco.Core.Models { SetPropertyValueAndDetectChanges(o => { - _alias = value; + _alias = value.ToCleanString(CleanStringType.Alias); return _alias; }, _alias, AliasSelector); } diff --git a/src/Umbraco.Core/Models/PartialView.cs b/src/Umbraco.Core/Models/PartialView.cs index e470d55037..0f0279d8d3 100644 --- a/src/Umbraco.Core/Models/PartialView.cs +++ b/src/Umbraco.Core/Models/PartialView.cs @@ -26,7 +26,7 @@ namespace Umbraco.Core.Models /// [Serializable] [DataContract(IsReference = true)] - internal class PartialView : File + public class PartialView : File, IPartialView { //public PartialView(): base(string.Empty) //{ diff --git a/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs index fbd37ea890..4020dbb713 100644 --- a/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs @@ -33,12 +33,12 @@ namespace Umbraco.Core.Persistence.Repositories get { return _fileSystem; } } - internal virtual void AddFolder(string folderPath) + public virtual void AddFolder(string folderPath) { _work.RegisterAdded(new Folder(folderPath), this); } - internal virtual void DeleteFolder(string folderPath) + public virtual void DeleteFolder(string folderPath) { _work.RegisterRemoved(new Folder(folderPath), this); } diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewMacroRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewMacroRepository.cs deleted file mode 100644 index 2d05af7438..0000000000 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewMacroRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Umbraco.Core.Models; - -namespace Umbraco.Core.Persistence.Repositories -{ - internal interface IPartialViewMacroRepository : IPartialViewRepository - { - /// - /// Adds or Updates an associated macro - /// - /// - void AddOrUpdate(IMacro macro); - } -} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs index f5e5a80a95..8ba28eeaee 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs @@ -2,8 +2,9 @@ namespace Umbraco.Core.Persistence.Repositories { - internal interface IPartialViewRepository : IRepository + internal interface IPartialViewRepository : IRepository { - + void AddFolder(string folderPath); + void DeleteFolder(string folderPath); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/PartialViewMacroRepository.cs b/src/Umbraco.Core/Persistence/Repositories/PartialViewMacroRepository.cs index e9a1b5f457..f4d6906cd1 100644 --- a/src/Umbraco.Core/Persistence/Repositories/PartialViewMacroRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/PartialViewMacroRepository.cs @@ -5,37 +5,18 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Persistence.Repositories { - internal class PartialViewMacroRepository : PartialViewRepository, IPartialViewMacroRepository + internal class PartialViewMacroRepository : PartialViewRepository { - private readonly IMacroRepository _macroRepository; - public PartialViewMacroRepository(IUnitOfWork work, IMacroRepository macroRepository) - : this(work, new PhysicalFileSystem(SystemDirectories.MvcViews + "/MacroPartials/"), macroRepository) + public PartialViewMacroRepository(IUnitOfWork work) + : this(work, new PhysicalFileSystem(SystemDirectories.MvcViews + "/MacroPartials/")) { } - public PartialViewMacroRepository(IUnitOfWork work, IFileSystem fileSystem, IMacroRepository macroRepository) + public PartialViewMacroRepository(IUnitOfWork work, IFileSystem fileSystem) : base(work, fileSystem) { - _macroRepository = macroRepository; } - /// - /// Ensure the macro repo is disposed which contains a database UOW - /// - protected override void DisposeResources() - { - base.DisposeResources(); - _macroRepository.Dispose(); - } - - /// - /// Adds or updates a macro associated with the partial view - /// - /// - public void AddOrUpdate(IMacro macro) - { - _macroRepository.AddOrUpdate(macro); - } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs b/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs index 8ef4c5ec43..b98b284a1f 100644 --- a/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs @@ -9,7 +9,7 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Persistence.Repositories { - internal class PartialViewRepository : FileRepository, IPartialViewRepository + internal class PartialViewRepository : FileRepository, IPartialViewRepository { public PartialViewRepository(IUnitOfWork work) : this(work, new PhysicalFileSystem(SystemDirectories.MvcViews + "/Partials/")) @@ -20,7 +20,7 @@ namespace Umbraco.Core.Persistence.Repositories { } - public override PartialView Get(string id) + public override IPartialView Get(string id) { if (FileSystem.FileExists(id) == false) { @@ -59,7 +59,7 @@ namespace Umbraco.Core.Persistence.Repositories return script; } - public override IEnumerable GetAll(params string[] ids) + public override IEnumerable GetAll(params string[] ids) { //ensure they are de-duplicated, easy win if people don't do this as this can cause many excess queries ids = ids.Distinct().ToArray(); diff --git a/src/Umbraco.Core/Persistence/RepositoryFactory.cs b/src/Umbraco.Core/Persistence/RepositoryFactory.cs index f549617d86..1a8752739f 100644 --- a/src/Umbraco.Core/Persistence/RepositoryFactory.cs +++ b/src/Umbraco.Core/Persistence/RepositoryFactory.cs @@ -149,9 +149,9 @@ namespace Umbraco.Core.Persistence return new PartialViewRepository(uow); } - internal virtual IPartialViewMacroRepository CreatePartialViewMacroRepository(IUnitOfWork uow, IDatabaseUnitOfWork duow) + internal virtual IPartialViewRepository CreatePartialViewMacroRepository(IUnitOfWork uow) { - return new PartialViewMacroRepository(uow, CreateMacroRepository(duow)); + return new PartialViewMacroRepository(uow); } public virtual IStylesheetRepository CreateStylesheetRepository(IUnitOfWork uow, IDatabaseUnitOfWork db) diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index 68b695ac1f..56f7d3a07b 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -388,7 +388,7 @@ namespace Umbraco.Core.Services #region Partial Views - internal IEnumerable GetPartialViewSnippetNames(params string[] filterNames) + public IEnumerable GetPartialViewSnippetNames(params string[] filterNames) { var snippetPath = IOHelper.MapPath(string.Format("{0}/PartialViewMacros/Templates/", SystemDirectories.Umbraco)); var files = Directory.GetFiles(snippetPath, "*.cshtml") @@ -404,7 +404,7 @@ namespace Umbraco.Core.Services return empty.Union(files.Except(empty)); } - internal void DeletePartialViewFolder(string folderPath) + public void DeletePartialViewFolder(string folderPath) { var uow = _fileUowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreatePartialViewRepository(uow)) @@ -414,18 +414,17 @@ namespace Umbraco.Core.Services } } - internal void DeletePartialViewMacroFolder(string folderPath) + public void DeletePartialViewMacroFolder(string folderPath) { var uow = _fileUowProvider.GetUnitOfWork(); - var duow = _dataUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreatePartialViewMacroRepository(uow, duow)) + using (var repository = _repositoryFactory.CreatePartialViewMacroRepository(uow)) { ((PartialViewMacroRepository)repository).DeleteFolder(folderPath); uow.Commit(); } } - internal PartialView GetPartialView(string path) + public IPartialView GetPartialView(string path) { using (var repository = _repositoryFactory.CreatePartialViewRepository(_fileUowProvider.GetUnitOfWork())) { @@ -433,20 +432,44 @@ namespace Umbraco.Core.Services } } - internal PartialView GetPartialViewMacro(string path) + public IPartialView GetPartialViewMacro(string path) { - using (var repository = _repositoryFactory.CreatePartialViewMacroRepository( - _fileUowProvider.GetUnitOfWork(), - _dataUowProvider.GetUnitOfWork())) + using (var repository = _repositoryFactory.CreatePartialViewMacroRepository(_fileUowProvider.GetUnitOfWork())) { return repository.Get(path); } } - internal Attempt CreatePartialView(PartialView partialView, string snippetName = null, int userId = 0) + public Attempt CreatePartialView(IPartialView partialView, string snippetName = null, int userId = 0) { - if (CreatingPartialView.IsRaisedEventCancelled(new NewEventArgs(partialView, true, partialView.Alias, -1), this)) - return Attempt.Fail(); + return CreatePartialViewMacro(partialView, PartialViewType.PartialView, snippetName, userId); + } + + public Attempt CreatePartialViewMacro(IPartialView partialView, string snippetName = null, int userId = 0) + { + return CreatePartialViewMacro(partialView, PartialViewType.PartialViewMacro, snippetName, userId); + } + + private Attempt CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string snippetName = null, int userId = 0) + { + if (CreatingPartialView.IsRaisedEventCancelled(new NewEventArgs(partialView, true, partialView.Alias, -1), this)) + return Attempt.Fail(); + + var uow = _fileUowProvider.GetUnitOfWork(); + string partialViewHeader = null; + IPartialViewRepository repository; + switch (partialViewType) + { + case PartialViewType.PartialView: + repository = _repositoryFactory.CreatePartialViewRepository(uow); + partialViewHeader = PartialViewHeader; + break; + case PartialViewType.PartialViewMacro: + default: + repository = _repositoryFactory.CreatePartialViewMacroRepository(uow); + partialViewHeader = PartialViewMacroHeader; + break; + } if (snippetName.IsNullOrWhiteSpace() == false) { @@ -463,213 +486,114 @@ namespace Umbraco.Core.Services //strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); - - var content = string.Format("{0}{1}{2}", PartialViewHeader, Environment.NewLine, snippetContent); + + var content = string.Format("{0}{1}{2}", + partialViewHeader, + Environment.NewLine, snippetContent); partialView.Content = content; } } - var uow = _fileUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreatePartialViewRepository(uow)) + using (repository) { repository.AddOrUpdate(partialView); uow.Commit(); - CreatedPartialView.RaiseEvent(new NewEventArgs(partialView, false, partialView.Alias, -1), this); + CreatedPartialView.RaiseEvent(new NewEventArgs(partialView, false, partialView.Alias, -1), this); } - Audit.Add(AuditTypes.Save, string.Format("Save PartialView performed by user"), userId, -1); + Audit.Add(AuditTypes.Save, string.Format("Save {0} performed by user", partialViewType), userId, -1); - return Attempt.Succeed(partialView); + return Attempt.Succeed(partialView); } - internal Attempt CreatePartialViewMacro(PartialView partialView, bool createMacro, string snippetName = null, int userId = 0) + public bool DeletePartialView(string path, int userId = 0) { - if (CreatingPartialView.IsRaisedEventCancelled(new NewEventArgs(partialView, true, partialView.Alias, -1), this)) - return Attempt.Fail(); - - if (snippetName.IsNullOrWhiteSpace() == false) - { - //create the file - var snippetPathAttempt = TryGetSnippetPath(snippetName); - if (snippetPathAttempt.Success == false) - { - throw new InvalidOperationException("Could not load snippet with name " + snippetName); - } - - using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result))) - { - var snippetContent = snippetFile.ReadToEnd().Trim(); - - //strip the @inherits if it's there - snippetContent = StripPartialViewHeader(snippetContent); - - var content = string.Format("{0}{1}{2}", PartialViewMacroHeader, Environment.NewLine, snippetContent); - partialView.Content = content; - } - } - - var uow = _fileUowProvider.GetUnitOfWork(); - var duow = _dataUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreatePartialViewMacroRepository(uow, duow)) - { - repository.AddOrUpdate(partialView); - - if (createMacro) - { - var name = Path.GetFileNameWithoutExtension(partialView.Path) - .SplitPascalCasing() - .ToFirstUpperInvariant() - .ToSafeAlias(false); - - //The partial view path to be saved with the macro must be a fully qualified virtual path - var virtualPath = string.Format("{0}/{1}/{2}", SystemDirectories.MvcViews, "MacroPartials", partialView.Path); - - repository.AddOrUpdate(new Macro(name, name) { ScriptPath = virtualPath }); - } - - //commit both - ensure that the macro is created if one was added - uow.Commit(); - duow.Commit(); - - CreatedPartialView.RaiseEvent(new NewEventArgs(partialView, false, partialView.Alias, -1), this); - } - - Audit.Add(AuditTypes.Save, string.Format("Save PartialViewMacro performed by user"), userId, -1); - - return Attempt.Succeed(partialView); + return DeletePartialViewMacro(path, PartialViewType.PartialView, userId); } - internal bool DeletePartialView(string path, int userId = 0) + public bool DeletePartialViewMacro(string path, int userId = 0) + { + return DeletePartialViewMacro(path, PartialViewType.PartialViewMacro, userId); + } + + private bool DeletePartialViewMacro(string path, PartialViewType partialViewType, int userId = 0) { var uow = _fileUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreatePartialViewRepository(uow)) + + IPartialViewRepository repository; + switch (partialViewType) + { + case PartialViewType.PartialView: + repository = _repositoryFactory.CreatePartialViewRepository(uow); + break; + case PartialViewType.PartialViewMacro: + default: + repository = _repositoryFactory.CreatePartialViewMacroRepository(uow); + break; + } + + using (repository) { var partialView = repository.Get(path); if (partialView == null) return true; - if (DeletingPartialView.IsRaisedEventCancelled(new DeleteEventArgs(partialView), this)) + if (DeletingPartialView.IsRaisedEventCancelled(new DeleteEventArgs(partialView), this)) return false; repository.Delete(partialView); uow.Commit(); - DeletedPartialView.RaiseEvent(new DeleteEventArgs(partialView, false), this); + DeletedPartialView.RaiseEvent(new DeleteEventArgs(partialView, false), this); - Audit.Add(AuditTypes.Delete, string.Format("Delete PartialView performed by user"), userId, -1); + Audit.Add(AuditTypes.Delete, string.Format("Delete {0} performed by user", partialViewType), userId, -1); } return true; } - internal bool DeletePartialViewMacro(string path, int userId = 0) + public Attempt SavePartialView(IPartialView partialView, int userId = 0) { - var uow = _fileUowProvider.GetUnitOfWork(); - var duow = _dataUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreatePartialViewMacroRepository(uow, duow)) - { - var partialView = repository.Get(path); - if (partialView == null) - return true; - - if (DeletingPartialView.IsRaisedEventCancelled(new DeleteEventArgs(partialView), this)) - return false; - - repository.Delete(partialView); - - //commit both (though in the case of deleting, there's no db changes) - uow.Commit(); - duow.Commit(); - - DeletedPartialView.RaiseEvent(new DeleteEventArgs(partialView, false), this); - - Audit.Add(AuditTypes.Delete, string.Format("Delete PartialViewMacro performed by user"), userId, -1); - } - - return true; - + return SavePartialView(partialView, PartialViewType.PartialView, userId); } - internal Attempt SavePartialView(PartialView partialView, int userId = 0) + public Attempt SavePartialViewMacro(IPartialView partialView, int userId = 0) { - if (SavingPartialView.IsRaisedEventCancelled(new SaveEventArgs(partialView), this)) - return Attempt.Fail(); + return SavePartialView(partialView, PartialViewType.PartialViewMacro, userId); + } + + private Attempt SavePartialView(IPartialView partialView, PartialViewType partialViewType, int userId = 0) + { + if (SavingPartialView.IsRaisedEventCancelled(new SaveEventArgs(partialView), this)) + return Attempt.Fail(); var uow = _fileUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreatePartialViewRepository(uow)) + + IPartialViewRepository repository; + switch (partialViewType) + { + case PartialViewType.PartialView: + repository = _repositoryFactory.CreatePartialViewRepository(uow); + break; + case PartialViewType.PartialViewMacro: + default: + repository = _repositoryFactory.CreatePartialViewMacroRepository(uow); + break; + } + + using (repository) { repository.AddOrUpdate(partialView); uow.Commit(); - SavedPartialView.RaiseEvent(new SaveEventArgs(partialView, false), this); + SavedPartialView.RaiseEvent(new SaveEventArgs(partialView, false), this); } - Audit.Add(AuditTypes.Save, string.Format("Save PartialView performed by user"), userId, -1); + Audit.Add(AuditTypes.Save, string.Format("Save {0} performed by user", partialViewType), userId, -1); - ////NOTE: I've left the below here just for informational purposes. If we save a file this way, then the UTF8 - //// BOM mucks everything up, strangely, if we use WriteAllText everything is ok! - //// http://issues.umbraco.org/issue/U4-2118 - ////using (var sw = System.IO.File.CreateText(savePath)) - ////{ - //// sw.Write(val); - ////} - - //System.IO.File.WriteAllText(partialView.Path, partialView.Content, Encoding.UTF8); - ////deletes the old file - //if (partialView.FileName != partialView.OldFileName) - //{ - // // Create a new PartialView class so that we can set the FileName of the file that needs deleting - // var deletePartial = partialView; - // deletePartial.FileName = partialView.OldFileName; - // DeletePartialView(deletePartial, userId); - //} - - //SavedPartialView.RaiseEvent(new SaveEventArgs(partialView), this); - - return Attempt.Succeed(partialView); - } - - internal Attempt SavePartialViewMacro(PartialView partialView, int userId = 0) - { - if (SavingPartialView.IsRaisedEventCancelled(new SaveEventArgs(partialView), this)) - return Attempt.Fail(); - - var uow = _fileUowProvider.GetUnitOfWork(); - var duow = _dataUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreatePartialViewMacroRepository(uow, duow)) - { - repository.AddOrUpdate(partialView); - - //commit both (though in the case of updating, there's no db changes) - uow.Commit(); - duow.Commit(); - - SavedPartialView.RaiseEvent(new SaveEventArgs(partialView, false), this); - } - - Audit.Add(AuditTypes.Save, string.Format("Save PartialViewMacro performed by user"), userId, -1); - - ////NOTE: I've left the below here just for informational purposes. If we save a file this way, then the UTF8 - //// BOM mucks everything up, strangely, if we use WriteAllText everything is ok! - //// http://issues.umbraco.org/issue/U4-2118 - ////using (var sw = System.IO.File.CreateText(savePath)) - ////{ - //// sw.Write(val); - ////} - - //System.IO.File.WriteAllText(partialView.Path, partialView.Content, Encoding.UTF8); - ////deletes the old file - //if (partialView.FileName != partialView.OldFileName) - //{ - // // Create a new PartialView class so that we can set the FileName of the file that needs deleting - // var deletePartial = partialView; - // deletePartial.FileName = partialView.OldFileName; - // DeletePartialView(deletePartial, userId); - //} - - //SavedPartialView.RaiseEvent(new SaveEventArgs(partialView), this); + SavedPartialView.RaiseEvent(new SaveEventArgs(partialView), this); return Attempt.Succeed(partialView); } @@ -696,7 +620,14 @@ namespace Umbraco.Core.Services return System.IO.File.Exists(snippetPath) ? Attempt.Succeed(snippetPath) : Attempt.Fail(); - } + } + + private enum PartialViewType + { + PartialView, + PartialViewMacro + } + #endregion //TODO Method to change name and/or alias of view/masterpage template @@ -765,32 +696,32 @@ namespace Umbraco.Core.Services /// /// Occurs before Save /// - internal static event TypedEventHandler> SavingPartialView; + public static event TypedEventHandler> SavingPartialView; /// /// Occurs after Save /// - internal static event TypedEventHandler> SavedPartialView; + public static event TypedEventHandler> SavedPartialView; /// /// Occurs before Create /// - internal static event TypedEventHandler> CreatingPartialView; + public static event TypedEventHandler> CreatingPartialView; /// /// Occurs after Create /// - internal static event TypedEventHandler> CreatedPartialView; + public static event TypedEventHandler> CreatedPartialView; /// /// Occurs before Delete /// - internal static event TypedEventHandler> DeletingPartialView; + public static event TypedEventHandler> DeletingPartialView; /// /// Occurs after Delete /// - internal static event TypedEventHandler> DeletedPartialView; + public static event TypedEventHandler> DeletedPartialView; #endregion diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 9bcce63373..924fb0c093 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -8,6 +8,18 @@ namespace Umbraco.Core.Services /// public interface IFileService : IService { + IEnumerable GetPartialViewSnippetNames(params string[] filterNames); + void DeletePartialViewFolder(string folderPath); + void DeletePartialViewMacroFolder(string folderPath); + IPartialView GetPartialView(string path); + IPartialView GetPartialViewMacro(string path); + Attempt CreatePartialView(IPartialView partialView, string snippetName = null, int userId = 0); + Attempt CreatePartialViewMacro(IPartialView partialView, string snippetName = null, int userId = 0); + bool DeletePartialView(string path, int userId = 0); + bool DeletePartialViewMacro(string path, int userId = 0); + Attempt SavePartialView(IPartialView partialView, int userId = 0); + Attempt SavePartialViewMacro(IPartialView partialView, int userId = 0); + /// /// Gets a list of all objects /// diff --git a/src/Umbraco.Core/Services/MacroService.cs b/src/Umbraco.Core/Services/MacroService.cs index fcfbe6d87a..2cff1fcf1a 100644 --- a/src/Umbraco.Core/Services/MacroService.cs +++ b/src/Umbraco.Core/Services/MacroService.cs @@ -209,5 +209,7 @@ namespace Umbraco.Core.Services /// public static event TypedEventHandler> Saved; #endregion + + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 7bd9b9ee1a..56a7d3e5b0 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -316,6 +316,7 @@ + @@ -403,7 +404,6 @@ - diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs index 51cff29f25..793a4e3e07 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using Umbraco.Core.CodeAnnotations; using Umbraco.Core.Configuration; using Umbraco.Core.IO; @@ -43,9 +44,10 @@ namespace umbraco { fileName += ".cshtml"; } - + var model = new PartialView(fileName); var fileService = (FileService)ApplicationContext.Current.Services.FileService; + var macroService = ApplicationContext.Current.Services.MacroService; if (IsPartialViewMacro == false) { @@ -59,8 +61,15 @@ namespace umbraco return attempt.Success; } else - { - var attempt = fileService.CreatePartialViewMacro(model, ParentID == 1, snippetName, User.Id); + { + + var attempt = fileService.CreatePartialViewMacro(model, /*ParentID == 1,*/ snippetName, User.Id); + if (attempt) + { + //The partial view path to be saved with the macro must be a fully qualified virtual path + var virtualPath = string.Format("{0}/{1}/{2}", SystemDirectories.MvcViews, "MacroPartials", attempt.Result.Path); + macroService.Save(new Macro(attempt.Result.Alias, attempt.Result.Alias) { ScriptPath = virtualPath }); + } //TODO: We currently need to hack this because we are using the same editor for views, partial views, partial view macros and // the editor is using normal UI whereas the partial view repo and these classes are using IFileSystem with relative references @@ -69,13 +78,13 @@ namespace umbraco _returnUrl = string.Format("settings/views/EditView.aspx?treeType=partialViewMacros&file={0}", model.Path.TrimStart('/').EnsureStartsWith("MacroPartials/")); return attempt.Success; } - + } public override bool PerformDelete() { var fileService = (FileService)ApplicationContext.Current.Services.FileService; - + if (IsPartialViewMacro == false) { if (Alias.Contains(".") == false)