2010-11-08 17:44:43 -08:00
using System ;
using System.Linq ;
using System.Web.Mvc ;
using Orchard.ContentManagement ;
using Orchard.ContentManagement.Aspects ;
using Orchard.ContentManagement.Drivers ;
2010-11-08 15:33:56 -08:00
using Orchard.Core.Containers.Models ;
2010-11-08 17:44:43 -08:00
using Orchard.Core.Containers.ViewModels ;
2010-11-13 01:52:53 -08:00
using Orchard.Core.Routable.Models ;
using Orchard.Core.Routable.Services ;
2010-11-08 17:44:43 -08:00
using Orchard.Localization ;
2010-11-13 01:52:53 -08:00
using Orchard.UI.Notify ;
2010-11-08 15:33:56 -08:00
namespace Orchard.Core.Containers.Drivers {
public class ContainablePartDriver : ContentPartDriver < ContainablePart > {
2010-11-08 17:44:43 -08:00
private readonly IContentManager _contentManager ;
2010-11-13 01:52:53 -08:00
private readonly IRoutableService _routableService ;
private readonly IOrchardServices _services ;
2010-11-08 17:44:43 -08:00
2010-11-13 01:52:53 -08:00
public ContainablePartDriver ( IContentManager contentManager , IRoutableService routableService , IOrchardServices services ) {
2010-11-08 17:44:43 -08:00
_contentManager = contentManager ;
2010-11-13 01:52:53 -08:00
_routableService = routableService ;
_services = services ;
2010-11-08 17:44:43 -08:00
T = NullLocalizer . Instance ;
}
public Localizer T { get ; set ; }
protected override DriverResult Editor ( ContainablePart part , dynamic shapeHelper ) {
return Editor ( part , null , shapeHelper ) ;
}
protected override DriverResult Editor ( ContainablePart part , IUpdateModel updater , dynamic shapeHelper ) {
return ContentShape (
"Parts_Containable_Edit" ,
( ) = > {
var commonPart = part . As < ICommonPart > ( ) ;
var model = new ContainableViewModel ( ) ;
if ( commonPart . Container ! = null ) {
model . ContainerId = commonPart . Container . Id ;
}
if ( updater ! = null ) {
var oldContainerId = model . ContainerId ;
updater . TryUpdateModel ( model , "Containable" , null , null ) ;
if ( oldContainerId ! = model . ContainerId ) {
commonPart . Container = _contentManager . Get ( model . ContainerId , VersionOptions . Latest ) ;
2010-11-13 01:52:53 -08:00
// reprocess slug
var routable = part . As < IRoutableAspect > ( ) ;
_routableService . ProcessSlug ( part . As < IRoutableAspect > ( ) ) ;
if ( ! _routableService . ProcessSlug ( routable ) ) {
var existingConflict = _services . Notifier . List ( ) . FirstOrDefault ( n = > n . Message . TextHint = = "Slugs in conflict. \"{0}\" is already set for a previously created {2} so now it has the slug \"{1}\"" ) ;
if ( existingConflict ! = null )
existingConflict . Message = T ( "Slugs in conflict. \"{0}\" is already set for a previously created {2} so now it has the slug \"{1}\"" ,
routable . Slug , routable . GetEffectiveSlug ( ) , routable . ContentItem . ContentType ) ;
else
_services . Notifier . Warning ( T ( "Slugs in conflict. \"{0}\" is already set for a previously created {2} so now it has the slug \"{1}\"" ,
routable . Slug , routable . GetEffectiveSlug ( ) , routable . ContentItem . ContentType ) ) ;
}
2010-11-08 17:44:43 -08:00
}
}
var containers = _contentManager . Query < ContainerPart , ContainerPartRecord > ( VersionOptions . Latest ) . List ( ) ;
var listItems = new [ ] { new SelectListItem { Text = T ( "(None)" ) . Text , Value = "0" } }
. Concat ( containers . Select ( x = > new SelectListItem {
Value = Convert . ToString ( x . Id ) ,
Text = x . ContentItem . TypeDefinition . DisplayName + ": " + x . As < IRoutableAspect > ( ) . Title ,
Selected = x . Id = = model . ContainerId ,
} ) )
. ToList ( ) ;
model . AvailableContainers = new SelectList ( listItems , "Value" , "Text" , model . ContainerId ) ;
return shapeHelper . EditorTemplate ( TemplateName : "Containable" , Model : model , Prefix : "Containable" ) ;
} ) ;
}
2010-11-08 15:33:56 -08:00
}
2010-11-08 17:44:43 -08:00
}