Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'remotes/OrchardCMS.Orchard/1.10.x' into…
Browse files Browse the repository at this point in the history
… Laser/1.10.x
MatteoPiovanelli committed Nov 8, 2016
2 parents 578b21b + c486355 commit b90629d
Showing 9 changed files with 101 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -11,35 +11,45 @@
using Orchard.Templates.Services;
using Orchard.Templates.ViewModels;
using Orchard.Utility.Extensions;
using Orchard.Core.Title.Models;

namespace Orchard.Templates.Drivers {
public class ShapePartDriver : ContentPartDriver<ShapePart> {
private readonly IEnumerable<ITemplateProcessor> _processors;
private readonly ITransactionManager _transactions;
private readonly IContentManager _contentManager;

public ShapePartDriver(
IEnumerable<ITemplateProcessor> processors,
ITransactionManager transactions) {
ITransactionManager transactions,
IContentManager contentManager) {
_processors = processors;
_transactions = transactions;
_contentManager = contentManager;
T = NullLocalizer.Instance;
}

Localizer T { get; set; }

protected override DriverResult Display(ShapePart part, string displayType, dynamic shapeHelper) {
return ContentShape("Parts_Shape_SummaryAdmin", () => shapeHelper.Parts_Shape_SummaryAdmin());
}

protected override DriverResult Editor(ShapePart part, dynamic shapeHelper) {
return Editor(part, null, shapeHelper);
}

protected override DriverResult Editor(ShapePart part, IUpdateModel updater, dynamic shapeHelper) {
var viewModel = new ShapePartViewModel {
Template = part.Template
Template = part.Template,
RenderingMode = part.RenderingMode
};

if (updater != null
&& updater.TryUpdateModel(viewModel, Prefix, null, new[] { "AvailableLanguages" })
&& ValidateShapeName(part, updater)) {
part.Template = viewModel.Template;
part.RenderingMode = viewModel.RenderingMode;

try {
var processor = _processors.FirstOrDefault(x => String.Equals(x.Type, part.ProcessorName, StringComparison.OrdinalIgnoreCase)) ?? _processors.First();
@@ -49,12 +59,32 @@ protected override DriverResult Editor(ShapePart part, IUpdateModel updater, dyn
updater.AddModelError("", T("Template processing error: {0}", ex.Message));
_transactions.Cancel();
}

// We need to query for the content type names because querying for content parts has no effect on the database side.
var contentTypesWithShapePart = _contentManager
.GetContentTypeDefinitions()
.Where(typeDefinition => typeDefinition.Parts.Any(partDefinition => partDefinition.PartDefinition.Name == "ShapePart"))
.Select(typeDefinition => typeDefinition.Name);

// If ShapePart is only dynamically added to this content type or even this content item then we won't find
// a corresponding content type definition, so using the current content type too.
contentTypesWithShapePart = contentTypesWithShapePart.Union(new[] { part.ContentItem.ContentType });

var existingShapes = _contentManager
.Query(VersionOptions.Latest, contentTypesWithShapePart.ToArray())
.Where<TitlePartRecord>(record => record.Title == part.As<TitlePart>().Title && record.ContentItemRecord.Id != part.ContentItem.Id);

if (existingShapes.List().Any(x => x.As<ShapePart>().RenderingMode == part.RenderingMode)) {
updater.AddModelError("ShapeNameAlreadyExists", T("A template with the given name and rendering mode already exists."));
}
}
return ContentShape("Parts_Shape_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts.Shape", Model: viewModel, Prefix: Prefix));
}

protected override void Exporting(ShapePart part, ExportContentContext context) {
context.Element(part.PartDefinition.Name).Add(new XCData(part.Template));
var element = context.Element(part.PartDefinition.Name);
element.Add(new XCData(part.Template));
element.SetAttributeValue("RenderingMode", part.RenderingMode.ToString());
}

protected override void Importing(ShapePart part, ImportContentContext context) {
@@ -65,8 +95,10 @@ protected override void Importing(ShapePart part, ImportContentContext context)

var shapeElement = context.Data.Element(part.PartDefinition.Name);

if (shapeElement != null)
if (shapeElement != null) {
part.Template = shapeElement.Value;
context.ImportAttribute(part.PartDefinition.Name, "RenderingMode", value => part.RenderingMode = (RenderingMode)Enum.Parse(typeof(RenderingMode), value));
}
}

private bool ValidateShapeName(ShapePart part, IUpdateModel updater) {

This file was deleted.

11 changes: 11 additions & 0 deletions src/Orchard.Web/Modules/Orchard.Templates/Models/ShapePart.cs
Original file line number Diff line number Diff line change
@@ -16,5 +16,16 @@ public string Template {
get { return this.Retrieve(x => x.Template); }
set { this.Store(x => x.Template, value); }
}

public RenderingMode RenderingMode {
get { return this.Retrieve(x => x.RenderingMode); }
set { this.Store(x => x.RenderingMode, value); }
}
}

public enum RenderingMode {
FrontEndAndAdmin,
FrontEnd,
Admin
}
}
Original file line number Diff line number Diff line change
@@ -180,7 +180,6 @@
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Drivers\TitlePartDriver.cs" />
<Compile Include="Helpers\StringExtensions.cs" />
<Compile Include="Handlers\ShapePartHandler.cs" />
<Compile Include="Drivers\ShapePartDriver.cs" />
@@ -227,6 +226,9 @@
<ItemGroup>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Parts.Shape.SummaryAdmin.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6 changes: 5 additions & 1 deletion src/Orchard.Web/Modules/Orchard.Templates/Placement.info
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<Placement>
<Place Parts_Shape_Edit="Content:0" />
<Place Parts_Shape_Edit="Content:0" />

<Match DisplayType="SummaryAdmin">
<Place Parts_Shape_SummaryAdmin="Content:5" />
</Match>
</Placement>
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
using System.Web.Mvc;
using Orchard.DisplayManagement;
using Orchard.DisplayManagement.Descriptors;
using System.Web.Routing;
using Orchard.UI.Admin;

namespace Orchard.Templates.Services {
public class TemplateShapeBindingResolver : IShapeBindingResolver {
@@ -18,30 +20,50 @@ public class TemplateShapeBindingResolver : IShapeBindingResolver {
private IContentManager _contentManager;
private IContentDefinitionManager _contentDefinitionManager;
private ITemplateService _templateService;
private readonly RequestContext _requestContext;

public TemplateShapeBindingResolver(
ICacheManager cacheManager,
ISignals signals,
IContentManager contentManager,
IContentDefinitionManager contentDefinitionManager,
ITemplateService templateService
) {
ITemplateService templateService,
RequestContext requestContext) {
_cacheManager = cacheManager;
_signals = signals;
_contentManager = contentManager;
_contentDefinitionManager = contentDefinitionManager;
_templateService = templateService;
_requestContext = requestContext;
}

public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding) {
var processors = BuildShapeProcessors();

var acceptableRenderingModes = new List<RenderingMode>() { RenderingMode.FrontEndAndAdmin };
if (AdminFilter.IsApplied(_requestContext)) {
acceptableRenderingModes.Add(RenderingMode.Admin);
}
else {
acceptableRenderingModes.Add(RenderingMode.FrontEnd);
}

var templateResults = processors[shapeType].Where(template => acceptableRenderingModes.Contains(template.RenderingMode));
TemplateResult templateResult = null;
if (processors.TryGetValue(shapeType, out templateResult)) {
var templateResultsCount = templateResults.Count();
if (templateResultsCount == 1) {
templateResult = templateResults.FirstOrDefault();
}
else if (templateResultsCount > 1) {
// Templates with the same name but specified rendering mode are prioritized.
templateResult = templateResults.FirstOrDefault(template => template.RenderingMode != RenderingMode.FrontEndAndAdmin);
}

if (templateResult != null) {
shapeBinding = new ShapeBinding {
BindingName = "Templates",
Binding = ctx => CoerceHtmlString(_templateService.Execute(
templateResult.Template,
templateResult.Template,
templateResult.Name,
templateResult.Processor, ctx.Value)),
ShapeDescriptor = new ShapeDescriptor { ShapeType = shapeType }
@@ -54,7 +76,7 @@ public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBind
return false;
}

private IDictionary<string, TemplateResult> BuildShapeProcessors() {
private ILookup<string, TemplateResult> BuildShapeProcessors() {
return _cacheManager.Get("Template.ShapeProcessors", true, ctx => {
ctx.Monitor(_signals.When(DefaultTemplateService.TemplatesSignal));

@@ -67,11 +89,12 @@ private IDictionary<string, TemplateResult> BuildShapeProcessors() {

var allTemplates = _contentManager.Query<ShapePart>(typesWithShapePart).List();

return allTemplates.Select(x => new TemplateResult {
Name = x.Name,
Template = x.Template,
Processor = x.ProcessorName
}).ToDictionary(x => x.Name, x => x);
return allTemplates.Select(shapePart => new TemplateResult {
Name = shapePart.Name,
Template = shapePart.Template,
Processor = shapePart.ProcessorName,
RenderingMode = shapePart.RenderingMode
}).ToLookup(template => template.Name);
});
}

@@ -83,6 +106,7 @@ private class TemplateResult {
public string Name { get; set; }
public string Processor { get; set; }
public string Template { get; set; }
public RenderingMode RenderingMode { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace Orchard.Templates.ViewModels {
using Orchard.Templates.Models;

namespace Orchard.Templates.ViewModels {
public class ShapePartViewModel {
public string Template { get; set; }

public RenderingMode RenderingMode { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@model Orchard.Templates.ViewModels.ShapePartViewModel
@using Orchard.Templates.Models
@model Orchard.Templates.ViewModels.ShapePartViewModel
@{
Style.Include("~/modules/orchard.templates/scripts/codemirror/lib/codemirror.css");
Style.Include("template-editor.css");
@@ -15,4 +16,8 @@
<div>
@Html.TextAreaFor(m => m.Template, new { @class = "text large code-editor" })
</div>
<div>
@Html.LabelFor(m => m.RenderingMode, T("Rendering Mode"))
@Html.DropDownListFor(m => m.RenderingMode, Enum.GetValues(typeof(RenderingMode)).Cast<RenderingMode>().Select(renderingMode => new SelectListItem { Text = renderingMode.ToString(), Value = renderingMode.ToString() }))
</div>
</fieldset>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@T("Rendering mode: {0}", Model.ContentPart.RenderingMode)

0 comments on commit b90629d

Please sign in to comment.