2010-06-08 07:02:54 +08:00
using System ;
2010-07-17 05:27:27 +08:00
using System.Collections.Generic ;
2010-06-08 07:02:54 +08:00
using System.Linq ;
2010-07-17 05:27:27 +08:00
using System.Reflection ;
2010-06-08 07:02:54 +08:00
using System.Web.Mvc ;
using System.Web.Routing ;
using Orchard.ContentManagement ;
2010-07-15 06:31:43 +08:00
using Orchard.ContentManagement.Aspects ;
2010-06-25 07:11:10 +08:00
using Orchard.ContentManagement.MetaData ;
2010-07-23 10:35:16 +08:00
using Orchard.ContentManagement.MetaData.Models ;
2010-11-05 04:20:46 +08:00
using Orchard.ContentManagement.Records ;
2010-07-17 07:37:12 +08:00
using Orchard.Core.Common.Models ;
2010-07-23 10:35:16 +08:00
using Orchard.Core.Contents.Settings ;
2010-06-08 07:02:54 +08:00
using Orchard.Core.Contents.ViewModels ;
using Orchard.Data ;
2010-09-04 07:04:42 +08:00
using Orchard.DisplayManagement ;
2010-06-08 07:02:54 +08:00
using Orchard.Localization ;
using Orchard.Logging ;
2010-11-01 15:37:23 +08:00
using Orchard.UI.Navigation ;
2010-06-08 07:02:54 +08:00
using Orchard.UI.Notify ;
namespace Orchard.Core.Contents.Controllers {
[ValidateInput(false)]
public class AdminController : Controller , IUpdateModel {
private readonly IContentManager _contentManager ;
2010-06-25 07:11:10 +08:00
private readonly IContentDefinitionManager _contentDefinitionManager ;
2010-06-08 07:02:54 +08:00
private readonly ITransactionManager _transactionManager ;
public AdminController (
2010-06-16 19:22:51 +08:00
IOrchardServices orchardServices ,
2010-06-08 07:02:54 +08:00
IContentManager contentManager ,
2010-06-25 07:11:10 +08:00
IContentDefinitionManager contentDefinitionManager ,
2010-09-04 07:04:42 +08:00
ITransactionManager transactionManager ,
2010-10-16 08:24:30 +08:00
IShapeFactory shapeFactory ) {
2010-06-16 19:22:51 +08:00
Services = orchardServices ;
2010-06-08 07:02:54 +08:00
_contentManager = contentManager ;
2010-06-25 07:11:10 +08:00
_contentDefinitionManager = contentDefinitionManager ;
2010-06-08 07:02:54 +08:00
_transactionManager = transactionManager ;
T = NullLocalizer . Instance ;
Logger = NullLogger . Instance ;
2010-10-16 08:24:30 +08:00
Shape = shapeFactory ;
2010-06-08 07:02:54 +08:00
}
2010-09-04 07:04:42 +08:00
dynamic Shape { get ; set ; }
2010-06-16 19:22:51 +08:00
public IOrchardServices Services { get ; private set ; }
2010-06-08 07:02:54 +08:00
public Localizer T { get ; set ; }
public ILogger Logger { get ; set ; }
2010-11-01 15:37:23 +08:00
public ActionResult List ( ListContentsViewModel model , Pager pager ) {
2010-07-17 07:37:12 +08:00
if ( model . ContainerId ! = null & & _contentManager . GetLatest ( ( int ) model . ContainerId ) = = null )
2010-10-15 02:30:58 +08:00
return HttpNotFound ( ) ;
2010-07-17 07:37:12 +08:00
2010-07-23 10:35:16 +08:00
var query = _contentManager . Query ( VersionOptions . Latest , GetCreatableTypes ( ) . Select ( ctd = > ctd . Name ) . ToArray ( ) ) ;
2010-06-08 07:02:54 +08:00
2010-07-09 01:12:34 +08:00
if ( ! string . IsNullOrEmpty ( model . TypeName ) ) {
var contentTypeDefinition = _contentDefinitionManager . GetTypeDefinition ( model . TypeName ) ;
if ( contentTypeDefinition = = null )
2010-10-15 02:30:58 +08:00
return HttpNotFound ( ) ;
2010-07-09 01:12:34 +08:00
model . TypeDisplayName = ! string . IsNullOrWhiteSpace ( contentTypeDefinition . DisplayName )
? contentTypeDefinition . DisplayName
: contentTypeDefinition . Name ;
query = query . ForType ( model . TypeName ) ;
2010-06-08 07:02:54 +08:00
}
2010-07-17 07:37:12 +08:00
if ( model . ContainerId ! = null )
2010-07-23 03:52:16 +08:00
query = query . Join < CommonPartRecord > ( ) . Where ( cr = > cr . Container . Id = = model . ContainerId ) ;
2010-07-17 07:37:12 +08:00
2010-07-20 04:53:46 +08:00
switch ( model . Options . OrderBy ) {
case ContentsOrder . Modified :
2010-11-05 04:20:46 +08:00
//query = query.OrderByDescending<ContentPartRecord, int>(ci => ci.ContentItemRecord.Versions.Single(civr => civr.Latest).Id);
2010-11-01 23:59:13 +08:00
query = query . OrderByDescending < CommonPartRecord , DateTime ? > ( cr = > cr . ModifiedUtc ) ;
break ;
case ContentsOrder . Published :
query = query . OrderByDescending < CommonPartRecord , DateTime ? > ( cr = > cr . PublishedUtc ) ;
2010-07-20 04:53:46 +08:00
break ;
case ContentsOrder . Created :
2010-11-05 04:20:46 +08:00
//query = query.OrderByDescending<ContentPartRecord, int>(ci => ci.Id);
2010-11-01 23:59:13 +08:00
query = query . OrderByDescending < CommonPartRecord , DateTime ? > ( cr = > cr . CreatedUtc ) ;
2010-07-20 04:53:46 +08:00
break ;
}
2010-09-10 04:53:06 +08:00
model . Options . SelectedFilter = model . TypeName ;
model . Options . FilterOptions = GetCreatableTypes ( )
. Select ( ctd = > new KeyValuePair < string , string > ( ctd . Name , ctd . DisplayName ) )
. ToList ( ) . OrderBy ( kvp = > kvp . Key ) ;
2010-11-03 05:55:01 +08:00
var pagerShape = Shape . Pager ( pager ) . TotalItemCount ( query . Count ( ) ) ;
var pageOfContentItems = query . Slice ( pager . GetStartIndex ( ) , pager . PageSize ) . ToList ( ) ;
2010-06-08 07:02:54 +08:00
2010-09-09 15:01:46 +08:00
var list = Shape . List ( ) ;
2010-11-01 15:37:23 +08:00
list . AddRange ( pageOfContentItems . Select ( ci = > _contentManager . BuildDisplay ( ci , "SummaryAdmin" ) ) ) ;
2010-09-10 04:53:06 +08:00
var viewModel = Shape . ViewModel ( )
. ContentItems ( list )
2010-11-01 15:37:23 +08:00
. Pager ( pagerShape )
2010-09-10 04:53:06 +08:00
. Options ( model . Options )
. TypeDisplayName ( model . TypeDisplayName ? ? "" ) ;
2010-09-09 15:01:46 +08:00
2010-09-10 04:53:06 +08:00
return View ( viewModel ) ;
2010-06-08 07:02:54 +08:00
}
2010-07-23 10:35:16 +08:00
private IEnumerable < ContentTypeDefinition > GetCreatableTypes ( ) {
return _contentDefinitionManager . ListTypeDefinitions ( ) . Where ( ctd = > ctd . Settings . GetModel < ContentTypeSettings > ( ) . Creatable ) ;
}
2010-07-20 01:08:41 +08:00
[HttpPost, ActionName("List")]
[FormValueRequired("submit.Filter")]
public ActionResult ListFilterPOST ( ContentOptions options ) {
var routeValues = ControllerContext . RouteData . Values ;
if ( options ! = null ) {
routeValues [ "Options.OrderBy" ] = options . OrderBy ; //todo: don't hard-code the key
2010-07-23 10:35:16 +08:00
if ( GetCreatableTypes ( ) . Any ( ctd = > string . Equals ( ctd . Name , options . SelectedFilter , StringComparison . OrdinalIgnoreCase ) ) ) {
2010-07-20 01:08:41 +08:00
routeValues [ "id" ] = options . SelectedFilter ;
}
else {
routeValues . Remove ( "id" ) ;
}
}
return RedirectToAction ( "List" , routeValues ) ;
}
2010-07-17 05:27:27 +08:00
[HttpPost, ActionName("List")]
[FormValueRequired("submit.BulkEdit")]
2010-07-18 00:20:16 +08:00
public ActionResult ListPOST ( ContentOptions options , IEnumerable < int > itemIds , string returnUrl ) {
2010-07-27 01:50:16 +08:00
if ( itemIds ! = null ) {
var accessChecked = false ;
switch ( options . BulkAction ) {
case ContentsBulkAction . None :
break ;
case ContentsBulkAction . PublishNow :
foreach ( var item in itemIds . Select ( itemId = > _contentManager . GetLatest ( itemId ) ) ) {
if ( ! accessChecked & & ! Services . Authorizer . Authorize ( Permissions . PublishContent , item , T ( "Couldn't publish selected content." ) ) )
return new HttpUnauthorizedResult ( ) ;
accessChecked = true ;
_contentManager . Publish ( item ) ;
Services . ContentManager . Flush ( ) ;
}
Services . Notifier . Information ( T ( "Content successfully published." ) ) ;
break ;
case ContentsBulkAction . Unpublish :
foreach ( var item in itemIds . Select ( itemId = > _contentManager . GetLatest ( itemId ) ) ) {
if ( ! accessChecked & & ! Services . Authorizer . Authorize ( Permissions . PublishContent , item , T ( "Couldn't unpublish selected content." ) ) )
return new HttpUnauthorizedResult ( ) ;
accessChecked = true ;
_contentManager . Unpublish ( item ) ;
Services . ContentManager . Flush ( ) ;
}
Services . Notifier . Information ( T ( "Content successfully unpublished." ) ) ;
break ;
case ContentsBulkAction . Remove :
foreach ( var item in itemIds . Select ( itemId = > _contentManager . GetLatest ( itemId ) ) ) {
if ( ! accessChecked & & ! Services . Authorizer . Authorize ( Permissions . DeleteContent , item , T ( "Couldn't remove selected content." ) ) )
return new HttpUnauthorizedResult ( ) ;
accessChecked = true ;
_contentManager . Remove ( item ) ;
Services . ContentManager . Flush ( ) ;
}
Services . Notifier . Information ( T ( "Content successfully removed." ) ) ;
break ;
default :
throw new ArgumentOutOfRangeException ( ) ;
}
2010-07-17 05:27:27 +08:00
}
2010-07-18 00:20:16 +08:00
if ( ! String . IsNullOrEmpty ( returnUrl ) )
return Redirect ( returnUrl ) ;
2010-07-17 05:27:27 +08:00
return RedirectToAction ( "List" ) ;
}
2010-06-08 07:02:54 +08:00
ActionResult CreatableTypeList ( ) {
2010-10-17 01:39:58 +08:00
var viewModel = Shape . ViewModel ( ContentTypes : GetCreatableTypes ( ) ) ;
2010-09-22 07:09:31 +08:00
2010-10-17 01:39:58 +08:00
return View ( "CreatableTypeList" , viewModel ) ;
2010-06-08 07:02:54 +08:00
}
public ActionResult Create ( string id ) {
if ( string . IsNullOrEmpty ( id ) )
return CreatableTypeList ( ) ;
var contentItem = _contentManager . New ( id ) ;
2010-07-23 06:40:27 +08:00
if ( ! Services . Authorizer . Authorize ( Permissions . PublishContent , contentItem , T ( "Cannot create content" ) ) )
return new HttpUnauthorizedResult ( ) ;
2010-10-11 17:06:01 +08:00
var model = _contentManager . BuildEditor ( contentItem ) ;
2010-09-14 03:02:50 +08:00
return View ( model ) ;
2010-06-08 07:02:54 +08:00
}
2010-09-04 07:04:42 +08:00
[HttpPost, ActionName("Create")]
2010-11-05 04:20:46 +08:00
[FormValueRequired("submit.Save")]
2010-09-04 07:04:42 +08:00
public ActionResult CreatePOST ( string id ) {
2010-11-05 04:20:46 +08:00
return CreatePOST ( id , contentItem = > {
if ( ! contentItem . Has < IPublishingControlAspect > ( ) & & ! contentItem . TypeDefinition . Settings . GetModel < ContentTypeSettings > ( ) . Draftable )
_contentManager . Publish ( contentItem ) ;
} ) ;
}
[HttpPost, ActionName("Create")]
[FormValueRequired("submit.Publish")]
public ActionResult CreateAndPublishPOST ( string id ) {
return CreatePOST ( id , contentItem = > _contentManager . Publish ( contentItem ) ) ;
}
private ActionResult CreatePOST ( string id , Action < ContentItem > conditionallyPublish ) {
2010-09-04 07:04:42 +08:00
var contentItem = _contentManager . New ( id ) ;
2010-07-23 06:40:27 +08:00
if ( ! Services . Authorizer . Authorize ( Permissions . PublishContent , contentItem , T ( "Couldn't create content" ) ) )
return new HttpUnauthorizedResult ( ) ;
2010-11-05 04:20:46 +08:00
var isDraftable = contentItem . TypeDefinition . Settings . GetModel < ContentTypeSettings > ( ) . Draftable ;
_contentManager . Create (
contentItem ,
isDraftable ? VersionOptions . Draft : VersionOptions . Published ) ;
2010-07-14 23:54:19 +08:00
2010-11-05 04:20:46 +08:00
var model = _contentManager . UpdateEditor ( contentItem , this ) ;
2010-06-08 07:02:54 +08:00
if ( ! ModelState . IsValid ) {
_transactionManager . Cancel ( ) ;
2010-09-14 03:02:50 +08:00
return View ( model ) ;
2010-06-08 07:02:54 +08:00
}
2010-11-05 04:20:46 +08:00
conditionallyPublish ( contentItem ) ;
2010-07-14 23:54:19 +08:00
2010-09-14 03:02:50 +08:00
Services . Notifier . Information ( string . IsNullOrWhiteSpace ( contentItem . TypeDefinition . DisplayName )
2010-09-04 07:04:42 +08:00
? T ( "Your content has been created." )
2010-09-08 02:53:57 +08:00
: T ( "Your {0} has been created." , contentItem . TypeDefinition . DisplayName ) ) ;
2010-06-08 07:02:54 +08:00
return RedirectToAction ( "Edit" , new RouteValueDictionary { { "Id" , contentItem . Id } } ) ;
}
public ActionResult Edit ( int id ) {
var contentItem = _contentManager . Get ( id , VersionOptions . Latest ) ;
2010-07-14 02:30:39 +08:00
if ( contentItem = = null )
2010-10-15 02:30:58 +08:00
return HttpNotFound ( ) ;
2010-07-14 02:30:39 +08:00
2010-10-08 02:47:09 +08:00
if ( ! Services . Authorizer . Authorize ( Permissions . EditContent , contentItem , T ( "Cannot edit content" ) ) )
2010-07-23 06:40:27 +08:00
return new HttpUnauthorizedResult ( ) ;
2010-10-11 17:06:01 +08:00
var model = _contentManager . BuildEditor ( contentItem ) ;
2010-07-14 02:30:39 +08:00
2010-09-14 03:02:50 +08:00
return View ( model ) ;
2010-06-08 07:02:54 +08:00
}
2010-09-04 07:04:42 +08:00
[HttpPost, ActionName("Edit")]
2010-11-05 04:20:46 +08:00
[FormValueRequired("submit.Save")]
2010-10-13 16:30:10 +08:00
public ActionResult EditPOST ( int id , string returnUrl ) {
2010-11-05 04:20:46 +08:00
return EditPOST ( id , returnUrl , contentItem = > {
if ( ! contentItem . Has < IPublishingControlAspect > ( ) & & ! contentItem . TypeDefinition . Settings . GetModel < ContentTypeSettings > ( ) . Draftable )
_contentManager . Publish ( contentItem ) ;
} ) ;
}
[HttpPost, ActionName("Edit")]
[FormValueRequired("submit.Publish")]
public ActionResult EditAndPublishPOST ( int id , string returnUrl ) {
return EditPOST ( id , returnUrl , contentItem = > _contentManager . Publish ( contentItem ) ) ;
}
private ActionResult EditPOST ( int id , string returnUrl , Action < ContentItem > conditionallyPublish ) {
2010-09-04 07:04:42 +08:00
var contentItem = _contentManager . Get ( id , VersionOptions . DraftRequired ) ;
2010-07-14 02:30:39 +08:00
if ( contentItem = = null )
2010-10-15 02:30:58 +08:00
return HttpNotFound ( ) ;
2010-07-14 02:30:39 +08:00
2010-10-08 02:47:09 +08:00
if ( ! Services . Authorizer . Authorize ( Permissions . EditContent , contentItem , T ( "Couldn't edit content" ) ) )
2010-07-23 06:40:27 +08:00
return new HttpUnauthorizedResult ( ) ;
2010-10-11 17:06:01 +08:00
var model = _contentManager . UpdateEditor ( contentItem , this ) ;
2010-06-08 07:02:54 +08:00
if ( ! ModelState . IsValid ) {
_transactionManager . Cancel ( ) ;
2010-09-14 03:02:50 +08:00
return View ( "Edit" , model ) ;
2010-06-08 07:02:54 +08:00
}
2010-07-13 17:52:02 +08:00
2010-11-05 04:20:46 +08:00
conditionallyPublish ( contentItem ) ;
2010-07-15 06:31:43 +08:00
2010-09-14 03:02:50 +08:00
Services . Notifier . Information ( string . IsNullOrWhiteSpace ( contentItem . TypeDefinition . DisplayName )
2010-09-04 07:04:42 +08:00
? T ( "Your content has been saved." )
2010-09-08 02:53:57 +08:00
: T ( "Your {0} has been saved." , contentItem . TypeDefinition . DisplayName ) ) ;
2010-10-13 16:30:10 +08:00
if ( ! String . IsNullOrEmpty ( returnUrl ) )
return Redirect ( returnUrl ) ;
2010-06-08 07:02:54 +08:00
return RedirectToAction ( "Edit" , new RouteValueDictionary { { "Id" , contentItem . Id } } ) ;
}
2010-10-16 03:01:04 +08:00
public ActionResult Remove ( int id , string returnUrl ) {
2010-07-13 17:52:02 +08:00
var contentItem = _contentManager . Get ( id , VersionOptions . Latest ) ;
2010-07-23 06:40:27 +08:00
2010-10-08 02:47:09 +08:00
if ( ! Services . Authorizer . Authorize ( Permissions . DeleteContent , contentItem , T ( "Couldn't remove content" ) ) )
2010-07-23 06:40:27 +08:00
return new HttpUnauthorizedResult ( ) ;
2010-07-27 05:25:10 +08:00
if ( contentItem ! = null ) {
2010-07-09 01:12:34 +08:00
_contentManager . Remove ( contentItem ) ;
2010-09-04 07:04:42 +08:00
Services . Notifier . Information ( string . IsNullOrWhiteSpace ( contentItem . TypeDefinition . DisplayName )
? T ( "That content has been removed." )
: T ( "That {0} has been removed." , contentItem . TypeDefinition . DisplayName ) ) ;
2010-07-27 05:25:10 +08:00
}
2010-07-09 01:12:34 +08:00
if ( ! String . IsNullOrEmpty ( returnUrl ) )
return Redirect ( returnUrl ) ;
return RedirectToAction ( "List" ) ;
}
2010-07-16 15:49:24 +08:00
[HttpPost]
2010-07-18 23:46:44 +08:00
public ActionResult Publish ( int id , string returnUrl ) {
2010-07-16 15:49:24 +08:00
var contentItem = _contentManager . GetLatest ( id ) ;
if ( contentItem = = null )
2010-10-15 02:30:58 +08:00
return HttpNotFound ( ) ;
2010-07-16 15:49:24 +08:00
2010-07-23 06:40:27 +08:00
if ( ! Services . Authorizer . Authorize ( Permissions . PublishContent , contentItem , T ( "Couldn't publish content" ) ) )
return new HttpUnauthorizedResult ( ) ;
2010-07-16 15:49:24 +08:00
_contentManager . Publish ( contentItem ) ;
Services . ContentManager . Flush ( ) ;
2010-07-27 05:25:10 +08:00
Services . Notifier . Information ( string . IsNullOrWhiteSpace ( contentItem . TypeDefinition . DisplayName ) ? T ( "That content has been published." ) : T ( "That {0} has been published." , contentItem . TypeDefinition . DisplayName ) ) ;
2010-07-16 15:49:24 +08:00
2010-07-18 23:46:44 +08:00
if ( ! String . IsNullOrEmpty ( returnUrl ) )
return Redirect ( returnUrl ) ;
2010-07-16 15:49:24 +08:00
return RedirectToAction ( "List" ) ;
}
[HttpPost]
2010-07-18 23:46:44 +08:00
public ActionResult Unpublish ( int id , string returnUrl ) {
2010-07-16 15:49:24 +08:00
var contentItem = _contentManager . GetLatest ( id ) ;
if ( contentItem = = null )
2010-10-15 02:30:58 +08:00
return HttpNotFound ( ) ;
2010-07-16 15:49:24 +08:00
2010-07-23 06:40:27 +08:00
if ( ! Services . Authorizer . Authorize ( Permissions . PublishContent , contentItem , T ( "Couldn't unpublish content" ) ) )
return new HttpUnauthorizedResult ( ) ;
2010-07-16 15:49:24 +08:00
_contentManager . Unpublish ( contentItem ) ;
Services . ContentManager . Flush ( ) ;
2010-07-27 05:25:10 +08:00
Services . Notifier . Information ( string . IsNullOrWhiteSpace ( contentItem . TypeDefinition . DisplayName ) ? T ( "That content has been unpublished." ) : T ( "That {0} has been unpublished." , contentItem . TypeDefinition . DisplayName ) ) ;
2010-07-16 15:49:24 +08:00
2010-07-18 23:46:44 +08:00
if ( ! String . IsNullOrEmpty ( returnUrl ) )
return Redirect ( returnUrl ) ;
2010-07-16 15:49:24 +08:00
return RedirectToAction ( "List" ) ;
}
2010-06-08 07:02:54 +08:00
bool IUpdateModel . TryUpdateModel < TModel > ( TModel model , string prefix , string [ ] includeProperties , string [ ] excludeProperties ) {
return TryUpdateModel ( model , prefix , includeProperties , excludeProperties ) ;
}
void IUpdateModel . AddModelError ( string key , LocalizedString errorMessage ) {
2010-06-08 07:07:57 +08:00
ModelState . AddModelError ( key , errorMessage . ToString ( ) ) ;
2010-06-08 07:02:54 +08:00
}
}
2010-07-17 05:27:27 +08:00
public class FormValueRequiredAttribute : ActionMethodSelectorAttribute {
private readonly string _submitButtonName ;
public FormValueRequiredAttribute ( string submitButtonName ) {
_submitButtonName = submitButtonName ;
}
public override bool IsValidForRequest ( ControllerContext controllerContext , MethodInfo methodInfo ) {
var value = controllerContext . HttpContext . Request . Form [ _submitButtonName ] ;
return ! string . IsNullOrEmpty ( value ) ;
}
}
2010-06-08 07:02:54 +08:00
}