Introduced IHomeAliasService to deal with managing the home page.

This commit is contained in:
Sipke Schoorstra
2015-07-21 11:52:09 +01:00
parent 2dad35731e
commit 32584416b9
2 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
using System.Web.Routing;
using Orchard.Alias;
using Orchard.ContentManagement;
namespace Orchard.Autoroute.Services {
public class HomeAliasService : IHomeAliasService {
private readonly IAliasService _aliasService;
private readonly IContentManager _contentManager;
private const string AliasSource = "Autoroute:Home";
private const string HomeAlias = "";
public HomeAliasService(IAliasService aliasService, IContentManager contentManager) {
_aliasService = aliasService;
_contentManager = contentManager;
}
public RouteValueDictionary GetHomeRoute() {
return _aliasService.Get(HomeAlias);
}
public int? GetHomePageId() {
var homePageRoute = GetHomeRoute();
var homePageId =
homePageRoute != null
? homePageRoute.ContainsKey("id")
? XmlHelper.Parse<int>((string)homePageRoute["id"])
: default(int?)
: default(int?);
return homePageId;
}
public IContent GetHomePage(VersionOptions version = null) {
var homePageId = GetHomePageId();
var homePage = homePageId != null ? _contentManager.Get(homePageId.Value, version ?? VersionOptions.Published) : default(IContent);
return homePage;
}
public void SetHomeAlias(IContent content) {
var routeValues = _contentManager.GetItemMetadata(content).DisplayRouteValues;
SetHomeAlias(routeValues);
}
public void SetHomeAlias(string route) {
_aliasService.Set(HomeAlias, route, AliasSource);
}
public void SetHomeAlias(RouteValueDictionary route) {
_aliasService.Set(HomeAlias, route, AliasSource);
}
}
}

View File

@@ -0,0 +1,14 @@
using System.Web.Routing;
using Orchard.ContentManagement;
namespace Orchard.Autoroute.Services {
public interface IHomeAliasService : IDependency {
RouteValueDictionary GetHomeRoute();
int? GetHomePageId();
IContent GetHomePage(VersionOptions version = null);
void SetHomeAlias(IContent content);
void SetHomeAlias(string route);
void SetHomeAlias(RouteValueDictionary route);
}
}