/// <summary>
/// Binds the grid.
/// </summary>
protected void BindGrid()
{
AddScheduleColumns();
var rockContext = new RockContext();
var groupLocationService = new GroupLocationService( rockContext );
var groupTypeService = new GroupTypeService( rockContext );
var groupService = new GroupService( rockContext );
IEnumerable<GroupTypePath> groupPaths = new List<GroupTypePath>();
var groupLocationQry = groupLocationService.Queryable();
List<int> currentAndDescendantGroupTypeIds = new List<int>();
var currentGroupTypeIds = this.CurrentGroupTypeIds.ToList();
currentAndDescendantGroupTypeIds.AddRange( currentGroupTypeIds );
foreach ( var templateGroupType in groupTypeService.Queryable().Where( a => currentGroupTypeIds.Contains( a.Id ) ) )
{
foreach ( var childGroupType in groupTypeService.GetChildGroupTypes( templateGroupType.Id ) )
{
currentAndDescendantGroupTypeIds.Add( childGroupType.Id );
currentAndDescendantGroupTypeIds.AddRange( groupTypeService.GetAllAssociatedDescendents( childGroupType.Id ).Select( a => a.Id ).ToList() );
}
}
groupLocationQry = groupLocationQry.Where( a => currentAndDescendantGroupTypeIds.Contains( a.Group.GroupTypeId ) );
groupLocationQry = groupLocationQry.OrderBy( a => a.Group.Name ).ThenBy( a => a.Location.Name );
List<int> currentDeviceLocationIdList = this.GetGroupTypesLocations( rockContext ).Select( a => a.Id ).Distinct().ToList();
var qryList = groupLocationQry
.Where( a => currentDeviceLocationIdList.Contains( a.LocationId ) )
.Select( a =>
new
{
GroupLocationId = a.Id,
a.Location,
GroupId = a.GroupId,
GroupName = a.Group.Name,
ScheduleIdList = a.Schedules.Select( s => s.Id ),
GroupTypeId = a.Group.GroupTypeId
} ).ToList();
var locationService = new LocationService( rockContext );
// put stuff in a datatable so we can dynamically have columns for each Schedule
DataTable dataTable = new DataTable();
dataTable.Columns.Add( "GroupLocationId" );
dataTable.Columns.Add( "GroupId" );
dataTable.Columns.Add( "GroupName" );
dataTable.Columns.Add( "GroupPath" );
dataTable.Columns.Add( "LocationName" );
dataTable.Columns.Add( "LocationPath" );
foreach ( var field in gGroupLocationSchedule.Columns.OfType<CheckBoxEditableField>() )
{
dataTable.Columns.Add( field.DataField, typeof( bool ) );
}
var locationPaths = new Dictionary<int, string>();
foreach ( var row in qryList )
{
DataRow dataRow = dataTable.NewRow();
dataRow["GroupLocationId"] = row.GroupLocationId;
dataRow["GroupName"] = groupService.GroupAncestorPathName( row.GroupId );
dataRow["GroupPath"] = groupPaths.Where( gt => gt.GroupTypeId == row.GroupTypeId ).Select( gt => gt.Path ).FirstOrDefault();
dataRow["LocationName"] = row.Location.Name;
if ( row.Location.ParentLocationId.HasValue )
{
int locationId = row.Location.ParentLocationId.Value;
if ( !locationPaths.ContainsKey( locationId ) )
{
var locationNames = new List<string>();
var parentLocation = locationService.Get( locationId );
while ( parentLocation != null )
{
locationNames.Add( parentLocation.Name );
parentLocation = parentLocation.ParentLocation;
}
if ( locationNames.Any() )
{
locationNames.Reverse();
locationPaths.Add( locationId, locationNames.AsDelimited( " > " ) );
}
else
{
locationPaths.Add( locationId, string.Empty );
}
}
dataRow["LocationPath"] = locationPaths[locationId];
}
foreach ( var field in gGroupLocationSchedule.Columns.OfType<CheckBoxEditableField>() )
{
int scheduleId = int.Parse( field.DataField.Replace( "scheduleField_", string.Empty ) );
dataRow[field.DataField] = row.ScheduleIdList.Any( a => a == scheduleId );
}
dataTable.Rows.Add( dataRow );
}
gGroupLocationSchedule.EntityTypeId = EntityTypeCache.Read<GroupLocation>().Id;
gGroupLocationSchedule.DataSource = dataTable;
gGroupLocationSchedule.DataBind();
}