2015-09-07 23:55:06 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2015-09-07 11:31:33 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Orchard.ContentManagement;
|
2015-09-07 23:55:06 +01:00
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
2015-09-07 11:31:33 +01:00
|
|
|
|
using Orchard.ContentManagement.MetaData.Models;
|
2015-09-07 23:55:06 +01:00
|
|
|
|
using Orchard.Core.Contents.Settings;
|
2015-09-07 11:31:33 +01:00
|
|
|
|
using Orchard.Environment;
|
|
|
|
|
|
using Orchard.Layouts.Elements;
|
|
|
|
|
|
using Orchard.Layouts.Framework.Display;
|
|
|
|
|
|
using Orchard.Layouts.Framework.Drivers;
|
|
|
|
|
|
using Orchard.Layouts.Framework.Elements;
|
|
|
|
|
|
using Orchard.Layouts.Framework.Harvesters;
|
2015-09-07 23:55:06 +01:00
|
|
|
|
using Orchard.Layouts.Helpers;
|
2015-09-07 11:31:33 +01:00
|
|
|
|
using Orchard.Layouts.Settings;
|
2015-09-07 23:55:06 +01:00
|
|
|
|
using Orchard.Layouts.ViewModels;
|
2015-09-30 01:07:43 +01:00
|
|
|
|
using ContentItem = Orchard.ContentManagement.ContentItem;
|
2015-09-07 11:31:33 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Layouts.Providers {
|
|
|
|
|
|
public class PlaceableContentElementHarvester : Component, IElementHarvester {
|
|
|
|
|
|
private readonly Work<IContentManager> _contentManager;
|
|
|
|
|
|
|
|
|
|
|
|
public PlaceableContentElementHarvester(Work<IContentManager> contentManager) {
|
|
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ElementDescriptor> HarvestElements(HarvestElementsContext context) {
|
|
|
|
|
|
var contentTypeDefinitions = GetPlaceableContentTypeDefinitions();
|
|
|
|
|
|
|
|
|
|
|
|
return contentTypeDefinitions.Select(contentTypeDefinition => {
|
|
|
|
|
|
var settings = contentTypeDefinition.Settings;
|
|
|
|
|
|
var description = settings.ContainsKey("Description") ? settings["Description"] : contentTypeDefinition.DisplayName;
|
|
|
|
|
|
var stereotype = settings.ContainsKey("Stereotype") ? settings["Stereotype"] : default(string);
|
|
|
|
|
|
var category = GetCategoryFromStereotype(stereotype);
|
|
|
|
|
|
return new ElementDescriptor(typeof (PlaceableContentItem), contentTypeDefinition.Name, T(contentTypeDefinition.DisplayName), T(description), category) {
|
|
|
|
|
|
Displaying = Displaying,
|
|
|
|
|
|
Editor = Editor,
|
|
|
|
|
|
UpdateEditor = UpdateEditor,
|
|
|
|
|
|
ToolboxIcon = "\uf1b2",
|
|
|
|
|
|
EnableEditorDialog = true,
|
2015-09-30 01:07:43 +01:00
|
|
|
|
Removing = RemoveContentItem,
|
2015-09-07 11:31:33 +01:00
|
|
|
|
StateBag = new Dictionary<string, object> {
|
|
|
|
|
|
{ "ContentTypeName", contentTypeDefinition.Name }
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Displaying(ElementDisplayingContext context) {
|
|
|
|
|
|
var contentTypeName = (string)context.Element.Descriptor.StateBag["ContentTypeName"];
|
|
|
|
|
|
var element = (PlaceableContentItem)context.Element;
|
|
|
|
|
|
var contentItemId = element.ContentItemId;
|
2015-09-07 23:55:06 +01:00
|
|
|
|
var versionOptions = context.DisplayType == "Design" ? VersionOptions.Latest : VersionOptions.Published;
|
2015-09-07 11:31:33 +01:00
|
|
|
|
var contentItem = contentItemId != null
|
2015-09-07 23:55:06 +01:00
|
|
|
|
? _contentManager.Value.Get(contentItemId.Value, versionOptions)
|
2015-09-07 11:31:33 +01:00
|
|
|
|
: _contentManager.Value.New(contentTypeName);
|
|
|
|
|
|
|
|
|
|
|
|
var contentShape = contentItem != null ? _contentManager.Value.BuildDisplay(contentItem) : default(dynamic);
|
|
|
|
|
|
context.ElementShape.ContentItem = contentItem;
|
|
|
|
|
|
context.ElementShape.ContentShape = contentShape;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Editor(ElementEditorContext context) {
|
|
|
|
|
|
UpdateEditor(context);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateEditor(ElementEditorContext context) {
|
|
|
|
|
|
var contentTypeName = (string)context.Element.Descriptor.StateBag["ContentTypeName"];
|
|
|
|
|
|
var element = (PlaceableContentItem) context.Element;
|
2015-09-07 23:55:06 +01:00
|
|
|
|
var elementViewModel = new PlaceableContentItemViewModel {
|
|
|
|
|
|
ContentItemId = element.ContentItemId
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (context.Updater != null) {
|
|
|
|
|
|
context.Updater.TryUpdateModel(elementViewModel, context.Prefix, null, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var contentItemId = elementViewModel.ContentItemId;
|
2015-09-07 11:31:33 +01:00
|
|
|
|
var contentItem = contentItemId != null
|
|
|
|
|
|
? _contentManager.Value.Get(contentItemId.Value, VersionOptions.Latest)
|
|
|
|
|
|
: _contentManager.Value.New(contentTypeName);
|
|
|
|
|
|
|
|
|
|
|
|
dynamic contentEditorShape;
|
|
|
|
|
|
|
|
|
|
|
|
if (context.Updater != null) {
|
|
|
|
|
|
if (contentItem.Id == 0) {
|
|
|
|
|
|
_contentManager.Value.Create(contentItem, VersionOptions.Draft);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
2015-09-07 23:55:06 +01:00
|
|
|
|
var isDraftable = contentItem.TypeDefinition.Settings.GetModel<ContentTypeSettings>().Draftable;
|
|
|
|
|
|
var versionOptions = isDraftable ? VersionOptions.DraftRequired : VersionOptions.Latest;
|
|
|
|
|
|
contentItem = _contentManager.Value.Get(contentItem.Id, versionOptions);
|
2015-09-07 11:31:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-07 23:55:06 +01:00
|
|
|
|
element.ContentItemId = contentItem.Id;
|
|
|
|
|
|
|
|
|
|
|
|
// If the placed content item has the CommonPart attached, set its Container property to the Content (if any).
|
|
|
|
|
|
// This helps preventing widget types from appearing as orphans.
|
|
|
|
|
|
var commonPart = contentItem.As<ICommonPart>();
|
|
|
|
|
|
if (commonPart != null)
|
|
|
|
|
|
commonPart.Container = context.Content;
|
|
|
|
|
|
|
|
|
|
|
|
contentItem.IsPlaceableContent(true);
|
2015-09-07 11:31:33 +01:00
|
|
|
|
contentEditorShape = _contentManager.Value.UpdateEditor(contentItem, context.Updater);
|
2015-09-07 23:55:06 +01:00
|
|
|
|
|
|
|
|
|
|
_contentManager.Value.Publish(contentItem);
|
2015-09-07 11:31:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
contentEditorShape = _contentManager.Value.BuildEditor(contentItem);
|
|
|
|
|
|
}
|
2015-09-07 23:55:06 +01:00
|
|
|
|
|
|
|
|
|
|
var elementEditorShape = context.ShapeFactory.EditorTemplate(TemplateName: "Elements.PlaceableContentItem", Model: elementViewModel, Prefix: context.Prefix);
|
|
|
|
|
|
var editorWrapper = context.ShapeFactory.PlacedContentElementEditor(ContentItem: contentItem);
|
|
|
|
|
|
var stereotype = contentItem.TypeDefinition.Settings.ContainsKey("Stereotype") ? contentItem.TypeDefinition.Settings["Stereotype"] : default(string);
|
|
|
|
|
|
|
|
|
|
|
|
if(!String.IsNullOrWhiteSpace(stereotype))
|
|
|
|
|
|
editorWrapper.Metadata.Alternates.Add(String.Format("PlacedContentElementEditor__{0}", stereotype));
|
|
|
|
|
|
|
|
|
|
|
|
editorWrapper.Metadata.Position = "Properties:0";
|
|
|
|
|
|
elementEditorShape.Metadata.Position = "Properties:0";
|
2015-09-07 11:31:33 +01:00
|
|
|
|
contentEditorShape.Metadata.Position = "Properties:0";
|
2015-09-07 23:55:06 +01:00
|
|
|
|
context.EditorResult.Add(elementEditorShape);
|
2015-09-07 11:31:33 +01:00
|
|
|
|
context.EditorResult.Add(contentEditorShape);
|
2015-09-07 23:55:06 +01:00
|
|
|
|
context.EditorResult.Add(editorWrapper);
|
2015-09-07 11:31:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-30 01:07:43 +01:00
|
|
|
|
private void RemoveContentItem(ElementRemovingContext context) {
|
|
|
|
|
|
var element = (PlaceableContentItem) context.Element;
|
|
|
|
|
|
var contentItemId = element.ContentItemId;
|
|
|
|
|
|
var contentItem = contentItemId != null ? _contentManager.Value.Get(contentItemId.Value, VersionOptions.Latest) : default(ContentItem);
|
|
|
|
|
|
|
|
|
|
|
|
if(contentItem != null)
|
|
|
|
|
|
_contentManager.Value.Remove(contentItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-07 11:31:33 +01:00
|
|
|
|
private IEnumerable<ContentTypeDefinition> GetPlaceableContentTypeDefinitions() {
|
2015-10-03 00:34:42 +02:00
|
|
|
|
// Select all types that have either "Placeable" set ot true or the "Widget" stereotype.
|
2015-09-07 11:31:33 +01:00
|
|
|
|
var contentTypeDefinitionsQuery =
|
|
|
|
|
|
from contentTypeDefinition in _contentManager.Value.GetContentTypeDefinitions()
|
2015-10-03 00:28:48 +02:00
|
|
|
|
let stereotype = contentTypeDefinition.Settings.ContainsKey("Stereotype") ? contentTypeDefinition.Settings["Stereotype"] : default(string)
|
2015-10-03 00:34:42 +02:00
|
|
|
|
where contentTypeDefinition.Settings.GetModel<ContentTypeLayoutSettings>().Placeable || stereotype == "Widget"
|
2015-09-07 11:31:33 +01:00
|
|
|
|
select contentTypeDefinition;
|
|
|
|
|
|
|
|
|
|
|
|
return contentTypeDefinitionsQuery.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetCategoryFromStereotype(string stereotype) {
|
|
|
|
|
|
switch (stereotype) {
|
|
|
|
|
|
case "Widget":
|
|
|
|
|
|
return "Widgets";
|
|
|
|
|
|
default:
|
|
|
|
|
|
return "Content Items";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|