Skip to content

Commit

Permalink
- Updated content item list to include any 'show on grid' attributes …
Browse files Browse the repository at this point in the history
…available filters
  • Loading branch information
azturner committed Aug 27, 2015
1 parent f8e0e2f commit 1e1b656
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Rock/Workflow/Action/TransferConnectionRequest.cs
Expand Up @@ -39,7 +39,7 @@ namespace Rock.Workflow.Action
new string[] { "Rock.Field.Types.ConnectionRequestFieldType" } )]
[WorkflowAttribute( "Connection Opportunity Attribute", "The attribute that contains the type of the new connection opportunity.", true, "", "", 1, null,
new string[] { "Rock.Field.Types.ConnectionOpportunityFieldType" } )]
[WorkflowTextOrAttribute("Tranfer Note", "Tranfer Note Attribute", "The note to include with the tranfer activity.", false, "", "", 2 )]
[WorkflowTextOrAttribute("Transfer Note", "Tranfer Note Attribute", "The note to include with the transfer activity.", false, "", "", 2 )]
public class TransferConnectionRequest : ActionComponent
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions RockWeb/Blocks/Cms/ContentChannelItemView.ascx
Expand Up @@ -46,6 +46,7 @@
<Rock:RockDropDownList ID="ddlStatus" runat="server" Label="Status" />
<Rock:DateRangePicker ID="drpDateRange" runat="server" Label="Date Range" />
<Rock:RockTextBox ID="tbTitle" runat="server" Label="Title" />
<asp:PlaceHolder ID="phAttributeFilters" runat="server" />
</Rock:GridFilter>

<Rock:ModalAlert ID="mdGridWarning" runat="server" />
Expand Down
198 changes: 156 additions & 42 deletions RockWeb/Blocks/Cms/ContentChannelItemView.ascx.cs
Expand Up @@ -54,6 +54,7 @@ public partial class ContentChannelItemView : Rock.Web.UI.RockBlock
#region Properties

protected int? SelectedChannelId { get; set; }
public List<AttributeCache> AvailableAttributes { get; set; }

#endregion

Expand All @@ -68,6 +69,13 @@ protected override void LoadViewState( object savedState )
base.LoadViewState( savedState );

SelectedChannelId = ViewState["SelectedChannelId"] as int?;

if ( SelectedChannelId.HasValue )
{
var channel = new ContentChannelService( new RockContext() ).Get( SelectedChannelId.Value );
BindAttributes( channel );
AddDynamicControls( channel );
}
}

protected override void OnInit( EventArgs e )
Expand Down Expand Up @@ -174,34 +182,47 @@ protected void rptChannels_ItemCommand( object source, RepeaterCommandEventArgs
/// <param name="e">The e.</param>
void gfFilter_DisplayFilterValue( object sender, GridFilter.DisplayFilterValueArgs e )
{
switch ( e.Key )
if ( AvailableAttributes != null && SelectedChannelId.HasValue )
{
case "Date Range":
var attribute = AvailableAttributes.FirstOrDefault( a => MakeKeyUniqueToChannel( SelectedChannelId.Value, a.Key ) == e.Key );
if ( attribute != null )
{
try
{
e.Value = DateRangePicker.FormatDelimitedValues( e.Value );
break;
var values = JsonConvert.DeserializeObject<List<string>>( e.Value );
e.Value = attribute.FieldType.Field.FormatFilterValues( attribute.QualifierValues, values );
return;
}
case "Status":
catch
{
var status = e.Value.ConvertToEnumOrNull<ContentChannelItemStatus>();
if (status.HasValue)
{
{
e.Value = status.ConvertToString();
}
}
break;
// intentionally ignore
}
case "Title":
{
break;
}
default:
}
}

if ( e.Key == "Date Range" )
{
e.Value = DateRangePicker.FormatDelimitedValues( e.Value );
}
else if ( e.Key == "Status" )
{
var status = e.Value.ConvertToEnumOrNull<ContentChannelItemStatus>();
if ( status.HasValue )
{
{
e.Value = string.Empty;
break;
e.Value = status.ConvertToString();
}
}
}
else if ( e.Key == "Title" )
{
return;
}
else
{
e.Value = string.Empty;
}

}

/// <summary>
Expand All @@ -215,6 +236,26 @@ void gfFilter_ApplyFilterClick( object sender, EventArgs e )
gfFilter.SaveUserPreference( "Status", ddlStatus.SelectedValue );
gfFilter.SaveUserPreference( "Title", tbTitle.Text );

if ( SelectedChannelId.HasValue && AvailableAttributes != null )
{
foreach ( var attribute in AvailableAttributes )
{
var filterControl = phAttributeFilters.FindControl( "filter_" + attribute.Id.ToString() );
if ( filterControl != null )
{
try
{
var values = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter );
gfFilter.SaveUserPreference( MakeKeyUniqueToChannel( SelectedChannelId.Value, attribute.Key ), attribute.Name, attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter ).ToJson() );
}
catch
{
// intentionally ignore
}
}
}
}

GetData();
}

Expand Down Expand Up @@ -370,8 +411,9 @@ private void GetData()
{
// show the content item panel
divItemPanel.Visible = true;

AddColumns( selectedChannel );

BindAttributes( selectedChannel );
AddDynamicControls( selectedChannel );

var itemQry = itemService.Queryable()
.Where( i => i.ContentChannelId == selectedChannel.Id );
Expand Down Expand Up @@ -407,6 +449,33 @@ private void GetData()
itemQry = itemQry.Where( i => i.Title.Contains( title ) );
}

