Changed RouteAspectHandler to generate ContainerPath from the url of the content type

--HG--
branch : dev
This commit is contained in:
Erik Porter
2010-03-08 02:39:41 -08:00
parent ef03c8f608
commit 4c4906aba0
14 changed files with 74 additions and 35 deletions

View File

@@ -1,7 +1,10 @@
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Common.Models;
using Orchard.Data;
@@ -9,21 +12,46 @@ using Orchard.Data;
namespace Orchard.Core.Common.Handlers {
[UsedImplicitly]
public class RoutableAspectHandler : ContentHandler {
public RoutableAspectHandler(IRepository<RoutableRecord> repository) {
private readonly IEnumerable<IContentItemDriver> _contentItemDrivers;
public RoutableAspectHandler(IRepository<RoutableRecord> repository, IEnumerable<IContentItemDriver> contentItemDrivers, UrlHelper urlHelper) {
_contentItemDrivers = contentItemDrivers;
Filters.Add(StorageFilter.For(repository));
OnGetEditorViewModel<RoutableAspect>((context, routable) => {
var containerPathBuilder = new StringBuilder();
var container = context.ContentItem.As<ICommonAspect>().Container;
var currentDriver = GetDriver(context.ContentItem);
var tempContentItem = context.ContentItem.ContentManager.New(context.ContentItem.ContentType);
tempContentItem.As<RoutableAspect>().Slug = "ABCDEFG";
while (container != null) {
if (container.Is<RoutableAspect>())
containerPathBuilder.Insert(0, string.Format("{0}/", container.As<RoutableAspect>().Slug));
container = container.ContentItem.As<ICommonAspect>().Container;
}
var routeValues = GetRouteValues(currentDriver, tempContentItem);
var url = urlHelper.RouteUrl(routeValues).Replace("/ABCDEFG", "");
routable.ContainerPath = containerPathBuilder.ToString();
});
if (url.StartsWith("/"))
url = url.Substring(1);
routable.ContentItemBasePath = url;
});
}
private static RouteValueDictionary GetRouteValues(IContentItemDriver driver, ContentItem contentItem) {
//TODO: (erikpo) Need to rearrange ContentItemDriver so reflection isn't needed here
var driverType = driver.GetType();
var method = driverType.GetMethod("GetDisplayRouteValues");
if (method != null) {
return (RouteValueDictionary)method.Invoke(driver, new object[] {contentItem.Get(driverType.BaseType.GetGenericArguments()[0])});
}
return null;
}
private IContentItemDriver GetDriver(ContentItem contentItem) {
return
_contentItemDrivers
.Where(cid => cid.GetContentTypes().Any(ct => string.Compare(ct.Name, contentItem.ContentType, true) == 0))
//TODO: (erikpo) SingleOrDefault should be called here, but for some reason, the amount of drivers registered is doubled sometimes.
.FirstOrDefault();
}
}
}

View File

