From 0b309677f8962a28a56222e4028cb7517493597b Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 9 Jun 2015 00:04:59 +0300 Subject: [PATCH] #5367: Implemented import/export for the Projection Element. --- .../Drivers/ProjectionElementDriver.cs | 56 +++++++++++++++---- .../Orchard.Layouts/Elements/Projection.cs | 6 ++ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ProjectionElementDriver.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ProjectionElementDriver.cs index f3f01a8f2..ebfb44807 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ProjectionElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ProjectionElementDriver.cs @@ -18,6 +18,7 @@ using Orchard.Projections.Services; using Orchard.Projections.ViewModels; using Orchard.Tokens; using Orchard.UI.Navigation; +using Orchard.Layouts.Helpers; using DescribeContext = Orchard.Forms.Services.DescribeContext; namespace Orchard.Layouts.Drivers { @@ -31,11 +32,11 @@ namespace Orchard.Layouts.Drivers { private readonly IDisplayHelperFactory _displayHelperFactory; public ProjectionElementDriver( - IFormManager formManager, - IProjectionManager projectionManager, - IOrchardServices services, - IRepository layoutRepository, - ITokenizer tokenizer, + IFormManager formManager, + IProjectionManager projectionManager, + IOrchardServices services, + IRepository layoutRepository, + ITokenizer tokenizer, IDisplayHelperFactory displayHelperFactory) : base(formManager) { @@ -56,11 +57,15 @@ namespace Orchard.Layouts.Drivers { protected override void OnDisplaying(Projection element, ElementDisplayContext context) { var queryId = element.QueryId; var layoutId = element.LayoutId; + var query = queryId != null ? _contentManager.Get(queryId.Value) : default(QueryPart); + var emptyContentItemsList = Enumerable.Empty(); - if (queryId == null || layoutId == null) + context.ElementShape.ContentItems = emptyContentItemsList; + context.ElementShape.BuildShapes = (Func>)(displayType => emptyContentItemsList.Select(x => _contentManager.BuildDisplay(x, displayType))); + + if (query == null || layoutId == null) { return; - - var query = _contentManager.Get(queryId.Value); + } // Retrieving paging parameters. var queryString = _services.WorkContext.HttpContext.Request.QueryString; @@ -104,14 +109,14 @@ namespace Orchard.Layouts.Drivers { var contentItems = _projectionManager.GetContentItems(query.Id, pager.GetStartIndex() + element.Skip, pager.PageSize).ToList(); context.ElementShape.ContentItems = contentItems; - context.ElementShape.BuildShapes = (Func>) (displayType => contentItems.Select(x => _contentManager.BuildDisplay(x, displayType))); + context.ElementShape.BuildShapes = (Func>)(displayType => contentItems.Select(x => _contentManager.BuildDisplay(x, displayType))); // TODO: Figure out if we need this for a Projection Element, and if so, how. //// Sanity check so that content items with ProjectionPart can't be added here, or it will result in an infinite loop. //contentItems = contentItems.Where(x => !x.Has()).ToList(); // Applying layout. - var layout = _layoutRepository.Get(layoutId.Value); + var layout = layoutId != null ? _layoutRepository.Get(layoutId.Value) : default(LayoutRecord); var layoutDescriptor = layout == null ? null : _projectionManager.DescribeLayouts().SelectMany(x => x.Descriptors).FirstOrDefault(x => x.Category == layout.Category && x.Type == layout.Type); // Create pager shape. @@ -140,7 +145,7 @@ namespace Orchard.Layouts.Drivers { var layoutComponents = contentItems.Select(contentItem => { var contentItemMetadata = _contentManager.GetItemMetadata(contentItem); - var propertyDescriptors = fieldDescriptors.Select( d => { + var propertyDescriptors = fieldDescriptors.Select(d => { var fieldContext = new PropertyContext { State = FormParametersHelper.ToDynamic(d.Property.State), Tokens = new Dictionary { { "Content", contentItem } } @@ -195,7 +200,7 @@ namespace Orchard.Layouts.Drivers { var list = context.ElementShape.List = _services.New.List(); foreach (var group in groups) { var localResult = layoutDescriptor.Render(renderLayoutContext, group.Components); - + // Add the Context to the shape. localResult.Context(renderLayoutContext); list.Add(_services.New.LayoutGroup(Key: new MvcHtmlString(group.Key), List: localResult)); @@ -281,6 +286,33 @@ namespace Orchard.Layouts.Drivers { }); } + protected override void OnExporting(Projection element, ExportElementContext context) { + var query = element.QueryId != null ? _contentManager.Get(element.QueryId.Value) : default(QueryPart); + var layout = element.LayoutId != null ? _layoutRepository.Get(element.LayoutId.Value) : default(LayoutRecord); + var queryIdentity = query != null ? _contentManager.GetItemMetadata(query).Identity.ToString() : default(string); + var layoutIndex = layout != null ? query.Layouts.IndexOf(layout) : default(int?); + + if (queryIdentity != null && layoutIndex != null) { + context.ExportableData["QueryId"] = queryIdentity; + context.ExportableData["LayoutIndex"] = layoutIndex.Value.ToString(); + } + } + + protected override void OnImporting(Projection element, ImportElementContext context) { + var queryIdentity = context.ExportableData.Get("QueryId"); + var query = queryIdentity != null ? context.Session.GetItemFromSession(queryIdentity) : default(ContentManagement.ContentItem); + + if (query == null) + return; + + var queryPart = query.As(); + var layoutIndex = XmlHelper.Parse(context.ExportableData.Get("LayoutIndex")); + var layout = queryPart.Layouts[layoutIndex]; + + element.QueryId = queryPart.Id; + element.LayoutId = layout.Id; + } + private static string GetLayoutDescription(IEnumerable layouts, LayoutRecord l) { var descriptor = layouts.First(x => l.Category == x.Category && l.Type == x.Type); return String.IsNullOrWhiteSpace(l.Description) ? descriptor.Display(new LayoutContext { State = FormParametersHelper.ToDynamic(l.State) }).Text : l.Description; diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Elements/Projection.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Elements/Projection.cs index 704855d84..052671303 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Elements/Projection.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Elements/Projection.cs @@ -19,12 +19,18 @@ namespace Orchard.Layouts.Elements { get { return String.IsNullOrWhiteSpace(QueryLayoutId) ? null : XmlHelper.Parse(QueryLayoutId.Split(new[] { ';' })[0]); } + set { + QueryLayoutId = String.Format("{0};{1}", value, LayoutId); + } } public int? LayoutId { get { return String.IsNullOrWhiteSpace(QueryLayoutId) ? null : XmlHelper.Parse(QueryLayoutId.Split(new[] { ';' })[1]); } + set { + QueryLayoutId = String.Format("{0};{1}", QueryId, value); + } } public int ItemsToDisplay {