Implemented import/export step for home alias.

This commit is contained in:
Sipke Schoorstra 2015-07-21 16:04:28 +01:00
parent f60643d460
commit b5bc89cb21
4 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using System.Xml.Linq;
using Orchard.Autoroute.Services;
using Orchard.ContentManagement;
using Orchard.Events;
namespace Orchard.Autoroute.ImportExport {
public interface IExportEventHandler : IEventHandler {
void Exporting(dynamic context);
void Exported(dynamic context);
}
public class HomeAliasHandler : IExportEventHandler {
private readonly IHomeAliasService _homeAliasService;
private readonly IContentManager _contentManager;
public HomeAliasHandler(IHomeAliasService homeAliasService, IContentManager contentManager) {
_homeAliasService = homeAliasService;
_contentManager = contentManager;
}
public void Exporting(dynamic context) {
}
public void Exported(dynamic context) {
if (!((IEnumerable<string>)context.ExportOptions.CustomSteps).Contains("HomeAlias")) {
return;
}
var homeAliasRoute = _homeAliasService.GetHomeRoute() ?? new RouteValueDictionary();
var root = new XElement("HomeAlias", homeAliasRoute.Select(x => new XElement(Capitalize(x.Key), x.Value)));
var homePage = _homeAliasService.GetHomePage(VersionOptions.Latest);
// If the home alias points to a content item, store its identifier in addition to the routevalues,
// so we can publish the home page alias during import where the ID primary key value of the home page might have changed,
// so we can't rely on the route values in that case.
if (homePage != null) {
var homePageIdentifier = _contentManager.GetItemMetadata(homePage).Identity.ToString();
root.Attr("Identifier", homePageIdentifier);
}
context.Document.Element("Orchard").Add(root);
}
private string Capitalize(string value) {
if (String.IsNullOrEmpty(value))
return value;
return Char.ToUpper(value[0]) + value.Substring(1);
}
}
}

View File

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

View File

@ -0,0 +1,38 @@
using System;
using System.Linq;
using System.Web.Routing;
using Orchard.Autoroute.Services;
using Orchard.ContentManagement;
using Orchard.Recipes.Models;
using Orchard.Recipes.Services;
namespace Orchard.Autoroute.ImportExport {
public class HomeAliasImportHandler : Component, IRecipeHandler {
private readonly IHomeAliasService _homeAliasService;
private readonly IContentManager _contentManager;
public HomeAliasImportHandler(IHomeAliasService homeAliasService, IContentManager contentManager) {
_homeAliasService = homeAliasService;
_contentManager = contentManager;
}
public void ExecuteRecipeStep(RecipeContext recipeContext) {
if (!String.Equals(recipeContext.RecipeStep.Name, "HomeAlias", StringComparison.OrdinalIgnoreCase)) {
return;
}
var root = recipeContext.RecipeStep.Step;
var routeValueDictionary = root.Elements().ToDictionary(x => x.Name.LocalName.ToLower(), x => (object)x.Value);
var homePageIdentifier = root.Attr("Identifier");
var homePageIdentity = new ContentIdentity(homePageIdentifier);
var homePage = !String.IsNullOrEmpty(homePageIdentifier) ? _contentManager.ResolveIdentity(homePageIdentity) : default(ContentItem);
if(homePage != null)
_homeAliasService.PublishHomeAlias(homePage);
else
_homeAliasService.PublishHomeAlias(new RouteValueDictionary(routeValueDictionary));
recipeContext.Executed = true;
}
}
}

View File

@ -76,6 +76,9 @@
<Content Include="Web.config" />
<Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" />
<Compile Include="ImportExport\HomeAliasExportStep.cs" />
<Compile Include="ImportExport\HomeAliasExportHandler.cs" />
<Compile Include="ImportExport\HomeAliasImportHandler.cs" />
<Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" />