orbit-basic/app/controllers/application_controller.rb

107 lines
2.8 KiB
Ruby
Raw Normal View History

2009-05-07 16:53:18 +00:00
class ApplicationController < ActionController::Base
protect_from_forgery
2010-03-08 08:04:05 +00:00
helper :all
before_filter :set_locale, :set_site
# Find the parent for the given item
def find_parent_item
@parent_item = Item.first(:conditions => { :id => BSON::ObjectId(params[:parent_id]) }) rescue nil
end
2009-06-19 09:31:10 +00:00
# Clean the link back
# Remove the locale but keep all the other params
def get_go_back
begin
if env['REQUEST_URI'].include?('locale=')
session[:last_page]
else
target = env['HTTP_REFERER'].split('?')
vars = target[1].split('&')
vars.delete_if {|var| var.include? 'locale=' }
if vars.size > 0
target[0].to_s + '?' + vars.join('&')
else
target[0].to_s
end
end
rescue
nil
end
end
# Check if the current_user is admin
def is_admin?
redirect_to root_url unless current_user.admin?
end
2011-02-22 08:00:13 +00:00
# Parse and render the pages with Radius
def render_page
if @page
@layout = @page.layout
2011-02-22 08:00:13 +00:00
@page_content = @page.content
@layout_content = (@page.layout)? @layout.content : "<r:content />"
context = Radius::Context.new do |c|
c.define_tag 'content' do |tag|
@page_content
end
c.define_tag 'snippet' do |tag|
snippet = Snippet.first(:conditions => {:name => tag.attr['name']})
if snippet
snippet.content
else
t('nothing')
end
end
c.define_tag 'language_bar' do
@site_in_use_locales.map{ |locale|
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
if I18n.locale.to_s.eql?(locale)
lang
else
"<a href='?locale=#{locale}'>#{lang}</a>"
end
}.join(' | ')
end
end
parser = Radius::Parser.new(context, :tag_prefix => 'r')
render :text => parser.parse(@layout_content)
else
render :text => '404 Not Found'
end
end
# Set I18n.locale
2009-06-19 09:31:10 +00:00
def set_locale
# update session if passed
session[:locale] = params[:locale] if params[:locale]
2009-06-19 09:31:10 +00:00
# set locale based on session or default
begin
# check if locale is valid for non site pages
if LIST[:forbidden_item_names].include?(env['PATH_INFO'].split('/')[1].to_s) && !VALID_LOCALES.include?(session[:locale])
I18n.locale = I18n.default_locale
else
I18n.locale = session[:locale]
end
rescue
I18n.locale = I18n.default_locale
end
2010-01-11 09:09:50 +00:00
end
2010-03-08 08:04:05 +00:00
# Set the site variables
def set_site
# set site if exist or create site
site = Site.first || Site.create({:valid_locales => [], :in_use_locales => []})
session[:site] = site.id
@site_in_use_locales = site.in_use_locales
@site_valid_locales = site.valid_locales
end
2009-05-07 16:53:18 +00:00
end