private ListRefinerView BuildAdminSettingsView(ModuleInstanceView parent, ListRefinerView settingsData)
        {
            settingsData.AvailableFields.Clear();

            settingsData.AvailableFields.Add(new Field("Event Topics", "Event Topics", FieldTypes.Filter, null) { FilterSettings = CreateTestFilterSettings("EventTopicId") });

            //Org Unit Types
            var orgUnitTypes = parent.ProcessRequest<ListOrgUnitTypesResponse>(new ListOrgUnitTypesRequest() { IsEnabled = true, ExcludeOutsideOrgUnit = true }).OrgUnitTypes;
            foreach (var type in orgUnitTypes.Where(t => t.Name != Constants.Locations.LocationTypePrintableProviderDirectory))
            {
                settingsData.AvailableFields.Add(new Field(type.Name, type.Name, FieldTypes.Filter, null) { FilterSettings = CreateTestFilterSettings("LocationId") });
            }

            //Dynamic Columns
            var dynamicColumns = CommonUtils.GetDynamicColumnInstances(parent, Constants.EventDynamicColumnEntityId);
            foreach (var col in dynamicColumns.Where(c => c.DataType != "contentEditor" && c.DataType != "datetime"))
            {
                settingsData.AvailableFields.Add(new Field(Constants.DynamicColumnPrefix + col.FormLabel, col.FormLabel, FieldTypes.Filter, null) { FilterSettings = CreateTestFilterSettings(Constants.DynamicColumnPrefix + col.Name) });
            }

            settingsData.AvailableFields = settingsData.AvailableFields.OrderBy(f => f.DisplayName).ToList();

            // Add admin-only settings
            LocationContextHelper.AddLocationSettingsGroup(parent, settingsData);

            return settingsData;
        }
        private ListRefinerView BuildPublicSettingsView(ModuleInstanceView parent, ListRefinerView settingsData, IDictionary<string, string> parameters)
        {
            // Establish correct search results page URL
            settingsData.UpdateSettingValue("SearchResultsPageUrl", EventUtils.CalculateSearchResultsUrl(settingsData, parent.SearchSettings.SearchResultsPageUrl));

            var searchRequest = BuildRequest(parent, settingsData, parameters);
            var searchResponse = parent.ProcessRequest<ListSearchFiltersResponse>(searchRequest);

            PopulateSearchFilterValues(searchResponse.FilterTypes, settingsData, parameters);

            return settingsData;
        }
        private ListSearchFiltersRequest BuildRequest(ModuleInstanceView parent, ListRefinerView settingsData, IDictionary<string, string> parameters)
        {
            var searchRequest = new ListSearchFiltersRequest();

            // Location Context
            searchRequest.OrganizationalUnitId = LocationContextHelper.ResolveLocationContext(parent, settingsData, parameters).Id;
            searchRequest.DescendantOption = LocationContextHelper.ResolveLocationDescendantOption(settingsData, parameters);
            //Keyword
            if (parameters.ContainsKey("Keyword"))
                searchRequest.Keyword = parameters["Keyword"];
            //Event Title
            if (parameters.ContainsKey("EventId"))
                searchRequest.Title = parameters["EventId"];
            if (parameters.ContainsKey("EventText"))
                searchRequest.Title = parameters["EventText"];
            //Location
            if (parameters.ContainsKey("LocationId"))
                searchRequest.OrganizationalUnitId = int.Parse(parameters["LocationId"]);
            if (parameters.ContainsKey("LocationText"))
                searchRequest.OrganizationalUnitName = parameters["LocationText"];
            //Event Topic
            if (parameters.ContainsKey("EventTopicId"))
                searchRequest.EventTopicId = int.Parse(parameters["EventTopicId"]);
            //Event Type
            if (parameters.ContainsKey("EventTypeId"))
                searchRequest.EventTypeId = int.Parse(parameters["EventTypeId"]);
            //Start Date
            if (parameters.ContainsKey("StartDate"))
                searchRequest.EventStartDate = DateTime.Parse(parameters["StartDate"]);
            //End Date
            if (parameters.ContainsKey("EndDate"))
                searchRequest.EventEndDate = DateTime.Parse(parameters["EndDate"]);
            //Postal Code
            if (parameters.ContainsKey("PostalCode"))
                searchRequest.PostalCode = parameters["PostalCode"];
            //Search Radius
            if (parameters.ContainsKey("RadiusDistance") && !string.IsNullOrEmpty(parameters["RadiusDistance"]))
                searchRequest.SearchRadius = int.Parse(parameters["RadiusDistance"]);

            //Dynamic Column Filtering
            if (parameters.Any(p => p.Key.StartsWith(Constants.DynamicColumnPrefix)))
            {
                var dynamicFilters = new List<string>();
                foreach (var item in parameters.Where(p => p.Key.StartsWith(Constants.DynamicColumnPrefix)))
                {
                    dynamicFilters.Add(item.Value);
                }
                searchRequest.DynamicColumns = dynamicFilters;
            }

            return searchRequest;
        }
        public string GetModelData(ModuleInstanceView parent, string settingsData, IDictionary<string, string> parameters, bool isConfigView)
        {
            ListRefinerView viewSettings = null;

            if (string.IsNullOrEmpty(settingsData))
                viewSettings = new ListRefinerView();
            else
                viewSettings = CommonUtils.JsonDeserialize<ListRefinerView>(settingsData);

            if (isConfigView)
                viewSettings = BuildAdminSettingsView(parent, viewSettings);
            else
            {
                viewSettings = BuildPublicSettingsView(parent, viewSettings, parameters);
                viewSettings.AvailableFields.Clear();
            }

            return CommonUtils.JsonSerializeMinimum(viewSettings);
        }
        private void PopulateSearchFilterValues(IEnumerable<SearchFilterType> filters, ListRefinerView settingsData, IDictionary<string, string> parameters)
        {
            foreach (var zone in settingsData.StaticPageZones.Select(z => z.Value).Union(settingsData.DynamicPageZones))
            {
                foreach (var field in zone.FieldColumns.SelectMany(f => f.Fields))
                {
                    field.FilterSettings.FilterValues.Clear();
                    string filterName = string.Empty;
                    SearchFilterType filter = null;
                    string queryStringKey = string.Empty;

                    if (field.FieldName.StartsWith(Constants.DynamicColumnPrefix))
                    {
                        filterName = field.FieldName.Replace(Constants.DynamicColumnPrefix, string.Empty);
                        filter = filters.FirstOrDefault(f => f.Name == filterName);
                        if (filter != null)
                            queryStringKey = Constants.DynamicColumnPrefix + filter.Name;
                    }
                    else
                    {
                        filterName = field.FieldName;
                        filter = filters.FirstOrDefault(f => f.Name == filterName);
                        if (filter != null)
                            queryStringKey = filter.QueryStringParameterName;
                    }

                    if (filter != null)
                    {
                        //Filter Item Selected
                        if (parameters.ContainsKey(field.FilterSettings.QueryStringKey))
                        {
                            var filterItem = filter.FilterValues.FirstOrDefault(f => f.QueryStringParameterValue == parameters[field.FilterSettings.QueryStringKey]);
                            if (filterItem != null)
                            {
                                field.FilterSettings.FilterValues.Add(new FilterValue
                                {
                                    Text = filterItem.DisplayName,
                                    Count = filterItem.Count,
                                    QueryStringValue = filterItem.QueryStringParameterValue,
                                    IsSelected = true
                                });
                            }
                        }
                        //Filter Items Not Selected
                        else
                        {
                            foreach (var filterItem in filter.FilterValues)
                            {
                                field.FilterSettings.FilterValues.Add(new FilterValue
                                {
                                    Text = filterItem.DisplayName,
                                    Count = filterItem.Count,
                                    QueryStringValue = filterItem.QueryStringParameterValue
                                });
                            }
                        }
                    }
                }
            }
        }
        private ListSearchFiltersRequest BuildRequest(ModuleInstanceView parent, ListRefinerView settingsData, IDictionary<string, string> parameters)
        {
            var searchRequest = new ListSearchFiltersRequest();

            // Location Context
            searchRequest.OrgUnitId = LocationContextHelper.ResolveLocationContext(parent, settingsData, parameters).Id;
            searchRequest.DescendantOption = LocationContextHelper.ResolveLocationDescendantOption(settingsData, parameters);
            //Provider
            if (parameters.ContainsKey("ProviderId"))
                searchRequest.ProviderId = int.Parse(parameters["ProviderId"]);
            if (parameters.ContainsKey("ProviderText"))
                searchRequest.ProviderName = parameters["ProviderText"];
            //Keyword
            if (parameters.ContainsKey("Keyword"))
                searchRequest.Keyword = parameters["Keyword"];
            //Gender
            if (parameters.ContainsKey("Gender"))
                searchRequest.GenderId = int.Parse(parameters["Gender"]);
            //Specialty
            if (parameters.ContainsKey("SpecialtyId"))
                searchRequest.SpecialtyIds = new List<string> { parameters["SpecialtyId"] };
            if (parameters.ContainsKey("SpecialtyText"))
                searchRequest.SpecialtyName = parameters["SpecialtyText"];
            //Condition
            if (parameters.ContainsKey("ConditionId"))
                searchRequest.ConditionIds = new List<string> { parameters["ConditionId"] };
            if (parameters.ContainsKey("ConditionText"))
                searchRequest.ConditionName = parameters["ConditionText"];
            //Insurance
            if (parameters.ContainsKey("InsuranceId"))
                searchRequest.InsuranceId = int.Parse(parameters["InsuranceId"]);
            if (parameters.ContainsKey("InsuranceText"))
                searchRequest.InsuranceName = parameters["InsuranceText"];
            //Language
            if (parameters.ContainsKey("LanguageId"))
                searchRequest.LanguageIds = new List<string> { parameters["LanguageId"] };
            if (parameters.ContainsKey("LanguageText"))
                searchRequest.LanguageName = parameters["LanguageText"];
            //Location
            if (parameters.ContainsKey("LocationId"))
                searchRequest.FilteringOrgUnitIds = new List<string> { parameters["LocationId"] };
            if (parameters.ContainsKey("LocationText"))
                searchRequest.OrgUnitName = parameters["LocationText"];
            //City
            if (parameters.ContainsKey("CityId"))
                searchRequest.City = parameters["CityId"];
            //State
            if (parameters.ContainsKey("StateId"))
                searchRequest.State = parameters["StateId"];
            //Postal Code
            if (parameters.ContainsKey("PostalCode"))
                searchRequest.PostalCode = parameters["PostalCode"];
            //Accpeting New Patients
            if (parameters.ContainsKey("OnlyAcceptingNewPatients"))
                searchRequest.IsAcceptingNewPatients = bool.Parse(parameters["OnlyAcceptingNewPatients"]) ? true : new bool?();
            //Clincical Interest
            if (parameters.ContainsKey("ClinicalInterestId"))
                searchRequest.ClinicalInterestIds = new List<string> { parameters["ClinicalInterestId"] };
            if (parameters.ContainsKey("ClinicalInterestText"))
                searchRequest.ClinicalInterestName = parameters["ClinicalInterestText"];
            //Provider Type
            if (parameters.ContainsKey("ProviderTypeId"))
                searchRequest.ProviderTypeIds = new List<string> { parameters["ProviderTypeId"] };
            if (parameters.ContainsKey("ProviderTypeText"))
                searchRequest.ProviderTypeName = parameters["ProviderTypeText"];
            //Letter Alpha Search
            if (parameters.ContainsKey("Letter"))
                searchRequest.Letter = parameters["Letter"];

            //Dynamic Column Filtering
            if (parameters.Any(p => p.Key.StartsWith(Constants.DynamicColumnPrefix)))
            {
                var dynamicFilters = new List<string>();
                foreach (var item in parameters.Where(p => p.Key.StartsWith(Constants.DynamicColumnPrefix)))
                {
                    dynamicFilters.Add(item.Value);
                }
                searchRequest.DynamicColumns = dynamicFilters;
            }

            return searchRequest;
        }