diff --git a/src/Umbraco.Core/Models/PartialView.cs b/src/Umbraco.Core/Models/PartialView.cs
new file mode 100644
index 0000000000..ff3dbbe367
--- /dev/null
+++ b/src/Umbraco.Core/Models/PartialView.cs
@@ -0,0 +1,76 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.Text.RegularExpressions;
+using Umbraco.Core.IO;
+
+namespace Umbraco.Core.Models
+{
+ ///
+ /// Represents a Partial View file
+ ///
+ [Serializable]
+ [DataContract(IsReference = true)]
+ internal class PartialView : File
+ {
+ private readonly Regex _headerMatch = new Regex("^@inherits\\s+?.*$", RegexOptions.Multiline | RegexOptions.Compiled);
+
+ public PartialView(string path)
+ : base(path)
+ {
+ base.Path = path;
+ }
+
+ ///
+ /// Boolean indicating whether the file could be validated
+ ///
+ /// True if file is valid, otherwise false
+ public override bool IsValid()
+ {
+ //Validate extension
+ var validExtension = IOHelper.VerifyFileExtension(Path, new List { "cshtml" });
+
+ return validExtension;
+ }
+
+ public string FileName { get; set; }
+
+ public string SnippetName { get; set; }
+
+ public bool CreateMacro { get; set; }
+
+ public string CodeHeader { get; set; }
+
+ public string ParentFolderName { get; set; }
+
+ public string AssignedApp { get; set; }
+
+ public string EditViewFile { get; set; }
+
+ public string BasePath { get; set; }
+
+ public string ReturnUrl { get; set; }
+
+ public bool SaveSucceeded { get; set; }
+
+ internal Regex HeaderMatch
+ {
+ get
+ {
+ return _headerMatch;
+ }
+ }
+
+ internal Attempt TryGetSnippetPath(string fileName)
+ {
+ var partialViewsFileSystem = new PhysicalFileSystem(BasePath);
+ var snippetPath = IOHelper.MapPath(string.Format("{0}/PartialViewMacros/Templates/{1}", SystemDirectories.Umbraco, fileName));
+
+ return partialViewsFileSystem.FileExists(snippetPath)
+ ? Attempt.Succeed(snippetPath)
+ : Attempt.Fail();
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs
index 848097f6f0..bddbf04910 100644
--- a/src/Umbraco.Core/Services/FileService.cs
+++ b/src/Umbraco.Core/Services/FileService.cs
@@ -1,7 +1,12 @@
using System;
using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using System.Web;
using Umbraco.Core.Auditing;
using Umbraco.Core.Events;
+using Umbraco.Core.IO;
+using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Repositories;
@@ -362,6 +367,95 @@ namespace Umbraco.Core.Services
return template.IsValid();
}
+ internal PartialView CreatePartialView(PartialView partialView)
+ {
+ var partialViewsFileSystem = new PhysicalFileSystem(partialView.BasePath);
+ var relativeFilePath = partialView.ParentFolderName.EnsureEndsWith('/') + partialViewsFileSystem.GetRelativePath(partialView.FileName);
+
+ //return the link to edit the file if it already exists
+ if (partialViewsFileSystem.FileExists(partialView.Path))
+ {
+ partialView.ReturnUrl = string.Format(partialView.EditViewFile + "?file={0}", HttpUtility.UrlEncode(relativeFilePath));
+ partialView.SaveSucceeded = true;
+ return partialView;
+ }
+
+ if (CreatingPartialView.IsRaisedEventCancelled(new NewEventArgs(partialView, true, partialView.Alias, -1), this))
+ {
+ partialView.SaveSucceeded = false;
+ return partialView;
+ }
+
+ //create the file
+ var snippetPathAttempt = partialView.TryGetSnippetPath(partialView.SnippetName);
+ if (snippetPathAttempt.Success == false)
+ {
+ throw new InvalidOperationException("Could not load template with name " + partialView.SnippetName);
+ }
+
+ using (var snippetFile = new StreamReader(partialViewsFileSystem.OpenFile(snippetPathAttempt.Result)))
+ {
+ var snippetContent = snippetFile.ReadToEnd().Trim();
+
+ //strip the @inherits if it's there
+ snippetContent = partialView.HeaderMatch.Replace(snippetContent, string.Empty);
+
+ var content = string.Format("{0}{1}{2}", partialView.CodeHeader, Environment.NewLine, snippetContent);
+
+ var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
+ partialViewsFileSystem.AddFile(partialView.Path, stream);
+ }
+
+ if (partialView.CreateMacro)
+ CreatePartialViewMacro(partialView);
+
+ partialView.ReturnUrl = string.Format(partialView.EditViewFile + "?file={0}", HttpUtility.UrlEncode(relativeFilePath));
+
+
+ CreatedPartialView.RaiseEvent(new NewEventArgs(partialView, false, partialView.Alias, -1), this);
+
+ return partialView;
+ }
+
+ internal static void CreatePartialViewMacro(PartialView partialView)
+ {
+ var name = partialView.FileName.Substring(0, (partialView.FileName.LastIndexOf('.') + 1))
+ .Trim('.')
+ .SplitPascalCasing()
+ .ToFirstUpperInvariant();
+
+ var macroService = new MacroService();
+ var macro = new Macro(name, name) { ScriptPath = partialView.BasePath + partialView.FileName };
+ macroService.Save(macro);
+ }
+
+ internal bool DeletePartialView(PartialView partialView, int userId = 0)
+ {
+ var partialViewsFileSystem = new PhysicalFileSystem(partialView.BasePath);
+
+ if (DeletingPartialView.IsRaisedEventCancelled(new DeleteEventArgs(partialView), this))
+ {
+ return false;
+ }
+
+ if (partialViewsFileSystem.FileExists(partialView.FileName))
+ {
+ partialViewsFileSystem.DeleteFile(partialView.FileName);
+ LogHelper.Info(string.Format("Partial View file {0} deleted by user {1}", partialViewsFileSystem.GetFullPath(partialView.FileName), userId));
+ }
+ // TODO: does this ever even happen? I don't think folders show up in the tree currently.
+ // Leaving this here as it was in the original PartialViewTasks code - SJ
+ else if (partialViewsFileSystem.DirectoryExists(partialView.FileName))
+ {
+ partialViewsFileSystem.DeleteDirectory(partialView.FileName, true);
+ LogHelper.Info(string.Format("Partial View directory {0} deleted by user {1}", partialViewsFileSystem.GetFullPath(partialView.FileName), userId));
+ }
+
+ DeletedPartialView.RaiseEvent(new DeleteEventArgs(partialView, false), this);
+
+ return true;
+ }
+
//TODO Method to change name and/or alias of view/masterpage template
#region Event Handlers
@@ -425,6 +519,26 @@ namespace Umbraco.Core.Services
///
public static event TypedEventHandler> SavedStylesheet;
+ ///
+ /// Occurs before Create
+ ///
+ internal static event TypedEventHandler> CreatingPartialView;
+
+ ///
+ /// Occurs after Create
+ ///
+ internal static event TypedEventHandler> CreatedPartialView;
+
+ ///
+ /// Occurs before Delete
+ ///
+ internal static event TypedEventHandler> DeletingPartialView;
+
+ ///
+ /// Occurs after Delete
+ ///
+ internal static event TypedEventHandler> DeletedPartialView;
+
#endregion
}
}
\ No newline at end of file
diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj
index 096ee04948..70fa8eddc5 100644
--- a/src/Umbraco.Core/Umbraco.Core.csproj
+++ b/src/Umbraco.Core/Umbraco.Core.csproj
@@ -366,6 +366,7 @@
+
diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs
index 30740d3f5e..7a096bf35f 100644
--- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs
+++ b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs
@@ -1,18 +1,11 @@
using System;
-using System.IO;
-using System.Text;
-using System.Text.RegularExpressions;
-using System.Web;
using Umbraco.Core.CodeAnnotations;
-using Umbraco.Core.Events;
using Umbraco.Core.IO;
-using Umbraco.Core.Logging;
using Umbraco.Core.Models;
-using Umbraco.Web.Mvc;
+using Umbraco.Core.Services;
using Umbraco.Web.UI;
using umbraco.BasePages;
using Umbraco.Core;
-using umbraco.BusinessLogic;
namespace umbraco
{
@@ -22,12 +15,21 @@ namespace umbraco
[UmbracoWillObsolete("http://issues.umbraco.org/issue/U4-1373", "This will one day be removed when we overhaul the create process")]
public abstract class PartialViewTasksBase : LegacyDialogTask
{
- private readonly Regex _headerMatch = new Regex("^@inherits\\s+?.*$", RegexOptions.Multiline | RegexOptions.Compiled);
+ private string _returnUrl = "";
+ public override string ReturnUrl
+ {
+ get { return _returnUrl; }
+ }
protected abstract string CodeHeader { get; }
protected abstract string ParentFolderName { get; }
+ public override string AssignedApp
+ {
+ get { return string.Empty; }
+ }
+
protected virtual string EditViewFile
{
get { return "Settings/Views/EditView.aspx"; }
@@ -49,67 +51,26 @@ namespace umbraco
}
var partialViewsFileSystem = new PhysicalFileSystem(BasePath);
- var relativeFilePath = ParentFolderName.EnsureEndsWith('/') + partialViewsFileSystem.GetRelativePath(fileName);
var fullFilePath = partialViewsFileSystem.GetFullPath(fileName);
- //return the link to edit the file if it already exists
- if (partialViewsFileSystem.FileExists(fullFilePath))
- {
- _returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(relativeFilePath));
- return true;
- }
+ var model = new PartialView(fullFilePath)
+ {
+ FileName = fileName,
+ SnippetName = snippetName,
+ CreateMacro = ParentID == 1,
+ CodeHeader = CodeHeader,
+ ParentFolderName = ParentFolderName,
+ AssignedApp = AssignedApp,
+ EditViewFile = EditViewFile,
+ BasePath = BasePath
+ };
- if (Creating.IsRaisedEventCancelled(new NewEventArgs(fullFilePath, fileName, ParentFolderName), this))
- {
- return false;
- }
+ var fileService = new FileService();
+ var partialView = fileService.CreatePartialView(model);
- //create the file
- var snippetPathAttempt = TryGetSnippetPath(snippetName);
- if (snippetPathAttempt.Success == false)
- {
- throw new InvalidOperationException("Could not load template with name " + snippetName);
- }
+ _returnUrl = partialView.ReturnUrl;
- using (var snippetFile = new StreamReader(partialViewsFileSystem.OpenFile(snippetPathAttempt.Result)))
- {
- var snippetContent = snippetFile.ReadToEnd().Trim();
-
- //strip the @inherits if it's there
- snippetContent = _headerMatch.Replace(snippetContent, string.Empty);
-
- var content = string.Format("{0}{1}{2}", CodeHeader, Environment.NewLine, snippetContent);
-
- var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
- partialViewsFileSystem.AddFile(fullFilePath, stream);
- }
-
- // Create macro?
- if (ParentID == 1)
- {
- var name = fileName.Substring(0, (fileName.LastIndexOf('.') + 1))
- .Trim('.')
- .SplitPascalCasing()
- .ToFirstUpperInvariant();
-
- var m = cms.businesslogic.macro.Macro.MakeNew(name);
-
- m.ScriptingFile = BasePath + fileName;
- m.Save();
- }
-
- _returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(relativeFilePath));
-
- Created.RaiseEvent(new NewEventArgs(fullFilePath, fileName, ParentFolderName), this);
-
- return true;
- }
-
- protected virtual void WriteTemplateHeader(StreamWriter sw)
- {
- //write out the template header
- sw.Write("@inherits ");
- sw.Write(typeof(UmbracoTemplatePage).FullName.TrimEnd("`1"));
+ return partialView.SaveSucceeded;
}
public override bool PerformDelete()
@@ -117,64 +78,11 @@ namespace umbraco
var partialViewsFileSystem = new PhysicalFileSystem(BasePath);
var path = Alias.TrimStart('/');
var fullFilePath = partialViewsFileSystem.GetFullPath(path);
-
- if (Deleting.IsRaisedEventCancelled(new DeleteEventArgs(fullFilePath), this))
- {
- return false;
- }
-
- if (partialViewsFileSystem.FileExists(path))
- partialViewsFileSystem.DeleteFile(path);
- else if (partialViewsFileSystem.DirectoryExists(path))
- partialViewsFileSystem.DeleteDirectory(path, true);
-
- LogHelper.Info(string.Format("{0} Deleted by user {1}", Alias, UmbracoEnsuredPage.CurrentUser.Id));
-
- Deleted.RaiseEvent(new DeleteEventArgs(fullFilePath, false), this);
-
- return true;
- }
-
-
- private string _returnUrl = "";
- public override string ReturnUrl
- {
- get { return _returnUrl; }
- }
-
- public override string AssignedApp
- {
- get { return string.Empty; }
- }
-
- private Attempt TryGetSnippetPath(string fileName)
- {
- var partialViewsFileSystem = new PhysicalFileSystem(BasePath);
- var snippetPath = IOHelper.MapPath(string.Format("{0}/PartialViewMacros/Templates/{1}", SystemDirectories.Umbraco, fileName));
- return partialViewsFileSystem.FileExists(snippetPath)
- ? Attempt.Succeed(snippetPath)
- : Attempt.Fail();
+ var model = new PartialView(fullFilePath) { BasePath = BasePath, FileName = path };
+
+ var fileService = new FileService();
+ return fileService.DeletePartialView(model, UmbracoEnsuredPage.CurrentUser.Id);
}
-
- ///
- /// Occurs before Create
- ///
- internal static event TypedEventHandler> Creating;
-
- ///
- /// Occurs after Create
- ///
- internal static event TypedEventHandler> Created;
-
- ///
- /// Occurs before Delete
- ///
- internal static event TypedEventHandler> Deleting;
-
- ///
- /// Occurs after Delete
- ///
- internal static event TypedEventHandler> Deleted;
}
}