<commit_before># Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  include Smerf
  
  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => '26c58c750ac36e1713e76184b3b8e162'

  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  filter_parameter_logging :password

  before_filter :set_locale
  before_filter :fetch_logged_in_user
  before_filter :count_new_arrived_items
  before_filter :set_up_feedback_form
  

  # Change current navigation state based on array containing new navi items.
  def save_navi_state(navi_items)
    session[:navi1] = navi_items[0] || session[:navi1]
    session[:navi2] = navi_items[1] || session[:navi2]
    session[:navi3] = navi_items[2] || session[:navi3]
    session[:navi4] = navi_items[3] || session[:navi4]
    session[:profile_navi] = navi_items[4] || session[:profile_navi]
  end

  # Sets navigation state to "nothing selected".
  def clear_navi_state
    session[:navi1] = session[:navi2] = session[:navi3] = session[:navi4] = session[:navi_profile] = nil
  end
  
  # Fetch listings based on conditions
  def fetch_listings(conditions, order = 'id DESC')
    @listings = Listing.paginate(:page => params[:page], 
                                 :per_page => per_page,
                                 :order => order,
                                 :select => 'id, created_at, author_id, title, status, times_viewed, category, good_thru, visibility', 
                                 :conditions => conditions)                                                                       
  end

  # Returns visibility conditions for object_type (item, favor or listing)
  def get_visibility_conditions(object_type)
    conditions = " AND (visibility = 'everybody'"
    if @current_user
      case object_type
      when "listing"
        person_type = "author_id"
      when "item"
        person_type = "owner_id"
      when "favor"  
        person_type = "owner_id"
      end  
      conditions += " OR visibility = 'kassi_users' OR #{person_type} = '#{@current_user.id}'"
      friend_ids = @current_user.get_friend_ids(session[:cookie]) 
      if friend_ids.size > 0
        conditions += " OR (visibility IN ('friends', 'f_c', 'f_g', 'f_c_g') 
        AND #{person_type} IN (" + friend_ids.collect { |id| "'#{id}'" }.join(",") + "))"
      end
      if Person.count_by_sql(@current_user.contact_query("COUNT(id)")) > 0
        conditions += " OR (visibility IN ('contacts', 'f_c', 'c_g', 'f_c_g') 
        AND #{person_type} IN (#{@current_user.contact_query('id')}))"
      end
      if @current_user.groups(session[:cookie]).size > 0
        group_ids = @current_user.get_group_ids(session[:cookie]).collect { |id| "'#{id}'" }.join(",")
        conditions += " OR (visibility IN ('groups', 'f_g', 'c_g', 'f_c_g')
        AND id IN (
          SELECT #{object_type}s.id 
          FROM groups_#{object_type}s, #{object_type}s
          WHERE groups_#{object_type}s.group_id IN (#{group_ids})
          AND groups_#{object_type}s.#{object_type}_id = #{object_type}s.id
        ))"
      end
    end
    conditions += ")"
  end
  
  # Returns the visibility value to be saved in db based 
  # on the visibility parameter and checkbox values
  def get_visibility(object_type)
    if params[object_type][:visibility].eql?("other")
      if params[:friends]
        if params[:contacts]
          if params[:groups]
            params[object_type][:visibility] = "f_c_g"
          else  
            params[object_type][:visibility] = "f_c"
          end  
        else
          if params[:groups]
            params[object_type][:visibility] = "f_g"
          else
            params[object_type][:visibility] = "friends"
          end  
        end  
      else
        if params[:contacts]
          if params[:groups]
            params[object_type][:visibility] = "c_g"
          else
            params[object_type][:visibility] = "contacts"
          end    
        else
          if params[:groups]
            params[object_type][:visibility] = "groups"
          else  
            params[object_type][:visibility] = "none"
          end  
        end  
      end
    end
  end
  
  # Renders friend and group checkboxes for visibility
  def visibility_form_checkboxes
    @visibility = params[:visibility]
    if params[:object_type]
      case params[:object_type]
      when "Item"
        @object = Item.find(params[:object_id])
      end
    end
    @groups = @object.groups if @object
    @object_visibility = params[:object_visibility]
  end

  # Define how many listed items are shown per page.<commit_msg>Korjattu bugi näkyvyyscheckboksien näyttämisessä.<commit_after># Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  include Smerf
  
  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => '26c58c750ac36e1713e76184b3b8e162'

  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  filter_parameter_logging :password

  before_filter :set_locale
  before_filter :fetch_logged_in_user
  before_filter :count_new_arrived_items
  before_filter :set_up_feedback_form
  

  # Change current navigation state based on array containing new navi items.
  def save_navi_state(navi_items)
    session[:navi1] = navi_items[0] || session[:navi1]
    session[:navi2] = navi_items[1] || session[:navi2]
    session[:navi3] = navi_items[2] || session[:navi3]
    session[:navi4] = navi_items[3] || session[:navi4]
    session[:profile_navi] = navi_items[4] || session[:profile_navi]
  end

  # Sets navigation state to "nothing selected".
  def clear_navi_state
    session[:navi1] = session[:navi2] = session[:navi3] = session[:navi4] = session[:navi_profile] = nil
  end
  
  # Fetch listings based on conditions
  def fetch_listings(conditions, order = 'id DESC')
    @listings = Listing.paginate(:page => params[:page], 
                                 :per_page => per_page,
                                 :order => order,
                                 :select => 'id, created_at, author_id, title, status, times_viewed, category, good_thru, visibility', 
                                 :conditions => conditions)                                                                       
  end

  # Returns visibility conditions for object_type (item, favor or listing)
  def get_visibility_conditions(object_type)
    conditions = " AND (visibility = 'everybody'"
    if @current_user
      case object_type
      when "listing"
        person_type = "author_id"
      when "item"
        person_type = "owner_id"
      when "favor"  
        person_type = "owner_id"
      end  
      conditions += " OR visibility = 'kassi_users' OR #{person_type} = '#{@current_user.id}'"
      friend_ids = @current_user.get_friend_ids(session[:cookie]) 
      if friend_ids.size > 0
        conditions += " OR (visibility IN ('friends', 'f_c', 'f_g', 'f_c_g') 
        AND #{person_type} IN (" + friend_ids.collect { |id| "'#{id}'" }.join(",") + "))"
      end
      if Person.count_by_sql(@current_user.contact_query("COUNT(id)")) > 0
        conditions += " OR (visibility IN ('contacts', 'f_c', 'c_g', 'f_c_g') 
        AND #{person_type} IN (#{@current_user.contact_query('id')}))"
      end
      if @current_user.groups(session[:cookie]).size > 0
        group_ids = @current_user.get_group_ids(session[:cookie]).collect { |id| "'#{id}'" }.join(",")
        conditions += " OR (visibility IN ('groups', 'f_g', 'c_g', 'f_c_g')
        AND id IN (
          SELECT #{object_type}s.id 
          FROM groups_#{object_type}s, #{object_type}s
          WHERE groups_#{object_type}s.group_id IN (#{group_ids})
          AND groups_#{object_type}s.#{object_type}_id = #{object_type}s.id
        ))"
      end
    end
    conditions += ")"
  end
  
  # Returns the visibility value to be saved in db based 
  # on the visibility parameter and checkbox values
  def get_visibility(object_type)
    if params[object_type][:visibility].eql?("other")
      if params[:friends]
        if params[:contacts]
          if params[:groups]
            params[object_type][:visibility] = "f_c_g"
          else  
            params[object_type][:visibility] = "f_c"
          end  
        else
          if params[:groups]
            params[object_type][:visibility] = "f_g"
          else
            params[object_type][:visibility] = "friends"
          end  
        end  
      else
        if params[:contacts]
          if params[:groups]
            params[object_type][:visibility] = "c_g"
          else
            params[object_type][:visibility] = "contacts"
          end    
        else
          if params[:groups]
            params[object_type][:visibility] = "groups"
          else  
            params[object_type][:visibility] = "none"
          end  
        end  
      end
    end
  end
  
  # Renders friend and group checkboxes for visibility
  def visibility_form_checkboxes
    @visibility = params[:visibility]
    if params[:object_type]
      case params[:object_type]
      when "Item"
        @object = Item.find(params[:object_id])
      when "Listing"
        @object = Listing.find(params[:object_id])
      when "Favor"
        @object = Favor.find(params[:object_id])   
      end
    end
    @groups = @object.groups if @object
    @object_visibility = params[:object_visibility]
  end

  # Define how many listed items are shown per page.
{'fraction_non_alphanumeric': 0.10786888434977142, 'fraction_numerical': 0.008364945044256396, 'mean_word_length': 2.610252808988764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2737032', 'n_tokens_mistral': 3164, 'n_tokens_neox': 3054, 'n_words': 774}