예제 #1
0
    public ActionResult AjaxList(int contentid, PagerParameters pagerParameters)
    {
        Pager pager = new Pager(_orchardServices.WorkContext.CurrentSite, pagerParameters);
        var   ci    = _orchardServices.ContentManager.Get(contentid);

        if (ci == null || ci.As <DynamicProjectionPart>() == null)
        {
            return(null);
        }
        else
        {
            var record      = ci.As <DynamicProjectionPart>().Record;
            var queryString = _orchardServices.WorkContext.HttpContext.Request.QueryString;
            pager.PageSize = ci.As <DynamicProjectionPart>().Record.Items;
            var pageSizeKey = "pageSize" + record.PagerSuffix;
            if (queryString.AllKeys.Contains(pageSizeKey))
            {
                int qsPageSize;

                if (int.TryParse(queryString[pageSizeKey], out qsPageSize))
                {
                    if (record.MaxItems == 0 || qsPageSize <= record.MaxItems)
                    {
                        pager.PageSize = qsPageSize;
                    }
                }
            }

            var contentItems = _projectionManager
                               .GetContentItems(
                record.QueryPartRecord.Id,
                ci.As <DynamicProjectionPart>(),
                pager.GetStartIndex() + record.Skip,
                pager.PageSize)
                               .ToList();
            var counttot = _projectionManager
                           .GetCount(record.QueryPartRecord.Id, ci.As <DynamicProjectionPart>());
            IEnumerable <ContentItem> pageOfContentItems = null;
            var pagerShape = _shapeFactory
                             .Pager(pager).TotalItemCount(counttot);
            var list = _shapeFactory.List();
            pageOfContentItems = contentItems;
            if (pageOfContentItems != null)
            {
                list.AddRange(pageOfContentItems
                              .Select(contentitem => _contentManager.BuildDisplay(contentitem, "SummaryAdmin")));
            }
            var viewModel = _shapeFactory.ViewModel()
                            .ContentItems(list)
                            .Pager(pagerShape)
                            .Part(ci.As <DynamicProjectionPart>());
            return(View(viewModel));
        }
    }
        protected override DriverResult Display(DynamicProjectionPart part, string displayType, dynamic shapeHelper)
        {
            var query = part.Record.QueryPartRecord;

            // retrieving paging parameters
            var queryString = _orchardServices.WorkContext.HttpContext.Request.QueryString;

            var pageKey = String.IsNullOrWhiteSpace(part.Record.PagerSuffix) ? "page" : "page-" + part.Record.PagerSuffix;
            var page    = 0;

            // default page size
            int pageSize = part.Record.Items;

            // don't try to page if not necessary
            if (part.Record.DisplayPager && queryString.AllKeys.Contains(pageKey))
            {
                Int32.TryParse(queryString[pageKey], out page);
            }

            // if 0, then assume "All", limit to 128 by default
            if (pageSize == 128)
            {
                pageSize = Int32.MaxValue;
            }

            // if pageSize is provided on the query string, ensure it is compatible with allowed limits
            var pageSizeKey = "pageSize" + part.Record.PagerSuffix;

            if (queryString.AllKeys.Contains(pageSizeKey))
            {
                int qsPageSize;

                if (Int32.TryParse(queryString[pageSizeKey], out qsPageSize))
                {
                    if (part.Record.MaxItems == 0 || qsPageSize <= part.Record.MaxItems)
                    {
                        pageSize = qsPageSize;
                    }
                }
            }

            var pager = new Pager(_orchardServices.WorkContext.CurrentSite, page, pageSize);

            var pagerShape = shapeHelper.Pager(pager)
                             .ContentPart(part)
                             .PagerId(pageKey);

            return(Combined(
                       ContentShape("Parts_ProjectionPart_Pager", shape => {
                if (!part.Record.DisplayPager)
                {
                    return null;
                }

                return pagerShape;
            }),
                       ContentShape("Parts_ProjectionPart_List", shape => {
                // generates a link to the RSS feed for this term
                var metaData = _orchardServices.ContentManager.GetItemMetadata(part.ContentItem);
                //      _feedManager.Register(metaData.DisplayText, "rss", new RouteValueDictionary { { "projection", part.Id } });

                // execute the query
                var contentItems = _projectionManager.GetContentItems(query.Id, part, pager.GetStartIndex() + part.Record.Skip, pager.PageSize).ToList();

                // 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 <ProjectionPart>()).ToList();

                // applying layout
                var layout = part.Record.LayoutRecord;

                LayoutDescriptor layoutDescriptor = layout == null ? null : _projectionManager.DescribeLayouts().SelectMany(x => x.Descriptors).FirstOrDefault(x => x.Category == layout.Category && x.Type == layout.Type);

                // create pager shape
                if (part.Record.DisplayPager)
                {
                    var contentItemsCount = _projectionManager.GetCount(query.Id, part) - part.Record.Skip;
                    contentItemsCount = Math.Max(0, contentItemsCount);
                    pagerShape.TotalItemCount(contentItemsCount);
                }

                // renders in a standard List shape if no specific layout could be found
                if (layoutDescriptor == null)
                {
                    var list = _orchardServices.New.List();
                    var contentShapes = contentItems.Select(item => _orchardServices.ContentManager.BuildDisplay(item, "Summary"));
                    list.AddRange(contentShapes);

                    return list;
                }

                var allFielDescriptors = _projectionManager.DescribeProperties().ToList();
                var fieldDescriptors = layout.Properties.OrderBy(p => p.Position).Select(p => allFielDescriptors.SelectMany(x => x.Descriptors).Select(d => new { Descriptor = d, Property = p }).FirstOrDefault(x => x.Descriptor.Category == p.Category && x.Descriptor.Type == p.Type)).ToList();

                var layoutComponents = contentItems.Select(
                    contentItem => {
                    var contentItemMetadata = _orchardServices.ContentManager.GetItemMetadata(contentItem);

                    var propertyDescriptors = fieldDescriptors.Select(
                        d => {
                        var fieldContext = new PropertyContext {
                            State = FormParametersHelper.ToDynamic(d.Property.State),
                            Tokens = new Dictionary <string, object> {
                                { "Content", contentItem }
                            }
                        };

                        return new { d.Property, Shape = d.Descriptor.Property(fieldContext, contentItem) };
                    });

                    // apply all settings to the field content, wrapping it in a FieldWrapper shape
                    var properties = _orchardServices.New.Properties(
                        Items: propertyDescriptors.Select(
                            pd => _orchardServices.New.PropertyWrapper(
                                Item: pd.Shape,
                                Property: pd.Property,
                                ContentItem: contentItem,
                                ContentItemMetadata: contentItemMetadata
                                )
                            )
                        );

                    return new LayoutComponentResult {
                        ContentItem = contentItem,
                        ContentItemMetadata = contentItemMetadata,
                        Properties = properties
                    };
                }).ToList();

                var tokenizedState = _tokenizer.Replace(layout.State, new Dictionary <string, object> {
                    { "Content", part.ContentItem }
                });

                var renderLayoutContext = new LayoutContext {
                    State = FormParametersHelper.ToDynamic(tokenizedState),
                    LayoutRecord = layout,
                };

                if (layout.GroupProperty != null)
                {
                    var groupPropertyId = layout.GroupProperty.Id;
                    var display = _displayHelperFactory.CreateHelper(new ViewContext {
                        HttpContext = _workContextAccessor.GetContext().HttpContext
                    }, new ViewDataContainer());

                    // index by PropertyWrapper shape
                    var groups = layoutComponents.GroupBy(
                        x => {
                        var propertyShape = ((IEnumerable <dynamic>)x.Properties.Items).First(p => ((PropertyRecord)p.Property).Id == groupPropertyId);

                        // clear the wrappers, as they shouldn't be needed to generate the grouping key itself
                        // otherwise the DisplayContext.View is null, and throws an exception if a wrapper is rendered (#18558)
                        ((IShape)propertyShape).Metadata.Wrappers.Clear();

                        string key = Convert.ToString(display(propertyShape));
                        return key;
                    }).Select(x => new { Key = x.Key, Components = x });

                    var list = _orchardServices.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(_orchardServices.New.LayoutGroup(Key: new MvcHtmlString(group.Key), List: localResult));
                    }

                    return list;
                }


                var layoutResult = layoutDescriptor.Render(renderLayoutContext, layoutComponents);

                // add the Context to the shape
                layoutResult.Context(renderLayoutContext);

                return layoutResult;
            })));
        }
