class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  
  

  
  before_filter :set_locale


  
  before_filter :popups
  
  before_filter :debug_translation
  
  
  def debug_translation
    if params[:debug_translation]
      if current_admin
       @debug_translation = true 
      end
    end
  end
  def popups

   
  end
  
  def get_public_layout
    "public"
   
      
  end
  

  def set_locale
    # if params[:locale] is nil then I18n.default_locale will be used
    #I18n.locale = params[:locale]#"fr" #
    
    I18n.locale = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug I18n.locale.to_s
  end
  
  def default_url_options(options={})
    { :lang => I18n.locale }  #
  end
  

  
  
  
  
  private
  
  
    def auth_admin
      if !current_admin
        redirect_to new_admin_admin_auth_path
      end
      
    
    end
  



    def current_admin
      
      if cookies[:admin_remember_token]
        if @current_admin = Admin.find_by_remember_token(cookies[:admin_remember_token]) 
          @current_admin = Admin.find_by_remember_token(cookies[:admin_remember_token]) 
        else
            cookies[:admin_remember_token] =nil
        end
        
      end
      
      
     
    end
    
   

    def get_specific_pref(key="")
      
      sp = SpecificPreference.where(:key => key).first
      
      if sp
        return sp.value
      else
        return ""
      end
      
    end
    
    def has_permission?(permission)
     if current_admin and current_admin.has_permission?(permission)  
       true
     else
       redirect_to "/admin"
       
     end
    end
    
    def is_super_admin
      if current_admin and current_admin.super_admin
        true
      else
        redirect_to "/admin"
      end
    end
    
    
    helper_method :current_admin, :get_specific_pref
  
  
end

