Tag patterns

--HG--
branch : autoroute
This commit is contained in:
randompete
2012-01-20 03:23:41 +00:00
parent b00fa010e2
commit 536a76e204
4 changed files with 114 additions and 2 deletions

View File

@@ -61,6 +61,8 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Projections\TagsFilterForms.cs" />
<Compile Include="Providers\TagPatterns.cs" />
<Compile Include="Providers\TagTokenProvider.cs" />
<Compile Include="ResourceManifest.cs" />
<Compile Include="Services\ITagService.cs" />
<Compile Include="Services\XmlRpcHandler.cs" />
@@ -104,6 +106,10 @@
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
<Name>Orchard.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Tokens\Orchard.Tokens.csproj">
<Project>{6F759635-13D7-4E94-BCC9-80445D63F117}</Project>
<Name>Orchard.Tokens</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Placement.info">
@@ -122,6 +128,7 @@
<ItemGroup>
<Content Include="Scripts\Web.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Tags.Services;
using Orchard.Localization;
using Orchard.Tags.Models;
using System.Web.Routing;
namespace Orchard.Tags.Providers {
public class TagPatterns {
private readonly ITagService _tagService;
public TagPatterns(
ITagService tagService
) {
_tagService = tagService;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(dynamic describe) {
describe.For<TagRecord>("Tags", T("Tags"), T("Tags url patterns"), (Func<TagRecord, string>)GetId, (Func<string, TagRecord>)GetTag)
.Pattern("Tags", T("View all tags"), T("A list of all tags are displayed on this page"), (Func<TagRecord, RouteValueDictionary>)GetTagsRouteValues)
.Pattern("View", T("View tagged content"), T("Tagged content will be listed on this Url for each tag"), (Func<TagRecord, RouteValueDictionary>)GetRouteValues);
}
protected RouteValueDictionary GetRouteValues(TagRecord tag) {
return new RouteValueDictionary(new{
area = "Orchard.Tags",
controller = "Home",
action = "Search",
tagName = tag.TagName
});
}
protected RouteValueDictionary GetTagsRouteValues(TagRecord tag) {
return new RouteValueDictionary(new {
area = "Orchard.Tags",
controller = "Home",
action = "Index"
});
}
protected string GetId(TagRecord tag) {
return tag.Id.ToString();
}
protected TagRecord GetTag(string id) {
return _tagService.GetTag(Convert.ToInt32(id));
}
public void Suggest(dynamic suggest) {
suggest.For("Tags")
.Suggest("View", "tags/tag-name", "{Tag.Slug}", T("Slugified tag name"))
.Suggest("Tags", "tags", "tags", T("Plain /tags url"));
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Tokens;
using Orchard.Localization;
using Orchard.Tags.Models;
namespace Orchard.Tags.Providers {
public class TagTokenProvider : ITokenProvider {
public TagTokenProvider() {
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(DescribeContext context) {
context.For("Tag", T("Tags"), T("Tag records"))
.Token("Name", T("Tag name"), T("Tag name"), "Text");
}
public void Evaluate(EvaluateContext context) {
context.For<TagRecord>("Tag")
.Token("Name", t => t.TagName)
// By chaining the name to text it can be slugified in Autoroute
.Chain("Name", "Text", t => t.TagName);
}
}
}

View File

@@ -10,6 +10,7 @@ using Orchard.ContentManagement;
using Orchard.Security;
using Orchard.Tags.Models;
using Orchard.UI.Notify;
using Orchard.Events;
namespace Orchard.Tags.Services {
[UsedImplicitly]
@@ -19,17 +20,20 @@ namespace Orchard.Tags.Services {
private readonly INotifier _notifier;
private readonly IAuthorizationService _authorizationService;
private readonly IOrchardServices _orchardServices;
private readonly IRoutePatternManager _routePatternManager;
public TagService(IRepository<TagRecord> tagRepository,
IRepository<ContentTagRecord> contentTagRepository,
INotifier notifier,
IAuthorizationService authorizationService,
IOrchardServices orchardServices) {
IOrchardServices orchardServices,
IRoutePatternManager routePatternManager) {
_tagRepository = tagRepository;
_contentTagRepository = contentTagRepository;
_notifier = notifier;
_authorizationService = authorizationService;
_orchardServices = orchardServices;
_routePatternManager = routePatternManager;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
@@ -60,7 +64,7 @@ namespace Orchard.Tags.Services {
result = new TagRecord { TagName = tagName };
_tagRepository.Create(result);
}
_routePatternManager.Generate(result, "Tags");
return result;
}
@@ -116,6 +120,7 @@ namespace Orchard.Tags.Services {
// Create new tag
tagRecord = _tagRepository.Get(tagId);
tagRecord.TagName = tagName;
_routePatternManager.Generate(tagRecord, "Tags");
}
public IEnumerable<IContent> GetTaggedContentItems(int tagId) {
@@ -197,4 +202,9 @@ namespace Orchard.Tags.Services {
}
}
}
public interface IRoutePatternManager : IEventHandler {
void Generate(TagRecord item, string scope);
}
}