mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Implemented import/export for element definitions.
This commit is contained in:
@@ -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))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -468,6 +468,9 @@
|
|||||||
<Compile Include="Helpers\PrefixHelper.cs" />
|
<Compile Include="Helpers\PrefixHelper.cs" />
|
||||||
<Compile Include="Helpers\JsonHelper.cs" />
|
<Compile Include="Helpers\JsonHelper.cs" />
|
||||||
<Compile Include="Helpers\StringHelper.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="Providers\BlueprintElementHarvester.cs" />
|
||||||
<Compile Include="ResourceManifest.cs" />
|
<Compile Include="ResourceManifest.cs" />
|
||||||
<Compile Include="Services\CurrentControllerAccessor.cs" />
|
<Compile Include="Services\CurrentControllerAccessor.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user