Files
Orchard/src/Orchard.Web/Modules/Orchard.PublishLater/Drivers/PublishLaterPartDriver.cs
Daniel Stolt 6eb9599450 Refactored IDateTimeLocalization and moved it into Orchard.Framework.
Refactored IDateServices to use format strings provided by IDateTimeLocalization.
Removed duplicated date localization logic from DateEditorDriver, ArchiveLaterPartDriver, PublishLaterPartDriver, FieldTokens, DateTokens, and replaced with calls to IDateServices instead.
2014-03-04 16:57:09 -08:00

120 lines
6.0 KiB
C#

using System;
using System.Xml;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Mvc;
using Orchard.PublishLater.Models;
using Orchard.PublishLater.Services;
using Orchard.PublishLater.ViewModels;
using Orchard.Localization;
using System.Globalization;
using Orchard.Services;
using Orchard.Localization.Services;
namespace Orchard.PublishLater.Drivers {
public class PublishLaterPartDriver : ContentPartDriver<PublishLaterPart> {
private const string TemplateName = "Parts/PublishLater";
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IPublishLaterService _publishLaterService;
private readonly IClock _clock;
private readonly IDateServices _dateServices;
public PublishLaterPartDriver(
IOrchardServices services,
IHttpContextAccessor httpContextAccessor,
IPublishLaterService publishLaterService,
IClock clock,
IDateServices dateServices) {
_httpContextAccessor = httpContextAccessor;
_publishLaterService = publishLaterService;
_clock = clock;
_dateServices = dateServices;
T = NullLocalizer.Instance;
Services = services;
}
public Localizer T {
get;
set;
}
public IOrchardServices Services {
get;
set;
}
protected override string Prefix {
get {
return "PublishLater";
}
}
protected override DriverResult Display(PublishLaterPart part, string displayType, dynamic shapeHelper) {
return Combined(
ContentShape("Parts_PublishLater_Metadata",
() => shapeHelper.Parts_PublishLater_Metadata(ScheduledPublishUtc: part.ScheduledPublishUtc.Value)),
ContentShape("Parts_PublishLater_Metadata_Summary",
() => shapeHelper.Parts_PublishLater_Metadata_Summary(ScheduledPublishUtc: part.ScheduledPublishUtc.Value)),
ContentShape("Parts_PublishLater_Metadata_SummaryAdmin",
() => shapeHelper.Parts_PublishLater_Metadata_SummaryAdmin(ScheduledPublishUtc: part.ScheduledPublishUtc.Value))
);
}
protected override DriverResult Editor(PublishLaterPart part, dynamic shapeHelper) {
var model = new PublishLaterViewModel(part) {
ScheduledPublishUtc = part.ScheduledPublishUtc.Value,
ScheduledPublishDate = !part.IsPublished() ? _dateServices.ConvertToLocalDateString(part.ScheduledPublishUtc.Value, "") : "",
ScheduledPublishTime = !part.IsPublished() ? _dateServices.ConvertToLocalTimeString(part.ScheduledPublishUtc.Value, "") : ""
};
return ContentShape("Parts_PublishLater_Edit",
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
}
protected override DriverResult Editor(PublishLaterPart part, IUpdateModel updater, dynamic shapeHelper) {
var model = new PublishLaterViewModel(part);
updater.TryUpdateModel(model, Prefix, null, null);
var httpContext = _httpContextAccessor.Current();
if (httpContext.Request.Form["submit.Save"] == "submit.PublishLater") {
if (!String.IsNullOrWhiteSpace(model.ScheduledPublishDate) && !String.IsNullOrWhiteSpace(model.ScheduledPublishTime)) {
try {
var utcDateTime = _dateServices.ConvertFromLocalString(model.ScheduledPublishDate, model.ScheduledPublishTime);
model.ScheduledPublishUtc = part.ScheduledPublishUtc.Value = utcDateTime;
if (model.ScheduledPublishUtc < _clock.UtcNow) {
updater.AddModelError("ScheduledPublishUtcDate", T("You cannot schedule a publishing date in the past."));
}
else {
_publishLaterService.Publish(model.ContentItem, model.ScheduledPublishUtc.Value);
}
}
catch (FormatException) {
updater.AddModelError(Prefix, T("'{0} {1}' could not be parsed as a valid date and time.", model.ScheduledPublishDate, model.ScheduledPublishTime));
}
}
else if (!String.IsNullOrWhiteSpace(model.ScheduledPublishDate) || !String.IsNullOrWhiteSpace(model.ScheduledPublishTime)) {
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."));
}
}
return ContentShape("Parts_PublishLater_Edit",
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
}
protected override void Importing(PublishLaterPart part, ImportContentContext context) {
var scheduledUtc = context.Attribute(part.PartDefinition.Name, "ScheduledPublishUtc");
if (scheduledUtc != null) {
part.ScheduledPublishUtc.Value = XmlConvert.ToDateTime(scheduledUtc, XmlDateTimeSerializationMode.Utc);
}
}
protected override void Exporting(PublishLaterPart part, ExportContentContext context) {
var scheduled = part.ScheduledPublishUtc.Value;
if (scheduled != null) {
context.Element(part.PartDefinition.Name)
.SetAttributeValue("ScheduledPublishUtc", XmlConvert.ToString(scheduled.Value, XmlDateTimeSerializationMode.Utc));
}
}
}
}