@@ -2,7 +2,7 @@ using Orchard.ContentManagement;
namespace Orchard.Core.Common.Models {
public class RoutableAspect : ContentPart<RoutableRecord> {
public string ContainerPath { get; set; }
public string ContentItemBasePath { get; set; }
public string Title {
get { return Record.Title; }

View File

@@ -10,7 +10,7 @@
<%=Html.TextBoxFor(m => m.Title, new { @class = "large text" }) %>
</fieldset>
<fieldset class="permalink">
<label class="sub" for="Slug"><%=_Encoded("Permalink")%><br /><span><%=Html.Encode(Request.Url.ToRootString()) %>/<%=Html.Encode(Model.RoutableAspect.ContainerPath) %></span></label>
<label class="sub" for="Slug"><%=_Encoded("Permalink")%><br /><span><%=Html.Encode(Request.Url.ToRootString()) %>/<%=Html.Encode(Model.RoutableAspect.ContentItemBasePath) %></span></label>
<span><%=Html.TextBoxFor(m => m.Slug, new { @class = "text" })%></span>
</fieldset>
<% using (this.Capture("end-of-page-scripts")) { %>

View File

@@ -34,7 +34,7 @@ namespace Futures.Widgets.Controllers {
}
public class WidgetDriver : ContentItemDriver<Widget> {
protected override RouteValueDictionary GetEditorRouteValues(Widget item) {
public override RouteValueDictionary GetEditorRouteValues(Widget item) {
return new RouteValueDictionary {
{"Area", "Futures.Widgets"},
{"Controller", "Admin"},

View File

@@ -50,7 +50,7 @@ namespace Orchard.Blogs.Drivers {
return item.Name;
}
protected override RouteValueDictionary GetDisplayRouteValues(Blog blog) {
public override RouteValueDictionary GetDisplayRouteValues(Blog blog) {
return new RouteValueDictionary {
{"Area", "Orchard.Blogs"},
{"Controller", "Blog"},
@@ -59,7 +59,7 @@ namespace Orchard.Blogs.Drivers {
};
}
protected override RouteValueDictionary GetEditorRouteValues(Blog blog) {
public override RouteValueDictionary GetEditorRouteValues(Blog blog) {
return new RouteValueDictionary {
{"Area", "Orchard.Blogs"},
{"Controller", "Blog"},

View File

@@ -43,7 +43,7 @@ namespace Orchard.Blogs.Drivers {
return post.Title;
}
protected override RouteValueDictionary GetDisplayRouteValues(BlogPost post) {
public override RouteValueDictionary GetDisplayRouteValues(BlogPost post) {
return new RouteValueDictionary {
{"Area", "Orchard.Blogs"},
{"Controller", "BlogPost"},
@@ -53,7 +53,7 @@ namespace Orchard.Blogs.Drivers {
};
}
protected override RouteValueDictionary GetEditorRouteValues(BlogPost post) {
public override RouteValueDictionary GetEditorRouteValues(BlogPost post) {
return new RouteValueDictionary {
{"Area", "Orchard.Blogs"},
{"Controller", "BlogPostAdmin"},

View File

@@ -52,6 +52,9 @@ namespace Orchard.Blogs.Routing {
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
if (routeDirection == RouteDirection.UrlGeneration)
return true;
object value;
if (values.TryGetValue(parameterName, out value)) {
var parameterValue = Convert.ToString(value);

View File

@@ -43,7 +43,7 @@ namespace Orchard.Pages.Drivers {
return page.Title;
}
protected override RouteValueDictionary GetDisplayRouteValues(Page page) {
public override RouteValueDictionary GetDisplayRouteValues(Page page) {
return new RouteValueDictionary {
{"Area", "Orchard.Pages"},
{"Controller", "Page"},
@@ -52,7 +52,7 @@ namespace Orchard.Pages.Drivers {
};
}
protected override RouteValueDictionary GetEditorRouteValues(Page page) {
public override RouteValueDictionary GetEditorRouteValues(Page page) {
return new RouteValueDictionary {
{"Area", "Orchard.Pages"},
{"Controller", "Admin"},

View File

@@ -52,6 +52,9 @@ namespace Orchard.Pages.Routing {
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
if (routeDirection == RouteDirection.UrlGeneration)
return true;
object value;
if (values.TryGetValue(parameterName, out value)) {
var parameterValue = Convert.ToString(value);

View File

@@ -21,7 +21,7 @@ namespace Orchard.Sandbox.Drivers {
return item.Record.Name;
}
protected override RouteValueDictionary GetDisplayRouteValues(SandboxPage item) {
public override RouteValueDictionary GetDisplayRouteValues(SandboxPage item) {
return new RouteValueDictionary(
new {
area = "Orchard.Sandbox",
@@ -31,7 +31,7 @@ namespace Orchard.Sandbox.Drivers {
});
}
protected override RouteValueDictionary GetEditorRouteValues(SandboxPage item) {
public override RouteValueDictionary GetEditorRouteValues(SandboxPage item) {
return new RouteValueDictionary(
new {
area = "Orchard.Sandbox",

View File

@@ -23,7 +23,7 @@ namespace Orchard.Users.Drivers {
return item.UserName;
}
protected override RouteValueDictionary GetEditorRouteValues(User item) {
public override RouteValueDictionary GetEditorRouteValues(User item) {
return new RouteValueDictionary {
{"Area", "Orchard.Users"},
{"Controller", "Admin"},

View File

@@ -5,15 +5,6 @@ using Orchard.ContentManagement.Handlers;
using Orchard.Mvc.ViewModels;
namespace Orchard.ContentManagement.Drivers {
public interface IContentItemDriver : IEvents {
IEnumerable<ContentType> GetContentTypes();
void GetContentItemMetadata(GetContentItemMetadataContext context);
DriverResult BuildDisplayModel(BuildDisplayModelContext context);
DriverResult BuildEditorModel(BuildEditorModelContext context);
DriverResult UpdateEditorModel(UpdateEditorModelContext context);
}
public abstract class ContentItemDriver<TContent> : ContentPartDriver<TContent>, IContentItemDriver where TContent : class, IContent {
private readonly ContentType _contentType;
protected virtual bool UseDefaultTemplate { get { return false; } }
@@ -74,8 +65,8 @@ namespace Orchard.ContentManagement.Drivers {
protected virtual ContentType GetContentType() { return _contentType; }
protected virtual string GetDisplayText(TContent item) { return null; }
protected virtual RouteValueDictionary GetDisplayRouteValues(TContent item) { return null; }
protected virtual RouteValueDictionary GetEditorRouteValues(TContent item) { return null; }
public virtual RouteValueDictionary GetDisplayRouteValues(TContent item) { return null; }
public virtual RouteValueDictionary GetEditorRouteValues(TContent item) { return null; }
protected virtual DriverResult Display(ContentItemViewModel<TContent> viewModel, string displayType) { return GetDefaultItemTemplate(); }
protected virtual DriverResult Editor(ContentItemViewModel<TContent> viewModel) { return GetDefaultItemTemplate(); }

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using Orchard.ContentManagement.Handlers;
namespace Orchard.ContentManagement.Drivers {
public interface IContentItemDriver : IEvents {
IEnumerable<ContentType> GetContentTypes();
void GetContentItemMetadata(GetContentItemMetadataContext context);
DriverResult BuildDisplayModel(BuildDisplayModelContext context);
DriverResult BuildEditorModel(BuildEditorModelContext context);
DriverResult UpdateEditorModel(UpdateEditorModelContext context);
}
}

View File

@@ -127,6 +127,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ContentManagement\Aspects\ICommonAspect.cs" />
<Compile Include="ContentManagement\Drivers\IContentItemDriver.cs" />
<Compile Include="ContentManagement\Extenstions\UrlHelperExtensions.cs" />
<Compile Include="ContentManagement\Handlers\ContentContextBase.cs" />
<Compile Include="ContentManagement\Handlers\PublishContentContext.cs" />