Custom Filters

Below is an example of a custom filter. This inherits from the parent abstract class for all filters, ListFilter.

At the top we see two properties: ordinary properties on the filter will automatically have editor fields generated for them in the the Filter bar.  You can then just work with the property in the rest of the filter class code as set by the user.

The constructor sets the ApplicableType property on ListFilter, which indicates what type of content container the filter can be applied to.  In this case, the filter can be applied to any container type that implements IPublishable.

The name property simply returns the name of the Filter as shown on the UI screen.

MergeOriginal has no functionality in this filter.  This method is run to copy properties from the registered filter object into the filter as sent via model binding from the user.  Usually this is used for attribute-based filter objects, because they are registered automatically with information about the property they were attached to.

The Active property is false when the filter is not going to affect the list of content returned, usually because the user has not entered anything into the filter.

The GetShowText method returns text that will appear in a column in the results list.  Notice that this code checks that the row is actually an item whose container is of the ApplicableType or else it returns an empty string.

The Apply method returns a function which operates on a IQueryable<T> to filter its results.  You can assume that all the items in the IQueryable are already assignable to ApplicableType.  Notice the use of AsFacade so you can work with the IQueryable as if it was an IPublishable.

/// <summary>
/// ListFilter for filtering by properties to do with Publishing
/// </summary>
public class PublishingFilter : ListFilter
{
  /// <summary>
  /// Show items which have never been published
  /// </summary>
  public bool NeverPublished { get; set; }
  /// <summary>
  /// Show items which have been updated since they were last published
  /// </summary>
  public bool RequiringPublishing { get; set; }
 
  public PublishingFilter()
  {
    ApplicableType = typeof(IPublishable);
  }
 
  public override string Name { get { return "Publishing"; } set { base.Name = value; } }
 
  public override void MergeOriginal(ListFilter filt)
  { }
 
  public override bool Active
  {
    get { return NeverPublished || RequiringPublishing; }
  }
 
  public override string GetShowText(Tuple<object, Lynicon.Models.Summary> row)
  {
    if (row.Item1 is IPublishable)
    {
      var iPub = row.Item1 as IPublishable;
      if (iPub.Published < iPub.Updated - TimeSpan.FromMilliseconds(750))
        return "Needs Pub";
    }
    return "";
  }
  
  public override Func<IQueryable<T>, IQueryable<T>> Apply<T>()
  {
    if (NeverPublished)
      return iq => iq.AsFacade<IPublishable>()
        .Where(ipub => ipub.Published == null)
        .AsFacade<T>();
    else
      return iq => iq
        .AsFacade<IPublishable>()
        .Where(ipub => ipub.Published == null || ipub.Published < ipub.Updated)
        .AsFacade<T>();
  }
}