2010-07-13 02:52:02 -07:00
using Orchard.ContentManagement ;
2010-06-09 15:18:12 -07:00
using Orchard.ContentManagement.Aspects ;
2010-06-08 10:50:08 -07:00
using Orchard.ContentManagement.Drivers ;
2010-07-21 10:10:39 -07:00
using Orchard.Core.ContentsLocation.Models ;
2010-06-08 10:50:08 -07:00
using Orchard.Core.Routable.Models ;
2010-06-09 15:18:12 -07:00
using Orchard.Core.Routable.Services ;
using Orchard.Core.Routable.ViewModels ;
2010-06-08 10:50:08 -07:00
using Orchard.Localization ;
using Orchard.UI.Notify ;
namespace Orchard.Core.Routable.Drivers {
public class RoutableDriver : ContentPartDriver < IsRoutable > {
2010-06-09 15:18:12 -07:00
private readonly IOrchardServices _services ;
private readonly IRoutableService _routableService ;
public RoutableDriver ( IOrchardServices services , IRoutableService routableService ) {
_services = services ;
_routableService = routableService ;
T = NullLocalizer . Instance ;
}
private const string TemplateName = "Parts/Routable.IsRoutable" ;
public Localizer T { get ; set ; }
protected override string Prefix {
get { return "Routable" ; }
}
int? GetContainerId ( IContent item ) {
var commonAspect = item . As < ICommonAspect > ( ) ;
if ( commonAspect ! = null & & commonAspect . Container ! = null ) {
return commonAspect . Container . ContentItem . Id ;
}
return null ;
}
string GetContainerSlug ( IContent item ) {
var commonAspect = item . As < ICommonAspect > ( ) ;
if ( commonAspect ! = null & & commonAspect . Container ! = null ) {
var routable = commonAspect . Container . As < IRoutableAspect > ( ) ;
if ( routable ! = null ) {
return routable . Slug ;
}
}
return null ;
2010-06-08 10:50:08 -07:00
}
2010-06-08 11:01:39 -07:00
2010-06-09 15:18:12 -07:00
protected override DriverResult Editor ( IsRoutable part ) {
var model = new RoutableEditorViewModel {
ContentType = part . ContentItem . ContentType ,
Id = part . ContentItem . Id ,
Slug = part . Slug ,
Title = part . Title ,
ContainerId = GetContainerId ( part ) ,
} ;
// TEMP: path format patterns replaces this logic
2010-07-13 22:27:01 -07:00
var path = part . Path ;
2010-06-24 14:35:18 -07:00
if ( path ! = null & & path . EndsWith ( part . Slug ) ) {
2010-06-09 15:18:12 -07:00
model . DisplayLeadingPath = path . Substring ( 0 , path . Length - part . Slug . Length ) ;
}
2010-07-14 23:03:46 -07:00
else {
2010-07-15 09:50:39 -07:00
var containerSlug = GetContainerSlug ( part ) ;
model . DisplayLeadingPath = ! string . IsNullOrWhiteSpace ( containerSlug )
? string . Format ( "{0}/" , containerSlug )
: "" ;
2010-07-14 23:03:46 -07:00
}
2010-06-09 15:18:12 -07:00
2010-07-21 17:17:13 -07:00
var location = part . GetLocation ( "Editor" ) ;
return ContentPartTemplate ( model , TemplateName , Prefix ) . Location ( location ) ;
2010-06-09 15:18:12 -07:00
}
protected override DriverResult Editor ( IsRoutable part , IUpdateModel updater ) {
var model = new RoutableEditorViewModel ( ) ;
updater . TryUpdateModel ( model , Prefix , null , null ) ;
part . Title = model . Title ;
part . Slug = model . Slug ;
// TEMP: path format patterns replaces this logic
var containerSlug = GetContainerSlug ( part ) ;
if ( string . IsNullOrEmpty ( containerSlug ) ) {
2010-07-13 22:27:01 -07:00
part . Path = model . Slug ;
2010-06-09 15:18:12 -07:00
}
else {
2010-07-13 22:27:01 -07:00
part . Path = containerSlug + "/" + model . Slug ;
2010-06-09 15:18:12 -07:00
}
if ( ! _routableService . IsSlugValid ( part . Slug ) ) {
2010-06-12 13:33:35 -07:00
updater . AddModelError ( "Routable.Slug" , T ( "Please do not use any of the following characters in your slugs: \"/\", \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\". No spaces are allowed (please use dashes or underscores instead)." ) ) ;
2010-06-09 15:18:12 -07:00
}
string originalSlug = part . Slug ;
if ( ! _routableService . ProcessSlug ( part ) ) {
_services . Notifier . Warning ( T ( "Slugs in conflict. \"{0}\" is already set for a previously created {2} so now it has the slug \"{1}\"" ,
originalSlug , part . Slug , part . ContentItem . ContentType ) ) ;
}
return Editor ( part ) ;
}
2010-06-08 10:50:08 -07:00
}
}