More flexible mail notifications settings at user level. A user has now 3 options:
[gitredmine.git] / app / controllers / my_controller.rb
blob5a1b128f9d3ede045e773ae9ae65034e0c996eba
1 # redMine - project management software
2 # Copyright (C) 2006  Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 class MyController < ApplicationController
19   helper :issues
20   
21   layout 'base'
22   before_filter :require_login
24   BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
25              'issuesreportedbyme' => :label_reported_issues,
26              'issueswatched' => :label_watched_issues,
27              'news' => :label_news_latest,
28              'calendar' => :label_calendar,
29              'documents' => :label_document_plural
30            }.freeze
32   DEFAULT_LAYOUT = {  'left' => ['issuesassignedtome'], 
33                       'right' => ['issuesreportedbyme'] 
34                    }.freeze
36   verify :xhr => true,
37          :session => :page_layout,
38          :only => [:add_block, :remove_block, :order_blocks]
40   def index
41     page
42     render :action => 'page'
43   end
45   # Show user's page
46   def page
47     @user = self.logged_in_user
48     @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
49   end
51   # Edit user's account
52   def account
53     @user = User.current
54     @pref = @user.pref
55     if request.post?
56       @user.attributes = params[:user]
57       @user.mail_notification = (params[:notification_option] == 'all')
58       @user.pref.attributes = params[:pref]
59       if @user.save
60         @user.pref.save
61         @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
62         set_language_if_valid @user.language
63         flash[:notice] = l(:notice_account_updated)
64         redirect_to :action => 'account'
65         return
66       end
67     end
68     @notification_options = [[l(:label_user_mail_option_all), 'all'],
69                              [l(:label_user_mail_option_none), 'none']]
70     # Only users that belong to more than 1 project can select projects for which they are notified
71     # Note that @user.membership.size would fail since AR ignores :include association option when doing a count
72     @notification_options.insert 1, [l(:label_user_mail_option_selected), 'selected'] if @user.memberships.length > 1
73     @notification_option = @user.mail_notification? ? 'all' : (@user.notified_projects_ids.empty? ? 'none' : 'selected')    
74   end
76   # Manage user's password
77   def password
78     @user = self.logged_in_user
79     flash[:error] = l(:notice_can_t_change_password) and redirect_to :action => 'account' and return if @user.auth_source_id
80     if request.post?
81       if @user.check_password?(params[:password])
82         @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
83         if @user.save
84           flash[:notice] = l(:notice_account_password_updated)
85           redirect_to :action => 'account'
86         end
87       else
88         flash[:error] = l(:notice_account_wrong_password)
89       end
90     end
91   end
92   
93   # Create a new feeds key
94   def reset_rss_key
95     if request.post? && User.current.rss_token
96       User.current.rss_token.destroy
97       flash[:notice] = l(:notice_feeds_access_key_reseted)
98     end
99     redirect_to :action => 'account'
100   end
102   # User's page layout configuration
103   def page_layout
104     @user = self.logged_in_user
105     @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
106     session[:page_layout] = @blocks
107     %w(top left right).each {|f| session[:page_layout][f] ||= [] }
108     @block_options = []
109     BLOCKS.each {|k, v| @block_options << [l(v), k]}
110   end
111   
112   # Add a block to user's page
113   # The block is added on top of the page
114   # params[:block] : id of the block to add
115   def add_block
116     block = params[:block]
117     render(:nothing => true) and return unless block && (BLOCKS.keys.include? block)
118     @user = self.logged_in_user
119     # remove if already present in a group
120     %w(top left right).each {|f| (session[:page_layout][f] ||= []).delete block }
121     # add it on top
122     session[:page_layout]['top'].unshift block
123     render :partial => "block", :locals => {:user => @user, :block_name => block}
124   end
125   
126   # Remove a block to user's page
127   # params[:block] : id of the block to remove
128   def remove_block
129     block = params[:block]
130     # remove block in all groups
131     %w(top left right).each {|f| (session[:page_layout][f] ||= []).delete block }
132     render :nothing => true
133   end
135   # Change blocks order on user's page
136   # params[:group] : group to order (top, left or right)
137   # params[:list-(top|left|right)] : array of block ids of the group
138   def order_blocks
139     group = params[:group]
140     group_items = params["list-#{group}"]
141     if group_items and group_items.is_a? Array
142       # remove group blocks if they are presents in other groups
143       %w(top left right).each {|f|
144         session[:page_layout][f] = (session[:page_layout][f] || []) - group_items
145       }
146       session[:page_layout][group] = group_items    
147     end
148     render :nothing => true
149   end
150   
151   # Save user's page layout  
152   def page_layout_save
153     @user = self.logged_in_user
154     @user.pref[:my_page_layout] = session[:page_layout] if session[:page_layout]
155     @user.pref.save
156     session[:page_layout] = nil
157     redirect_to :action => 'page'
158   end