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 @ 442:753f1380d6bc

History | View | Annotate | Download (6.84 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 :users
23
  helper :custom_fields
24

    
25
  BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
26
             'issuesreportedbyme' => :label_reported_issues,
27
             'issueswatched' => :label_watched_issues,
28
             'news' => :label_news_latest,
29
             'calendar' => :label_calendar,
30
             'documents' => :label_document_plural,
31
             'timelog' => :label_spent_time
32
           }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
33

    
34
  DEFAULT_LAYOUT = {  'left' => ['issuesassignedtome'], 
35
                      'right' => ['issuesreportedbyme'] 
36
                   }.freeze
37

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

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

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

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

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

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

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

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

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

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