public void MacroSerializer_ToXml()
        {
            var macro = new MacroEditorModel
                {
                    Alias = "test",
                    Name = "Test",
                    Id = new HiveId("my-macro.macro"),
                    CacheByPage = true,
                    CachePeriodSeconds = 1234,
                    CachePersonalized = false,
                    MacroType = "ChildAction",
                    RenderContentInEditor = true,
                    SelectedItem = "RenderTwitterFeed",
                    UseInEditor = false
                };
            macro.MacroParameters.Add(new MacroParameterDefinitionModel { Alias = "test1", Name = "Test1", ParameterEditorId = Guid.NewGuid(), Show = true });
            macro.MacroParameters.Add(new MacroParameterDefinitionModel { Alias = "test2", Name = "Test2", ParameterEditorId = Guid.NewGuid(), Show = false });

            var xml = MacroSerializer.ToXml(macro);

            Assert.AreEqual(macro.Alias, xml.Root.Attribute("alias").Value);
            Assert.AreEqual(macro.Name, xml.Root.Attribute("name").Value);
            Assert.AreEqual(macro.CacheByPage, (bool)xml.Root.Attribute("cacheByPage"));
            Assert.AreEqual(macro.CachePeriodSeconds, (int)xml.Root.Attribute("cachePeriodSeconds"));
            Assert.AreEqual(macro.CachePersonalized, (bool)xml.Root.Attribute("cachePersonalized"));
            Assert.AreEqual(macro.MacroType.ToString(), xml.Root.Attribute("macroType").Value);
            Assert.AreEqual(macro.RenderContentInEditor, (bool)xml.Root.Attribute("renderContentInEditor"));
            Assert.AreEqual(macro.SelectedItem, xml.Root.Attribute("selectedItem").Value);
            Assert.AreEqual(macro.UseInEditor, (bool)xml.Root.Attribute("useInEditor"));
            
            //TODO: test parameter values
            Assert.AreEqual(2, xml.Root.Elements("parameter").Count());
        }
        public ActionResult EditForm(MacroEditorModel model)
        {
            Mandate.ParameterNotNull(model, "model");

            //validate the model
            TryUpdateModel(model);

            model.Alias = model.Alias.ToUmbracoAlias();

            //if at this point the model state is invalid, return the result which is the CreateNew view
            if (!ModelState.IsValid)
            {
                EnsureModelListData(model);
                return View("Edit", model);
            }

            return ProcessSubmit(model);
        }
 public static XDocument ToXml(MacroEditorModel model)
 {
     return new XDocument(
         new XElement("macro",                             
                      new XAttribute("alias", model.Alias),
                      new XAttribute("name", model.Name),
                      new XAttribute("cacheByPage", model.CacheByPage),
                      new XAttribute("cachePeriodSeconds", model.CachePeriodSeconds),
                      new XAttribute("cachePersonalized", model.CachePersonalized),
                      new XAttribute("macroType", model.MacroType),
                      new XAttribute("renderContentInEditor", model.RenderContentInEditor),
                      new XAttribute("selectedItem", model.SelectedItem),
                      new XAttribute("useInEditor", model.UseInEditor),
                      model.MacroParameters.Select(x => new XElement("parameter",
                                                                     new XAttribute("alias", x.Alias),
                                                                     new XAttribute("name", x.Name),
                                                                     new XAttribute("show", x.Show),
                                                                     new XAttribute("parameterEditorId", x.ParameterEditorId)))));
 }
        protected override void OnAfterCreate(Framework.Persistence.Model.IO.File file)
        {
            if(Request.Form.ContainsKey("createMacro"))
            {
                var macroEditor = new MacroEditorController(BackOfficeRequestContext)
                {
                    ControllerContext = this.ControllerContext
                };

                var macroModel = new MacroEditorModel
                {
                    Name = file.GetFileNameWithoutExtension(),
                    Alias = file.GetFileNameWithoutExtension().ToUmbracoAlias(),
                    MacroType = "PartialView",
                    SelectedItem = file.GetFileNameWithoutExtension()
                };

                macroEditor.PerformSave(macroModel);
            }
        }
 /// <summary>
 /// Ensure the ViewBag has all of its required list data
 /// </summary>
 private void EnsureModelListData(MacroEditorModel model)
 {
     model.AvailableParameterEditors = GetMacroParameterEditors();
     model.AvailableMacroItems = BackOfficeRequestContext.RegisteredComponents
         .MacroEngines
         .Select(x => new Tuple<string, IEnumerable<SelectListItem>>(
                          x.Metadata.EngineName,
                          x.Value.GetMacroItems(BackOfficeRequestContext)));
 }
        private ActionResult ProcessSubmit(MacroEditorModel model)
        {
            Mandate.ParameterNotNull(model, "model");

            var macroFile = PerformSave(model);

            //set the notification
            Notifications.Add(new NotificationMessage(
                "Macro.Save.Message".Localize(this),
                "Macro.Save.Title".Localize(this),
                NotificationType.Success));

            //add path for entity for SupportsPathGeneration (tree syncing) to work     
            using (var uow = Hive.Create())
            {
                GeneratePathsForCurrentEntity(uow.Repositories.GetEntityPaths<File>(macroFile.Id, FixedRelationTypes.DefaultRelationType));
            }

            return RedirectToAction("Edit", new { id = macroFile.Id });
        }
        internal File PerformSave(MacroEditorModel model)
        {
            Mandate.ParameterNotNull(model, "model");

            using (var uow = Hive.Create())
            {
                var fileName = model.Alias.ToUmbracoAlias() + ".macro";
                var newFileId = new HiveId(fileName);
                var macroFile = new File
                {
                    Name = fileName,
                    ContentBytes = Encoding.UTF8.GetBytes(MacroSerializer.ToXml(model).ToString())
                };
                var existing = uow.Repositories.Get<File>(newFileId);
                //delete the file first before re-saving as AddOrUpdate seems to append to the file
                if (existing != null)
                    uow.Repositories.Delete<File>(newFileId);
                uow.Repositories.AddOrUpdate(macroFile);
                //TODO: the Hive IO provider commit seems to do nothing :( ... needs to be implemented!
                uow.Complete();

                return macroFile;
            }
        }
        public ActionResult CreateNewForm(CreateMacroModel createModel)
        {
            Mandate.ParameterNotNull(createModel, "createModel");            
            Mandate.That<NullReferenceException>(!string.IsNullOrEmpty(createModel.Name));

            //validate the model
            TryUpdateModel(createModel);
            //if at this point the model state is invalid, return the result which is the CreateNew view
            if (!ModelState.IsValid)
            {
                return View(createModel);
            }

            //everything is valid, now we need to render out the editor for this macro
            var editorModel = new MacroEditorModel
            {
                Name = createModel.Name,
                MacroType = createModel.MacroType,
                Alias = createModel.Name.ToUmbracoAlias(),
                SelectedItem = ""
            };

            EnsureModelListData(editorModel);


            return ProcessSubmit(editorModel);
        }