mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 12:53:33 +08:00
#17772: Making creation date editable on blog posts and pages by making it a setting on the common part
--HG-- branch : 1.x
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
using System.Xml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Xml;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Core.Common.Settings;
|
||||
using Orchard.Core.Common.ViewModels;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Security;
|
||||
@@ -16,6 +20,9 @@ namespace Orchard.Core.Common.Drivers {
|
||||
private readonly IMembershipService _membershipService;
|
||||
private readonly IClock _clock;
|
||||
|
||||
private const string DatePattern = "M/d/yyyy";
|
||||
private const string TimePattern = "h:mm tt";
|
||||
|
||||
public CommonPartDriver(
|
||||
IOrchardServices services,
|
||||
IContentManager contentManager,
|
||||
@@ -51,9 +58,7 @@ namespace Orchard.Core.Common.Drivers {
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(CommonPart part, dynamic shapeHelper) {
|
||||
return Combined(
|
||||
OwnerEditor(part, null, shapeHelper),
|
||||
ContainerEditor(part, null, shapeHelper));
|
||||
return BuildEditor(part, null, shapeHelper);
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
@@ -61,9 +66,22 @@ namespace Orchard.Core.Common.Drivers {
|
||||
part.ModifiedUtc = _clock.UtcNow;
|
||||
part.VersionModifiedUtc = _clock.UtcNow;
|
||||
|
||||
return Combined(
|
||||
OwnerEditor(part, updater, shapeHelper),
|
||||
ContainerEditor(part, updater, shapeHelper));
|
||||
return BuildEditor(part, updater, shapeHelper);
|
||||
}
|
||||
|
||||
private DriverResult BuildEditor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
List<DriverResult> parts = new List<DriverResult>();
|
||||
CommonTypePartSettings commonTypePartSettings = GetTypeSettings(part);
|
||||
|
||||
parts.Add(OwnerEditor(part, updater, shapeHelper));
|
||||
|
||||
if (commonTypePartSettings.ShowCreatedUtcEditor) {
|
||||
parts.Add(CreatedUtcEditor(part, updater, shapeHelper));
|
||||
}
|
||||
|
||||
parts.Add(ContainerEditor(part, updater, shapeHelper));
|
||||
|
||||
return Combined(parts.ToArray());
|
||||
}
|
||||
|
||||
DriverResult OwnerEditor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
@@ -95,6 +113,42 @@ namespace Orchard.Core.Common.Drivers {
|
||||
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Common.Owner", Model: model, Prefix: Prefix));
|
||||
}
|
||||
|
||||
DriverResult CreatedUtcEditor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
var currentUser = _authenticationService.GetAuthenticatedUser();
|
||||
if (!_authorizationService.TryCheckAccess(StandardPermissions.SiteOwner, currentUser, part)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var model = new CreatedUtcEditorViewModel();
|
||||
if (part.CreatedUtc != null) {
|
||||
model.CreatedDate = part.CreatedUtc.Value.ToLocalTime().ToString(DatePattern, CultureInfo.InvariantCulture);
|
||||
model.CreatedTime = part.CreatedUtc.Value.ToLocalTime().ToString(TimePattern, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
if (updater != null) {
|
||||
updater.TryUpdateModel(model, Prefix, null, null);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(model.CreatedDate) && !string.IsNullOrWhiteSpace(model.CreatedTime)) {
|
||||
DateTime createdUtc;
|
||||
string parseDateTime = String.Concat(model.CreatedDate, " ", model.CreatedTime);
|
||||
|
||||
// use an english culture as it is the one used by jQuery.datepicker by default
|
||||
if (DateTime.TryParse(parseDateTime, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AssumeLocal, out createdUtc)) {
|
||||
part.CreatedUtc = createdUtc;
|
||||
}
|
||||
else {
|
||||
updater.AddModelError(Prefix, T("{0} is an invalid date and time", parseDateTime));
|
||||
}
|
||||
}
|
||||
else {
|
||||
updater.AddModelError(Prefix, T("Both the date and time need to be specified."));
|
||||
}
|
||||
}
|
||||
|
||||
return ContentShape("Parts_Common_CreatedUtc_Edit",
|
||||
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Common.CreatedUtc", Model: model, Prefix: Prefix));
|
||||
}
|
||||
|
||||
DriverResult ContainerEditor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
var currentUser = _authenticationService.GetAuthenticatedUser();
|
||||
if (!_authorizationService.TryCheckAccess(StandardPermissions.SiteOwner, currentUser, part)) {
|
||||
@@ -124,6 +178,10 @@ namespace Orchard.Core.Common.Drivers {
|
||||
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Common.Container", Model: model, Prefix: Prefix));
|
||||
}
|
||||
|
||||
private static CommonTypePartSettings GetTypeSettings(CommonPart part) {
|
||||
return part.Settings.GetModel<CommonTypePartSettings>();
|
||||
}
|
||||
|
||||
protected override void Importing(CommonPart part, ImportContentContext context) {
|
||||
var owner = context.Attribute(part.PartDefinition.Name, "Owner");
|
||||
if (owner != null) {
|
||||
|
Reference in New Issue
Block a user