2010-01-22 22:25:52 +00:00
using System ;
using System.Linq ;
using JetBrains.Annotations ;
2010-01-10 11:18:28 +00:00
using Orchard.ContentManagement ;
using Orchard.Core.Common.Records ;
2010-01-22 22:25:52 +00:00
using Orchard.Core.Common.Services ;
using Orchard.Localization ;
2010-01-07 01:06:17 +00:00
using Orchard.Pages.Controllers ;
using Orchard.Core.Common.Models ;
using Orchard.Data ;
using Orchard.ContentManagement.Handlers ;
2010-01-22 22:25:52 +00:00
using Orchard.Pages.Services ;
using Orchard.UI.Notify ;
2010-01-07 01:06:17 +00:00
namespace Orchard.Pages.Models {
[UsedImplicitly]
public class PageHandler : ContentHandler {
2010-01-22 22:25:52 +00:00
private readonly IPageService _pageService ;
private readonly IRoutableService _routableService ;
private readonly IOrchardServices _orchardServices ;
public PageHandler ( IRepository < CommonVersionRecord > commonRepository , IPageService pageService , IRoutableService routableService , IOrchardServices orchardServices ) {
_pageService = pageService ;
_routableService = routableService ;
_orchardServices = orchardServices ;
T = NullLocalizer . Instance ;
2010-01-07 01:06:17 +00:00
Filters . Add ( new ActivatingFilter < Page > ( PageDriver . ContentType . Name ) ) ;
Filters . Add ( new ActivatingFilter < CommonAspect > ( PageDriver . ContentType . Name ) ) ;
2010-01-10 11:18:28 +00:00
Filters . Add ( new ActivatingFilter < ContentPart < CommonVersionRecord > > ( PageDriver . ContentType . Name ) ) ;
2010-01-07 01:06:17 +00:00
Filters . Add ( new ActivatingFilter < RoutableAspect > ( PageDriver . ContentType . Name ) ) ;
Filters . Add ( new ActivatingFilter < BodyAspect > ( PageDriver . ContentType . Name ) ) ;
2010-01-12 01:04:11 +00:00
Filters . Add ( new StorageFilter < CommonVersionRecord > ( commonRepository ) ) ;
2010-01-22 22:25:52 +00:00
OnPublished < Page > ( ( context , p ) = > ProcessSlug ( p ) ) ;
}
Localizer T { get ; set ; }
private void ProcessSlug ( Page page ) {
_routableService . FillSlug ( page . As < RoutableAspect > ( ) ) ;
var slugsLikeThis = _pageService . Get ( PageStatus . Published ) . Where (
p = > p . Slug . StartsWith ( page . Slug , StringComparison . OrdinalIgnoreCase ) & &
p . Id ! = page . Id ) . Select ( p = > p . Slug ) ;
//todo: (heskew) need better messages
if ( slugsLikeThis . Count ( ) > 0 ) {
//todo: (heskew) need better messages
var originalSlug = page . Slug ;
page . Slug = _routableService . GenerateUniqueSlug ( page . Slug , slugsLikeThis ) ;
if ( originalSlug ! = page . Slug )
_orchardServices . Notifier . Warning ( T ( "A different page is already published with this same slug ({0}) so a unique slug ({1}) was generated for this page." , originalSlug , page . Slug ) ) ;
}
2010-01-07 01:06:17 +00:00
}
}
}