Some work on slug generation for Page (comes with bonus bug in versioning of Page slug only it seems...)

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045500
This commit is contained in:
skewed 2010-01-16 00:17:19 +00:00
parent e25a36d1cc
commit 39d7aa8eb7
6 changed files with 24 additions and 27 deletions

View File

@ -5,8 +5,6 @@ using System.Text.RegularExpressions;
namespace Orchard.Core.Common.Services {
public class RoutableService : IRoutableService {
#region IRoutableService Members
public string Slugify(string title) {
if (!string.IsNullOrEmpty(title)) {
//todo: (heskew) improve - just doing multi-pass regex replaces for now with the simple rules of
@ -34,7 +32,7 @@ namespace Orchard.Core.Common.Services {
int v;
string[] slugParts = s.Split(new[] { slugCandidate }, StringSplitOptions.RemoveEmptyEntries);
if (slugParts.Length == 0) {
return 0;
return 1;
}
return int.TryParse(slugParts[0].TrimStart('-'), out v)
@ -48,7 +46,5 @@ namespace Orchard.Core.Common.Services {
? string.Format("{0}-{1}", slugCandidate, version)
: slugCandidate;
}
#endregion
}
}

View File

@ -111,7 +111,7 @@ namespace Orchard.Pages.Controllers {
if (!Services.Authorizer.Authorize(Permissions.CreatePages, T("Not allowed to create a page")))
return new HttpUnauthorizedResult();
var page = Services.ContentManager.BuildEditorModel(_pageService.New());
var page = Services.ContentManager.BuildEditorModel(Services.ContentManager.New<Page>("page"));
var model = new PageCreateViewModel {
Page = page
@ -137,16 +137,16 @@ namespace Orchard.Pages.Controllers {
}
}
Page page = _pageService.Create(publishNow, publishDate);
model.Page = Services.ContentManager.UpdateEditorModel(page, this);
model.Page = Services.ContentManager.UpdateEditorModel(Services.ContentManager.New<Page>("page"), this);
if (!publishNow && publishDate != null)
model.Page.Item.Published = publishDate.Value;
if (!ModelState.IsValid) {
Services.TransactionManager.Cancel();
return View(model);
}
var session = _sessionLocator.For(typeof(Page));
session.Flush();
Services.ContentManager.Create(model.Page.Item.ContentItem, publishNow ? VersionOptions.Published : VersionOptions.Draft);
return RedirectToAction("List");
}

View File

@ -15,6 +15,7 @@ namespace Orchard.Pages.Models {
public string Slug {
get { return this.As<RoutableAspect>().Slug; }
set { this.As<RoutableAspect>().Slug = value; }
}
public IUser Creator {

View File

@ -1,21 +1,36 @@
using JetBrains.Annotations;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Core.Common.Records;
using Orchard.Core.Common.Services;
using Orchard.Pages.Controllers;
using Orchard.Core.Common.Models;
using Orchard.Data;
using Orchard.ContentManagement.Handlers;
using Orchard.Pages.Services;
namespace Orchard.Pages.Models {
[UsedImplicitly]
public class PageHandler : ContentHandler {
public PageHandler(IRepository<CommonVersionRecord> commonRepository) {
public PageHandler(IRepository<CommonVersionRecord> commonRepository, IPageService pageService, IRoutableService routableService) {
Filters.Add(new ActivatingFilter<Page>(PageDriver.ContentType.Name));
Filters.Add(new ActivatingFilter<CommonAspect>(PageDriver.ContentType.Name));
Filters.Add(new ActivatingFilter<ContentPart<CommonVersionRecord>>(PageDriver.ContentType.Name));
Filters.Add(new ActivatingFilter<RoutableAspect>(PageDriver.ContentType.Name));
Filters.Add(new ActivatingFilter<BodyAspect>(PageDriver.ContentType.Name));
Filters.Add(new StorageFilter<CommonVersionRecord>(commonRepository));
OnCreating<Page>((context, blog) =>
{
string slug = !string.IsNullOrEmpty(blog.Slug)
? blog.Slug
: routableService.Slugify(blog.Title);
blog.Slug = routableService.GenerateUniqueSlug(slug,
pageService.Get(PageStatus.Published).Where(
p => p.Slug.StartsWith(slug)).Select(
p => p.Slug));
});
}
}
}

View File

@ -10,8 +10,6 @@ namespace Orchard.Pages.Services {
Page GetPageOrDraft(string slug);
Page GetLatest(string slug);
Page GetLatest(int id);
Page New();
Page Create(bool publishNow, DateTime? publishDate);
void Delete(Page page);
void Publish(Page page);
void Publish(Page page, DateTime publishDate);

View File

@ -59,19 +59,6 @@ namespace Orchard.Pages.Services {
return _contentManager.GetDraftRequired<Page>(page.Id);
}
public Page New() {
return _contentManager.New<Page>("page");
}
public Page Create(bool publishNow, DateTime? publishDate) {
//TODO: (erikpo) Evaluate if publish options should be moved into create or out of create to keep it clean
return _contentManager.Create<Page>("page", publishNow ? VersionOptions.Published : VersionOptions.Draft,
bp => {
if (!publishNow && publishDate != null)
bp.Published = publishDate.Value;
});
}
public void Delete(Page page) {
_contentManager.Remove(page.ContentItem);
}