private void SetRecipients( Panel pnl, HtmlAnchor htmlAnchor, Literal literalControl, Grid grid, IQueryable<CommunicationRecipient> qryRecipients ) { pnl.CssClass = pnlOpened.Visible ? "col-md-2-10 margin-b-md" : "col-md-3 margin-b-md"; int count = qryRecipients.Count(); if ( count <= 0 ) { htmlAnchor.Attributes["disabled"] = "disabled"; } else { htmlAnchor.Attributes.Remove( "disabled" ); } literalControl.Text = count.ToString( "N0" ); var sortProperty = grid.SortProperty; if ( sortProperty != null ) { qryRecipients = qryRecipients.AsQueryable().Sort( sortProperty ); } else { qryRecipients = qryRecipients.OrderBy( r => r.PersonAlias.Person.LastName ).ThenBy( r => r.PersonAlias.Person.NickName ); } grid.SetLinqDataSource( qryRecipients ); grid.DataBind(); }
/// <summary> /// Binds the grid. /// </summary> /// <param name="grid">The grid.</param> /// <param name="dataView">The data view.</param> /// <returns></returns> private bool BindGrid( Grid grid, DataView dataView, int? fetchRowCount = null ) { grid.DataSource = null; // Only respect the ShowResults option if fetchRowCount is null if ( !this.ShowResults && fetchRowCount == null ) { return false; } var errorMessages = new List<string>(); if ( dataView.EntityTypeId.HasValue ) { var cachedEntityType = EntityTypeCache.Read( dataView.EntityTypeId.Value ); if ( cachedEntityType != null && cachedEntityType.AssemblyName != null ) { Type entityType = cachedEntityType.GetEntityType(); if ( entityType != null ) { try { grid.CreatePreviewColumns( entityType ); var qry = dataView.GetQuery( grid.SortProperty, GetAttributeValue( "DatabaseTimeout" ).AsIntegerOrNull() ?? 180, out errorMessages ); if ( fetchRowCount.HasValue ) { qry = qry.Take( fetchRowCount.Value ); } grid.SetLinqDataSource( qry.AsNoTracking() ); grid.DataBind(); } catch ( Exception ex ) { this.LogException( ex ); Exception exception = ex; while ( exception != null ) { if ( exception is System.Data.SqlClient.SqlException ) { // if there was a SQL Server Timeout, have the warning be a friendly message about that. if ( ( exception as System.Data.SqlClient.SqlException ).Number == -2 ) { nbEditModeMessage.NotificationBoxType = NotificationBoxType.Warning; nbEditModeMessage.Text = "This dataview did not complete in a timely manner. You can try again or adjust the timeout setting of this block."; return false; } else { errorMessages.Add( exception.Message ); exception = exception.InnerException; } } else { errorMessages.Add( exception.Message ); exception = exception.InnerException; } } } } } } var errorBox = ( grid == gPreview) ? nbPreviewError : nbGridError; if ( errorMessages.Any() ) { errorBox.NotificationBoxType = NotificationBoxType.Warning; errorBox.Text = "WARNING: There was a problem with one or more of the filters for this data view...<br/><br/> " + errorMessages.AsDelimited( "<br/>" ); errorBox.Visible = true; } else { errorBox.Visible = false; } if ( dataView.EntityTypeId.HasValue ) { grid.RowItemText = EntityTypeCache.Read( dataView.EntityTypeId.Value ).FriendlyName; } if ( grid.DataSource != null ) { grid.ExportFilename = dataView.Name; return true; } return false; }
/// <summary> /// Shows the preview. /// </summary> /// <param name="report">The report.</param> /// <param name="gReport">The g report.</param> /// <param name="currentPerson">The current person.</param> /// <param name="databaseTimeoutSeconds">The database timeout seconds.</param> /// <param name="errorMessage">The error message.</param> public static void BindGrid( Report report, Grid gReport, Person currentPerson, int? databaseTimeoutSeconds, out string errorMessage ) { errorMessage = null; if ( report != null ) { var errors = new List<string>(); if ( !report.EntityTypeId.HasValue ) { gReport.Visible = false; return; } var rockContext = new RockContext(); if ( !report.IsAuthorized( Authorization.VIEW, currentPerson ) ) { gReport.Visible = false; return; } Type entityType = EntityTypeCache.Read( report.EntityTypeId.Value, rockContext ).GetEntityType(); if ( entityType == null ) { errorMessage = string.Format( "Unable to determine entityType for {0}", report.EntityType ); return; } gReport.EntityTypeId = report.EntityTypeId; bool isPersonDataSet = report.EntityTypeId == EntityTypeCache.Read( typeof( Rock.Model.Person ), true, rockContext ).Id; if ( isPersonDataSet ) { gReport.PersonIdField = "Id"; gReport.DataKeyNames = new string[] { "Id" }; } else { gReport.PersonIdField = null; } if ( report.EntityTypeId.HasValue ) { gReport.RowItemText = EntityTypeCache.Read( report.EntityTypeId.Value, rockContext ).FriendlyName; } List<EntityField> entityFields = Rock.Reporting.EntityHelper.GetEntityFields( entityType, true, false ); var selectedEntityFields = new Dictionary<int, EntityField>(); var selectedAttributes = new Dictionary<int, AttributeCache>(); var selectedComponents = new Dictionary<int, ReportField>(); // if there is a selectField, keep it to preserve which items are checked var selectField = gReport.Columns.OfType<SelectField>().FirstOrDefault(); gReport.Columns.Clear(); int columnIndex = 0; if ( !string.IsNullOrWhiteSpace( gReport.PersonIdField ) ) { // if we already had a selectField, use it (to preserve checkbox state) gReport.Columns.Add( selectField ?? new SelectField() ); columnIndex++; } var reportFieldSortExpressions = new Dictionary<Guid, string>(); foreach ( var reportField in report.ReportFields.OrderBy( a => a.ColumnOrder ) ) { columnIndex++; if ( reportField.ReportFieldType == ReportFieldType.Property ) { var entityField = entityFields.FirstOrDefault( a => a.Name == reportField.Selection ); if ( entityField != null ) { selectedEntityFields.Add( columnIndex, entityField ); BoundField boundField = entityField.GetBoundFieldType(); boundField.DataField = string.Format( "Entity_{0}_{1}", entityField.Name, columnIndex ); boundField.HeaderText = string.IsNullOrWhiteSpace( reportField.ColumnHeaderText ) ? entityField.Title : reportField.ColumnHeaderText; boundField.SortExpression = boundField.DataField; reportFieldSortExpressions.AddOrReplace( reportField.Guid, boundField.SortExpression ); boundField.Visible = reportField.ShowInGrid; gReport.Columns.Add( boundField ); } } else if ( reportField.ReportFieldType == ReportFieldType.Attribute ) { Guid? attributeGuid = reportField.Selection.AsGuidOrNull(); if ( attributeGuid.HasValue ) { var attribute = AttributeCache.Read( attributeGuid.Value, rockContext ); if ( attribute != null ) { selectedAttributes.Add( columnIndex, attribute ); BoundField boundField; if ( attribute.FieldType.Guid.Equals( Rock.SystemGuid.FieldType.BOOLEAN.AsGuid() ) ) { boundField = new BoolField(); } else if ( attribute.FieldType.Guid.Equals( Rock.SystemGuid.FieldType.DEFINED_VALUE.AsGuid() ) ) { boundField = new DefinedValueField(); } else { boundField = new CallbackField(); boundField.HtmlEncode = false; ( boundField as CallbackField ).OnFormatDataValue += (sender, e) => { string resultHtml = null; if (e.DataValue != null) { bool condensed = true; resultHtml = attribute.FieldType.Field.FormatValueAsHtml( gReport, e.DataValue.ToString(), attribute.QualifierValues, condensed ); } e.FormattedValue = resultHtml ?? string.Empty; }; } boundField.DataField = string.Format( "Attribute_{0}_{1}", attribute.Id, columnIndex ); boundField.HeaderText = string.IsNullOrWhiteSpace( reportField.ColumnHeaderText ) ? attribute.Name : reportField.ColumnHeaderText; boundField.SortExpression = boundField.DataField; reportFieldSortExpressions.AddOrReplace( reportField.Guid, boundField.SortExpression ); if ( attribute.FieldType.Guid.Equals( Rock.SystemGuid.FieldType.INTEGER.AsGuid() ) || attribute.FieldType.Guid.Equals( Rock.SystemGuid.FieldType.DATE.AsGuid() ) || attribute.FieldType.Guid.Equals( Rock.SystemGuid.FieldType.FILTER_DATE.AsGuid() ) ) { boundField.HeaderStyle.HorizontalAlign = HorizontalAlign.Right; boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Right; } boundField.Visible = reportField.ShowInGrid; // NOTE: Additional formatting for attributes is done in the gReport_RowDataBound event gReport.Columns.Add( boundField ); } } } else if ( reportField.ReportFieldType == ReportFieldType.DataSelectComponent ) { selectedComponents.Add( columnIndex, reportField ); DataSelectComponent selectComponent = DataSelectContainer.GetComponent( reportField.DataSelectComponentEntityType.Name ); if ( selectComponent != null ) { DataControlField columnField = selectComponent.GetGridField( entityType, reportField.Selection ); if ( columnField is BoundField ) { ( columnField as BoundField ).DataField = string.Format( "Data_{0}_{1}", selectComponent.ColumnPropertyName, columnIndex ); var customSortExpression = selectComponent.SortProperties( reportField.Selection ); if ( customSortExpression != null ) { if ( customSortExpression == string.Empty ) { // disable sorting if customSortExpression set to string.empty columnField.SortExpression = string.Empty; } else { columnField.SortExpression = customSortExpression.Split( ',' ).Select( a => string.Format( "Sort_{0}_{1}", a, columnIndex ) ).ToList().AsDelimited( "," ); } } else { // use default sorting if customSortExpression was null columnField.SortExpression = ( columnField as BoundField ).DataField; } } columnField.HeaderText = string.IsNullOrWhiteSpace( reportField.ColumnHeaderText ) ? selectComponent.ColumnHeaderText : reportField.ColumnHeaderText; if ( !string.IsNullOrEmpty(columnField.SortExpression) ) { reportFieldSortExpressions.AddOrReplace( reportField.Guid, columnField.SortExpression ); } columnField.Visible = reportField.ShowInGrid; gReport.Columns.Add( columnField ); } } } // if no fields are specified, show the default fields (Previewable/All) for the EntityType var dataColumns = gReport.Columns.OfType<object>().Where( a => a.GetType() != typeof( SelectField ) ); if ( dataColumns.Count() == 0 ) { // show either the Previewable Columns or all (if there are no previewable columns) bool showAllColumns = !entityFields.Any( a => a.FieldKind == FieldKind.Property && a.IsPreviewable ); foreach ( var entityField in entityFields.Where( a => a.FieldKind == FieldKind.Property ) ) { columnIndex++; selectedEntityFields.Add( columnIndex, entityField ); BoundField boundField = entityField.GetBoundFieldType(); boundField.DataField = string.Format( "Entity_{0}_{1}", entityField.Name, columnIndex ); boundField.HeaderText = entityField.Name; boundField.SortExpression = boundField.DataField; boundField.Visible = showAllColumns || entityField.IsPreviewable; gReport.Columns.Add( boundField ); } } try { gReport.Visible = true; gReport.ExportFilename = report.Name; SortProperty sortProperty = gReport.SortProperty; if ( sortProperty == null ) { var reportSort = new SortProperty(); var sortColumns = new Dictionary<string, SortDirection>(); foreach ( var reportField in report.ReportFields.Where( a => a.SortOrder.HasValue ).OrderBy( a => a.SortOrder.Value ) ) { if ( reportFieldSortExpressions.ContainsKey( reportField.Guid ) ) { var sortField = reportFieldSortExpressions[reportField.Guid]; if ( !string.IsNullOrWhiteSpace( sortField ) ) { sortColumns.Add( sortField, reportField.SortDirection ); } } } if ( sortColumns.Any() ) { reportSort.Property = sortColumns.Select( a => a.Key + ( a.Value == SortDirection.Descending ? " desc" : string.Empty ) ).ToList().AsDelimited( "," ); sortProperty = reportSort; } } dynamic qry = report.GetQueryable( entityType, selectedEntityFields, selectedAttributes, selectedComponents, sortProperty, databaseTimeoutSeconds ?? 180, out errors ); gReport.SetLinqDataSource( qry ); gReport.DataBind(); } catch ( Exception ex ) { Exception exception = ex; ExceptionLogService.LogException( ex, HttpContext.Current ); while ( exception != null ) { if ( exception is System.Data.SqlClient.SqlException ) { // if there was a SQL Server Timeout, have the warning be a friendly message about that. if ( ( exception as System.Data.SqlClient.SqlException ).Number == -2 ) { errorMessage = "This report did not complete in a timely manner. You can try again or adjust the timeout setting of this block."; return; } else { errors.Add( exception.Message ); exception = exception.InnerException; } } else { errors.Add( exception.Message ); exception = exception.InnerException; } } } if ( errors.Any() ) { errorMessage = "WARNING: There was a problem with one or more of the report's data components...<br/><br/> " + errors.AsDelimited( "<br/>" ); } } }