2010-07-13 10:14:46 -07:00
|
|
|
|
using System;
|
|
|
|
|
using Orchard.ContentManagement;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
|
|
|
|
using Orchard.Core.Common.Models;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
using Orchard.Core.Common.Services;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
using Orchard.Core.Common.ViewModels;
|
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
using Orchard.Security;
|
|
|
|
|
using Orchard.Services;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
using Orchard.UI.Notify;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Common.Drivers {
|
|
|
|
|
public class CommonDriver : ContentPartDriver<CommonAspect> {
|
2010-07-13 10:14:46 -07:00
|
|
|
|
private const string TemplatePrefix = "CommonAspect";
|
2010-06-09 15:18:12 -07:00
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
private readonly IAuthenticationService _authenticationService;
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
private readonly IMembershipService _membershipService;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
private readonly ICommonService _commonService;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
private readonly IClock _clock;
|
|
|
|
|
|
|
|
|
|
public CommonDriver(
|
2010-07-13 10:14:46 -07:00
|
|
|
|
IOrchardServices services,
|
2010-06-09 15:18:12 -07:00
|
|
|
|
IContentManager contentManager,
|
|
|
|
|
IAuthenticationService authenticationService,
|
|
|
|
|
IAuthorizationService authorizationService,
|
|
|
|
|
IMembershipService membershipService,
|
2010-07-13 10:14:46 -07:00
|
|
|
|
ICommonService commonService,
|
2010-06-09 15:18:12 -07:00
|
|
|
|
IClock clock) {
|
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
_authenticationService = authenticationService;
|
|
|
|
|
_authorizationService = authorizationService;
|
|
|
|
|
_membershipService = membershipService;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
_commonService = commonService;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
_clock = clock;
|
|
|
|
|
T = NullLocalizer.Instance;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
Services = services;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Localizer T { get; set; }
|
2010-07-13 10:14:46 -07:00
|
|
|
|
public IOrchardServices Services { get; set; }
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
2010-07-08 15:47:20 -07:00
|
|
|
|
protected override DriverResult Display(CommonAspect part, string displayType) {
|
|
|
|
|
var model = new CommonMetadataViewModel(part);
|
|
|
|
|
return Combined(
|
|
|
|
|
ContentPartTemplate(model, "Parts/Common.Metadata").LongestMatch(displayType, "Summary", "SummaryAdmin").Location("metadata"),
|
|
|
|
|
ContentPartTemplate(model, "Parts/Common.Publish").LongestMatch(displayType, "Summary", "SummaryAdmin").Location("secondary"));
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-09 15:18:12 -07:00
|
|
|
|
protected override DriverResult Editor(CommonAspect part) {
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return Combined(
|
|
|
|
|
OwnerEditor(part, null),
|
|
|
|
|
ContainerEditor(part, null),
|
|
|
|
|
PublishEditor(part, null));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(CommonAspect instance, ContentManagement.IUpdateModel updater) {
|
|
|
|
|
// this event is hooked so the modified timestamp is changed when an edit-post occurs.
|
|
|
|
|
instance.ModifiedUtc = _clock.UtcNow;
|
|
|
|
|
instance.VersionModifiedUtc = _clock.UtcNow;
|
|
|
|
|
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return Combined(
|
|
|
|
|
OwnerEditor(instance, updater),
|
|
|
|
|
ContainerEditor(instance, updater),
|
|
|
|
|
PublishEditor(instance, updater));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DriverResult PublishEditor(CommonAspect part, IUpdateModel updater) {
|
|
|
|
|
var model = new PublishEditorViewModel(part);
|
|
|
|
|
|
|
|
|
|
if (updater != null) {
|
|
|
|
|
updater.TryUpdateModel(model, TemplatePrefix, null, null);
|
|
|
|
|
switch (model.Command) {
|
|
|
|
|
case "PublishNow":
|
|
|
|
|
_commonService.Publish(model.ContentItem);
|
|
|
|
|
Services.Notifier.Information(T("{0} has been published!", model.ContentItem.TypeDefinition.DisplayName));
|
|
|
|
|
break;
|
|
|
|
|
case "PublishLater":
|
|
|
|
|
DateTime scheduled;
|
|
|
|
|
if (DateTime.TryParse(string.Format("{0} {1}", model.ScheduledPublishUtcDate, model.ScheduledPublishUtcTime), out scheduled))
|
|
|
|
|
model.ScheduledPublishUtc = scheduled;
|
|
|
|
|
_commonService.Publish(model.ContentItem, model.ScheduledPublishUtc.HasValue ? model.ScheduledPublishUtc.Value : DateTime.MaxValue);
|
|
|
|
|
Services.Notifier.Information(T("{0} has been scheduled for publishing!", model.ContentItem.TypeDefinition.DisplayName));
|
|
|
|
|
break;
|
|
|
|
|
case "SaveDraft":
|
|
|
|
|
Services.Notifier.Information(T("{0} draft has been saved!", model.ContentItem.TypeDefinition.DisplayName));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ContentPartTemplate(model, "Parts/Common.Publish", TemplatePrefix).Location("secondary", "1");
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DriverResult OwnerEditor(CommonAspect part, IUpdateModel updater) {
|
|
|
|
|
var currentUser = _authenticationService.GetAuthenticatedUser();
|
|
|
|
|
if (!_authorizationService.TryCheckAccess(Permissions.ChangeOwner, currentUser, part)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var model = new OwnerEditorViewModel();
|
|
|
|
|
if (part.Owner != null)
|
|
|
|
|
model.Owner = part.Owner.UserName;
|
|
|
|
|
|
|
|
|
|
if (updater != null) {
|
|
|
|
|
var priorOwner = model.Owner;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
updater.TryUpdateModel(model, TemplatePrefix, null, null);
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
|
|
|
|
if (model.Owner != null && model.Owner != priorOwner) {
|
|
|
|
|
var newOwner = _membershipService.GetUser(model.Owner);
|
|
|
|
|
if (newOwner == null) {
|
|
|
|
|
updater.AddModelError("CommonAspect.Owner", T("Invalid user name"));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
part.Owner = newOwner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return ContentPartTemplate(model, "Parts/Common.Owner", TemplatePrefix).Location("primary", "10");
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DriverResult ContainerEditor(CommonAspect part, IUpdateModel updater) {
|
|
|
|
|
var currentUser = _authenticationService.GetAuthenticatedUser();
|
|
|
|
|
if (!_authorizationService.TryCheckAccess(Permissions.ChangeOwner, currentUser, part)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var model = new ContainerEditorViewModel();
|
|
|
|
|
if (part.Container != null)
|
|
|
|
|
model.ContainerId = part.Container.ContentItem.Id;
|
|
|
|
|
|
|
|
|
|
if (updater != null) {
|
|
|
|
|
var priorContainerId = model.ContainerId;
|
2010-07-13 10:14:46 -07:00
|
|
|
|
updater.TryUpdateModel(model, TemplatePrefix, null, null);
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
|
|
|
|
if (model.ContainerId != null && model.ContainerId != priorContainerId) {
|
|
|
|
|
var newContainer = _contentManager.Get((int)model.ContainerId, VersionOptions.Latest);
|
|
|
|
|
if (newContainer == null) {
|
|
|
|
|
updater.AddModelError("CommonAspect.ContainerId", T("Invalid container"));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
part.Container = newContainer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return ContentPartTemplate(model, "Parts/Common.Container", TemplatePrefix).Location("primary", "10.1");
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|