예제 #1
0
    /// <summary>
    /// Adds item to list control.
    /// </summary>
    /// <param name="row">Field row</param>
    /// <param name="value">Value</param>
    /// <param name="displayName">Display name</param>
    private void AddItem(string row, string value, string displayName)
    {
        // Create new item

        if (FilterMode != SearchFilterModeEnum.DropdownList)
        {
            displayName = HTMLHelper.HTMLEncode(displayName);
        }

        ListItem item = FilterIsConditional ? new ListItem(displayName, SearchSyntaxHelper.GetFilterCondition(row, value) ?? "") : new ListItem(displayName, row);

        switch (FilterMode)
        {
        case SearchFilterModeEnum.Checkbox:
            // Add item to checkbox list
            chklstFilter.Items.Add(item);
            break;

        case SearchFilterModeEnum.RadioButton:
            // Add item to radio button list
            radlstFilter.Items.Add(item);
            break;

        default:
            // Add item to dropdown list
            drpFilter.Items.Add(item);
            break;
        }
    }
예제 #2
0
    /// <summary>
    /// Page load.
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (StopProcessing)
        {
            // Do nothing
        }
        else
        {
            if (!RequestHelper.IsPostBack() && (FilterMode != SearchFilterModeEnum.TextBox))
            {
                // If query name filled - execute it
                if (!string.IsNullOrEmpty(FilterQueryName))
                {
                    // Execute query
                    DataSet ds = ConnectionHelper.ExecuteQuery(FilterQueryName, null, FilterWhere, FilterOrderBy);
                    if (!DataHelper.DataSourceIsEmpty(ds))
                    {
                        // Check that dataset has at least 3 columns
                        if (ds.Tables[0].Columns.Count < 3)
                        {
                            lblError.ResourceString = "srch.filter.fewcolumns";
                            lblError.Visible        = true;
                            return;
                        }

                        // Loop through all rows
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            AddItem(dr[0].ToString(), dr[1].ToString(), ResHelper.LocalizeString(dr[2].ToString()));
                        }
                    }
                }
                // Else if values are filled - parse them
                else if (!string.IsNullOrEmpty(FilterValues))
                {
                    // Split values into rows
                    string[] rows = FilterValues.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                    // Loop through each row
                    foreach (string row in rows)
                    {
                        string trimmedRow = row.Trim().TrimEnd('\r');
                        if (!string.IsNullOrEmpty(trimmedRow))
                        {
                            string[] values = trimmedRow.Split(';');
                            if (values.Length == 3)
                            {
                                AddItem(values[0], values[1], values[2]);
                            }
                            else
                            {
                                lblError.ResourceString = "srch.filter.badformat";
                                lblError.Visible        = true;
                                return;
                            }
                        }
                    }
                }
            }

            // Get webpart ID
            string webpartID = ValidationHelper.GetString(GetValue("WebpartControlID"), ID);

            // Try to get selected values from query string - but only if is not postback
            if (!RequestHelper.IsPostBack())
            {
                string selectedItems = QueryHelper.GetString(webpartID, "");

                // If none of items are selected - try to get default values
                if (string.IsNullOrEmpty(selectedItems))
                {
                    selectedItems = DefaultSelectedIndex;
                }

                if (!string.IsNullOrEmpty(selectedItems))
                {
                    string[] splittedItems = selectedItems.Split(';');
                    foreach (string item in splittedItems)
                    {
                        switch (FilterMode)
                        {
                        case SearchFilterModeEnum.Checkbox:
                            SelectItem(item, chklstFilter);
                            break;

                        case SearchFilterModeEnum.RadioButton:
                            SelectItem(item, radlstFilter);
                            break;

                        case SearchFilterModeEnum.DropdownList:
                            SelectItem(item, drpFilter);
                            break;

                        default:
                            txtFilter.Text = item;
                            break;
                        }
                    }
                }
            }

            string applyFilter = "";
            string ids;

            // Set up controls
            switch (FilterMode)
            {
            // Set text box
            case SearchFilterModeEnum.TextBox:
            {
                txtFilter.Visible           = true;
                txtFilter.WatermarkCssClass = WatermarkCssClass;
                txtFilter.WatermarkText     = WatermarkText;
                txtFilter.AutoPostBack      = FilterAutoPostback;

                AppendClientHandlers(txtFilter);

                if (!String.IsNullOrEmpty(DefaultSelectedIndex) && String.IsNullOrEmpty(txtFilter.Text) && !URLHelper.IsPostback())
                {
                    txtFilter.Text = DefaultSelectedIndex;
                }

                // Apply filter only of textbox contains something
                if (!String.IsNullOrEmpty(txtFilter.Text))
                {
                    // Prepare right condition when filter values aren't empty
                    if (!String.IsNullOrEmpty(FilterValues))
                    {
                        string[] rows     = FilterValues.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                        var      keyWords = SearchSyntaxHelper.ProcessSearchKeywords(txtFilter.Text, SearchOptionsEnum.NoneSearch);

                        // Loop through each row
                        foreach (string row in rows)
                        {
                            string trimmedRow = row.Trim();
                            if (!string.IsNullOrEmpty(trimmedRow))
                            {
                                var rowFilter = SearchSyntaxHelper.GetFilterCondition(trimmedRow, keyWords);
                                applyFilter = SearchSyntaxHelper.AddSearchCondition(applyFilter, rowFilter);
                            }
                        }
                    }
                    else
                    {
                        applyFilter = txtFilter.Text;
                    }
                }

                ids = HttpUtility.UrlEncode(txtFilter.Text);
            }
            break;

            // Set checkbox list
            case SearchFilterModeEnum.Checkbox:
            {
                // Set visibility and layout
                chklstFilter.Visible         = true;
                chklstFilter.RepeatDirection = RepeatDirection.Vertical;
                if (FilterLayout == ControlLayoutEnum.Horizontal)
                {
                    chklstFilter.RepeatDirection = RepeatDirection.Horizontal;
                }

                chklstFilter.RepeatLayout  = GetLayoutEnumFromString(RepeatLayout);
                chklstFilter.RepeatColumns = RepeatColumns;

                // Get selected items
                applyFilter = GetSelectedItems(chklstFilter, out ids);

                // Set autopostback
                if (FilterAutoPostback)
                {
                    chklstFilter.AutoPostBack = true;
                }

                AppendClientHandlers(chklstFilter);
            }
            break;

            // Set radio list
            case SearchFilterModeEnum.RadioButton:
            {
                // Set visibility and layout
                radlstFilter.Visible         = true;
                radlstFilter.RepeatDirection = RepeatDirection.Vertical;
                if (FilterLayout == ControlLayoutEnum.Horizontal)
                {
                    radlstFilter.RepeatDirection = RepeatDirection.Horizontal;
                }

                radlstFilter.RepeatLayout  = GetLayoutEnumFromString(RepeatLayout);
                radlstFilter.RepeatColumns = RepeatColumns;

                // Get selected items
                applyFilter = GetSelectedItems(radlstFilter, out ids);

                // Set autopostback
                if (FilterAutoPostback)
                {
                    radlstFilter.AutoPostBack = true;
                }

                AppendClientHandlers(radlstFilter);
            }
            break;

            // Set dropdown list
            default:
            {
                // Set visibility
                drpFilter.Visible = true;

                // Get selected items
                applyFilter = GetSelectedItems(drpFilter, out ids);

                // Set auto postback
                if (FilterAutoPostback)
                {
                    drpFilter.AutoPostBack = true;
                }

                AppendClientHandlers(drpFilter);

                lblFilter.AssociatedControlID = drpFilter.ID;
            }
            break;
            }

            // Apply filter and add selected values to query string
            ISearchFilterable searchWebpart = (ISearchFilterable)CMSControlsHelper.GetFilter(SearchWebpartID);
            if (searchWebpart != null)
            {
                // Check if postback was caused by any control in the filter
                var postBackControl = ControlsHelper.GetPostBackControl(Page);
                var filterPostback  = (postBackControl != null) && (postBackControl.Parent == this);

                if (FilterIsConditional)
                {
                    // If filter fieldname or value is filled
                    if (!SearchSyntaxHelper.IsEmptyCondition(applyFilter))
                    {
                        // Handle filter clause
                        if (!string.IsNullOrEmpty(FilterClause))
                        {
                            applyFilter = FilterClause + SearchSyntaxHelper.GetGroup(new[] { applyFilter });
                        }

                        searchWebpart.ApplyFilter(applyFilter, null, filterPostback);
                    }

                    searchWebpart.AddFilterOptionsToUrl(webpartID, ids);
                }
                else
                {
                    searchWebpart.ApplyFilter(null, applyFilter, filterPostback);
                    searchWebpart.AddFilterOptionsToUrl(webpartID, ids);
                }
            }
        }
    }
