<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
  require_dependency File.join(Rails.root, 'vendor', 'plugins', 'annotations', 'lib', 'app', 'controllers', 'application_controller')

  skip_after_filter :add_piwik_analytics_tracking if Seek::Config.piwik_analytics_enabled == false

  self.mod_porter_secret = PORTER_SECRET

  include ExceptionNotifiable
  self.error_layout="errors"
  self.silent_exceptions = []
  self.rails_error_classes = {
  ActiveRecord::RecordNotFound => "404",
  ::ActionController::UnknownController => "406",
  ::ActionController::UnknownAction => "406",
  ::ActionController::RoutingError => "404",  
  ::ActionView::MissingTemplate => "406",
  ::ActionView::TemplateError => "500"
  }
  local_addresses.clear

  exception_data :additional_exception_notifier_data

  after_filter :log_event

  include AuthenticatedSystem
  around_filter :with_current_user
  def with_current_user
    User.with_current_user current_user do
      yield
    end
  end

  before_filter :project_membership_required,:only=>[:create,:new]

  helper :all

  layout "main"

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => '<KEY>'

  def set_no_layout
    self.class.layout nil
  end

  def base_host
    request.host_with_port
  end


  def self.fast_auto_complete_for(object, method, options = {})
    define_method("auto_complete_for_#{object}_#{method}") do
      render :json => object.to_s.camelize.constantize.find(:all).map(&method).to_json
    end
  end

  #Overridden from restful_authentication
  #Does a second check that there is a profile assigned to the user, and if not goes to the profile
  #selection page (GET people/select)
  def authorized?
    if super
      redirect_to(select_people_path) if current_user.person.nil?
      true
    else
      false
    end
  end

  def is_user_activated
    if Seek::Config.activation_required_enabled && current_user && !current_user.active?
      error("Activation of this account it required for gaining full access", "Activation required?")
      false
    end
  end

  def is_current_user_auth
    begin
      @user = User.find(params[:id], :conditions => ["id = ?", current_user.id])
    rescue ActiveRecord::RecordNotFound
      error("User not found (id not authorized)", "is invalid (not owner)")
      return false
    end

    unless @user
      error("User not found (or not authorized)", "is invalid (not owner)")
      return false
    end
  end

  def is_user_admin_auth
    unless User.admin_logged_in?
      error("Admin rights required", "is invalid (not admin)")
      return false
    end
    return true
  end

  def can_manage_announcements?
    User.admin_logged_in?
  end

  def logout_user
    current_user.forget_me if logged_in?
    cookies.delete :auth_token
    cookies.delete :open_id
    reset_session
  end

  def find_or_create_substance(new_substances, known_substance_ids_and_types)
    known_substances = []
    known_substance_ids_and_types.each do |text|
      id, type = text.split(',')
      id = id.strip
      type = type.strip.capitalize.constantize
      known_substances.push(type.find(id)) if type.find(id)
    end
    new_substances, known_substances = check_if_new_substances_are_known new_substances, known_substances
    #no substance
    if (new_substances.size + known_substances.size) == 0
      nil
    #one substance
    elsif (new_substances.size + known_substances.size) == 1
      if !known_substances.empty?
        known_substances.first
      else
        c = Compound.new(:name => new_substances.first)
          if  c.save
            c
          else
            nil
          end
      end
    #FIXME: update code when mixture table is created
    else
      nil
    end
  end

  def no_comma_for_decimal
    check_string = ''
    if self.controller_name.downcase == 'studied_factors'
      check_string.concat(params[:studied_factor][:start_value].to_s + params[:studied_factor][:end_value].to_s + params[:studied_factor][:standard_deviation].to_s)
    elsif self.controller_name.downcase == 'experimental_conditions'
      check_string.concat(params[:experimental_condition][:start_value].to_s + params[:experimental_condition][:end_value].to_s)
    end

    if check_string.match(',')
         render :update do |page|
           page.alert('Please use point instead of comma for decimal number')
         end
      return false
    else
      return true
    end
  end
  private

  def project_membership_required
    unless try_block {current_user.person.member? or User.admin_logged_in?}
      flash[:error] = "Only members of known projects, institutions or work groups are allowed to create new content."
      respond_to do |format|
        format.html do
          try_block {redirect_to eval("#{controller_name.camel_case}Controller.new").respond_to?("index")} or redirect_to root_url<commit_msg>Fixed buggy change in application controller<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
  require_dependency File.join(Rails.root, 'vendor', 'plugins', 'annotations', 'lib', 'app', 'controllers', 'application_controller')

  skip_after_filter :add_piwik_analytics_tracking if Seek::Config.piwik_analytics_enabled == false

  self.mod_porter_secret = PORTER_SECRET

  include ExceptionNotifiable
  self.error_layout="errors"
  self.silent_exceptions = []
  self.rails_error_classes = {
  ActiveRecord::RecordNotFound => "404",
  ::ActionController::UnknownController => "406",
  ::ActionController::UnknownAction => "406",
  ::ActionController::RoutingError => "404",  
  ::ActionView::MissingTemplate => "406",
  ::ActionView::TemplateError => "500"
  }
  local_addresses.clear

  exception_data :additional_exception_notifier_data

  after_filter :log_event

  include AuthenticatedSystem
  around_filter :with_current_user
  def with_current_user
    User.with_current_user current_user do
      yield
    end
  end

  before_filter :project_membership_required,:only=>[:create,:new]

  helper :all

  layout "main"

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => '<KEY>'

  def set_no_layout
    self.class.layout nil
  end

  def base_host
    request.host_with_port
  end


  def self.fast_auto_complete_for(object, method, options = {})
    define_method("auto_complete_for_#{object}_#{method}") do
      render :json => object.to_s.camelize.constantize.find(:all).map(&method).to_json
    end
  end

  #Overridden from restful_authentication
  #Does a second check that there is a profile assigned to the user, and if not goes to the profile
  #selection page (GET people/select)
  def authorized?
    if super
      redirect_to(select_people_path) if current_user.person.nil?
      true
    else
      false
    end
  end

  def is_user_activated
    if Seek::Config.activation_required_enabled && current_user && !current_user.active?
      error("Activation of this account it required for gaining full access", "Activation required?")
      false
    end
  end

  def is_current_user_auth
    begin
      @user = User.find(params[:id], :conditions => ["id = ?", current_user.id])
    rescue ActiveRecord::RecordNotFound
      error("User not found (id not authorized)", "is invalid (not owner)")
      return false
    end

    unless @user
      error("User not found (or not authorized)", "is invalid (not owner)")
      return false
    end
  end

  def is_user_admin_auth
    unless User.admin_logged_in?
      error("Admin rights required", "is invalid (not admin)")
      return false
    end
    return true
  end

  def can_manage_announcements?
    User.admin_logged_in?
  end

  def logout_user
    current_user.forget_me if logged_in?
    cookies.delete :auth_token
    cookies.delete :open_id
    reset_session
  end

  def find_or_create_substance(new_substances, known_substance_ids_and_types)
    known_substances = []
    known_substance_ids_and_types.each do |text|
      id, type = text.split(',')
      id = id.strip
      type = type.strip.capitalize.constantize
      known_substances.push(type.find(id)) if type.find(id)
    end
    new_substances, known_substances = check_if_new_substances_are_known new_substances, known_substances
    #no substance
    if (new_substances.size + known_substances.size) == 0
      nil
    #one substance
    elsif (new_substances.size + known_substances.size) == 1
      if !known_substances.empty?
        known_substances.first
      else
        c = Compound.new(:name => new_substances.first)
          if  c.save
            c
          else
            nil
          end
      end
    #FIXME: update code when mixture table is created
    else
      nil
    end
  end

  def no_comma_for_decimal
    check_string = ''
    if self.controller_name.downcase == 'studied_factors'
      check_string.concat(params[:studied_factor][:start_value].to_s + params[:studied_factor][:end_value].to_s + params[:studied_factor][:standard_deviation].to_s)
    elsif self.controller_name.downcase == 'experimental_conditions'
      check_string.concat(params[:experimental_condition][:start_value].to_s + params[:experimental_condition][:end_value].to_s)
    end

    if check_string.match(',')
         render :update do |page|
           page.alert('Please use point instead of comma for decimal number')
         end
      return false
    else
      return true
    end
  end
  private

  def project_membership_required
    unless try_block {current_user.person.member? or User.admin_logged_in?}
      flash[:error] = "Only members of known projects, institutions or work groups are allowed to create new content."
      respond_to do |format|
        format.html do
          if eval("#{controller_name.camelcase}Controller.new").respond_to?("index")
            redirect_to polymorphic_path(controller_name)
          else
            redirect_to root_url
          end
{'fraction_non_alphanumeric': 0.08588246646723921, 'fraction_numerical': 0.003859886133359066, 'mean_word_length': 3.5817860300618922, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '2260134', 'n_tokens_mistral': 3172, 'n_tokens_neox': 3072, 'n_words': 780}