<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services;
using Orchard.Taxonomies.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Common.Models;
using Orchard.Localization;

namespace Orchard.Taxonomies.Drivers {
    public class TermWidgetPartDriver : ContentPartDriver<TermWidgetPart> {
        private readonly IContentManager _contentManager;
        private readonly ITaxonomyService _taxonomyService;
        private readonly IContentDefinitionManager _contentDefinitionManager;

        public TermWidgetPartDriver(
            IContentManager contentManager, 
            ITaxonomyService taxonomyService,
            IContentDefinitionManager contentDefinitionManager) {
            _contentManager = contentManager;
            _taxonomyService = taxonomyService;
            _contentDefinitionManager = contentDefinitionManager;

            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override DriverResult Display(TermWidgetPart part, string displayType, dynamic shapeHelper) {
            return ContentShape("Parts_TermWidget_List",
                () => {
                    var termPart = _taxonomyService.GetTerm(part.TermPartRecord.Id);
                    var query = _taxonomyService.GetContentItemsQuery(termPart, part.FieldName);

                    Expression<Func<CommonPartRecord, DateTime?>> orderBy = d => d.CreatedUtc;
                    switch(part.OrderBy) {
                        case "Created": orderBy = d => d.CreatedUtc; break;
                        case "Published": orderBy = d => d.PublishedUtc; break;
                        case "Modified": orderBy = d => d.ModifiedUtc; break;
                    }

                    var results = query.Join<CommonPartRecord>().OrderByDescending(orderBy);

                    if(!String.IsNullOrWhiteSpace(part.ContentType)) {
                        results = results.Where(x => x.ContentItemRecord.ContentType.Name == part.ContentType);
                    }

                    // build the Summary display for each content item
                    var list = shapeHelper.List();
                    list.AddRange(
                        results
                            .Slice(0, part.Count)
                            .ToList()
                            .Select( tp => _contentManager.BuildDisplay(tp.ContentItem, "Summary"))
                    );

                    return shapeHelper.Parts_TermWidget_List(ContentPart: part, ContentItems: list);
                });
        }

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

        protected override DriverResult Editor(TermWidgetPart part, IUpdateModel updater, dynamic shapeHelper) {
            var viewModel = new TermWidgetViewModel {
                Part = part,
                ContentTypeNames = GetContentTypes(),
                Count = part.Count,
                FieldName = part.FieldName,<commit_msg>#19797: Selecting a specific content type doesn't show anything in frontend en resulted in exception.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services;
using Orchard.Taxonomies.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Common.Models;
using Orchard.Localization;

namespace Orchard.Taxonomies.Drivers {
    public class TermWidgetPartDriver : ContentPartDriver<TermWidgetPart> {
        private readonly IContentManager _contentManager;
        private readonly ITaxonomyService _taxonomyService;
        private readonly IContentDefinitionManager _contentDefinitionManager;

        public TermWidgetPartDriver(
            IContentManager contentManager, 
            ITaxonomyService taxonomyService,
            IContentDefinitionManager contentDefinitionManager) {
            _contentManager = contentManager;
            _taxonomyService = taxonomyService;
            _contentDefinitionManager = contentDefinitionManager;

            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override DriverResult Display(TermWidgetPart part, string displayType, dynamic shapeHelper) {
            return ContentShape("Parts_TermWidget_List",
                () => {
                    var termPart = _taxonomyService.GetTerm(part.TermPartRecord.Id);
                    var query = _taxonomyService.GetContentItemsQuery(termPart, part.FieldName);

                    Expression<Func<CommonPartRecord, DateTime?>> orderBy = d => d.CreatedUtc;
                    switch(part.OrderBy) {
                        case "Created": orderBy = d => d.CreatedUtc; break;
                        case "Published": orderBy = d => d.PublishedUtc; break;
                        case "Modified": orderBy = d => d.ModifiedUtc; break;
                    }

                    var results = query.Join<CommonPartRecord>().OrderByDescending(orderBy);

                    if(!String.IsNullOrWhiteSpace(part.ContentType)) {
                        results = results.ForType(part.ContentType).Join<CommonPartRecord>();
                    }

                    // build the Summary display for each content item
                    var list = shapeHelper.List();
                    list.AddRange(
                        results
                            .Slice(0, part.Count)
                            .ToList()
                            .Select( tp => _contentManager.BuildDisplay(tp.ContentItem, "Summary"))
                    );

                    return shapeHelper.Parts_TermWidget_List(ContentPart: part, ContentItems: list);
                });
        }

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

        protected override DriverResult Editor(TermWidgetPart part, IUpdateModel updater, dynamic shapeHelper) {
            var viewModel = new TermWidgetViewModel {
                Part = part,
                ContentTypeNames = GetContentTypes(),
                Count = part.Count,
                FieldName = part.FieldName,
{'fraction_non_alphanumeric': 0.0733015494636472, 'fraction_numerical': 0.0010429082240762813, 'mean_word_length': 2.1369158878504675, 'pattern_counts': {'":': 6, '<': 12, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2017624', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1532, 'n_words': 359}