/// <summary> /// Creates the filter control. /// </summary> /// <param name="parentControl">The parent control.</param> /// <param name="filter">The filter.</param> /// <param name="reportEntityType">Type of the report entity.</param> /// <param name="setSelection">if set to <c>true</c> [set selection].</param> /// <param name="rockContext">The rock context.</param> private void CreateFilterControl( Control parentControl, DataViewFilter filter, EntityType reportEntityType, bool setSelection, RockContext rockContext) { try { var filteredEntityTypeName = EntityTypeCache.Get(reportEntityType).Name; if (filter.ExpressionType == FilterExpressionType.Filter) { var filterControl = new FilterField(); bool filterIsVisible = _selectedDataFilterGuids.Contains(filter.Guid); if (filterIsVisible) { // only set FilterMode to simple if the filter is visible since SimpleFilters might have a different filtering behavior filterControl.FilterMode = FilterMode.SimpleFilter; } else { filterControl.FilterMode = FilterMode.AdvancedFilter; } bool filterIsConfigurable = _configurableDataFilterGuids.Contains(filter.Guid); bool showCheckbox = _togglableDataFilterGuids.Contains(filter.Guid) || !filterIsConfigurable; var dataFilterPrePostHtmlConfig = _dataFiltersPrePostHtmlConfig.GetValueOrNull(filter.Guid) ?? new DataFilterPrePostHtmlConfig(); filterControl.Visible = filterIsVisible; parentControl.Controls.Add(filterControl); filterControl.DataViewFilterGuid = filter.Guid; filterControl.HideFilterCriteria = !filterIsConfigurable; filterControl.ID = string.Format("ff_{0}", filterControl.DataViewFilterGuid.ToString("N")); filterControl.FilteredEntityTypeName = filteredEntityTypeName; if (filter.EntityTypeId.HasValue) { var entityTypeCache = EntityTypeCache.Get(filter.EntityTypeId.Value, rockContext); if (entityTypeCache != null) { filterControl.FilterEntityTypeName = entityTypeCache.Name; } } filterControl.Expanded = true; filterControl.ShowCheckbox = filterIsVisible && showCheckbox; var reportEntityTypeCache = EntityTypeCache.Get(reportEntityType); var reportEntityTypeModel = reportEntityTypeCache.GetEntityType(); var filterEntityType = EntityTypeCache.Get(filter.EntityTypeId ?? 0); var component = Rock.Reporting.DataFilterContainer.GetComponent(filterEntityType.Name); if (component != null) { string selectionUserPreference = null; bool? checkedUserPreference = null; if (setSelection && filterIsVisible) { if (filterIsConfigurable) { selectionUserPreference = this.GetUserPreference(string.Format("{0}_{1}_Selection", GetReportDataKeyPrefix(), filterControl.DataViewFilterGuid.ToString("N"))); } if (filterControl.ShowCheckbox) { checkedUserPreference = this.GetUserPreference(string.Format("{0}_{1}_Checked", GetReportDataKeyPrefix(), filterControl.DataViewFilterGuid.ToString("N"))).AsBooleanOrNull() ?? true; } } if (checkedUserPreference.HasValue) { filterControl.SetCheckBoxChecked(checkedUserPreference.Value); } if (setSelection) { var selection = filter.Selection; if (!string.IsNullOrWhiteSpace(selectionUserPreference)) { if (component is Rock.Reporting.DataFilter.PropertyFilter) { selection = (component as Rock.Reporting.DataFilter.PropertyFilter).UpdateSelectionFromUserPreferenceSelection(selection, selectionUserPreference); } else { selection = selectionUserPreference; } } if (component is Rock.Reporting.DataFilter.IUpdateSelectionFromPageParameters) { selection = (component as Rock.Reporting.DataFilter.IUpdateSelectionFromPageParameters).UpdateSelectionFromPageParameters(selection, this); } try { filterControl.SetSelection(selection); } catch (Exception ex) { this.LogException(new Exception("Exception setting selection for DataViewFilter: " + filter.Guid, ex)); } } string defaultFilterLabel; if (!filterIsConfigurable) { // not configurable so just label it with the selection summary defaultFilterLabel = component.FormatSelection(reportEntityTypeModel, filter.Selection); // configuration not visible, so set the selection to what it was in the dataview when it was saved, even if setSelection=False filterControl.SetSelection(filter.Selection); } else if (component is Rock.Reporting.DataFilter.PropertyFilter) { defaultFilterLabel = (component as Rock.Reporting.DataFilter.PropertyFilter).GetSelectionLabel(reportEntityTypeModel, filter.Selection); } else if (component is Rock.Reporting.DataFilter.EntityFieldFilter) { defaultFilterLabel = (component as Rock.Reporting.DataFilter.EntityFieldFilter).GetSelectedFieldName(filter.Selection); } else { defaultFilterLabel = component.GetTitle(reportEntityTypeModel); } var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson, new Rock.Lava.CommonMergeFieldsOptions { GetLegacyGlobalMergeFields = false }); mergeFields.Add("Filter", filter); mergeFields.Add("Label", defaultFilterLabel); filterControl.Label = dataFilterPrePostHtmlConfig.LabelHtml.ResolveMergeFields(mergeFields); if (!string.IsNullOrEmpty(dataFilterPrePostHtmlConfig.PreHtml)) { filterControl.PreHtml = dataFilterPrePostHtmlConfig.PreHtml.ResolveMergeFields(mergeFields); } if (!string.IsNullOrEmpty(dataFilterPrePostHtmlConfig.PostHtml)) { filterControl.PostHtml = dataFilterPrePostHtmlConfig.PostHtml.ResolveMergeFields(mergeFields); } if (component is Rock.Reporting.DataFilter.OtherDataViewFilter) { // don't include the actual DataView Picker filter, just the child filters parentControl.Controls.Remove(filterControl); Rock.Reporting.DataFilter.OtherDataViewFilter otherDataViewFilter = component as Rock.Reporting.DataFilter.OtherDataViewFilter; var otherDataView = otherDataViewFilter.GetSelectedDataView(filter.Selection); if (otherDataView != null) { CreateFilterControl(parentControl, otherDataView.DataViewFilter, reportEntityType, setSelection, rockContext); } } } } else { var groupControl = new FilterGroup(); parentControl.Controls.Add(groupControl); groupControl.DataViewFilterGuid = filter.Guid; groupControl.ID = string.Format("fg_{0}", groupControl.DataViewFilterGuid.ToString("N")); groupControl.FilteredEntityTypeName = filteredEntityTypeName; groupControl.IsDeleteEnabled = false; groupControl.HidePanelHeader = true; if (setSelection) { groupControl.FilterType = filter.ExpressionType; } foreach (var childFilter in filter.ChildFilters) { CreateFilterControl(groupControl, childFilter, reportEntityType, setSelection, rockContext); } } } catch (Exception ex) { this.LogException(new Exception("Exception creating FilterControl for DataViewFilter: " + filter.Guid, ex)); } }
/// <summary> /// Gets the filter recursive. /// </summary> /// <param name="filterList">The filter list.</param> /// <param name="filter">The filter.</param> /// <param name="reportEntityType">Type of the report entity.</param> private static void GetFilterListRecursive(List <FilterInfo> filterList, DataViewFilter filter, EntityType reportEntityType) { var result = new Dictionary <Guid, string>(); var entityType = EntityTypeCache.Read(filter.EntityTypeId ?? 0); var reportEntityTypeCache = EntityTypeCache.Read(reportEntityType); var reportEntityTypeModel = reportEntityTypeCache.GetEntityType(); var filterInfo = new FilterInfo(filter); filterInfo.FilterList = filterList; if (entityType != null) { var component = Rock.Reporting.DataFilterContainer.GetComponent(entityType.Name); filterInfo.Component = component; filterInfo.ReportEntityTypeModel = reportEntityTypeModel; if (component != null) { if (component is Rock.Reporting.DataFilter.EntityFieldFilter) { var entityFieldFilter = component as Rock.Reporting.DataFilter.EntityFieldFilter; var fieldName = entityFieldFilter.GetSelectedFieldName(filter.Selection); if (!string.IsNullOrWhiteSpace(fieldName)) { var entityFields = EntityHelper.GetEntityFields(reportEntityTypeModel); var entityField = entityFields.Where(a => a.Name == fieldName).FirstOrDefault(); if (entityField != null) { filterInfo.Title = entityField.Title; } else { filterInfo.Title = fieldName; } } } else { filterInfo.Title = component.GetTitle(reportEntityType.GetType()); } } } filterList.Add(filterInfo); if (filterInfo.Component is Rock.Reporting.DataFilter.OtherDataViewFilter) { Rock.Reporting.DataFilter.OtherDataViewFilter otherDataViewFilter = filterInfo.Component as Rock.Reporting.DataFilter.OtherDataViewFilter; var otherDataView = otherDataViewFilter.GetSelectedDataView(filterInfo.Selection); if (otherDataView != null) { var otherDataViewFilterList = new List <FilterInfo>(); GetFilterListRecursive(otherDataViewFilterList, otherDataView.DataViewFilter, reportEntityType); foreach (var otherFilter in otherDataViewFilterList) { if (otherFilter.FromOtherDataView == null) { otherFilter.FromOtherDataView = otherDataView.Name; } } filterList.AddRange(otherDataViewFilterList); } } foreach (var childFilter in filter.ChildFilters) { GetFilterListRecursive(filterList, childFilter, reportEntityType); } }