private DataViewFilter GetFilterFieldControl(FilterField filterField)
        {
            DataViewFilter filter = new DataViewFilter();

            filter.Guid           = filterField.DataViewFilterGuid;
            filter.ExpressionType = FilterExpressionType.Filter;
            filter.Expanded       = filterField.Expanded;
            if (filterField.FilterEntityTypeName != null)
            {
                filter.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(filterField.FilterEntityTypeName).Id;
                filter.Selection    = filterField.GetSelection();
            }

            return(filter);
        }
示例#2
0
        /// <summary>
        /// Gets the filter field control.
        /// </summary>
        /// <param name="filterField">The filter field.</param>
        /// <returns></returns>
        private static DataViewFilter GetFilterFieldControl(FilterField filterField)
        {
            if (filterField.ShowCheckbox && !filterField.CheckBoxChecked.GetValueOrDefault(true))
            {
                return(null);
            }

            DataViewFilter filter = new DataViewFilter();

            filter.Guid           = filterField.DataViewFilterGuid;
            filter.ExpressionType = FilterExpressionType.Filter;
            filter.Expanded       = filterField.Expanded;
            if (filterField.FilterEntityTypeName != null)
            {
                filter.EntityTypeId = EntityTypeCache.Get(filterField.FilterEntityTypeName).Id;
                filter.Selection    = filterField.GetSelection();
            }

            return(filter);
        }
示例#3
0
        /// <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;
                    filterControl.HideDescription = true;

                    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);

                                // if the selection is the same as what is stored in the database for that DataViewFilter,
                                // Do a GetSelection to get the selection in the current format for that filter
                                // This will prevent this dynamic report from thinking the selection has changed from the orig filter
                                if (selection == filter.Selection)
                                {
                                    var normalizedSelection = filterControl.GetSelection();
                                    if (normalizedSelection != filter.Selection)
                                    {
                                        // if the format of the filter.Selection has changed, update the dataViewFilter's Selection to match the current format
                                        filter.Selection = normalizedSelection;
                                        using (var updateSelectionContext = new RockContext())
                                        {
                                            var dataViewFilter = new DataViewFilterService(updateSelectionContext).Get(filter.Id);
                                            dataViewFilter.Selection = normalizedSelection;
                                            updateSelectionContext.SaveChanges();
                                        }
                                    }
                                }
                            }
                            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));
            }
        }