mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Refactored CustomElements import/export step.
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -367,9 +367,8 @@
|
|||||||
<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\ElementsExportHandler.cs" />
|
<Compile Include="Recipes\Builders\CustomElementsStep.cs" />
|
||||||
<Compile Include="ImportExport\ElementsExportStep.cs" />
|
<Compile Include="Recipes\Executors\CustomElementsStep.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" />
|
||||||
|
|||||||
@@ -1,40 +1,43 @@
|
|||||||
using System.Collections.Generic;
|
using System.Linq;
|
||||||
using System.Linq;
|
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
using Orchard.Events;
|
|
||||||
using Orchard.Layouts.Models;
|
using Orchard.Layouts.Models;
|
||||||
|
using Orchard.Localization;
|
||||||
|
using Orchard.Recipes.Services;
|
||||||
|
|
||||||
namespace Orchard.Layouts.ImportExport {
|
namespace Orchard.Layouts.Recipes.Builders {
|
||||||
public interface IExportEventHandler : IEventHandler {
|
|
||||||
void Exporting(dynamic context);
|
public class CustomElementsStep : RecipeBuilderStep {
|
||||||
void Exported(dynamic context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ElementsExportHandler : IExportEventHandler {
|
|
||||||
private readonly IRepository<ElementBlueprint> _repository;
|
private readonly IRepository<ElementBlueprint> _repository;
|
||||||
|
|
||||||
public ElementsExportHandler(IRepository<ElementBlueprint> repository) {
|
public CustomElementsStep(IRepository<ElementBlueprint> repository) {
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Exporting(dynamic context) {
|
public override string Name
|
||||||
|
{
|
||||||
|
get { return "CustomElements"; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Exported(dynamic context) {
|
public override LocalizedString DisplayName
|
||||||
|
{
|
||||||
|
get { return T("Custom Elements"); }
|
||||||
|
}
|
||||||
|
|
||||||
if (!((IEnumerable<string>)context.ExportOptions.CustomSteps).Contains("LayoutElements")) {
|
public override LocalizedString Description
|
||||||
return;
|
{
|
||||||
}
|
get { return T("Exports custom defined elements."); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Build(BuildContext context) {
|
||||||
var elements = _repository.Table.OrderBy(x => x.ElementTypeName).ToList();
|
var elements = _repository.Table.OrderBy(x => x.ElementTypeName).ToList();
|
||||||
|
|
||||||
if (!elements.Any()) {
|
if (!elements.Any()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var root = new XElement("LayoutElements");
|
var root = new XElement("CustomElements");
|
||||||
context.Document.Element("Orchard").Add(root);
|
context.RecipeDocument.Element("Orchard").Add(root);
|
||||||
|
|
||||||
foreach (var element in elements) {
|
foreach (var element in elements) {
|
||||||
root.Add(new XElement("Element",
|
root.Add(new XElement("Element",
|
||||||
@@ -1,23 +1,27 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
using Orchard.Layouts.Models;
|
using Orchard.Layouts.Models;
|
||||||
using Orchard.Recipes.Models;
|
using Orchard.Recipes.Models;
|
||||||
using Orchard.Recipes.Services;
|
using Orchard.Recipes.Services;
|
||||||
|
|
||||||
namespace Orchard.Layouts.ImportExport {
|
namespace Orchard.Layouts.Recipes.Executors {
|
||||||
public class ElementsImportHandler : Component, IRecipeHandler {
|
public class CustomElementsStep : RecipeExecutionStep {
|
||||||
private readonly IRepository<ElementBlueprint> _repository;
|
private readonly IRepository<ElementBlueprint> _repository;
|
||||||
|
|
||||||
public ElementsImportHandler(IRepository<ElementBlueprint> repository) {
|
public CustomElementsStep(IRepository<ElementBlueprint> repository) {
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ExecuteRecipeStep(RecipeContext recipeContext) {
|
public override string Name {
|
||||||
if (!String.Equals(recipeContext.RecipeStep.Name, "LayoutElements", StringComparison.OrdinalIgnoreCase)) {
|
get { return "CustomElements"; }
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var elementElement in recipeContext.RecipeStep.Step.Elements()) {
|
public override IEnumerable<string> Names {
|
||||||
|
get { return new[] { Name, "LayoutElements" }; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Execute(RecipeExecutionContext context) {
|
||||||
|
foreach (var elementElement in context.RecipeStep.Step.Elements()) {
|
||||||
var typeName = elementElement.Attribute("ElementTypeName").Value;
|
var typeName = elementElement.Attribute("ElementTypeName").Value;
|
||||||
var element = GetOrCreateElement(typeName);
|
var element = GetOrCreateElement(typeName);
|
||||||
|
|
||||||
@@ -27,10 +31,8 @@ namespace Orchard.Layouts.ImportExport {
|
|||||||
element.ElementCategory = elementElement.Attribute("ElementCategory").Value;
|
element.ElementCategory = elementElement.Attribute("ElementCategory").Value;
|
||||||
element.BaseElementState = elementElement.Element("BaseElementState").Value;
|
element.BaseElementState = elementElement.Element("BaseElementState").Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
recipeContext.Executed = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ElementBlueprint GetOrCreateElement(string typeName) {
|
private ElementBlueprint GetOrCreateElement(string typeName) {
|
||||||
var element = _repository.Get(x => x.ElementTypeName == typeName);
|
var element = _repository.Get(x => x.ElementTypeName == typeName);
|
||||||
|
|
||||||
Reference in New Issue
Block a user