2010-11-08 17:44:43 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Web.Mvc;
|
2013-12-04 17:09:03 -08:00
|
|
|
|
using System.Xml;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
2011-04-07 12:31:23 -07:00
|
|
|
|
using Orchard.Core.Common.Models;
|
2010-11-08 15:33:56 -08:00
|
|
|
|
using Orchard.Core.Containers.Models;
|
2013-12-04 17:09:03 -08:00
|
|
|
|
using Orchard.Core.Containers.Services;
|
|
|
|
|
|
using Orchard.Core.Containers.Settings;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
using Orchard.Core.Containers.ViewModels;
|
|
|
|
|
|
using Orchard.Localization;
|
2012-07-03 14:27:27 +02:00
|
|
|
|
using Orchard.ContentManagement.Handlers;
|
2010-11-08 15:33:56 -08:00
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Containers.Drivers {
|
|
|
|
|
|
public class ContainablePartDriver : ContentPartDriver<ContainablePart> {
|
2010-11-08 17:44:43 -08:00
|
|
|
|
private readonly IContentManager _contentManager;
|
2013-12-04 17:09:03 -08:00
|
|
|
|
private readonly IContainerService _containerService;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
|
2013-12-04 17:09:03 -08:00
|
|
|
|
public ContainablePartDriver(IContentManager contentManager, IContainerService containerService) {
|
2010-11-08 17:44:43 -08:00
|
|
|
|
_contentManager = contentManager;
|
2013-12-04 17:09:03 -08:00
|
|
|
|
_containerService = containerService;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(ContainablePart part, dynamic shapeHelper) {
|
2011-03-10 11:38:43 -08:00
|
|
|
|
return Editor(part, (IUpdateModel)null, shapeHelper);
|
2010-11-08 17:44:43 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(ContainablePart part, IUpdateModel updater, dynamic shapeHelper) {
|
|
|
|
|
|
return ContentShape(
|
|
|
|
|
|
"Parts_Containable_Edit",
|
|
|
|
|
|
() => {
|
2013-12-04 17:09:03 -08:00
|
|
|
|
var settings = part.TypePartDefinition.Settings.GetModel<ContainableTypePartSettings>();
|
2011-04-07 12:31:23 -07:00
|
|
|
|
var commonPart = part.As<CommonPart>();
|
2013-12-04 17:09:03 -08:00
|
|
|
|
var model = new ContainableViewModel {
|
|
|
|
|
|
ShowContainerPicker = settings.ShowContainerPicker,
|
2014-05-27 09:27:55 -07:00
|
|
|
|
ShowPositionEditor = settings.ShowPositionEditor,
|
|
|
|
|
|
Position = part.Position
|
2013-12-04 17:09:03 -08:00
|
|
|
|
};
|
2010-11-08 17:44:43 -08:00
|
|
|
|
|
2011-03-21 16:34:49 -07:00
|
|
|
|
if (commonPart != null && commonPart.Container != null) {
|
2010-11-08 17:44:43 -08:00
|
|
|
|
model.ContainerId = commonPart.Container.Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-04 17:09:03 -08:00
|
|
|
|
if (part.Id == 0 && commonPart != null && commonPart.Container != null) {
|
|
|
|
|
|
part.Position = _containerService.GetFirstPosition(commonPart.Container.Id) + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-11-08 17:44:43 -08:00
|
|
|
|
if (updater != null) {
|
|
|
|
|
|
var oldContainerId = model.ContainerId;
|
2013-12-04 17:09:03 -08:00
|
|
|
|
updater.TryUpdateModel(model, "Containable", null, new[] { "ShowContainerPicker", "ShowPositionEditor" });
|
2011-04-07 12:31:23 -07:00
|
|
|
|
if (oldContainerId != model.ContainerId) {
|
2011-03-21 16:34:49 -07:00
|
|
|
|
if (commonPart != null) {
|
2011-04-07 12:31:23 -07:00
|
|
|
|
var containerItem = _contentManager.Get(model.ContainerId, VersionOptions.Latest);
|
2012-06-29 17:16:29 -07:00
|
|
|
|
commonPart.Container = containerItem;
|
2011-03-21 16:34:49 -07:00
|
|
|
|
}
|
2011-04-07 12:31:23 -07:00
|
|
|
|
}
|
2013-12-04 17:09:03 -08:00
|
|
|
|
part.Position = model.Position;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-04 17:09:03 -08:00
|
|
|
|
var containers = _contentManager
|
|
|
|
|
|
.Query<ContainerPart, ContainerPartRecord>(VersionOptions.Latest)
|
|
|
|
|
|
.List()
|
|
|
|
|
|
.Where(container => container.ItemContentTypes.Any(type => type.Name == part.TypeDefinition.Name));
|
2011-03-10 16:11:46 -08:00
|
|
|
|
|
2010-11-08 17:44:43 -08:00
|
|
|
|
var listItems = new[] { new SelectListItem { Text = T("(None)").Text, Value = "0" } }
|
|
|
|
|
|
.Concat(containers.Select(x => new SelectListItem {
|
|
|
|
|
|
Value = Convert.ToString(x.Id),
|
2011-04-05 15:01:43 -07:00
|
|
|
|
Text = x.ContentItem.TypeDefinition.DisplayName + ": " + _contentManager.GetItemMetadata(x.ContentItem).DisplayText,
|
2010-11-08 17:44:43 -08:00
|
|
|
|
Selected = x.Id == model.ContainerId,
|
|
|
|
|
|
}))
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
model.AvailableContainers = new SelectList(listItems, "Value", "Text", model.ContainerId);
|
2013-12-04 17:09:03 -08:00
|
|
|
|
model.Position = part.Position;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
|
|
|
|
|
|
return shapeHelper.EditorTemplate(TemplateName: "Containable", Model: model, Prefix: "Containable");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2011-09-20 16:11:54 -07:00
|
|
|
|
|
2012-07-03 14:27:27 +02:00
|
|
|
|
protected override void Importing(ContainablePart part, ImportContentContext context) {
|
2013-12-04 17:09:03 -08:00
|
|
|
|
context.ImportAttribute(part.PartDefinition.Name, "Position", s => part.Position = XmlConvert.ToInt32(s));
|
2012-07-03 14:27:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Exporting(ContainablePart part, ExportContentContext context) {
|
2013-12-04 17:09:03 -08:00
|
|
|
|
context.Element(part.PartDefinition.Name).SetAttributeValue("Position", part.Position);
|
2012-07-03 14:27:27 +02:00
|
|
|
|
}
|
2010-11-08 15:33:56 -08:00
|
|
|
|
}
|
2010-11-08 17:44:43 -08:00
|
|
|
|
}
|