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 ;
2011-03-10 11:45:07 -08:00
using Orchard.Core.Containers.ViewModels ;
2010-12-01 15:55:02 -08:00
using Orchard.Localization ;
using Orchard.UI.Notify ;
2011-11-15 11:46:33 -08:00
using Orchard.DisplayManagement ;
using Orchard.Core.Containers.Extensions ;
using Orchard.Core.Routable.Models ;
using System.Web.Routing ;
using Orchard.Settings ;
using Orchard.Core.Feeds ;
using Orchard.UI.Navigation ;
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 ;
2011-11-15 11:46:33 -08:00
private readonly IOrchardServices _orchardServices ;
private readonly IContentManager _contentManager ;
private readonly dynamic _shapeFactory ;
private readonly ISiteService _siteService ;
private readonly IFeedManager _feedManager ;
public ContainerPartDriver (
IContentDefinitionManager contentDefinitionManager ,
IOrchardServices orchardServices ,
IShapeFactory shapeFactory ,
ISiteService siteService ,
IFeedManager feedManager ) {
2010-12-01 15:55:02 -08:00
_contentDefinitionManager = contentDefinitionManager ;
2011-11-15 11:46:33 -08:00
_orchardServices = orchardServices ;
_contentManager = orchardServices . ContentManager ;
_shapeFactory = shapeFactory ;
_siteService = siteService ;
_feedManager = feedManager ;
2010-12-01 15:55:02 -08:00
T = NullLocalizer . Instance ;
}
public Localizer T { get ; set ; }
protected override DriverResult Display ( ContainerPart part , string displayType , dynamic shapeHelper ) {
2011-11-15 11:46:33 -08:00
if ( ! part . ItemsShown )
return null ;
2010-12-01 15:55:02 -08:00
return Combined (
ContentShape ( "Parts_Container_Contained" ,
2011-11-15 11:46:33 -08:00
( ) = > {
var container = part . ContentItem ;
IContentQuery < ContentItem > query = _contentManager
. Query ( VersionOptions . Published )
. Join < CommonPartRecord > ( ) . Where ( cr = > cr . Container . Id = = container . Id ) ;
var descendingOrder = part . OrderByDirection = = ( int ) OrderByDirection . Descending ;
query = query . OrderBy ( part . OrderByProperty , descendingOrder ) ;
_feedManager . Register ( container . As < RoutePart > ( ) . Title , "rss" , new RouteValueDictionary { { "containerid" , container . Id } } ) ;
var pager = new Pager ( _siteService . GetSiteSettings ( ) , part . PagerParameters ) ;
pager . PageSize = part . PagerParameters . PageSize ! = null & & part . Paginated
? pager . PageSize
: part . PageSize ;
2011-11-27 22:50:16 +01:00
var pagerShape = shapeHelper . Pager ( pager ) . TotalItemCount ( query . Count ( ) ) ;
2011-11-15 11:46:33 -08:00
var startIndex = part . Paginated ? pager . GetStartIndex ( ) : 0 ;
var pageOfItems = query . Slice ( startIndex , pager . PageSize ) . ToList ( ) ;
2011-11-27 22:50:16 +01:00
var listShape = shapeHelper . List ( ) ;
listShape . AddRange ( pageOfItems . Select ( item = > _contentManager . BuildDisplay ( item , "Summary" ) ) ) ;
listShape . Classes . Add ( "content-items" ) ;
listShape . Classes . Add ( "list-items" ) ;
2011-11-15 11:46:33 -08:00
2011-11-27 22:50:16 +01:00
return shapeHelper . Parts_Container_Contained (
List : listShape ,
Pager : pagerShape
) ;
2011-11-15 11:46:33 -08:00
} ) ,
2010-12-01 15:55:02 -08:00
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
2011-11-15 11:46:33 -08:00
if ( ! _contentDefinitionManager . ListTypeDefinitions ( ) . Any ( typeDefinition = > typeDefinition . Parts . Any ( partDefinition = > partDefinition . PartDefinition . Name = = "ContainablePart" ) ) ) {
_orchardServices . 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." ) ) ;
2010-12-01 15:55:02 -08:00
}
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-11-15 11:46:33 -08:00
model . AvailableContainables = new SelectList ( listItems , "Value" , "Text" , model . Part . ItemContentType ) ;
2011-03-10 11:45:07 -08:00
if ( updater ! = null ) {
updater . TryUpdateModel ( model , "Container" , null , null ) ;
}
2011-11-15 11:46:33 -08:00
2011-03-10 11:45:07 -08:00
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 ;
}
}
2011-11-15 11:46:33 -08:00
var itemsShown = context . Attribute ( part . PartDefinition . Name , "ItemsShown" ) ;
if ( itemsShown ! = null ) {
part . Record . ItemsShown = Convert . ToBoolean ( itemsShown ) ;
}
2011-03-16 12:38:04 -07:00
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 ) ;
2011-11-15 11:46:33 -08:00
context . Element ( part . PartDefinition . Name ) . SetAttributeValue ( "ItemsShown" , part . Record . ItemsShown ) ;
2011-03-16 12:38:04 -07:00
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
}
}