Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: class MyController < ApplicationController Chris@909: before_filter :require_login Chris@909: Chris@909: helper :issues Chris@909: helper :users Chris@909: helper :custom_fields Chris@909: Chris@909: BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues, Chris@909: 'issuesreportedbyme' => :label_reported_issues, Chris@909: 'issueswatched' => :label_watched_issues, Chris@909: 'news' => :label_news_latest, Chris@909: 'calendar' => :label_calendar, Chris@909: 'documents' => :label_document_plural, Chris@909: 'timelog' => :label_spent_time Chris@909: }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze Chris@909: Chris@909: DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'], Chris@909: 'right' => ['issuesreportedbyme'] Chris@909: }.freeze Chris@909: Chris@909: verify :xhr => true, Chris@909: :only => [:add_block, :remove_block, :order_blocks] Chris@909: Chris@909: def index Chris@909: page Chris@909: render :action => 'page' Chris@909: end Chris@909: Chris@909: # Show user's page Chris@909: def page Chris@909: @user = User.current Chris@909: @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT Chris@909: end Chris@909: Chris@909: # Edit user's account Chris@909: def account Chris@909: @user = User.current Chris@909: @pref = @user.pref Chris@909: if request.post? Chris@909: @user.safe_attributes = params[:user] Chris@909: @user.pref.attributes = params[:pref] Chris@909: @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') Chris@909: if @user.save Chris@909: @user.pref.save Chris@909: @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) Chris@909: set_language_if_valid @user.language Chris@909: flash[:notice] = l(:notice_account_updated) Chris@909: redirect_to :action => 'account' Chris@909: return Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Manage user's password Chris@909: def password Chris@909: @user = User.current Chris@909: unless @user.change_password_allowed? Chris@909: flash[:error] = l(:notice_can_t_change_password) Chris@909: redirect_to :action => 'account' Chris@909: return Chris@909: end Chris@909: if request.post? Chris@909: if @user.check_password?(params[:password]) Chris@909: @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] Chris@909: if @user.save Chris@909: flash[:notice] = l(:notice_account_password_updated) Chris@909: redirect_to :action => 'account' Chris@909: end Chris@909: else Chris@909: flash[:error] = l(:notice_account_wrong_password) Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Create a new feeds key Chris@909: def reset_rss_key Chris@909: if request.post? Chris@909: if User.current.rss_token Chris@909: User.current.rss_token.destroy Chris@909: User.current.reload Chris@909: end Chris@909: User.current.rss_key Chris@909: flash[:notice] = l(:notice_feeds_access_key_reseted) Chris@909: end Chris@909: redirect_to :action => 'account' Chris@909: end Chris@909: Chris@909: # Create a new API key Chris@909: def reset_api_key Chris@909: if request.post? Chris@909: if User.current.api_token Chris@909: User.current.api_token.destroy Chris@909: User.current.reload Chris@909: end Chris@909: User.current.api_key Chris@909: flash[:notice] = l(:notice_api_access_key_reseted) Chris@909: end Chris@909: redirect_to :action => 'account' Chris@909: end Chris@909: Chris@909: # User's page layout configuration Chris@909: def page_layout Chris@909: @user = User.current Chris@909: @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup Chris@909: @block_options = [] Chris@909: BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]} Chris@909: end Chris@909: Chris@909: # Add a block to user's page Chris@909: # The block is added on top of the page Chris@909: # params[:block] : id of the block to add Chris@909: def add_block Chris@909: block = params[:block].to_s.underscore Chris@909: (render :nothing => true; return) unless block && (BLOCKS.keys.include? block) Chris@909: @user = User.current Chris@909: layout = @user.pref[:my_page_layout] || {} Chris@909: # remove if already present in a group Chris@909: %w(top left right).each {|f| (layout[f] ||= []).delete block } Chris@909: # add it on top Chris@909: layout['top'].unshift block Chris@909: @user.pref[:my_page_layout] = layout Chris@909: @user.pref.save Chris@909: render :partial => "block", :locals => {:user => @user, :block_name => block} Chris@909: end Chris@909: Chris@909: # Remove a block to user's page Chris@909: # params[:block] : id of the block to remove Chris@909: def remove_block Chris@909: block = params[:block].to_s.underscore Chris@909: @user = User.current Chris@909: # remove block in all groups Chris@909: layout = @user.pref[:my_page_layout] || {} Chris@909: %w(top left right).each {|f| (layout[f] ||= []).delete block } Chris@909: @user.pref[:my_page_layout] = layout Chris@909: @user.pref.save Chris@909: render :nothing => true Chris@909: end Chris@909: Chris@909: # Change blocks order on user's page Chris@909: # params[:group] : group to order (top, left or right) Chris@909: # params[:list-(top|left|right)] : array of block ids of the group Chris@909: def order_blocks Chris@909: group = params[:group] Chris@909: @user = User.current Chris@909: if group.is_a?(String) Chris@909: group_items = (params["list-#{group}"] || []).collect(&:underscore) Chris@909: if group_items and group_items.is_a? Array Chris@909: layout = @user.pref[:my_page_layout] || {} Chris@909: # remove group blocks if they are presents in other groups Chris@909: %w(top left right).each {|f| Chris@909: layout[f] = (layout[f] || []) - group_items Chris@909: } Chris@909: layout[group] = group_items Chris@909: @user.pref[:my_page_layout] = layout Chris@909: @user.pref.save Chris@909: end Chris@909: end Chris@909: render :nothing => true Chris@909: end Chris@909: end