<commit_before>#
#
# Here lie all the helpers for the fancy search form for pages.
#
# For many of the methods in this helper to work, 'page_search_path()' must be defined.
# Typically, this will call me_pages_path or group_pages_path.
#

require 'cgi'

module Common::Page::SearchHelper

  protected

  def search_filter_sections
    [:my_pages, :access, :properties, :popular_pages, :advanced]
  end

  #def opened_section?(section)
  #  [:my_pages].include?(section)
  #end

  # creates a quick lookup map of filter => true for the currently active filters
  def active_filters
    @active_filters ||= @path.filters.to_h {|f| [f[0],true]}
  end

  # returns true if the filter is active in the current path
  def filter_active?(filter)
    active_filters[filter]
  end

  #
  # Returns true if the specified filter should not be shown.
  #
  # There are two reasons to hide a filter:
  #
  # (1) some filters are incompatible with other filters. The filter definition
  #     for these filters will include filter.exclude. This will be a symbol
  #     that defines a set of mutually exclusive filters (e.g. :popular_pages).
  # (2) the current controller can define 'include_filter?(filter)'. If it returns
  #     false, then the filter is excluded.
  #
  def filter_excluded?(filter)
    @excluded_filters ||= begin
      @path.filters.to_h {|f| [f[0].exclude, true]}
    end
    if !show_filter?(filter)
      true
    elsif filter.exclude
      @excluded_filters[filter.exclude]
    else
      false
    end
  end

  def possible_filters_for_section(section)
    enabled, disabled = [], []
    SearchFilter.filters_for_section(section).each do |filter|
      if filter_active?(filter)
         # ignore?
      elsif filter_excluded?(filter)
        disabled.push(filter)
      else
        enabled.push(filter)
      end
    end
    return [enabled, disabled]
  end

  def filter_all
    SearchFilter['all']
  end

  #
  # if we should show the 'all' filter, return true.
  #
  def show_all?
    @show_all ||= !(@path.filters.detect {|filter,x| filter.has_label?})
  end

  #
  # mode: must be :add or :remove
  #
  def filter_checkbox_li_tag(mode, filter, args=nil, options={})
    if filter.has_args?
      filter_multivalue_li_tag(mode, filter, args, options)
    else
      filter_singlevalue_li_tag(mode, filter, options)
    end
  end

  #
  # for filters with no args
  #
  def filter_singlevalue_li_tag(mode, filter, options)
    if options[:disabled]
      link_to_function(label, '', :icon => 'check_off', :class => 'disabled')
    else
      spinbox_tag(filter.path_keyword,
        page_search_path(mode => filter.path_definition),
        :label => filter.label(nil, {mode => true, :current_user => current_user}),
        :with => 'FilterPath.encode()',
        :checked => (mode == :remove) )
    end
  end

  #
  # for filters with one or more args
  #
  def filter_multivalue_li_tag(mode, filter, args, options)
    label = filter.label(args, {mode => true, :current_user => current_user})
    if options[:disabled]
      link_to_function(label, '', :icon => 'check_off', :class => 'disabled')
    elsif mode == :add
      html = render(:partial => 'common/pages/search/popup',
        :locals => {:url => page_search_path(:add => filter.path_definition), :filter => filter})
      link_to_modal(label, :html => html, :icon => 'check_off')
    elsif mode == :remove
      if label<commit_msg>disabled filters referred to undef label - refs #4255<commit_after>#
#
# Here lie all the helpers for the fancy search form for pages.
#
# For many of the methods in this helper to work, 'page_search_path()' must be defined.
# Typically, this will call me_pages_path or group_pages_path.
#

require 'cgi'

module Common::Page::SearchHelper

  protected

  def search_filter_sections
    [:my_pages, :access, :properties, :popular_pages, :advanced]
  end

  #def opened_section?(section)
  #  [:my_pages].include?(section)
  #end

  # creates a quick lookup map of filter => true for the currently active filters
  def active_filters
    @active_filters ||= @path.filters.to_h {|f| [f[0],true]}
  end

  # returns true if the filter is active in the current path
  def filter_active?(filter)
    active_filters[filter]
  end

  #
  # Returns true if the specified filter should not be shown.
  #
  # There are two reasons to hide a filter:
  #
  # (1) some filters are incompatible with other filters. The filter definition
  #     for these filters will include filter.exclude. This will be a symbol
  #     that defines a set of mutually exclusive filters (e.g. :popular_pages).
  # (2) the current controller can define 'include_filter?(filter)'. If it returns
  #     false, then the filter is excluded.
  #
  def filter_excluded?(filter)
    @excluded_filters ||= begin
      @path.filters.to_h {|f| [f[0].exclude, true]}
    end
    if !show_filter?(filter)
      true
    elsif filter.exclude
      @excluded_filters[filter.exclude]
    else
      false
    end
  end

  def possible_filters_for_section(section)
    enabled, disabled = [], []
    SearchFilter.filters_for_section(section).each do |filter|
      if filter_active?(filter)
         # ignore?
      elsif filter_excluded?(filter)
        disabled.push(filter)
      else
        enabled.push(filter)
      end
    end
    return [enabled, disabled]
  end

  def filter_all
    SearchFilter['all']
  end

  #
  # if we should show the 'all' filter, return true.
  #
  def show_all?
    @show_all ||= !(@path.filters.detect {|filter,x| filter.has_label?})
  end

  #
  # mode: must be :add or :remove
  #
  def filter_checkbox_li_tag(mode, filter, args=nil, options={})
    if filter.has_args?
      filter_multivalue_li_tag(mode, filter, args, options)
    else
      filter_singlevalue_li_tag(mode, filter, options)
    end
  end

  #
  # for filters with no args
  #
  def filter_singlevalue_li_tag(mode, filter, options)
    label = filter.label(nil, {mode => true, :current_user => current_user})
    if options[:disabled]
      link_to_function(label, '', :icon => 'check_off', :class => 'disabled')
    else
      spinbox_tag(filter.path_keyword,
        page_search_path(mode => filter.path_definition),
        :label => label,
        :with => 'FilterPath.encode()',
        :checked => (mode == :remove) )
    end
  end

  #
  # for filters with one or more args
  #
  def filter_multivalue_li_tag(mode, filter, args, options)
    label = filter.label(args, {mode => true, :current_user => current_user})
    if options[:disabled]
      link_to_function(label, '', :icon => 'check_off', :class => 'disabled')
    elsif mode == :add
      html = render(:partial => 'common/pages/search/popup',
        :locals => {:url => page_search_path(:add => filter.path_definition), :filter => filter})
      link_to_modal(label, :html => html, :icon => 'check_off')
    elsif mode == :remove
      if label
{'fraction_non_alphanumeric': 0.11157864190365642, 'fraction_numerical': 0.0017411491584445734, 'mean_word_length': 3.2470733210104745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1675996', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2155, 'n_words': 638}