예제 #3
0
        public ActionResult AjaxList(int contentid, PagerParameters pagerParameters)
        {
            //TODO: protection level is too basic, should work based on permissions for the DynamicProjection
            Pager pager = new Pager(_orchardServices.WorkContext.CurrentSite, pagerParameters);
            var   ci    = _orchardServices.ContentManager.Get(contentid);
            DynamicProjectionPart part = ci.As <DynamicProjectionPart>();

            if (ci == null || part == null)
            {
                return(null);
            }
            else
            {
                var record      = part.Record;
                var queryString = _orchardServices.WorkContext.HttpContext.Request.QueryString;
                pager.PageSize = part.Record.Items;
                var pageSizeKey = "pageSize" + record.PagerSuffix;
                if (queryString.AllKeys.Contains(pageSizeKey))
                {
                    int qsPageSize;

                    if (int.TryParse(queryString[pageSizeKey], out qsPageSize))
                    {
                        if (record.MaxItems == 0 || qsPageSize <= record.MaxItems)
                        {
                            pager.PageSize = qsPageSize;
                        }
                    }
                }
                //TODO: Should works also for queries that are pure Hql and not only for ContentManager queries
                //Options:
                // 1.   Use QueryPickerPart that has the ability to configure HqlQueries
                //      However QueryPickerPart and HqlQueries need some improvements:
                //          a. Use of parameters instead of string substitution
                //          b. QueryPickerPart is not attachable
                //          c. QueryPickerPart does not support pagination by now
                //          d. QueryPickerPart does not support pure Hql queries because under the hood it uses a ContentQuery
                //          e. We should have a way to call a razor view in order to show results of the query
                // 2.   Use DynamicProjectionPart
                //          a. Firing only the Hql filter if exists otherwise it return null
                //          b. this approach is the most easy but also the worst because we have queries that will throw exceptions if used by projectionpart or the ProjectionManager
                //          c. using this approach we could use the laout of the query to customize the result view
                // 3.   Use a text tokenized as query
                //          a. Use of parameters instead of string substitution
                //          b. We should have a way to call a razor view in order to show results of the query

                if (part.ReturnsHqlResults)
                {
                    var queryPicker = ci.As <QueryPickerPart>();
                    if (queryPicker == null || !queryPicker.Ids.Any())
                    {
                        return(null);
                    }

                    //Results and Count
                    var countQuery = _queryPickerService.GetCustomQuery(queryPicker.Ids.First(), new Dictionary <string, object> {
                        { "Content", ci }
                    }, true);
                    var query = _queryPickerService.GetCustomQuery(queryPicker.Ids.First(), new Dictionary <string, object> {
                        { "Content", ci }
                    });
                    var queryresults = _dynamicProjectoinService.GetResults(query,
                                                                            pager.GetStartIndex() + record.Skip,
                                                                            pager.PageSize);
                    var counttot = _dynamicProjectoinService.GetCount(countQuery);

                    //Pager
                    var pagerShape = _shapeFactory
                                     .Pager(pager).TotalItemCount(counttot);

                    //Shape for results
                    var resultsfile = string.Empty;
                    if (!string.IsNullOrEmpty(part.ShapeForResults))
                    {
                        resultsfile = string.Format(
                            "~/App_Data/Sites/{0}/Code/{1}",
                            _shellSettings.Name,
                            ci.As <DynamicProjectionPart>().ShapeForResults);
                    }


                    var viewModel = _shapeFactory.ViewModel()
                                    .ResultShape(resultsfile)
                                    .Items(queryresults)
                                    .Pager(pagerShape)
                                    .Part(ci.As <DynamicProjectionPart>());
                    return(View(viewModel));
                }
                else
                {
                    var contentItems = _projectionManager
                                       .GetContentItems(
                        record.QueryPartRecord.Id,
                        ci.As <DynamicProjectionPart>(),
                        pager.GetStartIndex() + record.Skip,
                        pager.PageSize)
                                       .ToList();
                    var counttot = _projectionManager
                                   .GetCount(record.QueryPartRecord.Id, ci.As <DynamicProjectionPart>());
                    IEnumerable <ContentItem> pageOfContentItems = null;
                    var pagerShape = _shapeFactory
                                     .Pager(pager).TotalItemCount(counttot);
                    var list = _shapeFactory.List();
                    pageOfContentItems = contentItems;
                    if (pageOfContentItems != null)
                    {
                        list.AddRange(pageOfContentItems
                                      .Select(contentitem => _contentManager.BuildDisplay(contentitem, "SummaryAdmin")));
                    }
                    var viewModel = _shapeFactory.ViewModel()
                                    .ContentItems(list)
                                    .Pager(pagerShape)
                                    .Part(ci.As <DynamicProjectionPart>());
                    return(View(viewModel));
                }
            }
        }