2010-10-19 01:15:20 +08:00
|
|
|
|
using System.Collections.Generic;
|
2010-07-26 02:50:23 +08:00
|
|
|
|
using System.Linq;
|
2010-09-04 07:04:42 +08:00
|
|
|
|
using System.Web;
|
2010-03-04 15:31:42 +08:00
|
|
|
|
using JetBrains.Annotations;
|
2010-03-03 06:51:08 +08:00
|
|
|
|
using Orchard.ContentManagement;
|
2010-03-06 17:25:17 +08:00
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
2010-03-03 06:07:58 +08:00
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
2010-01-06 04:58:50 +08:00
|
|
|
|
using Orchard.Core.Common.Models;
|
2010-06-30 06:07:40 +08:00
|
|
|
|
using Orchard.Core.Common.Settings;
|
2010-01-06 04:58:50 +08:00
|
|
|
|
using Orchard.Core.Common.ViewModels;
|
2010-07-13 17:52:02 +08:00
|
|
|
|
using Orchard.Core.Routable.Models;
|
2010-07-26 02:50:23 +08:00
|
|
|
|
using Orchard.Services;
|
2010-01-06 04:58:50 +08:00
|
|
|
|
|
2010-03-04 15:31:42 +08:00
|
|
|
|
namespace Orchard.Core.Common.Drivers {
|
|
|
|
|
[UsedImplicitly]
|
2010-07-23 03:52:16 +08:00
|
|
|
|
public class BodyPartDriver : ContentPartDriver<BodyPart> {
|
2010-07-26 02:50:23 +08:00
|
|
|
|
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
|
|
|
|
|
|
2010-12-04 06:02:10 +08:00
|
|
|
|
private const string TemplateName = "Parts.Common.Body";
|
2010-03-03 06:07:58 +08:00
|
|
|
|
|
2010-07-26 02:50:23 +08:00
|
|
|
|
public BodyPartDriver(IOrchardServices services, IEnumerable<IHtmlFilter> htmlFilters) {
|
|
|
|
|
_htmlFilters = htmlFilters;
|
2010-03-03 06:07:58 +08:00
|
|
|
|
Services = services;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 02:50:23 +08:00
|
|
|
|
public IOrchardServices Services { get; set; }
|
|
|
|
|
|
2010-01-06 04:58:50 +08:00
|
|
|
|
protected override string Prefix {
|
2010-03-06 17:25:17 +08:00
|
|
|
|
get { return "Body"; }
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 17:06:01 +08:00
|
|
|
|
protected override DriverResult Display(BodyPart part, string displayType, dynamic shapeHelper) {
|
2010-10-12 16:02:18 +08:00
|
|
|
|
return Combined(
|
2010-10-13 12:08:27 +08:00
|
|
|
|
ContentShape("Parts_Common_Body",
|
|
|
|
|
() => {
|
|
|
|
|
var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text));
|
|
|
|
|
return shapeHelper.Parts_Common_Body(ContentPart: part, Html: new HtmlString(bodyText));
|
|
|
|
|
}),
|
|
|
|
|
ContentShape("Parts_Common_Body_Summary",
|
|
|
|
|
() => {
|
|
|
|
|
var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text));
|
|
|
|
|
return shapeHelper.Parts_Common_Body_Summary(ContentPart: part, Html: new HtmlString(bodyText));
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 17:06:01 +08:00
|
|
|
|
protected override DriverResult Editor(BodyPart part, dynamic shapeHelper) {
|
2010-03-06 17:25:17 +08:00
|
|
|
|
var model = BuildEditorViewModel(part);
|
2010-10-16 02:55:48 +08:00
|
|
|
|
return ContentShape("Parts_Common_Body_Edit",
|
2010-10-13 12:08:27 +08:00
|
|
|
|
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 17:06:01 +08:00
|
|
|
|
protected override DriverResult Editor(BodyPart part, IUpdateModel updater, dynamic shapeHelper) {
|
2010-03-06 17:25:17 +08:00
|
|
|
|
var model = BuildEditorViewModel(part);
|
2010-01-06 04:58:50 +08:00
|
|
|
|
updater.TryUpdateModel(model, Prefix, null, null);
|
2010-06-30 06:07:40 +08:00
|
|
|
|
|
2010-10-16 02:55:48 +08:00
|
|
|
|
return ContentShape("Parts_Common_Body_Edit",
|
2010-10-13 12:08:27 +08:00
|
|
|
|
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
2010-03-03 06:51:08 +08:00
|
|
|
|
|
2011-03-16 10:16:48 +08:00
|
|
|
|
protected override void Importing(BodyPart part, ContentManagement.Handlers.ImportContentContext context) {
|
|
|
|
|
var element = context.Element(part.PartDefinition.Name);
|
|
|
|
|
if (element != null) {
|
|
|
|
|
part.Text = element.Attribute("Text").Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-15 08:14:03 +08:00
|
|
|
|
protected override void Exporting(BodyPart part, ContentManagement.Handlers.ExportContentContext context) {
|
|
|
|
|
context.Element(part.PartDefinition.Name).SetAttributeValue("Text", part.Text);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-23 03:52:16 +08:00
|
|
|
|
private static BodyEditorViewModel BuildEditorViewModel(BodyPart part) {
|
2010-03-06 17:25:17 +08:00
|
|
|
|
return new BodyEditorViewModel {
|
2010-07-23 03:52:16 +08:00
|
|
|
|
BodyPart = part,
|
2010-11-19 06:44:42 +08:00
|
|
|
|
EditorFlavor = GetFlavor(part),
|
2010-07-26 02:50:23 +08:00
|
|
|
|
AddMediaPath = new PathBuilder(part).AddContentType().AddContainerSlug().AddSlug().ToString()
|
2010-03-06 17:25:17 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2010-06-30 06:07:40 +08:00
|
|
|
|
|
2010-07-23 03:52:16 +08:00
|
|
|
|
private static string GetFlavor(BodyPart part) {
|
2010-06-30 06:07:40 +08:00
|
|
|
|
var typePartSettings = part.Settings.GetModel<BodyTypePartSettings>();
|
|
|
|
|
return (typePartSettings != null && !string.IsNullOrWhiteSpace(typePartSettings.Flavor))
|
|
|
|
|
? typePartSettings.Flavor
|
|
|
|
|
: part.PartDefinition.Settings.GetModel<BodyPartSettings>().FlavorDefault;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-06 17:25:17 +08:00
|
|
|
|
class PathBuilder {
|
|
|
|
|
private readonly IContent _content;
|
|
|
|
|
private string _path;
|
|
|
|
|
|
|
|
|
|
public PathBuilder(IContent content) {
|
|
|
|
|
_content = content;
|
|
|
|
|
_path = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return _path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PathBuilder AddContentType() {
|
|
|
|
|
Add(_content.ContentItem.ContentType);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PathBuilder AddContainerSlug() {
|
2010-07-23 03:52:16 +08:00
|
|
|
|
var common = _content.As<ICommonPart>();
|
2010-03-06 17:25:17 +08:00
|
|
|
|
if (common == null)
|
|
|
|
|
return this;
|
|
|
|
|
|
2010-07-23 16:01:49 +08:00
|
|
|
|
var routable = common.Container.As<RoutePart>();
|
2010-03-06 17:25:17 +08:00
|
|
|
|
if (routable == null)
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
Add(routable.Slug);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PathBuilder AddSlug() {
|
2010-07-23 16:01:49 +08:00
|
|
|
|
var routable = _content.As<RoutePart>();
|
2010-03-06 17:25:17 +08:00
|
|
|
|
if (routable == null)
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
Add(routable.Slug);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Add(string segment) {
|
|
|
|
|
if (string.IsNullOrEmpty(segment))
|
|
|
|
|
return;
|
|
|
|
|
if (string.IsNullOrEmpty(_path))
|
|
|
|
|
_path = segment;
|
|
|
|
|
else
|
|
|
|
|
_path = _path + "/" + segment;
|
|
|
|
|
}
|
2010-03-03 06:51:08 +08:00
|
|
|
|
}
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
2010-03-04 15:31:42 +08:00
|
|
|
|
}
|