Пример #1
0
        public async Task <IActionResult> List(ListContentsViewModel model, PagerParameters pagerParameters, string ModelName)
        {
            var siteSettings = await _siteService.GetSiteSettingsAsync();

            Pager pager = new Pager(pagerParameters, siteSettings.PageSize);

            model.Id = ModelName;
            var query = _session.Query <ContentItem, ContentItemIndex>();

            switch (model.Options.ContentsStatus)
            {
            case ContentsStatus.Published:
                query = query.With <ContentItemIndex>(x => x.Published);
                break;

            case ContentsStatus.Draft:
                query = query.With <ContentItemIndex>(x => x.Latest && !x.Published);
                break;

            case ContentsStatus.AllVersions:
                query = query.With <ContentItemIndex>(x => x.Latest);
                break;

            default:
                query = query.With <ContentItemIndex>(x => x.Latest);
                break;
            }

            if (!string.IsNullOrEmpty(model.TypeName))
            {
                var contentTypeDefinition = _contentDefinitionManager.GetLastTypeDefinition(model.TypeName);
                if (contentTypeDefinition == null)
                {
                    return(NotFound());
                }

                model.TypeDisplayName = contentTypeDefinition.ToString();

                // We display a specific type even if it's not listable so that admin pages
                // can reuse the Content list page for specific types.
                query = query.With <ContentItemIndex>(x => x.ContentType == model.TypeName);
            }
            else
            {
                var listableTypes = (await GetListableTypesAsync()).Select(t => t.Name).ToArray();
                if (listableTypes.Any())
                {
                    query = query.With <ContentItemIndex>(x => x.ContentType.IsIn(listableTypes));
                }
            }

            switch (model.Options.OrderBy)
            {
            case ContentsOrder.Modified:
                query = query.OrderByDescending(x => x.ModifiedUtc);
                break;

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

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

            default:
                query = query.OrderByDescending(cr => cr.ModifiedUtc);
                break;
            }

            //if (!String.IsNullOrWhiteSpace(model.Options.SelectedCulture))
            //{
            //    query = _cultureFilter.FilterCulture(query, model.Options.SelectedCulture);
            //}

            //if (model.Options.ContentsStatus == ContentsStatus.Owner)
            //{
            //    query = query.Where<CommonPartRecord>(cr => cr.OwnerId == Services.WorkContext.CurrentUser.Id);
            //}

            model.Options.SelectedFilter = model.TypeName;
            model.Options.FilterOptions  = (await GetListableTypesAsync())
                                           .Select(ctd => new KeyValuePair <string, string>(ctd.Name, ctd.DisplayName))
                                           .ToList().OrderBy(kvp => kvp.Value);

            //model.Options.Cultures = _cultureManager.ListCultures();

            // Invoke any service that could alter the query
            await _contentAdminFilters.InvokeAsync(x => x.FilterAsync(query, model, pagerParameters, this), Logger);

            var maxPagedCount = siteSettings.MaxPagedCount;

            if (maxPagedCount > 0 && pager.PageSize > maxPagedCount)
            {
                pager.PageSize = maxPagedCount;
            }

            var pagerShape         = (await New.Pager(pager)).TotalItemCount(maxPagedCount > 0 ? maxPagedCount : await query.CountAsync());
            var pageOfContentItems = await query.Skip(pager.GetStartIndex()).Take(pager.PageSize).ListAsync();

            var contentItemSummaries = new List <dynamic>();

            foreach (var contentItem in pageOfContentItems)
            {
                contentItemSummaries.Add(await _contentItemDisplayManager.BuildDisplayAsync(contentItem, this, "SummaryAdmin"));
            }

            var viewModel = (await New.ViewModel())
                            .ContentItems(contentItemSummaries)
                            .Pager(pagerShape)
                            .Options(model.Options)
                            .TypeDisplayName(model.TypeDisplayName ?? "")
                            .TypeName(model.TypeName ?? "");

            return(View(viewModel));
        }