Implemented import/export for element definitions.

This commit is contained in:
Sipke Schoorstra
2015-04-28 18:10:45 +02:00
parent 6b18883e24
commit 8dbd003f61
4 changed files with 115 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Orchard.Data;
using Orchard.Events;
using Orchard.Layouts.Models;
namespace Orchard.Layouts.ImportExport {
public interface IExportEventHandler : IEventHandler {
void Exporting(dynamic context);
void Exported(dynamic context);
}
public class ElementsExportHandler : IExportEventHandler {
private readonly IRepository<ElementBlueprint> _repository;
public ElementsExportHandler(IRepository<ElementBlueprint> repository) {
_repository = repository;
}
public void Exporting(dynamic context) {
}
public void Exported(dynamic context) {
if (!((IEnumerable<string>)context.ExportOptions.CustomSteps).Contains("LayoutElements")) {
return;
}
var elements = _repository.Table.ToList();
if (!elements.Any()) {
return;
}
var root = new XElement("LayoutElements");
context.Document.Element("Orchard").Add(root);
foreach (var element in elements) {
root.Add(new XElement("Element",
new XAttribute("ElementTypeName", element.ElementTypeName),
new XAttribute("BaseElementTypeName", element.BaseElementTypeName),
new XAttribute("ElementDisplayName", element.ElementDisplayName),
new XAttribute("ElementDescription", element.ElementDescription),
new XAttribute("ElementCategory", element.ElementCategory),
new XElement("BaseElementState", new XCData(element.BaseElementState))));
}
}
}
}

View File

@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Orchard.Events;
namespace Orchard.Layouts.ImportExport {
public interface ICustomExportStep : IEventHandler {
void Register(IList<string> steps);
}
public class ElementsExportStep : ICustomExportStep {
public void Register(IList<string> steps) {
steps.Add("LayoutElements");
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using Orchard.Data;
using Orchard.Layouts.Models;
using Orchard.Recipes.Models;
using Orchard.Recipes.Services;
namespace Orchard.Layouts.ImportExport {
public class ElementsImportHandler : Component, IRecipeHandler {
private readonly IRepository<ElementBlueprint> _repository;
public ElementsImportHandler(IRepository<ElementBlueprint> repository) {
_repository = repository;
}
public void ExecuteRecipeStep(RecipeContext recipeContext) {
if (!String.Equals(recipeContext.RecipeStep.Name, "LayoutElements", StringComparison.OrdinalIgnoreCase)) {
return;
}
foreach (var elementElement in recipeContext.RecipeStep.Step.Elements()) {
var typeName = elementElement.Attribute("ElementTypeName").Value;
var element = GetOrCreateElement(typeName);
element.BaseElementTypeName = elementElement.Attribute("BaseElementTypeName").Value;
element.ElementDisplayName = elementElement.Attribute("ElementDisplayName").Value;
element.ElementDescription = elementElement.Attribute("ElementDescription").Value;
element.ElementCategory = elementElement.Attribute("ElementCategory").Value;
element.BaseElementState = elementElement.Element("BaseElementState").Value;
}
recipeContext.Executed = true;
}
private ElementBlueprint GetOrCreateElement(string typeName) {
var element = _repository.Get(x => x.ElementTypeName == typeName);
if (element == null) {
element = new ElementBlueprint {
ElementTypeName = typeName
};
_repository.Create(element);
}
return element;
}
}
}

View File

@@ -468,6 +468,9 @@
<Compile Include="Helpers\PrefixHelper.cs" />
<Compile Include="Helpers\JsonHelper.cs" />
<Compile Include="Helpers\StringHelper.cs" />
<Compile Include="ImportExport\ElementsExportStep.cs" />
<Compile Include="ImportExport\ElementsExportHandler.cs" />
<Compile Include="ImportExport\ElementsImportHandler.cs" />
<Compile Include="Providers\BlueprintElementHarvester.cs" />
<Compile Include="ResourceManifest.cs" />
<Compile Include="Services\CurrentControllerAccessor.cs" />