2011-03-10 11:45:07 -08:00
using System ;
using System.Linq ;
using System.Web.Mvc ;
2010-12-01 15:55:02 -08:00
using Orchard.ContentManagement ;
2010-11-09 16:00:51 -08:00
using Orchard.ContentManagement.Drivers ;
2010-11-08 17:44:43 -08:00
using Orchard.ContentManagement.Handlers ;
2010-12-01 15:55:02 -08:00
using Orchard.ContentManagement.MetaData ;
2010-11-10 16:35:17 -08:00
using Orchard.Core.Common.Models ;
2010-11-08 15:33:56 -08:00
using Orchard.Core.Containers.Models ;
2010-11-10 16:35:17 -08:00
using Orchard.Core.Containers.Settings ;
2011-03-10 11:45:07 -08:00
using Orchard.Core.Containers.ViewModels ;
2010-11-08 17:44:43 -08:00
using Orchard.Data ;
2010-12-01 15:55:02 -08:00
using Orchard.Localization ;
using Orchard.UI.Notify ;
2010-11-08 15:33:56 -08:00
namespace Orchard.Core.Containers.Drivers {
2010-11-08 17:44:43 -08:00
public class ContainerPartDriver : ContentPartDriver < ContainerPart > {
2010-12-01 15:55:02 -08:00
private readonly IContentDefinitionManager _contentDefinitionManager ;
public ContainerPartDriver ( IContentDefinitionManager contentDefinitionManager , IOrchardServices orchardServices ) {
_contentDefinitionManager = contentDefinitionManager ;
Services = orchardServices ;
T = NullLocalizer . Instance ;
}
public IOrchardServices Services { get ; private set ; }
public Localizer T { get ; set ; }
protected override DriverResult Display ( ContainerPart part , string displayType , dynamic shapeHelper ) {
return Combined (
ContentShape ( "Parts_Container_Contained" ,
( ) = > shapeHelper . Parts_Container_Contained ( ContentPart : part ) ) ,
ContentShape ( "Parts_Container_Contained_Summary" ,
( ) = > shapeHelper . Parts_Container_Contained_Summary ( ContentPart : part ) ) ,
ContentShape ( "Parts_Container_Contained_SummaryAdmin" ,
( ) = > shapeHelper . Parts_Container_Contained_SummaryAdmin ( ContentPart : part ) )
) ;
}
2010-11-08 17:44:43 -08:00
protected override DriverResult Editor ( ContainerPart part , dynamic shapeHelper ) {
2010-12-01 15:55:02 -08:00
// if there are no containable items then show a nice little warning
if ( ! _contentDefinitionManager . ListTypeDefinitions ( )
. Where ( typeDefinition = > typeDefinition . Parts . Any ( partDefinition = > partDefinition . PartDefinition . Name = = "ContainablePart" ) ) . Any ( ) ) {
Services . Notifier . Warning ( T ( "There are no content types in the system with a Containable part attached. Consider adding a Containable part to some content type, existing or new, in order to relate items to this (Container enabled) item." ) ) ;
}
2011-03-10 11:45:07 -08:00
return Editor ( part , ( IUpdateModel ) null , shapeHelper ) ;
2010-12-01 23:59:10 -08:00
}
protected override DriverResult Editor ( ContainerPart part , IUpdateModel updater , dynamic shapeHelper ) {
2010-11-08 17:44:43 -08:00
return ContentShape (
"Parts_Container_Edit" ,
( ) = > {
2011-03-10 11:45:07 -08:00
var model = new ContainerViewModel { Part = part } ;
2011-03-16 12:38:04 -07:00
// todo: is there a non-string comparison way to find ContainableParts?
2011-03-10 11:45:07 -08:00
var containables = _contentDefinitionManager . ListTypeDefinitions ( ) . Where ( td = > td . Parts . Any ( p = > p . PartDefinition . Name = = "ContainablePart" ) ) . ToList ( ) ;
var listItems = new [ ] { new SelectListItem { Text = T ( "(Any)" ) . Text , Value = "" } }
. Concat ( containables . Select ( x = > new SelectListItem {
Value = Convert . ToString ( x . Name ) ,
Text = x . DisplayName ,
Selected = x . Name = = model . Part . Record . ItemContentType ,
} ) )
. ToList ( ) ;
2010-11-08 17:44:43 -08:00
2011-03-10 11:45:07 -08:00
model . AvailableContainables = new SelectList ( listItems , "Value" , "Text" , model . Part . Record . ItemContentType ) ;
if ( updater ! = null ) {
updater . TryUpdateModel ( model , "Container" , null , null ) ;
}
return shapeHelper . EditorTemplate ( TemplateName : "Container" , Model : model , Prefix : "Container" ) ;
2010-11-08 17:44:43 -08:00
} ) ;
}
2011-03-16 12:38:04 -07:00
protected override void Importing ( ContainerPart part , ImportContentContext context ) {
var itemContentType = context . Attribute ( part . PartDefinition . Name , "ItemContentType" ) ;
if ( itemContentType ! = null ) {
if ( _contentDefinitionManager . GetTypeDefinition ( itemContentType ) ! = null ) {
part . Record . ItemContentType = itemContentType ;
}
}
var paginated = context . Attribute ( part . PartDefinition . Name , "Paginated" ) ;
if ( paginated ! = null ) {
part . Record . Paginated = Convert . ToBoolean ( paginated ) ;
}
var pageSize = context . Attribute ( part . PartDefinition . Name , "PageSize" ) ;
if ( pageSize ! = null ) {
part . Record . PageSize = Convert . ToInt32 ( pageSize ) ;
}
var orderByProperty = context . Attribute ( part . PartDefinition . Name , "OrderByProperty" ) ;
if ( orderByProperty ! = null ) {
part . Record . OrderByProperty = orderByProperty ;
}
var orderByDirection = context . Attribute ( part . PartDefinition . Name , "OrderByDirection" ) ;
if ( orderByDirection ! = null ) {
part . Record . OrderByDirection = Convert . ToInt32 ( orderByDirection ) ;
}
}
protected override void Exporting ( ContainerPart part , ExportContentContext context ) {
context . Element ( part . PartDefinition . Name ) . SetAttributeValue ( "ItemContentType" , part . Record . ItemContentType ) ;
context . Element ( part . PartDefinition . Name ) . SetAttributeValue ( "Paginated" , part . Record . Paginated ) ;
context . Element ( part . PartDefinition . Name ) . SetAttributeValue ( "PageSize" , part . Record . PageSize ) ;
context . Element ( part . PartDefinition . Name ) . SetAttributeValue ( "OrderByProperty" , part . Record . OrderByProperty ) ;
context . Element ( part . PartDefinition . Name ) . SetAttributeValue ( "OrderByDirection" , part . Record . OrderByDirection ) ;
}
2010-11-08 17:44:43 -08:00
}
public class ContainerPartHandler : ContentHandler {
2010-12-01 15:55:02 -08:00
public ContainerPartHandler ( IRepository < ContainerPartRecord > repository ) {
2010-11-08 17:44:43 -08:00
Filters . Add ( StorageFilter . For ( repository ) ) ;
2010-11-09 16:00:51 -08:00
OnInitializing < ContainerPart > ( ( context , part ) = > {
2010-11-10 16:35:17 -08:00
part . Record . PageSize = part . Settings . GetModel < ContainerTypePartSettings > ( ) . PageSizeDefault
? ? part . PartDefinition . Settings . GetModel < ContainerPartSettings > ( ) . PageSizeDefault ;
part . Record . Paginated = part . Settings . GetModel < ContainerTypePartSettings > ( ) . PaginatedDefault
? ? part . PartDefinition . Settings . GetModel < ContainerPartSettings > ( ) . PaginatedDefault ;
//hard-coded defaults for ordering
part . Record . OrderByProperty = part . Is < CommonPart > ( ) ? "CommonPart.PublishedUtc" : "" ;
part . Record . OrderByDirection = ( int ) OrderByDirection . Descending ;
2010-11-09 16:00:51 -08:00
} ) ;
2010-11-08 17:44:43 -08:00
}
2010-11-08 15:33:56 -08:00
}
2010-11-08 17:44:43 -08:00
}