2011-03-15 19:04:42 -07:00
|
|
|
|
using System.Xml;
|
|
|
|
|
using Orchard.ContentManagement;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
2011-03-14 17:14:03 -07:00
|
|
|
|
using Orchard.ContentManagement.Handlers;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
using Orchard.Core.Common.Models;
|
|
|
|
|
using Orchard.Core.Common.ViewModels;
|
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
using Orchard.Security;
|
|
|
|
|
using Orchard.Services;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Common.Drivers {
|
2010-07-22 12:52:16 -07:00
|
|
|
|
public class CommonPartDriver : ContentPartDriver<CommonPart> {
|
|
|
|
|
private const string TemplatePrefix = "CommonPart";
|
2010-06-09 15:18:12 -07:00
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
private readonly IAuthenticationService _authenticationService;
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
private readonly IMembershipService _membershipService;
|
2011-01-06 18:43:26 -08:00
|
|
|
|
private readonly IClock _clock;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
|
2010-07-22 12:52:16 -07:00
|
|
|
|
public CommonPartDriver(
|
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,
|
|
|
|
|
IClock clock) {
|
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
_authenticationService = authenticationService;
|
|
|
|
|
_authorizationService = authorizationService;
|
|
|
|
|
_membershipService = membershipService;
|
2011-01-06 18:43:26 -08:00
|
|
|
|
_clock = clock;
|
2010-06-09 15:18:12 -07:00
|
|
|
|
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-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Display(CommonPart part, string displayType, dynamic shapeHelper) {
|
2010-10-13 04:51:15 -07:00
|
|
|
|
return Combined(
|
|
|
|
|
ContentShape("Parts_Common_Metadata",
|
|
|
|
|
() => shapeHelper.Parts_Common_Metadata(ContentPart: part)),
|
|
|
|
|
ContentShape("Parts_Common_Metadata_Summary",
|
|
|
|
|
() => shapeHelper.Parts_Common_Metadata_Summary(ContentPart: part)),
|
|
|
|
|
ContentShape("Parts_Common_Metadata_SummaryAdmin",
|
|
|
|
|
() => shapeHelper.Parts_Common_Metadata_SummaryAdmin(ContentPart: part))
|
|
|
|
|
);
|
2010-07-08 15:47:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Editor(CommonPart part, dynamic shapeHelper) {
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return Combined(
|
2010-10-18 10:13:57 -07:00
|
|
|
|
OwnerEditor(part, null, shapeHelper),
|
|
|
|
|
ContainerEditor(part, null, shapeHelper));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-22 13:52:40 -08:00
|
|
|
|
protected override DriverResult Editor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
2011-01-06 18:43:26 -08:00
|
|
|
|
// this event is hooked so the modified timestamp is changed when an edit-post occurs
|
|
|
|
|
part.ModifiedUtc = _clock.UtcNow;
|
|
|
|
|
part.VersionModifiedUtc = _clock.UtcNow;
|
|
|
|
|
|
2010-07-13 10:14:46 -07:00
|
|
|
|
return Combined(
|
2010-11-22 13:52:40 -08:00
|
|
|
|
OwnerEditor(part, updater, shapeHelper),
|
|
|
|
|
ContainerEditor(part, updater, shapeHelper));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-18 10:13:57 -07:00
|
|
|
|
DriverResult OwnerEditor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
2010-06-09 15:18:12 -07:00
|
|
|
|
var currentUser = _authenticationService.GetAuthenticatedUser();
|
2010-12-08 14:47:56 -08:00
|
|
|
|
if (!_authorizationService.TryCheckAccess(StandardPermissions.SiteOwner, currentUser, part)) {
|
2010-06-09 15:18:12 -07:00
|
|
|
|
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) {
|
2010-07-22 12:52:16 -07:00
|
|
|
|
updater.AddModelError("CommonPart.Owner", T("Invalid user name"));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
part.Owner = newOwner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-18 10:13:57 -07:00
|
|
|
|
return ContentShape("Parts_Common_Owner_Edit",
|
2010-12-03 14:02:10 -08:00
|
|
|
|
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Common.Owner", Model: model, Prefix: Prefix));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-18 10:13:57 -07:00
|
|
|
|
DriverResult ContainerEditor(CommonPart part, IUpdateModel updater, dynamic shapeHelper) {
|
2010-06-09 15:18:12 -07:00
|
|
|
|
var currentUser = _authenticationService.GetAuthenticatedUser();
|
2010-12-08 14:47:56 -08:00
|
|
|
|
if (!_authorizationService.TryCheckAccess(StandardPermissions.SiteOwner, currentUser, part)) {
|
2010-06-09 15:18:12 -07:00
|
|
|
|
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) {
|
2010-07-22 12:52:16 -07:00
|
|
|
|
updater.AddModelError("CommonPart.ContainerId", T("Invalid container"));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
part.Container = newContainer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-21 17:17:13 -07:00
|
|
|
|
|
2010-10-18 10:13:57 -07:00
|
|
|
|
return ContentShape("Parts_Common_Container_Edit",
|
2010-12-03 14:02:10 -08:00
|
|
|
|
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Common.Container", Model: model, Prefix: Prefix));
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
2011-03-14 17:14:03 -07:00
|
|
|
|
|
|
|
|
|
protected override void Exporting(CommonPart part, ExportContentContext context) {
|
|
|
|
|
if (part.Owner != null) {
|
|
|
|
|
var ownerIdentity = _contentManager.GetItemMetadata(part.Owner).Identity;
|
|
|
|
|
context.Element(part.PartDefinition.Name).SetAttributeValue("Owner", ownerIdentity.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (part.Container != null) {
|
|
|
|
|
var containerIdentity = _contentManager.GetItemMetadata(part.Container).Identity;
|
|
|
|
|
context.Element(part.PartDefinition.Name).SetAttributeValue("Container", containerIdentity.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-15 19:04:42 -07:00
|
|
|
|
if (part.CreatedUtc != null) {
|
|
|
|
|
context.Element(part.PartDefinition.Name)
|
|
|
|
|
.SetAttributeValue("CreatedUtc", XmlConvert.ToString(part.CreatedUtc.Value, XmlDateTimeSerializationMode.Utc));
|
|
|
|
|
}
|
|
|
|
|
if (part.PublishedUtc != null) {
|
|
|
|
|
context.Element(part.PartDefinition.Name)
|
|
|
|
|
.SetAttributeValue("PublishedUtc", XmlConvert.ToString(part.PublishedUtc.Value, XmlDateTimeSerializationMode.Utc));
|
|
|
|
|
}
|
|
|
|
|
if (part.ModifiedUtc != null) {
|
|
|
|
|
context.Element(part.PartDefinition.Name)
|
|
|
|
|
.SetAttributeValue("ModifiedUtc", XmlConvert.ToString(part.ModifiedUtc.Value, XmlDateTimeSerializationMode.Utc));
|
|
|
|
|
}
|
2011-03-14 17:14:03 -07:00
|
|
|
|
}
|
2010-06-09 15:18:12 -07:00
|
|
|
|
}
|
|
|
|
|
}
|