qi6_app/app/controllers/application_controller.rb
2019-05-17 12:30:45 +02:00

166 lines
2.9 KiB
Ruby

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_action :set_locale
before_action :popups
before_action :debug_translation
before_action :get_sorting
def get_sorting
sorting_direction = %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
sorting_column = params[:column] ? params[:column] : nil
if sorting_direction and sorting_column
@sorting = [sorting_column, sorting_direction]
end
end
def sort_by_sorting(var)
if @sorting
klass = var.klass.to_s
if eval(klass).valid_sort.include?(@sorting[0])
if @sorting[0].split(",").size > 0
order_text = ""
i = 0
@sorting[0].split(",").each do |s|
order_text += "#{s} #{@sorting[1]}"
order_text += "," if i < (@sorting[0].split(",").size - 1 )
i+=1
end
var = var.order(order_text)
else
var = var.order("#{@sorting[0]} #{@sorting[1]}")
end
end
end
return var
end
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, :sort_by_sorting
end