/// <summary>
 /// Builds content shapes that contain the necessary prefix
 /// </summary>
 /// <param name="contents">The content objects to use</param>
 /// <param name="shapeFactory">Factory delegate to create the editor shape for the content objects</param>
 /// <returns>The prefixed shapes (multiple such shapes can be used on the same page)</returns>
 public static IEnumerable <dynamic> BuildShapes(this IPrefixedEditorManager prefixedEditorManager, IEnumerable <IContent> contents, Func <IContent, dynamic> shapeFactory)
 {
     return(contents.Select(content => prefixedEditorManager.BuildShape(content, shapeFactory)));;
 }
Example #2
0
        public ActionResult Rewriter(RewriterViewModel rewriterViewModel, PagerParameters pagerParameters)
        {
            // These Authorize() calls are mainly placeholders for future permissions, that's why they're copy-pasted around.
            if (!_authorizer.Authorize(Permissions.ManageSeo, T("You're not allowed to manage SEO settings.")))
            {
                return(new HttpUnauthorizedResult());
            }

            string title;

            switch (rewriterViewModel.RewriterType)
            {
            case "TitleRewriter":
                title = T("SEO Title Tag Rewriter").Text;
                break;

            case "DescriptionRewriter":
                title = T("SEO Description Tag Rewriter").Text;
                break;

            case "KeywordsRewriter":
                title = T("SEO Keywords Tag Rewriter").Text;
                break;

            default:
                return(new HttpNotFoundResult());
            }
            _orchardServices.WorkContext.Layout.Title = title;

            var siteSettings = _siteService.GetSiteSettings();
            var pager        = new Pager(siteSettings, pagerParameters);

            var seoContentTypes = _seoService.ListSeoContentTypes();
            var query           = _contentManager.Query(VersionOptions.Latest, seoContentTypes.Select(type => type.Name).ToArray());

            if (!String.IsNullOrEmpty(rewriterViewModel.Q))
            {
                IPageOfItems <ISearchHit> searchHits = new PageOfItems <ISearchHit>(new ISearchHit[] { });
                try {
                    searchHits = _searchService.Query(rewriterViewModel.Q, pager.Page, pager.PageSize, false,
                                                      siteSettings.As <SearchSettingsPart>().SearchedFields,
                                                      searchHit => searchHit);
                    // Could use this: http://orchard.codeplex.com/workitem/18664
                    // Converting to List, because the expression should contain an ICollection
                    var hitIds = searchHits.Select(hit => hit.ContentItemId).ToList();
                    query.Where <CommonPartRecord>(record => hitIds.Contains(record.Id));
                }
                catch (Exception ex) {
                    if (ex.IsFatal())
                    {
                        throw;
                    }
                    _orchardServices.Notifier.Error(T("Invalid search query: {0}", ex.Message));
                }
            }

            if (!string.IsNullOrEmpty(rewriterViewModel.TypeName))
            {
                var typeDefinition = seoContentTypes.SingleOrDefault(t => t.Name == rewriterViewModel.TypeName);
                if (typeDefinition == null)
                {
                    return(HttpNotFound());
                }

                rewriterViewModel.TypeDisplayName = typeDefinition.DisplayName;
                query = query.ForType(rewriterViewModel.TypeName);
            }

            switch (rewriterViewModel.Options.OrderBy)
            {
            case ContentsOrder.Modified:
                query = query.OrderByDescending <CommonPartRecord>(cr => cr.ModifiedUtc);
                break;

            case ContentsOrder.Published:
                query = query.OrderByDescending <CommonPartRecord>(cr => cr.PublishedUtc);
                break;

            case ContentsOrder.Created:
                query = query.OrderByDescending <CommonPartRecord>(cr => cr.CreatedUtc);
                break;
            }

            rewriterViewModel.Options.SelectedFilter = rewriterViewModel.TypeName;

            var pagerShape         = _shapeFactory.Pager(pager).TotalItemCount(query.Count());
            var pageOfContentItems = query.Slice(pager.GetStartIndex(), pager.PageSize).ToList();

            var list = _shapeFactory.List();

            list.AddRange(
                pageOfContentItems.Select(
                    item => _prefixedEditorManager.BuildShape(item, (content => _contentManager.BuildDisplay(content, "SeoSummaryAdmin-" + rewriterViewModel.RewriterType)))
                    )
                );

            dynamic viewModel = _shapeFactory.ViewModel()
                                .ContentItems(list)
                                .Options(rewriterViewModel.Options)
                                .Pager(pagerShape);

            // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation, despite
            // being it highly unlikely with Onestop, just in case...
            return(View((object)viewModel));
        }