From c0a303b328237fd39bfc312a6fcc237281d73163 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 19 Sep 2013 17:34:57 +1000 Subject: [PATCH] Gets macro editor working with new db changes and using new MacroService data layer --- src/Umbraco.Core/CoreBootManager.cs | 4 +- src/Umbraco.Core/Manifest/ManifestBuilder.cs | 11 +- src/Umbraco.Core/Models/IMacro.cs | 12 +- src/Umbraco.Core/Models/IMacroProperty.cs | 5 +- src/Umbraco.Core/Models/Macro.cs | 29 +-- src/Umbraco.Core/Models/MacroProperty.cs | 43 ++++ .../Models/MacroPropertyCollection.cs | 2 +- src/Umbraco.Core/ObservableDictionary.cs | 2 +- .../Persistence/Factories/MacroFactory.cs | 5 +- src/Umbraco.Core/PluginManager.cs | 10 + .../PropertyEditors/ParameterValueEditor.cs | 6 + src/Umbraco.Core/Services/IMacroService.cs | 1 + src/Umbraco.Core/Services/MacroService.cs | 30 +++ .../Services/MacroServiceTests.cs | 85 ++++++++ .../developer/Macros/EditMacro.aspx.cs | 16 +- .../umbraco/developer/Macros/editMacro.aspx | 18 +- .../umbraco/controls/macroParameterControl.cs | 9 +- .../developer/Macros/editMacro.aspx.cs | 193 ++++++++---------- .../umbraco/webservices/Developer.asmx.cs | 9 +- src/umbraco.cms/businesslogic/macro/Macro.cs | 1 + .../businesslogic/macro/MacroComment.cs | 36 ---- .../businesslogic/macro/MacroProperty.cs | 36 +++- .../businesslogic/macro/macroPropertyType.cs | 15 +- src/umbraco.cms/umbraco.cms.csproj | 3 - .../macrocontainer/MacroEditor.cs | 26 +-- 25 files changed, 368 insertions(+), 239 deletions(-) delete mode 100644 src/umbraco.cms/businesslogic/macro/MacroComment.cs diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index 527f853419..243ea71f52 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -257,9 +257,9 @@ namespace Umbraco.Core /// protected virtual void InitializeResolvers() { - - //setup custom resolvers... this is only temporary until we move into the umbraco core PropertyEditorResolver.Current = new PropertyEditorResolver(() => PluginManager.Current.ResolvePropertyEditors()); + ParameterEditorResolver.Current = new ParameterEditorResolver(() => PluginManager.Current.ResolveParameterEditors()); + //setup the validators resolver with our predefined validators ValidatorsResolver.Current = new ValidatorsResolver(new[] { diff --git a/src/Umbraco.Core/Manifest/ManifestBuilder.cs b/src/Umbraco.Core/Manifest/ManifestBuilder.cs index 66610824d7..fe7d83304f 100644 --- a/src/Umbraco.Core/Manifest/ManifestBuilder.cs +++ b/src/Umbraco.Core/Manifest/ManifestBuilder.cs @@ -31,7 +31,11 @@ namespace Umbraco.Core.Manifest var editors = new List(); foreach (var manifest in GetManifests()) { - editors.AddRange(ManifestParser.GetPropertyEditors(manifest.PropertyEditors)); + if (manifest.PropertyEditors != null) + { + editors.AddRange(ManifestParser.GetPropertyEditors(manifest.PropertyEditors)); + } + } return editors; }); @@ -52,7 +56,10 @@ namespace Umbraco.Core.Manifest var editors = new List(); foreach (var manifest in GetManifests()) { - editors.AddRange(ManifestParser.GetParameterEditors(manifest.ParameterEditors)); + if (manifest.ParameterEditors != null) + { + editors.AddRange(ManifestParser.GetParameterEditors(manifest.ParameterEditors)); + } } return editors; }); diff --git a/src/Umbraco.Core/Models/IMacro.cs b/src/Umbraco.Core/Models/IMacro.cs index 586b4aae82..7ca3d8d18f 100644 --- a/src/Umbraco.Core/Models/IMacro.cs +++ b/src/Umbraco.Core/Models/IMacro.cs @@ -7,7 +7,7 @@ namespace Umbraco.Core.Models /// /// Defines a Macro /// - internal interface IMacro : IAggregateRoot + public interface IMacro : IAggregateRoot { /// /// Gets or sets the alias of the Macro @@ -84,10 +84,10 @@ namespace Umbraco.Core.Models [DataMember] MacroPropertyCollection Properties { get; } - /// - /// Returns an enum based on the properties on the Macro - /// - /// - MacroTypes MacroType(); + ///// + ///// Returns an enum based on the properties on the Macro + ///// + ///// + //MacroTypes MacroType(); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Models/IMacroProperty.cs b/src/Umbraco.Core/Models/IMacroProperty.cs index f02297c14c..7e8e60690f 100644 --- a/src/Umbraco.Core/Models/IMacroProperty.cs +++ b/src/Umbraco.Core/Models/IMacroProperty.cs @@ -6,8 +6,11 @@ namespace Umbraco.Core.Models /// /// Defines a Property for a Macro /// - internal interface IMacroProperty : IValueObject + public interface IMacroProperty : IValueObject { + [DataMember] + int Id { get; set; } + /// /// Gets or sets the Alias of the Property /// diff --git a/src/Umbraco.Core/Models/Macro.cs b/src/Umbraco.Core/Models/Macro.cs index 63ce05ea4c..c0aa35e782 100644 --- a/src/Umbraco.Core/Models/Macro.cs +++ b/src/Umbraco.Core/Models/Macro.cs @@ -379,33 +379,6 @@ namespace Umbraco.Core.Models get { return _properties; } } - /// - /// Returns an enum based on the properties on the Macro - /// - /// - public MacroTypes MacroType() - { - if (string.IsNullOrEmpty(XsltPath) == false) - return MacroTypes.Xslt; - - if (string.IsNullOrEmpty(ScriptPath) == false) - { - //we need to check if the file path saved is a virtual path starting with ~/Views/MacroPartials, if so then this is - //a partial view macro, not a script macro - //we also check if the file exists in ~/App_Plugins/[Packagename]/Views/MacroPartials, if so then it is also a partial view. - return (ScriptPath.InvariantStartsWith(SystemDirectories.MvcViews + "/MacroPartials/") - || (Regex.IsMatch(ScriptPath, "~/App_Plugins/.+?/Views/MacroPartials", RegexOptions.Compiled | RegexOptions.IgnoreCase))) - ? MacroTypes.PartialView - : MacroTypes.Script; - } - - if (string.IsNullOrEmpty(ControlType) == false && ControlType.InvariantContains(".ascx")) - return MacroTypes.UserControl; - - if (string.IsNullOrEmpty(ControlType) == false && string.IsNullOrEmpty(ControlAssembly) == false) - return MacroTypes.CustomControl; - - return MacroTypes.Unknown; - } + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Models/MacroProperty.cs b/src/Umbraco.Core/Models/MacroProperty.cs index 3ac6d7838d..7752d82814 100644 --- a/src/Umbraco.Core/Models/MacroProperty.cs +++ b/src/Umbraco.Core/Models/MacroProperty.cs @@ -17,6 +17,13 @@ namespace Umbraco.Core.Models } + /// + /// Ctor for creating a new property + /// + /// + /// + /// + /// public MacroProperty(string @alias, string name, int sortOrder, string editorAlias) { _alias = alias; @@ -25,16 +32,52 @@ namespace Umbraco.Core.Models _editorAlias = editorAlias; } + /// + /// Ctor for creating an existing property + /// + /// + /// + /// + /// + /// + internal MacroProperty(int id, string @alias, string name, int sortOrder, string editorAlias) + { + _id = id; + _alias = alias; + _name = name; + _sortOrder = sortOrder; + _editorAlias = editorAlias; + } + private string _alias; private string _name; private int _sortOrder; + private int _id; private string _editorAlias; private static readonly PropertyInfo AliasSelector = ExpressionHelper.GetPropertyInfo(x => x.Alias); private static readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo(x => x.Name); private static readonly PropertyInfo SortOrderSelector = ExpressionHelper.GetPropertyInfo(x => x.SortOrder); + private static readonly PropertyInfo IdSelector = ExpressionHelper.GetPropertyInfo(x => x.Id); private static readonly PropertyInfo PropertyTypeSelector = ExpressionHelper.GetPropertyInfo(x => x.EditorAlias); + /// + /// Gets or sets the Alias of the Property + /// + [DataMember] + public int Id + { + get { return _id; } + set + { + SetPropertyValueAndDetectChanges(o => + { + _id = value; + return _alias; + }, _alias, IdSelector); + } + } + /// /// Gets or sets the Alias of the Property /// diff --git a/src/Umbraco.Core/Models/MacroPropertyCollection.cs b/src/Umbraco.Core/Models/MacroPropertyCollection.cs index e9614ba72b..c8c466c368 100644 --- a/src/Umbraco.Core/Models/MacroPropertyCollection.cs +++ b/src/Umbraco.Core/Models/MacroPropertyCollection.cs @@ -8,7 +8,7 @@ namespace Umbraco.Core.Models /// /// A macro's property collection /// - internal class MacroPropertyCollection : ObservableDictionary + public class MacroPropertyCollection : ObservableDictionary { public MacroPropertyCollection() : base(property => property.Alias) diff --git a/src/Umbraco.Core/ObservableDictionary.cs b/src/Umbraco.Core/ObservableDictionary.cs index e7fc899718..29ba500805 100644 --- a/src/Umbraco.Core/ObservableDictionary.cs +++ b/src/Umbraco.Core/ObservableDictionary.cs @@ -16,7 +16,7 @@ namespace Umbraco.Core /// /// The type of elements contained in the BindableCollection /// The type of the indexing key - internal class ObservableDictionary : ObservableCollection + public class ObservableDictionary : ObservableCollection { protected Dictionary Indecies = new Dictionary(); protected Func KeySelector; diff --git a/src/Umbraco.Core/Persistence/Factories/MacroFactory.cs b/src/Umbraco.Core/Persistence/Factories/MacroFactory.cs index 859714393f..206244d260 100644 --- a/src/Umbraco.Core/Persistence/Factories/MacroFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/MacroFactory.cs @@ -14,7 +14,7 @@ namespace Umbraco.Core.Persistence.Factories var model = new Macro(dto.Id, dto.UseInEditor, dto.RefreshRate, dto.Alias, dto.Name, dto.ScriptType, dto.ScriptAssembly, dto.Xslt, dto.CacheByPage, dto.CachePersonalized, dto.DontRender, dto.Python); foreach (var p in dto.MacroPropertyDtos) { - model.Properties.Add(new MacroProperty(p.Alias, p.Name, p.SortOrder, null)); + model.Properties.Add(new MacroProperty(p.Id, p.Alias, p.Name, p.SortOrder, p.EditorAlias)); } //on initial construction we don't want to have dirty properties tracked @@ -60,7 +60,8 @@ namespace Umbraco.Core.Persistence.Factories Name = p.Name, Macro = entity.Id, SortOrder = (byte)p.SortOrder, - EditorAlias = p.EditorAlias + EditorAlias = p.EditorAlias, + Id = p.Id }; list.Add(text); diff --git a/src/Umbraco.Core/PluginManager.cs b/src/Umbraco.Core/PluginManager.cs index 50df137ece..e24573e4d4 100644 --- a/src/Umbraco.Core/PluginManager.cs +++ b/src/Umbraco.Core/PluginManager.cs @@ -432,6 +432,16 @@ namespace Umbraco.Core //return all proeprty editor types found except for the base property editor type return ResolveTypes() .Except(new[] {typeof (PropertyEditor)}); + } + + /// + /// Returns all found parameter editors + /// + internal IEnumerable ResolveParameterEditors() + { + //return all paramter editor types found except for the base property editor type + return ResolveTypes() + .Except(new[] { typeof(ParameterEditor) }); } /// diff --git a/src/Umbraco.Core/PropertyEditors/ParameterValueEditor.cs b/src/Umbraco.Core/PropertyEditors/ParameterValueEditor.cs index f318b6e723..31a327d24b 100644 --- a/src/Umbraco.Core/PropertyEditors/ParameterValueEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/ParameterValueEditor.cs @@ -2,6 +2,12 @@ namespace Umbraco.Core.PropertyEditors { + [ParameterEditor("text", "Text", "textbox")] + public class TextParameterEditor : ParameterEditor + { + + } + /// /// Represents the value editor for the parameter editor during macro parameter editing /// diff --git a/src/Umbraco.Core/Services/IMacroService.cs b/src/Umbraco.Core/Services/IMacroService.cs index ce406f8251..3f52d36818 100644 --- a/src/Umbraco.Core/Services/IMacroService.cs +++ b/src/Umbraco.Core/Services/IMacroService.cs @@ -8,6 +8,7 @@ namespace Umbraco.Core.Services /// internal interface IMacroService : IService { + /// /// Gets an object by its alias /// diff --git a/src/Umbraco.Core/Services/MacroService.cs b/src/Umbraco.Core/Services/MacroService.cs index f8a2466b96..6d3b18010f 100644 --- a/src/Umbraco.Core/Services/MacroService.cs +++ b/src/Umbraco.Core/Services/MacroService.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using Umbraco.Core.Auditing; using Umbraco.Core.Events; +using Umbraco.Core.IO; using Umbraco.Core.Models; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Querying; @@ -35,6 +37,34 @@ namespace Umbraco.Core.Services _repositoryFactory = repositoryFactory; } + /// + /// Returns an enum based on the properties on the Macro + /// + /// + internal static MacroTypes GetMacroType(IMacro macro) + { + if (string.IsNullOrEmpty(macro.XsltPath) == false) + return MacroTypes.Xslt; + + if (string.IsNullOrEmpty(macro.ScriptPath) == false) + { + //we need to check if the file path saved is a virtual path starting with ~/Views/MacroPartials, if so then this is + //a partial view macro, not a script macro + //we also check if the file exists in ~/App_Plugins/[Packagename]/Views/MacroPartials, if so then it is also a partial view. + return (macro.ScriptPath.InvariantStartsWith(SystemDirectories.MvcViews + "/MacroPartials/") + || (Regex.IsMatch(macro.ScriptPath, "~/App_Plugins/.+?/Views/MacroPartials", RegexOptions.Compiled | RegexOptions.IgnoreCase))) + ? MacroTypes.PartialView + : MacroTypes.Script; + } + + if (string.IsNullOrEmpty(macro.ControlType) == false && macro.ControlType.InvariantContains(".ascx")) + return MacroTypes.UserControl; + + if (string.IsNullOrEmpty(macro.ControlType) == false && string.IsNullOrEmpty(macro.ControlAssembly) == false) + return MacroTypes.CustomControl; + + return MacroTypes.Unknown; + } /// /// Gets an object by its alias diff --git a/src/Umbraco.Tests/Services/MacroServiceTests.cs b/src/Umbraco.Tests/Services/MacroServiceTests.cs index 308d7fa040..28c62126e1 100644 --- a/src/Umbraco.Tests/Services/MacroServiceTests.cs +++ b/src/Umbraco.Tests/Services/MacroServiceTests.cs @@ -63,6 +63,91 @@ namespace Umbraco.Tests.Services Assert.AreEqual(3, result.Count()); } + [Test] + public void Can_Create() + { + // Arrange + var macroService = ServiceContext.MacroService; + + // Act + var macro = new Macro("test", "Test", scriptPath: "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + macroService.Save(macro); + + //assert + Assert.IsTrue(macro.HasIdentity); + Assert.Greater(macro.Id, 0); + var result = macroService.GetById(macro.Id); + Assert.AreEqual("test", result.Alias); + Assert.AreEqual("Test", result.Name); + Assert.AreEqual("~/Views/MacroPartials/Test.cshtml", result.ScriptPath); + Assert.AreEqual(1234, result.CacheDuration); + } + + [Test] + public void Can_Delete() + { + // Arrange + var macroService = ServiceContext.MacroService; + var macro = new Macro("test", "Test", scriptPath: "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + macroService.Save(macro); + + // Act + macroService.Delete(macro); + + //assert + var result = macroService.GetById(macro.Id); + Assert.IsNull(result); + } + + [Test] + public void Can_Update() + { + // Arrange + var macroService = ServiceContext.MacroService; + IMacro macro = new Macro("test", "Test", scriptPath: "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + macroService.Save(macro); + + // Act + macro.Name = "New name"; + macro.Alias = "NewAlias"; + macroService.Save(macro); + + + macro = macroService.GetById(macro.Id); + + //assert + Assert.AreEqual("New name", macro.Name); + Assert.AreEqual("NewAlias", macro.Alias); + + } + + [Test] + public void Can_Update_Property() + { + // Arrange + var macroService = ServiceContext.MacroService; + IMacro macro = new Macro("test", "Test", scriptPath: "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + macro.Properties.Add(new MacroProperty("blah", "Blah", 0, "blah")); + macroService.Save(macro); + + // Act + macro.Properties.First().Alias = "new Alias"; + macro.Properties.First().Name = "new Name"; + macro.Properties.First().SortOrder = 1; + macro.Properties.First().EditorAlias = "new"; + macroService.Save(macro); + + macro = macroService.GetById(macro.Id); + + //assert + Assert.AreEqual(1, macro.Properties.Count()); + Assert.AreEqual("new Alias", macro.Properties.First().Alias); + Assert.AreEqual("new Name", macro.Properties.First().Name); + Assert.AreEqual(1, macro.Properties.First().SortOrder); + Assert.AreEqual("new", macro.Properties.First().EditorAlias); + + } + //[Test] //public void Can_Get_Many_By_Alias() //{ diff --git a/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs b/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs index 8cc70f83ba..4ae89cc0c5 100644 --- a/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs +++ b/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs @@ -8,7 +8,7 @@ using System.Web; using System.Web.UI.WebControls; using Umbraco.Core.IO; using Umbraco.Core; -using umbraco.cms.businesslogic.macro; +using Umbraco.Core.Models; namespace Umbraco.Web.UI.Umbraco.Developer.Macros { @@ -29,16 +29,16 @@ namespace Umbraco.Web.UI.Umbraco.Developer.Macros /// /// /// - protected override void PopulateFieldsOnLoad(Macro macro, string macroAssemblyValue, string macroTypeValue) + protected override void PopulateFieldsOnLoad(IMacro macro, string macroAssemblyValue, string macroTypeValue) { base.PopulateFieldsOnLoad(macro, macroAssemblyValue, macroTypeValue); //check if the ScriptingFile property contains the MacroPartials path - if (macro.ScriptingFile.IsNullOrWhiteSpace() == false && - (macro.ScriptingFile.StartsWith(SystemDirectories.MvcViews + "/MacroPartials/") - || (Regex.IsMatch(macro.ScriptingFile, "~/App_Plugins/.+?/Views/MacroPartials", RegexOptions.Compiled)))) + if (macro.ScriptPath.IsNullOrWhiteSpace() == false && + (macro.ScriptPath.StartsWith(SystemDirectories.MvcViews + "/MacroPartials/") + || (Regex.IsMatch(macro.ScriptPath, "~/App_Plugins/.+?/Views/MacroPartials", RegexOptions.Compiled)))) { macroPython.Text = ""; - SelectedPartialView.Text = macro.ScriptingFile; + SelectedPartialView.Text = macro.ScriptPath; } } @@ -50,12 +50,12 @@ namespace Umbraco.Web.UI.Umbraco.Developer.Macros /// /// /// - protected override void SetMacroValuesFromPostBack(Macro macro, int macroCachePeriod, string macroAssemblyValue, string macroTypeValue) + protected override void SetMacroValuesFromPostBack(IMacro macro, int macroCachePeriod, string macroAssemblyValue, string macroTypeValue) { base.SetMacroValuesFromPostBack(macro, macroCachePeriod, macroAssemblyValue, macroTypeValue); if (!SelectedPartialView.Text.IsNullOrWhiteSpace()) { - macro.ScriptingFile = SelectedPartialView.Text; + macro.ScriptPath = SelectedPartialView.Text; } } diff --git a/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx b/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx index 28e2a7f65f..9949902022 100644 --- a/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx +++ b/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx @@ -112,13 +112,13 @@ - <%=umbraco.ui.Text("general", "alias",this.getUser())%> + <%=umbraco.ui.Text("general", "alias",UmbracoUser)%> - <%=umbraco.ui.Text("general", "name",this.getUser())%> + <%=umbraco.ui.Text("general", "name",UmbracoUser)%> - <%=umbraco.ui.Text("general", "type",this.getUser())%> + <%=umbraco.ui.Text("general", "type",UmbracoUser)%> @@ -128,18 +128,18 @@ - - + - + + SelectedValue='<%# Eval("EditorAlias") %>'> @@ -157,7 +157,9 @@ + DataTextField="Name" + DataValueField="Alias" + DataSource='<%# GetMacroParameterEditors()%>'> diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/controls/macroParameterControl.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/controls/macroParameterControl.cs index 509850893e..3048eea2cf 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/controls/macroParameterControl.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/controls/macroParameterControl.cs @@ -6,10 +6,11 @@ using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Umbraco.Core.Configuration; +using Umbraco.Core.IO; using umbraco.cms.businesslogic.macro; using System.Reflection; using System.Collections; -using umbraco.IO; + namespace umbraco.controls { [DefaultProperty("Text")] @@ -114,11 +115,11 @@ namespace umbraco.controls loadMacro(); - addParameterControls(); + AddParameterControls(); } - private void addParameterControls() + private void AddParameterControls() { Controls.Add(new LiteralControl("")); foreach (MacroProperty mp in m_macro.Properties) @@ -131,7 +132,7 @@ namespace umbraco.controls Assembly assembly = Assembly.LoadFrom( IOHelper.MapPath(SystemDirectories.Bin + "/" + macroAssembly + ".dll")); Type type = assembly.GetType(macroAssembly + "." + macroType); - interfaces.IMacroGuiRendering typeInstance = Activator.CreateInstance(type) as interfaces.IMacroGuiRendering; + var typeInstance = Activator.CreateInstance(type) as interfaces.IMacroGuiRendering; if (typeInstance != null) { ((Control) typeInstance).ID = mp.Alias; diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs index 06583d71c0..311a2bdf13 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs @@ -5,13 +5,15 @@ using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; +using Umbraco.Core; using Umbraco.Core.IO; +using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; using umbraco.BasePages; using umbraco.uicontrols; using umbraco.DataLayer; using umbraco.cms.presentation.Trees; -using umbraco.cms.businesslogic.macro; +using System.Linq; namespace umbraco.cms.presentation.developer { @@ -27,37 +29,38 @@ namespace umbraco.cms.presentation.developer protected PlaceHolder buttons; protected Table macroElements; - protected Macro m_macro; public TabPage InfoTabPage; public TabPage Parameters; + private IMacro _macro; + protected void Page_Load(object sender, EventArgs e) { - m_macro = new Macro(Convert.ToInt32(Request.QueryString["macroID"])); + _macro = Services.MacroService.GetById(Convert.ToInt32(Request.QueryString["macroID"])); if (!IsPostBack) { ClientTools .SetActiveTreeType(TreeDefinitionCollection.Instance.FindTree().Tree.Alias) - .SyncTree("-1,init," + m_macro.Id.ToString(), false); + .SyncTree("-1,init," + _macro.Id.ToString(), false); - string tempMacroAssembly = m_macro.Assembly ?? ""; - string tempMacroType = m_macro.Type ?? ""; + string tempMacroAssembly = _macro.ControlAssembly ?? ""; + string tempMacroType = _macro.ControlType ?? ""; - PopulateFieldsOnLoad(m_macro, tempMacroAssembly, tempMacroType); + PopulateFieldsOnLoad(_macro, tempMacroAssembly, tempMacroType); // Check for assemblyBrowser if (tempMacroType.IndexOf(".ascx") > 0) assemblyBrowserUserControl.Controls.Add( new LiteralControl("
")); else if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty) assemblyBrowser.Controls.Add( new LiteralControl("
")); // Load elements from macro @@ -78,41 +81,41 @@ namespace umbraco.cms.presentation.developer { ClientTools .SetActiveTreeType(TreeDefinitionCollection.Instance.FindTree().Tree.Alias) - .SyncTree("-1,init," + m_macro.Id.ToString(), true); //true forces the reload + .SyncTree("-1,init," + _macro.Id.ToInvariantString(), true); //true forces the reload - string tempMacroAssembly = macroAssembly.Text; - string tempMacroType = macroType.Text; - string tempCachePeriod = cachePeriod.Text; + var tempMacroAssembly = macroAssembly.Text; + var tempMacroType = macroType.Text; + var tempCachePeriod = cachePeriod.Text; if (tempCachePeriod == string.Empty) tempCachePeriod = "0"; if (tempMacroAssembly == string.Empty && macroUserControl.Text != string.Empty) tempMacroType = macroUserControl.Text; - SetMacroValuesFromPostBack(m_macro, Convert.ToInt32(tempCachePeriod), tempMacroAssembly, tempMacroType); - - m_macro.Save(); + SetMacroValuesFromPostBack(_macro, Convert.ToInt32(tempCachePeriod), tempMacroAssembly, tempMacroType); // Save elements + var sort = 0; foreach (RepeaterItem item in macroProperties.Items) { - HtmlInputHidden macroPropertyID = (HtmlInputHidden)item.FindControl("macroPropertyID"); - TextBox macroElementName = (TextBox)item.FindControl("macroPropertyName"); - TextBox macroElementAlias = (TextBox)item.FindControl("macroPropertyAlias"); - DropDownList macroElementType = (DropDownList)item.FindControl("macroPropertyType"); - - MacroProperty mp = new MacroProperty(int.Parse(macroPropertyID.Value)); - mp.Type = new MacroPropertyType(int.Parse(macroElementType.SelectedValue)); - mp.Alias = macroElementAlias.Text; - mp.Name = macroElementName.Text; - mp.Save(); + var macroPropertyId = (HtmlInputHidden)item.FindControl("macroPropertyID"); + var macroElementName = (TextBox)item.FindControl("macroPropertyName"); + var macroElementAlias = (TextBox)item.FindControl("macroPropertyAlias"); + var macroElementType = (DropDownList)item.FindControl("macroPropertyType"); + var prop = _macro.Properties[int.Parse(macroPropertyId.Value)]; + prop.Alias = macroElementAlias.Text.Trim(); + prop.Name = macroElementName.Text.Trim(); + prop.EditorAlias = macroElementType.SelectedValue; + prop.SortOrder = sort; + sort++; } + Services.MacroService.Save(_macro); + ClientTools.ShowSpeechBubble(speechBubbleIcon.save, "Macro saved", ""); - - + // Check for assemblyBrowser - if (tempMacroType.IndexOf(".ascx") > 0) + if (tempMacroType.IndexOf(".ascx", StringComparison.Ordinal) > 0) assemblyBrowserUserControl.Controls.Add( new LiteralControl("