2010-03-06 17:25:17 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
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;
|
|
|
|
|
using Orchard.Core.Common.ViewModels;
|
|
|
|
|
|
2010-03-04 15:31:42 +08:00
|
|
|
|
namespace Orchard.Core.Common.Drivers {
|
|
|
|
|
[UsedImplicitly]
|
2010-03-03 06:07:58 +08:00
|
|
|
|
public class BodyDriver : ContentPartDriver<BodyAspect> {
|
|
|
|
|
public IOrchardServices Services { get; set; }
|
2010-01-06 04:58:50 +08:00
|
|
|
|
private const string TemplateName = "Parts/Common.Body";
|
|
|
|
|
private const string DefaultTextEditorTemplate = "TinyMceTextEditor";
|
2010-03-03 06:07:58 +08:00
|
|
|
|
|
|
|
|
|
public BodyDriver(IOrchardServices services) {
|
|
|
|
|
Services = services;
|
|
|
|
|
}
|
|
|
|
|
|
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-03-03 06:07:58 +08:00
|
|
|
|
// \/\/ Haackalicious on many accounts - don't copy what has been done here for the wrapper \/\/
|
2010-01-06 04:58:50 +08:00
|
|
|
|
protected override DriverResult Display(BodyAspect part, string displayType) {
|
2010-03-03 19:32:01 +08:00
|
|
|
|
var model = new BodyDisplayViewModel { BodyAspect = part, Text = BbcodeReplace(part.Text) };
|
|
|
|
|
|
2010-03-03 06:07:58 +08:00
|
|
|
|
return Combined(
|
2010-03-04 05:15:50 +08:00
|
|
|
|
Services.Authorizer.Authorize(Permissions.ChangeOwner) ? ContentPartTemplate(model, "Parts/Common.Body.ManageWrapperPre").Location("primary", "5") : null,
|
|
|
|
|
Services.Authorizer.Authorize(Permissions.ChangeOwner) ? ContentPartTemplate(model, "Parts/Common.Body.Manage").Location("primary", "5") : null,
|
2010-03-03 19:32:01 +08:00
|
|
|
|
ContentPartTemplate(model, TemplateName, Prefix).LongestMatch(displayType, "Summary", "SummaryAdmin").Location("primary", "5"),
|
2010-03-04 05:15:50 +08:00
|
|
|
|
Services.Authorizer.Authorize(Permissions.ChangeOwner) ? ContentPartTemplate(model, "Parts/Common.Body.ManageWrapperPost").Location("primary", "5") : null);
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
2010-03-06 17:25:17 +08:00
|
|
|
|
|
2010-01-06 04:58:50 +08:00
|
|
|
|
protected override DriverResult Editor(BodyAspect part) {
|
2010-03-06 17:25:17 +08:00
|
|
|
|
var model = BuildEditorViewModel(part);
|
2010-01-07 08:12:56 +08:00
|
|
|
|
return ContentPartTemplate(model, TemplateName, Prefix).Location("primary", "5");
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-03 06:07:58 +08:00
|
|
|
|
protected override DriverResult Editor(BodyAspect part, IUpdateModel updater) {
|
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-01-07 08:12:56 +08:00
|
|
|
|
return ContentPartTemplate(model, TemplateName, Prefix).Location("primary", "5");
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
2010-03-03 06:51:08 +08:00
|
|
|
|
|
2010-03-06 17:25:17 +08:00
|
|
|
|
private static BodyEditorViewModel BuildEditorViewModel(BodyAspect part) {
|
|
|
|
|
return new BodyEditorViewModel {
|
|
|
|
|
BodyAspect = part,
|
|
|
|
|
TextEditorTemplate = DefaultTextEditorTemplate,
|
|
|
|
|
AddMediaPath= new PathBuilder(part).AddContentType().AddContainerSlug().AddSlug().ToString()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
|
var common = _content.As<ICommonAspect>();
|
|
|
|
|
if (common == null)
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
var routable = common.Container.As<RoutableAspect>();
|
|
|
|
|
if (routable == null)
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
Add(routable.Slug);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PathBuilder AddSlug() {
|
|
|
|
|
var routable = _content.As<RoutableAspect>();
|
|
|
|
|
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
|
|
|
|
// Can be moved somewhere else once we have IoC enabled body text filters.
|
|
|
|
|
private static string BbcodeReplace(string bodyText) {
|
|
|
|
|
Regex urlRegex = new Regex(@"\[url\]([^\]]+)\[\/url\]");
|
|
|
|
|
Regex urlRegexWithLink = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]");
|
|
|
|
|
Regex imgRegex = new Regex(@"\[img\]([^\]]+)\[\/img\]");
|
|
|
|
|
|
|
|
|
|
bodyText = urlRegex.Replace(bodyText, "<a href=\"$1\">$1</a>");
|
|
|
|
|
bodyText = urlRegexWithLink.Replace(bodyText, "<a href=\"$1\">$2</a>");
|
|
|
|
|
bodyText = imgRegex.Replace(bodyText, "<img src=\"$1\" />");
|
|
|
|
|
|
|
|
|
|
return bodyText;
|
|
|
|
|
}
|
2010-01-06 04:58:50 +08:00
|
|
|
|
}
|
2010-03-04 15:31:42 +08:00
|
|
|
|
}
|