예제 #3
0
        public IEnumerable <T> GetItems(IEnumerable <Guid> taxonomies, out int totalResults, int skip = 0, int take = Int32.MaxValue, string searchSort = "", string searchRootAliasPath = "/")
        {
            var taxonomyCondition = string.Empty;

            var taxonomiesList = taxonomies.ToList();

            if (taxonomiesList.Count > 0)
            {
                foreach (var type in TaxonomyRepository.GetAllTaxonomyTypes())
                {
                    var targetItems = type.TaxonomyItems.Where(x => taxonomiesList.Contains(x.NodeGUID)).ToList();
                    if (targetItems.Count > 0)
                    {
                        taxonomyCondition = SearchSyntaxHelper.AddSearchCondition(taxonomyCondition,
                                                                                  SearchSyntaxHelper.GetRequiredCondition(SearchSyntaxHelper.GetFilterCondition(
                                                                                                                              nameof(IBasePage.Taxonomy),
                                                                                                                              targetItems.Select(x => x.NodeGUID.ToString("D")).Join(" OR "))));
                    }
                }
            }

            var condition = SearchSyntaxHelper.CombineSearchCondition(string.Empty,
                                                                      new SearchCondition
            {
                DocumentCondition = new DocumentSearchCondition
                {
                    ClassNames = new T().ClassName, Culture = Constants.Cultures.Default.Name
                },
                ExtraConditions = taxonomyCondition
            });

            var parameters = new SearchParameters
            {
                SearchFor = condition,
                Path      = $"{searchRootAliasPath}%",
                CombineWithDefaultCulture = false,
                CheckPermissions          = false,
                SearchInAttachments       = false,
                User                     = MembershipContext.AuthenticatedUser,
                SearchIndexes            = Settings.Taxonomy.SearchIndex,
                StartingPosition         = skip,
                NumberOfProcessedResults = ((Int64)skip + (Int64)take) >= (Int64)Int32.MaxValue ? Int32.MaxValue : skip + take,
                DisplayResults           = take,
                SearchSort               = searchSort
            };

            var searchResults = SearchHelper.Search(parameters);

            totalResults = searchResults.Parameters.NumberOfResults;

            return(searchResults.Items.Select(x => x.Data).OfType <T>());
        }