To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / my_controller.rb @ 422:19549b0c417a

History | View | Annotate | Download (7.22 KB)

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
             'activitymyprojects' => :label_activity_my_recent,
28
             'news' => :label_news_latest,
29
             'tipoftheday' => :label_tipoftheday,
30
             'calendar' => :label_calendar,
31
             'documents' => :label_document_plural,
32
             'timelog' => :label_spent_time
33
           }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
34

    
35
  DEFAULT_LAYOUT = {  'left' => ['tipoftheday', 'activitymyprojects'], 
36
                      'right' => ['issueswatched','calendar'] 
37
                   }.freeze
38

    
39
  verify :xhr => true,
40
         :only => [:add_block, :remove_block, :order_blocks]
41

    
42
  def index
43
    page
44
    render :action => 'page'
45
  end
46

    
47
  # Show user's page
48
  def page
49
    @user = User.current
50
    @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
51
  end
52

    
53
  # Edit user's account
54
  def account
55
    @user = User.current
56
    @pref = @user.pref
57
    @ssamr_user_details = @user.ssamr_user_detail
58
    
59
    
60
    if @user.ssamr_user_detail == nil
61
       @selected_institution_id = nil
62
     else
63
       @selected_institution_id = @ssamr_user_details.institution_id.to_i
64
     end    
65
    
66
    if request.post?
67
      @user.attributes = params[:user]
68
      @user.mail_notification = params[:notification_option] || 'only_my_events'
69
      @user.pref.attributes = params[:pref]
70
      @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
71

    
72
      if @user.ssamr_user_detail == nil
73
        @ssamr_user_details = SsamrUserDetail.new()
74
        @user.ssamr_user_detail = @ssamr_user_details
75
      else
76
        @ssamr_user_details = @user.ssamr_user_detail
77
      end
78

    
79
      if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty?
80
        @ssamr_user_details.description = @user.ssamr_user_detail.description
81
        @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id
82
        @institution_type = @ssamr_user_details.institution_type
83
        @other_institution = @ssamr_user_details.other_institution
84
      else
85
        @ssamr_user_details.description = params[:ssamr_user_details][:description]
86
        @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id]
87
        @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type]
88
        @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution]
89
      end
90
                  
91
      if @user.save
92
        @user.pref.save
93
        @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
94
        set_language_if_valid @user.language
95
        flash[:notice] = l(:notice_account_updated)
96
        redirect_to :action => 'account'
97
        return
98
      end
99
    end
100
    @notification_options = @user.valid_notification_options
101
    @notification_option = @user.mail_notification #? ? 'all' : (@user.notified_projects_ids.empty? ? 'none' : 'selected')    
102
  end
103

    
104
  # Manage user's password
105
  def password
106
    @user = User.current
107
    unless @user.change_password_allowed?
108
      flash[:error] = l(:notice_can_t_change_password)
109
      redirect_to :action => 'account'
110
      return
111
    end
112
    if request.post?
113
      if @user.check_password?(params[:password])
114
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
115
        if @user.save
116
          flash[:notice] = l(:notice_account_password_updated)
117
          redirect_to :action => 'account'
118
        end
119
      else
120
        flash[:error] = l(:notice_account_wrong_password)
121
      end
122
    end
123
  end
124
  
125
  # Create a new feeds key
126
  def reset_rss_key
127
    if request.post?
128
      if User.current.rss_token
129
        User.current.rss_token.destroy
130
        User.current.reload
131
      end
132
      User.current.rss_key
133
      flash[:notice] = l(:notice_feeds_access_key_reseted)
134
    end
135
    redirect_to :action => 'account'
136
  end
137

    
138
  # Create a new API key
139
  def reset_api_key
140
    if request.post?
141
      if User.current.api_token
142
        User.current.api_token.destroy
143
        User.current.reload
144
      end
145
      User.current.api_key
146
      flash[:notice] = l(:notice_api_access_key_reseted)
147
    end
148
    redirect_to :action => 'account'
149
  end
150

    
151
  # User's page layout configuration
152
  def page_layout
153
    @user = User.current
154
    @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
155
    @block_options = []
156
    BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]}
157
  end
158
  
159
  # Add a block to user's page
160
  # The block is added on top of the page
161
  # params[:block] : id of the block to add
162
  def add_block
163
    block = params[:block].to_s.underscore
164
    (render :nothing => true; return) unless block && (BLOCKS.keys.include? block)
165
    @user = User.current
166
    layout = @user.pref[:my_page_layout] || {}
167
    # remove if already present in a group
168
    %w(top left right).each {|f| (layout[f] ||= []).delete block }
169
    # add it on top
170
    layout['top'].unshift block
171
    @user.pref[:my_page_layout] = layout
172
    @user.pref.save 
173
    render :partial => "block", :locals => {:user => @user, :block_name => block}
174
  end
175
  
176
  # Remove a block to user's page
177
  # params[:block] : id of the block to remove
178
  def remove_block
179
    block = params[:block].to_s.underscore
180
    @user = User.current
181
    # remove block in all groups
182
    layout = @user.pref[:my_page_layout] || {}
183
    %w(top left right).each {|f| (layout[f] ||= []).delete block }
184
    @user.pref[:my_page_layout] = layout
185
    @user.pref.save 
186
    render :nothing => true
187
  end
188

    
189
  # Change blocks order on user's page
190
  # params[:group] : group to order (top, left or right)
191
  # params[:list-(top|left|right)] : array of block ids of the group
192
  def order_blocks
193
    group = params[:group]
194
    @user = User.current
195
    if group.is_a?(String)
196
      group_items = (params["list-#{group}"] || []).collect(&:underscore)
197
      if group_items and group_items.is_a? Array
198
        layout = @user.pref[:my_page_layout] || {}
199
        # remove group blocks if they are presents in other groups
200
        %w(top left right).each {|f|
201
          layout[f] = (layout[f] || []) - group_items
202
        }
203
        layout[group] = group_items
204
        @user.pref[:my_page_layout] = layout
205
        @user.pref.save 
206
      end
207
    end
208
    render :nothing => true
209
  end
210
end