episerver / episerver-labs-content-manager-docs

Documentation for EPiServer.Labs.ContentManager
1 stars 0 forks source link

Filter by date, allways use Changed date #28

Closed tomahg closed 3 years ago

tomahg commented 4 years ago

If I choose my displayed date like this

        {
            itemModel.Image = Image;
            itemModel.Description = Description;
            itemModel.ChangeDate = StartPublish;
        }

I would expect the "Filter by date" to use this date. It will not.

gregwiechec commented 3 years ago

Hi,

There is no public API to change the date, but you can try to do this:


using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer.ServiceLocation;
using EPiServer;
using EPiServer.Cms.Shell.UI.Rest.ContentQuery;
using EPiServer.Core;
using EPiServer.Globalization;
using EPiServer.Labs.ContentManager.Core.ChildrenStore;
using EPiServer.Labs.ContentManager.Core.ChildrenStore.Internal;
using EPiServer.Shell.ContentQuery;

namespace Alloy.Sample.Business.Initialization
{
    [ServiceConfiguration(typeof(IContentQuery))]
    public class CustomContentChildrenSearchQuery : ContentChildrenSearchQuery
    {
        public override int Rank => ContentChildrenSearchQuery.QueryRank + 1;

        public CustomContentChildrenSearchQuery() : this(ServiceLocator.Current.GetInstance<IContentRepository>(),
            ServiceLocator.Current.GetInstance<IContentQueryHelper>(),
            ServiceLocator.Current.GetInstance<LanguageSelectorFactory>(),
            ServiceLocator.Current.GetInstance<IContentVersionRepository>(),
            ServiceLocator.Current.GetInstance<LanguageResolver>())
        {
        }

        public CustomContentChildrenSearchQuery(IContentRepository contentRepository, IContentQueryHelper queryHelper,
            LanguageSelectorFactory languageSelectorFactory, IContentVersionRepository contentVersionRepository,
            LanguageResolver languageResolver) : base(contentRepository, queryHelper, languageSelectorFactory,
            contentVersionRepository, languageResolver)
        {
        }

        protected override IEnumerable<IContent> FilterQuery(IEnumerable<IContent> queryResult,
            ContentChildrenSearchQueryParameters parameters, ChildrenQueryFilter childrenQueryFilter)
        {
            // date filter
            if (childrenQueryFilter != null)
            {
                if (childrenQueryFilter.ChangedDateFilter.HasValue)
                {
                    var dateTime = childrenQueryFilter.ChangedDateFilter.Value;
                    queryResult = queryResult.Where(x =>
                        x is IChangeTrackable changeTrackable && changeTrackable.Changed.Month == dateTime.Month &&
                        changeTrackable.Changed.Year == dateTime.Year);
                }
            }

            // status filter
            if (parameters.ContentStatuses != null)
            {
                queryResult = queryResult.Where(s =>
                    s is IVersionable versionable && parameters.ContentStatuses.Contains(versionable.Status)).ToList();
            }

            return queryResult;
        }

        protected override void CalculateFacets(StatusFacet statusFacet, Dictionary<DateTime, int> changedDateFacet,
            IEnumerable<IContent> result)
        {
            // status facet
            var versionableFacets = result.OfType<IVersionable>().ToList();
            foreach (var facetItem in StatusFacet.Statuses)
            {
                var count = versionableFacets.Count(x => facetItem.Value.Contains(x.Status));
                statusFacet.SetCount(facetItem.Key, count);
            }

            // date facet
            foreach (var groupedByMonth in result.GroupBy(x =>
                new {((IChangeTrackable) x).Changed.Date.Year, ((IChangeTrackable) x).Changed.Date.Month}))
            {
                var dateTime = new DateTime(groupedByMonth.Key.Year, groupedByMonth.Key.Month, 1);
                changedDateFacet.Add(dateTime, groupedByMonth.Count());
            }
        }
    }
}