mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-07-16 23:41:34 +08:00
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:
parent
e25a36d1cc
commit
39d7aa8eb7
@ -5,8 +5,6 @@ using System.Text.RegularExpressions;
|
|||||||
|
|
||||||
namespace Orchard.Core.Common.Services {
|
namespace Orchard.Core.Common.Services {
|
||||||
public class RoutableService : IRoutableService {
|
public class RoutableService : IRoutableService {
|
||||||
#region IRoutableService Members
|
|
||||||
|
|
||||||
public string Slugify(string title) {
|
public string Slugify(string title) {
|
||||||
if (!string.IsNullOrEmpty(title)) {
|
if (!string.IsNullOrEmpty(title)) {
|
||||||
//todo: (heskew) improve - just doing multi-pass regex replaces for now with the simple rules of
|
//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;
|
int v;
|
||||||
string[] slugParts = s.Split(new[] { slugCandidate }, StringSplitOptions.RemoveEmptyEntries);
|
string[] slugParts = s.Split(new[] { slugCandidate }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (slugParts.Length == 0) {
|
if (slugParts.Length == 0) {
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return int.TryParse(slugParts[0].TrimStart('-'), out v)
|
return int.TryParse(slugParts[0].TrimStart('-'), out v)
|
||||||
@ -48,7 +46,5 @@ namespace Orchard.Core.Common.Services {
|
|||||||
? string.Format("{0}-{1}", slugCandidate, version)
|
? string.Format("{0}-{1}", slugCandidate, version)
|
||||||
: slugCandidate;
|
: slugCandidate;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -111,7 +111,7 @@ namespace Orchard.Pages.Controllers {
|
|||||||
if (!Services.Authorizer.Authorize(Permissions.CreatePages, T("Not allowed to create a page")))
|
if (!Services.Authorizer.Authorize(Permissions.CreatePages, T("Not allowed to create a page")))
|
||||||
return new HttpUnauthorizedResult();
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
var page = Services.ContentManager.BuildEditorModel(_pageService.New());
|
var page = Services.ContentManager.BuildEditorModel(Services.ContentManager.New<Page>("page"));
|
||||||
|
|
||||||
var model = new PageCreateViewModel {
|
var model = new PageCreateViewModel {
|
||||||
Page = page
|
Page = page
|
||||||
@ -137,16 +137,16 @@ namespace Orchard.Pages.Controllers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Page page = _pageService.Create(publishNow, publishDate);
|
model.Page = Services.ContentManager.UpdateEditorModel(Services.ContentManager.New<Page>("page"), this);
|
||||||
model.Page = Services.ContentManager.UpdateEditorModel(page, this);
|
if (!publishNow && publishDate != null)
|
||||||
|
model.Page.Item.Published = publishDate.Value;
|
||||||
|
|
||||||
if (!ModelState.IsValid) {
|
if (!ModelState.IsValid) {
|
||||||
Services.TransactionManager.Cancel();
|
Services.TransactionManager.Cancel();
|
||||||
return View(model);
|
return View(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
var session = _sessionLocator.For(typeof(Page));
|
Services.ContentManager.Create(model.Page.Item.ContentItem, publishNow ? VersionOptions.Published : VersionOptions.Draft);
|
||||||
session.Flush();
|
|
||||||
|
|
||||||
return RedirectToAction("List");
|
return RedirectToAction("List");
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ namespace Orchard.Pages.Models {
|
|||||||
|
|
||||||
public string Slug {
|
public string Slug {
|
||||||
get { return this.As<RoutableAspect>().Slug; }
|
get { return this.As<RoutableAspect>().Slug; }
|
||||||
|
set { this.As<RoutableAspect>().Slug = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public IUser Creator {
|
public IUser Creator {
|
||||||
|
@ -1,21 +1,36 @@
|
|||||||
using JetBrains.Annotations;
|
using System.Linq;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.Core.Common.Records;
|
using Orchard.Core.Common.Records;
|
||||||
|
using Orchard.Core.Common.Services;
|
||||||
using Orchard.Pages.Controllers;
|
using Orchard.Pages.Controllers;
|
||||||
using Orchard.Core.Common.Models;
|
using Orchard.Core.Common.Models;
|
||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
using Orchard.ContentManagement.Handlers;
|
using Orchard.ContentManagement.Handlers;
|
||||||
|
using Orchard.Pages.Services;
|
||||||
|
|
||||||
namespace Orchard.Pages.Models {
|
namespace Orchard.Pages.Models {
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public class PageHandler : ContentHandler {
|
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<Page>(PageDriver.ContentType.Name));
|
||||||
Filters.Add(new ActivatingFilter<CommonAspect>(PageDriver.ContentType.Name));
|
Filters.Add(new ActivatingFilter<CommonAspect>(PageDriver.ContentType.Name));
|
||||||
Filters.Add(new ActivatingFilter<ContentPart<CommonVersionRecord>>(PageDriver.ContentType.Name));
|
Filters.Add(new ActivatingFilter<ContentPart<CommonVersionRecord>>(PageDriver.ContentType.Name));
|
||||||
Filters.Add(new ActivatingFilter<RoutableAspect>(PageDriver.ContentType.Name));
|
Filters.Add(new ActivatingFilter<RoutableAspect>(PageDriver.ContentType.Name));
|
||||||
Filters.Add(new ActivatingFilter<BodyAspect>(PageDriver.ContentType.Name));
|
Filters.Add(new ActivatingFilter<BodyAspect>(PageDriver.ContentType.Name));
|
||||||
Filters.Add(new StorageFilter<CommonVersionRecord>(commonRepository));
|
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));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,8 +10,6 @@ namespace Orchard.Pages.Services {
|
|||||||
Page GetPageOrDraft(string slug);
|
Page GetPageOrDraft(string slug);
|
||||||
Page GetLatest(string slug);
|
Page GetLatest(string slug);
|
||||||
Page GetLatest(int id);
|
Page GetLatest(int id);
|
||||||
Page New();
|
|
||||||
Page Create(bool publishNow, DateTime? publishDate);
|
|
||||||
void Delete(Page page);
|
void Delete(Page page);
|
||||||
void Publish(Page page);
|
void Publish(Page page);
|
||||||
void Publish(Page page, DateTime publishDate);
|
void Publish(Page page, DateTime publishDate);
|
||||||
|
@ -59,19 +59,6 @@ namespace Orchard.Pages.Services {
|
|||||||
return _contentManager.GetDraftRequired<Page>(page.Id);
|
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) {
|
public void Delete(Page page) {
|
||||||
_contentManager.Remove(page.ContentItem);
|
_contentManager.Remove(page.ContentItem);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user