// Filter query by any configured attribute filters
if ( AvailableAttributes != null && AvailableAttributes.Any() )
{
var attributeValueService = new AttributeValueService( rockContext );
var parameterExpression = attributeValueService.ParameterExpression;

foreach ( var attribute in AvailableAttributes )
{
var filterControl = phAttributeFilters.FindControl( "filter_" + attribute.Id.ToString() );
if ( filterControl != null )
{
var filterValues = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter );
var expression = attribute.FieldType.Field.AttributeFilterExpression( attribute.QualifierValues, filterValues, parameterExpression );
if ( expression != null )
{
var attributeValues = attributeValueService
.Queryable()
.Where( v => v.Attribute.Id == attribute.Id );

attributeValues = attributeValues.Where( parameterExpression, expression, null );

itemQry = itemQry.Where( w => attributeValues.Select( v => v.EntityId ).Contains( w.Id ) );
}
}
}
}

var items = new List<ContentChannelItem>();
foreach ( var item in itemQry.ToList() )
{
Expand Down Expand Up @@ -448,13 +517,36 @@ private void GetData()
{
divItemPanel.Visible = false;
}
}

protected void BindAttributes( ContentChannel channel )
{
AvailableAttributes = new List<AttributeCache>();
int entityTypeId = EntityTypeCache.Read( typeof( Rock.Model.ContentChannelItem ) ).Id;
string channelId = channel.Id.ToString();
string channelTypeId = channel.ContentChannelTypeId.ToString();
foreach ( var attributeModel in new AttributeService( new RockContext() ).Queryable()
.Where( a =>
a.EntityTypeId == entityTypeId &&
a.IsGridColumn && ( (
a.EntityTypeQualifierColumn.Equals( "ContentChannelTypeId", StringComparison.OrdinalIgnoreCase ) &&
a.EntityTypeQualifierValue.Equals( channelTypeId )
) || (
a.EntityTypeQualifierColumn.Equals( "ContentChannelId", StringComparison.OrdinalIgnoreCase ) &&
a.EntityTypeQualifierValue.Equals( channelId )
) ) )
.OrderBy( a => a.Order )
.ThenBy( a => a.Name ) )
{
AvailableAttributes.Add( Rock.Web.Cache.AttributeCache.Read( attributeModel ) );
}
}

protected void AddColumns( ContentChannel channel)
protected void AddDynamicControls ( ContentChannel channel)
{
// Remove all columns
gContentChannelItems.Columns.Clear();
phAttributeFilters.Controls.Clear();

if ( channel != null )
{
Expand All @@ -469,19 +561,42 @@ protected void AddColumns( ContentChannel channel)
int entityTypeId = EntityTypeCache.Read( typeof( Rock.Model.ContentChannelItem ) ).Id;
string channelId = channel.Id.ToString();
string channelTypeId = channel.ContentChannelTypeId.ToString();
foreach ( var attribute in new AttributeService( new RockContext() ).Queryable()
.Where( a =>
a.EntityTypeId == entityTypeId &&
a.IsGridColumn && ( (
a.EntityTypeQualifierColumn.Equals( "ContentChannelTypeId", StringComparison.OrdinalIgnoreCase ) &&
a.EntityTypeQualifierValue.Equals( channelTypeId )
) || (
a.EntityTypeQualifierColumn.Equals( "ContentChannelId", StringComparison.OrdinalIgnoreCase ) &&
a.EntityTypeQualifierValue.Equals( channelId )
) ) )
.OrderBy( a => a.Order )
.ThenBy( a => a.Name ) )
foreach ( var attribute in AvailableAttributes )
{
var control = attribute.FieldType.Field.FilterControl( attribute.QualifierValues, "filter_" + attribute.Id.ToString(), false, Rock.Reporting.FilterMode.SimpleFilter );
if ( control != null )
{
if ( control is IRockControl )
{
var rockControl = (IRockControl)control;
rockControl.Label = attribute.Name;
rockControl.Help = attribute.Description;
phAttributeFilters.Controls.Add( control );
}
else
{
var wrapper = new RockControlWrapper();
wrapper.ID = control.ID + "_wrapper";
wrapper.Label = attribute.Name;
wrapper.Controls.Add( control );
phAttributeFilters.Controls.Add( wrapper );
}

string savedValue = gfFilter.GetUserPreference( MakeKeyUniqueToChannel( channel.Id, attribute.Key ) );
if ( !string.IsNullOrWhiteSpace( savedValue ) )
{
try
{
var values = JsonConvert.DeserializeObject<List<string>>( savedValue );
attribute.FieldType.Field.SetFilterValues( control, attribute.QualifierValues, values );
}
catch
{
// intentionally ignore
}
}
}

string dataFieldExpression = attribute.Key;
bool columnExists = gContentChannelItems.Columns.OfType<AttributeField>().FirstOrDefault( a => a.DataField.Equals( dataFieldExpression ) ) != null;
if ( !columnExists )
Expand All @@ -490,13 +605,7 @@ protected void AddColumns( ContentChannel channel)
boundField.DataField = dataFieldExpression;
boundField.HeaderText = attribute.Name;
boundField.SortExpression = string.Empty;

var attributeCache = Rock.Web.Cache.AttributeCache.Read( attribute.Id );
if ( attributeCache != null )
{
boundField.ItemStyle.HorizontalAlign = attributeCache.FieldType.Field.AlignValue;
}

boundField.ItemStyle.HorizontalAlign = attribute.FieldType.Field.AlignValue;
gContentChannelItems.Columns.Add( boundField );
}
}
Expand Down Expand Up @@ -559,6 +668,11 @@ protected void AddColumns( ContentChannel channel)

}

private string MakeKeyUniqueToChannel( int channelId, string key )
{
return string.Format( "{0}-{1}", channelId, key );
}

protected string DisplayStatus (ContentChannelItemStatus contentItemStatus)
{
string labelType = "default";
Expand Down

0 comments on commit 1e1b656

Please sign in to comment.