mailr/app/controllers/application_controller.rb

100 lines
2.8 KiB
Ruby
Raw Normal View History

2011-07-21 20:20:15 +02:00
require 'yaml'
2011-06-24 23:48:08 +02:00
2011-07-21 20:20:15 +02:00
class ApplicationController < ActionController::Base
2011-06-24 23:48:08 +02:00
2011-07-21 20:20:15 +02:00
protect_from_forgery
2011-09-03 13:07:40 +02:00
2011-08-16 20:05:58 +02:00
before_filter :load_defaults,:current_user,:set_locale
before_filter :plugins_configuration
2011-09-03 13:07:40 +02:00
def load_defaults
$defaults ||= YAML::load(File.open(Rails.root.join('config','defaults.yml')))
end
2011-08-02 23:12:17 +02:00
################################# protected section ###########################################
2011-07-21 20:20:15 +02:00
protected
2009-09-01 14:23:05 +02:00
2011-07-21 20:20:15 +02:00
def theme_resolver
2011-07-23 21:55:26 +02:00
if @current_user.nil?
2011-07-24 15:56:47 +02:00
$defaults['theme']
2011-07-23 21:55:26 +02:00
else
2011-07-24 22:22:13 +02:00
@current_user.prefs.theme || $defaults['theme']
2011-07-23 21:55:26 +02:00
end
2011-07-21 20:20:15 +02:00
end
2009-09-01 14:23:05 +02:00
2011-07-21 20:20:15 +02:00
def set_locale
2011-07-23 21:55:26 +02:00
if @current_user.nil?
2011-07-24 15:56:47 +02:00
I18n.locale = $defaults['locale'] || I18n.default_locale
2011-07-23 21:55:26 +02:00
else
2011-08-16 20:05:58 +02:00
I18n.locale = @current_user.prefs.locale.to_sym || I18n.default_locale
2011-07-23 21:55:26 +02:00
end
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def check_current_user
if @current_user.nil?
session["return_to"] = request.fullpath
2011-08-24 19:20:13 +02:00
redirect_to :controller => 'user', :action => 'login'
2011-07-23 21:55:26 +02:00
return false
end
2011-07-21 20:20:15 +02:00
end
2011-07-27 20:34:30 +02:00
def selected_folder
2011-09-03 13:07:40 +02:00
if session[:selected_folder]
@selected_folder = session[:selected_folder]
else
folder = @current_user.folders.inbox.first
if not folder.nil?
@selected_folder = folder.full_name
end
end
2011-07-27 20:34:30 +02:00
end
2011-07-31 22:45:29 +02:00
def get_current_folders
@folders_shown = @current_user.folders.shown.order("name asc")
2011-09-03 13:07:40 +02:00
if not @selected_folder.nil?
@current_folder = @current_user.folders.find_by_full_name(@selected_folder)
end
2011-07-31 22:45:29 +02:00
end
2011-09-05 19:08:22 +02:00
def prepare_compose_buttons
@buttons = []
2011-09-09 22:10:25 +02:00
@buttons << {:text => 'send',:scope=>:compose,:image => 'email.png'}
@buttons << {:text => 'save_as_draft',:scope=>:compose,:image => 'save.png'}
2011-09-05 19:08:22 +02:00
end
2011-09-09 22:10:25 +02:00
def create_message_with_params
@message = Message.new
if params[:message]
@message.update_attributes(params[:message])
end
files = Dir.glob(File.join($defaults["msg_upload_dir"],@current_user.username + "*"))
@attachments = []
files.each do |f|
@attachments << {:name => File.basename(f).gsub!(/#{@current_user.username}_/,"") , :size => File.stat(f).size }
end
end
2011-09-05 19:08:22 +02:00
def get_system_folders
@drafts_folder = @current_user.folders.drafts.first
@sent_folder = @current_user.folders.sent.first
@inbox_folder = @current_user.folders.inbox.first
@trash_folder = @current_user.folders.trash.first
end
2011-08-02 23:12:17 +02:00
##################################### private section ##########################################
2011-08-16 20:05:58 +02:00
private
def plugins_configuration
2011-09-09 22:10:25 +02:00
WillPaginate::ViewHelpers.pagination_options[:previous_label] = t(:previous_page,:scope=>:common)
WillPaginate::ViewHelpers.pagination_options[:next_label] = t(:next_page,:scope=>:common)
2011-08-16 20:05:58 +02:00
end
2011-08-02 23:12:17 +02:00
end
2011-08-16 20:05:58 +02:00