Commit 351b329c authored by jplang's avatar jplang

Moves blocks definition to Redmine::MyPage.

git-svn-id: https://svn.redmine.org/redmine/trunk@15930 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 13e04f5b
......@@ -27,19 +27,6 @@ class MyController < ApplicationController
helper :users
helper :custom_fields
BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
'issuesreportedbyme' => :label_reported_issues,
'issueswatched' => :label_watched_issues,
'news' => :label_news_latest,
'calendar' => :label_calendar,
'documents' => :label_document_plural,
'timelog' => :label_spent_time
}.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
'right' => ['issuesreportedbyme']
}.freeze
def index
page
render :action => 'page'
......@@ -48,7 +35,7 @@ class MyController < ApplicationController
# Show user's page
def page
@user = User.current
@blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
@blocks = @user.pref.my_page_layout
end
# Edit user's account
......@@ -146,13 +133,7 @@ class MyController < ApplicationController
# User's page layout configuration
def page_layout
@user = User.current
@blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
@block_options = []
BLOCKS.each do |k, v|
unless @blocks.values.flatten.include?(k)
@block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
end
end
@blocks = @user.pref.my_page_layout
end
# Add a block to user's page
......@@ -160,14 +141,14 @@ class MyController < ApplicationController
# params[:block] : id of the block to add
def add_block
block = params[:block].to_s.underscore
if block.present? && BLOCKS.key?(block)
if block.present? && Redmine::MyPage.blocks.key?(block)
@user = User.current
layout = @user.pref[:my_page_layout] || {}
layout = @user.pref.my_page_layout
# remove if already present in a group
%w(top left right).each {|f| (layout[f] ||= []).delete block }
# add it on top
layout['top'].unshift block
@user.pref[:my_page_layout] = layout
@user.pref.my_page_layout = layout
@user.pref.save
end
redirect_to my_page_layout_path
......@@ -179,9 +160,9 @@ class MyController < ApplicationController
block = params[:block].to_s.underscore
@user = User.current
# remove block in all groups
layout = @user.pref[:my_page_layout] || {}
layout = @user.pref.my_page_layout
%w(top left right).each {|f| (layout[f] ||= []).delete block }
@user.pref[:my_page_layout] = layout
@user.pref.my_page_layout = layout
@user.pref.save
redirect_to my_page_layout_path
end
......@@ -196,13 +177,13 @@ class MyController < ApplicationController
group_items = (params["blocks"] || []).collect(&:underscore)
group_items.each {|s| s.sub!(/^block_/, '')}
if group_items and group_items.is_a? Array
layout = @user.pref[:my_page_layout] || {}
layout = @user.pref.my_page_layout
# remove group blocks if they are presents in other groups
%w(top left right).each {|f|
layout[f] = (layout[f] || []) - group_items
}
layout[group] = group_items
@user.pref[:my_page_layout] = layout
@user.pref.my_page_layout = layout
@user.pref.save
end
end
......
......@@ -40,7 +40,7 @@ module MyHelper
# Renders a single block content
def render_block_content(block, user)
unless MyController::BLOCKS.keys.include?(block)
unless Redmine::MyPage.blocks.key?(block)
Rails.logger.warn("Unknown block \"#{block}\" found in #{user.login} (id=#{user.id}) preferences")
return
end
......@@ -53,6 +53,15 @@ module MyHelper
end
end
def block_select_tag(user)
disabled = user.pref.my_page_layout.values.flatten
options = content_tag('option')
Redmine::MyPage.block_options.each do |label, block|
options << content_tag('option', label, :value => block, :disabled => disabled.include?(block))
end
content_tag('select', options, :id => "block-select")
end
def calendar_items(startdt, enddt)
Issue.visible.
where(:project_id => User.current.projects.map(&:id)).
......
......@@ -82,4 +82,12 @@ class UserPreference < ActiveRecord::Base
def textarea_font; self[:textarea_font] end
def textarea_font=(value); self[:textarea_font]=value; end
def my_page_layout
self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
end
def my_page_layout=(arg)
self[:my_page_layout] = arg
end
end
<div class="contextual">
<% if @block_options.present? %>
<%= form_tag({:action => "add_block"}, :id => "block-form") do %>
<%= form_tag({:action => "add_block"}, :id => "block-form") do %>
<%= label_tag('block-select', l(:label_my_page_block)) %>:
<%= select_tag 'block',
content_tag('option') + options_for_select(@block_options),
:id => "block-select" %>
<%= block_select_tag(@user) %>
<%= link_to l(:button_add), '#', :onclick => '$("#block-form").submit()', :class => 'icon icon-add' %>
<% end %>
<% end %>
<%= link_to l(:button_back), {:action => 'page'}, :class => 'icon icon-cancel' %>
</div>
......
......@@ -16,17 +16,47 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module Redmine
module Views
module MyPage
module Block
def self.additional_blocks
@@additional_blocks ||= Dir.glob("#{Redmine::Plugin.directory}/*/app/views/my/blocks/_*.{rhtml,erb}").inject({}) do |h,file|
name = File.basename(file).split('.').first.gsub(/^_/, '')
h[name] = name.to_sym
h
end
end
module MyPage
include Redmine::I18n
CORE_BLOCKS = {
'issuesassignedtome' => :label_assigned_to_me_issues,
'issuesreportedbyme' => :label_reported_issues,
'issueswatched' => :label_watched_issues,
'news' => :label_news_latest,
'calendar' => :label_calendar,
'documents' => :label_document_plural,
'timelog' => :label_spent_time
}
# Returns the available blocks
def self.blocks
CORE_BLOCKS.merge(additional_blocks).freeze
end
def self.block_options
options = []
blocks.each do |k, v|
options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
end
options
end
# Returns the additional blocks that are defined by plugin partials
def self.additional_blocks
@@additional_blocks ||= Dir.glob("#{Redmine::Plugin.directory}/*/app/views/my/blocks/_*.{rhtml,erb}").inject({}) do |h,file|
name = File.basename(file).split('.').first.gsub(/^_/, '')
h[name] = name.to_sym
h
end
end
# Returns the default layout for My Page
def self.default_layout
{
'left' => ['issuesassignedtome'],
'right' => ['issuesreportedbyme']
}
end
end
end
......@@ -52,7 +52,7 @@ class MyControllerTest < Redmine::ControllerTest
end
def test_page_with_all_blocks
blocks = MyController::BLOCKS.keys
blocks = Redmine::MyPage.blocks.keys
preferences = User.find(2).pref
preferences[:my_page_layout] = {'top' => blocks}
preferences.save!
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment