2010-07-14 15:31:43 -07:00
using System ;
2011-03-15 19:04:42 -07:00
using System.Xml ;
2010-07-14 15:31:43 -07:00
using Orchard.ContentManagement ;
using Orchard.ContentManagement.Drivers ;
2011-03-14 17:14:03 -07:00
using Orchard.ContentManagement.Handlers ;
2014-02-10 23:45:36 +01:00
using Orchard.Core.Common.ViewModels ;
using Orchard.Localization ;
using Orchard.Localization.Services ;
2010-12-10 11:12:29 -08:00
using Orchard.Mvc ;
2010-10-25 18:21:19 -07:00
using Orchard.PublishLater.Models ;
using Orchard.PublishLater.Services ;
using Orchard.PublishLater.ViewModels ;
2011-11-09 16:21:26 -08:00
using Orchard.Services ;
2010-07-14 15:31:43 -07:00
2010-10-25 18:21:19 -07:00
namespace Orchard.PublishLater.Drivers {
2010-07-14 15:31:43 -07:00
public class PublishLaterPartDriver : ContentPartDriver < PublishLaterPart > {
2010-10-13 04:51:15 -07:00
private const string TemplateName = "Parts/PublishLater" ;
2011-10-05 14:48:31 -07:00
private readonly IHttpContextAccessor _httpContextAccessor ;
2010-07-14 15:31:43 -07:00
private readonly IPublishLaterService _publishLaterService ;
2011-11-09 16:21:26 -08:00
private readonly IClock _clock ;
2014-08-02 03:19:34 +02:00
private readonly IDateLocalizationServices _dateLocalizationServices ;
2010-07-14 15:31:43 -07:00
public PublishLaterPartDriver (
IOrchardServices services ,
2011-10-05 14:48:31 -07:00
IHttpContextAccessor httpContextAccessor ,
2011-11-09 16:21:26 -08:00
IPublishLaterService publishLaterService ,
2013-11-28 22:24:40 +01:00
IClock clock ,
2014-08-02 03:19:34 +02:00
IDateLocalizationServices dateLocalizationServices ) {
2011-10-05 14:48:31 -07:00
_httpContextAccessor = httpContextAccessor ;
2010-07-14 15:31:43 -07:00
_publishLaterService = publishLaterService ;
2011-11-09 16:21:26 -08:00
_clock = clock ;
2014-08-02 03:19:34 +02:00
_dateLocalizationServices = dateLocalizationServices ;
2010-07-14 15:31:43 -07:00
T = NullLocalizer . Instance ;
Services = services ;
}
2013-11-28 22:24:40 +01:00
public Localizer T {
get ;
set ;
}
public IOrchardServices Services {
get ;
set ;
}
2010-07-14 15:31:43 -07:00
2010-10-13 04:51:15 -07:00
protected override string Prefix {
2013-11-28 22:24:40 +01:00
get {
return "PublishLater" ;
}
2010-07-14 15:31:43 -07:00
}
2010-10-13 04:51:15 -07:00
protected override DriverResult Display ( PublishLaterPart part , string displayType , dynamic shapeHelper ) {
return Combined (
ContentShape ( "Parts_PublishLater_Metadata" ,
2011-12-30 15:26:41 -08:00
( ) = > shapeHelper . Parts_PublishLater_Metadata ( ScheduledPublishUtc : part . ScheduledPublishUtc . Value ) ) ,
2010-10-13 04:51:15 -07:00
ContentShape ( "Parts_PublishLater_Metadata_Summary" ,
2011-12-30 15:26:41 -08:00
( ) = > shapeHelper . Parts_PublishLater_Metadata_Summary ( ScheduledPublishUtc : part . ScheduledPublishUtc . Value ) ) ,
2010-10-13 04:51:15 -07:00
ContentShape ( "Parts_PublishLater_Metadata_SummaryAdmin" ,
2011-12-30 15:26:41 -08:00
( ) = > shapeHelper . Parts_PublishLater_Metadata_SummaryAdmin ( ScheduledPublishUtc : part . ScheduledPublishUtc . Value ) )
2010-10-13 04:51:15 -07:00
) ;
2010-07-14 15:31:43 -07:00
}
2015-02-01 15:30:29 +01:00
private PublishLaterViewModel BuildViewModelFromPart ( PublishLaterPart part ) {
return new PublishLaterViewModel ( part ) {
Editor = new DateTimeEditor ( ) {
2014-02-10 23:45:36 +01:00
ShowDate = true ,
ShowTime = true ,
2014-08-02 03:19:34 +02:00
Date = ! part . IsPublished ( ) ? _dateLocalizationServices . ConvertToLocalizedDateString ( part . ScheduledPublishUtc . Value ) : "" ,
Time = ! part . IsPublished ( ) ? _dateLocalizationServices . ConvertToLocalizedTimeString ( part . ScheduledPublishUtc . Value ) : "" ,
2014-02-10 23:45:36 +01:00
}
2015-02-01 15:30:29 +01:00
} ;
}
protected override DriverResult Editor ( PublishLaterPart part , dynamic shapeHelper ) {
var model = BuildViewModelFromPart ( part ) ;
2010-10-26 07:56:56 -07:00
2010-10-15 11:55:48 -07:00
return ContentShape ( "Parts_PublishLater_Edit" ,
2010-10-13 04:51:15 -07:00
( ) = > shapeHelper . EditorTemplate ( TemplateName : TemplateName , Model : model , Prefix : Prefix ) ) ;
2010-07-14 15:31:43 -07:00
}
2013-11-28 22:24:40 +01:00
2010-10-13 04:51:15 -07:00
protected override DriverResult Editor ( PublishLaterPart part , IUpdateModel updater , dynamic shapeHelper ) {
2015-02-01 15:30:29 +01:00
var model = BuildViewModelFromPart ( part ) ;
2010-10-26 07:56:56 -07:00
2010-10-13 04:51:15 -07:00
updater . TryUpdateModel ( model , Prefix , null , null ) ;
2012-03-09 16:09:49 -08:00
var httpContext = _httpContextAccessor . Current ( ) ;
if ( httpContext . Request . Form [ "submit.Save" ] = = "submit.PublishLater" ) {
2014-02-10 23:45:36 +01:00
if ( ! String . IsNullOrWhiteSpace ( model . Editor . Date ) & & ! String . IsNullOrWhiteSpace ( model . Editor . Time ) ) {
2013-11-28 22:24:40 +01:00
try {
2014-08-02 03:19:34 +02:00
var utcDateTime = _dateLocalizationServices . ConvertFromLocalizedString ( model . Editor . Date , model . Editor . Time ) ;
2014-02-10 23:45:36 +01:00
if ( utcDateTime . HasValue ) {
if ( utcDateTime . Value < _clock . UtcNow ) {
updater . AddModelError ( "ScheduledPublishUtcDate" , T ( "You cannot schedule a publishing date in the past." ) ) ;
}
else {
_publishLaterService . Publish ( model . ContentItem , utcDateTime . Value ) ;
}
2011-11-09 16:21:26 -08:00
}
2010-12-10 11:12:29 -08:00
}
2013-11-28 22:24:40 +01:00
catch ( FormatException ) {
2014-02-10 23:45:36 +01:00
updater . AddModelError ( Prefix , T ( "'{0} {1}' could not be parsed as a valid date and time." , model . Editor . Date , model . Editor . Time ) ) ;
2010-12-10 11:12:29 -08:00
}
2010-11-04 13:20:46 -07:00
}
2015-04-15 14:56:41 +01:00
else {
2010-12-10 11:12:29 -08:00
updater . AddModelError ( Prefix , T ( "Both the date and time need to be specified for when this is to be published. If you don't want to schedule publishing then click Save or Publish Now." ) ) ;
2010-11-04 13:20:46 -07:00
}
}
2010-10-15 11:55:48 -07:00
return ContentShape ( "Parts_PublishLater_Edit" ,
2010-10-13 04:51:15 -07:00
( ) = > shapeHelper . EditorTemplate ( TemplateName : TemplateName , Model : model , Prefix : Prefix ) ) ;
}
2011-03-14 17:14:03 -07:00
2011-03-16 10:32:22 -07:00
protected override void Importing ( PublishLaterPart part , ImportContentContext context ) {
2015-08-28 17:05:51 -07:00
// Don't do anything if the tag is not specified.
if ( context . Data . Element ( part . PartDefinition . Name ) = = null ) {
return ;
2011-03-16 10:32:22 -07:00
}
2015-08-28 17:05:51 -07:00
context . ImportAttribute ( part . PartDefinition . Name , "ScheduledPublishUtc" , scheduledUtc = >
part . ScheduledPublishUtc . Value = XmlConvert . ToDateTime ( scheduledUtc , XmlDateTimeSerializationMode . Utc )
) ;
2011-03-16 10:32:22 -07:00
}
2011-03-14 17:14:03 -07:00
protected override void Exporting ( PublishLaterPart part , ExportContentContext context ) {
2011-03-15 19:04:42 -07:00
var scheduled = part . ScheduledPublishUtc . Value ;
if ( scheduled ! = null ) {
context . Element ( part . PartDefinition . Name )
. SetAttributeValue ( "ScheduledPublishUtc" , XmlConvert . ToString ( scheduled . Value , XmlDateTimeSerializationMode . Utc ) ) ;
}
2011-03-14 17:14:03 -07:00
}
2010-07-14 15:31:43 -07:00
}
}