Mercurial > hg > soundsoftware-site
comparison app/controllers/.svn/text-base/my_controller.rb.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | 94944d00e43c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2009 Jean-Philippe Lang | |
3 # | |
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. | |
17 | |
18 class MyController < ApplicationController | |
19 before_filter :require_login | |
20 | |
21 helper :issues | |
22 helper :custom_fields | |
23 | |
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 'timelog' => :label_spent_time | |
31 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze | |
32 | |
33 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'], | |
34 'right' => ['issuesreportedbyme'] | |
35 }.freeze | |
36 | |
37 verify :xhr => true, | |
38 :only => [:add_block, :remove_block, :order_blocks] | |
39 | |
40 def index | |
41 page | |
42 render :action => 'page' | |
43 end | |
44 | |
45 # Show user's page | |
46 def page | |
47 @user = User.current | |
48 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT | |
49 end | |
50 | |
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 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
60 if @user.save | |
61 @user.pref.save | |
62 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) | |
63 set_language_if_valid @user.language | |
64 flash[:notice] = l(:notice_account_updated) | |
65 redirect_to :action => 'account' | |
66 return | |
67 end | |
68 end | |
69 @notification_options = [[l(:label_user_mail_option_all), 'all'], | |
70 [l(:label_user_mail_option_none), 'none']] | |
71 # Only users that belong to more than 1 project can select projects for which they are notified | |
72 # Note that @user.membership.size would fail since AR ignores :include association option when doing a count | |
73 @notification_options.insert 1, [l(:label_user_mail_option_selected), 'selected'] if @user.memberships.length > 1 | |
74 @notification_option = @user.mail_notification? ? 'all' : (@user.notified_projects_ids.empty? ? 'none' : 'selected') | |
75 end | |
76 | |
77 # Manage user's password | |
78 def password | |
79 @user = User.current | |
80 unless @user.change_password_allowed? | |
81 flash[:error] = l(:notice_can_t_change_password) | |
82 redirect_to :action => 'account' | |
83 return | |
84 end | |
85 if request.post? | |
86 if @user.check_password?(params[:password]) | |
87 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | |
88 if @user.save | |
89 flash[:notice] = l(:notice_account_password_updated) | |
90 redirect_to :action => 'account' | |
91 end | |
92 else | |
93 flash[:error] = l(:notice_account_wrong_password) | |
94 end | |
95 end | |
96 end | |
97 | |
98 # Create a new feeds key | |
99 def reset_rss_key | |
100 if request.post? | |
101 if User.current.rss_token | |
102 User.current.rss_token.destroy | |
103 User.current.reload | |
104 end | |
105 User.current.rss_key | |
106 flash[:notice] = l(:notice_feeds_access_key_reseted) | |
107 end | |
108 redirect_to :action => 'account' | |
109 end | |
110 | |
111 # Create a new API key | |
112 def reset_api_key | |
113 if request.post? | |
114 if User.current.api_token | |
115 User.current.api_token.destroy | |
116 User.current.reload | |
117 end | |
118 User.current.api_key | |
119 flash[:notice] = l(:notice_api_access_key_reseted) | |
120 end | |
121 redirect_to :action => 'account' | |
122 end | |
123 | |
124 # User's page layout configuration | |
125 def page_layout | |
126 @user = User.current | |
127 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup | |
128 @block_options = [] | |
129 BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]} | |
130 end | |
131 | |
132 # Add a block to user's page | |
133 # The block is added on top of the page | |
134 # params[:block] : id of the block to add | |
135 def add_block | |
136 block = params[:block].to_s.underscore | |
137 (render :nothing => true; return) unless block && (BLOCKS.keys.include? block) | |
138 @user = User.current | |
139 layout = @user.pref[:my_page_layout] || {} | |
140 # remove if already present in a group | |
141 %w(top left right).each {|f| (layout[f] ||= []).delete block } | |
142 # add it on top | |
143 layout['top'].unshift block | |
144 @user.pref[:my_page_layout] = layout | |
145 @user.pref.save | |
146 render :partial => "block", :locals => {:user => @user, :block_name => block} | |
147 end | |
148 | |
149 # Remove a block to user's page | |
150 # params[:block] : id of the block to remove | |
151 def remove_block | |
152 block = params[:block].to_s.underscore | |
153 @user = User.current | |
154 # remove block in all groups | |
155 layout = @user.pref[:my_page_layout] || {} | |
156 %w(top left right).each {|f| (layout[f] ||= []).delete block } | |
157 @user.pref[:my_page_layout] = layout | |
158 @user.pref.save | |
159 render :nothing => true | |
160 end | |
161 | |
162 # Change blocks order on user's page | |
163 # params[:group] : group to order (top, left or right) | |
164 # params[:list-(top|left|right)] : array of block ids of the group | |
165 def order_blocks | |
166 group = params[:group] | |
167 @user = User.current | |
168 if group.is_a?(String) | |
169 group_items = (params["list-#{group}"] || []).collect(&:underscore) | |
170 if group_items and group_items.is_a? Array | |
171 layout = @user.pref[:my_page_layout] || {} | |
172 # remove group blocks if they are presents in other groups | |
173 %w(top left right).each {|f| | |
174 layout[f] = (layout[f] || []) - group_items | |
175 } | |
176 layout[group] = group_items | |
177 @user.pref[:my_page_layout] = layout | |
178 @user.pref.save | |
179 end | |
180 end | |
181 render :nothing => true | |
182 end | |
183 end |